:root {
  --bg: #efeee9;
  --panel: #ffffff;
  --ink: #23211e;
  --muted: #6b6862;
  --line: #e2dfd7;
  --accent: #2a6f97;
  --accent-ink: #ffffff;
  --warn: #b35c00;
  --error: #b42318;
  --ok: #1a7f4b;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1d21;
    --panel: #25282e;
    --ink: #f1efe9;
    --muted: #a8a49c;
    --line: #3a3e46;
    --accent: #5aa6d6;
    --accent-ink: #0f1720;
    --warn: #f0a050;
    --error: #ff8a80;
    --ok: #6fd39b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 960px;
  height: 100vh;
  overflow: hidden;
}

button, input, select { font: inherit; color: inherit; }

button {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 10px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--error); }
button.icon-btn { width: 32px; padding: 6px 0; text-align: center; font-weight: 700; }
button:disabled { opacity: .5; cursor: default; }

kbd {
  font: 600 11px/1 ui-monospace, Menlo, monospace;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 2px 4px;
  color: var(--muted);
  background: var(--bg);
  margin-left: 2px;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, Menlo, monospace; }

/* top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: baseline; gap: 10px; margin-right: auto; }
.brand strong { font-size: 16px; }
.brand span { color: var(--muted); font-size: 12px; }

.views { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg { border: 0; border-radius: 0; background: transparent; }
.seg + .seg { border-left: 1px solid var(--line); }
.seg.active { background: var(--accent); color: var(--accent-ink); }
.seg.active kbd { background: transparent; color: inherit; border-color: currentColor; }

.tools, .file { display: flex; align-items: center; gap: 10px; }
.check { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }
.time { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.time input { width: 120px; accent-color: var(--accent); }
.time #time-label { color: var(--ink); min-width: 42px; }

/* layout */
.layout {
  display: grid;
  grid-template-columns: 270px 1fr 320px;
  min-height: 0;
}
.sidebar {
  background: var(--panel);
  overflow-y: auto;
  padding: 14px;
}
.sidebar.left { border-right: 1px solid var(--line); }
.sidebar.right { border-left: 1px solid var(--line); }
.sidebar h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 14px 0 8px; font-weight: 600; }
.sidebar h2:first-child { margin-top: 0; }
.hint { color: var(--muted); font-size: 12px; margin: 0 0 10px; }

.stage { position: relative; min-width: 0; min-height: 0; }
.view-wrap { position: absolute; inset: 0; }
.view-wrap[hidden] { display: none; }
#canvas3d, #canvas-plan { width: 100%; height: 100%; display: block; }
#canvas3d { touch-action: none; }

.caption {
  position: absolute;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: rgba(20, 20, 20, .72);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  display: flex;
  gap: 12px;
  align-items: baseline;
  pointer-events: none;
  backdrop-filter: blur(4px);
}
.caption[hidden] { display: none; }
.caption b { font-size: 16px; }
.caption span { opacity: .8; font-size: 13px; }

.walk-hint {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  background: rgba(20, 20, 20, .66);
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 12px;
  pointer-events: none;
  white-space: nowrap;
}
.walk-hint[hidden] { display: none; }
.walk-hint kbd { color: #fff; background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.3); }

.tip {
  position: absolute;
  pointer-events: none;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  box-shadow: 0 4px 14px rgba(0,0,0,.12);
  max-width: 260px;
}
.tip[hidden] { display: none; }

.drag-chip {
  position: fixed;
  left: 0;
  top: 0;
  pointer-events: none;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  box-shadow: 0 6px 18px rgba(0,0,0,.2);
  z-index: 10;
}
.drag-chip[hidden] { display: none; }
body.dragging { cursor: grabbing; user-select: none; }

/* palette and list */
.palette { display: grid; gap: 6px; }
.palette-item {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 7px 8px;
  cursor: grab;
  touch-action: none;
}
.palette-item:active { cursor: grabbing; }
.palette-text { display: flex; flex-direction: column; min-width: 0; }
.palette-text b { font-weight: 600; font-size: 13px; }
.palette-text small { color: var(--muted); font-size: 11px; }

.chip {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--bg);
  color: var(--accent);
  flex: none;
}
.chip.small { width: 26px; height: 26px; }

.device-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
.device-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
}
.device-row:hover { background: var(--bg); }
.device-row.selected { background: var(--bg); box-shadow: inset 3px 0 0 var(--accent); }
.device-name { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.device-name { white-space: nowrap; text-overflow: ellipsis; }
.device-name small { color: var(--muted); font-size: 11px; }
.device-list .empty { color: var(--muted); font-size: 13px; padding: 6px; }
.mini-battery { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* signal bars */
.bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 16px; flex: none; }
.bars i { width: 3px; background: currentColor; opacity: .18; border-radius: 1px; }
.bars i.on { opacity: 1; }

/* card */
.card {
  display: grid;
  gap: 12px;
}
.card h3 { margin: 0; font-size: 15px; }
.card-head { display: flex; gap: 10px; align-items: center; }
.card-title { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.name-input {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 6px;
  margin-left: -6px;
  background: transparent;
  font-weight: 600;
  font-size: 15px;
  width: 100%;
}
.name-input:hover, .name-input:focus { border-color: var(--line); background: var(--bg); outline: none; }
.card-title small { color: var(--muted); font-size: 12px; margin-left: -6px; padding-left: 6px; }

.stat-list { display: grid; gap: 0; border-top: 1px solid var(--line); }
.stat {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.stat > span:first-child { color: var(--muted); }
.stat > span:last-child { text-align: right; }
.on-text { color: var(--ok); font-weight: 600; }
.off-text { color: var(--muted); }

.battery { display: inline-flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.battery-bar { width: 46px; height: 10px; border: 1px solid var(--muted); border-radius: 3px; padding: 1px; display: inline-block; }
.battery-bar i { display: block; height: 100%; border-radius: 1px; }

.controls { display: grid; gap: 12px; }
.field { display: grid; gap: 6px; font-size: 13px; }
.field > span { display: flex; justify-content: space-between; color: var(--muted); }
.field output { color: var(--ink); font-variant-numeric: tabular-nums; }
.field input[type="range"], .controls input[type="range"] { width: 100%; accent-color: var(--accent); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.btn-row.stepper b { min-width: 60px; text-align: center; font-size: 16px; }

.switch-row { display: flex; justify-content: space-between; align-items: center; font-size: 13px; cursor: pointer; }
.switch-row > span:first-child { color: var(--muted); }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  transition: background .15s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s;
  box-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.switch-row input:checked + .switch { background: var(--accent); }
.switch-row input:checked + .switch::after { transform: translateX(18px); }
.switch-row input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

.card-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.card-actions button:last-child { margin-left: auto; }

.room-signals { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.room-signals li { display: grid; grid-template-columns: 1fr auto 74px; gap: 8px; align-items: center; font-size: 13px; }
.room-signals .num { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }

/* status bar and help */
.statusbar {
  padding: 6px 14px;
  border-top: 1px solid var(--line);
  background: var(--panel);
  font-size: 12px;
  color: var(--muted);
  min-height: 30px;
}
.statusbar[data-tone="warn"] #status { color: var(--warn); }
.statusbar[data-tone="error"] #status { color: var(--error); }

dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--ink);
  max-width: 560px;
  padding: 22px 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
.keys { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0 0 14px; font-size: 13px; }
.keys dt { white-space: nowrap; }
.keys dd { margin: 0; }
