:root {
  --primary: #0e7490;
  --primary-dark: #155e75;
  --primary-soft: #e0f2f7;
  --accent: #f59e0b;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .06);
  --radius: 14px;
  --nav-h: 64px;
  --font: 'Tajawal', 'Segoe UI', Tahoma, Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --primary: #22b8d4;
    --primary-dark: #0e7490;
    --primary-soft: #0b3a45;
    --bg: #0b1220;
    --card: #131c2e;
    --text: #e5edf7;
    --muted: #93a3b8;
    --line: #22304a;
    --ok-soft: #0f2f1c;
    --warn-soft: #3a2a08;
    --danger-soft: #3a1414;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; font-family: var(--font); font-size: 16px; line-height: 1.5;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent; overscroll-behavior-y: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
a { color: var(--primary); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); background: var(--danger-soft); padding: 8px 12px; border-radius: 10px; font-size: 14px; }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; gap: 8px; align-items: center; }
.row.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.mt { margin-top: 12px; }

/* ---------- الأزرار ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: 12px; font-weight: 700; font-size: 15px;
  background: var(--card); color: var(--text); border: 1px solid var(--line); min-height: 44px;
  transition: transform .05s ease, background .15s ease;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); color: #fff; border-color: transparent; }
.btn.ok { background: var(--ok); color: #fff; border-color: transparent; }
.btn.danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 12px; min-height: 36px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .55; cursor: default; }

/* ---------- تسجيل الدخول ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; background: linear-gradient(160deg, var(--primary-dark), var(--primary) 60%, #0891b2); }
.login-card { background: var(--card); border-radius: 22px; padding: 28px 24px; width: min(400px, 100%); box-shadow: var(--shadow); text-align: center; }
.login-logo { border-radius: 22px; margin-bottom: 8px; }
.login-card h1 { margin: 0 0 4px; font-size: 24px; }
.login-card label { display: block; text-align: start; margin: 14px 0 0; font-weight: 700; font-size: 14px; }
.login-card input { width: 100%; margin-top: 6px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-size: 16px; }
.login-card .btn { margin-top: 18px; }
.login-card .error { margin-top: 12px; }

/* ---------- الهيكل ---------- */
.app { min-height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px; background: var(--primary); color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { border-radius: 10px; }
.brand .title { font-weight: 700; font-size: 18px; line-height: 1.2; }
.brand .subtitle { font-size: 12px; opacity: .85; }
.topbar-side { display: flex; align-items: center; gap: 10px; }
.presence { display: flex; gap: 4px; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-size: 13px; font-weight: 700;
  background: rgba(255, 255, 255, .22); color: #fff; position: relative; border: 2px solid transparent;
}
.avatar.online { border-color: #4ade80; }
.avatar.online::after { content: ''; position: absolute; bottom: -2px; inset-inline-end: -2px; width: 9px; height: 9px; border-radius: 50%; background: #4ade80; border: 2px solid var(--primary); }
/* مؤشر الاتصال: يظهر فقط عند الانقطاع أو أثناء إعادة الاتصال */
.conn { width: 10px; height: 10px; border-radius: 50%; background: #fbbf24; }
.conn.on { display: none; }
.conn.off { background: #f87171; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }

.view { flex: 1; padding: 14px 14px calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); max-width: 760px; width: 100%; margin: 0 auto; }

.nav {
  position: fixed; bottom: 0; inset-inline: 0; height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom); background: var(--card); border-top: 1px solid var(--line);
  display: flex; justify-content: space-around; z-index: 20;
}
.nav button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 12px; color: var(--muted); position: relative; padding: 6px 0; }
.nav button .ic { font-size: 22px; line-height: 1; }
.nav button.active { color: var(--primary); font-weight: 700; }
.badge { position: absolute; top: 4px; inset-inline-start: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center; }

/* ---------- البطاقات ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 12px; }
.card h2 { margin: 0 0 8px; font-size: 17px; }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.section-title { font-size: 14px; color: var(--muted); margin: 18px 4px 8px; font-weight: 700; }

/* رأس اليوم */
.dayhead { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.dayhead .date { font-weight: 700; font-size: 18px; }
.dayhead .sub { font-size: 13px; color: var(--muted); }
.progress { height: 8px; background: var(--line); border-radius: 6px; overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; background: linear-gradient(90deg, var(--ok), #4ade80); border-radius: 6px; transition: width .3s ease; }
.datenav { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.datenav input[type=date] { flex: 1; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); text-align: center; }
.iconbtn { width: 42px; height: 42px; border-radius: 12px; background: var(--card); border: 1px solid var(--line); display: grid; place-items: center; font-size: 18px; }

.week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; margin-bottom: 14px; }
.week button { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 6px 2px; display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.week button.sel { border-color: var(--primary); color: var(--primary); font-weight: 700; }
.week .d { font-size: 15px; font-weight: 700; color: var(--text); }
.week .bar { width: 70%; height: 5px; border-radius: 4px; background: var(--line); overflow: hidden; }
.week .bar span { display: block; height: 100%; background: var(--ok); }

/* فترات الجرعات */
.slot { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 12px; overflow: hidden; border-inline-start: 5px solid var(--line); }
.slot.upcoming { border-inline-start-color: #94a3b8; }
.slot.now { border-inline-start-color: var(--accent); }
.slot.late { border-inline-start-color: var(--danger); }
.slot.done { border-inline-start-color: var(--ok); }
.slot-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.slot-head .time { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.chip { font-size: 12px; padding: 2px 10px; border-radius: 999px; font-weight: 700; background: var(--line); color: var(--muted); }
.chip.now { background: var(--warn-soft); color: var(--warn); }
.chip.late { background: var(--danger-soft); color: var(--danger); }
.chip.done { background: var(--ok-soft); color: var(--ok); }
.chip.upcoming { background: var(--primary-soft); color: var(--primary); }
.slot ul { list-style: none; margin: 0; padding: 0; }
.dose { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.dose:last-child { border-bottom: 0; }
.dose .dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.dose .info { flex: 1; min-width: 0; }
.dose .name { font-weight: 700; font-size: 16px; }
.dose .meta { font-size: 13px; color: var(--muted); }
.dose.taken .name { color: var(--muted); text-decoration: line-through; text-decoration-color: var(--ok); }
.dose.skipped .name { color: var(--muted); }
.dose .actions { display: flex; align-items: center; gap: 6px; flex: none; }
.btn-check { width: 46px; height: 46px; border-radius: 50%; background: var(--ok-soft); color: var(--ok); font-size: 22px; font-weight: 900; border: 2px solid var(--ok); display: grid; place-items: center; }
.btn-check:active { background: var(--ok); color: #fff; }
.btn-more { width: 36px; height: 36px; border-radius: 50%; background: var(--bg); color: var(--muted); font-size: 16px; border: 1px solid var(--line); }
.status { font-size: 13px; padding: 6px 10px; border-radius: 10px; text-align: end; line-height: 1.3; }
.status.taken { background: var(--ok-soft); color: var(--ok); font-weight: 700; }
.status.skipped { background: var(--warn-soft); color: var(--warn); font-weight: 700; }
.status small { display: block; font-weight: 400; opacity: .8; }

.empty { text-align: center; color: var(--muted); padding: 40px 12px; }
.empty .big { font-size: 40px; }

/* لافتة الإشعارات */
.notice { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: var(--radius); background: var(--warn-soft); color: #7c2d12; margin-bottom: 12px; }
@media (prefers-color-scheme: dark) { .notice { color: #fde68a; } }
.notice .grow { font-size: 14px; }

/* ---------- المحادثة ---------- */
.chat { display: flex; flex-direction: column; min-height: calc(100dvh - 140px - var(--nav-h)); }
.chat-users { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; font-size: 13px; color: var(--muted); }
.chat-users span { display: inline-flex; align-items: center; gap: 4px; }
.chat-users i { width: 8px; height: 8px; border-radius: 50%; background: #cbd5e1; display: inline-block; }
.chat-users .on i { background: var(--ok); }
.messages { flex: 1; display: flex; flex-direction: column; gap: 6px; padding-bottom: 8px; }
.daysep { align-self: center; font-size: 12px; color: var(--muted); background: var(--card); padding: 2px 12px; border-radius: 999px; margin: 8px 0; box-shadow: var(--shadow); }
.msg { max-width: 82%; padding: 8px 12px; border-radius: 16px; background: var(--card); box-shadow: var(--shadow); align-self: flex-start; word-wrap: break-word; white-space: pre-wrap; }
.msg.mine { align-self: flex-end; background: var(--primary); color: #fff; }
.msg .who { font-size: 12px; font-weight: 700; color: var(--primary); margin-bottom: 2px; }
.msg .when { font-size: 11px; opacity: .7; text-align: end; margin-top: 2px; }
.typing { font-size: 12px; color: var(--muted); height: 18px; padding: 0 6px; }
.composer {
  position: fixed; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); inset-inline: 0; z-index: 15;
  background: var(--card); border-top: 1px solid var(--line); padding: 8px 12px;
}
.composer .inner { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; max-height: 120px; padding: 10px 14px; border-radius: 18px; border: 1px solid var(--line); background: var(--bg); line-height: 1.4; }
.composer .send { width: 46px; height: 46px; border-radius: 50%; background: var(--primary); color: #fff; font-size: 20px; display: grid; place-items: center; flex: none; }
.view.chatview { padding-bottom: calc(var(--nav-h) + 90px + env(safe-area-inset-bottom)); }

/* ---------- قوائم الأدوية ---------- */
.med { display: flex; gap: 12px; align-items: flex-start; }
.med .dot { width: 14px; height: 14px; border-radius: 50%; margin-top: 6px; flex: none; }
.med .name { font-weight: 700; font-size: 16px; }
.med .meta { color: var(--muted); font-size: 13px; }
.times { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.times .t { background: var(--primary-soft); color: var(--primary); border-radius: 8px; padding: 2px 8px; font-size: 13px; font-weight: 700; }
.times .t.x { padding-inline-end: 4px; display: inline-flex; align-items: center; gap: 4px; }
.times .t.x button { color: var(--danger); font-weight: 900; padding: 0 4px; }
.med .ops { display: flex; flex-direction: column; gap: 6px; }
.med.inactive { opacity: .6; }

/* ---------- النماذج ---------- */
.field { display: block; margin: 10px 0; font-size: 14px; font-weight: 700; }
.field input:not([type=checkbox]):not([type=radio]), .field select, .field textarea {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-weight: 400; font-size: 16px;
}
.field.inline { display: flex; align-items: center; gap: 10px; }
.field.inline input { width: auto; margin: 0; }
.daypick { display: flex; gap: 6px; flex-wrap: wrap; }
.daypick label { display: inline-flex; align-items: center; justify-content: center; width: 42px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-weight: 700; font-size: 13px; cursor: pointer; }
.daypick input { display: none; }
.daypick input:checked + span { color: var(--primary); }
.daypick label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatches label { width: 30px; height: 30px; border-radius: 50%; cursor: pointer; border: 3px solid transparent; }
.swatches input { display: none; }
.swatches label:has(input:checked) { border-color: var(--text); }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.switch:last-child { border-bottom: 0; }
.switch input { width: 22px; height: 22px; accent-color: var(--primary); }

/* ---------- المستخدمون ---------- */
.user { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.user:last-child { border-bottom: 0; }
.user .av { width: 38px; height: 38px; border-radius: 50%; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-weight: 700; }
.user .nm { font-weight: 700; }
.user .un { font-size: 12px; color: var(--muted); direction: ltr; text-align: end; }
.tag { font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--line); color: var(--muted); }
.tag.admin { background: var(--warn-soft); color: var(--warn); }

/* ---------- عام: نافذة منبثقة / تنبيهات ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: rgba(2, 6, 23, .55); display: flex; align-items: flex-end; justify-content: center; padding: 0; }
@media (min-width: 640px) { .modal { align-items: center; padding: 20px; } }
.modal-card { background: var(--card); width: min(560px, 100%); max-height: 92dvh; overflow: auto; border-radius: 22px 22px 0 0; padding: 18px 18px calc(18px + env(safe-area-inset-bottom)); box-shadow: 0 -8px 30px rgba(0, 0, 0, .25); }
@media (min-width: 640px) { .modal-card { border-radius: 22px; } }
.modal-card h2 { margin: 0 0 12px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

.toasts { position: fixed; top: calc(70px + env(safe-area-inset-top)); inset-inline: 0; z-index: 60; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; padding: 0 12px; }
.toast { background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow); max-width: 480px; animation: fade .25s ease; pointer-events: auto; }
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }
@keyframes fade { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

.banner { position: fixed; top: calc(64px + env(safe-area-inset-top)); inset-inline: 12px; z-index: 55; background: var(--card); border: 2px solid var(--accent); border-radius: var(--radius); box-shadow: 0 10px 30px rgba(0, 0, 0, .25); padding: 14px 16px; max-width: 560px; margin: 0 auto; animation: fade .3s ease; }
.banner h3 { margin: 0 0 6px; font-size: 16px; }
.banner pre { margin: 0 0 10px; font: inherit; white-space: pre-wrap; font-size: 14px; }
.banner .row .btn { flex: 1; }

.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.pill.on { background: var(--ok-soft); color: var(--ok); }
.pill.off { background: var(--danger-soft); color: var(--danger); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }

.kv { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--line); font-size: 14px; }
.kv:last-child { border-bottom: 0; }
.ltr { direction: ltr; unicode-bidi: embed; }
