/* ════════════════════════════════════════════════════════════════════════
   /paper — Paged.js print stylesheet.

   Loaded by PaperPrintBootstrap on the client when the URL carries
   `?print=1`. Paged.js reads this file, consumes the @page rules, and
   rewrites the DOM into paged output.

   Also usable from the browser's native Print dialog: the @media print
   block in paper.css hides chrome, and this file supplies the page
   geometry + running headers + page numbers.

   Everything here is light mode. Dark print is out of scope.
   ════════════════════════════════════════════════════════════════════════ */

/* ─── Page geometry ────────────────────────────────────────────────────
   A4 portrait with generous book-style margins so the body column reads
   like a trade-paperback rather than an academic manuscript. The only
   running furniture is a single bottom-center page number — readers cite
   "p. 47", and a preprint without page numbers can't be properly
   referenced. Front cover, title page, and back cover use the named
   `:first` frame which suppresses the counter. */

@page {
  size: A4;
  margin: 26mm 28mm 28mm 28mm;

  @bottom-center {
    content: counter(page);
    font-family: var(--upg-mono-label);
    font-size: 9pt;
    color: #666;
    font-variant-numeric: lining-nums tabular-nums;
  }
}

/* Front cover, title page, and back cover all use this frame — keep
   them visually clean (no page number). */
@page :first {
  margin: 26mm 28mm 28mm 28mm;

  @bottom-center { content: ""; }
}

@page toc {
  margin: 26mm 28mm 28mm 28mm;

  @bottom-center {
    content: counter(page, lower-roman);
    font-family: var(--upg-mono-label);
    font-size: 9pt;
    color: #666;
  }
}

/* ─── Hide site chrome in print ─────────────────────────────────────
   Skip-link, site nav, and site footer are screen-only. The Nav
   component renders <NAV class="upg-nav-bar"> inside an UNCLASSED
   wrapper <div> that hosts mouse handlers — that wrapper carries no
   styles of its own, but Paged.js still treats it as a leading block
   in print and pads the booklet with a phantom first page.

   Selectors are intentionally redundant — Paged.js + display:none has
   been flaky in past runs; if any one selector misses, others catch
   it. The :has() selector hides the unclassed wrapper by matching on
   its child .upg-nav-bar / .upg-nav-mobile. */
.skip-to-content,
.upg-nav-bar,
.upg-nav-desktop,
.upg-nav-mobile,
body > footer,
main > nav:not(.paper-toc):not(.paper-pagenav),
body > div:has(> nav.upg-nav-bar),
body > div:has(.upg-nav-mobile) {
  display: none !important;
  visibility: hidden !important;
  height: 0 !important;
  overflow: hidden !important;
}

/* ─── Defensive light-mode override ─────────────────────────────────────
   Puppeteer launches with the host OS colour-scheme, which on a dark-
   mode mac flips data-theme to dark before we can intercept. paper-pdf.ts
   now forces light mode three different ways before navigation, but this
   block is the final guard: even if a [data-theme="dark"] attribute
   leaks through, we re-pin every UPG token to its light value so the
   printed sheet is always black-on-white. */
:root,
[data-theme],
[data-theme="dark"] {
  color-scheme: light !important;
  --upg-bg: #ffffff !important;
  --upg-surface: #FAFAF8 !important;
  --upg-surface-raised: #FFFFFF !important;
  --upg-surface-muted: #F0EADB !important;
  --upg-text: #1C1E2E !important;
  --upg-body: #1C1E2E !important;
  --upg-muted: #8A857D !important;
  --upg-border: #d8d2c2 !important;
  --upg-border-subtle: #e8e3d3 !important;
  --upg-link: #1C1E2E !important;
}

/* ─── Global print typography reset ─────────────────────────────────── */

html, body {
  background: #fff !important;
  color: #111 !important;
}

body {
  font-family: var(--upg-serif);
  font-size: 10.5pt;
  line-height: 1.65;
  font-optical-sizing: auto;
  font-variant-numeric: oldstyle-nums proportional-nums;
}

/* Hide anything tagged as screen-only chrome. */
[data-print-hidden="true"] { display: none !important; }

.paper-route .paper-shell {
  display: block !important;
  padding: 0 !important;
  margin: 0 !important;
  max-width: none !important;
}

/* Body type scale for print. Screen value is 19.5px (calibrated for the
   1440px-wide reading column); on A4 with 28mm margins that lands
   oversized. Pin to the same 10.5pt as the body reset so paragraphs read
   like a preprint, not a zoomed webpage. */
.paper-body,
.paper-section__body {
  max-width: none !important;
  color: #222 !important;
  font-size: 10.5pt !important;
  line-height: 1.55 !important;
}

.paper-section__heading,
.paper-title-page__title,
.paper-title-page__author {
  color: #000 !important;
}

/* ─── Front cover as page 1 ─────────────────────────────────────────── */

.paper-front-cover {
  page: first;
  border-bottom: none !important;
  padding: 0 !important;
  margin: 0 !important;
  page-break-after: always;
  break-after: page;
  min-height: 24cm;
  display: flex !important;
  flex-direction: column;
  justify-content: center;
  gap: 3mm !important;
}

.paper-front-cover__eyebrow {
  font-size: 9pt !important;
  margin-bottom: 12mm !important;
}

.paper-front-cover__logo {
  margin: 6mm 0 12mm !important;
}

.paper-front-cover__logo svg {
  width: 70mm !important;
  height: 70mm !important;
}

.paper-front-cover__fullname {
  font-size: 20pt !important;
  line-height: 1.2 !important;
}

.paper-front-cover__acronym {
  font-size: 18pt !important;
}

.paper-front-cover__rule {
  width: 14mm !important;
  height: 0.7pt !important;
  margin: 6mm 0 3mm !important;
  background: #000 !important;
}

.paper-front-cover__title {
  font-family: var(--upg-display) !important;
  margin: 0 0 10mm !important;
  max-width: none !important;
  letter-spacing: -0.01em !important;
  display: flex !important;
  flex-direction: column !important;
  gap: 3mm !important;
}

.paper-front-cover__title-main {
  font-size: 28pt !important;
  font-weight: 600 !important;
  line-height: 1.15 !important;
}

.paper-front-cover__title-sub {
  font-size: 15pt !important;
  font-weight: 400 !important;
  line-height: 1.3 !important;
  color: #3d3f4e !important;
}

.paper-front-cover__byline {
  margin-top: 0 !important;
}

.paper-front-cover__author {
  font-size: 12pt !important;
}

.paper-front-cover__affil {
  font-size: 10pt !important;
}

.paper-front-cover__footer {
  position: absolute;
  bottom: 0;
  margin-top: auto !important;
  font-size: 8pt !important;
}

/* ─── Title page as page 2 ─────────────────────────────────────────── */

.paper-title-page {
  border-bottom: none !important;
  padding: 0 !important;
  margin: 0 !important;
  page-break-before: always;
  break-before: page;
  page-break-after: always;
  break-after: page;
  min-height: 22cm;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.paper-title-page__title {
  letter-spacing: -0.01em !important;
  margin: 0 0 18mm !important;
  max-width: none !important;
  display: flex !important;
  flex-direction: column !important;
  gap: 3mm !important;
}

.paper-title-page__title-main {
  font-size: 26pt !important;
  font-weight: 600 !important;
  line-height: 1.15 !important;
}

.paper-title-page__title-sub {
  font-size: 15pt !important;
  font-weight: 400 !important;
  line-height: 1.3 !important;
  color: #3d3f4e !important;
}

.paper-title-page__byline {
  font-size: 11pt !important;
  margin-bottom: 18mm !important;
}

.paper-title-page__author {
  font-size: 12pt !important;
  font-weight: 600 !important;
}

.paper-title-page__abstract-body {
  font-size: 10pt !important;
  line-height: 1.65 !important;
}

.paper-title-page__eyebrow {
  font-size: 8pt !important;
  letter-spacing: 0.14em !important;
}

/* ─── TOC (injected by Paged.js on page 2 when possible) ─────────────
   The live TOC in the web view is its own chrome. For print we emit a
   dedicated table of contents populated from section headings via
   `target-counter()` references on an auto-generated block. Paged.js
   resolves those at print time.
────────────────────────────────────────────────────────────────────── */

.paper-print-toc {
  page: toc;
  page-break-before: always;
  page-break-after: always;
  break-before: page;
  break-after: page;
  padding: 0;
}

.paper-print-toc__title {
  font-family: var(--upg-serif);
  font-size: 18pt;
  font-weight: 600;
  margin: 0 0 10mm;
  color: #000;
}

.paper-print-toc__list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.paper-print-toc__item {
  font-family: var(--upg-serif);
  font-size: 11pt;
  line-height: 1.55;
  margin-bottom: 2mm;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 4mm;
}

.paper-print-toc__item--l3 {
  padding-left: 6mm;
  font-size: 10pt;
  color: #333;
}

.paper-print-toc__link {
  color: inherit !important;
  text-decoration: none !important;
  flex: 1;
}

.paper-print-toc__dots {
  flex: 0 1 auto;
  border-bottom: 1px dotted #aaa;
  min-width: 10mm;
  margin: 0 2mm;
  position: relative;
  top: -2pt;
}

/* Paged.js resolves target-counter() against the element's href attribute. */
a.paper-print-toc__page::after {
  content: target-counter(attr(href url), page);
  font-family: var(--upg-mono-label);
  font-size: 9pt;
  color: #333;
  font-variant-numeric: lining-nums tabular-nums;
  text-decoration: none !important;
}

a.paper-print-toc__page {
  color: #333 !important;
  text-decoration: none !important;
  white-space: nowrap;
}

/* ─── Chapter page breaks + running header string ───────────────────── */

.paper-section--l2 {
  page-break-before: always;
  break-before: page;
  margin-top: 0 !important;
  padding-top: 0 !important;
}

/* The very first section after the TOC starts chapter pagination. */
.paper-section--l2:first-of-type {
  /* nothing extra — rule above already forces page break */
}

.paper-section__heading--main {
  string-set: chapter-title content(text);
  font-family: var(--upg-display);
  font-size: 18pt !important;
  font-weight: 600 !important;
  line-height: 1.2 !important;
  letter-spacing: -0.01em !important;
  border-top: none !important;
  padding-top: 0 !important;
  margin: 0 0 6mm !important;
  color: #000 !important;
}

.paper-section__heading--sub {
  font-family: var(--upg-display);
  font-size: 13pt !important;
  font-weight: 600 !important;
  line-height: 1.25 !important;
  letter-spacing: -0.01em !important;
  margin: 7mm 0 2mm !important;
  color: #111 !important;
  break-after: avoid;
}

.paper-section__anchor { display: none !important; }

/* ─── Body typography inside sections ───────────────────────────────── */

.paper-section__body p {
  margin: 0 0 3mm !important;
  orphans: 3;
  widows: 3;
}

.paper-section__body ul,
.paper-section__body ol {
  margin: 0 0 4mm !important;
  padding-left: 6mm !important;
}

.paper-section__body li {
  margin-bottom: 1mm !important;
}

.paper-section__body blockquote {
  background: #f4f4f0 !important;
  border-left: 2pt solid #444 !important;
  padding: 3mm 5mm !important;
  margin: 4mm 0 !important;
  break-inside: avoid;
  font-size: 10pt !important;
}

/* Tables: force print-friendly appearance. */
.paper-section__body .upg-paper-table-wrap {
  background: #fff !important;
  border: 1px solid #bbb !important;
  overflow: visible !important;
  margin: 4mm 0 !important;
  break-inside: avoid;
}

.paper-section__body table.upg-paper-table {
  /* Sora sans for tabular content — matches the screen rule and lifts
     legibility at small print sizes. */
  font-family: var(--upg-display), Sora, sans-serif !important;
  font-size: 8.5pt !important;
  font-variant-numeric: lining-nums tabular-nums !important;
}

.paper-section__body table.upg-paper-table th,
.paper-section__body table.upg-paper-table td {
  border-bottom: 0.5pt solid #ccc !important;
  padding: 2mm 3mm !important;
  font-family: var(--upg-display), Sora, sans-serif !important;
}

.paper-section__body table.upg-paper-table th {
  /* Sora semibold for headers. */
  font-weight: 600 !important;
  border-bottom: 1pt solid #444 !important;
  background: #f4f4f0 !important;
  color: #000 !important;
}

/* Inline code stays mono in print too. */
.paper-section__body table.upg-paper-table code {
  font-family: var(--upg-mono-code) !important;
}

/* Code blocks: clamp size + frame. Background and token colours are now
   left to Shiki — the slack-ochin palette already prints cleanly on
   white, and the previous "color: inherit !important" sweep was
   clobbering every Shiki token to black, leaving code monochrome in the
   PDF. The frame uses a soft warm tone so the block reads as a quoted
   region rather than floating monospace. */
.paper-section__body pre {
  background: #FFFFFF !important;
  border: 1px solid #d8d2c2 !important;
  font-size: 9pt !important;
  line-height: 1.45 !important;
  padding: 2.5mm 3mm !important;
  margin: 4mm 0 !important;
  break-inside: avoid;
  /* Wrap long lines in print so nothing clips off the page edge. */
  white-space: pre-wrap !important;
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
  overflow: visible !important;
}

.paper-section__body pre.shiki {
  background: #FFFFFF !important;
}

/* Mirror the screen wrap rules for Shiki tokens / line spans so PDF
   pickup matches what's on screen. */
.paper-section__body pre.shiki,
.paper-section__body pre.shiki code,
.paper-section__body pre.shiki .line {
  white-space: pre-wrap !important;
  overflow-wrap: anywhere !important;
  word-break: break-word !important;
}

/* Crucially: do NOT set `color` on .shiki, .line, or descendant spans —
   doing so wins via !important against Shiki's inline `style="color:..."`
   and strips every token colour. Print honours the light theme by virtue
   of the paper-pdf force-light chain pinning data-theme=light, which
   keeps --shiki-light values active throughout. */

/* Mermaid diagrams: keep an individual diagram together on one page;
   keep a `<div class="upg-mermaid-pair">` two-up split together as well
   so the left and right columns don't separate across a page break. */
.paper-section__body .upg-mermaid {
  break-inside: avoid;
}
.paper-section__body .upg-mermaid-pair {
  break-inside: avoid;
}

/* Inline code. */
.paper-section__body :not(pre) > code {
  background: transparent !important;
  color: #222 !important;
  font-size: 0.92em !important;
  border-bottom: 0.5pt dotted #aaa;
  padding: 0 !important;
}

/* Entity chips print as bold label + small type suffix, no box. */
.upg-paper-chip {
  background: transparent !important;
  border: none !important;
  font-weight: 700 !important;
  color: #000 !important;
  padding: 0 !important;
  font-family: var(--upg-mono-label) !important;
  font-size: 0.9em !important;
}

.upg-paper-chip::before {
  background: transparent !important;
  border-right: none !important;
  margin-right: 0.15em !important;
  padding-right: 0.15em !important;
  color: #666 !important;
  font-weight: 500 !important;
  font-size: 0.82em !important;
}

.upg-paper-chip::after {
  content: " (" attr(data-type) ")";
  display: none; /* suppress — the ::before already shows the type */
}

/* Links: show URL in parens in print ONLY for external links. */
.paper-section__body a[href^="http"]::after {
  content: " (" attr(href) ")";
  font-family: var(--upg-mono-label);
  font-size: 0.78em;
  color: #666;
  word-break: break-all;
}

/* ─── Back cover / colophon as standalone last page ──────────────── */

.paper-back-cover {
  page: first; /* reuse the :first page frame (no header/footer/running chapter) */
  page-break-before: always;
  break-before: page;
  border-top: none !important;
  padding: 0 !important;
  margin: 0 !important;
  min-height: 24cm;
  display: flex !important;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 4mm !important;
}

.paper-back-cover__mark {
  font-size: 48pt !important;
  margin: 0 0 4mm !important;
}

.paper-back-cover__rule {
  width: 14mm;
  height: 0.5pt;
  background: #bbb;
  margin: 2mm auto 4mm;
}

.paper-back-cover__title {
  font-size: 14pt !important;
}

.paper-back-cover__subtitle {
  font-size: 8pt !important;
  margin-bottom: 8mm !important;
}

.paper-back-cover__meta {
  font-size: 9.5pt !important;
  max-width: 11cm !important;
  margin: 4mm auto !important;
}

.paper-back-cover__meta dt {
  font-size: 7pt !important;
}

.paper-back-cover__colophon {
  font-size: 9pt !important;
  max-width: 12cm !important;
  margin-top: 10mm !important;
}

.paper-back-cover__end {
  margin-top: 10mm !important;
  font-size: 8pt !important;
}

/* Back matter ("References") and appendices style identical to main
   chapters — the `page-break-before: always` on --l2 already handles it. */

/* Orphans / widows on paragraphs by default. */
p, li {
  orphans: 3;
  widows: 3;
}

h2, h3, h4 {
  break-after: avoid;
}

figure, table, pre, blockquote {
  break-inside: avoid;
}

/* ════════════════════════════════════════════════════════════════════════
   Ring-model figure — full-bleed and at least half a page.

   The ring model carries the paper's central conceptual diagram.
   In screen rendering it spans the full body column for outside-label
   legibility; in print we additionally guarantee at least half an A4
   page (≈13.5cm of vertical real estate) so the SVG and its outside
   labels stay readable at print zoom. If it would land awkwardly on a
   page, the renderer is allowed to push it to the next page and avoid
   breaking inside the figure.
   ════════════════════════════════════════════════════════════════════════ */
.paper-route .paper-figure-host--bleed {
  width: 100%;
  margin: 4mm 0 !important;
}

.paper-route .paper-figure--ring-a,
.paper-route .paper-figure--ring-b {
  break-inside: avoid;
  break-after: avoid;
  /* At least half an A4 page tall. A4 is 297mm; minus ~30mm of
     top/bottom margins leaves ~267mm of usable height; half of that is
     ~135mm. Use a slightly conservative 130mm so labels and caption
     can breathe within the band. */
  min-height: 130mm;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.paper-route .paper-figure--ring-a .paper-figure__svg,
.paper-route .paper-figure--ring-b .paper-figure__svg {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
  /* Guarantee the SVG itself uses most of the half-page allotment. */
  min-height: 110mm;
}

/* Print typography for ring labels (rendered via foreignObject divs).
   Override screen px sizes with pt sizes that meet the ≥ 9pt eyebrow /
   ≥ 11pt headline target Captain set. */
.paper-route .paper-figure--ring-a foreignObject div,
.paper-route .paper-figure--ring-b foreignObject div {
  /* Inherit family/colour; sizes set on the inner spans below. */
}
.paper-route .paper-figure--ring-a foreignObject div div:first-child,
.paper-route .paper-figure--ring-b foreignObject div div:first-child {
  font-size: 9pt !important;
}
.paper-route .paper-figure--ring-a foreignObject div div:last-child,
.paper-route .paper-figure--ring-b foreignObject div div:last-child {
  font-size: 12pt !important;
}
