/* Official A4 Iranian invoice — دکاوب */
:root {
  color-scheme: light;
  --ink: #111;
  --paper: #fff;
  --stroke: #c8c8c8;
  --muted: #6b6b6b;
  --accent: #e45d1a;
  --paper-width: 210mm;
  --paper-height: 297mm;
  --print-gutter-x: 10mm;
  --print-gutter-top: 10mm;
  --print-gutter-bottom: 9mm;
  font-synthesis: none;
}
.invoice-page *, .invoice-page *::before, .invoice-page *::after { box-sizing: border-box; }
.invoice-page {
  display: flex;
  flex-direction: column;
  width: var(--paper-width);
  min-height: var(--paper-height);
  margin: 0 auto;
  padding: var(--print-gutter-top) var(--print-gutter-x) var(--print-gutter-bottom);
  background: var(--paper);
  color: var(--ink);
  font-family: "Modam", Tahoma, sans-serif;
  -webkit-font-smoothing: antialiased;
  box-shadow: 0 5px 30px rgb(0 0 0 / 12%);
  overflow: visible;
}
.invoice-page h1,
.invoice-page h2,
.invoice-page p { margin: 0; }
.invoice-page bdi { unicode-bidi: isolate; }

.invoice-brand {
  /* Physical LTR bar: title always left, logo always right (even if logo empty) */
  display: flex;
  flex-direction: row;
  direction: ltr;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 10px;
  border-bottom: 2px solid #111;
}
.invoice-brand-mark {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 0;
  order: 2;
  padding-top: 6px;
  direction: rtl;
}
.invoice-brand-mark[hidden],
.invoice-logo[hidden],
.invoice-logo-fallback[hidden] {
  display: none !important;
}
.invoice-logo {
  display: block;
  width: auto;
  max-width: 180px;
  height: auto;
  max-height: 56px;
  object-fit: contain;
  object-position: right center;
}
.invoice-logo-fallback {
  max-width: 200px;
  font-size: 16pt;
  font-weight: 800;
  color: #111;
  line-height: 1.25;
  letter-spacing: -0.02em;
  word-break: break-word;
  text-align: right;
  direction: rtl;
}
.invoice-doc-title {
  order: 1;
  margin-right: auto; /* pin to physical left in LTR brand bar */
  text-align: left;
  min-width: 0;
  max-width: 70%;
  direction: rtl;
}
.invoice-doc-kicker {
  font-size: 8pt;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--accent);
}
.invoice-doc-title h1 {
  margin-top: 2px;
  font-size: 14pt;
  font-weight: 800;
  line-height: 1.45;
}
.invoice-meta-bar {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
  margin-top: 10px;
  border: 0.3mm solid var(--stroke);
  background: #f7f7f7;
}
.meta-cell {
  padding: 8px 12px;
  border-left: 0.3mm solid var(--stroke);
}
.meta-cell:last-child { border-left: 0; }
.meta-label {
  display: block;
  font-size: 8pt;
  color: var(--muted);
  font-weight: 700;
  margin-bottom: 2px;
}
.meta-cell strong {
  font-size: 11pt;
  font-weight: 800;
}

.invoice { flex: 0 0 auto; width: 100%; min-width: 0; margin-top: 10px; }

.parties {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  min-height: 28mm;
  border: 0.3mm solid var(--stroke);
}
.party { padding: 10px 12px; text-align: right; }
.party + .party { border-right: 0.3mm solid var(--stroke); }
.party h2 {
  font-size: 9pt;
  font-weight: 800;
  color: var(--muted);
  margin-bottom: 4px;
}
.party p {
  font-size: 12pt;
  font-weight: 800;
  line-height: 1.5;
}
.party .party-sub {
  margin-top: 4px;
  font-size: 8.5pt;
  line-height: 1.7;
  color: #444;
  font-weight: 500;
}

.service-section { margin-top: 10px; }
.service-grid {
  display: grid;
  grid-template-columns: 58% 16% 26%;
  align-items: center;
}
.service-grid > * { min-width: 0; padding: 0 10px; text-align: right; }
.service-head {
  min-height: 9mm;
  background: #111;
  color: #fff;
  font-weight: 700;
  font-size: 9.5pt;
}
.service-head h2 { font-size: inherit; font-weight: inherit; }
.service-row {
  min-height: 14mm;
  border-inline: 0.3mm solid var(--stroke);
  border-bottom: 0.3mm solid var(--stroke);
  font-size: 9.2pt;
  font-weight: 600;
  break-inside: avoid;
}
.service-name { font-size: 9.5pt; font-weight: 700; line-height: 1.7; padding-block: 8px; }
.service-quantity, .service-cost { white-space: nowrap; }

.invoice-bottom {
  display: grid;
  grid-template-columns: 1.15fr 0.85fr;
  min-height: 40mm;
  margin-top: 10px;
  border: 0.3mm solid var(--stroke);
  break-inside: avoid;
}
.notes {
  min-width: 0;
  padding: 12px 14px;
  border-left: 0.3mm solid var(--stroke);
  text-align: right;
}
.notes h2 { font-size: 9.5pt; font-weight: 800; line-height: 1.6; }
.notes p {
  margin-top: 8px;
  color: #222;
  font-size: 11pt;
  font-weight: 700;
  line-height: 1.7;
}
.notes .note-detail {
  margin-top: 8px;
  color: #555;
  font-size: 8.5pt;
  font-weight: 500;
  line-height: 1.7;
}
.totals {
  display: grid;
  grid-template-rows: repeat(auto-fit, minmax(9mm, auto));
  align-content: stretch;
  min-width: 0;
}
.total-line {
  display: grid;
  grid-template-columns: 48% 52%;
  align-items: center;
  gap: 0;
  padding-inline: 12px;
  min-height: 9.5mm;
  font-size: 9pt;
  font-weight: 700;
  border-bottom: 0.3mm solid var(--stroke);
}
.total-line:last-child { border-bottom: 0; }
.total-label, .total-value { text-align: right; white-space: nowrap; }
.total-final {
  background: #111;
  color: #fff;
  font-size: 10pt;
  min-height: 11mm;
}

.invoice-footer {
  margin-top: auto;
  padding-top: 18px;
  flex-shrink: 0;
}
.invoice-footer-main {
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: center;
  font-size: 9pt;
  font-weight: 700;
  color: #333;
  margin-bottom: 14px;
}
.invoice-footer-main strong {
  font-size: 11pt;
  color: #111;
}
.invoice-stamps {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  min-height: 42mm;
}
.stamp-box {
  position: relative;
  min-height: 42mm;
  border: 0.3mm dashed var(--stroke);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 12px 10px;
  color: var(--muted);
  font-size: 8.5pt;
  font-weight: 700;
  overflow: visible;
  box-sizing: border-box;
}
.stamp-box > span {
  flex: 0 0 auto;
  line-height: 1.4;
}
.invoice-signature {
  position: static;
  display: block;
  width: auto;
  max-width: 58mm;
  max-height: 26mm;
  height: auto;
  margin: auto 0 2px;
  object-fit: contain;
  object-position: center bottom;
  background: transparent;
  pointer-events: none;
  user-select: none;
}
.invoice-signature[hidden] { display: none !important; }

@media screen and (max-width: 850px) {
  html, body { background: #fff; }
  .inv-actions { width: calc(100% - 32px); }
  .invoice-page {
    width: 100%;
    max-width: 210mm;
    min-height: 100dvh;
    margin: 12px auto 24px;
    padding: 18px 16px 14px;
    box-shadow: none;
  }
  .invoice-brand { flex-direction: column; gap: 10px; align-items: flex-start; }
  .invoice-doc-title { text-align: right; }
  .invoice-meta-bar { grid-template-columns: 1fr; }
  .meta-cell { border-left: 0; border-bottom: 0.3mm solid var(--stroke); }
  .meta-cell:last-child { border-bottom: 0; }
}

@media screen and (max-width: 650px) {
  .parties { grid-template-columns: 1fr; }
  .party + .party { border-right: 0; border-top: 0.3mm solid var(--stroke); }
  .service-head { display: block; padding: 10px 12px; }
  .service-head > * { display: none; }
  .service-head h2 { display: block; }
  .service-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 12px;
    min-height: 0;
  }
  .service-name { grid-column: 1 / -1; }
  .service-quantity::before, .service-cost::before {
    content: attr(data-label) ' : ';
    color: #666;
    font-weight: 400;
  }
  .invoice-bottom { grid-template-columns: 1fr; }
  .notes { border-left: 0; border-bottom: 0.3mm solid var(--stroke); }
  .invoice-stamps { grid-template-columns: 1fr; }
}

@page {
  size: A4 portrait;
  margin: 10mm 10mm 9mm;
}
@media print {
  html, body {
    width: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 0 !important;
    background: #fff !important;
    overflow: visible !important;
  }
  body {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .inv-actions, .inv-error { display: none !important; }
  .invoice-page {
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    height: auto !important;
    /* Fill printable A4 area so stamp section keeps bottom spacing */
    min-height: 277mm !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 0 !important;
    box-shadow: none !important;
    overflow: visible !important;
  }
  .app-version-footer { display: none !important; }
  .service-head,
  .total-final,
  .invoice-brand,
  .invoice-meta-bar,
  .parties,
  .invoice-bottom,
  .invoice-footer,
  .invoice-stamps,
  .service-row {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  /* Keep blocks together when possible; allow long item lists to paginate */
  .service-section { break-inside: auto; }
  .service-head { break-after: avoid; break-inside: avoid; }
  .service-row { break-inside: avoid; page-break-inside: avoid; }
  .invoice-brand,
  .invoice-meta-bar,
  .parties,
  .invoice-bottom {
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .invoice-footer {
    margin-top: auto !important;
    padding-top: 16px !important;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .invoice-stamps {
    min-height: 42mm !important;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .stamp-box {
    min-height: 42mm !important;
    overflow: visible !important;
  }
  .invoice-signature {
    max-height: 26mm !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}
