:root {
  --bg: #f5f4ef; --card: #ffffff; --card2: #f0eee8; --text: #1d2a2f; --muted: #667680; --border: #e2dfd6;
  --primary: #1f7a6d; --primary-ink: #ffffff; --primary-soft: #dcefe9; --danger: #c0463b; --danger-soft: #f8e3e0;
  --shadow: 0 1px 2px rgba(20, 30, 30, .06), 0 6px 18px rgba(20, 30, 30, .06);
  --font: -apple-system, BlinkMacSystemFont, "SF Arabic", "SF Pro Text", system-ui, "Segoe UI", "Noto Naskh Arabic", "Noto Sans Arabic", Tahoma, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1518; --card: #182126; --card2: #1f2a30; --text: #e9f0f2; --muted: #92a3ab; --border: #2a3840;
    --primary: #4fb3a2; --primary-ink: #06201b; --primary-soft: #1d3b37; --danger: #ef7d71; --danger-soft: #3a1f1c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1518; --card: #182126; --card2: #1f2a30; --text: #e9f0f2; --muted: #92a3ab; --border: #2a3840;
  --primary: #4fb3a2; --primary-ink: #06201b; --primary-soft: #1d3b37; --danger: #ef7d71; --danger-soft: #3a1f1c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html.locked, html.locked body { overflow: hidden; }
body {
  margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.5;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation; overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1 { font-size: 24px; margin: 0 0 14px; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 16px; margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; display: inline-block; }

#view {
  max-width: 640px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(104px + env(safe-area-inset-bottom));
}

/* شريط التنقل السفلي */
.tabbar {
  position: fixed; bottom: 0; inset-inline: 0; z-index: 20; background: var(--card); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar-in { max-width: 640px; margin: 0 auto; display: grid; grid-template-columns: 1fr 1fr 76px 1fr 1fr; align-items: center; }
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 7px; color: var(--muted);
  text-decoration: none; font-size: 11.5px; border: 0; background: none;
}
.tab[aria-current="page"] { color: var(--primary); font-weight: 600; }
.fab {
  justify-self: center; width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--primary); color: var(--primary-ink);
  display: grid; place-items: center; margin-top: -26px; box-shadow: 0 6px 16px rgba(31, 122, 109, .4);
}
.fab:active { transform: scale(.96); }

/* بطاقات */
.card { background: var(--card); border-radius: 16px; padding: 14px; box-shadow: var(--shadow); margin-bottom: 14px; }
.card > h2 { margin-bottom: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.stat { background: var(--card); border-radius: 16px; padding: 12px 14px; box-shadow: var(--shadow); min-width: 0; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 21px; font-weight: 700; margin-top: 2px; overflow-wrap: anywhere; }
.stat .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.stat.wide { grid-column: 1 / -1; }
.stat.hero { background: var(--primary); color: var(--primary-ink); }
.stat.hero .label, .stat.hero .sub { color: inherit; opacity: .85; }
.stat.hero .value { font-size: 28px; }

/* صفوف المصاريف */
.list { display: flex; flex-direction: column; }
.group-title { color: var(--muted); font-size: 13px; margin: 14px 4px 6px; font-weight: 600; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: start; background: var(--card); border: 0;
  padding: 10px 12px; border-radius: 14px; margin-bottom: 6px; box-shadow: var(--shadow);
}
.row .main { flex: 1; min-width: 0; }
.row .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .s { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .amt { font-weight: 700; white-space: nowrap; }
.bubble { display: inline-grid; place-items: center; border-radius: 12px; background: color-mix(in srgb, var(--c) 22%, transparent); flex: none; }

/* أزرار */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 46px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--card2); font-weight: 600;
}
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn-row { display: flex; gap: 10px; margin-top: 14px; }
.btn-row > .btn { flex: 1; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 12px; border: 0; background: var(--card2); }

/* نماذج */
label.field { display: block; margin-bottom: 12px; }
label.field > span, .field-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
input[type="text"], input[type="search"], input[type="number"], input[type="date"], select, textarea, input:not([type]) {
  width: 100%; min-height: 46px; padding: 8px 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--card); font-size: 16px;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
.amount-in { display: flex; align-items: center; gap: 8px; }
.amount-in input { font-size: 28px; font-weight: 700; min-height: 58px; text-align: start; }
.amount-in .cur { color: var(--muted); font-weight: 600; white-space: nowrap; }
.err { color: var(--danger); font-size: 13px; margin-top: 4px; }
.note-box { background: var(--card2); border-radius: 10px; padding: 8px 10px; font-size: 13.5px; margin-bottom: 10px; }
.note-box.warn { background: color-mix(in srgb, #d9a93a 22%, transparent); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 12px; min-height: 40px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--card); font-size: 15px;
}
.chip[aria-pressed="true"] { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }
.chip.suggest { border-style: dashed; border-color: var(--primary); }

.seg { display: flex; background: var(--card2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 8px 6px; border-radius: 9px; font-size: 14px; color: var(--muted); min-height: 38px; }
.seg button.on { background: var(--card); color: var(--text); font-weight: 700; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

.toolbar { display: flex; gap: 8px; margin-bottom: 10px; }
.toolbar .grow { flex: 1; }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.filters .full { grid-column: 1 / -1; }

/* نوافذ */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .45); display: flex; align-items: flex-end; justify-content: center; }
.overlay.center { align-items: center; padding: 20px; }
.sheet {
  background: var(--bg); width: 100%; max-width: 640px; max-height: 94vh; max-height: 94dvh; overflow-y: auto; overscroll-behavior: contain;
  border-radius: 22px 22px 0 0; padding: 14px 16px calc(18px + env(safe-area-inset-bottom)); animation: up .18s ease-out;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; position: sticky; top: -14px; background: var(--bg); padding: 4px 0 8px; z-index: 2; }
.dialog { background: var(--card); border-radius: 18px; padding: 18px; width: 100%; max-width: 380px; box-shadow: var(--shadow); }
@keyframes up { from { transform: translateY(40px); opacity: .5; } to { transform: none; opacity: 1; } }
#toast-root { position: fixed; inset-inline: 0; bottom: calc(96px + env(safe-area-inset-bottom)); display: flex; justify-content: center; z-index: 80; pointer-events: none; }
.toast { background: #22302f; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 15px; max-width: 90%; box-shadow: var(--shadow); }
.toast.error { background: var(--danger); }

.empty { text-align: center; padding: 36px 16px; }
.empty-icon { font-size: 44px; }

/* رسوم */
.pie-wrap { display: flex; justify-content: center; margin: 4px 0 10px; }
.pie { width: 210px; height: 210px; }
.pie text { fill: var(--text); font-family: var(--font); }
.bars-scroll { overflow-x: auto; direction: rtl; }
.bars svg { display: block; width: 100%; height: auto; }
.bars text { fill: var(--muted); font-family: var(--font); font-size: 10px; }
.bars .bar { fill: var(--primary); }
.bars .bar.sel { fill: var(--text); }
.bars .grid { stroke: var(--border); stroke-width: 1; }
.rank { display: flex; flex-direction: column; gap: 10px; }
.rank-row { display: block; width: 100%; text-align: start; background: none; border: 0; padding: 0; }
.rank-top { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.rank-top .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meter { height: 8px; border-radius: 6px; background: var(--card2); overflow: hidden; }
.meter > i { display: block; height: 100%; border-radius: 6px; background: var(--c); }
table.cmp { width: 100%; border-collapse: collapse; font-size: 14px; }
table.cmp th { text-align: start; color: var(--muted); font-weight: 500; padding: 4px 6px; }
table.cmp td { padding: 6px; border-top: 1px solid var(--border); }
.up { color: var(--danger); } .down { color: var(--primary); }
.setrow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 0; border-top: 1px solid var(--border); }
.setrow:first-of-type { border-top: 0; }
.detail-grid { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 8px 0 4px; }
.detail-grid dt { color: var(--muted); } .detail-grid dd { margin: 0; overflow-wrap: anywhere; }
.review-card { border: 1px solid var(--border); border-radius: 14px; padding: 12px; margin-bottom: 12px; background: var(--card); }
.review-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.review-head .raw { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.swatches { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; background: var(--c); }
.swatch[aria-pressed="true"] { border-color: var(--text); }
.icons-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.icons-grid button { font-size: 22px; min-height: 44px; border-radius: 10px; border: 1px solid var(--border); background: var(--card); }
.icons-grid button[aria-pressed="true"] { border-color: var(--primary); background: var(--primary-soft); }
.banner { background: var(--primary-soft); color: var(--text); border-radius: 14px; padding: 12px 14px; margin-bottom: 14px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.fatal { padding: 28px 20px; max-width: 520px; margin: 0 auto; }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }

a.btn, a.tab { text-decoration: none; color: inherit; }
a.btn.primary { color: var(--primary-ink); }
button.stat { font: inherit; color: inherit; cursor: pointer; width: 100%; }
