/* برنامج المحاسبة — التنسيقات (بدون أي مصادر خارجية) */

:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0f766e;
  --brand-dark: #115e59;
  --brand-soft: #ccfbf1;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
}

* { box-sizing: border-box; }

/* خاصية hidden في HTML تعمل عبر قاعدة display:none في تنسيقات المتصفح
   الافتراضية، وأي قاعدة display في هذا الملف تتغلّب عليها — وهذا ما كان
   يُبقي نافذة الحوار ظاهرة فوق الصفحة دائماً. هذه القاعدة تحسم الأمر. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", Tahoma, "Noto Naskh Arabic", "Arial", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.35; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1.1rem; }

/* ------------------------------------------------------------- الشريط العلوي */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: .75rem;
  background: var(--brand);
  color: #fff;
  padding: .65rem 1rem;
  padding-top: max(.65rem, env(safe-area-inset-top));
  box-shadow: 0 2px 8px rgba(15, 23, 42, .15);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.05rem; }
.logo { font-size: 1.3rem; }
.topbar-spacer { flex: 1; }
.today { font-size: .85rem; opacity: .9; white-space: nowrap; }
.menu-btn {
  display: none;
  background: rgba(255, 255, 255, .15);
  border: 0; color: #fff; font-size: 1.2rem;
  border-radius: 8px; width: 38px; height: 38px; cursor: pointer;
}

/* ------------------------------------------------------------------- التبويبات */
.tabs {
  position: sticky;
  top: 52px;
  z-index: 25;
  display: flex;
  gap: .25rem;
  overflow-x: auto;
  background: #fff;
  border-bottom: 1px solid var(--line);
  padding: .35rem .75rem;
  scrollbar-width: thin;
}
.tabs a {
  flex: 0 0 auto;
  padding: .45rem .85rem;
  border-radius: 999px;
  color: var(--muted);
  text-decoration: none;
  font-size: .93rem;
  font-weight: 600;
  white-space: nowrap;
}
.tabs a:hover { background: var(--bg); color: var(--ink); }
.tabs a.active { background: var(--brand-soft); color: var(--brand-dark); }

/* ----------------------------------------------------------------- التخطيط */
.container { max-width: 1200px; margin: 0 auto; padding: 1rem; padding-bottom: 3rem; }

.page-head {
  display: flex; align-items: center; flex-wrap: wrap; gap: .6rem; margin-bottom: 1rem;
}
.page-head h2 { margin: 0; flex: 1; min-width: 140px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  margin-bottom: 1rem;
}
.card > h3 { margin-bottom: .75rem; }

/* بطاقات الأرقام */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .85rem 1rem; box-shadow: var(--shadow);
}
.stat .label { font-size: .82rem; color: var(--muted); }
.stat .value { font-size: 1.45rem; font-weight: 700; margin-top: .15rem; }
.stat.brand .value { color: var(--brand); }
.stat.ok    .value { color: var(--ok); }
.stat.warn  .value { color: var(--warn); }
.stat.danger .value { color: var(--danger); }

/* ------------------------------------------------------------------- الأزرار */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 9px; padding: .5rem .9rem; font-size: .93rem; font-weight: 600;
  font-family: inherit; cursor: pointer; text-decoration: none; white-space: nowrap;
  min-height: 40px;
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-dark); }
.btn.danger { background: var(--danger-soft); border-color: #fecaca; color: var(--danger); }
.btn.sm { padding: .3rem .6rem; font-size: .82rem; min-height: 32px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn {
  background: transparent; border: 0; font-size: 1.1rem; cursor: pointer;
  color: var(--muted); padding: .25rem .4rem; border-radius: 6px; font-family: inherit;
}
.icon-btn:hover { background: var(--bg); color: var(--ink); }

/* ------------------------------------------------------------------- الحقول */
.field { display: flex; flex-direction: column; gap: .25rem; margin-bottom: .75rem; }
.field label { font-size: .85rem; color: var(--muted); font-weight: 600; }
input, select, textarea {
  font-family: inherit; font-size: 1rem; color: var(--ink);
  border: 1px solid var(--line); border-radius: 9px; padding: .55rem .7rem;
  background: #fff; width: 100%; min-height: 42px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--brand-soft); border-color: var(--brand);
}
textarea { min-height: 70px; resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 .75rem; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 .75rem; }
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: .85rem; }
.toolbar input, .toolbar select { width: auto; min-width: 140px; flex: 0 1 auto; }
.toolbar .grow { flex: 1 1 180px; min-width: 0; }
.num { text-align: left; direction: ltr; unicode-bidi: plaintext; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ الجداول */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .93rem; }
thead th {
  text-align: right; font-size: .82rem; color: var(--muted); font-weight: 700;
  border-bottom: 2px solid var(--line); padding: .55rem .5rem; white-space: nowrap;
  background: var(--card); position: sticky; top: 0;
}
tbody td { padding: .55rem .5rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr:hover { background: #f8fafc; }
tbody tr.void td { opacity: .5; text-decoration: line-through; }
tfoot td { padding: .6rem .5rem; font-weight: 700; border-top: 2px solid var(--line); }
.row-actions { display: flex; gap: .25rem; justify-content: flex-start; }

.badge {
  display: inline-block; padding: .1rem .5rem; border-radius: 999px;
  font-size: .78rem; font-weight: 700;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.muted { background: var(--bg); color: var(--muted); }

/* شريط نسخة الاطلاع */
.ro-bar {
  background: var(--warn-soft); color: var(--warn);
  border-bottom: 1px solid #fde68a;
  padding: .45rem 1rem; text-align: center;
  font-size: .88rem; font-weight: 600;
  position: sticky; top: 92px; z-index: 24;
}
/* في نسخة الاطلاع نُخفي كل ما يُدخل أو يعدّل، فلا يضغطه أحد ثم يُرفض */
body.read-only .btn.primary,
body.read-only .btn.danger,
body.read-only .row-actions .icon-btn,
body.read-only #logoFile { display: none !important; }

.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.empty .big { font-size: 2.5rem; display: block; margin-bottom: .5rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* ------------------------------------------------------------------ النوافذ */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 1rem; z-index: 50; overflow-y: auto;
}
.modal {
  background: #fff; border-radius: 14px; width: 100%; max-width: 620px;
  margin: auto; box-shadow: 0 20px 40px rgba(15, 23, 42, .25);
  display: flex; flex-direction: column; max-height: 92vh;
}
.modal.wide { max-width: 900px; }
.modal-head {
  display: flex; align-items: center; gap: .5rem;
  padding: .85rem 1rem; border-bottom: 1px solid var(--line);
}
.modal-head h3 { flex: 1; margin: 0; }
.modal-body { padding: 1rem; overflow-y: auto; }
.modal-foot {
  display: flex; gap: .5rem; justify-content: flex-start;
  padding: .85rem 1rem; border-top: 1px solid var(--line); background: #fafafa;
  border-radius: 0 0 14px 14px;
}

/* ------------------------------------------------------------ سطور الفاتورة */
.lines-table td { padding: .3rem .25rem; }
.lines-table input, .lines-table select { min-height: 38px; padding: .35rem .5rem; font-size: .92rem; }
.totals-box {
  background: var(--bg); border-radius: var(--radius); padding: .75rem 1rem; margin-top: .75rem;
}
.totals-box .row { display: flex; justify-content: space-between; padding: .2rem 0; }
.totals-box .row.grand { font-size: 1.15rem; font-weight: 700; color: var(--brand); border-top: 1px solid var(--line); margin-top: .35rem; padding-top: .45rem; }

/* -------------------------------------------------------------------- التنبيه */
.toast {
  position: fixed; bottom: 1.25rem; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: .65rem 1.1rem; border-radius: 999px;
  z-index: 100; font-size: .92rem; box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
  max-width: 90vw; text-align: center;
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ------------------------------------------------------------------- الموبايل */
@media (max-width: 760px) {
  body { font-size: 15px; }
  .container { padding: .75rem; }
  .tabs { top: 50px; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .stats { grid-template-columns: 1fr 1fr; gap: .5rem; }
  .stat .value { font-size: 1.2rem; }

  /* تحويل الجداول إلى بطاقات */
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td { display: block; width: 100%; }
  table.responsive tr {
    border: 1px solid var(--line); border-radius: 10px; margin-bottom: .6rem;
    padding: .5rem .65rem; background: #fff;
  }
  table.responsive td { border: 0; padding: .22rem 0; display: flex; justify-content: space-between; gap: .75rem; }
  table.responsive td::before {
    content: attr(data-label); color: var(--muted); font-size: .82rem; font-weight: 600; flex: 0 0 auto;
  }
  table.responsive td[data-label=""]::before { content: none; }
  table.responsive tfoot { display: block; }
  table.responsive tfoot tr { background: var(--brand-soft); }
  .row-actions { justify-content: flex-end; }
  .modal-foot .btn { flex: 1; }
}

/* -------------------------------------------------------------------- الطباعة */
.print-area { display: none; }
@media print {
  .topbar, .tabs, .modal-foot, .no-print, .toast { display: none !important; }
  body, .modal-backdrop { background: #fff; }
  .modal-backdrop { position: static; padding: 0; overflow: visible; }
  .modal { box-shadow: none; max-width: 100%; max-height: none; border-radius: 0; }
  .modal-body { overflow: visible; padding: 0; }
  .card { border: 0; box-shadow: none; padding: 0; }
  @page { margin: 12mm; }
}

/* ------------------------------------------------------ ملصقات الباركود */
.labels-sheet {
  display: flex; flex-wrap: wrap; gap: 4px; margin-top: .75rem;
}
.labels-sheet .label {
  width: 48mm; border: 1px dashed var(--line); border-radius: 4px;
  padding: 3px 4px; text-align: center; background: #fff;
  break-inside: avoid; page-break-inside: avoid;
}
.labels-sheet .l-name {
  font-size: 9pt; font-weight: 700; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.labels-sheet .bc { display: block; margin: 1px 0; }
.labels-sheet .l-code { font-size: 8pt; letter-spacing: .5px; }
.labels-sheet .l-price { font-size: 10pt; font-weight: 700; }
@media print {
  .labels-sheet .label { border: none; }
  .labels-sheet { gap: 2px; }
}

/* ------------------------------------------- طباعة إيصال حراري 80 مم */
@media print {
  body.receipt-print .modal { max-width: 72mm; }
  body.receipt-print .invoice-sheet { font-size: 9pt; line-height: 1.35; }
  body.receipt-print .invoice-sheet .head {
    display: block; text-align: center; border-bottom: 1px dashed #000;
  }
  body.receipt-print .invoice-sheet .co-block {
    display: block; text-align: center;
  }
  body.receipt-print .invoice-sheet .logo-img { max-height: 34px; margin: 0 auto; }
  body.receipt-print .invoice-sheet .co { font-size: 11pt; color: #000; }
  body.receipt-print .invoice-sheet .meta { text-align: center; font-size: 8pt; }
  body.receipt-print .invoice-sheet table { font-size: 8pt; }
  body.receipt-print .invoice-sheet thead th {
    background: none; border-bottom: 1px solid #000; padding: 2px 1px;
  }
  body.receipt-print .invoice-sheet tbody td { padding: 2px 1px; border: none; }
  body.receipt-print .totals-box { background: none; padding: 4px 0; }
  body.receipt-print .totals-box .row.grand { font-size: 10pt; color: #000; }
  body.receipt-print .table-wrap { overflow: visible; }
  body.receipt-print { background: #fff; }
}
/* حجم الورقة لا يُضبط داخل مُحدِّد، فتُحقن قاعدة @page عند الطباعة من app.js */

/* ------------------------------------------------------------- ورقة الفاتورة */
.invoice-sheet { font-size: .95rem; }
.invoice-sheet .head {
  display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  border-bottom: 2px solid var(--brand); padding-bottom: .75rem; margin-bottom: .85rem;
}
.invoice-sheet .head .co { font-size: 1.3rem; font-weight: 700; color: var(--brand); }
.invoice-sheet .co-block { display: flex; align-items: center; gap: .75rem; }
.invoice-sheet .logo-img {
  max-height: 64px; max-width: 160px; object-fit: contain;
}
@media print { .invoice-sheet .logo-img { max-height: 56px; } }
.invoice-sheet .meta { text-align: left; font-size: .88rem; }
.invoice-sheet table { font-size: .9rem; }
.invoice-sheet thead th { background: var(--bg); }
