/* ---------------------------------------------------------------- tokens */

:root {
  --bg: #f4f6fb;
  --bg-raised: #ffffff;
  --bg-sunken: #eaeef7;
  --ink: #16203a;
  --ink-soft: #5a6684;
  --ink-faint: #8a94ad;
  --line: #d3daea;
  --line-strong: #8f9cba;
  --accent: #2f6df6;
  --accent-soft: #dfe8ff;
  --accent-ink: #ffffff;
  --danger: #d92b4b;
  --danger-soft: #fde2e7;
  --good: #17915f;
  --good-soft: #d8f3e6;
  --given: #16203a;
  --entered: #2f6df6;
  --shadow: 0 1px 2px rgba(20, 32, 64, .06), 0 8px 24px rgba(20, 32, 64, .08);
  --radius: 14px;
  --panel-pad: clamp(12px, 2.4vw, 20px);
  /* Largest the grid may get, so the whole board stays on screen. */
  --board-max: 62vh;
  --radius-sm: 9px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1420;
    --bg-raised: #171e2e;
    --bg-sunken: #10161f;
    --ink: #e8edf9;
    --ink-soft: #a3adc6;
    --ink-faint: #6f7b96;
    --line: #2a3345;
    --line-strong: #62708e;
    --accent: #5b8dff;
    --accent-soft: #1e2b4d;
    --accent-ink: #08101f;
    --danger: #ff6b83;
    --danger-soft: #3a1a24;
    --good: #4ad295;
    --good-soft: #10312a;
    --given: #e8edf9;
    --entered: #7ea6ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
  }
}

:root[data-theme="dark"] {
  --bg: #0f1420;
  --bg-raised: #171e2e;
  --bg-sunken: #10161f;
  --ink: #e8edf9;
  --ink-soft: #a3adc6;
  --ink-faint: #6f7b96;
  --line: #2a3345;
  --line-strong: #62708e;
  --accent: #5b8dff;
  --accent-soft: #1e2b4d;
  --accent-ink: #08101f;
  --danger: #ff6b83;
  --danger-soft: #3a1a24;
  --good: #4ad295;
  --good-soft: #10312a;
  --given: #e8edf9;
  --entered: #7ea6ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { line-height: 1.2; margin: 0; }
button { font: inherit; color: inherit; }

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

.skip-link {
  position: absolute; left: 8px; top: -48px; z-index: 20;
  background: var(--accent); color: var(--accent-ink);
  padding: 8px 14px; border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  text-decoration: none; transition: top .15s;
}
.skip-link:focus { top: 0; }

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

/* ---------------------------------------------------------------- header */

.site-header {
  display: flex; flex-wrap: wrap; gap: 14px;
  align-items: center; justify-content: space-between;
  padding: 16px clamp(14px, 4vw, 34px);
  border-bottom: 1px solid var(--line);
  background: var(--bg-raised);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 38px; height: 38px; flex: none; }
.brand-mark rect { fill: var(--accent); }
.brand-mark path { stroke: var(--accent-ink); stroke-width: 2.2; fill: none; opacity: .9; }
.brand h1 { font-size: 1.2rem; letter-spacing: -.01em; }
.brand-tag { margin: 0; font-size: .8rem; color: var(--ink-soft); }

.header-tools { display: flex; align-items: center; gap: 10px; }
.date-nav {
  display: flex; align-items: center; gap: 4px;
  background: var(--bg-sunken); border: 1px solid var(--line);
  border-radius: 999px; padding: 3px;
}
.date-field input {
  font: inherit; color: inherit; background: none; border: 0;
  padding: 5px 6px; border-radius: 999px; min-width: 8.5em;
}
.date-field input::-webkit-calendar-picker-indicator { filter: invert(var(--picker-invert, 0)); cursor: pointer; }
:root[data-theme="dark"] { --picker-invert: 1; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --picker-invert: 1; } }

.icon-button {
  display: grid; place-items: center;
  width: 34px; height: 34px; padding: 0;
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: 50%; cursor: pointer; font-size: 1.05rem;
}
.icon-button:hover { border-color: var(--line-strong); }
.date-nav .icon-button { border-color: transparent; background: transparent; }
.date-nav .icon-button:hover { background: var(--bg-raised); }

.text-button {
  background: none; border: 0; border-radius: 999px;
  padding: 6px 12px; cursor: pointer; color: var(--accent); font-weight: 600;
}
.text-button:hover { background: var(--accent-soft); }

/* ------------------------------------------------------------------ main */

main {
  max-width: 1080px; margin: 0 auto;
  padding: clamp(16px, 3vw, 30px) clamp(14px, 4vw, 34px) 48px;
}

.levels {
  display: grid; gap: 8px;
  grid-template-columns: repeat(auto-fit, minmax(112px, 1fr));
}

.level {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  padding: 10px 14px; cursor: pointer; text-align: left;
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius-sm); transition: border-color .15s, background .15s;
}
.level:hover { border-color: var(--line-strong); }
.level[aria-current="true"] {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
}
.level-name { font-weight: 650; }
.level-dots { display: flex; gap: 3px; }
.level-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .25; }
.level-dot.on { opacity: .95; }

.level-blurb {
  margin: 12px 2px 18px; color: var(--ink-soft); font-size: .92rem;
  min-height: 1.4em;
}

.layout {
  display: grid; gap: clamp(16px, 3vw, 28px);
  grid-template-columns: minmax(0, 1fr) 244px;
  align-items: start;
}

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

.board-panel {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); padding: var(--panel-pad);
  box-shadow: var(--shadow);
  /*
   * A definite width, not max-width. With max-width plus auto margins the panel
   * shrinks to fit its contents, so its width tracked the text inside it — the
   * timer ticking or a longer hint resized the panel, and the board, which is a
   * percentage of it, resized too.
   */
  width: min(100%, calc(var(--board-max) + 2 * var(--panel-pad)));
  margin-inline: auto;
}

.board-meta {
  display: flex; flex-wrap: wrap; gap: 10px 22px;
  align-items: center; margin-bottom: 14px;
}
.meta-item { display: flex; align-items: center; gap: 8px; }
.meta-label { font-size: .74rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-faint); }
.meta-value { font-weight: 650; font-variant-numeric: tabular-nums; }
.meta-value.warn { color: var(--danger); }
.tabular { font-variant-numeric: tabular-nums; }

.meter {
  width: 88px; height: 7px; border-radius: 999px;
  background: var(--bg-sunken); overflow: hidden;
}
.meter-fill {
  height: 100%; width: 0;
  background: linear-gradient(90deg, var(--good), var(--accent) 55%, var(--danger));
  transition: width .3s ease;
}

.board-wrap {
  position: relative;
  /* Keep the whole board on screen without scrolling on a laptop. */
  width: min(100%, var(--board-max));
  margin-inline: auto;
}

.board {
  position: relative;
  display: grid;
  /* minmax(0, 1fr), not 1fr: otherwise a cell full of pencil marks is taller
     than its share of the grid and stretches the whole board. */
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  aspect-ratio: 1;
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  overflow: hidden;
  background: var(--line);
  gap: 1px;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: grid; place-items: center;
  min-width: 0; min-height: 0; overflow: hidden;
  padding: 0; border: 0; cursor: pointer;
  background: var(--bg-raised);
  color: var(--entered);
  font-size: clamp(1.05rem, 4.4vw, 1.7rem);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  transition: background .12s;
}
/*
 * The 3x3 block rules are painted over the grid rather than drawn per cell, so
 * they stay crisp and never shift the cells around.
 */
.board::after {
  content: "";
  position: absolute; inset: 0;
  pointer-events: none; z-index: 2;
  --rule: linear-gradient(var(--line-strong), var(--line-strong));
  background:
    var(--rule) calc(100% / 3 - 1px) 0 / 2px 100% no-repeat,
    var(--rule) calc(200% / 3 - 1px) 0 / 2px 100% no-repeat,
    var(--rule) 0 calc(100% / 3 - 1px) / 100% 2px no-repeat,
    var(--rule) 0 calc(200% / 3 - 1px) / 100% 2px no-repeat;
}

.cell.given { color: var(--given); font-weight: 680; }
.cell.peer { background: var(--bg-sunken); }
.cell.matching { background: var(--accent-soft); }
.cell.selected { background: var(--accent); color: var(--accent-ink); }
.cell.selected.given { color: var(--accent-ink); }
.cell.wrong { color: var(--danger); }
.cell.conflict { background: var(--danger-soft); }
.cell.selected.conflict { background: var(--danger); color: #fff; }
.cell.hinted { background: var(--good-soft); }
.cell.hint-target { outline: 2px solid var(--good); outline-offset: -2px; }
.cell.revealed { color: var(--good); }

.notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  width: 100%; height: 100%;
  font-size: .58em; line-height: 1;
  color: var(--ink-faint); font-weight: 500;
}
.notes span { display: grid; place-items: center; }
.notes span.cued { color: var(--good); font-weight: 700; }
.cell.selected .notes { color: color-mix(in srgb, var(--accent-ink) 75%, transparent); }

/* A class rule with `display` would otherwise beat the `hidden` attribute. */
.board-overlay[hidden] { display: none; }

.board-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg-raised) 82%, transparent);
  backdrop-filter: blur(3px);
  border-radius: 6px;
  z-index: 5;
}
.overlay-card {
  text-align: center; padding: 22px 26px;
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  max-width: 88%;
}
.overlay-card h2 { font-size: 1.25rem; margin-bottom: 6px; }
.overlay-card p { margin: 4px 0; color: var(--ink-soft); }
.overlay-card .big { font-size: 1.9rem; font-weight: 700; color: var(--ink); font-variant-numeric: tabular-nums; }
.overlay-card .row { display: flex; gap: 8px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }

.spinner {
  width: 30px; height: 30px; margin: 0 auto 12px;
  border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
  .meter-fill { transition: none; }
}

/*
 * Fixed height, not min-height: the board must not shift up and down as hint
 * text comes and goes. Anything unusually long scrolls inside the box instead
 * of pushing the layout around.
 */
.hint-bar {
  margin: 12px 2px 0;
  height: calc(3 * 1.5em);
  overflow-y: auto;
  font-size: .9rem; line-height: 1.5;
  color: var(--ink-soft);
}
.hint-bar strong { color: var(--ink); }
.hint-bar:empty::before {
  content: "Stuck? Press Hint and the next step will be explained here.";
  color: var(--ink-faint);
}

/* -------------------------------------------------------------- controls */

.controls { display: flex; flex-direction: column; gap: 12px; }

.numpad {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
}
.num {
  position: relative;
  aspect-ratio: 1.25; padding: 0; cursor: pointer;
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 1.35rem; font-weight: 600;
  transition: background .12s, border-color .12s;
}
.num:hover { border-color: var(--line-strong); }
.num.done { color: var(--ink-faint); background: var(--bg-sunken); }
.num .left {
  position: absolute; right: 5px; bottom: 3px;
  font-size: .62rem; font-weight: 600; color: var(--ink-faint);
}

.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.action {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 9px 4px; cursor: pointer;
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: .74rem; color: var(--ink-soft);
}
.action:hover { border-color: var(--line-strong); }
.action:disabled { opacity: .45; cursor: not-allowed; }
.action-icon { font-size: 1.1rem; color: var(--ink); }
.action[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.action[aria-pressed="true"] .action-icon { color: var(--accent-ink); }
.badge {
  display: inline-block; margin-left: 4px; padding: 0 5px;
  border-radius: 999px; background: var(--bg-sunken);
  font-size: .64rem; text-transform: uppercase; letter-spacing: .04em;
}
.action[aria-pressed="true"] .badge { background: rgba(255, 255, 255, .25); }

.panel {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius-sm); overflow: hidden;
}
.panel summary {
  padding: 10px 14px; cursor: pointer; font-weight: 600; font-size: .88rem;
  list-style: none;
}
.panel summary::-webkit-details-marker { display: none; }
.panel summary::after { content: "＋"; float: right; color: var(--ink-faint); }
.panel[open] summary::after { content: "−"; }
.panel-body {
  padding: 0 14px 14px; display: flex; flex-direction: column; gap: 9px;
  font-size: .86rem; color: var(--ink-soft);
}
.switch { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.switch input { accent-color: var(--accent); width: 16px; height: 16px; }
.panel-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.panel-actions .text-button { padding: 5px 10px; border: 1px solid var(--line); }

.stat-row { display: flex; justify-content: space-between; gap: 12px; }
.stat-row dt { color: var(--ink-faint); }
.stat-row dd { margin: 0; font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }
.technique-list { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 5px; }
.technique-list li { display: flex; justify-content: space-between; gap: 10px; }
.technique-list .count { color: var(--ink-faint); font-variant-numeric: tabular-nums; }

.primary {
  background: var(--accent); color: var(--accent-ink);
  border: 1px solid var(--accent); border-radius: var(--radius-sm);
  padding: 9px 18px; cursor: pointer; font-weight: 600;
}
.secondary {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 9px 18px; cursor: pointer; font-weight: 600;
}

/* ------------------------------------------------------------- explainer */

.explainer { margin-top: 44px; }
.explainer h2 { font-size: 1.05rem; margin-bottom: 14px; }
.explainer-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
.explainer article {
  background: var(--bg-raised); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 14px 16px;
}
.explainer h3 { font-size: .92rem; margin-bottom: 5px; }
.explainer p { margin: 0; font-size: .86rem; color: var(--ink-soft); }
kbd {
  background: var(--bg-sunken); border: 1px solid var(--line);
  border-bottom-width: 2px; border-radius: 5px;
  padding: 0 4px; font-size: .82em; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.site-footer {
  border-top: 1px solid var(--line); padding: 18px;
  text-align: center; color: var(--ink-faint); font-size: .82rem;
}
.noscript { text-align: center; padding: 20px; color: var(--danger); }

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

@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .controls { flex-direction: column; }
  .numpad { grid-template-columns: repeat(9, 1fr); }
  .num { aspect-ratio: auto; padding: 12px 0; font-size: 1.2rem; }
  .num .left { display: none; }
  .actions { grid-template-columns: repeat(6, 1fr); }
}

@media (max-width: 520px) {
  /* Narrower column, so the same sentence needs more lines. */
  .hint-bar { height: calc(5 * 1.5em); }
  .site-header { padding: 12px 14px; }
  .brand-tag { display: none; }
  .header-tools { width: 100%; justify-content: space-between; }
  .actions { grid-template-columns: repeat(3, 1fr); }
  .board-meta { gap: 8px 16px; font-size: .92rem; }
}

/* ------------------------------------------------------------- not found */

.notfound {
  max-width: 30rem;
  margin: clamp(48px, 14vh, 140px) auto;
  padding: 0 24px;
  text-align: center;
}
.notfound .brand-mark { width: 52px; height: 52px; margin-bottom: 18px; }
.notfound h1 { font-size: 1.4rem; margin-bottom: 10px; }
.notfound p { color: var(--ink-soft); margin: 0 0 18px; }
.notfound a.primary { display: inline-block; text-decoration: none; }

/* ------------------------------------------------------- print controls */

.field {
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  padding: 8px 10px 10px; margin: 0;
}
.field legend { padding: 0 4px; font-size: .76rem; color: var(--ink-faint); text-transform: uppercase; letter-spacing: .06em; }
.field-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.field-row select {
  font: inherit; color: inherit; padding: 4px 6px;
  background: var(--bg-raised); border: 1px solid var(--line); border-radius: 7px;
}
.print-status { margin: 0; font-size: .8rem; color: var(--ink-faint); min-height: 1.2em; }

/* --------------------------------------------------- printable document */

/*
 * The sheet is a document of its own. On screen it lives inside a preview
 * overlay; on paper the overlay disappears and the pages are all that is left.
 */
.print-preview {
  position: fixed; inset: 0; z-index: 40;
  overflow: auto; background: #7c828f;
  padding: 0 0 40px;
}
.print-preview[hidden] { display: none; }

.preview-bar {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 10px clamp(12px, 4vw, 28px);
  background: var(--bg-raised); border-bottom: 1px solid var(--line);
  color: var(--ink);
}
.preview-summary { font-size: .88rem; color: var(--ink-soft); }
.preview-actions { display: flex; gap: 8px; }

.print-sheet { padding: 20px 10px 0; }

.page {
  width: 190mm; min-height: 271mm;
  margin: 0 auto 18px;
  padding: 0;
  background: #fff; color: #000;
  box-shadow: 0 6px 22px rgba(0, 0, 0, .35);
  display: flex; flex-direction: column;
}

.page-header {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 8mm 8mm 3mm;
  font: 600 9pt/1.2 Helvetica, Arial, sans-serif;
  color: #000;
}
.page-header span:last-child { font-weight: 400; color: #444; }

.page-body {
  flex: 1;
  display: grid; gap: 6mm;
  padding: 0 8mm 8mm;
  align-content: start; justify-items: center;
}
.page-body.cols-1 { grid-template-columns: 1fr; }
.page-body.cols-2 { grid-template-columns: 1fr 1fr; }

.puzzle-card, .answer-card {
  break-inside: avoid; page-break-inside: avoid;
  display: flex; flex-direction: column; align-items: center;
  margin: 0; width: 100%;
}
.puzzle-card figcaption, .answer-card figcaption {
  display: flex; align-items: baseline; gap: 6px;
  margin-bottom: 2mm;
  font: 600 9pt/1.2 Helvetica, Arial, sans-serif; color: #000;
}
.answer-card figcaption { font-size: 7.5pt; font-weight: 400; }
.card-number {
  display: inline-grid; place-items: center;
  min-width: 5mm; height: 5mm; padding: 0 1mm;
  border: 0.3mm solid #000; border-radius: 1mm;
  font-size: 7pt;
}

.grid-svg { display: block; width: 100%; height: auto; }
/* Sized so each layout fills its share of the page without spilling over. */
.rows-1 .grid-svg { max-width: 168mm; }
.rows-2.cols-1 .grid-svg { max-width: 122mm; }
.rows-2.cols-2 .grid-svg { max-width: 86mm; }
.rows-3 .grid-svg { max-width: 76mm; }
.page-body.answers .grid-svg { max-width: 62mm; }

@media (max-width: 820px) {
  .page { width: auto; min-height: 0; }
}

/* --------------------------------------------------------------- print */

@page {
  size: A4 portrait;
  margin: 10mm;
}

@media print {
  /* Only the sheet survives; everything else is screen furniture. */
  body > *:not(#print-preview) { display: none !important; }

  #print-preview,
  #print-preview[hidden] {
    display: block !important;
    position: static; overflow: visible;
    background: none; padding: 0;
  }
  .preview-bar { display: none !important; }
  .print-sheet { padding: 0; }

  .page {
    width: auto; min-height: 0;
    margin: 0; box-shadow: none;
    break-after: page; page-break-after: always;
  }
  .page:last-child { break-after: auto; page-break-after: auto; }
  .page-header { padding: 0 0 3mm; }
  .page-body { padding: 0; }

  body { background: #fff; color: #000; }
}
