:root {
  color-scheme: dark;
  font-family: Inter, "Arial", system-ui, sans-serif;
  color: #f7f4e8;
  background: #202722;
  font-synthesis: none;
  --ivory: #f7f4e8;
  --muted: #c0c7b9;
  --lime: #d6ff53;
  --orange: #ff9b54;
  --panel: rgba(21, 29, 25, .82);
  --line: rgba(247, 244, 232, .18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  -webkit-tap-highlight-color: transparent;
  font-variant-numeric: tabular-nums;
}

[hidden] { display: none !important; }

button,
input {
  font: inherit;
}

button {
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 22px;
  color: var(--ivory);
  background: var(--panel);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .07em;
  line-height: 1.2;
  text-transform: uppercase;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  transition: background .16s ease, border-color .16s ease,
    transform .16s ease, box-shadow .16s ease;
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--lime);
  outline-offset: 5px;
}

button:active,
button.pressed {
  transform: translateY(2px);
  border-color: var(--lime);
  background: #465339;
}

button:disabled {
  opacity: .45;
  cursor: wait;
  transform: none;
}

a { color: var(--lime); }

#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  touch-action: none;
  background: linear-gradient(#a4c5cb, #e9d4ab 65%, #686e48);
}

#game > canvas {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
}

#loading,
#intro,
#result {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: calc(28px + var(--safe-top)) calc(24px + var(--safe-right))
    calc(28px + var(--safe-bottom)) calc(24px + var(--safe-left));
  text-align: center;
}

#loading {
  z-index: 100;
  color: var(--ivory);
  background: #1b2521;
}

#loading::before {
  content: "";
  width: 48px;
  height: 48px;
  border: 3px solid rgba(214, 255, 83, .15);
  border-top-color: var(--lime);
  border-radius: 50%;
  animation: loading-spin 1s linear infinite;
}

#loading p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  letter-spacing: .12em;
}

progress {
  width: min(280px, 80vw);
  height: 5px;
  overflow: hidden;
  border: 0;
  border-radius: 9px;
  background: #384137;
  accent-color: var(--lime);
}

progress::-webkit-progress-bar { background: #384137; }
progress::-webkit-progress-value { background: var(--lime); }
progress::-moz-progress-bar { background: var(--lime); }

#intro,
#result {
  z-index: 60;
  background: linear-gradient(180deg, rgba(15, 23, 20, .36),
    rgba(15, 23, 20, .72) 55%, rgba(15, 23, 20, .94));
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
  overflow-y: auto;
}

#intro > *,
#result > * {
  flex-shrink: 0;
  max-width: 600px;
}

#intro h1,
#resultTitle {
  margin: 0;
  font-family: Impact, "Arial Narrow", "Helvetica Neue", sans-serif;
  font-size: clamp(48px, 10vw, 112px);
  font-weight: 900;
  line-height: .91;
  letter-spacing: -.035em;
  text-transform: uppercase;
  text-wrap: balance;
  text-shadow: 0 8px 36px rgba(0, 0, 0, .2);
}

#intro h2,
#result h2 {
  margin: 0;
  text-transform: uppercase;
}

#intro p,
#result p {
  margin: 0;
  color: #d7ddce;
  font-size: clamp(14px, 2vw, 17px);
  line-height: 1.65;
  text-wrap: pretty;
}

#intro small,
#result small,
.keyboard-hints,
.hints {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.8;
}

kbd {
  display: inline-block;
  min-width: 23px;
  margin: 1px 2px;
  padding: 2px 6px;
  border: 1px solid rgba(247, 244, 232, .22);
  border-bottom-width: 3px;
  border-radius: 5px;
  color: var(--ivory);
  background: rgba(247, 244, 232, .07);
  font: 700 12px/1.4 system-ui, sans-serif;
}

#start,
#restart {
  min-width: 210px;
  min-height: 58px;
  border-color: var(--lime);
  color: #20291a;
  background: var(--lime);
  box-shadow: 0 7px 30px rgba(214, 255, 83, .13);
}

#explore {
  min-width: 210px;
  background: rgba(247, 244, 232, .07);
}

#resultTitle { font-size: clamp(40px, 8vw, 80px); }

#resultStats {
  width: min(100%, 460px);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  color: var(--ivory);
  background: rgba(15, 23, 20, .65);
  font-size: 16px;
  line-height: 1.9;
  white-space: pre-line;
}

#resultStats strong { color: var(--lime); }
#resultStats p { font-size: inherit; }
#resultStats dl { margin: 0; }
#resultStats dt { color: var(--muted); }
#resultStats dd { margin: 0; font-weight: 800; }

#hud {
  position: fixed;
  z-index: 10;
  top: calc(16px + var(--safe-top));
  left: calc(16px + var(--safe-left));
  right: calc(16px + var(--safe-right));
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 10px;
  pointer-events: none;
  text-shadow: 0 2px 12px rgba(0, 0, 0, .35);
}

#hud > :not(#toast):not(#airtime):not(#minimap) {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--panel);
  box-shadow: 0 4px 20px rgba(0, 0, 0, .1);
}

#hud label,
#hud small,
#hud .label {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.5;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.race-readouts { display: flex; gap: 18px; }
.readout { min-width: 0; }
#hud .boost-meter { min-height: 64px; height: auto; overflow: visible; }
#hud .unit { font-size: 12px; color: var(--muted); }

#speed,
#time,
#gate,
#score {
  font-weight: 900;
  letter-spacing: -.035em;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

#speed { font-size: clamp(34px, 5vw, 56px); }
#time { font-size: clamp(25px, 3.5vw, 38px); color: var(--lime); }
#gate,
#score { font-size: clamp(21px, 3vw, 30px); }

#time.warning,
#time.danger,
#time.low { color: var(--orange); }

.boost-track,
.boost-bar {
  min-width: 80px;
  height: 6px;
  margin-top: 7px;
  overflow: hidden;
  border-radius: 6px;
  background: rgba(247, 244, 232, .16);
}

#boostFill {
  display: block;
  height: 6px;
  max-width: 100%;
  border-radius: 6px;
  background: var(--lime);
  box-shadow: 0 0 12px rgba(214, 255, 83, .35);
  transform-origin: left center;
  transition: width .1s linear;
}

#minimap {
  position: fixed;
  z-index: 12;
  top: calc(110px + var(--safe-top));
  right: calc(18px + var(--safe-right));
  width: 156px;
  height: 156px;
  border: 1px solid rgba(247, 244, 232, .35);
  border-radius: 18px;
  background: rgba(24, 35, 29, .78);
  box-shadow: 0 8px 30px rgba(0, 0, 0, .2);
  pointer-events: none;
}

#toast,
#airtime {
  position: fixed;
  left: 50%;
  width: max-content;
  max-width: calc(100vw - 40px);
  text-align: center;
  pointer-events: none;
  transform: translateX(-50%);
}

#toast {
  top: calc(112px + var(--safe-top));
  z-index: 15;
  padding: 10px 18px;
  border-radius: 8px;
  color: var(--ivory);
  background: rgba(20, 29, 23, .86);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .04em;
  animation: toast-in .24s ease-out both;
}

#toast:empty,
#airtime:empty { display: none; }

#airtime {
  z-index: 14;
  top: 36%;
  color: var(--lime);
  font-size: clamp(28px, 5vw, 60px);
  font-weight: 950;
  font-style: italic;
  letter-spacing: -.04em;
  text-transform: uppercase;
  text-shadow: 0 3px 0 rgba(24, 35, 25, .6), 0 8px 30px rgba(0, 0, 0, .4);
}

#controls {
  position: fixed;
  inset: 0;
  z-index: 25;
  pointer-events: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#controls button,
[data-control],
#reset,
#mute {
  pointer-events: auto;
  touch-action: none;
}

[data-control] {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  display: flex;
  align-items: center;
  justify-content: center;
  width: 66px;
  height: 66px;
  min-height: 54px;
  padding: 6px;
  border: 1px solid rgba(247, 244, 232, .3);
  border-radius: 16px;
  background: rgba(24, 32, 27, .72);
  color: var(--ivory);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: .025em;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .07),
    0 5px 18px rgba(0, 0, 0, .16);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

[data-control="left"] { left: calc(16px + var(--safe-left)); }
[data-control="right"] { left: calc(90px + var(--safe-left)); }
[data-control="throttle"] {
  right: calc(16px + var(--safe-right));
  height: 88px;
  border-color: rgba(214, 255, 83, .7);
  color: var(--lime);
}
[data-control="brake"] { right: calc(90px + var(--safe-right)); }
[data-control="boost"] {
  right: calc(16px + var(--safe-right));
  bottom: calc(122px + var(--safe-bottom));
  color: #20291a;
  border-color: var(--lime);
  background: rgba(214, 255, 83, .92);
}
[data-control="handbrake"] {
  left: calc(16px + var(--safe-left));
  bottom: calc(100px + var(--safe-bottom));
  width: 140px;
  height: 54px;
  color: #ffbc89;
}

[data-control].pressed,
[data-control]:active {
  border-color: var(--lime);
  background: var(--lime);
  color: #20291a;
  box-shadow: 0 0 24px rgba(214, 255, 83, .25);
  transform: translateY(2px) scale(.97);
}

#reset,
#mute {
  position: fixed;
  z-index: 26;
  bottom: calc(24px + var(--safe-bottom));
  min-width: 54px;
  min-height: 54px;
  padding: 10px;
  border-radius: 12px;
  font-size: 12px;
  background: rgba(22, 30, 25, .78);
}

#reset { left: calc(170px + var(--safe-left)); bottom: calc(100px + var(--safe-bottom)); }
#mute {
  right: calc(18px + var(--safe-right));
  top: calc(276px + var(--safe-top));
  bottom: auto;
}
#mute[aria-pressed="true"] { color: var(--orange); }

@media (hover: hover) and (pointer: fine) {
  button:not(:disabled):hover {
    border-color: var(--lime);
    box-shadow: 0 0 20px rgba(214, 255, 83, .12);
  }
  #start:not(:disabled):hover,
  #restart:hover { background: #e4ff8c; }
  [data-control] { opacity: .68; }
  [data-control]:hover,
  [data-control].pressed { opacity: 1; }
}

@media (max-width: 600px) {
  #hud {
    top: calc(10px + var(--safe-top));
    left: calc(10px + var(--safe-left));
    right: calc(10px + var(--safe-right));
    gap: 6px;
    display: grid;
    grid-template-columns: 90px minmax(0, 1fr) 90px;
    grid-template-areas: "brand boost speed" "race race race";
  }
  #hud > :not(#toast):not(#airtime):not(#minimap) {
    padding: 5px 8px;
  #hud label,
  #hud small,
  #hud .label { font-size: 12px; letter-spacing: .02em; }
  }
  #hud label,
  #hud small,
  #hud .label { font-size: 10px; letter-spacing: .02em; }
  #hud .race-readouts { grid-area: race; justify-content: space-between; gap: 8px; }
  #hud .boost-meter { grid-area: boost; width: 100%; min-height: 0; align-self: stretch; }
  #hud .boost-track { min-width: 0; margin-top: 4px; }
  #hud .hud-brand { grid-area: brand; font-size: 12px; line-height: 1.3; }
  #hud .speed-readout { grid-area: speed; justify-self: stretch; text-align: right; }
  #hud .boost-meter .keyboard-hint { display: none; }
  #airtime { top: 50%; }
  #hud .keyboard-hint { font-size: 12px; }
  #speed { font-size: 28px; }
  #time { font-size: 24px; }
  #gate,
  #score { font-size: 21px; }
  #minimap {
    position: fixed;
    top: calc(112px + var(--safe-top));
    right: calc(10px + var(--safe-right));
    width: 80px;
    height: 80px;
    border-radius: 13px;
  }
  #mute {
    top: calc(200px + var(--safe-top));
    right: calc(10px + var(--safe-right));
    min-height: 54px;
  }
  #toast {
    position: fixed;
    top: calc(112px + var(--safe-top));
    left: calc(10px + var(--safe-left));
    width: 130px;
    max-width: 34vw;
    transform: none;
    font-size: 12px;
    padding: 7px 8px;
  }
  [data-control] {
    bottom: calc(12px + var(--safe-bottom));
    width: 58px;
    height: 58px;
  }
  [data-control="left"] { left: calc(10px + var(--safe-left)); }
  [data-control="right"] { left: calc(76px + var(--safe-left)); }
  [data-control="brake"] { right: calc(76px + var(--safe-right)); }
  [data-control="throttle"] { right: calc(10px + var(--safe-right)); height: 76px; }
  [data-control="boost"] { right: calc(10px + var(--safe-right)); bottom: calc(98px + var(--safe-bottom)); }
  [data-control="handbrake"] {
    left: calc(10px + var(--safe-left));
    bottom: calc(82px + var(--safe-bottom));
    width: 80px;
    height: 54px;
    min-height: 54px;
    font-size: 12px;
  }
  #reset {
    left: calc(10px + var(--safe-left));
    bottom: calc(140px + var(--safe-bottom));
    width: 80px;
    min-height: 54px;
  }
  #intro,
  #result { gap: 16px; }
  #intro h1 { font-size: clamp(48px, 15vw, 76px); }
  #intro p { font-size: 14px; }
  #resultStats { padding: 14px 18px; font-size: 14px; }
}

@media (max-width: 370px) {
  #hud { gap: 4px; }
  #hud > :not(#toast):not(#airtime):not(#minimap) { padding: 7px; }
  #speed { font-size: 30px; }
  #time { font-size: 25px; }
  #gate,
  #score { font-size: 19px; }
  [data-control] { width: 58px; height: 60px; font-size: 12px; }
  [data-control="right"] { left: calc(80px + var(--safe-left)); }
  [data-control="brake"] { right: calc(80px + var(--safe-right)); }
  [data-control="handbrake"] { width: 80px; }
  #reset { left: calc(10px + var(--safe-left)); }
}

@media (orientation: landscape) and (max-height: 540px) {
  #hud { top: calc(8px + var(--safe-top)); gap: 6px; }
  #hud > :not(#toast):not(#airtime):not(#minimap) { padding: 6px 11px; }
  #speed { font-size: 34px; }
  #time { font-size: 27px; }
  #gate,
  #score { font-size: 23px; }
  #minimap {
    top: calc(10px + var(--safe-top));
    position: fixed;
    right: calc(12px + var(--safe-right));
    width: 92px;
    height: 92px;
  }
  #hud { right: calc(116px + var(--safe-right)); display: flex; }
  #hud .race-readouts { gap: 12px; }
  #hud .boost-meter { width: auto; }
  #mute {
    top: auto;
    right: calc(170px + var(--safe-right));
    bottom: calc(14px + var(--safe-bottom));
  }
  #toast {
    top: calc(84px + var(--safe-top));
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    max-width: 48vw;
  }
  #airtime { top: 35%; font-size: 34px; }
  [data-control] { bottom: calc(14px + var(--safe-bottom)); height: 58px; }
  [data-control="throttle"] { height: 76px; }
  [data-control="boost"] { bottom: calc(100px + var(--safe-bottom)); }
  [data-control="handbrake"] { bottom: calc(82px + var(--safe-bottom)); }
  #reset { bottom: calc(82px + var(--safe-bottom)); }
  #intro,
  #result {
    gap: 10px;
    padding-top: calc(16px + var(--safe-top));
    padding-bottom: calc(16px + var(--safe-bottom));
  }
  #intro h1,
  #resultTitle { font-size: clamp(34px, 9vh, 54px); }
  #intro p,
  #result p { font-size: 12px; line-height: 1.4; }
  #resultStats { padding: 8px 16px; font-size: 12px; line-height: 1.5; }
  #start,
  #restart,
  #explore { min-height: 54px; padding: 10px 22px; }
  .keyboard-hints,
  .hints { font-size: 12px; }
}

@media (pointer: coarse) {
  .keyboard-hints,
  .keyboard-only { display: none; }
}

@keyframes loading-spin {
  to { transform: rotate(360deg); }
}

@keyframes toast-in {
  from { opacity: 0; margin-top: -6px; }
  to { opacity: 1; margin-top: 0; }
}

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