:root {
  --bg: #0d1117;
  --panel: #151b23;
  --panel2: #1c2430;
  --line: #2a3441;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #3b82f6;
  --accent2: #2563eb;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --rec: #10b981;
  --sel: rgba(59,130,246,.28);
  --radius: 8px;
  --top: 52px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", sans-serif; }
body { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.spacer { flex: 1; }

/* buttons & inputs */
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; cursor: pointer;
  white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; min-height: 32px; }
.btn:hover { border-color: #3b4757; background: #222c3a; }
.btn.primary { background: var(--accent2); border-color: var(--accent2); color: #fff; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { color: #fca5a5; }
.btn.danger:hover { background: #3a1d1d; border-color: #7f1d1d; }
.btn.sm { padding: 3px 8px; min-height: 26px; font-size: 12px; }
.btn.icon { padding: 4px 8px; }
.btn.on { background: var(--accent2); border-color: var(--accent2); color: #fff; }
.btn:disabled { opacity: .45; cursor: default; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=time], input[type=datetime-local], select, textarea {
  background: #0b0f15; border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; min-height: 32px; outline: none; width: 100%; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { min-height: 120px; font-family: Consolas, monospace; font-size: 12px; }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); vertical-align: middle; }
label.chk { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }

/* login */
.login-wrap { display: grid; place-items: center; height: 100vh; padding: 16px; }
.login { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; }
.login h1 { margin: 0 0 4px; font-size: 20px; }
.login p { margin: 0 0 20px; }
.login .f { margin-bottom: 12px; }
.login .btn { width: 100%; justify-content: center; min-height: 40px; }
.err { color: #fca5a5; min-height: 20px; margin: 8px 0; font-size: 13px; }

/* layout */
.top { height: var(--top); display: flex; align-items: center; gap: 6px; padding: 0 12px; background: var(--panel);
  border-bottom: 1px solid var(--line); padding-top: env(safe-area-inset-top); }
.brand { font-weight: 700; letter-spacing: .3px; margin-right: 12px; white-space: nowrap; display: flex; align-items: center; gap: 8px; }
.brand img { width: 26px; height: 26px; border-radius: 6px; }
.nav { display: flex; gap: 2px; }
.nav button { background: none; border: none; padding: 8px 14px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.nav button.active { color: var(--text); background: var(--panel2); }
.nav button:hover { color: var(--text); }
.user-btn { background: none; border: 1px solid transparent; padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.user-btn:hover { border-color: var(--line); }
.menu { position: absolute; right: 8px; top: calc(var(--top) + 4px); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px; z-index: 50; min-width: 180px; box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.menu button { display: block; width: 100%; text-align: left; background: none; border: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: #2a3441; }
.main { display: flex; height: calc(100vh - var(--top)); height: calc(100dvh - var(--top)); }
.side { width: 250px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side .search { padding: 8px; border-bottom: 1px solid var(--line); }
.tree { overflow: auto; flex: 1; padding: 4px 0 20px; }
.tree .grp { padding: 7px 10px; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 6px; color: #c9d4e0; user-select: none; }
.tree .grp:hover { background: #1b232e; }
.tree .grp .cnt { margin-left: auto; color: var(--muted); font-weight: 400; font-size: 12px; }
.tree .grp .play-all { font-size: 11px; padding: 1px 6px; min-height: 0; }
.tree .cam { padding: 6px 10px 6px 26px; cursor: pointer; display: flex; align-items: center; gap: 8px; user-select: none; }
.tree .cam:hover { background: #1b232e; }
.tree .cam.active { background: #1e3a5f; }
.tree .cam .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.dot.online { background: var(--ok); }
.dot.connecting { background: var(--warn); }
.dot.error { background: var(--err); }
.dot.idle { background: #64748b; }
.recmark { font-size: 10px; color: var(--rec); border: 1px solid #065f46; border-radius: 3px; padding: 0 3px; }
.content { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.toolbar { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }

/* live grid */
.grid { flex: 1; display: grid; gap: 3px; padding: 3px; background: #000; min-height: 0; }
.tile { position: relative; background: #05070a; overflow: hidden; border: 2px solid transparent; min-height: 0; min-width: 0; }
.tile.sel { border-color: var(--accent); }
.tile.drop { border-color: #facc15; box-shadow: inset 0 0 0 2px #facc15; }
.tile[draggable="true"] { cursor: grab; }
.side .cam[draggable="true"] { cursor: grab; }
.tile video { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.tile.fill video { object-fit: fill; }
.tile .bar { position: absolute; left: 0; right: 0; top: 0; display: flex; align-items: center; gap: 6px; padding: 4px 6px;
  background: linear-gradient(rgba(0,0,0,.75), rgba(0,0,0,0)); font-size: 12px; opacity: .95; }
.tile .bar .nm { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; text-shadow: 0 1px 2px #000; }
.tile .bar .acts { display: none; gap: 4px; }
.tile:hover .bar .acts, .tile.sel .bar .acts { display: flex; }
.tile .bar .acts button { background: rgba(0,0,0,.55); border: 1px solid rgba(255,255,255,.2); border-radius: 4px; color: #fff; cursor: pointer; padding: 1px 6px; font-size: 12px; }
.tile .msg { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 13px; pointer-events: none; text-align: center; padding: 8px; }
.tile .empty { color: #3b4757; }
.grid.portrait { overflow: auto; align-content: start; grid-auto-rows: min-content; }
.grid.portrait .tile { aspect-ratio: 16 / 9; }
.tile .q { font-size: 10px; border: 1px solid rgba(255,255,255,.3); border-radius: 3px; padding: 0 3px; }

/* playback */
.pb { flex: 1; display: flex; flex-direction: column; min-height: 0; background: #000; }
.pb-video { flex: 1; position: relative; min-height: 0; display: grid; place-items: center; }
.pb-video video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.pb-video .msg { position: absolute; color: var(--muted); }
.pb-clock { position: absolute; left: 10px; top: 8px; background: rgba(0,0,0,.6); padding: 3px 8px; border-radius: 4px; font-variant-numeric: tabular-nums; font-size: 15px; }
.pb-controls { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 10px; display: flex; flex-direction: column; gap: 8px; }
.timeline { width: 100%; height: 64px; display: block; cursor: pointer; touch-action: none; border-radius: 4px; background: #0b0f15; }
.pb-sel { font-variant-numeric: tabular-nums; }

/* admin */
.admin { flex: 1; overflow: auto; padding: 16px; }
.admin h2 { margin: 0 0 12px; font-size: 18px; }
.subnav { display: flex; gap: 4px; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--panel); overflow-x: auto; }
.subnav button { background: none; border: 1px solid transparent; padding: 6px 12px; border-radius: 6px; cursor: pointer; color: var(--muted); white-space: nowrap; }
.subnav button.active { color: var(--text); border-color: var(--line); background: var(--panel2); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; }
table.t { width: 100%; border-collapse: collapse; font-size: 13px; }
table.t th, table.t td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.t th { color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--panel); z-index: 1; }
table.t tr:hover td { background: #18202a; }
table.t td.c, table.t th.c { text-align: center; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: #253042; color: #c3cfdc; margin: 1px 2px 1px 0; }
.tag.admin { background: #3b2a12; color: #fcd34d; }
.tag.off { background: #3a1d1d; color: #fca5a5; }
.errtxt { color: #fca5a5; font-size: 12px; max-width: 420px; }
.form { display: grid; grid-template-columns: 140px 1fr; gap: 10px 12px; align-items: center; }
.form .full { grid-column: 1 / -1; }
.form label.l { color: var(--muted); }
.hint { color: var(--muted); font-size: 12px; margin-top: 3px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; margin-bottom: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.stat .v { font-size: 22px; font-weight: 600; margin-top: 2px; }
.gauge { height: 8px; background: #0b0f15; border-radius: 4px; overflow: hidden; margin-top: 8px; }
.gauge > div { height: 100%; background: var(--accent); }
.perm-table { max-height: 420px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; }
.perm-table td.g { color: var(--muted); font-size: 12px; background: #11161d; }
.probe-img { max-width: 100%; border-radius: 6px; margin-top: 8px; border: 1px solid var(--line); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 100; padding: 12px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: 100%; max-width: 640px; max-height: calc(100vh - 24px);
  display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.5); }
.modal.wide { max-width: 860px; }
.modal header { padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 600; display: flex; align-items: center; }
.modal header .x { margin-left: auto; background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal .body { padding: 16px; overflow: auto; }
.modal footer { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 200; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
#toast div { background: #263142; border: 1px solid var(--line); padding: 8px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
#toast div.e { background: #4a1f1f; border-color: #7f1d1d; }

.side-toggle { display: none; }
.side-backdrop { display: none; }

@media (max-width: 860px) {
  :root { --top: 50px; }
  .brand span { display: none; }
  .nav button { padding: 8px 10px; }
  .side { position: fixed; left: 0; top: var(--top); bottom: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; width: 82vw; max-width: 320px; }
  .side.open { transform: none; box-shadow: 10px 0 30px rgba(0,0,0,.5); }
  .side-backdrop.open { display: block; position: fixed; inset: var(--top) 0 0 0; background: rgba(0,0,0,.4); z-index: 39; }
  .side-toggle { display: inline-flex; }
  .form { grid-template-columns: 1fr; gap: 4px 0; }
  .form label.l { margin-top: 8px; }
  .admin { padding: 10px; }
  .hide-m { display: none !important; }
  .tile .bar .acts { display: none; }
  .tile.sel .bar .acts { display: flex; }
  [data-layout="16"], [data-layout="25"], [data-layout="36"] { display: none; }
  .timeline { height: 72px; }
}

/* calendar */
.date-btn { font-variant-numeric: tabular-nums; }
.cal-pop { position: fixed; z-index: 120; width: 290px; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 10px;
  box-shadow: 0 12px 40px rgba(0,0,0,.5); }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; text-align: center; }
.cal-w { font-size: 11px; color: var(--muted); padding: 2px 0; }
.cal-d { background: #0b0f15; border: 1px solid transparent; border-radius: 6px; height: 34px; cursor: pointer; color: var(--muted); position: relative; }
.cal-d:hover:not(:disabled) { border-color: var(--accent); }
.cal-d:disabled { opacity: .3; cursor: default; }
.cal-d.rec { color: var(--text); font-weight: 600; background: #10392d; }
.cal-d.rec::after { content: ''; position: absolute; left: 50%; bottom: 4px; width: 4px; height: 4px; margin-left: -2px; border-radius: 50%; background: var(--rec); }
.cal-d.today { border-color: #f59e0b; }
.cal-d.sel { background: var(--accent2); color: #fff; }
.sun { color: #f87171 !important; }
.cal-d.sel.sun { color: #fff !important; }
.cal-foot { display: flex; align-items: center; gap: 6px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.cal-legend { width: 12px; height: 12px; border-radius: 3px; background: #10392d; border: 1px solid var(--rec); }
input[type=date], input[type=time] { color-scheme: dark; }

.tl-legend { display: flex; gap: 6px; align-items: center; margin-top: -4px; }
.tl-legend .lg { display: inline-block; width: 12px; height: 8px; border-radius: 2px; margin-left: 8px; }

.search { display: flex; flex-direction: column; gap: 6px; }
.sort-sel { min-height: 28px; padding: 3px 8px; font-size: 12px; }
.tree .cam .srv { font-size: 10px; color: var(--muted); margin-left: 6px; border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; }
.tile .info { position: absolute; left: 6px; bottom: 6px; font-size: 11px; background: rgba(0,0,0,.55); color: #d1fae5; padding: 1px 6px; border-radius: 4px; pointer-events: none; font-variant-numeric: tabular-nums; }
.tile .info:empty { display: none; }
.tile .info.warn { color: #fcd34d; }
code.fedkey { background: #0b0f15; border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; font-size: 12px; word-break: break-all; user-select: all; }

/* live tabs */
.ltabs { display: flex; gap: 2px; padding: 6px 10px 0; background: var(--panel); overflow-x: auto; border-bottom: 1px solid var(--line); scrollbar-width: thin; }
.ltab { display: flex; align-items: center; gap: 6px; padding: 7px 14px; border: 1px solid transparent; border-bottom: none; border-radius: 8px 8px 0 0; cursor: pointer;
  color: var(--muted); white-space: nowrap; user-select: none; background: none; font: inherit; }
.ltab:hover { color: var(--text); background: #1b232e; }
.ltab.on { color: var(--text); background: var(--bg); border-color: var(--line); margin-bottom: -1px; font-weight: 600; }
.ltab.add { color: var(--accent); }
.ltab-edit { background: none; border: none; color: var(--muted); cursor: pointer; padding: 0 2px; font-size: 13px; }
.ltab-edit:hover { color: var(--text); }
.tab-cams { max-height: 380px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; padding: 8px; margin-top: 8px; }
.tab-grp { margin-bottom: 10px; }
.tab-grp-h { margin-bottom: 4px; }
.tab-grp-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 4px 12px; padding-left: 22px; }
.tab-grp-list .srv { font-size: 10px; color: var(--muted); border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; margin-left: 4px; }

@media (min-width: 861px) {
  .main.side-hidden .side, .main.side-hidden .side-backdrop { display: none; }
}
