:root {
  color-scheme: dark;
  --ink: #f7f5ff;
  --muted: #9d9ab6;
  --panel: #111126;
  --panel-2: #17152f;
  --line: #302d50;
  --cyan: #55f4e7;
  --pink: #ff4ec8;
  --yellow: #ffe45b;
}

* { box-sizing: border-box; }

html, body { width: 100%; height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background: #060713;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior: none;
}

button, input { font: inherit; }
button { color: inherit; }

.app {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  grid-template-rows: 66px minmax(0, 1fr);
  background: radial-gradient(circle at 50% 35%, #17163a 0, #08091a 46%, #04050e 100%);
}

.topbar {
  position: relative;
  z-index: 5;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 14px;
  padding: 9px clamp(12px, 3vw, 28px);
  border-bottom: 1px solid #292645;
  background: rgb(7 8 22 / 88%);
  backdrop-filter: blur(14px);
}

.brand {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: var(--ink);
  text-decoration: none;
  font-size: clamp(.82rem, 2.5vw, 1.05rem);
  font-weight: 950;
  letter-spacing: -.055em;
}

.brand > span:last-child > span { color: var(--pink); }

.brand-mark {
  width: 16px;
  height: 27px;
  display: block;
  border-radius: 999px;
  background: linear-gradient(145deg, var(--cyan), #8a5cff 58%, var(--pink));
  transform: rotate(35deg);
  box-shadow: 0 0 13px #55f4e788;
}

.race-stats { display: flex; gap: clamp(18px, 6vw, 70px); }
.race-stats div { display: grid; justify-items: center; }
.race-stats span, .section-label, .eyebrow, .seed-label {
  color: var(--muted);
  font-size: .61rem;
  font-weight: 850;
  letter-spacing: .16em;
}
.race-stats strong { font-variant-numeric: tabular-nums; font-size: clamp(.84rem, 2.8vw, 1.08rem); }

.icon-button, .close-button {
  justify-self: end;
  width: 42px;
  height: 42px;
  display: grid;
  place-content: center;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #131229;
  cursor: pointer;
}

.icon-button { gap: 4px; }
.icon-button span { width: 17px; height: 2px; border-radius: 2px; background: var(--ink); }
.icon-button:hover, .close-button:hover { border-color: var(--cyan); background: #1b1937; }

.game-stage {
  position: relative;
  min-height: 0;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  overflow: hidden;
  touch-action: none;
  isolation: isolate;
}

#game { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#game:active { cursor: grabbing; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgb(5 6 18 / 42%);
  backdrop-filter: blur(3px);
  transition: opacity 180ms ease;
}

.overlay.hidden { opacity: 0; pointer-events: none; }

.race-card {
  width: min(100%, 410px);
  padding: clamp(22px, 5vw, 34px);
  border: 1px solid #514b7b;
  border-radius: 24px;
  background: linear-gradient(145deg, rgb(25 23 54 / 96%), rgb(11 11 29 / 96%));
  box-shadow: 0 22px 80px #0009, inset 0 1px #ffffff12;
  text-align: center;
}

.eyebrow { margin: 0 0 11px; color: var(--cyan); }
.race-card h1 { margin: 0; font-size: clamp(2rem, 8vw, 3.3rem); line-height: .96; letter-spacing: -.07em; }
.race-card > p:not(.eyebrow, .control-hint) { margin: 15px auto 0; max-width: 330px; color: #b9b5ca; font-size: .9rem; line-height: 1.5; }
.seed-label { margin-top: 20px; }
.seed-label strong { color: var(--yellow); letter-spacing: .04em; }

.primary-button, .small-button {
  border: 0;
  border-radius: 12px;
  background: linear-gradient(110deg, #5aeee2, #9f7aff);
  color: #080917;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 6px 24px #695ffb36;
}

.primary-button { width: 100%; margin-top: 21px; padding: 14px 18px; font-size: 1rem; }
.primary-button:hover, .small-button:hover { filter: brightness(1.1); transform: translateY(-1px); }
.control-hint { margin: 13px 0 0; color: #817d99; font-size: .72rem; }
.touch-hint { display: none; }

.countdown {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  pointer-events: none;
  color: white;
  font-size: clamp(5rem, 24vw, 10rem);
  font-weight: 1000;
  letter-spacing: -.09em;
  text-shadow: 0 0 26px var(--pink), 0 6px 0 #40285f;
}

.terrain-chip, .challenge-banner {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  border: 1px solid;
  background: rgb(8 9 25 / 84%);
  backdrop-filter: blur(7px);
  font-size: .66rem;
  font-weight: 900;
  letter-spacing: .12em;
}

.terrain-chip {
  left: 50%;
  bottom: 15px;
  translate: -50% 15px;
  padding: 8px 12px;
  border-color: #ff735b;
  border-radius: 999px;
  color: #ff9a85;
  opacity: 0;
  transition: 150ms ease;
}
.terrain-chip.show { translate: -50% 0; opacity: 1; }
.terrain-chip.rough { border-color: #ffe45b; color: #ffe45b; }

.mobile-controls { display: none; }

.steer-button {
  width: clamp(62px, 18vw, 78px);
  height: clamp(58px, 16vw, 72px);
  pointer-events: auto;
  display: grid;
  place-content: center;
  gap: 1px;
  border: 1px solid #8c86b766;
  border-radius: 20px;
  background: rgb(15 16 39 / 58%);
  color: #f7f5ff;
  box-shadow: inset 0 1px #ffffff16, 0 8px 24px #0004;
  backdrop-filter: blur(5px);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.steer-button span { font-size: 1.65rem; font-weight: 900; line-height: .9; }
.steer-button small { color: #a7a3bd; font-size: .48rem; font-weight: 900; letter-spacing: .14em; }
.steer-button.pressed { border-color: var(--cyan); background: rgb(29 69 78 / 78%); box-shadow: inset 0 0 18px #55f4e733, 0 0 20px #55f4e733; }

.challenge-banner {
  top: 12px;
  left: 12px;
  display: grid;
  gap: 2px;
  padding: 8px 11px;
  border-color: #ff4ec866;
  border-radius: 10px;
}
.challenge-banner span { color: var(--pink); font-size: .52rem; }
.challenge-banner strong { max-width: min(46vw, 240px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.toast {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: 22px;
  translate: -50% 18px;
  padding: 10px 14px;
  border: 1px solid #6e68a0;
  border-radius: 999px;
  background: #17152fee;
  font-size: .78rem;
  font-weight: 750;
  opacity: 0;
  pointer-events: none;
  transition: 180ms ease;
  white-space: nowrap;
}
.toast.show { opacity: 1; translate: -50% 0; }

.menu-layer { position: fixed; inset: 0; z-index: 30; }
.menu-layer[hidden] { display: none; }
.menu-scrim { position: absolute; inset: 0; width: 100%; border: 0; background: #03040cc9; backdrop-filter: blur(5px); cursor: default; }
.menu-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: min(100%, 480px);
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: #0d0d20;
  border-left: 1px solid #393557;
  box-shadow: -24px 0 70px #0008;
  animation: slide-in 180ms ease-out;
}

@keyframes slide-in { from { transform: translateX(40px); opacity: 0; } }

.menu-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 20px 16px; border-bottom: 1px solid var(--line); }
.menu-header .eyebrow { margin-bottom: 4px; }
.menu-header h2 { margin: 0; font-size: 1.55rem; letter-spacing: -.045em; }
.close-button { font-size: 1.65rem; line-height: 1; }
.menu-scroll { min-height: 0; overflow-y: auto; padding: 4px 20px 30px; }
.menu-section { padding: 20px 0; border-bottom: 1px solid var(--line); }
.section-label { margin: 0 0 12px; color: var(--cyan); }
.field-label { display: block; margin-bottom: 6px; color: #b2aec4; font-size: .74rem; }

input {
  width: 100%;
  min-width: 0;
  border: 1px solid #3d395c;
  border-radius: 11px;
  outline: none;
  background: #17152e;
  color: var(--ink);
  padding: 11px 12px;
}
input:focus { border-color: var(--cyan); box-shadow: 0 0 0 3px #55f4e719; }

.palette-row { display: flex; flex-wrap: wrap; gap: 9px; margin-top: 13px; }
.palette-choice {
  width: 38px;
  height: 38px;
  border: 2px solid transparent;
  border-radius: 50%;
  background: var(--color);
  box-shadow: inset 0 0 0 5px #0d0d20, 0 0 0 1px #4b4669;
  cursor: pointer;
}
.palette-choice.selected { border-color: white; box-shadow: inset 0 0 0 5px #0d0d20, 0 0 15px var(--color); }

.seed-form { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px; }
.small-button { padding: 9px 12px; font-size: .75rem; }
.small-button.secondary { color: var(--ink); background: #292644; box-shadow: none; }
.section-heading { display: flex; align-items: baseline; justify-content: space-between; }
.section-heading span { color: var(--muted); font-size: .7rem; }

.runs-list { display: grid; gap: 9px; }
.empty-runs { padding: 25px 10px; border: 1px dashed #3a3657; border-radius: 13px; color: var(--muted); text-align: center; font-size: .82rem; line-height: 1.5; }
.run-card { padding: 12px; border: 1px solid #343151; border-radius: 13px; background: #15142a; }
.run-main { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 12px; align-items: center; }
.run-meta { min-width: 0; }
.run-meta strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .87rem; }
.run-meta span { display: block; margin-top: 3px; color: var(--muted); font-size: .67rem; }
.run-time { color: var(--yellow); font-size: .96rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.run-actions { display: flex; gap: 7px; margin-top: 10px; }
.run-actions button { flex: 1; padding: 7px 8px; border: 1px solid #403c61; border-radius: 8px; background: #211f3b; color: #d8d5e8; font-size: .68rem; font-weight: 800; cursor: pointer; }
.run-actions button:hover { border-color: var(--cyan); }
.run-actions .delete-run:hover { border-color: #ff6a70; color: #ff8d92; }
.shared-section { padding-top: 16px; }
.shared-card { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px; border: 1px solid #ff4ec866; border-radius: 13px; background: #2b1231; }
.shared-card strong, .shared-card span { display: block; }
.shared-card span { margin-top: 3px; color: #c9aeca; font-size: .68rem; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@media (pointer: coarse), (max-width: 700px) {
  .desktop-hint { display: none; }
  .touch-hint { display: inline; }
}

@media (pointer: coarse) {
  .mobile-controls {
    position: absolute;
    z-index: 2;
    right: 13px;
    bottom: max(13px, env(safe-area-inset-bottom));
    left: 13px;
    display: flex;
    justify-content: space-between;
    pointer-events: none;
    opacity: 0;
    translate: 0 8px;
    transition: opacity 140ms ease, translate 140ms ease;
  }

  .game-stage.race-active .mobile-controls { opacity: 1; translate: 0 0; }
}

@media (max-width: 520px) {
  .app { grid-template-rows: 58px minmax(0, 1fr); }
  .topbar { gap: 8px; padding: 7px 10px; }
  .brand-mark { width: 13px; height: 22px; }
  .race-stats { gap: 13px; }
  .race-stats div:first-child { min-width: 68px; }
  .race-stats span { font-size: .5rem; }
  .icon-button { width: 39px; height: 39px; }
  .challenge-banner { top: 8px; left: 8px; }
  .menu-panel { border-left: 0; }
  .seed-form { grid-template-columns: 1fr 1fr; }
  .seed-form input { grid-column: 1 / -1; }
}

@media (max-height: 610px) and (orientation: landscape) {
  .app { grid-template-rows: 52px minmax(0, 1fr); }
  .topbar { padding-block: 5px; }
  .race-card { padding: 18px 24px; }
  .race-card > p:not(.eyebrow, .control-hint) { margin-top: 9px; }
  .seed-label { margin-top: 10px; }
  .primary-button { margin-top: 12px; padding: 10px; }
}

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