:root {
  --bg:        #0d1117;
  --surface:   #161b22;
  --surface-2: #1c2330;
  --border:    #262d38;
  --text:      #e6edf3;
  --muted:     #7d8590;
  --accent:    #f0883e;
  --on:        #ffc663;
  --cool:      #58a6ff;
  --good:      #3fb950;
  --bad:       #f85149;
  --radius:    14px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ── Top bar ─────────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 1.05rem; letter-spacing: -0.01em; }
.brand-icon { width: 21px; height: 21px; fill: var(--accent); }
.topbar-right { display: flex; align-items: center; gap: 12px; }

.conn { display: flex; align-items: center; gap: 6px; font-size: 0.74rem; color: var(--muted); }
.conn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); flex: none; }
.conn.ok  .dot { background: var(--good); }
.conn.err .dot { background: var(--bad); }

.icon-btn {
  background: none; border: none; cursor: pointer; padding: 6px;
  display: grid; place-items: center; border-radius: 8px; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.spin svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Tabs ────────────────────────────────────────────── */
.tabs {
  display: flex; gap: 2px; padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 51px; z-index: 49;
  overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 0.85rem; font-weight: 550;
  padding: 11px 15px; border-bottom: 2px solid transparent; white-space: nowrap;
  font-family: inherit;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Layout ──────────────────────────────────────────── */
main { padding: 18px; max-width: 1400px; margin: 0 auto; }

.loading { display: grid; place-items: center; gap: 14px; padding: 90px 20px; color: var(--muted); }
.spinner {
  width: 26px; height: 26px; border: 2.5px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 0.7s linear infinite;
}

.room { margin-bottom: 26px; }
.room-head {
  display: flex; align-items: baseline; gap: 10px; margin-bottom: 11px; padding: 0 2px;
}
.room-head h2 { font-size: 0.95rem; font-weight: 650; letter-spacing: -0.01em; }
.room-head .count { font-size: 0.74rem; color: var(--muted); }
.room-head .room-actions { margin-left: auto; display: flex; gap: 6px; }
.mini-btn {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
  font-size: 0.7rem; padding: 4px 10px; border-radius: 7px; cursor: pointer; font-family: inherit;
}
.mini-btn:hover { color: var(--text); border-color: var(--muted); }

.grid {
  display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
}

/* ── Device tile ─────────────────────────────────────── */
.tile {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 13px;
  display: flex; flex-direction: column; gap: 9px;
  min-height: 104px; position: relative; transition: border-color 0.15s, background 0.15s;
}
.tile.active { background: var(--surface-2); border-color: #3d3222; }
.tile.stale { opacity: 0.45; }

.tile-top { display: flex; align-items: flex-start; gap: 9px; }
.tile-icon {
  width: 30px; height: 30px; border-radius: 9px; flex: none;
  display: grid; place-items: center; background: var(--surface-2); color: var(--muted);
}
.tile.active .tile-icon { background: rgba(255,198,99,0.14); color: var(--on); }
.tile-icon svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.tile-name {
  font-size: 0.79rem; font-weight: 550; line-height: 1.28;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.tile-sub { font-size: 0.68rem; color: var(--muted); margin-top: 2px; }

.tile-foot { margin-top: auto; display: flex; align-items: center; gap: 8px; }

/* toggle */
.toggle {
  width: 40px; height: 23px; border-radius: 12px; background: #2b3240;
  border: none; cursor: pointer; position: relative; flex: none; transition: background 0.18s;
}
.toggle::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 17px; height: 17px; border-radius: 50%; background: #8b949e; transition: 0.18s;
}
.toggle.on { background: rgba(255,198,99,0.3); }
.toggle.on::after { left: 20px; background: var(--on); }

/* slider */
.slider { -webkit-appearance: none; appearance: none; width: 100%; height: 4px; border-radius: 3px; background: #2b3240; outline: none; cursor: pointer; }
.slider::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--on); cursor: pointer; }
.slider::-moz-range-thumb { width: 14px; height: 14px; border: none; border-radius: 50%; background: var(--on); cursor: pointer; }

/* readouts */
.readout { font-size: 1.25rem; font-weight: 640; letter-spacing: -0.02em; }
.readout .unit { font-size: 0.72rem; font-weight: 500; color: var(--muted); margin-left: 1px; }

.badge {
  font-size: 0.66rem; font-weight: 600; padding: 3px 8px; border-radius: 20px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.good { color: var(--good); border-color: rgba(63,185,80,0.3); background: rgba(63,185,80,0.1); }
.badge.warn { color: var(--accent); border-color: rgba(240,136,62,0.3); background: rgba(240,136,62,0.1); }
.badge.bad  { color: var(--bad);  border-color: rgba(248,81,73,0.3);  background: rgba(248,81,73,0.1); }

.batt { font-size: 0.66rem; color: var(--muted); display: flex; align-items: center; gap: 3px; margin-left: auto; }
.batt.low { color: var(--bad); }

/* stepper for thermostats */
.stepper { display: flex; align-items: center; gap: 6px; width: 100%; }
.stepper button {
  width: 27px; height: 27px; border-radius: 8px; flex: none;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  font-size: 1rem; line-height: 1; cursor: pointer; font-family: inherit;
}
.stepper button:hover { border-color: var(--muted); }
.stepper .target { flex: 1; text-align: center; font-size: 0.95rem; font-weight: 640; color: var(--on); }

/* ── Scenes ──────────────────────────────────────────── */
.scene-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 24px; }
.scene-btn {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: 9px 15px; border-radius: 10px; cursor: pointer; font-size: 0.79rem;
  font-weight: 550; font-family: inherit;
}
.scene-btn:hover { border-color: var(--accent); color: var(--accent); }
.scene-btn:active { transform: scale(0.97); }

/* ── Panels ──────────────────────────────────────────── */
.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px; margin-bottom: 14px;
}
.panel h3 { font-size: 0.9rem; margin-bottom: 6px; }
.panel p  { font-size: 0.8rem; color: var(--muted); line-height: 1.6; }
.panel code {
  background: var(--bg); padding: 2px 6px; border-radius: 5px;
  font-size: 0.76rem; border: 1px solid var(--border);
}
.panel .row { display: flex; justify-content: space-between; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 0.8rem; }
.panel .row:last-child { border-bottom: none; }
.panel .row span:last-child { color: var(--muted); }

.field-row { display: flex; gap: 8px; margin-top: 14px; }
.field-row input {
  flex: 1; min-width: 0; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); padding: 10px 12px; border-radius: 9px;
  font-size: 0.85rem; font-family: inherit;
}
.field-row input:focus { outline: none; border-color: var(--accent); }
.field-row input:disabled { opacity: 0.5; }
.mini-btn.primary { background: var(--accent); color: #1a1205; border-color: var(--accent); font-weight: 650; padding: 0 16px; }
.mini-btn.primary:hover { opacity: 0.9; color: #1a1205; }
.mini-btn:disabled { opacity: 0.5; cursor: default; }

.panel .fine { font-size: 0.72rem; color: var(--muted); line-height: 1.55; margin-top: 10px; }
.panel .fine strong { color: var(--text); font-weight: 600; }

.notice {
  background: rgba(240,136,62,0.09); border: 1px solid rgba(240,136,62,0.3);
  border-radius: 10px; padding: 13px 15px; margin-bottom: 16px;
  font-size: 0.79rem; line-height: 1.55; color: #f5c396;
}
.notice strong { color: var(--accent); }

.empty { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty h3 { font-size: 1rem; margin-bottom: 7px; color: var(--text); }
.empty p { font-size: 0.82rem; line-height: 1.6; }

/* ── Toast ───────────────────────────────────────────── */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 14px);
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 11px 19px; border-radius: 10px; font-size: 0.81rem;
  opacity: 0; pointer-events: none; transition: 0.22s; z-index: 200; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { border-color: var(--bad); color: #ffa198; }
.toast.ok  { border-color: var(--good); color: #7ee787; }

@media (max-width: 500px) {
  main { padding: 13px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(146px, 1fr)); gap: 8px; }
}
