:root {
  color-scheme: light dark;
  --bg: #f6f4ef; --panel: #fffdf8; --ink: #1f2933; --muted: #6b7280; --line: #d9d4c7;
  --accent: #0f766e; --accent-ink: #fff; --board: #fffefb; --grid: #b9b3a3; --strong: #1f2933;
  --sel: #bfe6e1; --peer: #eef6f4; --same: #d4ece8; --given: #1f2933; --entry: #0f5f8a;
  --bad: #c0392b; --bad-bg: #fbdcd7; --hint: #ffe28a; --cage: #7c6f9a; --fill: #1f2933; --diag: #f1ece0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181d; --panel: #1c2229; --ink: #e6e9ed; --muted: #8b95a1; --line: #323b45;
    --accent: #2dd4bf; --accent-ink: #062b27; --board: #1a2027; --grid: #46515e; --strong: #cfd6de;
    --sel: #1f5f5a; --peer: #212b33; --same: #28524f; --given: #e6e9ed; --entry: #7cc4f0;
    --bad: #ff8a7a; --bad-bg: #5a2a26; --hint: #6b5a12; --cage: #a99ccc; --fill: #e6e9ed; --diag: #232c35;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
.top { display: flex; align-items: baseline; gap: 16px; padding: 14px 24px; border-bottom: 1px solid var(--line); background: var(--panel); }
.top h1 { margin: 0; font: 700 24px/1 Georgia, "Times New Roman", serif; letter-spacing: -.5px; }
.top h1 span { color: var(--accent); }
.tag { margin: 0; color: var(--muted); }
main { display: grid; grid-template-columns: 210px minmax(0, 1fr) 240px; gap: 20px; max-width: 1100px; margin: 20px auto; padding: 0 16px; align-items: start; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.panel hr { width: 100%; border: 0; border-top: 1px solid var(--line); margin: 2px 0; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
label[hidden] { display: none; }
label.check { flex-direction: row; align-items: center; gap: 8px; text-transform: none; letter-spacing: 0; font-size: 14px; color: var(--ink); }
select, textarea, button { font: inherit; color: var(--ink); }
select { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--board); text-transform: none; letter-spacing: 0; font-size: 14px; }
button { padding: 8px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--board); cursor: pointer; }
button:hover { border-color: var(--accent); }
button:focus-visible, select:focus-visible, .cell:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button[disabled] { opacity: .5; cursor: progress; }
button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.row { display: flex; gap: 6px; flex-wrap: wrap; }
.row button { flex: 1; }
.play { min-width: 0; }
.bar { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; font-weight: 600; }
#timer { font-variant-numeric: tabular-nums; font-size: 20px; color: var(--accent); }
.board-host { display: flex; justify-content: center; overflow-x: auto; padding: 4px; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
#status { min-height: 1.6em; margin-top: 10px; color: var(--muted); text-align: center; }
#status.win { color: var(--accent); font-weight: 700; font-size: 17px; }

.grid.sudoku, .grid.kenken { --cs: min(56px, calc((min(100vw, 640px) - 48px) / var(--n))); display: grid; grid-template-columns: repeat(var(--n), var(--cs)); border: 2px solid var(--strong); background: var(--board); }
.cell { position: relative; width: var(--cs); height: var(--cs); display: flex; align-items: center; justify-content: center; border: 1px solid var(--grid); font-size: calc(var(--cs) * .56); cursor: pointer; }
.sudoku .cell.bt { border-top: 2px solid var(--strong); }
.sudoku .cell.bl { border-left: 2px solid var(--strong); }
.sudoku .cell.diag { background: var(--diag); }
.cell .value { color: var(--entry); font-weight: 500; }
.cell.given .value { color: var(--given); font-weight: 700; }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); }
.cell.wrong { background: var(--bad-bg); }
.cell.wrong .value { color: var(--bad); }
.cell.hint { background: var(--hint); }
.cell.hint-elim { box-shadow: inset 0 0 0 2px var(--bad); }
.notes { position: absolute; inset: 2px; display: grid; grid-template-columns: repeat(var(--nc), 1fr); font-size: calc(var(--cs) * .22); line-height: 1; color: var(--muted); pointer-events: none; text-align: center; align-content: center; }
.cell .value:not(:empty) + .notes { display: none; }
.cage-label { position: absolute; top: 1px; left: 3px; font-size: calc(var(--cs) * .21); line-height: 1; color: var(--cage); font-weight: 700; pointer-events: none; z-index: 2; }
.grid[data-cages="dashed"] .cage-label { background: var(--board); padding: 0 1px; }
.grid[data-cages="dashed"] .cell::after { content: ""; position: absolute; inset: 3px; pointer-events: none; }
.grid[data-cages="dashed"] .cell.cg-t::after { border-top: 1.5px dashed var(--cage); }
.grid[data-cages="dashed"] .cell.cg-b::after { border-bottom: 1.5px dashed var(--cage); }
.grid[data-cages="dashed"] .cell.cg-l::after { border-left: 1.5px dashed var(--cage); }
.grid[data-cages="dashed"] .cell.cg-r::after { border-right: 1.5px dashed var(--cage); }
.grid[data-cages="solid"] .cell { border-color: color-mix(in srgb, var(--grid) 55%, transparent); }
.grid[data-cages="solid"] .cg-t { border-top: 2.5px solid var(--strong); }
.grid[data-cages="solid"] .cg-b { border-bottom: 2.5px solid var(--strong); }
.grid[data-cages="solid"] .cg-l { border-left: 2.5px solid var(--strong); }
.grid[data-cages="solid"] .cg-r { border-right: 2.5px solid var(--strong); }
.grid[data-cages="solid"] .cage-label { color: var(--strong); font-size: calc(var(--cs) * .24); top: 2px; left: 4px; }

.grid.nonogram { display: grid; gap: 0; touch-action: none; }
.grid.nonogram .cell { font-size: 12px; border: 1px solid var(--grid); background: var(--board); }
.nonogram .cell.gap-r { border-right: 2px solid var(--strong); }
.nonogram .cell.gap-b { border-bottom: 2px solid var(--strong); }
.nonogram .cell.filled { background: var(--fill); }
.nonogram .cell.cross::before, .nonogram .cell.cross::after { content: ""; position: absolute; width: 60%; height: 1.5px; background: var(--muted); transform: rotate(45deg); }
.nonogram .cell.cross::after { transform: rotate(-45deg); }
.nonogram .cell.wrong { background: var(--bad-bg); }
.nonogram .cell.filled.wrong { background: var(--bad); }
.nonogram .cell.hint { background: var(--hint); }
.nonogram .cell.sel { box-shadow: inset 0 0 0 2px var(--accent); }
.clue { display: flex; font-size: 12px; font-variant-numeric: tabular-nums; color: var(--ink); }
.clue.col { flex-direction: column; justify-content: flex-end; align-items: center; gap: 1px; padding-bottom: 3px; }
.clue.row { justify-content: flex-end; align-items: center; gap: 5px; padding-right: 6px; }
.clue.gap.col { border-right: 2px solid transparent; }
.clue.done { color: var(--muted); opacity: .55; }

.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.pad button { padding: 10px 0; font-size: 18px; }
.pad button.wide { grid-column: span 3; font-size: 14px; }
.hint-box { background: var(--hint); border-radius: 8px; padding: 10px; color: #1f2933; }
@media (prefers-color-scheme: dark) { .hint-box { color: #f5ecc0; } }
.hint-box p { margin: 6px 0 10px; }
.keys { font-size: 13px; color: var(--muted); }
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); width: min(620px, 94vw); padding: 20px; }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin-top: 0; font-family: Georgia, serif; }
dialog textarea { width: 100%; font-family: ui-monospace, monospace; font-size: 13px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--board); }
.help { color: var(--muted); font-size: 13px; white-space: pre-line; }
.result { margin: 10px 0; min-height: 1.5em; font-size: 14px; }
.result table, #stats-body table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.result td, .result th, #stats-body td, #stats-body th { padding: 4px 8px; border-bottom: 1px solid var(--line); text-align: left; }
.draw-grid { display: grid; margin: 10px auto; width: fit-content; border: 2px solid var(--strong); touch-action: none; user-select: none; }
.draw-grid div { width: var(--px, 22px); height: var(--px, 22px); border: 1px solid var(--grid); background: var(--board); }
.draw-grid div.on { background: var(--fill); }
.draw-grid div.suggest { box-shadow: inset 0 0 0 3px var(--hint); }
.stat-cards { display: flex; gap: 12px; margin: 10px 0; }
.stat-cards div { flex: 1; background: var(--bg); border-radius: 8px; padding: 10px; text-align: center; }
.stat-cards b { display: block; font-size: 26px; color: var(--accent); }

#print-sheet { display: none; }
@media (max-width: 860px) {
  main { grid-template-columns: 1fr; }
  .setup { flex-direction: row; flex-wrap: wrap; }
  .setup hr { display: none; }
  .setup label { flex: 1 1 120px; }
  .play { order: 1; }
  .setup { order: 2; }
  .tools { order: 3; }
  .pad { grid-template-columns: repeat(5, 1fr); }
  .top { padding: 12px 16px; }
}
@media print {
  @page { margin: 12mm; }
  body { background: #fff; color: #000; }
  .top, main, dialog { display: none !important; }
  #print-sheet { display: grid; color: #000; --bg: #fff; --board: #fff; --grid: #888; --strong: #000; --given: #000; --entry: #000; --cage: #444; --fill: #000; --diag: #eee; --muted: #444; }
  #print-sheet[data-layout="quad"] { grid-template-columns: 1fr 1fr; gap: 6mm 8mm; }
  #print-sheet[data-layout="quad"] .print-item { height: 118mm; break-inside: avoid; }
  #print-sheet[data-layout="quad"] .print-item:nth-child(4n + 1) { break-before: page; }
  #print-sheet[data-layout="quad"] .print-item:first-child { break-before: auto; }
  .print-item h2 { font: 600 12pt Georgia, serif; margin: 0 0 4mm; }
  .print-board { display: flex; justify-content: center; }
  #print-sheet[data-layout="quad"] .grid.sudoku, #print-sheet[data-layout="quad"] .grid.kenken { --cs: calc(80mm / var(--n)); }
  #print-sheet[data-layout="single"] .grid.sudoku, #print-sheet[data-layout="single"] .grid.kenken { --cs: calc(150mm / var(--n)); }
  #print-sheet[data-layout="quad"] .grid.nonogram { --cs: calc(60mm / (var(--w) + 4)) !important; }
  #print-sheet .cell, #print-sheet .cell * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  #print-sheet .cell.filled { background: #000; }
}
