/* ImmoWise : documents imprimables (fiche notaire, reçu, contrats, devis, brochure).
   Un document = <article class="doc-sheet"> ; à l'écran il ressemble à une page A4, à l'impression il occupe la page. */

@page { size: A4; margin: 14mm 14mm 16mm; }

.doc-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 auto 16px; max-width: 210mm; }
.doc-actions .spacer { flex: 1; }
.doc-actions .button, .doc-actions button { margin: 0; }

.doc-sheet {
  --doc-accent: var(--accent, #0f766e);
  --doc-ink: #17242f; --doc-muted: #66727d; --doc-line: #d9e0e5; --doc-soft: #f4f7f8;
  position: relative; max-width: 210mm; min-height: 270mm; margin: 0 auto 24px; padding: 16mm 17mm 14mm;
  background: #fff; color: var(--doc-ink); border: 1px solid var(--line, #e3e8ec); border-radius: 4px;
  box-shadow: 0 10px 34px rgba(16, 32, 44, .08); font: 12.5px/1.55 "Segoe UI", system-ui, -apple-system, sans-serif;
  display: flex; flex-direction: column;
}
.doc-sheet * { box-sizing: border-box; }

/* En-tête : entreprise à gauche, titre et références à droite */
.doc-head { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 24px; align-items: end; padding-bottom: 14px; margin-bottom: 22px; border-bottom: 3px solid var(--doc-accent); }
.doc-brand img { display: block; max-width: 190px; max-height: 64px; object-fit: contain; }
.doc-brand .doc-company { font-size: 19px; font-weight: 800; letter-spacing: .02em; text-transform: uppercase; }
.doc-brand small { display: block; margin-top: 6px; color: var(--doc-muted); font-size: 11px; }
.doc-title { text-align: right; }
.doc-title h1 { margin: 0 0 6px; color: var(--doc-accent); font-size: 20px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; }
.doc-meta { display: inline-grid; grid-template-columns: auto auto; gap: 2px 12px; margin: 0; font-size: 11.5px; text-align: left; }
.doc-meta dt { color: var(--doc-muted); }
.doc-meta dd { margin: 0; font-weight: 600; }

/* Sections numérotées */
.doc-sheet { counter-reset: doc-section; }
.doc-section { margin: 0 0 18px; break-inside: avoid; page-break-inside: avoid; }
.doc-section > h2 {
  display: flex; align-items: center; gap: 10px; margin: 0 0 10px; padding: 6px 10px; border-radius: 4px;
  background: var(--doc-soft); color: var(--doc-ink); font-size: 12px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase;
}
.doc-section > h2::before {
  counter-increment: doc-section; content: counter(doc-section);
  display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; background: var(--doc-accent); color: #fff; font-size: 11px; letter-spacing: 0;
}
.doc-section > h3 { margin: 12px 0 6px; color: var(--doc-muted); font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; }

/* Champs « libellé : valeur » */
.doc-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 22px; margin: 0; }
.doc-fields.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.doc-fields > div { display: grid; grid-template-columns: 42% 1fr; gap: 8px; padding: 5px 0; border-bottom: 1px solid var(--doc-line); }
.doc-fields > div.wide { grid-column: 1 / -1; grid-template-columns: 21% 1fr; }
.doc-fields dt { color: var(--doc-muted); font-size: 11px; }
.doc-fields dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.doc-fields dd:empty::after, .doc-empty { content: "—"; color: #a7b1b9; font-weight: 400; }

/* Tableaux */
.doc-table, .doc-body table { width: 100%; margin: 4px 0 10px; border-collapse: collapse; font-size: 12px; }
.doc-table th, .doc-table td, .doc-body th, .doc-body td { padding: 6px 8px; border: 1px solid var(--doc-line); text-align: left; vertical-align: top; }
.doc-table thead th, .doc-body thead th { background: var(--doc-soft); color: var(--doc-muted); font-size: 10.5px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; }
.doc-table .num, .doc-body .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.doc-table tr.total td, .doc-body tr.total td { background: var(--doc-soft); font-weight: 800; }

/* Montants mis en avant */
.doc-amounts { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin: 6px 0 4px; }
.doc-amounts > div { padding: 9px 12px; border: 1px solid var(--doc-line); border-radius: 6px; }
.doc-amounts span { display: block; color: var(--doc-muted); font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; }
.doc-amounts strong { display: block; font-size: 15px; font-variant-numeric: tabular-nums; }
.doc-amounts .accent { border-color: var(--doc-accent); background: color-mix(in srgb, var(--doc-accent) 7%, #fff); }
.doc-words { margin: 6px 0 0; color: var(--doc-muted); font-style: italic; }

/* Corps libre (contrats) */
.doc-body { font-size: 12.5px; line-height: 1.65; text-align: justify; }
.doc-body h1 { margin: 0 0 14px; font-size: 17px; text-align: center; text-transform: uppercase; letter-spacing: .05em; }
.doc-body h2 { margin: 18px 0 8px; font-size: 13.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--doc-accent); }
.doc-body h3 { margin: 14px 0 6px; font-size: 12.5px; }
.doc-body p { margin: 0 0 9px; }
.doc-body ul, .doc-body ol { margin: 0 0 10px; padding-left: 22px; }
.doc-body blockquote { margin: 0 0 10px; padding: 8px 12px; border-left: 3px solid var(--doc-accent); background: var(--doc-soft); }
.doc-body hr { border: 0; border-top: 1px solid var(--doc-line); margin: 14px 0; }
.doc-body .missing { padding: 0 3px; background: #fff4d6; color: #8a5a00; border-radius: 3px; }
.doc-body .unknown { padding: 0 3px; background: #fdecea; color: #b42318; border-radius: 3px; font-family: Consolas, monospace; }

/* Signatures */
.doc-sign { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 24px; margin-top: auto; padding-top: 26px; break-inside: avoid; page-break-inside: avoid; }
.doc-sign > div { min-height: 96px; padding: 8px 10px; border: 1px dashed var(--doc-line); border-radius: 6px; }
.doc-sign strong { display: block; font-size: 11.5px; }
.doc-sign small { color: var(--doc-muted); font-size: 10.5px; }

/* Pied de page */
.doc-foot { display: flex; justify-content: space-between; gap: 12px; margin-top: 18px; padding-top: 8px; border-top: 1px solid var(--doc-line); color: var(--doc-muted); font-size: 10px; }

.doc-note { max-width: 210mm; margin: 0 auto 14px; padding: 8px 12px; border-radius: 6px; background: #fff8e6; color: #7a5300; font-size: 12px; }

@media (max-width: 720px) {
  .doc-sheet { min-height: 0; padding: 18px 16px; }
  .doc-head { grid-template-columns: 1fr; }
  .doc-title { text-align: left; }
  .doc-fields, .doc-fields.cols-3 { grid-template-columns: 1fr; }
  .doc-fields > div.wide { grid-template-columns: 42% 1fr; }
}

@media print {
  .doc-actions, .doc-note { display: none !important; }
  body:has(.doc-sheet) main > *:not(.doc-sheet) { display: none !important; }
  .doc-sheet { max-width: none; min-height: 0; margin: 0; padding: 0; border: 0; border-radius: 0; box-shadow: none; display: block; }
  .doc-sheet + .doc-sheet { break-before: page; page-break-before: always; }
  .doc-body .missing { background: none; color: inherit; }
  .doc-sign { margin-top: 26px; }
  .doc-section > h2, .doc-table thead th, .doc-amounts .accent, .doc-section > h2::before { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ---------- Éditeur de modèles de contrats ---------- */
.contract-workspace { display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 16px; align-items: start; }
.contract-edit { padding: 12px; }
.editor-toolbar { position: sticky; top: calc(var(--topbar-h, 64px) + 6px); z-index: 5; display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; padding: 8px; border: 1px solid var(--line); border-radius: var(--radius-sm, 7px); background: var(--surface, #fff); }
.editor-toolbar button { margin: 0; padding: 6px 10px; min-width: 36px; }
.editor-toolbar select { width: auto; margin: 0; }
.contract-page { min-height: 0; margin: 0; box-shadow: none; }
.editor-area { min-height: 420px; outline: none; }
.editor-area:focus { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--accent, #0f766e) 25%, transparent); border-radius: 4px; }
.contract-vars { position: sticky; top: calc(var(--topbar-h, 64px) + 6px); max-height: calc(100vh - var(--topbar-h, 64px) - 24px); overflow-y: auto; }
.contract-vars h2 { margin-top: 0; }
.contract-vars details { margin: 0 0 8px; }
.contract-vars summary { cursor: pointer; font-weight: 700; padding: 4px 0; }
.var-chip { display: block; box-sizing: border-box; width: 100%; white-space: normal; margin: 4px 0; padding: 6px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface-2, #f8fafb); color: var(--ink-2, #3c4c59); font: 500 12.5px/1.3 var(--font, sans-serif); text-align: left; cursor: pointer; }
.var-chip:hover { border-color: var(--accent); color: var(--accent); }
.preview-head { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; }
.preview-head h2 { flex: 1; margin: 0; }
.preview-head label { margin: 0; }
.preview-head button { margin: 0; }
.contract-preview { min-height: 0; margin-top: 12px; box-shadow: none; }
.legend-missing { padding: 0 4px; background: #fff4d6; color: #8a5a00; border-radius: 3px; }
.legend-unknown { padding: 0 4px; background: #fdecea; color: #b42318; border-radius: 3px; }
.empty-state { text-align: center; padding: 36px 24px; }
.empty-state form { display: inline-block; margin-top: 8px; }
@media (max-width: 960px) {
  .contract-workspace { grid-template-columns: 1fr; }
  .contract-vars { position: static; max-height: none; }
}
