/* ========= إدارة العُهد — نظام التصميم ========= */
:root {
  --primary: #1f6f5c;
  --accent: #c9a227;
  --primary-ink: #ffffff;
  --primary-soft: color-mix(in srgb, var(--primary) 9%, var(--surface));
  --primary-line: color-mix(in srgb, var(--primary) 24%, transparent);

  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1c2321;
  --ink-2: #4a5552;
  --muted: #7a8582;
  --line: #e6e4de;
  --line-2: #efede8;

  --ok: #1f7a55;     --ok-soft: #e6f3ec;
  --warn: #a86a0b;   --warn-soft: #fbf1df;
  --bad: #b3362f;    --bad-soft: #fbe9e7;
  --info: #2c5d8f;   --info-soft: #e7eff8;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 28, .04), 0 4px 16px rgba(20, 30, 28, .05);
  --shadow-lg: 0 10px 40px rgba(20, 30, 28, .16);
  --font: "IBM Plex Sans Arabic", "Segoe UI", Tahoma, system-ui, sans-serif;
  --side-w: 252px;
}
:root[data-theme="dark"] {
  --bg: #101413; --surface: #171c1b; --surface-2: #1c2220; --ink: #e8ece9; --ink-2: #c2cac6;
  --muted: #8d9894; --line: #2a3230; --line-2: #222927;
  --ok: #4cc08b; --ok-soft: #173025; --warn: #e3a64a; --warn-soft: #33281a;
  --bad: #ec7b72; --bad-soft: #3a1f1d; --info: #7fb0e3; --info-soft: #1b2a3a;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  --primary-soft: color-mix(in srgb, var(--primary) 22%, var(--surface));
  --primary-line: color-mix(in srgb, var(--primary) 45%, transparent);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101413; --surface: #171c1b; --surface-2: #1c2220; --ink: #e8ece9; --ink-2: #c2cac6;
    --muted: #8d9894; --line: #2a3230; --line-2: #222927;
    --ok: #4cc08b; --ok-soft: #173025; --warn: #e3a64a; --warn-soft: #33281a;
    --bad: #ec7b72; --bad-soft: #3a1f1d; --info: #7fb0e3; --info-soft: #1b2a3a;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
    --primary-soft: color-mix(in srgb, var(--primary) 22%, var(--surface));
    --primary-line: color-mix(in srgb, var(--primary) 45%, transparent);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--ink); line-height: 1.6; font-size: 15px; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
.num { font-variant-numeric: tabular-nums; direction: ltr; unicode-bidi: isolate; display: inline-block; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
svg.i { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg.i.sm { width: 16px; height: 16px; }
svg.i.lg { width: 26px; height: 26px; }

/* ========= التخطيط ========= */
.app { display: grid; grid-template-columns: var(--side-w) 1fr; min-height: 100vh; }
.side { position: sticky; top: 0; height: 100vh; background: var(--surface); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 14px; gap: 6px; }
.side .org { display: flex; align-items: center; gap: 12px; padding: 4px 8px 18px; border-bottom: 1px solid var(--line-2); margin-bottom: 10px; }
.org-mark { width: 42px; height: 42px; border-radius: 12px; background: var(--primary); color: var(--primary-ink); display: grid; place-items: center; font-weight: 700; font-size: 1.15rem; flex: none; overflow: hidden; }
.org-mark img { width: 100%; height: 100%; object-fit: contain; background: #fff; }
.org-name { font-weight: 700; line-height: 1.35; font-size: .98rem; }
.org-sub { font-size: .78rem; color: var(--muted); }
.nav a { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; color: var(--ink-2); text-decoration: none; font-weight: 500; transition: background .15s, color .15s; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav a .count { margin-inline-start: auto; background: var(--warn-soft); color: var(--warn); font-size: .75rem; padding: 1px 8px; border-radius: 20px; font-weight: 600; }
.side-foot { margin-top: auto; display: flex; gap: 6px; padding-top: 12px; border-top: 1px solid var(--line-2); }
.side-foot button { flex: 1; }
.brandline { font-size: .72rem; color: var(--muted); text-align: center; padding-top: 8px; }
.brandline a { color: var(--muted); }

.main { min-width: 0; padding: 26px 32px 60px; max-width: 1280px; width: 100%; }
.demo-bar { background: var(--info-soft); color: var(--info); border: 1px solid color-mix(in srgb, var(--info) 25%, transparent); padding: 9px 14px; border-radius: var(--radius-sm); margin-bottom: 18px; font-size: .88rem; display: flex; gap: 10px; align-items: center; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 22px; }
.page-head h1 { margin: 0; font-size: 1.55rem; font-weight: 700; letter-spacing: -.01em; }
.page-head .sub { color: var(--muted); margin-top: 2px; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.crumb { font-size: .85rem; color: var(--muted); margin-bottom: 6px; display: flex; align-items: center; gap: 6px; }
.crumb a { color: var(--muted); text-decoration: none; }
.crumb a:hover { color: var(--primary); }

.tabbar { display: none; }

/* ========= الأزرار ========= */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 9px 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; font-weight: 600; font-size: .92rem; white-space: nowrap; transition: background .15s, border-color .15s, transform .05s; text-decoration: none; }
.btn:hover { background: var(--surface-2); border-color: color-mix(in srgb, var(--ink) 18%, var(--line)); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { background: color-mix(in srgb, var(--primary) 88%, #000); }
.btn.danger { color: var(--bad); }
.btn.danger.solid { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 6px 11px; font-size: .84rem; border-radius: 8px; }
.btn.icon { padding: 8px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* ========= البطاقات ========= */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 16px 20px; border-bottom: 1px solid var(--line-2); }
.card-head h2, .card-head h3 { margin: 0; font-size: 1.02rem; font-weight: 700; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

.kpi { padding: 18px 20px; display: flex; flex-direction: column; gap: 6px; position: relative; overflow: hidden; }
.kpi .k-label { color: var(--muted); font-size: .86rem; display: flex; align-items: center; gap: 8px; }
.kpi .k-icon { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); }
.kpi .k-value { font-size: 1.6rem; font-weight: 700; letter-spacing: -.01em; }
.kpi .k-sub { font-size: .8rem; color: var(--muted); }
.kpi.warn .k-icon { background: var(--warn-soft); color: var(--warn); }
.kpi.bad .k-icon { background: var(--bad-soft); color: var(--bad); }
.kpi.ok .k-icon { background: var(--ok-soft); color: var(--ok); }

/* ========= الجداول ========= */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-size: .92rem; }
.t th { text-align: start; font-weight: 600; color: var(--muted); font-size: .8rem; padding: 11px 16px; background: var(--surface-2); border-bottom: 1px solid var(--line); white-space: nowrap; }
.t td { padding: 12px 16px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.t tr:last-child td { border-bottom: 0; }
.t tbody tr.click { cursor: pointer; }
.t tbody tr.click:hover td { background: var(--surface-2); }
.t td.end, .t th.end { text-align: end; }
.t tfoot td { font-weight: 700; background: var(--surface-2); border-top: 1px solid var(--line); }
.cell-main { font-weight: 600; }
.cell-sub { font-size: .8rem; color: var(--muted); }

/* ========= الشارات ========= */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 10px; border-radius: 20px; font-size: .78rem; font-weight: 600; white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.b-ok { background: var(--ok-soft); color: var(--ok); }
.b-warn { background: var(--warn-soft); color: var(--warn); }
.b-bad { background: var(--bad-soft); color: var(--bad); }
.b-info { background: var(--info-soft); color: var(--info); }
.b-mute { background: var(--line-2); color: var(--muted); }

/* ========= النماذج ========= */
.form { display: grid; gap: 14px; }
.form.cols { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form .full { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label { font-size: .85rem; font-weight: 600; color: var(--ink-2); }
.field .hint { font-size: .78rem; color: var(--muted); }
.field .err { font-size: .8rem; color: var(--bad); }
.input, select.input, textarea.input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); transition: border-color .15s, box-shadow .15s; }
textarea.input { min-height: 80px; resize: vertical; }
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 16%, transparent); }
.input.money { direction: ltr; text-align: end; font-variant-numeric: tabular-nums; }
.input.bad { border-color: var(--bad); }
.check { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: .92rem; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line-2); }
.filters .input { width: auto; min-width: 150px; padding: 8px 10px; }
.search { position: relative; flex: 1; min-width: 200px; }
.search .input { width: 100%; padding-inline-start: 36px; }
.search svg { position: absolute; inset-inline-start: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); }

/* ========= التقدم والرسوم ========= */
.bar { height: 8px; border-radius: 8px; background: var(--line-2); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--primary); border-radius: 8px; }
.bar.over > span { background: var(--bad); }
.hbars { display: flex; flex-direction: column; gap: 12px; }
.hbar .hb-top { display: flex; justify-content: space-between; font-size: .88rem; margin-bottom: 5px; }
.chart { width: 100%; height: 220px; }
.chart .gl { stroke: var(--line-2); }
.chart .bx { fill: var(--primary); }
.chart .bx.cur { fill: var(--accent); }
.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); }

/* ========= ملخص العهدة ========= */
.sum-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0; }
.sum-row > div { padding: 16px 20px; border-inline-start: 1px solid var(--line-2); }
.sum-row > div:first-child { border-inline-start: 0; }
.sum-row .s-l { font-size: .8rem; color: var(--muted); }
.sum-row .s-v { font-size: 1.25rem; font-weight: 700; }

.alert { display: flex; gap: 12px; align-items: flex-start; padding: 12px 16px; border-radius: var(--radius-sm); font-size: .9rem; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert b { font-weight: 700; }

.empty { padding: 44px 20px; text-align: center; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 12px; }
.empty .e-icon { width: 54px; height: 54px; border-radius: 16px; background: var(--surface-2); border: 1px solid var(--line); display: grid; place-items: center; color: var(--muted); }

.list-item { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--line-2); }
.list-item:last-child { border-bottom: 0; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-weight: 700; flex: none; }

/* ========= النوافذ ========= */
dialog.modal { border: 0; padding: 0; border-radius: 18px; background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg); width: min(620px, calc(100vw - 24px)); max-height: calc(100vh - 32px); }
dialog.modal.wide { width: min(860px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(16, 22, 20, .45); backdrop-filter: blur(2px); }
.m-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--line-2); }
.m-head h3 { margin: 0; font-size: 1.1rem; }
.m-body { padding: 20px 22px; overflow-y: auto; max-height: calc(100vh - 190px); }
.m-foot { display: flex; gap: 8px; justify-content: flex-start; padding: 14px 22px; border-top: 1px solid var(--line-2); background: var(--surface-2); border-radius: 0 0 18px 18px; }

.toasts { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 1000; pointer-events: none; }
.toast { background: var(--ink); color: var(--bg); padding: 11px 18px; border-radius: 12px; box-shadow: var(--shadow-lg); font-size: .92rem; display: flex; align-items: center; gap: 10px; animation: tin .2s ease-out; }
.toast.bad { background: var(--bad); color: #fff; }
@keyframes tin { from { opacity: 0; transform: translateY(8px); } }

/* ========= الدخول ========= */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; background:
  radial-gradient(700px 380px at 90% -10%, color-mix(in srgb, var(--accent) 14%, transparent), transparent),
  radial-gradient(700px 400px at 0% 110%, color-mix(in srgb, var(--primary) 14%, transparent), transparent), var(--bg); }
.auth-card { width: min(440px, 100%); padding: 32px 30px; }
.auth-card .org-mark { width: 56px; height: 56px; font-size: 1.4rem; border-radius: 16px; margin-bottom: 16px; }
.auth-card h1 { margin: 0 0 4px; font-size: 1.35rem; }
.auth-card .sub { color: var(--muted); margin: 0 0 22px; font-size: .92rem; }
.code-box { font-family: Consolas, "Courier New", monospace; font-size: 1.35rem; letter-spacing: .12em; text-align: center; padding: 18px; background: var(--surface-2); border: 1px dashed var(--primary-line); border-radius: 12px; direction: ltr; user-select: all; }
.linkbtn { background: none; border: 0; color: var(--primary); cursor: pointer; padding: 0; font-weight: 600; }

/* ========= الطباعة ========= */
#print-root { display: none; }
@media print {
  @page { size: A4; margin: 14mm; }
  body { background: #fff; color: #000; font-size: 12px; }
  .app, .auth, .toasts, dialog { display: none !important; }
  #print-root { display: block; }
  .p-head { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px solid #222; padding-bottom: 10px; margin-bottom: 14px; }
  .p-head .p-org { display: flex; gap: 10px; align-items: center; }
  .p-head img { width: 46px; height: 46px; object-fit: contain; }
  .p-title { text-align: center; font-size: 17px; font-weight: 700; margin: 6px 0 14px; }
  .p-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 20px; margin-bottom: 12px; }
  .p-table { width: 100%; border-collapse: collapse; margin: 8px 0 12px; }
  .p-table th, .p-table td { border: 1px solid #999; padding: 5px 7px; text-align: start; }
  .p-table th { background: #f0f0f0; }
  .p-table tfoot td { font-weight: 700; background: #f7f7f7; }
  .p-sign { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-top: 40px; text-align: center; }
  .p-sign div { border-top: 1px solid #444; padding-top: 6px; }
  .p-foot { margin-top: 24px; font-size: 10px; color: #555; display: flex; justify-content: space-between; }
  .num { direction: ltr; }
}

/* ========= الجوال ========= */
@media (max-width: 960px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .side { display: none; }
  .main { padding: 16px 16px 96px; }
  .page-head h1 { font-size: 1.3rem; }
  .g2 { grid-template-columns: 1fr; }
  .form.cols { grid-template-columns: 1fr; }
  .sum-row { grid-template-columns: repeat(2, 1fr); }
  .sum-row > div:nth-child(3) { border-inline-start: 0; }
  .sum-row > div:nth-child(n+3) { border-top: 1px solid var(--line-2); }
  .tabbar { display: flex; position: fixed; bottom: 0; left: 0; right: 0; background: var(--surface); border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); z-index: 50; }
  .tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .7rem; color: var(--muted); text-decoration: none; padding: 4px 0; }
  .tabbar a.active { color: var(--primary); font-weight: 600; }
  .t th, .t td { padding: 10px 12px; }
  .hide-sm { display: none; }
  .filters .input { min-width: 0; flex: 1 1 140px; }
  .kpi .k-value { font-size: 1.3rem; }
  .m-foot { flex-wrap: wrap; }
}
