/* ============================================================
   Fawtara — bilingual AR/EN invoice builder
   Two layers: the app chrome (screen only) and the document
   (screen preview + the thing that actually prints).
   ============================================================ */

:root {
  --ink:        #14181d;
  --ink-soft:   #4a5560;
  --ink-faint:  #8b96a2;
  --line:       #dfe3e8;
  --line-soft:  #eef1f4;
  --paper:      #ffffff;
  --ground:     #f4f2ee;
  --accent:     #0f5c56;
  --accent-ink: #0b423e;
  --accent-wash:#e7f0ef;
  --sand:       #c9a227;
  --danger:     #8a3324;

  --radius: 10px;
  --shadow-lift: 0 1px 2px rgba(20,24,29,.06), 0 8px 24px rgba(20,24,29,.08);

  --ui: "IBM Plex Sans Arabic", "Segoe UI", system-ui, -apple-system, sans-serif;
  --doc: "IBM Plex Sans Arabic", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--ui);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------------- app shell ---------------- */

.app {
  display: grid;
  grid-template-columns: minmax(380px, 460px) 1fr;
  min-height: 100vh;
}

.panel {
  background: var(--paper);
  border-inline-end: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  position: sticky;
  top: 0;
}

.panel__head {
  padding: 18px 22px 14px;
  border-bottom: 1px solid var(--line-soft);
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-inline-end: auto;
}

.brand__mark {
  font-weight: 600;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.brand__mark span { color: var(--accent); }

.brand__tag {
  font-size: 11px;
  color: var(--ink-faint);
  letter-spacing: .04em;
  text-transform: uppercase;
}

.panel__body {
  overflow-y: auto;
  padding: 4px 22px 120px;
  flex: 1;
}

.panel__foot {
  position: sticky;
  bottom: 0;
  padding: 12px 22px;
  border-top: 1px solid var(--line);
  background: var(--paper);
  display: flex;
  gap: 8px;
}

/* ---------------- form primitives ---------------- */

.section {
  border-bottom: 1px solid var(--line-soft);
  padding: 16px 0;
}
.section:last-child { border-bottom: 0; }

.section__title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 12px;
}

.field { margin-bottom: 10px; }

.field > label {
  display: block;
  font-size: 12px;
  color: var(--ink-soft);
  margin-bottom: 4px;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  transition: border-color .15s, box-shadow .15s;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-wash);
}

.field textarea { resize: vertical; min-height: 62px; }

.row {
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr 1fr;
}

.row--3 { grid-template-columns: 1fr 1fr 1fr; }

.hint {
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 4px;
}

/* ---------------- buttons ---------------- */

.btn {
  border: 1px solid var(--line);
  background: var(--paper);
  padding: 9px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--ground); }
.btn:active { transform: translateY(1px); }

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  flex: 1;
}
.btn--primary:hover { background: var(--accent-ink); }

.btn--ghost { color: var(--ink-soft); }

.btn--tiny {
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 6px;
}

.btn--danger { color: var(--danger); border-color: transparent; background: transparent; }
.btn--danger:hover { background: #f7ecea; }

.langtoggle {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.langtoggle button {
  border: 0;
  background: var(--paper);
  padding: 6px 12px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-soft);
}
.langtoggle button[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
}

/* ---------------- line items editor ---------------- */

.items { display: flex; flex-direction: column; gap: 8px; }

.item {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  background: var(--paper);
}

.item__head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.item__idx {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent-wash);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 600;
  display: grid;
  place-items: center;
  flex: none;
}

.item__spacer { margin-inline-start: auto; }

/* ---------------- preview stage ---------------- */

.stage {
  padding: 32px;
  overflow-y: auto;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.stage__bar {
  width: 210mm;
  max-width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink-faint);
  font-size: 12px;
}

.stage__bar .btn { background: var(--paper); }

/* ============================================================
   THE DOCUMENT
   A4 = 210 x 297mm. Everything below prints.
   ============================================================ */

.doc {
  width: 210mm;
  min-height: 297mm;
  background: var(--paper);
  box-shadow: var(--shadow-lift);
  padding: 16mm 15mm;
  font-family: var(--doc);
  color: var(--ink);
  position: relative;
  display: flex;
  flex-direction: column;
}

.doc[dir="rtl"] { text-align: right; }

/* --- masthead --- */

.doc__top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16mm;
  padding-bottom: 6mm;
  border-bottom: 2px solid var(--accent);
}

.issuer { max-width: 95mm; }

.issuer__logo {
  max-height: 20mm;
  max-width: 60mm;
  object-fit: contain;
  margin-bottom: 3mm;
  display: block;
}

.issuer__name {
  font-size: 15pt;
  font-weight: 600;
  line-height: 1.25;
  margin: 0 0 1mm;
}

.issuer__alt {
  font-size: 11pt;
  color: var(--ink-soft);
  margin: 0 0 2mm;
}

.issuer__meta {
  font-size: 8.5pt;
  color: var(--ink-soft);
  line-height: 1.6;
  white-space: pre-line;
}

.doctitle { text-align: end; flex: none; }

.doctitle__word {
  font-size: 20pt;
  font-weight: 600;
  letter-spacing: .01em;
  color: var(--accent);
  line-height: 1.1;
  margin: 0;
}

.doctitle__alt {
  font-size: 12pt;
  color: var(--ink-faint);
  margin: 0 0 3mm;
}

.doctitle__ref {
  font-size: 9pt;
  color: var(--ink-soft);
  line-height: 1.7;
}

.doctitle__ref b { color: var(--ink); font-weight: 600; }

/* --- parties --- */

.parties {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8mm;
  margin: 7mm 0 6mm;
}

.party__label {
  font-size: 8pt;
  font-weight: 600;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 2mm;
}

.party__name {
  font-size: 11.5pt;
  font-weight: 600;
  margin-bottom: 1mm;
}

.party__body {
  font-size: 9pt;
  color: var(--ink-soft);
  line-height: 1.65;
  white-space: pre-line;
}

/* --- table --- */

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 9.5pt;
}

.table thead th {
  background: var(--accent);
  color: #fff;
  font-weight: 500;
  font-size: 8.5pt;
  letter-spacing: .03em;
  padding: 3mm 3mm;
  text-align: start;
}

.table thead th.num { text-align: end; }
.table thead th.mid { text-align: center; }

.table tbody td {
  padding: 3mm;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: top;
}

.table tbody tr:nth-child(even) td { background: #fafbfc; }

.table td.num { text-align: end; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table td.mid { text-align: center; font-variant-numeric: tabular-nums; }

.cell__main { font-weight: 500; }
.cell__alt { font-size: 8.5pt; color: var(--ink-soft); margin-top: 0.6mm; }

/* --- totals --- */

.tail {
  display: flex;
  justify-content: space-between;
  gap: 10mm;
  margin-top: 6mm;
  align-items: flex-start;
}

.tail__left { flex: 1; font-size: 8.5pt; color: var(--ink-soft); }

.words {
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 3mm;
  margin-bottom: 4mm;
  background: #fbfaf7;
}

.words__label {
  font-size: 7.5pt;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--ink-faint);
  margin-bottom: 1mm;
}

.words__value { font-size: 9pt; color: var(--ink); font-weight: 500; }

.notes { white-space: pre-line; line-height: 1.6; }
.notes__label {
  font-size: 7.5pt;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--ink-faint);
  margin-bottom: 1mm;
}

.totals { width: 72mm; flex: none; font-size: 9.5pt; }

.totals__row {
  display: flex;
  justify-content: space-between;
  padding: 2mm 3mm;
  color: var(--ink-soft);
}

.totals__row span:last-child { font-variant-numeric: tabular-nums; color: var(--ink); }

.totals__row--grand {
  background: var(--accent);
  color: #fff;
  border-radius: 4px;
  padding: 3.5mm 3mm;
  font-size: 11.5pt;
  font-weight: 600;
  margin-top: 1.5mm;
}
.totals__row--grand span:last-child { color: #fff; }

/* --- footer --- */

.doc__foot {
  margin-top: auto;
  padding-top: 6mm;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 8mm;
  font-size: 8.5pt;
  color: var(--ink-soft);
}

.pay { flex: 1; white-space: pre-line; line-height: 1.65; }

.pay__label {
  font-size: 7.5pt;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--ink-faint);
  margin-bottom: 1mm;
}

.sign { width: 60mm; flex: none; text-align: center; }

.sign__line {
  border-bottom: 1px solid var(--ink-faint);
  height: 14mm;
}

.sign__caption { font-size: 8pt; color: var(--ink-faint); margin-top: 1.5mm; }

/* --- free-tier mark --- */

.mark {
  position: absolute;
  inset-inline-end: 15mm;
  bottom: 8mm;
  font-size: 7.5pt;
  color: var(--ink-faint);
  letter-spacing: .03em;
}

.mark a { color: var(--ink-faint); }

/* ============================================================
   PRINT
   ============================================================ */

@page {
  size: A4;
  margin: 0;
}

@media print {
  html, body { background: #fff; }
  .panel, .stage__bar { display: none !important; }
  .app { display: block; }
  .stage { padding: 0; max-height: none; overflow: visible; display: block; }
  .doc {
    box-shadow: none;
    width: 210mm;
    min-height: 297mm;
    margin: 0;
    page-break-after: avoid;
  }
  .table thead { display: table-header-group; }
  .table tr { page-break-inside: avoid; }
  .totals__row--grand,
  .table thead th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ============================================================
   RESPONSIVE — the builder collapses to one column on small screens
   ============================================================ */

@media (max-width: 1100px) {
  .app { grid-template-columns: 1fr; }
  .panel { position: static; max-height: none; border-inline-end: 0; border-bottom: 1px solid var(--line); }
  .stage { padding: 16px; max-height: none; }
  .doc { width: 100%; min-height: 0; padding: 22px; }
  .stage__bar { width: 100%; }
}

/* ============================================================
   BIDI ISOLATION
   Mixed Arabic/Latin blocks are where every invoice tool breaks:
   a phone number in an RTL paragraph renders its parts reversed.
   `plaintext` lets each line take direction from its own first
   strong character; `.iso` pins a run to LTR inside RTL text.
   ============================================================ */

.issuer__meta,
.party__body,
.pay,
.notes,
.words__value {
  unicode-bidi: plaintext;
}

.iso {
  unicode-bidi: isolate;
  direction: ltr;
}

/* ---------------- upgrade ---------------- */

.upsell {
  margin: 0 0 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfaf7;
  font-size: 12px;
  color: var(--ink-soft);
}

.upsell b { color: var(--ink); font-weight: 600; }

.upsell button {
  margin-top: 8px;
  width: 100%;
}

.upsell--on {
  background: var(--accent-wash);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 29, .55);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}

.modal__card {
  background: var(--paper);
  border-radius: 14px;
  padding: 26px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(20, 24, 29, .3);
}

.modal__card h2 {
  margin: 0 0 4px;
  font-size: 19px;
  letter-spacing: -0.01em;
}

.modal__price {
  font-size: 30px;
  font-weight: 600;
  margin: 14px 0 2px;
}

.modal__price small {
  font-size: 13px;
  font-weight: 400;
  color: var(--ink-faint);
}

.modal ul {
  margin: 14px 0 18px;
  padding-inline-start: 18px;
  color: var(--ink-soft);
  line-height: 1.8;
  font-size: 13px;
}

.modal__close {
  float: inline-end;
  border: 0;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--ink-faint);
  line-height: 1;
}

.modal__note {
  font-size: 11px;
  color: var(--ink-faint);
  margin-top: 12px;
  line-height: 1.6;
}

@media print { .modal, .upsell { display: none !important; } }

/* ---------------- indexable pitch, below the tool ---------------- */

.pitch {
  width: 210mm;
  max-width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 28px;
  margin: 8px 0 40px;
  padding-top: 24px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  color: var(--ink-soft);
}

.pitch h2 {
  font-size: 15px;
  color: var(--ink);
  margin: 0 0 8px;
  letter-spacing: -0.01em;
}

.pitch p { margin: 0 0 10px; line-height: 1.7; }

.pitch ul {
  margin: 0;
  padding-inline-start: 18px;
  line-height: 1.8;
}

.pitch b { color: var(--ink); font-weight: 600; }

@media (max-width: 1100px) { .pitch { grid-template-columns: 1fr; } }
@media print { .pitch { display: none !important; } }
