/* ===== مستندات A4 (طولي وعرضي) — معاينة وطباعة وحفظ PDF ===== */
.doc-ov { position: fixed; inset: 0; z-index: 900; background: rgba(28, 33, 31, .82); display: flex; flex-direction: column; }
.doc-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: calc(10px + env(safe-area-inset-top, 0px)) 14px 10px; background: var(--surface); border-bottom: 1px solid var(--line); }
.doc-bar .doc-t { font-weight: 700; margin-inline-end: auto; }
.doc-scroll { flex: 1; overflow: auto; padding: 18px 10px 40px; }
.doc-fit { margin: 0 auto; overflow: hidden; direction: ltr; }
/* درجات اللون تُحسب في الكود (--dc-7/12/45) لأن مكتبة إنشاء الملف لا تفهم color-mix */
.a4 { --dc: #1d5b79; --dc-7: #f2f6f8; --dc-12: #e4edf1; --dc-45: #93aebc; background: #fff; color: #1b1f1e; box-shadow: 0 6px 30px rgba(0,0,0,.25); font-family: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, sans-serif; font-size: 12.5px; line-height: 1.6; direction: rtl; position: relative; }
.a4.portrait { width: 210mm; min-height: 297mm; padding: 14mm 14mm 16mm; }
.a4.landscape { width: 297mm; min-height: 210mm; padding: 12mm 14mm 14mm; }
.a4 * { box-sizing: border-box; }
.a4 .num { direction: ltr; unicode-bidi: isolate; font-variant-numeric: tabular-nums; }

.d-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 18px; padding-bottom: 12px; }
.d-id { display: flex; gap: 12px; align-items: center; min-width: 0; }
.d-logo { width: 64px; height: 64px; object-fit: contain; flex: none; }
.d-mark { width: 56px; height: 56px; border-radius: 14px; background: var(--dc); color: #fff; display: grid; place-items: center; font-size: 24px; font-weight: 700; flex: none; }
.d-shop { font-size: 17px; font-weight: 700; color: #111; }
.d-sub { color: #555; font-size: 11.5px; }
.d-title { text-align: left; }
.d-title h1 { margin: 0; font-size: 20px; color: var(--dc); font-weight: 700; }
.d-title .d-ref { color: #555; font-size: 11.5px; margin-top: 2px; }
.d-rule { height: 3px; background: var(--dc); border-radius: 3px; margin: 0 0 14px; }
.d-rule::after { content: ""; display: block; width: 22%; height: 3px; background: var(--dc-45); margin-inline-start: auto; border-radius: 3px; }

.d-boxes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.d-box { border: 1px solid #e3e6e5; border-radius: 10px; padding: 10px 12px; }
/* لا تباعد حروف في العربية: يقطع اتصال الحروف عند إنشاء الملف */
.d-box .d-lbl { font-size: 11px; font-weight: 700; color: var(--dc); margin-bottom: 4px; }
.d-kv { display: flex; justify-content: space-between; gap: 10px; padding: 2px 0; }
.d-kv span:first-child { color: #666; }
.d-kv span:last-child { font-weight: 600; text-align: left; }

.d-table { width: 100%; border-collapse: collapse; margin: 4px 0 12px; }
.d-table th { background: var(--dc-12); color: #222; font-weight: 700; font-size: 11.5px; padding: 7px 8px; text-align: start; border-bottom: 2px solid var(--dc); }
.d-table td { padding: 6px 8px; border-bottom: 1px solid #eceeed; vertical-align: top; }
.d-table tr:nth-child(even) td { background: #fafbfa; }
.d-table td.e, .d-table th.e { text-align: left; }
.d-table tfoot td { font-weight: 700; background: var(--dc-7); border-top: 2px solid var(--dc); border-bottom: 0; }
.d-table tr.flag td { background: #fff6e6 !important; }
.d-table thead { display: table-header-group; }
.d-table tr { break-inside: avoid; }

.d-sum { display: flex; justify-content: flex-end; margin: 6px 0 14px; break-inside: avoid; }
.d-head, .d-boxes, .d-note, .d-sum-in { break-inside: avoid; }
.d-sum .d-sum-in { min-width: 260px; border: 1px solid #e3e6e5; border-radius: 10px; overflow: hidden; }
.d-sum .d-kv { padding: 6px 12px; border-bottom: 1px solid #eef0ef; }
.d-sum .d-kv:last-child { border-bottom: 0; }
.d-sum .d-kv.total { background: var(--dc); color: #fff; font-size: 14px; }
.d-sum .d-kv.total span { color: #fff; }

.d-note { border-inline-start: 3px solid var(--dc); background: #f7f8f7; padding: 8px 12px; border-radius: 6px; margin-bottom: 12px; }
.d-note.warn { border-color: #c98a1a; background: #fff6e6; }
.d-signs { display: grid; grid-template-columns: repeat(var(--n, 2), 1fr); gap: 28px; margin-top: 18px; break-inside: avoid; }
.d-sign { text-align: center; color: #333; }
.d-sig-area { height: 76px; display: flex; align-items: flex-end; justify-content: center; position: relative; }
.d-sigimg { max-height: 70px; max-width: 78%; object-fit: contain; position: relative; z-index: 1; }
.d-stamp { position: absolute; height: 84px; max-width: 46%; object-fit: contain; opacity: .85; inset-inline-end: 6%; bottom: -6px; transform: rotate(-8deg); }
.d-sig-line { border-top: 1px solid #444; padding-top: 6px; }
.d-sig-sub { font-size: 10px; color: #666; margin-top: 2px; }
.d-foot { margin-top: 22px; padding-top: 8px; border-top: 1px solid #e3e6e5; display: flex; justify-content: space-between; gap: 10px; color: #777; font-size: 10px; }

/* معاينة حيّة مصغّرة تحت حقول العبارات في الإعدادات */
.doc-sec { display: flex; flex-direction: column; gap: 10px; padding: 16px 0; border-top: 1px solid var(--line-2); }
.doc-sec:first-child { border-top: 0; padding-top: 0; }
.doc-sec h4 { margin: 0; font-size: 1rem; }
.doc-thumb { position: relative; overflow: hidden; border: 1px solid var(--line); border-radius: 12px; background: #e9ecea; max-width: 560px; width: 100%; min-height: 60px; }
.doc-thumb-in { transform-origin: top right; width: max-content; }
.doc-thumb .a4 { box-shadow: none; }

/* النمط الأبيض: أبيض وأسود بلا تعبئة لونية، خطوط رفيعة فقط */
.a4.plain .d-mark { background: #fff; color: #222; border: 1.5px solid #222; }
.a4.plain .d-title h1 { color: #111; }
.a4.plain .d-rule { height: 1.5px; background: #222; }
.a4.plain .d-rule::after { display: none; }
.a4.plain .d-box .d-lbl { color: #111; }
.a4.plain .d-table th { background: #fff; border-bottom: 1.5px solid #222; border-top: 1px solid #222; }
.a4.plain .d-table tr:nth-child(even) td { background: #fff; }
.a4.plain .d-table tfoot td { background: #fff; border-top: 1.5px solid #222; }
.a4.plain .d-table tr.flag td { background: #fff !important; font-style: italic; text-decoration: underline dotted; }
.a4.plain .d-sum .d-kv.total { background: #fff; color: #111; border-top: 1.5px solid #222; }
.a4.plain .d-sum .d-kv.total span { color: #111; }
.a4.plain .d-note { background: #fff; border: 1px solid #ccc; border-inline-start: 3px solid #222; }

@media (max-width: 860px) { .doc-bar .btn { padding: 7px 10px; } }

/* ===== الطباعة / الحفظ PDF ===== */
@media print {
  body.printing-doc > *:not(.doc-ov) { display: none !important; }
  body.printing-doc { background: #fff !important; }
  body.printing-doc .doc-ov { position: static; background: none; display: block; }
  body.printing-doc .doc-bar { display: none; }
  body.printing-doc .doc-scroll { padding: 0; overflow: visible; }
  body.printing-doc .doc-fit { transform: none !important; width: auto !important; height: auto !important; overflow: visible; direction: rtl; }
  body.printing-doc .doc-fit > div { transform: none !important; width: auto !important; }
  body.printing-doc .a4 { box-shadow: none; min-height: 0; width: auto; }
  /* الهوامش من @page حتى تتكرر في كل صفحة */
  body.printing-doc .a4.portrait, body.printing-doc .a4.landscape { padding: 0; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
