/* ============================================================================
 * SwingInsight — Shared print stylesheet (Round-59)
 *
 * One source of truth for "Print → Save as PDF" output across every report
 * surface (main session report, training plan, Player Summary, portal silo
 * pages, one-pagers). Each shipping HTML file links this with
 * <link rel="stylesheet" href="/shared/print-styles.css" media="print">.
 *
 * Default screen styles win for screen viewing; only the @media print
 * block in here kicks in when the browser is rendering for print or
 * "Save as PDF". The block forces:
 *
 *   1. color-adjust:exact everywhere — preserves dark backgrounds,
 *      gradients, accent fills, badge colors, chart strokes that the
 *      browser would otherwise strip to save ink.
 *   2. Page sizing — US Letter portrait with 0.5in margins by default.
 *      Override per-surface with body[data-print-page=…] hooks.
 *   3. Screen-only chrome hidden — sidebars, top nav, action toolbars,
 *      modals, fixed banners, "Print" / "Share" / "Download" buttons,
 *      sticky tab strips.
 *   4. Sensible page-break rules — avoid breaking inside cards,
 *      drill blocks, charts; force major sections to start a new page.
 *   5. Anchor URLs not printed — `a[href]:after { content: "" }` so
 *      the report doesn't grow URL footers next to every link.
 *
 * If a surface needs a special tweak (e.g., training plan wants
 * landscape for the schedule grid), set `body.tp-print-landscape` (or
 * similar) and override below; do NOT scatter print-only CSS in the
 * surface files.
 * ============================================================================ */

@media print {

  /* ── 1. Color preservation — most important rule for our reports ────────
   *    Without this, Chrome / Edge strip backgrounds + gradients on the
   *    assumption you don't want to waste ink. Our reports rely heavily
   *    on dark-themed badges, gradient stat cards, color-coded cascades,
   *    and chart strokes — they all need to print as displayed.
   */
  *, *::before, *::after {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    color-adjust: exact !important;
  }

  /* ── 2. Page setup ────────────────────────────────────────────────────── */
  @page {
    size: Letter portrait;
    margin: 0.5in;
  }
  @page :first {
    /* Tighter first-page margin so cover hero fills the page. */
    margin: 0.4in;
  }

  /* Surface opt-in landscape for grid-heavy pages */
  body[data-print-orientation="landscape"] {
    /* @page rules can't be in body{} but the surface can set its own */
  }

  html, body {
    background: #ffffff !important;
    color: #0d1f3c !important;
    margin: 0 !important;
    padding: 0 !important;
    -webkit-font-smoothing: antialiased;
  }

  /* ── 3. Hide screen-only chrome ───────────────────────────────────────── */
  .topbar,
  .sidebar,
  nav.sidebar,
  aside.sidebar,
  .nav-item,
  .breadcrumb,
  .topbar-portal-link,
  .topbar-actions,
  .topbar-share-btn,
  .topbar-print-btn,
  .modal-overlay,
  .modal-card,
  .toast,
  .floating-action,
  .si-share-modal,
  #si-share-modal,
  .tp-share-modal,
  /* Action buttons inside reports — keep the underlying content visible
     but hide the controls themselves */
  .action-btn,
  .export-btn,
  .rpt-action-btn,
  .pgm-start-btn,
  .pgm-card-footer button,
  button.print-btn,
  button[data-print-hide="true"],
  [data-print-hide="true"],
  /* Tab strips on the main report — force "all panels" print mode */
  .report-tabs,
  .tp-print-controls,
  /* Sticky/fixed elements that misbehave on print */
  .sticky-banner {
    display: none !important;
  }

  /* ── 4. Force-show panels even if hidden on screen (tab UIs) ──────────── */
  /* The main report uses tab panels — only the active panel is visible on
     screen. For print, show them all so the document is complete. */
  .panel,
  .panel:not(.active),
  .tab-panel,
  .tab-panel:not(.active) {
    display: block !important;
    page-break-before: always;
    break-before: page;
  }
  .panel:first-of-type,
  .tab-panel:first-of-type {
    page-break-before: auto;
    break-before: auto;
  }

  /* ── 5. Layout — pull the main content out from under the sidebar ────── */
  .main-content,
  main.main-content,
  .page-container,
  .page.active {
    margin-left: 0 !important;
    padding: 0 !important;
    max-width: 100% !important;
    width: 100% !important;
  }
  .page {
    display: block !important;
  }

  /* ── 6. Page-break rules ──────────────────────────────────────────────── */
  /* Avoid breaking inside cards, drill rows, chart blocks, headlines */
  .card,
  .content-card,
  .stat-card,
  .pgm-card,
  .drill,
  .drill-card,
  .schedule-row,
  .ath-detail-panel,
  .rpt-detail-card,
  .cdash-stat,
  .pq,
  .whero,
  svg,
  table,
  pre,
  blockquote,
  figure,
  /* Cascade nodes from the main report should not split */
  .casc-node,
  .casc-chain {
    page-break-inside: avoid;
    break-inside: avoid;
  }

  h1, h2, h3, h4, h5, h6 {
    page-break-after: avoid;
    break-after: avoid;
    page-break-inside: avoid;
    break-inside: avoid;
  }

  /* Major sections start on a fresh page */
  .print-page-break,
  [data-print-page-break="before"] {
    page-break-before: always;
    break-before: page;
  }
  [data-print-page-break="after"] {
    page-break-after: always;
    break-after: page;
  }

  /* ── 7. Typography — pull point sizes down a touch for print ──────────── */
  body { font-size: 11pt; line-height: 1.45; }
  h1 { font-size: 22pt; }
  h2 { font-size: 18pt; }
  h3 { font-size: 14pt; }
  h4 { font-size: 12pt; }
  h5, h6 { font-size: 11pt; }
  .stat-value,
  .pgm-stat-val { font-size: 18pt !important; }
  .stat-label,
  .pgm-stat-label { font-size: 8pt !important; letter-spacing: 1.2px; }

  /* ── 8. Anchor URL suppression ───────────────────────────────────────── */
  /* By default browsers append "(http://…)" after every link in print —
     noisy and overflows lines. Suppress globally; if a surface truly
     needs URLs (e.g., a links-page page), opt back in via
     a.print-show-url { … }. */
  a[href]:after { content: "" !important; }
  a.print-show-url[href]:after { content: " (" attr(href) ")" !important; font-size: 0.85em; color: #71717a; }

  /* ── 9. Image / SVG / chart handling ───────────────────────────────── */
  img, svg, canvas {
    max-width: 100% !important;
    height: auto !important;
    /* Don't shrink-rasterize charts; preserve vector */
  }
  /* Charts often live in containers with fixed pixel widths set inline.
     Let them collapse to the print width. */
  .chart-container,
  .progress-chart,
  .trend-chart-container,
  #progress-chart-container,
  #trend-chart-area {
    width: 100% !important;
    max-width: 100% !important;
    overflow: visible !important;
  }

  /* ── 10. Dark-theme overrides for portal surfaces ─────────────────────
   *     The portal is dark-themed for screen but prints best on paper.
   *     We DON'T flip everything to white — gradient stat cards, cascade
   *     nodes, brand strips should keep their color. Only flip the
   *     plain text containers + page background.
   */
  body.portal-print,
  body[data-print-theme="paper"] {
    background: #ffffff !important;
    color: #0d1f3c !important;
  }
  body[data-print-theme="paper"] .text-primary,
  body[data-print-theme="paper"] .text-muted,
  body[data-print-theme="paper"] p,
  body[data-print-theme="paper"] li,
  body[data-print-theme="paper"] td,
  body[data-print-theme="paper"] dd {
    color: #0d1f3c !important;
  }
  body[data-print-theme="paper"] .card-bg,
  body[data-print-theme="paper"] .card,
  body[data-print-theme="paper"] .content-card,
  body[data-print-theme="paper"] .stat-card {
    background: #ffffff !important;
    border-color: #e5e0d8 !important;
    color: #0d1f3c !important;
    box-shadow: none !important;
  }

  /* ── 11. Tables — readable on paper ──────────────────────────────────── */
  table {
    border-collapse: collapse !important;
    width: 100% !important;
  }
  th, td {
    border: 0.5pt solid #cfcab8 !important;
    padding: 6pt 8pt !important;
    text-align: left;
    vertical-align: top;
  }
  thead { display: table-header-group; } /* repeat headers on each page */
  tfoot { display: table-footer-group; }

  /* ── 12. Utility hooks for per-surface tuning ─────────────────────────── */
  /* Per-surface hook so a page can say "for print, hide me" or
     "for print, show me" without bespoke selectors. */
  .screen-only { display: none !important; }
  .print-only { display: block !important; }
}

/* Outside @media print so a screen view can also opt in to the hook */
.print-only { display: none; }
