/* =========== RESET & BASE =========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-dark: #1a1a2e;
  --bg-card: #16213e;
  --bg-panel: #0f3460;
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --text: #eee;
  --text-muted: #aab;
  --success: #2ecc71;
  --warning: #f39c12;
  --danger: #e74c3c;
  --border: #2a2a4a;
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-dark);
  color: var(--text);
  min-height: 100vh;
  overflow: hidden;
}

.screen { display: none; width: 100vw; height: 100vh; position: absolute; top: 0; left: 0; }
.screen:not([hidden]) { display: flex; }

/* =========== BUTTONS =========== */
.btn {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-panel); border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { font-size: 24px; padding: 6px 10px; background: transparent; border: none; }
.btn-icon:hover { color: var(--accent); }
.btn svg { width: 1em; height: 1em; vertical-align: middle; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.btn svg .fill { fill: currentColor; stroke: none; }
.btn-active { background: var(--accent); border-color: var(--accent); }
.btn-toggle.active { background: var(--warning); border-color: var(--warning); }
.btn-rec.recording { background: var(--danger); animation: pulse 1s infinite; }
.btn-tl.active { background: var(--warning); }
.btn-patrol.patrolling { background: var(--warning); animation: pulse 1s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* =========== FORM =========== */
input, select, textarea {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-dark);
  color: var(--text);
  font-size: 14px;
  width: 100%;
}
input:focus, select:focus { border-color: var(--accent); outline: none; }
.input-wide { max-width: 600px; }

label { display: block; margin-bottom: 4px; font-size: 13px; color: var(--text-muted); }

.form-row { margin-bottom: 12px; }
.form-row-2col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.form-row-3col { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.form-row-inline { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.form-row-inline input, .form-row-inline select { width: auto; flex: 1; }
.form-row-checkboxes { display: flex; gap: 20px; margin-bottom: 12px; }
.form-row-checkboxes label { display: flex; align-items: center; gap: 6px; }
.form-row-checkboxes input { width: auto; }
.form-actions { display: flex; gap: 8px; margin-top: 16px; }

hr { border: none; border-top: 1px solid var(--border); margin: 8px 0; }

.error-msg { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* =========== LOGIN =========== */
#login-screen {
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, var(--bg-dark), var(--bg-panel));
}
.login-box {
  background: var(--bg-card);
  padding: 40px;
  border-radius: 12px;
  text-align: center;
  min-width: 340px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.login-box h1 { font-size: 32px; margin-bottom: 4px; color: var(--accent); }
.login-box .subtitle { color: var(--text-muted); margin-bottom: 24px; }
.login-box input { margin-bottom: 12px; }
.login-box .btn { width: 100%; }

/* =========== GRID =========== */
#grid-screen { flex-direction: column; }

.grid-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}
.grid-title { font-size: 18px; color: var(--accent); }
.grid-toolbar-right { display: flex; gap: 4px; }

.camera-grid {
  flex: 1;
  display: grid;
  gap: 4px;
  padding: 4px;
  overflow: hidden;
}

.grid-cell {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.2s;
}
.grid-cell:hover { border-color: var(--accent); }
.grid-cell img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.grid-cell-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 6px 10px;
  background: linear-gradient(transparent, rgba(0,0,0,0.8));
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.grid-cell-name { font-size: 12px; font-weight: bold; }
.grid-cell-index {
  position: absolute;
  top: 6px;
  left: 6px;
  background: rgba(0,0,0,0.65);
  color: #fff;
  font-weight: bold;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  z-index: 2;
  pointer-events: none;
}
.grid-cell.drag-over { outline: 3px dashed var(--accent); outline-offset: -3px; }
.grid-cell.dragging { opacity: 0.45; }
.grid-cell img { -webkit-user-drag: none; user-drag: none; pointer-events: none; }
.grid-cell-controls { display: flex; flex-direction: column; gap: 6px; }
.grid-cell-controls .btn-sm { padding: 4px 8px; font-size: 18px; display: inline-flex; align-items: center; justify-content: center; }
.grid-cell-status {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.grid-cell-status.online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.grid-cell-status.offline { background: var(--danger); box-shadow: 0 0 6px var(--danger); }
.grid-cell-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 14px;
}

/* =========== FULLSCREEN =========== */
#fullscreen-screen { flex-direction: row; }

/* Left sidebar */
.fs-sidebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 8px;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  z-index: 10;
  width: 64px;
  min-width: 64px;
}
.btn-sidebar {
  width: 50px;
  height: 50px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-dark);
  color: var(--text);
  cursor: pointer;
}
.btn-sidebar:hover { border-color: var(--accent); color: var(--accent); }
.btn-sidebar.btn-rec.recording { background: var(--danger); animation: pulse 1s infinite; }
.btn-sidebar.btn-tl.active { background: var(--warning); }
.fs-sidebar-spacer { flex: 0 0 1px; width: 80%; background: var(--border); margin: 2px 0; }
.fs-audio-btn[hidden] { display: none !important; }
.fs-audio-btn:not([hidden]) { display: flex !important; }
.fs-ptz-only[hidden] { display: none !important; }
.fs-ptz-only:not([hidden]) { display: flex !important; }
.btn-icon-grid { font-size: 8px; line-height: 0.85; letter-spacing: 2px; }

/* Video area */
.fs-video-area {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  overflow: hidden;
}
.fs-video-frame {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  margin: 8px;
  overflow: hidden;
  background: #111;
}
.fs-stream {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* Floating PTZ panel */
.fs-ptz-float {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background: rgba(22, 33, 62, 0.92);
  border-radius: var(--radius);
  padding: 8px;
  z-index: 20;
  min-width: 160px;
  backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  cursor: default;
}
.fs-ptz-float-handle {
  text-align: center;
  cursor: grab;
  font-size: 13px;
  padding: 2px 0 6px;
  color: var(--text-muted);
  user-select: none;
  font-weight: bold;
}
.fs-ptz-float-handle:hover { color: var(--accent); }
.fs-ptz-float-handle:active { cursor: grabbing; }

/* PTZ Step */
.ptz-step-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 6px;
}
.ptz-step-label {
  font-size: 12px;
  color: var(--text-muted);
}
.ptz-step-label span {
  font-weight: bold;
  color: var(--accent);
}
.btn-xs {
  padding: 2px 8px;
  font-size: 14px;
  font-weight: bold;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-dark);
  color: var(--text);
  cursor: pointer;
  line-height: 1;
}
.btn-xs:hover { background: var(--accent); }

/* PTZ Pad */
.ptz-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  margin: 0 0 4px;
}
.ptz-btn {
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg-dark);
  color: var(--text);
  cursor: pointer;
  font-size: 16px;
  text-align: center;
}
.ptz-btn:hover { background: var(--accent); }
.ptz-btn:active { transform: scale(0.95); }
.ptz-stop { background: var(--danger); }

.zoom-controls { display: flex; gap: 4px; margin-top: 4px; }
.zoom-controls .btn { flex: 1; text-align: center; }

/* Presets */
.preset-mode-toggle { margin-bottom: 6px; }
.preset-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 3px;
}
.preset-grid .btn { padding: 6px 2px; font-size: 11px; text-align: center; }
.preset-grid .btn.has-preset { background: var(--bg-panel); border-color: var(--accent); }

/* Shared video container (used by patrol) */
.fs-video-container {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
}
.fs-video-container .fs-stream {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* =========== PATROL =========== */
.patrol-overlay {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 16px;
  background: rgba(0,0,0,0.6);
  padding: 10px 20px;
  border-radius: var(--radius);
  backdrop-filter: blur(4px);
}
.patrol-cam-name { font-size: 18px; font-weight: bold; }

/* =========== CONFIG =========== */
#config-screen {
  flex-direction: row;
}
#recordings-screen {
  flex-direction: column;
  overflow-y: auto;
}

/* Config sidebar */
.config-sidebar {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 8px;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  min-width: 160px;
  width: 160px;
  overflow-y: auto;
}
.config-menu-btn {
  width: 100%;
  text-align: left;
  font-size: 13px;
  padding: 8px 10px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.config-menu-btn svg { width: 18px; height: 18px; flex-shrink: 0; }
.config-sidebar-spacer {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* Config content area */
.config-content {
  flex: 1;
  overflow-y: auto;
}
.config-container {
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 20px;
}
.config-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.config-header h2 { flex: 1; }

.config-panel {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}

/* Camera list */
.camera-list { margin-bottom: 20px; }
.camera-list-item {
  display: grid;
  grid-template-columns: 10px 1fr 150px 140px 120px 110px;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-card);
  border-radius: var(--radius);
  margin-bottom: 4px;
  border-left: 3px solid transparent;
}
.cam-actions { display: flex; flex-direction: row; gap: 8px; }
.cam-actions .btn { padding: 8px 12px; font-size: 16px; display: inline-flex; align-items: center; justify-content: center; }
.cam-actions .btn svg { width: 20px; height: 20px; }
.camera-list-item.online { border-left-color: var(--success); }
.camera-list-item.offline { border-left-color: var(--danger); }

.cam-status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.cam-status-dot.online { background: var(--success); }
.cam-status-dot.offline { background: var(--danger); }

.cam-info { min-width: 0; }
.cam-info-name { font-weight: bold; font-size: 14px; }
.cam-info-details { font-size: 12px; color: var(--text-muted); }
.cam-info-group { font-size: 11px; color: var(--accent); }

/* =========== SCHEDULE EDITOR =========== */
.schedule-editor { overflow-x: auto; }
.schedule-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.schedule-table th, .schedule-table td { padding: 4px; text-align: center; border: 1px solid var(--border); }
.schedule-table th { background: var(--bg-panel); position: sticky; top: 0; }
.schedule-cell {
  width: 12px;
  height: 20px;
  cursor: pointer;
  transition: background 0.1s;
}
.schedule-cell.active { background: var(--success); }
.schedule-cell:not(.active) { background: var(--bg-dark); }
.schedule-cell:hover { opacity: 0.7; }

/* =========== LOGS =========== */
.log-filters { display: flex; gap: 8px; margin-bottom: 12px; }
.log-filters select { width: auto; }
.log-list { max-height: 400px; overflow-y: auto; }
.log-entry {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  display: grid;
  grid-template-columns: 150px 70px 80px 1fr;
  gap: 8px;
}
.log-entry .log-time { color: var(--text-muted); }
.log-entry .log-level { font-weight: bold; }
.log-entry .log-level.info { color: var(--success); }
.log-entry .log-level.warn { color: var(--warning); }
.log-entry .log-level.error { color: var(--danger); }
.log-entry .log-cat { color: var(--accent); }

/* =========== GROUPS =========== */
.group-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--bg-dark);
  border-radius: var(--radius);
  margin-bottom: 4px;
}

/* =========== RECORDINGS =========== */
.rec-settings {
  background: var(--bg-card);
  padding: 16px;
  border-radius: var(--radius);
  margin-bottom: 20px;
}
.rec-view-toggle {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
}
.rec-view-toggle select { width: auto; max-width: 240px; }

.rec-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}
.rec-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  overflow: hidden;
}
.rec-card-thumb {
  width: 100%;
  height: 140px;
  object-fit: cover;
  background: #000;
  display: block;
}
.rec-card-info {
  padding: 10px;
}
.rec-card-name { font-size: 13px; font-weight: bold; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec-card-meta { font-size: 11px; color: var(--text-muted); margin: 4px 0; }
.rec-card-actions { display: flex; gap: 4px; }

.rec-table { width: 100%; }
.rec-table table { width: 100%; border-collapse: collapse; }
.rec-table th, .rec-table td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); font-size: 13px; }
.rec-table th { background: var(--bg-card); color: var(--text-muted); font-size: 12px; }

/* =========== MODAL =========== */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.8);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal[hidden] { display: none; }
.modal-content {
  position: relative;
  width: 90vw;
  max-width: 960px;
}
.modal-close {
  position: absolute;
  top: -30px;
  right: 0;
  font-size: 28px;
  color: var(--text);
  background: none;
  border: none;
  cursor: pointer;
}
.modal-content video { width: 100%; border-radius: var(--radius); }
.api-modal-content {
  background: var(--bg-card);
  padding: 24px;
  border-radius: var(--radius);
  max-height: 90vh;
  overflow-y: auto;
  max-width: 1100px;
}
.api-modal-content textarea {
  width: 100%;
  resize: vertical;
  font-family: monospace;
}
.api-modal-content h3 { color: var(--accent); }
.api-saved-panel {
  min-width: 220px;
  width: 220px;
  border-right: 1px solid var(--border);
  padding-right: 12px;
  max-height: 70vh;
  overflow-y: auto;
}
.api-saved-panel h4 { color: var(--text-muted); font-size: 13px; margin-bottom: 8px; }
.api-saved-list { display: flex; flex-direction: column; gap: 4px; }
.api-saved-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 8px;
  background: var(--bg-dark);
  border-radius: var(--radius);
  font-size: 12px;
  cursor: pointer;
  border: 1px solid transparent;
}
.api-saved-item:hover { border-color: var(--accent); }
.api-saved-item.selected { border-color: var(--accent); background: var(--bg-panel); }
.api-saved-item-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.api-saved-item-method {
  font-size: 10px;
  font-weight: bold;
  color: var(--accent);
  min-width: 32px;
}
.api-saved-item-actions { display: flex; gap: 2px; }
.api-saved-item-actions button {
  padding: 2px 5px;
  font-size: 10px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.api-saved-item-actions button:hover { color: var(--accent); }

/* =========== TOAST =========== */
.toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 12px 20px;
  border-radius: var(--radius);
  color: #fff;
  font-size: 14px;
  animation: slideIn 0.3s ease;
  max-width: 360px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); }
.toast.info { background: var(--bg-panel); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* =========== RESPONSIVE =========== */
@media (max-width: 768px) {
  .camera-list-item { grid-template-columns: 10px 1fr 80px 40px; }
  .config-header-actions { flex-wrap: wrap; }
  .form-row-2col, .form-row-3col { grid-template-columns: 1fr; }
  .log-entry { grid-template-columns: 1fr; }
  .fs-ptz-float { min-width: 140px; bottom: 10px; right: 10px; }
  .config-sidebar { min-width: 120px; width: 120px; font-size: 12px; }
  .config-menu-btn { font-size: 12px; padding: 6px 8px; }
}
