:root {
  --bg: #0f1216;
  --card: #181c22;
  --card-2: #20252d;
  --line: #2a3039;
  --text: #eef1f5;
  --muted: #8d96a3;
  --accent: #4da3ff;
  --ok: #3ccf8e;
  --warn: #f5b84a;
  --bad: #ff6b6b;
  --radius: 16px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f5f8; --card: #ffffff; --card-2: #eef1f5; --line: #dde2e8;
    --text: #12161c; --muted: #5d6673; color-scheme: light;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button { font: inherit; color: inherit; }
.loading { padding: 40vh 0; text-align: center; color: var(--muted); }

/* header */
header {
  position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  display: flex; align-items: center; justify-content: space-between; padding: 14px 18px 10px;
}
header h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.conn { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--bad); }
.dot.on { background: var(--ok); }
.menu-btn { background: none; border: 0; color: var(--muted); font-size: 14px; padding: 6px 0 6px 12px; cursor: pointer; }

main { padding: 4px 14px 40px; max-width: 900px; margin: 0 auto; }
section { margin: 18px 0 8px; }
section > h2 {
  font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); margin: 0 4px 8px; display: flex; justify-content: space-between; align-items: baseline;
}
section > h2 a { color: var(--accent); text-transform: none; letter-spacing: 0; font-weight: 500; font-size: 14px; text-decoration: none; }
.grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.card { background: var(--card); border-radius: var(--radius); padding: 14px 16px; border: 1px solid var(--line); }
.card.off { opacity: .55; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.title { font-weight: 600; }
.sub { color: var(--muted); font-size: 13px; }
.state { font-size: 26px; font-weight: 650; letter-spacing: -0.01em; margin: 6px 0 10px; text-transform: capitalize; }
.state.open, .state.opening, .state.closing { color: var(--warn); }
.state.closed { color: var(--ok); }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  flex: 1; min-height: 46px; border-radius: 12px; border: 1px solid var(--line); background: var(--card-2);
  font-weight: 600; cursor: pointer; padding: 0 14px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.busy { opacity: .6; }
.alert { color: var(--bad); font-size: 13px; font-weight: 600; margin-top: 8px; }

/* toggles */
.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-top: 1px solid var(--line); }
.toggle-row:first-child { border-top: 0; padding-top: 2px; }
.switch { position: relative; width: 52px; height: 32px; border-radius: 16px; border: 0; background: var(--line); cursor: pointer; flex: none; transition: background .15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch.on { background: var(--ok); }
.switch.on::after { transform: translateX(20px); }
.switch:disabled { opacity: .45; }

/* pool */
.temps { display: flex; gap: 10px; margin-bottom: 6px; }
.temp { flex: 1; background: var(--card-2); border-radius: 12px; padding: 10px; text-align: center; }
.temp b { display: block; font-size: 24px; }
.temp span { font-size: 12px; color: var(--muted); }
.setpoint { display: flex; align-items: center; gap: 8px; }
.setpoint .val { min-width: 54px; text-align: center; font-weight: 650; font-size: 18px; }
.round { width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line); background: var(--card-2); font-size: 20px; cursor: pointer; }

/* doors + sensors */
.list .toggle-row .badge { font-size: 13px; font-weight: 600; padding: 3px 10px; border-radius: 10px; background: var(--card-2); }
.badge.open { color: var(--warn); }
.badge.closed { color: var(--ok); }
.badge.unknown, .badge.offline { color: var(--muted); }

/* cameras */
.cams { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.cam { position: relative; border-radius: 12px; overflow: hidden; background: #000; aspect-ratio: 16 / 9; border: 1px solid var(--line); cursor: pointer; }
.cam img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam .label { position: absolute; left: 0; right: 0; bottom: 0; padding: 14px 8px 6px; font-size: 12px; font-weight: 600; color: #fff;
  background: linear-gradient(transparent, rgba(0,0,0,.7)); }
.cam.offline::before { content: "Offline"; position: absolute; inset: 0; display: grid; place-items: center; color: #aaa; font-size: 13px; }
.viewer { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.92); display: flex; flex-direction: column; justify-content: center; padding: 20px; }
.viewer img { width: 100%; max-height: 70vh; object-fit: contain; }
.viewer .row { color: #fff; margin-top: 14px; }

.empty { color: var(--muted); font-size: 14px; }
.people { display: flex; gap: 10px; flex-wrap: wrap; }
.person { background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px; font-weight: 600; display: flex; gap: 8px; align-items: center; }

/* login */
.login { max-width: 360px; margin: 14vh auto 0; padding: 0 20px; }
.login h1 { font-size: 28px; margin: 0 0 6px; }
.login p { color: var(--muted); margin: 0 0 22px; }
.login input { width: 100%; min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 0 14px; font-size: 16px; margin-bottom: 10px; }
.login .btn { width: 100%; }
.login .err { color: var(--bad); min-height: 22px; font-size: 14px; }

/* activity */
table.audit { width: 100%; border-collapse: collapse; font-size: 13px; }
table.audit td { padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: top; }
table.audit .bad { color: var(--bad); }

#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(120px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 600;
  transition: transform .2s; max-width: 90vw; z-index: 30;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--bad); color: #fff; }
