/* perfectmagicsquares.com: plain reference-publication styling. Palette and type scale follow DESIGN.md. */
:root {
  --bg: #FCFCFB; --text: #16181A; --text-muted: #4B5058; --rule: #D9DBDE;
  --border-control: #8A9099; --accent: #1F4FA3; --accent-hover: #173C7D;
  --on-accent: #FFFFFF; --error: #A61B1B; --selection: #DCE6F7;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1B1C1E; --text: #ECECEC; --text-muted: #A8ACB2; --rule: #3A3C40;
    --border-control: #7B8089; --accent: #8DB4F0; --accent-hover: #B3CDF6;
    --on-accent: #0F1113; --error: #FF9B94; --selection: #2C3A52;
  }
}
:root[data-theme="dark"] {
  --bg: #1B1C1E; --text: #ECECEC; --text-muted: #A8ACB2; --rule: #3A3C40;
  --border-control: #7B8089; --accent: #8DB4F0; --accent-hover: #B3CDF6;
  --on-accent: #0F1113; --error: #FF9B94; --selection: #2C3A52;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scrollbar-color: var(--border-control) var(--bg); accent-color: var(--accent); }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 400 17px/1.65 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow-wrap: break-word; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased;
}
@media (min-width: 600px) { body { font-size: 18px; } }
::selection { background: var(--selection); color: var(--text); }
img, video, svg, iframe, canvas { max-width: 100%; height: auto; }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }

.skip { position: absolute; left: 8px; top: -60px; z-index: 10; background: var(--bg); color: var(--text); padding: 10px 16px; border: 1px solid var(--border-control); border-radius: 2px; }
.skip:focus { top: 8px; }

a { color: var(--accent); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }
a { text-decoration-skip-ink: auto; }
a:hover { color: var(--accent-hover); text-decoration-thickness: 2px; }
input, select, textarea { caret-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.wrap { width: 100%; max-width: 64rem; margin: 0 auto; padding-left: 16px; padding-right: 16px; }
@media (min-width: 600px) { .wrap { padding-left: 24px; padding-right: 24px; } }
@media (min-width: 900px) { .wrap { padding-left: 32px; padding-right: 32px; } }

/* header */
.site-header { border-bottom: 1px solid var(--rule); }
.header-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 24px; padding-top: 6px; padding-bottom: 6px; }
.wordmark { font-size: 20px; font-weight: 650; letter-spacing: -.01em; color: var(--text); text-decoration: none; min-height: 44px; display: inline-flex; align-items: center; margin-right: auto; }
.wordmark:hover { color: var(--text); text-decoration: underline; }
.site-nav { display: flex; gap: 4px 20px; flex-wrap: wrap; order: 3; width: 100%; }
@media (min-width: 600px) { .site-nav { order: 0; width: auto; } }
.site-nav a { color: var(--text); text-decoration: none; min-height: 44px; display: inline-flex; align-items: center; padding: 2px 2px 0; border-bottom: 2px solid transparent; font-size: 17px; }
.site-nav a:hover { color: var(--text); border-bottom-color: var(--border-control); }
.site-nav a[aria-current="page"] { border-bottom-color: var(--accent); font-weight: 600; }
.theme-toggle { min-height: 44px; min-width: 44px; padding: 0 4px; background: var(--bg); color: var(--text-muted); border: 0; border-bottom: 2px solid transparent; border-radius: 0; font: inherit; font-size: 15px; cursor: pointer; }
.theme-toggle:hover { color: var(--text); border-bottom-color: var(--border-control); }
.theme-toggle [data-current] { color: var(--text); font-weight: 650; text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 4px; }
.theme-toggle:active, .btn:active { transform: translateY(1px); }

/* page */
.page { padding-top: 20px; padding-bottom: 64px; }
@media (min-width: 900px) { .page { padding-top: 32px; padding-bottom: 80px; } }
.page > *:not(.tool-wide):not(.table-wrap):not(.related):not(.toc):not(.ad-slot):not(figure) { max-width: 68ch; }
.page > .tool-wide, .page > .related { max-width: none; }
.crumbs { font-size: 15px; color: var(--text-muted); margin: 0 0 8px; }
.crumbs a { color: var(--text-muted); }
h1, h2, h3, h4 { font-weight: 650; color: var(--text); }
h1 { font-size: 29px; line-height: 1.2; margin: 8px 0 16px; letter-spacing: -.01em; text-wrap: balance; }
h2 { font-size: 23px; line-height: 1.3; margin: 48px 0 16px; text-wrap: balance; }
h3 { font-size: 19px; line-height: 1.35; margin: 32px 0 8px; text-wrap: balance; }
@media (min-width: 600px) { h1 { font-size: 36px; } h2 { font-size: 26px; } h3 { font-size: 20px; } }
@media (min-width: 900px) { h2 { margin-top: 56px; } .type-tool .page > .tool + section > h2:first-child { margin-top: 48px; } }
.type-article .page > h2, .type-tool .page > section > h2:first-child, .type-home .page > section > h2:first-child { padding-top: 24px; border-top: 1px solid var(--rule); }
p, li { text-wrap: pretty; }
p { margin: 0 0 1em; }
.lead { font-size: 18px; line-height: 1.55; }
@media (min-width: 600px) { .lead { font-size: 20px; } }
.meta { font-size: 15px; line-height: 1.5; color: var(--text-muted); margin-bottom: 16px; }
@media (min-width: 600px) { .meta { margin-bottom: 28px; } }
.meta a { color: var(--text-muted); }
.page-foot { margin-top: 48px; }
ul, ol { padding-left: 1.4em; margin: 0 0 1em; }
li { margin-bottom: .35em; }
code, kbd { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .92em; }
.num, .tabular { font-variant-numeric: tabular-nums lining-nums; }
.small, figcaption, .help { font-size: 15px; line-height: 1.5; color: var(--text-muted); }
.formula { font-variant-numeric: tabular-nums lining-nums; font-size: 1.1em; margin: 0 0 1em; }
.link-list { list-style: none; padding: 0; }
.link-list li { margin-bottom: 0; padding: 14px 0; border-bottom: 1px solid var(--rule); }
.link-list li:first-child { border-top: 1px solid var(--rule); }
.link-list li > a { font-weight: 600; }
@media (min-width: 720px) {
  .link-list { display: grid; grid-template-columns: 1fr 1fr; column-gap: 40px; }
  .link-list li:nth-child(2) { border-top: 1px solid var(--rule); }
  .related .link-list, .page > section .link-list { max-width: none; }
}
.item-note { display: block; font-size: 16px; line-height: 1.5; color: var(--text-muted); margin-top: 2px; }
.toc { margin: 0 0 32px; font-size: 16px; padding: 12px 0 4px; border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); }
.toc-h { font-size: 16px; margin: 0 0 4px; }
.sources li { margin-bottom: .6em; }

.tool + section > h2:first-child, .tool + h2,
.type-tool .page > .tool + section > h2:first-child, .type-home .page > .tool + section > h2:first-child { border-top: 0; padding-top: 0; margin-top: 40px; }
.result-empty { margin: 0; color: var(--text-muted); max-width: 36ch; }
.result:not([hidden]) + .result-empty { display: none; }

/* callout: no fill; a 1px outline in the rule colour */
.callout { border: 1px solid var(--rule); border-radius: 2px; padding: 12px 16px; margin: 0 0 1em; }
.callout > :last-child { margin-bottom: 0; }

/* tables */
.table-wrap { overflow-x: auto; max-width: 100%; margin: 0 0 1em; -webkit-overflow-scrolling: touch; }
.table-wrap:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
table { border-collapse: collapse; font-variant-numeric: tabular-nums lining-nums; }
table caption { text-align: left; font-size: 15px; color: var(--text-muted); padding-bottom: 8px; caption-side: top; }
.data-table { width: 100%; border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); }
.data-table th { font-size: 15px; font-weight: 600; color: var(--text-muted); text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--rule); }
.data-table td { padding: 10px 12px; border-bottom: 1px solid var(--rule); vertical-align: top; }
.data-table th:first-child, .data-table td:first-child { padding-left: 0; }
.data-table tr:last-child td { border-bottom: 0; }
.data-table .n, .data-table th.n { text-align: right; }

/* magic square grid (signature component) */
.msq { margin: 0; }
.msq td, .msq th { text-align: center; }
.msq { font-variant-numeric: tabular-nums lining-nums; }
.msq tbody td:not(.sum), .msq-input td { width: 48px; height: 48px; min-width: 48px; border: 1px solid var(--border-control); background: var(--bg); font-size: 20px; font-weight: 500; line-height: 1; padding: 0; }
.msq .sum { color: var(--text-muted); font-size: 16px; padding: 6px 10px; font-feature-settings: "tnum"; min-width: 48px; font-weight: 400; }
.msq .sum.diag { white-space: nowrap; }
@media (min-width: 600px) { .msq { font-variant-numeric: tabular-nums lining-nums; }
.msq tbody td:not(.sum), .msq-input td { width: 56px; height: 56px; min-width: 56px; font-size: 22px; } }
.msq-input td input { width: 100%; height: 100%; min-height: 44px; border: 0; background: transparent; color: var(--text); font: inherit; text-align: center; padding: 0; border-radius: 0; font-variant-numeric: tabular-nums; }
.msq-input td input { box-shadow: inset 0 -3px 0 var(--border-control); cursor: text; }
.msq-input td input:focus-visible { outline: 2px solid var(--accent); outline-offset: -3px; }
.msq-input td input.given { font-weight: 700; }
.msq-input td input[aria-invalid="true"] { outline: 2px dashed var(--error); outline-offset: -4px; }
.msq-input input[readonly] { cursor: default; }
.loshu td { width: 72px; height: 72px; vertical-align: middle; }
.loshu .digit { display: block; font-size: 22px; font-weight: 650; }
.loshu .count { display: block; font-size: 15px; color: var(--text-muted); }
.msq-cap { font-size: 15px; line-height: 1.5; color: var(--text-muted); margin: 0 0 8px; max-width: 68ch; }
.line-checks { margin: 0 0 1em; }
.line-checks summary { cursor: pointer; min-height: 44px; display: inline-flex; align-items: center; gap: 10px; padding: 0 16px; font-size: 16px; font-weight: 500; color: var(--text); border: 1px solid var(--border-control); border-radius: 2px; list-style: none; }
.line-checks summary::-webkit-details-marker { display: none; }
.line-checks summary::before { content: ""; width: 8px; height: 8px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg) translate(-2px, -2px); flex: none; }
.line-checks[open] summary::before { transform: rotate(-135deg) translate(-2px, -2px); }
.line-checks summary:hover { border-color: var(--text); }
.line-checks .when-open, .line-checks[open] .when-closed { display: none; }
.line-checks[open] .when-open { display: inline; }
.msq-legend { margin: 8px 0 0; max-width: 68ch; }
.scroll-hint { margin: 4px 0 0; }
.tools-row { margin: 16px 0 0; font-size: 16px; display: flex; flex-wrap: wrap; gap: 0 20px; align-items: center; }
.tools-row-h { color: var(--text-muted); }
.tools-row a { display: inline-flex; align-items: center; min-height: 44px; }
.result[data-stale] .solutions, .result[data-stale] .figure { opacity: .6; }
.line-h { font-size: 15px; margin: 12px 0 4px; color: var(--text-muted); }
.line-list { columns: 3 8rem; font-size: 15px; color: var(--text-muted); list-style: none; padding: 0; margin: 0 0 1em; font-variant-numeric: tabular-nums; }
.line-list li { margin-bottom: 2px; break-inside: avoid; }
.check-line { display: flex; gap: 8px; align-items: flex-start; font-weight: 600; margin: 8px 0 12px; }
.status .icon, .check-line .icon { color: var(--text); }
.icon { width: 16px; height: 16px; flex: none; margin-top: .35em; vertical-align: -2px; }
.ok { color: var(--text); }
.solutions { list-style: none; padding: 0; display: grid; gap: 24px 32px; grid-template-columns: repeat(auto-fill, minmax(17rem, 1fr)); }
.solution h3 { margin: 0 0 8px; font-size: 16px; color: var(--text-muted); font-weight: 600; }

/* tool frame */
.tool { border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule); padding: 16px 0; margin: 12px 0 0; }
@media (min-width: 600px) { .tool { padding: 24px 0; margin-top: 20px; } }
.page > *:last-child:not(.ad-slot) { margin-bottom: 0; }
.type-article .page :is(p, ul, ol, h2, h3, .callout) { max-width: 68ch; }
.tool-wide { display: block; }
.tool-form { max-width: 20rem; }
@media (min-width: 720px) {
  .tool.split { display: grid; grid-template-columns: 20rem minmax(0, 1fr); gap: 32px; align-items: start; }
}
.field { margin: 0 0 16px; }
.field label, .field .label, legend { display: block; font-weight: 600; margin-bottom: 6px; }
.help { margin: 4px 0 0; }
input[type="text"]:not(.msq-input input), input[type="number"], select {
  font: inherit; font-size: 16px; min-height: 44px; width: 100%; padding: 8px 12px; background: var(--bg); color: var(--text);
  border: 1px solid var(--border-control); border-radius: 2px;
}
input[type="text"]:not(.msq-input input):hover, input[type="number"]:hover, select:hover { border-color: var(--text-muted); }
input[aria-invalid="true"]:not(.msq-input input), select[aria-invalid="true"] { border: 2px solid var(--error); }
.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 20px; font: inherit; font-weight: 600; border-radius: 2px; cursor: pointer; border: 1px solid var(--accent); background: var(--accent); color: var(--on-accent); }
.btn:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.secondary { background: var(--bg); color: var(--text); border-color: var(--border-control); font-weight: 500; }
.btn.secondary:hover { background: var(--bg); border-color: var(--text); }
.btn:disabled, .btn[aria-disabled="true"] { background: var(--bg); color: var(--text-muted); border: 1px dashed var(--border-control); cursor: not-allowed; }
.btn-row { display: flex; gap: 12px; flex-wrap: wrap; margin: 0 0 16px; }
fieldset { border: 0; padding: 0; margin: 0 0 16px; min-width: 0; }
.err { color: var(--error); font-weight: 600; margin: 0 0 16px; display: flex; gap: 8px; align-items: flex-start; }
.result { min-width: 0; }
.result-label { font-size: 15px; color: var(--text-muted); margin: 0 0 2px; }
output.figure { display: inline-block; font-size: 30px; font-weight: 650; line-height: 1.2; font-variant-numeric: tabular-nums lining-nums; }
@media (min-width: 600px) { output.figure { font-size: 34px; } }
.unit { color: var(--text-muted); font-size: 15px; margin-left: 8px; }
.status { font-weight: 600; margin: 12px 0; }
.status .icon { margin-right: 4px; }
.hint-note, .repeat-note { min-height: 1.5em; font-size: 15px; color: var(--text-muted); margin: 4px 0 8px; }

/* ads: empty and invisible until filled */
.ad-slot:empty { display: none; }
.ad-slot:not(:empty) { margin: 48px 0; padding: 16px; border: 1px dashed var(--rule); min-height: 90px; }
.ad-slot:not(:empty)::before { content: "ADVERTISEMENT"; display: block; font-size: 13px; letter-spacing: .04em; color: var(--text-muted); margin-bottom: 8px; }
.ad-slot[data-slot="affiliate"]:not(:empty)::before { content: "SPONSORED LINK"; }

/* figures from articles */
figure { margin: 0 0 1.5em; }
figure img { outline: 1px solid var(--rule); outline-offset: -1px; }
figcaption { margin-top: 8px; }
.stepper-controls { display: flex; gap: 12px; flex-wrap: wrap; margin: 12px 0; }

/* footer */
.site-footer { border-top: 1px solid var(--rule); font-size: 15px; line-height: 1.5; padding: 40px 0 56px; }
.foot-cols { display: grid; gap: 24px; grid-template-columns: 1fr; margin-bottom: 24px; }
@media (min-width: 720px) { .foot-cols { grid-template-columns: repeat(3, 1fr); } }
.foot-h { font-size: 15px; font-weight: 650; margin: 0 0 10px; }
.site-footer ul { list-style: none; padding: 0; margin: 0; }
.site-footer li { margin-bottom: 0; }
.site-footer a { color: var(--text); }
.site-footer li a { display: inline-flex; align-items: center; min-height: 44px; text-decoration-color: var(--border-control); }
.site-footer li a:hover { text-decoration-color: currentColor; }
.foot-note { color: var(--text-muted); margin: 0; max-width: 68ch; padding-top: 24px; border-top: 1px solid var(--rule); }

/* worksheets */
.ws-sheet { margin-top: 32px; }
.ws-title { font-size: 20px; margin: 0 0 4px; }
.ws-meta { font-size: 15px; color: var(--text-muted); }
.ws-list { display: grid; gap: 24px 32px; grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr)); }
.ws-puzzle h2, .ws-puzzle h3 { font-size: 17px; margin: 0 0 4px; border: 0; padding: 0; }
.ws-grid td { width: 56px; height: 56px; border: 1px solid var(--border-control); font-size: 22px; }

@media print {
  :root:root:root { --bg: #fff; --text: #000; --text-muted: #000; --rule: #000; --border-control: #000; --accent: #000; --accent-hover: #000; }
  body { background: #fff; color: #000; font-size: 12pt; }
  .site-header, .site-footer, .ad-slot, .theme-toggle, .skip, .crumbs { display: none !important; }
  a { color: #000; text-decoration: underline; }
  .page { padding: 0; max-width: none; }
  .page-worksheets .page > *:not(#sheet-region) { display: none !important; }
  .page-worksheets #sheet-region { display: block !important; }
  .ws-sheet { margin: 0; }
  .ws-answers { break-before: page; }
  .ws-puzzle { break-inside: avoid; }
  .ws-grid td { width: 18mm; height: 18mm; font-size: 16pt; }
  .ws-list { grid-template-columns: repeat(2, 1fr); }
}
