/* ============================================================
   print.css — Paged media rules for weasyprint PDF output
   A4, running headers (section title / page number), dated footer
   ============================================================ */

@page {
  size: A4;
  margin-top: 3.5cm; /* was 2cm — extra space creates a gap between the
                          header (pinned to the top) and the body text */
  margin-bottom: 4cm;
  margin-left: 3.5cm;
  margin-right: 3.5cm;
  font-family: Brill, "Minion 3", Palatino, Georgia, Times, Serif;

  /* Footer: date in square brackets, centered, on every page */
  @bottom-center {
    content: "[" string(doc-date) "]";
    font-size: 9pt;
    vertical-align: top;
  }
}

/* --- Even pages: section title on the LEFT, page number on the RIGHT --- */
@page :left {
  @top-left {
    content: string(section-title);
    font-size: 9pt;
    vertical-align: top;
    padding-top: 1cm;
  }
  @top-right {
    content: counter(page);
    font-size: 14pt;
    font-weight: bold;
    vertical-align: top;
    padding-top: 1cm;
  }
}

/* --- Odd pages: section title on the RIGHT, page number on the LEFT --- */
@page :right {
  @top-right {
    content: string(section-title);
    font-size: 9pt;
    vertical-align: top;
    text-align: right;
    padding-top: 1cm;
  }
  @top-left {
    content: counter(page);
    font-size: 14pt;
    font-weight: bold;
    vertical-align: top;
    padding-top: 1cm;
  }
}

/* --- First page: no running headers (title page / opening page) --- */
@page :first {
  @top-left {
    content: none;
  }
  @top-right {
    content: none;
  }
}

/* ------------------------------------------------------------
   Running header source: capture the current section heading.
   Adjust the selector (h1/h2) to whatever level marks a new
   "section" in your handouts.
   ------------------------------------------------------------ */
h2 {
  string-set: section-title content();
}

/* ------------------------------------------------------------
   Running footer source: capture the date from an HTML attribute
   on <html> rather than from a hidden element's text content.
   Requires the template to output <html ... data-date="$date$">.
   Using attr() avoids WeasyPrint quirks around string-set on
   hidden / zero-size / clipped elements.
   ------------------------------------------------------------ */
html {
  string-set: doc-date attr(data-date);
}

/* ------------------------------------------------------------
   Basic print-friendly defaults — tweak to taste / merge with
   your existing screen stylesheet as needed.
   ------------------------------------------------------------ */
body {
  font-size: 11pt;
  line-height: 1.4;
  orphans: 3;
  widows: 3;
}

h1 {
  page-break-before: always;
  break-before: page;
}

h1:first-of-type {
  page-break-before: avoid;
  break-before: avoid;
}

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

/* ------------------------------------------------------------
   Override .wide image width for PDF only. thomas-style.css sets
   width: 80% (used as-is for HTML); this fixed cm value avoids
   the percentage/circular-sizing issue WeasyPrint has with
   percentage widths on images nested in a figure.
   ------------------------------------------------------------ */
img.wide {
  width: 11.2cm; /* 80% of ~14cm content width (A4 minus 3.5cm side margins) */
}

/* ------------------------------------------------------------
   .full-width — for very wide tables in the PDF only. Extends the
   table into the page margins so it uses the full page width minus
   2cm on each side (your @page margins are 3.5cm, so this bleeds in
   1.5cm on each side: 3.5cm - 2cm = 1.5cm).

   If the table is STILL too wide even at that width, tune
   --table-scale down from 1 (e.g. 0.85) on the specific table that
   needs it:
     <div class="full-width" style="--table-scale: 0.85;">

   IMPORTANT — pure CSS cannot measure a table's actual rendered
   width and calculate the exact scale factor needed automatically;
   there's no reactive "shrink to fit" in CSS. --table-scale has to
   be chosen by eye, by rebuilding and checking whether the table
   still overflows.

   IMPORTANT — transform: scale() shrinks what's drawn but does NOT
   shrink the space WeasyPrint reserves for the table in the page's
   normal flow (confirmed by testing: a table scaled to 0.5 left
   about 23pt of extra empty space below it that an unscaled table
   didn't have). If that gap bothers you, close it by hand with a
   negative margin-bottom on the table roughly equal to
   (table's unscaled height) × (1 − scale) — there's no way to
   compute this automatically either; it's another value to tune by
   eye alongside --table-scale.

   zoom would avoid both of these problems (it reflows layout,
   unlike transform) but WeasyPrint doesn't support it at all
   ("Ignored `zoom`, unknown property" — tested directly).
   ------------------------------------------------------------ */
.full-width {
  --table-scale: 1;
  margin-left: -1.5cm;
  margin-right: -1.5cm;
}

.full-width table {
  transform: scale(var(--table-scale));
  transform-origin: top left;
}