:root {
  --paper: #f6f2ea;
  --card: #fffdf8;
  --ink: #1c1b19;
  --muted: #6e6a62;
  --pen: #2f5da8;
  --pen-ink: #ffffff;
  --highlight: #ffe08a;
  --grid: #8a857c;
  --line: rgba(138, 133, 124, 0.35);
  --success: #2e7d4f;
  --key: #ebe5d9;
  --bar: rgba(47, 93, 168, 0.28);
  --shadow: 0 1px 2px rgba(28, 27, 25, 0.06), 0 6px 24px rgba(28, 27, 25, 0.06);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #18181a;
    --card: #222225;
    --ink: #ece7dd;
    --muted: #9c978e;
    --pen: #8ab4f8;
    --pen-ink: #0b1e3d;
    --highlight: #5e4d17;
    --grid: #55524d;
    --line: rgba(120, 116, 110, 0.35);
    --success: #7bc896;
    --key: #2c2c30;
    --bar: rgba(138, 180, 248, 0.36);
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--pen); }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.page {
  max-width: 640px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 16px 40px;
}
.title { font-size: 40px; font-weight: 900; letter-spacing: 2px; margin: 8px 0 4px; }
.intro { color: var(--muted); margin: 0 0 18px; }
.label {
  font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--muted); margin: 22px 0 10px;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 7px 14px; font-weight: 600; font-size: 15px;
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 52px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--card); font-weight: 600; padding: 10px 16px; text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--pen); color: var(--pen-ink); border-color: var(--pen); }
.btn.dark { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn-row { display: grid; gap: 10px; margin-top: 22px; }
.btn-row.two { grid-template-columns: 1fr 1fr; }

.continue {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  background: var(--ink); color: var(--paper); border: 0; border-radius: 16px; padding: 18px;
  margin-top: 8px;
}
.continue .t { font-weight: 700; font-size: 17px; }
.continue .s { opacity: 0.75; font-size: 13px; }
.progress { height: 4px; border-radius: 4px; background: rgba(255,255,255,0.18); margin-top: 10px; overflow: hidden; }
@media (prefers-color-scheme: dark) { .progress { background: rgba(0,0,0,0.25); } }
.progress > i { display: block; height: 100%; background: var(--highlight); }
@media (prefers-color-scheme: dark) { .progress > i { background: #e9c75a; } }

.row {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 6px 10px 14px; margin-bottom: 8px;
}
.row .grow { flex: 1; min-width: 0; }
.row .grow button { all: unset; display: block; cursor: pointer; width: 100%; }
.row .t { font-weight: 600; }
.row .s { font-size: 13px; color: var(--muted); }
.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 20px;
  display: inline-grid; place-items: center; color: var(--ink); flex: none;
}
.icon-btn:hover { background: var(--key); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }

.switch-row { cursor: pointer; }
.switch { width: 46px; height: 28px; border-radius: 14px; background: var(--key); position: relative; flex: none; border: 1px solid var(--line); }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--muted); transition: transform .15s; }
.switch[aria-checked="true"] { background: var(--pen); border-color: var(--pen); }
.switch[aria-checked="true"]::after { transform: translateX(18px); background: var(--pen-ink); }

.footer { margin-top: 36px; font-size: 13px; color: var(--muted); display: grid; gap: 6px; }
.footer a { color: var(--muted); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 4px; padding: max(6px, env(safe-area-inset-top)) 6px 6px;
  max-width: 1100px; margin: 0 auto; width: 100%;
}
.topbar .grow { flex: 1; min-width: 0; padding-left: 4px; }
.topbar .t { font-weight: 700; font-size: 17px; }
.topbar .s { font-size: 13px; color: var(--muted); }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 17px; padding: 0 6px; }
.timer.done { color: var(--success); }

/* ---------- game ---------- */
.game { height: 100dvh; display: flex; flex-direction: column; }
.game-body {
  flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px;
  max-width: 1100px; width: 100%; margin: 0 auto; padding: 0 10px max(12px, env(safe-area-inset-bottom));
}
.board-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
.board-wrap svg { width: 100%; height: 100%; display: block; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.controls { width: 100%; max-width: 560px; margin: 0 auto; }
@media (min-aspect-ratio: 5/4) and (min-width: 700px) {
  .game-body { flex-direction: row; align-items: center; }
  .board-wrap { height: 100%; }
  .controls { width: 380px; flex: none; }
}
.keypad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.key {
  height: 56px; border: 0; border-radius: 12px; background: var(--key); font-size: 24px; font-weight: 500;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
}
.key:active { filter: brightness(0.93); }
.key small { font-size: 11px; opacity: 0.7; margin-top: 3px; }
.key svg { width: 24px; height: 24px; fill: currentColor; }
.key[disabled] { opacity: 0.35; cursor: default; }
.key.check { grid-column: span 3; background: var(--ink); color: var(--paper); font-size: 18px; font-weight: 600; }
.solved {
  display: flex; align-items: center; gap: 12px; border-radius: 12px; padding: 14px;
  background: color-mix(in srgb, var(--success) 14%, transparent); margin-bottom: 8px;
}
.solved b { color: var(--success); font-size: 26px; }
.hint-keys { font-size: 12px; color: var(--muted); text-align: center; margin-top: 8px; }
@media (pointer: coarse) { .hint-keys { display: none; } }

/* board svg classes */
.b-bg { fill: var(--card); }
.b-sel { fill: var(--highlight); }
.b-grid { stroke: var(--grid); stroke-width: 1.5; }
.b-frame { fill: none; stroke: var(--ink); stroke-width: 6; }
.b-bar { stroke: var(--bar); stroke-width: 20; stroke-linecap: round; }
.b-g { fill: var(--ink); font-weight: 650; }
.b-v { fill: var(--pen); font-weight: 400; }
.b-v.ok { fill: var(--success); }
.b-hit { fill: transparent; cursor: pointer; }
.b-num { font-family: var(--font); text-anchor: middle; dominant-baseline: central; }

/* ---------- menu ---------- */
.menu {
  position: fixed; z-index: 20; min-width: 240px; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.18); padding: 6px;
}
.menu button {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%;
  border: 0; background: transparent; padding: 11px 12px; border-radius: 8px; text-align: left;
}
.menu button:hover { background: var(--key); }

/* ---------- collection ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 10px; }
.tile {
  aspect-ratio: 1; border-radius: 14px; border: 1px solid var(--line); background: var(--card);
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-decoration: none; color: var(--ink);
}
.tile b { font-size: 22px; }
.tile small { font-size: 11px; opacity: .8; }
.tile.started { background: var(--highlight); }
.tile.done { background: var(--ink); color: var(--paper); }

/* ---------- dialog ---------- */
dialog {
  border: 0; border-radius: 20px; background: var(--card); color: var(--ink); padding: 22px;
  width: min(420px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
dialog::backdrop { background: rgba(10, 10, 10, 0.45); }
dialog h2 { margin: 0 0 8px; font-size: 21px; }
dialog p { margin: 0 0 18px; color: var(--muted); }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; }
dialog .actions button { border: 0; background: transparent; color: var(--pen); font-weight: 700; padding: 10px 14px; border-radius: 10px; }
dialog .actions button:hover { background: var(--key); }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 4px solid var(--key); border-top-color: var(--pen); animation: spin 0.9s linear infinite; margin: 4px auto 14px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- print view ---------- */
.print-layout { max-width: 1100px; margin: 0 auto; padding: 0 16px 40px; display: grid; gap: 20px; }
@media (min-width: 900px) { .print-layout { grid-template-columns: 340px 1fr; align-items: start; } }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; }
.panel .label:first-child { margin-top: 0; }
.field { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.field input[type=number] {
  width: 72px; padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); color: var(--ink); font: inherit;
}
.check-row { display: flex; align-items: center; gap: 10px; margin-top: 14px; cursor: pointer; }
.check-row input { width: 18px; height: 18px; accent-color: var(--pen); }
.preview { background: color-mix(in srgb, var(--grid) 30%, var(--paper)); border-radius: 16px; padding: 16px; min-height: 300px; overflow: hidden; }
.preview .sheet { margin: 0 auto 16px; box-shadow: 0 2px 12px rgba(0,0,0,0.18); }
.preview-empty { color: var(--muted); text-align: center; padding: 80px 10px; }

/* printed sheets (screen preview + print) */
.sheet {
  width: var(--pw, 186mm); height: var(--ph, 272mm); background: #fff; color: #000;
  display: flex; flex-direction: column; padding: 2mm 3mm; overflow: hidden;
  font-family: var(--font);
}
.sheet h1 { font-size: 22pt; font-weight: 900; margin: 0; text-align: center; letter-spacing: 0.5pt; }
.sheet .instr { text-align: center; font-size: 10pt; margin: 1.5mm 8mm 4mm; color: #222; }
.sheet .grids { flex: 1; min-height: 0; display: grid; gap: 6mm 8mm; }
.sheet .grids.n1 { grid-template: 1fr / 1fr; }
.sheet .grids.n2 { grid-template: 1fr 1fr / 1fr; }
.sheet .grids.n4 { grid-template: 1fr 1fr / 1fr 1fr; }
.sheet .grids.ans { grid-template: repeat(3, 1fr) / 1fr 1fr; }
.sheet .item { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.sheet .item .cap { text-align: center; font-size: 9pt; letter-spacing: 0.6pt; text-transform: uppercase; margin-bottom: 1.5mm; }
.sheet .item svg { flex: 1; min-height: 0; width: 100%; }
.sheet .foot { display: flex; justify-content: space-between; font-size: 7.5pt; color: #555; margin-top: 3mm; }
.p-bg { fill: #fff; }
.p-grid { stroke: #000; stroke-width: 2; }
.p-frame { fill: none; stroke: #000; stroke-width: 6; }
.p-g { fill: #000; font-weight: 500; }
.p-a { fill: #8a8a8a; font-weight: 400; }

#print-root { display: none; }
@media print {
  html, body { background: #fff !important; height: auto; }
  #app, dialog, .menu { display: none !important; }
  #print-root { display: block; }
  #print-root .sheet { break-after: page; page-break-after: always; box-shadow: none; margin: 0; zoom: 1; }
  #print-root .sheet:last-child { break-after: auto; page-break-after: auto; }
}
