:root {
  --bg: #0c0e12;
  --panel: #151922;
  --panel2: #1c2230;
  --border: #2a3242;
  --text: #e6e9ef;
  --muted: #8b95a7;
  --accent: #3b82f6;
  --accent2: #2563eb;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --border-strong: #3a4256;
  --warn-bg: rgba(251,191,36,.12);
  --on-accent: #ffffff;   /* texto sobre azul */
  --on-video: #ffffff;    /* texto sobre el video (fondo oscuro siempre) */
  --radius: 10px;
  --shadow: 0 10px 40px rgba(0,0,0,.4);
  color-scheme: dark;
}
/* Modo claro (data-theme lo fija el script del <head> según la preferencia guardada
   o la del sistema). El video sigue sobre negro en ambos modos. */
:root[data-theme="light"] {
  --bg: #f3f5f8;
  --panel: #ffffff;
  --panel2: #eef1f5;
  --border: #d5dbe4;
  --border-strong: #b3bccb;
  --text: #1a2130;
  --muted: #5b6576;
  --accent: #2563eb;
  --accent2: #1d4ed8;
  --ok: #047857;
  --warn: #b45309;
  --err: #dc2626;
  --warn-bg: rgba(217,119,6,.12);
  --shadow: 0 10px 30px rgba(20,30,50,.12);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* Header */
header {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.status { margin-left: 4px; }
.user-box { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.user-box button { height: 30px; padding: 0 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand .logo { font-size: 20px; }
.tabs { display: flex; gap: 4px; }
.tab {
  background: transparent; color: var(--muted);
  border: 1px solid transparent; padding: 7px 14px; border-radius: 8px;
  cursor: pointer; font-size: 14px; font-weight: 600;
}
.tab.active { background: var(--panel2); color: var(--text); border-color: var(--border); }
.spacer { flex: 1; }
.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.warn { background: var(--warn); }
.dot.err { background: var(--err); }

/* Controls */
.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
select, button, input {
  font-size: 13px; padding: 7px 10px; border-radius: 8px; height: 34px;
  border: 1px solid var(--border); background: var(--panel2); color: var(--text);
}
select {
  -webkit-appearance: none; appearance: none; cursor: pointer;
  padding-right: 30px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238b95a7' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat; background-position: right 10px center;
}
select:hover { border-color: var(--border-strong); }
select:focus, input:focus { outline: none; border-color: var(--accent); }
button { cursor: pointer; }
button.primary { background: var(--accent2); border-color: var(--accent2); font-weight: 600; }
button.primary:hover { background: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--panel2); }
.seg button.active { background: var(--accent2); }

/* Main */
main { flex: 1; min-height: 0; padding: 14px; overflow: auto; }
.view { display: none; height: 100%; }
.view.active { display: block; }

/* Grid */
.grid { display: grid; gap: 10px; height: 100%; }
.grid.g1 { grid-template-columns: 1fr; }
.grid.g4 { grid-template-columns: repeat(2, 1fr); }
.grid.g8 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 760px) {
  .grid.g4, .grid.g8 { grid-template-columns: 1fr; }
}

.tile {
  position: relative; background: #000; border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; min-height: 180px;
  display: flex; align-items: center; justify-content: center;
}
.tile video { width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.tile .placeholder { position: absolute; color: var(--muted); font-size: 13px; text-align: center; padding: 10px; }
.tile .label {
  position: absolute; top: 8px; left: 8px; z-index: 2;
  background: rgba(0,0,0,.55); padding: 3px 8px; border-radius: 6px; font-size: 12px; font-weight: 600;
}
.tile .badge { margin-left: 6px; font-weight: 400; color: var(--muted); }
.tile .tile-actions {
  position: absolute; top: 8px; right: 8px; z-index: 2; display: flex; gap: 6px; opacity: 0; transition: opacity .15s;
}
.tile:hover .tile-actions { opacity: 1; }
@media (hover: none) { .tile .tile-actions { opacity: 1; } }  /* táctil: siempre visibles */
.tile-actions button {
  width: 30px; height: 30px; padding: 0; display: grid; place-items: center;
  background: rgba(0,0,0,.6); border-color: rgba(255,255,255,.15); font-size: 14px;
}
.tile.live .placeholder { display: none; }
/* cámara con el foco de audio */
.tile.audio-on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent), 0 0 16px rgba(59,130,246,.5); }
.tile.audio-on .label { background: var(--accent2); }
.tile-actions button.active { background: var(--accent2); border-color: var(--accent); opacity: 1; }
/* el botón de escuchar siempre visible si está activo, aunque no haya hover */
.tile.audio-on .tile-actions { opacity: 1; }

/* Playback */
/* campo de fecha propio: muestra DD/MM/AAAA y abre el calendario nativo */
.datefield { position: relative; display: inline-flex; align-items: center; gap: 8px; height: 34px; padding: 0 10px; min-width: 128px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--panel2); color: var(--text); font-size: 13px;
  font-variant-numeric: tabular-nums; cursor: pointer; }
.datefield::after { content: "📅"; font-size: 13px; margin-left: auto; }
.datefield:hover { border-color: var(--border-strong); }
.datefield:focus-within { border-color: var(--accent); }
.datefield input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; padding: 0; border: 0; }

.pb-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
/* lista a la izquierda, reproductor a la derecha */
#view-playback.active { display: flex; flex-direction: column; }
.pb-body { flex: 1; min-height: 0; display: grid; grid-template-columns: 250px 1fr; gap: 12px; }
.pb-stage { background: #000; border: 1px solid var(--border); border-radius: var(--radius); min-height: 300px; display: grid; place-items: center; overflow: hidden; }
.pb-stage { position: relative; flex: 1; }
.pb-player { display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; background: var(--bg); }
.pb-ctl { display: flex; align-items: center; gap: 10px; }
.pb-ctl button { width: 38px; padding: 0; flex: none; }
.pb-ctl input[type="range"] { flex: 1; min-width: 60px; height: 8px; }
.pb-ctl input[type="range"]:disabled { opacity: .4; cursor: default; }
.pb-time { font-size: 13px; font-variant-numeric: tabular-nums; flex: none; }
.pb-player:fullscreen { padding: 10px; }
.pb-player:fullscreen .pb-stage { border: 0; border-radius: 0; }
.pb-stage video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background:#000; }
.pb-list { display: flex; flex-direction: column; gap: 6px; overflow: auto; min-height: 0; padding-right: 2px; }
.pb-list:empty::before { content: "Busca una fecha para ver las grabaciones"; color: var(--muted); font-size: 13px; padding: 8px 2px; }
.pb-item.active { border-color: var(--accent); background: var(--accent2); }
.pb-item .muted { margin-left: auto; }
.pb-item.active .muted { color: var(--text); }
@media (max-width: 760px) {
  /* móvil: reproductor arriba, lista debajo */
  .pb-body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .pb-player { order: -1; }
  .pb-stage { min-height: 200px; flex: none; }
  .pb-stage video { position: static; height: auto; max-height: 45vh; }
  .pb-list { max-height: 40vh; }
}
.pb-item { display: flex; gap: 10px; align-items: center; padding: 8px 10px; background: var(--panel2); border: 1px solid var(--border); border-radius: 8px; cursor: pointer; font-size: 13px; }
.pb-item:hover { border-color: var(--accent); }
.pb-item .t { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }

/* Configuración */
.cfg-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 16px; }
#cfgMsg { font-size: 13px; }
#cfgMsg.ok { color: var(--ok); } #cfgMsg.err { color: var(--err); } #cfgMsg.warn { color: var(--warn); }
.cfg-pane { display: none; max-width: 820px; }
.cfg-pane.active { display: block; }
.cfg-note { margin: 0 0 14px; font-size: 13px; }
.cfg-row { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 16px; }
.cfg-row label, .field label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.cfg-row select { min-width: 160px; }
.cfg-actions { margin-top: 18px; display: flex; gap: 10px; }
button.ghost { background: var(--panel2); border-color: var(--border); color: var(--text); font-weight: 600; }
button.ghost:hover { border-color: var(--accent); }

/* Nombres */
.names-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.names-list .nrow { display: flex; flex-direction: column; gap: 5px; }
.names-list .nrow span { font-size: 12px; color: var(--muted); }
.names-list .nrow input { width: 100%; }

/* Imagen (sliders) */
.sliders { display: grid; gap: 16px; max-width: 460px; }
.sliders .srow { display: grid; grid-template-columns: 90px 1fr 36px; align-items: center; gap: 12px; }
.sliders .srow label { font-size: 13px; color: var(--text); }
.sliders .srow output { font-variant-numeric: tabular-nums; text-align: right; color: var(--muted); font-size: 13px; }
input[type="range"] { -webkit-appearance: none; appearance: none; height: 6px; padding: 0; background: var(--panel2); border: 1px solid var(--border); border-radius: 999px; cursor: pointer; }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: var(--accent); border: 2px solid var(--bg); }
input[type="range"]::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: var(--accent); border: 2px solid var(--bg); }

/* Switches / campos */
.field { margin-bottom: 14px; }
.field.actions-grid { display: flex; gap: 20px; flex-wrap: wrap; }
label.switch { flex-direction: row; align-items: center; gap: 8px; color: var(--text); cursor: pointer; }
label.switch input { width: 16px; height: 16px; accent-color: var(--accent); }

/* Grabación (grilla semanal 7x24) */
.rec-grid { display: grid; grid-template-columns: 42px repeat(24, 1fr); gap: 2px; max-width: 760px; user-select: none; touch-action: none; }
.rec-grid .rh { font-size: 10px; color: var(--muted); display: flex; align-items: center; justify-content: center; }
.rec-grid .rh.day { justify-content: flex-start; padding-left: 2px; }
.rec-grid .cell { height: 22px; background: var(--panel2); border-radius: 2px; cursor: pointer; }
.rec-grid .cell.on { background: var(--ok); }
@media (max-width: 640px) { .rec-grid .rh.hour { display: none; } .rec-grid { grid-template-columns: 34px repeat(24, 1fr); } .rec-grid .cell { height: 26px; } }

/* spinner de carga centrado sobre el video */
.pb-loading { position: absolute; inset: 0; z-index: 2; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: rgba(0,0,0,.45); color: var(--on-video); font-size: 13px; font-variant-numeric: tabular-nums; pointer-events: none; }
.pb-loading[hidden] { display: none; }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255,255,255,.2); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.pb-ctl button.active { background: var(--accent2); border-color: var(--accent); }

/* --- una cámara en pantalla completa ("solo"): la grilla muestra un solo cuadro --- */
.tile img.freeze { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; z-index: 1; pointer-events: none; }
.grid.solo { display: block; position: relative; background: #000; overflow: hidden; touch-action: pan-y; }
.grid.solo .tile { position: absolute; inset: 0; display: none; border: 0; border-radius: 0; min-height: 0; box-shadow: none; }
.grid.solo .tile.solo-on { display: flex; }
.grid.solo .tile .tile-actions { opacity: 1; }
.grid.pseudo-fs { position: fixed; inset: 0; z-index: 1000; height: 100vh; height: 100dvh; }
.tile.in-right { animation: soloInR .3s ease-out both; }
.tile.in-left { animation: soloInL .3s ease-out both; }
.tile.out-left { animation: soloOutL .3s ease-out both; }
.tile.out-right { animation: soloOutR .3s ease-out both; }
@keyframes soloInR { from { transform: translateX(100%); } to { transform: none; } }
@keyframes soloInL { from { transform: translateX(-100%); } to { transform: none; } }
@keyframes soloOutL { from { transform: none; } to { transform: translateX(-100%); } }
@keyframes soloOutR { from { transform: none; } to { transform: translateX(100%); } }
@media (prefers-reduced-motion: reduce) { .tile.in-right, .tile.in-left, .tile.out-left, .tile.out-right { animation-duration: .01s; } }

/* grabaciones: deslizar para cambiar de cámara */
.pb-stage { touch-action: pan-y; }
.pb-stage img.pb-ghost { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; z-index: 1; pointer-events: none; }
.pb-ghost.out-left { animation: soloOutL .3s ease-out both; }
.pb-ghost.out-right { animation: soloOutR .3s ease-out both; }
.pb-stage video.in-right { animation: soloInR .3s ease-out both; }
.pb-stage video.in-left { animation: soloInL .3s ease-out both; }
.pb-stage video.pb-blank { visibility: hidden; }

/* Configuración > Conexión */
#connHost { min-width: 200px; } #connPort { width: 110px; }
.conn-result { margin-top: 4px; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel2); font-size: 13px; line-height: 1.6; max-width: 520px; }
.conn-result.ok { border-color: var(--ok); } .conn-result.err { border-color: var(--err); }
.conn-result b { font-weight: 600; }

/* Configuración > Cuenta */
.acc-form { display: grid; gap: 12px; max-width: 320px; margin-top: 10px; }
.acc-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.acc-form input { width: 100%; }
#accWarn { margin-bottom: 4px; }
.warn-chip { height: 30px; padding: 0 10px; background: var(--warn-bg); border-color: var(--warn); color: var(--warn); font-weight: 600; }

/* --- textos sobre azul o sobre el video: siempre claros, en ambos modos --- */
button.primary, .seg button.active, .tile-actions button.active, .pb-ctl button.active,
.pb-item.active, .pb-item.active .muted, .tile.audio-on .label { color: var(--on-accent); }
.tile .label, .tile .label .badge, .tile-actions button { color: var(--on-video); }
.tile .label .badge { opacity: .75; }

/* botón de tema */
#themeBtn { width: 34px; height: 30px; padding: 0; font-size: 15px; line-height: 1; }

/* ---------- Celular: barra superior compacta con menú desplegable ---------- */
.menu-btn, .cur-view, .mob-title { display: none; }
@media (max-width: 760px) {
  header { padding: 6px 12px; gap: 8px; row-gap: 10px; position: relative; min-height: 47px; }
  header .brand { order: 0; flex: 1; min-width: 0; font-size: 15px; }
  /* nombre de la app centrado en la primera línea de la barra */
  header .brand #title { display: none; }
  header .brand .mob-title {
    display: block; position: absolute; left: 50%; top: 23px; transform: translate(-50%, -50%);
    max-width: calc(100% - 150px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    pointer-events: none; font-size: 16px;
  }
  @media (max-width: 350px) { header .brand .mob-title { font-size: 14px; } }
  header .status { order: 1; margin: 0; }
  header .status #statusText { display: none; }            /* solo el punto de color */
  .menu-btn { display: grid; place-items: center; order: 2; width: 38px; height: 34px; padding: 0; font-size: 18px; position: relative; }
  .menu-btn.has-warn::after { content: ""; position: absolute; top: 5px; right: 5px; width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
  header .spacer { display: none; }
  /* plegado: se esconden pestañas, layout y usuario */
  header .tabs, header #live-controls, header .user-box { order: 3; flex-basis: 100%; display: none; }
  header.open .tabs { display: flex; }
  header.open #live-controls:not([style*="none"]) { display: flex; }
  header.open .user-box { display: flex; flex-wrap: wrap; }
  header.open .tabs .tab { flex: 1; padding: 9px 6px; }
  header.open .user-box #userName { margin-left: auto; }
}

/* sub-pestañas de Configuración: en pantallas angostas se desplazan con el dedo */
#cfgTabs { max-width: 100%; min-width: 0; overflow-x: auto; overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch; scrollbar-width: none; scroll-snap-type: x proximity; }
#cfgTabs::-webkit-scrollbar { display: none; }
#cfgTabs button { flex: none; white-space: nowrap; scroll-snap-align: start; }
@media (max-width: 760px) {
  .cfg-head { flex-wrap: nowrap; flex-direction: column; align-items: stretch; gap: 8px; }
  #cfgTabs { display: flex; }
  #cfgTabs button { padding: 0 14px; height: 38px; }
}
