:root {
  --fog: #DCDAD5; --ground: #E8E6E1; --asphalt: #CFCCC6; --line: #F7F6F3;
  --hair: #B9B6B0; --muted: #5F5C57; --body: #3A3935; --ink: #141416; --ember: #FF4A1C;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --display: 'Space Grotesk', system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--fog); color: var(--ink); font-family: Inter, system-ui, sans-serif; }
#stage { position: fixed; inset: 0; }
#stage canvas { display: block; }
.mono { font-family: var(--mono); }
.label { font-size: 12px; font-weight: 600; letter-spacing: 0.16em; color: var(--muted); }
.label.right { text-align: right; }
.label.wide { letter-spacing: 0.2em; }
.sublabel { font-size: 12px; font-weight: 500; letter-spacing: 0.08em; color: var(--muted); }
.muted { color: var(--muted); font-size: 14px; }
[hidden] { display: none !important; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; }
.hud-score { position: absolute; left: 48px; top: 40px; display: flex; flex-direction: column; gap: 6px; }
.score { font-size: 64px; font-weight: 700; line-height: 1; letter-spacing: -0.02em; }
.mult-row { display: flex; align-items: center; gap: 14px; margin-top: 8px; }
.mult { font-size: 22px; font-weight: 700; padding: 3px 9px; border: 1px solid var(--muted); color: var(--muted); transition: background .15s, color .15s; }
.mult.hot { background: var(--ember); border-color: var(--ember); color: var(--ink); }
.mult.bump { animation: bump .25s ease-out; }
@keyframes bump { 0% { transform: scale(1.25); } 100% { transform: scale(1); } }
.meter-col { display: flex; flex-direction: column; gap: 6px; }
.meter { width: 200px; height: 4px; background: #C2BFB9; }
.meter-fill { height: 4px; width: 0; background: var(--ink); }
.meter-fill.drain { background: var(--muted); }
.hud-center { position: absolute; left: 50%; top: 40px; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 4px; }
.dist { font-size: 20px; font-weight: 500; }
.hud-board { position: absolute; right: 48px; top: 44px; min-width: 240px; display: flex; flex-direction: column; gap: 10px; background: rgba(232,230,225,.85); padding: 12px 14px; }
.board { display: grid; grid-template-columns: 20px 12px 1fr auto; column-gap: 10px; row-gap: 8px; align-items: center; font-size: 15px; }
.board .rank { color: var(--muted); }
.board .sw { width: 10px; height: 10px; }
.board .me { font-weight: 700; }
.board .num { text-align: right; }
.hud-speed { position: absolute; left: 48px; bottom: 44px; display: flex; align-items: flex-end; gap: 36px; }
.hud-speed .col { display: flex; flex-direction: column; gap: 4px; }
.big { font-size: 44px; font-weight: 700; line-height: 1; }
.unit { font-size: 16px; font-weight: 500; color: var(--muted); }
.mid { font-size: 32px; font-weight: 700; line-height: 1; }
.angle-row { display: flex; align-items: center; gap: 10px; }
.hud-map { position: absolute; right: 48px; bottom: 40px; width: 200px; display: flex; flex-direction: column; gap: 6px; }
.hud-map canvas { width: 200px; height: 120px; }
.floater { position: absolute; left: 0; top: 0; font-size: 30px; font-weight: 700; white-space: nowrap; transform: translate(-50%, -50%); opacity: 0; transition: opacity .2s; }
.floater.lost { color: var(--muted); text-decoration: line-through; }
.floater.banked { color: var(--ink); }
#tags .tag { position: absolute; left: 0; top: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; transform: translate(-50%, -100%); }
#tags .tag span:first-child { font-family: var(--mono); font-size: 12px; font-weight: 500; background: var(--line); padding: 2px 6px; }
#tags .tag span:last-child { width: 1px; height: 12px; background: var(--ink); }
.toast { position: absolute; left: 50%; top: 104px; transform: translateX(-50%); font-size: 12px; font-weight: 600; letter-spacing: 0.14em; color: var(--ink); background: var(--line); padding: 6px 10px; opacity: 0; transition: opacity .3s; }
.toast.show { opacity: 1; }

.countdown { position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%); font-size: 120px; font-weight: 700; line-height: 1; opacity: 0; transition: opacity .15s; }
.countdown.show { opacity: 1; }
.countdown.go { color: var(--ember); }
.countdown.pop { animation: countPop .35s ease-out; }
@keyframes countPop { 0% { transform: translate(-50%, -50%) scale(1.4); } 100% { transform: translate(-50%, -50%) scale(1); } }
.score.timer { font-variant-numeric: tabular-nums; }

/* ---------- Lobby ---------- */
#attract { position: fixed; inset: 0; }
#attract canvas { display: block; }
#lobby { position: fixed; inset: 0; background: var(--ground); overflow: auto; display: grid; grid-template-columns: minmax(0, 1fr) 420px; grid-template-rows: auto 1fr; column-gap: 64px; row-gap: 32px; padding: 64px 96px; }
/* AI cars drift behind the lobby (client/attract/): the lobby turns into a veil over them */
#lobby.over-attract { background: color-mix(in srgb, var(--ground) 60%, transparent); }
.lobby-side { grid-column: 2; grid-row: 1 / span 2; display: flex; flex-direction: column; gap: 28px; }
.lobby-hero { display: flex; flex-direction: column; gap: 20px; max-width: 640px; }
.lobby-hero h1 { margin: 0; font-family: var(--display); font-size: 168px; font-weight: 700; line-height: 0.86; letter-spacing: -0.05em; }
.lobby-hero p { margin: 0; font-size: 20px; line-height: 1.45; max-width: 460px; color: var(--body); }
.lobby-card { background: var(--line); padding: 36px; display: flex; flex-direction: column; gap: 22px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field input { height: 52px; border: 1px solid var(--ink); background: #fff; padding: 0 14px; font-family: var(--mono); font-size: 18px; color: var(--ink); border-radius: 0; }
.field input:focus { outline: 2px solid var(--ink); outline-offset: 2px; }
#fRoom { letter-spacing: 0.08em; text-transform: uppercase; }
.swatches { display: flex; gap: 10px; }
.swatches button { width: 48px; height: 48px; border: 1px solid var(--ink); background: var(--c); cursor: pointer; padding: 0; }
.swatches button[aria-checked="true"] { border-width: 2px; box-shadow: inset 0 0 0 4px var(--line); }
.pills { display: flex; gap: 8px; }
.pills button { height: 44px; padding: 0 16px; border: 1px solid var(--ink); background: transparent; font-family: var(--mono); font-size: 15px; color: var(--ink); cursor: pointer; }
.pills button[aria-checked="true"] { background: var(--ink); color: var(--line); }
button:focus-visible { outline: 2px solid var(--ember); outline-offset: 2px; }
.primary { height: 56px; border: none; background: var(--ink); color: var(--line); font-family: Inter, sans-serif; font-size: 16px; font-weight: 600; letter-spacing: 0.08em; cursor: pointer; }
.primary:disabled { opacity: .5; cursor: wait; }
.secondary { height: 48px; border: 1px solid var(--ink); background: transparent; color: var(--ink); font-family: Inter, sans-serif; font-size: 15px; font-weight: 500; cursor: pointer; }
.hint { font-size: 13px; color: var(--muted); line-height: 1.4; }
.err { margin: 0; min-height: 1em; font-size: 14px; color: #B3290A; }
.lobby-rooms { display: flex; flex-direction: column; gap: 10px; }
#roomList { display: flex; flex-direction: column; }
#roomList button { display: grid; grid-template-columns: 1fr auto auto auto; column-gap: 20px; align-items: center; height: 44px; border: none; border-bottom: 1px solid var(--hair); background: transparent; padding: 0; font-family: var(--mono); font-size: 15px; color: var(--ink); text-align: left; cursor: pointer; }
.lobby-keys { grid-column: 1; grid-row: 2; align-self: end; display: flex; flex-wrap: wrap; gap: 14px 28px; font-size: 14px; color: var(--body); }
.lobby-keys span { display: flex; align-items: center; gap: 8px; }
kbd { font-family: var(--mono); font-size: 13px; border: 1px solid var(--ink); padding: 4px 8px; background: var(--line); }

/* ---------- Pause / finish ---------- */
#finish { position: fixed; inset: 0; background: rgba(220,218,213,.6); display: flex; align-items: center; justify-content: center; }
.finish-time { font-size: 56px; font-weight: 700; line-height: 1; }
#pause { position: fixed; inset: 0; background: rgba(220,218,213,.82); display: flex; align-items: center; justify-content: center; }
.pause-card { background: var(--line); padding: 36px; width: 360px; display: flex; flex-direction: column; gap: 14px; }
.pause-card p { margin: 0 0 8px; font-size: 15px; line-height: 1.6; }

/* ---------- Touch controls (client/game/touch.js) ---------- */
#stage { touch-action: none; }
#touch { display: none; }
body.touch #touch { display: block; position: absolute; inset: 0; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.t-steer, .t-pedals { position: absolute; top: 34%; bottom: 0; pointer-events: auto; touch-action: none; }
.t-steer { left: 0; width: 50%; }
.t-pedals { right: 0; width: 50%; }
.t-hint { position: absolute; left: 24px; bottom: calc(24px + env(safe-area-inset-bottom)); font-size: 12px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); opacity: .8; }
.t-base { position: absolute; left: -60px; top: -60px; width: 120px; height: 120px; border-radius: 50%; border: 1px solid var(--ink); background: color-mix(in srgb, var(--line) 35%, transparent); opacity: 0; transition: opacity .12s; display: flex; align-items: center; justify-content: center; }
.t-base.on { opacity: .85; }
.t-steer:has(.t-base.on) .t-hint { opacity: 0; }
.t-knob { width: 52px; height: 52px; border-radius: 50%; background: var(--ink); }
.t-pedal { position: absolute; display: flex; align-items: center; justify-content: center; font-family: var(--mono); font-size: 13px; font-weight: 700; letter-spacing: 0.1em; color: var(--ink); border: 1px solid var(--ink); background: color-mix(in srgb, var(--line) 70%, transparent); }
.t-pedal.down { background: var(--ink); color: var(--line); }
.t-gas { right: calc(20px + env(safe-area-inset-right)); bottom: calc(24px + env(safe-area-inset-bottom)); width: 96px; height: 128px; }
.t-gas.down { background: var(--ember); border-color: var(--ember); color: var(--ink); }
.t-brake { right: calc(128px + env(safe-area-inset-right)); bottom: calc(24px + env(safe-area-inset-bottom)); width: 84px; height: 84px; }
.t-hand { right: calc(20px + env(safe-area-inset-right)); bottom: calc(164px + env(safe-area-inset-bottom)); width: 96px; height: 60px; }
.t-buttons { position: absolute; right: calc(16px + env(safe-area-inset-right)); top: calc(16px + env(safe-area-inset-top)); display: flex; gap: 10px; pointer-events: auto; }
.t-btn { width: 44px; height: 44px; border: 1px solid var(--ink); background: color-mix(in srgb, var(--line) 70%, transparent); color: var(--ink); font-family: var(--mono); font-size: 16px; padding: 0; cursor: pointer; touch-action: manipulation; }
/* move the HUD out of the thumbs' way */
body.touch .hud-speed { bottom: calc(240px + env(safe-area-inset-bottom)); }
body.touch .hud-map { bottom: calc(240px + env(safe-area-inset-bottom)); }
body.touch .hud-board { top: 76px; }
.lobby-touch { display: none; }
body.touch .lobby-keys { display: none; }
body.touch .lobby-keys.lobby-touch { display: flex; }

@media (max-width: 900px) {
  #lobby { grid-template-columns: 1fr; padding: 32px 16px; }
  .lobby-hero h1 { font-size: 104px; }
  .lobby-side, .lobby-keys { grid-column: 1; grid-row: auto; }
  .hud-score { left: 16px; top: 16px; } .score { font-size: 40px; }
  .hud-board, .hud-map { display: none; }
  .hud-speed { left: 16px; bottom: 16px; }
  /* touch: the pause/reset buttons take the top right, the distance goes under them */
  body.touch .hud-center { left: auto; right: calc(16px + env(safe-area-inset-right)); top: calc(70px + env(safe-area-inset-top)); transform: none; align-items: flex-end; }
  body.touch .hud-center .sublabel { font-size: 11px; }
  body.touch .toast { top: 130px; }
  /* the car sits low on a tall screen: speed and angle go under the score, out of its way */
  body.touch .meter { width: 140px; }
  body.touch .hud-speed { bottom: auto; top: calc(142px + env(safe-area-inset-top)); gap: 20px; }
  body.touch .hud-speed .big { font-size: 28px; }
  body.touch .hud-speed .mid { font-size: 22px; }
  body.touch .hud-speed svg { width: 32px; height: 32px; }
}
/* phones in landscape: little height, so a smaller HUD that sits beside the thumbs */
@media (max-height: 500px) {
  body.touch .hud-speed { bottom: auto; top: calc(142px + env(safe-area-inset-top)); left: 16px; gap: 20px; }
  body.touch .hud-speed .big { font-size: 28px; }
  body.touch .hud-speed .mid { font-size: 22px; }
  body.touch .hud-speed svg { width: 32px; height: 32px; }
  body.touch .t-steer, body.touch .t-pedals { top: 20%; }
}
