:root {
  --bg: #f1f0ea;
  --surface: #ffffff;
  --surface-2: #e7e5db;
  --structural: #12233a;
  --structural-soft: #4c6d58;
  --structural-tint: #dde5de;
  --accent: #d68a35;
  --accent-text: #ffffff;
  --text: #171d1a;
  --muted: #5c665f;
  --border: #d8d5c8;
  --danger: #b3462c;
  --success: #3f7a52;
  --radius: 10px;
  --radius-sm: 6px;
  --font: 'Cairo', 'Segoe UI', Tahoma, sans-serif;
  --shadow: 0 10px 24px -14px rgba(18, 35, 58, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11151b;
    --surface: #171d24;
    --surface-2: #1e252d;
    --structural: #e7ede8;
    --structural-soft: #8fb09b;
    --structural-tint: #1c2a24;
    --accent: #e2a15b;
    --accent-text: #171004;
    --text: #e9ece9;
    --muted: #909a93;
    --border: #293138;
  }
}
:root[data-theme="dark"] {
  --bg: #11151b;
  --surface: #171d24;
  --surface-2: #1e252d;
  --structural: #e7ede8;
  --structural-soft: #8fb09b;
  --structural-tint: #1c2a24;
  --accent: #e2a15b;
  --accent-text: #171004;
  --text: #e9ece9;
  --muted: #909a93;
  --border: #293138;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  direction: rtl;
  text-align: right;
}
h1, h2, h3, h4 { margin: 0; font-weight: 800; }
p { margin: 0; }
a { color: inherit; }
button { font-family: inherit; }
::selection { background: var(--accent); color: var(--accent-text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Login ---------- */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--structural);
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 36px 32px;
  box-shadow: 0 30px 60px -25px rgba(0, 0, 0, 0.5);
}
.brand-mark { display: flex; align-items: center; gap: 12px; margin-bottom: 26px; }
.brand-mark-centered { display: flex; flex-direction: column; align-items: center; gap: 8px; margin-bottom: 26px; text-align: center; }
.brand-logo { width: 96px; height: 96px; border-radius: 50%; object-fit: contain; background: #fff; box-shadow: 0 4px 14px -6px rgba(0,0,0,.25); }
.brand-badge {
  width: 44px; height: 44px; border-radius: 12px; background: var(--structural);
  color: var(--bg); display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 18px; flex-shrink: 0;
}
.brand-name { font-weight: 800; font-size: 16.5px; color: var(--structural); }
.brand-sub { font-size: 12px; color: var(--muted); }
.login-card h2 { font-size: 21px; margin-bottom: 6px; }
.login-lead { font-size: 13.5px; color: var(--muted); margin-bottom: 24px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 12.5px; font-weight: 700; color: var(--structural); margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; height: 44px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface-2); padding: 0 14px; font-size: 14px; color: var(--text); font-family: inherit;
}
.field textarea { height: auto; padding: 10px 14px; min-height: 80px; resize: vertical; }
.field-error { color: var(--danger); font-size: 12px; margin-top: 6px; min-height: 16px; }
.btn {
  border: 0; border-radius: var(--radius-sm); font-weight: 800; font-size: 14px;
  cursor: pointer; padding: 0 18px; height: 42px; display: inline-flex; align-items: center;
  justify-content: center; gap: 6px; transition: opacity .15s ease, transform .1s ease;
}
.btn:active { transform: scale(0.98); }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-primary:hover { opacity: .92; }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-block { width: 100%; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 12.5px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.login-error { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); color: var(--danger); border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent); border-radius: var(--radius-sm); padding: 10px 14px; font-size: 13px; margin-bottom: 16px; display: none; }
.login-error.show { display: block; }

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0; background: var(--structural); color: color-mix(in srgb, var(--bg) 92%, transparent);
  padding: 20px 14px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sb-brand { display: flex; align-items: center; gap: 10px; padding: 0 6px; }
.sb-badge { width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: var(--accent-text); display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 15px; flex-shrink: 0; }
.sb-logo { width: 38px; height: 38px; border-radius: 50%; object-fit: contain; background: #fff; flex-shrink: 0; }
.sb-name { font-weight: 800; font-size: 14px; color: var(--bg); line-height: 1.3; }
.sb-sub { font-size: 10.5px; color: color-mix(in srgb, var(--bg) 55%, transparent); }
.nav-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.nav-list a {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 8px;
  font-size: 13.5px; color: color-mix(in srgb, var(--bg) 75%, transparent); text-decoration: none; font-weight: 600; cursor: pointer;
}
.nav-list a:hover { background: color-mix(in srgb, var(--bg) 8%, transparent); }
.nav-list a.active { background: color-mix(in srgb, var(--bg) 14%, transparent); color: var(--bg); }
.nav-list .ic { width: 8px; height: 8px; border-radius: 50%; background: var(--structural-soft); flex-shrink: 0; }
.nav-list a.active .ic { background: var(--accent); }
.nav-group-label { font-size: 10.5px; color: color-mix(in srgb, var(--bg) 45%, transparent); font-weight: 700; letter-spacing: .04em; padding: 10px 12px 2px; }
.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding-top: 14px; border-top: 1px solid color-mix(in srgb, var(--bg) 15%, transparent); }
.sb-user { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: color-mix(in srgb, var(--bg) 80%, transparent); }
.sb-user .avatar { width: 28px; height: 28px; border-radius: 50%; background: color-mix(in srgb, var(--bg) 14%, transparent); display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 11.5px; flex-shrink: 0; }

.main { flex: 1; min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; padding: 16px 26px;
  border-bottom: 1px solid var(--border); background: var(--surface); position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 18px; color: var(--structural); }
.topbar .muted-line { font-size: 12px; color: var(--muted); margin-top: 3px; }
.icon-btn { width: 36px; height: 36px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 15px; }
.content { padding: 24px 26px 60px; }

/* ---------- KPI / cards ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 22px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.kpi .lbl { font-size: 12px; color: var(--muted); font-weight: 700; }
.kpi .val { font-size: 22px; font-weight: 800; color: var(--structural); font-variant-numeric: tabular-nums; }
.kpi .val.accent { color: var(--accent); }
.kpi .sub { font-size: 11.5px; color: var(--structural-soft); font-weight: 700; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 18px; margin-bottom: 18px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 10px; flex-wrap: wrap; }
.panel-head h3 { font-size: 15px; color: var(--structural); }
.panels-2col { display: grid; grid-template-columns: 1.5fr 1fr; gap: 16px; }

.alert-strip { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.alert-item { display: flex; align-items: center; gap: 10px; background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); border-radius: var(--radius-sm); padding: 10px 14px; font-size: 13px; color: var(--text); }
.alert-item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex-shrink: 0; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: right; color: var(--muted); font-weight: 700; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 12px; white-space: nowrap; }
td { padding: 11px 8px; border-bottom: 1px solid var(--border); color: var(--text); font-variant-numeric: tabular-nums; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tr.row-due td.money-due { color: var(--danger); font-weight: 800; }
tr.row-soon { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.money { font-weight: 800; color: var(--structural); }
.money.accent { color: var(--accent); }
.badge { font-size: 11px; font-weight: 700; padding: 4px 10px; border-radius: 999px; background: var(--structural-tint); color: var(--structural-soft); display: inline-block; }
.badge.warn { background: color-mix(in srgb, var(--accent) 18%, var(--surface)); color: color-mix(in srgb, var(--accent) 80%, black); }
.badge.danger { background: color-mix(in srgb, var(--danger) 15%, var(--surface)); color: var(--danger); }
.badge.success { background: color-mix(in srgb, var(--success) 15%, var(--surface)); color: var(--success); }
.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); font-size: 13.5px; }

/* ---------- Modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(10, 14, 18, .55); display: flex; align-items: flex-start; justify-content: center; padding: 40px 16px; overflow-y: auto; z-index: 100; }
.modal { background: var(--surface); border-radius: var(--radius); width: 100%; max-width: 480px; padding: 26px; box-shadow: var(--shadow); }
.modal h3 { font-size: 17px; margin-bottom: 18px; color: var(--structural); }
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions .btn { flex: 1; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.form-grid .field.full { grid-column: 1 / -1; }
.checkbox-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 4px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.perm-group-title { font-size: 12px; font-weight: 800; color: var(--structural-soft); margin: 14px 0 6px; }

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.toolbar input, .toolbar select { height: 38px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface); padding: 0 12px; font-size: 13px; color: var(--text); font-family: inherit; }
.spacer { flex: 1; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--structural); color: var(--bg); padding: 12px 22px; border-radius: 999px; font-size: 13.5px; font-weight: 700; box-shadow: var(--shadow); z-index: 200; opacity: 0; transition: opacity .2s ease; pointer-events: none; }
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); }

@media (max-width: 960px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .panels-2col { grid-template-columns: 1fr; }
  .sidebar { position: fixed; right: -260px; z-index: 60; transition: right .2s ease; box-shadow: var(--shadow); }
  .sidebar.open { right: 0; }
  .form-grid { grid-template-columns: 1fr; }
}
