/* Tokens, three layers: primitive (raw OKLCH values and scales) -> semantic
   (the names the contract allows components to use) -> component (small
   aliases in app.css built only from semantic names). Light and dark are
   both designed; dark is a lifted charcoal, never near-black. */

@font-face {
  font-family: "DM Serif Display"; font-style: normal; font-weight: 400; font-display: swap;
  src: url("../fonts/dm-serif-display-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* ---------- primitive ---------- */
:root {
  --p-n-0: oklch(1 0 0);
  --p-n-50: oklch(0.985 0.002 270);
  --p-n-100: oklch(0.968 0.002 270);
  --p-n-200: oklch(0.925 0.003 270);
  --p-n-300: oklch(0.87 0.004 270);
  --p-n-500: oklch(0.6 0.006 270);
  --p-n-600: oklch(0.46 0.006 270);
  --p-n-650: oklch(0.51 0.006 270);
  --p-n-900: oklch(0.22 0.004 270);

  --p-c-700: oklch(0.285 0.005 270);
  --p-c-650: oklch(0.325 0.006 270);
  --p-c-600: oklch(0.365 0.006 270);
  --p-c-300: oklch(0.76 0.006 270);
  --p-c-200: oklch(0.7 0.006 270);
  --p-c-100: oklch(0.965 0.002 270);

  --p-blue-600: oklch(0.52 0.19 260);
  --p-blue-300: oklch(0.76 0.12 255);
  --p-red-600: oklch(0.5 0.17 27);
  --p-red-300: oklch(0.78 0.13 25);

  --p-ease-out: cubic-bezier(0.22, 0.9, 0.3, 1);
  --p-ease-overshoot: cubic-bezier(0.34, 1.26, 0.64, 1);
  --p-font-body: "Avenir Next", Avenir, "Segoe UI", system-ui, sans-serif;
  --p-font-display: "DM Serif Display", Georgia, serif;
}

/* ---------- semantic: light ---------- */
:root {
  color-scheme: light dark;

  --color-bg: var(--p-n-50);
  --color-surface: var(--p-n-0);
  --color-surface-raised: var(--p-n-0);
  --color-text: var(--p-n-900);
  --color-text-muted: var(--p-n-600);
  --color-text-subtle: var(--p-n-650);
  --color-ring: var(--p-n-900);
  --color-focus: var(--p-blue-600);
  --color-scrim: oklch(0.968 0.002 270 / 0.6);
  --color-danger: var(--p-red-600);
  --color-field: var(--p-n-100);
  --color-page: oklch(1 0 0);
  --color-hairline: color-mix(in oklab, var(--color-text) 14%, transparent);
  /* field boundary: 3:1 against the page and the field fill (WCAG 1.4.11) */
  --color-field-edge: oklch(0.58 0.006 270);

  --glass-bg: oklch(0.985 0.002 270 / 0.72);
  --glass-blur: 22px;
  --glass-saturate: 180%;

  --shadow-1: 0 1px 2px oklch(0 0 0 / 0.06), 0 8px 28px oklch(0 0 0 / 0.08);
  --shadow-2: 0 2px 4px oklch(0 0 0 / 0.06), 0 14px 40px oklch(0 0 0 / 0.1);
  --shadow-sheet: 0 2px 6px oklch(0 0 0 / 0.05), 0 24px 60px oklch(0 0 0 / 0.1);
}

/* ---------- semantic: dark ---------- */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: var(--p-c-700);
    --color-surface: var(--p-c-650);
    --color-surface-raised: var(--p-c-600);
    --color-text: var(--p-c-100);
    --color-text-muted: var(--p-c-300);
    --color-text-subtle: var(--p-c-200);
    --color-ring: var(--p-c-100);
    --color-focus: var(--p-blue-300);
    --color-scrim: oklch(0.2 0.004 270 / 0.55);
    --color-danger: var(--p-red-300);
    --color-field: var(--p-c-600);
    /* soft paper: pure white glares against charcoal */
    --color-page: oklch(0.945 0.006 85);

    --color-hairline: color-mix(in oklab, var(--color-text) 20%, transparent);
    --color-field-edge: oklch(0.68 0.006 270);

    --glass-bg: oklch(0.285 0.005 270 / 0.7);

    --shadow-1: 0 1px 2px oklch(0 0 0 / 0.3), 0 8px 28px oklch(0 0 0 / 0.28);
    --shadow-2: 0 2px 4px oklch(0 0 0 / 0.3), 0 14px 40px oklch(0 0 0 / 0.34);
    --shadow-sheet: 0 2px 6px oklch(0 0 0 / 0.25), 0 24px 60px oklch(0 0 0 / 0.38);
  }
}

/* ---------- semantic: shape, space, type, motion, size (mode independent) ---------- */
:root {
  --radius-s: 10px;
  --radius-m: 16px;
  --radius-l: 28px;
  --radius-full: 999px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --space-8: 72px;
  --space-9: 112px;

  --font-body: var(--p-font-body);
  --font-display: var(--p-font-display);
  --text-xs: 12px;
  --text-s: 14px;
  --text-m: 17px;
  --text-l: clamp(18px, 1.6vw, 21px);
  --text-xl: clamp(28px, 4.4vw, 52px);
  --text-display: clamp(42px, 8vw, 92px);
  --leading-tight: 1.06;
  --leading-body: 1.45;
  --weight-regular: 400;
  --weight-medium: 500;
  --weight-bold: 600;

  --ease-out: var(--p-ease-out);
  --ease-overshoot: var(--p-ease-overshoot);
  --dur-fast: 150ms;
  --dur-base: 260ms;
  --dur-slow: 600ms;

  --target: 44px;
  --gut: 20px;
  --bar-h: 56px;
  --stage-h: clamp(120px, min(34svh, 100svh - var(--bar-h) - 450px), 360px);

  /* stacking: main < stage < bar < dock < popovers < toasts. Sheets and popovers also
     use the top layer where the browser has it. */
  --layer-stage: 20;
  --layer-bar: 40;
  --layer-dock: 45;
  --layer-pop: 70;
  --layer-toast: 80;
}
@media (min-width: 760px) {
  :root {
    --gut: 48px;
    --stage-h: clamp(150px, min(42svh, 100svh - var(--bar-h) - 360px), 460px);
  }
}
@media (min-width: 760px) and (max-height: 520px) {
  :root { --stage-h: clamp(90px, 26svh, 150px); }
}

/* Notebook Atelier UI kit. Semantic tokens only (CONTRACT.md 3c). All motion sits in the
   final prefers-reduced-motion: no-preference block, so reduced motion gets none. */

/* ---------- base ---------- */
.ui, .ui * { box-sizing: border-box; }
.ui {
  font: var(--weight-regular) var(--text-m) / var(--leading-body) var(--font-body);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
}
:where(.ui) :is(button, input, textarea) { font: inherit; color: inherit; }
:where(.ui) button { background: none; border: 0; padding: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
:where(.ui) :is(button:disabled, [aria-disabled="true"]) { cursor: default; }
:where(.ui) :focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; border-radius: var(--radius-m); }
.ui-icon { fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ui-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ui-label { display: block; font-size: var(--text-s); color: var(--color-text-muted); margin: 0 0 var(--space-2) var(--space-1); }
html.ui-locked { overflow: hidden; }

/* ---------- sliding ring (segmented, swatches, gallery tabs) ---------- */
.ui-ring { position: absolute; left: 0; top: 0; width: 0; height: 0; opacity: 0; border-radius: var(--radius-m); box-shadow: 0 0 0 2px var(--color-ring); pointer-events: none; }
.ui-ring--round { border-radius: 50%; }
.ui-choice { position: relative; display: flex; gap: var(--space-1); padding: 0 var(--space-3); margin: 0 auto; width: max-content; max-width: none; }
.ui-choice__scroll { max-width: 100%; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; padding: var(--space-2) 0; display: flex; }
.ui-choice__scroll::-webkit-scrollbar { display: none; }
.ui-choice__scroll--wrap { overflow: visible; }
.ui-choice__scroll--wrap .ui-choice { flex-wrap: wrap; width: auto; justify-content: center; }
/* rows that overflow and scroll (phones) fade at both edges so the cut reads as scrollable */
.ui-choice__scroll.is-overflow { -webkit-mask-image: linear-gradient(90deg, transparent, #000 24px, #000 calc(100% - 24px), transparent); mask-image: linear-gradient(90deg, transparent, #000 24px, #000 calc(100% - 24px), transparent); }
@media (min-width: 1024px) {
  .ui-choice__scroll { overflow: visible; }
  .ui-choice__scroll .ui-choice { flex-wrap: wrap; width: auto; justify-content: center; }
  .ui-choice__scroll.is-overflow { -webkit-mask-image: none; mask-image: none; }
}

/* ---------- segmented ---------- */
.ui-seg__opt { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: var(--target); min-height: var(--target); padding: var(--space-2) var(--space-4); border-radius: var(--radius-m); color: var(--color-text-muted); white-space: nowrap; }
.ui-seg__opt:hover { color: var(--color-text); }
.ui-seg__opt[aria-checked="true"] { color: var(--color-text); font-weight: var(--weight-bold); }
.ui-seg__opt:disabled { opacity: .4; }
.ui-seg__name { font-size: var(--text-s); text-wrap: balance; }
.ui-seg__sub { font-size: var(--text-xs); font-weight: var(--weight-regular); color: var(--color-text-muted); }
.ui-seg > .ui-label { text-align: center; margin-left: 0; }

/* ---------- swatches ---------- */
.ui-sw { display: flex; flex-direction: column; align-items: stretch; gap: var(--space-1); min-width: 0; max-width: 100%; }
.ui-seg { min-width: 0; max-width: 100%; }
.ui-sw__dot { position: relative; width: 48px; height: 48px; min-width: 48px; border-radius: 50%; display: grid; place-items: center; }
.ui-sw__dot i { display: block; width: 34px; height: 34px; border-radius: 50%; background: var(--swatch); box-shadow: inset 0 0 0 1px var(--color-text-subtle); }
/* Made to order: full color, a dashed ring at the selection ring's offset. Selection wins. */
.ui-sw__dot[data-dim]:not(:focus-visible) { outline: 1px dashed var(--color-text-muted); outline-offset: 0; }
.ui-sw__dot[data-dim][aria-checked="true"]:not(:focus-visible) { outline: none; }
.ui-sw__dot[aria-checked="true"] i { transform: scale(.94); }
.ui-sw__label { min-height: 3em; text-align: center; }
.ui-sw__label[data-empty="1"] { visibility: hidden; }
.ui-sw__name { text-wrap: balance; display: block; font-size: var(--text-m); font-weight: var(--weight-bold); }
.ui-sw__note { text-wrap: balance; display: block; font-size: var(--text-s); color: var(--color-text-muted); }

/* ---------- photo swatches ---------- */
.ui-photoswatches { display: flex; flex-direction: column; align-items: stretch; gap: var(--space-4); min-width: 0; }
.ui-photo { margin: 0 auto; width: 100%; max-width: 640px; }
.ui-photo__frame { position: relative; aspect-ratio: 16 / 10; overflow: hidden; border-radius: var(--radius-l); background: var(--swatch, var(--color-field)); box-shadow: var(--shadow-1); }
.ui-photo__img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; }
.ui-photo__img[data-on] { opacity: 1; }

/* ---------- stepper ---------- */
.ui-stepper { display: inline-flex; align-items: center; background: var(--color-field); border-radius: var(--radius-m); }
.ui-stepper__btn { width: var(--target); height: var(--target); display: grid; place-items: center; border-radius: var(--radius-m); color: var(--color-text); }
.ui-stepper__btn[aria-disabled="true"] { color: var(--color-text-subtle); opacity: .5; }
.ui-stepper__value { display: flex; align-items: baseline; justify-content: center; gap: var(--space-1); min-width: 5.5em; padding: 0 var(--space-1); }
.ui-stepper__input { width: 4ch; min-width: 0; flex: 1 1 auto; height: var(--target); border: 0; background: transparent; text-align: center; font-size: var(--text-m); font-variant-numeric: tabular-nums; border-radius: var(--radius-s); }
.ui-stepper__input:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--color-focus); }
.ui-stepper__unit { font-size: var(--text-s); color: var(--color-text-muted); }

/* ---------- select ---------- */
.ui-select { position: relative; display: block; }
.ui-select__btn { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); width: 100%; min-height: var(--target); padding: var(--space-2) var(--space-4); background: var(--color-field); box-shadow: inset 0 0 0 1px var(--color-field-edge); border-radius: var(--radius-m); text-align: left; }
.ui-select.is-placeholder .ui-select__val { color: var(--color-text-muted); }
.ui-select__val { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ui-select__chev { color: var(--color-text-muted); }
.ui-select__pop { position: fixed; inset: auto; margin: 0; border: 0; z-index: var(--layer-pop); overflow-y: auto; overscroll-behavior: contain; padding: var(--space-2); background: var(--glass-bg); -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); border-radius: var(--radius-m); box-shadow: var(--shadow-2); visibility: hidden; opacity: 0; outline: none; }
.ui-select__pop.is-open { visibility: visible; opacity: 1; }
.ui-select__gname { padding: var(--space-3) var(--space-3) var(--space-1); font-size: var(--text-xs); color: var(--color-text-muted); }
.ui-select__opt { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); min-height: var(--target); padding: var(--space-2) var(--space-3); border-radius: var(--radius-s); cursor: pointer; }
.ui-select__opt.is-active { background: var(--color-field); }
.ui-select__opt[aria-selected="true"] { font-weight: var(--weight-bold); }
.ui-select__opt[aria-disabled="true"] { opacity: .4; cursor: default; }
.ui-select__tick { visibility: hidden; }
.ui-select__opt[aria-selected="true"] .ui-select__tick { visibility: visible; }

/* ---------- toggle ---------- */
.ui-toggle { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); width: 100%; min-height: var(--target); padding: var(--space-1) 0; text-align: left; border-radius: var(--radius-m); }
.ui-toggle__text { display: flex; flex-direction: column; }
.ui-toggle__hint { font-size: var(--text-s); color: var(--color-text-muted); }
.ui-toggle__track { position: relative; flex: none; width: 52px; height: 32px; border-radius: var(--radius-full); background: var(--color-field); box-shadow: inset 0 0 0 1px var(--color-text-subtle); }
.ui-toggle__thumb { position: absolute; left: 4px; top: 4px; width: 24px; height: 24px; border-radius: 50%; background: var(--color-text-subtle); box-shadow: var(--shadow-1); }
.ui-toggle[aria-checked="true"] .ui-toggle__track { background: var(--color-text); box-shadow: none; }
.ui-toggle[aria-checked="true"] .ui-toggle__thumb { transform: translateX(20px); background: var(--color-surface-raised); }
.ui-toggle__label { font-weight: var(--weight-regular); }

/* ---------- slider ---------- */
.ui-slider__head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-3); }
.ui-slider__head .ui-label { margin: 0; font-size: var(--text-m); color: var(--color-text); }
.ui-slider__out { font-size: var(--text-s); font-variant-numeric: tabular-nums; color: var(--color-text); }
.ui-slider__track { position: relative; }
.ui-slider__input { -webkit-appearance: none; appearance: none; display: block; width: 100%; height: var(--target); margin: 0; background: transparent; cursor: pointer; --thumb: 22px; }
.ui-slider__input::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: linear-gradient(to right, var(--color-text) calc(var(--f) * 100%), var(--color-field) calc(var(--f) * 100%)); }
.ui-slider__input::-moz-range-track { height: 4px; border-radius: 2px; background: var(--color-field); }
.ui-slider__input::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--color-text); }
.ui-slider__input::-webkit-slider-thumb { -webkit-appearance: none; width: var(--thumb); height: var(--thumb); margin-top: calc((4px - var(--thumb)) / 2); border-radius: 50%; background: var(--color-surface-raised); border: 0; box-shadow: var(--shadow-2), 0 0 0 1.5px var(--color-ring); }
.ui-slider__input::-moz-range-thumb { width: var(--thumb); height: var(--thumb); border-radius: 50%; background: var(--color-surface-raised); border: 0; box-shadow: var(--shadow-2), 0 0 0 1.5px var(--color-ring); }
.ui-slider__input:focus-visible { outline: none; }
.ui-slider__input:focus-visible::-webkit-slider-thumb { box-shadow: var(--shadow-2), 0 0 0 2px var(--color-focus), 0 0 0 4px var(--color-surface); }
.ui-slider__input:focus-visible::-moz-range-thumb { box-shadow: var(--shadow-2), 0 0 0 2px var(--color-focus), 0 0 0 4px var(--color-surface); }
.ui-slider__tick { position: absolute; top: calc(50% + 14px); left: calc(11px + (100% - 22px) * var(--f)); width: 1.5px; height: 8px; margin-left: -.75px; border-radius: 1px; background: var(--color-text-subtle); pointer-events: none; }

/* ---------- text field ---------- */
.ui-text__input { display: block; width: 100%; min-height: var(--target); padding: var(--space-3) var(--space-4); border: 0; border-radius: var(--radius-m); background: var(--color-field); box-shadow: inset 0 0 0 1px var(--color-field-edge); font-size: var(--text-m); line-height: var(--leading-body); }
textarea.ui-text__input { resize: none; overflow-y: auto; min-height: 76px; }
.ui-text__input::placeholder { color: var(--color-text-muted); }
.ui-text__input:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--color-focus); border-radius: var(--radius-m); }
.ui-text__note { display: flex; justify-content: space-between; gap: var(--space-3); margin: var(--space-1) var(--space-1) 0; font-size: var(--text-xs); color: var(--color-text-muted); }
.ui-text__note:has(.ui-text__msg:empty):not(:has(.ui-text__count)) { display: none; }
.ui-text__count { margin-left: auto; font-variant-numeric: tabular-nums; }
.ui-text.has-error .ui-text__msg { color: var(--color-danger); }
.ui-text.has-error .ui-text__input { box-shadow: 0 0 0 1.5px var(--color-danger); }

/* ---------- color field ---------- */
.ui-color__row { display: flex; align-items: center; gap: var(--space-3); }
.ui-color__chip { position: relative; flex: none; width: var(--target); height: var(--target); display: grid; place-items: center; border-radius: 50%; }
.ui-color__chip i { display: block; width: 32px; height: 32px; border-radius: 50%; background: var(--swatch); box-shadow: inset 0 0 0 1px var(--color-text-subtle); }
.ui-color__native { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; border: 0; padding: 0; }
.ui-color__chip:has(:focus-visible) { outline: 2px solid var(--color-focus); outline-offset: 3px; }
.ui-color__hex { width: auto; flex: 1; min-width: 0; min-height: var(--target); padding: var(--space-2) var(--space-4); border: 0; border-radius: var(--radius-m); background: var(--color-field); box-shadow: inset 0 0 0 1px var(--color-field-edge); font-family: var(--font-body); font-variant-numeric: tabular-nums; }
.ui-color__hex:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--color-focus); }
.ui-color.has-error .ui-color__hex { box-shadow: 0 0 0 1.5px var(--color-danger); }

/* ---------- disclosure ---------- */
.ui-disc__btn { display: inline-flex; align-items: center; gap: var(--space-1); min-height: var(--target); padding: 0 var(--space-2) 0 0; font-size: var(--text-m); font-weight: var(--weight-bold); border-radius: var(--radius-m); }
/* inside a disclosure, sub-headings are quiet group labels so the disclosure stays the parent */
.ui-disc .ui-disc__body :is(h3, h4) { text-align: left; font-size: var(--text-s); font-weight: var(--weight-medium); color: var(--color-text-muted); letter-spacing: 0; }
.ui-disc.is-open .ui-disc__chev { transform: rotate(180deg); }
.ui-disc__panel { display: grid; grid-template-rows: 0fr; visibility: hidden; }
.ui-disc.is-open .ui-disc__panel { grid-template-rows: 1fr; visibility: visible; }
.ui-disc__clip { min-height: 0; overflow: hidden; padding: 0 var(--space-1); margin: 0 calc(var(--space-1) * -1); }
.ui-disc__body { padding: var(--space-3) 0 var(--space-4); display: grid; gap: var(--space-4); }

/* ---------- sheet ---------- */
.ui-sheet { padding: 0; border: 0; color: var(--color-text); background: var(--color-surface); border-radius: var(--radius-l); box-shadow: var(--shadow-sheet); width: min(640px, 94vw); max-width: none; max-height: min(84dvh, 820px); margin: auto; overflow: hidden; }
.ui-sheet[data-size="l"] { width: min(960px, 94vw); height: min(80dvh, 780px); }
.ui-sheet[open] { display: flex; flex-direction: column; }
.ui-sheet::backdrop { background: var(--color-scrim); -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); }
.ui-sheet__head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-5) var(--space-6) var(--space-2); }
.ui-sheet__title { margin: 0; font-size: var(--text-xl); font-weight: var(--weight-medium); letter-spacing: -.01em; }
.ui-sheet__x { width: var(--target); height: var(--target); display: grid; place-items: center; border-radius: 50%; margin-right: calc(var(--space-2) * -1); }
.ui-sheet__x:hover { background: var(--color-field); }
.ui-sheet__body { flex: 1 1 auto; min-height: 0; overflow: auto; padding: 0 var(--space-6) var(--space-6); overscroll-behavior: contain; }
@media (max-width: 600px) {
  .ui-sheet, .ui-sheet[data-size="l"] { width: 100%; max-width: 100%; margin: auto 0 0; border-radius: var(--radius-l) var(--radius-l) 0 0; max-height: 88dvh; height: auto; }
  .ui-sheet[data-size="l"] { height: 88dvh; }
  .ui-sheet__head { padding: var(--space-4) var(--space-4) var(--space-1); }
  .ui-sheet__body { padding: 0 var(--space-4) calc(var(--space-5) + env(safe-area-inset-bottom)); }
}

/* ---------- gallery ---------- */
.ui-gallery__bar { position: relative; z-index: 2; display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); flex-wrap: wrap; padding: var(--space-2) 0; }
.ui-gallery__bar:empty { display: none; }
.ui-sheet .ui-gallery__bar { position: sticky; top: 0; background: var(--glass-bg); -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); margin: 0 calc(var(--space-6) * -1); padding: var(--space-2) var(--space-6); }
@media (max-width: 600px) { .ui-sheet .ui-gallery__bar { margin: 0 calc(var(--space-4) * -1); padding: var(--space-2) var(--space-4); } }
.ui-gallery__tabscroll { flex: 1 1 280px; min-width: 0; overflow-x: auto; scrollbar-width: none; padding: var(--space-1) 0; }
.ui-gallery__tabscroll::-webkit-scrollbar { display: none; }
.ui-gallery__tabs { position: relative; display: flex; gap: var(--space-1); width: max-content; padding: 0 var(--space-1); }
.ui-gallery__tab { min-height: var(--target); padding: 0 var(--space-4); border-radius: var(--radius-m); color: var(--color-text-muted); font-size: var(--text-s); white-space: nowrap; }
.ui-gallery__tab:hover { color: var(--color-text); }
.ui-gallery__tab[aria-selected="true"] { color: var(--color-text); font-weight: var(--weight-bold); }
.ui-gallery__search { display: flex; align-items: center; gap: var(--space-2); flex: 0 1 220px; min-height: var(--target); padding: 0 var(--space-4); border-radius: var(--radius-m); background: var(--color-field); color: var(--color-text-muted); }
.ui-gallery__search:focus-within { box-shadow: 0 0 0 2px var(--color-focus); }
.ui-gallery__q { flex: 1; min-width: 0; height: var(--target); border: 0; background: transparent; outline: none; font-size: var(--text-m); color: var(--color-text); -webkit-appearance: none; appearance: none; }
.ui-gallery__q::-webkit-search-cancel-button { -webkit-appearance: none; }
.ui-gallery__q::placeholder { color: var(--color-text-muted); }
.ui-gallery__gh { display: none; margin: var(--space-6) 0 var(--space-3); font-size: var(--text-s); font-weight: var(--weight-medium); color: var(--color-text-muted); }
.ui-gallery.is-all .ui-gallery__gh { display: block; }
.ui-gallery__sec:first-child .ui-gallery__gh { margin-top: var(--space-3); }
.ui-gallery__grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--min, 104px), 1fr)); gap: var(--space-3) var(--space-2); padding: var(--space-2) var(--space-1); }
.ui-gallery__grid-wrap { margin: 0 calc(var(--space-1) * -1); }
.ui-gallery__item { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); padding: var(--space-2); border-radius: var(--radius-m); color: var(--color-text); min-width: 0; }
.ui-gallery__item[hidden] { display: none; }
.ui-gallery__thumb { display: block; width: 100%; aspect-ratio: var(--aspect); border-radius: var(--radius-s); background: var(--color-page); box-shadow: var(--shadow-1); overflow: hidden; }
.ui-gallery__img { display: block; width: 100%; height: 100%; object-fit: cover; opacity: 0; }
.ui-gallery__img.is-loaded { opacity: 1; }
.ui-gallery__cap { text-wrap: balance; font-size: var(--text-xs); line-height: 1.25; text-align: center; color: var(--color-text-muted); overflow-wrap: anywhere; }
.ui-gallery__item[aria-selected="true"] { box-shadow: 0 0 0 2px var(--color-ring); }
.ui-gallery__item[aria-selected="true"] .ui-gallery__cap { color: var(--color-text); font-weight: var(--weight-bold); }
.ui-gallery__empty { margin: var(--space-7) 0; text-align: center; color: var(--color-text-muted); }
@media (max-width: 600px) { .ui-gallery__grid { grid-template-columns: repeat(3, 1fr); } .ui-gallery__search { flex-basis: 100%; } }

/* ---------- list editor ---------- */
.ui-le__list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--space-2); }
.ui-le__row { background: var(--color-surface); border-radius: var(--radius-m); box-shadow: var(--shadow-1); position: relative; }
.ui-le__head { display: flex; align-items: stretch; }
.ui-le__grip { flex: none; width: var(--target); min-height: var(--target); display: grid; place-items: center; color: var(--color-text-muted); cursor: grab; touch-action: none; border-radius: var(--radius-m); }
.ui-le__grip:hover { color: var(--color-text); }
.ui-le__sum { flex: 1; min-width: 0; min-height: var(--target); display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-2) var(--space-4) var(--space-2) 0; text-align: left; border-radius: var(--radius-m); }
.ui-le__text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ui-le__sum[aria-expanded="true"] .ui-le__text { font-weight: var(--weight-bold); }
.ui-le__chev { color: var(--color-text-muted); }
.ui-le__row.is-open .ui-le__chev { transform: rotate(180deg); }
.ui-le__panel { display: grid; grid-template-rows: 0fr; visibility: hidden; }
.ui-le__row.is-open .ui-le__panel { grid-template-rows: 1fr; visibility: visible; }
.ui-le__clip { min-height: 0; overflow: hidden; }
.ui-le__inner { padding: var(--space-2) var(--space-4) var(--space-4) var(--space-4); display: grid; gap: var(--space-4); }
.ui-le__editor { display: grid; gap: var(--space-4); }
.ui-le__acts { display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-2); margin-left: calc(var(--space-2) * -1); }
.ui-le__act { display: inline-flex; align-items: center; gap: var(--space-2); min-height: var(--target); padding: 0 var(--space-3); border-radius: var(--radius-m); font-size: var(--text-s); color: var(--color-text-muted); }
.ui-le__act:hover { color: var(--color-text); background: var(--color-field); }
.ui-le__act[aria-disabled="true"] { opacity: .4; pointer-events: none; }
.ui-le__act.is-danger:hover { color: var(--color-danger); }
.ui-le__add { display: inline-flex; align-items: center; gap: var(--space-2); min-height: var(--target); margin-top: var(--space-2); padding: 0 var(--space-4); border-radius: var(--radius-m); font-weight: var(--weight-medium); font-size: var(--text-s); background: var(--color-field); }
.ui-le__add[aria-disabled="true"] { opacity: .4; pointer-events: none; }
.ui-le__empty { margin: var(--space-3) 0; color: var(--color-text-muted); font-size: var(--text-s); }
.ui-le__empty[hidden] { display: none; }
.ui-le__row.is-dragging { z-index: 3; box-shadow: var(--shadow-2); cursor: grabbing; }
.ui-le__list.is-sorting { user-select: none; }
.ui-le__row.is-removing { opacity: 0; transform: scale(.98); }

/* ---------- toast ---------- */
.ui-toasts { position: fixed; left: 0; right: 0; bottom: calc(var(--space-5) + env(safe-area-inset-bottom)); z-index: var(--layer-toast); display: flex; justify-content: center; pointer-events: none; padding: 0 var(--space-4); }
.ui-toast { pointer-events: auto; display: flex; align-items: center; gap: var(--space-4); max-width: min(92vw, 460px); min-height: var(--target); padding: var(--space-2) var(--space-5); border-radius: var(--radius-l); background: var(--glass-bg); -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); box-shadow: var(--shadow-2); font: var(--weight-regular) var(--text-s) / var(--leading-body) var(--font-body); color: var(--color-text); opacity: 0; }
.ui-toast.is-in { opacity: 1; }
.ui-toast[data-tone="danger"] .ui-toast__msg { color: var(--color-danger); }
.ui-toast__action { min-height: var(--target); padding: 0 var(--space-2); margin-right: calc(var(--space-2) * -1); font: inherit; font-weight: var(--weight-bold); color: var(--color-text); background: none; border: 0; cursor: pointer; border-radius: var(--radius-m); }
.ui-toast__action:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }

/* ---------- motion (everything that moves lives here) ---------- */
@media (prefers-reduced-motion: no-preference) {
  .ui-ring[data-ready] { transition: transform var(--dur-base) var(--ease-overshoot), width var(--dur-base) var(--ease-overshoot), height var(--dur-base) var(--ease-overshoot), opacity var(--dur-fast); }
  .ui-seg__opt { transition: color var(--dur-fast); }
  .ui-sw__dot i { transition: transform var(--dur-base) var(--ease-overshoot), opacity var(--dur-fast); }
  .ui-sw__dot:hover i { transform: scale(1.06); }
  .ui-sw__dot[aria-checked="true"]:hover i { transform: scale(.98); }
  .ui-sw__name, .ui-sw__note { animation: ui-rise var(--dur-base) var(--ease-overshoot); }
  .ui-photo__img { transition: opacity var(--dur-slow) var(--ease-out); }
  .ui-stepper__btn { transition: transform var(--dur-fast) var(--ease-overshoot), opacity var(--dur-fast); }
  .ui-stepper__btn:active { transform: scale(.92); }
  .ui-stepper__value.is-bump { animation: ui-bump var(--dur-base) var(--ease-overshoot); }
  .ui-select__chev { transition: transform var(--dur-base) var(--ease-overshoot); }
  .ui-select.is-open .ui-select__chev { transform: rotate(180deg); }
  .ui-select__pop { transform: translateY(-6px) scale(.98); transform-origin: top; transition: opacity var(--dur-fast) var(--ease-out), transform var(--dur-base) var(--ease-overshoot), visibility 0s linear var(--dur-base); }
  .ui-select__pop[data-side="up"] { transform: translateY(6px) scale(.98); transform-origin: bottom; }
  .ui-select__pop.is-open { transform: none; transition-delay: 0s; }
  .ui-toggle__track { transition: box-shadow var(--dur-base) var(--ease-out); }
  .ui-toggle__thumb { transition: transform var(--dur-base) var(--ease-overshoot), background-color var(--dur-fast); }
  .ui-slider__input::-webkit-slider-thumb { transition: transform var(--dur-fast) var(--ease-overshoot); }
  .ui-slider__input:active::-webkit-slider-thumb { transform: scale(1.12); }
  .ui-slider__input::-moz-range-thumb { transition: transform var(--dur-fast) var(--ease-overshoot); }
  .ui-slider__input:active::-moz-range-thumb { transform: scale(1.12); }
  .ui-text__input, .ui-color__hex, .ui-gallery__search { transition: box-shadow var(--dur-base) var(--ease-out); }
  .ui-disc__chev, .ui-le__chev { transition: transform var(--dur-base) var(--ease-overshoot); }
  .ui-disc__panel, .ui-le__panel { transition: grid-template-rows var(--dur-base) var(--ease-overshoot), visibility 0s linear var(--dur-base); }
  .ui-disc.is-open .ui-disc__panel, .ui-le__row.is-open .ui-le__panel { transition-delay: 0s; }
  .ui-sheet[open] { animation: ui-pop var(--dur-base) var(--ease-overshoot); }
  .ui-sheet[open]::backdrop { animation: ui-fade var(--dur-base) var(--ease-out); }
  .ui-sheet.is-closing { animation: ui-pop-out var(--dur-fast) var(--ease-out) forwards; }
  .ui-sheet.is-closing::backdrop { animation: ui-fade-out var(--dur-fast) var(--ease-out) forwards; }
  .ui-gallery__img { transition: opacity var(--dur-base) var(--ease-out); }
  .ui-gallery__item { transition: box-shadow var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-overshoot); }
  .ui-gallery__item:hover { transform: translateY(-2px); }
  .ui-gallery__tab { transition: color var(--dur-fast); }
  .ui-le__row { transition: transform var(--dur-base) var(--ease-overshoot), opacity var(--dur-base) var(--ease-out), box-shadow var(--dur-base) var(--ease-out); }
  .ui-le__row.is-dragging { transition: box-shadow var(--dur-base) var(--ease-out); }
  .ui-le__list.is-sorting .ui-le__row.is-dragging { transform-origin: center; }
  .ui-le__row.is-new { animation: ui-rise var(--dur-base) var(--ease-overshoot); }
  .ui-toast { transform: translateY(12px); transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-overshoot); }
  .ui-toast.is-in { transform: none; }
  .ui-toast.is-out { opacity: 0; transform: translateY(8px); animation: ui-fade-out var(--dur-fast) var(--ease-out) forwards; }
  @keyframes ui-rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
  @keyframes ui-bump { 0% { transform: scale(.94); } 100% { transform: none; } }
  @keyframes ui-pop { from { opacity: 0; transform: translateY(18px) scale(.985); } to { opacity: 1; transform: none; } }
  @keyframes ui-pop-out { to { opacity: 0; transform: translateY(12px) scale(.985); } }
  @keyframes ui-fade { from { opacity: 0; } to { opacity: 1; } }
  @keyframes ui-fade-out { to { opacity: 0; } }
  @media (max-width: 600px) {
    @keyframes ui-pop { from { opacity: 1; transform: translateY(100%); } to { opacity: 1; transform: none; } }
    @keyframes ui-pop-out { to { transform: translateY(100%); } }
  }
}

/* ---------- dark: stage drawing rims (dark leather and black elastic must not vanish) ---------- */
@media (prefers-color-scheme: dark) {
  .stage svg .pd-lea, .stage svg .pd-ela { stroke: color-mix(in oklab, var(--color-text) 22%, transparent); stroke-width: 1; vector-effect: non-scaling-stroke; }
  .stage svg .pd-ela { stroke: color-mix(in oklab, var(--color-text) 30%, transparent); }
  .ui-sw__dot i, .ui-color__chip i { box-shadow: inset 0 0 0 1px var(--color-field-edge); }
}

/* ---------- forced colors: selection and focus carried by system colors ---------- */
/* Selection is a 2px solid Highlight outline; focus is a 3px dashed one that outranks it, so a focused, selected control shows both. */
@media (forced-colors: active) {
  .ui-ring { display: none; }
  .ui-sw__dot i, .ui-color__chip i, .ik__chip i { forced-color-adjust: none; box-shadow: inset 0 0 0 1px CanvasText; }
  [role="radio"][aria-checked="true"], .ui-gallery__item[aria-selected="true"], .ui-gallery__tab[aria-selected="true"], .pg-pick[aria-pressed="true"], .pg-pick[aria-checked="true"], .ik__chip[aria-checked="true"] { outline: 2px solid Highlight; outline-offset: 2px; }
  .ui-sw__dot[data-dim]:not(:focus-visible) { outline: 1px dashed CanvasText; }
  .ui-sw__dot[data-dim][aria-checked="true"]:not(:focus-visible) { outline: 2px solid Highlight; outline-offset: 2px; }
  .leather .ui-sw__dot[data-dim][aria-checked="true"]:not(:focus-visible) { outline: 2px solid Highlight; outline-offset: 2px; }
  :focus-visible:focus-visible:focus-visible, :where(.ui) :focus-visible:focus-visible { outline: 3px dashed Highlight; outline-offset: 3px; }
  .pd-slot:focus-visible .pd-ghost { stroke: Highlight; stroke-width: 3px; stroke-dasharray: 5 3; }
  /* Backgrounds and shadows are dropped here, so every control and field keeps its edge with a real border. */
  .ui-toggle__track { border: 1px solid CanvasText; background: Canvas; }
  .ui-toggle__thumb { background: CanvasText; box-shadow: none; }
  .ui-toggle[aria-checked="true"] .ui-toggle__track { background: Highlight; border-color: Highlight; }
  .ui-toggle[aria-checked="true"] .ui-toggle__thumb { background: HighlightText; }
  .ui-text__input, .ui-color__hex, .ui-select__btn, .ui-stepper, .ui-gallery__search, .ui-select__pop, .db textarea { border: 1px solid CanvasText; }
  .fb-btn, .ik__btn, .rv-btn, .pr-btn, .pg-pick, .pg-pager__btn, .rv-nav, .ik__chip { border: 1px solid ButtonText; }
}

/* App shell: bar, stage, chapter rhythm, dock, dialog, states. Uses semantic
   tokens only; the few component aliases live at the top. */

:root {
  --bar-fill: var(--glass-bg);
  --btn-bg: var(--color-text);
  --btn-fg: var(--color-bg);
  --stage-top: calc(var(--bar-h) + env(safe-area-inset-top));
  --col: 760px;
  --col-wide: 960px;
  --dock-fade: 20px;
}

*, *::before, *::after { box-sizing: border-box; }
/* Focused controls clear the stage and the sticky heading; JS does the gliding, so native scrolling stays instant. */
:root { --focus-pad: calc(var(--stage-top) + var(--stage-h) + 76px); }
html { scroll-behavior: auto; -webkit-text-size-adjust: 100%; }
/* Focused controls clear the stage and heading through their own margins, so the chapters' snap areas stay whole. */
.chap * { scroll-margin-top: var(--focus-pad); scroll-margin-bottom: var(--space-5); }
html:has(dialog[open]) { overflow: hidden; }
body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  overflow-x: clip;
  font: var(--weight-regular) var(--text-m)/var(--leading-body) var(--font-body);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; border-radius: var(--radius-s); }
.i { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.skip { position: fixed; left: var(--space-3); top: -80px; z-index: 100; padding: var(--space-3) var(--space-4); background: var(--color-surface); color: var(--color-text); border-radius: var(--radius-s); box-shadow: var(--shadow-2); }
.skip:focus { top: var(--space-3); }

/* ---------- primary action ---------- */
.go {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--target); padding: 0 var(--space-5); border-radius: var(--radius-full);
  background: var(--btn-bg); color: var(--btn-fg); font-weight: var(--weight-bold); font-size: 15px;
  transition: transform var(--dur-slow) var(--ease-overshoot), opacity var(--dur-base) var(--ease-out);
}
.go:hover { transform: scale(1.03); }
.go:active { transform: scale(0.98); }
.go[disabled] { opacity: 0.5; cursor: default; transform: none; }
.go:focus-visible { outline-offset: 3px; border-radius: var(--radius-full); }

/* ---------- top bar ---------- */
.bar {
  position: fixed; inset: 0 0 auto 0; z-index: 40;
  height: var(--stage-top); padding: env(safe-area-inset-top) var(--gut) 0;
  display: flex; align-items: center; gap: var(--space-4);
  background: linear-gradient(color-mix(in oklab, var(--color-bg) calc(78% + var(--hero-p, 1) * 16%), transparent) 0 0), var(--bar-fill);
  -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
  backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
  box-shadow: 0 6px 24px oklch(0 0 0 / 0.05);
}
.brand { min-height: var(--target); display: flex; align-items: center; font-weight: var(--weight-bold); font-size: 15px; letter-spacing: 0.01em; white-space: nowrap; }
.chapnav { position: relative; }
.navbtn {
  display: inline-flex; align-items: center; gap: var(--space-1); min-height: var(--target); padding: 0 var(--space-3);
  border-radius: var(--radius-full); font-size: 15px; font-weight: var(--weight-medium); color: var(--color-text);
  transition: background var(--dur-base) var(--ease-out);
}
.navbtn:hover { background: var(--color-field); }
.navbtn .i { transition: transform var(--dur-base) var(--ease-overshoot); }
.navbtn[aria-expanded="true"] .i { transform: rotate(180deg); }
.navpanel {
  position: fixed; top: calc(var(--stage-top) + var(--space-2)); left: var(--gut);
  width: min(300px, calc(100vw - 2 * var(--gut))); max-height: calc(100svh - var(--stage-top) - var(--space-5)); overflow: auto;
  padding: var(--space-2); border-radius: var(--radius-m); background: var(--glass-bg);
  -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
  backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
  box-shadow: var(--shadow-2);
}
.navpanel:not([hidden]) { animation: pop var(--dur-slow) var(--ease-overshoot); }
.navpanel ol { list-style: none; margin: 0; padding: 0; }
.navpanel button {
  display: flex; width: 100%; align-items: center; min-height: var(--target); padding: 0 var(--space-3);
  border-radius: var(--radius-s); text-align: left; font-size: 15px; color: var(--color-text-muted);
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast);
}
.navpanel button:hover { background: var(--color-field); color: var(--color-text); }
.navpanel button[aria-current="step"] { color: var(--color-text); font-weight: var(--weight-bold); }
.navextra { margin-top: var(--space-2); padding-top: var(--space-2); box-shadow: 0 -1px 0 oklch(0 0 0 / 0.04); }
.navextra:empty { display: none; }
.summary { flex: 1; min-width: 0; font-size: var(--text-s); color: var(--color-text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.summary b { font-weight: var(--weight-bold); color: var(--color-text); }
@media (min-width: 760px) {
  /* Centred on the viewport, not in whatever the brand, nav and action leave; ellipsis before it meets either side. */
  .summary { position: absolute; left: 50%; top: 50%; flex: none; transform: translate(-50%, -50%); width: max-content; max-width: max(0px, calc(100vw - 2 * (var(--gut) + 250px))); }
}
.bar .go { opacity: 0; visibility: hidden; transform: translateY(-6px); transition: transform var(--dur-slow) var(--ease-overshoot), opacity var(--dur-base) var(--ease-out), visibility 0s linear var(--dur-base); }
.bar .go.on { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }
.bar .go.on:hover { transform: scale(1.03); }

/* ---------- stage ---------- */
.stage {
  position: fixed; left: 0; right: 0; top: var(--stage-top); height: var(--stage-h); z-index: 20;
  background: var(--color-bg);
}
.stage svg { display: block; width: 100%; height: 100%; overflow: visible; }
.pd-lea { fill: var(--leather, #9a5431); transition: fill var(--dur-slow) var(--ease-out); }
.pd-ela { fill: var(--elastic, #262626); transition: fill var(--dur-slow) var(--ease-out); }
.pd-page { fill: var(--color-page); }
.pd-img { transition: opacity var(--dur-slow) var(--ease-out); }
.pd-ghost { fill: var(--color-text); fill-opacity: 0.03; stroke: var(--color-text); stroke-opacity: 0.22; stroke-width: 1; vector-effect: non-scaling-stroke; transition: fill-opacity var(--dur-base), stroke-opacity var(--dur-base); }
.pd-ghostband { fill: var(--color-text); fill-opacity: 0.1; display: none; }
.pd-hit { fill: transparent; }
.pd-slot { cursor: pointer; transition: opacity var(--dur-base); }
.pd-slot:hover .pd-ghost { fill-opacity: 0.07; stroke-opacity: 0.4; }
.pd-slot:focus-visible { outline: none; }
.pd-slot:focus-visible .pd-ghost { stroke: var(--color-focus); stroke-width: 2; }
.pd-lbl { fill: var(--color-text-muted); font-size: 12px; text-anchor: middle; }
@media (max-width: 1100px) { .pd-lbl { font-size: 10px; } }
/* Tablet widths keep the to-scale row unlabeled but for the chosen size; phones lay the row out with every label. */
@media (min-width: 720px) and (max-width: 1100px) { .pd-lbl:not(.sel) { display: none; } }
.pd-lbl.sel { fill: var(--color-text); font-weight: var(--weight-bold); }
.pd-deboss { font-weight: var(--weight-bold); letter-spacing: 0.12em; text-anchor: middle; }

.engine-status {
  position: absolute; left: var(--gut); bottom: var(--space-4); z-index: 2; display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-1) var(--space-3); border-radius: var(--radius-full);
  background: color-mix(in oklab, var(--color-bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
  font-size: var(--text-s); color: var(--color-text);
}
.engine-status[hidden] { display: none; }
.engine-status[data-state="booting"] span { animation: breathe 2.4s ease-in-out infinite; }
.engine-status button { min-height: var(--target); padding: 0 var(--space-3); border-radius: var(--radius-full); background: var(--color-field); color: var(--color-text); font-weight: var(--weight-medium); }
.engine-status[data-state="error"] { color: var(--color-danger); }
@keyframes breathe { 0%, 100% { opacity: 0.8; } 50% { opacity: 1; } }

/* ---------- chapters ---------- */
main { position: relative; z-index: 10; }
.chap {
  min-height: 100svh; display: flex; flex-direction: column; justify-content: flex-start;
  padding: calc(var(--stage-top) + var(--stage-h)) var(--gut) clamp(24px, 5svh, 56px);
}
.chap > * { width: 100%; max-width: var(--col); margin-inline: auto; }
.chap h1, .chap h2 { margin-block: 0; letter-spacing: -0.02em; text-align: center; }
/* The heading stays under the stage while a tall chapter scrolls past it. */
.chap > h2 + * { margin-top: var(--space-2); }
.chap > h2 {
  position: sticky; top: calc(var(--stage-top) + var(--stage-h)); z-index: 5;
  font-weight: var(--weight-bold); font-size: clamp(28px, 3.4vw, 44px); line-height: var(--leading-tight);
  padding: var(--space-2) 0 var(--space-3);
  background: var(--color-bg); box-shadow: 0 0 0 100vmax var(--color-bg); clip-path: inset(-24px -100vmax 0);
}
@media (max-height: 700px) { .chap > h2 { font-size: min(30px, 7.6vw); padding-bottom: var(--space-2); } }
/* Phones and short windows need the room: the heading scrolls away with its chapter. */
@media (max-width: 759px), (max-height: 520px) {
  .chap > h2 { position: static; box-shadow: none; clip-path: none; }
  :root { --focus-pad: calc(var(--stage-top) + var(--stage-h) + 16px); }
}
/* Sections take focus only as paging/skip targets; the section-sized ring reads as noise. */
.chap:focus { outline: none; }
.hero { justify-content: flex-end; padding-top: calc(var(--stage-top) + var(--stage-h) + var(--space-2)); }
.chap h1 { font-family: var(--font-display); font-weight: var(--weight-regular); font-size: var(--text-display); line-height: 1; letter-spacing: -0.015em; }
.chap[data-chapter="review"], .chap[data-chapter="printing"] { padding-top: var(--stage-top); }
.chap[data-chapter="review"] > h2, .chap[data-chapter="printing"] > h2 { top: var(--stage-top); padding-top: var(--space-5); }
/* Phones: the review ends with its actions and the printing heading follows without a screen of air. */
@media (max-width: 759px) { .chap[data-chapter="printing"] > h2 { padding-top: var(--space-2); } }
.hero .blk { text-align: center; }
.hero { position: relative; isolation: isolate; }
.chap > .hero-photo { position: absolute; inset: 0; width: auto; max-width: none; margin: 0; z-index: -1; overflow: hidden; opacity: calc(1 - var(--hero-p, 1) * 0.6); }
.hero-photo picture { display: block; width: 100%; height: 100%; }
.hero-photo img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 0; }
@media (max-width: 640px) { .hero-photo img { object-position: 50% 30%; } }
.hero-photo::after { content: ""; position: absolute; inset: 0; background: linear-gradient(to top, var(--color-bg) 0, var(--color-bg) 8%, color-mix(in oklab, var(--color-bg) 60%, transparent) 24%, transparent 42%); }
.stage { background: color-mix(in oklab, var(--color-bg) calc(var(--hero-p, 1) * 100%), transparent); }
.stage svg { opacity: var(--hero-svg, 1); }
.lede { margin: var(--space-3) auto 0; max-width: 36em; text-wrap: balance; font-size: var(--text-l); color: var(--color-text-muted); line-height: 1.4; }
.cue {
  display: inline-flex; flex-direction: column; align-items: center; gap: 2px; margin-top: var(--space-5);
  min-height: var(--target); min-width: var(--target); font-weight: var(--weight-bold); font-size: 15px;
}
.cue .i { width: 20px; height: 20px; animation: nudge 2.4s var(--ease-overshoot) infinite; }
@keyframes nudge { 0%, 60%, 100% { transform: translateY(0); } 30% { transform: translateY(5px); } }
.chap-coming { text-align: center; color: var(--color-text-muted); margin-block: var(--space-4); }

.js.motion .rise { opacity: 0; transform: translateY(22px); transition: opacity 0.8s var(--ease-out), transform 0.9s var(--ease-overshoot); }
.js.motion .rise.in { opacity: 1; transform: none; }

/* Printing and the footer read as one closing page: the chapter takes what the footer leaves of the screen. */
:root { --foot-h: clamp(400px, 56svh, 560px); --pr-h: 232px; }
.chap[data-chapter="printing"] { min-height: var(--pr-h); padding-bottom: var(--space-5); }
.foot {
  position: relative; z-index: 10; min-height: var(--foot-h); display: flex; flex-direction: column; justify-content: center;
  padding: var(--space-5) var(--gut) calc(var(--space-6) + env(safe-area-inset-bottom)); text-align: center; color: var(--color-text-muted); font-size: var(--text-s);
}
.foot > * { width: 100%; max-width: var(--col-wide); margin-inline: auto; }
.foot p { margin-block: 0 var(--space-2); max-width: 40em; text-wrap: balance; }
.foot-sub { color: var(--color-text-subtle); }
.foot-photos { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); margin: 0 auto var(--space-5); }
/* One shared crop and tone, so the warm tabletop and the gray studio read as a pair. */
.foot-photos img { display: block; width: 100%; height: 100%; aspect-ratio: 16 / 10; object-fit: cover; object-position: 50% 50%; border-radius: var(--radius-l); filter: saturate(0.6) sepia(0.1) contrast(1.02); }
@media (min-width: 760px) {
  /* The closing page is one screen: the photos take what the heading, disclosure and captions leave. */
  .foot { min-height: calc(100svh - var(--pr-h)); justify-content: flex-start; padding-top: var(--space-4); }
  .foot-photos { flex: 1 1 auto; min-height: 220px; max-height: 520px; width: 100%; }
  .foot-photos img { aspect-ratio: auto; }
}

/* ---------- previews ---------- */
.na-preview { display: grid; place-items: center; min-height: 80px; }
.na-preview-canvas, .na-preview-thumb { display: block; max-width: 100%; height: auto; background: var(--color-page); border-radius: 3px; box-shadow: var(--shadow-1); }
.na-preview-note { margin: 0; text-align: center; font-size: var(--text-s); color: var(--color-text-muted); }
.na-preview-note.bad { color: var(--color-danger); }

/* Baked page renders are pure white; in dark they read as warm paper, not glare. */
@media (prefers-color-scheme: dark) {
  .na-preview-canvas, .na-preview-thumb, .ui-gallery__img, .pg-pick__img { filter: brightness(0.92) sepia(0.06); }
}

/* ---------- dialog, toast, fatal ---------- */
.na-dialog {
  border: 0; padding: 0; background: var(--color-surface); color: var(--color-text); border-radius: var(--radius-l);
  box-shadow: var(--shadow-sheet); width: min(520px, 94vw); max-width: none;
}
.na-dialog[open] { display: flex; flex-direction: column; animation: pop var(--dur-slow) var(--ease-overshoot); }
.na-dialog::backdrop { background: var(--color-scrim); -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); }
.dh { display: flex; align-items: center; justify-content: space-between; padding: var(--space-5) var(--space-5) var(--space-2); }
.dh h3 { margin: 0; font-size: 22px; font-weight: var(--weight-bold); letter-spacing: -0.01em; }
.x { width: var(--target); height: var(--target); display: grid; place-items: center; border-radius: 50%; transition: background var(--dur-base); }
.x:hover { background: var(--color-field); }
.db { padding: 0 var(--space-5) var(--space-5); }
.db p { margin: 0 0 var(--space-3); color: var(--color-text-muted); font-size: 15px; }
.db textarea { width: 100%; border: 0; border-radius: var(--radius-m); background: var(--color-field); padding: var(--space-3) var(--space-4); font-size: 14px; resize: none; word-break: break-all; }
.db textarea:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }
.db .row { display: flex; justify-content: center; margin-top: var(--space-4); }
@keyframes pop { from { opacity: 0; transform: translateY(18px) scale(0.985); } to { opacity: 1; transform: none; } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--space-5) + env(safe-area-inset-bottom)); z-index: 60; transform: translate(-50%, 16px);
  max-width: min(92vw, 460px); padding: var(--space-3) var(--space-5); border-radius: var(--radius-m);
  background: var(--glass-bg); -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
  box-shadow: var(--shadow-1); font-size: 15px; text-align: center; opacity: 0; visibility: hidden;
  transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-slow) var(--ease-overshoot), visibility var(--dur-base);
}
.toast.on { opacity: 1; visibility: visible; transform: translate(-50%, 0); }
.toast.bad { color: var(--color-danger); }

.fatal { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; padding: var(--gut); background: var(--color-bg); }
.fatal[hidden] { display: none; }
.fatal-box { max-width: 28em; text-align: center; }
.fatal-box h2 { margin: 0 0 var(--space-3); font-size: clamp(24px, 4vw, 34px); letter-spacing: -0.02em; }
.fatal-box p { margin: 0 0 var(--space-5); color: var(--color-text-muted); }
.noscript { position: relative; z-index: 30; max-width: 34em; margin: 0 auto; padding: var(--space-9) var(--gut); text-align: center; }
.noscript h2 { margin: 0 0 var(--space-3); font-size: var(--text-xl); letter-spacing: -0.02em; line-height: var(--leading-tight); }
.noscript p { margin: 0; color: var(--color-text-muted); }

/* ---------- phone: compact bar, bottom-anchored action ---------- */
.dock { display: none; }
@media (max-width: 759px) {
  .brand, .bar .go { display: none; }
  .summary { text-align: right; }
  .dock {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 45; display: flex; justify-content: center;
    padding: var(--space-3) var(--gut) calc(var(--space-3) + env(safe-area-inset-bottom));
  }
  /* A glass band like the top bar, near-opaque under the button; only its top edge fades out, so cut rows never read through the Next button. */
  .dock::before {
    content: ""; position: absolute; inset: calc(-1 * var(--dock-fade)) 0 0 0; z-index: -1; pointer-events: none;
    background: linear-gradient(color-mix(in oklab, var(--color-bg) 86%, transparent) 0 0), var(--bar-fill);
    -webkit-backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur)); backdrop-filter: saturate(var(--glass-saturate)) blur(var(--glass-blur));
    -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 var(--dock-fade)); mask-image: linear-gradient(to bottom, transparent 0, #000 var(--dock-fade));
  }
  .dock[hidden] { display: none; }
  .dock .go { width: 100%; max-width: 420px; min-height: 52px; font-size: 16px; }
  .foot { min-height: calc(100svh - var(--pr-h)); justify-content: flex-start; padding-top: var(--space-4); padding-bottom: calc(var(--space-9) + env(safe-area-inset-bottom)); }
  .foot-photos { flex: 1 1 auto; min-height: 140px; max-height: 300px; }
  .foot-photos img { aspect-ratio: auto; }
  .na-dialog { width: 100vw; margin: auto 0 0; border-radius: var(--radius-l) var(--radius-l) 0 0; }
  .toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
  .chap * { scroll-margin-bottom: calc(76px + env(safe-area-inset-bottom) + var(--space-3)); }
  .engine-status { top: var(--space-2); bottom: auto; }
}
@media (max-width: 520px) { .summary { display: none; } .bar { justify-content: flex-start; } }

/* ---------- reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}

/* A scroll or swipe settles on the next chapter's top; a chapter taller than the screen scrolls freely inside. */
html { scroll-snap-type: y mandatory; }
html.snap-off { scroll-snap-type: none; }
.chap, .hero { scroll-snap-align: start; scroll-snap-stop: always; }
.foot { scroll-snap-align: end; }
/* A chapter taller than the screen ends with a scroll step of room, so its last row is in full view before the next chapter takes over. */
.chap.tall { padding-bottom: 160px; }
@media (max-width: 759px) { .chap.tall { padding-bottom: calc(220px + env(safe-area-inset-bottom)); } }

/* Size chapter. Semantic tokens only. */
.sz { display: flex; flex-direction: column; align-items: stretch; gap: var(--space-3); min-width: 0; }
.sz > .ui-seg { max-width: 100%; }
.sz .ui-choice__scroll { overflow: visible; padding: 0; }
.sz .ui-choice { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: var(--space-1); width: 100%; padding: 0; }
.sz .ui-seg__opt { padding: var(--space-1) var(--space-2); text-align: center; white-space: normal; }
.sz__detail { min-height: 3.4em; }
.sz__detail p { margin: 0; }
.sz__dims { font-size: var(--text-m); }
.sz__sub, .sz__note { margin-top: var(--space-1) !important; color: var(--color-text-muted); font-size: var(--text-s); text-wrap: balance; }
.sz__custom { display: flex; flex-direction: column; align-items: flex-start; gap: var(--space-4); width: 100%; }
.sz__custom[hidden] { display: none; }
.sz__grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-4) var(--space-5); width: 100%; }
.sz__field { min-width: 0; display: flex; flex-direction: column; }
.sz__field > .ui-label { margin-left: 0; }
@media (max-width: 720px) { .sz .ui-choice { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 480px) { .sz__grid { grid-template-columns: minmax(0, 1fr); } }

/* Pages chapter. Semantic tokens only. */
.pg { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-6); max-width: var(--col); text-align: left; }
.pg-h { margin: 0; font-size: var(--text-m); font-weight: var(--weight-bold); line-height: var(--leading-tight); }
.pg-note { margin: 0; font-size: var(--text-s); line-height: var(--leading-body); color: var(--color-text-muted); }
.pg-note:empty { display: none; }

.pg-sum { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; column-gap: var(--space-5); row-gap: var(--space-1); text-align: center; }
.pg-sum > .pg-note { flex-basis: 100%; }
.pg-total { margin: 0; font-size: var(--text-l); font-weight: var(--weight-medium); }
.pg-pager { display: inline-flex; align-items: center; gap: var(--space-2); }
.pg-pager[hidden] { display: none; }
.pg-pager__label { min-width: 8.5em; text-align: center; font-size: var(--text-s); color: var(--color-text-muted); font-variant-numeric: tabular-nums; }
.pg-pager__btn { display: grid; place-items: center; width: var(--target); height: var(--target); border-radius: var(--radius-full); color: var(--color-text); background: var(--color-field); }
.pg-pager__btn:hover:not(:disabled) { background: color-mix(in oklab, var(--color-field), var(--color-text) 8%); }
.pg-pager__btn:disabled { opacity: 0.35; cursor: default; }
.pg-pager__btn .i { width: 20px; height: 20px; }
.pg-sum .pg-note { max-width: 36em; text-wrap: balance; }

.pg-quick { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-3); }
.pg-quick:empty { display: none; }
.pg-quick__head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); min-height: var(--target); }
.pg-link { min-height: var(--target); padding: 0 var(--space-3); border-radius: var(--radius-m); color: var(--color-text); font-weight: var(--weight-medium); font-size: var(--text-s); text-decoration: underline; text-underline-offset: 3px; }
.pg-link:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }

.pg .ui-le__text { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; white-space: normal; }
.pg-sections { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-3); }

.pg .ui-le__list { grid-template-columns: minmax(0, 1fr); }
.pg-editor { display: grid; gap: var(--space-4); }
.pg-field { display: grid; gap: var(--space-1); justify-items: start; min-width: 0; }
.pg-field .ui-seg { max-width: 100%; }
.pg-picks { display: grid; gap: var(--space-3); grid-template-columns: 1fr; }
.pg-picks.is-split { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.pg-pick {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; align-items: center; column-gap: var(--space-3); row-gap: var(--space-1);
  width: 100%; min-height: var(--target); padding: var(--space-2); border-radius: var(--radius-m);
  background: var(--color-field); color: var(--color-text); text-align: left;
  transition: background var(--dur-fast) var(--ease-out);
}
.pg-pick:hover { background: var(--color-surface-raised); }
.pg-pick:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }
.pg-pick__thumb { grid-row: 1 / span 2; width: 56px; aspect-ratio: 360 / 500; overflow: hidden; border-radius: var(--radius-s); background: var(--color-page); box-shadow: var(--shadow-1); }
.pg-pick__img { display: block; width: 100%; height: 100%; object-fit: cover; }
.pg-pick__text { display: grid; min-width: 0; }
.pg-pick__side { font-size: var(--text-xs); color: var(--color-text-muted); }
.pg-pick__name { text-wrap: balance; font-weight: var(--weight-medium); overflow-wrap: anywhere; }
.pg-pick__go { font-size: var(--text-s); color: var(--color-text-muted); text-decoration: underline; text-underline-offset: 3px; }

.pg-more { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-2); }
.pg-stack { display: grid; gap: var(--space-4); min-width: 0; }
.pg-stack[hidden], .pg-field[hidden] { display: none; }
.pg-grid { display: grid; gap: var(--space-3); grid-template-columns: repeat(2, minmax(0, 1fr)); }
.pg-labels:empty { display: none; }

.pg-pair { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: var(--space-3); }
.pg-pair__name { font-size: var(--text-m); color: var(--color-text); }
.pg-pair__ctl { display: flex; gap: var(--space-5); }
.pg-pair .ui-toggle { width: auto; gap: var(--space-2); }
.pg-pair .ui-toggle__label { font-size: var(--text-s); color: var(--color-text-muted); }

@media (max-width: 600px) {
  .pg { gap: var(--space-5); }
  .pg-picks.is-split { grid-template-columns: 1fr; }
  .pg-grid, .pg-pair__ctl { grid-template-columns: 1fr; }
  .pg .ui-seg__opt { white-space: normal; text-align: center; padding-inline: var(--space-3); }
}

@media (prefers-reduced-motion: reduce) {
  .pg-pick { transition: none; }
}

/* Short screens: the style cards must clear the fold under the stage. */
@media (max-height: 760px) { .pg { gap: var(--space-4); } .pg-quick { gap: var(--space-1); } .pg-quick .ui-gallery__grid { grid-template-columns: repeat(auto-fill, minmax(90px, 96px)); justify-content: space-between; } .pg-sum { row-gap: 0; } }

/* Front and back pages. Semantic tokens only. The fb-* classes are shared with Endpapers. */
.fb { width: 100%; display: grid; gap: var(--space-3); min-width: 0; }
.fb-seg { display: flex; justify-content: flex-start; }
/* The scroller clips on overflow, so it keeps room for the 2px selection and focus rings it would otherwise cut. */
.fb .ui-choice__scroll { padding: 6px 0; margin: -6px 0; }
.fb .ui-choice { margin-inline: 0; }
@media (min-width: 760px) { .fb .ui-le__list { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; } }
.fb-list { display: grid; gap: var(--space-2); min-width: 0; }
.fb-list[hidden] { display: none; }
.fb-sum { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.fb-sum small { font-size: var(--text-xs); color: var(--color-text-muted); }
.fb-quiet { margin: 0; font-size: var(--text-s); line-height: var(--leading-body); color: var(--color-text-muted); }
.fb-count { min-height: 1.5em; }
.fb-edit { display: grid; gap: var(--space-4); min-width: 0; }
.fb-grid { display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); min-width: 0; }
.fb-row { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.fb-fyv { display: grid; gap: var(--space-4); min-width: 0; padding-top: var(--space-2); }
.fb-sub { display: grid; gap: var(--space-2); min-width: 0; }
.fb-sub h4 { margin: 0; font-size: var(--text-s); font-weight: var(--weight-bold); color: var(--color-text-muted); }
.fb-btn { display: inline-flex; align-items: center; justify-content: center; min-height: var(--target); padding: 0 var(--space-4); border-radius: var(--radius-m); background: var(--color-field); color: var(--color-text); font-size: var(--text-s); font-weight: var(--weight-medium); }
.fb-btn:hover { background: var(--color-surface-raised); }
.fb-btn[aria-disabled="true"] { opacity: .5; pointer-events: none; }
.fb-link { min-height: var(--target); min-width: var(--target); padding: 0 var(--space-2); font-size: var(--text-s); color: var(--color-text-muted); visibility: hidden; }
.fb-link:hover { color: var(--color-text); }
.fb-color { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: end; gap: var(--space-1); min-width: 0; }
.fb-color.is-set .fb-link { visibility: visible; }
.fb-colors { grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)); }
.fb-picker { min-width: 0; }
.fb-picker .fb-quiet { margin-top: var(--space-3); }
.fb-spread { grid-column: span 2; }
.fb .ui-text, .fb .ui-color { min-width: 0; }
@media (max-width: 600px) {
  .fb { gap: var(--space-3); }
  .fb-grid, .fb-colors { grid-template-columns: 1fr; }
  .fb-row .fb-btn { flex: 1 1 100%; }
}

/* Endpapers. Shares the fb-* classes from front-back.css. */
.ep { width: 100%; display: grid; gap: var(--space-4); min-width: 0; }
.ep[hidden] { display: none; }
.ep-side { display: grid; gap: var(--space-3); min-width: 0; }
.ep-side[hidden] { display: none; }
.ep-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-3); }
.ep-head h3 { margin: 0; font-size: var(--text-m); font-weight: var(--weight-bold); }
.ep-name { font-size: var(--text-s); color: var(--color-text-muted); }
.ep-picks { min-width: 0; }
.ep-picks .ui-gallery__grid { grid-template-columns: repeat(6, minmax(0, 84px)); justify-content: space-between; padding: 0 var(--space-1); }
.ep-all { justify-self: start; }
.ep-opts { display: grid; gap: var(--space-4); min-width: 0; }
.ep-desc { margin: 0; font-size: var(--text-s); color: var(--color-text-muted); }
.ep-pages { display: grid; gap: var(--space-3); min-width: 0; }
@media (max-width: 600px) { .ep { gap: var(--space-4); } }

/* One row for the quiet controls; an open disclosure takes the full width below. */
.ep-side { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); }
.ep-side > .ep-head, .ep-side > .ep-picks { flex: 1 1 100%; }
.ep-side > .ui-disc { flex: 0 1 auto; }
.ep-side > .ui-disc.is-open { flex-basis: 100%; }
@media (min-width: 760px) {
  .ep { grid-template-columns: minmax(0, 1fr) auto; align-items: center; }
  .ep > .ep-side { grid-column: 1 / -1; }
  .ep > .ui-toggle-wrap { grid-column: 1; grid-row: 2; }
  .ep > .ui-disc { grid-column: 2; grid-row: 2; }
  /* The switch hugs its own label instead of sitting against the Endpaper pages disclosure. */
  .ep > .ui-toggle-wrap .ui-toggle { width: auto; justify-content: flex-start; gap: var(--space-5); }
  .ep > .ui-disc.is-open { grid-column: 1 / -1; grid-row: 4; }
}
@media (max-width: 600px) { .ep-picks .ui-gallery__grid { grid-template-columns: repeat(3, minmax(0, 1fr)); justify-content: stretch; } }

/* Ink and type chapter. Semantic tokens only. */
.ik { display: flex; flex-direction: column; gap: var(--space-5); width: 100%; margin-inline: auto; min-width: 0; }
.ik__block { display: flex; flex-direction: column; align-items: center; gap: var(--space-3); min-width: 0; }
.ik__h { margin: 0 0 var(--space-2); font-size: var(--text-m); font-weight: var(--weight-bold); text-align: center; }
.ik__pals { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-4) var(--space-6); width: 100%; }
.ik__group { display: flex; flex-direction: column; align-items: center; min-width: 0; max-width: 100%; }
.ik__group > .ui-label { margin-left: 0; }
.ik__chips { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-1); }
.ik__chip {
  display: inline-flex; align-items: center; justify-content: center; gap: 3px; min-width: var(--target); min-height: var(--target);
  padding: 0 var(--space-2); border-radius: var(--radius-full); background: none; border: 0; cursor: pointer;
}
.ik__chip i { width: 14px; height: 14px; border-radius: var(--radius-full); background: var(--c); box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--color-text) 18%, transparent); }
.ik__chip:hover { background: var(--color-field); }
.ik__chip[aria-checked="true"] { outline: 2px solid var(--color-ring); outline-offset: -2px; }
.ik__chip:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; }
.ik__palname { margin: 0; min-height: 1.5em; color: var(--color-text-muted); font-size: var(--text-s); }
.ik__fonts { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-4); width: 100%; }
.ik__font { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
.ik__sample { padding: 0 var(--space-1); color: var(--color-text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ik__up { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: var(--space-3); }
.ik__btn { min-height: var(--target); padding: 0 var(--space-4); border-radius: var(--radius-full); background: var(--color-field); font-weight: var(--weight-medium); font-size: var(--text-s); cursor: pointer; border: 0; color: var(--color-text); }
.ik__btn:hover { background: var(--color-surface-raised); }
.ik__msg, .ik__note { color: var(--color-text-muted); font-size: var(--text-s); }
.ik__note { margin: 0; text-align: center; text-wrap: balance; }
.ik__note[hidden] { display: none; }
.ik__more { display: flex; flex-direction: column; gap: var(--space-6); padding-top: var(--space-4); }
.ik__colors { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-4); }
.ik__sections { display: flex; flex-direction: column; gap: var(--space-4); }
.ik__sec { display: flex; flex-direction: column; gap: var(--space-3); padding-bottom: var(--space-4); border-bottom: 1px solid var(--color-field); }
.ik__sec .ik__fonts { display: none; }
.ik__sec[data-on="1"] .ik__fonts { display: grid; }
@media (max-width: 640px) { .ik__fonts { grid-template-columns: minmax(0, 1fr); } }

/* One alignment rule: sub-heads and controls start on the column's left edge. */
.ik__block { align-items: stretch; gap: var(--space-2); }
.ik__h { text-align: left; margin: 0; }
.ik__pals { display: grid; justify-content: stretch; gap: var(--space-2); }
.ik__group { align-items: flex-start; }
.ik__chips { justify-content: flex-start; margin-left: calc(var(--space-2) * -1); }
.ik .ui-choice { margin-inline: 0; }
.ik .ui-choice__scroll { padding: 6px 0; margin: -6px 0; }
.ik .ui-seg > .ui-label { text-align: left; }
.ik__up { justify-content: flex-start; }
.ik__note { text-align: left; }
.ik__colors { grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); }
.ik__chips { gap: 0; }
.ik__chip { padding: 0 6px; min-width: 40px; }

.leather { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-2); padding-top: var(--space-5); align-items: start; width: 100%; max-width: var(--col); margin-inline: auto; }
.leather .ui-photoswatches { gap: var(--space-1); }
.leather .ui-photo { max-width: 640px; }
.leather .ui-photo__frame { aspect-ratio: 3 / 2; width: 100%; max-height: clamp(150px, calc(100svh - var(--stage-top) - var(--stage-h) - 300px), 420px); }
.leather .ui-sw__label { position: absolute; width: 1px; height: 1px; min-height: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.leather .ui-choice__scroll { overflow: visible; padding: 6px 0; margin: -2px 0; }
.leather .ui-choice__scroll .ui-choice { flex-wrap: wrap; width: auto; justify-content: center; padding: 0; gap: 0; }
.leather .ui-choice__scroll.is-overflow { -webkit-mask-image: none; mask-image: none; }
.leather__info { text-align: center; max-width: 34em; margin-inline: auto; }
.leather__name { margin: 0; font-size: var(--text-l); font-weight: var(--weight-bold); }
.leather__line { margin: var(--space-1) 0 0; color: var(--color-text-muted); line-height: var(--leading-body); text-wrap: balance; }

@media (min-width: 900px) {
  .leather .ui-sw__dot { width: 44px; height: 44px; min-width: 44px; }
  .leather .ui-sw__dot i { width: 30px; height: 30px; }
  .leather .ui-choice__scroll .ui-choice { flex-wrap: nowrap; width: max-content; position: relative; left: 50%; transform: translateX(-50%); }
}

@media (max-width: 899px) {
  .leather .ui-photo__frame { max-height: clamp(150px, calc(100svh - var(--stage-top) - var(--stage-h) - 335px), 420px); }
}

/* Short or narrow screens: heading, swatches, color name, then the photo, so the choice and its name sit above the dock. */
@media (max-width: 759px), (max-height: 520px) {
  .leather { padding-top: var(--space-2); }
  .leather .ui-photoswatches { display: contents; }
  .leather .ui-sw { order: 1; }
  .leather__info { order: 3; }
  .leather .ui-photo { order: 4; margin-top: var(--space-2); }
  .leather .ui-photo__frame { max-height: min(34svh, 280px); }
}

@media (max-width: 600px) {
  .leather .ui-choice__scroll { overflow-x: auto; }
  .leather .ui-choice__scroll .ui-choice { flex-wrap: nowrap; width: max-content; padding: 0 var(--space-3); }
  .leather .ui-choice__scroll[data-fade="end"] { -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 32px), transparent); mask-image: linear-gradient(90deg, #000 calc(100% - 32px), transparent); }
  .leather .ui-choice__scroll[data-fade="start"] { -webkit-mask-image: linear-gradient(90deg, transparent, #000 32px); mask-image: linear-gradient(90deg, transparent, #000 32px); }
  .leather .ui-choice__scroll[data-fade="both"] { -webkit-mask-image: linear-gradient(90deg, transparent, #000 32px, #000 calc(100% - 32px), transparent); mask-image: linear-gradient(90deg, transparent, #000 32px, #000 calc(100% - 32px), transparent); }
}

/* 31 colors: 8 columns leave one gap in the last row, 16 leave one. The mobile and desktop counts are both
   chosen for that, never a lone dot. */
.elastic { width: 100%; }
.elastic .ui-sw { align-items: stretch; }
.elastic .ui-choice__scroll { overflow: visible; padding: 0; }
/* Rows centered like the leather swatches: each dot sits in a 1/--cols cell, and a short last row stays in the middle. */
.elastic { --cols: 7; }
.elastic .ui-choice { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-1) 0; width: 100%; padding: 0; }
.elastic .ui-sw__dot { flex: none; width: 44px; min-width: 44px; height: 44px; margin-inline: max(0px, calc((100% / var(--cols) - 44px) / 2 - 0.1px)); }
.elastic .ui-sw__label { text-align: left; min-height: 1.6em; margin-top: var(--space-2); }
/* 31 colors: 7 columns leave 3 in the last row, 8 leave 7, 12 leave 7, 16 leave 15; every column holds a 44px target. */
@media (min-width: 440px) { .elastic { --cols: 8; } }
@media (min-width: 640px) { .elastic { --cols: 12; } }
@media (min-width: 860px) { .elastic { --cols: 16; } }

.details { display: grid; gap: var(--space-3); }
.details__group { display: grid; gap: var(--space-2); }
.details__group h3 { margin: 0; font-size: var(--text-m); font-weight: var(--weight-bold); }
.details__main, .details__more { display: grid; gap: var(--space-4); }
.details__fine { margin: 0; font-size: var(--text-xs); color: var(--color-text-muted); }
.details .ui-disc { justify-self: stretch; }
.details__fine { margin-top: calc(var(--space-2) * -1); }
@media (min-width: 900px) {
  .details__group:has(> .ui-text + .ui-text):not(.details__quote) { grid-template-columns: 1fr 1fr; }
  .details__group h3 { grid-column: 1 / -1; }
}

/* Review: summary, page viewer, actions. Semantic tokens only. */
.rv { display: grid; gap: var(--space-5); max-width: var(--col-wide); }
.chap[data-chapter="review"] > .rv { margin-inline: auto; }
.rv-block { display: grid; gap: var(--space-3); }

.rv-btn, .pr-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--target); padding: 0 var(--space-5); border-radius: var(--radius-m);
  font: inherit; font-weight: var(--weight-medium); font-size: var(--text-m); cursor: pointer; border: 0;
  background: var(--color-field); color: var(--color-text); -webkit-tap-highlight-color: transparent;
}
.rv-btn:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; }
.rv-btn:disabled { cursor: default; opacity: 0.6; }
.rv-btn--primary { background: var(--color-text); color: var(--color-bg); }
.rv-btn--quiet { background: transparent; color: var(--color-text); box-shadow: inset 0 0 0 1px var(--color-text-subtle); font-size: var(--text-s); }
.rv-btn--quiet:hover:not(:disabled) { background: var(--color-field); }

.rv-banner {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); flex-wrap: wrap;
  padding: var(--space-4) var(--space-5); border-radius: var(--radius-l); background: var(--color-surface); box-shadow: var(--shadow-1);
}
.rv-banner__text { display: grid; gap: 2px; min-width: 0; }
.rv-banner__text strong { font-weight: var(--weight-bold); }
.rv-banner__text span { color: var(--color-text-muted); font-size: var(--text-s); overflow-wrap: anywhere; }

/* summary: name, then the value in its own right-aligned column. No row carries a thumbnail. */
.rv-sum { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); width: 100%; max-width: var(--col); margin-inline: auto; }
.rv-sum li { border-bottom: 1px solid var(--color-field); }
.rv-sum__row {
  display: grid; grid-template-columns: 8em minmax(0, 1fr) auto; align-items: center; column-gap: var(--space-4);
  width: 100%; min-height: var(--target); padding: var(--space-2) var(--space-1); text-align: left;
  background: none; border: 0; font: inherit; color: var(--color-text); cursor: pointer;
}
.rv-sum__row:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 2px; border-radius: var(--radius-s); }
.rv-sum__name { font-size: var(--text-s); color: var(--color-text-muted); }
.rv-sum__val { text-wrap: balance; min-width: 0; overflow: hidden; overflow-wrap: anywhere; text-align: right; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; }
.rv-sum__go { color: var(--color-text-subtle); }

/* viewer */
/* The spread sets the width: the bar lines up with its edges, not the column's. */
.rv-viewer { --rv-maxh: clamp(300px, 68svh, 720px); --rv-aspect: 1.41; }
.rv-bar { width: min(calc(100% - 2 * (var(--target) + var(--space-2))), calc(var(--rv-maxh) * var(--rv-aspect))); margin-inline: auto; display: flex; align-items: flex-end; justify-content: space-between; gap: var(--space-4); flex-wrap: wrap; }
.rv-jump { width: min(320px, 100%); }
/* The Page/Spread control starts on the spread's left edge; the scroller keeps 6px for its focus ring. */
.rv-bar .ui-choice { flex-wrap: nowrap; width: max-content; margin-inline: 0; padding: 0 6px; }
.rv-bar .ui-seg { margin-left: -6px; }
.rv-bar .ui-choice__scroll { margin-block: -6px; padding-block: 6px; }
.rv-stage { display: grid; grid-template-columns: var(--target) minmax(0, 1fr) var(--target); align-items: center; gap: var(--space-2); }
.rv-holder-wrap { position: relative; display: grid; width: min(100%, calc(var(--rv-maxh) * var(--rv-aspect))); margin-inline: auto; }
.rv-holder { aspect-ratio: var(--rv-aspect); }
.rv-holder[data-state="loading"] { opacity: 0.85; }
.rv-holder .na-preview-canvas, .rv-holder .na-preview-thumb { max-height: 100%; width: auto; }
.rv-nav {
  display: grid; place-items: center; width: var(--target); height: var(--target); border-radius: var(--radius-full);
  background: var(--color-field); color: var(--color-text); border: 0; cursor: pointer;
}
.rv-nav:disabled { opacity: 0.35; cursor: default; }
.rv-nav:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; }
.rv-state { position: absolute; inset: auto 0 var(--space-3); display: flex; justify-content: center; align-items: center; gap: var(--space-3); pointer-events: none; }
.rv-state p { margin: 0; padding: var(--space-2) var(--space-4); border-radius: var(--radius-m); background: var(--glass-bg); backdrop-filter: blur(var(--glass-blur)); font-size: var(--text-s); color: var(--color-text-muted); }
.rv-state[data-kind="error"] { inset: 0; flex-direction: column; pointer-events: auto; background: var(--color-bg); }
.rv-state[data-kind="error"] p { color: var(--color-danger); }
.rv-caption, .rv-count { margin: 0; text-align: center; font-size: var(--text-s); color: var(--color-text-muted); }
.rv-caption.is-blank { font-style: italic; color: var(--color-text-subtle); }
.rv-count { font-variant-numeric: tabular-nums; }

/* actions */
.rv-actions { justify-items: center; text-align: center; max-width: var(--col); width: 100%; margin-inline: auto; gap: var(--space-3); }
.rv-btn--send { min-height: 52px; padding: 0 var(--space-6); font-size: var(--text-l); }
.rv-actions__more { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2); }
.rv-busy { margin: 0; min-height: 1.2em; font-size: var(--text-s); color: var(--color-text-muted); }
.rv-file { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

@media (max-width: 720px) {
  .chap[data-chapter="review"] { padding-bottom: var(--space-2); }
  .rv-stage { grid-template-columns: 1fr 1fr; }
  .rv-holder-wrap { grid-row: 1; grid-column: 1 / -1; width: 100%; }
  .rv-bar { width: 100%; flex-direction: column; align-items: stretch; }
  .rv-jump { width: 100%; }
  .rv-nav { width: 100%; border-radius: var(--radius-m); }
  .rv-sum__row { grid-template-columns: 6em minmax(0, 1fr) auto; column-gap: var(--space-3); }
  .rv-btn--send { width: 100%; }
  .rv-actions__more { width: 100%; }
  .rv-actions__more .rv-btn { flex: 1 1 100%; }
}
@media (prefers-reduced-motion: no-preference) {
  .rv-holder { transition: opacity var(--dur-base) var(--ease-out); }
  .rv-btn, .rv-nav { transition: background-color var(--dur-fast) var(--ease-out), opacity var(--dur-fast); }
}

/* Printing, for Jared: one disclosure, small titled groups. Semantic tokens only. */
.pr { display: grid; gap: var(--space-2); justify-items: center; text-align: center; }
.chap[data-chapter="printing"] > .pr { margin-inline: auto; }
.pr-lede { margin: 0; font-size: var(--text-s); color: var(--color-text-muted); }
.pr .ui-disc { width: 100%; }
.pr .ui-disc > .ui-disc__btn { margin-inline: auto; display: flex; }
.pr-groups { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-5) var(--space-7); text-align: left; padding-top: var(--space-2); }
.pr-group { display: grid; align-content: start; gap: var(--space-3); min-width: 0; padding-bottom: var(--space-4); border-bottom: 1px solid var(--color-field); }
.pr-group__title { margin: 0; font-size: var(--text-m); font-weight: var(--weight-bold); }
.pr-row { min-height: var(--target); display: flex; align-items: center; }
.pr-field { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); min-height: var(--target); }
.pr-field__label { font-size: var(--text-s); color: var(--color-text-muted); min-width: 0; }
.pr-field--wide { display: block; }
.pr-act { display: grid; gap: var(--space-1); justify-items: start; }
.pr-btn { padding: 0 var(--space-4); font-size: var(--text-s); }
.pr-btn:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; }
.pr-btn:disabled { cursor: default; opacity: 0.6; }
.pr-btn--quiet { background: var(--color-field); }
.pr-busy { margin: 0; min-height: 1.2em; font-size: var(--text-xs); color: var(--color-text-muted); }

@media (max-width: 720px) {
  .pr-groups { grid-template-columns: minmax(0, 1fr); }
  .pr-field { flex-wrap: wrap; }
}

.pr-row { display: block; }
.pr-row .ui-toggle { width: 100%; display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); text-align: left; }
.pr-row .ui-toggle__label { font-weight: var(--weight-regular); font-size: var(--text-s); }
.pr .ui-seg > .ui-label { text-align: left; margin-left: 6px; }
.pr .ui-choice { flex-wrap: nowrap; width: max-content; margin: 0; padding: 0 6px; }
.pr .ui-seg { margin-left: -6px; }
.pr .ui-choice__scroll { margin-block: -6px; padding-block: 6px; }
