:root {
  --bg: #0f141b;
  --panel: #161d27;
  --panel-2: #1d2733;
  --line: #2a3646;
  --text: #e6edf5;
  --muted: #8d9bae;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --good: #22c55e;
  --warn: #f59e0b;
  --bad: #ef4444;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.4 var(--font); overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
kbd { font: 600 10px/1 var(--font); padding: 2px 5px; border-radius: 4px; background: rgba(255,255,255,.12); margin-left: 4px; color: var(--muted); }

#app { display: grid; grid-template-columns: 268px 1fr; grid-template-rows: 52px 1fr; height: 100%; }
#topbar { grid-column: 1 / 3; display: flex; align-items: center; gap: 10px; padding: 0 14px; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 20; }
.brand { display: flex; align-items: center; gap: 10px; margin-right: 8px; }
.logo { width: 30px; height: 30px; display: grid; place-items: center; border-radius: 9px; background: linear-gradient(135deg, #38bdf8, #6366f1); color: #fff; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text small { color: var(--muted); font-size: 11px; }
.spacer { flex: 1; }

.seg { display: inline-flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; color: var(--muted); }
.seg button.active { background: var(--accent-2); color: #fff; }
.seg button.active kbd { background: rgba(255,255,255,.25); color: #fff; }
.seg button:hover:not(.active) { color: var(--text); }
.btn { display: inline-flex; align-items: center; gap: 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 11px; }
.btn:hover { border-color: #3c4d63; }
.btn.accent { background: #0b5b83; border-color: #0e7bb0; }
.btn.accent:hover { background: #0e6f9e; }
.btn.accent.running { background: #b45309; border-color: #d97706; }
.btn.toggle.on { background: rgba(56,189,248,.16); border-color: var(--accent); color: #bfe9ff; }
.icon { border: 0; background: transparent; font-size: 16px; padding: 2px 4px; line-height: 1; }
.daynight { display: flex; align-items: center; gap: 4px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; }
.daynight input { width: 110px; accent-color: var(--accent); }
#timeLabel { min-width: 38px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.menu-wrap { position: relative; }
.menu { position: absolute; right: 0; top: 40px; width: 230px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px; box-shadow: 0 12px 40px rgba(0,0,0,.45); z-index: 40; }
.menu button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 6px; }
.menu button:hover { background: var(--panel-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.save-state { color: var(--muted); font-size: 11px; min-width: 44px; }
.save-state.dirty { color: var(--warn); }

#left { grid-row: 2; display: flex; flex-direction: column; gap: 10px; padding: 10px; background: var(--bg); border-right: 1px solid var(--line); overflow: hidden; z-index: 10; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; }
.panel.grow { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.panel h2 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); display: flex; justify-content: space-between; align-items: center; }
.count { background: var(--panel-2); border-radius: 99px; padding: 1px 8px; font-size: 11px; color: var(--text); }
.hint { margin: 0 0 8px; color: var(--muted); font-size: 11.5px; }
.palette { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.tile { position: relative; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px 5px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; cursor: grab; user-select: none; touch-action: none; }
.tile:hover { border-color: var(--accent); background: #213041; }
.tile img { width: 52px; height: 52px; object-fit: contain; pointer-events: none; -webkit-user-drag: none; }
.tile span { font-size: 10.5px; text-align: center; line-height: 1.15; color: #cbd6e4; pointer-events: none; }
.tile .mount { position: absolute; top: 3px; right: 4px; font-size: 8.5px; text-transform: uppercase; color: var(--muted); letter-spacing: .04em; pointer-events: none; }
.tile.armed { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(56,189,248,.35); }

.device-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.device-list li { display: grid; grid-template-columns: 30px 1fr auto; gap: 8px; align-items: center; padding: 5px 7px; border-radius: 8px; border: 1px solid transparent; cursor: pointer; background: var(--panel-2); }
.device-list li:hover { border-color: #3a4c63; }
.device-list li.sel { border-color: var(--accent); background: #1c3347; }
.device-list img { width: 28px; height: 28px; object-fit: contain; }
.device-list .nm { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-list .sub { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { color: var(--muted); padding: 8px; font-size: 12px; }

.bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 14px; }
.bars i { width: 3px; border-radius: 1px; background: #3a4656; }
.bars i:nth-child(1) { height: 4px; } .bars i:nth-child(2) { height: 7px; } .bars i:nth-child(3) { height: 10px; } .bars i:nth-child(4) { height: 14px; }
.bars.l1 i:nth-child(-n+1), .bars.l2 i:nth-child(-n+2), .bars.l3 i:nth-child(-n+3), .bars.l4 i:nth-child(-n+4) { background: var(--c, var(--good)); }

#stage { position: relative; grid-row: 2; overflow: hidden; background: #000; }
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; outline: none; touch-action: none; }
#gl.locked { cursor: none; }
#labels, #pins, #dims { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
#dims { width: 100%; height: 100%; }
.room-label { position: absolute; left: 0; top: 0; text-align: center; white-space: nowrap; color: #1b2430; text-shadow: 0 0 3px rgba(255,255,255,.9), 0 0 6px rgba(255,255,255,.7); line-height: 1.15; will-change: transform; }
.room-label b { display: block; font-size: 12px; font-weight: 700; }
.room-label span { font-size: 10px; opacity: .75; }
.room-label.compact span { display: none; }
#stage.night .room-label { color: #e8eef6; text-shadow: 0 0 3px rgba(0,0,0,.9), 0 0 6px rgba(0,0,0,.7); }
.dim-line { fill: none; stroke: #2563eb; stroke-width: 1.2; }
.dim-line.outer { stroke: #1e3a8a; stroke-width: 1.4; }
.dim-text { fill: #1d4ed8; font: 600 10.5px var(--font); paint-order: stroke; stroke: rgba(255,255,255,.9); stroke-width: 3px; }
.dim-text.outer { fill: #1e3a8a; font-size: 12px; }
#stage.night .dim-line { stroke: #7dd3fc; } #stage.night .dim-text { fill: #bae6fd; stroke: rgba(10,16,26,.9); }
#stage.night .dim-line.outer { stroke: #bae6fd; } #stage.night .dim-text.outer { fill: #e0f2fe; }

.pin { position: absolute; left: 0; top: 0; width: 34px; height: 34px; padding: 0; border-radius: 50%; border: 2px solid #94a3b8; background: rgba(15,20,27,.88); display: grid; place-items: center; pointer-events: none; will-change: transform; box-shadow: 0 2px 8px rgba(0,0,0,.4); }
.pin img { width: 24px; height: 24px; object-fit: contain; pointer-events: none; }
.pin i { position: absolute; right: -2px; top: -2px; width: 9px; height: 9px; border-radius: 50%; background: #64748b; border: 1.5px solid #0f141b; }
.pin.on i { background: var(--good); }
.pin.sel { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(56,189,248,.35); }
.pin.hover { border-color: #fff; }

#scalebar { position: absolute; left: 14px; bottom: 14px; display: flex; align-items: center; gap: 6px; color: #1e3a8a; font: 600 11px var(--font); pointer-events: none; }
#scalebar i { display: block; height: 7px; border: 2px solid currentColor; border-top: 0; }
#stage.night #scalebar { color: #bae6fd; }

#hud-top { position: absolute; left: 12px; top: 12px; display: flex; gap: 8px; align-items: center; pointer-events: none; max-width: calc(100% - 340px); }
#modeBadge { background: rgba(15,20,27,.78); border: 1px solid var(--line); border-radius: 99px; padding: 4px 11px; font-weight: 600; backdrop-filter: blur(6px); }
#hintLine { background: rgba(15,20,27,.78); border: 1px solid var(--line); border-radius: 99px; padding: 4px 11px; color: #cfe8fb; backdrop-filter: blur(6px); }
#hintLine:empty { display: none; }
#caption { position: absolute; left: 50%; bottom: 36px; transform: translateX(-50%); text-align: center; background: rgba(8,12,18,.72); border: 1px solid rgba(255,255,255,.16); border-radius: 14px; padding: 10px 22px; backdrop-filter: blur(8px); pointer-events: none; }
#caption b { display: block; font-size: 20px; letter-spacing: .01em; }
#caption span { color: #b8c7da; font-size: 12px; }
#caption .bar { margin-top: 6px; height: 3px; background: rgba(255,255,255,.15); border-radius: 2px; overflow: hidden; }
#caption .bar i { display: block; height: 100%; background: var(--accent); }
#legend { position: absolute; left: 12px; bottom: 12px; width: 250px; background: rgba(15,20,27,.85); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; backdrop-filter: blur(6px); }
#stage.mode-plan #legend { bottom: 42px; }
#legend .ramp { height: 8px; border-radius: 4px; margin: 6px 0 3px; background: linear-gradient(90deg, #22c55e, #84cc16, #facc15, #f97316, #dc2626, #5a143c); }
#legend .ticks { display: flex; justify-content: space-between; color: var(--muted); font-size: 10px; }
#legend small { color: var(--muted); display: block; margin-top: 4px; font-size: 10.5px; }
#help { position: absolute; right: 12px; bottom: 12px; color: #d3deea; font-size: 11.5px; background: rgba(15,20,27,.7); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; max-width: 340px; backdrop-filter: blur(6px); pointer-events: none; }
#help kbd { background: rgba(255,255,255,.16); color: #fff; }
#stage.mode-plan #help { color: #1b2430; background: rgba(255,255,255,.72); border-color: #cbd5e1; }
#stage.mode-plan #help kbd { background: rgba(0,0,0,.1); color: #111; }
#tip { position: absolute; pointer-events: none; background: rgba(15,20,27,.92); border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; font-size: 12px; z-index: 5; white-space: nowrap; transform: translate(12px, 12px); }
#tip small { display: block; color: var(--muted); }
#toast { position: absolute; left: 50%; top: 14px; transform: translateX(-50%); background: #0e7bb0; padding: 7px 14px; border-radius: 99px; z-index: 30; box-shadow: 0 6px 20px rgba(0,0,0,.35); }
#toast.err { background: #b91c1c; }
#loading { position: absolute; inset: 0; display: grid; place-content: center; gap: 12px; justify-items: center; background: var(--bg); z-index: 50; color: var(--muted); transition: opacity .4s; }
#loading.done { opacity: 0; pointer-events: none; }
.spin { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* status card */
#card { position: absolute; right: 12px; top: 12px; width: 296px; max-height: calc(100% - 24px); overflow-y: auto; background: rgba(22,29,39,.96); border: 1px solid var(--line); border-radius: 14px; padding: 12px; box-shadow: 0 16px 50px rgba(0,0,0,.5); backdrop-filter: blur(10px); z-index: 15; }
.card-head { display: grid; grid-template-columns: 46px 1fr auto; gap: 10px; align-items: center; }
.card-head img { width: 46px; height: 46px; object-fit: contain; background: var(--panel-2); border-radius: 10px; padding: 3px; }
.card-head input { width: 100%; background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 3px 5px; margin-left: -5px; font: 600 15px var(--font); color: var(--text); }
.card-head input:hover { border-color: var(--line); } .card-head input:focus { border-color: var(--accent); outline: none; background: var(--panel-2); }
.card-head .meta { color: var(--muted); font-size: 11.5px; }
.xbtn { border: 0; background: transparent; color: var(--muted); font-size: 18px; width: 26px; height: 26px; border-radius: 6px; }
.xbtn:hover { background: var(--panel-2); color: var(--text); }
.status-line { display: flex; align-items: center; gap: 8px; margin: 10px 0 6px; }
.pill { padding: 3px 10px; border-radius: 99px; font-size: 11.5px; background: var(--panel-2); border: 1px solid var(--line); }
.pill.on { background: rgba(34,197,94,.14); border-color: rgba(34,197,94,.5); color: #a7f0c0; }
.pill.alert { background: rgba(239,68,68,.15); border-color: rgba(239,68,68,.55); color: #fecaca; }
.row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid rgba(255,255,255,.06); }
.row label { color: #c3cfdd; }
.row .val { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 52px; text-align: right; }
.row.full { grid-template-columns: 1fr; gap: 4px; }
.row input[type=range] { width: 100%; accent-color: var(--accent); }
.row .rng { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; }
.row select { background: var(--panel-2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; padding: 4px 6px; }
.switch { position: relative; width: 38px; height: 22px; border-radius: 99px; background: #3a4656; border: 0; padding: 0; transition: background .15s; }
.switch::after { content: ''; position: absolute; left: 3px; top: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch.on { background: var(--accent-2); } .switch.on::after { transform: translateX(16px); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.meter { height: 8px; background: #2a3441; border-radius: 4px; overflow: hidden; }
.meter i { display: block; height: 100%; border-radius: 4px; background: var(--c, var(--good)); }
.block { border-top: 1px solid rgba(255,255,255,.06); padding: 8px 0 4px; }
.block .top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.block .top b { display: flex; align-items: center; gap: 8px; }
.block small { color: var(--muted); display: block; margin-top: 4px; }
.actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.actions .btn { flex: 1; justify-content: center; }
.actions .btn.danger { color: #fca5a5; }
.actions .btn[disabled] { opacity: .4; cursor: not-allowed; }

@media (max-width: 1100px) {
  #app { grid-template-columns: 220px 1fr; }
  .palette { grid-template-columns: repeat(2, 1fr); }
  kbd { display: none; }
  .daynight input { width: 70px; }
}
