* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Chakra Petch", "Quicksand", sans-serif;
  background: #1e272e;
  height: 100vh;
  overflow: hidden;
  color: white;
}

button {
  font-family: "Chakra Petch", "Quicksand", sans-serif;
}

/* Full-screen 3D stage */
.game-stage {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

/* Heads-up display overlaid on the canvas. The container ignores pointer
   events so taps fall through to the canvas (lane change); only the logo
   captures clicks. */
.hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "left lane .";
  align-items: start;
  gap: 12px;
  padding: 14px 18px;
  /* NOTE: raising this above .game-over (1000) does nothing — .game-stage is
     position: fixed, which creates a stacking context, so every z-index in
     here is scoped to the stage and can't outrank a sibling of the stage.
     .game-over stays clickable-through via pointer-events instead. */
  z-index: 50;
  pointer-events: none;
}

.hud-logo {
  pointer-events: auto;
}

.hud-left {
  grid-area: left;
  justify-self: start;
  display: flex;
  align-items: center;
  gap: 14px;
}

.hud-logo {
  display: flex;
}

.hud-logo img {
  width: 38px;
  display: block;
}

.timer-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

#timer {
  font-size: 1.3rem;
  font-weight: 600;
  color: #fff;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.55);
}

.best-time {
  font-size: 0.9rem;
  color: #f7b733;
  font-weight: 600;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.55);
}

.game-info {
  grid-area: lane;
  justify-self: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 1.2rem;
  font-weight: 600;
}

/* Start-screen-only note under the lane pill. Mobile widths only — desktop
   players are already there — and the game hides it once play begins. */
.desktop-hint {
  display: none;
  margin-top: 5px;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.03em;
  color: rgba(234, 244, 255, 0.72);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}

@media (max-width: 768px) {
  .desktop-hint {
    display: inline-block;
  }
}

#current-lane {
  font-weight: 700;
  color: #eaf4ff;
  background: rgba(18, 26, 36, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.35);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  padding: 5px 14px;
  border-radius: 16px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  white-space: nowrap;
}

/* Brief pop + glow each time the player changes lane */
@keyframes lane-flash {
  0% {
    transform: scale(1.3);
    background: #3498db;
    border-color: #8fd0ff;
    color: #fff;
    box-shadow: 0 0 20px rgba(52, 152, 219, 0.95);
  }
  25% {
    transform: scale(1.16);
    background: #3498db;
    border-color: #8fd0ff;
    color: #fff;
    box-shadow: 0 0 16px rgba(52, 152, 219, 0.8);
  }
  100% {
    transform: scale(1);
    background: rgba(18, 26, 36, 0.5);
    border-color: rgba(255, 255, 255, 0.35);
    color: #eaf4ff;
    box-shadow: 0 0 0 rgba(52, 152, 219, 0);
  }
}

#current-lane.lane-flash {
  animation: lane-flash 0.6s ease-out;
}

#gameCanvas {
  display: block;
  width: 100vw;
  height: 100vh;
  border: none;
  border-radius: 0;
  background: #1e272e;
  touch-action: none;
}

/* Black wash that peaks while the camera is inside a tunnel, hiding the
   scene swap. Driven from JS via inline opacity. */
.tunnel-fade {
  position: fixed;
  inset: 0;
  background: #000;
  opacity: 0;
  z-index: 65;
  pointer-events: none;
}

.game-over {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  /* Soft gradient scrim: darkens the center column where the text sits while
     the crash / victory scene stays visible toward the edges. */
  background: radial-gradient(
    ellipse 70% 60% at 50% 45%,
    rgba(8, 13, 21, 0.75) 0%,
    rgba(8, 13, 21, 0.45) 55%,
    rgba(8, 13, 21, 0) 100%
  );
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  /* The scrim spans the whole viewport and fades to fully transparent at the
     edges, but a transparent element still captures taps — which left the HUD
     logo and the sound toggle dead on the crash / win screen. Raising their
     z-index can't fix it: they live inside .game-stage, and position: fixed
     makes that a stacking context, so nothing in it can outrank this sibling.
     So the scrim itself goes click-through and its content opts back in.
     Safe for the canvas underneath: its pointerdown handler is guarded by
     `gameRunning`, which is false while this screen is up. */
  pointer-events: none;
}

.game-over > * {
  pointer-events: auto;
}

.game-over h2 {
  font-size: 2.5rem;
  margin-bottom: 15px;
  font-weight: 600;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.9),
    0 2px 6px rgba(0, 0, 0, 0.8),
    0 4px 18px rgba(0, 0, 0, 0.7);
}

.game-over p {
  font-size: 1.2rem;
  margin-bottom: 25px;
  color: #dbe7f3;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.9),
    0 2px 10px rgba(0, 0, 0, 0.75);
}

.progress-reached {
  font-size: 1rem;
  color: #6dc4ff;
  font-weight: 500;
  margin-bottom: 10px;
}

/* Win screen: the line carries the best recorded time — it shares the amber
   chip design with the start screen's goal line (.start-goal). Crashes keep
   the plain "Reached lane X" look. */

.new-record {
  font-size: 1.3rem;
  color: #ffb63b;
  font-weight: 600;
  margin-top: 5px;
  margin-bottom: 20px;
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.8;
  }
}

#restart-btn {
  padding: 14px 35px;
  font-size: 1rem;
  font-weight: 600;
  background: #3498db;
  color: #fff;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  transition: all 0.15s ease;
}

#restart-btn:hover {
  background: #2980b9;
}

.hidden {
  display: none !important;
}

.countdown-overlay {
  position: fixed;
  inset: 0;
  background: rgba(30, 39, 46, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 60;
}

.start-overlay {
  position: fixed;
  inset: 0;
  background: rgba(18, 26, 36, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 40;
  cursor: pointer;
  padding: 24px;
}

.start-card {
  text-align: center;
  max-width: 85%;
}

.start-msg {
  font-size: 1.35rem;
  font-weight: 600;
  color: #fff;
  line-height: 1.5;
  margin-bottom: 10px;
}

/* Shared highlight chip: the start screen's goal line and the win screen's
   best time */
.start-goal,
.progress-reached.is-best {
  display: inline-block;
  font-size: 1.1rem;
  font-weight: 700;
  color: #ffd479;
  background: rgba(243, 156, 18, 0.14);
  border: 1px solid rgba(243, 156, 18, 0.45);
  border-radius: 999px;
  padding: 6px 16px;
}

.start-goal {
  margin-bottom: 18px;
}

.start-msg .arrow-key {
  display: inline-block;
  padding: 0 9px;
  border: 1.5px solid rgba(255, 255, 255, 0.7);
  border-radius: 6px;
  font-weight: 700;
  margin: 0 2px;
}

.start-cta {
  font-size: 1.05rem;
  font-weight: 600;
  color: #f39c12;
  text-transform: uppercase;
  letter-spacing: 1px;
  animation: pulse 1.2s ease-in-out infinite;
}

@media (max-width: 768px) {
  .start-msg {
    font-size: 1.1rem;
  }
  .start-goal,
  .progress-reached.is-best {
    font-size: 0.95rem;
  }
  .start-cta {
    font-size: 0.95rem;
  }
}

#countdown-text {
  font-size: 8rem;
  font-weight: 700;
  color: #fff;
  animation: countdown-pulse 0.4s ease-in-out;
}

@keyframes countdown-pulse {
  0% {
    transform: scale(0.5);
    opacity: 0;
  }
  50% {
    transform: scale(1.2);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@media (max-width: 768px) {
  #countdown-text {
    font-size: 5rem;
  }
}

/* Sound toggle pinned to the top-right, above the in-stage overlays so it
   stays reachable on the start screen. It also works on the crash / win
   screen, but via .game-over's pointer-events: none — not via this z-index,
   which can't escape the .game-stage stacking context. */
.sound-btn {
  position: fixed;
  top: 14px;
  right: 18px;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* The stage behind this button is a bright daytime sky, so the icon needs a
     dark chip to sit on. Don't dim it with `opacity` — that multiplies with the
     icon color and washes the whole control out against the haze. */
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 50%;
  color: #fff;
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
  z-index: 70;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  user-select: none;
  -webkit-user-select: none;
}
.sound-btn:hover {
  background: rgba(0, 0, 0, 0.68);
  border-color: rgba(255, 255, 255, 0.55);
}
/* Muted reads as "off" from the crossed-out icon plus a slightly softer
   stroke — never from near-invisibility */
.sound-btn.is-muted {
  color: rgba(255, 255, 255, 0.75);
  border-color: rgba(255, 255, 255, 0.22);
}
.sound-btn svg {
  width: 18px;
  height: 18px;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.55));
}
.sound-btn .icon-sound-off,
.sound-btn.is-muted .icon-sound-on {
  display: none;
}
.sound-btn.is-muted .icon-sound-off {
  display: block;
}

@media (max-width: 640px) {
  .sound-btn {
    top: 10px;
    right: 12px;
    width: 36px;
    height: 36px;
  }
}

/* Subtle keyboard hint pinned to the bottom-right; desktop only. */
.key-hint {
  position: fixed;
  bottom: 16px;
  right: 16px;
  width: 58px;
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  color: rgba(255, 255, 255, 0.22);
  font-size: 1.7rem;
  z-index: 50;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

@media (max-width: 640px) {
  .key-hint {
    display: none;
  }
}

/* Flashing "Tap" nudge that sits over the arrow hint while the player is
   still stuck in lane 1 — the one moment they may not know the control yet.
   It clears itself the instant the first lane change lands. */
.tap-hint {
  position: fixed;
  bottom: 84px;
  right: 16px;
  min-width: 58px;
  text-align: center;
  font-family: "Chakra Petch", sans-serif;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #fff;
  background: rgba(52, 152, 219, 0.85);
  border: 1px solid rgba(143, 208, 255, 0.9);
  border-radius: 14px;
  padding: 5px 12px;
  white-space: nowrap;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  z-index: 51;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  animation: tap-hint-flash 1s ease-in-out infinite;
}

@keyframes tap-hint-flash {
  0%,
  100% {
    opacity: 0.3;
    transform: translateY(0) scale(0.96);
    box-shadow: 0 0 0 rgba(52, 152, 219, 0);
  }
  50% {
    opacity: 1;
    transform: translateY(-3px) scale(1.05);
    box-shadow: 0 0 18px rgba(52, 152, 219, 0.75);
  }
}

/* The arrow hint is desktop-only, so on phones the nudge drops into its slot */
@media (max-width: 640px) {
  .tap-hint {
    bottom: 16px;
  }
}

/* Speedometer pinned to the bottom-left; the needle sweeps right as lane
   speeds climb. It reads over moving road and terrain, so it carries its own
   opaque dial face, a lit rim and a cast shadow rather than borrowing the
   near-transparent key-hint chrome — a dial you have to hunt for is useless
   at speed. */
.speedo {
  position: fixed;
  bottom: 16px;
  left: 16px;
  width: 96px;
  height: 96px;
  z-index: 50;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  filter: drop-shadow(0 3px 10px rgba(0, 0, 0, 0.55));
}
.speedo svg {
  width: 100%;
  height: 100%;
  display: block;
}
.speedo-bg      { fill: rgba(8, 12, 18, 0.78); stroke: rgba(255, 255, 255, 0.42); stroke-width: 1.5; }
.speedo-track   { fill: none; stroke: rgba(255, 255, 255, 0.5); stroke-width: 3.5; stroke-linecap: round; }
.speedo-redzone { fill: none; stroke: #ff4433; stroke-width: 3.5; stroke-linecap: round; }
.speedo-ticks   { stroke: rgba(255, 255, 255, 0.78); stroke-width: 1.8; }
.speedo-needle  {
  stroke: #ff5a4e;
  stroke-width: 3.4;
  stroke-linecap: round;
  filter: drop-shadow(0 0 3px rgba(255, 90, 78, 0.85));
}
.speedo-cap     { fill: #f2f6fb; stroke: rgba(0, 0, 0, 0.45); stroke-width: 0.8; }
.speedo-value   {
  fill: #fff;
  font-family: "Chakra Petch", sans-serif;
  font-size: 18px;
  font-weight: 700;
  text-anchor: middle;
  letter-spacing: 0.5px;
  /* Dark outline drawn behind the glyphs so the readout survives a bright
     tick or the needle passing under it */
  paint-order: stroke;
  stroke: rgba(0, 0, 0, 0.7);
  stroke-width: 3;
  stroke-linejoin: round;
}

@media (max-width: 640px) {
  .speedo {
    width: 74px;
    height: 74px;
    bottom: 12px;
    left: 12px;
  }
}

.lane-change-btn {
  margin-top: 20px;
  padding: 12px 28px;
  font-size: 1.1rem;
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 25px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  transition: background 0.1s, transform 0.1s;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}
.lane-change-btn:hover {
  background: linear-gradient(45deg, #764ba2, #667eea);
  transform: translateY(-2px);
}
.lane-change-btn:active {
  transform: translateY(0px);
  transition: transform 0.05s;
}

/* On narrow screens keep the desktop single-row grid (timer | lane | .) so
   the lane pill stays top-aligned with the timer; just tighten padding and
   sizes. The auto centre column and fr sides mean the pill nudges right
   instead of overlapping if the timer block needs the width. */
@media (max-width: 640px) {
  .hud {
    padding: 10px 12px;
  }

  .hud-left {
    gap: 10px;
  }

  .game-info {
    font-size: 1rem;
  }

  #timer {
    font-size: 1.1rem;
  }

  .hud-logo img {
    width: 32px;
  }
}
