:root {
  --bg: #0b0d10;
  --surface: #13161b;
  --surface-2: #1a1e25;
  --panel: rgba(11, 13, 17, 0.78);
  --line: rgba(255, 255, 255, 0.1);
  --line-strong: rgba(255, 255, 255, 0.2);
  --text: #f3f4f6;
  --muted: #949ba7;
  --accent: #e8112d;
  --accent-hi: #ff2d48;
  --ghost: #4aa3ff;
  --purple: #b36bff;
  --green: #34d27b;
  --yellow: #f5c542;
  --red: #ff4d4d;
  --font: 'Barlow Condensed', 'Arial Narrow', system-ui, sans-serif;
  --gap: max(12px, env(safe-area-inset-top));
  --gl: max(12px, env(safe-area-inset-left));
  --gr: max(12px, env(safe-area-inset-right));
  --gb: max(14px, env(safe-area-inset-bottom));
  --map: 168px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

button { font-family: inherit; color: inherit; cursor: pointer; }
button:focus-visible, a:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

#game, #game3d {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
/* The 3D canvas only shows in cockpit view; the 2D one hides behind it. */
#game3d { display: none; }
body.cockpit #game3d { display: block; }
body.cockpit #game { visibility: hidden; }

.label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.label b { color: var(--text); font-size: 14px; }

/* ---------- HUD ---------- */

.panel {
  position: fixed;
  top: var(--gap);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  font-variant-numeric: tabular-nums;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.timing { left: var(--gl); width: 232px; padding: 10px 12px 12px; }
.timing-head { display: flex; justify-content: space-between; gap: 10px; }
.track-label { letter-spacing: 0.08em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.timing-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 24px; }

.lap-time { font-size: 44px; font-weight: 700; line-height: 1; margin: 4px 0 2px; letter-spacing: 0.01em; }
.lap-line { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; }
.race-pos { font-size: 34px; font-weight: 800; font-style: italic; line-height: 1; color: var(--yellow); margin-bottom: 4px; white-space: nowrap; }
.race-pos small { font-size: 16px; color: var(--muted); font-style: normal; }

/* Race standings under the map. */
body:not(.racing) #racePos, body:not(.racing) #standings { display: none; }
body.racing #racePos, body.racing #standings { display: grid; }
body.racing #racePos { display: block; }
.standings { list-style: none; margin: 4px 0 0; padding: 4px 4px 2px; border-top: 1px solid var(--line); display: grid; gap: 2px; }
.standings li { display: grid; grid-template-columns: 16px 4px 1fr auto; align-items: center; gap: 6px; font-size: 15px; font-weight: 600; line-height: 1.25; }
.standings li.me { color: var(--yellow); }
.standings .p { color: var(--muted); font-weight: 700; }
.standings .c { width: 4px; height: 12px; border-radius: 2px; }
.standings .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.standings .g { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }

.delta { font-size: 22px; font-weight: 700; }
.delta.ahead { color: var(--green); }
.delta.behind { color: var(--red); }

.badge {
  display: none;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  background: var(--red);
  color: #fff;
  padding: 2px 8px;
  border-radius: 4px;
}
.badge.on { display: block; }
.badge.flash { animation: flash 0.6s 2; }

/* Watch an ad to keep a lap that went over the limits. */
.keep-lap {
  display: none;
  width: 100%;
  margin-top: 6px;
  padding: 5px 8px;
  border: 1px solid rgba(245, 197, 66, 0.6);
  border-radius: 6px;
  background: rgba(245, 197, 66, 0.14);
  color: var(--yellow);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.keep-lap.on { display: block; }
.keep-lap small { font-size: 11px; opacity: 0.75; }
.keep-lap:hover { background: rgba(245, 197, 66, 0.24); }
@keyframes flash { 50% { opacity: 0.2; } }

.sectors { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-top: 6px; }
.sector {
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  padding: 3px 4px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
}
.sector.active { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.4); color: var(--text); }
.sector.purple { background: var(--purple); color: #fff; }
.sector.green { background: var(--green); color: #07210f; }
.sector.yellow { background: var(--yellow); color: #2a2000; }
.sector.invalid { background: rgba(255, 255, 255, 0.15); color: var(--muted); text-decoration: line-through; }

/* Track map: top right, where the eye goes. */
.map-panel { right: var(--gr); width: var(--map); padding: 6px; }
#minimap { display: block; width: 100%; height: calc(var(--map) - 12px); }
.map-times { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 4px 4px 2px; border-top: 1px solid var(--line); margin-top: 4px; }
.map-stat { display: flex; flex-direction: column; font-size: 17px; font-weight: 700; line-height: 1.1; }
.map-stat .label { font-size: 10px; }

.hud-buttons {
  position: fixed;
  top: var(--gap);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}
.round-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: grid;
  place-items: center;
  padding: 0;
}
.round-btn:hover { background: rgba(30, 34, 40, 0.9); }
.mute-btn .cross { display: none; }
.mute-btn.muted .waves { display: none; }
.mute-btn.muted .cross { display: inline; }

/* Speed cluster: rev lights over gear and speed, pedal bars either side. */
.cluster {
  position: fixed;
  left: 50%;
  bottom: var(--gb);
  transform: translateX(-50%);
  width: 250px;
  padding: 8px 12px 6px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.revs {
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}
.rev-fill {
  height: 100%;
  width: 100%;
  transform-origin: left;
  transform: scaleX(0);
  background: linear-gradient(90deg, var(--green) 0 55%, var(--yellow) 55% 80%, var(--red) 80% 100%);
  background-size: 250px 100%;
}
.rev-fill.limit { animation: limiter 0.12s steps(2) infinite; }
@keyframes limiter { 50% { opacity: 0.35; } }
.cluster-main { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.gear {
  min-width: 38px;
  font-size: 40px;
  font-weight: 800;
  line-height: 1;
  color: var(--yellow);
  text-align: center;
}
.speed { flex: 1; display: flex; align-items: baseline; justify-content: center; gap: 4px; }
#speedVal { font-size: 44px; font-weight: 800; line-height: 1; min-width: 3ch; text-align: right; }
.unit { font-size: 14px; font-weight: 600; color: var(--muted); }
.pedal-bar {
  width: 8px;
  height: 40px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: flex-end;
  overflow: hidden;
}
.pedal-bar div { width: 100%; height: 100%; transform-origin: bottom; transform: scaleY(0); border-radius: 4px; }
.gas-bar div { background: var(--green); }
.brake-bar div { background: var(--red); }

/* ---------- start lights + toast ---------- */

#lights {
  position: fixed;
  top: 24%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
}
.lights-row {
  display: flex;
  gap: 14px;
  padding: 12px 16px;
  background: #0d0d0e;
  border-radius: 14px;
  border: 1px solid #2a2a2e;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}
.light { width: 38px; height: 38px; border-radius: 50%; background: #2a0d0d; transition: background 0.05s; }
.light.on { background: #ff2a2a; box-shadow: 0 0 22px #ff2a2a; }
.lights-text { font-size: 64px; font-weight: 800; font-style: italic; color: var(--green); text-shadow: 0 3px 14px rgba(0, 0, 0, 0.6); }

.toast {
  position: fixed;
  top: 36%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.92);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  text-align: center;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 28px;
  pointer-events: none;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.toast-title { font-size: 13px; letter-spacing: 0.16em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.toast-time { font-size: 46px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.05; }
.toast-sub { font-size: 18px; font-weight: 700; }
.toast.best .toast-sub { color: var(--purple); }
.toast.slower .toast-sub { color: var(--yellow); }
.toast.bad .toast-sub { color: var(--red); }
.toast.info .toast-time { font-size: 30px; }

/* ---------- touch controls ---------- */

#touch { display: none; }
body.touch #hud:not([hidden]) ~ #touch { display: block; }

#touch button {
  position: fixed;
  bottom: var(--gb);
  border: 2px solid rgba(255, 255, 255, 0.3);
  background: rgba(10, 12, 16, 0.42);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  color: #fff;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  touch-action: none;
  display: grid;
  place-items: center;
  transition: transform 0.05s, background 0.05s;
}
#touch button.down { background: rgba(255, 255, 255, 0.28); transform: scale(0.95); }

.steer button { width: 84px; height: 84px; border-radius: 50%; }
.steer button[data-control="left"] { left: var(--gl); }
.steer button[data-control="right"] { left: calc(var(--gl) + 96px); }

.pedal { width: 80px; height: 118px; border-radius: 16px; font-size: 16px; }
.pedal.gas { right: var(--gr); border-color: rgba(52, 210, 123, 0.75) !important; }
.pedal.brake { right: calc(var(--gr) + 92px); border-color: rgba(255, 77, 77, 0.75) !important; }
#touch .pedal.gas.down { background: rgba(52, 210, 123, 0.35); }
#touch .pedal.brake.down { background: rgba(255, 77, 77, 0.35); }

/* On touch screens the cluster moves up out of the thumbs' way. */
body.touch .cluster {
  bottom: auto;
  top: calc(var(--gap) + 50px);
  width: 170px;
  padding: 6px 10px 4px;
}
body.touch .gear { font-size: 28px; min-width: 26px; }
body.touch #speedVal { font-size: 30px; }
body.touch .pedal-bar { display: none; }
body.touch .rev-fill { background-size: 170px 100%; }

@media (max-width: 720px), (max-height: 480px) {
  :root { --map: 128px; }
  .timing { width: 184px; padding: 8px 10px 10px; }
  .lap-time { font-size: 32px; }
  .delta { font-size: 18px; }
  .sector { font-size: 13px; }
  .map-stat { font-size: 15px; }
  .light { width: 28px; height: 28px; }
  .lights-text { font-size: 48px; }
  .toast-time { font-size: 36px; }
}

/* Short landscape phones in a race: standings matter more than best/last,
   and the map panel must stay clear of the pedals. */
@media (max-height: 480px) {
  body.racing .map-times { display: none; }
  body.racing .standings li { font-size: 13px; line-height: 1.1; }
  body.racing .standings .g { font-size: 11px; }
  body.racing .map-panel { --map: 104px; }
}

@media (max-width: 460px) {
  :root { --map: 112px; }
  .race-pos { font-size: 22px; }
  .race-pos small { font-size: 12px; }
  .timing { width: 150px; }
  .lap-time { font-size: 27px; }
  .track-label { display: none; }
  .sector { font-size: 12px; padding: 2px; }
  .hud-buttons { flex-direction: column; left: auto; right: calc(var(--gr) + var(--map) + 8px); transform: none; }
  .round-btn { width: 36px; height: 36px; }
  body.touch .cluster { top: auto; bottom: calc(var(--gb) + 130px); }
  .steer button { width: 74px; height: 74px; }
  .steer button[data-control="right"] { left: calc(var(--gl) + 84px); }
  .pedal { width: 72px; height: 104px; }
  .pedal.brake { right: calc(var(--gr) + 82px); }
}

/* ---------- overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: rgba(6, 8, 10, 0.6);
  overflow-y: auto;
  touch-action: pan-y;
  z-index: 10;
}
#settings { z-index: 20; background: rgba(6, 8, 10, 0.7); }

.menu-overlay {
  background:
    radial-gradient(ellipse at 70% 0%, rgba(232, 17, 45, 0.16), transparent 55%),
    linear-gradient(180deg, rgba(6, 8, 10, 0.55), rgba(6, 8, 10, 0.85));
}

.menu {
  width: min(1040px, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: rgba(13, 15, 19, 0.94);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px 20px 16px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}

.menu-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.menu-top-buttons { display: flex; gap: 8px; }
.account-btn.signed-in { border-color: rgba(245, 197, 66, 0.4); color: var(--yellow); }

/* Daily challenge card. */
.challenge-card {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 12px; margin-bottom: 12px; border-radius: 12px;
  border: 1px solid rgba(179, 107, 255, 0.45);
  background: linear-gradient(90deg, rgba(179, 107, 255, 0.18), rgba(179, 107, 255, 0.04));
}
.challenge-card .label { color: var(--purple); }
.challenge-track { font-size: 22px; font-weight: 800; line-height: 1.1; }
.challenge-ends { font-size: 14px; color: var(--muted); }
.challenge-card .secondary { padding: 8px 18px; font-size: 16px; }

/* Leaderboard in the circuit details. */
.board { border-top: 1px solid var(--line); padding-top: 10px; }
.board-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.board-tabs { display: flex; gap: 4px; }
.board-tabs button { border: 1px solid var(--line); background: transparent; border-radius: 6px; padding: 2px 8px; font-size: 13px; font-weight: 700; color: var(--muted); }
.board-tabs button[aria-pressed="true"] { background: var(--text); color: #0b0d10; }
.board-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.board-row {
  width: 100%; display: grid; grid-template-columns: 20px 1fr auto 16px; gap: 8px; align-items: center;
  padding: 4px 6px; border: 0; border-radius: 6px; background: transparent; text-align: left;
  font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.board-row:hover { background: rgba(255, 255, 255, 0.06); }
.board-row.me { color: var(--yellow); }
.board-row .p { color: var(--muted); }
.board-row .go { color: var(--muted); font-size: 12px; opacity: 0; }
.board-row:hover .go { opacity: 1; }
.board-gap { text-align: center; color: var(--muted); line-height: 0.8; }
.board-empty { font-size: 15px; color: var(--muted); padding: 4px 6px; }
.link-btn { margin-top: 6px; border: 0; background: none; padding: 0; color: var(--text); font-size: 15px; font-weight: 700; text-decoration: underline; }
.versus-note { margin: 8px 0 0; font-size: 16px; color: var(--ghost); font-weight: 700; }

/* Medals: bronze, silver, gold. */
.medal {
  display: inline-block; width: 0.72em; height: 0.72em; margin-right: 6px; border-radius: 50%; vertical-align: 0;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.25), 0 0 0 1px rgba(0, 0, 0, 0.4);
}
.medal.m1 { background: radial-gradient(circle at 35% 30%, #f3b98a, #a5622b); }
.medal.m2 { background: radial-gradient(circle at 35% 30%, #ffffff, #98a1ab); }
.medal.m3 { background: radial-gradient(circle at 35% 30%, #fff3b0, #d4a017); }
.medal-targets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.medal-target {
  display: inline-flex; align-items: center; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line);
  font-size: 13px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted);
}
.medal-target b { margin-left: 6px; font-size: 16px; letter-spacing: 0; font-variant-numeric: tabular-nums; color: var(--text); }
.medal-target .medal { opacity: 0.35; }
.medal-target.got { border-color: var(--line-strong); background: rgba(255, 255, 255, 0.06); color: var(--text); }
.medal-target.got .medal { opacity: 1; }
.garage-btn b {
  min-width: 1.7em; padding: 1px 6px; border-radius: 999px; text-align: center; letter-spacing: 0;
  font-size: 14px; background: rgba(245, 197, 66, 0.16); color: var(--yellow);
}

/* Account dialog. */
.field { display: flex; flex-direction: column; gap: 4px; margin: 4px 0 10px; }
.field span { font-size: 13px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.field input {
  font: inherit; font-size: 20px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line-strong); background: rgba(255, 255, 255, 0.05); color: var(--text);
  -webkit-user-select: text; user-select: text;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field-error { color: var(--red); font-size: 16px; margin: 0; min-height: 1em; }
.account-sheet .primary { width: 100%; }
.google-btn { width: 100%; display: flex; align-items: center; justify-content: center; gap: 10px; }
.or { display: flex; align-items: center; gap: 10px; margin: 12px 0 4px; font-size: 13px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.or::before, .or::after { content: ''; flex: 1; border-top: 1px solid var(--line); }

/* Online race lobby. */
.lobby-code { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); }
.lobby-code b { display: block; font-size: 32px; font-weight: 800; letter-spacing: 0.18em; line-height: 1; }
.lobby-code .secondary { padding: 8px 16px; font-size: 16px; }
.lobby-players { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.lobby-players li { display: grid; grid-template-columns: 6px 1fr auto; gap: 10px; align-items: center; font-size: 19px; font-weight: 700; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.lobby-players .c { width: 6px; height: 18px; border-radius: 3px; }
.lobby-players .tag { font-size: 12px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--yellow); }
.check { display: flex; align-items: center; gap: 8px; font-size: 17px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.lobby-ai { font-size: 15px; color: var(--muted); min-height: 1em; }
.lobby-status { font-size: 16px; }
.lobby-join { display: flex; gap: 8px; }
.lobby-join input { flex: 1; min-width: 0; font: inherit; font-size: 18px; letter-spacing: 0.14em; text-transform: uppercase; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line-strong); background: rgba(255, 255, 255, 0.05); color: var(--text); -webkit-user-select: text; user-select: text; }
.lobby-join .secondary { padding: 8px 14px; font-size: 16px; }

.logo {
  font-size: 38px;
  font-weight: 800;
  font-style: italic;
  letter-spacing: 0.01em;
  color: var(--text);
  text-decoration: none;
  line-height: 1;
}
.logo span { color: var(--accent); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.04);
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.09); }

.menu-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  gap: 18px;
  min-height: 0;
}

.menu-heading {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}

.circuits { display: flex; flex-direction: column; min-height: 0; }
.track-list { display: flex; flex-direction: column; gap: 6px; overflow-y: auto; padding: 2px; margin: -2px; }

.track-card {
  display: grid;
  grid-template-columns: 64px 1fr auto;
  align-items: center;
  gap: 12px;
  text-align: left;
  padding: 8px 12px 8px 8px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  transition: background 0.12s, border-color 0.12s;
}
.track-card:hover { background: rgba(255, 255, 255, 0.07); }
.track-card.selected { border-color: var(--accent); background: rgba(232, 17, 45, 0.1); box-shadow: inset 3px 0 0 var(--accent); }
.track-card canvas { width: 64px; height: 48px; display: block; }
.track-name { font-size: 21px; font-weight: 800; line-height: 1.05; }
.track-sub { font-size: 14px; color: var(--muted); }
.track-best { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; color: var(--muted); }
.track-best b { display: block; color: var(--text); }
.track-best .label { font-size: 10px; }

.detail {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
}
.detail-map {
  width: 100%;
  height: 210px;
  display: block;
  border-radius: 10px;
  background:
    radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.04), transparent 70%),
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.025) 0 1px, transparent 1px 24px),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.025) 0 1px, transparent 1px 24px),
    #0e1115;
}
.detail-country { font-size: 13px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--accent-hi); }
.detail-name { font-size: 40px; font-weight: 800; font-style: italic; line-height: 1; margin: 2px 0 6px; }
.detail-blurb { font-size: 17px; color: #c3c8d0; margin: 0 0 10px; line-height: 1.3; }
.detail-stats { display: grid; grid-template-columns: auto auto 1fr; gap: 6px 18px; margin: 0; }
.detail-stats div { min-width: 0; }
.detail-stats dt { font-size: 11px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.detail-stats dd { margin: 0; font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

.modes { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.mode-heading { margin-top: 14px; }
.race-options { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-top: 10px; }
.race-option { display: flex; align-items: center; gap: 8px; }
.race-option .segmented button { padding: 5px 10px; font-size: 15px; }

/* Race results. */
.results-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.results-pos { font-size: 56px; font-weight: 800; font-style: italic; line-height: 0.9; color: var(--yellow); }
.results-table { width: 100%; border-collapse: collapse; font-size: 18px; font-variant-numeric: tabular-nums; }
.results-table th { text-align: left; font-size: 12px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); padding: 6px 8px; }
.results-table td { padding: 8px; border-top: 1px solid var(--line); }
.results-table tr.me td { color: var(--yellow); font-weight: 700; }
.results-table .swatch-dot { display: inline-block; width: 6px; height: 14px; border-radius: 2px; margin-right: 8px; vertical-align: -2px; }
.results-overlay { background: rgba(6, 8, 10, 0.45); }
.mode {
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.03);
  font-size: 18px;
  font-weight: 700;
  text-align: left;
}
.mode small { display: block; font-size: 13px; font-weight: 500; color: var(--muted); }
.mode.selected { border-color: var(--accent); }
.mode:disabled { opacity: 0.45; cursor: default; }

.primary, .secondary {
  padding: 13px;
  border-radius: 11px;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.primary {
  border: 0;
  background: linear-gradient(180deg, var(--accent-hi), var(--accent));
  color: #fff;
  box-shadow: 0 6px 20px rgba(232, 17, 45, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.primary:hover { filter: brightness(1.08); }
.detail #startBtn {
  position: sticky; bottom: 0; flex-shrink: 0; margin-top: auto;
  /* Fade out whatever scrolls behind it. */
  box-shadow: 0 -18px 16px 2px var(--surface), 0 6px 20px rgba(232, 17, 45, 0.3);
}
.primary:active { transform: translateY(1px); }
.primary kbd { border-color: rgba(255, 255, 255, 0.4); color: #fff; font-size: 12px; letter-spacing: 0.04em; }
.secondary { border: 1px solid var(--line); background: rgba(255, 255, 255, 0.05); font-size: 18px; }
.secondary:hover { background: rgba(255, 255, 255, 0.1); }
.secondary.danger { color: var(--red); border-color: rgba(255, 77, 77, 0.35); padding: 8px 14px; font-size: 16px; }

.menu-foot {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 16px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  font-size: 15px;
  color: var(--muted);
}
.menu-foot a { color: var(--text); font-weight: 700; }
.foot-links { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 16px; }
.menu-foot .policies { display: flex; gap: 12px; }
.menu-foot .policies a { color: var(--muted); font-weight: 600; }
.fine-print { margin: 2px 0 0; font-size: 13px; line-height: 1.4; color: var(--muted); }
.fine-print a { color: var(--text); }
.menu-foot em { font-style: normal; font-size: 12px; font-weight: 700; letter-spacing: 0.1em; text-transform: uppercase; padding: 1px 6px; margin-left: 6px; border-radius: 4px; background: rgba(255, 255, 255, 0.08); color: var(--muted); }

/* Test-mode stand-in for a portal ad (?platform=test). */
.test-ad {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.88);
  text-align: center;
}
.test-ad > div { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.test-ad b { font-size: 28px; text-transform: uppercase; letter-spacing: 0.08em; }
.test-ad span { color: var(--muted); font-size: 17px; }
.test-ad em { font-style: normal; font-size: 56px; font-weight: 800; }
.test-ad button { padding: 8px 16px; border-radius: 8px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); font-size: 16px; font-weight: 700; }

.controls-help { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 15px; color: var(--muted); justify-content: center; }
.pad-hint { opacity: 0.8; }
kbd {
  font-family: inherit;
  font-weight: 700;
  font-size: 13px;
  padding: 1px 6px;
  margin-right: 3px;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text);
}
body.touch .keys-only { display: none; }

/* Dialog sheets: pause and settings. */
.sheet {
  width: min(560px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: rgba(15, 17, 22, 0.97);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.5);
}
.sheet.small { width: min(320px, 100%); }
#help .sheet { width: min(380px, 100%); }
.sheet-text { font-size: 18px; color: #c3c8d0; line-height: 1.3; margin: 0; }
.line-legend { display: flex; gap: 6px; margin: 2px 0 6px; }
.line-legend .swatch { width: 100%; height: 6px; margin: 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-title { font-size: 28px; font-weight: 800; font-style: italic; margin: 0 0 4px; text-transform: uppercase; letter-spacing: 0.02em; }
.sheet .icon-btn { padding: 6px; }

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 0;
  border-top: 1px solid var(--line);
}
.setting-name { font-size: 20px; font-weight: 700; }
.setting-help { font-size: 15px; color: var(--muted); line-height: 1.25; }
.swatch { display: inline-block; width: 14px; height: 5px; border-radius: 3px; margin: 0 4px 0 2px; vertical-align: middle; }
.swatch.go { background: var(--green); }
.swatch.lift { background: var(--yellow); }
.swatch.stop { background: var(--accent-hi); }

.segmented {
  display: inline-flex;
  flex-shrink: 0;
  padding: 3px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
}
.segmented button {
  border: 0;
  background: transparent;
  padding: 7px 12px;
  border-radius: 7px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.segmented button[aria-checked="true"] { background: var(--text); color: #0b0d10; }
.sheet .primary { margin-top: 6px; }

/* Garage: car colour cards, two to a row. */
.sheet.garage-sheet { width: min(420px, 100%); }
.colour-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.colour-list button {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 8px 10px 10px; text-align: left;
  border-radius: 12px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.04);
}
.colour-list button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.08); }
.colour-list button[aria-checked="true"] { border-color: var(--accent); background: rgba(232, 17, 45, 0.1); box-shadow: inset 0 0 0 1px var(--accent); }
.colour-list button:disabled { cursor: default; }
.colour-list button.locked canvas, .colour-list button.locked .colour-name { opacity: 0.35; }
.colour-list canvas { width: 100%; height: 44px; display: block; }
.colour-name { font-size: 16px; font-weight: 700; }
.buy-panel { display: flex; flex-direction: column; gap: 8px; padding: 12px; border-radius: 12px; border: 1px solid var(--line-strong); background: rgba(255, 255, 255, 0.04); }
.colour-list small { min-height: 1.2em; font-size: 12px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }

/* ---------- menu on small screens ---------- */

@media (max-width: 760px) {
  .menu-overlay { align-items: stretch; padding: 0; }
  .menu {
    border-radius: 0; border: 0; max-height: none; height: 100%; overflow-y: auto;
    padding: max(14px, env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
  }
  .menu > * { flex-shrink: 0; }
  .menu-body { display: flex; flex-direction: column; gap: 16px; }
  .track-list { overflow: visible; }
  .detail { min-height: auto; }
  .detail-map { height: 140px; }
  .detail-name { font-size: 32px; }
  .detail-stats { grid-template-columns: auto auto; }
  .detail-stats div:last-child { grid-column: 1 / -1; }
  /* Start stays under the thumb while the list scrolls. */
  #startBtn {
    position: fixed;
    left: 16px;
    right: 16px;
    bottom: max(14px, env(safe-area-inset-bottom));
    z-index: 2;
    box-shadow: 0 -12px 28px 8px rgba(13, 15, 19, 0.95), 0 6px 20px rgba(232, 17, 45, 0.3);
  }
  .logo { font-size: 32px; }
  .icon-btn span { display: none; }
  .setting { flex-direction: column; align-items: stretch; gap: 8px; }
  .segmented { align-self: flex-start; }
}

@media (max-height: 780px) and (min-width: 761px) {
  .detail-map { height: 150px; }
  .detail-name { font-size: 34px; }
  .detail-blurb { margin-bottom: 4px; }
}

@media (max-height: 560px) and (min-width: 761px) {
  .menu { padding: 12px 16px; gap: 10px; }
  .detail-map { height: 120px; }
  .detail-name { font-size: 30px; }
  .detail-blurb { font-size: 15px; margin-bottom: 6px; }
  .track-card { padding: 5px 10px 5px 6px; }
  .track-card canvas { height: 38px; }
  .controls-help { display: none; }
  .detail { overflow-y: auto; }
}

/* ---------- fatal error ---------- */

.fatal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  text-align: center;
  font-size: 20px;
}
.fatal > div { max-width: 420px; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.fatal p { color: var(--muted); margin: 0; }
.fatal .primary { padding: 12px 32px; }

@media (prefers-reduced-motion: reduce) {
  .toast, .track-card, #touch button { transition: none; }
  .rev-fill.limit, .badge.flash { animation: none; }
}
