:root {
  --bg: #0B0F12;
  --panel: rgba(20, 26, 31, 0.9);
  --text: #F2F4EF;
  --muted: #98A2A0;
  --accent: #C8FF2E;
  --red: #ff233d;
  --green: #28e07a;
  --amber: #F59E0B;
  --cyan: #58d1ff;
  --purple: #C084FC;
  --line: #2A343B;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at 20% 0, rgba(255, 35, 61, 0.12), transparent 24rem),
    radial-gradient(circle at 50% 0, rgba(200, 255, 46, 0.08), transparent 24rem),
    radial-gradient(circle at 80% 14%, rgba(88, 209, 255, 0.14), transparent 26rem),
    #0B0F12;
  color: var(--text);
  font-family: 'IBM Plex Sans', Arial, Helvetica, sans-serif;
  touch-action: pan-y;
}

body {
  display: flex;
  flex-direction: column;
}

button {
  border: 0;
  border-radius: 0;
  background: var(--accent);
  color: var(--bg);
  cursor: pointer;
  font: 800 0.9rem/1 'IBM Plex Mono', monospace;
  letter-spacing: 0.08em;
  padding: 0.95rem 1.25rem;
  text-transform: uppercase;
}

button:hover,
button:focus-visible {
  background: #d9ff6b;
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

button:disabled {
  cursor: wait;
  opacity: 0.64;
}

select {
  background: #0d1018;
  border: 1px solid var(--line);
  border-radius: 0;
  color: var(--text);
  font: 700 0.95rem/1 'IBM Plex Sans', Arial, Helvetica, sans-serif;
  padding: 0.75rem 0.8rem;
  width: 100%;
}

.game-shell {
  width: min(100%, 1420px);
  margin: 0 auto;
  padding: clamp(0.75rem, 2vw, 1.5rem);
}

.game-header {
  align-items: end;
  display: flex;
  gap: 1rem;
  justify-content: space-between;
  margin-bottom: 0.8rem;
}

.eyebrow,
.label {
  color: var(--muted);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  margin: 0 0 0.35rem;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1,
h2 {
  font-family: 'Oswald', sans-serif;
  font-weight: 700;
}

h1 {
  font-size: clamp(1.7rem, 4vw, 3.2rem);
  line-height: 0.94;
  margin-bottom: 0;
  text-transform: uppercase;
}

h2 {
  font-size: clamp(1.6rem, 3vw, 2.6rem);
  line-height: 1;
  margin-bottom: 0.8rem;
  text-transform: uppercase;
}

.header-times {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: flex-end;
}

.header-times span,
.hud-grid > div {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.085), rgba(255, 255, 255, 0.02)),
    rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
  border-radius: 0;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.header-times span {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 700;
  min-width: 6.5rem;
  padding: 0.55rem 0.7rem;
  text-align: center;
}

.game-stage {
  aspect-ratio: 16 / 9;
  background:
    radial-gradient(circle at 18% 20%, rgba(255, 35, 61, 0.16), transparent 18rem),
    radial-gradient(circle at 82% 16%, rgba(88, 209, 255, 0.16), transparent 22rem),
    linear-gradient(180deg, #10151f, #05070b);
  border: 1px solid rgba(88, 209, 255, 0.26);
  border-radius: 0;
  box-shadow:
    0 22px 70px rgba(0, 0, 0, 0.42),
    inset 0 0 0 1px rgba(255, 255, 255, 0.035);
  overflow: hidden;
  position: relative;
  width: 100%;
}

.game-stage::before {
  background:
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.04) 0 1px, transparent 1px 5px),
    linear-gradient(90deg, rgba(255, 35, 61, 0.08), transparent 34%, rgba(88, 209, 255, 0.08));
  content: "";
  inset: 0;
  opacity: 0.25;
  pointer-events: none;
  position: absolute;
  z-index: 2;
}

.game-stage::after {
  background:
    radial-gradient(circle at 50% 35%, transparent 22%, rgba(0, 0, 0, 0.28) 82%),
    linear-gradient(90deg, rgba(5, 7, 11, 0.58), transparent 20%, transparent 80%, rgba(5, 7, 11, 0.58));
  content: "";
  inset: 0;
  pointer-events: none;
  position: absolute;
  z-index: 3;
}

canvas {
  display: block;
  height: 100%;
  filter: saturate(1.14) contrast(1.04);
  position: relative;
  touch-action: none;
  width: 100%;
  z-index: 1;
}

.overlay {
  align-items: center;
  background: rgba(2, 4, 8, 0.68);
  display: flex;
  inset: 0;
  justify-content: center;
  padding: 1rem;
  position: absolute;
  z-index: 8;
}

.overlay.is-hidden {
  display: none;
}

.panel {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.075), rgba(255, 255, 255, 0.015)),
    var(--panel);
  border: 1px solid rgba(88, 209, 255, 0.22);
  border-radius: 0;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
  max-width: 780px;
  padding: clamp(1rem, 3vw, 2rem);
  width: min(100%, 780px);
}

.panel p {
  color: var(--muted);
  line-height: 1.55;
}

.panel__header {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 1.1rem;
}

.setup-card,
.car-card,
.briefing-card {
  background:
    radial-gradient(circle at 88% 12%, rgba(88, 209, 255, 0.09), transparent 5rem),
    rgba(255, 255, 255, 0.055);
  border: 1px solid var(--line);
  border-radius: 0;
}

.setup-card {
  display: grid;
  gap: 1rem;
  padding: 0.9rem;
}

.setup-controls {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.setup-field {
  display: grid;
  gap: 0.45rem;
}

.setup-field span {
  color: var(--muted);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.track-preview {
  background:
    radial-gradient(circle at 30% 40%, rgba(184, 92, 255, 0.1), transparent 7rem),
    rgba(255, 255, 255, 0.055);
  border: 1px solid var(--line);
  border-radius: 0;
  min-height: 190px;
  overflow: hidden;
  padding: 0.75rem;
}

.track-preview svg {
  display: block;
  height: 170px;
  width: 100%;
}

.briefing-grid {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.briefing-card {
  display: grid;
  gap: 0.25rem;
  padding: 0.8rem;
}

.briefing-card strong {
  display: block;
  font-family: 'Oswald', sans-serif;
  font-weight: 700;
  font-size: 0.98rem;
  line-height: 1.15;
  text-transform: uppercase;
}

.briefing-card span {
  color: var(--muted);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.76rem;
  line-height: 1.35;
}

.car-card {
  align-items: center;
  display: grid;
  gap: 0.85rem;
  grid-template-columns: 4.6rem 1fr;
  padding: 0.8rem;
}

.car-card strong {
  display: block;
  font-family: 'Oswald', sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
  margin-bottom: 0.25rem;
  text-transform: uppercase;
}

.car-card span {
  color: var(--muted);
  display: block;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.78rem;
  line-height: 1.35;
}

.car-card__swatch {
  aspect-ratio: 1.55;
  background:
    linear-gradient(90deg, transparent 0 14%, #f6f7fb 14% 22%, transparent 22% 100%),
    linear-gradient(135deg, #ff233d, #58d1ff);
  border-radius: 0;
  box-shadow:
    0 0 24px rgba(255, 35, 61, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.22);
}

.panel__actions {
  display: grid;
  gap: 0.8rem;
}

.panel__actions button {
  min-height: 3.1rem;
  width: 100%;
}

.start-lights {
  display: grid;
  gap: 0.55rem;
  grid-template-columns: repeat(5, minmax(2.5rem, 1fr));
}

.start-lights span {
  aspect-ratio: 1;
  background: #24060b;
  border: 3px solid #4a111a;
  border-radius: 50%;
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.55);
}

.start-lights span.is-on {
  background: var(--red);
  box-shadow: 0 0 22px rgba(255, 35, 61, 0.9), inset 0 0 8px #fff;
}

.hud-grid {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: 1.45fr repeat(4, minmax(0, 1fr));
  margin-top: 0.8rem;
}

.hud-grid > div {
  min-height: 76px;
  padding: 0.75rem;
}

.hud-grid p:last-child {
  color: var(--text);
  font-family: 'IBM Plex Mono', monospace;
  font-size: clamp(0.78rem, 1.4vw, 0.98rem);
  font-weight: 700;
  margin-bottom: 0;
}

body.is-lights,
body.is-running {
  height: 100svh;
  overflow: hidden;
}

body.is-lights .nav,
body.is-running .nav,
body.is-lights .game-header,
body.is-running .game-header,
body.is-lights .hud-grid,
body.is-running .hud-grid,
body.is-lights .footer,
body.is-running .footer {
  display: none;
}

body.is-lights .game-shell,
body.is-running .game-shell {
  height: 100svh;
  max-width: none;
  padding: 0;
  width: 100%;
}

body.is-lights .game-stage,
body.is-running .game-stage {
  aspect-ratio: auto;
  border: 0;
  height: 100svh;
  min-height: 0;
}

.touch-controls {
  display: none;
  gap: 0.75rem;
  grid-template-columns: 0.7fr 1.3fr;
  inset: auto 0 0;
  padding: 0 0.75rem max(0.75rem, env(safe-area-inset-bottom));
  pointer-events: none;
  position: absolute;
  touch-action: none;
  user-select: none;
  z-index: 5;
}

.touch-controls__steer,
.touch-controls__pedals {
  display: grid;
  gap: 0.65rem;
  pointer-events: auto;
}

.touch-controls__steer {
  grid-template-columns: 1fr 1fr;
}

.touch-controls__pedals {
  grid-template-columns: 0.9fr 1fr 1.15fr;
}

.touch-btn {
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow:
    0 10px 26px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  min-height: 68px;
  opacity: 0.9;
  padding: 0.65rem;
  touch-action: none;
  user-select: none;
}

.touch-btn--steer {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.08), transparent),
    rgba(35, 41, 54, 0.86);
  opacity: 0.72;
}

.touch-btn--go {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.2), transparent),
    rgba(40, 224, 122, 0.9);
  color: #06100a;
}

.touch-btn--brake,
.touch-btn--handbrake {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.2), transparent),
    rgba(255, 191, 61, 0.9);
  color: #160f03;
}

.touch-btn--handbrake {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.2), transparent),
    rgba(184, 92, 255, 0.9);
  color: #fff;
}

.touch-btn.is-pressed {
  filter: brightness(1.2);
  transform: translateY(1px);
}

.results {
  border-top: 1px solid var(--line);
  color: var(--text);
  display: grid;
  gap: 0.7rem;
  margin-top: 1.25rem;
  overflow-x: auto;
  padding-top: 1rem;
}

.results table {
  background: rgba(0, 0, 0, 0.12);
  border-collapse: collapse;
  width: 100%;
}

.results th,
.results td {
  border-bottom: 1px solid var(--line);
  font-family: 'IBM Plex Mono', monospace;
  padding: 0.45rem 0.3rem;
  text-align: left;
}

.results th {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
}

.green {
  color: var(--green);
}

.amber {
  color: var(--amber);
}

@media (max-width: 920px) {
  .game-header {
    align-items: start;
    flex-direction: column;
  }

  .hud-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (pointer: coarse), (max-width: 760px) {
  body {
    overflow-x: hidden;
    overscroll-behavior: none;
  }

  body.is-lights,
  body.is-running {
    overflow: hidden;
  }

  .game-stage {
    aspect-ratio: auto;
    height: min(66svh, 600px);
    min-height: 390px;
  }

  body.is-lights .game-stage,
  body.is-running .game-stage {
    height: 100svh;
    min-height: 0;
  }

  .touch-controls {
    display: grid;
  }

  body.is-menu .touch-controls,
  body.is-finished .touch-controls {
    display: none;
  }

  body.is-lights .hud-grid,
  body.is-running .hud-grid {
    display: none;
  }

  .overlay {
    align-items: stretch;
    padding: 0.65rem;
  }

  .panel {
    max-height: 100%;
    overflow: auto;
  }
}

@media (max-width: 560px) {
  .game-shell {
    padding: 0.5rem;
  }

  h1 {
    font-size: clamp(1.35rem, 8vw, 2rem);
  }

  .header-times {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
  }

  .header-times span {
    font-size: 0.78rem;
    min-width: 0;
    padding: 0.5rem 0.35rem;
  }

  .setup-controls,
  .briefing-grid,
  .car-card {
    grid-template-columns: 1fr;
  }

  .hud-grid > div:first-child {
    grid-column: 1 / -1;
  }

  .touch-controls {
    gap: 0.45rem;
    grid-template-columns: 0.72fr 1.28fr;
    padding: 0 0.45rem max(0.55rem, env(safe-area-inset-bottom));
  }

  .touch-controls__steer,
  .touch-controls__pedals {
    gap: 0.4rem;
  }

  .touch-btn {
    font-size: 0.72rem;
    min-height: clamp(64px, 11svh, 90px);
  }
}

/* ============================================================
   PORTRAIT PHONE - active driving only
   Controls move below the canvas so they never overlap the track.
   ============================================================ */
@media (pointer: coarse) and (orientation: portrait) {
  /* Hide the header during active play - the on-canvas HUD covers it */
  body.is-lights .game-header,
  body.is-running .game-header {
    display: none;
  }

  /* Full-bleed shell so no padding wastes screen space */
  body.is-lights .game-shell,
  body.is-running .game-shell {
    padding: 0;
  }

  /* Canvas fills everything above the fixed control strip */
  body.is-lights .game-stage,
  body.is-running .game-stage {
    border-left: none;
    border-radius: 0;
    border-right: none;
    border-top: none;
    height: calc(100svh - 108px - max(0px, env(safe-area-inset-bottom)));
    min-height: 320px;
  }

  /* Fixed strip at the viewport bottom - no canvas overlap */
  body.is-lights .touch-controls,
  body.is-running .touch-controls {
    background: rgba(7, 8, 13, 0.97);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    bottom: 0;
    display: grid;
    gap: 0.4rem;
    grid-template-columns: 0.72fr 1.28fr;
    inset: auto;
    left: 0;
    padding: 0.45rem 0.45rem max(0.55rem, env(safe-area-inset-bottom));
    position: fixed;
    right: 0;
    z-index: 10;
  }

  body.is-lights .touch-controls .touch-controls__steer,
  body.is-running .touch-controls .touch-controls__steer,
  body.is-lights .touch-controls .touch-controls__pedals,
  body.is-running .touch-controls .touch-controls__pedals {
    gap: 0.4rem;
  }

  /* Bigger tap targets in portrait */
  .touch-btn {
    font-size: 0.86rem;
    font-weight: 900;
    letter-spacing: 0.03em;
    min-height: clamp(78px, 13svh, 104px);
    padding: 0.6rem 0.3rem;
  }
}
