:root {
  --bg-dark: #0f1216;
  --bg-card: #161b22;
  --bg-card-hover: #1c232c;
  --bg-input: #0d1117;
  --border: #2b3542;
  --border-focus: #71dcff;
  --text-main: #f0f6fc;
  --text-muted: #8b949e;
  --accent-blue: #71dcff;
  --accent-green: #3fb950;
  --accent-red: #f85149;
  --accent-orange: #d29922;
  --sidebar-w: 240px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body {
  background-color: var(--bg-dark);
  color: var(--text-main);
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0.75rem;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0 0.5rem 1.25rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}

.brand svg {
  color: var(--accent-blue);
}

.brand h2 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand span {
  color: var(--accent-blue);
}

.nav-menu {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.85rem;
  border-radius: 6px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-item:hover {
  background: var(--bg-card-hover);
  color: var(--text-main);
}

.nav-item.active {
  background: rgba(113, 220, 255, 0.12);
  color: var(--accent-blue);
  font-weight: 600;
}

.sidebar-footer {
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
}

.storage-bar-container {
  margin-top: 0.5rem;
}

.storage-bar {
  height: 6px;
  background: var(--bg-input);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 4px;
}

.storage-bar-fill {
  height: 100%;
  background: var(--accent-blue);
  width: 0%;
  transition: width 0.3s;
}

/* Main Content */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  background: var(--bg-dark);
}

.topbar {
  height: 56px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  flex-shrink: 0;
}

.topbar-title {
  font-size: 1.15rem;
  font-weight: 600;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.content-pane {
  padding: 1.5rem;
  display: none;
}

.content-pane.active {
  display: block;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.85rem;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-main);
  transition: all 0.15s ease;
}

.btn:hover {
  background: var(--bg-card-hover);
  border-color: #3f4e61;
}

.btn-primary {
  background: #1a44ba;
  border-color: #2655dd;
  color: #fff;
}

.btn-primary:hover {
  background: #2355e3;
}

.btn-success {
  background: #1f6f32;
  border-color: var(--accent-green);
  color: #fff;
}

.btn-danger {
  background: #8b1d1d;
  border-color: var(--accent-red);
  color: #fff;
}

.btn-danger:hover {
  background: #a92424;
}

.btn-sm {
  padding: 0.25rem 0.55rem;
  font-size: 0.75rem;
}

/* Grid controls */
.grid-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.view-matrix {
  display: flex;
  gap: 0.25rem;
  background: var(--bg-card);
  padding: 0.25rem;
  border-radius: 6px;
  border: 1px solid var(--border);
}

.matrix-btn {
  padding: 0.25rem 0.6rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 600;
}

.matrix-btn.active, .matrix-btn:hover {
  background: var(--bg-input);
  color: var(--accent-blue);
}

/* Video Grid */
.cctv-grid {
  display: grid;
  gap: 0.75rem;
  width: 100%;
}

.cctv-grid.grid-1, .cctv-grid.layout-1x1 { grid-template-columns: 1fr; }
.cctv-grid.grid-2, .cctv-grid.layout-2x2 { grid-template-columns: repeat(2, 1fr); }
.cctv-grid.grid-3, .cctv-grid.layout-3x3 { grid-template-columns: repeat(3, 1fr); }
.cctv-grid.grid-4, .cctv-grid.layout-4x4 { grid-template-columns: repeat(4, 1fr); }

/* Asymmetric Hero Layouts */
.cctv-grid.layout-1p5 {
  grid-template-columns: repeat(3, 1fr);
}
.cctv-grid.layout-1p5 > .cam-card:nth-child(1) {
  grid-column: span 2;
  grid-row: span 2;
}

.cctv-grid.layout-1p7 {
  grid-template-columns: repeat(4, 1fr);
}
.cctv-grid.layout-1p7 > .cam-card:nth-child(1) {
  grid-column: span 3;
  grid-row: span 3;
}

.cctv-grid.layout-1p11 {
  grid-template-columns: repeat(4, 1fr);
}
.cctv-grid.layout-1p11 > .cam-card:nth-child(1) {
  grid-column: span 2;
  grid-row: span 2;
}

.cctv-grid.layout-2p8 {
  grid-template-columns: repeat(4, 1fr);
}
.cctv-grid.layout-2p8 > .cam-card:nth-child(1),
.cctv-grid.layout-2p8 > .cam-card:nth-child(2) {
  grid-column: span 2;
  grid-row: span 2;
}

.cam-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  aspect-ratio: 16 / 9;
}

.cam-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 0.4rem 0.65rem;
  background: linear-gradient(180deg, rgba(0,0,0,0.85) 0%, transparent 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 5;
  font-size: 0.8rem;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
}

.cam-title {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 600;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.status-dot.online { background: var(--accent-green); box-shadow: 0 0 6px var(--accent-green); }
.status-dot.offline { background: var(--text-muted); }

.cam-video-container {
  width: 100%;
  height: 100%;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cam-video-container video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Cards & Tables */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.card-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.table-responsive {
  width: 100%;
  overflow-x: auto;
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  text-align: left;
}

table.data-table th {
  padding: 0.65rem 0.85rem;
  background: var(--bg-input);
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

table.data-table td {
  padding: 0.75rem 0.85rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

table.data-table tr:hover td {
  background: var(--bg-card-hover);
}

.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 600;
}

.badge-green { background: rgba(63, 185, 80, 0.15); color: var(--accent-green); border: 1px solid rgba(63, 185, 80, 0.4); }
.badge-blue { background: rgba(113, 220, 255, 0.15); color: var(--accent-blue); border: 1px solid rgba(113, 220, 255, 0.4); }
.badge-gray { background: rgba(139, 148, 158, 0.15); color: var(--text-muted); border: 1px solid rgba(139, 148, 158, 0.4); }

/* Timeline & Scrubber */
.timeline-container {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  margin-top: 1rem;
}

.timeline-bar-wrapper {
  position: relative;
  height: 48px;
  background: #11161d;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  overflow: hidden;
  margin: 1rem 0;
}

.timeline-hours {
  display: flex;
  justify-content: space-between;
  padding: 2px 4px;
  font-size: 0.7rem;
  color: var(--text-muted);
  border-bottom: 1px solid rgba(255,255,255,0.06);
}

.timeline-segments {
  position: relative;
  height: 28px;
  width: 100%;
}

.timeline-segment {
  position: absolute;
  top: 4px;
  bottom: 4px;
  background: rgba(63, 185, 80, 0.6);
  border-radius: 2px;
  border-left: 1px solid var(--accent-green);
  border-right: 1px solid var(--accent-green);
}

.timeline-cursor {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--accent-red);
  box-shadow: 0 0 6px var(--accent-red);
  pointer-events: none;
  z-index: 10;
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

.modal-overlay.active {
  display: flex;
}

.modal-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 100%;
  max-width: 580px;
  box-shadow: 0 12px 36px rgba(0,0,0,0.5);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
}

.modal-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  font-size: 1.05rem;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.2rem;
}

.modal-body {
  padding: 1.25rem;
  overflow-y: auto;
}

.modal-footer {
  padding: 0.85rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* Forms */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
  font-weight: 500;
}

.form-control {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border-radius: 6px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-main);
  font-size: 0.85rem;
}

.form-control:focus {
  outline: none;
  border-color: var(--accent-blue);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

/* Code block */
pre.code-block {
  background: var(--bg-input);
  border: 1px solid var(--border);
  padding: 0.75rem;
  border-radius: 6px;
  font-family: monospace;
  font-size: 0.78rem;
  color: #c9d1d9;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 200px;
  overflow-y: auto;
}

/* Modal Sub-tabs */
.modal-tabs {
  display: flex;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}

.modal-tab-btn {
  background: none;
  border: none;
  padding: 0.5rem 0.8rem;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.15s ease;
}

.modal-tab-btn:hover {
  color: var(--text-main);
}

.modal-tab-btn.active {
  color: var(--accent-blue);
  border-bottom-color: var(--accent-blue);
  font-weight: 600;
}

.modal-tab-content {
  display: none;
}

.modal-tab-content.active {
  display: block;
}

.badge-purple {
  background: rgba(163, 113, 247, 0.15);
  color: #d2a8ff;
  border: 1px solid rgba(163, 113, 247, 0.4);
}

/* Wall & Kiosk Monitor Mode */
body.kiosk-mode .sidebar,
body.kiosk-mode .topbar,
body.kiosk-mode .grid-controls,
body.kiosk-mode header {
  display: none !important;
}

body.kiosk-mode .main-content {
  margin: 0 !important;
  padding: 4px !important;
  width: 100vw !important;
  height: 100vh !important;
  max-width: 100vw !important;
  max-height: 100vh !important;
  background: #000 !important;
  overflow: hidden !important;
}

body.kiosk-mode .content-pane {
  padding: 0 !important;
  height: 100vh !important;
}

body.kiosk-mode .cctv-grid {
  height: 100vh !important;
  gap: 4px !important;
}

body.kiosk-mode .cam-card {
  height: 100% !important;
  aspect-ratio: auto !important;
  border-radius: 4px;
}

/* Display Slots Config */
.disp-slot-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem;
  transition: border-color 0.15s ease;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.disp-slot-card:focus-within {
  border-color: var(--accent-blue);
}

/* Modal Preview Grid Layouts */
.matrix-preview-grid {
  display: grid;
  gap: 0.5rem;
  width: 100%;
}
.matrix-preview-grid.layout-1x1 { grid-template-columns: 1fr; }
.matrix-preview-grid.layout-2x2 { grid-template-columns: repeat(2, 1fr); }
.matrix-preview-grid.layout-3x3 { grid-template-columns: repeat(3, 1fr); }
.matrix-preview-grid.layout-4x4 { grid-template-columns: repeat(4, 1fr); }

.matrix-preview-grid.layout-1p5 {
  grid-template-columns: repeat(3, 1fr);
}
.matrix-preview-grid.layout-1p5 > .disp-slot-card:nth-child(1) {
  grid-column: span 2;
  grid-row: span 2;
}

.matrix-preview-grid.layout-1p7 {
  grid-template-columns: repeat(4, 1fr);
}
.matrix-preview-grid.layout-1p7 > .disp-slot-card:nth-child(1) {
  grid-column: span 3;
  grid-row: span 3;
}

.matrix-preview-grid.layout-1p11 {
  grid-template-columns: repeat(4, 1fr);
}
.matrix-preview-grid.layout-1p11 > .disp-slot-card:nth-child(1) {
  grid-column: span 2;
  grid-row: span 2;
}

.matrix-preview-grid.layout-2p8 {
  grid-template-columns: repeat(4, 1fr);
}
.matrix-preview-grid.layout-2p8 > .disp-slot-card:nth-child(1),
.matrix-preview-grid.layout-2p8 > .disp-slot-card:nth-child(2) {
  grid-column: span 2;
  grid-row: span 2;
}

.slot-cam-row {
  display: flex;
  gap: 4px;
  align-items: center;
  margin-bottom: 4px;
}

.slot-cam-row select {
  font-size: 0.78rem;
  padding: 0.25rem 0.45rem;
  flex: 1;
}

.slot-btn-add {
  background: rgba(88, 166, 255, 0.1);
  color: var(--accent-blue);
  border: 1px dashed rgba(88, 166, 255, 0.4);
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 600;
  width: 100%;
  padding: 3px 6px;
  cursor: pointer;
  transition: all 0.15s ease;
  margin-top: 4px;
}

.slot-btn-add:hover {
  background: rgba(88, 166, 255, 0.2);
  border-color: var(--accent-blue);
}

.patrol-indicator {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  background: rgba(163, 113, 247, 0.18);
  color: #d2a8ff;
  border: 1px solid rgba(163, 113, 247, 0.35);
  animation: pulse-patrol 2.5s infinite;
}

@keyframes pulse-patrol {
  0%, 100% { opacity: 0.85; }
  50% { opacity: 1; border-color: #d2a8ff; }
}
