/* ------------------------------------------------------------------ theme */

:root {
  --bg: #f4f6fb;
  --bg-tint: #e9edf7;
  --surface: #ffffff;
  --surface-2: #f2f5fa;
  --surface-3: #e7ecf6;
  --text: #16203a;
  --text-soft: #56618a;
  --muted: #7b86ab;
  --line: #d5dced;
  --line-strong: #8e9ab9;
  --accent: #3f6ae0;
  --accent-soft: #dbe4fd;
  --accent-text: #ffffff;
  --danger: #d63b4a;
  --danger-soft: #fbe0e3;
  --good: #17916b;
  --good-soft: #d9f2e8;
  --warn-soft: #fdf0d2;
  --given: #16203a;
  --entry: #3f6ae0;
  --shadow: 0 1px 2px rgba(24, 36, 74, .06), 0 8px 24px rgba(24, 36, 74, .07);
  --shadow-lg: 0 12px 48px rgba(24, 36, 74, .18);
  --radius: 14px;
  --radius-sm: 9px;
}

[data-theme="dark"] {
  --bg: #10141f;
  --bg-tint: #161c2b;
  --surface: #1a2032;
  --surface-2: #212940;
  --surface-3: #2a344f;
  --text: #e9edf8;
  --text-soft: #a6b0cd;
  --muted: #7e89aa;
  --line: #2e3752;
  --line-strong: #6a769c;
  --accent: #6b93ff;
  --accent-soft: #263457;
  --accent-text: #0d1220;
  --danger: #ff7b87;
  --danger-soft: #3d2029;
  --good: #4fd4a4;
  --good-soft: #17342c;
  --warn-soft: #38301b;
  --given: #e9edf8;
  --entry: #8fb0ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .3);
  --shadow-lg: 0 12px 48px rgba(0, 0, 0, .55);
}

/* -------------------------------------------------------------- baseline */

*, *::before, *::after { box-sizing: border-box; }

/* Several components below set `display`, which would otherwise beat the
   user-agent rule for [hidden] and leave "hidden" elements on screen. */
[hidden] { display: none !important; }

/* Swapping themes repaints every surface at once. Without this the board and
   the buttons would each ease to their new colour and the page would look
   like it was reloading in pieces. */
.theme-switching *, .theme-switching *::before, .theme-switching *::after {
  transition: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { line-height: 1.25; margin: 0; }
p { margin: 0 0 .85em; }
p:last-child { margin-bottom: 0; }

button { font: inherit; color: inherit; }

kbd {
  display: inline-block;
  padding: .1em .45em;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .82em;
  white-space: nowrap;
}

.tabular { font-variant-numeric: tabular-nums; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: .5rem;
  padding: .5rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-text);
  z-index: 100;
}
.skip-link:focus { left: .5rem; }

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .85rem clamp(.9rem, 3vw, 2rem);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand { display: flex; align-items: center; gap: .6rem; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  font-size: 16px;
}

.brand-name { font-weight: 650; font-size: 1.1rem; letter-spacing: -.01em; }

.topbar-actions { display: flex; align-items: center; gap: .5rem; }

/* --------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  padding: .55rem .95rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .06s;
}
.btn:hover { background: var(--surface-2); border-color: var(--line-strong); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.btn-primary:hover { background: var(--accent); border-color: var(--accent); filter: brightness(1.08); }

.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }

.btn-icon { padding: .5rem; width: 38px; }
.btn-small { padding: .38rem .7rem; font-size: .86rem; }
.btn-icon.btn-small { width: 30px; padding: .25rem; }
.btn-block { width: 100%; }
.btn-block + .btn-block { margin-top: .5rem; }

/* ---------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, auto) minmax(280px, 340px);
  gap: clamp(1rem, 3vw, 2.25rem);
  align-items: start;
  justify-content: center;
  max-width: 1120px;
  margin: 0 auto;
  padding: clamp(1rem, 3vw, 2rem) clamp(.75rem, 3vw, 2rem);
}

.board-column { display: flex; flex-direction: column; gap: .85rem; min-width: 0; }
.side-column { display: flex; flex-direction: column; gap: 1rem; min-width: 0; }

/* ------------------------------------------------------------ board meta */

.board-meta {
  display: flex;
  align-items: center;
  gap: clamp(.75rem, 2.5vw, 1.5rem);
  padding: .6rem .95rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.meta-item { display: flex; flex-direction: column; line-height: 1.2; }
.meta-label { font-size: .68rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }
.meta-value { font-weight: 650; font-size: 1.02rem; }
.meta-item:last-of-type { margin-right: auto; }
#mistake-count.has-mistakes { color: var(--danger); }

/* ----------------------------------------------------------------- board */

.board-frame { position: relative; align-self: center; }

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: min(92vw, 540px);
  aspect-ratio: 1;
  border: 2px solid var(--line-strong);
  border-radius: 10px;
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: calc(min(92vw, 540px) / 15);
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0;
  background: transparent;
  color: var(--entry);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  transition: background-color .12s;
}
.cell[data-col="2"], .cell[data-col="5"] { border-right: 2px solid var(--line-strong); }
.cell[data-col="8"] { border-right: 0; }
.cell[data-row="2"], .cell[data-row="5"] { border-bottom: 2px solid var(--line-strong); }
.cell[data-row="8"] { border-bottom: 0; }

.cell.is-given { color: var(--given); font-weight: 650; cursor: default; }

.cell-value { line-height: 1; pointer-events: none; }

.cell-notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 6%;
  font-size: .3em;
  font-weight: 550;
  line-height: 1;
  color: var(--muted);
  pointer-events: none;
}
.cell-notes span { display: grid; place-items: center; }
.cell-notes .note-hot { color: var(--accent); font-weight: 700; }

/* Board state layers, painted from least to most important. */
.cell.is-peer { background: var(--surface-2); }
.cell.is-same-digit { background: var(--accent-soft); }
.cell.is-selected { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); z-index: 2; }
.cell.is-conflict { background: var(--danger-soft); }
.cell.is-error { background: var(--danger-soft); color: var(--danger); }
.cell.is-hint-support { background: var(--warn-soft); }
.cell.is-hint {
  background: var(--good-soft);
  box-shadow: inset 0 0 0 2px var(--good);
  z-index: 3;
  animation: hint-pulse 1.4s ease-out 2;
}
.cell.is-just-solved { animation: pop .3s ease-out; }

@keyframes hint-pulse {
  0%, 100% { box-shadow: inset 0 0 0 2px var(--good); }
  50% { box-shadow: inset 0 0 0 4px var(--good); }
}
@keyframes pop {
  0% { transform: scale(.6); }
  70% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

.board.is-hidden .cell-value,
.board.is-hidden .cell-notes { visibility: hidden; }

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

.board-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  border-radius: 10px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(3px);
  z-index: 5;
}
.board-overlay[hidden] { display: none; }

.overlay-card {
  max-width: 20rem;
  padding: 1.4rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.overlay-card h2 { margin-bottom: .4rem; font-size: 1.3rem; }
.overlay-card p { color: var(--text-soft); font-size: .92rem; }
.overlay-buttons { display: flex; gap: .5rem; justify-content: center; margin-top: 1rem; flex-wrap: wrap; }
.win-mark { font-size: 2.4rem; line-height: 1; margin-bottom: .3rem; }
.board-overlay-win .overlay-card { border-color: var(--good); }

.spinner {
  width: 26px;
  height: 26px;
  margin: 0 auto .7rem;
  border: 3px solid var(--surface-3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

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

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: clamp(.25rem, 1vw, .45rem);
  width: min(92vw, 540px);
  align-self: center;
}

.numkey {
  position: relative;
  display: grid;
  place-items: center;
  padding: .55rem 0 .95rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: clamp(1.05rem, 3.6vw, 1.45rem);
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .15s, border-color .15s, transform .06s;
}
.numkey:hover { background: var(--surface-2); border-color: var(--line-strong); }
.numkey:active { transform: translateY(1px); }

.numkey-left {
  position: absolute;
  bottom: .28rem;
  font-size: .58rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: .02em;
}
.numkey.is-done { color: var(--muted); background: var(--surface-2); }
.numkey.is-done .numkey-left { visibility: hidden; }
.numpad.is-notes .numkey { border-style: dashed; color: var(--text-soft); }

/* ----------------------------------------------------------------- tools */

.tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(.3rem, 1vw, .5rem);
  width: min(92vw, 540px);
  align-self: center;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .15rem;
  padding: .55rem .3rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .15s, border-color .15s, transform .06s;
}
.tool:hover { background: var(--surface-2); border-color: var(--line-strong); }
.tool:active { transform: translateY(1px); }
.tool:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.tool-icon { font-size: 1.1rem; line-height: 1.1; }
.tool-label { font-size: .78rem; font-weight: 550; color: var(--text-soft); }
.tool-state { color: var(--muted); font-weight: 500; }

.tool[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.tool[aria-pressed="true"] .tool-label,
.tool[aria-pressed="true"] .tool-state { color: var(--accent); }
.tool-accent .tool-label { color: var(--accent); }

/* ----------------------------------------------------------------- cards */

.card {
  padding: 1.05rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.card-title {
  margin-bottom: .75rem;
  font-size: .74rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--muted);
}

.difficulty-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .4rem;
  margin-bottom: .6rem;
}

.difficulty-option {
  padding: .5rem .3rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-weight: 550;
  font-size: .92rem;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
.difficulty-option:hover { background: var(--surface-2); }
.difficulty-option[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.difficulty-blurb {
  margin-bottom: .8rem;
  min-height: 2.6em;
  font-size: .84rem;
  color: var(--text-soft);
}

/* ------------------------------------------------------------ hint card */

.hint-card { border-color: var(--accent); }
.hint-card[hidden] { display: none; }
.hint-head { display: flex; align-items: flex-start; justify-content: space-between; gap: .5rem; }
.hint-head .card-title { color: var(--accent); text-transform: none; font-size: .98rem; letter-spacing: 0; }
.hint-text { font-size: .89rem; color: var(--text-soft); }
.hint-actions { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-top: .85rem; }
.hint-count { font-size: .76rem; color: var(--muted); }

/* -------------------------------------------------------------- switches */

.switch {
  display: flex;
  align-items: flex-start;
  gap: .65rem;
  margin-bottom: .8rem;
  cursor: pointer;
}
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }

.switch-track {
  flex: none;
  position: relative;
  width: 36px;
  height: 21px;
  margin-top: .1rem;
  border-radius: 999px;
  background: var(--surface-3);
  transition: background .18s;
}
.switch-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .18s;
}
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(15px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

.switch-text { font-size: .88rem; font-weight: 550; }
.switch-text small { display: block; font-weight: 400; font-size: .78rem; color: var(--muted); line-height: 1.4; }

/* ------------------------------------------------------------- progress */

.stats-empty { font-size: .86rem; color: var(--muted); }

.stats-headline {
  display: flex;
  align-items: center;
  gap: .55rem;
  margin-bottom: .85rem;
}

.stats-total {
  font-size: 2.1rem;
  font-weight: 700;
  line-height: 1;
  color: var(--accent);
}
.stats-total-label {
  font-size: .72rem;
  line-height: 1.15;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
}
.stats-streak {
  margin-left: auto;
  padding: .25rem .55rem;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: .76rem;
  font-weight: 600;
  color: var(--text-soft);
  text-align: right;
}

.stats-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.stats-table th {
  padding: 0 0 .3rem;
  border-bottom: 1px solid var(--line);
  font-size: .68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  text-align: right;
}
.stats-table th:first-child, .stats-table td:first-child { text-align: left; }
.stats-table td {
  padding: .34rem 0;
  border-bottom: 1px solid var(--line);
  text-align: right;
}
.stats-table tr:last-child td { border-bottom: 0; }

.stats-note { margin: .6rem 0 .8rem; font-size: .78rem; color: var(--muted); }

.win-extra {
  margin-top: .5rem;
  padding-top: .6rem;
  border-top: 1px solid var(--line);
  font-size: .85rem;
  font-weight: 550;
  color: var(--good);
}

.tips-card .tip { font-size: .88rem; color: var(--text-soft); min-height: 4.2em; }

/* ---------------------------------------------------------------- footer */

.footer {
  padding: 1.5rem 1rem 2.5rem;
  text-align: center;
  font-size: .82rem;
  color: var(--muted);
}

/* ----------------------------------------------------------------- modal */

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 1rem; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(9, 13, 25, .55); backdrop-filter: blur(2px); }

.modal-panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100%, 720px);
  max-height: min(88vh, 820px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.modal-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.15rem .85rem;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { font-size: 1.15rem; }

/* flex: none on the chrome, min-height: 0 on the body — otherwise the header
   and tabs get squeezed and the body refuses to scroll. */
.tabs {
  flex: none;
  display: flex;
  gap: .25rem;
  padding: .6rem .8rem 0;
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.tab {
  padding: .5rem .8rem;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--text-soft);
  font-weight: 550;
  font-size: .9rem;
  white-space: nowrap;
  cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--accent); border-bottom-color: var(--accent); }

.modal-body { flex: 1 1 auto; min-height: 0; padding: 1.2rem 1.3rem 1.6rem; overflow-y: auto; }
.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

.modal-body h3 { margin: 1.5rem 0 .5rem; font-size: 1rem; }
.modal-body h3:first-child { margin-top: 0; }
.modal-body .lede { font-size: 1rem; color: var(--text-soft); margin-bottom: 1.2rem; }

.rule-list { margin: 0 0 1em; padding-left: 1.2rem; }
.rule-list li { margin-bottom: .35em; }

.technique { padding-top: .5rem; }
.technique + .technique { border-top: 1px solid var(--line); margin-top: 1.4rem; }

.example {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: min(100%, 260px);
  aspect-ratio: 1;
  margin: .9rem 0 .5rem;
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface);
  font-size: 11px;
}
.example-cell {
  display: grid;
  place-items: center;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-weight: 600;
  color: var(--text-soft);
}
.example-cell[data-col="2"], .example-cell[data-col="5"] { border-right: 1px solid var(--line-strong); }
.example-cell[data-col="8"] { border-right: 0; }
.example-cell[data-row="2"], .example-cell[data-row="5"] { border-bottom: 1px solid var(--line-strong); }
.example-cell[data-row="8"] { border-bottom: 0; }
.example-cell.is-swept { background: var(--danger-soft); }
.example-cell.is-support { background: var(--warn-soft); color: var(--text); }
/* The target square is empty by design — the ring is what makes it findable. */
.example-cell.is-target {
  background: var(--good-soft);
  box-shadow: inset 0 0 0 2px var(--good);
  color: var(--good);
  font-weight: 700;
}
.example-caption { font-size: .82rem; color: var(--muted); }

.callout {
  margin-top: 1.5rem;
  padding: .9rem 1rem;
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--accent-soft);
  font-size: .89rem;
}

.swatch {
  display: inline-block;
  width: 1.1em;
  height: 1.1em;
  margin-right: .45em;
  vertical-align: -.2em;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
}

/* The shortcut table reserves a wide first column for key combinations;
   colour names are short, so claw that space back for the descriptions. */
.colour-key td:first-child { width: 22%; }
.swatch-same { background: var(--accent-soft); }
.swatch-peer { background: var(--surface-2); }
.swatch-error { background: var(--danger-soft); }
.swatch-hint { background: var(--good-soft); }

.shortcuts { width: 100%; border-collapse: collapse; font-size: .89rem; }
.shortcuts td { padding: .4rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.shortcuts td:first-child { width: 42%; white-space: nowrap; }
.shortcuts tr:last-child td { border-bottom: 0; }

/* ----------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  z-index: 80;
  padding: .6rem 1.1rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  font-size: .88rem;
  font-weight: 550;
  animation: toast-in .2s ease-out;
}
.toast[hidden] { display: none; }
@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ------------------------------------------------------------ responsive */

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); max-width: 560px; }
  .side-column { order: 2; }
}

@media (max-width: 420px) {
  .board-meta { gap: .6rem; padding: .5rem .6rem; flex-wrap: wrap; }
  .tool-label { font-size: .72rem; }
  .brand-name { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
