:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --card-2: #f0f2f5;
  --text: #15171c;
  --muted: #6b7280;
  --line: #e3e6eb;
  --accent: #2f6bff;
  --accent-soft: #e6eeff;
  --on: #1f9d55;
  --cool: #2f86ff;
  --heat: #f2711c;
  --warn: #b7791f;
  --warn-soft: #fff6e0;
  --danger: #d93f3f;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 14px rgb(16 24 40 / 5%);
  --radius: 18px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #181b21;
    --card-2: #20242c;
    --text: #eef0f4;
    --muted: #9aa3b2;
    --line: #2a2f38;
    --accent: #5b8cff;
    --accent-soft: #1f2a44;
    --on: #3ccf7f;
    --cool: #5aa2ff;
    --heat: #ff8a3d;
    --warn: #f0b455;
    --warn-soft: #3a2f17;
    --danger: #ff6b6b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; }

#app { max-width: 1240px; margin: 0 auto; padding: max(20px, env(safe-area-inset-top)) 18px 60px; }

.top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 18px; }
.top h1 { font-size: 28px; letter-spacing: -0.02em; }
.top p { margin: 4px 0 0; }

.pill {
  display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line);
  background: var(--card); border-radius: 999px; padding: 8px 14px; white-space: nowrap; box-shadow: var(--shadow);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--on); }
.dot.warn { background: var(--warn); }
.dot.err { background: var(--danger); }

.alerts { display: grid; gap: 8px; margin-bottom: 14px; }
.alert {
  display: flex; gap: 10px; align-items: center; background: var(--warn-soft); color: var(--text);
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); border-radius: 14px; padding: 12px 14px;
}

.scenes-row { margin: 0 -18px 22px; overflow-x: auto; scrollbar-width: none; }
.scenes { display: flex; gap: 10px; padding: 2px 18px; width: max-content; }
.scene {
  display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--card);
  border-radius: 14px; padding: 10px 14px; box-shadow: var(--shadow); position: relative;
}
.scene .emoji { font-size: 18px; }
.scene.running { opacity: 0.6; }
.scene .edit { border: 0; background: none; color: var(--muted); padding: 0 0 0 4px; font-size: 13px; }
.scene.add { border-style: dashed; color: var(--muted); box-shadow: none; background: transparent; }

.section { margin-bottom: 28px; }
.section > h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 4px 10px; font-weight: 600; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.grid.cams { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; min-width: 0;
}
.card.offline { opacity: 0.72; }
.card-head { display: flex; align-items: center; gap: 12px; }
.card-head .title { flex: 1; min-width: 0; }
.card-head h3 { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-head .sub { font-size: 12.5px; color: var(--muted); }
.badge { font-size: 11.5px; padding: 3px 8px; border-radius: 999px; background: var(--card-2); color: var(--muted); white-space: nowrap; }
.badge.open { background: var(--warn-soft); color: var(--warn); font-weight: 600; }
.badge.on { background: color-mix(in srgb, var(--on) 15%, transparent); color: var(--on); }
.note { font-size: 12.5px; color: var(--muted); background: var(--card-2); border-radius: 10px; padding: 8px 10px; margin: 0; }

.power {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line); background: var(--card-2);
  display: grid; place-items: center; flex: none; color: var(--muted);
}
.power svg { width: 18px; height: 18px; }
.power.on { background: var(--accent); border-color: var(--accent); color: #fff; }

.switch { position: relative; width: 48px; height: 28px; flex: none; border-radius: 999px; border: 0; background: var(--line); transition: background 0.15s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / 25%); transition: transform 0.15s; }
.switch.on { background: var(--on); }
.switch.on::after { transform: translateX(20px); }

.thermo { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.thermo .value { text-align: center; flex: 1; }
.thermo .big { font-size: 46px; font-weight: 300; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; line-height: 1; }
.thermo .big small { font-size: 20px; color: var(--muted); }
.thermo .label { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.step-btn {
  width: 48px; height: 48px; border-radius: 50%; border: 1px solid var(--line); background: var(--card-2);
  font-size: 22px; display: grid; place-items: center; flex: none;
}
.step-btn:active { transform: scale(0.95); }
.mode-cool .big { color: var(--cool); }
.mode-heat .big { color: var(--heat); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--card-2); border-radius: 999px; padding: 6px 12px; font-size: 13px; text-transform: capitalize; }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }

.compartments { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.comp { background: var(--card-2); border-radius: 14px; padding: 12px; display: grid; gap: 8px; }
.comp .name { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--muted); }
.comp .temp { font-size: 30px; font-weight: 300; font-variant-numeric: tabular-nums; line-height: 1; }
.comp .set { display: flex; align-items: center; justify-content: space-between; gap: 4px; font-size: 12.5px; color: var(--muted); }
.comp .set span { white-space: nowrap; }
.comp .set button { width: 30px; height: 30px; flex: none; border-radius: 50%; border: 1px solid var(--line); background: var(--card); }
.toggles { display: grid; gap: 10px; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.stat-row { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); flex-wrap: wrap; }

.tv-remote { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 14px; align-items: center; }
.tv-remote .vol { align-content: center; }
.dpad { display: grid; grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 44px); gap: 4px; }
.dpad button { border: 1px solid var(--line); background: var(--card-2); border-radius: 12px; font-size: 16px; }
.dpad .ok { border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 13px; }
.dpad .up { grid-column: 2; grid-row: 1; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .ok { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 3; }
.vol { display: grid; gap: 8px; }
.vol-row { display: flex; align-items: center; gap: 8px; }
.vol-row button { width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line); background: var(--card-2); font-size: 18px; }
.bar { flex: 1; height: 6px; border-radius: 999px; background: var(--line); overflow: hidden; min-width: 40px; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.keys { display: flex; gap: 6px; flex-wrap: wrap; }
.key { border: 1px solid var(--line); background: var(--card-2); border-radius: 10px; padding: 7px 11px; font-size: 13px; }
.pair-box { display: grid; gap: 8px; }
.pair-box input { text-transform: uppercase; letter-spacing: 0.3em; font-size: 20px; text-align: center; }

.range { width: 100%; accent-color: var(--accent); }
.readings { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; }
.reading { background: var(--card-2); border-radius: 12px; padding: 10px; }
.reading .v { font-size: 20px; font-variant-numeric: tabular-nums; }
.reading .l { font-size: 12px; color: var(--muted); }

.camera { padding: 0; overflow: hidden; gap: 0; align-self: start; }
.camera .frame { position: relative; aspect-ratio: 16 / 9; background: #000; border: 0; padding: 0; width: 100%; display: block; }
.camera img { width: 100%; height: 100%; object-fit: cover; display: block; }
.camera .frame .err { position: absolute; inset: 0; display: grid; place-items: center; color: #ccc; font-size: 13px; padding: 20px; text-align: center; }
.camera .card-head { padding: 12px 14px; }

.btn { border: 1px solid var(--line); background: var(--card-2); border-radius: 12px; padding: 9px 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); }
.btn.subtle { background: none; color: var(--muted); }
.link { border: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; }
.icon-btn { border: 0; background: var(--card-2); width: 34px; height: 34px; border-radius: 50%; }
.row { display: flex; align-items: center; }
.gap { gap: 8px; }
.end { justify-content: flex-end; margin-top: 18px; }

input:not([type="range"]), select {
  width: 100%; border: 1px solid var(--line); background: var(--card-2); border-radius: 10px; padding: 9px 11px;
}

dialog { border: 0; padding: 0; background: transparent; color: var(--text); }
dialog::backdrop { background: rgb(0 0 0 / 45%); backdrop-filter: blur(2px); }
.sheet { width: min(560px, calc(100vw - 24px)); }
.sheet.wide { width: min(720px, calc(100vw - 24px)); }
.sheet > *, .sheet > form { background: var(--card); }
.sheet, .camera-dialog { border-radius: 20px; overflow: hidden; }
.sheet { background: var(--card); padding: 18px; max-height: calc(100dvh - 32px); overflow-y: auto; overscroll-behavior: contain; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.sheet h3 { font-size: 14px; margin: 18px 0 8px; color: var(--muted); }
.integration { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; padding: 12px 0; border-top: 1px solid var(--line); align-items: start; }
.integration .dot { margin-top: 6px; }
.integration .msg { grid-column: 2; font-size: 13px; color: var(--muted); }
.integration .actions { margin-top: 6px; flex-wrap: wrap; }
.integration .actions:empty { display: none; }
.int-form { grid-column: 2; }
.int-form:empty { display: none; }
.int-settings { display: grid; gap: 12px; margin-top: 10px; padding: 12px; border-radius: 14px; background: var(--card-2); }
.int-settings input, .int-settings select { background: var(--card); }
.int-settings .row input { flex: 1; min-width: 0; }
.int-settings .end { margin-top: 0; }
.hint { font-size: 12px; color: var(--muted); margin: 0; }
.hint code { font-size: 11.5px; word-break: break-all; color: var(--text); }
.tag { font-size: 11px; background: var(--accent-soft); color: var(--accent); border-radius: 6px; padding: 1px 6px; margin-left: 4px; }
.field label { color: var(--text); font-weight: 500; }

.camera-dialog { width: min(1100px, 96vw); background: var(--card); padding: 14px; }
.camera-dialog img { width: 100%; border-radius: 12px; background: #000; display: block; }

.field { display: grid; gap: 4px; font-size: 12.5px; color: var(--muted); }
.field-row { display: flex; gap: 10px; }
.grow { flex: 1; }
.icon-field { width: 80px; }
.icon-field input { text-align: center; font-size: 18px; }
.steps { list-style: none; padding: 0; margin: 0 0 10px; display: grid; gap: 8px; }
.steps li { display: grid; grid-template-columns: 1.4fr 1fr 1fr auto; gap: 6px; align-items: center; background: var(--card-2); padding: 8px; border-radius: 12px; }
.steps li.delay { grid-template-columns: auto 120px 1fr auto; }
.steps li input, .steps li select { background: var(--card); }

.empty { text-align: center; color: var(--muted); margin-top: 60px; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 90vw; z-index: 10;
}

.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-box { width: min(340px, 100%); display: grid; gap: 12px; text-align: center; justify-items: center; }
.login-box input { text-align: center; font-size: 18px; }
.login-box .btn { width: 100%; }

/* ---- house → floors → rooms ---- */
a { color: var(--accent); }
a.pill, a.btn { text-decoration: none; color: inherit; display: inline-flex; align-items: center; }
a.btn.primary { color: #fff; }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.small { font-size: 12.5px; }
.section-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin: 0 4px 10px; }
.section-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; margin: 0; }
.section-head .setup-h { margin: 0; }

.welcome { margin-bottom: 22px; }
.welcome h2 { font-size: 20px; }
.welcome p { margin: 0; }

.floor-tabs { display: flex; gap: 6px; margin: 0 0 14px; overflow-x: auto; scrollbar-width: none; }
.floor-tab { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 8px 16px; white-space: nowrap; font-weight: 500; }
.floor-tab.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.floor-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 4px 10px; font-weight: 600; }

.rooms { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); margin-bottom: 28px; }
.room-tile {
  display: grid; gap: 4px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); text-decoration: none; color: inherit; min-height: 132px; align-content: start;
  transition: transform 0.1s, border-color 0.15s;
}
.room-tile:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.room-tile:active { transform: scale(0.98); }
.room-tile.active { background: linear-gradient(160deg, var(--accent-soft), var(--card) 70%); }
.room-top { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 6px; }
.room-icon { font-size: 26px; line-height: 1; }
.room-icon.big { font-size: 34px; }
.room-name { font-weight: 600; font-size: 16px; }
.room-meta { font-size: 12.5px; color: var(--muted); }
.room-types { font-size: 13px; margin-top: 4px; letter-spacing: 2px; }

.room-head { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.room-head h2 { font-size: 22px; }
.room-head p { margin: 2px 0 0; }
.back {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; flex: none; text-decoration: none;
  background: var(--card); border: 1px solid var(--line); color: var(--text); font-size: 24px; line-height: 1; padding-bottom: 3px;
}

.setup-grid { display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; padding-bottom: 70px; }
.setup-h { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 6px 0 0; }
.setup-floor { border: 1px solid var(--line); border-radius: 14px; padding: 10px; display: grid; gap: 8px; }
.setup-row { display: flex; gap: 6px; align-items: center; }
.setup-row input { min-width: 0; flex: 1; }
.setup-row .icon-btn { flex: none; font-size: 13px; }
.setup-row .icon-btn:disabled { opacity: 0.35; cursor: default; }
.floor-name { font-weight: 600; }
.setup-rooms { list-style: none; margin: 0; padding: 0 0 0 10px; display: grid; gap: 6px; border-left: 2px solid var(--line); }
.icon-select { width: 58px !important; flex: none; padding: 8px 4px !important; font-size: 17px; }
.count { font-size: 12px; white-space: nowrap; }
.setup-devices { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.setup-devices li { display: grid; grid-template-columns: 26px minmax(0, 1fr) minmax(0, 0.9fr); gap: 8px; align-items: center; padding: 8px; border-radius: 12px; background: var(--card-2); }
.setup-devices li.unplaced { outline: 1.5px dashed color-mix(in srgb, var(--warn) 70%, transparent); }
.setup-devices li input, .setup-devices li select { background: var(--card); }
.dev-icon { font-size: 18px; text-align: center; }
.dev-name { display: grid; gap: 2px; min-width: 0; }
.dev-name .small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.save-bar {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; gap: 10px; align-items: center; background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; padding: 8px 8px 8px 16px; box-shadow: 0 8px 30px rgb(0 0 0 / 18%); z-index: 5; white-space: nowrap;
}
.save-bar .btn:disabled { opacity: 0.5; cursor: default; }

@media (max-width: 860px) {
  .setup-grid { grid-template-columns: 1fr; }
}

@media (max-width: 520px) {
  .rooms { grid-template-columns: 1fr 1fr; }
  .setup-rooms { padding-left: 6px; }
  .setup-row { gap: 4px; }
  .setup-row .icon-btn { width: 28px; height: 28px; font-size: 12px; }
  .setup-row .count { display: none; }
  .icon-select { width: 50px !important; }
  .setup-devices li { grid-template-columns: 26px 1fr; }
  .setup-devices li select { grid-column: 1 / -1; }
  .top-actions .pill { padding: 7px 11px; font-size: 13.5px; }
  .top h1 { font-size: 24px; }
  .grid, .grid.cams { grid-template-columns: 1fr; }
  .steps li { grid-template-columns: 1fr 1fr; }
  .steps li.delay { grid-template-columns: auto 1fr auto; }
}
