:root {
  --bg: #f3f6fa;
  --card: #ffffff;
  --ink: #1b2533;
  --muted: #6b7889;
  --line: #dde3ea;
  --primary: #1f3f8f;
  --primary-ink: #ffffff;
  --accent: #0c8f7a;
  --danger: #c0392b;
  --warn: #c88a00;
  --radius: 10px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Cairo", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }
h1, h2, h3 { margin: 0 0 .6rem; }
a { color: var(--primary); }

/* ---------- أزرار وحقول ---------- */
.btn {
  font: inherit; cursor: pointer; border: 1px solid var(--line);
  background: #fff; color: var(--ink); padding: .5rem 1rem; border-radius: 8px;
  text-decoration: none; display: inline-flex; align-items: center; gap: .35rem; white-space: nowrap;
}
.btn:hover { filter: brightness(.97); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.accent { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.danger { color: var(--danger); border-color: #e7b8b2; }
.btn.ghost { background: transparent; }
.btn.small { padding: .25rem .6rem; font-size: .85rem; }
.btn:disabled { opacity: .6; cursor: wait; }
label { display: flex; flex-direction: column; gap: .25rem; font-weight: 600; font-size: .9rem; }
input, select, textarea {
  font: inherit; font-weight: 400; padding: .5rem .6rem; border: 1px solid var(--line);
  border-radius: 8px; background: #fff; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9db4ea; border-color: var(--primary); }

/* ---------- تخطيط ---------- */
.topbar {
  background: var(--primary); color: #fff; display: flex; align-items: center; gap: 1rem;
  padding: .6rem 1.2rem; flex-wrap: wrap; position: sticky; top: 0; z-index: 5;
}
.topbar .brand { font-weight: 700; font-size: 1.15rem; }
.topbar nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.topbar nav a { color: #dfe7ff; text-decoration: none; padding: .35rem .75rem; border-radius: 6px; }
.topbar nav a.active, .topbar nav a:hover { background: rgba(255,255,255,.15); color: #fff; }
.topbar .user { display: flex; align-items: center; gap: .5rem; }
.topbar .user .muted { color: #c8d4f2; font-size: .85rem; }
.topbar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.4); }
main { max-width: 1150px; margin: 0 auto; padding: 1.2rem 1rem 3rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; margin-bottom: 1rem; }
.row { display: flex; gap: .75rem; flex-wrap: wrap; align-items: center; }
.row.between { justify-content: space-between; }
.grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.stats { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1rem; }
.stat b { display: block; font-size: 1.5rem; }
.badge { display: inline-block; padding: .05rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 600; background: #e8eefb; color: var(--primary); }
.badge.sale { background: #dff4ef; color: var(--accent); }
.badge.warn { background: #fff1cc; color: #7a5600; }
.badge.ok { background: #dff4e3; color: #1d7a35; }

/* ---------- جداول ---------- */
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; }
table.list th, table.list td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); text-align: right; vertical-align: middle; }
table.list th { font-size: .82rem; color: var(--muted); font-weight: 600; background: #f8fafc; }
table.list tr.clickable { cursor: pointer; }
table.list tr.clickable:hover td { background: #f6f9ff; }
.num { font-variant-numeric: tabular-nums; direction: ltr; text-align: left !important; }
.empty { text-align: center; padding: 2rem; color: var(--muted); }

/* ---------- نموذج الأصناف ---------- */
table.items { width: 100%; border-collapse: collapse; }
table.items th { font-size: .82rem; color: var(--muted); text-align: right; padding: .3rem; }
table.items td { padding: .25rem; }
table.items td.amount input { direction: ltr; text-align: left; }
.total-box { font-size: 1.25rem; font-weight: 700; }

/* ---------- الدخول ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: .8rem; }

/* ---------- PDF preview ---------- */
.pdf-frame { width: 100%; height: 80vh; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }

.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: .6rem 1.2rem; border-radius: 8px; z-index: 50;
  box-shadow: 0 4px 16px rgba(0,0,0,.2);
}
.toast.err { background: var(--danger); }

.topbar nav i { font-style: normal; margin-inline-end: .3rem; }
.m-only { display: none; }
.inv-preview { overflow: hidden; direction: ltr; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; }
#inv-preview-inner { width: 760px; transform-origin: 0 0; }

/* ================================================================ الجوال */
@media (max-width: 760px) {
  body { font-size: 15px; padding-bottom: calc(70px + env(safe-area-inset-bottom)); }
  input, select, textarea { font-size: 16px; } /* يمنع التكبير التلقائي في الآيفون */
  main { padding: .8rem .75rem 1.5rem; }
  h2 { font-size: 1.25rem; }
  .card { padding: .85rem; border-radius: 12px; }
  .d-only { display: none !important; }
  .m-only { display: initial; }
  div.m-only, .inv-preview.m-only { display: block; }

  /* الشريط العلوي + قائمة سفلية مثل التطبيقات */
  .topbar { padding: calc(.5rem + env(safe-area-inset-top)) .75rem .5rem; gap: .5rem; }
  .topbar .brand { flex: 1; font-size: 1.05rem; }
  .topbar .user #user-email { display: none; }
  .topbar nav {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; gap: 0;
    background: #fff; border-top: 1px solid var(--line); box-shadow: 0 -2px 12px rgba(0,0,0,.06);
    padding: .3rem .25rem calc(.3rem + env(safe-area-inset-bottom)); flex-wrap: nowrap;
  }
  .topbar nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px;
    color: var(--muted); font-size: .72rem; padding: .3rem 0; border-radius: 10px;
  }
  .topbar nav a i { font-size: 1.25rem; margin: 0; line-height: 1.2; }
  .topbar nav a.active, .topbar nav a:hover { background: #eef3ff; color: var(--primary); }
  .topbar nav a.nav-new i {
    background: var(--primary); color: #fff; width: 42px; height: 42px; border-radius: 50%;
    display: grid; place-items: center; font-size: 1.5rem; margin-top: -18px; box-shadow: 0 3px 10px rgba(31,63,143,.35);
  }
  .topbar nav a.nav-new.active, .topbar nav a.nav-new:hover { background: transparent; }

  /* أزرار أكبر للمس */
  .btn { padding: .65rem 1rem; justify-content: center; }
  .btn.small { padding: .4rem .7rem; }
  .row.between > h2 { width: 100%; }
  .row.between > .btn.primary { width: 100%; }
  .actions { display: grid; grid-template-columns: 1fr 1fr; width: 100%; gap: .5rem; }
  .actions .btn.accent, .actions .btn.primary { grid-column: 1 / -1; font-size: 1.05rem; }

  .stats { grid-template-columns: 1fr 1fr; gap: .5rem; }
  .stat { padding: .7rem; }
  .stat b { font-size: 1.2rem; }
  .grid { grid-template-columns: 1fr 1fr; gap: .6rem; }
  .grid { grid-auto-flow: dense; }
  .grid > .full { grid-column: 1 / -1; }

  /* الجداول تتحوّل إلى بطاقات */
  table.cards thead { display: none; }
  table.cards, table.cards tbody { display: block; }
  table.cards tr {
    display: grid; grid-template-columns: 1fr auto; gap: .15rem .5rem; align-items: center;
    grid-template-areas: "no total" "name date" "items items" "pdf wa";
    border: 1px solid var(--line); border-radius: 12px; padding: .7rem .8rem; margin-bottom: .6rem; background: #fff;
  }
  table.cards td { border: 0; padding: 0; background: none !important; }
  table.cards td.c-no { grid-area: no; }
  table.cards td.c-total { grid-area: total; font-size: 1.05rem; }
  table.cards td.c-name { grid-area: name; font-weight: 600; }
  table.cards td.c-date { grid-area: date; color: var(--muted); font-size: .85rem; text-align: left; }
  table.cards td.c-items { grid-area: items; font-size: .85rem; }
  table.cards td.c-pdf { grid-area: pdf; }
  table.cards td.c-wa { grid-area: wa; text-align: left; }
  table.cards td.c-kind { display: none; }
  /* بطاقة العميل: الاسم + النوع */
  #c-list table.cards tr { grid-template-areas: "name total" "kind date" "items items"; }
  #c-list table.cards td.c-kind { display: block; grid-area: kind; }

  /* أصناف الفاتورة: كل صنف بطاقة */
  table.items thead { display: none; }
  table.items, table.items tbody { display: block; }
  table.items tr {
    display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .4rem;
    border: 1px solid var(--line); border-radius: 12px; padding: .6rem; margin-bottom: .6rem; position: relative;
  }
  table.items td { padding: 0; }
  table.items td[data-label]::before { content: attr(data-label); display: block; font-size: .75rem; color: var(--muted); margin-bottom: 2px; }
  table.items td.item { grid-column: 1 / -1; padding-left: 2.6rem; }
  table.items td.date { grid-column: 1 / -1; }
  table.items td.del { position: absolute; left: .5rem; top: .5rem; }
  .total-box { width: 100%; text-align: center; background: #eef3ff; border-radius: 10px; padding: .6rem; }

  .toast { bottom: calc(80px + env(safe-area-inset-bottom)); width: max-content; max-width: 90vw; text-align: center; }
}
