/* ─────────────────────────────────────────────────────────────────────────
   Report export print stylesheet.

   Consumed twice:
     1. `pagedjs` Previewer — paginates the cloned report DOM into
        `.pagedjs_page` blocks and applies these rules for layout preview.
     2. Browser print engine — when we call `window.print()` the same
        rules apply via the `<link rel="stylesheet" media="print">`
        declaration in index.html, so the saved PDF matches the preview.

   Kept intentionally light. The heavy greyscale-on-white rules for app
   chrome live in client/src/index.css `@media print` block.
   ───────────────────────────────────────────────────────────────────── */

@page {
  size: A4;
  /* Generous margins — top is larger to give the running title visual
     breathing room; bottom a touch larger still for the page counter. */
  margin: 28mm 22mm 26mm 22mm;

  @top-left {
    content: string(report-title);
    font-family: system-ui, sans-serif;
    font-size: 9pt;
    color: #666;
  }

  @bottom-right {
    content: "Page " counter(page) " of " counter(pages);
    font-family: system-ui, sans-serif;
    font-size: 9pt;
    color: #666;
  }

  @bottom-left {
    content: string(export-date);
    font-family: system-ui, sans-serif;
    font-size: 9pt;
    color: #666;
  }
}

/* Cover / first page has no running header/footer. */
@page :first {
  @top-left { content: none; }
  @bottom-right { content: none; }
  @bottom-left { content: none; }
}

/* The first <h1> in the report becomes the running title in the header. */
h1 {
  string-set: report-title content();
}

/* The [data-export-date] attribute populates the footer-left date. Set by
   export-pdf.ts before paging so it stays in sync with the filename. */
[data-export-date] {
  string-set: export-date attr(data-export-date);
}

/* Body defaults for the paginated preview + printed output. orphans/widows
   keep paragraphs from stranding a single line at a page boundary. */
body {
  orphans: 3;
  widows: 3;
  line-height: 1.45;
}

/* Break-inside protection.
   Matches our in-app components: <DataCard> renders `bg-card`, recharts
   renders `.recharts-wrapper`, tables wrap in `.overflow-x-auto` / plain
   <table>. [data-pdf-keep-together] is an explicit opt-in any report can
   use to pin a chart + title + legend as a single unit. */
.bg-card,
[class*="bg-card"],
.card,
.recharts-wrapper,
.recharts-surface,
table,
thead,
tbody tr,
figure,
[data-pdf-keep-together],
[class*="rounded-lg"] > [class*="p-"],
.space-y-2 > *,
.space-y-3 > *,
.space-y-4 > *,
.space-y-6 > * {
  break-inside: avoid;
  page-break-inside: avoid;
  -webkit-column-break-inside: avoid;
}

/* Don't orphan a heading at the bottom of a page — always keep it with
   the following content block. */
h1, h2, h3, h4 {
  break-after: avoid;
  page-break-after: avoid;
  break-inside: avoid;
  page-break-inside: avoid;
}

/* Tables: keep header rows with their first data row. */
thead { display: table-header-group; }
tfoot { display: table-footer-group; }
tr    { break-inside: avoid; page-break-inside: avoid; }

/* Long URLs in links should wrap instead of overflowing. */
a { word-break: break-word; }

/* Images + SVGs must not exceed the printable width. */
img, svg {
  max-width: 100% !important;
  height: auto;
}

/* Anything marked no-export / no-print is already hidden by index.css's
   @media print rules — no need to repeat here.

   Opt-ins reports can use:
     className="pdf-break-before"   — force a page break before this block
     className="pdf-break-after"    — force a page break after this block
     data-pdf-keep-together          — treat the block as a single unit */
.pdf-break-before {
  break-before: page;
  page-break-before: always;
}
.pdf-break-after {
  break-after: page;
  page-break-after: always;
}

/* Recharts: the chart wrapper sometimes sits next to its <h2> / <h3>
   title as a sibling. Giving siblings of .recharts-wrapper a bit of
   stickiness (via :has()) keeps them together where supported. */
:has(> .recharts-wrapper) {
  break-inside: avoid;
  page-break-inside: avoid;
}
