:root {
  --bg: #fff7ec;
  --panel: #ffffff;
  --ink: #1f2a44;
  --muted: #5b6475;
  --primary: #e8590c;
  --primary-ink: #ffffff;
  --good: #2b8a3e;
  --bad: #c92a2a;
  --floor: #d9a066;
  --hoop: #e8590c;
  --ball: #f08c00;
  --border: #f0d9bd;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1f2a;
    --panel: #262c3a;
    --ink: #f1f3f7;
    --muted: #aab2c2;
    --good: #69db7c;
    --bad: #ff8787;
    --floor: #8a5a2b;
    --border: #3a4256;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-size: 18px;
  line-height: 1.4;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
}

.screen { display: none; }
.screen.active { display: block; }

h1, h2 { text-align: center; margin: 12px 0; }
h1 { font-size: 2rem; }
.lead { text-align: center; color: var(--muted); }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 700;
  font-size: 1.1rem;
  padding: 12px 20px;
  border-radius: 14px;
  border: 2px solid var(--border);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  min-height: 52px;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn:disabled { cursor: default; }
.link-btn {
  font: inherit; background: none; border: none; color: var(--primary);
  font-weight: 700; cursor: pointer; padding: 4px 0;
}

/* Level select */
.level-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 16px;
}
.level-card {
  font: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 16px 8px;
  border-radius: 16px;
  border: 3px solid var(--primary);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
}
.level-card.locked { border-color: var(--border); opacity: 0.6; cursor: default; }
.level-num {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--ball); color: #fff; font-weight: 800; font-size: 1.3rem;
  display: flex; align-items: center; justify-content: center;
}
.level-card.locked .level-num { background: var(--muted); }
.level-name { font-weight: 800; font-size: 1.2rem; }
.level-stage, .level-best { font-size: 0.9rem; color: var(--muted); }

/* Scoreboard */
.scoreboard {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: var(--ink);
  color: var(--bg);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 0.95rem;
}
.scoreboard .label { opacity: 0.7; }
.scoreboard .link-btn { color: #ffb36b; }

/* Court + ball */
.court { position: relative; margin: 12px 0; }
.court-svg { width: 100%; height: auto; display: block; }
.ball {
  position: absolute;
  left: 21%;
  top: 58%;
  z-index: 2;
  font-size: 2.2rem;
  line-height: 1;
  transform: translate(-50%, -50%);
}
.ball.shot-in { animation: shot-in 0.9s ease-in-out forwards; }
.ball.shot-miss { animation: shot-miss 1s ease-in-out forwards; }

@keyframes shot-in {
  0%   { left: 21%; top: 58%; }
  50%  { left: 45%; top: 0%; }
  80%  { left: 74%; top: 26%; }
  100% { left: 74%; top: 72%; }
}
@keyframes shot-miss {
  0%   { left: 21%; top: 58%; }
  50%  { left: 45%; top: 0%; }
  75%  { left: 82%; top: 22%; }
  100% { left: 96%; top: 72%; }
}

/* Question */
.prompt { text-align: center; font-size: 1.3rem; margin: 8px 0 16px; }
.big-number { font-size: 2.4rem; color: var(--primary); display: inline-block; padding: 0 4px; }

.choices { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.choice { min-width: 96px; font-size: 1.4rem; }
.choice.right { background: var(--good); border-color: var(--good); color: #fff; }
.choice.wrong { background: var(--bad); border-color: var(--bad); color: #fff; }

.type-form { display: flex; gap: 12px; justify-content: center; }
.type-form input {
  font: inherit;
  font-size: 1.6rem;
  font-weight: 700;
  width: 9ch;
  text-align: center;
  padding: 8px;
  border-radius: 14px;
  border: 3px solid var(--border);
  background: var(--panel);
  color: var(--ink);
}
.type-form input:focus { outline: none; border-color: var(--primary); }

.feedback { text-align: center; font-weight: 800; font-size: 1.3rem; min-height: 1.8em; margin-top: 12px; }
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--bad); }

.hint { text-align: center; color: var(--ink); }
.hint .numline { width: 100%; max-width: 420px; height: auto; }
.hint p { margin: 4px 0 12px; }

#btn-next { display: block; margin: 8px auto 0; }

/* Results */
.result-score { text-align: center; font-size: 2rem; font-weight: 800; color: var(--primary); margin: 8px 0; }
#result-msg { text-align: center; }
.result-actions { display: flex; flex-direction: column; gap: 12px; max-width: 320px; margin: 20px auto 0; }

@media (prefers-reduced-motion: reduce) {
  .ball.shot-in, .ball.shot-miss { animation-duration: 0.01s; }
}

/* Medals on level cards */
.level-card { position: relative; }
.level-medal { position: absolute; top: 6px; right: 8px; font-size: 1.6rem; }

.trophy-btn { display: block; width: 100%; margin-top: 16px; background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* Results */
.result-medal { text-align: center; font-size: 1.2rem; }
.medal-big { display: block; font-size: 3.5rem; line-height: 1.1; }
.result-new { text-align: center; }
.result-new h3 { margin: 12px 0 6px; }
.new-trophy { font-weight: 700; font-size: 1.1rem; margin: 4px 0; }
.new-trophy span { font-size: 1.6rem; vertical-align: middle; }

/* Trophy room */
.shelf-title { margin: 20px 0 8px; border-bottom: 4px solid var(--floor); padding-bottom: 4px; }
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.medal-shelf { grid-template-columns: repeat(3, 1fr); }
.shelf-item {
  display: flex; flex-direction: column; align-items: center; text-align: center; gap: 2px;
  padding: 12px 6px; border-radius: 14px; background: var(--panel); border: 2px solid var(--border);
}
.shelf-item.won { border-color: var(--ball); box-shadow: 0 0 0 3px rgba(240, 140, 0, 0.18); }
.shelf-item.empty { opacity: 0.55; }
.shelf-item.empty .shelf-icon { filter: grayscale(1); }
.shelf-icon { font-size: 2.6rem; line-height: 1.1; }
.shelf-name { font-weight: 800; }
.shelf-desc { font-size: 0.85rem; color: var(--muted); }

/* Trophy pop-up */
.toast {
  position: fixed; left: 50%; bottom: 16px; transform: translate(-50%, calc(100% + 24px));
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 14px;
  font-size: 1.05rem; max-width: calc(100% - 32px); box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  transition: transform 0.35s ease, visibility 0.35s; z-index: 10; text-align: center;
  visibility: hidden;
}
.toast.show { transform: translate(-50%, 0); visibility: visible; }
.toast-icon { font-size: 1.5rem; vertical-align: middle; }

/* Gold counter (top right) */
.topbar { display: flex; justify-content: flex-end; margin: 4px 0 10px; }
.gold-counter {
  position: relative;
  display: inline-flex; align-items: center; gap: 10px;
  padding: 6px 18px 6px 8px;
  border-radius: 999px;
  background: linear-gradient(180deg, #2a3557 0%, #1a2240 100%);
  border: 3px solid #f2b705;
  box-shadow: 0 4px 0 #b37400, 0 6px 14px rgba(0, 0, 0, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.15);
  color: #ffd23f;
}
.coin { width: 40px; height: 40px; flex: none; filter: drop-shadow(0 2px 1px rgba(0,0,0,0.35)); }
.gold-text { display: flex; flex-direction: column; align-items: flex-start; line-height: 1; }
.gold-label {
  font-size: 0.7rem; font-weight: 800; letter-spacing: 0.14em; text-transform: uppercase; color: #f7e3a1; opacity: 0.85;
}
.gold-count {
  font-size: 1.6rem; font-weight: 900; font-variant-numeric: tabular-nums;
  min-width: 2.5ch; text-shadow: 0 2px 0 #8a5a00;
}
.gold-counter.bump .coin { animation: coin-spin 0.6s ease-out; }
.gold-counter.bump .gold-count { animation: count-bump 0.45s ease-out; }
@keyframes coin-spin {
  0% { transform: rotateY(0) scale(1); }
  50% { transform: rotateY(180deg) scale(1.25); }
  100% { transform: rotateY(360deg) scale(1); }
}
@keyframes count-bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.3); color: #fff3b0; }
  100% { transform: scale(1); }
}
.gold-pop {
  position: absolute; right: 16px; top: 100%; margin-top: 4px;
  color: #ffd23f; font-weight: 900; font-size: 1.3rem;
  text-shadow: 0 2px 0 #8a5a00, 0 0 6px rgba(0,0,0,0.3);
  opacity: 0; pointer-events: none;
}
.gold-pop.show { animation: gold-pop 1s ease-out; }
@keyframes gold-pop {
  0% { opacity: 0; transform: translateY(-14px) scale(0.8); }
  20% { opacity: 1; transform: translateY(0) scale(1.15); }
  100% { opacity: 0; transform: translateY(18px) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .gold-counter.bump .coin, .gold-counter.bump .gold-count, .gold-pop.show { animation: none; }
}

/* Player on the court */
.court-player { position: absolute; left: 2%; bottom: 16.7%; height: 55%; aspect-ratio: 100 / 140; }
.court-player svg { width: 100%; height: 100%; display: block; }

/* Top bar avatar button */
.topbar { justify-content: space-between; align-items: center; }
.avatar-btn {
  font: inherit; cursor: pointer; display: inline-flex; align-items: center; gap: 8px;
  background: var(--panel); color: var(--ink); border: 3px solid var(--primary);
  border-radius: 999px; padding: 3px 14px 3px 3px; font-weight: 800;
  box-shadow: 0 4px 0 #b8460a;
}
.topbar-avatar { width: 40px; height: 40px; border-radius: 50%; overflow: hidden; background: #ffe8cc; display: block; }
.topbar-avatar svg { width: 100%; height: 100%; display: block; }

.home-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 16px; }
.home-buttons .trophy-btn { margin-top: 0; }
.locker-btn { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }

/* Locker room */
.locker-stage {
  display: flex; justify-content: center; margin: 8px 0 12px; padding: 12px 0 0;
  background: radial-gradient(ellipse at 50% 95%, rgba(0,0,0,0.12) 0 22%, transparent 23%),
              linear-gradient(180deg, #ffe8cc 0%, #ffd8a8 100%);
  border-radius: 20px; border: 3px solid var(--border);
}
.big-avatar { width: 150px; height: auto; display: block; }
.slot-tabs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: thin; }
.slot-tab {
  font: inherit; font-weight: 700; font-size: 0.95rem; white-space: nowrap; cursor: pointer;
  padding: 8px 12px; border-radius: 999px; border: 2px solid var(--border);
  background: var(--panel); color: var(--ink);
}
.slot-tab.active { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.item-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 10px; }
.item-card {
  display: flex; flex-direction: column; border-radius: 14px; background: var(--panel);
  border: 3px solid var(--border); overflow: hidden;
}
.item-card.wearing { border-color: var(--good); }
.item-card.trying { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(232, 89, 12, 0.2); }
.item-pick {
  font: inherit; color: var(--ink); background: none; border: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 4px 4px;
}
.item-preview { width: 64px; height: 64px; display: flex; align-items: center; justify-content: center; }
.item-svg { width: 100%; height: 100%; }
.swatch { width: 48px; height: 48px; border-radius: 50%; border: 3px solid rgba(0,0,0,0.15); display: block; }
.none-mark { font-size: 2rem; color: var(--muted); }
.item-name { font-weight: 800; font-size: 0.9rem; text-align: center; }
.item-foot { padding: 4px 6px 8px; display: flex; flex-direction: column; align-items: center; gap: 2px; min-height: 40px; justify-content: center; }
.tag { font-size: 0.8rem; color: var(--muted); font-weight: 700; }
.wearing-tag { color: var(--good); }
.buy-btn {
  font: inherit; font-weight: 900; cursor: pointer; display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 12px; border-radius: 999px; border: 2px solid #b37400;
  background: linear-gradient(180deg, #ffe066, #f2b705); color: #5c3d00;
}
.buy-btn:disabled { cursor: default; filter: grayscale(0.8); opacity: 0.6; }
.mini-coin {
  width: 14px; height: 14px; border-radius: 50%; display: inline-block;
  background: radial-gradient(circle at 35% 35%, #fff6c2 0 20%, #ffd23f 21% 60%, #e09b00 61%);
  border: 1.5px solid #b37400;
}
.need { font-size: 0.72rem; color: var(--muted); }

/* Economy */
.level-pay { display: inline-flex; align-items: center; gap: 4px; font-size: 0.8rem; font-weight: 800; color: #b37400; }
.level-card.locked .level-pay { color: var(--muted); }
.result-gold {
  max-width: 320px; margin: 10px auto; padding: 10px 14px; border-radius: 14px;
  background: linear-gradient(180deg, #2a3557 0%, #1a2240 100%); color: #f7e3a1; border: 3px solid #f2b705;
}
.pay-row { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; }
.pay-row b { color: #ffd23f; }
.pay-row.total { border-top: 1px solid rgba(255,255,255,0.2); margin-top: 4px; padding-top: 6px; font-weight: 800; font-size: 1.1rem; }
.pay-row.total span { display: inline-flex; align-items: center; gap: 6px; }
.tier {
  font-size: 0.65rem; font-weight: 900; letter-spacing: 0.08em; text-transform: uppercase;
  padding: 1px 8px; border-radius: 999px; color: #fff;
}
.tier-rookie { background: #2b8a3e; }
.tier-pro { background: #1c7ed6; }
.tier-allstar { background: #7048e8; }
.tier-legend { background: linear-gradient(90deg, #f08c00, #e03131); }

/* Player name */
.avatar-btn-label { max-width: 9.5rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.95rem; }
.name-form { margin: 0 0 14px; }
.name-label { display: block; font-weight: 800; margin-bottom: 6px; }
.name-row { display: flex; gap: 8px; }
.name-row input {
  flex: 1; min-width: 0; font: inherit; font-size: 1.15rem; font-weight: 700;
  padding: 8px 12px; border-radius: 14px; border: 3px solid var(--border);
  background: var(--panel); color: var(--ink);
}
.name-row input:focus { outline: none; border-color: var(--primary); }
.dice-btn { padding: 8px 12px; font-size: 1.4rem; }
.name-msg { min-height: 1.3em; margin: 6px 0 0; color: var(--muted); font-size: 0.95rem; }

/* Bonus clock / time trial countdown */
.clock { display: flex; align-items: center; gap: 10px; margin: 4px 0 8px; }
.clock-bar { flex: 1; height: 10px; border-radius: 999px; background: var(--border); overflow: hidden; }
.clock-fill { display: block; height: 100%; width: 100%; border-radius: 999px; background: #94d82d; }
.clock-label { font-size: 0.85rem; font-weight: 800; color: var(--muted); white-space: nowrap; min-width: 13.5em; text-align: right; }
.clock.bonus[data-tier="lightning"] .clock-fill { background: #f2b705; }
.clock.bonus[data-tier="lightning"] .clock-label { color: #b37400; }
.clock.bonus[data-tier="quick"] .clock-fill { background: #ff922b; }
.clock.bonus[data-tier="quick"] .clock-label { color: #d9480f; }
.clock.trial .clock-bar { height: 14px; }
.clock.trial .clock-fill { background: #1c7ed6; }
.clock.trial .clock-label { color: var(--ink); font-size: 1rem; min-width: 6.5em; }
.clock.trial.ending .clock-fill { background: #e8590c; }

/* Time trial card */
.trial-card {
  font: inherit; color: #fff; cursor: pointer; width: 100%; margin-top: 16px;
  display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 12px 16px; border-radius: 16px; border: none;
  background: linear-gradient(135deg, #1c7ed6, #7048e8);
  box-shadow: 0 4px 0 #3b2a9e;
}
.trial-icon { font-size: 2.2rem; }
.trial-text { flex: 1; display: flex; flex-direction: column; }
.trial-title { font-weight: 900; font-size: 1.25rem; }
.trial-sub { font-size: 0.9rem; opacity: 0.9; }
.trial-best { display: flex; flex-direction: column; align-items: center; font-size: 0.75rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; }
.trial-best b { font-size: 1.6rem; letter-spacing: 0; }

/* Time trial penalty flash */
.clock { position: relative; }
.clock-penalty {
  position: absolute; right: 0; top: -22px; font-weight: 900; font-size: 1.3rem;
  color: var(--bad); opacity: 0; pointer-events: none;
}
.clock-penalty.show { animation: penalty-pop 1s ease-out; }
@keyframes penalty-pop {
  0% { opacity: 0; transform: translateY(6px) scale(0.8); }
  20% { opacity: 1; transform: translateY(0) scale(1.2); }
  100% { opacity: 0; transform: translateY(-14px) scale(1); }
}
.clock.trial.penalised .clock-fill { background: var(--bad); }
