:root {
  --bg: #f4f1ec;
  --surface: #fffdf9;
  --ink: #1f1b17;
  --muted: #51483f;
  --line: #cdc3b6;
  --accent: #8a3a17;
  --accent-ink: #ffffff;
  --accent-soft: #f2e1d5;
  --err: #a1261d;
  --r: 3px;
  --head: Seravek, "Gill Sans Nova", "Gill Sans", "Gill Sans MT", Ubuntu, Calibri, "Trebuchet MS", sans-serif;
  --body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1714;
    --surface: #24201c;
    --ink: #eee7de;
    --muted: #bfb3a5;
    --line: #4a4139;
    --accent: #eba27a;
    --accent-ink: #1a1714;
    --accent-soft: #3d2a1f;
    --err: #ff9a8f;
  }
}

* { 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); }
main { max-width: 1180px; margin: 0 auto; padding: 0 16px 48px; }
a { color: var(--accent); }
.vh { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -999px; top: 0; }
.skip:focus { left: 16px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 10; }
[hidden] { display: none !important; }

h1, h2, h3, legend { font-family: var(--head); line-height: 1.2; }
h1 { font-size: clamp(1.7rem, 4.4vw, 2.5rem); margin: 26px 0 6px; font-weight: 700; letter-spacing: -0.01em; }
h2 { font-size: 1.4rem; margin: 0 0 12px; font-weight: 700; }
h3 { font-size: 1.1rem; margin: 22px 0 4px; }
.lede { margin: 0 0 18px; max-width: 62ch; color: var(--muted); font-size: 1.06rem; }

/* form basics */
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
legend { font-size: 1.15rem; font-weight: 700; padding: 0; margin-bottom: 8px; float: left; width: 100%; }
fieldset > :not(legend) { clear: both; }
label { font-weight: 600; font-size: 1rem; display: block; margin-bottom: 4px; }
input[type="text"], select {
  font: inherit; font-size: 16px; color: var(--ink); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--r); padding: 9px 10px; min-height: 44px; width: 100%;
}
input[type="color"] {
  width: 100%; min-width: 56px; height: 44px; padding: 3px; border: 1px solid var(--line);
  border-radius: var(--r); background: var(--surface); cursor: pointer;
}
input:focus-visible, select:focus-visible, button:focus-visible, summary:focus-visible, a:focus-visible,
.sw:has(input:focus-visible), .chip:has(input:focus-visible), .seg label:has(input:focus-visible) {
  outline: 3px solid var(--accent); outline-offset: 2px;
}
input[aria-invalid] { border-color: var(--err); border-width: 2px; }
.err { color: var(--err); font-size: 1rem; margin: 4px 0 0; }
.note { color: var(--muted); font-size: 1rem; margin: 8px 0 0; overflow-wrap: anywhere; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; min-height: 44px; margin: 0; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

.btn {
  font: inherit; font-weight: 600; font-size: 1rem; min-height: 44px; padding: 8px 16px; border-radius: var(--r);
  border: 1.5px solid var(--accent); background: transparent; color: var(--accent); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.btn:hover { background: var(--accent-soft); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.quiet { border-color: transparent; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.btn.danger { border-color: var(--err); color: var(--err); }
.btn.small { font-size: 1rem; padding: 6px 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* pattern picker: real little floors */
.swatches { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.sw { position: relative; margin: 0; cursor: pointer; font-weight: 600; font-size: 1rem; }
.sw input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.sw svg { display: block; width: 100%; height: auto; aspect-ratio: 64 / 40; border: 1px solid var(--line); }
@media (min-width: 960px) { .sw svg { aspect-ratio: auto; height: 60px; } }
.sw span { display: block; padding: 4px 0 0; }
.sw:has(input:checked) svg { outline: 3px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.sw:has(input:checked) span { color: var(--accent); }
@media (max-width: 359px) { .swatches:not(.small) { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.sw span { overflow-wrap: anywhere; }
.swatches.small { display: flex; flex-wrap: wrap; gap: 6px 8px; margin-bottom: 12px; }
.chip { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 4px 12px 4px 6px; margin: 0;
  border: 1px solid var(--line); border-radius: 22px; background: var(--surface); cursor: pointer; font-weight: 500; }
.chip input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chip i { width: 28px; height: 28px; border-radius: 50%; border: 1px solid rgb(0 0 0 / 0.25); flex: none; }
.chip:has(input:checked) { border: 2px solid var(--accent); padding: 3px 11px 3px 5px; font-weight: 650; }

.setbar { display: flex; flex-wrap: wrap; gap: 12px 14px; margin-top: 16px; }
.setbar .field { flex: 1 1 140px; }

.group { margin-top: 24px; padding-top: 14px; border-top: 2px solid var(--ink); }
.pair { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px 14px; align-items: start; }
.pair .check { align-self: end; }
.dir-field { max-width: 240px; margin-top: 10px; }
.seg { display: flex; flex-wrap: wrap; border: 1px solid var(--line); border-radius: var(--r); margin: 0 0 12px; width: fit-content; max-width: 100%; }
.seg label { margin: 0; padding: 0 14px; min-height: 44px; display: flex; align-items: center; cursor: pointer; font-weight: 500; white-space: nowrap; }
.seg label + label { border-left: 1px solid var(--line); }
@media (max-width: 379px) {
  .seg { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); width: 100%; }
  .seg label { white-space: normal; text-align: center; justify-content: center; padding: 4px 6px; line-height: 1.25; }
}
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg label:has(input:checked) { background: var(--accent); color: var(--accent-ink); }
.colours { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px 14px; margin-bottom: 4px; }

/* result */
.result { margin-top: 20px; padding: 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); }
.answer p { margin: 0 0 6px; }
.answer .big { font-size: 1.15rem; }
.answer strong { font-size: 1.35rem; font-family: var(--head); white-space: nowrap; }
.answer .fix { color: var(--err); font-weight: 600; }
.preview { margin: 12px 0; }
.preview svg { display: block; width: 100%; height: auto; max-height: 62vh; }
.preview .pg-under { fill: none; stroke: #fff; stroke-width: 3px; vector-effect: non-scaling-stroke; }
.preview .pg { fill: none; stroke: #1f1b17; stroke-width: 1.5px; vector-effect: non-scaling-stroke; stroke-dasharray: 6 4; }
.preview .pg-tag { fill: rgb(255 255 255 / 0.92); stroke: #1f1b17; stroke-width: 1px; vector-effect: non-scaling-stroke; }
.preview .pg-name { fill: #111; font-family: var(--body); font-weight: 700; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.hint { margin: 8px 0 0; color: var(--muted); font-size: 1rem; }

@media (min-width: 960px) {
  .tool { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); column-gap: 36px;
    grid-template-areas: "pick result" "set result" "size result" "part result" "colour result" ". result"; align-items: start; }
  .picker { grid-area: pick; }
  .setbar { grid-area: set; }
  #size-group { grid-area: size; }
  #part-group { grid-area: part; }
  #colour-group { grid-area: colour; }
  .result { grid-area: result; position: sticky; top: 12px; margin-top: 0; }
  .preview svg { max-height: calc(100vh - 330px); }
}

/* pages */
.pages-sec { margin-top: 40px; }
.pages-sum { margin: 0 0 12px; }
.defs-wrap { position: absolute; width: 0; height: 0; overflow: hidden; }
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 18px; margin: 0 0 16px; align-items: start; }
/* the page preview shows the paper margin too (--pad = 10 mm as a share of the paper width) */
.sheet-wrap { background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / 0.25); padding: var(--pad, 0); }
.sheet-wrap svg { display: block; }
@media screen { .sheet-wrap svg { width: 100%; height: auto; } }
@media (min-width: 700px) { .pages { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); } }
.print-msg { display: none; }
.page-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.ask { margin-top: 12px; padding: 12px 14px; border: 1.5px solid var(--err); border-radius: var(--r); background: var(--surface); }
.ask p { margin: 0 0 10px; }
.ask .btn { margin: 0 8px 4px 0; }
.earlier { margin-top: 12px; }
.earlier summary { cursor: pointer; font-weight: 650; min-height: 44px; display: flex; align-items: center; color: var(--accent); }
.earlier ul { 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: 8px 0; border-top: 1px solid var(--line); }
.earlier li span { overflow-wrap: anywhere; }

.undo {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 20;
  background: var(--ink); color: var(--bg); padding: 8px 8px 8px 16px; border-radius: var(--r);
  display: flex; align-items: center; gap: 12px; max-width: calc(100% - 32px); box-shadow: 0 2px 8px rgb(0 0 0 / 0.3);
}
.undo .btn { color: var(--bg); border-color: var(--bg); }
.undo .btn:hover { background: transparent; }

/* content */
.content { margin-top: 48px; max-width: 70ch; }
.content section { margin-bottom: 32px; }
.content ol { padding-left: 1.3em; }
.content li { margin-bottom: 6px; }
.faq p { margin: 0 0 8px; }

/* footer */
.foot { border-top: 1px solid var(--line); margin-top: 24px; padding: 20px 16px 32px; color: var(--muted); font-size: 0.95rem; }
.foot .wrap { max-width: 1180px; margin: 0 auto; }
.sb-footer p { margin: 0 0 10px; }
.sb-more { line-height: 1.9; }
.sb-footer a { color: var(--accent); }
.sb-credit { color: var(--ink); }

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; } }

/* print: only the pages, at exact size */
@media print {
  body { background: #fff; color: #000; }
  main > *:not(.pages-sec), .foot, .undo, .skip, .pages-sec > *:not(.pages):not(.defs-wrap) { display: none !important; }
  main { padding: 0; max-width: none; }
  .pages-sec { margin: 0; }
  .pages { display: block; margin: 0; }
  .sheet-wrap { box-shadow: none; padding: 0; break-after: page; page-break-after: always; break-inside: avoid; }
  .sheet-wrap:last-child { break-after: auto; page-break-after: auto; }
  .print-msg { display: block; font: 12pt/1.5 Helvetica, Arial, sans-serif; color: #000; }
  .print-msg p { margin: 0 0 6pt; }
  .print-msg-h { font-weight: 700; font-size: 14pt; }
  .sheet-wrap svg { print-color-adjust: exact; -webkit-print-color-adjust: exact; }
}
