:root {
  --bg: #0e1116;
  --panel: #171c24;
  --line: #262d38;
  --accent: #3b82f6;
  --text: #e6e9ee;
  --muted: #8b94a3;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
/* Use the *dynamic* viewport height where supported so the mobile address bar
   never pushes the bottom (timeline) off-screen. */
body { min-height: 100dvh; }

.hidden { display: none !important; }

/* Login */
.login {
  height: 100%;
  display: grid;
  place-items: center;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 28px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-card h1 { font-size: 18px; margin: 0 0 8px; }
.login-card input {
  background: #0e131a;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
}
.login-card button, .mode {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  cursor: pointer;
}
.error { color: #f87171; font-size: 13px; min-height: 16px; }

/* App layout */
.app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden; /* children manage their own size; nothing spills the viewport */
}
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.cameras, .modes { display: flex; gap: 8px; }
.cameras button, .mode {
  background: #0e131a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 13px;
}
.cameras button.active, .mode.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.status { margin-left: auto; color: var(--muted); font-size: 13px; }

.stage {
  position: relative;
  flex: 1;
  min-height: 0; /* lets the flex item shrink instead of forcing the column taller */
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
/* Size to the intrinsic aspect ratio, capped by the box — never forced to 100%,
   so the picture is always fully visible (letterboxed), never cropped. */
#video {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  background: #000;
}

/* Debug readout lives in the header (which is otherwise mostly empty). */
.debug {
  color: var(--muted);
  font: 11px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre;
}
.debug:empty { display: none; }

/* Fullscreen / landscape toggle. */
.fs-btn {
  position: absolute;
  bottom: 10px; right: 10px;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .5);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, .25);
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
}

/* Archive transport controls. */
.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.ctrl {
  background: #0e131a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.ctrl.seek { padding: 8px 10px; font-size: 13px; min-width: 44px; }
.ctrl-main { min-width: 56px; font-size: 16px; }
.ctrl:active { background: var(--accent); border-color: var(--accent); }
/* "Ускоренный обзор" trigger lives in the topbar so it's reachable from live too. */
.mode-tl { background: #0e131a; border-color: var(--line); }
.mode-tl:active { filter: brightness(1.2); }

/* Live mode hides archive-only chrome. */
.app.live #controls,
.app.live #timeline-wrap { display: none; }

/* Version label, bottom-right corner of the page. */
.version {
  position: fixed;
  bottom: 4px; right: 6px;
  color: var(--muted);
  opacity: .5;
  font: 10px/1 ui-monospace, monospace;
  pointer-events: none;
  z-index: 10;
}
.preview {
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  background: #000;
}
.badge {
  position: absolute;
  top: 12px; left: 12px;
  background: #dc2626;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 6px;
  letter-spacing: .04em;
}

/* Timeline */
.timeline-wrap {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 8px 14px 14px;
}
.daynav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding-bottom: 8px;
}
.daybtn {
  background: #0e131a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 13px;
}
.daybtn:disabled { opacity: .35; cursor: default; }
.day-label { font-size: 14px; font-variant-numeric: tabular-nums; min-width: 9ch; text-align: center; }
.clock {
  margin-left: 12px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}
.timeline {
  position: relative;
  height: 44px;
  background: repeating-linear-gradient(90deg, #0c1015 0 1px, #11161d 1px 2px); /* faint ticks */
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  touch-action: none;
  overflow: hidden;
}
/* Availability: shaded spans where recording exists; gaps = no recording. */
.avail { position: absolute; inset: 0; pointer-events: none; }
.avail .seg {
  position: absolute;
  top: 0; bottom: 0;
  background: rgba(59, 130, 246, .35);
  border-left: 1px solid rgba(59, 130, 246, .5);
  border-right: 1px solid rgba(59, 130, 246, .5);
}
/* Future part of the current day: not yet recorded. */
.future {
  position: absolute;
  top: 0; bottom: 0; right: 0;
  background: repeating-linear-gradient(45deg, transparent 0 6px, rgba(255,255,255,.04) 6px 12px);
  border-left: 1px dashed var(--line);
  pointer-events: none;
}
.playhead {
  position: absolute;
  top: -2px; bottom: -2px;
  width: 2px;
  background: #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,.5);
  pointer-events: none;
}
.range {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 12px;
  padding-top: 6px;
  font-variant-numeric: tabular-nums;
}

/* ---- Timelapse ("ускоренный обзор") modal -------------------------------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .6);
  display: grid;
  place-items: center;
  z-index: 20;
  padding: 16px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: min(560px, 100%);
  max-height: 90dvh;
  overflow: auto;
  display: flex;
  flex-direction: column;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  font-size: 15px;
  font-weight: 600;
}
.modal-x {
  background: transparent;
  color: var(--muted);
  border: 0;
  font-size: 18px;
  cursor: pointer;
}
.tl-form { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.tl-field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.tl-form input[type="datetime-local"],
.tl-form input[type="number"],
.tl-form select {
  background: #0e131a;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.tl-end-row { display: flex; gap: 8px; align-items: stretch; }
.tl-end-row input { flex: 1 1 auto; min-width: 0; }
.tl-dur-row { display: flex; gap: 8px; }
.tl-dur-row input { flex: 1 1 auto; min-width: 0; }
.tl-dur-row select { flex: 0 0 auto; }
.tl-speed-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tl-speed-row input { flex: 0 0 92px; }
.tl-speed-quick, .tl-dur-quick { display: flex; gap: 6px; flex-wrap: wrap; }
.tl-mini {
  flex: 0 0 auto;
  background: #0e131a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 14px;
  cursor: pointer;
  font-size: 13px;
}
.tl-mini:active { background: var(--accent); border-color: var(--accent); }
.tl-smooth { display: flex; flex-direction: row; align-items: center; gap: 8px; font-size: 13px; color: var(--text); }
.tl-smooth input { width: 16px; height: 16px; }
.tl-q, .tl-s {
  background: #0e131a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  font-size: 13px;
}
.tl-q:active, .tl-s:active { background: var(--accent); border-color: var(--accent); }
.tl-summary {
  background: #0e131a;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
  font-variant-numeric: tabular-nums;
}
.tl-actions { display: flex; gap: 10px; margin-top: 2px; }
.tl-btn {
  background: #0e131a;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
}
.tl-btn:disabled { opacity: .5; cursor: default; }
.tl-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.tl-status { color: var(--muted); font-size: 13px; min-height: 16px; }
.tl-player { padding: 0 16px 16px; }
.tl-player video { width: 100%; border-radius: 8px; background: #000; }

/* ---- Smartphone ---------------------------------------------------------- */
@media (max-width: 640px) {
  .topbar {
    flex-wrap: wrap;
    gap: 8px 10px;
    padding: 8px 10px;
  }
  .status { margin-left: 0; width: 100%; order: 3; }
  .cameras { flex-wrap: wrap; }
  .cameras button, .mode { padding: 8px 12px; }
  .timeline-wrap { padding: 8px 10px 12px; }
  .timeline { height: 52px; } /* fatter target for finger scrubbing */
  .ctrl { padding: 10px 16px; }
  .debug { font-size: 10px; max-width: 70%; }
}

/* In landscape (or fullscreen) on a phone, give the whole screen to the video:
   hide the timeline/controls chrome; tap the fullscreen button to exit. */
@media (max-width: 900px) and (orientation: landscape) {
  .topbar { padding: 4px 8px; }
  #timeline-wrap { display: none; }
  .controls { padding: 4px; }
}
.app:fullscreen .topbar,
.app:fullscreen #controls,
.app:fullscreen #timeline-wrap { display: none; }
.app:fullscreen .stage { height: 100%; }
