:root {
  --bg: #0b0f17;
  --bg-2: #121826;
  --card: #171e2e;
  --card-hi: #1e273b;
  --line: #26304a;
  --text: #e8edf7;
  --muted: #8592ad;
  --accent: #ffb454;
  --accent-2: #6ea8fe;
  --on: #ffcf7a;
  --good: #4ade80;
  --bad: #f87171;
  --radius: 18px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html, body { overflow-x: clip; max-width: 100%; }
body {
  background: radial-gradient(1200px 700px at 80% -10%, #1a2440 0%, var(--bg) 55%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 14px;
  padding: 18px 22px;
  background: linear-gradient(180deg, rgba(11,15,23,0.92), rgba(11,15,23,0.55));
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 20px; margin: 0; font-weight: 650; letter-spacing: 0.2px; flex: 0 0 auto; }
.topbar .spacer { flex: 1; }
.conn {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 12.5px; color: var(--muted); min-width: 0; overflow: hidden;
  padding: 6px 11px; border: 1px solid var(--line); border-radius: 999px;
}
#conn-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* On phones keep just the status dot — the pill text would crowd the bar. */
@media (max-width: 480px) {
  #conn-text { display: none; }
  .conn { padding: 8px; }
  .topbar { padding: 16px; gap: 10px; }
}
.dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--bad); box-shadow: 0 0 0 0 rgba(74,222,128,.6); }
.dot.ok { background: var(--good); animation: pulse 2.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(74,222,128,.5);} 70%{box-shadow:0 0 0 7px rgba(74,222,128,0);} 100%{box-shadow:0 0 0 0 rgba(74,222,128,0);} }
.icon-btn {
  background: var(--card); border: 1px solid var(--line); color: var(--muted);
  border-radius: 12px; padding: 8px 12px; font-size: 13px;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent-2); }

/* ---------- layout ---------- */
main { max-width: 1180px; margin: 0 auto; padding: 8px 18px 60px; }
.room { margin-top: 30px; }
.room-head { display: flex; align-items: center; gap: 10px; margin: 0 4px 14px; }
.room-head .emoji { font-size: 20px; }
.room-head h2 { font-size: 15px; font-weight: 600; color: var(--muted); margin: 0; text-transform: uppercase; letter-spacing: 1.4px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }

/* ---------- card ---------- */
.card {
  background: linear-gradient(180deg, var(--card-hi), var(--card));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  transition: border-color .2s, transform .12s;
}
.card:hover { border-color: #33405f; }
.card-top { display: flex; align-items: center; gap: 12px; }
.card-name { font-weight: 600; font-size: 15px; line-height: 1.2; }
.card-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.card .spacer { flex: 1; }

/* power orb toggle */
.orb {
  width: 46px; height: 46px; border-radius: 50%; flex: none;
  border: 1px solid var(--line); background: #10162299;
  display: grid; place-items: center; color: var(--muted);
  transition: all .2s;
}
.orb svg { width: 22px; height: 22px; }
.card.on .orb { color: #201400; background: radial-gradient(circle at 30% 30%, #ffd98a, var(--on)); border-color: transparent; box-shadow: 0 0 22px rgba(255,180,84,.45); }
.card.on .card-name { color: #fff; }

/* sliders */
.controls { margin-top: 14px; display: flex; flex-direction: column; gap: 12px; }
.slider-row { display: flex; align-items: center; gap: 10px; }
.slider-row .lbl { font-size: 11px; color: var(--muted); width: 16px; text-align: center; }
input[type=range] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 8px;
  border-radius: 999px; background: var(--bg-2); outline: none; border: 1px solid var(--line);
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; border: 3px solid var(--accent); cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,.4);
}
input[type=range].temp { background: linear-gradient(90deg, #ffb454, #fff, #6ea8fe); }
input[type=range].bri { background: linear-gradient(90deg, #3a2a10, var(--accent)); }

/* swatches / chips */
.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  border: 1px solid var(--line); background: var(--bg-2); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; font-size: 12.5px;
}
.chip.active { color: #201400; background: var(--accent); border-color: transparent; font-weight: 600; }
.chip:hover { color: var(--text); }

/* ---------- climate (AC) ---------- */
.temp-row { display: flex; align-items: center; justify-content: center; gap: 16px; }
.tbtn {
  width: 42px; height: 42px; border-radius: 12px; flex: none;
  border: 1px solid var(--line); background: var(--bg-2); color: var(--text);
  font-size: 24px; line-height: 1; cursor: pointer; display: grid; place-items: center;
}
.tbtn:hover { border-color: var(--accent-2); color: #fff; }
.tbtn:active { transform: scale(0.94); }
.temp-val { font-size: 28px; font-weight: 700; min-width: 72px; text-align: center; }
.card.climate.on .temp-val { color: var(--on); }
.chips.extras { margin-top: 2px; }
.chip.sw-on { color: #06210f; background: var(--good); border-color: transparent; font-weight: 600; }
.swatches { display: flex; gap: 8px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #ffffff22; cursor: pointer; }
.swatch:hover { transform: scale(1.12); }
.color-pick { width: 34px; height: 26px; padding: 0; border: none; background: none; cursor: pointer; }

/* scene / button cards */
.card.action { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.card.action:hover { border-color: var(--accent); transform: translateY(-1px); }
.card.action .emoji { font-size: 22px; }

/* sensor metric */
.card.metric { padding: 14px 16px; }
.metric .val { font-size: 26px; font-weight: 680; margin-top: 6px; }
.metric .val small { font-size: 14px; color: var(--muted); font-weight: 500; margin-left: 3px; }
.metric .m-name { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login {
  width: 100%; max-width: 360px; background: linear-gradient(180deg, var(--card-hi), var(--card));
  border: 1px solid var(--line); border-radius: 22px; padding: 30px; box-shadow: var(--shadow);
}
.login h1 { margin: 0 0 4px; font-size: 24px; }
.login p { color: var(--muted); margin: 0 0 22px; font-size: 14px; }
.login input {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg-2); color: var(--text); font-size: 15px; margin-bottom: 12px;
}
.login input:focus { outline: none; border-color: var(--accent-2); }
.btn-primary {
  width: 100%; padding: 13px; border-radius: 12px; border: none; font-size: 15px; font-weight: 600;
  background: linear-gradient(90deg, var(--accent), #ff9a3c); color: #201400;
}
.btn-primary:hover { filter: brightness(1.05); }
.login .err { color: var(--bad); font-size: 13px; min-height: 18px; margin-bottom: 8px; }

/* ---------- clickable metric ---------- */
.card.metric { cursor: pointer; position: relative; }
.card.metric:hover { border-color: var(--accent-2); transform: translateY(-1px); }
.card.metric .chart-hint { position: absolute; top: 12px; right: 14px; color: var(--muted); opacity: .5; font-size: 13px; }
.card.metric:hover .chart-hint { opacity: 1; color: var(--accent-2); }

/* ---------- chart modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(4, 7, 13, 0.72); backdrop-filter: blur(6px); padding: 18px;
}
.modal {
  width: 100%; max-width: 680px; background: linear-gradient(180deg, var(--card-hi), var(--card));
  border: 1px solid var(--line); border-radius: 22px; padding: 22px; box-shadow: var(--shadow);
  animation: pop .16s ease-out;
}
@keyframes pop { from { transform: scale(.97); opacity: .4; } to { transform: scale(1); opacity: 1; } }
.modal-head { display: flex; align-items: flex-start; gap: 12px; }
.modal-title { font-size: 18px; font-weight: 650; }
.modal-cur { font-size: 13px; color: var(--muted); margin-top: 3px; }
.modal .spacer { flex: 1; }
.modal-close {
  margin-left: auto; background: var(--bg-2); border: 1px solid var(--line); color: var(--muted);
  width: 34px; height: 34px; border-radius: 10px; font-size: 15px; flex: none;
}
.modal-close:hover { color: var(--text); border-color: var(--accent-2); }
.ranges { display: flex; gap: 8px; margin: 16px 0 12px; }
.ranges .chip.active { color: #201400; background: var(--accent); border-color: transparent; font-weight: 600; }
.chart-wrap { min-height: 250px; display: grid; place-items: center; }
.chart-wrap svg { width: 100%; height: auto; display: block; }
.chart-loading, .chart-empty { color: var(--muted); font-size: 14px; padding: 40px 0; }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart-grid { stroke: var(--line); stroke-width: 1; }
.chart-axis { fill: var(--muted); font-size: 11px; }
.chart-cursor-line { stroke: var(--accent-2); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-cursor-dot { fill: #fff; stroke: var(--accent); stroke-width: 3; }
.chart-tip { fill: var(--text); font-size: 12.5px; font-weight: 600; }
.chart-tip-t { fill: var(--muted); font-size: 11px; }
.stats { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.stat { flex: 1; min-width: 84px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.stat .k { font-size: 11px; color: var(--muted); }
.stat .v { font-size: 18px; font-weight: 650; margin-top: 2px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--bad); color: #fff; padding: 10px 18px; border-radius: 12px;
  font-size: 13.5px; box-shadow: var(--shadow); opacity: 0; transition: opacity .25s; pointer-events: none;
}
.toast.show { opacity: 1; }
