:root {
  --bg: #f2f2f7; --card: #ffffff; --card2: #f7f7fa; --text: #111114; --muted: #6b6b75; --line: #e2e2e8;
  --accent: #e8781a; --accent-ink: #fff; --ok: #22a447; --warn: #d97706; --bad: #d62828; --info: #3474e0;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
  --radius: 14px; --safe-top: env(safe-area-inset-top); --safe-bottom: env(safe-area-inset-bottom);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0b0d16; --card: #161a28; --card2: #1d2233; --text: #eef0f6; --muted: #9aa1b5; --line: #262c3f;
  --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
code, .mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .92em; }

/* ---------- Layout ---------- */
.app { min-height: 100%; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 8px; min-height: 52px; }
.topbar h1 { font-size: 17px; margin: 0; flex: 1; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .side { min-width: 72px; display: flex; gap: 4px; align-items: center; }
.topbar .side.right { justify-content: flex-end; }
.main { flex: 1; width: 100%; max-width: 860px; margin: 0 auto; padding: 16px 16px calc(var(--safe-bottom) + 40px); }
.big-title { font-size: 30px; font-weight: 700; margin: 6px 0 14px; }
.footer { text-align: center; color: var(--muted); font-size: 12px; padding: 18px 0 4px; }

/* ---------- Lists ---------- */
.section { margin: 0 0 22px; }
.section > h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; color: var(--muted); margin: 0 0 6px 14px; display: flex; justify-content: space-between; align-items: center; }
.section > .note { font-size: 13px; color: var(--muted); margin: 6px 14px 0; }
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 14px; min-height: 48px; position: relative; width: 100%; background: none; border: 0; text-align: left; }
.row + .row::before { content: ""; position: absolute; top: 0; left: 14px; right: 0; border-top: 1px solid var(--line); }
.row.link { cursor: pointer; }
.row.link:hover { background: var(--card2); }
.row.link::after { content: "›"; color: var(--muted); font-size: 22px; line-height: 1; margin-left: 2px; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .value { color: var(--muted); text-align: right; white-space: nowrap; }
.row.danger .title { color: var(--bad); }
.icon { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; color: #fff; flex: none; font-size: 15px; }
.icon svg { width: 18px; height: 18px; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--bad); } .dot.warn { background: var(--warn); } .dot.off { background: #8a8f9e; opacity: .6; }

/* ---------- Controls ---------- */
.btn { border: 0; border-radius: 10px; padding: 9px 14px; background: var(--card2); color: var(--text); cursor: pointer; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; justify-content: center; }
.btn:hover { filter: brightness(.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { background: color-mix(in srgb, var(--bad) 14%, transparent); color: var(--bad); }
.btn.plain { background: none; color: var(--accent); padding: 6px 8px; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; padding: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }
.grid-btns { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.field { display: flex; align-items: center; gap: 10px; padding: 10px 14px; position: relative; min-height: 48px; }
.field + .field::before, .row + .field::before, .field + .row::before { content: ""; position: absolute; top: 0; left: 14px; right: 0; border-top: 1px solid var(--line); }
.field > label:not(.switch) { width: 38%; max-width: 170px; flex: none; color: var(--text); }
.field input:not([type=checkbox]), .field select, .field textarea { flex: 1; min-width: 0; border: 0; background: transparent; outline: none; padding: 4px 0; text-align: right; }
.field textarea { text-align: left; min-height: 80px; resize: vertical; }
.field.stack { flex-direction: column; align-items: stretch; }
.field.stack > label:not(.switch) { width: auto; max-width: none; color: var(--muted); font-size: 13px; }
.field.stack input, .field.stack textarea { text-align: left; }
input.box, select.box, textarea.box { width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); outline: none; }
input.box:focus { border-color: var(--accent); }
.switch { position: relative; width: 51px; height: 31px; flex: none; margin-left: auto; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: #c8c8d0; border-radius: 31px; transition: .2s; cursor: pointer; }
:root[data-theme="dark"] .switch span { background: #3a3f52; }
.switch span::before { content: ""; position: absolute; width: 27px; height: 27px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 2px 4px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::before { transform: translateX(20px); }
.seg { display: inline-flex; background: var(--card2); border-radius: 10px; padding: 2px; gap: 2px; flex-wrap: wrap; }
.field .seg { flex-wrap: nowrap; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; cursor: pointer; font-weight: 500; font-size: 14px; }
.seg button.on { background: var(--card); box-shadow: var(--shadow); }
.tabs { display: flex; gap: 6px; overflow-x: auto; margin: 0 0 16px; padding-bottom: 2px; }
.tabs button { border: 0; border-radius: 999px; padding: 7px 14px; background: var(--card); box-shadow: var(--shadow); cursor: pointer; white-space: nowrap; font-weight: 500; }
.tabs button.on { background: var(--accent); color: var(--accent-ink); }
.search { width: 100%; padding: 10px 12px; border-radius: 10px; border: 0; background: color-mix(in srgb, var(--muted) 14%, transparent); margin-bottom: 12px; outline: none; }

/* ---------- Bits ---------- */
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--card2); color: var(--muted); vertical-align: middle; }
.badge.ok { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); }
.badge.bad { background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--bad); }
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.info { background: color-mix(in srgb, var(--info) 16%, transparent); color: var(--info); }
.bar { height: 6px; border-radius: 3px; background: var(--card2); overflow: hidden; margin-top: 5px; }
.bar > i { display: block; height: 100%; background: var(--ok); border-radius: 3px; }
.bar > i.warn { background: var(--warn); } .bar > i.bad { background: var(--bad); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-bottom: 22px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat .k { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.stat .v { font-size: 20px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .s { font-size: 12px; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 36px 16px; }
.empty .em { font-size: 40px; margin-bottom: 8px; }
.error-box { background: color-mix(in srgb, var(--bad) 10%, var(--card)); color: var(--bad); padding: 12px 14px; border-radius: var(--radius); margin-bottom: 16px; }
.info-box { background: color-mix(in srgb, var(--info) 10%, var(--card)); padding: 12px 14px; border-radius: var(--radius); margin-bottom: 16px; font-size: 14px; }
.spinner { width: 22px; height: 22px; border: 2.5px solid color-mix(in srgb, var(--muted) 30%, transparent); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
.center { display: grid; place-items: center; padding: 40px; }
@keyframes spin { to { transform: rotate(360deg); } }
.muted { color: var(--muted); } .small { font-size: 13px; } .right { text-align: right; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; padding: 12px 14px; }
.kv > :nth-child(odd) { color: var(--muted); }
.kv > :nth-child(even) { text-align: right; word-break: break-all; }

/* ---------- Sheets & toasts ---------- */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
@media (min-width: 700px) { .sheet-bg { align-items: center; } }
.sheet { background: var(--bg); width: 100%; max-width: 620px; max-height: 92vh; overflow: auto; border-radius: 16px 16px 0 0; padding: 0 0 calc(var(--safe-bottom) + 16px); animation: up .2s; }
@media (min-width: 700px) { .sheet { border-radius: 16px; } }
.sheet-head { position: sticky; top: 0; background: var(--bg); display: flex; align-items: center; padding: 12px 10px; border-bottom: 1px solid var(--line); z-index: 2; }
.sheet-head h3 { flex: 1; text-align: center; margin: 0; font-size: 16px; }
.sheet-head .side { min-width: 80px; display: flex; } .sheet-head .side.right { justify-content: flex-end; }
.sheet-body { padding: 16px; }
@keyframes fade { from { opacity: 0; } } @keyframes up { from { transform: translateY(30px); opacity: 0; } }
.toasts { position: fixed; left: 0; right: 0; bottom: calc(var(--safe-bottom) + 16px); display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 80; pointer-events: none; }
.toast { background: #1d2233; color: #fff; padding: 10px 16px; border-radius: 12px; max-width: 90vw; box-shadow: 0 6px 24px rgba(0,0,0,.25); animation: up .2s; pointer-events: auto; }
.toast.bad { background: var(--bad); } .toast.ok { background: #1f7a3a; }
.menu { position: absolute; right: 8px; top: 100%; background: var(--card); border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.2); min-width: 200px; z-index: 30; overflow: hidden; }
.menu button { display: block; width: 100%; text-align: left; padding: 11px 14px; border: 0; background: none; cursor: pointer; }
.menu button:hover { background: var(--card2); } .menu button.danger { color: var(--bad); }

/* ---------- Auth ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(ellipse at top, #1b2139, #0b0d16 70%); color: #eef0f6; }
.auth .card { width: 100%; max-width: 380px; background: #161a28; border-radius: 20px; padding: 28px 24px; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.auth h1 { margin: 0 0 4px; text-align: center; font-size: 26px; } .auth p { color: #9aa1b5; text-align: center; margin: 0 0 20px; font-size: 14px; }
.auth input.box { background: #0f1220; border-color: #2a3047; color: #eef0f6; margin-bottom: 10px; }
.auth .logo { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 14px; image-rendering: pixelated; }
.auth .qr { background: #fff; padding: 10px; border-radius: 12px; display: block; margin: 0 auto 12px; width: 200px; height: 200px; }
.auth .secret { text-align: center; font-family: ui-monospace, Menlo, monospace; font-size: 13px; color: #c5cbe0; word-break: break-all; margin-bottom: 14px; user-select: all; }
.auth label.check { display: flex; gap: 8px; align-items: center; font-size: 14px; color: #9aa1b5; margin: 4px 0 14px; }
.code-input { letter-spacing: .4em; text-align: center; font-size: 22px !important; font-family: ui-monospace, Menlo, monospace; }

/* ---------- Console ---------- */
.console-page { position: fixed; inset: 0; background: #000; display: flex; flex-direction: column; z-index: 40; }
.console-bar { display: flex; align-items: center; gap: 8px; padding: calc(var(--safe-top) + 6px) 10px 6px; background: #111; color: #ddd; }
.console-bar .t { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.console-bar .btn { background: #222; color: #ddd; }
.console-screen { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.console-screen .xterm { height: 100%; padding: 4px; }
.console-screen .vnc { position: absolute; inset: 0; }
.console-status { position: absolute; inset: 0; display: grid; place-items: center; color: #bbb; text-align: center; padding: 20px; pointer-events: none; }
.keys { display: flex; gap: 6px; overflow-x: auto; padding: 6px 8px calc(var(--safe-bottom) + 6px); background: #111; }
.keys button { flex: none; background: #262626; color: #eee; border: 0; border-radius: 7px; padding: 8px 11px; font: 13px ui-monospace, Menlo, monospace; cursor: pointer; }
.keys button.on { background: var(--accent); color: #fff; }

/* ---------- Cameras ---------- */
.cams { display: grid; gap: 12px; grid-template-columns: 1fr; }
.cams.two { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.cam { background: #000; border-radius: var(--radius); overflow: hidden; position: relative; aspect-ratio: 16/9; }
.cam img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cam .label { position: absolute; left: 10px; top: 8px; color: #fff; font-weight: 600; text-shadow: 0 1px 3px rgba(0,0,0,.8); display: flex; gap: 6px; align-items: center; }
.cam .err { position: absolute; inset: 0; display: grid; place-items: center; color: #ddd; text-align: center; padding: 16px; font-size: 14px; }

/* ---------- Splash ---------- */
.splash { position: fixed; inset: 0; z-index: 100; background: #000; display: grid; place-items: center; transition: opacity .45s; cursor: pointer; }
.splash canvas { width: min(100vw, 160vh); max-width: 100vw; image-rendering: pixelated; image-rendering: crisp-edges; }
.splash.out { opacity: 0; pointer-events: none; }
.cam { cursor: pointer; }
.cam video { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.cam:fullscreen, .cam:-webkit-full-screen { border-radius: 0; aspect-ratio: auto; }
