/**
 * RunMyCampus print grammar — Phase R (2026-05-12).
 *
 * Schools care about print. This sheet handles:
 *   - report cards / transcripts (.rmc-print-document)
 *   - invoices (.rmc-print-invoice)
 *   - tables: widow/orphan + repeating headers
 *   - color-safe output (force WebKit print-color adjustment)
 *   - hide UI chrome (.rmc-no-print)
 *
 * Consumers wrap printable content with class="rmc-print-document" so the print
 * sheet can target it and ignore surrounding shell chrome.
 */

@media print {
  /* Page geometry — A4 portrait by default; consumers can override @page in
     their own template <style> for landscape or US Letter. */
  @page {
    size: A4 portrait;
    margin: 16mm 14mm 18mm 14mm;
  }
  @page :first {
    margin-top: 12mm;
  }

  /* Reset surfaces to print-safe — pure white background, dark ink, no shadows.
     v3.55.0: override the .rmc-app-shell document lock so multi-page print works.
     Without this, html/body { overflow: hidden } from rmc-app-shell.css would
     prevent the browser from generating pages 2+. */
  html, body {
    background: #ffffff !important; /* off-token-allow: hex-literal-decorative */
    color: #000000 !important; /* off-token-allow: hex-literal-decorative */
    font-size: 11pt;
    line-height: 1.4;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    color-scheme: light;
    overflow: visible !important;
    height: auto !important;
  }

  /* Unfold the shell grid for print so the canvas body flows naturally
     across pages instead of staying confined to the viewport-height column. */
  .rmc-app-shell {
    display: block !important;
    height: auto !important;
    overflow: visible !important;
  }
  .rmc-app-shell__canvas,
  .rmc-app-shell__canvas-body {
    display: block !important;
    overflow: visible !important;
    height: auto !important;
    min-height: 0 !important;
  }

  /* Hide shell chrome by default. Pages that mark printable content use
     .rmc-print-document to opt the surface in. */
  .navbar, .topbar, .footer, .ai-copilot-wrapper, .rmc-cmdk,
  .rmc-section-nav, .rmc-shell-switcher, .toast-container,
  .breadcrumb, .skip-link, #cp-context-drawer-shell,
  .rmc-no-print, [data-rmc-no-print="1"], .btn-back-outline,
  .rmc-form-savebar, .htmx-progress-bar,
  .rmc-app-shell__header, .rmc-app-shell__sidebar, .rmc-app-shell__footer {
    display: none !important;
  }

  /* The printable surface. */
  .rmc-print-document, .rmc-print-invoice {
    background: #ffffff !important; /* off-token-allow: hex-literal-decorative */
    color: #000000 !important; /* off-token-allow: hex-literal-decorative */
    border: 0 !important;
    box-shadow: none !important;
    padding: 0 !important;
    margin: 0 !important;
    max-width: 100% !important;
    width: 100% !important;
  }

  /* Heading hierarchy — print-friendly type scale (pt, not rem). */
  .rmc-print-document h1, .rmc-print-invoice h1 { font-size: 18pt; margin: 0 0 4mm; }
  .rmc-print-document h2, .rmc-print-invoice h2 { font-size: 14pt; margin: 6mm 0 2mm; }
  .rmc-print-document h3, .rmc-print-invoice h3 { font-size: 12pt; margin: 4mm 0 1mm; }
  .rmc-print-document p, .rmc-print-invoice p { margin: 0 0 2mm; }

  /* Print-aware tables. Hairline grid is the only acceptable rule. */
  table.rmc-data-table,
  table.gradebook-table,
  .rmc-print-document table {
    border-collapse: collapse !important;
    width: 100%;
    border: 0 !important;
    background: transparent !important;
    page-break-inside: auto;
    box-shadow: none !important;
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
  }
  table.rmc-data-table thead,
  table.gradebook-table thead,
  .rmc-print-document table thead { display: table-header-group; }
  table.rmc-data-table tfoot,
  .rmc-print-document table tfoot { display: table-footer-group; }
  table.rmc-data-table thead th,
  table.gradebook-table thead th,
  .rmc-print-document table thead th {
    background: #f6f6f6 !important; /* off-token-allow: hex-literal-decorative */
    color: #111 !important; /* off-token-allow: hex-literal-decorative */
    border-bottom: 0.5pt solid #888 !important;
    font-size: 9pt;
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }
  table.rmc-data-table tbody td,
  table.gradebook-table tbody td,
  .rmc-print-document table tbody td {
    border-bottom: 0.5pt solid #ddd !important;
    page-break-inside: avoid;
    color: #000 !important; /* off-token-allow: hex-literal-decorative */
    background: transparent !important;
  }
  /* Widow/orphan defense — keep at least 3 rows on each page. */
  table.rmc-data-table tbody tr,
  .rmc-print-document table tbody tr {
    page-break-inside: avoid;
    orphans: 3;
    widows: 3;
  }

  /* Cards print as hairline-bordered blocks (no shadow / no backdrop blur). */
  .rmc-print-document .card,
  .rmc-print-document .rmc-empty {
    border: 0.5pt solid #ccc !important; /* off-token-allow: rgba-decorative */
    border-radius: 4pt !important;
    box-shadow: none !important;
    background: transparent !important;
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
    page-break-inside: avoid;
  }

  /* Invoice / report-card layouts: hard page-break utility. */
  .rmc-print-page-break { page-break-after: always; }
  .rmc-print-avoid-break { page-break-inside: avoid; }

  /* Anchors print without underline noise; show URL only for external links. */
  a[href^="http"]::after,
  a[href^="//"]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #555; /* off-token-allow: hex-literal-decorative */
  }
  a[href^="#"]::after,
  a[href^="mailto:"]::after,
  a[href^="tel:"]::after { content: ""; }

  /* Print signature / footer line for official docs. */
  .rmc-print-signature {
    margin-top: 18mm;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12mm;
    page-break-inside: avoid;
  }
  .rmc-print-signature__line {
    border-top: 0.5pt solid #000;
    padding-top: 2mm;
    font-size: 9pt;
    color: #444; /* off-token-allow: hex-literal-decorative */
  }

  /* Color-safe status badges — convert tinted backgrounds to hatched outlines. */
  .badge, .cell-fail, .cell-success, .cell-warning {
    background: transparent !important;
    color: #000 !important; /* off-token-allow: hex-literal-decorative */
    border: 0.5pt solid #555 !important; /* off-token-allow: rgba-decorative */
    padding: 0.5mm 1.5mm;
  }
}
