:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --surface-2: #f0f2ef;
  --border: #e2e5e0;
  --text: #1c2421;
  --muted: #5f6b66;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #e3f2ef;
  --accent: #b45309;
  --good: #15803d;
  --good-soft: #e6f4ea;
  --warn: #a16207;
  --warn-soft: #fdf3dc;
  --bad: #b91c1c;
  --bad-soft: #fbe9e9;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 2px 8px rgb(0 0 0 / .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121715;
    --surface: #1a201e;
    --surface-2: #222a27;
    --border: #2e3734;
    --text: #e8ece9;
    --muted: #9aa6a1;
    --primary: #2bb3a3;
    --primary-ink: #06201d;
    --primary-soft: #18332f;
    --accent: #f59e0b;
    --good: #4ade80;
    --good-soft: #163021;
    --warn: #facc15;
    --warn-soft: #2f2a12;
    --bad: #f87171;
    --bad-soft: #351a1a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Tajawal', 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
button, input, select, textarea { font: inherit; color: inherit; }

/* layout */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side {
  background: var(--surface);
  border-inline-start: 1px solid var(--border);
  padding: 20px 12px;
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 18px; font-weight: 700; font-size: 18px; }
.brand img { width: 32px; height: 32px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 10px; color: var(--text); font-weight: 500;
}
.nav a:hover { background: var(--surface-2); }
.nav a.on { background: var(--primary-soft); color: var(--primary); }
.nav .count { margin-inline-start: auto; background: var(--accent); color: #fff; border-radius: 99px; font-size: 12px; padding: 0 7px; }
.side .me { margin-top: auto; padding: 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--muted); }
.side .me b { display: block; color: var(--text); }
main { padding: 24px 28px 48px; max-width: 1180px; width: 100%; min-width: 0; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .grow { flex: 1; }
.tabbar { display: none; }

@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .side { display: none; }
  main { padding: 16px 16px 96px; }
  .tabbar {
    display: grid; grid-template-columns: repeat(5, 1fr);
    position: fixed; inset-inline: 0; bottom: 0; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 6px; font-size: 11.5px; color: var(--muted); position: relative; }
  .tabbar a.on { color: var(--primary); }
  .tabbar svg { width: 22px; height: 22px; }
  .tabbar .dot { position: absolute; top: 4px; inset-inline-end: calc(50% - 18px); background: var(--accent); color: #fff; font-size: 10px; border-radius: 99px; padding: 0 5px; }
}

/* elements */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card + .card { margin-top: 14px; }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.card-head .grow { flex: 1; }
.grid { display: grid; gap: 14px; }
.grid.k4 { grid-template-columns: repeat(4, 1fr); }
.grid.k2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 820px) { .grid.k4 { grid-template-columns: repeat(2, 1fr); } .grid.k2 { grid-template-columns: 1fr; } }
.kpi .v { font-size: 28px; font-weight: 700; }
.kpi .l { color: var(--muted); font-size: 13px; }
.kpi.alert .v { color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 8px 14px; cursor: pointer; font-weight: 500; white-space: nowrap; min-height: 40px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.good { color: var(--good); border-color: currentColor; }
.btn.bad { color: var(--bad); border-color: currentColor; }
.btn.sm { padding: 4px 10px; min-height: 32px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }

.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 13px; color: var(--muted); font-weight: 500; }
input, select, textarea {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px;
  padding: 8px 10px; min-height: 40px; width: 100%;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.form { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.form .full { grid-column: 1 / -1; }
@media (max-width: 600px) { .form { grid-template-columns: 1fr; } }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { width: 18px; height: 18px; min-height: 0; }
.filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }

.ac { position: relative; }
.ac-list { position: absolute; inset-inline: 0; top: 100%; z-index: 30; margin-top: 4px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: 0 8px 24px rgb(0 0 0 / .12); max-height: 340px; overflow-y: auto; }
.ac-item { padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--border); }
.ac-item:last-child { border-bottom: 0; }
.ac-item:hover, .ac-item.on { background: var(--primary-soft); }
.ac-item mark { background: #fde68a; color: inherit; border-radius: 3px; padding: 0 1px; }
.ac-empty { padding: 14px; text-align: center; color: var(--muted); }

.toggles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.toggle { border: 1px solid var(--border); border-radius: 10px; padding: 10px; cursor: pointer; text-align: center; user-select: none; }
.toggle small { display: block; color: var(--muted); font-size: 12px; }
.toggle.on { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.preview { display: flex; align-items: baseline; gap: 8px; padding: 12px; background: var(--surface-2); border-radius: 10px; }
.preview .v { font-size: 26px; font-weight: 700; color: var(--primary); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: start; padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12.5px; color: var(--muted); font-weight: 500; }
tr:last-child td { border-bottom: 0; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }
.num { font-variant-numeric: tabular-nums; }

.badge { display: inline-block; padding: 1px 9px; border-radius: 99px; font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.approved { background: var(--good-soft); color: var(--good); }
.badge.rejected { background: var(--bad-soft); color: var(--bad); }
.badge.level { background: var(--primary-soft); color: var(--primary); }
.badge.conf { background: var(--bad-soft); color: var(--bad); }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.bars { display: flex; flex-direction: column; gap: 8px; }
.bar { display: grid; grid-template-columns: 110px 1fr 48px; align-items: center; gap: 8px; font-size: 13px; }
.bar .track { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar .fill { height: 100%; background: var(--primary); border-radius: 99px; }
.cols { display: flex; align-items: flex-end; gap: 6px; height: 150px; padding-top: 8px; }
.cols .c { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; font-size: 11px; color: var(--muted); }
.cols .c .b { width: 100%; max-width: 34px; background: var(--primary); border-radius: 6px 6px 2px 2px; min-height: 2px; }
.cols .c .n { color: var(--text); font-weight: 500; }

.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.progress div { height: 100%; background: var(--accent); }
.fam-head { display: grid; grid-template-columns: 1fr auto; gap: 16px; align-items: start; }
.score-box { text-align: center; min-width: 150px; }
.score-box .v { font-size: 34px; font-weight: 700; color: var(--primary); line-height: 1.1; }
.facts { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 8px; font-size: 13.5px; color: var(--muted); }
.facts b { color: var(--text); font-weight: 500; }
@media (max-width: 600px) { .fam-head { grid-template-columns: 1fr; } .score-box { text-align: start; } }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin: 18px 0 14px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 10px 14px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 500; white-space: nowrap; }
.tabs button.on { color: var(--primary); border-bottom-color: var(--primary); }

.list { display: flex; flex-direction: column; gap: 10px; }
.item { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.item .top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.item .top .grow { flex: 1; }
.note-kind-positive { border-inline-start: 4px solid var(--good); }
.note-kind-need { border-inline-start: 4px solid var(--warn); }
.note-kind-alert { border-inline-start: 4px solid var(--bad); }
.empty { text-align: center; color: var(--muted); padding: 36px 12px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 380px; padding: 28px; }
.login .brand { justify-content: center; padding-bottom: 6px; font-size: 22px; }
.login p { text-align: center; color: var(--muted); margin: 0 0 20px; }
.login form { display: flex; flex-direction: column; gap: 12px; }
.err { color: var(--bad); font-size: 13.5px; min-height: 1em; }

/* modal + toast */
.overlay { position: fixed; inset: 0; background: rgb(0 0 0 / .4); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 16px; width: 100%; max-width: 620px; max-height: calc(100vh - 32px); overflow: auto; padding: 20px; }
.modal-head { display: flex; align-items: center; margin-bottom: 14px; }
.modal-head h2 { flex: 1; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.x { background: none; border: 0; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; padding: 4px 8px; }
@media (max-width: 600px) { .overlay { place-items: end center; padding: 0; } .modal { border-radius: 16px 16px 0 0; max-height: 92vh; } }
#toast { position: fixed; bottom: 24px; inset-inline-start: 50%; transform: translateX(50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }
#toast.bad { background: var(--bad); color: #fff; }
@media (max-width: 820px) { #toast { bottom: 84px; } }

/* البيانات التفصيلية */
.pf-group { border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px 14px; margin: 0 0 12px; }
.pf-group legend { font-weight: 600; padding: 0 6px; color: var(--primary); }
.chips { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.pf-view { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.pf-view .card { margin: 0; }
.pf-view h3 { margin: 0 0 8px; font-size: 15px; color: var(--primary); }
.pf-view dl { display: grid; grid-template-columns: minmax(120px, 40%) 1fr; gap: 6px 12px; margin: 0; font-size: 14px; }
.pf-view dt { color: var(--muted); }
.pf-view dd { margin: 0; }
@media (max-width: 820px) { .pf-view { grid-template-columns: 1fr; } }
button.link { background: none; border: 0; color: var(--primary); cursor: pointer; padding: 0 4px; font: inherit; font-size: 13px; text-decoration: underline; }
.badge.econ-0 { background: var(--bad-soft); color: var(--bad); }
.badge.econ-1 { background: var(--warn-soft); color: var(--warn); }
.badge.econ-2 { background: var(--primary-soft); color: var(--primary); }
.badge.econ-3 { background: var(--good-soft); color: var(--good); }

/* لوحة التحكم: مجموعات بطاقات */
.dgroup { margin-top: 18px; }
.dgroup h2 { font-size: 15px; color: var(--muted); margin: 0 0 8px; font-weight: 600; }
.dgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.dcard { display: flex; flex-direction: column; gap: 4px; margin: 0; color: var(--text); text-decoration: none; min-height: 118px; }
a.dcard:hover { border-color: var(--primary); }
.dcard .l { color: var(--muted); font-size: 13px; }
.dcard .v { font-size: 26px; font-weight: 700; line-height: 1.2; }
.dcard .v small { font-size: 13px; font-weight: 500; color: var(--muted); }
.dcard.alert { border-inline-start: 4px solid var(--accent); }
.dcard.alert .v { color: var(--accent); }
.delta { font-size: 12px; color: var(--muted); }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.spark { display: flex; align-items: flex-end; gap: 3px; height: 34px; margin-top: 6px; }
.spark i { flex: 1; background: var(--primary-soft); border-radius: 2px; min-height: 3px; }
.spark i:last-child { background: var(--primary); }
.dist { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 12px; color: var(--muted); margin-top: 4px; }
.dist b { color: var(--text); }

/* نتائج البحث: الفرد المطابق تحت أسرته، وبطاقة الفرد */
.person-hit{display:flex;align-items:center;gap:6px;width:100%;margin-top:4px;padding:4px 8px;border:1px solid var(--border);border-radius:8px;background:var(--surface-2);color:inherit;font:inherit;font-size:.85rem;text-align:start;cursor:pointer}
.person-hit:hover{border-color:var(--primary)}
.person-hit svg{width:14px;height:14px;flex:none;opacity:.6}
.person-head{display:flex;flex-wrap:wrap;gap:8px;align-items:center;margin-bottom:6px}
.modal h3.sec{margin:16px 0 6px;font-size:.95rem;color:var(--primary)}
.kv{display:grid;grid-template-columns:repeat(auto-fill,minmax(170px,1fr));gap:8px}
.kv>div{display:flex;flex-direction:column;gap:2px;padding:6px 8px;border-radius:8px;background:var(--surface-2)}
.list .item.on{border-color:var(--primary)}

/* المراسلات عبر واتساب */
.wa-qr{display:flex;gap:16px;align-items:center;flex-wrap:wrap}
.wa-qr img{background:#fff;padding:8px;border-radius:12px;border:1px solid var(--border)}
.wa-qr ol{margin:0;padding-inline-start:18px;line-height:2}
.wa-rf{display:flex;flex-wrap:wrap;gap:6px;align-items:center;margin-top:10px}
.wa-rf .badge a{color:inherit;text-decoration:none;margin-inline-start:4px;font-weight:bold}
.wa-sum{margin-top:12px;padding:8px 10px;border-radius:8px;background:var(--primary-soft)}
.wa-bubble{margin-top:10px;padding:10px 12px;border-radius:12px;background:#dcf8c6;color:#111;line-height:1.8;max-width:520px;white-space:normal}
@media (prefers-color-scheme: dark){.wa-bubble{background:#1f3d2b;color:#e8f5ec}}
