:root {
  --race-ink: #141a1c;
  --race-paper: #f6f4e9;
  --race-accent: #e5f26a;
  --race-muted: #abb5b4;
  --race-line: rgba(246, 244, 233, 0.2);
  --race-panel: rgba(20, 26, 28, 0.9);
  --edge-x: max(24px, env(safe-area-inset-left), env(safe-area-inset-right));
  --edge-top: max(24px, env(safe-area-inset-top));
  --edge-bottom: max(24px, env(safe-area-inset-bottom));
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: "Chakra Petch", sans-serif;
  background: var(--race-ink);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  color: var(--race-paper);
}
button { font: inherit; touch-action: manipulation; }
button:focus-visible, a:focus-visible {
  outline: 3px solid var(--race-accent);
  outline-offset: 5px;
}
button:active { transform: translateY(2px); }
.hidden { display: none !important; }

.game-stage { position: fixed; inset: 0; overflow: hidden; }
/* Edge shading supports instruments without tinting the road ahead. */
.game-stage::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(rgba(6, 12, 14, 0.2), transparent 20%, transparent 80%, rgba(6, 12, 14, 0.24));
  pointer-events: none;
  z-index: 20;
}
#gameCanvas {
  display: block;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  border: 0;
  border-radius: 0;
  background: var(--race-ink);
  touch-action: none;
}

/* All instrument surfaces pass taps to the canvas; the home link opts in. */
.hud {
  position: fixed;
  inset: var(--edge-top) var(--edge-x) auto;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "left lane .";
  gap: 16px;
  align-items: start;
  pointer-events: none;
  z-index: 50;
}
.hud-left { grid-area: left; display: flex; align-items: flex-start; gap: 16px; }
.hud-back {
  flex-shrink: 0;
  pointer-events: auto;
}
.timer-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 152px;
  padding: 11px 16px 10px;
  background: var(--race-panel);
  border-left: 3px solid var(--race-accent);
  border-radius: 2px 6px 6px 2px;
  box-shadow: 0 4px 20px #0002;
}
.instrument-label, .countdown-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--race-muted);
}
#timer {
  font-size: 28px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}
.best-time { color: var(--race-accent); font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums; }
.game-info {
  grid-area: lane;
  justify-self: center;
  width: 260px;
  padding: 10px 16px 11px;
  background: var(--race-panel);
  border: 1px solid var(--race-line);
  border-radius: 6px;
  text-align: center;
  box-shadow: 0 4px 20px #0002;
}
#current-lane {
  display: inline-block;
  color: var(--race-paper);
  font-size: 19px;
  line-height: 1.3;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.route-ticks { display: flex; gap: 8px; margin-top: 8px; }
.route-ticks > span { display: flex; flex: 1; gap: 3px; }
.route-ticks i { flex: 1; height: 5px; background: #f6f4e933; border-radius: 1px; }
.route-ticks i.is-reached { background: var(--race-accent); }
.route-ticks i.is-current { background: var(--race-paper); box-shadow: 0 0 6px #e5f26a80; }
.route-stages { display: flex; gap: 8px; margin-top: 5px; }
.route-stages span { flex: 1; color: var(--race-muted); font-size: 9px; text-transform: uppercase; letter-spacing: 0.1em; }
.route-stages .is-active { color: var(--race-accent); }
.route-stages .is-complete { color: var(--race-paper); }
#current-lane.lane-flash { animation: lane-flash 0.45s ease-out; }
@keyframes lane-flash {
  0% { color: var(--race-accent); transform: translateY(-2px); }
  100% { color: var(--race-paper); transform: translateY(0); }
}

.sound-btn {
  position: fixed;
  top: var(--edge-top);
  right: var(--edge-x);
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  background: var(--race-panel);
  border: 1px solid var(--race-line);
  border-radius: 50%;
  color: var(--race-paper);
  cursor: pointer;
  z-index: 70;
  transition: color 0.16s, border-color 0.16s, background 0.16s;
  -webkit-user-select: none;
  user-select: none;
}
.sound-btn:hover { border-color: var(--race-accent); color: var(--race-accent); }
.sound-btn.is-muted { color: var(--race-muted); }
.sound-btn svg { width: 18px; height: 18px; }
.sound-btn .icon-sound-off, .sound-btn.is-muted .icon-sound-on { display: none; }
.sound-btn.is-muted .icon-sound-off { display: block; }

/* The scene itself is the title-screen artwork. */
.start-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 80px 24px;
  background: radial-gradient(ellipse at 50% 43%, rgba(13, 21, 23, 0.86), rgba(13, 21, 23, 0.42) 76%), linear-gradient(transparent 60%, #0d151799);
  cursor: pointer;
  z-index: 40;
}
.start-card { width: min(100%, 760px); text-align: center; animation: menu-arrive 0.5s ease-out both; }
.start-eyebrow, .result-eyebrow {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  color: var(--race-accent);
  text-transform: uppercase;
  letter-spacing: 0.22em;
  font-size: 11px;
  font-weight: 600;
}
.racing-mark { width: 28px; height: 12px; transform: skewX(-22deg); background: repeating-linear-gradient(90deg, var(--race-accent) 0 5px, transparent 5px 9px); }
.game-title {
  display: flex;
  justify-content: center;
  gap: 0.16em;
  margin: 20px 0 12px;
  font-size: clamp(64px, 9vw, 124px);
  font-weight: 700;
  font-style: italic;
  line-height: 0.95;
  text-transform: uppercase;
  text-shadow: 0 5px 0 #111a1c55;
}
.game-title span:last-child { color: var(--race-accent); }
.start-route {
  display: flex;
  justify-content: center;
  width: min(100%, 380px);
  margin: 32px auto 22px;
  padding: 14px 0;
  border-top: 1px solid var(--race-line);
  border-bottom: 1px solid var(--race-line);
}
.start-route span { flex: 1; color: var(--race-paper); font-size: 11px; text-transform: uppercase; letter-spacing: 0.12em; }
.start-route span + span { border-left: 1px solid var(--race-line); }
.start-route b { color: var(--race-accent); font-weight: 500; margin-right: 6px; }
.start-msg { font-size: 15px; color: #d1d8d3; line-height: 1.7; }
.start-cta, #restart-btn, .lane-change-btn {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 42px;
  min-width: 238px;
  min-height: 56px;
  margin-top: 26px;
  padding: 15px 24px;
  color: var(--race-ink);
  background: var(--race-accent);
  border: 1px solid var(--race-accent);
  border-radius: 4px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.16s, box-shadow 0.16s, transform 0.16s;
}
.start-cta span, #restart-btn span { font-size: 24px; line-height: 1; font-weight: 400; }
.start-cta:hover, #restart-btn:hover, .lane-change-btn:hover { background: #f1ffa0; box-shadow: 0 5px 28px #e5f26a20; }
.start-controls, .result-controls { display: flex; justify-content: center; align-items: center; gap: 9px; margin-top: 17px; color: #d1d8d3; font-size: 17px; }
kbd { display: inline-grid; place-items: center; width: 26px; height: 23px; border: 1px solid #f6f4e955; border-bottom-width: 3px; border-radius: 4px; font: 18px "Chakra Petch", sans-serif; line-height: 1; }
/* The key badge tracks the hint text; the HUD's own kbd keeps the base size. */
.start-controls kbd, .result-controls kbd { width: 31px; height: 28px; font-size: 22px; }
.game-stage:has(.start-overlay:not(.hidden)) .timer-container,
.game-stage:has(.start-overlay:not(.hidden)) .game-info,
.game-stage:has(.start-overlay:not(.hidden)) .speedo,
.game-stage:has(.start-overlay:not(.hidden)) .key-hint { visibility: hidden; }

.countdown-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(13, 21, 23, 0.42);
  pointer-events: none;
  z-index: 60;
}
.countdown-label { color: var(--race-paper); }
#countdown-text {
  font-size: clamp(100px, 16vw, 180px);
  font-weight: 700;
  font-style: italic;
  line-height: 1;
  color: var(--race-accent);
  text-shadow: 0 4px 28px #0005;
  animation: countdown-pulse 0.4s ease-out;
}
@keyframes countdown-pulse {
  0% { transform: scale(1.15); opacity: 0; }
  35%, 100% { transform: scale(1); opacity: 1; }
}
.tunnel-fade { position: fixed; inset: 0; background: #000; opacity: 0; z-index: 65; pointer-events: none; }

/* An instrument dial, with its original speed/needle mapping intact. */
.speedo {
  position: fixed;
  bottom: var(--edge-bottom);
  left: var(--edge-x);
  width: 112px;
  height: 124px;
  z-index: 50;
  pointer-events: none;
  -webkit-user-select: none;
  user-select: none;
  filter: drop-shadow(0 4px 12px #0005);
}
.speedo svg { display: block; width: 100%; height: auto; }
.speedo-bg { fill: var(--race-panel); stroke: #f6f4e940; stroke-width: 1; }
.speedo-track { fill: none; stroke: #f6f4e970; stroke-width: 3; stroke-linecap: round; }
.speedo-redzone { fill: none; stroke: var(--race-accent); stroke-width: 3; stroke-linecap: round; }
.speedo-ticks { stroke: #f6f4e9a0; stroke-width: 1.3; }
.speedo-needle { stroke: var(--race-accent); stroke-width: 2.6; stroke-linecap: round; }
.speedo-cap { fill: var(--race-paper); stroke: var(--race-ink); stroke-width: 1; }
.speedo-value { fill: var(--race-paper); font-family: "Chakra Petch", sans-serif; font-size: 20px; font-weight: 600; text-anchor: middle; paint-order: stroke; stroke: var(--race-ink); stroke-width: 3; stroke-linejoin: round; }
.speedo-unit { display: block; text-align: center; color: var(--race-paper); font-size: 9px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; }
.key-hint {
  position: fixed;
  right: var(--edge-x);
  bottom: var(--edge-bottom);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  background: var(--race-panel);
  border: 1px solid var(--race-line);
  border-radius: 6px;
  color: #c9d1c9;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  pointer-events: none;
  user-select: none;
  z-index: 50;
}
.key-hint kbd { color: var(--race-paper); width: 32px; height: 30px; font-size: 22px; }
.tap-hint {
  position: fixed;
  right: var(--edge-x);
  bottom: calc(var(--edge-bottom) + 68px);
  padding: 7px 12px;
  background: var(--race-accent);
  color: var(--race-ink);
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  pointer-events: none;
  z-index: 51;
  animation: tap-nudge 1.5s ease-in-out infinite;
}
@keyframes tap-nudge { 50% { transform: translateY(-4px); } }

/* Keep the result scrim click-through so home and sound remain reachable. */
.game-over {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 88px 24px 32px;
  background: radial-gradient(ellipse at center, #0d151ccc, #0d151c33);
  pointer-events: none;
  z-index: 1000;
}
.game-over > * { pointer-events: auto; }
.result-card {
  width: min(100%, 450px);
  padding: 34px 32px;
  background: rgba(20, 26, 28, 0.96);
  border: 1px solid var(--race-line);
  border-top: 3px solid #f38a73;
  border-radius: 6px;
  text-align: center;
  box-shadow: 0 24px 100px #0004;
  animation: menu-arrive 0.3s ease-out both;
}
.result-flag { width: 36px; height: 24px; margin: 0 auto 20px; background: repeating-conic-gradient(var(--race-paper) 0% 25%, transparent 0% 50%) 0 0 / 12px 12px; transform: skewY(-8deg); opacity: 0.8; }
.result-eyebrow { color: var(--race-muted); font-size: 9px; }
.game-over h2 { margin: 14px 0 24px; font-size: clamp(36px, 5vw, 52px); font-weight: 700; font-style: italic; line-height: 1.1; }
#final-score { padding-top: 20px; border-top: 1px solid var(--race-line); font-size: 20px; font-weight: 500; font-variant-numeric: tabular-nums; }
.progress-reached { margin-top: 9px; font-size: 13px; color: var(--race-muted); }
.progress-reached.is-best { color: var(--race-accent); }
.new-record { display: inline-block; margin-top: 18px; padding: 5px 10px; background: #e5f26a15; border: 1px solid #e5f26a55; border-radius: 3px; color: var(--race-accent); font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; }
#restart-btn { width: 100%; margin-top: 28px; }
.game-over[data-result="won"] .result-card { border-top-color: var(--race-accent); }
.game-over[data-result="won"] .result-flag { background-image: repeating-conic-gradient(var(--race-accent) 0% 25%, transparent 0% 50%); }
body:has(.game-over:not(.hidden)) .key-hint,
body:has(.game-over:not(.hidden)) .timer-container,
body:has(.game-over:not(.hidden)) .game-info,
body:has(.game-over:not(.hidden)) .speedo { visibility: hidden; }
@keyframes menu-arrive { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }

@media (max-width: 760px) {
  :root { --edge-x: max(14px, env(safe-area-inset-left), env(safe-area-inset-right)); --edge-top: max(14px, env(safe-area-inset-top)); --edge-bottom: max(14px, env(safe-area-inset-bottom)); }
  .hud { grid-template-columns: 1fr auto 1fr; gap: 10px; }
  .hud-left { gap: 0; }
  .timer-container { position: fixed; top: calc(var(--edge-top) + 78px); left: var(--edge-x); min-width: 116px; padding: 8px 11px; }
  #timer { font-size: 22px; }
  .instrument-label { font-size: 8px; }
  .best-time { font-size: 10px; }
  .game-info { width: 200px; padding: 8px 12px; }
  #current-lane { font-size: 17px; }
  .route-ticks { gap: 6px; margin-top: 6px; }
  .route-ticks > span { gap: 2px; }
  .route-stages { gap: 6px; }
  .route-stages span { font-size: 8px; }
  .game-title { font-size: clamp(55px, 12vw, 88px); margin-top: 22px; }
  .start-eyebrow { font-size: 9px; }
  .start-route { margin-top: 26px; }
  .start-msg { font-size: 14px; }
  .speedo { width: 88px; height: 102px; }
  .key-hint { padding: 10px 12px; gap: 10px; }
}
@media (max-width: 420px) {
  .game-info { width: 176px; padding-inline: 10px; }
  .route-ticks { gap: 5px; }
  .route-stages { gap: 5px; }
  .start-overlay { padding-inline: 22px; }
  .start-route { width: 290px; max-width: 100%; }
  .start-route span { font-size: 10px; }
  .result-card { padding: 28px 22px; }
}
@media (pointer: coarse) {
  .key-hint { display: none; }
  .tap-hint { bottom: var(--edge-bottom); }
}
@media (max-height: 560px) and (min-width: 600px) {
  :root { --edge-top: max(12px, env(safe-area-inset-top)); --edge-bottom: max(12px, env(safe-area-inset-bottom)); }
  .start-overlay { padding-block: 24px; }
  .start-card { max-width: 580px; }
  .game-title { font-size: 70px; margin: 12px 0 8px; }
  .start-route { margin: 16px auto 12px; padding: 10px 0; }
  .start-msg br { display: none; }
  .start-msg { font-size: 13px; }
  .start-cta { min-height: 46px; margin-top: 15px; padding-block: 10px; }
  .start-controls, .result-controls { margin-top: 10px; }
  .game-over { padding: 30px 90px; }
  .result-card { padding: 20px 28px; }
  .result-flag { display: none; }
  .game-over h2 { margin: 10px 0 16px; font-size: 36px; }
  #final-score { padding-top: 14px; }
  #restart-btn { min-height: 46px; margin-top: 18px; padding-block: 10px; }
  .new-record { margin-top: 10px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
