:root {
  --bg: #14111f;
  --panel: rgba(28, 23, 44, 0.92);
  --panel-2: rgba(44, 37, 68, 0.9);
  --line: rgba(255, 255, 255, 0.12);
  --text: #f4f0ff;
  --dim: #a9a2c4;
  --gold: #ffc93c;
  --gold-2: #ff9f1c;
  --red: #ff5d6c;
  --green: #7bd88f;
  --radius: 16px;
  --font: 'Nunito', system-ui, -apple-system, sans-serif;
  --display: 'Fredoka', 'Nunito', system-ui, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }
body { overflow: hidden; overscroll-behavior: none; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; }
a { color: var(--gold); }
[hidden] { display: none !important; }

#stage { position: fixed; inset: 0; }
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* ------------------------------------------------------------ screens */
#ui {
  position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden;
  padding: calc(14px + var(--safe-t)) calc(16px + var(--safe-r)) calc(18px + var(--safe-b)) calc(16px + var(--safe-l));
  background: radial-gradient(ellipse at 50% 30%, rgba(20, 17, 31, 0.35), rgba(20, 17, 31, 0.88) 70%);
}
body[data-screen='game'] #ui { display: none; }

.home {
  min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; text-align: center;
}
.logo { display: flex; align-items: center; gap: 14px; }
.logo h1 {
  margin: 0; font-family: var(--display); font-weight: 700; font-size: clamp(52px, 11vw, 104px); line-height: 0.86; letter-spacing: 1px;
  color: var(--gold); text-shadow: 0 5px 0 #b35c00, 0 10px 30px rgba(255, 160, 40, 0.35); text-align: left;
}
.logo-hand { font-size: clamp(56px, 12vw, 110px); filter: drop-shadow(0 6px 16px rgba(0, 0, 0, 0.5)); animation: wave 2.4s ease-in-out infinite; transform-origin: 70% 90%; }
@keyframes wave { 0%, 60%, 100% { transform: rotate(0); } 70% { transform: rotate(14deg); } 80% { transform: rotate(-8deg); } 90% { transform: rotate(10deg); } }
.tagline { margin: 0 0 6px; color: var(--dim); font-size: 17px; max-width: 30ch; }
.menu { display: flex; flex-direction: column; gap: 10px; width: min(340px, 100%); }
.home-foot { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }
.credit { color: var(--dim); font-size: 13px; margin: 4px 0 0; }

.btn {
  appearance: none; border: 0; cursor: pointer; border-radius: 14px; padding: 13px 20px; font-weight: 800; font-size: 16px;
  background: var(--panel-2); color: var(--text); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.35), 0 1px 0 rgba(255, 255, 255, 0.08) inset;
  transition: transform 0.08s ease, filter 0.15s;
}
.btn:hover { filter: brightness(1.15); }
.btn:active { transform: translateY(2px); box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.35); }
.btn.primary { background: linear-gradient(180deg, var(--gold), var(--gold-2)); color: #2a1600; }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 2px var(--line); }
.btn.big { font-size: 19px; padding: 16px 22px; font-family: var(--display); letter-spacing: 0.3px; }
.btn.icon { padding: 8px 12px; font-size: 14px; }
.chip {
  border: 0; cursor: pointer; background: var(--panel); border-radius: 999px; padding: 9px 15px; font-weight: 700; font-size: 14px;
  box-shadow: inset 0 0 0 1px var(--line); display: inline-flex; gap: 6px; align-items: center;
}
.dim { color: var(--dim); }
.small { font-size: 13px; }
.center { text-align: center; display: flex; justify-content: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.screen { max-width: 960px; margin: 0 auto; }
.bar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; position: sticky; top: calc(-14px - var(--safe-t)); padding: 8px 0; z-index: 2; }
.bar h2 { margin: 0; font-family: var(--display); font-size: 26px; flex: 1; }
.btn.back { padding: 9px 14px; }
.total { font-weight: 800; color: var(--gold); white-space: nowrap; }

.star { font-style: normal; color: rgba(255, 255, 255, 0.18); }
.star.on { color: var(--gold); text-shadow: 0 0 10px rgba(255, 200, 60, 0.6); }
.stars { display: inline-flex; gap: 2px; font-size: 16px; }

/* level select */
.tabs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.tab {
  flex: 1 0 auto; min-width: 130px; border: 0; cursor: pointer; text-align: left; border-radius: 12px; padding: 10px 12px;
  background: var(--panel); box-shadow: inset 0 0 0 1px var(--line); display: flex; flex-direction: column; gap: 2px;
}
.tab small { color: var(--dim); font-weight: 700; }
.tab.on { box-shadow: inset 0 0 0 2px var(--wc), 0 0 22px -6px var(--wc); }
.tab.on b { color: var(--wc); }
.tab.locked { opacity: 0.55; }
.world-blurb { grid-column: 1 / -1; margin: 4px 0 2px; color: var(--wc); font-weight: 800; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: 8px; }
.level {
  position: relative; border: 0; cursor: pointer; border-radius: 14px; padding: 12px; min-height: 112px; text-align: left;
  background: var(--panel); box-shadow: inset 0 0 0 1px var(--line), inset 0 -3px 0 rgba(0, 0, 0, 0.3);
  display: flex; flex-direction: column; gap: 4px; transition: transform 0.1s;
}
.level:hover:not(:disabled) { transform: translateY(-2px); box-shadow: inset 0 0 0 2px var(--wc); }
.level .num { font-family: var(--display); font-size: 26px; font-weight: 700; color: var(--wc); line-height: 1; }
.level .name { font-weight: 800; font-size: 14px; }
.level .meta, .level .rate { font-size: 12px; color: var(--dim); font-weight: 700; }
.level .rate { position: absolute; top: 12px; right: 12px; }
.level.locked { cursor: not-allowed; opacity: 0.45; }
.level.done { background: linear-gradient(160deg, color-mix(in srgb, var(--wc) 16%, transparent), var(--panel) 60%); }

/* leaderboard */
.board-controls { display: flex; flex-direction: column; gap: 10px; align-items: center; margin-bottom: 14px; }
.seg { display: inline-flex; background: var(--panel); border-radius: 12px; padding: 4px; gap: 4px; box-shadow: inset 0 0 0 1px var(--line); }
.seg button { border: 0; background: transparent; cursor: pointer; padding: 8px 14px; border-radius: 9px; font-weight: 800; color: var(--dim); }
.seg button.on { background: var(--gold); color: #2a1600; }
.seg.small button { padding: 6px 11px; font-size: 13px; }
.level-pick { display: flex; align-items: center; gap: 12px; font-weight: 800; }
.arrow { border: 0; background: var(--panel-2); width: 38px; height: 38px; border-radius: 10px; font-size: 22px; cursor: pointer; }
.board { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; }
.board th, .board td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--line); }
.board th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--dim); }
.board td.rank { width: 48px; font-weight: 800; }
.board tr.me td { background: rgba(255, 201, 60, 0.13); font-weight: 800; }
.board tr.gap td { text-align: center; color: var(--dim); padding: 4px; }
.tag { color: var(--dim); font-weight: 600; margin-left: 4px; font-size: 12px; }
.empty { text-align: center; color: var(--dim); padding: 30px 10px; }

/* profile */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; }
.card { background: var(--panel); border-radius: var(--radius); padding: 16px; box-shadow: inset 0 0 0 1px var(--line); }
.card h3 { margin: 0 0 10px; font-family: var(--display); font-size: 19px; }
.card p { margin: 0 0 10px; }
.text {
  flex: 1; min-width: 0; background: rgba(0, 0, 0, 0.35); border: 1px solid var(--line); color: var(--text); border-radius: 12px;
  padding: 12px; font: inherit; font-weight: 700; font-size: 16px;
}
.text:focus { outline: 2px solid var(--gold); }
.mono, .savecode { font-family: ui-monospace, 'SF Mono', Menlo, monospace; letter-spacing: 0.04em; }
.savecode { flex: 1; min-width: 0; background: rgba(0, 0, 0, 0.35); padding: 12px; border-radius: 12px; font-size: 14px; overflow-wrap: anywhere; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(80px, 1fr)); gap: 8px; }
.stat { display: flex; flex-direction: column; align-items: center; background: rgba(0, 0, 0, 0.25); border-radius: 12px; padding: 10px 6px; }
.stat .icon { font-size: 18px; }
.stat b { font-size: 19px; font-family: var(--display); }
.stat small { color: var(--dim); font-weight: 700; }

/* how to */
.steps { list-style: none; padding: 0; margin: 0 0 16px; display: grid; gap: 10px; }
.steps li { display: flex; gap: 14px; align-items: flex-start; background: var(--panel); border-radius: var(--radius); padding: 14px; box-shadow: inset 0 0 0 1px var(--line); }
.steps .icon { font-size: 30px; width: 44px; text-align: center; flex: none; color: var(--gold); }
.steps b { font-family: var(--display); font-size: 18px; }
.steps p { margin: 3px 0 0; color: var(--dim); }

/* ------------------------------------------------------------ modals */
#modals { position: absolute; inset: 0; pointer-events: none; z-index: 20; }
.modal-wrap {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; pointer-events: auto;
  background: rgba(8, 6, 16, 0.55); backdrop-filter: blur(3px); animation: fade 0.2s ease;
}
.modal {
  width: min(460px, 100%); max-height: 100%; overflow-y: auto; background: var(--panel); border-radius: 22px; padding: 22px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55), inset 0 0 0 1px var(--line); animation: pop 0.25s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.modal h2 { margin: 0 0 10px; font-family: var(--display); font-size: 28px; }
.modal h3 { margin: 0 0 8px; font-family: var(--display); }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(0.9); opacity: 0; } }
.ask p { margin: 0 0 16px; font-size: 16px; }
.ask .row { justify-content: flex-end; }
.loading { text-align: center; }
.loading small { color: var(--dim); }
.spinner { width: 34px; height: 34px; margin: 10px auto; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--gold); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.choose { display: flex; flex-direction: column; gap: 10px; }
.choice {
  border: 0; cursor: pointer; border-radius: 16px; padding: 14px 16px; text-align: left; background: var(--panel-2);
  display: grid; grid-template-columns: 48px 1fr; column-gap: 12px; align-items: center; box-shadow: inset 0 0 0 1px var(--line);
}
.choice .big { grid-row: span 2; font-size: 34px; text-align: center; }
.choice b { font-size: 17px; }
.choice small { color: var(--dim); }
.choice.primary { box-shadow: inset 0 0 0 2px var(--gold); }
.pause .menu { width: 100%; }
.settings { display: flex; flex-direction: column; gap: 12px; }
.toggle { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-weight: 800; }

/* result */
.result { text-align: center; }
.result > small { color: var(--dim); font-weight: 800; }
.result h2 { margin: 4px 0 12px; }
.result-stars { display: flex; justify-content: center; gap: 12px; margin-bottom: 12px; }
.rstar { display: flex; flex-direction: column; align-items: center; width: 92px; }
.rstar i { font-style: normal; font-size: 46px; color: rgba(255, 255, 255, 0.14); line-height: 1.1; }
.rstar.on i { color: var(--gold); animation: starIn 0.45s cubic-bezier(0.2, 1.6, 0.4, 1) both; animation-delay: inherit; text-shadow: 0 0 18px rgba(255, 201, 60, 0.7); }
.rstar small { color: var(--dim); font-weight: 700; font-size: 12px; }
@keyframes starIn { from { transform: scale(0) rotate(-60deg); opacity: 0; } }
.result-time b { font-family: var(--display); font-size: 34px; }
.badge { background: var(--green); color: #0c2a14; padding: 3px 9px; border-radius: 999px; font-weight: 800; font-size: 13px; margin-left: 8px; vertical-align: middle; }
.rank { font-weight: 800; min-height: 1.4em; }
.unlock { color: var(--green); font-weight: 800; }
.result-buttons { justify-content: center; margin-top: 10px; }

/* ------------------------------------------------------------ hud */
#hud { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
.hud-top {
  position: absolute; top: calc(8px + var(--safe-t)); left: calc(10px + var(--safe-l)); right: calc(10px + var(--safe-r));
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: start; gap: 8px;
}
.hud-left, .hud-right { display: flex; align-items: center; gap: 8px; }
.hud-right { justify-content: flex-end; }
.hud-left .btn { pointer-events: auto; }
.hud-level, .hud-coins, .hearts, .hud-keys {
  background: rgba(14, 11, 24, 0.7); border-radius: 12px; padding: 7px 11px; font-weight: 800; font-size: 15px; display: flex; gap: 7px; align-items: center;
}
.hud-level b { color: var(--gold); font-family: var(--display); }
.hud-mid { display: flex; flex-direction: column; align-items: center; background: rgba(14, 11, 24, 0.7); border-radius: 12px; padding: 4px 14px; }
.hud-time { font-family: var(--display); font-size: 24px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.hud-time.over { color: var(--dim); }
.hud-par { color: var(--dim); font-weight: 700; font-size: 11px; }
.coin { display: inline-block; width: 14px; height: 14px; border-radius: 50%; background: var(--gold); box-shadow: inset 0 0 0 2px #c98a12; }
.hearts { color: var(--red); letter-spacing: 2px; }
.hud-prompt {
  position: absolute; left: 50%; top: 58%; transform: translate(-50%, -50%); font-family: var(--display); font-weight: 700;
  font-size: clamp(20px, 3.4vw, 34px); text-align: center; text-shadow: 0 3px 0 rgba(0, 0, 0, 0.6), 0 0 18px rgba(0, 0, 0, 0.6);
  background: rgba(14, 11, 24, 0.55); padding: 10px 20px; border-radius: 16px; white-space: nowrap;
}
.hud-prompt.count { background: none; font-size: clamp(64px, 14vw, 140px); color: var(--gold); top: 45%; animation: count 1s ease-out infinite; }
@keyframes count { from { transform: translate(-50%, -50%) scale(1.25); } to { transform: translate(-50%, -50%) scale(1); } }
.hud-lost {
  position: absolute; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%); background: var(--red); color: #fff;
  font-weight: 800; padding: 9px 16px; border-radius: 999px; animation: blink 0.8s steps(2) infinite; white-space: nowrap;
}
@keyframes blink { 50% { opacity: 0.6; } }
.intro {
  position: absolute; left: 50%; top: 22%; transform: translateX(-50%); width: min(520px, calc(100% - 24px)); text-align: center;
  background: rgba(20, 16, 34, 0.86); border-radius: 20px; padding: 16px 20px; box-shadow: inset 0 0 0 2px var(--wc), 0 18px 40px rgba(0, 0, 0, 0.4);
  animation: pop 0.3s cubic-bezier(0.2, 1.4, 0.4, 1); transition: opacity 0.35s, transform 0.35s;
}
.intro.out { opacity: 0; transform: translate(-50%, -20px); }
.intro small { color: var(--wc); font-weight: 800; letter-spacing: 0.04em; }
.intro h2 { margin: 2px 0 6px; font-family: var(--display); font-size: 30px; }
.intro p { margin: 0 0 8px; color: #ddd6f5; }
.intro-meta { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; font-size: 13px; font-weight: 800; color: var(--dim); }
.intro-meta span { background: rgba(255, 255, 255, 0.07); padding: 4px 9px; border-radius: 999px; }

/* ------------------------------------------------------------ toasts */
#toasts { position: absolute; left: 50%; bottom: calc(20px + var(--safe-b)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 30; pointer-events: none; }
.toast { background: rgba(14, 11, 24, 0.92); padding: 10px 16px; border-radius: 12px; font-weight: 700; box-shadow: inset 0 0 0 1px var(--line); animation: pop 0.25s; transition: opacity 0.35s; max-width: 90vw; }
.toast.out { opacity: 0; }

.rotate-hint { display: none; }
@media (orientation: portrait) and (max-width: 700px) {
  body[data-screen='game'] .rotate-hint {
    display: block; position: absolute; left: 50%; bottom: calc(70px + var(--safe-b)); transform: translateX(-50%);
    background: rgba(14, 11, 24, 0.8); padding: 8px 14px; border-radius: 999px; font-weight: 700; font-size: 13px; white-space: nowrap; color: var(--dim);
  }
  .hud-top { grid-template-columns: 1fr auto; }
  .hud-mid { grid-row: 2; grid-column: 1 / -1; justify-self: center; }
  .hud-level span { display: none; }
}
@media (max-height: 460px) {
  .home { gap: 8px; }
  .logo h1 { font-size: 54px; }
  .logo-hand { font-size: 54px; }
  .menu { flex-direction: row; flex-wrap: wrap; width: min(640px, 100%); justify-content: center; }
  .menu .btn { flex: 1 1 40%; }
  .tagline { display: none; }
  .intro { top: 14%; padding: 10px 16px; }
  .intro h2 { font-size: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
