/* ============================================================== tokens === */

:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f8;
  --ink: #16192b;
  --ink-soft: #5b6180;
  --line: #d2d8e8;
  --line-strong: #2c3350;
  --accent: #4c6ef5;
  --accent-ink: #ffffff;
  --accent-soft: #dbe2fe;
  --good: #16a34a;
  --good-soft: #d6f5e0;
  --bad: #dc2626;
  --bad-soft: #fde2e2;
  --given: #16192b;
  --entry: #3b5bdb;
  --gold: #f59f0a;

  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgb(16 24 64 / .06), 0 8px 28px rgb(16 24 64 / .08);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1120;
    --surface: #181c2f;
    --surface-2: #222840;
    --ink: #eef1fa;
    --ink-soft: #99a1c4;
    --line: #333b5c;
    --line-strong: #7e88b5;
    --accent: #7b93ff;
    --accent-ink: #0e1120;
    --accent-soft: #28315a;
    --good: #4ade80;
    --good-soft: #17381f;
    --bad: #f87171;
    --bad-soft: #3d1c1c;
    --given: #eef1fa;
    --entry: #9db2ff;
    --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 10px 30px rgb(0 0 0 / .35);
  }
}

:root[data-theme="dark"] {
  --bg: #0e1120;
  --surface: #181c2f;
  --surface-2: #222840;
  --ink: #eef1fa;
  --ink-soft: #99a1c4;
  --line: #333b5c;
  --line-strong: #7e88b5;
  --accent: #7b93ff;
  --accent-ink: #0e1120;
  --accent-soft: #28315a;
  --good: #4ade80;
  --good-soft: #17381f;
  --bad: #f87171;
  --bad-soft: #3d1c1c;
  --given: #eef1fa;
  --entry: #9db2ff;
  --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 10px 30px rgb(0 0 0 / .35);
}

/* ================================================================ base === */

* { box-sizing: border-box; }

/* Any element given a `display` below would otherwise out-rank the user-agent
   rule for [hidden] and stay on screen — including the overlays that sit on
   top of the board. Author styles beat UA styles, so state this explicitly. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  line-height: 1.5;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
dl, dd { margin: 0; }

.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: 18px 16px 40px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

.muted { color: var(--ink-soft); margin: 0; }

:where(button, select, input):focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ============================================================= buttons === */

.btn {
  font: inherit;
  font-weight: 600;
  padding: 11px 18px;
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  transition: transform .08s ease, background .15s ease, border-color .15s ease;
}
.btn:hover:not(:disabled) { border-color: var(--line-strong); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-quiet { background: transparent; border-color: transparent; color: var(--ink-soft); }
.btn-danger { background: transparent; border-color: var(--bad); color: var(--bad); width: 100%; }
.btn-small { padding: 6px 12px; min-height: 0; font-size: .85rem; }

.icon-btn {
  font: inherit;
  font-size: 1.15rem;
  line-height: 1;
  min-width: var(--tap);
  min-height: var(--tap);
  display: grid;
  place-items: center;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* ======================================================== level select === */

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}
.site-header h1 { font-size: 1.9rem; letter-spacing: -.02em; }
.tagline { margin: 2px 0 0; color: var(--ink-soft); font-size: .92rem; }

.warning {
  background: var(--bad-soft);
  color: var(--bad);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: .88rem;
  margin: 0 0 14px;
}

.resume-card {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 16px;
}
.resume-card h2 { font-size: 1rem; }
.resume-card .muted { font-size: .88rem; }
.resume-actions { display: flex; gap: 6px; }

.level-list { list-style: none; margin: 0 0 24px; padding: 0; display: grid; gap: 10px; }

.level-card {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  text-align: left;
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .12s ease, border-color .15s ease;
}
.level-card:hover:not(:disabled) { transform: translateY(-2px); border-color: var(--accent); }
.level-card:disabled { cursor: not-allowed; box-shadow: none; background: var(--surface-2); }

.level-num {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
}
.level-card:disabled .level-num { background: var(--bg); color: var(--ink-soft); }

.level-name { font-weight: 650; font-size: 1.08rem; }
.level-blurb, .level-stats, .level-lock { color: var(--ink-soft); font-size: .84rem; }
.level-stats { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 2px; }
.level-stats b { color: var(--ink); font-weight: 600; }

.lock-bar {
  height: 5px;
  border-radius: 99px;
  background: var(--line);
  overflow: hidden;
  margin-top: 6px;
  max-width: 190px;
}
.lock-bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .4s ease; }

.level-badge { font-size: 1.3rem; }
.level-card .chev { color: var(--ink-soft); font-size: 1.2rem; }

.totals-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 10px;
}
.totals-grid > div {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}
.totals-grid dt { color: var(--ink-soft); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.totals-grid dd { font-size: 1.3rem; font-weight: 650; font-variant-numeric: tabular-nums; }

/* ================================================================ game === */

.game-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}
.game-level { font-size: 1.25rem; flex: 1; }
.game-meta { display: flex; align-items: center; gap: 6px; }

.meta-chip {
  font-variant-numeric: tabular-nums;
  font-size: .9rem;
  font-weight: 600;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 5px 11px;
}
.meta-chip.alarm { color: var(--bad); border-color: var(--bad); }

.board-wrap { position: relative; }

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1;
  width: 100%;
  background: var(--line-strong);
  gap: 1px;
  padding: 2px;
  border: 2px solid var(--line-strong);
  border-radius: var(--radius-sm);
  overflow: hidden;
  touch-action: manipulation;
}
.board-row { display: contents; }

.cell {
  position: relative;
  font: inherit;
  font-size: clamp(1.1rem, 5.2vw, 1.6rem);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  display: grid;
  place-items: center;
  border: 0;
  padding: 0;
  background: var(--surface);
  color: var(--entry);
  cursor: pointer;
  transition: background .12s ease;
  user-select: none;
}
/* Thicker rules between the 3x3 boxes. */
.cell[data-col="2"], .cell[data-col="5"] { margin-right: -1px; border-right: 2px solid var(--line-strong); }
.cell[data-row="2"], .cell[data-row="5"] { margin-bottom: -1px; border-bottom: 2px solid var(--line-strong); }

.cell.given { color: var(--given); font-weight: 700; background: var(--surface-2); }
.cell.peer { background: var(--accent-soft); }
.cell.given.peer { background: color-mix(in srgb, var(--accent-soft) 70%, var(--line)); }
.cell.same-digit { background: color-mix(in srgb, var(--accent) 24%, var(--surface)); }
.cell.selected { background: color-mix(in srgb, var(--accent) 40%, var(--surface)); }
.cell.conflict { color: var(--bad); background: var(--bad-soft); }
.cell.wrong { color: var(--bad); background: var(--bad-soft); }
.cell.hinted { color: var(--good); }
.cell:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; z-index: 2; }

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  font-size: clamp(.45rem, 1.9vw, .62rem);
  font-weight: 600;
  line-height: 1;
  color: var(--ink-soft);
  pointer-events: none;
}
.notes span { display: grid; place-items: center; }

/* ------------------------------------------------------------ overlays --- */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(7px);
  border-radius: var(--radius-sm);
  z-index: 5;
}
.overlay-inner { display: grid; gap: 14px; justify-items: center; }
.overlay-title { font-size: 1.1rem; font-weight: 650; margin: 0; }

.spinner {
  width: 34px; height: 34px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* -------------------------------------------------------------- numpad --- */

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 5px;
  margin: 14px 0 10px;
}
.num {
  position: relative;
  font: inherit;
  font-size: clamp(1rem, 4.6vw, 1.35rem);
  font-weight: 650;
  min-height: 52px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
  padding-bottom: 6px;
}
.num:hover:not(:disabled) { border-color: var(--accent); }
.num:disabled { opacity: .35; cursor: default; }
.num .left {
  position: absolute;
  inset-inline: 0;
  bottom: 3px;
  font-size: .6rem;
  font-weight: 600;
  color: var(--ink-soft);
}
.numpad.noting .num { background: var(--accent-soft); }

.tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.tool {
  font: inherit;
  font-size: .76rem;
  display: grid;
  justify-items: center;
  gap: 3px;
  padding: 9px 4px;
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink-soft);
  cursor: pointer;
}
.tool span:first-child { font-size: 1.2rem; line-height: 1; }
.tool:hover:not(:disabled) { border-color: var(--accent); color: var(--ink); }
.tool:disabled { opacity: .4; cursor: not-allowed; }
.tool[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ======================================================= praise layer === */

.toasts {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  display: grid;
  gap: 7px;
  justify-items: center;
  z-index: 60;
  pointer-events: none;
  width: max-content;
  max-width: min(92vw, 420px);
}
.toast {
  background: var(--ink);
  color: var(--bg);
  font-size: .9rem;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 99px;
  box-shadow: var(--shadow);
  animation: toast-in .22s ease both;
}
.toast.good { background: var(--good); color: #fff; }
.toast.gold { background: var(--gold); color: #26180a; }
.toast.leaving { animation: toast-out .25s ease forwards; }

@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(.94); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-8px) scale(.97); } }

.cell.pulse-good { animation: pulse-good .5s ease; }
@keyframes pulse-good {
  0%   { background: var(--good); color: #fff; }
  100% { background: var(--surface); }
}

.cell.shake { animation: shake .3s ease; }
@keyframes shake {
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

.cell.unit-flash { animation: unit-flash .55s ease; }
@keyframes unit-flash {
  0%   { background: var(--good-soft); transform: scale(1); }
  40%  { background: var(--good); color: #fff; transform: scale(1.09); }
  100% { transform: scale(1); }
}

.confetti-layer { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 80; }
.confetti-bit { position: absolute; width: 9px; height: 15px; border-radius: 2px; will-change: transform, opacity; }

.unlock-banner {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--gold);
  color: #26180a;
  font-weight: 650;
  padding: 10px 12px 10px 18px;
  border-radius: 99px;
  box-shadow: var(--shadow);
  z-index: 70;
  max-width: min(94vw, 460px);
  animation: drop-in .35s cubic-bezier(.2, 1.3, .5, 1) both;
}
.unlock-banner .btn { background: #26180a; color: var(--gold); border-color: #26180a; }
.unlock-banner .icon-btn { color: #26180a; }
@keyframes drop-in { from { opacity: 0; transform: translate(-50%, -22px); } }

/* =============================================================== dialog === */

.dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 24px;
  width: min(92vw, 400px);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgb(8 10 22 / .55); backdrop-filter: blur(3px); }
.dialog[open] { animation: dialog-in .2s ease both; }
@keyframes dialog-in { from { opacity: 0; transform: scale(.95) translateY(8px); } }

.dialog h2 { font-size: 1.45rem; margin-bottom: 4px; }
.dialog hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }

.win-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 18px 0 12px;
  text-align: center;
}
.win-stats > div { background: var(--surface-2); border-radius: var(--radius-sm); padding: 10px 6px; }
.win-stats dt { font-size: .72rem; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .04em; }
.win-stats dd { font-size: 1.15rem; font-weight: 650; font-variant-numeric: tabular-nums; }

.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge {
  background: var(--gold);
  color: #26180a;
  font-size: .78rem;
  font-weight: 700;
  padding: 4px 11px;
  border-radius: 99px;
}
.badge.flawless { background: var(--good); color: #fff; }

.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
.dialog-actions .btn { flex: 1; min-width: 110px; }

.setting {
  display: flex;
  align-items: flex-start;
  gap: 11px;
  padding: 11px 0;
  cursor: pointer;
}
.setting input { width: 20px; height: 20px; margin-top: 2px; accent-color: var(--accent); flex-shrink: 0; }
.setting span { display: grid; }
.setting small { color: var(--ink-soft); font-size: .82rem; }
.setting-row { justify-content: space-between; align-items: center; }
.setting-row select {
  font: inherit;
  padding: 8px 10px;
  min-height: var(--tap);
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
}

/* ========================================================== responsive === */

@media (max-width: 380px) {
  .screen { padding: 12px 10px 30px; }
  .tools { grid-template-columns: repeat(5, 1fr); gap: 4px; }
  .tool { font-size: .68rem; }
  .numpad { gap: 4px; }
}

@media (min-width: 720px) {
  .screen { max-width: 620px; }
}

/* Honour the OS setting: keep the information, drop the motion. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
  .confetti-layer { display: none; }
}
