@font-face {
  font-family: Fredoka;
  src: url('../fonts/Fredoka.woff') format('woff');
  font-weight: 400 700;
  font-display: swap;
}
:root {
  --ink: #17213b;
  --ink-soft: #55607d;
  --card: #fffaf0;
  --card-edge: #f0e3c6;
  --chip: #f1e6cc;
  --chip-2: #f6efdd;
  --accent: #e5432e;
  --accent-ink: #ffffff;
  --accent-text: #c2341f;
  --shadow: rgba(14, 40, 70, .38);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #eef2ff; --ink-soft: #a9b4d4; --card: #17213b; --card-edge: #2a375e; --chip: #243157; --chip-2: #1d2849; --accent-text: #ff8d7a; --shadow: rgba(0, 0, 0, .55);
  }
}
:root[data-theme="dark"] { --ink: #eef2ff; --ink-soft: #a9b4d4; --card: #17213b; --card-edge: #2a375e; --chip: #243157; --chip-2: #1d2849; --accent-text: #ff8d7a; --shadow: rgba(0, 0, 0, .55); }

html, body {
  height: 100%; margin: 0; overflow: hidden; background: #1b8aa6;
  font-family: Fredoka, "Trebuchet MS", "Segoe UI", system-ui, sans-serif; color: var(--ink);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: none;
}
input { -webkit-user-select: text; user-select: text; }
#c { position: fixed; inset: 0; display: block; }
#safe { position: fixed; inset: 0; pointer-events: none; visibility: hidden; padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px); }
[hidden] { display: none !important; }

/* ---------- overlay + cards ---------- */
.overlay {
  position: fixed; inset: 0; display: none; align-items: flex-end; justify-content: flex-start; box-sizing: border-box; pointer-events: none;
  padding: calc(env(safe-area-inset-top, 0px) + 16px) calc(env(safe-area-inset-right, 0px) + 16px) calc(env(safe-area-inset-bottom, 0px) + 16px) calc(env(safe-area-inset-left, 0px) + 16px);
}
.overlay.show { display: flex; }
.card {
  pointer-events: auto; width: min(460px, 100%); max-height: 100%; overflow-y: auto; overscroll-behavior: contain; touch-action: pan-y; box-sizing: border-box;
  background: var(--card); border-radius: 26px; padding: 22px 22px 18px; box-shadow: 0 0 0 3px var(--card-edge), 0 18px 40px var(--shadow);
}
.card.small { width: min(420px, 100%); }
.card h1 { margin: 0; font-size: clamp(34px, 7vw, 50px); line-height: .95; font-weight: 700; letter-spacing: -.02em; color: var(--accent); transform: rotate(-2deg); transform-origin: left bottom; text-shadow: 0 3px 0 var(--card-edge); }
.card h2 { margin: 0; font-size: 28px; line-height: 1.05; font-weight: 700; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
.card p { margin: 10px 0 0; font-size: 16px; line-height: 1.35; color: var(--ink-soft); font-weight: 500; }
.card p.sub { margin-top: 2px; font-size: 14.5px; }
.card p.note { background: var(--chip); border-radius: 12px; padding: 8px 12px; font-size: 14.5px; }
.card p.hint, .card p.how { font-size: 13.5px; margin-top: 8px; }
.card p.err { color: var(--accent-text); font-weight: 600; min-height: 0; }
.card p.err:empty { display: none; }
.topline { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.chips { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.coins, .chip { display: inline-flex; align-items: center; gap: 7px; flex: none; background: var(--chip); border-radius: 999px; padding: 6px 13px 6px 8px; font-weight: 700; font-size: 16px; white-space: nowrap; color: var(--ink); }
.chip { appearance: none; border: 0; cursor: pointer; font: inherit; font-weight: 600; min-height: 36px; padding: 6px 12px; }
.chip svg { fill: currentColor; }
.chip.snd { padding: 6px 10px; }
.chip.snd[aria-pressed="false"] .on, .chip.snd[aria-pressed="true"] .off { display: none; }
.coin { width: 19px; height: 19px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #ffe58a, #f2b01e); box-shadow: inset 0 -2px 0 rgba(0,0,0,.2); flex: none; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: .06em; padding: 2px 6px; border-radius: 6px; background: #ffc83d; color: #3a2a00; vertical-align: middle; }
.draft { margin-left: 6px; font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: #8f5de2; color: #fff; vertical-align: middle; }
.lbl { margin: 14px 0 7px; font-size: 14px; font-weight: 600; color: var(--ink-soft); }
.banner { margin-top: 12px; padding: 9px 12px; border-radius: 12px; background: #1f6fd1; color: #fff; font-weight: 600; font-size: 14.5px; }
.banner.warn { background: #b4501b; }
.banner.preview { background: #8f5de2; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 0; cursor: pointer; font: inherit; font-weight: 700; font-size: 21px; width: 100%; margin-top: 14px; padding: 13px 18px; min-height: 52px;
  color: var(--accent-ink); background: var(--accent); border-radius: 18px; box-shadow: 0 5px 0 #a82a1a, 0 10px 18px rgba(168, 42, 26, .35);
  transition: transform .08s ease, box-shadow .08s ease; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn svg { fill: currentColor; }
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 #a82a1a; }
.btn.alt { background: var(--chip); color: var(--ink); box-shadow: 0 5px 0 var(--card-edge); }
.btn.alt:active { box-shadow: 0 1px 0 var(--card-edge); }
.btn.small { font-size: 17px; padding: 10px 12px; min-height: 46px; }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.row2 { display: flex; gap: 10px; }
.row2 .btn { margin-top: 12px; }
.link { appearance: none; border: 0; background: none; cursor: pointer; font: inherit; font-weight: 600; color: var(--accent-text); text-decoration: underline; padding: 12px 0 0; min-height: 44px; }
.icon { appearance: none; border: 0; background: var(--chip); color: var(--ink); width: 44px; height: 44px; border-radius: 50%; cursor: pointer; display: grid; place-items: center; flex: none; }
.icon svg { fill: currentColor; }
button:focus-visible, input:focus-visible { outline: 3px solid #ffc83d; outline-offset: 3px; }

/* ---------- forms ---------- */
form { display: grid; gap: 6px; margin-top: 14px; }
form label { font-weight: 600; font-size: 15px; color: var(--ink); margin-top: 6px; }
form input { font: inherit; font-size: 18px; font-weight: 600; padding: 12px 14px; border-radius: 14px; border: 2px solid var(--card-edge); background: var(--chip-2); color: var(--ink); min-height: 48px; box-sizing: border-box; }
.code { margin-top: 14px; font-size: 30px; font-weight: 700; letter-spacing: .08em; text-align: center; padding: 14px; border-radius: 16px; background: var(--chip); -webkit-user-select: all; user-select: all; }

/* ---------- segmented ---------- */
.seg { display: flex; background: var(--chip); border-radius: 14px; padding: 4px; gap: 4px; }
.seg button { appearance: none; border: 0; cursor: pointer; font: inherit; font-weight: 600; font-size: 15px; flex: 1; padding: 8px 6px; min-height: 40px; border-radius: 10px; background: transparent; color: var(--ink); }
.seg button[aria-checked="true"] { background: var(--accent-text); color: #fff; }

/* ---------- home ---------- */
.card.home { width: min(480px, 100%); display: flex; flex-direction: column; padding-bottom: 14px; overflow: hidden; }
.home-body { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.pane { display: flex; flex-direction: column; min-height: 0; }
.home[data-view="themes"] .p-tracks, .home[data-view="tracks"] .p-themes { display: none; }
.themes, .tracks { display: flex; flex-direction: column; gap: 9px; overflow-y: auto; min-height: 0; padding: 3px; margin: -3px; }
.theme, .track, .feature { appearance: none; border: 0; cursor: pointer; font: inherit; text-align: left; color: var(--ink); width: 100%; }
.theme { display: flex; align-items: center; gap: 12px; padding: 10px 12px 10px 10px; border-radius: 20px; background: var(--tc, var(--chip)); color: #fff; min-height: 92px; box-shadow: 0 4px 0 rgba(0,0,0,.18); }
.theme svg { fill: #fff; flex: none; }
.theme.locked { background: var(--chip); color: var(--ink-soft); box-shadow: none; }
.theme.locked .tname { color: var(--ink); }
.theme.sel { outline: 3px solid #ffc83d; outline-offset: 2px; }
.tthumb canvas { width: 72px; height: 72px; border-radius: 14px; display: block; }
.tinfo { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.tname { font-size: 19px; line-height: 1.1; font-weight: 700; }
.ttag { font-size: 14px; line-height: 1.25; font-weight: 500; }
.prog { display: flex; align-items: center; gap: 8px; margin-top: 3px; font-size: 13px; font-weight: 600; }
.prog .bar { flex: 1; height: 8px; border-radius: 4px; background: rgba(255,255,255,.28); overflow: hidden; }
.prog .bar i { display: block; height: 100%; background: #ffc83d; border-radius: 4px; }
.lock { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; font-weight: 600; color: var(--ink-soft); }
.lock svg { fill: currentColor; }
.feature { display: grid; grid-template-columns: auto 1fr; gap: 0 10px; padding: 10px 14px; border-radius: 16px; background: #ffc83d; color: #3a2a00; }
.feature .ftag { grid-row: span 2; align-self: center; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; background: #3a2a00; color: #ffc83d; padding: 3px 7px; border-radius: 7px; }
.feature .fname { font-weight: 700; font-size: 17px; }
.feature .fsub { font-size: 13px; font-weight: 600; }
.thead { display: flex; align-items: center; gap: 8px; margin: 12px 0 10px; }
.back { appearance: none; border: 0; background: var(--chip); color: var(--ink); cursor: pointer; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; flex: none; }
.back svg { fill: currentColor; }
.track { display: flex; align-items: center; gap: 12px; padding: 8px; border-radius: 18px; background: var(--chip); box-shadow: inset 0 0 0 3px transparent; }
.track canvas { width: 104px; height: 70px; border-radius: 12px; flex: none; display: block; }
.track.sel { box-shadow: inset 0 0 0 3px var(--accent); }
.track.locked { background: var(--chip-2); color: var(--ink-soft); }
.trow { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.track .tname { font-size: 17.5px; }
.stars { font-size: 14px; font-weight: 600; color: var(--accent-text); letter-spacing: .06em; }
.track.locked .stars { color: var(--ink-soft); }
.meta { font-size: 13.5px; font-weight: 500; color: var(--ink-soft); }
.medal { font-size: 12.5px; font-weight: 700; padding: 3px 8px; border-radius: 999px; color: #3a2a00; flex: none; }
.medal.m1 { background: #ffc83d; } .medal.m2 { background: #d6dbe3; } .medal.m3 { background: #e8b38a; } .medal.m4 { background: var(--card-edge); color: var(--ink); }
.blurb { font-size: 15px !important; }
.blurb small { display: block; margin-top: 4px; font-size: 12px; opacity: .85; }
.foot { margin-top: 4px; }
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.controls .seg button { font-size: 14px; padding: 8px 2px; }
.controls .lbl { margin-top: 10px; }
.actions .btn.go { margin-top: 14px; }

@media (min-width: 900px) and (min-height: 560px) {
  .overlay { align-items: center; justify-content: center; }
  .card.home { width: min(1060px, 100%); height: min(740px, 100%); }
  .home-body { display: grid; grid-template-columns: minmax(300px, 360px) 1fr; grid-template-rows: minmax(0, 1fr); gap: 24px; margin-top: 8px; }
  .home[data-view] .p-tracks, .home[data-view] .p-themes { display: flex; }
  .p-tracks .back { display: none; }
  .controls { grid-template-columns: 1fr 1fr; }
  .tracks { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); align-content: start; }
}

/* ---------- garage ---------- */
.card.garage { width: min(800px, 100%); }
.gg { display: grid; gap: 4px 22px; grid-template-columns: 1fr; margin-top: 8px; }
@media (min-width: 760px) { .gg { grid-template-columns: 1fr 1.05fr; } .overlay:has(.garage) { align-items: center; justify-content: center; } }
#carPrev { width: 100%; height: auto; aspect-ratio: 12 / 5; border-radius: 18px; display: block; background: #3f4452; }
.sw { display: flex; flex-wrap: wrap; gap: 8px; }
.sw button { appearance: none; border: 0; cursor: pointer; width: 32px; height: 32px; border-radius: 50%; padding: 0; box-shadow: inset 0 0 0 2px rgba(0,0,0,.16); }
.sw button[aria-checked="true"] { box-shadow: inset 0 0 0 2px rgba(0,0,0,.16), 0 0 0 3px var(--card), 0 0 0 6px var(--accent); }
.sw.sm button { width: 28px; height: 28px; }
.up, .devcar { display: grid; grid-template-columns: 1fr auto; gap: 3px 10px; align-items: center; padding: 10px 12px; background: var(--chip); border-radius: 14px; margin-bottom: 8px; }
.up .un, .devcar .un { font-weight: 700; font-size: 16.5px; }
.up .ud, .devcar .ud { grid-column: 1; font-size: 13.5px; color: var(--ink-soft); font-weight: 500; }
.pips { display: flex; gap: 4px; margin-top: 4px; grid-column: 1; }
.pip { width: 26px; height: 7px; border-radius: 4px; background: rgba(0,0,0,.15); }
.pip.on { background: var(--accent); }
.buy { appearance: none; border: 0; cursor: pointer; font: inherit; font-weight: 700; font-size: 16px; grid-column: 2; grid-row: 1 / span 3; align-self: center; display: inline-flex; align-items: center; gap: 6px; padding: 9px 13px; min-height: 44px; border-radius: 12px; background: var(--accent); color: #fff; box-shadow: 0 3px 0 #a82a1a; }
.buy:active { transform: translateY(2px); box-shadow: 0 1px 0 #a82a1a; }
.buy:disabled { background: var(--card); color: var(--ink-soft); box-shadow: inset 0 0 0 2px var(--card-edge); cursor: default; transform: none; }
.buy .coin { width: 15px; height: 15px; }
.devcar { background: #2b2f3a; color: #fff; }
.devcar .ud { color: #cfd6e6 !important; }
.toggle { appearance: none; border: 0; cursor: pointer; width: 58px; height: 34px; border-radius: 999px; background: #55607d; position: relative; padding: 0; }
.toggle span { position: absolute; top: 4px; left: 4px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle[aria-checked="true"] { background: #1f9d63; }
.toggle[aria-checked="true"] span { transform: translateX(24px); }

/* ---------- results ---------- */
.res-pos { font-size: clamp(56px, 13vw, 84px); font-weight: 700; line-height: .9; color: var(--accent); letter-spacing: -.03em; }
.res-pos small { font-size: .38em; color: var(--ink-soft); letter-spacing: 0; margin-left: 6px; }
.laps { margin: 14px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.laps li { display: flex; justify-content: space-between; padding: 8px 12px; background: var(--chip); border-radius: 12px; font-weight: 600; font-size: 16px; }
.laps li.fast { background: #ffc83d; color: #3a2a00; }
.laps li span:first-child { font-weight: 500; opacity: .85; }
.total { margin-top: 12px; display: flex; justify-content: space-between; font-size: 18px; font-weight: 600; }
.reward { margin-top: 12px; padding: 12px 14px; border-radius: 14px; background: var(--chip); display: grid; gap: 4px; font-size: 15.5px; font-weight: 500; }
.reward:empty { display: none; }
.reward div { display: flex; justify-content: space-between; }
.reward .sum { margin-top: 4px; padding-top: 6px; border-top: 2px solid var(--card-edge); font-weight: 700; font-size: 18px; align-items: center; }
.inl { display: inline-flex; align-items: center; gap: 6px; }
.unlocked { margin-top: 10px; padding: 10px 12px; border-radius: 12px; background: #1f9d63; color: #fff; font-weight: 600; }
.status { font-weight: 600; }
.status:empty { display: none; }
.status.err { color: var(--accent-text); }

/* ---------- leaderboard ---------- */
.board { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 5px; }
.board li { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 12px; background: var(--chip); font-weight: 600; }
.board li.me { background: #ffc83d; color: #3a2a00; }
.board li.muted { display: block; color: var(--ink-soft); font-weight: 500; }
.board .pos { color: var(--ink-soft); }
.board li.me .pos { color: inherit; }
.board .who { display: flex; gap: 6px; align-items: center; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .time { font-variant-numeric: tabular-nums; }
p.mine { font-weight: 600; color: var(--ink) !important; }

/* ---------- HUD buttons, pedals, toast ---------- */
#icons { position: fixed; z-index: 5; top: calc(env(safe-area-inset-top, 0px) + 10px); right: calc(env(safe-area-inset-right, 0px) + 10px); display: flex; gap: 8px; }
.ibtn { appearance: none; border: 0; cursor: pointer; width: 44px; height: 44px; border-radius: 50%; background: rgba(255, 250, 240, .9); color: #17213b; box-shadow: 0 3px 0 rgba(0,0,0,.18); display: grid; place-items: center; padding: 0; }
.ibtn svg { width: 20px; height: 20px; fill: currentColor; }
.ibtn[aria-pressed="false"] .on { display: none; }
.ibtn[aria-pressed="true"] .off { display: none; }
body:not(.racing) #icons { display: none; }
body.ingarage #icons { display: none; }
#pedals { position: fixed; z-index: 4; display: none; left: calc(env(safe-area-inset-left, 0px) + 14px); right: calc(env(safe-area-inset-right, 0px) + 14px); bottom: calc(env(safe-area-inset-bottom, 0px) + 14px); justify-content: space-between; align-items: flex-end; pointer-events: none; }
body.touch.racing #pedals { display: flex; }
.pedal { pointer-events: auto; border-radius: 28px; font-weight: 700; font-size: 20px; display: grid; place-items: center; color: #fff; text-shadow: 0 2px 0 rgba(0,0,0,.25); box-shadow: 0 5px 0 rgba(0,0,0,.25); opacity: .82; touch-action: none; }
.pedal.on { opacity: 1; transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,.25); }
#brakeP { width: 29vw; max-width: 170px; height: 96px; background: #d8392b; }
#gasP { width: 52vw; max-width: 300px; height: 130px; background: #1da35d; font-size: 26px; }
#toast { position: fixed; z-index: 9; left: 50%; bottom: calc(env(safe-area-inset-bottom, 0px) + 24px); transform: translate(-50%, 30px); opacity: 0; pointer-events: none; background: #17213b; color: #fff; padding: 10px 16px; border-radius: 14px; font-weight: 600; transition: opacity .2s, transform .2s; max-width: calc(100vw - 40px); text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) { .btn, .toggle span, #toast { transition: none; } }
@media (max-width: 560px) {
  .overlay { justify-content: center; padding-left: calc(env(safe-area-inset-left, 0px) + 8px); padding-right: calc(env(safe-area-inset-right, 0px) + 8px); padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 8px); }
  .card { padding: 18px 16px 14px; }
  .card.home { width: 100%; }
  .card.home h1 { font-size: 32px; }
  .card.home p.how { display: none; }
  .card.home .blurb { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .card.home .blurb small { display: none; }
  .chip span:first-of-type { max-width: 92px; overflow: hidden; text-overflow: ellipsis; }
}
@media (max-height: 700px) { .card p.how { display: none; } }
