/* ==========================================================================
   DOMARGO Camera Monitor — Main Stylesheet
   ========================================================================== */

/* ---------- CSS Variables ---------- */
:root {
  /* Backgrounds */
  --bg-body: #0a0a0a;
  --bg-header: #111111;
  --bg-card: linear-gradient(145deg, #1a1a1a, #0f0f0f);
  --bg-object: linear-gradient(90deg, #1a1a1a, #0f0f0f);
  --bg-elevated: #1a1a1a;
  --bg-input: #0a0a0a;
  --bg-subtle: #222222;

  /* Text */
  --text-primary: #ffffff;
  --text-secondary: #e0e0e0;
  --text-muted: #888888;
  --text-dim: #666666;

  /* Borders */
  --border-default: #2a2a2a;
  --border-header: #333333;
  --border-accent: #444444;

  /* Status: Online (green) */
  --status-online: #2ecc71;
  --status-online-bg: rgba(46, 204, 113, 0.15);
  --status-online-border: rgba(46, 204, 113, 0.3);

  /* Status: Offline (red) */
  --status-offline: #e74c3c;
  --status-offline-bg: rgba(231, 76, 60, 0.15);
  --status-offline-border: rgba(231, 76, 60, 0.3);

  /* Accent: Admin (orange) */
  --accent-admin: #e66a22;
  --accent-admin-bg: rgba(230, 106, 34, 0.15);
  --accent-admin-border: rgba(230, 106, 34, 0.3);

  /* Accent: Info (blue) */
  --accent-info: #3498db;
  --accent-info-bg: rgba(52, 152, 219, 0.15);
  --accent-info-border: rgba(52, 152, 219, 0.3);

  /* Accent: Warning (yellow) */
  --accent-warning: #f39c12;
  --accent-warning-bg: rgba(243, 156, 18, 0.15);
  --accent-warning-border: rgba(243, 156, 18, 0.3);

  /* Font & Spacing */
  --font-base: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --space-xs: 8px;
  --space-sm: 12px;
  --space-md: 16px;
  --space-lg: 20px;
  --space-xl: 24px;
  --space-2xl: 32px;
}

/* ---------- Base Reset ---------- */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-base);
  background-color: var(--bg-body);
  color: var(--text-secondary);
  line-height: 1.5;
  font-weight: 300;
}

/* ---------- Utility ---------- */
.hidden {
  display: none !important;
}

/* ==========================================================================
   LOGIN
   ========================================================================== */
.login-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.95);
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.2s,
    visibility 0.2s;
}

.login-overlay.active {
  opacity: 1;
  visibility: visible;
}

.login-card {
  background: var(--bg-elevated);
  padding: 32px;
  border: 1px solid var(--border-header);
  width: 100%;
  max-width: 400px;
}

.login-card h2 {
  text-align: center;
  margin-bottom: 24px;
  color: var(--text-primary);
  font-weight: 400;
  letter-spacing: 1px;
}

.input-group {
  margin-bottom: 16px;
}

.input-group label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-muted);
  font-size: 0.9em;
}

.input-group input {
  width: 100%;
  padding: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-header);
  color: var(--text-primary);
  font-size: 1em;
  font-family: inherit;
}

.input-group input:focus {
  outline: none;
  border-color: var(--text-primary);
}

#login-btn {
  width: 100%;
  padding: 10px;
  background: #222;
  color: var(--text-primary);
  border: 1px solid var(--text-primary);
  font-size: 1em;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.2s;
}

#login-btn:hover {
  background: var(--border-header);
}

#login-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.error-msg {
  color: var(--status-offline);
  text-align: center;
  margin-top: 12px;
  font-size: 0.9em;
  min-height: 18px;
}

/* ==========================================================================
   HEADER
   ========================================================================== */
header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--space-md);
  background: var(--bg-header);
  padding: var(--space-md);
  border-bottom: 1px solid var(--border-header);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  justify-content: flex-start;
}

.header-center {
  display: flex;
  justify-content: center;
}

.header-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-sm);
}

.header-buttons {
  display: flex;
  gap: 8px;
}

#header-logo {
  height: 40px;
  width: auto;
}

h1 {
  font-weight: 300;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--text-primary);
  font-size: 1.4em;
  margin: 0;
}

#current-time {
  color: var(--text-muted);
  font-size: 0.95em;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

/* ==========================================================================
   GLOBAL STATS
   ========================================================================== */
.global-stats {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: var(--space-lg);
  background: var(--bg-object);
  padding: var(--space-sm) var(--space-md);
  border-bottom: 1px solid var(--border-header);
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-value {
  font-size: 1.3em;
  font-weight: 500;
  color: var(--text-primary);
}

.stat-label {
  font-size: 0.75em;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 1px;
}

#total-online {
  color: var(--status-online);
}
#total-offline {
  color: var(--status-offline);
}

.stat-divider {
  width: 1px;
  height: 20px;
  background: var(--border-accent);
}

/* ==========================================================================
   OBJECTS & CAMERA CARDS
   ========================================================================== */
.objects-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--space-xl) var(--space-md) var(--space-2xl);
}

.object-section {
  margin-top: 1rem;
}

.object-header {
  background: var(--bg-object);
  padding: var(--space-sm) var(--space-lg);
  margin-bottom: var(--space-md);
  border-left: 3px solid var(--border-accent);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-xs);
}

.object-name {
  font-size: 1.1em;
  font-weight: 400;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-primary);
}

.object-stats {
  font-size: 0.85em;
  color: var(--text-dim);
  display: flex;
  gap: var(--space-md);
}

.stat-online {
  color: var(--status-online);
}
.stat-offline {
  color: var(--status-offline);
}

.cameras-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--space-md);
}

.camera-card {
  background: var(--bg-card);
  padding: var(--space-md);
  border: 1px solid var(--border-default);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 120px;
  cursor: pointer;
  transition: border-color 0.2s;
}

.camera-card:hover {
  border-color: var(--border-accent);
}

.camera-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
  background: var(--border-accent);
}

.status-online::before {
  background: var(--status-online);
}
.status-offline::before {
  background: var(--status-offline);
}

.camera-name {
  font-size: 1.05em;
  font-weight: 400;
  margin-bottom: var(--space-xs);
  color: var(--text-primary);
}

.camera-info {
  color: var(--text-muted);
  font-size: 0.85em;
  margin-bottom: 4px;
}

.camera-offline-time {
  margin-top: 6px;
  font-size: 0.78em;
  color: var(--status-offline);
  font-weight: 500;
  letter-spacing: 0.3px;
  opacity: 0.9;
}
/* ---------- Status Indicator (badge style) ---------- */
.status-indicator {
  display: inline-block;
  padding: 3px 8px;
  font-size: 0.75em;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-top: auto;
  font-weight: 500;
  align-self: flex-start;
  border: 1px solid;
}

.status-online .status-indicator {
  background: var(--status-online-bg);
  color: var(--status-online);
  border-color: var(--status-online-border);
}

.status-offline .status-indicator {
  background: var(--status-offline-bg);
  color: var(--status-offline);
  border-color: var(--status-offline-border);
}

/* ==========================================================================
   UNIFIED BUTTON SYSTEM (btn-indicator)
   ========================================================================== */
.btn-indicator {
  display: inline-block;
  padding: 6px 12px;
  font-size: 0.85em;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid;
  background: transparent;
  font-family: inherit;
  transition: opacity 0.2s;
  text-decoration: none;
}

.btn-indicator:hover {
  opacity: 0.8;
}

.btn-indicator.admin {
  background: var(--accent-admin-bg);
  color: var(--accent-admin);
  border-color: var(--accent-admin-border);
}

.btn-indicator.logout {
  background: var(--status-offline-bg);
  color: var(--status-offline);
  border-color: var(--status-offline-border);
}

.btn-indicator.success {
  background: var(--status-online-bg);
  color: var(--status-online);
  border-color: var(--status-online-border);
}

.btn-indicator.info {
  background: var(--accent-info-bg);
  color: var(--accent-info);
  border-color: var(--accent-info-border);
}

.btn-indicator.warning {
  background: var(--accent-warning-bg);
  color: var(--accent-warning);
  border-color: var(--accent-warning-border);
}

.btn-indicator.danger {
  background: var(--status-offline-bg);
  color: var(--status-offline);
  border-color: var(--status-offline-border);
}

/* ==========================================================================
   LOADING & EMPTY STATES
   ========================================================================== */
.loading,
.empty-state {
  text-align: center;
  padding: var(--space-2xl);
  color: var(--text-dim);
}

/* ==========================================================================
   MODAL SYSTEM
   ========================================================================== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.9);
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.2s,
    visibility 0.2s;
}

.modal.active {
  opacity: 1;
  visibility: visible;
}

.modal-content {
  background: #111;
  border: 1px solid var(--border-header);
  width: 90%;
  max-width: 1200px;
  height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-compact {
  max-width: 600px;
}

.modal-header {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-header);
  background: var(--bg-elevated);
}

.modal-header h2 {
  font-size: 1.1em;
  font-weight: 400;
  color: var(--text-primary);
  margin: 0;
}

.modal-controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

.modal-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  padding: 20px;
}

.btn-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.8em;
  cursor: pointer;
  line-height: 1;
  padding: 0 4px;
  transition: color 0.2s;
}

.btn-close:hover {
  color: var(--text-primary);
}

/* ==========================================================================
   VIDEO PLAYER
   ========================================================================== */
.video-container {
  flex-grow: 1;
  background: #000;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  min-height: 300px;
}

#stream-image {
  max-width: 100%;
  max-height: 80vh;
  display: none;
}

#stream-image.active {
  display: block;
}

.loading-indicator {
  color: var(--text-dim);
  font-size: 1em;
  position: absolute;
}

.mode-btn {
  background: var(--border-header);
  color: var(--text-primary);
  border: none;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 0.85em;
  font-weight: 500;
  font-family: inherit;
  transition: background 0.2s;
}

.mode-btn:hover {
  background: var(--border-accent);
}

.mode-btn.active {
  background: var(--status-online) !important;
  color: #000 !important;
}

/* ==========================================================================
   ADMIN PANEL
   ========================================================================== */
.admin-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border-header);
  padding-bottom: 8px;
}

.admin-tabs .mode-btn {
  padding: 8px 16px;
}

.admin-tab-content {
  display: none;
}

.admin-tab-content.active {
  display: block;
}

.admin-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.btn-action {
  border: 1px solid;
  padding: 10px 20px;
  cursor: pointer;
  font-size: 0.95em;
  font-weight: 500;
  font-family: inherit;
  transition: opacity 0.2s;
}

.btn-action:hover {
  opacity: 0.85;
}

.btn-add {
  background: var(--status-online-bg);
  color: var(--status-online);
  border-color: var(--status-online-border);
}

.btn-reload {
  background: var(--accent-info-bg);
  color: var(--accent-info);
  border-color: var(--accent-info-border);
}

/* ---------- Table Action Buttons ---------- */
.btn-edit,
.btn-toggle,
.btn-delete {
  border: 1px solid;
  padding: 5px 12px;
  font-size: 0.85em;
  cursor: pointer;
  font-family: inherit;
  transition: opacity 0.2s;
  background: transparent;
}

.btn-edit:hover,
.btn-toggle:hover,
.btn-delete:hover {
  opacity: 0.8;
}

.btn-edit {
  background: var(--accent-info-bg);
  color: var(--accent-info);
  border-color: var(--accent-info-border);
  margin-right: 6px;
}

.btn-toggle {
  background: var(--status-offline-bg);
  color: var(--status-offline);
  border-color: var(--status-offline-border);
  margin-right: 6px;
}

.btn-toggle.on {
  background: var(--status-online-bg);
  color: var(--status-online);
  border-color: var(--status-online-border);
}

.btn-delete {
  background: var(--status-offline-bg);
  color: var(--status-offline);
  border-color: var(--status-offline-border);
}

/* ==========================================================================
   TABLE
   ========================================================================== */
.table-wrapper {
  width: 100%;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9em;
  min-width: 800px;
}

th {
  text-align: left;
  padding: 12px;
  border-bottom: 2px solid var(--border-accent);
  color: #aaa;
  font-weight: 500;
  white-space: nowrap;
}

td {
  padding: 12px;
  border-bottom: 1px solid #222;
  color: #ddd;
}

tr:hover td {
  background: var(--bg-elevated);
}

/* ==========================================================================
   STATS TAB
   ========================================================================== */
.stats-controls {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

#stats-period {
  background: var(--bg-input);
  border: 1px solid var(--border-header);
  color: var(--text-primary);
  padding: 8px 12px;
  font-size: 0.95em;
  font-family: inherit;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 16px;
}

.stats-empty {
  grid-column: 1 / -1;
}

/* Stats card (inline styles moved to classes) */
.stats-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border-header);
  padding: 20px;
}

.stats-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.stats-card-title {
  font-size: 1.1em;
  color: var(--text-primary);
}

.stats-card-percent {
  font-weight: 600;
  font-size: 1.2em;
}

.stats-card-bar {
  background: var(--bg-subtle);
  height: 32px;
  overflow: hidden;
  margin-bottom: 16px;
  position: relative;
}

.stats-card-bar-fill {
  height: 100%;
  transition: width 0.6s ease;
}

.stats-card-bar-fill.good {
  background: linear-gradient(90deg, #2ecc71, #27ae60);
}

.stats-card-bar-fill.warn {
  background: linear-gradient(90deg, #f39c12, #e67e22);
}

.stats-card-bar-fill.bad {
  background: linear-gradient(90deg, #e74c3c, #c0392b);
}

.stats-card-bar-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.stats-card-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 12px;
}

.stats-card-metric {
  background: var(--bg-subtle);
  padding: 12px;
  text-align: center;
}

.stats-card-metric-value {
  font-size: 1.3em;
  font-weight: 600;
}

.stats-card-metric-value.online {
  color: var(--status-online);
}

.stats-card-metric-value.offline {
  color: var(--status-offline);
}

.stats-card-metric-label {
  color: var(--text-dim);
  font-size: 0.85em;
}

.stats-card-footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.9em;
}

.stats-card-footer span {
  color: var(--accent-info);
  font-weight: 600;
}

/* ==========================================================================
   FORM
   ========================================================================== */
.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  color: var(--text-muted);
  font-size: 0.9em;
}

.form-group input:not([type="checkbox"]),
.form-group select {
  width: 100%;
  padding: 9px;
  background: var(--bg-input);
  border: 1px solid var(--border-header);
  color: var(--text-primary);
  font-size: 0.95em;
  font-family: inherit;
}

.form-group input:focus {
  outline: none;
  border-color: var(--text-primary);
}

.form-checkbox {
  display: flex;
  align-items: center;
  gap: 10px;
}

.form-checkbox input {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.form-checkbox label {
  margin: 0;
  cursor: pointer;
}

.btn-submit {
  width: 100%;
  padding: 10px;
  background: var(--status-online-bg);
  color: var(--status-online);
  border: 1px solid var(--status-online-border);
  font-size: 0.95em;
  font-weight: 600;
  cursor: pointer;
  margin-top: 10px;
  font-family: inherit;
  transition: opacity 0.2s;
}

.btn-submit:hover {
  opacity: 0.85;
}

/* ==========================================================================
   RESPONSIVE
   ========================================================================== */
@media (max-width: 768px) {
  header {
    grid-template-columns: 1fr;
    gap: var(--space-sm);
    text-align: center;
  }

  .header-left,
  .header-center,
  .header-right {
    justify-content: center;
  }

  .header-right {
    align-items: center;
  }

  .object-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .cameras-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-sm);
  }

  .camera-card {
    padding: var(--space-sm);
    min-height: 90px;
  }

  .camera-name {
    font-size: 0.9em;
    margin-bottom: 4px;
  }

  .camera-info {
    font-size: 0.75em;
    margin-bottom: 2px;
  }

  .status-indicator {
    font-size: 0.65em;
    padding: 2px 6px;
  }

  .modal-content {
    width: 95%;
    height: 90vh;
  }

  .stats-container {
    grid-template-columns: 1fr;
  }
}

/* ==========================================================================
   PWA NATIVE-LIKE BEHAVIOR (ЧИСТОВАЯ ВЕРСИЯ)
   ========================================================================== */

/* 1. Запрет выделения текста и вызова меню (кроме полей ввода) */
*:not(input):not(textarea):not([contenteditable]) {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

/* 2. Разрешаем выделение и ввод в полях */
input, textarea, [contenteditable] {
  -webkit-user-select: text !important;
  user-select: text !important;
  -webkit-touch-callout: default !important;
}

input[type="password"] {
  autocorrect: off;
  autocapitalize: off;
  spellcheck: false;
}

/* 3. Базовый скролл и запрет "пружинивания" (bounce) на iOS/Android */
html, body {
  overscroll-behavior: none; /* Запрещает pull-to-refresh и bounce */
  margin: 0;
  padding: 0;
}

body {
  /* Естественный поток документа, но с плавным скроллом на iOS */
  -webkit-overflow-scrolling: touch;
}

/* 4. УПРАВЛЕНИЕ ЖЕСТАМИ (Самое важное) */
/* Глобально: разрешаем только вертикальный скролл, запрещаем зум и горизонтальный свайп */
html, body {
  touch-action: pan-y;
}

/* Контейнерам явно указываем, что вертикальный свайп — это скролл страницы, а не попытка клика */
.camera-card, 
.object-section, 
.modal-body, 
.table-wrapper,
#objects-container {
  touch-action: pan-y;
}

/* ИСКЛЮЧЕНИЕ: Видео-контейнеру разрешаем ВСЕ жесты (включая pinch-to-zoom) */
.video-container,
.video-container * {
  touch-action: auto !important;
}

/* 5. Скрытие скроллбара для эстетики */
body::-webkit-scrollbar {
  width: 0;
  height: 0;
  background: transparent;
}
body {
  -ms-overflow-style: none;
  scrollbar-width: none;
}