/**
 * Toon Grand Prix — shell styling.
 *
 * The frame the host gives us is the game's own 960×540 ratio, so every pixel
 * spent on chrome is a pixel taken from the circuit. The HUD and the control
 * bar are therefore deliberately shallow, and the canvas keeps its declared
 * ratio inside whatever is left (`aspect-ratio` + `max-width/max-height`), so
 * the artwork can never be stretched by the host frame.
 *
 * Everything clickable lives out here, not over the playfield: the contract in
 * docs/GAME_BUILDING_AGENT.md §2, and the reason a visitor never has to click
 * on top of a moving car to reach Pause.
 */

:root {
  color-scheme: dark;
  --ink: #241a3d;
  --ink-deep: #150e2a;
  --panel: #2b1f4d;
  --panel-2: #372a60;
  --line: #4b3a7d;
  --text: #fff8ec;
  --muted: #c3b6e6;
  --gold: #ffd166;
  --gold-dark: #a06a12;
  --coral: #ff5d73;
  --coral-dark: #a32c40;
  --mint: #7ef9c0;
  --cyan: #7ef9ff;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--ink-deep);
}

body {
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--panel) 0%, var(--ink-deep) 62%);
}

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

#hud {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: 1.5fr 0.7fr 0.8fr 0.9fr 1.2fr 1.4fr;
  gap: 2px;
  padding: 0;
  background: var(--ink-deep);
  border-bottom: 3px solid var(--gold);
}

.meter {
  min-width: 0;
  padding: 4px 7px 5px;
  background: var(--panel);
  text-align: center;
}

.meter span {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 8px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meter strong {
  display: block;
  overflow: hidden;
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.meter-nitro {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 3px;
  padding-bottom: 6px;
}

#nitroBar {
  position: relative;
  height: 9px;
  overflow: hidden;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: #1a1233;
}

#nitroFill {
  width: 100%;
  height: 100%;
  border-radius: 3px;
  background: linear-gradient(90deg, #7ef9ff, #4dabf7);
  transition: width 90ms linear;
}

/* --- Stage ---------------------------------------------------------------- */

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #0b0718;
}

canvas {
  display: block;
  width: auto;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  aspect-ratio: 960 / 540;
  background: #bfeaff;
  border-radius: 4px;
  outline: none;
  touch-action: none;
}

canvas:focus-visible {
  box-shadow: 0 0 0 3px var(--cyan);
}

/* Screen-reader race commentary; visually hidden, never display:none so the
   live region keeps announcing. */
#status {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

/* --- Control panel -------------------------------------------------------- */

#panel {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px 8px 7px;
  border-top: 3px solid var(--ink);
  background: rgba(29, 20, 56, 0.98);
}

#brief {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 30px;
}

#briefText {
  flex: 1 1 auto;
  min-width: 0;
  color: var(--muted);
  font-size: 10.5px;
  line-height: 1.32;
  text-align: left;
}

#briefText strong {
  display: block;
  overflow: hidden;
  color: var(--text);
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.01em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 0;
}

button {
  min-height: 30px;
  padding: 5px 11px;
  border: 2px solid var(--ink);
  border-radius: 9px;
  color: var(--text);
  background: var(--panel-2);
  box-shadow: 0 3px 0 #1b1236;
  font: inherit;
  font-size: 10.5px;
  font-weight: 900;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 60ms ease, box-shadow 60ms ease, background 120ms ease;
}

button:hover:not(:disabled) {
  background: #463480;
}

button:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 0 0 #1b1236;
}

button:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.4;
  cursor: default;
  box-shadow: none;
}

#primaryBtn {
  flex: 0 0 auto;
  min-width: 104px;
  border-color: #6d1c2c;
  color: #fff4f2;
  background: var(--coral);
  box-shadow: 0 3px 0 var(--coral-dark);
  font-size: 13px;
}

#primaryBtn:hover:not(:disabled) {
  background: #ff7c8d;
}

#trackSelect {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  flex-wrap: wrap;
}

#trackSelect[hidden] {
  display: none;
}

#trackSelect button {
  min-height: 26px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 9.5px;
  letter-spacing: 0.02em;
}

#trackSelect button[aria-pressed="true"] {
  border-color: var(--gold-dark);
  color: #2a1c07;
  background: var(--gold);
  box-shadow: 0 3px 0 var(--gold-dark);
}

.keyHint {
  flex: 1 1 auto;
  min-width: 90px;
  color: var(--muted);
  font-size: 9.5px;
  line-height: 1.3;
  text-align: center;
}

/* --- Touch controls ------------------------------------------------------- */

#touchPad {
  display: none;
  grid-template-columns: repeat(2, 48px);
  gap: 4px;
}

#touchPad button {
  min-height: 30px;
  padding: 2px;
  font-size: 10.5px;
}

#touchBoost {
  display: none;
  min-width: 62px;
  border-color: #1d5c63;
  color: #06282c;
  background: var(--cyan);
  box-shadow: 0 3px 0 #1d5c63;
}

@media (pointer: coarse) {
  #panel {
    gap: 4px;
    padding: 5px 6px 6px;
  }
  #touchPad {
    display: grid;
  }
  #touchBoost {
    display: inline-block;
    align-self: stretch;
  }
  .keyHint {
    display: none;
  }
  #controls {
    gap: 4px;
  }
  #controls > button:not(#touchBoost) {
    padding-inline: 7px;
  }
  #briefText {
    font-size: 10px;
  }
}

/* Short viewports: drop the meter captions before dropping the meters. */
@media (max-height: 520px) {
  .meter {
    padding-block: 2px;
  }
  .meter span {
    display: none;
  }
  #panel {
    padding-block: 4px;
  }
  button {
    min-height: 26px;
    padding-block: 3px;
  }
  #brief {
    min-height: 26px;
  }
}

@media (max-width: 560px) {
  #hud {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .meter-wide {
    grid-column: span 2;
  }
  /* Six meters in three columns leaves the nitro bar orphaned beside two empty
     cells; give it the row so the gauge reads as a gauge. */
  .meter-nitro {
    grid-column: span 3;
  }
}
