:root {
  color-scheme: dark;
  --ink: #050814;
  --night: #080b24;
  --navy: #101a38;
  --panel: #111a33;
  --panel-light: #1a2949;
  --teal: #18d4bd;
  --teal-light: #79f4e5;
  --gold: #f0bd5a;
  --white: #f4f7ef;
  --muted: #9ab0c8;
  --danger: #f25a59;
  --track: #923f47;
  --pixel-line: #2e4667;
  --pixel-shadow: #02040c;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--ink);
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  color: var(--white);
  background:
    linear-gradient(rgba(24, 212, 189, 0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(24, 212, 189, 0.025) 1px, transparent 1px),
    var(--night);
  background-size: 12px 12px;
  font-family: "Courier New", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

button,
select {
  font: inherit;
}

button {
  touch-action: manipulation;
}

.sprite-asset {
  display: none;
}

.app-shell {
  width: min(1460px, 100%);
  margin: 0 auto;
  padding: 14px clamp(10px, 2.2vw, 28px) 30px;
}

.brand-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 70px;
  margin-bottom: 12px;
  border: 3px solid var(--pixel-line);
  padding: 8px 12px;
  background: var(--panel);
  box-shadow: 5px 5px 0 var(--pixel-shadow);
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.brand-mark {
  width: 52px;
  height: 52px;
  border: 3px solid var(--teal);
  object-fit: cover;
  image-rendering: pixelated;
  box-shadow: 3px 3px 0 #071510;
}

.brand-kicker,
.eyebrow {
  margin: 0 0 4px;
  color: var(--teal-light);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.brand-bar h1 {
  margin: 0;
  overflow: hidden;
  color: var(--white);
  font-size: clamp(1.35rem, 3vw, 2rem);
  font-weight: 900;
  letter-spacing: 0.02em;
  line-height: 1;
  text-overflow: ellipsis;
  text-shadow: 3px 3px 0 #233655;
  text-transform: uppercase;
  white-space: nowrap;
}

.race-state {
  flex: 0 0 auto;
  border: 2px solid var(--gold);
  padding: 8px 10px;
  color: var(--gold);
  background: var(--ink);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  box-shadow: 3px 3px 0 #231a10;
}

.header-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 9px;
}

.sound-toggle {
  display: inline-flex;
  min-width: 112px;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 2px solid var(--teal);
  padding: 7px 10px;
  color: var(--teal-light);
  background: #071522;
  box-shadow: 3px 3px 0 #02100f;
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.sound-toggle:hover,
.sound-toggle:focus-visible {
  color: var(--ink);
  background: var(--teal-light);
  outline: none;
}

.sound-toggle[aria-pressed="false"] {
  border-color: var(--muted);
  color: var(--muted);
  background: #0b1020;
}

.sound-icon {
  font-family: Arial, sans-serif;
  font-size: 1.05rem;
  line-height: 1;
}

.race-console {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  gap: 14px;
  align-items: start;
}

.track-column {
  min-width: 0;
}

.metric-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  overflow: hidden;
  border: 3px solid var(--pixel-line);
  border-bottom: 0;
  background: #0b1228;
  box-shadow: 5px 0 0 var(--pixel-shadow);
}

.metric {
  min-width: 0;
  padding: 10px 13px;
  border-right: 2px solid var(--pixel-line);
}

.metric:last-child {
  border-right: 0;
}

.metric span,
.energy-heading span,
.result-grid span,
.pacing-read > span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.metric strong {
  display: block;
  overflow: hidden;
  color: var(--white);
  font-size: clamp(1rem, 2.2vw, 1.4rem);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  text-overflow: ellipsis;
  text-shadow: 2px 2px 0 #1c3450;
  white-space: nowrap;
}

.metric-main strong {
  color: var(--teal-light);
}

.track-stage {
  position: relative;
  min-height: 480px;
  overflow: hidden;
  border: 3px solid var(--pixel-line);
  background: #080b24;
  box-shadow: 5px 5px 0 var(--pixel-shadow);
  isolation: isolate;
}

#raceCanvas {
  display: block;
  width: 100%;
  height: clamp(480px, 54vw, 680px);
  min-height: 480px;
  image-rendering: auto;
}

.start-panel {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  align-content: center;
  width: min(620px, calc(100% - 30px));
  height: fit-content;
  margin: auto;
  border: 4px solid var(--teal);
  padding: clamp(20px, 4vw, 34px);
  background: #0c1730;
  box-shadow:
    7px 7px 0 #02040c,
    inset 0 0 0 3px #223a5a;
}

.start-panel::before {
  content: "";
  position: absolute;
  inset: 7px;
  z-index: -1;
  border: 1px dashed rgba(121, 244, 229, 0.25);
  pointer-events: none;
}

.start-panel[hidden] {
  display: none;
}

.start-panel h2 {
  margin: 0;
  color: var(--white);
  font-size: clamp(2.2rem, 7vw, 4.6rem);
  font-weight: 900;
  letter-spacing: -0.04em;
  line-height: 0.9;
  text-shadow:
    4px 0 0 #294978,
    0 4px 0 #294978,
    4px 4px 0 #050814;
  text-transform: uppercase;
}

.start-copy {
  max-width: 54ch;
  margin: 16px 0 18px;
  color: #cad6e3;
  font-size: 1rem;
  line-height: 1.5;
}

.setup-grid {
  display: grid;
  grid-template-columns: 0.85fr 1.15fr;
  gap: 10px;
}

.setup-grid label span {
  display: block;
  margin: 0 0 7px;
  color: var(--gold);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

select {
  width: 100%;
  min-height: 48px;
  border: 3px solid #365476;
  border-radius: 0;
  padding: 0 34px 0 11px;
  color: var(--white);
  background: #071122;
  font-size: 1rem;
  font-weight: 800;
  box-shadow: inset 3px 3px 0 #02040c;
}

select:focus-visible,
button:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 3px;
}

.field-timing {
  min-height: 20px;
  margin: 10px 0 12px;
  color: var(--teal-light);
  font-size: 0.8rem;
  font-weight: 800;
  line-height: 1.4;
}

.primary-button,
.secondary-button {
  min-height: 50px;
  border-radius: 0;
  padding: 0 18px;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
}

.primary-button {
  border: 3px solid #b6fff3;
  color: #02110f;
  background: var(--teal);
  box-shadow:
    4px 4px 0 #055d55,
    inset 2px 2px 0 #79f4e5;
}

.primary-button:hover {
  background: #4de7d5;
}

.primary-button:active,
.secondary-button:active,
.pace-button:active,
.kick-button:active {
  transform: translate(3px, 3px);
  box-shadow: none;
}

.secondary-button {
  border: 3px solid var(--pixel-line);
  color: var(--white);
  background: #111a33;
  box-shadow: 4px 4px 0 #02040c;
}

.key-hint {
  margin: 13px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
}

.track-callout {
  position: absolute;
  left: 14px;
  bottom: 14px;
  z-index: 2;
  max-width: min(52%, 520px);
  border: 2px solid var(--white);
  padding: 7px 10px;
  color: var(--white);
  background: #050814;
  font-size: 0.75rem;
  font-weight: 900;
  line-height: 1.3;
  text-align: center;
  box-shadow: 3px 3px 0 rgba(0, 0, 0, 0.7);
}

.control-deck {
  display: grid;
  grid-template-columns: minmax(220px, 0.64fr) minmax(420px, 1.36fr);
  gap: 10px;
  border: 3px solid var(--pixel-line);
  border-top: 0;
  padding: 10px;
  background: #0b1228;
  box-shadow: 5px 5px 0 var(--pixel-shadow);
}

.energy-panel {
  border: 2px solid #365476;
  padding: 10px;
  background: #071122;
  box-shadow: inset 3px 3px 0 #02040c;
}

.energy-heading,
.energy-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.energy-heading strong {
  color: var(--teal-light);
  font-size: 1.2rem;
  font-variant-numeric: tabular-nums;
  text-shadow: 2px 2px 0 #173e47;
}

.energy-track {
  height: 15px;
  margin: 10px 0 8px;
  overflow: hidden;
  border: 3px solid #02040c;
  background:
    repeating-linear-gradient(90deg, #141a2b 0 10px, #0a0d18 10px 12px);
}

.energy-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #0f8f80, var(--teal-light));
  transition: width 120ms linear;
}

.energy-meta {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 800;
}

.button-bank {
  display: grid;
  grid-template-columns: repeat(3, 1fr) minmax(150px, 1.16fr);
  gap: 7px;
}

.pace-button,
.kick-button {
  position: relative;
  display: grid;
  align-content: center;
  min-height: 86px;
  border: 3px solid #365476;
  border-radius: 0;
  padding: 9px;
  color: var(--white);
  background: #15213d;
  cursor: pointer;
  box-shadow:
    4px 4px 0 #02040c,
    inset 2px 2px 0 #29456b;
}

.pace-button:hover:not(:disabled),
.pace-button.active {
  border-color: var(--teal);
  color: var(--teal-light);
  background: #12343e;
}

.pace-button.active {
  box-shadow:
    4px 4px 0 #02040c,
    inset 0 0 0 2px #18d4bd;
}

.pace-button:disabled,
.kick-button:disabled {
  cursor: not-allowed;
  filter: grayscale(0.7);
  opacity: 0.5;
}

.pace-button strong,
.kick-button strong {
  font-size: 0.95rem;
  font-weight: 900;
  text-transform: uppercase;
}

.pace-button small,
.kick-button small {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.75rem;
}

.button-key,
.kick-lock {
  position: absolute;
  top: 6px;
  right: 7px;
  font-size: 0.75rem;
  font-weight: 900;
}

.button-key {
  color: #6984a3;
}

.kick-button {
  border-color: #9b7037;
  color: #ffe0a0;
  background: #3a2731;
  box-shadow:
    4px 4px 0 #120b10,
    inset 2px 2px 0 #71474b;
}

.kick-button.ready {
  border-color: var(--gold);
  background: #5a3936;
  animation: readyPulse 0.7s steps(2, end) infinite;
}

.kick-lock {
  color: var(--gold);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.race-sidebar {
  display: grid;
  gap: 12px;
}

.panel {
  overflow: hidden;
  border: 3px solid var(--pixel-line);
  padding: 14px;
  background: var(--panel);
  box-shadow: 5px 5px 0 var(--pixel-shadow);
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 11px;
  padding-bottom: 9px;
  border-bottom: 2px dashed #334a68;
}

.panel h2 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.lap-badge {
  border: 2px solid var(--teal);
  padding: 6px 8px;
  color: var(--teal-light);
  background: #061924;
  font-size: 0.75rem;
  font-weight: 900;
  box-shadow: 2px 2px 0 #02040c;
}

.leaderboard {
  display: grid;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
  counter-reset: place;
}

.leaderboard li {
  counter-increment: place;
  display: grid;
  grid-template-columns: 22px 12px minmax(0, 1fr) auto;
  align-items: center;
  gap: 7px;
  min-height: 38px;
  border: 1px solid #293b58;
  padding: 5px 7px;
  color: #d4e0e9;
  background: #0a1226;
}

.leaderboard li::before {
  content: counter(place);
  color: var(--gold);
  font-size: 0.75rem;
  font-weight: 900;
}

.leaderboard li.player-row {
  border-color: var(--teal);
  color: var(--white);
  background: #0b3035;
  box-shadow: inset 4px 0 0 var(--teal);
}

.runner-dot {
  width: 9px;
  height: 9px;
  border: 2px solid #050814;
  background: var(--runner-color);
}

.runner-name {
  overflow: hidden;
  font-size: 0.8rem;
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.runner-gap {
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

.coach-panel {
  border-color: #8b6537;
}

.coach-panel blockquote {
  margin: 8px 0 1px;
  color: #e0e9ed;
  font-size: 0.94rem;
  font-weight: 700;
  line-height: 1.5;
}

.rules-panel ul {
  display: grid;
  gap: 10px;
  margin: 10px 0 0;
  padding: 0;
  color: #aebfd0;
  font-size: 0.82rem;
  line-height: 1.45;
  list-style: none;
}

.rules-panel li span {
  display: inline-block;
  min-width: 58px;
  color: var(--gold);
  font-weight: 900;
}

.pro-note {
  margin: 14px 0 0;
  padding-top: 11px;
  border-top: 2px dashed #334a68;
  color: #849cb4;
  font-size: 0.75rem;
  line-height: 1.45;
}

.result-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(2, 4, 12, 0.9);
}

.result-backdrop[hidden] {
  display: none;
}

.result-card {
  width: min(620px, 100%);
  max-height: calc(100dvh - 36px);
  overflow: auto;
  border: 4px solid var(--teal);
  padding: clamp(22px, 5vw, 36px);
  background: #0c1730;
  box-shadow:
    8px 8px 0 #02040c,
    inset 0 0 0 3px #243d5e;
}

.result-card h2 {
  margin: 0 0 18px;
  color: var(--white);
  font-size: clamp(2rem, 7vw, 4rem);
  font-weight: 900;
  letter-spacing: -0.03em;
  line-height: 0.95;
  text-shadow: 4px 4px 0 #294978;
  text-transform: uppercase;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  overflow: hidden;
  border: 3px solid var(--pixel-line);
}

.result-grid > div {
  min-width: 0;
  padding: 12px;
  border-right: 2px solid var(--pixel-line);
  background: #081122;
}

.result-grid > div:last-child {
  border-right: 0;
}

.result-grid strong {
  font-size: clamp(1rem, 3vw, 1.3rem);
  font-variant-numeric: tabular-nums;
}

.pacing-read {
  margin: 14px 0;
  border: 2px dashed #456080;
  padding: 14px;
  background: #101d38;
}

.pacing-read strong {
  display: block;
  color: var(--teal-light);
  font-size: 1.05rem;
}

.pacing-read p {
  margin: 7px 0 0;
  color: #b9cad6;
  line-height: 1.5;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@keyframes readyPulse {
  0%,
  100% {
    filter: brightness(1);
  }
  50% {
    filter: brightness(1.35);
  }
}

@media (max-width: 1080px) {
  .race-console {
    grid-template-columns: 1fr;
  }

  .race-sidebar {
    grid-template-columns: 1.25fr 1fr 1fr;
  }
}

@media (max-width: 760px) {
  .app-shell {
    padding: 8px 8px calc(18px + env(safe-area-inset-bottom));
  }

  .brand-bar {
    min-height: 58px;
    margin-bottom: 8px;
    border-width: 2px;
    padding: 6px 8px;
    box-shadow: 3px 3px 0 var(--pixel-shadow);
  }

  .brand-mark {
    width: 44px;
    height: 44px;
    border-width: 2px;
  }

  .brand-kicker {
    font-size: 0.75rem;
    letter-spacing: 0.05em;
  }

  .brand-bar h1 {
    font-size: 1.2rem;
  }

  .race-state {
    max-width: 92px;
    overflow: hidden;
    border-width: 2px;
    padding: 6px 7px;
    font-size: 0.75rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-actions {
    gap: 6px;
  }

  .sound-toggle {
    min-width: 42px;
    min-height: 42px;
    padding: 6px;
    box-shadow: 2px 2px 0 #02100f;
  }

  .sound-copy {
    display: none;
  }

  .metric-strip,
  .track-stage,
  .control-deck {
    border-width: 2px;
    box-shadow: 3px 3px 0 var(--pixel-shadow);
  }

  .metric {
    padding: 8px 6px;
    border-right-width: 1px;
  }

  .metric span {
    font-size: 0.75rem;
    letter-spacing: 0;
  }

  .metric strong {
    font-size: 0.9rem;
  }

  .track-stage {
    min-height: 420px;
  }

  #raceCanvas {
    height: 440px;
    min-height: 420px;
  }

  .start-panel {
    width: calc(100% - 18px);
    max-height: calc(100% - 18px);
    overflow: auto;
    border-width: 3px;
    padding: 19px 14px;
  }

  .start-panel h2 {
    font-size: clamp(2.2rem, 13vw, 3.5rem);
  }

  .start-copy {
    margin: 12px 0 14px;
    font-size: 0.92rem;
  }

  .setup-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  select {
    min-height: 44px;
    font-size: 0.9rem;
  }

  .field-timing {
    font-size: 0.75rem;
  }

  .key-hint {
    display: none;
  }

  .track-callout {
    left: 8px;
    bottom: 8px;
    width: min(58%, 230px);
    max-width: none;
    font-size: 0.75rem;
  }

  .control-deck {
    position: sticky;
    bottom: 0;
    z-index: 9;
    grid-template-columns: 1fr;
    gap: 7px;
    padding: 7px;
  }

  .energy-panel {
    padding: 7px 9px;
  }

  .energy-track {
    margin: 6px 0;
  }

  .button-bank {
    grid-template-columns: repeat(3, 1fr);
  }

  .pace-button {
    min-height: 62px;
    padding: 7px 4px;
    box-shadow: 3px 3px 0 #02040c;
  }

  .pace-button small {
    display: none;
  }

  .kick-button {
    grid-column: 1 / -1;
    min-height: 56px;
    box-shadow: 3px 3px 0 #120b10;
  }

  .race-sidebar {
    grid-template-columns: 1fr;
  }

  .rules-panel {
    display: none;
  }

  .leaderboard-panel {
    order: 1;
  }

  .coach-panel {
    order: 0;
  }

  .panel {
    border-width: 2px;
    box-shadow: 3px 3px 0 var(--pixel-shadow);
  }

  .result-grid {
    grid-template-columns: 1fr 1fr;
  }

  .result-grid > div:nth-child(2) {
    border-right: 0;
  }

  .result-grid > div:nth-child(-n + 2) {
    border-bottom: 2px solid var(--pixel-line);
  }
}

@media (max-width: 360px) {
  .brand-kicker {
    display: none;
  }

  .race-state {
    max-width: 72px;
  }
}

@media (max-width: 430px) {
  .brand-kicker {
    display: none;
  }

  .brand-lockup {
    gap: 8px;
  }

  .start-panel::before {
    display: none;
  }

  .result-actions {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
