:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --ink: #17202b;
  --muted: #687586;
  --line: #e2e7ee;
  --soft: #f7f9fb;
  --primary: #0e7490;
  --primary-ink: #0b5d73;
  --primary-soft: #e3f4f8;
  --side: #0f1d26;
  --side-ink: #b9c6cf;
  --ok: #15803d;
  --ok-soft: #e6f6ea;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --bad: #c0262d;
  --bad-soft: #fdeaea;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 2px 8px rgba(16, 24, 40, .05);
  --font: 'Cairo', 'Segoe UI', Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 24px; font-weight: 800; }
h2 { font-size: 17px; font-weight: 700; margin-bottom: 12px; }
h3 { font-size: 15px; font-weight: 700; }
small { font-size: .78em; }
.muted { color: var(--muted); }
.neg { color: var(--bad); font-weight: 700; }
.pos { color: var(--ok); font-weight: 700; }
.num, td.num, th.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.print-only { display: none !important; }

/* ---------- layout ---------- */
.layout { display: flex; min-height: 100vh; }
.side {
  width: 236px; flex-shrink: 0;
  background: var(--side); color: var(--side-ink);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
  padding: 18px 12px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; }
.brand b { color: #fff; font-size: 18px; display: block; line-height: 1.2; }
.brand small { color: var(--side-ink); }
.logo {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, #22a6c4, var(--primary));
  color: #fff; font-weight: 800; font-size: 22px; font-family: Arial, sans-serif;
  display: grid; place-items: center; flex-shrink: 0;
}
.logo.big { width: 64px; height: 64px; font-size: 34px; border-radius: 16px; margin: 0 auto 8px; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
nav a, .logout {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 9px;
  color: var(--side-ink); font-weight: 600; font-size: 14.5px;
  text-decoration: none !important; background: none; border: 0; font-family: inherit; cursor: pointer; width: 100%;
}
nav a:hover, .logout:hover { background: rgba(255, 255, 255, .06); color: #fff; }
nav a.active { background: rgba(34, 166, 196, .16); color: #fff; }
nav a.nav-cta { background: var(--primary); color: #fff; margin: 4px 0 8px; }
nav a.nav-cta:hover { background: #128aa8; }
nav svg, .logout svg, .btn svg {
  width: 19px; height: 19px; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
main { flex: 1; min-width: 0; padding: 26px 30px 60px; }
#view { max-width: 1280px; margin: 0 auto; }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .sub { margin: 4px 0 0; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 18px;
}
.card > h2:first-child { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.grid2 > .card, .grid2 > div > .card:last-child { margin-bottom: 0; }
.grid2 { margin-bottom: 18px; }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 18px; }
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); border-top: 3px solid var(--primary);
}
.stat.ok { border-top-color: var(--ok); }
.stat.warn { border-top-color: var(--warn); }
.stat.bad { border-top-color: var(--bad); }
.stat-label { color: var(--muted); font-size: 13px; font-weight: 600; }
.stat-value { font-size: 23px; font-weight: 800; font-variant-numeric: tabular-nums; margin: 2px 0; }
.stat-value small { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat-sub { color: var(--muted); font-size: 12.5px; }

/* ---------- buttons & forms ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 700; font-size: 14px;
  padding: 8px 14px; border-radius: 9px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  text-decoration: none !important; white-space: nowrap; line-height: 1.4;
}
.btn:hover { border-color: #c9d2dd; background: var(--soft); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-ink); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.ok:hover { filter: brightness(.95); }
.btn.danger { color: var(--bad); }
.btn.danger:hover { background: var(--bad-soft); border-color: #f3c3c5; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--soft); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.lg { padding: 11px 26px; font-size: 16px; }
.btn:disabled { opacity: .6; cursor: wait; }
.icon-btn {
  border: 0; background: none; cursor: pointer; color: var(--muted);
  font-size: 20px; line-height: 1; padding: 4px 8px; border-radius: 6px;
}
.icon-btn:hover { background: var(--bad-soft); color: var(--bad); }

label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 13.5px; color: #3a4654; }
input, select, textarea {
  font: inherit; font-size: 14.5px; color: var(--ink);
  border: 1px solid #d5dce5; border-radius: 9px; padding: 8px 11px; background: #fff;
  width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(14, 116, 144, .25); border-color: var(--primary); }
input[type=number] { font-variant-numeric: tabular-nums; }
textarea { resize: vertical; }
label.check { flex-direction: row; align-items: center; gap: 8px; cursor: pointer; white-space: nowrap; }
label.check input { width: auto; }
.form { display: flex; flex-direction: column; gap: 14px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.with-btn { display: flex; gap: 6px; }
.with-btn > :first-child { flex: 1; }
.hint { font-size: 13px; color: var(--muted); background: var(--soft); border-radius: 8px; padding: 8px 11px; }
.hint:empty { display: none; }
.hint b { color: var(--ink); }

.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; padding: 14px 16px; }
.filters label { flex: 1 1 150px; }
.filters > input[type=search] { flex: 1 1 260px; width: auto; }

.tabs { display: flex; gap: 4px; margin-bottom: 14px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.tabs a {
  padding: 9px 16px; font-weight: 700; color: var(--muted); border-bottom: 3px solid transparent;
  margin-bottom: -1px; text-decoration: none !important;
}
.tabs a:hover { color: var(--ink); }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th {
  text-align: right; font-weight: 700; color: var(--muted); font-size: 12.5px;
  padding: 9px 10px; border-bottom: 1px solid var(--line); background: var(--soft); white-space: nowrap;
}
td { padding: 9px 10px; border-bottom: 1px solid #eef1f5; vertical-align: middle; }
tbody tr:hover td { background: #fafcfd; }
tfoot td { font-weight: 800; background: var(--soft); border-bottom: 0; border-top: 2px solid var(--line); }
td.actions-cell { white-space: nowrap; text-align: left; }
td.wrap { white-space: normal; min-width: 140px; color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.loading { text-align: center; color: var(--muted); padding: 60px 0; }

.badge {
  display: inline-block; padding: 1px 10px; border-radius: 99px; font-size: 12px; font-weight: 700; white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.gray { background: #eef1f5; color: var(--muted); }

.bar { height: 8px; background: #eef2f6; border-radius: 99px; overflow: hidden; min-width: 80px; }
.bar > span { display: block; height: 100%; background: var(--primary); border-radius: 99px; }

/* ---------- invoice form ---------- */
table.items td { padding: 6px 5px; border-bottom: 0; vertical-align: top; }
table.items tbody tr:hover td { background: none; }
table.items input, table.items select { padding: 7px 8px; }
table.items .i-sheets, table.items .i-copies, table.items .i-price { width: 90px; }
table.items .i-total { font-weight: 800; padding-top: 14px; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.items .i-hint { font-size: 11.5px; color: var(--primary); min-height: 0; }
.inv-bottom { display: grid; grid-template-columns: 1fr 380px; gap: 24px; align-items: start; }
.totals { background: var(--soft); border-radius: 10px; padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.totals > div { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.totals > div > span { color: var(--muted); font-weight: 600; white-space: nowrap; }
.totals > div > :last-child:not(span) { max-width: 200px; }
.totals b { font-size: 16px; font-variant-numeric: tabular-nums; }
.totals .big { border-top: 1px dashed #cbd3dd; padding-top: 8px; }
.totals .big b { font-size: 22px; color: var(--primary); }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; border-top: 1px solid var(--line); padding-top: 14px; }

/* ---------- invoice document ---------- */
.doc { padding: 30px 34px; }
.doc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; padding-bottom: 18px; border-bottom: 2px solid var(--ink); margin-bottom: 18px; }
.doc-shop { font-size: 26px; font-weight: 800; }
.doc-no { text-align: left; }
.doc-no b { font-size: 26px; display: block; line-height: 1.2; }
.doc-to { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; font-size: 16px; }
.doc-totals { display: flex; justify-content: flex-end; margin-top: 16px; }
.doc-totals .totals { min-width: 320px; }
.doc-notes { margin-top: 16px; padding: 10px 14px; border-right: 3px solid var(--line); color: var(--muted); }
.doc-foot { margin-top: 26px; padding-top: 14px; border-top: 1px dashed var(--line); display: flex; justify-content: space-between; flex-wrap: wrap; gap: 10px; color: var(--muted); }

/* ---------- modal & toast ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(15, 29, 38, .5); z-index: 50;
  display: grid; place-items: center; padding: 16px; overflow-y: auto;
}
.modal-box { background: #fff; border-radius: 14px; width: 100%; max-width: 520px; box-shadow: 0 20px 50px rgba(0, 0, 0, .25); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head .x { border: 0; background: none; font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted); }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 13px; }
.modal-foot { display: flex; gap: 8px; padding-top: 4px; }
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: #fff; padding: 10px 22px; border-radius: 10px; font-weight: 700;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2); max-width: 90vw;
}
.toast.bad { background: var(--bad); }

/* ---------- auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(160deg, var(--side), #123848); }
.auth-box { background: #fff; padding: 32px 28px; border-radius: 16px; width: 100%; max-width: 360px; text-align: center; display: flex; flex-direction: column; gap: 12px; }
.auth-box h1 { font-size: 26px; }
.auth-box p { margin: 0 0 6px; }

/* ---------- responsive ---------- */
@media screen and (max-width: 1000px) {
  .grid2, .grid3, .inv-bottom { grid-template-columns: 1fr; }
}
table.items .i-paper { min-width: 140px; }
table.items .i-desc { min-width: 170px; }

/* ---------- mobile ---------- */
@media screen and (max-width: 860px) {
  body { font-size: 14.5px; }
  .layout { flex-direction: column; }
  /* شريط تنقل ثابت تحت الشاشة */
  .side {
    position: fixed; bottom: 0; top: auto; left: 0; right: 0; z-index: 40;
    width: 100%; height: auto; flex-direction: row; align-items: stretch;
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); gap: 0;
    overflow-x: auto; box-shadow: 0 -4px 16px rgba(0, 0, 0, .18);
    scrollbar-width: none;
  }
  .side::-webkit-scrollbar { display: none; }
  .brand { display: none; }
  nav { flex-direction: row; gap: 0; flex: none; }
  nav a, .logout {
    flex-direction: column; gap: 2px; width: auto; min-width: 68px;
    padding: 6px 6px; font-size: 11px; white-space: nowrap; border-radius: 8px; text-align: center;
  }
  nav a.nav-cta { margin: 0; }
  nav svg, .logout svg { width: 21px; height: 21px; }
  main { padding: 14px 12px 96px; }
  h1 { font-size: 20px; }
  .page-head { margin-bottom: 14px; align-items: stretch; flex-direction: column; gap: 10px; }
  .page-head .actions { width: 100%; }
  .page-head .actions .btn { flex: 1 1 auto; }
  .card { padding: 14px 12px; border-radius: 10px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 10px 12px; }
  .stat-value { font-size: 19px; }
  .row2 { grid-template-columns: 1fr; }
  /* منع الزووم التلقائي على iPhone */
  input, select, textarea { font-size: 16px; }
  .filters label, .filters > input[type=search] { flex: 1 1 100%; }
  th, td { padding: 7px 7px; }
  table { font-size: 13.5px; }
  .tabs a { padding: 8px 10px; font-size: 14px; }
  .doc { padding: 14px 12px; }
  .doc-head { flex-direction: column; gap: 10px; }
  .doc-no { text-align: right; }
  .doc-totals .totals { min-width: 0; width: 100%; }
  .modal { place-items: end center; padding: 0; }
  .modal-box { border-radius: 16px 16px 0 0; max-height: 92vh; overflow-y: auto; }
  .modal-foot .btn { flex: 1; }
  .form-actions .btn { width: 100%; }
  .totals > div > :last-child:not(span) { max-width: 60%; }

  /* بنود الفاتورة تتحول لكروت */
  table.items thead { display: none; }
  table.items, table.items tbody { display: block; }
  table.items tr {
    display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px;
    border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 10px; background: var(--soft);
  }
  table.items td { display: block; padding: 0; }
  table.items td[data-label]::before { content: attr(data-label); display: block; font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 2px; }
  table.items td:nth-child(1), table.items td:nth-child(2) { grid-column: 1 / -1; }
  table.items .i-paper, table.items .i-desc, table.items .i-sheets, table.items .i-copies, table.items .i-price { min-width: 0; width: 100%; }
  table.items .i-total { grid-column: 1 / 3; padding-top: 0; font-size: 17px; }
  table.items .i-del-cell { text-align: left; align-self: end; }
  .table-wrap:has(table.items) { overflow: visible; margin: 0; }
}

/* ---------- print ---------- */
@page { size: A4; margin: 12mm; }
@media print {
  body { background: #fff; font-size: 12.5px; }
  .no-print, .side, .modal, .toast, .tabs, .btn, .filters { display: none !important; }
  .print-only { display: block !important; }
  main { padding: 0; }
  .card, .stat { box-shadow: none; border-color: #ccc; break-inside: avoid; }
  .card { padding: 10px 0; border: 0; }
  .doc { padding: 0; }
  .stats { grid-template-columns: repeat(3, 1fr); }
  th { background: #f0f0f0 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  td, th { padding: 5px 6px; }
  tr { break-inside: avoid; }
  a { color: inherit; }
  .print-brand { font-weight: 800; border-bottom: 1px solid #999; padding-bottom: 6px; margin-bottom: 12px; display: flex !important; justify-content: space-between; }
  .is-doc .print-brand { display: none !important; }
  .badge { border: 1px solid currentColor; }
}

/* ---------- statement ---------- */
.st-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 8px; }
.st-box { border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; display: flex; flex-direction: column; gap: 2px; border-top: 3px solid var(--primary); }
.st-box.ok { border-top-color: var(--ok); }
.st-box.bad { border-top-color: var(--bad); }
.st-box span { color: var(--muted); font-size: 12.5px; font-weight: 600; }
.st-box b { font-size: 19px; font-variant-numeric: tabular-nums; }
.st-title { margin: 22px 0 10px; padding-bottom: 6px; border-bottom: 2px solid var(--ink); font-size: 16px; }
.inv-block { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 14px; overflow: hidden; break-inside: avoid; }
.inv-block-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 14px; background: var(--soft); border-bottom: 1px solid var(--line); }
.inv-block-head > div { display: flex; gap: 12px; align-items: baseline; flex-wrap: wrap; }
.inv-block-head b { font-size: 15.5px; }
.inv-block .table-wrap { margin: 0; }
table.inv-lines th { background: #fff; font-size: 12px; }
table.inv-lines td, table.inv-lines th { padding: 7px 12px; }
.inv-block-sum { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px 26px; padding: 9px 14px; border-top: 1px dashed var(--line); }
.inv-block-sum > div { display: flex; gap: 8px; align-items: baseline; }
.inv-block-sum span { color: var(--muted); font-size: 13px; font-weight: 600; }
.inv-block-sum b { font-variant-numeric: tabular-nums; }
.inv-block-notes { padding: 0 14px 10px; font-size: 13px; }
.inv-grand { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; padding: 12px 14px; border-radius: 10px; background: var(--ink); color: #fff; }
.inv-grand > div { display: flex; flex-direction: column; }
.inv-grand span { font-size: 12px; opacity: .75; }
.inv-grand b { font-size: 17px; font-variant-numeric: tabular-nums; }
.inv-grand .pos { color: #7ee2a0; }
.inv-grand .neg { color: #ffaaae; }
.pick-list { display: flex; flex-direction: column; gap: 6px; max-height: 280px; overflow-y: auto; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
.pick-list label.check { font-weight: 500; white-space: normal; }
@media print {
  .st-summary { grid-template-columns: repeat(4, 1fr); }
  .inv-block-head, .inv-grand, .st-box { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .inv-block { margin-bottom: 10px; }
  .inv-block-head, .inv-block-sum { padding: 6px 10px; }
  table.inv-lines td, table.inv-lines th { padding: 5px 8px; }
}
/* جداول البنود على الموبايل: كل بند كارت واضح */
@media screen and (max-width: 860px) {
  table.stack thead, table.inv-lines thead { display: none; }
  table.stack, table.stack tbody, table.inv-lines, table.inv-lines tbody { display: block; }
  table.stack tr, table.inv-lines tr {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 16px;
    padding: 10px 12px; border-bottom: 1px solid var(--line);
  }
  table.stack td, table.inv-lines td { display: flex; align-items: baseline; gap: 4px; padding: 2px 0; border: 0; white-space: normal; }
  table.stack td::before, table.inv-lines td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; margin-inline-end: auto; }
  table.stack td[data-label="م"], table.inv-lines td[data-label="م"] { display: none; }
  table.stack td[data-label="نوع الورق"], table.inv-lines td[data-label="نوع الورق"],
  table.stack td[data-label="البيان"], table.inv-lines td[data-label="البيان"] { grid-column: 1 / -1; }
  table.stack td[data-label="الإجمالي"], table.inv-lines td[data-label="الإجمالي"] { grid-column: 1 / -1; border-top: 1px dashed var(--line); padding-top: 6px; font-size: 15px; }
  .table-wrap:has(.stack), .table-wrap:has(.inv-lines) { overflow: visible; margin: 0; }
  .inv-block-sum { justify-content: flex-start; }
}