/* ==========================================================================
   Sidlya — صيدلية البيت
   Mobile-first, RTL, light/dark aware.
   ========================================================================== */

:root {
  --primary: #0f766e;
  --primary-600: #0d655e;
  --primary-soft: #d9f1ee;
  --primary-ink: #064e48;
  --accent: #f59e0b;
  --accent-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --info: #2563eb;
  --info-soft: #dbeafe;
  --success: #16a34a;
  --success-soft: #dcfce7;
  --bg: #f3f6f5;
  --surface: #ffffff;
  --surface-2: #f7faf9;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 8px 24px -12px rgba(15, 23, 42, .18);
  --radius: 16px;
  --radius-sm: 10px;
  --nav-h: 68px;
  --font: 'Cairo', 'Segoe UI', Tahoma, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --primary: #2dd4bf;
    --primary-600: #14b8a6;
    --primary-soft: #134e4a;
    --primary-ink: #ccfbf1;
    --accent: #fbbf24;
    --accent-soft: #451a03;
    --danger: #f87171;
    --danger-soft: #450a0a;
    --info: #60a5fa;
    --info-soft: #172554;
    --success: #4ade80;
    --success-soft: #052e16;
    --bg: #0b1220;
    --surface: #111a2b;
    --surface-2: #16213a;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #253047;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -12px rgba(0, 0, 0, .6);
  }
}
:root[data-theme="dark"] {
  --primary: #2dd4bf; --primary-600: #14b8a6; --primary-soft: #134e4a; --primary-ink: #ccfbf1;
  --accent: #fbbf24; --accent-soft: #451a03; --danger: #f87171; --danger-soft: #450a0a;
  --info: #60a5fa; --info-soft: #172554; --success: #4ade80; --success-soft: #052e16;
  --bg: #0b1220; --surface: #111a2b; --surface-2: #16213a; --text: #e5e7eb; --muted: #94a3b8; --border: #253047;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--primary); text-decoration: none; }
h1, h2, h3, h4 { margin: 0; line-height: 1.3; font-weight: 700; }
p { margin: 0; }
img { max-width: 100%; display: block; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* --------------------------------------------------------------------------
   App frame
   -------------------------------------------------------------------------- */
#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  background: var(--bg);
  position: relative;
}
@media (min-width: 720px) {
  body { background: linear-gradient(160deg, var(--primary-soft), var(--bg) 60%); }
  #app { box-shadow: var(--shadow); min-height: 100dvh; }
}

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { width: 36px; height: 36px; border-radius: 10px; }
.brand h1 { font-size: 18px; font-weight: 800; }
.brand small { display: block; font-size: 11px; color: var(--muted); font-weight: 600; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: inline-grid; place-items: center;
  color: #fff; font-weight: 800; font-size: 15px;
  background: var(--primary);
  flex: none;
}
.avatar.lg { width: 56px; height: 56px; font-size: 22px; }
.avatar.xl { width: 72px; height: 72px; font-size: 28px; }
.member-chip {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 4px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
}
.member-chip .avatar { width: 28px; height: 28px; font-size: 12px; }
.member-chip span { font-size: 13px; font-weight: 700; }

main.screen {
  padding: 4px 16px calc(var(--nav-h) + 24px);
  animation: fade .18s ease;
}
main.screen.has-fab { padding-bottom: calc(var(--nav-h) + 96px); } /* room so the FAB never covers the last card */
@keyframes fade { from { opacity: 0; transform: translateY(4px);} to { opacity: 1; transform: none;} }

.bottomnav {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 560px; height: var(--nav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(5, 1fr);
  z-index: 30;
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 11px; font-weight: 700;
}
.bottomnav a svg { width: 24px; height: 24px; }
.bottomnav a.active { color: var(--primary); }
.bottomnav a.cta { position: relative; }
.bottomnav a.cta .pill {
  width: 52px; height: 52px; border-radius: 18px; margin-top: -26px;
  background: var(--primary); color: #fff; display: grid; place-items: center;
  box-shadow: 0 10px 20px -8px color-mix(in srgb, var(--primary) 70%, transparent);
}
.bottomnav a.cta .pill svg { width: 26px; height: 26px; }

/* --------------------------------------------------------------------------
   Primitives
   -------------------------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card + .card, .section + .section { margin-top: 14px; }
.section { margin-top: 18px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-head h2 { font-size: 16px; }
.section-head a, .section-head button.link { font-size: 13px; font-weight: 700; color: var(--primary); background: none; border: 0; padding: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 18px; border-radius: 12px; border: 1px solid transparent;
  font-weight: 700; font-size: 15px; line-height: 1;
  background: var(--primary); color: #fff;
  transition: transform .08s ease, filter .15s ease;
  min-height: 46px;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.secondary { background: var(--primary-soft); color: var(--primary-ink); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.danger { background: var(--danger-soft); color: var(--danger); }
.btn.success { background: var(--success); color: #fff; }
.btn.sm { padding: 8px 12px; min-height: 36px; font-size: 13px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn.icon { padding: 10px; min-height: 40px; width: 40px; }
.btn svg { width: 18px; height: 18px; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; font-weight: 700; color: var(--muted); }
.field .hint { font-size: 12px; color: var(--muted); }
.input, .select, .textarea {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: 16px; outline: none; min-height: 48px;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }
.textarea { min-height: 84px; resize: vertical; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row-3 { display: grid; grid-template-columns: 2fr 1fr; gap: 12px; }
.inline { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-top: 1px solid var(--border); }
.switch:first-child { border-top: 0; }
.switch input { width: 44px; height: 26px; appearance: none; background: var(--border); border-radius: 999px; position: relative; transition: background .2s; flex: none; }
.switch input::after { content: ''; position: absolute; top: 3px; inset-inline-start: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch input:checked { background: var(--primary); }
.switch input:checked::after { transform: translateX(-18px); }
.switch .lbl { font-weight: 700; }
.switch .sub { font-size: 12px; color: var(--muted); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 7px 12px; border-radius: 999px; font-size: 13px; font-weight: 700;
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
}
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip.time { display: inline-flex; gap: 6px; align-items: center; }
.chip .x { opacity: .6; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 800; }
.badge.ok { background: var(--success-soft); color: var(--success); }
.badge.expiring, .badge.warning, .badge.low { background: var(--accent-soft); color: #b45309; }
.badge.expired, .badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.empty { background: var(--border); color: var(--muted); }
.badge.info { background: var(--info-soft); color: var(--info); }
:root[data-theme="dark"] .badge.expiring, :root[data-theme="dark"] .badge.low { color: var(--accent); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .badge.expiring, :root:not([data-theme="light"]) .badge.low { color: var(--accent); } }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.tiny { font-size: 11px; }
.bold { font-weight: 800; }
.center { text-align: center; }
.mt { margin-top: 12px; }
.mb { margin-bottom: 12px; }
.ltr { direction: ltr; unicode-bidi: isolate; display: inline-block; }

.empty {
  text-align: center; padding: 36px 16px; color: var(--muted);
}
.empty .big { font-size: 44px; margin-bottom: 8px; }
.empty h3 { color: var(--text); margin-bottom: 4px; font-size: 16px; }

/* --------------------------------------------------------------------------
   Auth
   -------------------------------------------------------------------------- */
.auth {
  min-height: 100dvh; display: flex; flex-direction: column; justify-content: center;
  padding: 32px 22px;
}
.auth .hero { text-align: center; margin-bottom: 26px; }
.auth .hero .logo { width: 84px; height: 84px; margin: 0 auto 12px; border-radius: 24px; }
.auth .hero h1 { font-size: 26px; font-weight: 900; }
.auth .hero p { color: var(--muted); margin-top: 6px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 12px; padding: 4px; margin-bottom: 18px; }
.tabs button { border: 0; background: transparent; padding: 10px; border-radius: 10px; font-weight: 800; color: var(--muted); }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 14px; }
.picker button {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 10px; display: flex; flex-direction: column; align-items: center; gap: 10px;
  font-weight: 800;
}
.picker button small { color: var(--muted); font-weight: 600; font-size: 12px; }

/* --------------------------------------------------------------------------
   Home
   -------------------------------------------------------------------------- */
.greet { margin: 6px 0 14px; }
.greet h2 { font-size: 22px; font-weight: 900; }
.greet p { color: var(--muted); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px; text-align: center; }
.stat .n { font-size: 24px; font-weight: 900; line-height: 1.1; }
.stat .l { font-size: 12px; color: var(--muted); font-weight: 700; }
.stat.warn .n { color: #b45309; }
.stat.danger .n { color: var(--danger); }
.stat.good .n { color: var(--primary); }

.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.quick button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px 8px; display: flex; flex-direction: column; align-items: center; gap: 8px;
  font-weight: 800; font-size: 12.5px; color: var(--text);
}
.quick button .ic { width: 44px; height: 44px; border-radius: 13px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary-ink); }
.quick button .ic svg { width: 22px; height: 22px; }

.progress { height: 10px; border-radius: 999px; background: var(--border); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 999px; transition: width .4s ease; }

/* Dose timeline */
.dose {
  display: flex; align-items: center; gap: 12px; padding: 12px 0;
  border-top: 1px solid var(--border);
}
.dose:first-child { border-top: 0; padding-top: 4px; }
.dose .time { width: 52px; font-weight: 900; font-size: 14px; direction: ltr; text-align: center; color: var(--muted); }
.dose .time.now { color: var(--primary); }
.dose .body { flex: 1; min-width: 0; }
.dose .body .name { font-weight: 800; }
.dose .body .sub { font-size: 12px; color: var(--muted); }
.dose .actions { display: flex; gap: 6px; }
.dose.taken .name { text-decoration: line-through; color: var(--muted); }
.dose.taken .time { color: var(--success); }
.dose.missed .time { color: var(--danger); }
.dose.skipped .name { color: var(--muted); }
.check-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 2px solid var(--border); background: var(--surface);
  display: grid; place-items: center; color: var(--success);
}
.check-btn.on { background: var(--success); border-color: var(--success); color: #fff; }
.check-btn svg { width: 20px; height: 20px; }

/* Alerts */
.alert { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border-radius: 14px; border: 1px solid var(--border); background: var(--surface); }
.alert + .alert { margin-top: 8px; }
.alert .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; flex: none; }
.alert.danger .dot { background: var(--danger); }
.alert.warning .dot { background: var(--accent); }
.alert.info .dot { background: var(--info); }
.alert .msg { font-size: 14px; font-weight: 600; }

/* --------------------------------------------------------------------------
   Pharmacy list
   -------------------------------------------------------------------------- */
.searchbar { display: flex; gap: 8px; margin-bottom: 12px; }
.searchbar .input { flex: 1; }
.med {
  display: flex; gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px;
}
.med + .med { margin-top: 8px; }
.med .thumb { width: 52px; height: 52px; border-radius: 12px; background: var(--primary-soft); display: grid; place-items: center; overflow: hidden; flex: none; color: var(--primary-ink); font-size: 22px; }
.med .thumb img { width: 100%; height: 100%; object-fit: cover; }
.med .info { flex: 1; min-width: 0; }
.med .info .name { font-weight: 800; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.med .info .meta { font-size: 12.5px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.med .qty { text-align: center; min-width: 56px; }
.med .qty .n { font-size: 20px; font-weight: 900; line-height: 1; }
.med .qty .u { font-size: 11px; color: var(--muted); }
.med.expired { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.med.expiring { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); }

.fab {
  position: fixed; bottom: calc(var(--nav-h) + 16px); inset-inline-end: max(16px, calc(50% - 280px + 16px));
  width: 58px; height: 58px; border-radius: 20px; border: 0;
  background: var(--primary); color: #fff; display: grid; place-items: center;
  box-shadow: 0 14px 28px -10px color-mix(in srgb, var(--primary) 80%, transparent);
  z-index: 25;
}
.fab svg { width: 26px; height: 26px; }

/* --------------------------------------------------------------------------
   Check before buying
   -------------------------------------------------------------------------- */
.verdict { border-radius: var(--radius); padding: 20px; text-align: center; border: 2px solid; }
.verdict .icon { font-size: 46px; line-height: 1; margin-bottom: 8px; }
.verdict h3 { font-size: 19px; font-weight: 900; }
.verdict p { margin-top: 6px; font-size: 14px; }
.verdict.have { background: var(--success-soft); border-color: var(--success); color: var(--success); }
.verdict.have_equivalent { background: var(--info-soft); border-color: var(--info); color: var(--info); }
.verdict.have_expired { background: var(--accent-soft); border-color: var(--accent); color: #b45309; }
.verdict.buy { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.verdict h3, .verdict p { color: var(--text); }
.verdict.have h3 { color: var(--success); }
.verdict.have_equivalent h3 { color: var(--info); }
.verdict.have_expired h3 { color: #b45309; }
.verdict.buy h3 { color: var(--danger); }

.suggest { position: relative; }
.suggest .list {
  position: absolute; top: calc(100% + 4px); inset-inline: 0; z-index: 40;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow);
  max-height: 260px; overflow: auto;
}
.suggest .list button { display: flex; justify-content: space-between; gap: 8px; width: 100%; text-align: start; padding: 10px 14px; background: none; border: 0; border-top: 1px solid var(--border); }
.suggest .list button:first-child { border-top: 0; }
.suggest .list button b { font-weight: 800; }
.suggest .list button small { color: var(--muted); font-size: 12px; }
.suggest .list button:hover { background: var(--surface-2); }

.history li { display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-top: 1px solid var(--border); font-size: 13.5px; }
.history li:first-child { border-top: 0; }
.history { list-style: none; margin: 0; padding: 0; }

/* --------------------------------------------------------------------------
   Family
   -------------------------------------------------------------------------- */
.member-card { display: flex; align-items: center; gap: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px; }
.member-card + .member-card { margin-top: 8px; }
.member-card .info { flex: 1; }
.member-card .info .name { font-weight: 800; }
.member-card .info .sub { color: var(--muted); font-size: 12.5px; }
.colors { display: flex; gap: 8px; flex-wrap: wrap; }
.colors label { width: 32px; height: 32px; border-radius: 50%; cursor: pointer; border: 3px solid transparent; }
.colors input { display: none; }
.colors input:checked + label { border-color: var(--text); }

.code-box { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--surface-2); border: 1px dashed var(--border); border-radius: 12px; padding: 12px 14px; }
.code-box b { font-size: 24px; letter-spacing: 4px; direction: ltr; font-weight: 900; }

/* --------------------------------------------------------------------------
   Sheet (bottom modal)
   -------------------------------------------------------------------------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(2, 6, 23, .5); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease; }
.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; overflow: auto;
  background: var(--surface); border-radius: 22px 22px 0 0; padding: 10px 18px calc(20px + env(safe-area-inset-bottom));
  animation: up .22s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet .grab { width: 44px; height: 5px; border-radius: 99px; background: var(--border); margin: 4px auto 14px; }
.sheet h2 { font-size: 18px; margin-bottom: 14px; display: flex; justify-content: space-between; align-items: center; }
.sheet h2 button { background: none; border: 0; color: var(--muted); font-size: 22px; line-height: 1; }

.scan-area {
  border: 2px dashed var(--border); border-radius: 16px; padding: 18px; text-align: center; background: var(--surface-2);
}
.scan-area img { max-height: 220px; margin: 0 auto 10px; border-radius: 12px; }
.scan-area .ocr-progress { margin-top: 10px; }
.ocr-raw { font-family: ui-monospace, monospace; font-size: 11.5px; direction: ltr; text-align: left; white-space: pre-wrap; background: var(--surface-2); padding: 10px; border-radius: 10px; max-height: 160px; overflow: auto; color: var(--muted); }

/* --------------------------------------------------------------------------
   Toast
   -------------------------------------------------------------------------- */
.toasts { position: fixed; bottom: calc(var(--nav-h) + 14px); left: 50%; transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; width: min(92%, 420px); pointer-events: none; }
.toast { background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; font-weight: 700; font-size: 14px; box-shadow: var(--shadow); animation: up .2s ease; }
.toast.error { background: var(--danger); color: #fff; }
.toast.success { background: var(--success); color: #fff; }

/* Settings list */
.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.list .item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-top: 1px solid var(--border); width: 100%; background: none; border-inline: 0; border-bottom: 0; text-align: start; }
.list .item:first-child { border-top: 0; }
.list .item .t { font-weight: 700; }
.list .item .d { font-size: 12.5px; color: var(--muted); }
.list .item .chev { color: var(--muted); }

/* Adherence bars */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 72px; }
.bars .bar { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.bars .bar span { width: 100%; background: var(--primary); border-radius: 6px 6px 0 0; min-height: 3px; opacity: .9; }
.bars .bar small { font-size: 10px; color: var(--muted); }

.offline-banner { background: var(--accent-soft); color: #92400e; font-size: 12.5px; font-weight: 700; text-align: center; padding: 6px; }
