:root {
  --bg: #07080d;
  --panel: rgba(13, 16, 24, 0.92);
  --track: #242830;
  --track-edge: #e8edf5;
  --grass: #16281c;
  --text: #f6f7fb;
  --muted: #a9b0c1;
  --red: #ff233d;
  --purple: #b85cff;
  --green: #28e07a;
  --amber: #ffbf3d;
  --line: rgba(255, 255, 255, 0.15);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at top, #161927 0, var(--bg) 42rem);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

body {
  display: flex;
  justify-content: center;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--red);
  color: #fff;
  cursor: pointer;
  font: 800 0.9rem/1 Arial, Helvetica, sans-serif;
  letter-spacing: 0.04em;
  padding: 0.95rem 1.25rem;
  text-transform: uppercase;
}

button:hover,
button:focus-visible {
  background: #ff4058;
  outline: 2px solid #fff;
  outline-offset: 3px;
}

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

.game-shell {
  width: min(100%, 1420px);
  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-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  margin: 0 0 0.35rem;
  text-transform: uppercase;
}

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

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;
}

h3 {
  font-size: clamp(1.25rem, 2.5vw, 1.8rem);
  line-height: 1;
  margin: 0;
  text-transform: uppercase;
}

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

.header-times span,
.hud-grid > div {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.header-times span {
  font-family: "Courier New", monospace;
  font-weight: 700;
  padding: 0.55rem 0.7rem;
}

.game-stage {
  aspect-ratio: 16 / 9;
  background: #06080c;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  width: 100%;
}

canvas {
  display: block;
  height: 100%;
  touch-action: none;
  width: 100%;
}

.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: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
  max-height: 92%;
  max-width: 1160px;
  overflow: auto;
  padding: clamp(1rem, 3vw, 2rem);
  width: min(100%, 1160px);
}

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

.panel__header {
  display: grid;
  gap: 0.35rem;
  margin-bottom: 1.1rem;
  max-width: 760px;
}

.panel__header h2 {
  margin-bottom: 0;
}

.panel__header p:last-child {
  margin-bottom: 0;
}

.panel__body {
  align-items: start;
  display: grid;
  gap: 1rem;
  grid-template-columns: minmax(290px, 0.72fr) minmax(0, 1.28fr);
}

body.is-lights .panel__body,
body.is-finished .panel__body,
.panel__body:has(.race-briefing[hidden]) {
  grid-template-columns: minmax(340px, 540px);
  justify-content: center;
}

.setup-card {
  background: rgba(255, 255, 255, 0.055);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 1rem;
  padding: 0.9rem;
}

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

.setup-controls .track-picker:first-child,
.setup-controls .mode-toggle {
  grid-column: 1 / -1;
}

.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));
  margin: 0;
}

.track-picker {
  display: grid;
  gap: 0.45rem;
  margin: 0;
}

.track-picker span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.mode-toggle {
  align-items: center;
  background: rgba(255, 255, 255, 0.055);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  display: flex;
  gap: 0.65rem;
  font-size: 0.88rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  margin: 0;
  padding: 0.75rem;
  text-transform: uppercase;
}

.mode-toggle input {
  accent-color: var(--red);
  height: 1.1rem;
  width: 1.1rem;
}

.race-briefing {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: minmax(180px, 0.85fr) minmax(0, 1.15fr);
  margin: 0;
}

.track-preview,
.rival-card,
.objective-card {
  background: rgba(255, 255, 255, 0.055);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.track-preview {
  min-height: 210px;
  overflow: hidden;
  padding: 0.75rem;
}

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

.track-preview__meta {
  align-items: center;
  color: var(--muted);
  display: flex;
  font: 700 0.78rem/1.3 "Courier New", monospace;
  justify-content: space-between;
  margin-top: 0.45rem;
}

.rival-grid {
  display: grid;
  gap: 0.55rem;
}

.objective-card {
  border-color: rgba(184, 92, 255, 0.34);
  display: grid;
  gap: 0.55rem;
  padding: 0.75rem;
}

.objective-card .label {
  color: var(--purple);
  margin-bottom: 0;
}

.objective-card__row {
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  display: grid;
  gap: 0.2rem;
  padding-top: 0.55rem;
}

.objective-card__row strong {
  color: var(--text);
  font-size: 0.86rem;
  line-height: 1;
}

.objective-card__row span {
  color: var(--muted);
  font-family: "Courier New", monospace;
  font-size: 0.74rem;
}

.rival-card {
  display: grid;
  gap: 0.35rem;
  grid-template-columns: 0.35rem 1fr;
  padding: 0.6rem;
}

.rival-card__stripe {
  border-radius: 999px;
  grid-row: 1 / -1;
}

.rival-card strong {
  color: var(--text);
  font-size: 0.9rem;
  line-height: 1;
}

.rival-card span,
.rival-card small,
.points-rules {
  color: var(--muted);
  font-family: "Courier New", monospace;
  font-size: 0.74rem;
  line-height: 1.35;
}

.rival-card small {
  color: var(--amber);
}

.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: repeat(7, 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: "Courier New", monospace;
  font-size: clamp(0.78rem, 1.4vw, 0.98rem);
  font-weight: 700;
  margin-bottom: 0;
}

.touch-controls {
  display: none;
  gap: clamp(0.75rem, 7vw, 3rem);
  grid-template-columns: minmax(0, 0.62fr) minmax(0, 1fr);
  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;
  grid-template-columns: 1fr 1fr;
  pointer-events: auto;
}

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

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

.touch-btn--steer {
  background: rgba(35, 41, 54, 0.86);
  min-height: 58px;
  opacity: 0.64;
}

.touch-btn--go {
  background: rgba(40, 224, 122, 0.9);
  color: #06100a;
}

.touch-btn--brake {
  background: rgba(255, 191, 61, 0.9);
  color: #160f03;
}

.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;
  padding-top: 1rem;
}

.results table {
  border-collapse: collapse;
  width: 100%;
}

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

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

.trophy-panel {
  background: linear-gradient(135deg, rgba(255, 191, 61, 0.14), rgba(184, 92, 255, 0.1));
  border: 1px solid rgba(255, 191, 61, 0.28);
  border-radius: 8px;
  display: grid;
  gap: 0.8rem;
  padding: 1rem;
}

.trophy-panel .label {
  color: var(--amber);
  margin-bottom: 0;
}

.trophy-panel p {
  margin-bottom: 0;
}

.trophy-podium {
  display: grid;
  gap: 0.55rem;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.trophy-podium__place {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: grid;
  gap: 0.25rem;
  min-width: 0;
  padding: 0.7rem;
}

.trophy-podium__place span {
  color: var(--amber);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.trophy-podium__place strong,
.trophy-podium__place small {
  min-width: 0;
  overflow-wrap: anywhere;
}

.trophy-podium__place small {
  color: var(--muted);
  font-family: "Courier New", monospace;
}

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

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

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

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

  .panel {
    max-width: 760px;
  }

  body.is-lights .panel__body,
  body.is-finished .panel__body,
  .panel__body,
  .panel__body:has(.race-briefing[hidden]) {
    grid-template-columns: 1fr;
  }

  .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-grid,
  body.is-lights,
  body.is-racing {
    overflow: hidden;
  }

  .game-shell {
    min-height: 100svh;
  }

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

  .touch-controls {
    display: grid;
  }

  body.is-grid .game-stage,
  body.is-lights .game-stage,
  body.is-racing .game-stage {
    height: calc(100svh - 5.9rem);
    min-height: 430px;
  }

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

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

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

  .panel {
    max-height: 100%;
  }

  .setup-controls {
    grid-template-columns: 1fr;
  }
}

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

  .game-stage {
    height: min(62svh, 560px);
    min-height: 360px;
  }

  .game-header {
    gap: 0.65rem;
    margin-bottom: 0.55rem;
  }

  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;
    padding: 0.5rem 0.35rem;
    text-align: center;
  }

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

  .hud-grid > div {
    min-height: 58px;
    padding: 0.55rem;
  }

  .hud-grid > div:first-child,
  .hud-grid > div:nth-child(3),
  .hud-grid > div:nth-child(6) {
    display: none;
  }

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

  .touch-controls {
    gap: 0.65rem;
    grid-template-columns: 0.7fr 1fr;
    padding: 0 0.65rem max(0.7rem, env(safe-area-inset-bottom));
  }

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

  .touch-btn {
    font-size: 0.78rem;
    min-height: clamp(68px, 12svh, 92px);
    padding: 0.55rem;
  }

  .touch-btn--steer {
    min-height: clamp(56px, 10svh, 78px);
  }

  .panel {
    padding: 0.9rem;
  }

  .panel__header {
    margin-bottom: 0.75rem;
  }

  .panel h2 {
    font-size: clamp(1.35rem, 8vw, 1.9rem);
  }

  .panel p {
    font-size: 0.9rem;
    line-height: 1.42;
  }

  .race-briefing {
    gap: 0.6rem;
    grid-template-columns: 1fr;
  }

  .setup-card {
    gap: 0.75rem;
    padding: 0.75rem;
  }

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

  .track-preview {
    min-height: 175px;
  }

  .track-preview svg {
    height: 112px;
  }

  .results {
    overflow-x: auto;
  }

  .trophy-podium {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) and (orientation: landscape) {
  .game-header {
    align-items: center;
    flex-direction: row;
  }

  .game-header .eyebrow {
    display: none;
  }

  h1 {
    font-size: 1.35rem;
  }

  .header-times {
    display: flex;
    width: auto;
  }

  .header-times span {
    min-width: 5.2rem;
  }

  body.is-grid .game-stage,
  body.is-lights .game-stage,
  body.is-racing .game-stage {
    height: calc(100svh - 4.1rem);
    min-height: 300px;
  }

  .touch-btn {
    min-height: 54px;
  }

  .touch-btn--steer {
    min-height: 48px;
  }
}

/* ============================================================
   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-grid .game-header,
  body.is-lights .game-header,
  body.is-racing .game-header {
    display: none;
  }

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

  /* Canvas fills everything above the fixed control strip */
  body.is-grid .game-stage,
  body.is-lights .game-stage,
  body.is-racing .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-grid .touch-controls,
  body.is-lights .touch-controls,
  body.is-racing .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: 1fr 1fr;
    inset: auto;
    left: 0;
    padding: 0.45rem 0.45rem max(0.55rem, env(safe-area-inset-bottom));
    position: fixed;
    right: 0;
    z-index: 10;
  }

  /* Equal gaps inside each group */
  body.is-grid .touch-controls .touch-controls__steer,
  body.is-lights .touch-controls .touch-controls__steer,
  body.is-racing .touch-controls .touch-controls__steer,
  body.is-grid .touch-controls .touch-controls__pedals,
  body.is-lights .touch-controls .touch-controls__pedals,
  body.is-racing .touch-controls .touch-controls__pedals {
    gap: 0.4rem;
  }

  /* Bigger tap targets in portrait */
  .touch-btn {
    font-size: 0.92rem;
    font-weight: 900;
    letter-spacing: 0.04em;
    min-height: clamp(82px, 13svh, 108px);
    padding: 0.6rem 0.4rem;
  }

  .touch-btn--steer {
    min-height: clamp(74px, 12svh, 100px);
    opacity: 0.85;
  }
}
