:root {
  --bg: #f4f5f7; --panel: #ffffff; --panel2: #eceef1; --text: #16191d; --muted: #5c6570;
  --line: #d9dde2; --accent: #1f6feb; --accent-text: #fff; --on: #1f883d; --on-bg: #dafbe1;
  --warn: #9a6700; --warn-bg: #fff8c5; --crit: #cf222e; --crit-bg: #ffebe9; --info-bg: #ddf4ff;
  --off-bg: #e6e8eb; --strip: #16191d; --strip-text: #f4f5f7; --shadow: 0 1px 2px rgba(0,0,0,.08);
  --strip-w: 46px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --panel: #181b20; --panel2: #20242b; --text: #e8eaed; --muted: #9aa3ad;
    --line: #2c3139; --accent: #4493f8; --on: #3fb950; --on-bg: #12361d; --warn: #d29922; --warn-bg: #3a2e0c;
    --crit: #f85149; --crit-bg: #42161a; --info-bg: #0c2d4a; --off-bg: #2a2f37; --strip: #000; --strip-text: #e8eaed;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --panel: #181b20; --panel2: #20242b; --text: #e8eaed; --muted: #9aa3ad;
  --line: #2c3139; --accent: #4493f8; --on: #3fb950; --on-bg: #12361d; --warn: #d29922; --warn-bg: #3a2e0c;
  --crit: #f85149; --crit-bg: #42161a; --info-bg: #0c2d4a; --off-bg: #2a2f37; --strip: #000; --strip-text: #e8eaed;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
a { color: var(--accent); }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- forms (login / add) ---------- */
.page-form { display: grid; place-items: center; padding: 16px; min-height: 100%; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 24px; width: 100%; max-width: 380px; box-shadow: var(--shadow); }
.card h1 { margin: 0 0 16px; font-size: 1.4rem; display: flex; gap: 10px; align-items: center; }
.card label { display: block; margin: 12px 0; font-weight: 600; font-size: .9rem; }
.card input { display: block; width: 100%; margin-top: 4px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.card input.code { font: 600 1.6rem/1.2 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: .2em; text-transform: uppercase; text-align: center; }
.steps { padding-left: 1.2em; color: var(--muted); font-size: .92rem; }
.primary { width: 100%; padding: 12px; margin-top: 8px; border: 0; border-radius: 8px; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.err { background: var(--crit-bg); color: var(--crit); padding: 8px 12px; border-radius: 8px; }

/* ---------- app shell ---------- */
.page-app { overflow: hidden; }
#strip {
  position: fixed; inset: 0 auto 0 0; width: calc(var(--strip-w) + env(safe-area-inset-left));
  padding-left: env(safe-area-inset-left);
  background: var(--strip); color: var(--strip-text);
  display: flex; flex-direction: column; align-items: stretch; z-index: 2; touch-action: none; user-select: none;
}
.strip-arrow { position: relative; height: 56px; background: none; border: 0; color: inherit; font-size: 18px; flex: none; }
.strip-arrow:disabled { opacity: .25; }
.strip-arrow .dot { position: absolute; top: 12px; right: 10px; width: 10px; height: 10px; border-radius: 50%; background: var(--crit); box-shadow: 0 0 0 2px var(--strip); }
.spine { flex: 1; background: none; border: 0; color: inherit; display: flex; align-items: center; justify-content: center; padding: 8px 0; overflow: hidden; }
.spine span { writing-mode: vertical-rl; font-weight: 700; font-size: 1.15rem; letter-spacing: .12em; text-transform: uppercase; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-height: 100%; }

#main {
  position: fixed; inset: 0 0 0 calc(var(--strip-w) + env(safe-area-inset-left));
  display: flex; flex-direction: column; overflow: hidden;
}
#bar { display: flex; align-items: center; gap: 4px; padding: 8px 8px 4px; padding-top: max(8px, env(safe-area-inset-top)); background: var(--bg); border-bottom: 1px solid var(--line); flex: none; }
.nav-arrow { width: 44px; height: 44px; border: 0; background: none; font-size: 18px; color: var(--muted); border-radius: 8px; flex: none; }
.nav-arrow:disabled { opacity: .25; }
.bar-mid { flex: 1; min-width: 0; text-align: center; }
#screen-title { margin: 0; font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#dots { display: flex; justify-content: center; gap: 6px; height: 14px; align-items: center; }
#dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--line); }
#dots i.cur { background: var(--accent); }
.conn { background: var(--warn-bg); color: var(--warn); text-align: center; padding: 4px; font-size: .85rem; flex: none; }
.banner { background: var(--crit-bg); color: var(--crit); padding: 10px 16px; font-weight: 600; flex: none; }
.banner.offline { background: var(--off-bg); color: var(--muted); }
#content { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 10px 12px calc(24px + env(safe-area-inset-bottom)); touch-action: pan-y; }
#content.offline .row button, #content.offline .row input { opacity: .45; }

h2.group { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 18px 0 6px; }
h2.group:first-child { margin-top: 4px; }
.empty { color: var(--muted); padding: 16px 0; }

/* ---------- status / alerts ---------- */
.status-ok { display: flex; align-items: center; gap: 10px; background: var(--on-bg); color: var(--on); border-radius: 10px; padding: 14px 16px; font-weight: 700; font-size: 1.15rem; }
.alert { border-radius: 10px; padding: 10px 14px; margin: 6px 0; border-left: 4px solid; }
.alert.crit { background: var(--crit-bg); border-color: var(--crit); }
.alert.warn { background: var(--warn-bg); border-color: var(--warn); }
.alert.info { background: var(--info-bg); border-color: var(--accent); }
.alert small { display: block; color: var(--muted); font-size: .8rem; }
.hubinfo { color: var(--muted); font-size: .8rem; margin-top: 20px; line-height: 1.6; }
.hubinfo button { font-size: .8rem; padding: 4px 10px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--crit); }

/* ---------- device rows: [OFF] middle [ON], all rows share the grid ---------- */
.row {
  display: grid; grid-template-columns: 56px minmax(0, 1fr) 56px; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px; margin: 6px 0; box-shadow: var(--shadow);
}
.row .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row.switch .mid, .row.sensor .mid { text-align: center; }
.row .on, .row .off { height: 44px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel2); font-weight: 700; font-size: .85rem; color: var(--muted); }
.row .on.active { background: var(--on); border-color: var(--on); color: #fff; }
.row .off.active { background: var(--off-bg); border-color: var(--muted); color: var(--text); }
.row .status { grid-column: 1 / -1; font-size: .82rem; min-height: 0; }
.row .status:empty { display: none; }
.row .status.pending { color: var(--muted); }
.row .status.failed { color: var(--crit); }
.row .status.done { color: var(--on); }
.row.pending { border-color: var(--accent); }
.row.unreachable .name::after { content: " · not responding"; color: var(--crit); font-weight: 400; font-size: .8rem; }
.nameline { display: flex; align-items: baseline; gap: 6px; padding: 0 4px; }
.nameline .name { flex: 1; min-width: 0; }
.pct { font-variant-numeric: tabular-nums; color: var(--muted); font-size: .85rem; flex: none; }
.slide { display: flex; align-items: center; gap: 2px; }
.slide button { width: 26px; height: 32px; padding: 0; border: 0; border-radius: 6px; background: none; color: var(--muted); flex: none; font-size: 13px; }
.slide input { flex: 1; min-width: 60px; margin: 0; accent-color: var(--accent); height: 28px; }
.row.dimmer.is-off .slide input { filter: grayscale(1); opacity: .45; }
.row.dimmer.is-off .pct { opacity: .6; }
.row.sensor .icon { display: flex; justify-content: center; color: var(--muted); }
.row.sensor .icon svg { width: 22px; height: 22px; }
.row.sensor .value { text-align: center; font-weight: 700; font-size: .85rem; }
.row.sensor .value.bad { color: var(--crit); }
.row.sensor .value.good { color: var(--on); }
.scene { display: inline-block; margin: 4px 6px 4px 0; padding: 10px 16px; border-radius: 20px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; }

/* ---------- master monitor ---------- */
.mon { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 0 0 14px; box-shadow: var(--shadow); }
.mon-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.mon-head h3 { margin: 0; font-size: 1.1rem; flex: 1; min-width: 0; }
.mon-head button { background: none; border: 0; color: var(--accent); font: inherit; font-weight: 700; padding: 0; text-align: left; }
.pill { font-size: .75rem; font-weight: 700; padding: 2px 8px; border-radius: 10px; white-space: nowrap; }
.pill.online { background: var(--on-bg); color: var(--on); }
.pill.offline { background: var(--off-bg); color: var(--muted); }
.mon .alert { margin: 4px 0; padding: 6px 10px; font-size: .9rem; }
.mon-devs { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px; margin-top: 8px; }
.chip { display: flex; justify-content: space-between; gap: 6px; background: var(--panel2); border-radius: 8px; padding: 6px 8px; font-size: .85rem; min-width: 0; }
.chip span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip b { white-space: nowrap; }
.chip.on b { color: var(--on); }
.chip.bad b { color: var(--crit); }
.chip.gone { opacity: .5; }
.mon.offline .mon-devs { opacity: .55; }

/* ---------- picker ---------- */
dialog#picker { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); padding: 16px; width: min(92vw, 380px); }
dialog#picker::backdrop { background: rgba(0,0,0,.45); }
#picker h2 { margin: 0 0 8px; font-size: 1.1rem; }
#picker ul { list-style: none; margin: 0; padding: 0; }
#picker li button { width: 100%; display: flex; align-items: center; gap: 10px; padding: 12px; border: 0; border-radius: 8px; background: none; text-align: left; font-weight: 600; }
#picker li button.cur { background: var(--panel2); }
#picker li .sdot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
#picker li .sdot.online { background: var(--on); }
#picker li .alertdot { margin-left: auto; width: 10px; height: 10px; border-radius: 50%; background: var(--crit); }
.picker-foot { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; border-top: 1px solid var(--line); padding-top: 12px; }
.picker-foot form { margin: 0; }
.btn { display: inline-block; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel2); text-decoration: none; color: var(--text); font-size: .9rem; }

@media (min-width: 720px) {
  #content { padding-left: 24px; padding-right: 24px; }
  #content > * { max-width: 760px; margin-left: auto; margin-right: auto; }
}
.scene:disabled { opacity: .45; }
.note { background: var(--on-bg); color: var(--on); padding: 8px 12px; border-radius: 8px; }
.aux { margin: 16px 0 0; text-align: center; font-size: .92rem; }
