/* =========================================================
   S DESIGNS — CASH INVOICE (A5 PORTRAIT PRINT)
   Design ported from crm-invoice.html.
   Loaded after main.css — only adds invoice-specific rules.

   A5 PORTRAIT: 148mm × 210mm (5.83in × 8.27in)
   Font: 'Cera Pro' ONLY (loaded via main.css @font-face) — no fallback fonts

   MOBILE ADDITION — screen-only scaling so the fixed-width A5
   invoice fits inside narrow phone viewports without horizontal
   page scrolling. Print rules are untouched.
   ========================================================= */

:root {
  --ink:   #292929;
  --muted: #777;
  --line:  #d7d7d7;
  --pale:  #f0f1f4;
  --panel: #f7f7f7;
}

html, body {
  margin: 0;
  padding: 0;
  color: var(--ink);
  font-family: 'Cera Pro';
}

body {
  background: #ececec;
}

/* ---------- Page shell ---------- */
.invoice {
  position: relative;
  width: 148mm;
  min-height: 210mm;
  margin: 12px auto;
  padding: 6mm 7mm 5mm;
  background: #fff;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-family: 'Cera Pro';
  font-size: 8pt;
  color: var(--ink);
}

/* ---------- Cera Pro ONLY ---------- */
.invoice,
.invoice *,
.invoice *::before,
.invoice *::after {
  font-family: 'Cera Pro';
  font-synthesis: none;
}

/* ---------- Header (3-column grid) ---------- */
.invoice .top {
  display: grid;
  grid-template-columns: 0.76fr 1.44fr 19mm;
  gap: 1.125mm;
  align-items: stretch;
  min-height: 15mm;
  position: relative;
}

.invoice .invoice-meta {
  padding-right: 1.125mm;
  line-height: 1.5;
  font-size: 7.3pt;
  font-weight: 500;
}

.invoice .invoice-meta .heading {
  display: inline-block;
  font-size: 9.5pt;
  font-weight: 700;
  text-decoration: underline;
  margin-bottom: 1.5mm;
}

.invoice .meta-line {
  margin-top: 0.9mm;
  white-space: nowrap;
}

.invoice .invoice-meta .meta-line strong { font-weight: 700; }

.invoice .billed {
  position: relative;
  align-self: start;
  line-height: 1.5;
  padding-right: 11.5mm;
  font-size: 7.6pt;
  min-width: 0;
  font-weight: 500;
}

.invoice .billed::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: -2.475mm;
  width: 0.35mm;
  background: var(--line);
}

.invoice .billed,
.invoice .billed * { overflow-wrap: anywhere; }

.invoice .billed .label {
  font-size: 6.8pt;
  letter-spacing: 0.04em;
  margin-bottom: 0.175mm;
}

.invoice .billed .customer {
  font-size: 10.5pt;
  line-height: 1.25;
  font-weight: 700;
  margin-bottom: 0.175mm;
}

.invoice .billed > div:nth-child(n+3) { display: inline; }
.invoice .billed > div:nth-child(n+4)::before { content: " "; }

.invoice .billed > div.addr {
  display: block;
  font-family: 'Cera Pro';
  font-weight: 400;
}

.invoice .logo-wrap {
  text-align: center;
  position: relative;
  left: -7mm;
}

.invoice .logo {
  display: block;
  width: 14.436mm;
  height: 27.6mm;
  margin: 0 auto 1mm;
}

/* ---------- Quote band ---------- */
.invoice .tagline {
  position: relative;
  display: flex;
  align-items: flex-end;
  border-bottom: 0.35mm solid var(--line);
  margin: 0 -7mm 6mm;
  padding: 0 7mm 0;
  color: #d7d8d9;
  font-size: 13pt;
  font-weight: 700;
  letter-spacing: 0.09em;
  white-space: nowrap;
  line-height: 0.7;
}

.invoice .tagline-agency {
  position: absolute;
  right: 24mm;
  bottom: 0;
  transform: translate(50%, 50%);
  background: #fff;
  padding: 0 2.5mm;
  color: var(--ink);
  font-size: 7.5pt;
  font-weight: 700;
  letter-spacing: normal;
  line-height: 1;
}

/* ---------- Items table ---------- */
.invoice .items {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: 7.3pt;
}

.invoice .items th {
  background: var(--pale);
  text-align: left;
  padding: 2mm 1.6mm;
  border: 0.25mm solid var(--line);
  font-size: 7pt;
  font-weight: 700;
}

.invoice .items td {
  padding: 2mm 1.6mm;
  border: 0.25mm solid #dedede;
  line-height: 1.25;
  vertical-align: middle;
  overflow-wrap: anywhere;
  font-weight: 400;
}

.invoice .items .num,
.invoice .items .date,
.invoice .items .qty,
.invoice .items .rate,
.invoice .items .amount { white-space: nowrap; }

.invoice .items .num        { width: 7%; }
.invoice .items .date       { width: 13.5%; }
.invoice .items .particular { width: 38.5%; }
.invoice .items .qty        { width: 10%; text-align: right; }
.invoice .items .rate       { width: 14%; text-align: right; }
.invoice .items .amount     { width: 17%; text-align: right; }

.invoice .items th.qty,
.invoice .items th.rate,
.invoice .items th.amount { text-align: right; }

.invoice .items .empty-cell {
  text-align: center;
  color: #999;
  padding: 5mm 2mm;
}

/* ---------- Totals ---------- */
.invoice .items tfoot td {
  font-size: 7.5pt;
  font-weight: 400;
  color: var(--ink);
  background: #fff;
}

.invoice .items tfoot td.label {
  text-align: right;
  color: #777;
}

.invoice .items tfoot tr.grand td {
  font-weight: 500;
  color: #222;
  background: #fafafa;
}

.invoice .items tfoot td.words-cell {
  background: #fff;
  padding: 0;
  vertical-align: bottom;
}

.invoice .items tfoot td.words-cell .words {
  margin: 0;
  border: 0;
  border-radius: 0;
  min-height: 0;
  background: transparent;
  padding: 2.5mm 3mm 2mm;
}

/* ---------- Amount in words (standalone variant) ---------- */
.invoice .words {
  margin-top: 3mm;
  border: 0.3mm dashed #cfcfcf;
  border-radius: 2mm;
  background: #f8f8f8;
  padding: 2.5mm 3mm;
  min-height: 12mm;
}

.invoice .words .label {
  color: #777;
  font-weight: 700;
  font-size: 7pt;
  margin-bottom: 1.2mm;
}

.invoice .words .value {
  font-size: 8.3pt;
  line-height: 1.25;
  font-weight: 400;
}

/* ---------- Spacer + Jurisdiction ---------- */
.invoice .spacer { flex: 1; min-height: 2mm; }

.invoice .jurisdiction {
  font-size: 6.1pt;
  margin: 1mm 0 2mm;
  font-weight: 400;
}

.invoice .jurisdiction strong { font-weight: 700; }

/* ---------- Footer ---------- */
.invoice .footer {
  border-top: 0.35mm solid var(--line);
  margin: 0 -7mm;
  padding: 2.5mm 7mm 0;
  display: grid;
  grid-template-columns: 1fr 1.15fr 0.7fr;
  gap: 2.5mm;
  min-height: 20mm;
  align-items: stretch;
}

.invoice .bank {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2mm;
  align-items: stretch;
  height: 20mm;
  font-size: 5.7pt;
  line-height: 1;
  font-weight: 500;
}

.invoice .bank > div {
  display: flex;
  flex-direction: column;
  min-height: 0;
  align-self: center;
}

.invoice .qr {
  box-sizing: border-box;
  display: block;
  width: 20mm !important;
  height: 20mm !important;
  min-width: 20mm;
  min-height: 20mm;
  max-width: 20mm;
  max-height: 20mm;
  object-fit: fill;
  border: 0.2mm solid #eee;
}

.invoice .bank-title {
  font-weight: 700;
  margin-bottom: 1.50mm;
}

.invoice .bank-details {
  display: grid;
  grid-template-columns: max-content auto 1fr;
  align-items: start;
  align-content: start;
  row-gap: 1.50mm;
  flex: 1;
}

.invoice .bank-details .k {
  white-space: nowrap;
  font-weight: 700;
}

.invoice .bank-details .s {
  padding: 0 0.8mm;
  white-space: nowrap;
}

.invoice .bank-details .v { white-space: nowrap; }

.invoice .address {
  position: relative;
  padding-left: 2.8mm;
  padding-right: 2.8mm;
  margin-left: 1mm;
  box-sizing: border-box;
  height: 20mm;
  font-size: 5.7pt;
  line-height: calc(1 * 5.7pt + 1.50mm);
  --addr-shift: calc((20mm - (5 * 5.7pt + 4 * 1.50mm)) / 2 - 0.75mm);
  top: var(--addr-shift);
  font-weight: 500;
}

.invoice .address strong { font-weight: 700; }

.invoice .address::before {
  content: "";
  position: absolute;
  top: calc(-1 * var(--addr-shift));
  left: 0;
  width: 0.3mm;
  height: 100%;
  background: var(--line);
}

.invoice .address::after {
  content: "";
  position: absolute;
  top: calc(-1 * var(--addr-shift));
  right: 0;
  width: 0.3mm;
  height: 100%;
  background: var(--line);
}

.invoice .sign {
  box-sizing: border-box;
  height: 20mm;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  font-size: 6.7pt;
  font-weight: 500;
}

.invoice .sign .for { white-space: nowrap; }

.invoice .sign .for strong {
  font-size: 8.4pt;
  text-decoration: underline;
  font-weight: 700;
}

.invoice .signature {
  display: block;
  width: 28mm;
  height: 11mm;
  margin: 1mm auto 1mm;
  object-fit: contain;
}

.invoice .sign .role {
  font-size: 7pt;
  font-weight: 500;
}

/* ---------- Screen ---------- */
@media screen {
  /* Only `body` is clipped horizontally. Setting overflow on `html` would
     make the root a scroll container and break `position: sticky` on the
     CRM sidebar (.crm-sidebar), which caused a light gap below the sidebar
     on tall invoice pages. `body { overflow-x: hidden; }` is already in
     foundation.css and is sufficient to prevent horizontal scrolling. */
  body {
    overflow-x: hidden;
  }

  .invoice {
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.15);
    max-width: 100%;
  }

  .print-sheet {
    max-width: 100%;
    overflow-x: hidden;
  }
}

/* ---------- Page + print ---------- */
@page {
  size: A4 landscape;
  margin: 0;
}

@media print {
  html, body {
    width: 297mm;
    height: 209.5mm;
    margin: 0 !important;
    padding: 0 !important;
    background: #fff;
    overflow: hidden;
  }

  .invoice {
    width: 148mm;
    height: 210mm;
    min-height: 210mm;
    margin: 0;
    box-shadow: none;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  thead { display: table-header-group; }
  tr, .words, .footer { break-inside: avoid; page-break-inside: avoid; }
}

/* =========================================================
   ACTION TOOLBAR — Print / Download PDF / WhatsApp
   ========================================================= */
.invoice-actions {
  box-sizing: border-box;
  width: 148mm;
  max-width: 100%;
  margin: 14px auto;
  padding: 0 4mm;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.invoice-actions,
.invoice-actions * {
  font-family: 'Cera Pro';
  font-synthesis: none;
}

.invoice-actions.top-actions    { margin-bottom: 6px; }
.invoice-actions.bottom-actions { margin-top: 6px; }

@media print {
  .invoice-actions {
    display: none !important;
    visibility: hidden !important;
  }
}

@media (max-width: 768px) {
  .invoice-actions {
    width: 100%;
    padding: 0 14px;
    gap: 8px;
  }
  .invoice-actions .btn {
    flex: 1 1 auto;
    justify-content: center;
  }
}

/* =========================================================
   TWO-COPY PRINT SHEET  (A4 landscape = 297mm x 210mm)
   ========================================================= */
.print-copy { display: none; }

@media print {
  .print-sheet {
    position: relative;
    display: grid;
    grid-template-columns: 148mm 148mm;
    column-gap: 1mm;
    width: 297mm;
    height: 209.5mm;
    margin: 0;
    padding: 0;
    overflow: hidden;
    break-inside: avoid;
    page-break-inside: avoid;
    break-after: avoid;
    page-break-after: avoid;
  }

  .print-sheet .invoice {
    width: 148mm;
    height: 209.5mm;
    min-height: 209.5mm;
    margin: 0;
    box-shadow: none;
  }

  .print-copy {
    display: block;
    width: 148mm;
    height: 209.5mm;
  }

  /* Dotted vertical cutting line — exact centre of the A4 sheet */
  .print-sheet::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: 0;
    border-left: 0.3mm dotted var(--muted);
    transform: translateX(-50%);
    pointer-events: none;
  }
}

/* =========================================================
   PDF EXPORT (html2pdf)
   ========================================================= */
.invoice.pdf-export {
  width: 148mm;
  min-height: 209.5mm;
  margin: 0;
  box-shadow: none;
}

.invoice.pdf-export .billed > .customer,
.invoice.pdf-export .billed > div:nth-child(3) {
  display: block;
  width: 100%;
  clear: both;
}


/* =========================================================
   MOBILE SCREEN SCALING  (screen only — print untouched)
   ---------------------------------------------------------
   The invoice is a fixed 148mm (≈559 px) sheet. On phones the
   natural width exceeds the viewport, so we proportionally
   scale it down with `zoom`. Print rules above are never
   affected because everything here is inside `@media screen`.
   The toolbar is scaled in the same step to match.
   ========================================================= */
@media screen and (max-width: 640px) {
  .invoice { zoom: 0.78; }
  .invoice-actions { zoom: 0.95; }
}

@media screen and (max-width: 480px) {
  .invoice { zoom: 0.78; }
  body { padding: 8px; }
  .invoice { margin-top: 4px; margin-bottom: 4px; }
}

@media screen and (max-width: 430px) { .invoice { zoom: 0.72; } }

@media screen and (max-width: 390px) { .invoice { zoom: 0.66; } }

@media screen and (max-width: 375px) { .invoice { zoom: 0.63; } }

@media screen and (max-width: 360px) { .invoice { zoom: 0.60; } }

@media screen and (max-width: 320px) { .invoice { zoom: 0.54; } }