:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #17201d;
  --muted: #66736e;
  --line: #e3e8e6;
  --soft: #eef2f1;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --a: #2563eb;
  --a-soft: #e8efff;
  --b: #c2410c;
  --b-soft: #fdeee6;
  --danger: #c0262d;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 4px 14px rgb(0 0 0 / .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --surface: #171e1c;
    --text: #e7eeeb;
    --muted: #94a39d;
    --line: #26302d;
    --soft: #1e2724;
    --accent: #2bb3a5;
    --accent-ink: #062320;
    --a: #7aa2ff;
    --a-soft: #1b2643;
    --b: #fb8a52;
    --b-soft: #3a2116;
    --danger: #ff6b70;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input { font: inherit; color: inherit; }
h1, h2 { margin: 0; }
h2 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }
[hidden] { display: none !important; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; }

/* ---- عناصر عامة ---- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input {
  width: 100%; min-height: 44px; padding: 8px 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  color: var(--text); font-size: 16px; outline: none;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
input[type="date"] { min-width: 0; }
.btn {
  min-height: 44px; padding: 0 18px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .6; }
.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center;
  border: 0; background: transparent; border-radius: 10px; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--soft); color: var(--text); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; font-weight: 600; padding: 8px; }
.err { color: var(--danger); margin: 0; font-size: 14px; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 32px; height: 32px; display: grid; place-items: center; border-radius: 9px; background: var(--accent); color: var(--accent-ink); font-weight: 700; }

/* ---- الدخول ---- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 14px; padding: 24px; }
.login-card h1 { font-size: 22px; }
.login-card .brand { justify-content: center; margin-bottom: 6px; }

/* ---- الهيدر والتابات ---- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.me { display: flex; align-items: center; gap: 2px; color: var(--muted); }
.me span { margin-inline-end: 6px; font-weight: 600; color: var(--text); }
.tabs { display: flex; gap: 6px; max-width: 1100px; margin: 14px auto 0; padding: 0 16px; }
.tabs button {
  flex: 1; max-width: 180px; min-height: 42px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 10px; cursor: pointer; font-weight: 600; color: var(--muted);
}
.tabs button.on { background: var(--text); color: var(--bg); border-color: var(--text); }

.wrap { max-width: 1100px; margin: 0 auto; padding: 14px 16px 40px; display: grid; gap: 14px; }
.wrap > *, .exp-form > * { min-width: 0; }

/* ---- فورم المصروف ---- */
.exp-form { display: grid; gap: 12px; grid-template-columns: repeat(4, minmax(0, 1fr)); align-items: end; }
.f-title { grid-column: span 2; }
.f-date { grid-column: span 1; }
.f-due { grid-column: span 1; }
.f-amount { grid-column: span 1; }
.f-method { grid-column: span 1; }
.f-desc { grid-column: span 2; }
.filter-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
select {
  min-height: 44px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--text); font: inherit; font-size: 15px; max-width: 160px;
}
.grid td.meth { white-space: nowrap; color: var(--muted); font-size: 13px; }
.mrow { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; padding: 8px 0; border-top: 1px solid var(--line); align-items: center; }
.mrow:first-child { border-top: 0; }
.mrow small { color: var(--muted); }
.mbar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--soft); overflow: hidden; }
.mbar i { display: block; height: 100%; background: var(--accent); }
.who { display: grid; gap: 8px; }
.paid {
  flex-direction: row; align-items: center; gap: 8px; cursor: pointer;
  color: var(--text); font-size: 13px; min-height: 28px; user-select: none;
}
.paid input { width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--accent); }
.badge-n {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; margin-inline-start: 4px;
  border-radius: 10px; background: var(--danger); color: #fff; font-size: 12px; font-weight: 700;
}
.chip { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 8px; border-radius: 20px; white-space: nowrap; }
.chip.late { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); }
.chip.today { background: color-mix(in srgb, #d97706 18%, transparent); color: #b45309; }
.chip.soon { background: var(--a-soft); color: var(--a); }
.chip.later, .chip.none { background: var(--soft); color: var(--muted); }
.chip.paid { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); }
@media (prefers-color-scheme: dark) { .chip.today { color: #fbbf24; } }
.tick { color: var(--accent); font-weight: 700; margin-inline-start: 4px; }
.grid td.due { white-space: nowrap; font-size: 13px; }
.grid td.due small { display: block; color: var(--muted); }

/* ---- الدفع القادم ---- */
#tab-due { display: grid; gap: 14px; }
#tab-due[hidden] { display: none; }
.due-seg { background: var(--surface); border: 1px solid var(--line); }
.due-list { display: grid; gap: 10px; }
.due-item { display: grid; gap: 10px; }
.due-item.late { border-inline-start: 4px solid var(--danger); }
.due-item.today { border-inline-start: 4px solid #d97706; }
.due-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.due-head b { font-size: 16px; }
.due-head p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.due-head .amt { font-size: 17px; font-weight: 700; text-align: end; white-space: nowrap; }
.due-meta { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; font-size: 12px; color: var(--muted); }
.due-pay { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pay-btn {
  min-height: 46px; border-radius: 10px; border: 1px dashed var(--line); background: var(--surface);
  cursor: pointer; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; gap: 2px; padding: 8px 12px; font-weight: 600; text-align: start;
}
.pay-btn.a { color: var(--a); border-color: color-mix(in srgb, var(--a) 45%, transparent); }
.pay-btn.b { color: var(--b); border-color: color-mix(in srgb, var(--b) 45%, transparent); }
.pay-btn .st { font-size: 12px; color: var(--muted); font-weight: 500; }
.pay-btn.done { border-style: solid; background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); border-color: var(--accent); }
.pay-btn.done .st { color: var(--accent); }
.pay-btn:disabled { opacity: .45; cursor: default; }
.due-all { justify-self: start; }
.split {
  grid-column: 1 / -1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; gap: 12px; align-items: end;
  padding: 12px; background: var(--soft); border-radius: 12px;
}
.who-a { color: var(--a); font-weight: 600; }
.who-b { color: var(--b); font-weight: 600; }
.quick { display: flex; gap: 6px; }
.quick button {
  min-height: 44px; padding: 0 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-size: 13px; white-space: nowrap;
}
.quick button:hover { border-color: var(--accent); }
.quick button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.exp-form .err, .exp-form .meta { grid-column: 1 / -1; }
.f-submit { grid-column: 1 / -1; }
.meta { margin: 0; font-size: 12px; color: var(--muted); }

/* ---- الفلتر ---- */
.filter { display: grid; gap: 10px; padding: 12px; }
.seg { display: flex; background: var(--soft); border-radius: 10px; padding: 3px; overflow-x: auto; scrollbar-width: none; }
.seg button {
  flex: 1; min-height: 38px; min-width: 52px; border: 0; background: transparent; border-radius: 8px;
  cursor: pointer; color: var(--muted); font-weight: 600; white-space: nowrap;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .12); }
.nav-row { display: flex; align-items: center; gap: 4px; }
.nav-row b { flex: 1; text-align: center; font-size: 15px; }
.custom-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---- الإجماليات ---- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat small { display: block; color: var(--muted); font-size: 12px; }
.stat b { display: block; font-size: 20px; margin-top: 2px; }
.stat i { font-style: normal; font-size: 12px; color: var(--muted); }
.stat.a b { color: var(--a); }
.stat.b b { color: var(--b); }
.stat.a { border-top: 3px solid var(--a); }
.stat.b { border-top: 3px solid var(--b); }
.stat.t { border-top: 3px solid var(--accent); }

/* ---- الجدول ---- */
.table-card { padding: 0; overflow: hidden; }
.grid { width: 100%; border-collapse: collapse; }
.grid th { font-size: 12px; font-weight: 600; color: var(--muted); text-align: start; padding: 10px 12px; background: var(--soft); }
.grid td { padding: 11px 12px; border-top: 1px solid var(--line); vertical-align: top; }
.grid tbody tr { cursor: pointer; }
.grid tbody tr:hover { background: var(--soft); }
.grid .ca, .grid td.ca { color: var(--a); }
.grid .cb, .grid td.cb { color: var(--b); }
.grid td.desc { color: var(--muted); max-width: 280px; }
.grid td.date { white-space: nowrap; color: var(--muted); }
.grid tfoot td { font-weight: 700; background: var(--soft); border-top: 2px solid var(--line); }
.grid tbody tr.new { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: color-mix(in srgb, var(--accent) 22%, transparent); } }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; margin: 0; }

.pager { display: flex; justify-content: center; align-items: center; gap: 6px; padding: 12px; flex-wrap: wrap; }
.pager button {
  min-width: 38px; height: 38px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer;
}
.pager button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.pager button:disabled { opacity: .4; cursor: default; }
.pager span { color: var(--muted); padding: 0 4px; }

/* ---- التقارير ---- */
.legend { display: flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); margin: -4px 0 10px; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.dot.a { background: var(--a); }
.dot.b { background: var(--b); margin-inline-start: 10px; }
.chart { display: flex; align-items: flex-end; gap: 3px; height: 190px; padding-top: 18px; overflow-x: auto; }
.bar { flex: 1 0 8px; max-width: 46px; display: flex; flex-direction: column; align-items: stretch; height: 100%; justify-content: flex-end; cursor: pointer; position: relative; }
.bar .stack { display: flex; flex-direction: column-reverse; border-radius: 4px 4px 0 0; overflow: hidden; min-height: 2px; background: var(--line); }
.bar .sa { background: var(--a); }
.bar .sb { background: var(--b); }
.bar em { font-style: normal; font-size: 10px; color: var(--muted); text-align: center; margin-top: 4px; white-space: nowrap; min-height: 15px; }
.bar.sel .stack { outline: 2px solid var(--text); outline-offset: 1px; }
.chart-tip { font-size: 13px; color: var(--muted); min-height: 20px; margin-top: 8px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.urow { display: flex; justify-content: space-between; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.urow:first-child { border-top: 0; }
.urow small { color: var(--muted); }
.toplist { margin: 0; padding-inline-start: 20px; display: grid; gap: 8px; }
.toplist li span { color: var(--muted); font-size: 12px; margin-inline-start: 6px; }
.toplist li b { float: inline-end; }
.log { list-style: none; margin: 0; padding: 0; }
.log li { display: grid; grid-template-columns: auto 1fr; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.log li:first-child { border-top: 0; }
.log .badge { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: 14px; }
.log .badge.add { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); }
.log .badge.edit { background: var(--a-soft); color: var(--a); }
.log .badge.delete { background: color-mix(in srgb, var(--danger) 13%, transparent); color: var(--danger); }
.log small { display: block; color: var(--muted); font-size: 12px; }
.log .chg { font-size: 13px; color: var(--muted); }

/* ---- الحوار ---- */
dialog {
  width: min(560px, calc(100% - 24px)); border: 1px solid var(--line); border-radius: 18px; padding: 18px;
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog .exp-form h2 { grid-column: 1 / -1; margin: 0; }
.dlg-actions { grid-column: 1 / -1; display: flex; gap: 8px; }
.dlg-actions .danger { margin-inline-start: auto; }
#pwForm { grid-template-columns: 1fr; }

.toast {
  position: fixed; inset-inline: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); margin: 0 auto; max-width: 360px;
  background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; text-align: center;
  opacity: 0; transform: translateY(10px); transition: .2s; pointer-events: none; z-index: 50;
}
.toast.show { opacity: 1; transform: none; }

/* ---- الموبايل ---- */
@media (max-width: 760px) {
  .exp-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .f-title, .f-desc { grid-column: 1 / -1; }
  .split { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid td.due { grid-area: 4 / 1 / 5 / 2; margin-top: 4px; }
  .grid td.meth { grid-area: 4 / 2; margin-top: 4px; text-align: end; font-size: 12px; }
  .grid td.meth:empty { display: none; }
  select { max-width: 130px; }
  .grid td.due small { display: inline; margin-inline-start: 6px; }
  .quick { grid-column: 1 / -1; }
  .quick button { flex: 1; }
  .two { grid-template-columns: 1fr; }
  .stats { gap: 8px; }
  .stat { padding: 10px; }
  .stat b { font-size: 16px; }

  /* الجدول يبقى كروت على الموبايل */
  .grid thead { display: none; }
  .grid, .grid tbody, .grid tfoot { display: block; }
  .grid tr {
    display: grid; grid-template-columns: 1fr auto; gap: 2px 12px;
    padding: 12px 14px; border-top: 1px solid var(--line);
  }
  .grid tbody tr:first-child { border-top: 0; }
  .grid td { padding: 0; border: 0; }
  .grid td.title { grid-area: 1 / 1; font-weight: 600; }
  .grid td.amt { grid-area: 1 / 2; font-weight: 700; text-align: end; }
  .grid td.desc { grid-area: 2 / 1; max-width: none; font-size: 13px; }
  .grid td.date { grid-area: 2 / 2; text-align: end; font-size: 12px; }
  .grid td.ca { grid-area: 3 / 1; }
  .grid td.cb { grid-area: 3 / 2; }
  .grid td.ca, .grid td.cb { font-size: 13px; }
  .grid td.ca::before { content: "أحمد: "; color: var(--muted); }
  .grid td.cb::before { content: "بودي: "; color: var(--muted); }
  .grid td.cb { text-align: end; }
  .grid tfoot tr { background: var(--soft); }
  .grid tfoot td { border: 0; background: none; }
}
@media (max-width: 380px) {
  .stat b { font-size: 14px; }
}
