:root {
  --bg: #f3f6f8;
  --surface: #ffffff;
  --surface-2: #eef3f6;
  --text: #15232d;
  --muted: #5d6f7b;
  --line: #dbe3e8;
  --brand: #0f6e8c;
  --brand-ink: #ffffff;
  --ok: #1d8a4e;
  --ok-bg: #e5f5ec;
  --high: #c62d2d;
  --high-bg: #fdeaea;
  --low: #1f5fbf;
  --low-bg: #e7effc;
  --off: #7a8790;
  --off-bg: #eceff1;
  --warn: #9a6200;
  --warn-bg: #fff4dc;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 35, 45, .06), 0 2px 8px rgba(16, 35, 45, .05);
  --nav-h: 62px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e161b;
    --surface: #16222a;
    --surface-2: #1d2b34;
    --text: #e6eef2;
    --muted: #93a6b2;
    --line: #2a3a44;
    --brand: #4fb3d4;
    --brand-ink: #062530;
    --ok: #4cc785;
    --ok-bg: #133325;
    --high: #ff7b7b;
    --high-bg: #3a1a1c;
    --low: #7fb0ff;
    --low-bg: #172843;
    --off: #93a0a8;
    --off-bg: #222e35;
    --warn: #f0b552;
    --warn-bg: #3a2c12;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Thonburi", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .back {
  border: 0; background: none; padding: 6px 8px 6px 0; font-size: 22px; line-height: 1; color: var(--brand);
}
.pill {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  background: var(--warn-bg); color: var(--warn); white-space: nowrap;
}
main { padding: 14px 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 20px); max-width: 960px; margin: 0 auto; }

.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--muted); font-size: 12px; position: relative;
}
.bottomnav a svg { width: 24px; height: 24px; }
.bottomnav a.active { color: var(--brand); font-weight: 600; }
.bottomnav .dot {
  position: absolute; top: 8px; left: calc(50% + 8px); min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--high); color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center;
}

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login {
  width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px; padding: 28px 22px;
  box-shadow: var(--shadow); border: 1px solid var(--line);
}
.login .logo { width: 64px; height: 64px; border-radius: 16px; display: block; margin: 0 auto 12px; }
.login h1 { text-align: center; margin: 0 0 4px; font-size: 22px; }
.login p.sub { text-align: center; margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); font-size: 16px; outline: none;
}
.field input:focus { border-color: var(--brand); background: var(--surface); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 10px; padding: 12px 18px; font-weight: 600;
  background: var(--brand); color: var(--brand-ink);
}
.btn.block { width: 100%; }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--high-bg); color: var(--high); }
.btn:disabled { opacity: .6; cursor: default; }
.err { background: var(--high-bg); color: var(--high); border-radius: 10px; padding: 10px 12px; font-size: 14px; margin-bottom: 14px; }
.login .pill { display: table; margin: 0 auto 16px; }

/* ---------- summary chips ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; margin-bottom: 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 7px 14px; font-size: 14px; display: inline-flex; gap: 6px; align-items: center;
}
.chip b { font-variant-numeric: tabular-nums; }
.chip.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.chip.alert b { color: var(--high); }
.chip.on.alert b { color: inherit; }

.search {
  width: 100%; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); margin-bottom: 14px; font-size: 16px; outline: none;
}
.search:focus { border-color: var(--brand); }

.group-h { margin: 18px 2px 8px; font-size: 15px; font-weight: 700; }
.group-h:first-child { margin-top: 4px; }
.room-h { margin: 10px 2px 6px; font-size: 13px; color: var(--muted); font-weight: 600; }

/* ---------- device cards ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
a.dev {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 12px 14px;
  text-decoration: none; color: inherit; border-left: 5px solid var(--off);
}
a.dev.s-normal { border-left-color: var(--ok); }
a.dev.s-high { border-left-color: var(--high); }
a.dev.s-low { border-left-color: var(--low); }
a.dev.s-offline { border-left-color: var(--off); }
.dev .name { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev .sn { font-size: 12px; color: var(--muted); }
.dev .val { grid-row: 1 / span 2; grid-column: 2; text-align: right; align-self: center; }
.dev .val .t { font-size: 28px; font-weight: 700; line-height: 1.1; }
.dev .val .t small { font-size: 14px; font-weight: 600; margin-left: 2px; }
.dev .val .h { font-size: 13px; color: var(--muted); }
.dev .meta { grid-column: 1 / -1; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.c-normal { color: var(--ok); }
.c-high { color: var(--high); }
.c-low { color: var(--low); }
.c-offline { color: var(--off); }

.badge { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 6px; white-space: nowrap; }
.badge.normal { background: var(--ok-bg); color: var(--ok); }
.badge.high { background: var(--high-bg); color: var(--high); }
.badge.low { background: var(--low-bg); color: var(--low); }
.badge.offline { background: var(--off-bg); color: var(--off); }
.badge.stale { background: var(--warn-bg); color: var(--warn); }

.empty { text-align: center; color: var(--muted); padding: 40px 10px; }

/* ---------- device detail ---------- */
.hero { padding: 18px 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hero .big { font-size: 44px; font-weight: 700; line-height: 1; }
.hero .big small { font-size: 18px; margin-left: 2px; }
.hero .lbl { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.hero .rng { font-size: 13px; color: var(--muted); margin-top: 6px; }
.hero .foot { grid-column: 1 / -1; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 10px; }
.sec { margin-top: 14px; padding: 14px; }
.sec h2 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.seg-row { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.chart-box { position: relative; height: 240px; }
.chart-box canvas { width: 100%; height: 100%; display: block; touch-action: pan-y; }
.chart-tip {
  position: absolute; top: 4px; pointer-events: none; background: var(--text); color: var(--bg);
  font-size: 12px; padding: 4px 8px; border-radius: 6px; white-space: nowrap; transform: translateX(-50%);
}
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.stat .k { font-size: 12px; color: var(--muted); }
.stat .v { font-size: 17px; font-weight: 700; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* ---------- alarms ---------- */
.alarm { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 10px; padding: 11px 14px; border-bottom: 1px solid var(--line); text-decoration: none; color: inherit; }
.alarm:last-child { border-bottom: 0; }
.alarm .ic { grid-row: 1 / span 2; width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; }
.alarm .ic.high { background: var(--high); }
.alarm .ic.low { background: var(--low); }
.alarm .ic.normal { background: var(--ok); }
.alarm .tt { font-weight: 600; font-size: 14px; }
.alarm .wh { font-size: 12px; color: var(--muted); grid-column: 2 / -1; }
.alarm .tm { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.more { display: block; margin: 12px auto 0; }

/* ---------- settings ---------- */
.list { padding: 0; }
.row { display: flex; align-items: center; gap: 12px; padding: 14px; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .t { font-weight: 600; }
.row .d { font-size: 13px; color: var(--muted); }
.lock {
  display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600;
  color: var(--warn); background: var(--warn-bg); padding: 3px 8px; border-radius: 6px; white-space: nowrap;
}
.lock svg { width: 13px; height: 13px; }
.switch { width: 46px; height: 28px; border-radius: 14px; background: var(--line); position: relative; flex: 0 0 auto; border: 0; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch:disabled { opacity: .55; }
.note { background: var(--warn-bg); color: var(--warn); border-radius: 10px; padding: 10px 12px; font-size: 13px; margin: 0 14px 14px; }
.steps { margin: 0; padding-left: 20px; font-size: 14px; }
.steps li { margin-bottom: 4px; }

.banner {
  display: flex; gap: 10px; align-items: center; background: var(--warn-bg); color: var(--warn);
  border-radius: 10px; padding: 9px 12px; font-size: 13px; margin-bottom: 12px;
}
.banner.err { background: var(--high-bg); color: var(--high); }
.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 14px); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-size: 14px; z-index: 20;
  max-width: calc(100% - 32px);
}
.skeleton { height: 76px; border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 0%, var(--surface-2) 50%, var(--surface) 100%); background-size: 200% 100%; animation: sk 1.2s infinite; }
@keyframes sk { to { background-position: -200% 0; } }

@media (min-width: 720px) {
  .hero .big { font-size: 52px; }
  .chart-box { height: 300px; }
}
