/* =============================================================================
   TerraTrack — Design PDF premium partagé (TD-031 / TD-023)
   =============================================================================
   Langage visuel commun aux documents commerciaux imprimables :
     - Audit Parc PL          (#pa-pdf  → src/modules/parc_audit_pdf.js)
     - Simulateur budgétaire  (#sim-pdf → src/modules/simulation_pdf.js)

   Intégré depuis une maquette « Claude Design » : feuille A4, neutre chaud +
   accent encre, carte hero noire. Tout est SCOPÉ sous `.ap-*` pour ne jamais
   fuiter dans l'app (pas de `table`/`.panel` génériques) ni se faire clobber par
   le design system (`.ap-table` 0,0,1,0 bat `table` 0,0,0,1).

   IMPRESSION : page nommée `premium` (margin:0) assignée à `.ap-sheet` via la
   propriété `page` → la feuille est plein cadre A4 SANS toucher au `@page` global
   (12mm) dont dépendent les autres documents (feuille de tournée, fiche client).
   L'isolation print (styles/print.css : `body > :not(.pdfpage):not(.fiche-pdf)`)
   reste la garde-fou qui masque l'app ; ce fichier ne fait que la mise en forme.
   ========================================================================= */

/* ---------- Palette (scopée à la feuille, ne fuite pas en :root) ---------- */
.ap-sheet {
  --ap-bg: #fafaf9;
  --ap-ink: #0a0b0d;
  --ap-ink-2: #3d3f46;
  --ap-ink-3: #6b6d75;
  --ap-ink-4: #9b9da3;
  --ap-surface: #ffffff;
  --ap-surface-2: #f5f5f4;
  --ap-surface-3: #efeeec;
  --ap-border: #e8e7e4;
  --ap-border-strong: #d9d8d4;
  --ap-divider: #efeeec;
  --ap-accent: #2563eb;
  --ap-accent-press: #1e40af;
  --ap-accent-soft: #eff4ff;
  --ap-accent-border: #c7d7fb;
  --ap-success: #15803d;
  --ap-success-soft: #ecfdf3;
  --ap-success-border: #bbf0cb;
  --ap-warn: #b45309;
  --ap-rech: #2563eb;   /* levier rechapage */
  --ap-presta: #0a0b0d; /* levier prestation */
  --ap-font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.ap-num { font-variant-numeric: tabular-nums; }

/* ---------- Hôte écran : fond chaud + feuille centrée (aperçu) ---------- */
.ap-pdf-overlay { background: #ecebe8 !important; }
.ap-pdf-host {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 8px 0 24px;
}

/* ---------- Feuille A4 ---------- */
.ap-sheet {
  box-sizing: border-box;
  width: 210mm;
  min-height: 296mm;
  background: var(--ap-surface);
  color: var(--ap-ink);
  padding: 13mm 13mm 9mm;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 40px rgba(15, 17, 21, 0.14);
  position: relative;
  font-family: var(--ap-font);
  -webkit-font-smoothing: antialiased;
}
.ap-sheet *,
.ap-sheet *::before,
.ap-sheet *::after { box-sizing: border-box; }

/* ---------- Brand topbar ---------- */
.ap-brandbar { display: flex; align-items: center; justify-content: space-between; padding-bottom: 9px; border-bottom: 1px solid var(--ap-border); }
.ap-brand { display: flex; align-items: center; gap: 10px; }
.ap-brand-mark { width: 30px; height: 30px; border-radius: 7px; background: var(--ap-ink); color: #fff; display: grid; place-items: center; font-size: 11px; font-weight: 700; letter-spacing: 0.04em; }
.ap-brand-name { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; line-height: 1; }
.ap-brand-sub { font-size: 10px; color: var(--ap-ink-3); letter-spacing: 0.04em; margin-top: 3px; }
.ap-brandbar-right { text-align: right; font-size: 10px; color: var(--ap-ink-3); line-height: 1.5; }
.ap-brandbar-right b { color: var(--ap-ink-2); font-weight: 600; }

/* ---------- Title + hero ---------- */
.ap-head { display: grid; grid-template-columns: 1fr 300px; gap: 16px; align-items: stretch; padding: 16px 0 18px; }
.ap-eyebrow { font-size: 10px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: var(--ap-ink-3); }
.ap-client { font-size: 32px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.05; margin: 8px 0 6px; }
.ap-client-meta { font-size: 12.5px; color: var(--ap-ink-3); }
.ap-head-tags { display: flex; gap: 6px; margin-top: 14px; flex-wrap: wrap; }

.ap-hero { background: var(--ap-ink); color: #fff; border-radius: 10px; padding: 18px 20px; display: flex; flex-direction: column; justify-content: space-between; }
.ap-hero-label { font-size: 10px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: rgba(255, 255, 255, 0.55); }
.ap-hero-value { font-size: 52px; font-weight: 700; letter-spacing: -0.03em; line-height: 1; margin: 6px 0; }
.ap-hero-value.is-eur { font-size: 36px; } /* montants € (simulateur) : tient dans la carte */
.ap-hero-value span { font-size: 18px; font-weight: 500; color: rgba(255, 255, 255, 0.6); letter-spacing: 0; margin-left: 6px; }
.ap-hero-foot { display: flex; align-items: flex-end; justify-content: space-between; border-top: 1px solid rgba(255, 255, 255, 0.14); padding-top: 10px; margin-top: 4px; }
.ap-hero-foot .l { font-size: 10px; letter-spacing: 0.1em; text-transform: uppercase; color: rgba(255, 255, 255, 0.5); }
.ap-hero-foot .r { text-align: right; font-size: 11px; color: rgba(255, 255, 255, 0.85); line-height: 1.4; }
.ap-hero-foot .r b { font-weight: 600; }

/* ---------- Badges ---------- */
.ap-badge { display: inline-flex; align-items: center; gap: 5px; height: 20px; padding: 0 9px; border-radius: 999px; font-size: 10.5px; font-weight: 600; letter-spacing: 0.02em; border: 1px solid var(--ap-border-strong); background: var(--ap-surface-2); color: var(--ap-ink-2); }
.ap-badge .ap-dot { width: 6px; height: 6px; border-radius: 50%; }
.ap-badge-accent { background: var(--ap-accent-soft); border-color: var(--ap-accent-border); color: var(--ap-accent-press); }
.ap-badge-success { background: var(--ap-success-soft); border-color: var(--ap-success-border); color: var(--ap-success); }

/* ---------- Section ---------- */
.ap-section { margin-top: 4px; }
.ap-section-label { font-size: 10.5px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ap-ink-3); margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
.ap-section-label::after { content: ''; flex: 1; height: 1px; background: var(--ap-divider); }

/* ---------- Panels + tables ---------- */
.ap-panel { border: 1px solid var(--ap-border); border-radius: 9px; overflow: hidden; background: var(--ap-surface); }
.ap-table { width: 100%; border-collapse: collapse; font-size: 12.5px; color: var(--ap-ink); }
.ap-table thead th { font-size: 10px; font-weight: 600; letter-spacing: 0.07em; text-transform: uppercase; color: var(--ap-ink-3); text-align: right; padding: 9px 14px; background: var(--ap-surface-2); border-bottom: 1px solid var(--ap-border); }
.ap-table thead th:first-child { text-align: left; }
.ap-table tbody td { padding: 11px 14px; text-align: right; border-bottom: 1px solid var(--ap-divider); }
.ap-table tbody td:first-child { text-align: left; font-weight: 600; }
.ap-table tbody tr:last-child td { border-bottom: none; }
.ap-table tfoot td { padding: 10px 14px; text-align: right; font-weight: 700; background: var(--ap-surface-2); border-top: 1px solid var(--ap-border); }
.ap-table tfoot td:first-child { text-align: left; font-size: 10px; font-weight: 600; letter-spacing: 0.07em; text-transform: uppercase; color: var(--ap-ink-3); }

/* ---------- Gauges ---------- */
.ap-gauges { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 6px; }
.ap-gauge { border: 1px solid var(--ap-border); border-radius: 9px; padding: 14px 16px; background: var(--ap-surface); }
.ap-gauge-top { display: flex; align-items: center; justify-content: space-between; }
.ap-gauge-name { font-size: 13px; font-weight: 600; }
.ap-gauge-cible { font-size: 10.5px; color: var(--ap-ink-3); }
.ap-gauge-cible b { color: var(--ap-ink-2); font-weight: 600; }
.ap-gauge-val { display: flex; align-items: baseline; gap: 8px; margin: 8px 0 10px; }
.ap-gauge-pct { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; line-height: 1; }
.ap-gauge-frac { font-size: 11.5px; color: var(--ap-ink-3); }
.ap-bar { position: relative; height: 7px; background: var(--ap-surface-3); border-radius: 999px; }
.ap-bar-fill { position: absolute; inset: 0 auto 0 0; background: var(--ap-accent); border-radius: 999px; }
.ap-bar-target { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ap-warn); border-radius: 2px; }
.ap-bar-target::after { content: 'Cible'; position: absolute; top: -13px; left: 50%; transform: translateX(-50%); font-size: 8px; font-weight: 600; color: var(--ap-warn); letter-spacing: 0.04em; white-space: nowrap; }
.ap-gauge-opp { margin-top: 12px; display: inline-flex; align-items: center; gap: 7px; font-size: 11.5px; font-weight: 600; color: var(--ap-accent-press); background: var(--ap-accent-soft); border: 1px solid var(--ap-accent-border); border-radius: 7px; padding: 7px 11px; width: 100%; }
.ap-gauge-opp.is-ok { color: var(--ap-success); background: var(--ap-success-soft); border-color: var(--ap-success-border); }
.ap-gauge-opp .big { font-size: 13px; }

/* ---------- Lower two-column zone ---------- */
.ap-lower { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 6px; }

/* Répartition (stacked bar) */
.ap-split { display: flex; height: 10px; border-radius: 999px; overflow: hidden; margin-bottom: 8px; background: var(--ap-surface-3); }
.ap-split-seg { height: 100%; }
.ap-legend { display: flex; gap: 16px; font-size: 11px; color: var(--ap-ink-2); margin-bottom: 12px; flex-wrap: wrap; }
.ap-legend span { display: inline-flex; align-items: center; gap: 6px; }
.ap-legend .ap-dot { width: 8px; height: 8px; border-radius: 50%; }
.ap-legend b { font-weight: 600; }

.ap-seg-rech { background: var(--ap-rech); }
.ap-seg-presta { background: var(--ap-presta); }

.ap-callout { margin-top: 10px; display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--ap-ink-2); background: var(--ap-surface-2); border: 1px solid var(--ap-border); border-radius: 8px; padding: 10px 12px; line-height: 1.4; }
.ap-callout b { color: var(--ap-ink); font-weight: 700; }

/* Économie (stat chips) */
.ap-eco-chips { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.ap-eco-chip { border-radius: 9px; padding: 12px 14px; border: 1px solid var(--ap-border); }
.ap-eco-chip.is-soft { background: var(--ap-accent-soft); border-color: var(--ap-accent-border); }
.ap-eco-chip.is-dark { background: var(--ap-ink); border-color: var(--ap-ink); }
.ap-eco-chip .k { font-size: 9.5px; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ap-ink-3); }
.ap-eco-chip.is-soft .k { color: var(--ap-accent-press); }
.ap-eco-chip.is-dark .k { color: rgba(255, 255, 255, 0.55); }
.ap-eco-chip .v { font-size: 21px; font-weight: 700; letter-spacing: -0.02em; margin-top: 5px; line-height: 1; }
.ap-eco-chip.is-soft .v { color: var(--ap-accent-press); }
.ap-eco-chip.is-dark .v { color: #fff; }

.ap-accent-num { color: var(--ap-accent-press); font-weight: 600; }

/* ---------- Footer ---------- */
.ap-foot { margin-top: auto; padding-top: 12px; }
.ap-disclaimer { font-size: 9.5px; line-height: 1.5; color: var(--ap-ink-4); border-top: 1px solid var(--ap-divider); padding-top: 10px; }
.ap-disclaimer b { color: var(--ap-ink-3); font-weight: 600; }
.ap-foot-bar { display: flex; align-items: center; justify-content: space-between; margin-top: 10px; font-size: 10px; color: var(--ap-ink-3); gap: 10px; }
.ap-foot-bar .l { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ap-foot-bar .l b { color: var(--ap-ink-2); font-weight: 600; }
.ap-foot-sep { width: 3px; height: 3px; border-radius: 50%; background: var(--ap-border-strong); }

/* ---------- Impression : page A4 plein cadre, scopée à la feuille ---------- */
@page premium { size: A4 portrait; margin: 0; }

@media print {
  .ap-pdf-overlay { background: #fff !important; }
  .ap-pdf-host { display: block; padding: 0; }
  .ap-sheet {
    page: premium;
    width: 210mm;
    min-height: 0;
    box-shadow: none !important;
    margin: 0 auto;
  }
  /* Multi-feuille (simulateur) : chaque feuille suivante démarre une nouvelle
     page physique. La 1re n'a pas de saut → pas de page blanche en tête. */
  .ap-sheet + .ap-sheet { break-before: page; page-break-before: always; }
  /* Couleurs pleines préservées (hero/eco encre) — print.css pose déjà
     -webkit-print-color-adjust:exact globalement, on le renforce localement. */
  .ap-sheet,
  .ap-sheet * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
}

/* ---------- Adaptation mobile de l'aperçu écran (overlay <1024px) ---------- */
@media screen and (max-width: 800px) {
  .ap-pdf-host { padding: 0; overflow-x: auto; }
  .ap-sheet { box-shadow: 0 4px 18px rgba(15, 17, 21, 0.12); }
}
