/* Color tokens follow polyomino-panel / multiverse-star-battle. */
:root {
  color-scheme: light;
  --primary-blue: #1976d2;
  --color-solved: #2e7d32;
  --color-progress: #ffca28;
  --color-progress-text: #3a2a00;
  --color-error: #c62828;
  --bg-white: #fff;
  --bg-light: #f8f9fa;
  --border-light: #e0e0e0;
  --border-input: #ddd;
  --border-subtle: #eee;
  --border-strong: #bbb;
  --primary-blue-hover-bg: #f0f6ff;
  --text-primary: #111;
  --text-secondary: #666;

  --cell-bg: #fff;
  --cell-border: #ccc;
  --clue-bg: #e9ecef;
  --void-bg: #e9ecef;
  --clue-ink: #111;
  --clue-ok-bg: #d7ecd9;
  --clue-ok-ink: #2e7d32;
  --clue-bad-bg: #f6c9c6;
  --shaded: #111;
  --shaded-bad: #c62828;
  --loop: #1976d2;
  --hint-focus: rgba(244, 236, 156, 0.8); --hint-clue: rgba(25, 118, 210, 0.28); --hint-range: rgba(25, 118, 210, 0.13);
  --hint-path: rgba(122, 81, 149, 0.28); --hint-problem: #d55e00; --hint-ink: #8a6d00;
  --axis: #888;
  --dot: #6b7a8c;
  --frame: #111;
  --cell-hover: rgba(0, 0, 0, 0.08);
  --pointer: rgba(25, 118, 210, 0.75);       /* the outline of an arrow that counts the cell under the cursor */
  --axis-here: #1976d2;                      /* the row and column labels of that cell */
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --primary-blue: #64b5f6;
  --color-solved: #66bb6a;
  --color-progress: #b8860b;
  --color-progress-text: #fff;
  --color-error: #ef5350;
  --bg-white: #1a1a1a;
  --bg-light: #1e1e1e;
  --border-light: #3a3a3a;
  --border-input: #444;
  --border-subtle: #333;
  --border-strong: #666;
  --primary-blue-hover-bg: #1a2a3a;
  --text-primary: #eee;
  --text-secondary: #aaa;

  --cell-bg: #222;
  --cell-border: #444;
  --clue-bg: #343a40;
  --void-bg: #343a40;
  --clue-ink: #eee;
  --clue-ok-bg: #28452c;
  --clue-ok-ink: #81c784;
  --clue-bad-bg: #5a2623;
  --shaded: #c9d1d9;   /* light on the dark board: black squares vanish against #222 */
  --shaded-bad: #ef5350;
  --loop: #64b5f6;
  --hint-focus: rgba(255, 213, 79, 0.35); --hint-clue: rgba(100, 181, 246, 0.35); --hint-range: rgba(100, 181, 246, 0.16);
  --hint-path: rgba(179, 157, 219, 0.35); --hint-problem: #ff8a50; --hint-ink: #ffd54f;
  --axis: #999;
  --dot: #8a99ab;
  --frame: #ccc;
  --cell-hover: rgba(255, 255, 255, 0.1);
  --pointer: rgba(100, 181, 246, 0.85);
  --axis-here: #64b5f6;
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg-white); color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
#game-container { max-width: 640px; margin: 0 auto; padding: 8px 12px 40px; }

.topbar { display: flex; align-items: center; justify-content: space-between; }
.topbar h1 { font-size: 1.3rem; margin: 6px 0; }
.icon-btn {
  width: 34px; height: 34px; padding: 0; font-size: 1.3rem; line-height: 1; border-radius: 50%;
  background: var(--bg-light); border: 1px solid var(--border-light); color: var(--text-secondary);
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:not(:disabled):hover { background: var(--primary-blue-hover-bg); border-color: var(--primary-blue); color: var(--primary-blue); }
button, select, input[type=number] {
  font: inherit; color: inherit; background: var(--bg-light);
  border: 1px solid var(--border-input); border-radius: 6px; padding: 5px 10px; cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--primary-blue); }
button:disabled { opacity: 0.45; cursor: default; }
button.ghost { background: transparent; }

.nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin: 4px 0; }
.pager { display: flex; align-items: center; gap: 4px; }
.pager input { width: 4.2rem; text-align: center; cursor: text; }
.pager-total { color: var(--text-secondary); margin-right: 4px; }
.step-btn { width: 2rem; padding: 4px 0; }

.infobar { display: flex; justify-content: space-between; align-items: baseline; min-height: 1.8rem; color: var(--text-secondary); }
.status { font-weight: 600; }
.rotate-btn { padding: 0.15rem 0.6rem; font-size: 0.85rem; margin: 0 0.5rem; }
.status.solved { color: var(--color-solved); }
.controls { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin: 6px 0 10px; }
.btn-group { display: flex; gap: 6px; }

#board { margin: 0 auto; touch-action: none; user-select: none; -webkit-user-select: none; }
#board svg { width: 100%; height: auto; display: block; }
#board.solved svg { filter: drop-shadow(0 0 6px var(--color-solved)); }
.cell-bg { fill: var(--cell-bg); }
.grid-line { stroke: var(--cell-border); stroke-width: 1; }
.frame { fill: none; stroke: var(--frame); stroke-width: 3; }
.clue-bg { fill: var(--clue-bg); }
.void-bg { fill: var(--void-bg); pointer-events: none; }                                  /* a void: the gray of a clue cell */
.void-wall { stroke: var(--frame); stroke-width: 3; stroke-linecap: square; fill: none; pointer-events: none; }   /* the thick outline round the voids */
.clue-bg.ok { fill: var(--clue-ok-bg); }
.clue-bg.bad { fill: var(--clue-bad-bg); }
.region-line { fill: none; stroke: var(--frame); stroke-width: 3; stroke-linecap: square; pointer-events: none; }
.region-num { fill: var(--clue-ink); font-weight: 700; paint-order: stroke; stroke: var(--cell-bg); stroke-width: 3px; stroke-linejoin: round; pointer-events: none; }
.region-num.ok { fill: var(--clue-ok-ink); }
.region-num.bad { fill: var(--shaded-bad); }
.clue-text { fill: var(--clue-ink); font-weight: 700; text-anchor: middle; dominant-baseline: central; }
.clue-arrow { fill: none; stroke: var(--clue-ink); stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }   /* a hex arrow, drawn at its true angle */
.shaded { fill: var(--shaded); }
.shaded.bad { fill: var(--shaded-bad); }
.seg { stroke: var(--loop); stroke-linecap: round; fill: none; }
.dot { fill: var(--dot); }
.dot.auto { opacity: 0.5; } /* placed automatically: goes away with its reason */
.hover { fill: var(--cell-hover); pointer-events: none; }

/* ---- hint overlay: each role its own color or texture ---- */
.hint-undecided { fill: rgba(244, 236, 156, 0.4); pointer-events: none; }
.hint-range { fill: var(--hint-range); pointer-events: none; }
.hover-ray { fill: var(--hint-clue); opacity: 0.6; pointer-events: none; }    /* an open cell: where a box could still go */
.hover-ray.decided { fill: var(--hint-range); opacity: 0.45; }              /* a box, a dot or a line is already there */
.hint-clue { fill: var(--hint-clue); pointer-events: none; }
.hint-path { fill: var(--hint-path); pointer-events: none; }
.hint-focus { fill: var(--hint-focus); pointer-events: none; }
.hint-problem { fill: url(#hatch); pointer-events: none; }
.hatch-bg { fill: transparent; }
.hatch-line { stroke: var(--hint-problem); opacity: 0.55; }
.hint-focus-ring { fill: none; stroke: var(--hint-ink); stroke-width: 3; pointer-events: none; }
.ghost-shade { fill: var(--hint-focus); stroke: var(--hint-ink); stroke-width: 2; stroke-dasharray: 4 3; pointer-events: none; }
.imag-shade { fill: var(--hint-problem); fill-opacity: 0.18; stroke: var(--hint-problem); stroke-width: 2; stroke-dasharray: 4 3; pointer-events: none; }
.imag-dot { fill: none; stroke: var(--hint-problem); stroke-width: 2.5; pointer-events: none; }
.ghost-dot { fill: none; stroke: var(--hint-ink); stroke-width: 2.5; pointer-events: none; }
.chain-seg { stroke: var(--hint-problem); stroke-linecap: round; stroke-dasharray: 6 5; opacity: 0.9; pointer-events: none; }
.ghost-seg { stroke: var(--hint-ink); stroke-linecap: round; stroke-dasharray: 5 4; pointer-events: none; }
.ghost-x { stroke: var(--color-error); stroke-width: 4; stroke-linecap: round; fill: none; pointer-events: none; }
.ghost-x-halo { stroke: var(--bg-white); stroke-width: 8; stroke-linecap: round; fill: none; pointer-events: none; }
.over { fill: none; stroke: var(--color-error); stroke-width: 3; pointer-events: none; }
.axis-label { fill: var(--axis); font-size: 11px; text-anchor: middle; dominant-baseline: auto; pointer-events: none; }
.axis-label.side { text-anchor: end; dominant-baseline: central; }
.axis-label.here { fill: var(--axis-here); font-weight: 700; }
.hover-pointer { fill: none; stroke: var(--pointer); stroke-width: 2; pointer-events: none; }
.line-end { fill: none; stroke: var(--clue-ink); stroke-width: 2.5; pointer-events: none; }   /* ringed at both ends of the line under the pointer */
.line-end-halo { fill: none; stroke: var(--cell-bg); stroke-width: 6; pointer-events: none; }
.given-ring { fill: none; stroke: var(--cell-bg); stroke-width: 2.5; pointer-events: none; }
.hint-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 6px; font-size: 0.75rem; opacity: 0.9; }
.hint-legend .chip { display: inline-block; width: 11px; height: 11px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; border: 1px solid rgba(255, 255, 255, 0.6); }
.wrong-seg { stroke: var(--color-error); stroke-linecap: round; opacity: 0.45; pointer-events: none; }
.wrong { fill: none; stroke: var(--color-error); stroke-width: 3; stroke-dasharray: 5 3; }

.toast {
  position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); max-width: 90vw;
  background: var(--text-primary); color: var(--bg-white); padding: 8px 14px; border-radius: 8px; cursor: pointer;
}
.toast[hidden] { display: none; }
.toast.below { position: static; transform: none; margin: 12px auto 0; width: min(560px, 94vw); }   /* a hint that would cover the board sits under it instead */
.toast.ghost { visibility: hidden; }   /* a dismissed in-page message: gone, but its space stays so the board does not move */
.toast.hint { text-align: left; width: min(560px, 94vw); line-height: 1.4; }
.toast.hint .hint-meta { font-size: 0.75rem; opacity: 0.75; margin-bottom: 2px; font-family: ui-monospace, Menlo, monospace; }
.toast.hint .hint-more { margin-top: 4px; font-size: 0.78rem; opacity: 0.75; }

/* --- Modals (same look as polyomino-panel / multiverse-star-battle) ------------------------ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5);
  display: flex; justify-content: center; align-items: center; z-index: 2000; transition: opacity 0.2s;
}
.modal-hidden { opacity: 0; pointer-events: none; }
.modal-box {
  background: var(--bg-white); border-radius: 8px; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  max-width: 480px; width: 90%; max-height: 80vh; overflow-y: auto;
}
.modal-box--wide { max-width: 560px; width: 94%; max-height: 85vh; }
.modal-header {
  display: flex; justify-content: space-between; align-items: center; padding: 16px 20px;
  border-bottom: 1px solid var(--border-subtle);
  position: sticky; top: 0; z-index: 1; background: var(--bg-white); /* the close button stays reachable when the box scrolls */
}
.modal-header h2 { margin: 0; font-size: 1.2rem; }
.modal-header button { background: none; border: none; font-size: 1.2rem; padding: 4px 8px; color: var(--text-secondary); }
.modal-header button:not(:disabled):hover { background: var(--border-light); border-radius: 4px; }
.modal-body { padding: 16px 20px; line-height: 1.6; }
.modal-body h3 { margin: 18px 0 6px; font-size: 1rem; }
.modal-body p { margin: 0 0 8px; }
.modal-body ul { margin: 4px 0 8px; padding-left: 1.25em; }
.modal-body li { margin: 6px 0; }

.settings-list { display: flex; flex-direction: column; }
.settings-section { margin: 18px 0 0; padding-top: 14px; border-top: 1px solid var(--border-light); font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-secondary); }
.settings-row {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 0;
  border-bottom: 1px solid var(--border-subtle); cursor: pointer;
}
.settings-row:last-child { border-bottom: none; }
.settings-row-text { display: flex; flex-direction: column; gap: 2px; }
.settings-label { font-weight: 600; font-size: 0.95rem; color: var(--text-primary); }
.settings-desc { font-size: 0.78rem; color: var(--text-secondary); line-height: 1.35; }
.settings-row button { white-space: nowrap; flex-shrink: 0; }
#palette-group[hidden], .palette-custom[hidden] { display: none; }
.settings-row--sub { cursor: default; border-bottom: none; padding-bottom: 8px; padding-left: 14px; }
.settings-row--sub select { flex-shrink: 0; max-width: 55%; }
.palette-panel { padding: 0 0 14px 14px; border-bottom: 1px solid var(--border-subtle); }
.palette-strip { display: flex; flex-wrap: wrap; gap: 8px 7px; padding-top: 4px; }
.swatch { position: relative; width: 24px; height: 24px; border-radius: 6px; border: 1px solid var(--border-strong); }
.swatch input[type=color] { position: absolute; inset: 0; width: 100%; height: 100%; padding: 0; border: none; background: none; cursor: pointer; opacity: 0; }
.swatch-x {
  position: absolute; top: -7px; right: -7px; width: 16px; height: 16px; padding: 0; border-radius: 50%; line-height: 1; font-size: 11px;
  background: var(--bg-white); border: 1px solid var(--border-strong); color: var(--text-secondary);
}
.palette-custom { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.palette-custom .settings-desc { flex-basis: 100%; }
.settings-row .danger-btn { border: 1.5px solid #d9534f; background: transparent; color: #d9534f; font-weight: 600; }
.settings-row .danger-btn:hover, .settings-row .danger-btn.danger-btn--confirm { background: #d9534f; color: #fff; border-color: #d9534f; }

.toggle-switch { flex-shrink: 0; position: relative; }
.toggle-switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.toggle-track { display: block; width: 44px; height: 24px; background: var(--border-strong); border-radius: 12px; position: relative; transition: background 0.2s; cursor: pointer; }
.toggle-switch input:checked + .toggle-track { background: var(--primary-blue); }
.toggle-switch input:focus-visible + .toggle-track { outline: 2px solid var(--primary-blue); outline-offset: 2px; }
.toggle-thumb { position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; background: var(--bg-white); border-radius: 50%; transition: transform 0.2s; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }
.toggle-switch input:checked + .toggle-track .toggle-thumb { transform: translateX(20px); }

.ok-text { color: var(--color-solved); font-weight: 600; }
.bad-text { color: var(--color-error); font-weight: 600; }

/* --- Puzzle selector (Browse): augments the prev/next arrows, like polyomino-panel's --------------------------------- */
.browse-btn { padding: 4px 10px; font-size: 0.85rem; margin-left: 4px; }
.browse-modal-body { padding: 12px 16px 20px; }
.browse-summary { font-weight: 600; margin-bottom: 8px; }
.browse-toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.browse-legend { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 10px; font-size: 0.8rem; color: var(--text-secondary); }
.legend-dot { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: middle; margin-right: 2px; }
.legend-solved { background: var(--color-solved); }
.legend-in-progress { background: var(--color-progress); }
.legend-current { background: var(--primary-blue); }
.legend-unsolved { background: var(--border-light); border: 1px solid var(--border-strong); }
.browse-jump-btn { font-size: 0.82rem; padding: 5px 10px; white-space: nowrap; }
.browse-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; max-height: 55vh; overflow-y: auto; padding: 2px; }
.browse-tile {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  font-size: 0.8rem; font-weight: 700; border-radius: 6px; cursor: pointer; padding: 0; min-width: 0;
  border: 2px solid transparent; background: var(--border-light); color: var(--text-primary);
}
.browse-tile:hover { filter: brightness(0.93); }
.browse-tile.bt-solved { background: var(--color-solved); color: #fff; }
.browse-tile.bt-in-progress { background: var(--color-progress); color: var(--color-progress-text); }
.browse-tile.bt-current { border-color: var(--primary-blue); box-shadow: 0 0 0 2px var(--primary-blue); }
.browse-section-header { grid-column: 1 / -1; padding: 8px 2px 2px; margin-bottom: 2px; border-bottom: 1px solid var(--border-light); }
.browse-section-name { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-secondary); }

/* --- Book picker: a button showing the current book opens a modal of all the books, like polyomino-panel's ---------- */
.book-btn {
  display: flex; align-items: center; gap: 6px; min-width: 0; max-width: 60%; padding: 5px 10px; text-align: left;
  background: var(--bg-white); border: 1px solid var(--border-input); border-radius: 6px; color: var(--text-primary);
}
.book-btn .book-btn-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-btn::after { content: '▾'; flex-shrink: 0; color: var(--text-secondary); }
.bp-body { padding: 8px 16px 20px; }
.bp-title { margin: 8px 0 2px; font-size: 1.05rem; }
.bp-blurb { margin: 0 0 8px; font-size: 0.85rem; color: var(--text-secondary); line-height: 1.35; }
.bp-back { background: none; border: none; padding: 4px 0; color: var(--primary-blue); font: inherit; font-size: 0.9rem; cursor: pointer; }
.bp-back:hover { text-decoration: underline; }
/* a card per group or sub-category */
.bp-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 6px 0; }
.bp-card {
  display: flex; flex-direction: column; align-items: flex-start; gap: 3px; padding: 12px; text-align: left; background: var(--bg-white);
  border: 1px solid var(--border-light); border-radius: 10px; color: var(--text-primary); cursor: pointer;
}
.bp-card:hover { border-color: var(--border-strong); }
.bp-card.bp-current { background: var(--primary-blue-hover-bg); border-color: var(--primary-blue); }
.bp-card-icon { font-size: 1.6rem; line-height: 1.1; }
.bp-card-name { font-weight: 600; font-size: 1rem; }
.bp-card-blurb { font-size: 0.78rem; color: var(--text-secondary); line-height: 1.3; }
.bp-card-meta { margin-top: auto; padding-top: 4px; font-size: 0.78rem; color: var(--text-secondary); }
/* the books of a group or sub-category */
.bp-list { display: flex; flex-direction: column; gap: 6px; margin: 6px 0; }
.bp-book {
  display: flex; width: 100%; justify-content: space-between; align-items: center; gap: 8px; padding: 9px 12px; text-align: left;
  background: var(--bg-white); border: 1px solid var(--border-light); border-radius: 8px; color: var(--text-primary); cursor: pointer;
}
.bp-book:hover { border-color: var(--border-strong); }
.bp-book.bp-current { background: var(--primary-blue-hover-bg); border-color: var(--primary-blue); }
.bp-book-text { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.bp-book-title { font-weight: 600; font-size: 0.95rem; }
.bp-book-blurb { font-size: 0.78rem; color: var(--text-secondary); line-height: 1.3; }
.bp-count { flex-shrink: 0; font-size: 0.8rem; color: var(--text-secondary); white-space: nowrap; }
.bp-count-done { color: var(--color-solved); font-weight: 600; }
@media (max-width: 380px) { .bp-cards { grid-template-columns: 1fr; } }

/* --- Print dialog (Settings -> Print puzzles) ------------------------------------------ */
.print-form { display: grid; grid-template-columns: auto 1fr; gap: 10px 12px; align-items: center; margin: 8px 0; }
.print-form input[type="text"], .print-form select { min-width: 0; font: inherit; padding: 6px 8px; }
.print-form input[type="checkbox"] { justify-self: start; width: 18px; height: 18px; }
.print-error { color: var(--color-error); font-size: 0.85rem; margin: 6px 0; }
.print-summary { color: var(--text-secondary); font-size: 0.85rem; min-height: 1.3em; }
.print-actions { display: flex; justify-content: flex-end; }

/* --- Printing: #print-root (see js/print.js) is the only thing printed ---------------- */
#print-root { display: none; }
#print-root, #print-root * { box-sizing: border-box; }
#print-root .print-page {
  display: flex; flex-direction: column; gap: 5mm; color: #111; background: #fff;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif; break-after: page;
}
#print-root .print-page:last-child { break-after: auto; }
#print-root .print-head { display: flex; align-items: center; gap: 4mm; border-bottom: 0.4mm solid #111; padding-bottom: 2mm; }
#print-root .print-brand { font-weight: 700; font-size: 5mm; }
#print-root .print-name, #print-root .print-url { color: inherit; text-decoration: none; }
#print-root .print-url { font-size: 3.2mm; color: #555; }
#print-root .print-book { margin-left: auto; font-size: 3.6mm; color: #333; }
/* the number of columns is --cols (js/print.js: as many boards as fit at the chosen cell size); the gaps match PAGE there */
#print-root .print-grid { display: grid; grid-template-columns: repeat(var(--cols, 1), max-content); gap: 6mm 5mm; justify-content: center; justify-items: center; align-content: start; }
#print-root .print-item { margin: 0; text-align: center; }
#print-root .print-caption { font-weight: 600; font-size: 3.6mm; margin-bottom: 1.5mm; text-align: left; }
#print-root .print-board { display: block; height: auto; }        /* (its width is set per board from the cell size) */
@media print {
  @page { margin: 12mm; }
  body.printing-booklet { background: #fff !important; }
  body.printing-booklet > *:not(#print-root) { display: none !important; }
  body.printing-booklet #print-root { display: block; }
}
