:root {
  --bg: #f5f5f4;
  --surface: #ffffff;
  --surface-2: #f0efed;
  --text: #1c1917;
  --muted: #78716c;
  --border: #e7e5e4;
  --accent: #e11d48;
  --accent-ink: #ffffff;
  --green: #16a34a;
  --amber: #d97706;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 14px rgb(0 0 0 / .06);
  --radius: 16px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0c0a09;
  --surface: #1c1917;
  --surface-2: #292524;
  --text: #fafaf9;
  --muted: #a8a29e;
  --border: #33302d;
  --accent: #fb3b64;
  --green: #22c55e;
  --amber: #f59e0b;
  --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 4px 14px rgb(0 0 0 / .3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
input { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(10px);
}
.brand {
  display: flex; align-items: center; gap: 8px;
  background: none; border: 0; padding: 4px; font-weight: 800; font-size: 1.2rem;
}
.top-actions { display: flex; gap: 6px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface);
  display: grid; place-items: center;
}
.icon-btn svg { width: 20px; height: 20px; }

main { max-width: 560px; margin: 0 auto; padding: 4px 14px 24px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px;
}
h2 { font-size: 1rem; margin: 0 0 10px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }

/* ---------- Setup ---------- */
.setup { display: grid; gap: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 10px 16px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); font-weight: 700;
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.custom-score { width: 110px; padding: 10px 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); font-weight: 700; }
.players { display: grid; gap: 8px; margin-bottom: 10px; }
.player-row { display: flex; gap: 8px; align-items: center; }
.player-row .num { width: 26px; text-align: center; color: var(--muted); font-weight: 700; }
.player-row input {
  flex: 1; min-width: 0; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2);
}
.player-row button {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2);
}
.btn {
  border: 0; border-radius: 14px; padding: 14px 18px; font-weight: 800; font-size: 1.05rem;
  background: var(--surface-2); border: 1px solid var(--border);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.rules { color: var(--muted); font-size: .92rem; line-height: 1.5; margin: 0; }

/* ---------- Game ---------- */
.game { display: grid; gap: 12px; }
.scoreboard { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
.player {
  position: relative; padding: 12px 14px; border-radius: var(--radius);
  background: var(--surface); border: 2px solid var(--border); box-shadow: var(--shadow);
  transition: border-color .15s, transform .15s;
}
.player.active { border-color: var(--accent); transform: translateY(-1px); }
.player .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 12px; }
.player .score { font-size: 2.4rem; font-weight: 900; font-variant-numeric: tabular-nums; line-height: 1.1; }
.player .last { font-size: .8rem; color: var(--muted); min-height: 1.2em; }
.player .last.bust { color: var(--amber); font-weight: 700; }
.player.active::after {
  content: ""; position: absolute; top: 12px; right: 12px; width: 9px; height: 9px;
  border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 25%, transparent);
}

.turn { display: grid; gap: 10px; }
.turn-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.turn-head .who { font-size: 1.25rem; font-weight: 800; }
.turn-head .who small { display: block; font-size: .8rem; color: var(--muted); font-weight: 600; }
.turn-head .remain { text-align: right; }
.turn-head .remain b { font-size: 1.8rem; font-variant-numeric: tabular-nums; }
.turn-head .remain small { display: block; color: var(--muted); font-size: .75rem; }
.slots { display: grid; grid-template-columns: repeat(3, 1fr) 1.1fr; gap: 8px; }
.slot {
  height: 52px; border-radius: 12px; display: grid; place-items: center;
  background: var(--surface-2); border: 1px dashed var(--border); font-weight: 800; font-size: 1.1rem;
}
.slot.filled { border-style: solid; background: var(--surface); }
.slot.total { background: var(--text); color: var(--bg); border: 0; }

.mode-toggle { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 12px; padding: 4px; border: 1px solid var(--border); }
.mode-toggle button { border: 0; background: none; padding: 8px; border-radius: 9px; font-weight: 700; color: var(--muted); }
.mode-toggle button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.mults { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.mult { padding: 10px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2); font-weight: 800; }
.mult[aria-pressed="true"][data-m="2"] { background: var(--green); color: #fff; border-color: var(--green); }
.mult[aria-pressed="true"][data-m="3"] { background: var(--accent); color: #fff; border-color: var(--accent); }
.mult[aria-pressed="true"][data-m="1"] { background: var(--text); color: var(--bg); border-color: var(--text); }

.pad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.key {
  min-height: 52px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 800; font-size: 1.15rem; font-variant-numeric: tabular-nums;
  transition: transform .06s;
}
.key:active { transform: scale(.94); }
.key:disabled { opacity: .3; }
.key.wide { grid-column: span 2; }
.key.alt { background: var(--surface-2); font-size: .95rem; }
.key.go { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.numpad .key { min-height: 58px; font-size: 1.4rem; }
.entry {
  text-align: center; font-size: 2.6rem; font-weight: 900; font-variant-numeric: tabular-nums;
  padding: 6px; border-radius: 12px; background: var(--surface-2); min-height: 68px;
}
.entry.placeholder { color: var(--muted); }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.history { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; max-height: 260px; overflow: auto; }
.history li { display: flex; justify-content: space-between; gap: 8px; font-size: .9rem; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); }
.history .h-bust { color: var(--amber); font-weight: 700; }
.history .h-win { color: var(--green); font-weight: 800; }
.history .muted { color: var(--muted); }
details summary { cursor: pointer; font-weight: 700; color: var(--muted); list-style: none; }
details summary::-webkit-details-marker { display: none; }
details[open] summary { margin-bottom: 10px; }

/* ---------- Win ---------- */
.win { text-align: center; display: grid; gap: 14px; padding-top: 10px; }
.win canvas { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }
.trophy { font-size: 4rem; line-height: 1; }
.win h1 { margin: 0; font-size: 2.2rem; }
.final { display: grid; gap: 6px; text-align: left; }
.final div { display: flex; justify-content: space-between; padding: 10px 14px; border-radius: 12px; background: var(--surface-2); font-weight: 700; }
.final div.w { background: color-mix(in srgb, var(--green) 18%, var(--surface)); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; top: 40%; transform: translate(-50%, -50%) scale(.8);
  padding: 18px 30px; border-radius: 20px; font-size: 2rem; font-weight: 900; letter-spacing: .04em;
  background: var(--text); color: var(--bg); opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s; z-index: 30; text-align: center;
}
.toast small { display: block; font-size: .95rem; font-weight: 600; opacity: .8; letter-spacing: 0; }
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.toast.bust { background: var(--amber); color: #1c1917; }

@media (min-width: 520px) {
  .key { min-height: 58px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
