/* ══════════════════════════════════════════════════════════════════════════
   Facility console — monochrome visual system.

   Core is black & white: near-black ink ground, paper-white text, graded greys
   with a faint cool bias. Colour is reserved for one role — status
   (--good / --warn / --crit). High-contrast ink (--accent) is the only accent,
   used for interactive elements. Tokens are theme-level: redefined under the
   media query and the data-theme overrides so the viewer's toggle always wins.
   ══════════════════════════════════════════════════════════════════════════ */

:root {
  --bg:#0B0B0D; --surface:#151517; --raised:#1D1D20; --hover:#27272B;
  --line:rgba(255,255,255,.11); --line-2:rgba(255,255,255,.20);
  --text:#F3F4F6; --muted:#9C9CA3; --dim:#66666C;
  --accent:#F3F4F6; --on-accent:#0B0B0D;
  --good:#54B67D; --warn:#E6B23F; --crit:#E5563E;
  --radius:16px; --gap:14px; --tabbar-h:64px;
  --sans:system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;
  --mono:ui-monospace,"SF Mono",SFMono-Regular,Menlo,Consolas,monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg:#F6F6F7; --surface:#FFFFFF; --raised:#F0F0F2; --hover:#E8E8EB;
    --line:rgba(0,0,0,.11); --line-2:rgba(0,0,0,.18);
    --text:#161619; --muted:#6A6A70; --dim:#9A9AA1;
    --accent:#161619; --on-accent:#FFFFFF;
    --good:#1E8B4E; --warn:#A9781A; --crit:#C33F2A;
  }
}

:root[data-theme="dark"] {
  --bg:#0B0B0D; --surface:#151517; --raised:#1D1D20; --hover:#27272B;
  --line:rgba(255,255,255,.11); --line-2:rgba(255,255,255,.20);
  --text:#F3F4F6; --muted:#9C9CA3; --dim:#66666C;
  --accent:#F3F4F6; --on-accent:#0B0B0D;
  --good:#54B67D; --warn:#E6B23F; --crit:#E5563E;
}
:root[data-theme="light"] {
  --bg:#F6F6F7; --surface:#FFFFFF; --raised:#F0F0F2; --hover:#E8E8EB;
  --line:rgba(0,0,0,.11); --line-2:rgba(0,0,0,.18);
  --text:#161619; --muted:#6A6A70; --dim:#9A9AA1;
  --accent:#161619; --on-accent:#FFFFFF;
  --good:#1E8B4E; --warn:#A9781A; --crit:#C33F2A;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}
.muted { color: var(--muted); }
.error { color: var(--crit); font-size: .9rem; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.small { font-size: .82rem; }
h1 { font-size: 1.18rem; margin: 0; letter-spacing: -.01em; font-weight: 750; }
h2 { font-size: 1rem; margin: 0 0 10px; font-weight: 700; letter-spacing: -.005em; }

/* ── Setup screen ─────────────────────────────────────────────────── */
.screen { max-width: 560px; margin: 0 auto; padding: 32px 16px; }
.setup-card h1 { margin-bottom: 4px; font-size: 1.4rem; }

/* ── App shell: header / view / tabbar ────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 13px) 20px 13px;
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  backdrop-filter: saturate(120%) blur(14px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 9px; }
.brand .flag { width: 17px; height: 17px; color: var(--text); flex: none; }

.conn-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--dim); box-shadow: 0 0 0 3px transparent; }
.conn-dot.ok  { background: var(--good); }
.conn-dot.bad { background: var(--crit); box-shadow: 0 0 0 3px color-mix(in srgb, var(--crit) 20%, transparent); }

.health { display: flex; gap: 14px; }
.hdot { display: inline-flex; align-items: center; gap: 6px; font-size: .68rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .09em; }

/* Shell fills the visual viewport (dvh) as a column so the tab bar sits on the
   real device bottom and the content — not the <body> — scrolls between them.
   :not([hidden]) keeps the JS `shell.hidden` toggle working. */
#shell:not([hidden]) {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
}

.view {
  flex: 1 1 auto; min-height: 0;               /* scroll here, not on <body> */
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  width: 100%; max-width: 560px; margin: 0 auto;
  padding: 16px 16px 24px;
}

.tabbar {
  flex: none; z-index: 20;
  display: flex; justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 86%, transparent);
  backdrop-filter: saturate(120%) blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  flex: 1; appearance: none; border: none; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 4px; height: var(--tabbar-h); color: var(--dim);
}
.tabbar .ti { display: flex; }
.tabbar .ti svg { width: 22px; height: 22px; display: block; }
.tabbar .tl { font-size: .64rem; font-weight: 700; letter-spacing: .03em; text-transform: uppercase; }
.tabbar button.active { color: var(--text); }

/* ── Cards & shared ───────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; margin-bottom: var(--gap);
}
.status-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.updated { font-size: .78rem; color: var(--muted); font-family: var(--mono); }
.updated.stale { color: var(--warn); font-weight: 700; }

/* ── Pills: neutral chips; current room-state filled in ink ────────── */
.pill {
  display: inline-block; padding: 5px 12px; border-radius: 999px;
  background: var(--raised); color: var(--muted); border: 1px solid var(--line);
  font-weight: 700; font-size: .7rem; letter-spacing: .08em; text-transform: uppercase;
}
.pill[data-state="occupied"],
.pill[data-state="pre_conditioning"],
.pill[data-state="setback"] { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.pill[data-state="idle"] { background: var(--raised); color: var(--muted); }

/* ── Session card (Now / Next) ────────────────────────────────────── */
.session-body { display: grid; gap: 6px; }
.sess-now { font-size: 1.02rem; display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.sess-now strong { font-weight: 700; }
.sess-badge {
  display: inline-block; font-size: .66rem; font-weight: 800; letter-spacing: .06em;
  text-transform: uppercase; border-radius: 999px; padding: 3px 9px;
}
.sess-badge.on   { background: var(--good); color: #08130d; }
.sess-badge.warn { background: var(--warn); color: #1c1405; }
.sess-badge.bad  { background: var(--crit); color: #fff; }
.sess-next {
  font-size: .9rem; margin-top: 4px; padding-top: 9px;
  border-top: 1px solid var(--line);
}
/* Tappable Session card → drill-in to the session detail screen */
.card.tappable { cursor: pointer; transition: border-color .12s ease, background .12s ease; }
.card.tappable:hover { border-color: var(--line-2); }
.card.tappable:active { background: var(--raised); }
.card.tappable:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sh-right { display: flex; align-items: center; gap: 8px; }
.chev { color: var(--muted); font-size: 1.35rem; line-height: 1; font-weight: 600; }

/* Session detail screen */
.back-btn { margin-bottom: 14px; }
.bk-list { list-style: none; display: flex; flex-direction: column; }
.bk-item { display: flex; align-items: baseline; gap: 12px; padding: 11px 0; border-top: 1px solid var(--line); }
.bk-item:first-child { border-top: none; }
.bk-time { font-weight: 700; min-width: 60px; flex: none; }
.bk-main { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.bk-when { flex: none; white-space: nowrap; }
.bk-empty { padding: 11px 0; }

/* ── Temperatures ─────────────────────────────────────────────────── */
.temps { display: flex; gap: 10px; }
.temp-block {
  flex: 1; background: var(--raised); border: 1px solid var(--line); border-radius: 12px;
  padding: 14px; text-align: center; display: flex; flex-direction: column; justify-content: center;
}
.temp-val {
  font-family: var(--mono); font-variant-numeric: tabular-nums;
  font-size: 2.05rem; font-weight: 700; line-height: 1; letter-spacing: -.02em;
}
.temp-label { font-size: .68rem; color: var(--muted); margin-top: 6px; text-transform: uppercase; letter-spacing: .1em; }
#room-temps { text-align: left; align-items: stretch; gap: 4px; }
.room-row { display: flex; justify-content: space-between; align-items: baseline; font-size: .92rem; padding: 3px 0; }
.room-row .rt { font-family: var(--mono); font-variant-numeric: tabular-nums; font-weight: 700; }

/* ── Buttons ──────────────────────────────────────────────────────── */
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  appearance: none; border: 1px solid var(--line-2); border-radius: 12px; padding: 15px 12px;
  font: 700 1rem var(--sans); color: var(--text); background: var(--raised);
  cursor: pointer; transition: transform .05s ease, background .15s ease, border-color .15s ease;
  min-height: 52px;
}
.btn:active { transform: scale(.975); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; width: 100%; margin-top: 8px; }
.btn.small { padding: 9px 14px; min-height: 0; font-size: .84rem; background: var(--raised); border-color: var(--line-2); }
.btn.ghost { background: transparent; border-color: var(--line-2); color: var(--muted); }
.state-btn.active { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn[disabled] { opacity: .45; cursor: default; }
.btn.small + .btn.small { margin-left: 8px; }

/* A·V power controls: On tinted green, Off tinted red; the button matching the
   device's current state fills in so the state is obvious at a glance. */
.toggle-row .btn[data-cmd$="_on"]  { color: var(--good); border-color: color-mix(in srgb, var(--good) 45%, transparent); }
.toggle-row .btn[data-cmd$="_off"] { color: var(--crit); border-color: color-mix(in srgb, var(--crit) 45%, transparent); }
.toggle-row .btn[data-cmd$="_on"].is-current  { background: var(--good); border-color: var(--good); color: #08130d; }
.toggle-row .btn[data-cmd$="_off"].is-current { background: var(--crit); border-color: var(--crit); color: #fff; }

/* ── A·V: device state + duration picker ──────────────────────────── */
.dev-state { font-family: var(--mono); font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-left: 6px; }
.dev-state[data-s="on"]  { color: var(--good); }
.dev-state[data-s="off"] { color: var(--crit); }
.dur-row { display: flex; gap: 8px; margin-bottom: 12px; }
.dur-row .btn.dur { flex: 1; margin-left: 0; }
.dur.active { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 0; }
.toggle-row + .toggle-row { border-top: 1px solid var(--line); }
.toggle-row > span { font-weight: 650; }

/* ── PINs ─────────────────────────────────────────────────────────── */
.pins { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
/* Separate the "rotated … · expires …" meta from the PIN list above it. */
#pins-meta { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.pins li.pin {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--raised); border: 1px solid var(--line); border-radius: 11px; padding: 12px 15px;
  font: 700 1.28rem/1 var(--mono); letter-spacing: .14em;
}
.pins li.pin .idx { color: var(--dim); font-size: .78rem; font-weight: 600; letter-spacing: normal; }

/* ── Unlocks ──────────────────────────────────────────────────────── */
.unlocks { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.unlocks li.entry { background: var(--raised); border: 1px solid var(--line); border-left: 3px solid var(--good); border-radius: 9px; padding: 11px 13px; font-size: .85rem; }
.unlocks .ts { color: var(--muted); font-size: .74rem; font-family: var(--mono); display: block; margin-bottom: 3px; }

/* ── Errors / KV ──────────────────────────────────────────────────── */
.errors { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.errors li.err { background: var(--raised); border: 1px solid var(--line); border-left: 3px solid var(--crit); border-radius: 9px; padding: 11px 13px; font-size: .85rem; }
.errors li.ok { color: var(--muted); }
.errors .ts { color: var(--muted); font-size: .74rem; font-family: var(--mono); display: block; margin-bottom: 3px; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; font-size: .92rem; }
.kv + .kv { border-top: 1px solid var(--line); }
.kv > span:last-child { font-family: var(--mono); text-align: right; word-break: break-all; }
.ok-text  { color: var(--good); font-weight: 700; }
.bad-text { color: var(--crit); font-weight: 700; }

/* ── Inputs ───────────────────────────────────────────────────────── */
label { display: block; margin: 16px 0 0; font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
input {
  width: 100%; margin-top: 8px; padding: 13px; border-radius: 11px;
  border: 1px solid var(--line-2); background: var(--raised); color: var(--text);
  font: 1rem var(--sans);
}
input:focus { outline: none; border-color: var(--accent); }

/* ── Toast ────────────────────────────────────────────────────────── */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translateX(-50%);
  background: var(--accent); color: var(--on-accent);
  padding: 12px 18px; border-radius: 12px; font-size: .9rem; font-weight: 600;
  box-shadow: 0 8px 28px -8px rgba(0,0,0,.5); z-index: 50; max-width: 90%; text-align: center;
}
.toast.err { background: var(--crit); color: #fff; }
.toast.ok  { background: var(--good); color: #08130d; }

/* ── Status: system banner ────────────────────────────────────────── */
.banner {
  display: flex; align-items: center; gap: 13px; padding: 15px 16px; margin-bottom: var(--gap);
  border: 1px solid var(--line); border-radius: 14px; background: var(--surface);
}
.banner .bigdot { width: 13px; height: 13px; border-radius: 50%; flex: none; background: var(--muted); }
.banner b { font-size: 1.05rem; letter-spacing: -.01em; }
.banner .bsub { color: var(--muted); font-size: .82rem; margin-top: 2px; }
.banner.good { background: linear-gradient(180deg, color-mix(in srgb, var(--good) 12%, var(--surface)), var(--surface)); border-color: color-mix(in srgb, var(--good) 30%, transparent); }
.banner.good .bigdot { background: var(--good); box-shadow: 0 0 0 5px color-mix(in srgb, var(--good) 18%, transparent); }
.banner.warn { background: linear-gradient(180deg, color-mix(in srgb, var(--warn) 13%, var(--surface)), var(--surface)); border-color: color-mix(in srgb, var(--warn) 32%, transparent); }
.banner.warn .bigdot { background: var(--warn); box-shadow: 0 0 0 5px color-mix(in srgb, var(--warn) 18%, transparent); }
.banner.crit { background: linear-gradient(180deg, color-mix(in srgb, var(--crit) 14%, var(--surface)), var(--surface)); border-color: color-mix(in srgb, var(--crit) 34%, transparent); }
.banner.crit .bigdot { background: var(--crit); box-shadow: 0 0 0 5px color-mix(in srgb, var(--crit) 18%, transparent); }

/* ── Status: tile grid ────────────────────────────────────────────── */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile {
  position: relative; overflow: hidden; text-align: left; appearance: none; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px 15px;
  color: inherit; font: inherit; transition: transform .05s ease;
}
.tile::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--good); }
.tile.warn::before { background: var(--warn); }
.tile.crit::before { background: var(--crit); }
.tile.idle::before { background: var(--dim); }
.tile:active { transform: scale(.98); }
.tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tile .tl { font-size: .64rem; font-weight: 700; letter-spacing: .09em; text-transform: uppercase; color: var(--muted); }
.tile .tv { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 1.35rem; font-weight: 700; margin-top: 6px; letter-spacing: -.01em; }
.tile .tv small { font-size: .7rem; color: var(--muted); font-weight: 500; }
.tile .tsx { font-size: .74rem; margin-top: 3px; }
.tsx.good { color: var(--good); } .tsx.warn { color: var(--warn); } .tsx.crit { color: var(--crit); } .tsx.dim { color: var(--dim); }

/* ── Climate: unit cards + sparkline ──────────────────────────────── */
.unit { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 15px 16px; margin-bottom: 10px; }
.unit .uhead { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.unit .uname { font-weight: 700; text-transform: capitalize; }
.umode { font-family: var(--mono); font-size: .66rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; padding: 3px 9px; border-radius: 999px; background: var(--raised); color: var(--muted); border: 1px solid var(--line); }
.umode.cool { color: #6fa8dc; }
.umode.off  { color: var(--dim); }
.unit .reading { display: flex; align-items: baseline; gap: 9px; margin-top: 9px; }
.unit .big { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 1.95rem; font-weight: 700; line-height: 1; letter-spacing: -.02em; }
.unit .to { font-family: var(--mono); font-size: .82rem; color: var(--muted); }
.conv { margin-left: auto; font-size: .64rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; padding: 3px 9px; border-radius: 999px; }
.conv.good { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.conv.warn { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }
.conv.crit { background: color-mix(in srgb, var(--crit) 16%, transparent); color: var(--crit); }
.spark { margin-top: 12px; }
.spark svg { display: block; width: 100%; height: 46px; }
.spark .axl { display: flex; justify-content: space-between; font-size: .62rem; color: var(--dim); font-family: var(--mono); margin-top: 4px; }
.live-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 0 2px 10px; }
#live-status.live-ok { color: var(--good); font-weight: 600; }
#live-status.stale { color: var(--warn); font-weight: 600; }
.outdoor { display: flex; align-items: center; gap: 8px; font-size: .86rem; color: var(--muted); padding: 6px 2px 2px; }
.outdoor svg { color: var(--muted); flex: none; }
.outdoor b { font-family: var(--mono); color: var(--text); font-size: 1rem; }

/* ── Radar: competitor busyness ───────────────────────────────────── */
.radar-now { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.radar-now-val { font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 2.4rem; font-weight: 700; line-height: 1; letter-spacing: -.02em; }
.radar-now-lbl { color: var(--muted); font-size: .82rem; }
.radar-row { display: flex; align-items: center; gap: 12px; padding: 6px 0; }
.radar-row + .radar-row { border-top: 1px solid var(--line); }
.radar-row.now { background: color-mix(in srgb, var(--accent) 7%, transparent); border-radius: 8px; margin: 0 -6px; padding: 6px; }
.radar-row .rlabel { width: 32px; font-family: var(--mono); font-size: .8rem; color: var(--muted); }
.radar-row .pips { display: flex; gap: 5px; flex: 1; }
.radar-row .rcount { font-family: var(--mono); font-size: .78rem; color: var(--muted); width: 30px; text-align: right; }
.pip { width: 22px; height: 12px; border-radius: 3px; border: 1px solid var(--line-2); background: transparent; }
.pip.on { background: var(--accent); border-color: transparent; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
