:root {
  color-scheme: dark;
  --bg: #0f1115;
  --panel: #171a21;
  --text: #e8eaf0;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --line: #262b36;
  --danger: #ff7a7a;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button { -webkit-tap-highlight-color: transparent; }

/* шапка и панель мозаик */
.top {
  position: sticky; top: 0; z-index: 2;
  background: rgba(15, 17, 21, .94); backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
}
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px 6px; padding-top: max(10px, env(safe-area-inset-top));
}
.title { font-weight: 600; font-size: 17px; }
.who { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }
.who form { margin: 0; }
.link { background: none; border: 0; color: var(--accent); font: inherit; padding: 6px 0; cursor: pointer; }
.tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; padding: 0 16px 10px; }
.chips { display: flex; gap: 6px; overflow-x: auto; flex: 1 1 100%; min-width: 0; scrollbar-width: none; }
@media (min-width: 720px) { .chips { flex: 1 1 0; } }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px;
  background: transparent; color: var(--muted); font: inherit; font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip.on { color: #fff; background: var(--accent); border-color: var(--accent); }
.chip.add { border-style: dashed; }
.cols { display: flex; align-items: center; gap: 4px; flex-shrink: 0; margin-left: auto; }
.icon {
  width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line);
  background: transparent; color: var(--text); font: inherit; font-size: 18px; cursor: pointer;
}
.cols-val { min-width: 62px; text-align: center; color: var(--muted); font-size: 13px; }
.cols .link { margin-left: 6px; }
.traffic { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* сетка камер */
.grid {
  display: grid; gap: 10px; padding: 10px 16px 16px;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr));
}
.grid.fixed { grid-template-columns: repeat(var(--cols), minmax(0, 1fr)); gap: 6px; padding: 6px; }
/* жесты: вертикальную прокрутку ведёт браузер, остальное (удержание, вбок, два пальца) — страница */
.grid { touch-action: pan-y; }
.tile { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.tile.dragging { opacity: .3; }
.drag-ghost {
  position: fixed; left: 0; top: 0; z-index: 20; pointer-events: none; overflow: hidden;
  border: 1px solid var(--accent); border-radius: 10px; background: var(--panel);
  box-shadow: 0 12px 32px rgba(0, 0, 0, .6);
}
.drag-ghost canvas { display: block; width: 100%; height: auto; aspect-ratio: 16 / 9; background: #000; }
.drag-ghost span { display: block; padding: 4px 8px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 30;
  transform: translateX(-50%); padding: 7px 16px; border-radius: 999px; pointer-events: none;
  background: rgba(23, 26, 33, .94); border: 1px solid var(--line); color: var(--text); font-size: 14px;
  opacity: 0; transition: opacity .2s;
}
.toast.show { opacity: 1; }
.tip {
  display: flex; align-items: flex-start; gap: 10px; margin: 0 16px 10px; padding: 8px 10px;
  border-radius: 8px; background: #1b2232; color: var(--muted); font-size: 13px; line-height: 1.35;
}
.single .tip { margin: 8px 16px 0; }
.tip button { flex-shrink: 0; margin-left: auto; padding: 0 4px; border: 0; background: none; color: var(--muted); font-size: 16px; cursor: pointer; }
/* крупнее кнопки под палец */
@media (pointer: coarse) {
  .chip { padding: 8px 14px; }
  .icon { width: 40px; height: 40px; }
}
.tile {
  position: relative; display: flex; flex-direction: column; padding: 0; min-width: 0;
  background: var(--panel); color: inherit; font: inherit; text-align: left;
  border: 1px solid var(--line); border-radius: 10px; overflow: hidden; cursor: pointer;
}
.tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.screen { display: block; position: relative; width: 100%; aspect-ratio: 16 / 9; background: #000; }
.cap { padding: 8px 10px; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grid.dense .tile { border-radius: 6px; }
.grid.dense .cap {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 2px 6px; font-size: 11px;
  background: linear-gradient(transparent, rgba(0, 0, 0, .8));
}
.empty { padding: 40px 16px; text-align: center; color: var(--muted); }
/* заголовок регистратора в «Все камеры» — на всю ширину сетки */
.grp { grid-column: 1 / -1; padding: 6px 2px 0; color: var(--muted); font-size: 13px; font-weight: 600; }
/* камера ждёт места: одновременно играет не больше max_live */
.screen.paused::after {
  content: '⏸ на паузе — нажмите, чтобы смотреть'; position: absolute; inset: 0; z-index: 1;
  display: grid; place-items: center; padding: 8px; text-align: center;
  color: var(--muted); font-size: 13px; background: rgba(0, 0, 0, .55);
}
.grid.dense .screen.paused::after { content: '⏸'; font-size: 14px; place-items: start end; padding: 3px 5px; }

/* плеер: кадр регистратора растягивается в 16:9 — так исправляется сжатый субпоток 960×576 */
cam-stream { display: block; position: absolute; inset: 0; }
cam-stream video { width: 100%; height: 100%; object-fit: fill; background: #000; }
/* браузер не может показать видео этой камеры (например, нет H.265) */
cam-stream[data-msg]::after {
  content: attr(data-msg); position: absolute; inset: 0; z-index: 1;
  display: grid; place-items: center; padding: 8px; text-align: center;
  color: var(--muted); font-size: 13px; background: #000;
}
.grid.dense cam-stream[data-msg]::after { font-size: 10px; padding: 4px; }
/* камера замолчала — плеер переподключается */
cam-stream[data-state="stall"]::before {
  content: 'нет сигнала — переподключаюсь…'; position: absolute; left: 6px; top: 6px; z-index: 1;
  padding: 2px 6px; border-radius: 4px; background: rgba(0, 0, 0, .7); color: #ffb86b; font-size: 11px;
  white-space: nowrap;
}
.grid.dense cam-stream[data-state="stall"]::before { content: 'нет сигнала'; font-size: 9px; left: 3px; top: 3px; padding: 1px 4px; }

/* одна камера на весь экран */
.single { position: fixed; inset: 0; z-index: 3; display: flex; flex-direction: column; background: #000; }
.single-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.single-title { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabs { display: flex; gap: 6px; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 6px;
  background: transparent; color: var(--muted); font: inherit; cursor: pointer;
}
.tab.on { color: #fff; background: var(--accent); border-color: var(--accent); }
.single-screen {
  flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; container-type: size;
  position: relative; overflow: hidden; touch-action: none;  /* жесты одной камеры — свои */
}
.single-screen cam-stream {
  position: relative; inset: auto; width: min(100cqw, 100cqh * 16 / 9); aspect-ratio: 16 / 9;
  transform-origin: 0 0;
}
.timeline { touch-action: none; }

/* записи */
.arch {
  display: flex; flex-direction: column; gap: 10px;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line);
}
.arch-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--muted); font-size: 14px; }
.arch input {
  font: inherit; color: var(--text); background: var(--bg); color-scheme: dark;
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px;
}
.btn {
  display: inline-block; font: inherit; font-weight: 600; color: #fff; background: var(--accent);
  border: 0; border-radius: 8px; padding: 7px 14px; cursor: pointer; text-decoration: none;
}
.timeline {
  position: relative; height: 34px; overflow: hidden; cursor: pointer;
  background: #0b0d11; border: 1px solid var(--line); border-radius: 6px;
}
.timeline .seg { position: absolute; top: 6px; bottom: 6px; background: rgba(79, 140, 255, .55); border-radius: 2px; }
.timeline .cursor { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: #ff5c5c; z-index: 1; }
@media (pointer: coarse) { .timeline { height: 44px; } }  /* шкалу ведут пальцем — повыше */
.timeline::after {
  content: attr(data-empty); position: absolute; inset: 0; display: grid; place-items: center;
  color: var(--muted); font-size: 12px; pointer-events: none;
}
.ticks { display: flex; justify-content: space-between; margin-top: -6px; color: var(--muted); font-size: 11px; }
.jumps { justify-content: center; }
.pos { min-width: 76px; text-align: center; color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.dl-status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
.progress { flex: 1; min-width: 120px; height: 6px; background: var(--bg); border-radius: 3px; overflow: hidden; }
.progress div { width: 0; height: 100%; background: var(--accent); transition: width .5s; }

/* редактор мозаики */
.editor {
  width: min(100% - 32px, 520px); height: min(85dvh, 760px); padding: 0; overflow: hidden;
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px;
}
.editor[open] { display: flex; flex-direction: column; }
.editor::backdrop { background: rgba(0, 0, 0, .6); }
.editor form { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 12px; padding: 18px; }
.editor h2 { margin: 0; font-size: 18px; }
.field { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 14px; }
.field input, .field select {
  font: inherit; font-size: 16px; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
.ed-tools { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 13px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; }
.ed-list { flex: 1; min-height: 120px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; }
.ed-item {
  display: flex; align-items: center; justify-content: flex-start; gap: 10px; padding: 8px 10px;
  text-align: left; border-bottom: 1px solid var(--line); cursor: pointer;
}
.ed-item > span:last-child { flex: 1; min-width: 0; }
.ed-item:last-child { border-bottom: 0; }
.ed-grp {
  position: sticky; top: 0; z-index: 1; padding: 6px 10px; background: var(--bg);
  color: var(--muted); font-size: 12px; font-weight: 600; border-bottom: 1px solid var(--line);
}
.ed-item input { accent-color: var(--accent); width: 18px; height: 18px; }
.ed-num {
  flex-shrink: 0; min-width: 22px; height: 22px; border-radius: 11px; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-size: 12px;
}
.ed-num:empty { background: transparent; }
.ed-actions { display: flex; align-items: center; gap: 14px; }
.spacer { flex: 1; }
.danger { color: var(--danger); }

/* вход */
.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login {
  width: min(100%, 340px); display: flex; flex-direction: column; gap: 14px; padding: 24px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.login h1 { margin: 0 0 4px; font-size: 22px; }
.login label { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 14px; }
.login input {
  font: inherit; font-size: 16px; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
}
.login input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login button {
  font: inherit; font-weight: 600; color: #fff; background: var(--accent);
  border: 0; border-radius: 8px; padding: 11px; cursor: pointer;
}
.err { margin: 0; color: var(--danger); font-size: 14px; }
