:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --text: #1f2333;
  --muted: #6b7085;
  --line: #d5d9e4;
  --line-strong: #2b3045;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --accent-soft: #dcdafe;
  --peer: #eef0f7;
  --same: #c4c0fb;
  --user: #4338ca;
  --error: #dc2626;
  --error-bg: #fde2e2;
  --btn: #e9ebf3;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11131a;
    --surface: #1b1e28;
    --text: #e7e9f2;
    --muted: #9097ad;
    --line: #2f3443;
    --line-strong: #8a90a8;
    --accent: #8b85ff;
    --accent-text: #11131a;
    --accent-soft: #34317a;
    --peer: #232735;
    --same: #45418a;
    --user: #aaa5ff;
    --error: #f87171;
    --error-bg: #4a1f24;
    --btn: #262a37;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }

.app {
  --size: max(260px, min(calc(100vw - 24px), 540px, calc(100dvh - 290px)));
  width: var(--size);
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 0 max(14px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.top h1 { margin: 0; font-size: 1.5rem; letter-spacing: -0.02em; }
.top-actions { display: flex; gap: 8px; }

.pill {
  border: 0;
  border-radius: 999px;
  padding: 9px 16px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
.pill.ghost { background: var(--btn); color: var(--text); }
.pill:active { transform: scale(0.97); }

.status {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 0.92rem;
  font-variant-numeric: tabular-nums;
}
#diffLabel { font-weight: 600; color: var(--text); }
#mistakes.has { color: var(--error); }

/* Board */
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  aspect-ratio: 1;
  width: 100%;
  background: var(--surface);
  border: 2px solid var(--line-strong);
  border-radius: 10px;
  overflow: hidden;
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: calc(var(--size) * 0.06);
  font-weight: 500;
  color: var(--user);
  cursor: pointer;
  transition: background-color 0.12s;
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+73) { border-bottom: 0; }
.cell.br { border-right: 2px solid var(--line-strong); }
.cell.bb { border-bottom: 2px solid var(--line-strong); }
.cell.given { color: var(--text); font-weight: 650; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.selected { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); }
.cell.error { color: var(--error); }
.cell.error:not(.selected) { background: var(--error-bg); }

.notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  place-items: center;
  font-size: calc(var(--size) * 0.025);
  font-weight: 500;
  color: var(--muted);
  line-height: 1;
}
.notes .hl { color: var(--accent); font-weight: 800; }

.cell.pop .v { animation: pop 0.25s ease-out; }
.cell.shake { animation: shake 0.3s; }
.board.won .cell { animation: wave 0.6s ease-out both; animation-delay: var(--d); }

@keyframes pop { 0% { transform: scale(0.5); } 70% { transform: scale(1.15); } 100% { transform: scale(1); } }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
@keyframes wave { 40% { background: var(--accent-soft); transform: scale(0.88); } }

/* Tools */
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tool {
  position: relative;
  border: 0;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 0;
  color: var(--muted);
  font-size: 0.8rem;
  border-radius: 12px;
}
.tool svg {
  width: 26px; height: 26px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.tool:active { background: var(--btn); }
.tool .badge {
  position: absolute;
  top: 0; right: calc(50% - 30px);
  font-size: 0.6rem;
  padding: 1px 5px;
  border-radius: 6px;
  background: var(--btn);
  color: var(--muted);
}
.tool[aria-pressed="true"] { color: var(--accent); }
.tool[aria-pressed="true"] .badge { background: var(--accent); color: var(--accent-text); }

/* Number pad */
.pad { display: grid; grid-template-columns: repeat(9, 1fr); gap: 5px; }
.num {
  border: 0;
  border-radius: 10px;
  background: var(--btn);
  padding: 8px 0 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1;
}
.num b { font-size: calc(var(--size) * 0.075); font-weight: 500; color: var(--accent); }
.num small { font-size: 0.65rem; color: var(--muted); margin-top: 4px; }
.num:active { transform: scale(0.94); }
.num.complete { opacity: 0.3; }
.notes-on .num b { font-size: calc(var(--size) * 0.05); color: var(--muted); }

/* Dialogs */
dialog {
  border: 0;
  border-radius: 18px;
  padding: 22px;
  background: var(--surface);
  color: var(--text);
  width: min(90vw, 360px);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.3);
}
dialog::backdrop { background: rgb(10 12 20 / 0.5); }
dialog h2 { margin: 0 0 4px; }
.muted { color: var(--muted); margin: 0 0 14px; }
.diff-list { display: grid; gap: 8px; }
.diff-btn {
  border: 0;
  border-radius: 12px;
  padding: 13px 16px;
  background: var(--btn);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}
.diff-btn small { color: var(--muted); font-weight: 400; font-variant-numeric: tabular-nums; }
.diff-btn.current { box-shadow: inset 0 0 0 2px var(--accent); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

.loading {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: rgb(10 12 20 / 0.35);
  z-index: 10;
}
.loading div { background: var(--surface); padding: 14px 22px; border-radius: 12px; font-weight: 600; }
.loading[hidden] { display: none; }
