:root {
  --bg: #0d1117;
  --panel: rgba(14, 19, 27, 0.82);
  --panel-solid: #111823;
  --line: rgba(160, 185, 215, 0.14);
  --line-strong: rgba(160, 185, 215, 0.28);
  --text: #dfe7f1;
  --muted: #8494a8;
  --dim: #5a6778;
  --accent: #ff8a2a;
  --accent-soft: rgba(255, 138, 42, 0.16);
  --blue: #3aa0ff;
  --good: #3ddc84;
  --warn: #ffc443;
  --bad: #ff4d4d;
  --r: 12px;
  --dash-h: 262px;
  --font: 'Barlow', system-ui, sans-serif;
  --font-cond: 'Barlow Condensed', 'Barlow', system-ui, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 14px; }
button { font: inherit; color: inherit; }
kbd {
  display: inline-block; min-width: 1.5em; padding: 0 .35em; margin-right: .35em;
  font-family: var(--mono); font-size: 11px; line-height: 1.6; text-align: center;
  color: var(--text); background: rgba(255,255,255,0.07); border: 1px solid var(--line-strong);
  border-bottom-width: 2px; border-radius: 5px;
}

#scene { position: fixed; inset: 0; }
#scene canvas { display: block; outline: none; }

/* ---------- loading ---------- */
#loading { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: var(--bg); transition: opacity .6s; }
#loading.done { opacity: 0; pointer-events: none; }
.load-box { text-align: center; }
.load-title { font-family: var(--font-cond); font-weight: 700; font-size: 34px; letter-spacing: .12em; }
.load-bar { width: 220px; height: 3px; margin: 16px auto; background: var(--line); border-radius: 3px; overflow: hidden; }
.load-bar span { display: block; width: 40%; height: 100%; background: var(--accent); animation: load 1.1s infinite ease-in-out; }
@keyframes load { 0% { transform: translateX(-100%); } 100% { transform: translateX(250%); } }
.load-msg { color: var(--muted); max-width: 420px; }

/* ---------- top bar ---------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 18px; padding: 10px 14px;
  background: linear-gradient(to bottom, rgba(13,17,23,0.95), rgba(13,17,23,0));
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; background: var(--accent-soft); border: 1px solid rgba(255,138,42,.4); }
.brand-mark svg { width: 20px; height: 20px; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; }
.brand-title { font-family: var(--font-cond); font-weight: 700; font-size: 20px; letter-spacing: .1em; line-height: 1; }
.brand-sub { color: var(--muted); font-size: 12px; }
.cams, .toggles { display: flex; gap: 6px; flex-wrap: wrap; }
.toggles { margin-left: auto; align-items: center; }
.cams button, .tog, .mini {
  display: inline-flex; align-items: center; height: 32px; padding: 0 10px; border-radius: 8px;
  background: var(--panel); border: 1px solid var(--line); cursor: pointer; white-space: nowrap;
  font-size: 13px; transition: background .15s, border-color .15s, color .15s;
}
.cams button:hover, .tog:hover, .mini:hover { border-color: var(--line-strong); background: #172130; }
.tog { color: var(--muted); }
.tog.on { color: var(--text); border-color: rgba(255,138,42,.45); background: rgba(255,138,42,.1); }
.tog.on kbd { border-color: rgba(255,138,42,.5); }
.help-btn { width: 32px; justify-content: center; font-weight: 700; color: var(--text); }
.sel { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 4px 0 10px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line); color: var(--muted); font-size: 13px; }
.sel select { background: #172130; color: var(--text); border: 1px solid var(--line); border-radius: 6px; height: 24px; font: inherit; font-size: 12px; }
body.paused #scene::after { content: 'PAUSED'; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); font-family: var(--font-cond); font-size: 40px; letter-spacing: .3em; color: rgba(255,255,255,.35); pointer-events: none; }

/* ---------- labels ---------- */
#labels { position: fixed; inset: 0; pointer-events: none; z-index: 5; overflow: hidden; }
.label3d {
  position: absolute; left: 0; top: 0; pointer-events: auto; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px 3px 6px; margin: -11px 0 0 -6px;
  font-size: 11.5px; font-weight: 500; color: #e9f0f8; white-space: nowrap;
  background: rgba(10, 14, 20, 0.62); border: 1px solid rgba(160,185,215,.2); border-radius: 20px;
  backdrop-filter: blur(3px); transition: background .15s, border-color .15s;
}
.label3d:hover { background: rgba(255,138,42,.25); border-color: rgba(255,138,42,.6); }
.label3d .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 6px var(--accent); }

/* ---------- explainer ---------- */
#explain {
  position: fixed; right: 12px; top: 62px; bottom: calc(var(--dash-h) + 20px); width: 340px; z-index: 8;
  overflow-y: auto; padding: 14px 16px; border-radius: var(--r);
  background: var(--panel); border: 1px solid var(--line); backdrop-filter: blur(8px);
}
#explain h3 { margin: 14px 0 8px; font-family: var(--font-cond); font-weight: 600; font-size: 15px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.ex-item { display: grid; grid-template-columns: 62px 1fr; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.ex-tag { font-family: var(--font-cond); font-weight: 600; font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); padding-top: 2px; }
.ex-item p, .ex-tip p { margin: 0; line-height: 1.45; font-size: 13px; color: #c9d4e1; }
.ex-item b { color: var(--text); }
b.good { color: var(--good) !important; } b.warn { color: var(--warn) !important; } b.bad { color: var(--bad) !important; } b.cool { color: var(--blue) !important; }
.ex-tip { margin-top: 10px; padding: 10px 12px; border-radius: 10px; background: rgba(58,160,255,.08); border: 1px solid rgba(58,160,255,.3); }
.ex-tip span { display: block; font-family: var(--font-cond); font-size: 12px; font-weight: 600; letter-spacing: .12em; text-transform: uppercase; color: var(--blue); margin-bottom: 4px; }

.flow-chain { display: flex; align-items: center; gap: 0; }
.flow-chain i { flex: 1; min-width: 4px; height: 2px; background: var(--line-strong); }
.flow-node {
  padding: 4px 6px; border-radius: 6px; font-size: 10.5px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase;
  color: var(--dim); border: 1px solid var(--line); background: rgba(255,255,255,.02); transition: all .2s;
}
.flow-node.drive { color: #fff; background: rgba(255,122,26,.35); border-color: var(--accent); box-shadow: 0 0 10px rgba(255,122,26,.45); }
.flow-node.brake { color: #fff; background: rgba(42,140,255,.3); border-color: var(--blue); box-shadow: 0 0 10px rgba(42,140,255,.4); }
.flow-node.slip { color: #111; background: var(--warn); border-color: var(--warn); }
.flow-node.open { color: var(--muted); border-style: dashed; }
.flow-node.crank { color: #fff; border-color: var(--warn); }
.flow-node.grind { color: #fff; background: var(--bad); border-color: var(--bad); animation: shake .08s infinite; }
.flow-node.braking { color: #fff; background: rgba(255,77,77,.3); border-color: var(--bad); }
@keyframes shake { 50% { transform: translateX(1px); } }

/* ---------- toasts ---------- */
#toasts { position: fixed; top: 64px; left: 50%; transform: translateX(-50%); z-index: 20; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast {
  padding: 9px 16px; border-radius: 10px; font-size: 14px; font-weight: 500; max-width: 520px; text-align: center;
  background: rgba(17,24,35,.95); border: 1px solid var(--line-strong); box-shadow: 0 8px 24px rgba(0,0,0,.4);
  opacity: 0; transform: translateY(-8px); transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: none; }
.toast.bad { border-color: var(--bad); background: rgba(80,16,16,.95); color: #ffe1e1; font-weight: 600; }
.toast.warn { border-color: var(--warn); background: rgba(70,52,10,.95); }
.toast.good { border-color: var(--good); background: rgba(12,58,34,.95); }

/* ---------- dashboard ---------- */
#dash {
  position: fixed; left: 12px; right: 12px; bottom: 12px; height: var(--dash-h); z-index: 9;
  display: grid; grid-template-columns: minmax(250px, 300px) minmax(420px, 1fr) minmax(210px, 250px) 214px; gap: 10px;
}
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 10px 12px; backdrop-filter: blur(8px); min-width: 0; }
.panel-title { display: flex; justify-content: space-between; align-items: baseline; font-family: var(--font-cond); font-weight: 600; font-size: 13px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.hint { font-family: var(--font); font-size: 11px; letter-spacing: 0; text-transform: none; color: var(--dim); }
.hint kbd { font-size: 10px; }

/* pedals */
#pedals { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; height: 136px; }
.pedal { position: relative; display: grid; grid-template-rows: 1fr auto auto; justify-items: center; }
.pedal-track { position: absolute; left: 4px; top: 4px; bottom: 44px; width: 5px; border-radius: 3px; background: rgba(255,255,255,.07); overflow: hidden; }
.pedal-fill { position: absolute; left: 0; right: 0; bottom: 0; height: 0; background: var(--muted); border-radius: 3px; }
.pedal-clutch .pedal-fill { background: var(--blue); }
.pedal-brake .pedal-fill { background: var(--bad); }
.pedal-gas .pedal-fill { background: var(--good); }
.bite { position: absolute; left: 0; right: 0; background: rgba(255,196,67,.45); }
.pedal-pad {
  align-self: start; margin-top: 10px; width: 54px; height: 70px; border-radius: 10px 10px 14px 14px; cursor: pointer; user-select: none; touch-action: none;
  display: grid; place-items: center; transform-origin: 50% 0;
  background: repeating-linear-gradient(0deg, #262d37 0 5px, #1b2129 5px 9px); border: 1px solid #39424f; box-shadow: 0 6px 12px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.08);
}
.pedal-gas .pedal-pad { width: 42px; height: 86px; border-radius: 8px; }
.pedal-pad span { font-family: var(--mono); font-weight: 600; font-size: 15px; color: var(--muted); background: rgba(0,0,0,.45); padding: 1px 7px; border-radius: 5px; }
.pedal.on .pedal-pad span { color: #fff; }
.pedal-clutch.on .pedal-pad { border-color: var(--blue); }
.pedal-brake.on .pedal-pad { border-color: var(--bad); }
.pedal-gas.on .pedal-pad { border-color: var(--good); }
.pedal-name { font-family: var(--font-cond); font-weight: 600; font-size: 13px; letter-spacing: .1em; text-transform: uppercase; }
.pedal-val { font-family: var(--mono); font-size: 11px; color: var(--muted); }

.clutch-meter { margin-top: 6px; }
.cm-head { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--muted); margin-bottom: 4px; }
.eng-state { font-family: var(--mono); font-weight: 600; font-size: 11px; }
.eng-state.good { color: var(--good); } .eng-state.warn { color: var(--warn); } .eng-state.off { color: var(--muted); }
.cm-bar { height: 7px; background: rgba(255,255,255,.07); border-radius: 4px; overflow: hidden; }
.eng-fill { height: 100%; width: 0; background: var(--muted); border-radius: 4px; transition: background .2s; }
.eng-fill.good { background: var(--good); } .eng-fill.warn { background: var(--warn); }
.cm-rel { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 11px; color: var(--dim); }
.cm-rel input { flex: 1; accent-color: var(--blue); min-width: 40px; }
.cm-rel output { font-family: var(--mono); color: var(--muted); width: 34px; text-align: right; }

/* gauges */
.gauges-panel { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 6px; }
.gauge { height: 100%; max-height: 236px; display: grid; place-items: center; min-width: 0; }
.gauge-svg { width: 100%; height: 100%; max-width: 236px; }
.g-bezel { fill: #0a0e13; stroke: #2a3340; stroke-width: 2; }
.g-face { fill: #0f151d; stroke: #1d2530; stroke-width: 1; }
.g-tick { stroke: #56657a; stroke-width: 1.2; }
.g-tick.major { stroke: #d6e0ec; stroke-width: 2.2; }
.g-num { fill: #c5d0dd; font-family: var(--font-cond); font-weight: 600; font-size: 13px; text-anchor: middle; }
.g-red { fill: none; stroke: var(--bad); stroke-width: 7; opacity: .85; }
.g-needle { fill: var(--accent); filter: drop-shadow(0 0 3px rgba(255,138,42,.8)); }
.g-cap { fill: #1c232d; stroke: #3b4654; stroke-width: 2; }
.g-readout { fill: var(--text); font-family: var(--mono); font-weight: 600; font-size: 15px; text-anchor: middle; }
.g-label { fill: var(--muted); font-family: var(--font-cond); font-size: 11px; letter-spacing: .2em; text-anchor: middle; }

.gear-box { display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 128px; }
.gear-big {
  width: 96px; height: 96px; border-radius: 18px; display: grid; place-items: center;
  font-family: var(--font-cond); font-weight: 700; font-size: 70px; line-height: 1;
  background: #0a0e13; border: 1px solid var(--line-strong); color: var(--accent); text-shadow: 0 0 18px rgba(255,138,42,.55);
}
.gear-big.n { color: var(--good); text-shadow: 0 0 18px rgba(61,220,132,.45); }
.gear-big.r { color: var(--bad); text-shadow: 0 0 18px rgba(255,77,77,.5); }
.gear-big.grind { animation: shake .06s infinite; border-color: var(--bad); }
.gear-sub { font-family: var(--mono); font-size: 12px; color: var(--muted); height: 16px; }
.lamps { display: flex; gap: 4px; }
.lamp { font-family: var(--mono); font-size: 10px; font-weight: 600; padding: 1px 5px; border-radius: 4px; color: #3a4350; border: 1px solid #262e39; }
.lamp.on.amber { color: #1a1300; background: var(--warn); border-color: var(--warn); box-shadow: 0 0 8px rgba(255,196,67,.6); }
.lamp.on.red { color: #fff; background: var(--bad); border-color: var(--bad); box-shadow: 0 0 8px rgba(255,77,77,.6); }
.key-row { display: flex; flex-direction: column; gap: 4px; margin-top: 4px; }
.mini { height: 26px; font-size: 12px; padding: 0 8px; }

/* telemetry */
.tele { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; margin: 0; font-size: 12px; }
.tele dt { color: var(--muted); }
.tele dd { margin: 0; font-family: var(--mono); font-size: 12px; text-align: right; color: var(--text); }
.tele dd.hot { color: var(--bad); }
.tele-unit { margin-top: 6px; font-size: 10.5px; color: var(--dim); }

/* shifter */
.shifter-panel { display: flex; flex-direction: column; }
#shifter { flex: 1; display: grid; place-items: center; min-height: 0; }
.shifter-svg { height: 100%; max-height: 214px; width: auto; touch-action: none; user-select: none; }
.sh-plate { fill: #0a0e13; stroke: #262e39; }
.sh-gate-outer { fill: none; stroke: #2c3541; stroke-width: 30; stroke-linecap: round; stroke-linejoin: round; }
.sh-gate { fill: none; stroke: #05080b; stroke-width: 24; stroke-linecap: round; stroke-linejoin: round; }
.sh-num { fill: var(--muted); font-family: var(--font-cond); font-weight: 700; font-size: 20px; text-anchor: middle; cursor: pointer; }
.sh-num:hover { fill: var(--text); }
.sh-num.active { fill: var(--accent); }
.sh-n { fill: var(--dim); font-family: var(--font-cond); font-size: 13px; text-anchor: middle; }
.sh-knob { cursor: grab; }
.sh-knob-ring { fill: #1c232d; stroke: #4b5667; stroke-width: 2; }
.sh-knob-core { fill: #cfd8e3; }
.sh-knob.engaged .sh-knob-core { fill: var(--accent); }
.sh-knob.grind .sh-knob-ring { stroke: var(--bad); stroke-width: 3; }

/* ---------- help ---------- */
#help { position: fixed; inset: 0; z-index: 40; display: none; place-items: center; background: rgba(5,8,12,.72); backdrop-filter: blur(4px); padding: 16px; }
#help.open { display: grid; }
.help-card { position: relative; max-width: 860px; width: 100%; max-height: 100%; overflow-y: auto; padding: 26px 30px; border-radius: 16px; background: var(--panel-solid); border: 1px solid var(--line-strong); box-shadow: 0 30px 60px rgba(0,0,0,.5); }
.help-card h2 { margin: 0 0 6px; font-family: var(--font-cond); font-size: 30px; letter-spacing: .04em; }
.lead { color: var(--muted); margin: 0 0 16px; }
.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.help-card h4 { margin: 0 0 8px; font-family: var(--font-cond); font-size: 15px; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); }
.help-card ul, .help-card ol { margin: 0; padding-left: 18px; line-height: 1.9; }
.help-card ul.keys { list-style: none; padding: 0; }
.note { color: var(--warn); font-size: 13px; }
.legend { display: flex; flex-wrap: wrap; gap: 16px; margin: 18px 0 6px; font-size: 12.5px; color: var(--muted); }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw.drive { background: #ff7a1a; } .sw.brake { background: #2a8cff; } .sw.air { background: #2f9bff; opacity: .6; } .sw.fire { background: #ffae42; }
.help-x { position: absolute; top: 12px; right: 14px; width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: transparent; font-size: 20px; cursor: pointer; }
.start-btn { margin-top: 14px; height: 42px; padding: 0 22px; border-radius: 10px; border: 0; background: var(--accent); color: #1a0e00; font-weight: 700; font-size: 15px; cursor: pointer; }
.start-btn:hover { filter: brightness(1.1); }

/* ---------- responsive ---------- */
@media (max-width: 1500px) {
  .cams button kbd, .tog kbd { display: none; }
}
@media (max-width: 1280px) {
  :root { --dash-h: 250px; }
  #dash { grid-template-columns: 260px 1fr 200px; }
  .tele-panel { display: none; }
  #explain { width: 300px; }
  .brand-sub { display: none; }
}
@media (max-width: 960px) {
  #topbar { flex-wrap: wrap; gap: 8px; }
  .toggles { margin-left: 0; }
  #explain { display: none; }
  #dash { grid-template-columns: 1fr 1fr; height: auto; left: 8px; right: 8px; bottom: 8px; }
  .gauges-panel { grid-column: 1 / -1; height: 170px; }
  .help-grid { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  .flow-node.grind, .gear-big.grind { animation: none; }
}
