/* ─── Print / Save-as-PDF styles ──────────────────────────────────────────
   @page sets the paper size; @media print handles layout.

   Why display:contents?
   ──────────────────────
   position:fixed caused Safari to repeat the handout on every page and gave
   Edge an incorrect coordinate origin.  display:contents "dissolves" each
   wrapper element — its children participate directly in the parent's layout
   as if the wrapper doesn't exist.  After dissolving the chain:

     body (block, 8.5in × 11in)
       .shell    → display:contents
       .preview  → display:contents
       .scale-outer → display:contents
       .scale-wrap  → display:contents
       .handout  ← only real box; body's direct block child

   No positioning tricks.  No container-chain sizing surprises.
   The handout at 8.5in × 11in fills the body page exactly.

   Note: onBeforePrint() sets display:none !important (inline) on .shell in
   bulk-print mode — inline !important beats the stylesheet display:contents
   rule, so bulk mode is unaffected.

   Overflow scaling
   ─────────────────
   fitHandoutToPage() stores --print-zoom / --print-natural-h as inline CSS
   custom properties when content is taller than 1056 px.

     pre-zoom width  = 8.5in / zoom  →  post-zoom visual = 8.5in  ✓
     pre-zoom height = naturalH      →  post-zoom visual = PAGE_H  ✓
   ────────────────────────────────────────────────────────────────────────── */

@page {
  size: 8.5in 11in portrait;
  margin: 0;
}

@media print {

  *,
  *::before,
  *::after {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  html, body {
    margin: 0 !important;
    padding: 0 !important;
    background: white !important;
    display: block !important;
    width: 8.5in !important;
  }

  /* ── SINGLE-DISTRICT MODE ──────────────────────────────────────────────
     Clamp body to one page, then dissolve the wrapper chain so .handout
     becomes body's direct block child — no positioning, no surprises.   */

  body:not(:has(#bulk-print-root)) {
    height: 11in !important;
    overflow: hidden !important;
  }

  /* Remove app chrome from layout entirely */
  .top,
  .sb,
  .preview-empty,
  #empty-state { display: none !important; }

  /* Dissolve wrappers — .handout becomes body's direct block child */
  .shell,
  .preview,
  .scale-outer,
  .scale-wrap { display: contents !important; }

  /* The handout is now the only block child of body */
  .handout {
    display: flex !important;
    flex-direction: column !important;
    transform: none !important;
    zoom: var(--print-zoom, 1);
    width: calc(8.5in / var(--print-zoom, 1)) !important;
    height: var(--print-natural-h, 11in) !important;
    overflow: hidden !important;
    page-break-after: avoid !important;
    break-after: avoid !important;
  }

  /* ── BULK-PRINT MODE ───────────────────────────────────────────────────
     onBeforePrint() hides .shell and other chrome with inline
     display:none !important — that beats the display:contents above.
     Body height is auto here to allow multiple pages to flow.           */

  body:has(#bulk-print-root) {
    height: auto !important;
    overflow: visible !important;
  }

  .print-page {
    width: 8.5in !important;
    height: 11in !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
    page-break-after: always !important;
    break-after: page !important;
  }

  .print-page:last-child {
    page-break-after: avoid !important;
    break-after: avoid !important;
  }

  /* Each cloned .handout carries its own --print-zoom / --print-natural-h */
  .print-page .handout {
    width: calc(8.5in / var(--print-zoom, 1)) !important;
    height: var(--print-natural-h, 11in) !important;
  }
}
