:root {
  color-scheme: dark;
  --bg: #090c11;
  --surface-1: #0e131b;
  --surface-2: #141b25;
  --surface-hover: #1a2330;
  --border: #222c39;
  --border-strong: #344153;
  --text-primary: #f2f4f7;
  --text-secondary: #a9b4c3;
  --text-muted: #748195;
  --accent: #78aaf0;
  --accent-soft: #162944;
  --success: #3ac7a1;
  --success-soft: #102d28;
  --warning: #d5a84d;
  --danger: #e46f7b;
  --danger-soft: #321a21;
  --radius-control: 7px;
  --radius-card: 10px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --header-height: 64px;
  --content-width: 1480px;
  --panel: var(--surface-1);
  --line: var(--border);
  --text: var(--text-primary);
  --muted: var(--text-secondary);
}

* { box-sizing: border-box; }
html { width: 100%; max-width: 100%; overflow-x: clip; background: var(--bg); scroll-behavior: smooth; }
body {
  width: 100%;
  max-width: 100%;
  overflow-x: clip;
  margin: 0;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg);
  color: var(--text-primary);
  font: 400 14px/1.45 Inter, Geist, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
body.no-scroll { overflow: hidden; }
button, input, select { font: inherit; }
button, .button, select, input {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  background: var(--surface-2);
  color: var(--text-primary);
  padding: 8px 12px;
}
button, .button { cursor: pointer; text-decoration: none; transition: border-color .16s ease, background .16s ease, color .16s ease, opacity .16s ease; }
button:hover, .button:hover { border-color: var(--border-strong); background: var(--surface-hover); }
button:disabled { cursor: not-allowed; opacity: .42; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
select, input { width: 100%; }
select { color-scheme: dark; }
.primary { border-color: #4b7ebb; background: #37689f; color: #fff; font-weight: 650; }
.primary:hover { border-color: #6096d3; background: #4279b6; }
.danger { border-color: #63303a; background: transparent; color: #f49aa4; }
.danger:hover { border-color: #8d4653; background: var(--danger-soft); }
.button-ghost { background: transparent; color: var(--text-secondary); }
.icon-button { display: grid; place-items: center; width: 36px; min-height: 36px; padding: 0; background: transparent; font-size: 22px; }

h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(24px, 3vw, 28px); line-height: 1.15; letter-spacing: -.025em; }
h2 { font-size: 20px; line-height: 1.25; letter-spacing: -.015em; }
h3 { font-size: 16px; line-height: 1.3; }
p, time, .status { color: var(--text-secondary); }
.eyebrow { display: block; margin-bottom: 5px; color: var(--text-muted); font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.empty { grid-column: 1 / -1; display: grid; gap: 5px; min-height: 120px; place-content: center; padding: var(--space-5); color: var(--text-muted); text-align: center; }
.empty strong { color: var(--text-secondary); font-size: 16px; }

.app-header { position: sticky; z-index: 50; top: 0; height: auto; min-height: var(--header-height); border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 96%, transparent); backdrop-filter: blur(14px); }
.header-inner, main { width: min(100%, var(--content-width)); max-width: 100%; min-width: 0; margin-inline: auto; padding-inline: clamp(16px, 3vw, 32px); }
.header-inner { min-height: var(--header-height); display: flex; align-items: center; gap: var(--space-4); padding-top: max(0px, env(safe-area-inset-top)); }
.brand { display: inline-flex; align-items: center; gap: 10px; min-width: 0; color: var(--text-primary); text-decoration: none; }
.brand > span:last-child { display: grid; min-width: 0; }
.brand strong { font-size: 16px; letter-spacing: -.01em; white-space: nowrap; }
.brand small { color: var(--text-muted); font-size: 11px; }
.brand-icon { display: block; flex: 0 0 auto; width: 30px; height: 30px; }
.header-tools { display: flex; flex: 0 0 auto; align-items: center; justify-content: flex-end; gap: 8px; margin-left: auto; }
.system-status { display: inline-flex; align-items: center; gap: 7px; min-height: 32px; padding: 5px 9px; border: 1px solid var(--border); border-radius: 999px; color: var(--text-secondary); font-size: 12px; white-space: nowrap; }
.system-status i { width: 6px; height: 6px; border-radius: 50%; background: var(--warning); box-shadow: 0 0 0 3px #d5a84d18; }
.system-status.online i { background: var(--success); box-shadow: 0 0 0 3px #3ac7a118; }
.system-status.error i { background: var(--danger); box-shadow: 0 0 0 3px #e46f7b18; }
.refresh-button { display: inline-flex; align-items: center; gap: 7px; min-height: 34px; padding: 6px 10px; }
.refresh-button svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.app-menu { position: relative; }
.app-menu summary { display: grid; place-items: center; width: 38px; height: 34px; border: 1px solid var(--border); border-radius: var(--radius-control); color: var(--text-secondary); cursor: pointer; list-style: none; }
.app-menu summary::-webkit-details-marker { display: none; }
.app-menu[open] summary, .app-menu summary:hover { border-color: var(--border-strong); background: var(--surface-hover); color: var(--text-primary); }
.menu-panel { position: absolute; z-index: 60; top: calc(100% + 8px); right: 0; width: 220px; padding: 6px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-2); box-shadow: 0 14px 32px #0008; }
.menu-panel button { width: 100%; border-color: transparent; background: transparent; color: var(--text-secondary); text-align: left; }
.menu-panel button:hover { background: var(--surface-hover); color: var(--text-primary); }
.menu-panel form { margin: 0; }
.menu-separator { height: 1px; margin: 5px 3px; background: var(--border); }

.primary-nav { align-self: stretch; display: flex; min-width: 0; }
.nav-inner { display: flex; align-items: stretch; gap: 2px; min-width: 0; margin: 0; padding: 0; overflow: visible; }
.nav-inner::-webkit-scrollbar { display: none; }
.tab { position: relative; min-height: var(--header-height); padding: 8px 12px; border: 0; border-radius: 0; background: transparent; color: var(--text-muted); white-space: nowrap; }
.tab:hover { background: transparent; color: var(--text-secondary); }
.tab::after { content: ""; position: absolute; right: 10px; bottom: -1px; left: 10px; height: 2px; border-radius: 2px 2px 0 0; background: transparent; }
.tab.active { color: var(--text-primary); }
.tab.active::after { background: var(--accent); }
.mobile-nav-head, .mobile-nav-actions, .mobile-menu-button, .nav-backdrop { display: none; }

main { padding-top: var(--space-5); padding-bottom: 52px; }
.view { display: none; }
.view.active { display: block; animation: view-in .15s ease-out; }
@keyframes view-in { from { opacity: .6; transform: translateY(2px); } }
.section-title, .page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--space-4); margin-bottom: var(--space-4); }
.section-title > div, .page-heading > div:first-child { display: grid; gap: 5px; }
.section-title h1, .page-heading h1 { font-size: 24px; }
.section-title p, .page-heading p { color: var(--text-muted); }
.heading-with-action { align-items: center; }
.actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }

.camera-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.monitor { position: relative; min-width: 0; overflow: hidden; border: 1px solid var(--border); border-radius: var(--radius-card); background: #05070a; cursor: pointer; isolation: isolate; }
.monitor:hover { border-color: var(--border-strong); }
.monitor:focus-within { border-color: var(--accent); }
.monitor img { display: block; width: 100%; aspect-ratio: 16 / 9; object-fit: contain; background: #030405; }
.mobile-open-hint, .mobile-play { display: none; }
.monitor-head { position: absolute; z-index: 3; inset: 0 0 auto; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 28px 12px 20px; pointer-events: none; background: linear-gradient(to bottom, #030405d9, transparent); }
.monitor-head strong { max-width: 70%; overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.monitor .status { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); font-size: 11px; white-space: nowrap; }
.monitor .status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--warning); }
.monitor .status.online { color: #bdebdc; }
.monitor .status.online::before { background: var(--success); }
.tap-hint { position: absolute; z-index: 3; right: 10px; bottom: 9px; padding: 4px 7px; border-radius: 5px; background: #05070ab8; color: #d9e0e8; font-size: 11px; opacity: 0; transition: opacity .16s ease; }
.monitor:hover .tap-hint, .monitor:focus-within .tap-hint { opacity: 1; }
.monitor-ptz { position: absolute; z-index: 4; left: 10px; bottom: 9px; display: flex; gap: 3px; padding: 3px; border: 1px solid #ffffff1f; border-radius: 8px; background: #05070acc; opacity: .25; transition: opacity .16s ease; }
.monitor:hover .monitor-ptz, .monitor:focus-within .monitor-ptz { opacity: 1; }
.monitor-ptz button { width: 30px; min-height: 28px; padding: 0; border-color: transparent; background: transparent; color: #e8edf3; }
.monitor-ptz button:hover { background: #ffffff14; }
.monitor-ptz .ptz-stop { color: var(--danger); font-size: 10px; }
.status.online, .online { color: var(--success); }

.camera-list, .device-list { display: grid; gap: 7px; }
.camera-row, .device-row { display: grid; grid-template-columns: minmax(210px, 1.3fr) minmax(170px, 1fr) minmax(110px, auto) auto; align-items: center; gap: 16px; min-height: 68px; padding: 11px 12px 11px 16px; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-1); }
.camera-row:hover, .device-row:hover { background: #101720; }
.camera-name { display: grid; gap: 3px; min-width: 0; }
.camera-name strong { overflow: hidden; font-size: 15px; text-overflow: ellipsis; white-space: nowrap; }
.camera-name small, .camera-address, .device-meta { color: var(--text-muted); font-size: 12px; }
.camera-address { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.state-indicator { display: inline-flex; align-items: center; gap: 7px; color: var(--text-secondary); font-size: 12px; white-space: nowrap; }
.state-indicator::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); }
.state-indicator.online::before { background: var(--success); box-shadow: 0 0 0 3px #3ac7a112; }
.row-menu { position: relative; justify-self: end; }
.row-menu summary { display: grid; place-items: center; width: 38px; height: 34px; border: 1px solid transparent; border-radius: 7px; color: var(--text-secondary); cursor: pointer; list-style: none; }
.row-menu summary::-webkit-details-marker { display: none; }
.row-menu summary:hover, .row-menu[open] summary { border-color: var(--border); background: var(--surface-hover); color: var(--text-primary); }
.row-menu .menu-panel { top: 38px; width: 184px; }
.row-menu .menu-panel .danger-action { color: #f49aa4; }
.settings-heading { align-items: center; }
.camera-global-actions .primary { order: -1; }
.discovered { align-items: center; margin-top: var(--space-6); padding-top: var(--space-5); border-top: 1px solid var(--border); }
.discovered h2 { font-size: 18px; }
.discovery-meta { display: flex; align-items: center; gap: 10px; color: var(--text-muted); font-size: 12px; }
.device-row { grid-template-columns: minmax(170px, .8fr) minmax(250px, 1.5fr) auto; }

dialog { width: min(460px, calc(100vw - 32px)); max-width: none; padding: 0; border: 1px solid var(--border-strong); border-radius: var(--radius-card); background: var(--surface-1); color: var(--text-primary); box-shadow: 0 20px 60px #000a; }
dialog::backdrop { background: #030405c7; backdrop-filter: blur(3px); }
.form-dialog form { display: grid; gap: 15px; padding: 20px; }
.dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.dialog-heading h2 { font-size: 20px; }
.form-dialog label { display: grid; gap: 6px; color: var(--text-secondary); font-size: 13px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.dialog-actions { justify-content: flex-end; padding-top: 5px; }

.immersive { position: fixed; z-index: 9999; inset: 0; display: none; align-items: center; justify-content: center; background: #000; }
.immersive.open { display: flex; }
.immersive img { width: 100%; height: 100%; object-fit: contain; }
.immersive-heading { position: absolute; z-index: 3; top: max(14px, env(safe-area-inset-top)); left: max(14px, env(safe-area-inset-left)); display: flex; align-items: center; gap: 8px; padding: 6px 9px; border: 1px solid #ffffff1f; border-radius: 6px; background: #05070ab8; }
.immersive-heading strong { font-size: 13px; }
.immersive-heading span { color: #aab5c2; font-size: 11px; }
.immersive-close { position: absolute; z-index: 5; top: max(10px, env(safe-area-inset-top)); right: max(10px, env(safe-area-inset-right)); width: 40px; min-height: 40px; padding: 0; border-color: #ffffff25; border-radius: 50%; background: #05070ab8; font-size: 25px; }
.immersive-arrow { position: absolute; z-index: 5; top: 50%; width: 48px; min-height: 64px; padding: 0; border-color: #ffffff24; background: #05070ab8; font-size: 40px; line-height: 1; transform: translateY(-50%); }
.immersive-arrow.previous { left: max(10px, env(safe-area-inset-left)); }
.immersive-arrow.next { right: max(10px, env(safe-area-inset-right)); }
.immersive-arrow:active { background: #ffffff24; }
.immersive-arrow:disabled { display: none; }
.ptz-controls { position: absolute; z-index: 4; right: max(14px, env(safe-area-inset-right)); bottom: max(14px, env(safe-area-inset-bottom)); display: grid; justify-items: center; gap: 8px; width: 174px; height: 170px; padding: 10px; border: 1px solid #ffffff24; border-radius: 10px; background: #05070ac7; box-sizing: border-box; }
.ptz-pad { display: grid; grid-template-columns: repeat(3, 40px); gap: 3px; }
.ptz-pad button { min-height: 36px; padding: 0; border-color: #ffffff1a; background: #ffffff0c; }
.ptz-pad button:active { background: #ffffff26; }
.ptz-pad .ptz-stop { color: var(--danger); font-size: 10px; }
.ptz-controls small { display: grid; place-items: center; width: 100%; min-height: 24px; color: #bcc6d3; font-size: 10px; line-height: 1.2; text-align: center; }
.install-help { position: fixed; z-index: 10000; right: 16px; bottom: max(16px, env(safe-area-inset-bottom)); left: 16px; width: min(100% - 32px, 520px); margin: auto; padding: 14px 16px; border: 1px solid var(--border-strong); border-radius: 9px; background: var(--surface-2); box-shadow: 0 14px 36px #000b; color: var(--text-secondary); text-align: center; }

@media (max-width: 1120px) {
  .camera-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 900px) {
  body.mobile-nav-open { overflow: hidden; }
  .primary-nav {
    position: fixed;
    z-index: 80;
    inset: 0 auto 0 0;
    display: flex;
    flex-direction: column;
    width: min(86vw, 304px);
    max-width: calc(100vw - 36px);
    height: 100dvh;
    padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
    border-right: 1px solid var(--border-strong);
    background: var(--surface-1);
    box-shadow: 18px 0 48px #0009;
    transform: translateX(-105%);
    visibility: hidden;
    transition: transform .2s ease, visibility .2s;
  }
  body.mobile-nav-open .primary-nav { transform: translateX(0); visibility: visible; }
  .mobile-nav-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; padding: 0 3px 10px 8px; border-bottom: 1px solid var(--border); }
  .mobile-nav-head > span { display: flex; align-items: center; gap: 9px; }
  .mobile-nav-head strong { font-size: 15px; }
  .mobile-nav-head .brand-icon { width: 28px; height: 28px; }
  .primary-nav .nav-inner { flex-direction: column; align-items: stretch; gap: 4px; padding: 12px 0; }
  .primary-nav .tab { min-height: 44px; padding: 10px 13px; border: 1px solid transparent; border-radius: 7px; text-align: left; }
  .primary-nav .tab:hover { background: var(--surface-hover); }
  .primary-nav .tab.active { border-color: var(--border); background: var(--accent-soft); color: var(--text-primary); }
  .primary-nav .tab::after { top: 9px; right: auto; bottom: 9px; left: -1px; width: 2px; height: auto; border-radius: 0 2px 2px 0; }
  .mobile-nav-actions { display: grid; gap: 5px; margin-top: auto; padding-top: 12px; border-top: 1px solid var(--border); }
  .mobile-nav-actions button { width: 100%; min-height: 40px; border-color: transparent; background: transparent; color: var(--text-secondary); text-align: left; }
  .mobile-nav-actions button:hover { background: var(--surface-hover); color: var(--text-primary); }
  .mobile-nav-actions form { margin: 0; }
  .mobile-menu-button { display: grid; place-items: center; width: 38px; min-height: 34px; padding: 0; background: transparent; }
  .mobile-menu-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
  .refresh-button, .app-menu { display: none; }
  .nav-backdrop { position: fixed; z-index: 49; inset: 0; width: 100%; height: 100dvh; min-height: 0; padding: 0; border: 0; border-radius: 0; background: #030405b8; opacity: 0; pointer-events: none; transition: opacity .2s ease; }
  body.mobile-nav-open .nav-backdrop { display: block; opacity: 1; pointer-events: auto; }
}
@media (max-width: 760px) {
  :root { --header-height: 58px; }
  .header-inner, main { padding-inline: 14px; }
  .brand small { display: none; }
  .system-status { max-width: 120px; overflow: hidden; text-overflow: ellipsis; }
  main { padding-top: 18px; }
  .section-title, .page-heading, .settings-heading { align-items: flex-start; flex-direction: column; }
  .camera-global-actions { width: 100%; }
  .camera-global-actions button { flex: 1 1 auto; }
  .camera-row { grid-template-columns: minmax(0, 1fr) auto auto; gap: 10px; }
  .camera-address { display: none; }
  .device-row { grid-template-columns: 1fr auto; }
  .device-meta { grid-column: 1 / -1; grid-row: 2; }
  .discovered { align-items: flex-start; }
  .discovery-meta { align-items: flex-start; flex-direction: column; }
  .form-row { grid-template-columns: 1fr; }
}
@media (max-width: 620px) {
  .camera-grid { grid-template-columns: 1fr; gap: 8px; }
  .monitor.mobile-lite { min-height: 0; background: #030405; }
  .monitor.mobile-lite img { display: block; }
  .monitor.mobile-lite .monitor-head { position: absolute; padding: 24px 12px 20px; background: linear-gradient(to bottom, #030405e8, transparent); }
  .monitor.mobile-lite .mobile-play { position: absolute; z-index: 4; top: 50%; left: 50%; display: grid; place-items: center; width: 54px; height: 54px; padding-left: 3px; border: 1px solid #ffffff5c; border-radius: 50%; background: #07101ed9; color: #fff; font-size: 20px; box-shadow: 0 8px 24px #0008; transform: translate(-50%, -50%); }
  .monitor.mobile-lite .mobile-open-hint { position: absolute; z-index: 3; right: 10px; bottom: 9px; display: block; padding: 4px 7px; border-radius: 5px; background: #05070acc; color: #e2e8ef; font-size: 11px; }
  .monitor.mobile-lite .monitor-ptz { display: none; }
  .section-title h1, .page-heading h1 { font-size: 22px; }
  .tap-hint { display: none; }
  .monitor-ptz { opacity: .8; }
  .system-status { min-width: 0; max-width: 34px; width: 34px; justify-content: center; padding-inline: 0; font-size: 0; }
}
@media (max-width: 420px) {
  .brand strong { font-size: 14px; }
  .header-tools { gap: 5px; }
  .camera-row { grid-template-columns: minmax(0, 1fr) auto; }
  .camera-row .state-indicator { grid-column: 1; }
  .row-menu { grid-column: 2; grid-row: 1 / span 2; }
}
@media (hover: none) { .monitor-ptz { opacity: .82; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; } }
@media (display-mode: standalone) { .app-header { padding-top: env(safe-area-inset-top); } }
@media (orientation: landscape) and (max-height: 600px) { .immersive-heading strong { font-size: 11px; }.immersive-close { width: 36px; min-height: 36px; }.immersive-arrow { width: 42px; min-height: 54px; }.ptz-controls { transform: scale(.82); transform-origin: bottom right; } }
