/* Standard costing practice: a cost accountant's working sheet. Cream ledger paper, brown-black ink, one burnt
   sienna accent; book serif headings like a study text, every figure in tabular numerals, rows ruled like a
   variance statement. */
:root {
  --bg: #f6f3ea; --paper: #fffdf8; --ink: #221c17; --muted: #54493f; --line: #ddd4c4; --line2: #8a7d6e;
  --accent: #9c3d17; --accent-ink: #ffffff; --accent-soft: #f5e3d8;
  --ok: #1d6436; --ok-soft: #e1efe3; --bad: #a3231a; --bad-soft: #fae3df; --warn: #6f4e00; --warn-soft: #f6ebcc;
  --head: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --r: 3px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1715; --paper: #24201c; --ink: #efe7dc; --muted: #c2b5a5; --line: #3d352e; --line2: #7d7064;
    --accent: #f0956b; --accent-ink: #2a1206; --accent-soft: #3c2418;
    --ok: #8fd6a2; --ok-soft: #173020; --bad: #ff9b8f; --bad-soft: #3d1f1b; --warn: #efc46a; --warn-soft: #382e15;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 17px/1.55 var(--sans); }
.wrap { max-width: 1100px; margin: 0 auto; padding: 0 16px; }
a { color: var(--accent); }
.skip { position: absolute; left: -999px; top: 8px; background: var(--paper); padding: 8px 12px; z-index: 9; }
.skip:focus { left: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
h1, h2, h3 { font-family: var(--head); line-height: 1.15; font-weight: 700; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* hero */
.hero { padding: 22px 0 4px; }
.hero h1 { font-size: clamp(1.8rem, 5vw, 2.6rem); margin: 0 0 8px; }
.lede { margin: 4px 0 0; max-width: 64ch; color: var(--muted); }

/* setup */
.setup { margin: 14px 0 10px; display: grid; gap: 10px; }
.setup fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
.setup legend { font-weight: 700; margin-bottom: 4px; padding: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.chip span { display: flex; align-items: center; min-height: 44px; padding: 6px 14px; border: 1px solid var(--line2); background: var(--paper); border-radius: 22px; white-space: nowrap; }
.chip input:checked + span { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.chip input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
.setrow { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; }
.inline { display: inline-flex; gap: 8px; align-items: center; font-weight: 600; }
select, input { font: inherit; font-size: 16px; color: var(--ink); background: var(--paper); border: 1px solid var(--line2); border-radius: var(--r); }
select { min-height: 44px; padding: 6px 10px; max-width: 100%; }
.btn { font: inherit; min-height: 44px; padding: 8px 16px; border-radius: var(--r); border: 1px solid var(--ink); background: var(--paper); color: var(--ink); cursor: pointer; font-weight: 600; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.small { padding: 6px 14px; }
.btn:hover { filter: brightness(0.96); }
.link { font: inherit; background: none; border: 0; color: var(--accent); text-decoration: underline; cursor: pointer; padding: 10px 4px; min-height: 44px; }
.note { margin: 8px 0; padding: 10px 12px; background: var(--warn-soft); border-radius: var(--r); }
#store-note { overflow-wrap: anywhere; }
.note.info { background: var(--paper); border: 1px solid var(--line); }
@media (max-width: 600px) {
  .hero { padding-top: 12px; }
  .setup { gap: 6px; margin-top: 8px; }
  .chips { gap: 4px; }
  #presets { display: grid; grid-template-columns: 1fr 1fr; }
  /* five topics in two rows: three, then the long one beside Mixed */
  #levels { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); }
  #levels .chip { grid-column: span 2; }
  #levels .chip:nth-child(4) { grid-column: span 4; }
  .chip span { padding: 6px 10px; white-space: normal; line-height: 1.2; border-radius: var(--r); }
  .sheet { padding: 12px 10px 14px; }
}
@media (max-width: 359px) {
  #levels { grid-template-columns: 1fr 1fr; }
  #levels .chip, #levels .chip:nth-child(4) { grid-column: auto; }
}

/* the sheet */
.sheet { background: var(--paper); border: 1px solid var(--line); border-radius: var(--r); padding: 14px 20px 16px; margin: 10px 0 18px; box-shadow: 0 1px 0 var(--line); }
.sheet:focus { outline: none; }
.sheet-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 16px; border-bottom: 3px double var(--ink); padding-bottom: 6px; }
.sheet-head h2 { margin: 0; font-size: 1.35rem; }
.code { font-family: var(--mono); letter-spacing: 0.08em; color: var(--accent); }
.lvname { font-family: var(--sans); font-size: 1rem; font-weight: 400; color: var(--muted); }
.score { margin: 0; color: var(--muted); }
.rules { margin: 6px 0 2px; }
.rules summary, .work summary, .recent summary, .faq summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; gap: 8px; font-weight: 600; list-style: none; }
.rules summary::-webkit-details-marker, .work summary::-webkit-details-marker, .recent summary::-webkit-details-marker, .faq summary::-webkit-details-marker { display: none; }
.rules summary::before, .work summary::before, .recent summary::before, .faq summary::before { content: ""; flex: none; width: 0; height: 0; border-style: solid; border-width: 5px 0 5px 8px; border-color: transparent transparent transparent currentColor; }
.rules[open] > summary::before, .work[open] > summary::before, .recent[open] > summary::before, .faq[open] > summary::before { transform: rotate(90deg); }
.rules ul { margin: 4px 0 8px; padding-left: 20px; max-width: 82ch; }
.rules li { margin: 0 0 4px; }
.qs { list-style: none; margin: 0; padding: 0; }
.q { padding: 16px 0 6px; border-top: 1px solid var(--line2); margin-top: 12px; }
.q:first-child { margin-top: 6px; border-top: 0; }
.qhead { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 2px 16px; margin: 0 0 6px; }
.qhead .see { font-weight: 600; }
.q h3 { margin: 0; font-size: 1.3rem; display: flex; gap: 10px; align-items: baseline; }
.qn { font-family: var(--mono); color: var(--accent-ink); background: var(--accent); border-radius: 50%; min-width: 1.6em; height: 1.6em; display: inline-flex; align-items: center; justify-content: center; font-size: 0.95rem; }
.scen { margin: 0 0 12px; max-width: 75ch; }

/* tables: ruled like a printed statement */
.twrap { max-width: 100%; margin: 0 0 12px; }
table.dt { border-collapse: collapse; font-variant-numeric: tabular-nums; border-top: 2px solid var(--ink); border-bottom: 2px solid var(--ink); }
.dt caption { text-align: left; font-family: var(--head); font-weight: 700; font-size: 1.05rem; padding: 0 0 4px; }
.dt th, .dt td { padding: 6px 10px; text-align: left; vertical-align: top; font-weight: 400; border-bottom: 1px solid var(--line); }
.dt thead th { font-weight: 700; border-bottom: 1px solid var(--ink); vertical-align: bottom; }
.dt .num { text-align: right; font-family: var(--mono); }
.dt thead th.num { font-family: var(--sans); }
.dt tbody th[scope="row"] { font-weight: 600; }
.dt tr.tot th, .dt tr.tot td { border-top: 1px solid var(--ink); font-weight: 700; }
.tpair { display: flex; flex-wrap: wrap; gap: 0 28px; align-items: flex-start; }
.stmt tr.st-total th, .stmt tr.st-total td { font-weight: 700; border-top: 1px solid var(--ink); }
.stmt tr.st-sub th, .stmt tr.st-sub td { font-style: italic; }
input.cell { min-height: 44px; padding: 6px 8px; text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; max-width: 100%; }
input.cell { width: calc(var(--w, 12) * 1ch + 18px); }
/* a box holds the set's longest key written the long way; anything longer typed in it widens it, up to the row */
@supports (field-sizing: content) { input.cell:not([type=radio]) { field-sizing: content; width: auto; min-width: calc(var(--w, 12) * 1ch + 18px); } }
.cell.is-right { border-color: var(--ok); background: var(--ok-soft); box-shadow: inset 0 0 0 1px var(--ok); }
.cell.is-wrong, .cell.is-unread { border-color: var(--bad); background: var(--bad-soft); box-shadow: inset 0 0 0 1px var(--bad); }
.cell.is-close { border-color: var(--warn); background: var(--warn-soft); box-shadow: inset 0 0 0 1px var(--warn); }
.cell.is-own { border-color: var(--warn); border-style: dashed; box-shadow: inset 0 0 0 1px var(--warn); }

/* answers: a variance is an amount box and an F/A pair */
.agroup { margin: 8px 0 4px; }
.agh { margin: 6px 0 2px; font-family: var(--head); font-weight: 700; font-size: 1.1rem; }
.answers { display: grid; gap: 0; max-width: 900px; }
.arow { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 4px 16px; padding: 5px 0; border-bottom: 1px dotted var(--line2); }
.alab { font-weight: 600; }
.aval { display: inline-flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px 10px; min-width: 0; max-width: 100%; }
@media (max-width: 640px) { .arow { grid-template-columns: minmax(0, 1fr); } .aval { justify-content: flex-start; gap: 6px; } }
.cwfa { display: inline-flex; align-items: center; gap: 4px; }
.fas { display: inline-flex; gap: 0; border: 1px solid var(--line2); border-radius: var(--r); overflow: hidden; }
.fa { position: relative; display: flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; cursor: pointer; font-family: var(--mono); font-weight: 700; background: var(--paper); }
.fa + .fa { border-left: 1px solid var(--line2); }
.fa input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.fa:has(input:checked) { background: var(--ink); color: var(--paper); }
.fa:has(input:focus-visible) { outline: 3px solid var(--accent); outline-offset: -3px; }
.fas.is-right .fa:has(input:checked) { background: var(--ok); color: var(--paper); }
.fas.is-wrong .fa:has(input:checked), .fas.is-unread .fa:has(input:checked) { background: var(--bad); color: var(--paper); }
.fas.is-own .fa:has(input:checked) { background: var(--warn); color: var(--paper); }
.kv { font-family: var(--mono); font-weight: 700; }

/* tables that do not fit (fit.js measures them; screen only): each row becomes a block, each value labelled with its column */
@media screen {
  .twrap.rows { width: 100%; flex: 1 1 100%; }
  .twrap.rows table, .twrap.rows tbody { display: block; width: 100%; }
  .twrap.rows table.dt { border-bottom: 0; }
  .twrap.rows caption { display: block; }
  .twrap.rows thead { display: none; }
  .twrap.rows tr { display: grid; grid-template-columns: repeat(auto-fill, minmax(11em, 1fr)); gap: 4px 16px; padding: 8px 0; border-bottom: 1px solid var(--line2); }
  .twrap.rows tbody th[scope="row"] { grid-column: 1 / -1; }
  .twrap.rows th, .twrap.rows td { border: 0; padding: 0; display: flex; justify-content: space-between; align-items: center; gap: 10px; min-width: 0; }
  .twrap.rows th { justify-content: flex-start; font-weight: 700; color: var(--ink); }
  .twrap.rows td::before { content: attr(data-l); font: 600 1rem var(--sans); color: var(--muted); }
  .twrap.rows td:empty { display: none; }
}
@media screen and (max-width: 560px) {
  .content .twrap { width: 100%; }
  .content .twrap table, .content .twrap tbody { display: block; width: 100%; }
  .content .twrap table.dt { border-bottom: 0; }
  .content .twrap caption { display: block; }
  .content .twrap thead { display: none; }
  .content .twrap tr { display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; padding: 8px 0; border-bottom: 1px solid var(--line2); }
  .content .twrap th, .content .twrap td { border: 0; padding: 0; display: flex; justify-content: space-between; gap: 10px; min-width: 0; }
  .content .twrap td::before { content: attr(data-l); font: 600 1rem var(--sans); color: var(--muted); flex: none; }
}

/* feedback */
.fbwrap { margin: 6px 0; }
.fbl { margin: 4px 0; padding: 0 0 0 2.2em; max-width: 80ch; }
.fb { margin: 4px 0; padding: 6px 10px; border-radius: var(--r); background: var(--bad-soft); max-width: 80ch; }
.fbl .fb::marker { font-weight: 700; }
.fb.right { background: var(--ok-soft); }
.fb.close { background: var(--warn-soft); }
.fb.own { background: var(--paper); border: 1px dashed var(--warn); }
.fb.empty { background: var(--paper); border: 1px solid var(--line); }
.cw { display: inline-flex; align-items: center; gap: 4px; max-width: 100%; min-width: 0; }
.cw > .cell { flex: none; }
.mk { flex: none; min-width: 1.6em; font-weight: 700; font-size: 1.1rem; line-height: 1; white-space: nowrap; }
.mn { font-size: 1rem; font-family: var(--mono); }
.mk-right { color: var(--ok); }
.mk-close, .mk-own { color: var(--warn); }
.mk-wrong, .mk-unread { color: var(--bad); }
.qfoot { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin: 6px 0; }
.work { margin: 4px 0 8px; }
.keyfig { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.kpart { min-width: 0; }
.kh { margin: 8px 0 2px; font-weight: 700; }
.kq { display: none; }
.steps { margin: 4px 0 8px; padding-left: 2.6em; font-family: var(--mono); font-size: 1rem; max-width: 90ch; }
.steps li { margin: 4px 0; overflow-wrap: anywhere; }

.sheet-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin-top: 10px; padding-top: 10px; border-top: 3px double var(--ink); }
.confirm { flex-basis: 100%; margin: 0; padding: 10px; background: var(--warn-soft); border-radius: var(--r); }
.save-hint { flex-basis: 100%; margin: 0; color: var(--muted); }

/* print and share */
.share { margin: 0 0 24px; }
.print-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.print-row label { display: inline-flex; gap: 8px; align-items: center; font-weight: 600; flex-wrap: wrap; }
.share-out label { display: grid; gap: 4px; font-weight: 600; }
.share-out input { width: 100%; min-height: 44px; padding: 6px 10px; font-family: var(--mono); }
.recent { margin-top: 12px; background: var(--paper); border: 1px solid var(--line); border-radius: var(--r); padding: 4px 12px; }
.recent ul { list-style: none; margin: 0; padding: 0; }
.recent li { display: flex; justify-content: space-between; gap: 10px; align-items: center; border-top: 1px solid var(--line); padding: 6px 0; }
.recent .when { display: block; color: var(--muted); }
.hint { color: var(--muted); }

/* content */
.content { max-width: 78ch; padding-bottom: 20px; }
.content h2 { font-size: 1.5rem; margin: 28px 0 8px; }
.content .dt { margin: 6px 0 10px; }
.faq details { border-bottom: 1px solid var(--line); }
.faq details p { margin: 0 0 12px; }
.sources { color: var(--muted); overflow-wrap: anywhere; }
footer { border-top: 3px double var(--ink); padding: 18px 0 30px; color: var(--muted); }
.indep { margin: 0 0 10px; }
.sb-footer p { margin: 6px 0; }
.sb-more { line-height: 1.8; }
.sb-more a { color: var(--muted); }
.sb-credit a { color: var(--accent); }

.toast { position: fixed; left: 50%; bottom: 16px; transform: translate(-50%, 140%); background: var(--ink); color: var(--paper); padding: 10px 16px; border-radius: var(--r); max-width: min(92vw, 560px); z-index: 20; pointer-events: none; transition: transform 0.2s; }
.toast.on { transform: translate(-50%, 0); }
.toast.top { top: 16px; bottom: auto; transform: translate(-50%, -140%); }
.toast.top.on { transform: translate(-50%, 0); }
@media (max-width: 600px) {
  .toast { left: 8px; right: 8px; bottom: 8px; max-width: none; text-align: center; transform: translateY(140%); }
  .toast.top { top: 8px; bottom: auto; transform: translateY(-140%); }
  .toast.on, .toast.top.on { transform: none; }
}
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }
[hidden] { display: none !important; }
.print-root { display: none; }

/* ---------- print ---------- */
@page { margin: 12mm; }
@media print {
  :root { --ink: #000; --muted: #333; --line: #999; --line2: #555; --paper: #fff; --bg: #fff; --accent: #000; }
  body { background: #fff; color: #000; font: 10.5pt/1.35 var(--sans); }
  main, footer, .skip, .toast { display: none !important; }
  .print-root { display: block; }
  .psheet, .pkey { break-after: page; }
  .pkey:last-child, .psheet:last-child { break-after: auto; }
  table.pt { width: 100%; border-collapse: collapse; }
  .pt > thead .prun { font-size: 10pt; color: #333; padding-bottom: 4px; border-bottom: 0.5pt solid #888; }
  .ph { display: flex; justify-content: space-between; align-items: baseline; gap: 6pt 12pt; flex-wrap: wrap; margin-bottom: 4pt; }
  .ph h2 { margin: 4px 0 0; font-size: 15pt; }
  .pmeta { display: flex; gap: 14pt; margin: 0; }
  .prules { margin: 3px 0 4px; padding-left: 14pt; font-size: 9.5pt; line-height: 1.28; }
  .prules li { margin: 0 0 2px; break-inside: avoid; }
  .pq, .pk { list-style: none; margin: 0; padding: 0; }
  .pq > li, .pk > li { margin: 0 0 8pt; padding-top: 5pt; border-top: 1.2pt solid #000; break-inside: avoid; }
  .pk > li:last-child, .pq > li:last-child { margin-bottom: 0; }
  .pq h3, .pk h3 { font-size: 12pt; margin: 0 0 3pt; break-after: avoid; }
  .scen { margin: 0 0 5pt; max-width: none; }
  .twrap { break-inside: avoid; margin: 0 0 5pt; }
  /* collapsed borders: inset so both outer rules sit inside the A4 content box */
  table.dt { font-size: 10pt; border-top: 1.2pt solid #000; border-bottom: 1.2pt solid #000; margin: 0 1pt; }
  .dt caption { font-size: 10pt; padding-bottom: 2pt; }
  .dt th, .dt td { padding: 1.5pt 7pt; border-bottom: 0.5pt solid #999; }
  .dt thead th { border-bottom: 0.75pt solid #000; }
  .tpair { gap: 0 10pt; flex-wrap: nowrap; }
  .tpair .twrap { flex: 0 1 auto; min-width: 0; }
  .tpair .dt th, .tpair .dt td { padding: 1.5pt 4pt; }
  .tpair .dt td { white-space: nowrap; }
  .pblank { display: inline-block; border: 0.75pt solid #000; height: 17pt; vertical-align: middle; }
  .pfa { font-family: var(--mono); font-weight: 700; margin-left: 6pt; }
  .answers { max-width: none; display: grid; grid-template-columns: 1fr 1fr; column-gap: 18pt; }
  .arow { display: flex; justify-content: space-between; align-items: center; border-bottom: 0; padding: 1.5pt 0; gap: 8pt; }
  .agh { font-size: 10.5pt; margin: 3pt 0 0; }
  .agroup { margin: 2pt 0; break-inside: avoid; }
  /* the reconciliation's boxes name themselves (profit, sales, cost), so paper drops the group headings to fit page 1 */
  .pq > li.k-recon .agh { display: none; }
  .keyfig { display: block; }
  .kpart + .kpart { margin-top: 3pt; }
  .kh { margin: 2pt 0 1pt; font-size: 10pt; }
  .steps { font-size: 10pt; margin: 0; padding-left: 22pt; max-width: none; }
  .steps li { margin: 0.5pt 0; break-inside: avoid; }
  .kq { display: inline; }
  /* the key on paper: the working lines carry every value; a reconciliation also prints its statement */
  .pkey .kans:not(:has(.stmt)) { display: none; }
  .kpart .answers .arow { min-height: 0; }
  /* page breaks are set by app.js (pageStarts), the same in every copy of a class set */
  .pq > li.pbreak, .pk > li.pbreak { break-before: page; }
  * { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
}
