/* ============================================================================
   SSM Site Profile — PDF export (browser print of the dedicated .sp-pdf DOM)
   Built by: pages/site_mon_sections/profile_pdf.py
   Print trigger: assets/basic_dash_prints/site_profile_pdf.js
   Layout only; no fixed heights except the map.
   ========================================================================== */

/* screen: the print DOM is never shown */
.sp-pdf { display: none; }
.sp-contents { display: contents; }

@media print {
  @page { size: A4 portrait; margin: 10mm; }

  * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  html, body { background: #fff !important; }

  /* PostScript-safe font: avoids Distiller crash when Inter is unavailable */
  .sp-pdf, .sp-pdf * {
    font-family: 'Segoe UI', -apple-system, Arial, Helvetica, sans-serif !important;
  }

  /* hide app chrome and every screen element of the page */
  .app-sidebar, .sidebar, .topnav, .navbar, .footer,
  .dash-debug-menu, .dash-undo-redo, .page-header,
  .site-profile-page > :not(.sp-pdf) {
    display: none !important;
  }
  .page, .content, .site-profile-page {
    position: static !important;
    max-width: none !important;
    width: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    background: #fff !important;
    box-shadow: none !important;
    border: 0 !important;
  }

  .sp-pdf {
    --sp-blue: #3f2b80;
    --sp-green: rgb(38, 168, 112);
    --sp-yellow: #FEBD33;
    --sp-red: rgb(228, 34, 95);
    --sp-muted: #6b7280;
    --sp-line: #E5E7EB;
    display: block;
    color: #303030;
    font-size: 8pt;
    line-height: 1.2;
  }

  /* ── header ───────────────────────────────────────────── */
  .sp-hdr {
    display: flex; justify-content: space-between; align-items: center;
    padding-bottom: 1.5mm; margin-bottom: 2mm;
    border-bottom: .7mm solid var(--sp-blue);
  }
  .sp-title { font-size: 16pt; font-weight: 700; color: #000; line-height: 1.2; }
  .sp-sub { font-size: 8.5pt; color: #666; margin-top: .8mm; }
  .sp-logos { display: flex; align-items: center; gap: 4mm; }
  .sp-logo { height: 9mm; width: auto; object-fit: contain; }

  /* ── top row: details | population | map (cards do NOT stretch) ── */
  .sp-top {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, .8fr) 48mm;   /* minmax(0,..): long content must not widen a column */
    gap: 4mm;
    align-items: start;
    margin-bottom: 2.5mm;
  }
  .sp-box-title {
    font-weight: 700; font-size: 9pt; color: var(--sp-blue);
    border-bottom: .3mm solid var(--sp-line);
    padding-bottom: .8mm; margin-bottom: 1mm;
  }
  .sp-sub-title { font-weight: 700; font-size: 7.5pt; color: var(--sp-blue); margin-top: 1mm; }

  /* Site Management Details key/value list (reuses screen markup) */
  .sp-pdf .card-table dl, .sp-pdf dl { margin: 0; }
  .sp-pdf .card-table-row {
    display: grid; grid-template-columns: minmax(25mm, 38mm) minmax(0, 1fr); gap: 0 2mm;
    padding: .35mm 0; border-bottom: .2mm solid var(--sp-line);
    break-inside: avoid;
  }
  .sp-pdf .card-table-row dt, .sp-pdf .card-table-row dd {
    margin: 0; font-size: 7.5pt; line-height: 1.25;
    overflow-wrap: anywhere; hyphens: auto;
  }
  .sp-pdf .card-table-row dt { color: var(--sp-muted); }

  /* Population stack: reuse screen KPI markup; keep the coloured pills (they break up the
     text), drop the proportion bars to save height */
  .sp-pop, .sp-pop div, .sp-pop span { font-size: 7.5pt; line-height: 1.25; }
  .sp-pop .card-pill-bar { display: none; }
  .sp-pop .card-bar-row { display: inline-flex; gap: 1mm; margin: 0 4mm 0 0; padding: 0; }
  .sp-pop .card-bar-label, .sp-pop .card-bar-value { margin: 0; padding: 0; }
  .sp-pop .card-pill { font-size: 7pt !important; padding: .2mm 1.6mm !important; line-height: 1.3; }
  .sp-pop div[style*="flex"] { margin: .5mm 0 0 !important; gap: 1mm !important; }
  .sp-sub-title { margin-top: 1mm; }

  /* the only fixed size: the map snapshot */
  .sp-map:not([src]), .sp-map-box:has(.sp-map:not([src])) { display: none; }   /* no snapshot -> collapse, no empty card */
  .sp-map { display: block; width: 48mm; height: 36mm; object-fit: cover; border: .2mm solid var(--sp-line); }

  /* ── indicators: one column, grid rows ────────────────── */
  .sp-ind-title { font-weight: 700; font-size: 10pt; color: var(--sp-blue); margin-bottom: .5mm; }
  .sp-colhead, .sp-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 28mm 36mm 5mm;
    gap: 2mm;
  }
  .sp-colhead {
    padding: .8mm 2mm; font-weight: 700; font-size: 8pt;
    border-bottom: .4mm solid var(--sp-blue);
  }
  .sp-sector { margin-top: 1.2mm; }
  .sp-sector h3 {
    break-after: avoid;                 /* never orphan a sector header */
    margin: 0; padding: .5mm 2mm; font-size: 8.5pt;
    background: #f1eff8; border-left: 1.2mm solid var(--sp-blue);
    display: flex; justify-content: space-between; gap: 3mm;
  }
  .sp-sector h3 em { font-style: normal; font-weight: 400; color: var(--sp-muted); font-size: 8pt; text-align: right; }
  .sp-row {
    align-items: start;
    padding: .45mm 2mm; border-bottom: .2mm solid var(--sp-line);
    break-inside: avoid;
    font-size: 8pt;
  }
  .sp-row > * { overflow-wrap: anywhere; hyphens: auto; }
  .sp-tgt { color: var(--sp-muted); }
  .sp-val { font-weight: 700; color: var(--sp-blue); }
  .sp-org { font-weight: 400; color: var(--sp-muted); font-size: 7pt; margin-left: 1mm; }
  /* status shades the Value cell only (not the whole row) */
  .sp-val { align-self: stretch; padding: .2mm 1.2mm; border-radius: .6mm; }   /* shades the full Value cell */
  .sp-row.sp-g .sp-val { background: rgba(38, 168, 112, .16); }
  .sp-row.sp-y .sp-val { background: rgba(254, 189, 51, .26); }
  .sp-row.sp-r .sp-val { background: rgba(228, 34, 95, .14); }
  .sp-dot { display: inline-block; width: 2.6mm; height: 2.6mm; border-radius: 50%; }
  .sp-row .sp-dot { margin-top: .3mm; justify-self: center; }
  .sp-dot.sp-g { background: var(--sp-green); }
  .sp-dot.sp-y { background: var(--sp-yellow); }
  .sp-dot.sp-r { background: var(--sp-red); }
  .sp-dot.sp-none { border: .3mm solid #9ca3af; }
  .sp-na { color: #9ca3af; text-align: center; line-height: 2.6mm; }
  .sp-empty { color: var(--sp-muted); padding: 2mm; }

  /* ── Area view: no status-dot column, wider Value cell holding a stacked proportion bar ── */
  .sp-area .sp-colhead, .sp-area .sp-row { grid-template-columns: minmax(0, 1fr) 28mm 64mm; }
  /* bar itself (.sbar*) is shared with the screen table: see styles.css */
  .sp-pdf { --sbar-h: 3.3mm; --sbar-font: 7pt; }
  .sp-valbar { align-self: start; display: flex; }          /* bar keeps its height when the label wraps */
  .sp-valbar .sbar-cell { width: 100%; }

  /* ── legend + footer ──────────────────────────────────── */
  .sp-legend {
    display: flex; gap: 5mm; align-items: center;
    margin: .5mm 0 1mm; font-size: 7.5pt; color: var(--sp-muted);
    break-inside: avoid;
  }
  .sp-legend .sp-dot { margin-right: 1mm; vertical-align: -.5mm; }
  .sp-foot { margin-top: 1.5mm; font-size: 7pt; color: #9ca3af; word-break: break-all; }
}
