/* ============================================================
   Kontor · Belege — A4-Briefbogen für die PDF-Erzeugung.

   Taken over from EENetzwerk's consultant area (pdf_sales_document.css)
   so the letterhead, its page geometry and the fold marks stay the ones
   checked there; only the typeface is Kontor's (Source Sans 3).

   Shared by every sales document: Angebot, Auftragsbestätigung and
   Rechnung all render through sales/documents/_paper, so nothing in
   here is specific to one of them.

   Ported from redesign-files/angebote/angebot-vorschau.html, hence
   the ".ap-*" prefix. The same sheet renders in two places, so the
   styles are self-contained (own token block, no Tailwind dependency):

   1. The consultant-facing preview page (inside the app chrome),
      where it sits on a muted stage with a paper drop-shadow.
   2. The bare token page that Gotenberg turns into the PDF,
      where only the paper itself is printed at A4 scale.

   The typeface comes from Google Fonts in layouts/pdf; Gotenberg waits
   for the network to settle before it prints.
   ============================================================ */

:root {
  /* Surfaces (warm neutral) */
  --ap-bg-canvas: #ffffff;
  --ap-bg-muted: #efeee8;

  /* Text — muted/subtle kept dark enough to stay legible in print. */
  --ap-fg: #0f0f0e;
  --ap-fg-muted: #57564f;
  --ap-fg-subtle: #7a7970;

  /* Borders */
  --ap-border: #e7e5de;
  --ap-border-strong: #d4d2c9;

  /* Families — sans throughout; numbers rely on tabular-nums for alignment
     rather than a monospace face, which proved hard to read in print. */
  --ap-font-sans: "Source Sans 3", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --ap-font-display: "Source Sans 3", -apple-system, sans-serif;

  /* Page geometry — shared by the embedded preview box and the paginated
     print/PDF flow so both keep identical margins. The footer band is the
     vertical space reserved at the bottom of every printed page for the
     repeating letterhead footer (its constant height + breathing room). */
  --ap-page-pad-x: 18mm;
  --ap-page-pad-top: 18mm;
  --ap-page-pad-bottom: 14mm;
  /* The header band repeats on every page (logo + issuer in the thead).
     Fixed so the fold marks — anchored below it — land at deterministic mm
     offsets, and so the reserved top space is identical on every sheet.
     = pad-top (18mm) + logo height (44px ≈ 11.6mm) + breathing room below the
     header so the repeated table head on page 2+ is not cramped against it. */
  --ap-head-h: 40mm;
  /* Worst-case footer height: the fullest column is ~6 lines at 8px/1.55
     plus the 9px top padding ≈ 22mm. The footer sticks to the sheet bottom
     (bottom:0 of the page-margin box); the band reserved by @page
     margin-bottom must exceed that height by a clear gap so the flowing
     content never butts up against the footer (the report PDFs follow the
     same "rather leave air at the bottom than clip" rule). */
  --ap-footer-h: 22mm;
  --ap-footer-gap: 8mm;
  --ap-footer-band: calc(var(--ap-footer-h) + var(--ap-footer-gap) + var(--ap-page-pad-bottom));
}

/* ---------- Eingebettete Vorschau-Hülle ----------
   On-screen preview (consultant preview page + public portal). The same paper
   markup renders here as ONE continuous A4-wide sheet — page breaks, the
   repeating header/footer and the fixed footer only apply in the PDF render
   (.ap-pdf). The .ap-paper wrapper is the single white sheet that holds both
   the table and the footer, floating on the muted backdrop with a shadow. */
.ap-stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--ap-bg-muted);
  padding: 36px 24px 72px;
}
.ap-stage .ap-paper {
  width: 210mm;
  /* Fill at least one A4 sheet so a short document reads as a full page (with
     white space below), not a cramped strip — this is the main reason the
     seamless preview felt "off". Longer documents grow past it as one reel; the
     "pages may differ" notice in the views covers the missing page breaks. */
  min-height: 297mm;
  display: flex;
  flex-direction: column;
  background: var(--ap-bg-canvas);
  box-shadow: 0 18px 50px -14px rgba(0, 0, 0, 0.4);
}

/* In the PDF the wrapper is inert — the table is the sheet and the footer is
   fixed; the wrapper must add no width/background/shadow there. */
.ap-pdf .ap-paper {
  display: contents;
}

/* ---------- A4-Papier (maßstabgetreu) ----------

   The paper is a single <table class="ap-sheet"> per logical document:

     <thead> = the letterhead head (logo + issuer). The print engine repeats
               it at the top of every page the table spans.
     <tbody> = the flowing content (address, line-item table, totals, terms,
               foot text). Chrome fragments it across A4 sheets.
     <tfoot> = an invisible spacer. The print engine repeats it at the bottom
               of every page AND reserves its height as flow space, keeping
               content clear of the fixed letterhead footer (.ap-footer),
               which is painted at the sheet bottom on every page.

   This renders via Gotenberg into the PDF. The consultant preview simply
   embeds that generated PDF in an iframe, so it is identical by construction
   (no second pagination engine). */
.ap-sheet {
  width: 210mm;
  border-collapse: collapse;
  background: var(--ap-bg-canvas);
  color: var(--ap-fg);
  font-family: var(--ap-font-sans);
  font-size: 11px;
  line-height: 1.5;
}

/* Header cell — repeats on every page (thead). Fixed height so the band it
   reserves is identical on each sheet and the fold marks below it land at
   deterministic offsets. Carries the page's top + side insets. */
.ap-sheet > thead > tr > .ap-head-cell {
  /* A table cell's height leaves its padding out: without subtracting the
     top inset the band came to 58mm, and the address and the fold marks sat
     18mm too low (Kontor). */
  height: calc(var(--ap-head-h) - var(--ap-page-pad-top));
  padding: var(--ap-page-pad-top) var(--ap-page-pad-x) 0;
  vertical-align: top;
}

/* The body cell carries the side insets only (the header above supplies the
   top inset). position:relative anchors the absolute fold marks. The repeating
   tfoot spacer below keeps content clear of the fixed footer, so the body
   needs no bottom inset or min-height of its own. */
.ap-sheet > tbody > tr > .ap-body {
  padding: 0 var(--ap-page-pad-x);
  vertical-align: top;
  position: relative;
}

/* @page only fixes the sheet size; all margins are modelled by the table
   cells + the fixed footer so the fold marks and footer share one system. */
@page {
  size: A4 portrait;
  margin: 0;
}

/* ----- Footer: paged in the PDF, in-flow in the on-screen preview -----

   In the PDF (.ap-pdf) the visible footer is fixed to the sheet bottom and
   repeats on every page; the invisible tfoot spacer reserves matching flow
   space so content never runs under it.

   On screen (.ap-stage) there are no pages — the paper is one long sheet — so
   the footer simply flows at the end of the content and the spacer collapses. */
.ap-pdf .ap-sheet > tfoot > tr > .ap-foot-spacer {
  height: var(--ap-footer-band);
  padding: 0;
}
.ap-pdf .ap-footer {
  position: fixed;
  left: var(--ap-page-pad-x);
  right: var(--ap-page-pad-x);
  bottom: var(--ap-page-pad-bottom);
}

/* On screen the footer is the bottom of the single sheet (the .ap-paper wrapper
   supplies the white background + shadow). margin-top:auto pins it to the foot
   of the ≥A4 sheet; padding-top guarantees air above it even on a full page. */
.ap-stage .ap-footer {
  margin-top: auto;
  padding-top: var(--ap-footer-gap);
  padding-left: var(--ap-page-pad-x);
  padding-right: var(--ap-page-pad-x);
  padding-bottom: var(--ap-page-pad-bottom);
}
/* Keep the table at its natural height inside the flex column (don't stretch). */
.ap-stage .ap-sheet {
  flex: 0 0 auto;
}

/* Avoid splitting atomic blocks across a page boundary. A line-item row,
   the totals box and each terms block must stay whole; legacy aliases for
   engines that only honour page-break-*. */
.ap-pos-row,
.ap-sub-row,
.ap-totals,
.ap-totals-box,
.ap-terms-block {
  break-inside: avoid;
  page-break-inside: avoid;
}

/* Repeat the line-item table's column header on every page it spans. */
.ap-table thead {
  display: table-header-group;
}

/* The signature/acceptance record always starts on a fresh sheet. The
   repeating thead supplies the top inset, so no extra padding is needed. */
.ap-signature-page {
  break-before: page;
  page-break-before: always;
}

/* On screen there is no page break, so the acceptance record would sit right
   under the document content. Give it clear separation: air above plus a divider,
   so it reads as its own section (the PDF gets a real new page instead). */
.ap-stage .ap-signature-page {
  margin-top: 20mm;
  padding-top: 14mm;
  border-top: 1px solid var(--ap-border);
}

/* DIN-Falzmarken (Fensterumschlag) — anchored to the first page's body cell.
   The body cell sits directly below the (fixed-height) header band, so its top
   is --ap-head-h from the sheet edge. The marks subtract that to land at their
   true mm offsets from the sheet top, flush on the left edge. They live within
   the first sheet, so they show on page 1 only (absolute, not fixed). */
.ap-foldmarks {
  position: absolute;
  left: 0;
  top: 0;
}
.ap-fm {
  position: absolute;
  left: 0;
  border-top: 1px solid var(--ap-border-strong);
}
.ap-fm-1 {
  top: calc(105mm - var(--ap-head-h));
  width: 5mm;
}
.ap-fm-loch {
  top: calc(148.5mm - var(--ap-head-h));
  width: 7mm;
}
.ap-fm-2 {
  top: calc(210mm - var(--ap-head-h));
  width: 5mm;
}

/* Kopf */
.ap-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
.ap-logo {
  height: 44px;
  width: auto;
  display: block;
}
.ap-head-issuer {
  text-align: right;
}
.ap-issuer-name {
  font-family: var(--ap-font-display);
  font-size: 13px;
  font-weight: 600;
  color: var(--ap-fg);
}
.ap-issuer-tag {
  font-size: 9.5px;
  color: var(--ap-fg-muted);
  margin-top: 2px;
}

/* Adresse + Info — DIN 5008 Form B, for a DL window envelope (Kontor).
   The body starts below the 40mm header band, so 5mm more puts the address
   field at 45mm from the sheet top; 2mm on top of the 18mm page inset puts
   it 20mm from the left edge. The field is 85 × 45mm: the 5mm return line
   on top, the recipient below — all of it inside the window once the sheet
   is folded at the 105mm mark. The info block starts at 50mm. */
.ap-address-zone {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-top: calc(45mm - var(--ap-head-h));
}
.ap-addr {
  box-sizing: border-box;
  width: 85mm;
  height: 45mm;
  margin-left: calc(20mm - var(--ap-page-pad-x));
  overflow: hidden;
}
.ap-sender-line {
  /* Muted (not subtle) so it stays legible when printed. */
  height: 5mm;
  display: flex;
  align-items: flex-end;
  white-space: nowrap;
  overflow: hidden;
  font-size: 7.5px;
  color: var(--ap-fg-muted);
  text-decoration: underline;
  text-underline-offset: 2px;
  margin-bottom: 3mm;
}
.ap-recipient {
  line-height: 1.55;
}
.ap-recipient .nm {
  font-weight: 600;
}
.ap-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 64mm;
  margin-top: 5mm;
}
.ap-info-row {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 2px 0;
}
.ap-info-row .k {
  color: var(--ap-fg-muted);
}
.ap-info-row .v {
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--ap-fg);
}
/* The document number is its primary identifier — give it weight. */
.ap-info-row.is-primary {
  padding-top: 0;
}
.ap-info-row.is-primary .k {
  align-self: center;
}
.ap-info-row.is-primary .v {
  font-size: 12px;
  font-weight: 600;
  color: var(--ap-fg);
}

/* Titel */
.ap-title-zone {
  /* Right below the address field (ending at 90mm), so the subject sits
     just above the first fold mark. */
  margin-top: 6mm;
}
.ap-title {
  font-family: var(--ap-font-display);
  font-size: 19px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 0;
}

/* Kopf-/Fußtext — rendered HTML from the rich-text editor. */
.ap-text {
  margin-top: 8mm;
  line-height: 1.65;
}
.ap-text p {
  margin: 0 0 0.6em;
}
.ap-text p:last-child {
  margin-bottom: 0;
}

/* Everything the rich-text editor can produce. The preview page renders inside
   the app, where Tailwind's preflight strips list markers and heading sizes, so
   the formats are restored here rather than relying on browser defaults — the
   PDF and the preview have to show the same thing. */
.ap-text strong,
.ap-foot-text strong {
  font-weight: 600;
}
.ap-text em,
.ap-foot-text em {
  font-style: italic;
}
.ap-text s,
.ap-foot-text s,
.ap-text del,
.ap-foot-text del {
  text-decoration: line-through;
}
.ap-text u,
.ap-foot-text u,
.ap-text ins,
.ap-foot-text ins {
  text-decoration: underline;
}
.ap-text ul,
.ap-foot-text ul,
.ap-text ol,
.ap-foot-text ol {
  margin: 0 0 0.6em;
  padding-left: 1.4em;
}
.ap-text ul,
.ap-foot-text ul {
  list-style: disc;
}
.ap-text ol,
.ap-foot-text ol {
  list-style: decimal;
}
.ap-text li,
.ap-foot-text li {
  margin: 0.15em 0;
}
.ap-text h1,
.ap-text h2,
.ap-text h3,
.ap-foot-text h1,
.ap-foot-text h2,
.ap-foot-text h3 {
  margin: 0 0 0.4em;
  font-weight: 600;
  line-height: 1.3;
}
.ap-text h1,
.ap-foot-text h1 {
  font-size: 1.3em;
}
.ap-text h2,
.ap-foot-text h2 {
  font-size: 1.15em;
}
.ap-text h3,
.ap-foot-text h3 {
  font-size: 1.05em;
}
.ap-text blockquote,
.ap-foot-text blockquote {
  margin: 0 0 0.6em;
  padding-left: 3mm;
  border-left: 1px solid var(--ap-border-strong);
  color: var(--ap-fg-muted);
}
.ap-text a,
.ap-foot-text a {
  color: inherit;
  text-decoration: underline;
}

/* Positionstabelle */
.ap-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8mm;
  font-variant-numeric: tabular-nums;
}
.ap-table thead th {
  text-align: left;
  font-size: 8.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ap-fg-muted);
  border-bottom: 1.5px solid var(--ap-fg);
  padding: 0 8px 8px;
}
.ap-table th.c-num,
.ap-table td.c-num,
.ap-table th.c-ust,
.ap-table td.c-ust {
  text-align: right;
}
/* Keep "19 %" on one line — the narrow column would otherwise break the % off. */
.ap-table td.c-ust {
  white-space: nowrap;
}
.ap-table tbody td {
  padding: 10px 8px;
  vertical-align: top;
  border-bottom: 1px solid var(--ap-border);
}
.ap-table td.c-pos {
  width: 34px;
  color: var(--ap-fg-muted);
  font-variant-numeric: tabular-nums;
}
.ap-table td.c-num {
  white-space: nowrap;
}
.ap-pos-title {
  font-weight: 600;
}
.ap-pos-desc {
  color: var(--ap-fg-muted);
  font-size: 10px;
  margin-top: 2px;
  line-height: 1.5;
  white-space: pre-line;
}
.ap-pos-rabatt {
  color: var(--ap-fg-muted);
  font-size: 9.5px;
  margin-top: 6px;
}
.ap-table .c-num .muted {
  color: var(--ap-fg-muted);
}
.ap-table .c-num .strong {
  font-weight: 700;
  margin-top: 3px;
}
/* Without a discount the amount is the only line in the cell — drop the gap
   that otherwise separates it from the brutto/discount lines above, so it
   aligns with the rest of the row. */
.ap-table .c-num .strong:first-child {
  margin-top: 0;
}
.ap-sub-title {
  font-weight: 600;
}

/* Summen */
.ap-totals {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.ap-totals-box {
  width: 80mm;
}
.ap-total-row {
  display: flex;
  justify-content: space-between;
  padding: 2px 8px;
  font-size: 10.5px;
}
.ap-total-row span:last-child {
  font-variant-numeric: tabular-nums;
}
.ap-total-row.is-grand {
  border-top: 1.5px solid var(--ap-fg);
  margin-top: 4px;
  padding-top: 6px;
  font-weight: 700;
  font-size: 14px;
}
.ap-total-row.is-grand span:first-child {
  font-family: var(--ap-font-display);
}

/* § 19 UStG note below the totals (Kleinunternehmer) */
.ap-tax-note {
  margin-top: 8px;
  padding: 0 8px;
  font-size: 9px;
  color: var(--ap-fg-muted);
  line-height: 1.45;
}

/* Konditionen */
.ap-terms {
  margin-top: 11mm;
  font-size: 9.5px;
  color: var(--ap-fg-muted);
  line-height: 1.6;
  border-top: 1px solid var(--ap-border);
  padding-top: 11px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
/* Skonto is a single line rather than a labelled block: the page is a fixed
   297mm box, so every extra line eats into the space the next section needs. */
.ap-terms-line {
  margin: 0;
}
.ap-terms-label {
  font-weight: 600;
  color: var(--ap-fg);
  margin-bottom: 2px;
}
.ap-terms-text {
  white-space: pre-line;
}

/* Fußtext + Unterschrift */
.ap-foot-text {
  margin-top: 8mm;
  line-height: 1.65;
}
/* Statutory notes under the foot text (§ 14 UStG) */
.ap-legal-notes {
  margin-top: 5mm;
  font-size: 9px;
  color: var(--ap-fg-muted);
  line-height: 1.45;
}
.ap-legal-notes p {
  margin: 0 0 2px;
}
.ap-signature {
  margin-top: 7mm;
}
.ap-sign-line {
  width: 62mm;
  border-top: 1px solid var(--ap-fg);
}
.ap-sign-name {
  font-family: var(--ap-font-display);
  font-weight: 600;
  margin-top: 6px;
}
.ap-sign-sub {
  color: var(--ap-fg-muted);
  font-size: 10px;
  margin-top: 1px;
}

/* Briefbogen-Fußzeile */
/* Visual look — unchanged from the in-flow footer so the repeating fixed
   footer is pixel-identical. Positioning (fixed, insets) lives in the
   pagination block above; the bottom pin no longer comes from flex. */
.ap-footer {
  padding-top: 9px;
  border-top: 1px solid var(--ap-border);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
  font-size: 8px;
  color: var(--ap-fg-muted);
  line-height: 1.55;
  background: var(--ap-bg-canvas);
}
.ap-footer-col .t {
  color: var(--ap-fg-muted);
  font-weight: 600;
  margin-bottom: 3px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* ---------- PDF-Render: nur das Papier, randlos auf A4 ---------- */
.ap-pdf {
  background: var(--ap-bg-canvas);
}
.ap-pdf .ap-page {
  box-shadow: none;
  margin: 0;
}

/* ---------- Annahmebestätigung (Unterschriftsseite) ----------
   Starts on its own sheet via break-before (see pagination block). It keeps
   the .ap-page padding + relative positioning, so the repeating footer and
   page geometry apply here too. */
.ap-sig-head {
  margin-bottom: 28px;
}
.ap-eyebrow {
  font-size: 9px;
  color: var(--ap-fg-subtle);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}
.ap-sig-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--ap-fg);
  margin: 0;
}
.ap-sig-facts {
  border: 1px solid var(--ap-border);
  border-radius: 6px;
  overflow: hidden;
  margin-bottom: 36px;
}
.ap-sig-fact {
  display: flex;
  justify-content: space-between;
  padding: 9px 14px;
  border-bottom: 1px solid var(--ap-border);
}
.ap-sig-fact:last-child {
  border-bottom: none;
}
.ap-sig-fact .k {
  color: var(--ap-fg-muted);
}
.ap-sig-fact .v {
  color: var(--ap-fg);
  font-weight: 500;
}
.ap-sig-label {
  font-size: 9px;
  color: var(--ap-fg-subtle);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.ap-sig-image {
  display: block;
  max-width: 320px;
  max-height: 120px;
  margin-bottom: 4px;
}
.ap-sig-line {
  border-bottom: 1px solid var(--ap-border-strong);
  width: 320px;
  margin-bottom: 6px;
}
.ap-sig-name {
  color: var(--ap-fg);
  font-weight: 500;
}
.ap-sig-note {
  margin-top: 36px;
  color: var(--ap-fg-muted);
  font-size: 10px;
  line-height: 1.6;
  max-width: 440px;
}

/* Kontor: a letterhead fact the configuration does not have yet. */
.ap-missing {
  color: #b42318;
}

/* Kontor: the footer sits outside the sheet's table and would otherwise
   fall back to the browser's serif face. */
.ap-pdf {
  font-family: var(--ap-font-sans);
}
