/* ── Page geometry ──────────────────────────────────────────────────────────
   The preview sheet is rendered at true physical size (CSS in/mm) and then
   scaled down with a transform to fit the viewport, so what you see is what
   prints. The physical @page rule lives in page-letter.css / page-a4.css,
   which are swapped by toggling link.disabled.                             */

:root {
  --page-w: 8.5in;
  --page-h: 11in;
  --page-margin: 0.5in;

  --bg: #f3f2ef;
  --panel: #ffffff;
  --ink: #16171a;
  --ink-soft: #5c6068;
  --ink-faint: #8a8f98;
  --line: #dcdad5;
  --line-soft: #e9e7e3;
  --accent: #1f6feb;
  --radius: 8px;
}

[data-paper="a4"] {
  --page-w: 210mm;
  --page-h: 297mm;
  --page-margin: 12.7mm;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181b;
    --panel: #1f2126;
    --ink: #e8e8ea;
    --ink-soft: #a2a7b0;
    --ink-faint: #767b85;
    --line: #32353c;
    --line-soft: #292c32;
    --accent: #5b9cf8;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 ui-sans-serif, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  /* App shell: the window is the frame, and each pane scrolls inside it.
     Without a bounded height the preview would grow to the sheet's full
     1056px and the fit-to-viewport zoom would never engage. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ── Top bar ─────────────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.topbar h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.topbar-actions { display: flex; align-items: center; gap: 14px; }

.fit { font-size: 12px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.fit[data-state="over"] { color: #c0392b; font-weight: 600; }
.fit[data-state="tight"] { color: #a8700d; }

@media (prefers-color-scheme: dark) {
  .fit[data-state="over"] { color: #ff8a7a; }
  .fit[data-state="tight"] { color: #e0a74a; }
}

button.offer {
  padding: 5px 11px;
  font-size: 12px;
  border-color: var(--accent);
  color: var(--accent);
  background: transparent;
}

button.offer:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }

button {
  font: inherit;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  padding: 7px 14px;
  cursor: pointer;
}

button:hover { border-color: var(--ink-faint); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button.primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
  font-weight: 600;
}

button.primary:hover { filter: brightness(1.08); }
button.quiet { color: var(--ink-soft); padding: 6px 12px; font-size: 13px; }

/* ── Shared-link notice ──────────────────────────────────────────────────── */

.notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 9px 20px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--accent) 10%, var(--panel));
  font-size: 13px;
}

.notice-actions { display: flex; gap: 8px; flex: none; }
.notice button { padding: 5px 11px; font-size: 12px; }

/* ── Layout ──────────────────────────────────────────────────────────────── */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  min-height: 0;
  overflow: hidden;
}

.editor {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 18px;
  min-width: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
  overflow-y: auto;
}

.field { display: flex; flex-direction: column; gap: 5px; }
.field.grow { flex: 1; min-height: 240px; }

.field label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.hint { font-weight: 400; text-transform: none; letter-spacing: 0; color: var(--ink-faint); }

input[type="text"], textarea, select {
  font: inherit;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  width: 100%;
}

textarea {
  flex: 1;
  resize: vertical;
  min-height: 220px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.6;
  tab-size: 2;
}

input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
}

.syntax { font-size: 12px; color: var(--ink-soft); }
.syntax summary { cursor: pointer; user-select: none; }
.syntax dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 10px 0 0; }
.syntax dt { white-space: nowrap; }
.syntax dd { margin: 0; color: var(--ink-faint); }
.syntax code { background: var(--bg); border: 1px solid var(--line-soft); border-radius: 4px; padding: 1px 4px; font-size: 11px; }

.options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 16px;
  padding-top: 12px;
  border-top: 1px solid var(--line-soft);
}

.field.inline { flex: 0 0 auto; }
.field.inline select { width: auto; padding: 6px 8px; }
.field.inline.check { flex-direction: row; align-items: center; gap: 6px; }
.field.inline.check label { text-transform: none; letter-spacing: 0; font-size: 13px; font-weight: 400; color: var(--ink); }
.field.inline.check input { width: auto; accent-color: var(--accent); }
.options button { margin-left: auto; }

/* ── Preview ─────────────────────────────────────────────────────────────── */

.preview {
  min-width: 0;
  min-height: 0;
  overflow: auto;
  padding: 28px;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* The sheet keeps its true 8.5in layout box and is only visually shrunk by a
   transform, so without this clip that full-size box would widen the document
   on narrow screens. The scaled sheet lands exactly inside these bounds. */
.sheet-scaler {
  width: calc(var(--page-w) * var(--zoom, 1));
  height: var(--stack-h, calc(var(--page-h) * var(--zoom, 1)));
  overflow: hidden;
  flex: none;
}

/* Every printed page is one .sheet; the stack is scaled as a unit. */
.sheets {
  display: flex;
  flex-direction: column;
  gap: 22px;
  transform: scale(var(--zoom, 1));
  transform-origin: top left;
  width: var(--page-w);
}

/* Trial layouts are measured here, out of sight and out of flow. */
.sheet.probe {
  position: absolute;
  top: 0;
  left: 0;
  visibility: hidden;
  pointer-events: none;
}

.sheet {
  width: var(--page-w);
  height: var(--page-h);
  padding: var(--page-margin);
  background: #fff;
  color: #000;
  box-shadow: 0 1px 2px rgba(0,0,0,.12), 0 8px 28px rgba(0,0,0,.14);
  flex: none;
}

.sheet-inner {
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-size: calc(10pt * var(--fit, 1));
  line-height: 1.35;
  font-family: ui-sans-serif, -apple-system, "Helvetica Neue", Helvetica, Arial, sans-serif;
}

/* ── The printed document ────────────────────────────────────────────────── */

.doc-head {
  flex: none;
  margin: 0 0 1.1em;
  padding-bottom: 0.5em;
  border-bottom: 2px solid #000;
}

.doc-head h2 {
  margin: 0;
  font-size: 2em;
  font-weight: 700;
  letter-spacing: -0.015em;
  line-height: 1.1;
}

.doc-head p {
  margin: 0.35em 0 0;
  font-size: 0.95em;
  color: #4a4a4a;
}

.doc-head p:empty, .doc-head h2:empty { display: none; }

/* Pages after the first carry a compact running head instead of the title. */
.doc-head.running {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 1em;
  margin-bottom: 0.9em;
  padding-bottom: 0.35em;
  border-bottom: 1px solid #000;
  font-size: 0.9em;
}

.running-title { font-weight: 650; }
.running-page { color: #4a4a4a; font-variant-numeric: tabular-nums; }

.doc-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  columns: var(--cols, 1);
  column-gap: 2.2em;
}

/* Probe state: drop the flex height so the browser reports the natural
   balanced height of the columns, which is what fitToPage() compares. */
.doc-body.measuring {
  flex: none;
  height: auto;
  min-height: 0;
  overflow: visible;
}

/* Sections must be breakable or a long one would fill column 1 and leave the
   rest of the page empty. Orphan control is handled per-element instead:
   a heading never ends a column, and a row never splits across one. */
.group { margin: 0 0 1em; }
.group:first-child > .group-title { margin-top: 0; }

.group-title {
  margin: 0 0 0.45em;
  font-size: 0.78em;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #000;
  border-bottom: 1px solid #aaa;
  padding-bottom: 0.25em;
  break-after: avoid;
  break-inside: avoid;
}

.row {
  display: flex;
  align-items: baseline;
  gap: 0.55em;
  padding: 0.3em 0;
  break-inside: avoid;
}

[data-rules="on"] .row { border-bottom: 1px dotted #c2c2c2; }
[data-rules="on"] .group .row:last-child { border-bottom: none; }

/* One box per unit, right-aligned inside a gutter shared by the whole list
   (--gutter, set by render) so item names line up in a single column whatever
   each row's quantity is. min-width, not width: a row with more boxes than the
   gutter reserves grows instead of overflowing. */
.boxes {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.22em;
  /* Align to the list's widest box run, but never spend more than a third of
     the row on it: past that the reserved space costs more in name width than
     the alignment is worth, and long names start breaking mid-word. */
  min-width: min(var(--gutter, 0.85em), 40%);
  align-self: center;
}

.box {
  flex: none;
  width: 0.85em;
  height: 0.85em;
  border: 1px solid #444;
  border-radius: 1.5px;
}

[data-boxes="off"] .boxes { display: none; }

.name { flex: 1; min-width: 0; overflow-wrap: break-word; }

.qty {
  flex: none;
  font-variant-numeric: tabular-nums;
  font-weight: 650;
  font-size: 0.92em;
  color: #000;
}

.note {
  display: block;
  font-size: 0.82em;
  font-style: italic;
  color: #666;
  line-height: 1.25;
}

.empty {
  color: #999;
  font-style: italic;
  font-size: 1em;
}

/* ── Footer ──────────────────────────────────────────────────────────────── */

.foot {
  padding: 10px 20px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  font-size: 12px;
  color: var(--ink-faint);
}

.foot p { margin: 0; }

/* ── Narrow screens ──────────────────────────────────────────────────────── */

@media (max-width: 820px) {
  /* Stacked: let the whole page scroll instead of each pane. */
  body { height: auto; min-height: 100vh; overflow: visible; }
  .layout { grid-template-columns: 1fr; overflow: visible; }
  .editor { border-right: none; border-bottom: 1px solid var(--line); overflow: visible; }
  .preview { padding: 18px; overflow: visible; }
  .topbar { position: sticky; top: 0; z-index: 2; }
}

/* ── Print ───────────────────────────────────────────────────────────────── */

@media print {
  .topbar, .editor, .foot, .notice { display: none !important; }

  body { background: #fff; display: block; height: auto; min-height: 0; overflow: visible; }
  .layout { display: block; overflow: visible; }
  .preview { display: block; padding: 0; overflow: visible; min-height: 0; }

  /* @page already supplies the margin, so the sheet is just the live area. */
  .sheet-scaler { width: auto; height: auto; overflow: visible; }

  .sheets {
    display: block;
    width: auto;
    gap: 0;
    transform: none;
  }

  .sheet {
    width: auto;
    height: calc(var(--page-h) - 2 * var(--page-margin));
    padding: 0;
    box-shadow: none;
  }

  /* One sheet per sheet of paper. */
  .sheet + .sheet { break-before: page; }
  .sheet.probe { display: none; }
}
