/* Document rendering — shared by the on-screen preview and the ferrum PDF.
 *
 * One stylesheet, one source of truth: the editor preview cannot drift from
 * the generated PDF because they load the same rules. Values reference the
 * design tokens; nothing here hardcodes a brand colour.
 *
 * Layout uses `table`, not flex: Chrome's print pagination breaks flex rows
 * across pages unpredictably, which is what made multi-page contracts fall
 * apart. */

.doc {
  background: #fff;
  color: var(--color-text-primary);
  font-family: var(--font-sans);
  font-size: 10.5pt;
  line-height: var(--leading-normal);
}

/* Screen preview only: an A4-proportioned sheet. The PDF sets its own page
 * box, so this padding is neutralised in print (see @media print below). */
.doc-sheet {
  width: 100%;
  max-width: 210mm;
  margin: 0 auto;
  padding: 18mm 16mm;
  box-sizing: border-box;
}

.doc-block { margin-bottom: 22px; }
.doc-block:last-child { margin-bottom: 0; }

/* A clause, a signature panel or a totals table must never be split across
 * two pages — that was the single worst artefact of the old renderer. */
.doc-block--atomic { break-inside: avoid; page-break-inside: avoid; }

.doc-pagebreak { break-after: page; page-break-after: always; height: 0; }

/* ---- Headings ----------------------------------------------------------- */
.doc-h {
  font-size: 12pt;
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-tight);
  margin: 0 0 8px;
}
.doc-h--num::before {
  content: attr(data-num) ". ";
  color: var(--color-accent);
}
.doc-eyebrow {
  font-size: 7.5pt;
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--color-text-secondary);
  margin-bottom: 6px;
}
.doc-p { margin: 0 0 8px; }
.doc-p:last-child { margin-bottom: 0; }
.doc-muted { color: var(--color-text-secondary); }
.doc-list { margin: 0; padding-left: 16px; }
.doc-list li { margin-bottom: 4px; }

/* ---- Cover page (contratto) --------------------------------------------- */
.doc-cover { min-height: 232mm; position: relative; }
.doc-cover-panel {
  background: var(--ls-graphite);
  color: var(--ls-linen);
  padding: 24mm 18mm 30mm;
  text-align: right;
}
.doc-cover-date { font-size: 10pt; font-style: italic; opacity: .85; margin-bottom: 26mm; }
.doc-cover-title {
  font-family: var(--font-serif-accent);
  font-size: 34pt;
  line-height: 1.05;
  letter-spacing: var(--tracking-tight);
  margin: 0 0 14mm;
}
.doc-cover-for { font-size: 11pt; font-weight: var(--weight-semibold); }
.doc-cover-addr { font-size: 9.5pt; font-style: italic; opacity: .8; margin-top: 2px; }
.doc-cover-rule { border: 0; border-top: 1px solid rgba(255,255,255,.35); margin: 6mm 0; }
.doc-cover-sub { font-size: 10pt; opacity: .9; }
.doc-cover-foot {
  display: table; width: 100%; margin-top: 12mm;
  font-size: 9.5pt;
}
.doc-cover-foot > div { display: table-cell; vertical-align: bottom; }

/* ---- Letterhead (fattura / offerta) ------------------------------------- */
.doc-head { display: table; width: 100%; margin-bottom: 10mm; }
.doc-head > div { display: table-cell; vertical-align: top; }
.doc-head-right { text-align: right; }
.doc-logo { height: 34px; width: auto; }
.doc-title {
  font-size: 20pt;
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-tight);
  text-transform: uppercase;
}
.doc-numero {
  font-size: 13pt;
  font-weight: var(--weight-bold);
  color: var(--color-accent);
  margin-top: 2px;
}

/* ---- Parties ------------------------------------------------------------ */
.doc-parties { display: table; width: 100%; border-spacing: 10px 0; margin: 0 -10px; }
.doc-party {
  display: table-cell;
  width: 50%;
  vertical-align: top;
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}
.doc-party-name { font-weight: var(--weight-semibold); font-size: 11pt; margin-bottom: 2px; }
.doc-kv { font-size: 9.5pt; color: var(--color-text-secondary); }
.doc-kv dt { display: inline; font-weight: var(--weight-semibold); }
.doc-kv dd { display: inline; margin: 0; }
.doc-kv div { margin-top: 2px; }

/* ---- Tables ------------------------------------------------------------- */
.doc-table { width: 100%; border-collapse: collapse; margin-top: 6px; }
.doc-table thead { display: table-header-group; } /* repeat header across pages */
.doc-table th {
  text-align: left;
  padding: 8px 10px;
  font-size: 7.5pt;
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--color-text-secondary);
  border-bottom: 1px solid var(--color-border-default);
}
.doc-table td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--color-border-subtle);
  vertical-align: top;
}
.doc-table tr { break-inside: avoid; page-break-inside: avoid; }
.doc-num { text-align: right; white-space: nowrap; }
.doc-line-desc { font-weight: var(--weight-semibold); }
.doc-line-detail { font-size: 9pt; color: var(--color-text-secondary); margin-top: 2px; }

/* ---- Totals ------------------------------------------------------------- */
.doc-totals { width: 55%; margin-left: 45%; border-collapse: collapse; }
.doc-totals td { padding: 5px 10px; }
.doc-totals .doc-total-row td {
  border-top: 2px solid var(--color-text-primary);
  font-weight: var(--weight-bold);
  font-size: 12pt;
  padding-top: 8px;
}
.doc-scadenza {
  margin-top: 8px;
  padding: 10px 14px;
  background: var(--color-bg-muted);
  border-radius: var(--radius-sm);
  font-size: 9.5pt;
}

/* ---- Payment ------------------------------------------------------------ */
.doc-pay {
  border: 1px solid var(--color-border-subtle);
  border-radius: var(--radius-md);
  padding: 12px 14px;
}

/* ---- Signatures --------------------------------------------------------- */
.doc-signs { display: table; width: 100%; border-spacing: 16px 0; margin: 8mm -16px 0; }
.doc-sign { display: table-cell; width: 50%; vertical-align: top; }
.doc-sign-box {
  height: 26mm;
  border: 1px dashed var(--color-border-default);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
}
.doc-sign-name { font-weight: var(--weight-bold); font-size: 11pt; }

.doc-footnote {
  margin-top: 10mm;
  padding-top: 8px;
  border-top: 1px solid var(--color-border-subtle);
  font-size: 8.5pt;
  color: var(--color-text-secondary);
}

@media print {
  .doc-sheet { max-width: none; padding: 0; margin: 0; }
  /* The cover panel bleeds to the page edge; ferrum is told to print
     backgrounds, so this survives into the PDF. */
  .doc-cover-panel { margin: -12mm -14mm 0; }
}
