/* عرض الفاتورة على الشاشة وقوالب الطباعة (A4، عربي RTL).
   القوالب: tpl-classic (رسمي بجدول) و tpl-modern (عصري ملوّن) و tpl-receipt (شبه إيصال).
   اللون الرئيسي يأتي من --accent، ولون النص فوقه من --on-accent. */

.inv-page { max-width: 820px; }

.invoice-sheet {
  --pad: 18px;
  --accent: #0f1b3d;
  --on-accent: #ffffff;
  background: #fff;
  color: #111;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .12);
  padding: var(--pad);
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.invoice-sheet[hidden] { display: none; }

.inv-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border-bottom: 2px solid var(--accent);
  padding-bottom: 12px;
}
.inv-center { text-align: center; }
.inv-center h2 { font-size: 1.6rem; color: var(--accent); }
.inv-logo { max-width: 120px; max-height: 80px; object-fit: contain; }
/* مجموعة العنوان والهاتف تحت الاسم: العنوان يمينًا والهاتف يسارًا وهما متقاربان */
.inv-contact { display: flex; flex-wrap: wrap; justify-content: center; align-items: baseline; gap: 4px 22px; }
.inv-side p { color: #444; font-size: .95rem; }
.inv-phone p { color: #111; font-size: 1.15rem; font-weight: 700; letter-spacing: .3px; }

.inv-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 18px; }
.inv-meta div { display: flex; gap: 6px; align-items: baseline; }
.inv-meta span { color: #666; font-size: .9rem; min-width: 5.2em; }
.inv-meta strong { overflow-wrap: anywhere; }

.inv-table { width: 100%; border-collapse: collapse; }
.inv-table th { background: var(--accent); color: var(--on-accent); padding: 6px 8px; text-align: start; font-size: .95rem; }
.inv-table td { padding: 6px 8px; border-bottom: 1px solid #dfe3e8; }
.inv-table tr { break-inside: avoid; page-break-inside: avoid; }
.inv-table thead { display: table-header-group; }
.inv-table .c-no { width: 36px; text-align: center; }
.inv-table .c-num { width: 90px; text-align: center; }

.inv-totals { align-self: flex-end; min-width: 0; display: flex; flex-direction: column; gap: 4px; break-inside: avoid; }
.inv-totals div { display: flex; justify-content: flex-start; gap: 10px; padding: 3px 0; border-bottom: 1px dashed #cfd8dc; }
.inv-totals div span { min-width: 8.5em; }
.inv-totals .inv-remaining { border-bottom: 0; border-top: 2px solid var(--accent); font-size: 1.15rem; font-weight: 700; color: #c62828; }

.inv-sign { display: flex; justify-content: space-between; gap: 24px; margin-top: 20px; break-inside: avoid; }
.inv-sign div { flex: 1; text-align: center; }
.inv-sign span { color: #444; font-size: .9rem; }
.inv-sign i { display: block; margin-top: 36px; border-top: 1px solid #333; }

.inv-foot { margin-top: auto; text-align: center; padding-top: 18px; font-weight: 700; color: var(--accent); }

@media screen and (max-width: 700px) {
  .invoice-sheet { --pad: 12px; }
  .inv-meta { grid-template-columns: 1fr; }
  .inv-table th, .inv-table td { padding: 6px 4px; font-size: .85rem; }
  .inv-table .c-num { width: 62px; }
  .inv-totals { min-width: 0; width: 100%; }
}

/* ---------- القالب العصري: رأس وتذييل ملوّنان ---------- */
.tpl-modern { overflow: hidden; }
.tpl-modern .inv-head {
  margin: calc(-1 * var(--pad)) calc(-1 * var(--pad)) 0;
  padding: 22px var(--pad);
  background: var(--accent);
  color: var(--on-accent);
  border-bottom: 0;
}
.tpl-modern .inv-center h2, .tpl-modern .inv-side p, .tpl-modern .inv-phone p { color: inherit; }
.tpl-modern .inv-meta { background: #f3f5fa; border-radius: 10px; padding: 12px 14px; }
.tpl-modern .inv-table th:first-child { border-start-start-radius: 8px; }
.tpl-modern .inv-table th:last-child { border-start-end-radius: 8px; }
.tpl-modern .inv-table tbody tr:nth-child(even) td { background: #f7f8fb; }
.tpl-modern .inv-totals div { border-bottom-color: #e3e7ee; }
.tpl-modern .inv-totals .inv-remaining {
  background: var(--accent); color: var(--on-accent);
  padding: 8px 12px; border-radius: 8px; border-top: 0;
}
.tpl-modern .inv-foot {
  margin: auto calc(-1 * var(--pad)) calc(-1 * var(--pad));
  padding: 12px; background: var(--accent); color: var(--on-accent);
}

/* ---------- القالب البسيط: شبه إيصال ---------- */
.tpl-receipt { --pad: 16px; max-width: 420px; margin-inline: auto; margin-top: 12px; gap: 12px; font-size: .95rem; }
.tpl-receipt .inv-head { border-bottom: 2px dashed #999; }
.tpl-receipt .inv-center h2 { font-size: 1.35rem; }
.tpl-receipt .inv-meta { grid-template-columns: 1fr; gap: 4px; padding-bottom: 10px; border-bottom: 1px dashed #999; }
.tpl-receipt .inv-meta div { justify-content: flex-start; }
.tpl-receipt .inv-table th { background: none; color: #111; border-top: 1px dashed #666; border-bottom: 1px dashed #666; padding: 6px 4px; }
.tpl-receipt .inv-table td { border-bottom: 1px dotted #bbb; padding: 6px 4px; }
.tpl-receipt .inv-table .c-num { width: 64px; }
.tpl-receipt .inv-totals { align-self: stretch; min-width: 0; border-top: 1px dashed #666; padding-top: 6px; }
.tpl-receipt .inv-totals .inv-remaining { border-top: 1px dashed #666; color: var(--accent); }
.tpl-receipt .inv-sign { margin-top: 8px; gap: 24px; }
.tpl-receipt .inv-sign i { margin-top: 28px; }
.tpl-receipt .inv-foot { border-top: 2px dashed #999; color: #111; }

/* ---------- نسخة الفاتورة المصوَّرة للمشاركة: عرض ثابت بغض النظر عن حجم الشاشة ---------- */
.invoice-sheet.capture { --pad: 18px; width: 794px; box-shadow: none; border-radius: 0; margin: 0; }
.invoice-sheet.capture .inv-meta { grid-template-columns: 1fr 1fr; }
.invoice-sheet.capture .inv-table th, .invoice-sheet.capture .inv-table td { padding: 8px; font-size: .95rem; }
.invoice-sheet.capture .inv-table .c-num { width: 90px; }
.invoice-sheet.capture .inv-totals { min-width: 0; width: auto; }
.invoice-sheet.capture.tpl-receipt { --pad: 16px; width: 420px; max-width: none; }
.invoice-sheet.capture.tpl-receipt .inv-meta { grid-template-columns: 1fr; }
.invoice-sheet.capture.tpl-receipt .inv-table th, .invoice-sheet.capture.tpl-receipt .inv-table td { padding: 6px 4px; font-size: .95rem; }
.invoice-sheet.capture.tpl-receipt .inv-table .c-num { width: 64px; }
.invoice-sheet.capture.tpl-receipt .inv-totals { min-width: 0; width: 100%; }

/* ---------- الطباعة: ورق A4 ---------- */
@page { size: A4; margin: 15mm; }

@media print {
  html, body {
    background: #fff !important;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
    /* body له min-height: 100vh في style.css، وهذا يسبب صفحة ثانية فارغة عند الطباعة */
    min-height: 0 !important; height: auto !important;
  }
  .no-print, .app-header { display: none !important; }
  .page, .inv-page { max-width: none; margin: 0; padding: 0; }
  .invoice-sheet {
    --pad: 0px;
    box-shadow: none;
    border-radius: 0;
    margin: 0;
    min-height: 258mm; /* أقل من المساحة المتاحة (267مم) كي لا تنزل صفحة ثانية فارغة */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .invoice-sheet * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .invoice-sheet.tpl-receipt { min-height: 0; margin-inline: auto; --pad: 4mm; }
  .invoice-sheet.tpl-modern .inv-head { margin: 0; padding: 18px 20px; border-radius: 8px; }
  .invoice-sheet.tpl-modern .inv-foot { margin: auto 0 0; border-radius: 8px; }
}

/* ---------- وصل القبض وكشف الحساب ---------- */
.doc-title { text-align: center; font-size: 1.3rem; color: var(--accent); }
.doc-sub { font-size: 1rem; color: var(--accent); margin-bottom: -6px; }
.inv-empty { color: #666; text-align: center; padding: 8px 0; }
.tpl-modern .doc-title { background: color-mix(in srgb, var(--accent) 10%, #fff); padding: 6px; border-radius: 8px; }

/* ---------- كشف الحساب عند الطباعة: مضغوط ليبقى على صفحة A4 واحدة (الألوان بلا تغيير) ---------- */
.stmt-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: start; }

@media print {
  .doc-statement { gap: 6px; font-size: 11px; }
  .doc-statement .inv-head { gap: 6px; padding-bottom: 6px; }
  .doc-statement .inv-logo { max-width: 80px; max-height: 40px; }
  .doc-statement .inv-center h2 { font-size: 1.1rem; }
  .doc-statement .inv-side p { font-size: .85rem; margin: 0; }
  .doc-statement .inv-phone p { font-size: 1rem; }
  .doc-statement .inv-contact { gap: 2px 18px; }
  .invoice-sheet.tpl-modern.doc-statement .inv-head { padding: 7px 12px; }
  .doc-statement .doc-title { font-size: 1rem; padding: 2px 6px; margin: 0; }

  /* بيانات الزبون في سطر واحد */
  .doc-statement .inv-meta { grid-template-columns: repeat(3, auto); justify-content: space-between; gap: 2px 14px; padding: 4px 10px; font-size: .85rem; border-bottom: 0; }
  .doc-statement .inv-meta span { min-width: 0; font-size: .8rem; }

  /* الإجمالي والمدفوع والباقي بجانب بعضها */
  .doc-statement .inv-totals { flex-direction: row; align-self: stretch; gap: 8px; }
  .doc-statement .inv-totals div { flex: 1; justify-content: space-between; gap: 6px; padding: 3px 8px; border: 1px solid #cfd8dc; border-radius: 6px; }
  .doc-statement .inv-totals div span { min-width: 0; }
  .doc-statement .inv-totals .inv-remaining { border: 1px solid var(--accent); font-size: 1rem; }
  .doc-statement .doc-sub { font-size: .85rem; margin: 0; }

  /* سجل الدفعات: خط أصغر ومسافات أقل بين الأسطر */
  .doc-statement .inv-table th, .doc-statement .inv-table td { padding: 2px 6px; font-size: 10px; line-height: 1.25; }
  .doc-statement .inv-table .c-no { width: 24px; }
  .doc-statement .inv-table .c-num { width: 66px; }
  .doc-statement.stmt-dense .inv-table th, .doc-statement.stmt-dense .inv-table td { padding: 1px 4px; font-size: 8.5px; line-height: 1.2; }
  .doc-statement .inv-foot { margin-top: 4px; padding-top: 6px; font-size: .85rem; }
  .doc-statement.tpl-modern .inv-foot { padding: 6px; }
}