:root {
  --bg: #f4f6f8; --surface: #fff; --surface-2: #f8fafb; --line: #dfe4e8; --line-strong: #c3ccd3;
  --text: #17212b; --muted: #66727d;
  --brand: #0e7c86; --brand-ink: #fff; --brand-soft: #e3f3f4;
  --in: #d8f3f5; --in-ink: #0b5d64;
  --out: #fff4cc; --out-ink: #7a5b00;
  --neg: #b4413a; --pos: #23824a; --warn: #a86400; --warn-soft: #fff2db; --bad-soft: #fde7e5; --ok-soft: #e4f5ea;
  --off: #fde2e0;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1418; --surface: #171e23; --surface-2: #1c252b; --line: #2a353d; --line-strong: #3a4751;
    --text: #e6ecf0; --muted: #93a1ac;
    --brand: #3cb4be; --brand-ink: #062326; --brand-soft: #143238;
    --in: #12343a; --in-ink: #8fdbe2; --out: #3a3110; --out-ink: #f0d27a;
    --neg: #f08a82; --pos: #6fcf97; --warn: #f0b45a; --warn-soft: #3a2c12; --bad-soft: #3d1d1b; --ok-soft: #173325;
    --off: #3d1d1b; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1418; --surface: #171e23; --surface-2: #1c252b; --line: #2a353d; --line-strong: #3a4751;
  --text: #e6ecf0; --muted: #93a1ac;
  --brand: #3cb4be; --brand-ink: #062326; --brand-soft: #143238;
  --in: #12343a; --in-ink: #8fdbe2; --out: #3a3110; --out-ink: #f0d27a;
  --neg: #f08a82; --pos: #6fcf97; --warn: #f0b45a; --warn-soft: #3a2c12; --bad-soft: #3d1d1b; --ok-soft: #173325;
  --off: #3d1d1b; 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 "Tajawal", system-ui, sans-serif; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: 0; font-weight: 800; }
h2 { font-size: 1.08rem; margin: 0 0 12px; }
h3 { font-size: 1rem; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.left { text-align: left; }
.strong { font-weight: 700; }
.neg { color: var(--neg); }
.warn-text { color: var(--warn); font-weight: 600; }

input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px 10px; min-height: 40px; max-width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: var(--brand); }
textarea { width: 100%; resize: vertical; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid transparent; border-radius: 8px;
  padding: 8px 14px; min-height: 40px; cursor: pointer; background: var(--surface-2); text-decoration: none !important; white-space: nowrap; }
.btn.primary { background: var(--brand); color: var(--brand-ink); font-weight: 700; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: var(--line-strong); color: var(--text); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: transparent; border-color: var(--neg); color: var(--neg); }
.btn.small { padding: 4px 10px; min-height: 32px; font-size: .88rem; }
.btn.big { padding: 12px 34px; font-size: 1.05rem; }
.btn.disabled { pointer-events: none; opacity: .4; }

/* شريط علوي */
.topbar { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 10; }
.brand { font-weight: 800; color: var(--brand); font-size: 1.1rem; }
.topbar nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
.topbar nav a { padding: 6px 12px; border-radius: 8px; color: var(--text); }
.topbar nav a.on { background: var(--brand-soft); color: var(--brand); font-weight: 700; }
.topbar nav a:hover { text-decoration: none; background: var(--surface-2); }
.who { display: flex; gap: 12px; align-items: center; font-size: .88rem; color: var(--muted); }

.wrap { max-width: 1200px; margin: 0 auto; padding: 20px 16px 60px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 14px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow); }
.empty { padding: 28px; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--line-strong); border-radius: var(--radius); margin-bottom: 14px; }

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; background: var(--ok-soft); border: 1px solid color-mix(in srgb, var(--pos) 35%, transparent); }
.flash.err { background: var(--bad-soft); border-color: color-mix(in srgb, var(--neg) 35%, transparent); }
.status { padding: 8px 12px; border-radius: 8px; margin-bottom: 12px; font-size: .92rem; }
.status.ok { background: var(--ok-soft); } .status.warn { background: var(--warn-soft); } .status.bad { background: var(--bad-soft); }

/* تسجيل الدخول */
.auth-card { max-width: 400px; margin: 8vh auto 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); }
.auth-card h1 { color: var(--brand); margin-bottom: 4px; }
.stack { display: flex; flex-direction: column; gap: 12px; margin-top: 14px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }

/* جدول التقفيل (شكل الشيت) */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; }
.sheet { background: var(--surface); border: 1px solid var(--line-strong); min-width: 760px; }
.sheet th, .sheet td { border: 1px solid var(--line); padding: 4px 6px; text-align: center; font-variant-numeric: tabular-nums; }
.sheet thead th { background: var(--surface-2); font-weight: 700; font-size: .9rem; }
.sheet th.grp-in { background: var(--in); color: var(--in-ink); }
.sheet th.grp-out { background: var(--out); color: var(--out-ink); }
.sheet .name-col { text-align: right; font-weight: 600; min-width: 130px; background: var(--surface-2); }
.sheet input.num { width: 100%; min-width: 74px; text-align: center; border-color: transparent; background: transparent; direction: ltr; min-height: 36px; }
.sheet input.num:hover { border-color: var(--line-strong); }
.sheet input.num:focus { background: var(--surface); }
.sheet input.remarks { width: 100%; min-width: 120px; border-color: transparent; background: transparent; min-height: 36px; }
.sheet input.off { width: 20px; height: 20px; min-height: 0; accent-color: var(--neg); }
.sheet .calc { background: var(--surface-2); font-weight: 700; direction: ltr; }
.sheet tr.is-off td { background: var(--off); }
.sheet tr.is-off .remarks-tag { color: var(--neg); }
.sheet tfoot th { background: var(--surface-2); font-weight: 800; direction: ltr; }
.sheet tfoot th.name-col { direction: rtl; }
.sheet .add-row input { width: 100%; border-style: dashed; }
.is-readonly input.num, .is-readonly input.remarks { pointer-events: none; }
.readonly td { direction: ltr; } .readonly td.name-col, .readonly td:last-child { direction: rtl; }

.actions { display: flex; justify-content: center; margin: 18px 0; }

/* بطاقات الأرقام */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; box-shadow: var(--shadow); }
.kpi span { color: var(--muted); font-size: .86rem; }
.kpi b { font-size: 1.35rem; font-variant-numeric: tabular-nums; direction: ltr; text-align: right; }
.kpi.sub b { font-size: 1.1rem; }
.kpi.out b { color: var(--neg); }
.kpi.net { border-color: var(--brand); background: var(--brand-soft); }
.kpi.net b { color: var(--brand); }
.kpi.cash { border-inline-start: 4px solid var(--pos); }
.summary { margin-top: 16px; }
.delta { font-weight: 600; } .delta.up { color: var(--pos); } .delta.down { color: var(--neg); }

/* تقارير */
.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--line); margin-bottom: 14px; flex-wrap: wrap; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.on { color: var(--brand); border-bottom-color: var(--brand); font-weight: 700; }
.tabs .spacer { flex: 1; }
.report { min-width: 640px; }
.report th, .report td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; direction: ltr; font-variant-numeric: tabular-nums; white-space: nowrap; }
.report th { background: var(--surface-2); font-size: .86rem; color: var(--muted); font-weight: 700; text-align: center; }
.report .name-col { text-align: right; direction: rtl; }
.report tfoot th { color: var(--text); font-size: .95rem; text-align: left; border-top: 2px solid var(--line-strong); }
.report tfoot th.name-col { text-align: right; }
.report tr.missing-row td { background: var(--warn-soft); }
.report tbody tr:hover td { background: var(--surface-2); }
.bar { height: 6px; background: var(--surface-2); border-radius: 3px; width: 80px; display: inline-block; vertical-align: middle; margin-inline-end: 6px; }
.bar i { display: block; height: 100%; background: var(--brand); border-radius: 3px; }

.chart { display: grid; grid-template-columns: repeat(var(--n), 1fr); gap: 3px; height: 170px; align-items: end; direction: ltr; }
.chart .col { height: 100%; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; }
.chart .col i { width: 100%; max-width: 22px; background: var(--brand); border-radius: 3px 3px 0 0; }
.chart .col.missing i { height: 100% !important; background: repeating-linear-gradient(45deg, var(--warn-soft), var(--warn-soft) 4px, transparent 4px, transparent 8px); border: 1px dashed var(--warn); }
.chart .col span { font-size: .7rem; color: var(--muted); }
.legend { margin-top: 8px; display: flex; gap: 8px; align-items: center; }
.sw { display: inline-block; width: 12px; height: 12px; background: var(--brand); border-radius: 2px; }
.sw.missing { background: var(--warn-soft); border: 1px dashed var(--warn); }

.day-block h3 { color: var(--brand); }
.grand { display: flex; flex-wrap: wrap; gap: 18px; padding: 14px 16px; background: var(--text); color: var(--bg); border-radius: var(--radius); font-weight: 700; }
.grand b { direction: ltr; display: inline-block; }

/* لوحة الفروع */
.branch-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.branch-card { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; background: var(--surface-2); }
.bc-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; font-size: 1.05rem; }
.bc-status { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.pill { padding: 3px 10px; border-radius: 99px; font-size: .82rem; color: var(--text); }
.pill.ok { background: var(--ok-soft); } .pill.warn { background: var(--bad-soft); } .pill.idle { background: var(--warn-soft); }
.bc-nums { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.bc-nums span { display: block; font-size: .78rem; color: var(--muted); }
.bc-nums b { direction: ltr; display: block; text-align: right; font-variant-numeric: tabular-nums; }

/* نماذج الإدارة */
.inline-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.inline-form.grow { flex: 1; min-width: 240px; }
.inline-form.grow input[name="name"] { flex: 1; min-width: 140px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; margin-bottom: 14px; align-items: center; }
.list { display: flex; flex-direction: column; margin-top: 12px; }
.list-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); flex-wrap: wrap; }
.list-row.inactive, tr.inactive { opacity: .55; }
.btns { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.btns form { display: inline; }
.plain th, .plain td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: right; }
.plain th { color: var(--muted); font-size: .86rem; }
.tag { background: var(--brand-soft); color: var(--brand); padding: 1px 8px; border-radius: 99px; font-size: .8rem; font-weight: 700; }
.tag.off { background: var(--bad-soft); color: var(--neg); }

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .who { width: 100%; justify-content: space-between; }
  .wrap { padding: 14px 12px 50px; }
  .kpi b { font-size: 1.15rem; }
  .btn.big { width: 100%; }
}

@media print {
  .no-print, .topbar { display: none !important; }
  body { background: #fff; color: #000; font-size: 11px; }
  .wrap { max-width: none; padding: 0; }
  .card, .kpi { box-shadow: none; break-inside: avoid; }
  .table-scroll { overflow: visible; }
  .sheet, .report { min-width: 0; }
  .sheet input { border: 0 !important; }
}

/* تبديل اللغة */
.lang-switch { font-weight: 700; border: 1px solid var(--line-strong); border-radius: 99px; padding: 2px 10px; color: var(--brand); }
.lang-switch:hover { text-decoration: none; background: var(--brand-soft); }
.lang-float { position: absolute; top: 14px; inset-inline-end: 16px; }

/* الإنجليزي: اتجاه من اليسار لليمين */
[dir="ltr"] .sheet .name-col,
[dir="ltr"] .plain th, [dir="ltr"] .plain td { text-align: left; }
[dir="ltr"] .sheet tfoot th.name-col,
[dir="ltr"] .readonly td.name-col, [dir="ltr"] .readonly td:last-child { direction: ltr; }
[dir="ltr"] .kpi b, [dir="ltr"] .bc-nums b { text-align: left; }
[dir="ltr"] .report th, [dir="ltr"] .report td, [dir="ltr"] .report tfoot th { text-align: right; }
[dir="ltr"] .report .name-col, [dir="ltr"] .report tfoot th.name-col { text-align: left; direction: ltr; }
[dir="ltr"] .report th { text-align: center; }
