:root {
  --bg: #0b1220;
  --panel: #131c2e;
  --panel-2: #1a2740;
  --line: #273653;
  --text: #e7eefb;
  --muted: #8fa1bf;
  --accent: #4da3ff;
  --accent-2: #2b6cb0;
  --danger: #ff6b6b;
  --ok: #7ee2a0;
  --warn-bg: rgba(240, 180, 41, 0.12);
  --warn-line: rgba(240, 180, 41, 0.45);
  --warn-text: #ffd479;
  font-size: 16px;
}

* { box-sizing: border-box; }

/* hidden attribute must always win over component display styles
   (without this, elements with display:flex/etc ignore hidden) */
[hidden] { display: none !important; }


body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(1200px 600px at 80% -10%, #16233d 0%, var(--bg) 55%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

/* ── Brand ── */
.brand { display: flex; align-items: center; gap: 0.8rem; }
.brand-mark {
  width: 44px; height: 44px;
  border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 1.3rem; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 4px 18px rgba(77, 163, 255, 0.35);
}
.brand h1 { margin: 0; font-size: 1.15rem; letter-spacing: 0.02em; }
.brand p { margin: 0; color: var(--muted); font-size: 0.8rem; letter-spacing: 0.14em; text-transform: uppercase; }

/* ── Login page ── */
body.auth { display: grid; place-items: center; padding: 1rem; }

.auth-card {
  width: 100%; max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
}
.auth-card .brand { margin-bottom: 1.6rem; }

form label { display: block; margin: 0.9rem 0 0.3rem; color: var(--muted); font-size: 0.85rem; }
form input, form select {
  width: 100%; padding: 0.7rem 0.85rem;
  background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 10px;
  color: var(--text); font-size: 1rem;
}
form input:focus, form select:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(77, 163, 255, 0.2);
}

button {
  width: 100%; margin-top: 1.4rem; padding: 0.75rem;
  border: none; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; font-size: 1rem; font-weight: 600; cursor: pointer;
}
button:hover { filter: brightness(1.08); }
button:disabled { opacity: 0.4; cursor: not-allowed; }

.error { color: var(--danger); font-size: 0.85rem; margin: 0.9rem 0 0; }
.hint { margin: 1.4rem 0 0; text-align: center; color: var(--muted); font-size: 0.75rem; }

/* ── Top bar ── */
.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.9rem 1.4rem;
  border-bottom: 1px solid var(--line);
  background: rgba(11, 18, 32, 0.85);
  backdrop-filter: blur(6px);
  position: sticky; top: 0; z-index: 5;
}
.user-box { display: flex; align-items: center; gap: 0.9rem; color: var(--muted); font-size: 0.9rem; }
button.ghost, .ghost-link {
  width: auto; margin: 0; padding: 0.45rem 0.9rem;
  background: transparent; border: 1px solid var(--line);
  color: var(--text); border-radius: 10px;
  font-size: 0.85rem; font-weight: 500; cursor: pointer;
  text-decoration: none; display: inline-block;
}
button.ghost:hover, .ghost-link:hover { border-color: var(--accent); }

main { max-width: 1100px; margin: 1.4rem auto; padding: 0 1.2rem; }

/* ── Watch page ── */
.cam-list { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-bottom: 1rem; }
.cam-btn {
  width: auto; margin: 0; padding: 0.55rem 1rem;
  background: var(--panel); border: 1px solid var(--line);
  color: var(--text); border-radius: 999px; font-size: 0.9rem; font-weight: 500;
}
.cam-btn:hover { border-color: var(--accent); }
.cam-btn.active { background: var(--accent-2); border-color: var(--accent); }
.cam-btn.offline { opacity: 0.45; cursor: not-allowed; }

.player-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  min-height: 320px;
  display: grid; place-items: center;
}
.player-wrap video { width: 100%; max-height: 72vh; display: block; background: #000; }
.status { color: var(--muted); padding: 2rem; text-align: center; }

/* ── Admin page ── */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.4rem 1.5rem;
  margin-bottom: 1.2rem;
}
.panel h2 { margin: 0 0 1rem; font-size: 1.05rem; }
.muted { color: var(--muted); font-size: 0.88rem; line-height: 1.5; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 0.85rem; }

.warn-banner {
  background: var(--warn-bg);
  border: 1px solid var(--warn-line);
  color: var(--warn-text);
  border-radius: 12px;
  padding: 0.8rem 1rem;
  font-size: 0.9rem;
  margin-bottom: 1.2rem;
}

table.users { width: 100%; border-collapse: collapse; }
table.users th, table.users td {
  text-align: left; padding: 0.55rem 0.6rem;
  border-bottom: 1px solid var(--line); font-size: 0.92rem;
}
table.users th {
  color: var(--muted); font-weight: 600; font-size: 0.75rem;
  text-transform: uppercase; letter-spacing: 0.08em;
}
table.users select {
  padding: 0.35rem 0.5rem; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; color: var(--text);
}
.col-actions { text-align: right !important; white-space: nowrap; }
.col-actions button { margin-left: 0.4rem; }

button.small {
  width: auto; margin: 0; padding: 0.5rem 1rem;
  font-size: 0.88rem; border-radius: 9px;
}
button.ghost-btn {
  background: transparent; border: 1px solid var(--line);
  color: var(--text); font-weight: 500;
}
button.ghost-btn:hover { border-color: var(--accent); filter: none; }
button.danger-btn {
  background: transparent; border: 1px solid rgba(255, 107, 107, 0.55);
  color: var(--danger); font-weight: 500;
}
button.danger-btn:hover { border-color: var(--danger); filter: none; }

.inline-form {
  display: flex; gap: 0.7rem; flex-wrap: wrap;
  align-items: flex-end; margin-top: 1.1rem;
}
.inline-form .field { display: flex; flex-direction: column; gap: 0.25rem; }
.inline-form .field.grow { flex: 1; min-width: 220px; }
.inline-form label { margin: 0; font-size: 0.8rem; }
.inline-form input, .inline-form select { width: 100%; }

.message {
  padding: 0.7rem 0.9rem; border-radius: 10px;
  margin-top: 0.9rem; font-size: 0.88rem;
}
.message.ok { background: rgba(46, 160, 67, 0.15); color: var(--ok); border: 1px solid rgba(46, 160, 67, 0.4); }
.message.err { background: rgba(255, 107, 107, 0.12); color: #ff9c9c; border: 1px solid rgba(255, 107, 107, 0.4); }

#restart-btn { margin-top: 1rem; }

/* ── Logo (shown when server/static/logo.png exists) ── */
.brand-logo { height: 44px; width: auto; max-width: 170px; object-fit: contain; }
.topbar .brand-logo { height: 36px; }
body.auth .brand-logo { height: 56px; max-width: 220px; margin-bottom: 4px; }

/* ── PTZ control pad (admins) ── */
.ptz-pad {
  display: flex; gap: 1.2rem; align-items: center; flex-wrap: wrap;
  margin-top: 1rem; padding: 1rem 1.2rem;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
}
.ptz-grid {
  display: grid; grid-template-columns: repeat(3, 52px); grid-auto-rows: 52px; gap: 6px;
}
.ptz-btn {
  width: 100%; height: 100%; margin: 0; padding: 0;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); font-size: 1.15rem; cursor: pointer;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.ptz-btn:active { background: var(--accent-2); border-color: var(--accent); }
.ptz-stop { color: var(--danger); font-size: 0.9rem; }
.ptz-zoom { display: flex; flex-direction: column; gap: 6px; }
.ptz-zoom .ptz-btn { width: 92px; height: 52px; font-size: 0.95rem; }
.ptz-hint { color: var(--muted); font-size: 0.8rem; margin: 0; flex-basis: 100%; }

/* ── View tabs (video / contraseña) ── */
.view-tabs { display: flex; gap: 8px; margin-bottom: 14px; }
.view-tab {
  width: auto; margin: 0; padding: 8px 18px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  color: var(--text-secondary); font-size: 0.9rem; cursor: pointer;
}
.view-tab.active { background: var(--accent-2); border-color: var(--accent); color: #fff; }

/* ── Password form ── */
.field-row { display: flex; gap: 14px; flex-wrap: wrap; }
.field-row .field { flex: 1; min-width: 200px; display: flex; flex-direction: column; gap: 4px; }
.field-row label { margin: 0; font-size: 0.8rem; }
.pw-submit { width: auto; margin-top: 14px; padding: 9px 22px; }

/* ── Welcome / contact cards ── */
.welcome-head { display: flex; align-items: center; gap: 0.9rem; margin-bottom: 0.9rem; }
.welcome-head .brand-logo { height: 52px; max-width: 200px; }
.welcome h2 { margin: 0; }
.welcome .muted { line-height: 1.55; }

.contact-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 0.9rem; margin-top: 1.1rem; }
.contact-card {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 14px;
  padding: 1rem 1.1rem;
}
.contact-card h3 { margin: 0 0 0.7rem; font-size: 0.95rem; color: var(--text); }
.contact-card.emergency { border-color: rgba(255, 107, 107, 0.55); background: rgba(255, 107, 107, 0.07); }
.contact-card.emergency h3 { color: var(--danger); }
.contact-link {
  display: block; padding: 0.45rem 0; color: var(--accent);
  text-decoration: none; font-size: 0.95rem; font-weight: 500;
}
.contact-link:hover { text-decoration: underline; }
.contact-note { color: var(--muted); font-size: 0.8rem; margin: 0.35rem 0 0; }

/* ── Language toggle ── */
.lang-toggle {
  display: inline-flex; border: 1px solid var(--line); border-radius: 9px;
  overflow: hidden;
}
.lang-btn {
  width: auto; margin: 0; padding: 4px 10px;
  background: transparent; border: none; border-radius: 0;
  color: var(--muted); font-size: 0.78rem; font-weight: 600; cursor: pointer;
}
.lang-btn.active { background: var(--accent-2); color: #fff; }
.auth-lang { position: absolute; top: 14px; right: 14px; }
.auth-card { position: relative; }

/* ── Camera permission checkboxes ── */
.cam-field { flex-basis: 100%; }
.cam-checks { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 6px 0; }
.cam-check { display: inline-flex; align-items: center; gap: 5px; font-size: 0.85rem; color: var(--text); cursor: pointer; }
.cam-check input { width: auto; margin: 0; }
.cam-hint { font-size: 0.75rem; color: var(--muted); margin: 2px 0 0; }
.cam-edit-row td { background: var(--panel-2); }
.cam-edit-row button { margin-left: 8px; }

/* ── Hangar Watch button ── */
.hw-btn { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.hw-btn:hover { background: var(--accent-2); color: #fff; border-color: var(--accent); }

/* ── AI Panel ── */
.ai-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.ai-tab {
  width: auto; margin: 0; padding: 7px 14px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  color: var(--text-secondary); font-size: 0.85rem; cursor: pointer;
}
.ai-tab.active { background: var(--accent-2); border-color: var(--accent); color: #fff; }
.ai-cnt { color: var(--muted); font-size: 0.78rem; margin-left: 4px; }
.ai-tab.active .ai-cnt { color: rgba(255,255,255,0.8); }

/* ── Camera grid (multiple simultaneous live views) ── */
.player-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr));
  gap: 14px;
}
@media (max-width: 900px) {
  .player-grid { grid-template-columns: 1fr; }
}
.cam-tile {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.cam-tile.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(77,163,255,0.35); }
.cam-tile-title {
  padding: 8px 12px; font-size: 0.9rem; font-weight: 600;
  color: var(--text); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 8px;
}
.cam-tile video { width: 100%; aspect-ratio: 16/9; background: #000; display: block; }
.cam-tile-status {
  padding: 10px 12px; color: var(--muted); font-size: 0.82rem;
}

/* ── AI Panel: fotos de detecciones ── */
.det-thumb { width: 180px; border-radius: 8px; display: block; cursor: zoom-in; }
.lightbox {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0, 0, 0, 0.92);
  display: flex; align-items: center; justify-content: center; flex-direction: column;
}
.lightbox img { max-width: 96vw; max-height: 82vh; object-fit: contain; border-radius: 10px; background: #000; }
.lightbox-close {
  position: absolute; top: 10px; right: 16px;
  width: 48px; height: 48px; margin: 0; padding: 0;
  background: rgba(255,255,255,0.12); border: none; border-radius: 50%;
  color: #fff; font-size: 2rem; line-height: 1; cursor: pointer;
}
.lightbox-hint { color: rgba(255,255,255,0.6); font-size: 0.85rem; margin: 14px 0 0; }

.hd-badge {
  display: inline-block; margin-top: 4px; padding: 1px 8px;
  background: var(--accent-2); border-radius: 6px;
  color: #fff; font-size: 0.7rem; font-weight: 700; letter-spacing: 0.05em;
}

/* ── Responsive: teléfonos ── */
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 6px; padding: 8px 12px; }
  .brand h1 { font-size: 1rem; }
  .brand p { font-size: 0.68rem; }
  .brand-mark { width: 36px; height: 36px; }
  .user-box { flex-wrap: wrap; gap: 6px; font-size: 0.8rem; }
  button.ghost, .ghost-link { padding: 6px 10px; font-size: 0.78rem; }
  .hw-btn { order: -1; flex-basis: 100%; }
  main { padding: 0 10px; }
  .view-tab { padding: 7px 12px; font-size: 0.82rem; }
  .ptz-pad { gap: 0.7rem; padding: 0.8rem; }
  .ptz-grid { grid-template-columns: repeat(3, 56px); grid-auto-rows: 56px; }
  .ptz-zoom .ptz-btn { width: 84px; height: 48px; font-size: 0.85rem; }
  table.users { display: block; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
  .inline-form .field.grow { min-width: 100%; }
  .cam-checks { gap: 4px 10px; }
  .det-thumb { width: 130px; }
  .contact-grid { grid-template-columns: 1fr; }
}

/* ── Tablets (2 columnas de video) ── */
@media (min-width: 721px) and (max-width: 1100px) {
  .player-grid { grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr)); }
  table.users { display: block; overflow-x: auto; white-space: nowrap; -webkit-overflow-scrolling: touch; }
}

.tipo-badge { padding: 1px 8px; border-radius: 6px; font-size: 0.72rem; font-weight: 700; }
.tipo-fijo { background: rgba(70,255,110,0.15); color: var(--ok); }
.tipo-trans { background: rgba(240,180,41,0.15); color: #ffd479; }

button.ghost.active-pw { border-color: var(--accent); color: var(--accent); }
#pass-panel { margin-bottom: 14px; }

.lightbox-bar { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.lightbox-bar button { width: auto; margin: 0; }

/* ── Overlay de cajas de deteccion en vivo ── */
.cam-video-wrap { position: relative; }
.cam-tile video { width: 100%; aspect-ratio: 16/9; background: #000; display: block; }
.bbox-overlay {
  position: absolute; border: 2px solid #46ff6e; border-radius: 4px;
  pointer-events: none; box-shadow: 0 0 12px rgba(70,255,110,0.4); z-index: 2;
}
.bbox-overlay::after {
  content: attr(data-label);
  position: absolute; top: -20px; left: -2px;
  background: #46ff6e; color: #06130a; font-size: 11px; font-weight: 700;
  padding: 1px 7px; border-radius: 4px 4px 4px 0; white-space: nowrap;
}

/* ── Tarjetas de revision en vivo (AI Panel) ── */
.ai-live-strip {
  display: flex; gap: 10px; overflow-x: auto; padding-bottom: 10px;
  margin-bottom: 12px; -webkit-overflow-scrolling: touch;
}
.live-card {
  flex: 0 0 190px; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 12px; overflow: hidden;
}
.live-card-img { width: 100%; height: 100px; object-fit: cover; display: block; cursor: zoom-in; }
.live-card-meta { padding: 6px 10px 2px; font-size: 0.8rem; }
.live-card-acts { display: flex; gap: 5px; padding: 6px 8px 8px; flex-wrap: wrap; }
.live-card-acts button { width: auto; margin: 0; padding: 4px 9px; font-size: 0.75rem; }

/* ── Panel live de camaras (pestaña En vivo) ── */
.ai-live-cams {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 10px; margin-bottom: 4px;
}
@media (max-width: 1200px) { .ai-live-cams { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 640px) { .ai-live-cams { grid-template-columns: 1fr; } }
.live-cam-cell {
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 12px; overflow: hidden;
}
.live-cam-video video { width: 100%; height: 210px; object-fit: cover; display: block; background: #000; }
.live-cam-cap { padding: 5px 10px; font-weight: 600; font-size: 0.85rem; border-bottom: 1px solid var(--line); }
.live-cam-acts { display: flex; align-items: center; gap: 5px; padding: 7px 8px; flex-wrap: wrap; min-height: 34px; }
.live-cam-acts button { width: auto; margin: 0; padding: 4px 9px; font-size: 0.75rem; }

/* ── Redireccion a Weather Watch ── */
.weather-redirect { text-align: center; padding: 1rem 0 0.4rem; }
.weather-btn {
  display: inline-block; width: 100%; margin-top: 0.9rem; padding: 0.8rem;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border-radius: 10px; font-weight: 600; text-decoration: none;
}
.ww-btn { border-color: rgba(70,255,110,0.4); }
