/* Fanoos family panel: calm, readable, RTL, mobile-first. */
:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --surface-2: #f1eadc;
  --line: #e2d8c5;
  --text: #1f1b14;
  --muted: #5d5547;
  --accent: #9a4b0b;
  --accent-ink: #fffaf0;
  --accent-soft: #fbead2;
  --ok: #1d6b3a;
  --ok-soft: #e1f1e4;
  --warn: #8a5300;
  --warn-soft: #fbedd0;
  --crit: #b3261e;
  --crit-soft: #fde6e3;
  --alarm-bg: #b3261e;
  --alarm-ink: #ffffff;
  --idle: #8b8478;
  --focus: #1f5fbf;
  --shadow: 0 1px 2px rgb(60 40 10 / 6%), 0 6px 20px rgb(60 40 10 / 6%);
  --radius: 18px;
  --font: "Vazirmatn", Tahoma, "Segoe UI", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15130f;
    --surface: #1e1b16;
    --surface-2: #2a261f;
    --line: #3a342a;
    --text: #f4efe6;
    --muted: #bdb3a2;
    --accent: #f2a640;
    --accent-ink: #1d1404;
    --accent-soft: #3a2a12;
    --ok: #6fd18f;
    --ok-soft: #173322;
    --warn: #f4bc5a;
    --warn-soft: #3a2c10;
    --crit: #ff8a80;
    --crit-soft: #431b18;
    --alarm-bg: #a52a21;
    --alarm-ink: #ffffff;
    --idle: #8f877a;
    --focus: #8ab4ff;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 24px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.75;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}
h1, h2, h3 { line-height: 1.4; margin: 0 0 .4em; }
h1 { font-size: 1.6rem; font-weight: 800; }
h2 { font-size: 1.2rem; font-weight: 700; }
h3 { font-size: 1.05rem; font-weight: 700; }
p { margin: 0 0 .6em; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 8px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; inset-inline-start: -999px; top: 8px; background: var(--surface); padding: 8px 14px; border-radius: 10px; }
.skip:focus { inset-inline-start: 12px; z-index: 50; }
.muted { color: var(--muted); }
.small { font-size: .88rem; }
.ltr { direction: ltr; unicode-bidi: isolate; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1 1 auto; }
.brand > div { min-width: 0; }
.lantern { width: 38px; height: 38px; color: var(--accent); flex: none; }
.brand-name { font-weight: 800; font-size: 1.15rem; line-height: 1.2; }
.brand-sub { color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 6px; flex: none; }
@media (max-width: 420px) { #live-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); } }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 1px solid var(--line); background: var(--surface); border-radius: 14px; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; color: var(--muted); padding: 0 6px; }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--idle); }
.live[data-state="open"] .live-dot { background: var(--ok); box-shadow: 0 0 0 4px var(--ok-soft); }
.live[data-state="error"] .live-dot { background: var(--warn); }

/* Layout */
main { max-width: 760px; margin: 0 auto; padding: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.between { justify-content: space-between; }
.grid2 { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 560px) { .grid2 { grid-template-columns: 1fr 1fr; } }

/* Buttons & fields */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--surface-2); cursor: pointer; font-weight: 700;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--crit); background: transparent; border-color: color-mix(in srgb, var(--crit) 40%, var(--line)); }
.btn.small { min-height: 38px; padding: 0 12px; font-size: .9rem; border-radius: 11px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .6; cursor: progress; }
.field { display: block; margin-bottom: 14px; }
.field > span, .field-label { display: block; font-weight: 500; margin-bottom: 6px; }
.field small { display: block; color: var(--muted); font-size: .85rem; margin-top: 4px; }
input[type="text"], input[type="number"], input[type="url"], input[type="tel"], input:not([type]), select, textarea, input[type="time"] {
  width: 100%; min-height: 50px; padding: 10px 14px; border-radius: 13px;
  border: 1.5px solid var(--line); background: var(--surface); font-size: 1rem;
}
textarea { min-height: 96px; resize: vertical; line-height: 1.7; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
input[type="time"] { direction: ltr; text-align: center; max-width: 150px; }
input[type="checkbox"] { width: 24px; height: 24px; accent-color: var(--accent); flex: none; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 12px; min-height: 48px; cursor: pointer; margin-bottom: 8px; }
.code-input { font-size: 2rem !important; letter-spacing: .4em; text-align: center; font-weight: 800; }
.form-error { color: var(--crit); background: var(--crit-soft); border-radius: 12px; padding: 10px 14px; font-weight: 500; }
.form-ok { color: var(--ok); }

/* Pairing */
.pair-card { margin-top: 6vh; padding: 26px 20px; }
.lead { color: var(--muted); margin-bottom: 1.2em; }

/* Tabs */
.tabs {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; padding: 6px;
  background: var(--surface-2); border-radius: 16px; margin-bottom: 16px;
  position: sticky; top: 76px; z-index: 10;
}
.tabs button {
  min-height: 46px; border: 0; border-radius: 12px; background: transparent; cursor: pointer; font-weight: 700; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* Status */
.status-head { display: flex; align-items: center; gap: 14px; margin-bottom: 8px; }
.status-head h2 { margin: 0; font-size: 1.35rem; }
.status-head p { margin: 0; }
.status-dot { width: 22px; height: 22px; border-radius: 50%; background: var(--idle); flex: none; }
.status-card[data-state="online"] .status-dot { background: var(--ok); box-shadow: 0 0 0 7px var(--ok-soft); }
.status-card[data-state="offline"] .status-dot { background: var(--crit); box-shadow: 0 0 0 7px var(--crit-soft); }
.status-card[data-state="offline"] { border-color: color-mix(in srgb, var(--crit) 35%, var(--line)); }
.facts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 14px 0 6px; }
@media (min-width: 560px) { .facts { grid-template-columns: repeat(4, 1fr); } }
.facts div { background: var(--surface-2); border-radius: 13px; padding: 10px 12px; }
.facts dt { color: var(--muted); font-size: .82rem; }
.facts dd { margin: 0; font-weight: 700; font-size: 1.05rem; }
.health summary { cursor: pointer; font-weight: 500; padding: 8px 0; }
.health ul { list-style: none; padding: 0; margin: 4px 0 0; }
.health li { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px dashed var(--line); }

/* Chips */
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: .82rem; font-weight: 700; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.crit { background: var(--crit-soft); color: var(--crit); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.toggle-chip {
  min-height: 42px; padding: 0 14px; border-radius: 999px; border: 1.5px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 500;
}
.toggle-chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* Segmented filter */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 12px; padding: 4px; }
.seg-btn { border: 0; background: transparent; padding: 6px 12px; border-radius: 9px; cursor: pointer; font-size: .9rem; color: var(--muted); }
.seg-btn[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 700; }

/* Timeline */
.timeline { list-style: none; margin: 12px 0 0; padding: 0; }
.ev { display: grid; grid-template-columns: 16px 1fr; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.ev:first-child { border-top: 0; }
.ev-dot { width: 12px; height: 12px; border-radius: 50%; margin-top: 9px; background: var(--idle); }
.ev[data-sev="warning"] .ev-dot { background: var(--warn); }
.ev[data-sev="critical"] .ev-dot { background: var(--crit); }
.ev[data-sev="info"][data-good="1"] .ev-dot { background: var(--ok); }
.ev-title { font-weight: 700; }
.ev-detail { color: var(--muted); font-size: .95rem; }
.ev-time { color: var(--muted); font-size: .85rem; }
.ev.sos {
  grid-template-columns: 1fr; background: var(--crit-soft); border: 2px solid var(--crit);
  border-radius: 16px; padding: 14px 16px; margin: 10px 0;
}
.ev.sos .ev-dot { display: none; }
.ev.sos .ev-title { color: var(--crit); font-size: 1.2rem; }
.ev.sos .ev-detail, .ev.sos .ev-time { color: var(--text); }
.ev.new { animation: glow 2.4s ease-out; }
@keyframes glow { from { background: var(--accent-soft); } to { background: transparent; } }
@media (prefers-reduced-motion: reduce) { .ev.new { animation: none; } }
.map-link { display: inline-flex; align-items: center; gap: 6px; margin-top: 6px; font-weight: 700; }
.empty { color: var(--muted); text-align: center; padding: 18px 0; }

/* Alert banner */
.alert-banner {
  position: sticky; top: 70px; z-index: 30; max-width: 760px; width: calc(100% - 32px); margin: 10px auto 0; padding: 14px 18px;
  background: var(--alarm-bg); color: var(--alarm-ink); border-radius: 16px; font-weight: 700; box-shadow: var(--shadow);
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.alert-banner button { background: rgb(255 255 255 / 18%); color: #fff; border: 0; border-radius: 10px; min-height: 40px; padding: 0 14px; cursor: pointer; font-weight: 700; }

/* Adherence bars */
.bars { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; align-items: end; height: 190px; margin-top: 10px; }
.bar { display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.bar-stack { width: 100%; max-width: 40px; display: flex; flex-direction: column-reverse; border-radius: 10px; overflow: hidden; background: var(--surface-2); min-height: 6px; }
.bar-stack .taken { background: var(--ok); }
.bar-stack .missed { background: var(--crit); }
.bar-label { font-size: .74rem; color: var(--muted); text-align: center; line-height: 1.35; white-space: nowrap; height: 2.7em; flex: none; }
.bar-num { flex: none; }
.bar-num { font-size: .8rem; font-weight: 700; }
.legend { display: flex; gap: 14px; align-items: center; margin-top: 12px; font-size: .9rem; color: var(--muted); }
.key { display: inline-block; width: 12px; height: 12px; border-radius: 4px; margin-inline-end: -8px; }
.key.taken { background: var(--ok); }
.key.missed { background: var(--crit); }

/* Vitals */
.vitals { list-style: none; padding: 0; margin: 8px 0 0; }
.vitals li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 0; border-top: 1px solid var(--line); }
.vitals li:first-child { border-top: 0; }
.vital-val { font-size: 1.25rem; font-weight: 800; }

/* Editors */
.editor > summary {
  list-style: none; cursor: pointer; display: flex; justify-content: space-between; align-items: center; gap: 10px;
  font-weight: 700; font-size: 1.1rem;
}
.editor > summary::-webkit-details-marker { display: none; }
.editor > summary::after { content: "▾"; color: var(--muted); transition: transform .2s; }
.editor[open] > summary::after { transform: rotate(180deg); }
.editor[open] > summary { margin-bottom: 14px; }
.item {
  border: 1px solid var(--line); border-radius: 15px; padding: 14px; margin-bottom: 12px; background: var(--surface-2);
}
.item-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.item-head h3 { margin: 0; }
.times { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.time-pill { display: inline-flex; align-items: center; gap: 4px; }
.time-pill button { width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.save-row {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 8px;
  position: sticky; bottom: 10px; z-index: 5; padding: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
}
.save-row .status-msg { font-weight: 500; }
.settings-note { margin: 0 4px 10px; }

/* Toast */
.toast {
  position: fixed; inset-inline: 16px; bottom: max(16px, env(safe-area-inset-bottom)); z-index: 60;
  max-width: 560px; margin: 0 auto; background: var(--text); color: var(--bg);
  padding: 14px 18px; border-radius: 15px; box-shadow: var(--shadow); font-weight: 500;
}
.toast[data-kind="error"] { background: var(--alarm-bg); color: var(--alarm-ink); }
