:root {
  --bg: #000;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --separator: #38383a;
  --text: #fff;
  --text-2: #98989f;
  --text-3: #636366;
  --accent: #0a84ff;
  --danger: #ff453a;
  --live: #30d158;
  --warn: #ffd60a;
  --bar-h: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.3 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }
code { font-size: 0.9em; }

.view {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* --- navigation bar --------------------------------------------------------- */

.bar {
  flex: none;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  min-height: calc(var(--bar-h) + var(--safe-top));
  padding: var(--safe-top) calc(12px + var(--safe-right)) 0 calc(12px + var(--safe-left));
  background: rgba(22, 22, 24, 0.94);
  border-bottom: 0.5px solid var(--separator);
}
.bar h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 50vw;
}
.bar-side { display: flex; align-items: center; gap: 4px; min-width: 0; }
.bar-side.end { justify-content: flex-end; }
.host {
  color: var(--text-2);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.text-btn, .icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: none;
  border: 0;
  color: var(--accent);
  padding: 8px 4px;
  border-radius: 8px;
}
.icon-btn { padding: 8px; }
.text-btn.strong { font-weight: 600; }
.text-btn:disabled { color: var(--text-3); cursor: default; }
.back svg { margin-left: -6px; }
.text-btn:focus-visible, .icon-btn:focus-visible, .tile:focus-visible, .row-main:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.banner {
  margin: 12px 16px 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: #3a1d1b;
  color: #ffb4ab;
  font-size: 15px;
}

/* --- DVR list ---------------------------------------------------------------- */

.list {
  list-style: none;
  margin: 20px max(16px, var(--safe-right)) 20px max(16px, var(--safe-left));
  padding: 0;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
  overflow-y: auto;
}
.list:empty { display: none; }
.list li {
  display: flex;
  align-items: center;
  min-height: 60px;
  padding-right: 8px;
}
.list li + li .row-main { border-top: 0.5px solid var(--separator); }
.list li + li .row-actions { border-top: 0.5px solid var(--separator); align-self: stretch; }
.row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0 10px 16px;
  align-self: stretch;
}
.row-main > svg { color: var(--text-2); width: 26px; }
.row-text { min-width: 0; flex: 1; }
.row-name, .row-host {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-host { color: var(--text-2); font-size: 13px; }
.row-chevron { color: var(--text-3); width: 16px; }
.row-actions { display: flex; align-items: center; }
.row-actions .icon-btn { color: var(--text-2); }
.row-actions .icon-btn:disabled { color: var(--text-3); opacity: 0.4; cursor: default; }
.row-actions .delete { color: var(--danger); }
@media (hover: hover) {
  .row-main:hover { background: rgba(255, 255, 255, 0.04); }
}

.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-2);
  gap: 2px;
}
.empty svg { width: 44px; height: 44px; margin-bottom: 8px; }
.empty p { margin: 0; font-size: 15px; }
.empty .empty-title { font-size: 17px; font-weight: 600; }

/* --- live grid --------------------------------------------------------------- */

.grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr));
  grid-auto-rows: var(--row-h, 25%);
  gap: 2px;
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
  overflow-y: auto;
  overscroll-behavior: contain;
  align-content: start;
}
.tile {
  position: relative;
  background: #0b0b0c;
  overflow: hidden;
  cursor: zoom-in;
  outline: none;
}
.tile canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}
.tile-label, .tile-status {
  position: absolute;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 5px;
  background: rgba(0, 0, 0, 0.55);
  pointer-events: none;
}
.tile-label { left: 6px; top: 6px; }
.tile-status {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 90%;
  font-weight: 500;
  color: var(--text-2);
}
.tile-status .text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); flex: none; }
.tile[data-status="connecting"] .dot { background: var(--warn); animation: pulse 1.2s infinite; }
.tile[data-status="stalled"] .dot { background: var(--warn); animation: pulse 1.2s infinite; }
.tile[data-status="error"] .dot { background: var(--danger); }
.tile[data-status="live"] .tile-status {
  /* Small "live" pip in the corner instead of a centered message. */
  left: auto; top: 6px; right: 6px; transform: none; padding: 5px;
}
.tile[data-status="live"] .tile-status .text { display: none; }
.tile[data-status="live"] .dot { background: var(--live); }
.tile[data-status="down"] { cursor: default; }
@keyframes pulse { 50% { opacity: 0.3; } }

/* Tap a tile to view it alone. */
.tile.zoomed {
  position: fixed;
  inset: 0;
  z-index: 10;
  cursor: zoom-out;
  background: #000;
}
.tile.zoomed .tile-label { top: calc(8px + var(--safe-top)); left: calc(8px + var(--safe-left)); font-size: 14px; }

/* --- sheets (dialogs) -------------------------------------------------------- */

dialog.sheet {
  width: min(560px, 100vw);
  max-width: 100vw;
  max-height: min(92dvh, 900px);
  margin: auto;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: #000;
  color: var(--text);
  overflow: hidden;
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog.sheet form { display: flex; flex-direction: column; max-height: inherit; }
dialog.sheet .bar { --safe-top: 0px; border-radius: 14px 14px 0 0; }
@media (max-width: 600px) {
  dialog.sheet {
    margin: auto 0 0;
    max-height: calc(100dvh - 24px - var(--safe-top));
    border-radius: 14px 14px 0 0;
  }
}

.form-body {
  overflow-y: auto;
  padding: 8px 16px calc(24px + var(--safe-bottom));
}
.section-label {
  margin: 18px 16px 6px;
  color: var(--text-2);
  font-size: 13px;
  text-transform: uppercase;
}
.group {
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
}
.group label {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  padding: 0 16px;
  white-space: nowrap;
}
.group label + label { border-top: 0.5px solid var(--separator); }
.group input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: none;
  background: none;
  text-align: right;
  color: var(--text-2);
  padding: 11px 0;
}
.group input:focus { color: var(--text); }
.group input::placeholder { color: var(--text-3); }
.note { margin: 8px 16px 0; color: var(--text-2); font-size: 13px; }
.note:empty { display: none; }
#editor-error { color: var(--danger); }
.danger-btn {
  display: block;
  width: 100%;
  margin-top: 24px;
  min-height: 44px;
  border: 0;
  border-radius: 10px;
  background: var(--surface);
  color: var(--danger);
}
