:root {
  --bg: rgba(14, 18, 24, 0.86); --fg: #e8edf2; --muted: #9aa7b4; --accent: #4aa3ff; --line: rgba(255,255,255,0.12);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
html, body { margin: 0; height: 100%; overflow: hidden; background: #0b0e12; color: var(--fg); }
#view { display: block; width: 100vw; height: 100vh; cursor: grab; touch-action: none; }
#view:active { cursor: grabbing; }
button { font: inherit; color: var(--fg); background: #26303b; border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; cursor: pointer; }
button:hover { background: #31404f; }
button.primary { background: #1f5fa8; border-color: #2c74c7; }
button.primary:hover { background: #2a6fbf; }
button.on { background: #7a5a12; }
button.big { padding: 14px 18px; font-size: 16px; line-height: 1.35; min-width: 240px; }
button small { color: #c8d6e6; font-size: 12px; }
select, input[type=range] { vertical-align: middle; }
select { background: #1c242d; color: var(--fg); border: 1px solid var(--line); border-radius: 4px; padding: 3px; }

.overlay { position: fixed; inset: 0; display: none; align-items: center; justify-content: center; background: rgba(4, 7, 10, 0.55); z-index: 20; }
.overlay.show { display: flex; }
#loading { flex-direction: column; gap: 16px; color: var(--muted); background: #0b0e12; }
.spinner { width: 34px; height: 34px; border: 3px solid #2a3542; border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.menu-box { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 22px 26px; max-width: 760px; width: calc(100vw - 64px); box-shadow: 0 20px 60px rgba(0,0,0,0.5); backdrop-filter: blur(6px); }
.menu-box h1 { margin: 2px 0 4px; font-size: 34px; letter-spacing: 0.5px; }
.menu-box h2 { margin: 0 0 10px; }
.kicker { text-transform: uppercase; letter-spacing: 2px; font-size: 12px; color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.start-buttons { display: flex; gap: 14px; margin: 18px 0; flex-wrap: wrap; }
.keys { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 18px; font-size: 13px; color: #cdd6df; border-top: 1px solid var(--line); padding-top: 14px; }
.keys b { color: #fff; font-weight: 600; margin-right: 4px; }
#pause .menu-box { width: auto; text-align: center; }

#controls .menu-box { max-width: 680px; max-height: 86vh; display: flex; flex-direction: column; }
.table-wrap { overflow: auto; flex: 1; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); margin: 10px 0; }
#controls table { width: 100%; border-collapse: collapse; font-size: 13px; }
#controls td { padding: 4px 6px; border-bottom: 1px solid rgba(255,255,255,0.05); }
.bind { min-width: 120px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
.bind.wait { background: #7a5a12; }
.sens { display: flex; gap: 20px; flex-wrap: wrap; font-size: 13px; }
.menu-actions { display: flex; justify-content: space-between; }

#dev { position: fixed; top: 10px; right: 10px; width: 480px; max-height: calc(100vh - 20px); overflow: auto; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; font-size: 12px; display: none; z-index: 10; }
#dev.show { display: block; }
.dev-head { display: flex; justify-content: space-between; margin-bottom: 6px; }
.dev-hint { color: var(--muted); }
.dev-row { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; align-items: center; }
.dev-row label { display: flex; align-items: center; gap: 6px; margin-right: 8px; }
.dev-row button { padding: 4px 8px; font-size: 12px; }
#dev pre { margin: 6px 0 0; font: 11px/1.45 ui-monospace, Menlo, Consolas, monospace; color: #cfe3d4; white-space: pre; overflow-x: auto; }
.dev-sub { margin-top: 8px; color: var(--muted); }
#fps { position: fixed; left: 8px; top: 6px; font: 11px ui-monospace, Menlo, monospace; color: rgba(255,255,255,0.55); display: none; z-index: 5; pointer-events: none; }
#fps.show { display: block; }
#tooltip { position: fixed; pointer-events: none; background: rgba(10,12,15,0.85); border: 1px solid var(--line); padding: 4px 8px; border-radius: 4px; font-size: 12px; display: none; z-index: 30; max-width: 320px; }
#tooltip.show { display: block; }
@media (max-width: 700px) { .keys { grid-template-columns: 1fr; } #dev { width: calc(100vw - 20px); } }

/* ---------- Touch devices & small screens ---------- */
html, body { overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
button { touch-action: manipulation; }
.overlay { overflow: auto; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); box-sizing: border-box; }
.menu-box { max-height: calc(100% - 24px); overflow: auto; box-sizing: border-box; margin: auto; }
.touch-only { display: none; }
body.touch-ui .touch-only { display: grid; }
body.touch-ui .no-touch { display: none; }

#touch { position: fixed; inset: 0; pointer-events: none; z-index: 8; display: none; font-size: 13px; }
#touch.show { display: block; }
#touch button, #touch .t-stick, #touch .t-thr { pointer-events: auto; touch-action: none; }
#touch button { background: rgba(20, 26, 33, 0.62); border: 1px solid rgba(255,255,255,0.22); color: #e9eef3; border-radius: 8px; font-weight: 600; letter-spacing: 0.3px; backdrop-filter: blur(3px); }
#touch button.pressed { background: rgba(70, 120, 180, 0.75); }
#touch button.lit { border-color: #ffb020; color: #ffcf6a; }
.t-bar { position: absolute; top: calc(6px + env(safe-area-inset-top)); left: calc(8px + env(safe-area-inset-left)); right: calc(8px + env(safe-area-inset-right)); display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.t-bar button { min-width: 48px; height: 38px; padding: 0 9px; font-size: 12px; }
.t-read { pointer-events: none; align-self: center; background: rgba(0,0,0,0.5); padding: 4px 7px; border-radius: 6px; font: 600 12px ui-monospace, Menlo, monospace; color: #8ff0b0; min-width: 52px; text-align: center; }
.t-left { position: absolute; left: calc(14px + env(safe-area-inset-left)); bottom: calc(14px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; align-items: center; gap: 8px; }
.t-stick { width: min(34vmin, 170px); height: min(34vmin, 170px); border-radius: 50%; background: radial-gradient(circle, rgba(255,255,255,0.10), rgba(20,26,33,0.55) 70%); border: 1px solid rgba(255,255,255,0.25); position: relative; }
.t-stick::before, .t-stick::after { content: ''; position: absolute; background: rgba(255,255,255,0.15); }
.t-stick::before { left: 50%; top: 10%; bottom: 10%; width: 1px; }
.t-stick::after { top: 50%; left: 10%; right: 10%; height: 1px; }
.t-knob { position: absolute; left: 50%; top: 50%; width: 40%; height: 40%; margin: -20% 0 0 -20%; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #59636e, #22282f); border: 1px solid rgba(255,255,255,0.35); pointer-events: none; }
.t-rudder { display: flex; gap: 8px; }
.t-rudder button { height: 40px; min-width: 70px; }
.t-right { position: absolute; right: calc(14px + env(safe-area-inset-right)); bottom: calc(14px + env(safe-area-inset-bottom)); display: flex; align-items: flex-end; gap: 10px; }
.t-brake { width: 74px; height: 74px; border-radius: 50% !important; }
.t-thr { position: relative; width: 64px; height: min(48vmin, 230px); background: rgba(20,26,33,0.55); border: 1px solid rgba(255,255,255,0.25); border-radius: 10px; }
.t-mark { position: absolute; right: calc(100% + 4px); font-size: 9px; text-shadow: 0 1px 2px #000; color: rgba(255,255,255,0.6); pointer-events: none; }
.t-thumb { position: absolute; left: 6px; right: 6px; height: 10%; min-height: 20px; background: linear-gradient(#5b6670, #2a3138); border: 1px solid rgba(255,255,255,0.45); border-radius: 6px; pointer-events: none; }
.t-thr-read { position: absolute; top: -20px; width: 100%; text-align: center; font: 600 12px ui-monospace, Menlo, monospace; color: #9ad0ff; pointer-events: none; }
body.touch-ui #fps { top: auto; bottom: 4px; left: 50%; transform: translateX(-50%); }
body.touch-ui #dev { top: calc(52px + env(safe-area-inset-top)); z-index: 12; }
@media (max-height: 420px) {
  .t-bar button { height: 32px; min-width: 42px; font-size: 11px; padding: 0 6px; }
  .menu-box { padding: 14px 16px; }
  .menu-box h1 { font-size: 24px; }
  button.big { padding: 9px 12px; min-width: 200px; font-size: 14px; }
  #dev { max-height: calc(100vh - 60px); }
}
@media (orientation: portrait) {
  body.touch-ui:not(.rotate-dismissed) #rotate { display: flex; z-index: 25; }
}

/* ---------- Game menu ---------- */
#menu { z-index: 22; }
#controls { z-index: 24; }
#menu-btn { position: fixed; top: calc(8px + env(safe-area-inset-top)); left: calc(8px + env(safe-area-inset-left)); z-index: 9; background: rgba(20,26,33,0.65); font-size: 12px; font-weight: 600; padding: 6px 10px; }
body.touch-ui #menu-btn { display: none; }
.game-menu { max-width: 880px; }
.menu-list { display: flex; flex-direction: column; gap: 8px; max-width: 340px; margin: 10px 0; }
.menu-list button { padding: 10px 14px; text-align: left; font-size: 15px; }
.start-links { display: flex; gap: 10px; margin-top: 12px; }
.ctl-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 10px 22px; margin: 8px 0 14px; }
.ctl-group h3 { margin: 6px 0 4px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--accent); }
.ctl-group.highlight h3 { color: #ffcf6a; }
.ctl-group table { width: 100%; border-collapse: collapse; font-size: 13px; }
.ctl-group td { padding: 3px 4px; border-bottom: 1px solid rgba(255,255,255,0.06); vertical-align: top; }
.ctl-group td:last-child { text-align: right; color: #cfd8e1; }
kbd { display: inline-block; min-width: 18px; padding: 1px 6px; border: 1px solid rgba(255,255,255,0.3); border-bottom-width: 2px; border-radius: 4px; background: #1a2129; font: 600 12px ui-monospace, Menlo, monospace; text-align: center; margin-left: 3px; }
.settings { display: grid; gap: 10px; margin: 10px 0 16px; max-width: 520px; }
.settings label { display: flex; justify-content: space-between; align-items: center; gap: 12px; font-size: 14px; }

/* ---------- On-screen instrument panel ---------- */
#ipanel { position: fixed; left: 0; right: 0; bottom: 0; z-index: 7; display: none; pointer-events: none;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); background: linear-gradient(to bottom, rgba(32,38,44,0.0), rgba(32,38,44,0.92) 14px, #2a3036 40px); }
#ipanel.show { display: block; }
.ip-units { display: flex; justify-content: center; align-items: flex-end; gap: 10px; }
.ip-unit { display: flex; flex-direction: column; align-items: center; gap: 4px; pointer-events: auto; }
.ip-screen { background: #000; border: 6px solid #15181b; border-radius: 6px; box-shadow: 0 0 0 1px #4a535b, inset 0 0 6px rgba(0,0,0,0.8); overflow: hidden; box-sizing: content-box; }
.ip-screen canvas { width: 100%; height: 100%; display: block; }
.ip-tabs { display: flex; gap: 4px; align-items: center; height: 22px; }
.ip-tabs button { font-size: 11px; padding: 2px 8px; background: #1c2227; border-color: #4a535b; }
.ip-tabs button.sel { background: #3a6ea8; border-color: #6aa3e0; }
.ip-title { font-size: 11px; letter-spacing: 1px; color: #aeb8c2; }
.ip-val { font: 600 12px ui-monospace, Menlo, monospace; color: #8ff0b0; }
.ip-val.amber { color: #ffb020; }
.ip-status { pointer-events: auto; display: flex; flex-direction: column; justify-content: space-between; align-items: stretch; gap: 4px; background: #23292f; border: 1px solid #4a535b; border-radius: 6px; padding: 6px; box-sizing: border-box; min-width: 92px; font-size: 11px; }
.ip-masters { display: flex; gap: 4px; }
.ip-master { flex: 1; font: 700 10px/1.1 Arial, sans-serif; padding: 4px 2px; background: #2a1d1a; color: #5c4038; border: 1px solid #000; border-radius: 4px; }
.ip-master.warn.on { background: #ff2a18; color: #fff; box-shadow: 0 0 10px #ff3020; }
.ip-master.caut.on { background: #ffa214; color: #1a1000; box-shadow: 0 0 10px #ffa214; }
.ip-gear { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.ip-gl { display: flex; gap: 10px; }
.ip-gl i { width: 16px; height: 14px; background: #1b1d20; clip-path: polygon(0 0, 100% 0, 50% 100%); display: block; }
.ip-gl i.dn { background: #2dff6a; }
.ip-gl i.unlk { background: #ff3020; }
#ip-gearlever { font-size: 10px; padding: 3px 4px; width: 100%; margin-top: 2px; }
.ip-lbl { color: #aeb8c2; font-size: 10px; letter-spacing: 0.5px; margin-right: 4px; }
.ip-rows div { display: flex; justify-content: space-between; }
.ip-brk { align-self: center; border-width: 3px; }
body.ipanel-on #fps { bottom: auto; top: 6px; left: auto; right: 8px; transform: none; }
