:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #16202c;
  --muted: #5b6676;
  --rule: #dde2ea;
  --primary: #0d3b8c;
  --primary-ink: #ffffff;
  --primary-soft: #dbe6f8;
  --green: #2e9e3e;
  --danger: #b3261e;
  --warn: #c97a00;
  --low: #5b7083;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1117; --surface: #161b22; --surface-2: #1f2630; --text: #e6edf3; --muted: #9aa6b4;
    --rule: #2b333d; --primary: #7fb0ff; --primary-ink: #08162e; --primary-soft: #1b2b45;
    --green: #5cc46b; --danger: #ff7b72; --warn: #f0a640; --low: #9aa6b4;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1117; --surface: #161b22; --surface-2: #1f2630; --text: #e6edf3; --muted: #9aa6b4;
  --rule: #2b333d; --primary: #7fb0ff; --primary-ink: #08162e; --primary-soft: #1b2b45;
  --green: #5cc46b; --danger: #ff7b72; --warn: #f0a640; --low: #9aa6b4;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }

#app { max-width: 760px; margin: 0 auto; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px); -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--rule);
}
.topbar h1 { font-size: 1.2rem; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .logo { width: 36px; height: 36px; border-radius: 8px; flex: none; }
.iconbtn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 20px;
  display: grid; place-items: center; cursor: pointer; color: var(--primary); flex: none;
}
.iconbtn:hover { background: var(--surface-2); }
.iconbtn svg { width: 22px; height: 22px; }

/* ---------- Layout ---------- */
.page { padding: 12px 16px; display: grid; gap: 12px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; border: 1px solid var(--rule); }
.card h2 { font-size: 1.02rem; margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: .86rem; }
.danger { color: var(--danger); }
.row { display: flex; align-items: center; gap: 12px; }
.row > .grow { flex: 1; min-width: 0; }
.stack { display: grid; gap: 8px; }
.center { display: grid; place-items: center; min-height: 60dvh; padding: 24px; text-align: center; }
.list { display: grid; gap: 0; }
.list > .item { padding: 12px 0; border-bottom: 1px solid var(--rule); }
.list > .item:last-child { border-bottom: 0; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Buttons ---------- */
.btn {
  border: 0; border-radius: 999px; padding: 12px 18px; min-height: 44px; cursor: pointer;
  background: var(--primary); color: var(--primary-ink); font-weight: 600;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.ghost { background: transparent; color: var(--primary); padding: 8px 10px; min-height: 36px; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 12px; min-height: 32px; font-size: .9rem; }

/* ---------- Forms ---------- */
label.field { display: grid; gap: 6px; }
label.field > span { font-size: .9rem; color: var(--muted); }
input[type=email], input[type=password], input[type=text], input[type=number], input[type=time], select, textarea {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--rule);
  background: var(--surface); min-height: 44px;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
input[type=range] { width: 100%; accent-color: var(--primary); }

/* iOS-style switch */
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 31px; background: var(--rule); transition: background .2s; cursor: pointer; }
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s;
}
.switch input:checked + span { background: var(--green); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Device cards ---------- */
.device { cursor: pointer; }
.device:active { transform: scale(.995); }
.badge { background: var(--danger); color: #fff; border-radius: 999px; min-width: 24px; height: 24px; padding: 0 7px; display: grid; place-items: center; font-size: .8rem; font-weight: 700; }
.phoneicon { width: 40px; height: 40px; border-radius: 12px; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; flex: none; }

/* ---------- Tabs & chips ---------- */
.tabs {
  display: flex; gap: 4px; overflow-x: auto; padding: 8px 16px; scrollbar-width: none;
  position: sticky; top: calc(57px + env(safe-area-inset-top)); z-index: 4; background: var(--bg);
  border-bottom: 1px solid var(--rule);
}
.tabs::-webkit-scrollbar { display: none; }
.tab { border: 0; background: transparent; padding: 8px 14px; border-radius: 999px; white-space: nowrap; cursor: pointer; color: var(--muted); font-weight: 600; }
.tab[aria-selected="true"] { background: var(--primary); color: var(--primary-ink); }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { border: 1px solid var(--rule); background: var(--surface); padding: 6px 12px; border-radius: 999px; white-space: nowrap; cursor: pointer; font-size: .9rem; }
.chip[aria-pressed="true"] { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); font-weight: 600; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 8px 6px; border-radius: 10px; cursor: pointer; font-size: .88rem; }
.seg button[aria-pressed="true"] { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }

/* ---------- Alerts ---------- */
.alert { display: grid; grid-template-columns: 10px 1fr; gap: 12px; }
.dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; }
.sev3 { background: var(--danger); } .sev2 { background: var(--warn); } .sev1 { background: var(--low); }
.alert.unread .title { font-weight: 700; }
.alert .title { margin: 0; }

/* ---------- Bars ---------- */
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--primary); border-radius: 999px; }
.bar.over > i { background: var(--danger); }

.daypicker { display: flex; align-items: center; justify-content: space-between; }
.daypicker strong { font-size: 1.05rem; }

/* ---------- Sticky save bar ---------- */
.savebar {
  position: sticky; bottom: calc(12px + env(safe-area-inset-bottom)); z-index: 4;
  display: flex; gap: 8px; justify-content: flex-end; align-items: center;
  background: var(--surface); border: 1px solid var(--rule); border-radius: 999px; padding: 8px 8px 8px 16px;
  box-shadow: 0 6px 24px rgba(0,0,0,.15);
}
.savebar .grow { flex: 1; font-weight: 600; }

/* ---------- Banners, toast, spinner ---------- */
.banner { background: var(--primary-soft); border: 1px solid var(--primary); color: var(--text); border-radius: var(--radius); padding: 12px 14px; }
.banner.warn { background: color-mix(in srgb, var(--warn) 14%, var(--surface)); border-color: var(--warn); }
#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: #1f2937; color: #fff; padding: 10px 16px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; max-width: calc(100% - 32px); z-index: 20; font-size: .95rem;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--rule); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Sign in ---------- */
.signin { max-width: 420px; margin: 0 auto; padding: calc(32px + env(safe-area-inset-top)) 16px 32px; display: grid; gap: 14px; text-align: center; }
.signin img { width: 180px; height: 180px; margin: 0 auto; border-radius: 24px; }
.signin form { display: grid; gap: 12px; text-align: left; }

dialog { border: 0; border-radius: 18px; padding: 20px; max-width: min(440px, calc(100% - 32px)); background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin: 0 0 8px; }
dialog .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
