:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232c;
  --border: #2a2f3a;
  --text: #e8eaef;
  --muted: #9aa1ad;
  --accent: #4f8cff;
  --accent-press: #3a74e0;
  --live: #2fd27a;
  --warn: #f5b942;
  --danger: #ff5d5d;
  --radius: 14px;
  --gap: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

/* ---------- comuns ---------- */
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: .2px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--live); box-shadow: 0 0 10px var(--live); }
.muted { color: var(--muted); margin: 0; }
.error { color: var(--danger); min-height: 1.4em; margin: 0; }
.spacer { flex: 1; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.badge.mock { color: var(--warn); border-color: #5a4a20; }

.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px;
  padding: 10px 16px; cursor: pointer; text-decoration: none; text-align: center;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-press); }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 12px; font-size: 14px; }
.btn:focus-visible, .icon-btn:focus-visible, .ptz button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px 24px; display: grid; gap: 12px;
}
.login-card .brand { font-size: 22px; }
.login-card label { font-size: 14px; color: var(--muted); }
.login-card input {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px; color: var(--text);
  padding: 12px; font-size: 16px; /* 16px evita zoom no iOS */
}
.login-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* ---------- topo ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px; background: rgba(15,17,21,.9);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}

/* ---------- grade ---------- */
.grid {
  display: grid; gap: var(--gap); padding: var(--gap) 16px;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
  max-width: 1500px; margin: 0 auto;
}
.tile {
  position: relative; aspect-ratio: 16 / 9; background: #000; border-radius: var(--radius);
  overflow: hidden; border: 1px solid var(--border); outline: none; scroll-margin-top: 72px;
}
.video, .canvas { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.tile.pseudo-fs { position: fixed; inset: 0; z-index: 50; border-radius: 0; border: 0; aspect-ratio: auto; }

.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.power-dialog { max-width: min(420px, 94vw); }
.power-dialog h2 { margin: 4px 0 8px; font-size: 18px; }
.power-actions { display: grid; gap: 8px; margin-top: 16px; }
.power-banner {
  display: flex; align-items: center; gap: 12px; justify-content: space-between; flex-wrap: wrap;
  margin: 12px 16px 0; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--danger);
  background: rgba(255, 93, 93, .12); max-width: 1500px;
}
#power-btn { color: var(--muted); }
#power-btn:hover { color: var(--danger); }

.overlay { position: absolute; left: 0; right: 0; display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.overlay.top { top: 0; background: linear-gradient(rgba(0,0,0,.65), transparent); pointer-events: none; }
.overlay.bottom { bottom: 0; background: linear-gradient(transparent, rgba(0,0,0,.7)); }
.cam-name { font-weight: 600; text-shadow: 0 1px 2px #000; }
.status { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.tile[data-state="live"] .status .dot { background: var(--live); box-shadow: 0 0 8px var(--live); }
.tile[data-state="live"] .status-text { color: var(--text); }
.tile[data-state="loading"] .status .dot { background: var(--warn); animation: pulse 1s infinite; }
.tile[data-state="error"] .status .dot { background: var(--danger); }
.tile[data-state="error"] .status-text { color: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.icon-btn {
  min-width: 40px; height: 40px; padding: 0 10px; border-radius: 10px; border: 1px solid rgba(255,255,255,.12);
  background: rgba(20,22,28,.7); display: inline-grid; place-items: center; cursor: pointer; font-weight: 650; font-size: 13px;
}
.icon-btn:hover { background: rgba(40,44,54,.9); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; stroke-linecap: round; }
.mute-toggle svg path:first-child { fill: currentColor; stroke: none; }
.mute-toggle[aria-pressed="true"] .x { display: none; }
.q-toggle[data-q="hd"] { color: var(--accent); border-color: var(--accent); }

.paused-cover {
  position: absolute; inset: 0; display: grid; place-content: center; gap: 4px; text-align: center;
  background: rgba(0,0,0,.55); border: 0; cursor: pointer;
}
.paused-title { font-size: 18px; font-weight: 650; }
.paused-sub { color: var(--muted); font-size: 14px; }

/* ---------- modo ampliado ---------- */
body.focus-mode .grid { grid-template-columns: 1fr; max-width: 1200px; }
body.focus-mode .tile:not(.focused) { display: none; }
body.focus-mode .focus-btn svg path { d: path("M6 6l12 12M18 6L6 18"); }

.focus-panel {
  max-width: 1200px; margin: 0 auto; padding: 4px 16px max(24px, env(safe-area-inset-bottom));
  display: flex; flex-wrap: wrap; gap: 24px; align-items: flex-start; justify-content: center;
}
.ptz-wrap { display: grid; justify-items: center; gap: 8px; }
.ptz { display: grid; grid-template-columns: repeat(3, 64px); grid-template-rows: repeat(3, 64px); gap: 8px; touch-action: none; user-select: none; -webkit-user-select: none; }
.ptz button {
  border-radius: 14px; border: 1px solid var(--border); background: var(--surface-2); font-size: 22px; cursor: pointer;
  touch-action: none; -webkit-touch-callout: none;
}
.ptz button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.ptz-center { border-radius: 50%; margin: 14px; background: var(--surface); border: 1px solid var(--border); }
.hint { color: var(--muted); font-size: 13px; margin: 0; text-align: center; }

.presets { min-width: min(320px, 100%); display: grid; gap: 10px; }
.presets-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 14px; }
.preset-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.btn.preset { font-size: 18px; font-weight: 650; padding: 14px 0; }
.presets.saving .btn.preset { border-color: var(--warn); color: var(--warn); }
#preset-save-toggle[aria-pressed="true"] { border-color: var(--warn); color: var(--warn); }

/* ---------- foto ---------- */
.snap-dialog { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 12px; max-width: min(960px, 94vw); }
.snap-dialog::backdrop { background: rgba(0,0,0,.7); }
.snap-dialog img { display: block; max-width: 100%; max-height: 70vh; border-radius: 10px; }
.snap-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }

/* ---------- aviso ---------- */
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 10px 16px;
  z-index: 20; max-width: calc(100% - 32px); box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.toast.error { border-color: var(--danger); }

/* tela cheia */
.tile:fullscreen { border-radius: 0; border: 0; aspect-ratio: auto; }

@media (hover: none) { .desktop-only { display: none; } }
@media (max-width: 480px) {
  .grid { padding: 8px; gap: 8px; }
  .icon-btn { min-width: 38px; height: 38px; }
  .ptz { grid-template-columns: repeat(3, 72px); grid-template-rows: repeat(3, 72px); }
}
