.hidden {
  display: none !important;
}

:root {
  --night-deep: #07142f;
  --night: #102451;
  --night-soft: #1a3567;
  --cream: #fff8e8;
  --cream-dim: rgba(255, 248, 232, 0.72);
  --gold: #ffd977;
  --gold-deep: #eaa93f;
  --red: #d9414d;
  --red-dark: #9d2231;
  --green: #2c9a67;
  --green-dark: #176443;
  --glass: rgba(9, 24, 54, 0.64);
  --glass-strong: rgba(8, 22, 49, 0.84);
  --stroke: rgba(255, 222, 154, 0.5);
  --stroke-soft: rgba(255, 255, 255, 0.18);
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.42);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--night-deep);
  color: var(--cream);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

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

#game {
  position: fixed;
  inset: 0;
  overflow: hidden;
  isolation: isolate;
  touch-action: none;
  background:
    radial-gradient(circle at 50% 8%, rgba(96, 145, 224, 0.35), transparent 38%),
    linear-gradient(180deg, #09183a 0%, #17346a 58%, #cfe7f4 100%);
}

#game canvas:not(#minimap) {
  position: absolute !important;
  inset: 0;
  display: block;
  width: 100% !important;
  height: 100% !important;
  touch-action: none;
}

#game::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background:
    radial-gradient(circle at 50% 38%, transparent 46%, rgba(4, 12, 30, 0.28) 100%),
    linear-gradient(180deg, rgba(7, 18, 43, 0.12), transparent 28%, transparent 80%, rgba(7, 18, 43, 0.2));
}

/* ---------- Overlay cards ---------- */

#overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: calc(16px + var(--safe-top)) calc(16px + var(--safe-right)) calc(16px + var(--safe-bottom)) calc(16px + var(--safe-left));
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 18%, rgba(255, 220, 128, 0.16), transparent 32%),
    rgba(4, 13, 33, 0.42);
  -webkit-backdrop-filter: blur(7px) saturate(118%);
  backdrop-filter: blur(7px) saturate(118%);
}

#overlay::before {
  content: "";
  position: absolute;
  inset: -20%;
  z-index: 0;
  pointer-events: none;
  opacity: 0.55;
  background-image:
    radial-gradient(circle, rgba(255, 255, 255, 0.8) 0 1.5px, transparent 2px),
    radial-gradient(circle, rgba(255, 255, 255, 0.55) 0 1px, transparent 1.7px),
    radial-gradient(circle, rgba(255, 226, 152, 0.55) 0 1.2px, transparent 2px);
  background-position: 0 0, 60px 40px, 130px 90px;
  background-size: 180px 180px, 210px 210px, 260px 260px;
  animation: snow-drift 22s linear infinite;
}

#overlay::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 22%, rgba(5, 15, 36, 0.22));
}

@keyframes snow-drift {
  from {
    background-position: 0 0, 60px 40px, 130px 90px;
  }
  to {
    background-position: 0 260px, 60px 300px, 130px 350px;
  }
}

#title,
#finish {
  position: relative;
  z-index: 1;
  width: min(92vw, 440px);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 30px 24px 24px;
  text-align: center;
  border: 1px solid var(--stroke);
  border-radius: 30px;
  background:
    radial-gradient(circle at 50% 0%, rgba(255, 223, 148, 0.2), transparent 42%),
    linear-gradient(155deg, rgba(18, 44, 89, 0.9), rgba(6, 19, 43, 0.86));
  box-shadow:
    var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.32),
    inset 0 -1px 0 rgba(0, 0, 0, 0.25);
  -webkit-backdrop-filter: blur(16px) saturate(130%);
  backdrop-filter: blur(16px) saturate(130%);
  animation: card-in 0.35s cubic-bezier(0.2, 0.9, 0.2, 1.15) both;
  scrollbar-width: thin;
  scrollbar-color: var(--gold-deep) transparent;
}

#title::-webkit-scrollbar,
#finish::-webkit-scrollbar {
  width: 6px;
}

#title::-webkit-scrollbar-thumb,
#finish::-webkit-scrollbar-thumb {
  background: var(--gold-deep);
  border-radius: 99px;
}

#title::before,
#finish::before {
  content: "";
  position: absolute;
  inset: 10px;
  pointer-events: none;
  border: 1px dashed rgba(255, 226, 157, 0.26);
  border-radius: 22px;
}

#title::after {
  content: "✦  ❄  ✦";
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  padding: 0 12px;
  color: var(--gold);
  background: rgba(9, 25, 56, 0.92);
  border-radius: 99px;
  font-size: 12px;
  letter-spacing: 0.12em;
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

#title h1,
#finish h2 {
  margin: 4px 0 10px;
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-size: clamp(34px, 9.5vw, 54px);
  line-height: 0.96;
  font-weight: 900;
  letter-spacing: -0.045em;
  background: linear-gradient(180deg, #fff9df 0%, var(--gold) 56%, #e9a844 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 0 rgba(70, 25, 16, 0.35));
}

#finish h2 {
  font-size: clamp(32px, 8.5vw, 48px);
}

#title p,
#finish p {
  margin: 8px auto;
  max-width: 34ch;
  color: var(--cream-dim);
  font-size: 14px;
  line-height: 1.5;
}

#title strong,
#finish strong {
  color: var(--gold);
}

#title ul,
#finish ul {
  display: grid;
  gap: 7px;
  margin: 18px 0;
  padding: 0;
  list-style: none;
  text-align: left;
}

#title li,
#finish li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 248, 232, 0.86);
  font-size: 13px;
}

#title kbd,
#finish kbd,
.key {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  min-height: 24px;
  padding: 3px 7px;
  border: 1px solid rgba(255, 231, 171, 0.5);
  border-bottom-width: 3px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.1);
  color: #fff5d6;
  font-family: "Space Grotesk", ui-sans-serif, system-ui, sans-serif;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

#title .actions,
#finish .actions,
.card-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}

#start,
#restart,
#share {
  appearance: none;
  -webkit-appearance: none;
  min-width: 144px;
  min-height: 56px;
  padding: 13px 22px;
  border-radius: 18px;
  border: 1px solid rgba(255, 224, 151, 0.55);
  cursor: pointer;
  touch-action: manipulation;
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  transition: transform 0.12s ease, filter 0.12s ease, box-shadow 0.12s ease;
}

#start,
#restart {
  color: #fff8e8;
  background: linear-gradient(180deg, #ef5360 0%, var(--red) 48%, var(--red-dark) 100%);
  box-shadow:
    0 8px 0 #741724,
    0 16px 28px rgba(181, 38, 52, 0.32),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

#share {
  color: #3b2a0c;
  background: linear-gradient(180deg, #fff0b9 0%, var(--gold) 55%, var(--gold-deep) 100%);
  box-shadow:
    0 8px 0 #a66b19,
    0 16px 28px rgba(234, 169, 63, 0.24),
    inset 0 1px 0 rgba(255, 255, 255, 0.55);
}

#start:active,
#restart:active,
#share:active {
  transform: translateY(5px);
  box-shadow:
    0 3px 0 #741724,
    0 8px 16px rgba(0, 0, 0, 0.24),
    inset 0 1px 0 rgba(255, 255, 255, 0.28);
}

#share:active {
  box-shadow:
    0 3px 0 #a66b19,
    0 8px 16px rgba(0, 0, 0, 0.2),
    inset 0 1px 0 rgba(255, 255, 255, 0.5);
}

#final-place {
  margin: 4px 0 2px;
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-size: clamp(44px, 14vw, 72px);
  line-height: 1;
  font-weight: 900;
  letter-spacing: -0.05em;
  color: var(--gold);
  text-shadow: 0 5px 0 rgba(90, 49, 5, 0.55), 0 0 28px rgba(255, 217, 119, 0.35);
}

#final-time {
  margin: 8px 0 14px;
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-size: 18px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.08em;
  color: var(--cream);
}

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

#hud {
  position: absolute;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  color: var(--cream);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.55);
}

#hud * {
  pointer-events: none;
}

.hud-top,
.top-hud,
.hud-row {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}

.hud-bottom,
.bottom-hud,
.touch-controls,
.controls,
.touch-pad,
.mobile-controls,
#controls {
  position: absolute;
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  bottom: calc(14px + var(--safe-bottom));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
  pointer-events: none;
}

#lap,
#time,
#speed,
#place {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
  padding: 7px 12px;
  border: 1px solid var(--stroke-soft);
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(18, 42, 82, 0.78), rgba(7, 20, 46, 0.66));
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.26), inset 0 1px 0 rgba(255, 255, 255, 0.16);
  -webkit-backdrop-filter: blur(10px) saturate(125%);
  backdrop-filter: blur(10px) saturate(125%);
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-size: 14px;
  font-weight: 900;
  letter-spacing: 0.025em;
  white-space: nowrap;
}

#lap,
#place {
  min-width: 78px;
}

#time {
  min-width: 104px;
  font-variant-numeric: tabular-nums;
  color: #fff6d9;
}

#place {
  color: var(--gold);
  border-color: rgba(255, 217, 119, 0.4);
}

#speed {
  min-width: 86px;
  font-size: 13px;
  color: rgba(255, 248, 232, 0.9);
}

/* Direct-child fallback positioning when index.html places HUD items directly. */

#hud > #lap {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
}

#hud > #time {
  position: absolute;
  top: calc(12px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
}

#hud > #place {
  position: absolute;
  top: calc(12px + var(--safe-top));
  right: calc(12px + var(--safe-right));
}

#hud > #speed {
  position: absolute;
  left: calc(12px + var(--safe-left));
  bottom: calc(136px + var(--safe-bottom));
}

#hud > #stamina-bar {
  position: absolute;
  left: calc(12px + var(--safe-left));
  bottom: calc(104px + var(--safe-bottom));
  width: min(42vw, 190px);
}

#hud > #message {
  position: absolute;
  top: calc(58px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
}

#hud > #minimap {
  position: absolute;
  top: calc(88px + var(--safe-top));
  right: calc(12px + var(--safe-right));
}

/* Stamina */

.stamina,
.stamina-meter,
.stamina-track,
#stamina {
  position: relative;
  width: min(42vw, 190px);
  height: 16px;
  padding: 2px;
  overflow: hidden;
  border: 1px solid rgba(255, 226, 157, 0.35);
  border-radius: 999px;
  background: rgba(4, 14, 34, 0.72);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.3), inset 0 2px 5px rgba(0, 0, 0, 0.45);
}

#stamina-bar {
  position: relative;
  display: block;
  height: 100%;
  min-width: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, #63dc98 0%, #bde870 45%, var(--gold) 72%, #ff8d62 100%);
  box-shadow: 0 0 14px rgba(255, 217, 119, 0.45);
  transition: width 0.12s linear;
}

#stamina-bar::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.45), transparent 48%);
  opacity: 0.7;
}

/* Toast */

#message {
  opacity: 0;
  translate: 0 -8px;
  padding: 8px 16px;
  border: 1px solid rgba(255, 228, 159, 0.5);
  border-radius: 999px;
  background: rgba(8, 23, 51, 0.78);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.32), 0 0 24px rgba(255, 217, 119, 0.16);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: #fff5d4;
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-size: 16px;
  font-weight: 900;
  letter-spacing: 0.03em;
  white-space: nowrap;
  transition: opacity 0.2s ease, translate 0.2s ease;
}

#message:not(:empty),
#message.show,
#message.visible {
  opacity: 1;
  translate: 0 0;
}

/* Mini-map */

#minimap {
  display: block;
  width: 112px;
  height: 112px;
  border: 1px solid rgba(255, 224, 154, 0.42);
  border-radius: 26px;
  background: rgba(7, 21, 49, 0.66);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(9px);
  backdrop-filter: blur(9px);
}

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

#throttle,
#boost,
#left,
#right {
  pointer-events: auto;
  display: grid;
  place-items: center;
  margin: 0;
  border: 1px solid rgba(255, 227, 163, 0.45);
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  cursor: pointer;
  font-family: "Space Grotesk", "Inter", ui-sans-serif, system-ui, sans-serif;
  font-weight: 900;
  box-shadow:
    0 8px 0 rgba(3, 12, 29, 0.62),
    0 14px 24px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.22);
  transition: transform 0.08s ease, filter 0.08s ease, box-shadow 0.08s ease;
}

#left,
#right {
  width: 66px;
  height: 66px;
  border-radius: 22px;
  color: #fff8e8;
  background: linear-gradient(180deg, rgba(47, 91, 152, 0.9), rgba(11, 32, 70, 0.88));
  font-size: 26px;
}

#throttle {
  width: 82px;
  height: 82px;
  border-radius: 50%;
  color: #fff9e5;
  background:
    radial-gradient(circle at 36% 28%, rgba(255, 255, 255, 0.35), transparent 28%),
    linear-gradient(180deg, #45b97c, var(--green-dark));
  font-size: 30px;
  box-shadow:
    0 9px 0 #0d3f2b,
    0 16px 28px rgba(11, 75, 50, 0.36),
    inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

#boost {
  width: 82px;
  height: 82px;
  border-radius: 26px;
  color: #40280a;
  background:
    radial-gradient(circle at 38% 26%, rgba(255, 255, 255, 0.6), transparent 26%),
    linear-gradient(180deg, #fff0b3, var(--gold) 52%, var(--gold-deep));
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  box-shadow:
    0 9px 0 #9a6318,
    0 16px 28px rgba(234, 169, 63, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.55);
}

#throttle:active,
#boost:active,
#left:active,
#right:active,
#throttle.pressed,
#boost.pressed,
#left.pressed,
#right.pressed {
  transform: translateY(6px) scale(0.97);
  filter: brightness(1.12);
  box-shadow:
    0 3px 0 rgba(3, 12, 29, 0.62),
    0 8px 14px rgba(0, 0, 0, 0.28),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
}

#boost[disabled],
#boost.spent,
#boost.cooling {
  opacity: 0.5;
  filter: saturate(0.5);
}

#hud > #left {
  left: calc(12px + var(--safe-left));
  bottom: calc(14px + var(--safe-bottom));
}

#hud > #right {
  left: calc(88px + var(--safe-left));
  bottom: calc(14px + var(--safe-bottom));
}

#hud > #boost {
  right: calc(104px + var(--safe-right));
  bottom: calc(18px + var(--safe-bottom));
}

#hud > #throttle {
  right: calc(14px + var(--safe-right));
  bottom: calc(14px + var(--safe-bottom));
}

/* Small and landscape refinements */

@media (max-width: 380px) {
  #title,
  #finish {
    padding: 26px 18px 20px;
    border-radius: 24px;
  }

  #title h1 {
    font-size: 34px;
  }

  #start,
  #restart,
  #share {
    min-width: 132px;
    min-height: 52px;
    padding: 11px 18px;
  }

  #lap,
  #time,
  #speed,
  #place {
    min-height: 30px;
    padding: 6px 9px;
    font-size: 12px;
  }

  #time {
    min-width: 92px;
  }

  #minimap {
    width: 94px;
    height: 94px;
    border-radius: 22px;
  }

  #hud > #minimap {
    top: calc(82px + var(--safe-top));
  }

  #left,
  #right {
    width: 58px;
    height: 58px;
    border-radius: 18px;
    font-size: 22px;
  }

  #throttle,
  #boost {
    width: 68px;
    height: 68px;
  }

  #boost {
    border-radius: 20px;
    font-size: 10px;
  }

  #hud > #left {
    left: calc(8px + var(--safe-left));
  }

  #hud > #right {
    left: calc(72px + var(--safe-left));
  }

  #hud > #boost {
    right: calc(84px + var(--safe-right));
  }

  #hud > #throttle {
    right: calc(8px + var(--safe-right));
  }

  #hud > #speed {
    bottom: calc(122px + var(--safe-bottom));
  }

  #hud > #stamina-bar {
    width: min(44vw, 165px);
    bottom: calc(94px + var(--safe-bottom));
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  #title,
  #finish {
    width: min(92vw, 520px);
    padding: 22px 20px 18px;
  }

  #title h1,
  #finish h2 {
    font-size: 34px;
  }

  #title ul {
    margin: 12px 0;
  }

  #title li {
    padding: 5px 10px;
  }

  #start,
  #restart,
  #share {
    min-height: 48px;
  }

  #minimap {
    width: 82px;
    height: 82px;
    border-radius: 20px;
  }

  #hud > #minimap {
    top: calc(62px + var(--safe-top));
  }

  #hud > #message {
    top: calc(52px + var(--safe-top));
  }

  #throttle,
  #boost {
    width: 64px;
    height: 64px;
  }

  #left,
  #right {
    width: 54px;
    height: 54px;
  }

  #hud > #speed {
    bottom: calc(104px + var(--safe-bottom));
  }

  #hud > #stamina-bar {
    bottom: calc(78px + var(--safe-bottom));
  }
}

@media (min-width: 900px) {
  #title,
  #finish {
    width: 470px;
    padding: 34px 30px 28px;
  }

  #title h1,
  #finish h2 {
    font-size: 58px;
  }

  #minimap {
    width: 124px;
    height: 124px;
  }
}

@media (hover: hover) {
  #start:hover,
  #restart:hover,
  #share:hover {
    filter: brightness(1.08);
    transform: translateY(-1px);
  }

  #throttle:hover,
  #boost:hover,
  #left:hover,
  #right:hover {
    filter: brightness(1.1);
  }
}

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