:root {
  --bg: #fdfcfa;
  --paper: #ffffff;
  --linen: #f4f1ec;
  --ink: #1f1c1a;
  --muted: #5e5650;
  --line: #e2ddd6;
  --rule: #1f1c1a;
  --accent: #b03a22;
  --accent-ink: #ffffff;
  --accent-soft: #f7e3dc;
  --focus: #1d64a8;
  --warn-bg: #fdf3d6;
  --warn-line: #c8951a;
  --note-bg: #eaf2ed;
  --note-line: #4f8a70;
  --err-bg: #fbe4de;
  --fabric: #faf8f4;
  --selvage: #e9e3d9;
  --t1: #f2b8a6; --t2: #f4d58d; --t3: #b9d7c4; --t4: #bccbee; --t5: #d8c4ea; --t6: #f3c1d2;
  --tr: #f7e6bd; --tw: #d9cbb6;
  --head: "Avenir Next", Avenir, Futura, "Century Gothic", "Gill Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --r-ctl: 6px;
  --r-card: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181614; --paper: #211e1b; --linen: #2a2623; --ink: #f0ebe4; --muted: #b9b0a6; --line: #3b3632; --rule: #f0ebe4;
    --accent: #f08a6c; --accent-ink: #181614; --accent-soft: #3d2620; --focus: #8cc4ff;
    --warn-bg: #3a3016; --warn-line: #c9a13a; --note-bg: #1f3129; --note-line: #7fb39b; --err-bg: #45231d;
    --fabric: #26221f; --selvage: #3a332d;
    --t1: #7b4436; --t2: #75602a; --t3: #3f6450; --t4: #3f4f7a; --t5: #5b4775; --t6: #7a4458;
    --tr: #6b5a33; --tw: #5a4d40;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 17px/1.55 var(--sans); }
.wrap { max-width: 1120px; margin: 0 auto; padding: 0 16px; }
h1, h2, h3, h4, figcaption { font-family: var(--head); line-height: 1.2; letter-spacing: -0.01em; }
a { color: var(--accent); text-underline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--paper); padding: 8px 12px; z-index: 5; }
.skip:focus { left: 8px; }
.print-only { display: none; }
.mt { margin-top: 10px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* header */
.masthead { padding: 28px 0 22px; border-bottom: 1px solid var(--line); margin-bottom: 24px; }
h1 { margin: 0 0 6px; font-size: clamp(30px, 5vw, 42px); font-weight: 700; display: flex; align-items: center; gap: 10px; }
.mark { flex: none; }
.lede { margin: 0; color: var(--muted); max-width: 60ch; font-size: 18px; }
@media (max-width: 600px) { .masthead { padding: 18px 0 14px; margin-bottom: 8px; } .mark { width: 28px; height: 28px; } .lede { font-size: 16px; } }

/* form: plain sections, no boxes */
#calc { display: grid; gap: 28px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 960px) {
  #calc { grid-template-columns: 360px minmax(0, 1fr); gap: 48px; align-items: start; }
  .form.fits { position: sticky; top: 16px; }
  .content { grid-column: 1 / -1; }
}
.form { display: grid; gap: 0; }
.part { padding: 16px 0 18px; border-bottom: 1px solid var(--line); }
.part:first-child { padding-top: 0; }
.part:last-child { border-bottom: 0; }
.part h2 { margin: 0 0 12px; font-size: 19px; }
.part-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.part-head h2 { margin: 0; }
.field { margin: 0 0 14px; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field > label, .label { display: block; font-weight: 600; font-size: 15px; margin-bottom: 5px; }
.grid3, .grid2 { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); margin-bottom: 10px; }
.grid3 .field, .grid2 .field { margin: 0; }
.inp { display: flex; align-items: stretch; border: 1px solid color-mix(in srgb, var(--ink) 28%, transparent); border-radius: var(--r-ctl); background: var(--paper); overflow: hidden; }
.inp:focus-within { border-color: var(--focus); box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 30%, transparent); }
.inp input { flex: 1; min-width: 0; width: 100%; border: 0; background: transparent; color: var(--ink); font: 600 18px/1.2 var(--sans); padding: 10px 6px 10px 10px; font-variant-numeric: tabular-nums; }
.inp input:focus { outline: none; }
.inp input[aria-invalid=true] { color: var(--accent); }
.inp .u { display: grid; place-items: center; padding: 0 9px; color: var(--muted); font-size: 15px; }
.inp-sm { max-width: 200px; margin-top: 8px; }
select { width: 100%; min-height: 44px; font: 16px var(--sans); padding: 10px; border-radius: var(--r-ctl); border: 1px solid color-mix(in srgb, var(--ink) 28%, transparent); background: var(--paper); color: var(--ink); }
.hint { color: var(--muted); font-size: 15px; margin: 8px 0 0; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label { position: relative; flex: 1 1 auto; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span {
  display: flex; align-items: center; justify-content: center; gap: 4px; min-height: 44px; text-align: center; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: var(--r-ctl); background: var(--linen); font-weight: 600; font-size: 15px; cursor: pointer; white-space: nowrap;
}
.seg span small { font-weight: 500; color: var(--muted); font-size: 14px; }
.seg label:hover span { border-color: color-mix(in srgb, var(--ink) 35%, transparent); }
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.seg input:checked + span small { color: inherit; }
.seg input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }
.seg-num { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); }
.seg-unit { flex-wrap: nowrap; gap: 0; }
.seg-unit label { flex: none; }
.seg-unit span { min-width: 64px; padding: 6px 10px; }
.seg-unit label:first-child span { border-radius: var(--r-ctl) 0 0 var(--r-ctl); }
.seg-unit label:last-child span { border-radius: 0 var(--r-ctl) var(--r-ctl) 0; border-left: 0; }
@media (max-width: 359px) { .seg-num { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.per-tier { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); margin-top: 10px; }
.per-tier .field { margin: 0; }
.check { display: flex; gap: 10px; align-items: center; min-height: 44px; margin: 2px 0; font-weight: 600; font-size: 15px; cursor: pointer; }
.check input { width: 20px; height: 20px; margin: 0; accent-color: var(--accent); flex: none; }
.more { margin-top: 6px; }
.more summary { cursor: pointer; font-weight: 600; color: var(--accent); padding: 10px 0; min-height: 44px; }
.more[open] summary { margin-bottom: 8px; }
.form-actions { margin: 6px 0 0; }
.link-btn { font: 600 15px var(--sans); color: var(--muted); background: none; border: 0; padding: 10px 0; min-height: 44px; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.link-btn:hover { color: var(--accent); }

.btn, .btn-ghost { font: 700 15px var(--sans); border-radius: var(--r-ctl); padding: 9px 18px; min-height: 44px; cursor: pointer; border: 1px solid var(--accent); }
.btn { background: var(--accent); color: var(--accent-ink); }
.btn:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-ghost:hover { background: var(--accent-soft); }

/* results */
.results { display: grid; gap: 16px; min-width: 0; }
#out { display: grid; gap: 32px; transition: opacity .2s; }
#out.stale { opacity: .35; }

/* the one card: pattern-envelope style answer + cut list */
.envelope { background: var(--paper); border: 1px solid var(--line); border-top: 5px solid var(--accent); border-radius: var(--r-card); padding: 20px 22px 18px; box-shadow: 0 1px 3px color-mix(in srgb, var(--ink) 10%, transparent); min-width: 0; }
.env-top { display: grid; gap: 14px; }
@media (min-width: 700px) { .env-top { grid-template-columns: auto minmax(0, 1fr); gap: 28px; align-items: end; } }
.env-k { margin: 0; font-size: 16px; font-weight: 600; color: var(--muted); }
.env-big { margin: 0; font: 700 clamp(44px, 9vw, 60px)/1.02 var(--head); color: var(--accent); letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.env-sub { margin: 4px 0 0; font-size: 16px; }
.env-facts { margin: 0; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 18px; }
@media (min-width: 700px) { .env-facts { grid-template-columns: repeat(4, minmax(0, auto)); justify-content: end; } }
.env-facts dt { font-size: 14px; color: var(--muted); }
.env-facts dd { margin: 0; font-weight: 700; font-size: 18px; font-variant-numeric: tabular-nums; white-space: nowrap; }
#msgs { margin-top: 14px; }
#msgs:empty { display: none; }
.msg { padding: 10px 14px; border-left: 4px solid; font-size: 15px; margin-bottom: 8px; border-radius: 0 var(--r-ctl) var(--r-ctl) 0; }
.msg ul { margin: 4px 0 0; padding-left: 20px; }
.msg-warn { background: var(--warn-bg); border-color: var(--warn-line); }
.msg-note { background: var(--note-bg); border-color: var(--note-line); }
.msg-err { background: var(--err-bg); border-color: var(--accent); }

.cut-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin: 22px 0 4px; border-top: 2px solid var(--rule); padding-top: 12px; }
.cut-head h3 { margin: 0; font-size: 20px; }
.cut-head .link-btn { color: var(--accent); padding: 8px 0; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 16px; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 9px 10px 9px 0; border-bottom: 1px solid var(--line); vertical-align: top; }
thead th { font-size: 14px; font-weight: 700; color: var(--ink); border-bottom: 1.5px solid var(--rule); padding-top: 6px; }
.cut tbody th { white-space: nowrap; font-weight: 700; }
.cut td strong { font-size: 17px; }
.cut tbody tr:last-child th, .cut tbody tr:last-child td { border-bottom: 1.5px solid var(--rule); }
.nw { white-space: nowrap; }
.math { display: none; color: var(--muted); font-size: 14px; margin-top: 2px; }
.cut.show-math .math { display: block; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 2px; border: 1px solid var(--ink); margin-right: 8px; vertical-align: -1px; }
.cut-note { margin: 10px 0 0; font-size: 15px; color: var(--muted); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
@media screen and (max-width: 600px) {
  .envelope { padding: 18px 16px 16px; }
  .cut thead, .gather thead { display: none; }
  .cut tr, .gather tr { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
  .cut tr { grid-template-columns: 0.9fr 1.2fr 1fr; }
  .gather tr { grid-template-columns: 1.3fr 1.3fr 0.8fr; }
  .cut tbody tr:last-child { border-bottom: 1.5px solid var(--rule); }
  .cut th, .cut td, .cut tbody tr:last-child th, .cut tbody tr:last-child td, .gather th, .gather td { border: 0; padding: 0; }
  .cut tbody th, .gather tbody th { grid-column: 1 / -1; font-size: 17px; }
  .cut td::before, .gather td::before { content: attr(data-l); display: block; font-size: 14px; color: var(--muted); }
}

/* sewing notes: plain, no boxes */
.sew { display: grid; gap: 24px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 700px) { .sew { grid-template-columns: minmax(0, 230px) minmax(0, 1fr); gap: 32px; align-items: start; } }
.sew h3 { margin: 0 0 6px; font-size: 19px; }
.sew-notes { display: grid; gap: 22px; min-width: 0; grid-template-columns: minmax(0, 1fr); }
.sew-notes p { margin: 0 0 6px; }
.fig { margin: 0; min-width: 0; }
.fig figcaption { font-size: 19px; font-weight: 700; margin-bottom: 10px; }
.skirt { width: 100%; max-width: 260px; display: block; }
.svg-lab { fill: var(--ink); font-family: var(--sans); }
.big-line { font-size: 18px; }
.gather td, .gather th { font-size: 15px; }
.gather thead th { font-size: 14px; }

/* cutting layout: the one place for dashed cut lines (in the drawing) */
.layout-card { border-top: 1px solid var(--line); padding-top: 22px; }
.layout-body { display: grid; gap: 14px; }
.layout-wrap { min-width: 0; }
.layout { width: 100%; display: block; height: auto; }
.layout-text p { margin: 0 0 6px; }
.rows-h { font-size: 16px; margin: 14px 0 6px; }
.rows { margin: 0; padding: 0; list-style: none; font-size: 15px; }
.rows li { margin-bottom: 6px; }
.rows .rn { font-weight: 600; color: var(--accent); margin-right: 6px; }
.sub-i { color: var(--muted); }
@media screen and (min-width: 700px) {
  .layout-body { grid-template-columns: minmax(0, 340px) minmax(0, 1fr); align-items: start; gap: 24px; }
}

/* phone: jump to results (shown only while scrolling through the form; see app.js) */
.jump {
  position: fixed; left: 50%; bottom: max(12px, env(safe-area-inset-bottom)); z-index: 4; transform: translateX(-50%);
  width: max-content; max-width: calc(100% - 32px); text-align: center; text-decoration: none; white-space: nowrap;
  background: var(--ink); color: var(--bg); padding: 11px 18px; min-height: 44px; border-radius: 999px; font-weight: 600; font-size: 15px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); transition: opacity .2s, transform .2s;
}
.jump.gone { opacity: 0; transform: translate(-50%, 20px); pointer-events: none; visibility: hidden; }
@media (max-width: 959px) { .form { padding-bottom: 40px; } }
@media (min-width: 960px) { .jump { display: none; } }

/* content */
.content { margin: 56px 0 0; padding-top: 8px; border-top: 2px solid var(--rule); }
.content section { max-width: 70ch; padding: 6px 0 14px; }
.content h2 { font-size: 26px; margin: 22px 0 10px; }
.content li { margin-bottom: 6px; }
.formulas { padding-left: 20px; }
.ref td, .ref th { font-size: 15px; }
.aside summary, .faq summary { cursor: pointer; font-weight: 600; padding: 12px 0; min-height: 44px; }
.aside summary { color: var(--accent); }
.aside[open] { padding-bottom: 6px; }
.faq details { border-bottom: 1px solid var(--line); }
.faq details:first-of-type { border-top: 1px solid var(--line); }
.faq details p { margin: 0 0 14px; }
.sources p { color: var(--muted); font-size: 15px; }

.foot { margin-top: 40px; padding: 22px 0 32px; color: var(--muted); font-size: 15px; border-top: 1px solid var(--line); }
.foot p { margin: 6px 0; max-width: 70ch; }
.sb-footer { margin-top: 12px; display: grid; gap: 2px; }
.sb-more a, .sb-credit a { color: var(--accent); font-weight: 600; text-underline-offset: 2px; }
.sb-more { font-size: 15px; }
.sb-privacy, .sb-credit, .foot-note { font-size: 14px; }

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

/* print: a one-page cut sheet */
@media print {
  @page { size: auto; margin: 12mm; }
  :root { --bg: #fff; --ink: #000; --paper: #fff; --linen: #fff; --line: #999; --rule: #000; --muted: #333; --fabric: #fff; --selvage: #e6e6e6; --accent: #000; --accent-ink: #000;
    --t1: #f2b8a6; --t2: #f4d58d; --t3: #b9d7c4; --t4: #bccbee; --t5: #d8c4ea; --t6: #f3c1d2; --tr: #f7e6bd; --tw: #d9cbb6; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { background: #fff; color: #000; font-size: 10pt; line-height: 1.35; }
  .form, .content, .lede, .mark, .skip, .jump, .foot, .skirt-fig, .noprint { display: none !important; }
  .wrap { max-width: none; padding: 0; }
  #calc, .results, #out { display: block; }
  .print-only { display: block; }
  .masthead { padding: 0 0 2pt; margin: 0; border: 0; }
  h1 { font-size: 16pt; margin: 0; }
  .print-spec { margin: 0 0 6pt; font-size: 9pt; color: #333; }
  .envelope { padding: 6pt 10pt; margin: 0 0 6pt; border: 1.5pt solid #000; border-radius: 6pt; box-shadow: none; }
  .env-top { display: flex; align-items: baseline; gap: 14pt; }
  .env-main { display: flex; align-items: baseline; gap: 8pt; flex-wrap: wrap; flex: none; }
  .env-k { font-size: 8pt; }
  .env-big { font-size: 18pt; white-space: nowrap; }
  .env-sub { font-size: 9pt; margin: 0; }
  .env-facts { flex: 1; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 2pt 14pt; }
  .env-facts dt { font-size: 7pt; }
  .env-facts dd { font-size: 10pt; }
  .env-facts div:nth-child(2), .env-facts div:nth-child(3) { display: none; } /* keep tiers + strips to cut */
  #msgs { margin: 4pt 0 0; }
  .msg { padding: 3pt 6pt; margin: 0 0 4pt; font-size: 9pt; background: #fff !important; color: #000; border: 1pt solid #999; border-left-width: 3pt; }
  .cut-head { margin: 4pt 0 0; padding-top: 3pt; border-top-width: 1pt; }
  .cut-head h3, .fig figcaption, .sew h3 { font-size: 12pt; margin: 0 0 3pt; }
  table { font-size: 9.5pt; }
  th, td { padding: 2pt 5pt 2pt 0; }
  thead th { font-size: 7.5pt; }
  .cut td strong { font-size: 10pt; }
  .math, .cut .math { display: inline; font-size: 8pt; color: #333; margin-left: 5pt; white-space: nowrap; }
  .cut-note { font-size: 8.5pt; margin-top: 2pt; }
  .sew { display: block; margin: 0 0 5pt; }
  .sew-notes { display: grid; grid-template-columns: 1fr 1.7fr; gap: 10pt; }
  .sew-notes p { margin: 0 0 2pt; font-size: 9pt; }
  .big-line { font-size: 10.5pt !important; }
  .gather td, .gather th { font-size: 8.5pt; }
  .gather thead th { font-size: 7.5pt; }
  .layout-card { break-inside: avoid; border-top: 1pt solid #999; padding-top: 4pt; }
  .layout-body { grid-template-columns: auto 1fr; align-items: start; gap: 12pt; }
  .layout-text p { font-size: 8.5pt; color: #333; margin: 0 0 2pt; }
  .layout { height: 3in; width: min(3.3in, calc(3in * var(--ar, .5))) !important; max-width: none !important; margin: 0; }
  .rows-h { font-size: 9pt; margin: 4pt 0 2pt; }
  .rows { font-size: 8.5pt; }
  .rows.many { columns: 2; column-gap: 14pt; }
  .rows li { margin-bottom: 1pt; }
  .rows .rn { color: #000; }
  .sub-i { font-size: 8pt; }
  a { color: #000; text-decoration: none; }
}

/* 404 */
.notfound { max-width: 560px; }
.notfound .btn { display: inline-flex; align-items: center; text-decoration: none; }
