:root {
  --bg: #0f1115;
  --panel: #1a1d24;
  --line: #2a2f3a;
  --fg: #e6e8ec;
  --muted: #8b93a3;
  --accent: #3da9fc;
  --green: #34c759;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
}
header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  flex-wrap: wrap;
}
header h1 { font-size: 16px; margin: 0; font-weight: 600; }
nav { display: flex; gap: 6px; }
nav button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
}
nav button.active { color: var(--fg); border-color: var(--accent); }
.token { margin-left: auto; display: flex; gap: 6px; }
.token input { background: var(--bg); border: 1px solid var(--line); color: var(--fg); padding: 6px; border-radius: 6px; }
button { cursor: pointer; }
main { padding: 18px; }
.tab { display: none; }
.tab.active { display: block; }
.toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.toolbar button { background: var(--accent); border: 0; color: #04121f; padding: 7px 14px; border-radius: 6px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.err { color: #ff6b6b; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
/* content-visibility: o navegador pula layout/paint dos cards fora da tela.
   Com 91+ pessoas isso é a diferença entre travar e rolar liso. */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
  content-visibility: auto; contain-intrinsic-size: auto 280px; }
.card .thumb { height: 160px; background: #000; display: flex; align-items: center; justify-content: center; position: relative; }
.card .thumb img { cursor: zoom-in; }
.hist-btn { background: var(--line); color: var(--fg); border: 0; padding: 4px 10px; border-radius: 6px; font-size: 12px; margin-right: 6px; }
.psel-wrap { position: absolute; top: 6px; left: 6px; background: rgba(0,0,0,0.5); border-radius: 5px; padding: 2px 4px; }
.psel { width: 18px; height: 18px; cursor: pointer; }
#merge-btn { background: var(--green); color: #04140a; border: 0; padding: 7px 14px; border-radius: 6px; font-weight: 600; }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; }
.card .noimg { color: var(--muted); font-size: 13px; }
.card .meta { padding: 10px; }
.card input.name {
  width: 100%; background: transparent; border: 1px solid transparent;
  color: var(--fg); font-size: 15px; font-weight: 600; padding: 4px; border-radius: 5px;
}
.card input.name:hover, .card input.name:focus { border-color: var(--line); background: var(--bg); outline: none; }
.badge { display: inline-block; font-size: 11px; background: #3a2f12; color: #f5b942; padding: 1px 6px; border-radius: 4px; margin: 4px 0; }
.count { margin: 6px 0 2px; }
.count b { color: var(--green); font-size: 18px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: 13px; }
.logthumb { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; }

/* ===== DVR layout ===== */
.dvr { display: flex; gap: 14px; align-items: flex-start; }
.dvr-wall { flex: 1 1 auto; min-width: 0; }
.live-toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 10px;
  padding: 8px 12px; background: #2a1d14; border: 1px solid #5a3a22; border-radius: 8px; }
.live-toolbar[hidden] { display: none; }
.btn-warn { background: #e0894a; border: 0; color: #1a0e04; padding: 6px 12px;
  border-radius: 6px; font-weight: 600; }
.btn-warn:disabled { opacity: .6; cursor: default; }
.btn-ghost { background: transparent; border: 1px solid var(--line); color: var(--fg);
  padding: 6px 12px; border-radius: 6px; }

/* Modal de confirmação de dedup: lista o plano (mantém vs remove) com miniaturas */
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 18px; width: min(920px, 94vw); max-height: 86vh; display: flex; flex-direction: column; }
.dedup-card h2 { margin: 0 0 4px; }
.dedup-body { overflow-y: auto; margin: 10px 0; display: flex; flex-direction: column; gap: 10px; }
.ddg { display: flex; gap: 14px; align-items: flex-start; padding: 8px;
  background: #14181f; border: 1px solid var(--line); border-radius: 8px; }
.ddg-keep { flex: 0 0 150px; display: flex; flex-direction: column; gap: 4px; align-items: center; }
.ddg-rem { flex: 1 1 auto; display: flex; flex-wrap: wrap; gap: 8px; }
.ddg-cam { display: flex; flex-direction: column; gap: 3px; align-items: center; width: 120px; }
.ddg img { width: 100%; height: 78px; object-fit: cover; border-radius: 5px; background: #000; }
.ddg-keep img { height: 96px; border: 2px solid var(--green); }
.ddg-tag { font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 4px; }
.ddg-tag.keep { background: var(--green); color: #04121f; }
.ddg-tag.rem { background: #ff5a5a; color: #1a0404; }
.dedup-actions { display: flex; justify-content: flex-end; gap: 10px; padding-top: 6px; }

/* Verificar rostos (IA): revisão par a par */
.tb-spacer { flex: 1 1 auto; }
.verify-card { width: min(820px, 95vw); }
.verify-filters { display: flex; gap: 18px; font-size: 13px; padding: 4px 0 8px; }
.verify-body { overflow-y: auto; display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.vpair { display: flex; align-items: center; gap: 12px; padding: 6px 10px; border-radius: 8px;
  background: #14181f; border: 1px solid var(--line); cursor: pointer; }
.vpair.doubt { border-color: #5a3a22; background: #1c1710; }
.vp-tag { font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 4px; flex: 0 0 auto; }
.vp-tag.auto { background: var(--green); color: #04121f; }
.vp-tag.doubt { background: #e0894a; color: #1a0e04; }
.vp-face { display: flex; flex-direction: column; align-items: center; gap: 2px; width: 92px; }
.vp-face img { width: 84px; height: 84px; object-fit: cover; border-radius: 6px; background: #000; }
.vp-eq { font-size: 20px; color: var(--muted); }
.vp-score { margin-left: auto; color: var(--muted); white-space: nowrap; }
.vsec { margin: 12px 0 4px; font-size: 14px; }
.vsec.del { color: #ff6b6b; }
.vp-rem { display: flex; flex-wrap: wrap; gap: 8px; }
.vdel { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px;
  border: 1px solid #5a2222; border-radius: 8px; background: #1c1010; }
.vp-tag.del { background: #ff5a5a; color: #1a0404; }
.dvr-side {
  flex: 0 0 320px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 12px; position: sticky; top: 12px;
  max-height: calc(100vh - 90px); overflow-y: auto;
}
.dvr-side h2 { margin: 0 0 10px; }
.recent { display: flex; flex-direction: column; gap: 8px; }
.recent-item { display: flex; gap: 10px; align-items: center; padding: 6px; border-radius: 8px; background: var(--bg); }
.recent-thumb { width: 52px; height: 52px; flex: 0 0 52px; border-radius: 8px; overflow: hidden; background: #000; display: flex; align-items: center; justify-content: center; }
.recent-thumb img { width: 100%; height: 100%; object-fit: cover; }
.recent-thumb span { color: var(--muted); }
.recent-name { font-weight: 600; font-size: 14px; }

h2 { font-size: 15px; margin: 22px 0 8px; font-weight: 600; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
.live-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 12px; }
.live-cell { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #000; }
.live-cell .live-label { padding: 5px 10px; background: var(--panel); font-size: 13px; font-weight: 600; }
/* Preview uniforme em 16:9 (DVR wall). cover preenche o tile sem deformar;
   o enquadramento real/completo aparece só ao ampliar no modal (#modal-img). */
.live-cell img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover;
  display: block; background: #000; }
.addcam { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.addcam input, .addcam select {
  background: var(--bg); border: 1px solid var(--line); color: var(--fg);
  padding: 7px 9px; border-radius: 6px;
}
.addcam input { min-width: 240px; }
.addcam button { background: var(--accent); border: 0; color: #04121f; padding: 8px 14px; border-radius: 6px; font-weight: 600; }
.addcam #cam-test { background: var(--line); color: var(--fg); }
#cam-test-result { margin: 8px 0; min-height: 18px; }
.del { background: #3a1414; color: #ff8a8a; border: 1px solid #5a2222; padding: 5px 10px; border-radius: 6px; }
.switch input { width: 18px; height: 18px; }
.edit { background: var(--line); color: var(--fg); border: 0; padding: 5px 10px; border-radius: 6px; margin-right: 6px; }
.fix { background: #14323a; color: #5fd0e0; border: 1px solid #225a66; padding: 5px 10px; border-radius: 6px; margin-right: 6px; }

/* ===== Scan de rede ===== */
#scan-results { margin: 10px 0 6px; display: flex; flex-direction: column; gap: 6px; }
.scan-host { display: flex; align-items: center; gap: 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; }
.scan-host .mono { font-weight: 600; }
.scan-host .ports { color: var(--muted); font-size: 12px; flex: 1; }
.scan-host .obadge { background: #14323a; color: #5fd0e0; border: 1px solid #225a66;
  font-size: 11px; padding: 1px 6px; border-radius: 4px; }
.scan-host button { background: var(--accent); border: 0; color: #04121f;
  padding: 5px 12px; border-radius: 6px; font-weight: 600; }

/* ===== Controles PTZ + áudio no modal de câmera ===== */
.ptz { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 20px; align-items: center; background: rgba(0,0,0,0.6);
  padding: 12px 18px; border-radius: 14px; z-index: 3; }
.ptz[hidden] { display: none; }
.ptz-pad { display: grid; grid-template-columns: repeat(3, 42px);
  grid-template-rows: repeat(3, 42px); gap: 4px; }
.ptz-pad button, .ptz-zoom button { background: #1d2129; color: #fff;
  border: 1px solid #3a4150; border-radius: 8px; font-size: 17px; cursor: pointer; }
.ptz-pad button:active, .ptz-zoom button:active { background: var(--accent); color: #04121f; }
.ptz-up { grid-column: 2; grid-row: 1; } .ptz-left { grid-column: 1; grid-row: 2; }
.ptz-stop { grid-column: 2; grid-row: 2; color: #ff8a8a; }
.ptz-right { grid-column: 3; grid-row: 2; } .ptz-down { grid-column: 2; grid-row: 3; }
.ptz-zoom { display: flex; flex-direction: column; gap: 4px; align-items: center; }
.ptz-zoom button { width: 42px; height: 42px; font-size: 20px; }
.ptz-audio { display: flex; flex-direction: column; gap: 8px; }
.ptz-audio button { background: #14323a; color: #5fd0e0; border: 1px solid #225a66;
  border-radius: 8px; padding: 7px 12px; cursor: pointer; white-space: nowrap; }
.ptz-audio button:disabled { opacity: 0.4; cursor: not-allowed; }
.ptz-audio button.on { background: var(--green); color: #04140a; border-color: var(--green); }

/* ===== Modais ===== */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.88); z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.modal[hidden] { display: none; }
/* Lightbox de rosto ampliado */
#img-modal { cursor: zoom-out; }
#img-zoom { max-width: 94vw; max-height: 90vh; border-radius: 8px; image-rendering: auto;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6); }
.img-bar { position: absolute; top: 14px; right: 16px; display: flex; gap: 8px; z-index: 2; }
#img-close { background: #333; color: #fff;
  border: 0; padding: 8px 14px; border-radius: 6px; font-weight: 600; }
#img-del { padding: 8px 14px; border-radius: 6px; font-weight: 600; }
.pm-thumb img { cursor: zoom-in; }
.modal-bar { position: absolute; top: 0; left: 0; right: 0; display: flex;
  justify-content: space-between; align-items: center; padding: 10px 16px;
  background: rgba(0,0,0,0.5); color: #fff; font-weight: 600; }
#modal-close, #e-cancel { background: #333; color: #fff; border: 0; padding: 7px 14px; border-radius: 6px; }
/* Palco que recorta o zoom; a imagem é posicionada/escalada por JS */
#modal-stage { position: relative; width: 96vw; height: 86vh; overflow: hidden;
  background: #000; border-radius: 8px; touch-action: none; }
#modal-img { position: absolute; left: 0; top: 0; transform-origin: 0 0;
  cursor: grab; user-select: none; -webkit-user-drag: none; }
#modal-img.grabbing { cursor: grabbing; }
.modal-bar { z-index: 5; }
/* Minimapa (navegador): mostra o quadro inteiro + retângulo da área visível */
#cam-minimap { position: absolute; left: 16px; bottom: 16px; width: 190px;
  border: 2px solid rgba(255,255,255,0.55); border-radius: 6px; overflow: hidden;
  background: #000; z-index: 4; box-shadow: 0 4px 18px rgba(0,0,0,0.6); cursor: pointer; }
#cam-minimap[hidden] { display: none; }
#minimap-img { width: 100%; display: block; }
#minimap-view { position: absolute; border: 2px solid var(--accent);
  background: rgba(61,169,252,0.18); pointer-events: none; box-sizing: border-box; }
.edit-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 20px; width: min(460px, 92vw); display: flex; flex-direction: column; gap: 10px; }
.edit-box label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.edit-box input, .edit-box select { background: var(--bg); border: 1px solid var(--line);
  color: var(--fg); padding: 8px; border-radius: 6px; }
.edit-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
#e-save { background: var(--accent); color: #04121f; border: 0; padding: 8px 16px; border-radius: 6px; font-weight: 600; }
.del-person { margin-top: 8px; background: #3a1414; color: #ff8a8a; border: 1px solid #5a2222; padding: 4px 10px; border-radius: 6px; font-size: 12px; }
.card .thumb { cursor: pointer; }

/* ===== Modal de detalhe da pessoa (histórico) ===== */
.person-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  width: min(560px, 94vw); max-height: 88vh; display: flex; flex-direction: column; }
.pm-head { display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
#pm-close { background: #333; color: #fff; border: 0; padding: 6px 12px; border-radius: 6px; }
#pm-meta { padding: 8px 16px; }
.pm-history { overflow-y: auto; padding: 8px 16px 16px; display: flex; flex-direction: column; gap: 8px; }
.pm-row { display: flex; gap: 10px; align-items: center; background: var(--bg); border-radius: 8px; padding: 6px; }
.pm-thumb { width: 60px; height: 60px; flex: 0 0 60px; border-radius: 8px; overflow: hidden; background: #000; display: flex; align-items: center; justify-content: center; }
.pm-thumb img { width: 100%; height: 100%; object-fit: cover; }
.pm-thumb span { color: var(--muted); }
.pm-info { flex: 1; min-width: 0; }
.pm-fix { margin-top: 4px; font-size: 12px; color: var(--muted); }
.pm-fix select { background: var(--bg); border: 1px solid var(--line); color: var(--fg); border-radius: 5px; padding: 3px 6px; }

/* ===== Toasts (notificações in-page) ===== */
.toast-container { position: fixed; top: 16px; right: 16px; z-index: 1000;
  display: flex; flex-direction: column-reverse; gap: 10px;
  max-width: min(380px, 92vw); pointer-events: none; }
.toast { pointer-events: auto; display: flex; align-items: flex-start; gap: 10px;
  background: var(--panel); border: 1px solid var(--line); border-left-width: 4px;
  border-radius: 8px; padding: 12px 14px; color: var(--fg); font-size: 14px;
  box-shadow: 0 6px 22px rgba(0,0,0,0.5); word-break: break-word;
  opacity: 0; transform: translateX(20px);
  transition: opacity 0.25s ease, transform 0.25s ease; }
.toast.show { opacity: 1; transform: translateX(0); }
.toast.ok { border-left-color: var(--green); }
.toast.error { border-left-color: #ff5a5a; }
.toast .toast-msg { flex: 1; min-width: 0; white-space: pre-wrap; }
.toast .toast-close { flex: 0 0 auto; background: transparent; border: 0;
  color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; padding: 0 2px; }
.toast .toast-close:hover { color: var(--fg); }

/* ===== Descrição da câmera + info na tela cheia ===== */
.cam-desc { display: block; margin-top: 2px; font-weight: 400; opacity: 0.85; }
.modal-info { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px;
  padding: 6px 16px 10px; color: var(--fg); font-size: 13px; }
.modal-info .mi-status { font-weight: 600; }
.modal-info .mi-desc { opacity: 0.9; }
.modal-info .mi-meta { width: 100%; }
.modal-info .mono { font-size: 12px; }

/* ===== Câmeras: grupo de DVR + ações em massa ===== */
#cam-bulk { margin: 6px 0 10px; gap: 6px; flex-wrap: wrap; }
#cam-table tr.grp-head td { background: #14202b; border-top: 2px solid var(--accent);
  padding: 8px 10px; font-size: 13px; }
#cam-table tr.grp-head button, #cam-bulk button { font-size: 12px; padding: 3px 8px;
  margin-left: 6px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--bg); color: var(--fg); cursor: pointer; }
.cam-rec, .cam-lpr, .cam-sel { accent-color: var(--accent); }

/* Toolbar de câmeras: filtros + barra de seleção */
.cam-filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 8px 0 6px; }
.cam-filters input[type="search"] { min-width: 240px; padding: 7px 10px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--bg); color: var(--fg); }
.cam-filters select { padding: 7px 8px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--bg); color: var(--fg); }
#cam-filter-count { margin-left: auto; }

.cam-selbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  margin: 4px 0 8px; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: #11202b; }
.cam-selall { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.cam-sel-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.cam-sel-actions button { font-size: 12px; padding: 4px 9px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--bg); color: var(--fg); cursor: pointer; }
.cam-sel-actions button:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-accent { background: var(--accent) !important; color: #04121f !important; border: 0 !important; }
.cam-cb-col { width: 28px; text-align: center; }

/* Placas (LPR) */
.plate-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 700;
  font-size: 15px; letter-spacing: 1px; }
#plates-table .logthumb { max-height: 38px; border-radius: 4px; }
#plates-table .del-plate { font-size: 12px; padding: 3px 8px; border-radius: 6px;
  border: 1px solid var(--line); background: var(--bg); color: var(--fg); cursor: pointer; }

/* ===== Autenticação / login ===== */
/* Enquanto decide (auth-loading) e quando deslogado, esconde o app e mostra a
   tela de login. O backend é a autoridade — isto é só apresentação. */
body.auth-loading header,
body.auth-loading main,
body.logged-out header,
body.logged-out main { display: none !important; }
body.authed #auth-screen { display: none; }

.auth-screen {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1b2433, var(--bg));
  padding: 20px;
}
.auth-card {
  width: 100%; max-width: 360px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 26px 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
}
.auth-logo { font-size: 18px; margin: 0 0 18px; text-align: center; }
.auth-form h2 { font-size: 15px; margin: 0 0 14px; font-weight: 600; }
.auth-form label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.auth-form input {
  display: block; width: 100%; margin-top: 5px;
  background: var(--bg); border: 1px solid var(--line); color: var(--fg);
  padding: 9px 10px; border-radius: 8px; font-size: 14px;
}
.auth-primary {
  width: 100%; background: var(--accent); color: #04121f; border: 0;
  padding: 10px; border-radius: 8px; font-weight: 700; font-size: 14px; margin-top: 4px;
}
.auth-switch { font-size: 13px; text-align: center; margin: 14px 0 0; color: var(--muted); }
.auth-switch a { color: var(--accent); text-decoration: none; }
.auth-note { text-align: center; margin-top: 10px; }
.auth-err { color: #ff6b6b; font-size: 13px; margin: 10px 0 0; }
.auth-ok { color: var(--green); font-size: 13px; margin: 10px 0 0; }

/* Barra de usuário no header */
.user-box { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.user-badge { font-size: 13px; color: var(--muted); }

/* ===== Aba Usuários ===== */
.pending-list { margin: 8px 0 22px; display: flex; flex-direction: column; gap: 8px; }
.pending-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: #1c2733; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px;
}
.pending-actions { display: flex; gap: 6px; }
.pending-actions button, .pending-row .btn-accent { font-size: 12px; padding: 5px 10px; border-radius: 6px; }
#users-table { width: 100%; border-collapse: collapse; }
#users-table th, #users-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); font-size: 13px; }
#users-table select {
  background: var(--bg); border: 1px solid var(--line); color: var(--fg);
  padding: 5px 8px; border-radius: 6px;
}

/* Controles destrutivos só p/ operador+ (o 'user' vê Pessoas/Placas, não altera).
   O backend é a autoridade; isto evita mostrar botões que dariam 403. */
body:not(.role-operator) .op-only { display: none !important; }
