:root {
  --bg-0: #0a0d0a;
  --bg-1: #10140f;
  --bg-2: #161c15;
  --line: #2a3428;
  --text-0: #e6f0e3;
  --text-1: #93a68d;
  --text-2: #5c6b57;
  --accent: #6fe36f;
  --accent-dim: #2f6b35;
  --danger: #e35b5b;
  --mono: "JetBrains Mono", "Roboto Mono", ui-monospace, "SF Mono", Consolas, monospace;
  --sans: "Inter", -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg-0);
  color: var(--text-0);
  font-family: var(--sans);
}

button, input, select { font-family: inherit; }

[hidden] { display: none !important; }

/* ------------------------- Топбар ------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 18px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; align-items: center; gap: 8px; }
.brand-mark { color: var(--accent); font-size: 20px; text-shadow: 0 0 8px var(--accent-dim); }
.brand-name { font-family: var(--mono); letter-spacing: 0.08em; font-size: 15px; font-weight: 600; }

.conn-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-1);
  margin-right: auto;
}
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.dot-off { background: var(--text-2); }
.dot-on { background: var(--accent); box-shadow: 0 0 6px var(--accent); }

/* ------------------------- Кнопки ------------------------- */
.btn {
  background: var(--bg-2);
  border: 1px solid var(--line);
  color: var(--text-0);
  padding: 9px 14px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  transition: border-color 0.15s, background 0.15s;
}
.btn:hover:not(:disabled) { border-color: var(--accent-dim); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn-block { width: 100%; margin-top: 8px; }
.btn-ghost { background: transparent; }
.btn-accent { background: var(--accent-dim); border-color: var(--accent); color: #ecffec; }
.btn-accent:hover:not(:disabled) { background: #3a7d40; }
.btn-sm { padding: 4px 8px; font-size: 12px; }

.btn-alarm {
  min-width: 104px;
  border-color: rgba(227, 91, 91, .75);
  background: rgba(227, 91, 91, .12);
  color: #ffdede;
  font-family: var(--mono);
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}

.btn-alarm:hover:not(:disabled) {
  border-color: var(--danger);
  background: rgba(227, 91, 91, .22);
}

.btn-alarm.alarm-active {
  border-color: #ff5a5a;
  background: #8f2020;
  color: #fff;
  box-shadow: 0 0 10px rgba(255, 70, 70, .45);
  animation: cyclops-alarm-blink .75s steps(2, start) infinite;
}

@keyframes cyclops-alarm-blink {
  0%, 100% {
    background: #a51f1f;
    box-shadow: 0 0 8px rgba(255, 70, 70, .35);
  }
  50% {
    background: #ef3030;
    box-shadow: 0 0 22px rgba(255, 70, 70, .9);
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn-alarm.alarm-active {
    animation: none;
    background: #d62d2d;
  }
}

/* ------------------------- Layout ------------------------- */
.layout {
  display: grid;
  grid-template-columns: 1.6fr 1fr;
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 49px);
}

.panel {
  background: var(--bg-1);
  border: 1px solid var(--line);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-head {
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.panel-title {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-1);
}

.panel-foot {
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  display: flex;
  gap: 18px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-1);
}
.panel-foot b { color: var(--text-0); font-weight: 600; }

/* ------------------------- Видео ------------------------- */
.video-frame {
  position: relative;
  flex: 1;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.stream-image {
  display: none;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  background: #000;
}
.stream-image.active-stream { display: block; }

.detection-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  display: none;
}
.detection-canvas.active-stream { display: block; }

.video-overlay {
  position: absolute;
  color: var(--text-2);
  font-family: var(--mono);
  font-size: 13px;
  pointer-events: none;
}

.switch { display: flex; align-items: center; gap: 6px; font-family: var(--mono); font-size: 11px; color: var(--text-1); cursor: pointer; user-select: none; }
.switch input { display: none; }
.switch-track { width: 34px; height: 18px; border-radius: 10px; background: var(--bg-2); border: 1px solid var(--line); position: relative; display: inline-block; }
.switch-thumb { position: absolute; top: 1px; left: 1px; width: 14px; height: 14px; border-radius: 50%; background: var(--text-1); transition: left 0.15s, background 0.15s; }
.switch input:checked + .switch-track { border-color: var(--accent-dim); }
.switch input:checked + .switch-track .switch-thumb { left: 17px; background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.switch input:disabled + .switch-track { opacity: 0.4; cursor: not-allowed; }

/* ------------------------- PTZ-крестовина ------------------------- */
.control-panel {
  padding-bottom: 16px;
  overflow-y: auto;
  align-items: stretch;
}

/*
 * Ручное PTZ больше НЕ flex:1.
 * Это устраняет зависимость позиции крестовины от свободной высоты панели
 * и её наложение на заголовок/следующие элементы.
 */
.manual-ptz-section {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(190px, 0.8fr) minmax(190px, 1fr);
  align-items: center;
  gap: 22px;
  padding: 22px 18px 18px;
  border-bottom: 1px solid var(--line);
}

.ptz-status { font-family: var(--mono); font-size: 10px; color: var(--text-1); }
.ptz-status.ok { color: var(--accent); }
.ptz-status.error { color: var(--danger); }

.ptz-pad-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 168px;
  padding: 6px 0;
}

.ptz-pad {
  display: grid;
  grid-template-columns: 52px 52px 52px;
  grid-template-rows: 46px 46px 46px;
  gap: 6px;
  grid-template-areas:
    ". up ."
    "left center right"
    ". down .";
}

.ptz-btn {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg-2);
  color: var(--text-0);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: border-color .12s, color .12s, background .12s;
  user-select: none;
}

.ptz-btn:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent-dim);
  background: rgba(111, 227, 111, .08);
}

.ptz-btn:active:not(:disabled) { transform: translateY(1px); }
.ptz-btn:disabled { opacity: .35; cursor: not-allowed; }

.ptz-up    { grid-area: up; }
.ptz-down  { grid-area: down; }
.ptz-left  { grid-area: left; }
.ptz-right { grid-area: right; }

.ptz-af {
  grid-area: center;
  border-radius: 50%;
  font-size: 12px;
  font-family: var(--mono);
  font-weight: 700;
  letter-spacing: 0.04em;
}

.ptz-af:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent);
  background: rgba(111, 227, 111, .12);
}

.manual-ptz-options {
  min-width: 0;
}

.step-row {
  width: 100%;
  max-width: 320px;
  margin: 0 auto;
  padding: 0;
}

.step-row select {
  width: 100%;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--bg-2);
  color: var(--text-0);
  padding: 0 9px;
  outline: none;
}

.step-row select:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; }

.home-row {
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 320px;
  margin: 12px auto 0;
  padding: 0;
}

.home-row .btn {
  width: 100%;
}

/* ------------------------- Детекция + автонаведение ------------------------- */
.automation-grid {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 12px;
  padding: 12px 14px 16px;
  align-items: start;
}

.detection-fset {
  width: 100%;
  min-width: 0;
  margin: 0;
}

.detection-fset .field-row {
  padding-left: 10px;
  padding-right: 10px;
}

.detection-fset input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  margin-top: 4px;
}

.detection-classes {
  max-height: 140px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 8px;
  margin-top: 4px;
  font-size: 12px;
  color: var(--text-1);
  background: var(--bg-2);
}

.detection-class-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 0;
  cursor: pointer;
}

.detection-class-row input { width: 14px; height: 14px; accent-color: var(--accent); flex: none; }
.detection-class-row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.detection-row {
  flex-direction: row;
  gap: 8px;
  padding: 0 10px;
  margin-top: 12px;
}

.detection-row .btn { flex: 1; min-width: 0; }

#detectionStatus,
#autocenterStatus {
  margin: 8px 10px 0;
}

.autocenter-row {
  margin-top: 14px;
}

/* ------------------------- Общие поля ------------------------- */
.field-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 0 12px;
  margin-top: 12px;
}
.field-row label { font-size: 11px; color: var(--text-1); }
.field-row input {
  background: var(--bg-2);
  border: 1px solid var(--line);
  color: var(--text-0);
  padding: 7px 9px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 13px;
}
.field-row input:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 0;
  font-size: 13px;
  cursor: pointer;
}
.checkbox-row input { width: 16px; height: 16px; accent-color: var(--accent); }

.hint { font-size: 11px; color: var(--text-2); margin: 6px 12px 0; line-height: 1.4; }

.form-message {
  min-height: 15px;
  margin: 10px 12px 0;
  color: var(--danger);
  font-size: 11px;
  line-height: 1.4;
}
.form-message.success { color: var(--accent); }

/* ------------------------- Settings drawer ------------------------- */
.settings-drawer {
  position: fixed;
  top: 0; right: -420px;
  width: 400px; height: 100%;
  background: var(--bg-1);
  border-left: 1px solid var(--line);
  transition: right 0.2s ease;
  z-index: 1000;
  display: flex;
  flex-direction: column;
}
.settings-drawer.open { right: 0; }
.settings-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--line);
  font-family: var(--mono); font-size: 12px; letter-spacing: 0.08em;
  text-transform: uppercase; color: var(--text-1);
}
.settings-body { overflow-y: auto; padding: 8px 16px 24px; }

.fset {
  border: 1px solid var(--line);
  border-radius: 6px;
  margin-top: 14px;
  padding: 4px 8px 12px;
}
.fset legend { font-family: var(--mono); font-size: 11px; color: var(--accent); padding: 0 6px; letter-spacing: 0.05em; }

.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 999; }

/* ------------------------- Адаптив ------------------------- */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    grid-auto-rows: min-content;
    height: auto;
    overflow-y: auto;
  }

  .video-frame { min-height: 260px; }
  .settings-drawer { width: 92vw; right: -100vw; }

  .manual-ptz-section {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .ptz-pad-wrap {
    min-height: 168px;
  }

  .automation-grid {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 901px) and (max-width: 1180px) {
  /*
   * На небольших desktop-окнах правая колонка уже, поэтому ручное
   * управление складываем вертикально, но две нижние панели оставляем рядом.
   * Это не допускает пересечения крестовины с заголовками.
   */
  .manual-ptz-section {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .ptz-pad-wrap {
    min-height: 168px;
  }

  .automation-grid {
    gap: 8px;
    padding-left: 8px;
    padding-right: 8px;
  }
}

/* ------------------------- Видео / Карта tabs ------------------------- */
.view-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
}
.view-tab {
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--text-1);
  padding: 6px 12px;
  cursor: pointer;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: .05em;
  text-transform: uppercase;
}
.view-tab:hover { color: var(--text-0); border-color: var(--line); }
.view-tab.active { color: var(--accent); border-color: var(--accent-dim); background: rgba(111, 227, 111, .08); }

.map-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 49px);
}
.map-panel { min-width: 0; }
.tracking-map { flex: 1; min-height: 360px; background: #0b0e0b; }
.map-status { font-family: var(--mono); font-size: 11px; color: var(--text-1); }
.map-sidebar { overflow-y: auto; }
.target-grid,
.size-priors {
  display: grid;
  grid-template-columns: minmax(110px, .8fr) minmax(0, 1.2fr);
  gap: 7px 10px;
  padding: 14px;
  font-family: var(--mono);
  font-size: 11px;
}
.target-grid span,
.size-priors span { color: var(--text-1); }
.target-grid b,
.size-priors b { color: var(--text-0); font-weight: 600; overflow-wrap: anywhere; }
.geo-fset { margin: 0 12px 14px; }
.geo-actions { display: grid; grid-template-columns: 1fr; gap: 8px; padding: 12px 10px 0; }

/* Leaflet is light by default; tone controls/popups down to match the app. */
.leaflet-control-zoom a,
.leaflet-control-attribution,
.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--bg-1) !important;
  color: var(--text-0) !important;
}
.leaflet-control-zoom a { border-color: var(--line) !important; }
.target-course-arrow {
  color: var(--accent);
  font-size: 28px;
  line-height: 28px;
  text-align: center;
  text-shadow: 0 0 5px #000;
  transform-origin: 50% 50%;
}

@media (max-width: 900px) {
  .view-tabs { margin-left: 0; }
  .map-layout {
    grid-template-columns: 1fr;
    height: auto;
    overflow-y: auto;
  }
  .tracking-map { min-height: 55vh; }
}

.auth-screen {
  position: fixed;
  inset: 0;
  z-index: 5000;

  display: flex;
  align-items: center;
  justify-content: center;

  background: var(--bg-0);
}

.auth-card {
  width: min(420px, calc(100vw - 32px));

  padding: 28px;

  background: var(--bg-1);
  border: 1px solid var(--line);
}

.auth-title {
  margin-bottom: 6px;

  font-family: var(--mono);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.auth-subtitle {
  margin-bottom: 24px;

  color: var(--text-2);
  font-size: 13px;
}

.auth-card .field-row {
  margin-bottom: 14px;
}

.auth-card .form-message {
  min-height: 22px;
  margin: 8px 0 12px;
}

.admin-only[hidden] {
  display: none !important;
}
