:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f2430;
  --text: #e8eaf0;
  --muted: #8b93a7;
  --accent: #4fd1a5;
  --accent-2: #2fa886;
  --danger: #ff6b6b;
  --radius: 12px;
  --gap: 8px;
  --top-h: 56px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
button, select { font: inherit; }
code { background: var(--panel-2); padding: 1px 6px; border-radius: 6px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

header#top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  min-height: var(--top-h); padding: 8px 12px;
  background: linear-gradient(var(--bg), rgba(15,17,21,.92));
  backdrop-filter: blur(6px);
  border-bottom: 1px solid #222634;
  flex-wrap: wrap;
}
.brand { font-weight: 700; letter-spacing: .3px; display: flex; align-items: center; gap: 8px; }
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent);
  box-shadow: 0 0 10px var(--accent); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
#cams { margin-left: auto; padding-left: 10px; border-left: 1px solid #2a2f3d; }
.chip {
  border: 1px solid #2a2f3d; background: var(--panel); color: var(--text);
  padding: 7px 12px; border-radius: 999px; cursor: pointer; white-space: nowrap;
}
.chip:hover { background: var(--panel-2); }
.chip.active { background: var(--accent); color: #062018; border-color: var(--accent); font-weight: 600; }
#cams .chip { padding: 6px 10px; font-size: 14px; }
.icon-btn {
  border: 1px solid #2a2f3d; background: var(--panel); color: var(--text);
  width: 36px; height: 36px; border-radius: 10px; cursor: pointer; display: inline-grid; place-items: center;
}
.icon-btn:hover { background: var(--panel-2); }
.btn { border: 0; background: var(--accent); color: #062018; font-weight: 600;
  padding: 10px 16px; border-radius: 10px; cursor: pointer; }

.grid-wrap { display: flex; justify-content: center; }
main.grid {
  display: grid; gap: var(--gap); padding: var(--gap); width: 100%;
  grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr));
  grid-auto-rows: max-content; align-content: start;
}
.cell-empty { aspect-ratio: 16 / 9; border-radius: var(--radius); border: 2px dashed transparent; }
.grid.is-dragging .cell-empty { border-color: #3a4258; }
.cell-empty.drop-target { border-color: var(--accent); background: rgba(79,209,165,.12); }
.tile.drop-center { outline: 3px dashed var(--accent); outline-offset: -3px; }
.tile.drop-left { box-shadow: -8px 0 0 var(--accent); }
.tile.drop-right { box-shadow: 8px 0 0 var(--accent); }
.tile.drop-top { box-shadow: 0 -8px 0 var(--accent); }
.tile.drop-bottom { box-shadow: 0 8px 0 var(--accent); }
.tile {
  position: relative; background: #000; border-radius: var(--radius); overflow: hidden;
  aspect-ratio: 16 / 9; outline: 2px solid transparent; outline-offset: 0;
  transition: outline-color .15s;
}
.tile:focus { outline-color: transparent; }
.tile.selected { outline-color: var(--accent); }
.tile:fullscreen { aspect-ratio: auto; border-radius: 0; }
lookout-cam { display: block; width: 100%; height: 100%; transform-origin: 50% 50%; will-change: transform; }
.tile.zoomed lookout-cam { cursor: grab; }
.tile.zoomed.panning lookout-cam { cursor: grabbing; }
.tile.zoomed { touch-action: none; }
.tile .tools .zoom-reset { font-size: 12px; font-weight: 600; }
lookout-cam video { width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.tile .label {
  position: absolute; left: 0; right: 0; top: 0; padding: 8px 10px;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  background: linear-gradient(rgba(0,0,0,.6), transparent); pointer-events: none;
  font-weight: 600; text-shadow: 0 1px 2px #000;
}
.tile .label .state { font-weight: 400; font-size: 12px; color: #cdd3e1; }
.tile .label .state.err { color: var(--danger); }
.tile .tools { position: absolute; bottom: 6px; left: 6px; opacity: 0; transition: opacity .15s; display: flex; gap: 4px; }
.tile:hover .tools, .tile.selected .tools, .tile:fullscreen .tools { opacity: 1; }
.tile .tools .icon-btn { background: rgba(0,0,0,.5); border-color: rgba(255,255,255,.2); }

/* pan / tilt pad */
.ptz {
  position: absolute; right: 10px; bottom: 10px;
  display: grid; grid-template-columns: 44px 44px 44px; grid-template-rows: 44px 44px 44px;
  gap: 4px; align-items: center; justify-items: center;
}
.ptz button {
  width: 44px; height: 44px; border-radius: 10px; border: 1px solid rgba(255,255,255,.25);
  background: rgba(0,0,0,.55); color: #fff; cursor: pointer; font-size: 15px;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.ptz button:active, .ptz button.held { background: var(--accent); color: #062018; }
.ptz [data-op="up"]    { grid-column: 2; grid-row: 1; }
.ptz [data-op="left"]  { grid-column: 1; grid-row: 2; }
.ptz [data-op="right"] { grid-column: 3; grid-row: 2; }
.ptz [data-op="down"]  { grid-column: 2; grid-row: 3; }
.ptz [data-op="zoom_in"]  { grid-column: 3; grid-row: 1; font-size: 20px; }
.ptz [data-op="zoom_out"] { grid-column: 1; grid-row: 1; font-size: 20px; }
.ptz .presets { grid-column: 1 / span 3; grid-row: 4; width: 100%; margin-top: 4px;
  background: rgba(0,0,0,.6); color: #fff; border: 1px solid rgba(255,255,255,.25); border-radius: 8px; padding: 6px; }
.ptz .save-view { grid-column: 1 / span 3; grid-row: 5; width: 100%; height: 32px; font-size: 13px; margin-top: 2px; }
.ptz { grid-template-rows: 44px 44px 44px auto auto; }
.ptz.has-presets { grid-template-rows: 44px 44px 44px auto auto; }
.tile.small-tile .ptz { transform: scale(.8); transform-origin: bottom right; }

.empty { text-align: center; padding: 60px 20px; }

.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 20; display: grid; place-items: end center; }
.sheet-inner { background: var(--panel); width: min(560px, 100%); border-radius: 16px 16px 0 0; padding: 20px; }
.sheet h2 { margin: 0 0 6px; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 12px 0; }
.row select { background: var(--panel-2); color: var(--text); border: 1px solid #2a2f3d; border-radius: 8px; padding: 8px; max-width: 65%; }

/* phone */
@media (max-width: 720px) {
  :root { --gap: 6px; }
  header#top { padding: 6px 8px; gap: 6px; }
  .brand { font-size: 14px; }
  .brand { order: 0; }
  #menu { order: 1; margin-left: auto; }
  #areas { order: 2; width: 100%; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  #cams { order: 3; margin-left: 0; border-left: 0; padding-left: 0; width: 100%; overflow-x: auto; flex-wrap: nowrap; scrollbar-width: none; }
  .chip { padding: 7px 11px; font-size: 14px; }
  .ptz { right: 6px; bottom: 6px; }
  .tile.small-tile .tools { top: auto; bottom: 6px; left: 6px; right: auto; opacity: 1; }
}
@media (prefers-color-scheme: light) {
  /* we stay dark on purpose: camera video looks best on a dark background */
}

/* recordings */
.rec-chip { background: var(--panel-2); }
.rec-chip.active { background: var(--danger); color: #fff; border-color: var(--danger); }
.tile .label .meta { display: flex; gap: 8px; align-items: center; font-weight: 400; font-size: 12px; color: #cdd3e1; }
.tile .label .rec { color: #fff; background: var(--danger); padding: 1px 7px; border-radius: 6px; font-weight: 700; font-size: 11px; animation: blink 1.4s infinite; }
.tile .label .motion { color: #ffcf5c; font-size: 12px; }
@keyframes blink { 50% { opacity: .3; } }
.recview { padding: 10px 12px; display: flex; flex-direction: column; gap: 10px; }
.rec-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.rec-bar select { background: var(--panel); color: var(--text); border: 1px solid #2a2f3d; border-radius: 999px; padding: 7px 12px; }
.timeline { position: relative; height: 54px; background: var(--panel); border-radius: 10px; overflow: hidden; cursor: pointer; }
.tl-hours { position: absolute; inset: 0 0 auto 0; height: 16px; font-size: 10px; color: var(--muted); }
.tl-hours span { position: absolute; top: 1px; transform: translateX(-50%); }
.tl-track { position: absolute; left: 0; right: 0; top: 18px; bottom: 0; }
.tl-clip { position: absolute; top: 4px; bottom: 4px; background: #3a4258; border-radius: 3px; min-width: 2px; }
.tl-clip.motion { background: var(--accent-2); }
.tl-clip.playing { outline: 2px solid #fff; }
.tl-motion { position: absolute; top: 0; height: 4px; background: var(--danger); min-width: 2px; }
.tl-now { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; opacity: .6; }
.tl-sel { position: absolute; top: 0; bottom: 0; background: rgba(79,209,165,.28); cursor: grab; }
.tl-handle { position: absolute; top: -2px; bottom: -2px; width: 10px; background: var(--accent); border-radius: 3px; cursor: ew-resize; }
.tl-handle[data-side=from] { left: -5px; } .tl-handle[data-side=to] { right: -5px; }
.timeline { overflow: visible; }
.chip:disabled { opacity: .45; cursor: default; }
.timeline { touch-action: none; user-select: none; }
.rec-body { display: grid; grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr); gap: 12px; }
.rec-player { background: #000; border-radius: var(--radius); overflow: hidden; position: relative; aspect-ratio: 16/9; display: grid; place-items: center; }
.rec-player video, .rec-player lookout-cam { width: 100%; height: 100%; }
.rec-player lookout-cam video { width: 100%; height: 100%; object-fit: contain; }
#rec-player-title { position: absolute; left: 10px; top: 8px; text-shadow: 0 1px 2px #000; color: #fff; pointer-events: none; }
.rec-list { list-style: none; margin: 0; padding: 0; max-height: 70vh; overflow: auto; display: flex; flex-direction: column; gap: 4px; }
.rec-list li { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; background: var(--panel); border-radius: 8px; cursor: pointer; }
.rec-list li:hover { background: var(--panel-2); }
.rec-list li.playing { outline: 2px solid var(--accent); }
.rec-list .tag { font-size: 11px; padding: 2px 6px; border-radius: 6px; background: var(--panel-2); color: var(--muted); }
.rec-list .tag.motion { background: var(--accent-2); color: #062018; }
.rec-list .tag.live { background: var(--danger); color: #fff; }
.rec-list a { color: var(--muted); text-decoration: none; font-size: 12px; }
@media (max-width: 720px) { .rec-body { grid-template-columns: 1fr; } .rec-list { max-height: 40vh; } }

/* users */
.users-inner { max-height: 92vh; overflow: auto; }
.user-list { list-style: none; padding: 0; margin: 10px 0; display: flex; flex-direction: column; gap: 6px; }
.user-list li { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; display: flex; justify-content: space-between; gap: 10px; align-items: center; flex-wrap: wrap; }
.user-list .uname { font-weight: 600; }
.user-list .udesc { color: var(--muted); font-size: 13px; }
.user-list .uacts { display: flex; gap: 6px; flex-wrap: wrap; }
.user-list .uacts .chip { padding: 5px 10px; font-size: 13px; }
.tvkey { width: 100%; font-size: 12px; background: var(--panel); padding: 8px; border-radius: 8px; word-break: break-all; }
.urow { display: flex; gap: 8px; margin: 8px 0; }
.urow input { flex: 1; }
#uform input[type=text], #uform input:not([type]), #uform input[type=password], #pwform input {
  width: 100%; padding: 10px; border-radius: 10px; border: 1px solid #2a2f3d; background: var(--panel-2); color: var(--text); margin: 4px 0; }
.uf-cams { display: flex; flex-direction: column; margin: 8px 0; }
.chk { display: flex; gap: 8px; align-items: center; padding: 4px 0; }
.err { color: var(--danger); min-height: 1.2em; }

.cam-row select { background: var(--panel); color: var(--text); border: 1px solid #2a2f3d; border-radius: 8px; padding: 6px 10px; }
.cam-row .rstate { font-size: 12px; color: var(--muted); }
.cam-row .rstate.on { color: var(--accent); }
.cam-row .rstate.bad { color: var(--danger); }

/* layout controls + drag to reorder */
.layout { border-left: 1px solid #2a2f3d; padding-left: 10px; }
.layout .chip { padding: 6px 9px; font-size: 13px; }
.tile { touch-action: pan-y; }
.tile.dragging { opacity: .4; }
.tile.drop-target { outline: 3px dashed var(--accent); outline-offset: -3px; }
.drag-ghost { position: fixed; z-index: 50; pointer-events: none; background: var(--accent); color: #062018; font-weight: 700;
  padding: 6px 12px; border-radius: 999px; box-shadow: 0 4px 16px rgba(0,0,0,.5); }
.rec-export { display: flex; gap: 8px; align-items: center; margin-left: auto; flex-wrap: wrap; }
.rec-export label { font-size: 13px; color: var(--muted); display: flex; gap: 4px; align-items: center; }
.rec-export input[type=time] { background: var(--panel); color: var(--text); border: 1px solid #2a2f3d; border-radius: 8px; padding: 5px 8px; }
@media (max-width: 720px) { .layout { display: none; } .rec-export { margin-left: 0; } }

/* settings sidebar */
.side-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; }
.sidebar { position: fixed; top: 0; right: 0; bottom: 0; width: min(340px, 92vw); background: var(--panel); z-index: 31;
  box-shadow: -8px 0 30px rgba(0,0,0,.5); padding: 14px; display: flex; flex-direction: column; gap: 10px; animation: slidein .18s ease-out; }
@keyframes slidein { from { transform: translateX(40px); opacity: 0; } }
.side-head { display: flex; justify-content: space-between; align-items: center; }
.side-list { display: flex; flex-direction: column; gap: 6px; margin-top: 6px; }
.side-item { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; width: 100%;
  background: var(--panel-2); border: 1px solid #2a2f3d; color: var(--text); border-radius: 12px; padding: 12px 14px; cursor: pointer; font-weight: 600; }
.side-item:hover { border-color: var(--accent); }
.side-item span:last-child { font-weight: 400; }
#side-info { margin-top: auto; }

/* centered dialog */
.dlg-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40; display: grid; place-items: center; padding: 16px; }
.dlg { background: var(--panel); border-radius: 16px; width: min(760px, 100%); max-height: 92vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.6); }
.dlg-head { padding: 18px 20px 8px; } .dlg-head h2 { margin: 0 0 4px; }
.dlg-body { padding: 8px 20px; overflow: auto; flex: 1; }
.dlg-foot { padding: 12px 20px 16px; display: flex; gap: 10px; align-items: center; border-top: 1px solid #2a2f3d; }
.grow { flex: 1; }
.dlg input[type=text], .dlg input:not([type]), .dlg input[type=password] { width: 100%; padding: 10px; border-radius: 10px; border: 1px solid #2a2f3d; background: var(--panel-2); color: var(--text); margin: 4px 0; }
.dlg select { background: var(--panel-2); color: var(--text); border: 1px solid #2a2f3d; border-radius: 8px; padding: 7px 10px; }
.dlg h3 { margin: 14px 0 6px; font-size: 15px; }
.rows { list-style: none; padding: 0; margin: 6px 0; display: flex; flex-direction: column; gap: 6px; }
.rows li { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; display: flex; justify-content: space-between; gap: 10px; align-items: center; flex-wrap: wrap; }
.rows .title { font-weight: 600; } .rows .desc { color: var(--muted); font-size: 13px; }
.rows .acts { display: flex; gap: 6px; flex-wrap: wrap; } .rows .acts .chip { padding: 5px 10px; font-size: 13px; }
/* category editor */
.cat { background: var(--panel-2); border-radius: 12px; padding: 10px 12px; margin: 8px 0; }
.cat-head { display: flex; gap: 8px; align-items: center; }
.cat-head input { flex: 1; }
.cat-cams { display: flex; flex-direction: column; margin: 6px 0 0 26px; }
.name-grid { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; align-items: center; }
.name-grid .muted { white-space: nowrap; }
.mode-note { background: var(--panel-2); border-radius: 10px; padding: 8px 12px; margin: 6px 0; font-size: 13px; }

/* user form */
.uform2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.ucol h3 { margin-top: 6px; }
.fld { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.fld span { font-size: 13px; color: var(--muted); }
.sublist { margin: 2px 0 8px 26px; display: flex; flex-direction: column; }
.sublist.disabled { opacity: .4; }
.rows .tvkey { margin-top: 6px; }
@media (max-width: 720px) { .uform2 { grid-template-columns: 1fr; } }

.grid-wrap { min-height: calc(100vh - var(--top-h) - 8px); }     /* blank space below the cameras is a drop target too */
main.grid.drop-blank { outline: 2px dashed var(--accent); outline-offset: -2px; border-radius: 12px; }

.permtab { border-collapse: collapse; width: 100%; margin: 6px 0 4px; }
.permtab th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 6px; }
.permtab td { padding: 5px 6px; border-top: 1px solid #2a2f3d; }
.permtab td:not(:first-child), .permtab th:not(:first-child) { text-align: center; width: 84px; }
.permtab.disabled { opacity: .4; }
.permtab input[type=checkbox] { width: 16px; height: 16px; }

.grid-wrap { cursor: grab; }
.grid-wrap .tile, .grid-wrap .cell-empty { cursor: default; }
.grid-wrap.sliding { cursor: grabbing; }

.checktab td:not(:first-child), .checktab th:not(:first-child) { text-align: left; width: auto; }
.checktab tr.bad td { color: var(--danger); }

/* recording dialog: per-camera motion margins */
.rows li.rec-row { display: block; }
.rows li.rec-row .rec-main { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.rec-margins { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; margin-top: 6px; padding-left: 2px; font-size: 13px; color: var(--muted, #aab); }
.rec-margins input { width: 5.5em; padding: 6px 8px; border-radius: 8px; border: 1px solid #2a2f3d; background: var(--panel); color: var(--text); }
.rec-margins input::placeholder { color: var(--muted); opacity: .7; }

/* camera controls dialog */
.ctl-cam { background: var(--panel-2); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.ctl-cam h3 { margin: 0 0 8px; font-size: 15px; }
.ctl-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 5px 0; border-top: 1px solid rgba(255,255,255,.06); }
.ctl-row:first-of-type { border-top: 0; }
.ctl-name { flex: 1 1 220px; font-size: 14px; }
.ctl-row .seg { display: flex; gap: 4px; }
.ctl-row .chip.active { background: var(--accent); color: #062018; border-color: var(--accent); }
.ctl-row .chip.danger { border-color: rgba(255,120,120,.5); }
.ctl-msg { min-width: 3em; }

/* quick camera buttons down the left side of a tile: spotlight, night vision, siren */
.tile .quick { position: absolute; left: 6px; top: 50%; transform: translateY(-50%); display: flex; flex-direction: column; gap: 8px; opacity: 0; transition: opacity .15s; }
.tile:hover .quick, .tile.selected .quick, .tile:fullscreen .quick, .tile.small-tile .quick { opacity: 1; }
.qbtn { width: 40px; height: 40px; border-radius: 50%; border: 1px solid rgba(255,255,255,.3); background: rgba(0,0,0,.55); color: #fff; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; padding: 0; font-size: 17px; touch-action: manipulation; }
.qbtn svg { display: block; pointer-events: none; }
.qbtn small { font-size: 9px; margin-top: 1px; color: #cdd3e1; }
.q-night svg { width: 16px; height: 16px; }
.qbtn.on { background: var(--accent); color: #062018; border-color: var(--accent); }
.qbtn.on small { color: #062018; }
.qbtn.armed { background: var(--danger); border-color: var(--danger); animation: qpulse .6s ease-in-out infinite alternate; }
@keyframes qpulse { from { transform: scale(1); } to { transform: scale(1.12); } }
.qbtn.busy { opacity: .6; }
.q-talk { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.tile.small-tile .quick { gap: 6px; }
.tile.small-tile .qbtn { width: 32px; height: 32px; font-size: 14px; }
.tile.small-tile .qbtn svg { width: 16px; height: 16px; }
.tile.small-tile .qbtn small { display: none; }
