:root {
  --bg: #0a1016;
  --panel: rgba(16, 23, 32, 0.92);
  --panel-strong: rgba(20, 28, 39, 0.98);
  --line: rgba(255, 255, 255, 0.1);
  --text: #f2f6fb;
  --muted: #99a6b7;
  --accent: #ffb300;
  --accent-2: #47d1ff;
  --danger: #ff6157;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at top, #152235, #081019 58%, #05090d 100%);
  color: var(--text);
  font-family: Inter, system-ui, Arial, sans-serif;
}
body {
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}
button, input { font: inherit; }
button {
  appearance: none;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #111;
  font-weight: 800;
  padding: 13px 18px;
  cursor: pointer;
}
button.secondary {
  background: #273244;
  color: var(--text);
}

button.secondary:hover { filter: brightness(1.08); }
button:hover { filter: brightness(1.05); }
input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #0d141d;
  color: var(--text);
  padding: 12px 14px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.eyebrow {
  color: var(--accent-2);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-size: 0.78rem;
  font-weight: 800;
  margin: 0 0 10px;
}

.app-shell {
  min-height: 100vh;
}
.screen {
  min-height: 100vh;
}

.screen-menu {
  display: grid;
  place-items: center;
  padding: 28px 16px;
}
.menu-card {
  width: min(980px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding: 24px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.35);
}
.menu-logo {
  display: grid;
  place-items: center;
  padding: 8px 8px 2px;
}
.menu-logo img {
  display: block;
  width: min(100%, 560px);
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 16px 28px rgba(0, 0, 0, 0.35));
}
.menu-card h1 {
  margin: 0 0 18px;
  font-size: clamp(2rem, 4vw, 3.3rem);
  line-height: 1.02;
}
.info-card, .menu-section {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 18px;
  margin-top: 18px;
}
.info-card h2, .menu-section h2 {
  margin: 0 0 10px;
}
.track-picker {
  position: relative;
  margin-top: 8px;
}
.track-carousel {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
}
.track-carousel__viewport {
  position: relative;
  min-height: 310px;
  display: grid;
  place-items: center;
  overflow: hidden;
  touch-action: pan-y;
}
.track-carousel__arrow {
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 2rem;
  line-height: 1;
  background: rgba(25, 37, 50, 0.92);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.14);
}
.track-card {
  position: absolute;
  width: min(100%, 430px);
  display: grid;
  gap: 8px;
  text-align: left;
  padding: 16px;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(42, 58, 79, 0.98), rgba(28, 39, 55, 0.98)),
    radial-gradient(circle at top, rgba(109, 232, 255, 0.12), transparent 45%);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.28);
  transform-origin: center center;
  transition: transform 220ms ease, opacity 220ms ease, box-shadow 220ms ease, background 220ms ease;
}
.track-card[data-offset="-2"] {
  transform: translateX(-72%) scale(0.78) rotateY(22deg);
  opacity: 0.24;
  z-index: 1;
}
.track-card[data-offset="-1"] {
  transform: translateX(-38%) scale(0.88) rotateY(12deg);
  opacity: 0.58;
  z-index: 2;
}
.track-card[data-offset="0"] {
  transform: translateX(0) scale(1);
  opacity: 1;
  z-index: 3;
}
.track-card[data-offset="1"] {
  transform: translateX(38%) scale(0.88) rotateY(-12deg);
  opacity: 0.58;
  z-index: 2;
}
.track-card[data-offset="2"] {
  transform: translateX(72%) scale(0.78) rotateY(-22deg);
  opacity: 0.24;
  z-index: 1;
}
.track-card__meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: baseline;
}
.track-card__status {
  display: flex;
  justify-content: flex-end;
  margin-top: -2px;
  min-height: 18px;
}
.track-card__medals {
  position: relative;
  height: 24px;
  width: 64px;
}
.track-card__medal {
  position: absolute;
  bottom: 0;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  border: 2px solid rgba(8, 16, 25, 0.82);
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.24);
}
.track-card__medal--gold {
  right: 0;
  background: linear-gradient(180deg, #ffd95a, #e0ab00);
  z-index: 3;
}
.track-card__medal--silver {
  right: 18px;
  background: linear-gradient(180deg, #eef3f8, #aeb9c6);
  z-index: 2;
}
.track-card__medal--bronze {
  right: 36px;
  background: linear-gradient(180deg, #e5a16d, #ad6233);
  z-index: 1;
}
.track-card__medal.is-earned {
  transform: translateY(-1px);
}
.track-card__medal:not(.is-earned) {
  opacity: 0.18;
  filter: grayscale(0.85);
}
.track-card strong { font-size: 1.05rem; }
.track-card span {
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.88rem;
}
.track-card canvas {
  width: 100%;
  height: auto;
  aspect-ratio: 240 / 126;
  display: block;
  touch-action: pan-y;
  border-radius: 14px;
  background:
    radial-gradient(circle at 20% 18%, rgba(255, 255, 255, 0.14), transparent 34%),
    linear-gradient(180deg, #2f7442, #255d33 58%, #234b2e);
  border: 1px solid rgba(255, 255, 255, 0.1);
}
.track-card.active {
  outline: 2px solid var(--accent-2);
  background:
    linear-gradient(180deg, rgba(47, 69, 96, 1), rgba(31, 46, 66, 1)),
    radial-gradient(circle at top, rgba(109, 232, 255, 0.18), transparent 45%);
  box-shadow: 0 28px 58px rgba(0, 0, 0, 0.34);
}
.play-btn {
  margin-top: 14px;
  width: 100%;
}
.medal-panel {
  margin-top: 16px;
  padding: 16px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
}
.medal-panel__head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  margin-bottom: 12px;
}
.medal-panel__head h3 {
  margin: 0;
  font-size: 1rem;
}
.medal-panel__summary {
  margin: 0;
  color: var(--accent-2);
  font-size: 0.9rem;
  font-weight: 700;
}
.medal-slots {
  display: grid;
  gap: 10px;
}
.medal-slot {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 12px 14px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
}
.medal-slot.is-achieved {
  border-color: rgba(255, 179, 0, 0.42);
  background: rgba(255, 179, 0, 0.08);
}
.medal-slot.is-active-ghost {
  border-color: rgba(71, 209, 255, 0.7);
  background: rgba(71, 209, 255, 0.09);
}
.medal-slot__badge {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 0.82rem;
  font-weight: 900;
  color: #10151d;
}
.medal-slot--gold .medal-slot__badge {
  background: linear-gradient(180deg, #ffd95a, #e0ab00);
}
.medal-slot--silver .medal-slot__badge {
  background: linear-gradient(180deg, #eef3f8, #aeb9c6);
}
.medal-slot--bronze .medal-slot__badge {
  background: linear-gradient(180deg, #e5a16d, #ad6233);
}
.medal-slot__info {
  display: grid;
  gap: 3px;
  min-width: 0;
}
.medal-slot__title {
  font-weight: 800;
}
.medal-slot__meta {
  font-size: 0.84rem;
  color: rgba(255, 255, 255, 0.74);
}
.medal-slot__actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.medal-slot__ghost-btn {
  min-width: 96px;
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 0.78rem;
}
.finish-medal-summary {
  display: grid;
  gap: 4px;
  margin: 0 0 18px;
  padding: 14px 16px;
  border-radius: 18px;
  background: rgba(255, 179, 0, 0.08);
  border: 1px solid rgba(255, 179, 0, 0.18);
}
.finish-medal-summary__head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.finish-medal-badge {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  font-weight: 900;
  color: #10151d;
}
.finish-medal-badge--gold {
  background: linear-gradient(180deg, #ffd95a, #e0ab00);
}
.finish-medal-badge--silver {
  background: linear-gradient(180deg, #eef3f8, #aeb9c6);
}
.finish-medal-badge--bronze {
  background: linear-gradient(180deg, #e5a16d, #ad6233);
}
.finish-medal-badge--none {
  background: linear-gradient(180deg, #546476, #354252);
  color: #eef2f7;
}
.finish-medal-summary strong {
  font-size: 1.02rem;
}
.finish-medal-summary p {
  margin: 0;
  color: rgba(255, 255, 255, 0.82);
}
.menu-profile {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}
.menu-profile label {
  font-size: 0.9rem;
  color: rgba(255, 255, 255, 0.82);
}
.form-error {
  margin: 0;
  color: #ff8f85;
  font-size: 0.9rem;
  font-weight: 700;
}
.ghost-panel {
  margin-top: 14px;
  padding: 14px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--line);
}
.ghost-panel__head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}
.ghost-panel__head h3 {
  margin: 0;
  font-size: 1rem;
}
.ghost-panel__clear {
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
}
.ghost-panel__summary {
  margin: 10px 0 0;
  color: rgba(255, 255, 255, 0.82);
}
.ghost-panel__actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
.ghost-panel__actions button {
  width: 100%;
}
.leaderboard-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
  margin-bottom: 12px;
}
.leaderboard-toggle {
  display: flex;
  gap: 8px;
  align-items: center;
}
.tab {
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 800;
}
.leaderboard-toggle .tab {
  background: #273244;
  color: var(--text);
}
.leaderboard-toggle .tab.active {
  background: var(--accent);
  color: #111;
}
.leaderboard {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 8px;
}
.leaderboard li {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 12px 14px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--line);
}
.leaderboard li.leaderboard-entry--with-action {
  grid-template-columns: 36px minmax(0, 1fr) auto auto;
}
.leaderboard li.is-selected-ghost {
  border-color: rgba(71, 209, 255, 0.7);
  background: rgba(71, 209, 255, 0.09);
}
.rank { color: var(--accent); font-weight: 800; }
.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.time { font-variant-numeric: tabular-nums; white-space: nowrap; }
.ghost-pick-btn {
  padding: 8px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 800;
  min-width: 86px;
}
.ghost-pick-btn.secondary {
  background: #273244;
}
.ghost-pick-btn.is-active {
  background: rgba(71, 209, 255, 0.96);
  color: #07202a;
}

.screen-race {
  display: block;
}
.game-shell {
  position: relative;
  min-height: 100vh;
  overflow: hidden;
  background: #081017;
}
.game-shell.is-masked canvas,
.game-shell.is-masked .race-hud {
  filter: blur(8px) saturate(0.8);
}
canvas {
  display: block;
  width: 100%;
  height: 100dvh;
  background: #285d29;
  touch-action: none;
}
.race-hud {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  left: max(16px, env(safe-area-inset-left));
  right: max(16px, env(safe-area-inset-right));
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  z-index: 4;
}
.hud-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}
.hud-stack {
  display: grid;
  gap: 2px;
  padding: 14px 16px;
  border-radius: 18px;
  background: rgba(0, 0, 0, 0.42);
  border: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(5px);
}
.hud-stack strong {
  font-size: clamp(1.4rem, 2vw, 2rem);
  font-variant-numeric: tabular-nums;
}
.hud-stack span {
  font-size: 1.02rem;
  font-weight: 700;
}
.hud-stack small {
  color: rgba(255, 255, 255, 0.82);
  font-size: 0.82rem;
}
.icon-btn {
  width: 54px;
  height: 54px;
  padding: 0;
  border-radius: 16px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  font-size: 1.3rem;
  line-height: 1;
}
#pauseBtn {
  font-size: 1.15rem;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 6;
  overflow-y: auto;
  padding: 20px 14px;
}
.overlay-panel {
  background: rgba(7, 11, 17, 0.34);
  backdrop-filter: blur(8px);
}
.overlay-card {
  width: min(90vw, 420px);
  padding: 24px;
  border-radius: 26px;
  background: var(--panel-strong);
  border: 1px solid var(--line);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  text-align: center;
}
.overlay-card--wide {
  width: min(94vw, 620px);
}
.overlay-card h2 { margin-top: 0; }
.overlay-actions {
  display: grid;
  gap: 10px;
  margin-top: 16px;
}
.countdown {
  font-size: clamp(5rem, 11vw, 9rem);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  background: rgba(0, 0, 0, 0.25);
}
.finish-time {
  margin: 0 0 10px;
  font-size: clamp(2.2rem, 5vw, 3.6rem);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}
.finish-rank {
  margin: 0 0 18px;
  color: var(--accent-2);
  font-weight: 700;
}
.score-form {
  display: grid;
  gap: 10px;
  text-align: left;
}
.score-form__actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 8px;
}
.score-form__actions button {
  width: 100%;
}
.finish-leaderboard {
  margin-top: 18px;
}
.finish-leaderboard h3 {
  margin: 0;
}

.touch-ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 7;
}
.game-shell.is-finish-active .touch-ui,
.game-shell.is-pause-active .touch-ui {
  z-index: 3;
}
.touch-left, .touch-right {
  position: absolute;
  bottom: max(20px, env(safe-area-inset-bottom));
  display: flex;
  gap: 12px;
  pointer-events: auto;
}
.touch-left { left: 18px; }
.touch-right { right: 18px; }
.touch-btn {
  width: 92px;
  height: 92px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.14);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.18);
  backdrop-filter: blur(6px);
  font-size: 1.8rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px;
  touch-action: none;
}
.touch-btn small {
  font-size: 0.74rem;
  line-height: 1;
  font-weight: 700;
}
.touch-btn--pedal {
  width: 92px;
  height: 92px;
}
.touch-btn:active,
.touch-btn.is-active {
  background: rgba(255, 179, 0, 0.92);
  color: #111;
}

@media (max-width: 820px) {
  .game-shell {
    min-height: 100dvh;
  }
  .menu-card {
    padding: 18px;
    border-radius: 22px;
  }
  .track-picker {
    margin-top: 10px;
  }
  .track-carousel {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .track-carousel__viewport {
    min-height: 260px;
  }
  .track-carousel__arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 5;
    width: 42px;
    height: 42px;
  }
  .track-carousel__arrow--prev {
    left: 6px;
  }
  .track-carousel__arrow--next {
    right: 6px;
  }
  .track-card {
    width: min(100%, 340px);
  }
  .track-card[data-offset="-2"],
  .track-card[data-offset="2"] {
    opacity: 0;
    pointer-events: none;
  }
  .track-card[data-offset="-1"] {
    transform: translateX(-28%) scale(0.86);
  }
  .track-card[data-offset="1"] {
    transform: translateX(28%) scale(0.86);
  }
  .race-hud {
    top: max(10px, calc(env(safe-area-inset-top) + 2px));
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    gap: 8px;
  }
  .hud-stack {
    min-width: 0;
    padding: 10px 12px;
    gap: 1px;
  }
  .hud-stack strong {
    font-size: 1.1rem;
  }
  .hud-stack span {
    font-size: 0.92rem;
  }
  .hud-stack small {
    font-size: 0.72rem;
  }
  .pause-btn {
    min-width: 88px;
    padding: 11px 12px;
    font-size: 0.82rem;
  }
  .hud-actions {
    gap: 8px;
  }
  .icon-btn {
    width: 46px;
    height: 46px;
    border-radius: 14px;
    font-size: 1.08rem;
  }
  .score-form__actions {
    grid-template-columns: 1fr;
  }
  .medal-slot {
    grid-template-columns: auto minmax(0, 1fr);
  }
  .medal-slot__actions {
    grid-column: 1 / -1;
    justify-content: stretch;
  }
  .medal-slot__actions button {
    width: 100%;
  }
  .overlay {
    align-items: start;
    padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom)) 10px;
  }
  .overlay-card,
  .overlay-card--wide {
    width: min(100%, 480px);
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 20px);
    overflow-y: auto;
  }
  .leaderboard {
    gap: 6px;
  }
  .leaderboard li {
    gap: 8px;
    padding: 10px 11px;
    grid-template-columns: 28px minmax(0, 1fr) auto;
  }
  .leaderboard li.leaderboard-entry--with-action {
    grid-template-columns: 28px minmax(0, 1fr) auto auto;
  }
  .rank,
  .time,
  .name {
    font-size: 0.88rem;
  }
  .ghost-pick-btn {
    min-width: 68px;
    padding: 7px 9px;
    font-size: 0.72rem;
  }
  .leaderboard-head {
    flex-direction: column;
    align-items: stretch;
  }
  .touch-btn {
    width: 80px;
    height: 80px;
  }
  .touch-btn--pedal {
    width: 80px;
    height: 80px;
  }
  .touch-btn small {
    display: none;
  }
}

@media (max-width: 380px) {
  .leaderboard li,
  .leaderboard li.leaderboard-entry--with-action {
    grid-template-columns: 26px minmax(0, 1fr);
  }
  .leaderboard .time,
  .leaderboard .ghost-pick-btn {
    grid-column: 2;
  }
  .leaderboard .name {
    white-space: normal;
    line-height: 1.15;
  }
}

