:root {
  --bg: #0b0f14; --bg2: #121821; --card: #17202b; --card2: #1e2935; --line: #253241;
  --text: #e8eef5; --muted: #8b9aab; --accent: #2dd4bf; --accent-d: #0f766e; --warn: #f59e0b; --bad: #ef4444;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
  --list-w: 380px; --hinge: 0px; --seg0w: 50vw; --seg0h: 50vh;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden;
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; text-align: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.ic.big { width: 34px; height: 34px; }
[hidden] { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12.5px; } .center { text-align: center; }
.scroll { overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

/* ---------- sign-in ---------- */
#login { position: fixed; inset: 0; display: grid; place-items: center; padding: calc(24px + var(--sat)) 24px calc(24px + var(--sab)); }
.login-card { width: min(380px, 100%); text-align: center; }
.login-logo { width: 72px; height: 72px; border-radius: 18px; }
.login-card h1 { margin: 12px 0 4px; font-size: 26px; }
.login-card p { color: var(--muted); margin: 0 0 20px; }
.field { display: block; text-align: left; margin-bottom: 14px; }
.field span { display: block; font-size: 12.5px; color: var(--muted); margin: 0 0 6px 4px; }
input[type=url], select { width: 100%; background: var(--card); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; font: inherit; }
select { width: auto; max-width: 60%; padding: 8px 10px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 11px 16px; border-radius: 12px; background: var(--card2); font-weight: 600; }
.btn.primary { background: var(--accent); color: #04211d; }
.btn.danger { background: #3b1518; color: #fca5a5; }
.btn.wide { width: 100%; } .btn.big { padding: 16px; font-size: 17px; margin: 6px 0 14px; }
.btn + .btn { margin-top: 8px; }

/* ---------- app shell & layouts ---------- */
#app { position: fixed; inset: 0; display: grid; }
#pane-list { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg); }
#pane-detail { position: relative; min-height: 0; min-width: 0; background: #000; display: flex; flex-direction: column; }
#hinge { background: #000; }

body[data-layout=compact] #app { grid-template-columns: 1fr; }
body[data-layout=compact] #hinge { display: none; }
body[data-layout=compact] #pane-detail { position: fixed; inset: 0; z-index: 20; transform: translateX(100%); transition: transform .22s ease; }
body[data-layout=compact][data-detail=live] #pane-detail { transform: none; }
body[data-layout=compact] .only-expanded { display: none; }

body[data-layout=expanded] #app { grid-template-columns: var(--list-w) 0 1fr; }
body[data-layout=expanded] #pane-list { border-right: 1px solid var(--line); }

body[data-layout=split-cols] #app { grid-template-columns: var(--seg0w) var(--hinge) 1fr; }
body[data-layout=split-rows] #app { grid-template-rows: var(--seg0h) var(--hinge) 1fr; grid-template-columns: 1fr; }
body[data-layout=split-rows] #pane-detail { grid-row: 1; } body[data-layout=split-rows] #hinge { grid-row: 2; }
body[data-layout=split-rows] #pane-list { grid-row: 3; }

/* ---------- bars ---------- */
.bar { display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--sat)) calc(16px + var(--sar)) 10px calc(16px + var(--sal)); min-height: 52px; }
body[data-layout=split-rows] #pane-list .bar { padding-top: 10px; }
.bar h1 { font-size: 26px; margin: 0; flex: 1; letter-spacing: -.3px; }
.bar h2 { font-size: 17px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; background: var(--card); }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.conn.online { background: #22c55e; } .conn.offline { background: var(--bad); } .conn.connecting { background: var(--warn); }

.view { flex: 1; min-height: 0; padding: 0 calc(14px + var(--sar)) 16px calc(14px + var(--sal)); display: none; }
body[data-tab=home] #view-home, body[data-tab=events] #view-events, body[data-tab=settings] #view-settings { display: block; }

#tabs { display: flex; border-top: 1px solid var(--line); background: var(--bg2); padding: 6px calc(6px + var(--sar)) calc(6px + var(--sab)) calc(6px + var(--sal)); }
#tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); padding: 4px 0; }
#tabs button.on { color: var(--accent); }
body[data-layout=split-cols] #tabs, body[data-layout=expanded] #tabs { padding-bottom: calc(6px + var(--sab)); }

.sec { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 18px 4px 8px; font-weight: 600; }
.empty { display: grid; place-items: center; text-align: center; padding: 40px 20px; color: var(--muted); align-content: center; gap: 6px; }
.empty p { margin: 0; } .empty svg { width: 46px; height: 46px; opacity: .5; }
#detail-empty { flex: 1; }

/* ---------- camera tiles ---------- */
.cams { display: grid; grid-template-columns: 1fr; gap: 12px; }
body[data-layout=compact] .cams { grid-template-columns: 1fr; }
@media (min-width: 520px) { body[data-layout=compact] .cams { grid-template-columns: 1fr 1fr; } }
.cam { display: block; width: 100%; background: var(--card); border-radius: 16px; overflow: hidden; border: 2px solid transparent; }
.cam.sel { border-color: var(--accent); }
.thumb { position: relative; aspect-ratio: 16/9; background: #05080c; }
.thumb img, .mcam img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; }
img.broken { opacity: 0; }
.cam.off .thumb img { filter: grayscale(1) brightness(.4); }
.badge { position: absolute; top: 8px; left: 8px; font-size: 11.5px; font-weight: 700; padding: 3px 8px; border-radius: 99px; background: rgba(0,0,0,.65); }
.badge.motion { left: auto; right: 8px; background: var(--warn); color: #241400; }
.play { position: absolute; right: 10px; bottom: 10px; width: 34px; height: 34px; border-radius: 50%; background: rgba(0,0,0,.55); display: grid; place-items: center; }
.play svg { width: 18px; height: 18px; }
.cam-name { padding: 10px 12px; font-weight: 600; }

/* ---------- device cards ---------- */
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.card { min-width: 0; position: relative; background: var(--card); border-radius: 16px; min-height: 96px; display: flex; transition: background .15s; }
.card-main { flex: 1; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; padding: 12px; min-width: 0; }
.card-main svg { width: 26px; height: 26px; color: var(--muted); margin-bottom: 6px; }
.card-name { font-weight: 600; width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 22px; }
.card-state { font-size: 12.5px; color: var(--muted); }
.card-more { position: absolute; top: 6px; right: 4px; width: 32px; height: 32px; display: grid; place-items: center; color: var(--muted); }
.card.on { background: var(--card2); }
.card.on .card-main svg { color: var(--accent); }
.card.d-light.on .card-main svg { color: var(--glow, #ffd27a); filter: drop-shadow(0 0 6px var(--glow, #ffd27a)); }
.card.d-lock .card-main svg { color: #22c55e; } .card.d-lock.on .card-main svg { color: var(--warn); }
.card.off { opacity: .5; }

.rows { display: flex; flex-direction: column; background: var(--card); border-radius: 14px; overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%; min-height: 50px; }
.row + .row { border-top: 1px solid var(--line); }
.row svg { color: var(--muted); flex: none; }
.row.active svg { color: var(--warn); }
.row-name { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-state { color: var(--muted); font-size: 13.5px; }

/* ---------- events ---------- */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 7px 13px; border-radius: 99px; background: var(--card); font-size: 13.5px; white-space: nowrap; }
.chip.on { background: var(--accent); color: #04211d; font-weight: 600; }
.event { display: flex; align-items: center; gap: 12px; width: 100%; padding: 8px 4px; border-bottom: 1px solid var(--line); }
.ev-thumb { width: 92px; aspect-ratio: 16/9; border-radius: 8px; object-fit: cover; background: #05080c; flex: none; }
.ev-icon { width: 92px; aspect-ratio: 16/9; border-radius: 8px; background: var(--card); display: grid; place-items: center; flex: none; color: var(--muted); }
.ev-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.ev-text b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev-time { color: var(--muted); font-size: 13px; }

/* ---------- settings ---------- */
.panel { background: var(--card); border-radius: 14px; padding: 6px 14px 12px; }
.kv { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; min-height: 44px; }
.kv + .kv { border-top: 1px solid var(--line); }
.kv b { font-weight: 500; color: var(--muted); text-align: right; word-break: break-all; }
.kv.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.panel .btn { margin-top: 10px; }

/* ---------- multi-camera grid ---------- */
#multi { flex: 1; display: grid; gap: 4px; padding: calc(4px + var(--sat)) calc(4px + var(--sar)) calc(4px + var(--sab)) 4px;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr)); grid-auto-rows: max-content; align-content: center; overflow-y: auto; }
.mcam { position: relative; display: block; width: 100%; aspect-ratio: 16/9; background: #05080c; border-radius: 10px; overflow: hidden; }
.mcam.off img { filter: grayscale(1) brightness(.4); }
.mcam-name { position: absolute; left: 8px; bottom: 8px; font-size: 13px; font-weight: 600; padding: 3px 8px; border-radius: 8px; background: rgba(0,0,0,.6); }
.mcam-name em { color: var(--warn); font-style: normal; }

/* ---------- live view ---------- */
#live { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.live-bar { background: var(--bg); }
body[data-layout=expanded] #liveBack svg, body[data-layout=split-cols] #liveBack svg { transform: none; }
.pill { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 700; padding: 3px 9px; border-radius: 99px; background: var(--card); color: var(--muted); }
.pill:empty { display: none; }
#liveMode { background: #7f1d1d; color: #fecaca; }
#stage { position: relative; background: #000; aspect-ratio: 16/9; width: 100%; max-height: 70vh; }
body[data-layout=split-rows] #stage, body[data-layout=split-rows] #live { max-height: none; }
body[data-layout=split-rows] #stage { flex: 1; aspect-ratio: auto; }
body[data-layout=split-rows] #liveExtras { display: none; }
body[data-layout=split-rows] .live-bar { padding-top: calc(6px + var(--sat)); min-height: 44px; }
#stage video, #stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: transparent; }
#livePoster { filter: brightness(.7); }
.live-status { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); background: rgba(0,0,0,.65); padding: 8px 14px; border-radius: 10px; font-size: 13.5px; text-align: center; max-width: 85%; }
.live-ctrls { position: absolute; right: 10px; bottom: 10px; display: flex; gap: 8px; }
.round { width: 40px; height: 40px; border-radius: 50%; background: rgba(0,0,0,.6); display: grid; place-items: center; }
.round svg { width: 20px; height: 20px; }
#muteBtn .x { opacity: 1; } #muteBtn.on .x { opacity: 0; } #muteBtn.on { background: var(--accent-d); }
#liveExtras { flex: 1; min-height: 0; padding: 6px calc(14px + var(--sar)) calc(16px + var(--sab)) calc(14px + var(--sal)); }
.live-sens { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.live-sens .pill svg { width: 14px; height: 14px; }
.pill.hot { background: var(--warn); color: #241400; }
.ctl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 10px; margin-top: 8px; }
.ctl { background: var(--card); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 3px; align-items: flex-start; }
.ctl svg { color: var(--muted); } .ctl small { color: var(--muted); }
.ctl span { font-weight: 600; font-size: 13.5px; }
.ctl.on { background: var(--card2); } .ctl.on svg { color: var(--accent); }

/* ---------- sheet ---------- */
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 40; }
#sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; background: var(--bg2); border-radius: 20px 20px 0 0;
  padding: 8px calc(18px + var(--sar)) calc(18px + var(--sab)) calc(18px + var(--sal)); max-height: 85vh; overflow-y: auto;
  transform: translateY(100%); transition: transform .22s ease; margin: 0 auto; max-width: 560px; }
#sheet.open { transform: none; }
body[data-layout=split-cols] #sheet { right: auto; width: var(--seg0w); }
.grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 2px auto 12px; }
.sheet-head { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.sheet-head h2 { margin: 0; font-size: 20px; } .sheet-head p { margin: 2px 0 0; }
.sheet-head svg { color: var(--accent); }
.slider { display: block; margin: 6px 0 16px; } .slider span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.slider input { width: 100%; height: 34px; accent-color: var(--accent); }
.slider.temp input { accent-color: #ffcf8a; }
.swatches { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.sw { width: 38px; height: 38px; border-radius: 50%; border: 2px solid rgba(255,255,255,.15); }
.tgl { width: 48px; height: 28px; border-radius: 99px; background: var(--line); position: relative; flex: none; }
.tgl::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: left .15s; }
.tgl.on { background: var(--accent); } .tgl.on::after { left: 23px; }

#toast { position: fixed; left: 50%; bottom: calc(84px + var(--sab)); transform: translateX(-50%); z-index: 60; background: #263445; color: var(--text);
  padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.4); max-width: 90vw; text-align: center; }
