:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #f0f2ef;
  --line: #e2e5e0;
  --text: #1b2420;
  --muted: #66716b;
  --brand: #0f5c4d;
  --brand-soft: #e3f0ec;
  --in: #157a4a;
  --in-soft: #e4f4ea;
  --out: #c2352b;
  --out-soft: #fbe9e7;
  --warn: #9a6200;
  --warn-soft: #fff4dc;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 4px 16px rgba(20, 30, 25, .05);
  --font: -apple-system, BlinkMacSystemFont, "SF Arabic", "Segoe UI", "Noto Sans Arabic", Tahoma, sans-serif;
  --num: "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111614; --surface: #1a201d; --surface-2: #222a26; --line: #2e3833; --text: #e8eeea; --muted: #97a39c;
    --brand: #4fb39b; --brand-soft: #1d3530; --in: #4cc286; --in-soft: #183526; --out: #ff7a6e; --out-soft: #3a1f1c;
    --warn: #f0b74d; --warn-soft: #3a2d12; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111614; --surface: #1a201d; --surface-2: #222a26; --line: #2e3833; --text: #e8eeea; --muted: #97a39c;
  --brand: #4fb39b; --brand-soft: #1d3530; --in: #4cc286; --in-soft: #183526; --out: #ff7a6e; --out-soft: #3a1f1c;
  --warn: #f0b74d; --warn-soft: #3a2d12; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.5; -webkit-text-size-adjust: 100%; }
body { min-height: 100dvh; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); text-decoration: none; }
.num, .amount { font-family: var(--num); font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.boot { display: grid; place-items: center; height: 100dvh; color: var(--muted); }
[hidden] { display: none !important; }

/* التخطيط: شريط سفلي على الهاتف وقائمة جانبية يمينية على الكمبيوتر */
.layout { display: flex; min-height: 100dvh; }
.side { display: none; }
.main { flex: 1; min-width: 0; padding: 16px 16px calc(96px + env(safe-area-inset-bottom)); max-width: 980px; margin: 0 auto; width: 100%; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; padding-top: env(safe-area-inset-top); }
.topbar h1 { font-size: 1.3rem; margin: 0; }
.back { background: none; border: 0; font-size: 1.4rem; padding: 4px 8px; cursor: pointer; color: var(--brand); }
.tabbar { position: fixed; inset: auto 0 0 0; background: var(--surface); border-top: 1px solid var(--line); display: flex; justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); z-index: 20; }
.tabbar a { display: flex; flex: 1; flex-direction: column; align-items: center; gap: 2px; font-size: .7rem; color: var(--muted); padding: 4px 2px; min-width: 0; position: relative; white-space: nowrap; }
.tabbar a.on { color: var(--brand); font-weight: 600; }
.tabbar svg, .side svg { width: 22px; height: 22px; }
.badge { background: var(--out); color: #fff; border-radius: 99px; font-size: .68rem; padding: 0 6px; min-width: 18px; text-align: center; font-family: var(--num); }
.tabbar .badge { position: absolute; top: 0; left: 8px; }
@media (min-width: 900px) {
  .tabbar { display: none; }
  .side { display: flex; flex-direction: column; gap: 2px; width: 230px; background: var(--surface); border-left: 1px solid var(--line); padding: 20px 12px; position: sticky; top: 0; height: 100dvh; }
  .side .brand { font-weight: 700; font-size: 1.15rem; padding: 0 10px 16px; color: var(--brand); }
  .side a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px; color: var(--text); }
  .side a.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
  .side .foot { margin-top: auto; font-size: .85rem; color: var(--muted); padding: 10px; }
  .main { padding: 24px 28px 40px; }
}

/* البطاقات والقوائم */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); padding: 14px; margin-bottom: 12px; }
.card h2, .card h3 { margin: 0 0 10px; font-size: 1.02rem; }
.section-title { font-size: .9rem; color: var(--muted); margin: 18px 4px 8px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.list { background: var(--surface); border-radius: var(--radius); border: 1px solid var(--line); overflow: hidden; }
.row { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); color: var(--text); }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { color: var(--muted); font-size: .82rem; }
a.row:active, button.row:active { background: var(--surface-2); }
button.row { width: 100%; background: none; border: 0; border-bottom: 1px solid var(--line); text-align: right; cursor: pointer; }
.empty { text-align: center; color: var(--muted); padding: 24px 12px; }

/* الصناديق حسب العملة */
.cur-group { margin-bottom: 12px; }
.cur-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 4px 6px; }
.cur-head .name { font-weight: 700; }
.cur-head .total { font-weight: 700; font-size: 1.15rem; }
.boxes { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.box { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; color: var(--text); }
.box .bname { font-size: .85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.box .bbal { font-weight: 700; font-size: 1.05rem; }
.box.neg .bbal { color: var(--out); }
.pending-note { font-size: .75rem; color: var(--warn); }

.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 6px 0 14px; }
.quick button { border: 0; border-radius: 16px; padding: 16px; font-size: 1.15rem; font-weight: 700; color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px; }
.quick svg { width: 26px; height: 26px; }
.quick .out { background: var(--out); }
.quick .in { background: var(--in); }
.shortcuts { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 8px; }
.shortcuts a { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 4px; text-align: center; font-size: .8rem; color: var(--text); display: flex; flex-direction: column; align-items: center; gap: 4px; }
.shortcuts svg { width: 22px; height: 22px; color: var(--brand); }
@media (min-width: 600px) { .shortcuts { grid-template-columns: repeat(7, 1fr); } }

/* النماذج */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; position: relative; }
.field > label { font-size: .85rem; color: var(--muted); font-weight: 600; }
.input, select.input, textarea.input { width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; font-size: 1rem; outline: none; min-height: 46px; }
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.input.big { font-size: 1.8rem; font-weight: 700; text-align: center; font-family: var(--num); direction: ltr; padding: 14px; }
.input.num { text-align: left; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { font-size: .8rem; color: var(--muted); }
.rate-line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.rate-line .input { width: 140px; flex: 0 0 auto; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: none; padding: 9px; border-radius: 9px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg button.on.in { color: var(--in); }
.seg button.on.out { color: var(--out); }
details.more { margin: 4px 0 12px; }
details.more summary { cursor: pointer; color: var(--brand); font-weight: 600; padding: 6px 0; list-style: none; }
details.more summary::before { content: "+ "; }
details.more[open] summary::before { content: "− "; }

.btn { border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 700; cursor: pointer; background: var(--surface-2); color: var(--text); min-height: 46px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.in { background: var(--in); color: #fff; }
.btn.out { background: var(--out); color: #fff; }
.btn.ghost { background: none; color: var(--brand); padding: 8px 10px; min-height: 0; }
.btn.danger { background: var(--out-soft); color: var(--out); }
.btn.small { padding: 6px 12px; min-height: 34px; font-size: .88rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.actions .btn { flex: 1; }

/* منتقي الحسابات */
.picker { position: relative; }
.picker .chosen { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.picker-list { position: absolute; inset: calc(100% + 4px) 0 auto 0; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.12); max-height: 300px; overflow: auto; z-index: 30; }
.picker-list button { display: flex; width: 100%; text-align: right; justify-content: space-between; gap: 8px; padding: 10px 12px; border: 0; background: none; border-bottom: 1px solid var(--line); cursor: pointer; }
.picker-list button:hover, .picker-list button.hl { background: var(--surface-2); }
.picker-list .add { color: var(--brand); font-weight: 700; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 7px 12px; cursor: pointer; font-size: .9rem; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }

/* المعاينة والأثر */
.effects { display: flex; flex-direction: column; gap: 6px; }
.effect { display: flex; justify-content: space-between; gap: 8px; align-items: center; padding: 8px 10px; border-radius: 10px; background: var(--surface-2); flex-wrap: wrap; }
.effect .ename { font-weight: 600; }
.effect .ekind { color: var(--muted); font-size: .78rem; }
.effect .arrow { color: var(--muted); }
.warn { background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 9px 12px; margin: 6px 0; font-size: .9rem; }
.warn.bad { background: var(--out-soft); color: var(--out); }
.impact { font-size: .88rem; color: var(--muted); padding: 8px 2px; min-height: 2.2em; }
table.t { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.t th, table.t td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: right; vertical-align: top; }
table.t th { color: var(--muted); font-weight: 600; font-size: .8rem; }
table.t td.n, table.t th.n { text-align: left; font-family: var(--num); font-variant-numeric: tabular-nums; direction: ltr; white-space: nowrap; }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tag { display: inline-block; font-size: .72rem; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.tag.posted { background: var(--in-soft); color: var(--in); }
.tag.cancelled { background: var(--out-soft); color: var(--out); }
.tag.draft { background: var(--warn-soft); color: var(--warn); }
.tag.pending { background: var(--warn-soft); color: var(--warn); }
.tag.offline { background: var(--brand-soft); color: var(--brand); }
.in-txt { color: var(--in); }
.out-txt { color: var(--out); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: .92rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.filters .input { flex: 1 1 140px; min-height: 40px; padding: 8px 10px; }

/* النافذة المنبثقة من الأسفل */
.sheet-back { position: fixed; inset: 0; background: rgba(10, 15, 12, .45); z-index: 40; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease; }
.sheet { background: var(--bg); width: 100%; max-width: 560px; max-height: 92dvh; overflow: auto; border-radius: 20px 20px 0 0; padding: 10px 16px calc(16px + env(safe-area-inset-bottom)); animation: up .2s ease; }
.sheet .grab { width: 40px; height: 5px; background: var(--line); border-radius: 9px; margin: 0 auto 10px; }
.sheet h2 { margin: 0 0 12px; font-size: 1.15rem; display: flex; justify-content: space-between; align-items: center; }
@media (min-width: 700px) { .sheet-back { align-items: center; } .sheet { border-radius: 20px; } }
@keyframes up { from { transform: translateY(30px); opacity: .5; } }
@keyframes fade { from { opacity: 0; } }

.toast-wrap { position: fixed; inset: calc(10px + env(safe-area-inset-top)) 12px auto 12px; display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 60; pointer-events: none; }
.toast { background: #1b2420; color: #fff; padding: 11px 14px; border-radius: 12px; display: flex; gap: 10px; align-items: center; pointer-events: auto; box-shadow: 0 6px 24px rgba(0,0,0,.2); max-width: 520px; width: 100%; }
.toast.err { background: var(--out); }
.toast .grow { flex: 1; }
.toast button { background: rgba(255,255,255,.15); color: #fff; border: 0; border-radius: 8px; padding: 6px 10px; cursor: pointer; font-weight: 600; }

.netbar { background: var(--warn-soft); color: var(--warn); font-size: .85rem; padding: 8px 12px; border-radius: 10px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.login { max-width: 380px; margin: 10dvh auto 0; padding: 0 16px; }
.login h1 { color: var(--brand); text-align: center; }
.perm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
@media (max-width: 480px) { .perm-grid { grid-template-columns: 1fr; } }
.check { display: flex; align-items: center; gap: 8px; font-size: .92rem; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--brand); }
.role-users .row { padding: 9px 12px; }
.lines-table .input { min-height: 40px; padding: 8px; font-size: .95rem; }
.inv-line { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr 1fr auto; gap: 6px; align-items: center; margin-bottom: 6px; }
@media (max-width: 640px) { .inv-line { grid-template-columns: 1fr 1fr 1fr; background: var(--surface-2); padding: 8px; border-radius: 10px; } .inv-line .desc { grid-column: 1 / -1; } }
.balance-ind { font-weight: 700; padding: 10px 12px; border-radius: 10px; text-align: center; }
.balance-ind.ok { background: var(--in-soft); color: var(--in); }
.balance-ind.no { background: var(--out-soft); color: var(--out); }

/* الطباعة */
.print-only { display: none; }
@media print {
  .tabbar, .side, .topbar .actions, .no-print, #toast-root, #sheet-root { display: none !important; }
  .print-only { display: block; }
  body, .main { background: #fff; color: #000; padding: 0; }
  .card { box-shadow: none; border: 1px solid #ccc; }
}
.card.scroll-x table.t { min-width: 600px; }
table.t td.num { white-space: nowrap; font-family: var(--num); }

/* شجرة الحسابات */
.tree .trow { display: flex; align-items: center; gap: 6px; padding: 10px 12px; padding-inline-start: calc(12px + var(--d, 0) * 18px); border-bottom: 1px solid var(--line); color: var(--text); min-height: 48px; }
.tree .trow:last-child { border-bottom: 0; }
.tree .trow.group { background: var(--surface-2); }
.tree .trow.group .tname { font-weight: 700; }
.tree .trow[hidden] { display: none; }
.tree .caret { width: 22px; height: 22px; flex: none; display: inline-flex; align-items: center; justify-content: center; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: .9rem; transition: transform .15s; padding: 0; }
.tree .caret.open { transform: rotate(-90deg); }
.tree .dot { width: 22px; flex: none; text-align: center; color: var(--muted); font-size: .7rem; }
.tree .tmain { flex: 1; min-width: 0; color: inherit; }
.tree .tname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree .tsub { color: var(--muted); font-size: .78rem; }
.tree .tbal { text-align: left; font-size: .9rem; white-space: nowrap; }
.tree .tbal .loc { display: block; color: var(--muted); font-size: .75rem; }
.tree .tact { display: flex; gap: 2px; flex: none; }
.tree .tact button { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; width: 30px; height: 30px; cursor: pointer; color: var(--text); padding: 0; }
.tree .inactive .tname { text-decoration: line-through; color: var(--muted); }
.seg { display: flex; gap: 6px; }
.seg label { flex: 1; border: 1px solid var(--line); border-radius: 10px; padding: 10px; text-align: center; cursor: pointer; }
.seg input { display: none; }
.seg label:has(input:checked) { border-color: var(--brand); background: var(--brand); color: #fff; }
.ctr { display: block; color: var(--muted); font-size: .75rem; }

/* كشف الحساب: قائمة على الهاتف، جدول على الشاشة الكبيرة وعند الطباعة */
.stmt-d { display: none; }
@media (min-width: 720px) { .stmt-m { display: none; } .stmt-d { display: block; } }
@media print { .stmt-m { display: none !important; } .stmt-d { display: block !important; } }
