:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #ece9e2;
  --text: #1d1f24;
  --muted: #6b6f78;
  --line: #d9d5cc;
  --zero: #f08a4b;
  --zero-deep: #d26a2c;
  --one: #2f6fdf;
  --one-deep: #1f52b0;
  --accent: #2f6fdf;
  --danger: #d93a3a;
  --ok: #2a9d62;
  --cell-empty: #ece9e2;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161b;
    --surface: #1d2028;
    --surface-2: #262a34;
    --text: #eef0f4;
    --muted: #9aa0ab;
    --line: #2f3440;
    --zero: #f39a5d;
    --zero-deep: #d97a3a;
    --one: #4f8bf0;
    --one-deep: #3469c9;
    --accent: #5b93f2;
    --cell-empty: #262a34;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.screen {
  display: none;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 16px);
}
.screen.active { display: flex; }

/* ---------- Buttons ---------- */
.btn {
  border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600;
  background: var(--surface-2);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--line); }
.btn.wide { width: 100%; }
.btn:active { transform: scale(.98); }

.icon-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent;
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn-spacer { width: 44px; }

/* ---------- Home ---------- */
.home-head { text-align: center; padding: 28px 0 20px; }
.home-head h1 { margin: 12px 0 2px; font-size: 34px; letter-spacing: -.5px; }
.sub { margin: 0; color: var(--muted); }
.logo { display: inline-grid; grid-template-columns: repeat(2, 26px); gap: 6px; }
.dot { width: 26px; height: 26px; border-radius: 8px; }
.dot.z, .chip.z { background: var(--zero); }
.dot.o, .chip.o { background: var(--one); }
.chip { display: inline-block; width: 14px; height: 14px; border-radius: 4px; vertical-align: -2px; }

#btn-continue { margin-bottom: 16px; }

.tiers { display: grid; gap: 10px; }
.tier {
  display: flex; align-items: center; gap: 14px; text-align: left;
  border: 0; background: var(--surface); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow);
}
.tier:active { transform: scale(.99); }
.tier-size {
  flex: none; width: 52px; height: 52px; border-radius: 12px; display: grid; place-items: center;
  font-weight: 700; font-size: 15px; background: var(--surface-2);
}
.tier-body { flex: 1; min-width: 0; }
.tier-name { font-weight: 650; font-size: 17px; }
.tier-desc { font-size: 13px; color: var(--muted); }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--ok); border-radius: 3px; }
.tier-count { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.home-foot { display: flex; justify-content: center; gap: 8px; margin-top: auto; padding-top: 20px; }

/* ---------- Bar ---------- */
.bar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.bar-title { flex: 1; text-align: center; min-width: 0; }
.bar-title h2 { margin: 0; font-size: 18px; }
.bar-title span { font-size: 13px; }
.timer { width: 44px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 15px; }

/* ---------- Level grid ---------- */
.level-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; padding-bottom: 12px; }
.lvl {
  aspect-ratio: 1; border: 0; border-radius: 12px; background: var(--surface); box-shadow: var(--shadow);
  font-weight: 650; font-size: 17px; position: relative; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 1px;
}
.lvl small { font-size: 10px; font-weight: 500; color: var(--muted); font-variant-numeric: tabular-nums; }
.lvl.done { background: var(--ok); color: #fff; }
.lvl.done small { color: rgba(255,255,255,.85); }
.lvl.current { outline: 2px solid var(--accent); outline-offset: 2px; }
.lvl.locked { background: var(--surface-2); box-shadow: none; color: var(--muted); opacity: .6; }
.lvl.locked svg { width: 16px; height: 16px; }

/* ---------- Board ---------- */
.board-wrap { flex: 1; display: flex; align-items: center; justify-content: center; }
.board {
  --n: 6;
  --size: min(calc(100vw - 32px), calc(100dvh - 270px), 528px);
  width: var(--size); height: var(--size);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: calc(var(--size) / var(--n) * .08);
}
.cell {
  border: 0; padding: 0; border-radius: 22%;
  background: var(--cell-empty);
  position: relative;
  display: grid; place-items: center;
  font-weight: 700; font-size: calc(var(--size) / var(--n) * .42);
  transition: background-color .12s, transform .08s;
}
.cell:active { transform: scale(.94); }
.cell.v0 { background: var(--zero); box-shadow: inset 0 -3px 0 var(--zero-deep); }
.cell.v1 { background: var(--one);  box-shadow: inset 0 -3px 0 var(--one-deep); }
.cell.given::after {
  content: ""; position: absolute; width: 22%; height: 22%; border-radius: 50%;
  background: rgba(255,255,255,.55);
}
.cell.given { cursor: default; }
.cell.given:active { transform: none; }
.cell.err { box-shadow: inset 0 0 0 3px var(--danger) !important; }
.cell.err::before {
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  background: repeating-linear-gradient(45deg, rgba(255,255,255,.0) 0 5px, rgba(255,255,255,.25) 5px 8px);
}
.cell.hint { animation: pulse 1s ease-in-out 2; }
.cell.wrong { animation: shake .4s; box-shadow: inset 0 0 0 3px var(--danger) !important; }
.board.digits .cell { color: #fff; }
.board.digits .cell.v0::after, .board.digits .cell.v1::after { display: none; }
.board.digits .cell.given { text-decoration: underline; text-underline-offset: 3px; }
.board.solved .cell { animation: pop .5s ease both; }

@keyframes pulse { 50% { transform: scale(1.12); box-shadow: 0 0 0 4px var(--accent); } }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
@keyframes pop { 50% { transform: scale(1.08); } }

.msg { min-height: 2.8em; margin: 10px 0 4px; text-align: center; color: var(--muted); font-size: 14px; }
.msg.bad { color: var(--danger); }
.msg.good { color: var(--ok); }

.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool {
  border: 0; background: var(--surface); box-shadow: var(--shadow); border-radius: 14px;
  padding: 10px 4px; display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 12px;
}
.tool:active { transform: scale(.97); }
.tool:disabled { opacity: .4; }

/* ---------- Dialogs ---------- */
.dlg {
  border: 0; border-radius: 20px; padding: 24px; width: min(92vw, 400px);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.dlg::backdrop { background: rgba(10,12,16,.5); backdrop-filter: blur(2px); }
.dlg h2 { margin: 0 0 12px; }
.dlg-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.dlg-actions .btn { flex: 1; }
.rules { padding-left: 20px; margin: 0 0 12px; }
.rules li { margin-bottom: 6px; }
.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.switch-row input { width: 22px; height: 22px; accent-color: var(--accent); }
#btn-wipe { margin-top: 18px; }
#dlg-win { text-align: center; }
#dlg-win h2 { font-size: 28px; }
.win-burst { height: 56px; display: flex; justify-content: center; gap: 8px; margin-bottom: 8px; }
.win-burst::before, .win-burst::after {
  content: ""; width: 44px; height: 44px; border-radius: 12px; animation: pop .6s ease infinite alternate;
}
.win-burst::before { background: var(--zero); }
.win-burst::after { background: var(--one); animation-delay: .3s; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
