:root {
    color-scheme: light;
    --ink: #37313e;
    --muted: #625d68;
    --accent: #67476f;
    --line: #d9d2dc;
    --paper: #fffefb;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--ink);
    background: #f4f1f5;
}
* { box-sizing: border-box; }
body { margin: 0; line-height: 1.5; }
[hidden], .print-only { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button, select, input[type="text"], summary, .check-control, .top-nav a { min-height: 44px; }
button, select, input[type="text"], textarea { border: 1px solid #b6aabc; border-radius: 8px; background: #fff; padding: 9px 12px; }
button { cursor: pointer; font-weight: 650; touch-action: manipulation; }
button:hover:not(:disabled) { background: #f0eaf3; }
button:disabled { opacity: .5; cursor: default; }
.primary { color: #fff; background: var(--accent); border-color: var(--accent); }
.primary:hover:not(:disabled) { background: #4f3455; }
a { color: var(--accent); text-underline-offset: 3px; }
:focus-visible { outline: 3px solid #b2571c; outline-offset: 3px; }
.top-nav { display: flex; align-items: center; gap: 20px; background: #49334f; color: #fff; padding: 0 max(16px, calc((100vw - 1080px) / 2)); font-size: .85rem; }
.top-nav a { color: #fff; display: inline-flex; align-items: center; }
.top-nav a:last-child { margin-left: auto; }
.top-nav span { font-weight: 650; }
.page-shell { max-width: 1080px; margin: 0 auto; padding: 18px 24px 36px; }
.hero { margin-bottom: 18px; }
.hero h1 { font-family: Georgia, serif; font-size: clamp(2rem, 5vw, 2.8rem); line-height: 1.15; margin: 0 0 8px; }
.hero p { color: var(--muted); margin: 0; }
#load-status { color: var(--muted); font-size: .85rem; margin: 8px 0 16px; }
.page-shell:has(> #game-panel:not([hidden])) > #load-status { display: none; }
.toolbar { display: grid; grid-template-columns: minmax(180px, 1fr) auto auto; gap: 10px; align-items: end; margin-bottom: 18px; }
.category-field { display: grid; gap: 4px; font-size: .8rem; font-weight: 650; color: var(--muted); min-width: 0; }
.category-field select { width: 100%; font-size: .95rem; color: var(--ink); min-width: 0; }
.puzzle-layout { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 24px; align-items: start; }
.board-panel, .word-bank { border: 1px solid var(--line); border-radius: 14px; background: var(--paper); padding: 18px; min-width: 0; }
.board-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.board-heading h2 { font-family: Georgia, serif; line-height: 1.2; font-size: 1.5rem; margin: 0; overflow-wrap: anywhere; }
#progress { color: var(--muted); font-size: .8rem; font-weight: 500; white-space: nowrap; }
.board-options { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; font-size: .8rem; }
.check-control { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check-control input { width: 19px; height: 19px; accent-color: var(--accent); margin: 0; flex: 0 0 auto; }
.words-link, .back-to-board { min-height: 44px; display: inline-flex; align-items: center; }
.board-scroller { max-width: 100%; overflow: auto; border: 1px solid var(--line); border-radius: 10px; background: #f9f7fa; container-type: inline-size; scroll-padding: 8px; }
.letter-grid { --board-cols: 12; display: grid; grid-template-columns: repeat(var(--board-cols), minmax(0, 1fr)); gap: 1px; padding: 6px; width: 100%; max-width: 380px; margin-inline: auto; user-select: none; touch-action: pan-x pan-y; container-type: inline-size; }
.board-row { display: contents; }
.cell { display: flex; justify-content: center; align-items: center; aspect-ratio: 1; min-width: 0; min-height: 0; padding: 0; border: 1px solid transparent; border-radius: 4px; background: transparent; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: clamp(12px, calc(65cqw / var(--board-cols)), 22px); font-weight: 650; line-height: 1; touch-action: pan-x pan-y; }
.cell:hover:not(:disabled) { background: #e9e1ed; }
.cell.found { background: #d5eadd; color: #224b35; }
.cell.selected { background: #f3dca7; color: #3d3321; }
.cell.hint-start { border: 2px dashed #67476f; }
.cell:focus-visible { position: relative; z-index: 1; outline-width: 2px; outline-offset: 0; }
.large-letters { max-height: 70svh; }
.large-letters .letter-grid { width: max(100%, calc(var(--board-cols) * 45px + 11px)); max-width: none; }
.large-letters .cell { font-size: 22px; }
.board-help { color: var(--muted); font-size: .8rem; margin: 12px 0; }
.selection-bar { display: flex; align-items: center; gap: 10px; border-top: 1px solid var(--line); padding-top: 10px; }
#selection-text { font-size: .8rem; margin: 0; overflow-wrap: anywhere; flex: 1; }
#cancel-selection { font-size: .75rem; padding-inline: 8px; }
.status { font-size: .85rem; min-height: 1.5em; margin: 12px 0; }
.save-status { font-size: .75rem; color: var(--muted); margin: 10px 0 0; }
.storage-warning, .notice { background: #fff0d1; padding: 12px; border-radius: 8px; font-size: .85rem; }
.result { background: #e5f2e8; border: 1px solid #a6cbb1; border-radius: 10px; padding: 16px; margin: 14px 0; }
.result h3 { margin: 0 0 8px; font-family: Georgia, serif; font-size: 1.4rem; }
.result p { font-size: .9rem; }
.word-bank { position: sticky; top: 16px; max-height: calc(100svh - 32px); overflow: auto; scroll-margin-top: 16px; }
.word-bank h2 { font-family: Georgia, serif; font-size: 1.5rem; margin: 0 0 6px; }
.word-bank > p { color: var(--muted); font-size: .8rem; margin: 0 0 10px; }
.word-list { display: grid; grid-template-columns: 1fr; gap: 4px; list-style: none; margin: 0 0 12px; padding: 0; }
.word-button { width: 100%; text-align: left; min-height: 44px; font-size: .85rem; font-weight: 550; padding: 8px 10px; border-color: transparent; background: #f5f1f7; overflow-wrap: anywhere; }
.word-button[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 3px 0 var(--accent); }
.word-item.found .word-button, .word-button.found { text-decoration: line-through; color: #346349; background: #e3f0e7; }
.word-item.hinted .word-button::after, .word-button.hinted::after { content: " · hint"; color: var(--muted); font-size: .7rem; }
.bank-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.bank-actions button { font-size: .8rem; padding-inline: 6px; }
.back-to-board { font-size: .8rem; margin-top: 4px; }
.print-options { border-top: 1px solid var(--line); padding-top: 8px; margin-top: 8px; font-size: .8rem; }
#print-btn { width: 100%; }
.custom-editor { border: 1px solid #bda9c5; background: #f9f5fb; border-radius: 12px; padding: 20px; margin: 0 0 20px; scroll-margin-top: 16px; }
.custom-editor h2 { font-family: Georgia, serif; margin: 0 0 8px; }
.custom-editor > p { max-width: 650px; font-size: .9rem; color: var(--muted); }
#custom-form { max-width: 650px; }
#custom-form > label { display: block; font-size: .85rem; font-weight: 650; margin: 12px 0 6px; }
#custom-form input, #custom-form textarea { width: 100%; }
#custom-words { resize: vertical; min-height: 170px; line-height: 1.6; }
.validation { color: #8a3326; font-size: .85rem; white-space: pre-line; }
.editor-actions, .dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.guide { margin-top: 30px; max-width: 780px; scroll-margin-top: 18px; }
.guide h2 { font-family: Georgia, serif; font-size: 1.6rem; }
.guide p { font-size: .9rem; color: var(--muted); }
details { border-top: 1px solid var(--line); padding: 6px 0; }
summary { cursor: pointer; padding: 10px 0; font-weight: 650; font-size: .9rem; }
dialog { max-width: min(430px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 12px; background: var(--paper); color: var(--ink); padding: 24px; box-shadow: 0 12px 60px #281c3944; }
dialog::backdrop { background: #281c3977; }
dialog h2 { margin: 0 0 12px; font-family: Georgia, serif; }
.dialog-actions { margin-top: 20px; }
.dialog-actions button { flex: 1; }
@media (max-width: 760px) {
    .page-shell { padding: 16px 12px 28px; }
    .hero h1 { font-size: 2rem; }
    .hero p { font-size: .9rem; }
    .toolbar { grid-template-columns: 1fr 1fr; gap: 8px; }
    .category-field { grid-column: 1 / -1; }
    .toolbar > button { font-size: .85rem; padding-inline: 6px; }
    .puzzle-layout { grid-template-columns: 1fr; gap: 18px; }
    .board-panel, .word-bank { padding: 14px; }
    .board-heading { align-items: start; flex-wrap: wrap; gap: 6px; }
    .board-heading h2 { font-size: 1.35rem; }
    .board-options { margin-bottom: 6px; }
    .letter-grid { max-width: 440px; }
    .word-bank { position: static; max-height: none; overflow: visible; }
    .word-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .custom-editor { padding: 16px; }
    .selection-bar { align-items: start; }
    .guide { margin-top: 24px; }
}
@media (max-width: 360px) {
    .top-nav { font-size: .75rem; gap: 12px; padding-inline: 10px; }
    .page-shell { padding: 14px 8px 24px; }
    .hero h1 { font-size: 1.8rem; }
    .hero p { font-size: .85rem; }
    .board-panel, .word-bank { padding: 10px; }
    .word-button { font-size: .8rem; padding-inline: 7px; }
}
@media print {
    @page { margin: 12mm; }
    body { background: #fff; color: #000; font-family: Arial, sans-serif; }
    body > :not(#print-sheet) { display: none !important; }
    #print-sheet { display: block !important; }
    #print-title { margin: 0 0 3mm; font-size: 20pt; line-height: 1.15; overflow-wrap: anywhere; }
    #print-sheet > p, #answer-sheet > p { margin: 0; font-size: 10pt; line-height: 1.25; }
    /* Limit the letters to 140 mm high, plus 4.6 mm of padding/border.
       The remaining Letter portrait area holds the heading and word bank. */
    .print-grid { display: grid; grid-template-columns: repeat(var(--board-cols), minmax(0, 1fr)); width: min(170mm, calc(140mm * var(--board-cols) / var(--board-rows) + 4.6mm)); max-width: 100%; margin: 4mm auto; border: .3mm solid #555; padding: 2mm; break-inside: avoid; }
    .print-cell { display: flex; justify-content: center; align-items: center; min-width: 0; aspect-ratio: 1; font-family: Consolas, monospace; font-size: 11pt; line-height: 1; color: #000; }
    .print-cell.answer, .print-cell.solution, .print-cell.found { background: #ddd; font-weight: 700; outline: .2mm solid #777; print-color-adjust: exact; -webkit-print-color-adjust: exact; }
    .print-words { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1.5mm 5mm; margin: 0; padding: 0; font-size: 10pt; line-height: 1.2; list-style: none; }
    .print-words li { break-inside: avoid; overflow-wrap: anywhere; }
    #answer-sheet { break-before: page; page-break-before: always; break-inside: avoid; }
    #answer-sheet h2 { font-size: 20pt; line-height: 1.15; margin: 0; }
}
