:root {
  --bg: #fff7ec;
  --card: #ffffff;
  --ink: #2b2140;
  --muted: #7a7090;
  --line: #ece4f5;
  --brand: #ff6b6b;
  --brand-2: #ffb347;
  --accent: #6c5ce7;
  --ok: #2ecc71;
  --x: #3b82f6;
  --o: #f97316;
  --coin: #f5b301;
  --coin-ink: #9a6b00;
  --shadow: 0 6px 20px rgba(60, 40, 100, .10);
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1630; --card: #261f40; --ink: #f4efff; --muted: #b1a7c9; --line: #3a3157;
    --coin-ink: #ffd96a; --shadow: 0 6px 20px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  --bg: #1b1630; --card: #261f40; --ink: #f4efff; --muted: #b1a7c9; --line: #3a3157;
  --coin-ink: #ffd96a; --shadow: 0 6px 20px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Fredoka', system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100dvh;
  padding-bottom: 90px;
  -webkit-tap-highlight-color: transparent;
}
main { max-width: 560px; margin: 0 auto; padding: 16px; }
h2 { margin: 0 0 10px; font-size: 20px; }
h3 { margin: 0 0 8px; font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.logo { font-size: 26px; }
.brand h1 { font-size: 18px; margin: 0; line-height: 1.1; }
.tag { margin: 2px 0 0; font-size: 12px; opacity: .92; }
.coinpill {
  border: 0; cursor: pointer; white-space: nowrap;
  background: rgba(255, 255, 255, .24); color: #fff;
  font: inherit; font-weight: 700; font-size: 18px;
  padding: 8px 14px; border-radius: 999px;
}
.coinpill.pulse { animation: pulse .5s; }
@keyframes pulse { 0% { transform: scale(1); } 40% { transform: scale(1.25); } 100% { transform: scale(1); } }

/* ---------- cards & buttons ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.screen { display: none; }
.screen.active { display: block; }

.btn {
  border: 0; border-radius: 999px; cursor: pointer;
  font: inherit; font-weight: 600; padding: 10px 18px;
  background: var(--line); color: var(--ink);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.big { width: 100%; padding: 14px; font-size: 18px; }
.btn.small { padding: 6px 12px; font-size: 14px; }
.btn.ghost { background: transparent; border: 2px solid var(--line); }
.btn.danger { color: #e74c3c; border-color: #e74c3c; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- level picker ---------- */
.levels { display: grid; gap: 10px; }
.level {
  display: grid; grid-template-columns: 44px 1fr; align-items: center; gap: 2px 12px;
  text-align: left; padding: 12px; cursor: pointer;
  border: 2px solid var(--line); border-radius: 14px;
  background: transparent; color: var(--ink); font: inherit;
}
.level .lv-ico { font-size: 30px; grid-row: span 2; text-align: center; }
.level b { font-size: 16px; }
.level small { color: var(--muted); font-size: 13px; }
.level.active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.level:disabled { opacity: .5; cursor: not-allowed; }
.options { display: flex; flex-wrap: wrap; gap: 10px 18px; margin: 14px 2px; align-items: center; }
.opt { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.opt input { width: 18px; height: 18px; }
.seg { display: inline-flex; border: 2px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg button { border: 0; background: transparent; color: var(--ink); font: inherit; padding: 5px 12px; cursor: pointer; }
.seg button.active { background: var(--accent); color: #fff; }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin: 10px 0 0; min-height: 1.3em; }
.how { margin-top: 14px; font-size: 14px; color: var(--muted); }
.how summary { cursor: pointer; font-weight: 600; color: var(--ink); }
.how ol { padding-left: 20px; margin: 8px 0 0; }
.how li { margin: 4px 0; }

/* ---------- game ---------- */
.gamehead { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; font-weight: 600; }
.gamebtns { display: flex; gap: 6px; flex-shrink: 0; }
.bubble {
  position: relative; background: var(--bg); border-radius: 14px;
  padding: 10px 14px; margin: 6px 0 16px; font-size: 15px; min-height: 42px;
}
.bubble::after {
  content: ''; position: absolute; left: 22px; bottom: -8px;
  border: 8px solid transparent; border-top-color: var(--bg); border-bottom: 0;
}
.bubble:empty { display: none; }
.board-wrap { padding: 12px; border-radius: 16px; }
.board { display: grid; grid-template-columns: repeat(var(--n, 3), 1fr); gap: 8px; }
.cell {
  aspect-ratio: 1; border: 0; border-radius: 14px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font: inherit; font-size: clamp(26px, calc(66vw / var(--n, 3) * .5), 64px); line-height: 1;
  background: var(--card); color: var(--ink);
  transition: transform .12s, background .2s;
}
.cell:not(:disabled):hover { transform: scale(1.04); }
.cell:disabled { cursor: default; }
.cell.letter[data-p="X"] { color: var(--x); font-weight: 700; }
.cell.letter[data-p="O"] { color: var(--o); font-weight: 700; }
.cell.win { background: var(--ok) !important; color: #fff !important; animation: pop .4s; }
.cell.hint { outline: 4px dashed var(--coin); outline-offset: -4px; }
.cell .glyph { display: inline-block; animation: drop .25s ease-out; }
@keyframes pop { 0% { transform: scale(.8); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes drop { from { transform: scale(.3); opacity: 0; } }

/* board styles (bought in the shop) */
.board-wrap.paper {
  background: #fdfaf2;
  background-image: linear-gradient(#e6e0d0 1px, transparent 1px), linear-gradient(90deg, #e6e0d0 1px, transparent 1px);
  background-size: 22px 22px;
}
.board-wrap.paper .cell { background: #fff; box-shadow: inset 0 0 0 2px #e6e0d0; color: #2b2140; }
.board-wrap.neon { background: #0b0a1f; }
.board-wrap.neon .cell { background: #15133a; color: #fff; box-shadow: 0 0 0 2px #7c3aed, 0 0 14px #7c3aed88; }
.board-wrap.neon .cell.letter[data-p="X"] { color: #22d3ee; text-shadow: 0 0 12px #22d3ee; }
.board-wrap.neon .cell.letter[data-p="O"] { color: #f472b6; text-shadow: 0 0 12px #f472b6; }
.board-wrap.wood { background: repeating-linear-gradient(90deg, #a9743f 0 18px, #9a6736 18px 36px); }
.board-wrap.wood .cell { background: #f3e1c4; box-shadow: inset 0 -4px 0 #d2b48c; color: #4a2e10; }
.board-wrap.wood .cell.letter[data-p="X"] { color: #1d4ed8; }
.board-wrap.wood .cell.letter[data-p="O"] { color: #c2410c; }
.board-wrap.chalk { background: #2d4a3e; }
.board-wrap.chalk .cell { background: #365a4b; color: #fff; box-shadow: inset 0 0 0 2px #ffffff55; }
.board-wrap.chalk .cell.letter { color: #fff !important; }

.status { text-align: center; font-weight: 600; margin: 12px 0 0; min-height: 1.4em; }
.result { text-align: center; margin-top: 12px; display: grid; gap: 8px; justify-items: center; }
.result-text { font-size: 22px; font-weight: 700; }
.result-coins { font-size: 17px; color: var(--coin-ink); font-weight: 700; min-height: 1.2em; }

/* ---------- shop ---------- */
.shop-h { margin: 18px 4px 8px; }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.item {
  background: var(--card); border-radius: var(--radius); padding: 14px;
  box-shadow: var(--shadow); display: grid; gap: 6px; justify-items: center; text-align: center;
  align-content: start;
}
.item .ico { font-size: 38px; line-height: 1.2; }
.item b { font-size: 15px; }
.item small { color: var(--muted); font-size: 12px; min-height: 1.2em; }
.item .price { background: color-mix(in srgb, var(--coin) 22%, transparent); color: var(--coin-ink); border-radius: 999px; padding: 2px 10px; font-weight: 700; font-size: 13px; }
.item.owned { outline: 2px solid var(--ok); }
.item.soon { opacity: .6; }
.item.soon .price { background: var(--line); color: var(--muted); }

/* ---------- hall of fame & vault ---------- */
.bigstat, .vault-split { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; margin: 10px 0; }
.vault-split { grid-template-columns: repeat(2, 1fr); }
.bigstat b, .vault-split b { display: block; font-size: 28px; }
.bigstat span, .vault-split span { font-size: 12px; color: var(--muted); }
.boss-card { background: linear-gradient(135deg, var(--card), color-mix(in srgb, var(--coin) 18%, var(--card))); }
table { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
td, th { padding: 6px 4px; text-align: center; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
.vault-total { font-size: 42px; font-weight: 700; text-align: center; color: var(--coin-ink); }
.ledger { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow: auto; }
.ledger li { display: flex; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.ledger .plus { color: var(--ok); font-weight: 700; white-space: nowrap; }
.ledger .minus { color: var(--brand); font-weight: 700; white-space: nowrap; }
.settings label { display: flex; align-items: center; gap: 8px; margin: 10px 0; }
.settings input[type="text"] { font: inherit; padding: 6px 10px; border-radius: 10px; border: 2px solid var(--line); background: var(--bg); color: var(--ink); }
.settings input[type="checkbox"] { width: 18px; height: 18px; }
.settings .btn { margin-top: 6px; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
}
.tabs button {
  flex: 1; border: 0; background: transparent; cursor: pointer;
  color: var(--muted); font: inherit; font-size: 22px;
  display: grid; justify-items: center; gap: 2px; padding: 4px 0;
}
.tabs button span { font-size: 11px; font-weight: 600; }
.tabs button.active { color: var(--accent); }

/* ---------- toast & confetti ---------- */
.toast {
  position: fixed; left: 50%; bottom: 92px; z-index: 10;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 999px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: .25s;
  max-width: 92vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 9; }
