:root {
  /* HOUM theme — neutral-black surfaces with the brand red accent. */
  --bg: #0c0c0e;
  --bg-2: #101012;
  --panel: #161618;
  --panel-2: #1d1d20;
  --line: #2a2a2e;
  --line-2: #38383d;
  --text: #f1f1f3;
  --muted: #9a9aa3;
  --accent: #fc022b;     /* HOUM red */
  --accent-2: #c80021;   /* darker red (hover/active) */
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --radius: 14px;
  --shadow: 0 8px 28px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background:
    radial-gradient(1200px 500px at 80% -10%, rgba(252, 2, 43, .10), transparent 60%),
    var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 24px;
  background: rgba(13, 13, 15, .9);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; }
.brand-logo { height: 30px; width: auto; display: block; }

.actions { margin-left: auto; display: flex; align-items: center; gap: 10px; }

.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 4px rgba(255, 255, 255, .04);
  transition: .25s;
}
.dot.on { background: var(--ok); box-shadow: 0 0 0 4px rgba(34, 197, 94, .18), 0 0 10px rgba(34, 197, 94, .5); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border: 1px solid var(--line-2);
  border-radius: 11px;
  background: var(--panel-2);
  color: var(--text);
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  transition: .15s;
}
.btn:hover { border-color: #3c496a; transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn svg { width: 16px; height: 16px; flex: 0 0 auto; }

.btn-primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  border: 1px solid rgba(255, 255, 255, .14);
  color: #fff;
  box-shadow: 0 6px 18px rgba(252, 2, 43, .40);
}
.btn-primary:hover { filter: brightness(1.08); border-color: rgba(255, 255, 255, .2); }

.btn-ghost {
  background: transparent;
  border-color: var(--line-2);
  color: var(--muted);
}
.btn-ghost svg { color: #d14a4a; }
.btn-ghost:hover {
  background: rgba(239, 68, 68, .1);
  border-color: rgba(239, 68, 68, .5);
  color: #ff9a9a;
}

/* ---------- Layout ---------- */
/* Same horizontal padding as the top bar so the grid's left edge lines up with the logo. */
main {
  flex: 1;
  padding: 22px 24px;
  width: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.grid {
  display: grid;
  /* 6 cameras per page as a fixed 3 × 2 layout that fills the viewport height. */
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 18px;
  flex: 1;
  min-height: 0;
}

/* Pagination bar — centered in the footer (shown only when >6 cameras). */
.pager {
  display: flex;
  align-items: center;
  gap: 12px;
}
.pager[hidden] { display: none; }
.pager .btn { padding: 7px 12px; font-size: .82rem; }
.pager .page-info {
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  min-width: 84px;
  text-align: center;
}
.pager .btn[disabled] { opacity: 0.4; pointer-events: none; }

/* ---------- Toasts ---------- */
.toast-host {
  position: fixed;
  /* Override the UA popover default (inset:0; margin:auto) → pin bottom-right. */
  inset: auto 18px 18px auto;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
  /* Strip the UA popover chrome. */
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  overflow: visible;
}
/* When used as a popover it's display:none until shown; restore flex when open. */
.toast-host:popover-open { display: flex; }
.toast {
  min-width: 220px;
  max-width: 360px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line-2);
  border-left: 3px solid var(--accent);
  box-shadow: var(--shadow);
  font-size: .9rem;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-warn { border-left-color: var(--warn); }
.toast-err  { border-left-color: var(--err); }
.toast-info { border-left-color: var(--accent); }

/* Empty state: collapse the (empty) grid so the message centers in <main>. */
.grid:empty { display: none; }
.empty {
  color: var(--muted);
  text-align: center;
  margin: auto;
  max-width: 420px;
  font-size: 1rem;
}

/* ---------- Camera card ---------- */
.card {
  background: var(--panel);
  border: 1px dashed var(--line-2);
  border-radius: 18px;
  padding: 10px;
  box-shadow: var(--shadow);
  transition: border-color .18s, box-shadow .18s, transform .05s;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.card:hover { border-color: var(--line-2); }
/* Drag-to-reorder */
.card { cursor: grab; }
.card.dragging {
  cursor: grabbing;
  opacity: .5;
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}
.card.dragging * { pointer-events: none; }
/* Tile currently under the cursor — this is the one that will be swapped. */
.card.drop-target {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  border-color: var(--accent);
}
/* Solid border while a live stream is present; dashed only when offline/idle. */
.card[data-state="connected"] { border-style: solid; border-color: var(--line-2); }
.card[data-state="connected"]:hover { border-color: var(--line-2); }

.card-video {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #0a0d13;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
}
/* SDK player fills the rounded video box */
.card-video .hcsdk-player { border-radius: 12px; }

@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: .25; } }

/* Centered offline / status placeholder */
.cam-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  text-align: center;
  padding: 16px;
  background: radial-gradient(400px 220px at 50% 45%, #121724, #0a0d13);
}
.cam-overlay[hidden] { display: none; }
.cam-overlay-icon { width: 38px; height: 38px; color: #56617a; margin-bottom: 6px; }
.cam-overlay-title { color: #d7deec; font-weight: 600; font-size: 1.02rem; }
.cam-overlay-sub { color: var(--muted); font-size: .85rem; }
.cam-overlay-sub[hidden] { display: none; }
.cam-overlay-status {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 3px;
  font-size: .82rem;
  color: var(--muted);
}
.cam-overlay-status[hidden] { display: none; }
.cam-overlay-status i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.cam-overlay-status[data-tone="warn"] { color: #f0b663; }
.cam-overlay-status[data-tone="warn"] i { background: var(--warn); animation: blink 1.2s infinite; }
.cam-overlay-status[data-tone="err"] { color: #f08a8a; }
.cam-overlay-status[data-tone="err"] i { background: var(--err); }

.video-hint {
  position: absolute;
  left: 50%; bottom: 12px;
  transform: translateX(-50%);
  z-index: 4;
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .55);
  color: #e8ecf4;
  font-size: .74rem;
  opacity: 0;
  transition: opacity .18s;
  pointer-events: none;
}
.card-video:hover .video-hint { opacity: 1; }

/* ---------- Bottom bar ---------- */
.card-bar {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 6px 5px;
}

.status-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
  transition: .2s;
}
.status-dot[data-tone="ok"] { background: var(--ok); box-shadow: 0 0 8px rgba(34, 197, 94, .6); }
.status-dot[data-tone="warn"] { background: var(--warn); animation: blink 1.1s infinite; }
.status-dot[data-tone="err"] { background: var(--err); }

.cam-ident { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.cam-name { color: var(--text); font-weight: 700; font-size: .95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-serial { color: var(--muted); font-size: .76rem; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.spacer { margin-left: auto; }

.icon-group { display: flex; align-items: center; gap: 6px; }

.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border: none;
  border-radius: 9px;
  background: var(--panel-2);
  color: #c4ccdc;
  cursor: pointer;
  transition: .14s;
}
.icon svg { width: 17px; height: 17px; }
.icon:hover { background: #28324a; color: #fff; }
.icon-danger:hover { background: rgba(239, 68, 68, .18); color: #ff8e8e; }

/* ---------- Footer ---------- */
.footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--muted);
  font-size: .82rem;
  letter-spacing: .3px;
  padding: 14px 24px;
  border-top: 1px solid var(--line);
}
/* Footer only holds the pager — hide the empty bar when there's a single page. */
.footer:has(.pager[hidden]) { display: none; }

/* ---------- Dialog ---------- */
.dialog {
  border: 1px solid var(--line-2);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 24px;
  width: min(430px, 92vw);
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(4, 6, 10, .6); backdrop-filter: blur(3px); }
.dialog h2 { margin: 0 0 18px; font-size: 1.15rem; }
.dialog label { display: block; margin-bottom: 14px; font-size: .8rem; color: var(--muted); font-weight: 600; }
.dialog input, .dialog select {
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--line-2);
  border-radius: 9px;
  background: var(--bg-2);
  color: var(--text);
  font-size: 15px;
}
.dialog input:focus, .dialog select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(252, 2, 43, .20); }

.input-wrap { position: relative; display: block; margin-top: 6px; }
.input-wrap input { padding-right: 44px; margin-top: 0; }
.pw-toggle {
  position: absolute;
  top: 50%; right: 6px;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px; height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: .14s;
}
.pw-toggle:hover { color: var(--text); background: var(--panel-2); }
.pw-toggle svg { width: 18px; height: 18px; }
/* Show exactly one icon, driven by the input type (never both). */
.pw-toggle .pw-eye-off { display: none; }
.input-wrap input[type="text"] ~ .pw-toggle .pw-eye { display: none; }
.input-wrap input[type="text"] ~ .pw-toggle .pw-eye-off { display: block; }
/* Hide the browser's own native password-reveal control to avoid a duplicate eye. */
.input-wrap input::-ms-reveal,
.input-wrap input::-ms-clear { display: none; }
.dialog-text { margin: 0 0 18px; color: var(--muted); overflow-wrap: anywhere; }
.dialog-text strong { color: var(--text); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }

/* ---------- Fullscreen ---------- */
.grid:fullscreen { background: var(--bg); padding: 18px; overflow: auto; align-content: start; }
.card:fullscreen { display: flex; flex-direction: column; background: #000; border: none; }
.card:fullscreen .card-video { flex: 1; aspect-ratio: auto; }
:fullscreen .hcsdk-player video { object-fit: contain; }

/* ---------- Responsive ---------- */
/* Keep 3 cameras per row (6 per page) down to small tablet widths. */
@media (max-width: 640px) {
  .topbar { padding: 12px 16px; gap: 10px; }
  .btn { padding: 8px 12px; font-size: .85rem; }
  main { padding: 16px; }
  .grid { grid-template-columns: 1fr; grid-auto-rows: auto; }
  .card-video { aspect-ratio: 16 / 9; flex: none; }
}
