:root {
  --bg: #fbfbf9;
  --paper: #ffffff;
  --ink: #161a19;
  --soft: #4a5452;
  --line: #c9cfcc;
  --hair: #e3e7e5;
  --accent: #00676e;
  --accent-ink: #ffffff;
  --tint: #e5f2f1;
  --blank: #f1f2ee;
  --warn-bg: #fff4dc;
  --warn: #6b4500;
  --err: #a3271c;
  --mono: ui-monospace, "SFMono-Regular", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --body: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121615;
    --paper: #1a201f;
    --ink: #e6ece9;
    --soft: #aab6b2;
    --line: #3d4846;
    --hair: #2a3331;
    --accent: #5fc6c9;
    --accent-ink: #0b1717;
    --tint: #19302f;
    --blank: #1f2624;
    --warn-bg: #3a2c0c;
    --warn: #ffd98a;
    --err: #ff9c8f;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 17px/1.55 var(--body); }
a { color: var(--accent); }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.wrap { max-width: 1180px; margin: 0 auto; padding: 0 20px; }
.vh { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -9999px; top: 8px; background: var(--paper); padding: 8px 12px; z-index: 10; }
.skip:focus { left: 8px; }
[hidden] { display: none !important; }

.top { border-bottom: 1px solid var(--line); }
.brand { display: inline-flex; align-items: center; gap: 10px; padding: 12px 0; font: 700 18px/1 var(--mono); color: var(--ink); text-decoration: none; letter-spacing: -0.01em; }
.mark { width: 30px; height: 30px; }
.m-sheet { fill: var(--paper); stroke: var(--ink); stroke-width: 1.8; }
.m-fold { stroke: var(--accent); stroke-width: 1.8; stroke-dasharray: 2.5 2; }
.m-staple { stroke: var(--ink); stroke-width: 2.4; stroke-linecap: round; }

h1, h2, h3 { font-family: var(--mono); letter-spacing: -0.02em; line-height: 1.2; }
h1 { font-size: clamp(28px, 5vw, 40px); margin: 26px 0 8px; }
h2 { font-size: 21px; margin: 0 0 12px; }
h3 { font-size: 17px; margin: 22px 0 6px; }
.lede { margin: 0 0 18px; max-width: 64ch; color: var(--ink); }

/* The answer: page count and sheets, first thing under the heading. */
.answer { border-top: 3px solid var(--ink); border-bottom: 1px solid var(--line); padding: 14px 0 12px; margin-bottom: 18px; }
.big { font: 700 clamp(24px, 4.4vw, 34px)/1.15 var(--mono); margin: 0; letter-spacing: -0.02em; }
.big #a-pages { color: var(--accent); }
.big .on { font-weight: 400; color: var(--soft); }
.big.too-long .on { display: none; }
.sub { margin: 6px 0 0; }
.warn { margin: 10px 0 0; padding: 8px 12px; background: var(--warn-bg); color: var(--warn); }
.jumps { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 8px 0 0; }
.jumps a { display: inline-block; padding: 8px 0; font-weight: 600; }
.note { padding: 8px 12px; background: var(--tint); margin: 0 0 14px; }
.share-ask { padding: 12px 14px; border: 2px solid var(--accent); margin: 0 0 16px; }
.share-ask p { margin: 0 0 8px; }

.tool { display: grid; gap: 26px; }
@media (min-width: 1100px) {
  .tool { grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr); grid-template-areas: "pieces results" "settings results"; grid-template-rows: auto 1fr; column-gap: 44px; row-gap: 10px; align-items: start; }
  .pieces-s { grid-area: pieces; } .results { grid-area: results; } .settings { grid-area: settings; }
}
.results section { margin-bottom: 26px; }
.results section + section { border-top: 1px solid var(--line); padding-top: 18px; }
.settings { border-top: 1.5px solid var(--ink); }
.settings > summary { cursor: pointer; font: 700 19px/1.3 var(--mono); letter-spacing: -0.02em; padding: 14px 0; min-height: 44px; }
.settings section { margin-bottom: 22px; }
.settings section + section { border-top: 1px solid var(--line); padding-top: 16px; }
.settings h2 { font-size: 18px; }

/* Form controls */
input[type=text], select, textarea {
  font: inherit; font-size: 16px; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 4px;
  min-height: 44px; padding: 8px 10px; width: 100%;
}
textarea { line-height: 1.45; resize: vertical; font-family: var(--body); }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input[readonly].auto { background: var(--tint); }
input:disabled { opacity: 0.6; }
label { font-weight: 600; }
.field { display: grid; gap: 4px; margin: 0 0 12px; align-content: start; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
fieldset { border: 0; margin: 0 0 8px; padding: 0; min-width: 0; }
legend { font-weight: 600; padding: 0; margin-bottom: 4px; }
.check { display: flex; gap: 10px; align-items: flex-start; font-weight: 400; padding: 9px 0; min-height: 44px; }
.check input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--accent); flex: none; }
.err { color: var(--err); margin: 4px 0 10px; font-weight: 600; }
.empty { color: var(--soft); }
.sample { margin: -4px 0 10px; color: var(--soft); }
.link { background: none; border: 0; padding: 10px 2px; font: inherit; color: var(--accent); text-decoration: underline; cursor: pointer; }

.btn {
  font: 600 16px/1.2 var(--body); min-height: 44px; padding: 10px 16px; border: 1.5px solid var(--ink); border-radius: 4px;
  background: transparent; color: var(--ink); cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { border-color: var(--err); color: var(--err); }
.btn.small { min-height: 40px; padding: 6px 12px; }
.btn:disabled { opacity: 0.5; cursor: default; }
.row-btns { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 10px 0; }
.done { margin: 6px 0 0; overflow-wrap: anywhere; }

.seg { display: flex; flex-wrap: wrap; border: 1.5px solid var(--ink); border-radius: 4px; overflow: hidden; }
.seg label { flex: 1 1 auto; position: relative; font-weight: 600; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; }
.seg span { display: flex; align-items: center; justify-content: center; min-height: 44px; padding: 8px 12px; text-align: center; white-space: nowrap; }
.seg label + label span { border-left: 1px solid var(--line); }
.seg input:checked + span { background: var(--ink); color: var(--bg); }
.seg input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: -3px; }
.seg.wide span { white-space: normal; }
.trim-sum { margin: 8px 0 10px; color: var(--soft); }

details.more summary { cursor: pointer; font-weight: 600; padding: 10px 0; min-height: 44px; }
details.more[open] summary { margin-bottom: 6px; }
.paste-box { margin: 6px 0 14px; }
.paste-box label { display: block; font-weight: 400; margin-bottom: 6px; }

/* Pieces: one ruled line per piece, number in the margin like a manuscript list. */
.pieces { list-style: none; margin: 0 0 6px; padding: 0; border-top: 1px solid var(--line); }
.piece { border-bottom: 1px solid var(--hair); padding: 6px 0; background: var(--bg); }
.piece.dragging { background: var(--tint); box-shadow: 0 2px 10px rgb(0 0 0 / 0.15); position: relative; z-index: 2; }
.prow { display: grid; grid-template-columns: 22px 24px minmax(0, 1fr) 72px max-content max-content; gap: 6px; align-items: center; }
.grip { display: flex; align-items: center; justify-content: center; height: 44px; cursor: grab; touch-action: none; color: var(--soft); }
.grip svg { width: 16px; height: 16px; fill: currentColor; }
.no { font: 600 15px var(--mono); color: var(--soft); text-align: right; }
.p-pages-w { position: relative; }
.prow .p-pages { padding-right: 30px; text-align: right; font-family: var(--mono); }
.pp { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); color: var(--soft); pointer-events: none; font-size: 15px; }
.at { font: 600 15px var(--mono); color: var(--accent); white-space: nowrap; width: var(--atw, 4ch); }
.acts { display: flex; gap: 2px; margin-left: 6px; }
.ib { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; border: 0; background: transparent; color: var(--ink); border-radius: 4px; cursor: pointer; font: 600 15px var(--body); }
.ib:hover { background: var(--tint); }
.ib svg { width: 16px; height: 16px; fill: currentColor; stroke: currentColor; stroke-width: 1.8; }
.ib.p-del svg { fill: none; }
.ib.p-text { width: auto; padding: 0 6px; white-space: nowrap; text-decoration: underline; text-decoration-color: var(--line); }
.ib.p-text.on { color: var(--accent); background: var(--tint); text-decoration-color: currentColor; }
.ib.gone { visibility: hidden; }
.ptext { padding: 4px 0 6px 58px; }
.est { margin: 4px 0 0; color: var(--soft); }
.p-err { padding-left: 58px; }
.p-check { margin: 2px 0 4px; padding: 4px 10px; margin-left: 58px; background: var(--warn-bg); color: var(--warn); }
.p-check .link { color: inherit; padding: 6px 2px; }
@media (max-width: 620px) {
  .prow { display: flex; flex-wrap: wrap; row-gap: 2px; }
  .grip { flex: 0 0 22px; } .no { flex: 0 0 22px; } .p-pages-w { flex: 0 0 72px; }
  .p-title { flex: 1 0 calc(100% - 134px); min-width: 0; }
  .at { width: auto; padding-left: 50px; }
  .acts { margin-left: auto; }
  .ptext, .p-err { padding-left: 0; }
  .p-check { margin-left: 0; }
}

/* Page plan: each row is an opening, with the fold down the middle and folios on the outer edges. */
.legend { display: grid; grid-template-columns: 1fr 1fr; margin: 0 0 4px; color: var(--soft); font-size: 16px; }
.legend span:last-child { text-align: right; }
.ledger { list-style: none; margin: 0; padding: 0; border-top: 1.5px solid var(--ink); }
.ledger li { display: grid; grid-template-columns: 1fr 1fr; border-bottom: 1px solid var(--hair); }
.pg { padding: 8px 10px; display: grid; grid-template-columns: minmax(0, 1fr); align-content: start; gap: 1px; min-height: 52px; overflow-wrap: break-word; min-width: 0; }
.pg.L { border-right: 1.5px dashed var(--line); }
.pg.R { text-align: right; }
.pg .fo { font: 700 16px var(--mono); color: var(--accent); }
.pg .what { font-weight: 600; }
.nw { white-space: nowrap; }
.pg .why { color: var(--soft); font-size: 16px; line-height: 1.35; }
.pg.k-blank { background: var(--blank); }
.pg.k-blank .what { font-weight: 400; color: var(--soft); }
.pg.k-blank .fo { color: var(--soft); }
.pg.none .what { color: var(--soft); font-weight: 400; }
.pg.k-title .what, .pg.k-copyright .what, .pg.k-dedication .what, .pg.k-epigraph .what, .pg.k-contents .what,
.pg.k-ack .what, .pg.k-notes .what, .pg.k-about .what { font-style: italic; }

/* Contents page, ready to paste */
.toc { background: var(--paper); border: 1px solid var(--line); padding: 18px 20px; max-width: 34rem; }
.toc-h { font: 700 18px var(--mono); margin: 0 0 10px; }
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list li { display: flex; align-items: baseline; gap: 8px; padding: 3px 0; }
.toc-list .t { flex: 0 1 auto; overflow-wrap: break-word; min-width: 0; }
.toc-list li::after { content: ""; order: 1; flex: 1 1 24px; border-bottom: 1.5px dotted var(--line); transform: translateY(-4px); }
.toc-list .n { order: 2; font-family: var(--mono); font-weight: 600; text-align: right; }
.pnote { margin: 8px 0; }
.clear-ask { padding: 10px 14px; border: 1.5px solid var(--err); margin: 8px 0; }
.clear-ask p { margin: 0 0 4px; }
.earlier { list-style: none; padding: 0; margin: 0; }
.earlier li { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--hair); }

.undo-bar { position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%); z-index: 20; display: flex; gap: 12px; align-items: center;
  background: var(--ink); color: var(--bg); padding: 8px 10px 8px 16px; border-radius: 6px; width: max-content; max-width: min(36rem, calc(100vw - 24px)); box-shadow: 0 4px 18px rgb(0 0 0 / 0.25); }
.undo-bar .btn { border-color: var(--bg); color: var(--bg); }

/* Explainer */
.explain { max-width: 72ch; border-top: 3px solid var(--ink); margin-top: 20px; padding-top: 18px; }
.explain h2 { margin-top: 10px; }
.faq h3 { margin-top: 20px; }
.faq p { margin: 0; }
.sources { margin-top: 26px; color: var(--soft); }

.foot { border-top: 1px solid var(--line); margin-top: 40px; padding: 18px 0 70px; }
.sb-footer p { margin: 0 0 10px; color: var(--soft); }
.sb-footer a { color: var(--ink); }
.sb-more { line-height: 1.7; }

@media (max-width: 480px) {
  .wrap { padding: 0 16px; }
  .pair { grid-template-columns: 1fr; gap: 0; }
  .pg { padding: 7px 8px; }
  .toc { padding: 14px; }
}
/* Narrow phones: stack each opening (left page above right) so no word has to break. */
@media (max-width: 360px) {
  .legend { display: none; }
  .ledger li { grid-template-columns: 1fr; }
  .pg.L { border-right: 0; border-bottom: 1.5px dashed var(--line); }
  .pg.R { text-align: left; }
  .pg.none { display: none; }
  .at { padding-left: 0; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; } }

/* ---------- Print ---------- */
#print-root { position: absolute; left: -30000px; top: 0; visibility: hidden; color: #000; font-family: var(--body); width: 7.4in; }
@page { margin: 0.4in; }
@page port { size: portrait; }
@page land { size: landscape; }
.pdoc { page: port; color: #000; font-size: 10.5pt; line-height: 1.35; }
.ph { border-bottom: 1.5pt solid #000; padding-bottom: 6pt; margin-bottom: 12pt; }
.ph h2 { font: 700 17pt/1.2 var(--mono); margin: 0 0 3pt; overflow-wrap: break-word; }
.ph h2.long { font-size: 13pt; }
.ph p { margin: 0; font-size: 10pt; }
.pdoc h3 { font: 700 12pt var(--mono); margin: 14pt 0 6pt; break-after: avoid; }
.ptoc-wrap { max-width: 4.6in; }
.ptoc tr { break-inside: avoid; }
.pwarn { margin: 0 0 10pt; padding: 4pt 0; border-top: 1pt solid #000; border-bottom: 1pt solid #000; font-weight: 700; }
.ptoc-wrap h3 { margin-top: 0; }
.ptoc { width: calc(100% - 2px); margin-left: 1px; border-collapse: collapse; table-layout: fixed; }
.ptoc td { padding: 2pt 0; vertical-align: bottom; border-bottom: 0.5pt dotted #777; overflow-wrap: break-word; }
.ptoc col.c-n { width: 0.6in; }
.ptoc td:last-child { text-align: right; font-family: var(--mono); font-weight: 700; }
.pempty { font-style: italic; }
.pplan, .pmap { width: calc(100% - 2px); margin-left: 1px; border-collapse: collapse; table-layout: fixed; }
.pplan tr.run th, .pmap tr.run th { font: 400 9pt var(--body); border-bottom: 0; padding: 0 5pt 2pt; color: #333; }
.pplan th, .pmap th { text-align: left; font-size: 9.5pt; border-bottom: 1pt solid #000; padding: 3pt 5pt; }
.pplan td, .pmap td { padding: 3pt 5pt; border-bottom: 0.5pt solid #bbb; vertical-align: top; overflow-wrap: break-word; }
.pplan tbody, .pmap tbody { break-inside: avoid; }
.pplan tbody tr:last-child td { border-bottom-color: #555; }
.pplan col.c-p { width: 0.55in; } .pplan col.c-f { width: 0.75in; } .pplan col.c-s { width: 0.55in; }
.pplan td:first-child, .pplan td.fo { font-family: var(--mono); font-weight: 700; }
.pplan tr.blank td:last-child { font-style: italic; }
.pplan .why, .pmap .pf { color: #333; font-size: 9pt; }
.pmap col.c-sh { width: 0.6in; } .pmap col.c-sd { width: 0.6in; }
.pmap tbody { border-bottom: 1pt solid #000; }
.pmap td.sh { font: 700 13pt var(--mono); }
.pmap b { font-family: var(--mono); }
.how { margin: 0 0 10pt; }
.dsheet { page: land; position: relative; margin: 0 auto; width: 10.15in; height: 7.4in; color: #000; break-after: page; display: flex; flex-direction: column; overflow: hidden; }
.dsheet:last-child { break-after: auto; }
.dhead { display: flex; justify-content: space-between; gap: 12pt; font-size: 10pt; border-bottom: 0.75pt solid #000; padding-bottom: 3pt; }
.dhead span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; max-width: 60%; }
.dhow { margin: 4pt 0 0; height: 13pt; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 9.5pt; font-weight: 700; }
.dspread { flex: 1; display: flex; align-items: center; justify-content: center; }
/* One solid outline around the spread and a separate dashed fold line, so every edge is drawn the same way
   (a dashed side on the left page made Chrome drop its top rule at some sizes). */
.dpair { position: relative; display: flex; border: 1pt solid #000; }
.dfold { position: absolute; top: 0; bottom: 0; left: 50%; border-left: 1pt dashed #000; }
.dpage { position: relative; padding: 0.3in 0.3in 0.4in; display: flex; flex-direction: column; overflow: hidden; }
.dmid { flex: 1; display: flex; flex-direction: column; justify-content: center; text-align: center; gap: 4pt; overflow-wrap: break-word; }
.dlab { margin: 0; font-weight: 700; font-size: 1.15em; }
.dtitle { margin: 0; font: 700 1.6em/1.2 var(--mono); }
.dblank { margin: 0; color: #444; font-style: italic; }
.dwhy { margin: 0; font-size: 0.8em; color: #444; }
.dfol { position: absolute; bottom: 0.14in; font: 700 1em var(--mono); }
.dpage.l .dfol { left: 0.22in; }
.dpage.r .dfol { right: 0.22in; }
.dtoc .dh { font: 700 1.1em var(--mono); margin: 0 0 6pt; }
.dtoc .ptoc { font-size: 0.85em; line-height: 1.2; }
.dtoc .ptoc td { padding: 1pt 0; }
.dtoc .ptoc col.c-n { width: 3em; }
.dfoot { display: flex; justify-content: space-between; gap: 12pt; font-size: 9pt; border-top: 0.5pt solid #000; padding-top: 3pt; }

@media print {
  body { background: #fff; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { position: static; visibility: visible; width: auto; }
}
