/* ═══════════════════════════════════════════════
   MONITOR THE SITUATION — White / Black
   ═══════════════════════════════════════════════ */

:root {
  --bg:        #ffffff;
  --bg2:       #f5f5f5;
  --bg3:       #ebebeb;
  --border:    #cccccc;
  --border2:   #aaaaaa;
  --accent:    #000000;
  --accent2:   #333333;
  --accent3:   #ff6b35;
  --text:      #111111;
  --text-dim:  #555555;
  --text-meta: #888888;
  --danger:    #cc0000;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Courier New', monospace;
  --radius:    2px;
  --sidebar-w: 280px;
  --header-h:  44px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
  overflow: hidden;
}

/* ══ HEADER ═══════════════════════════════════ */
#header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 20px;
  z-index: 100;
  letter-spacing: .08em;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.logo-bracket { color: var(--accent3); font-size: 16px; font-weight: 700; }
.logo-text    { color: #000; font-size: 13px; font-weight: 700; letter-spacing: .1em; }
.logo-sub     { color: var(--text-dim); font-size: 10px; letter-spacing: .12em; }

.header-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.stat-label { color: var(--text-dim); font-size: 10px; letter-spacing: .12em; }
.stat-value { color: var(--accent); font-size: 13px; font-weight: 700; min-width: 28px; text-align: center; }
.stat-sep   { color: var(--border2); }

.header-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}
.clock          { color: var(--accent); font-size: 13px; font-weight: 700; letter-spacing: .1em; }
.tz             { color: var(--text-dim); font-size: 10px; }
/* .presence-label { color: var(--text-dim); font-size: 10px; letter-spacing: .1em; } */
/* .presence-count { color: var(--accent3); font-size: 13px; font-weight: 700; min-width: 20px; } */

/* ══ MAIN LAYOUT ══════════════════════════════ */
#main {
  position: fixed;
  top: var(--header-h);
  left: 0; right: 0; bottom: 0;
  display: flex;
  overflow: hidden;
}

/* ══ SIDEBAR ══════════════════════════════════ */
#sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
}

#sidebar::-webkit-scrollbar { width: 4px; }
#sidebar::-webkit-scrollbar-track { background: var(--bg2); }
#sidebar::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg3);
}

.panel-label {
  color: var(--accent);
  font-size: 10px;
  letter-spacing: .14em;
  font-weight: 700;
}

/* ── MAP ── */
#map-container {
  position: relative;
  flex: 0 0 auto;
  height: 240px;
  min-height: 80px;
}

#map-resizer {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 18px;
  height: 18px;
  cursor: nwse-resize;
  z-index: 600;
  /* Three diagonal grip lines */
  background:
    linear-gradient(135deg, transparent calc(50% - 0.5px), var(--border) calc(50% - 0.5px), var(--border) calc(50% + 0.5px), transparent calc(50% + 0.5px)),
    linear-gradient(135deg, transparent calc(70% - 0.5px), var(--border) calc(70% - 0.5px), var(--border) calc(70% + 0.5px), transparent calc(70% + 0.5px)),
    linear-gradient(135deg, transparent calc(30% - 0.5px), var(--border) calc(30% - 0.5px), var(--border) calc(30% + 0.5px), transparent calc(30% + 0.5px));
  opacity: 0.6;
  transition: opacity .15s;
}
#map-resizer:hover,
#map-resizer.dragging {
  background:
    linear-gradient(135deg, transparent calc(50% - 0.5px), var(--accent3) calc(50% - 0.5px), var(--accent3) calc(50% + 0.5px), transparent calc(50% + 0.5px)),
    linear-gradient(135deg, transparent calc(70% - 0.5px), var(--accent3) calc(70% - 0.5px), var(--accent3) calc(70% + 0.5px), transparent calc(70% + 0.5px)),
    linear-gradient(135deg, transparent calc(30% - 0.5px), var(--accent3) calc(30% - 0.5px), var(--accent3) calc(30% + 0.5px), transparent calc(30% + 0.5px));
  opacity: 1;
}

#map {
  width: 100%;
  height: 100%;
  background: #f0f0f0;
}


/* Leaflet — CartoDB Positron (white map, no invert needed) */
.leaflet-container { background: #f0f0f0 !important; }
.leaflet-control-attribution { display: none !important; }
.leaflet-control-zoom a {
  background: #fff !important;
  color: #000 !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--bg3) !important; }
.leaflet-popup-content-wrapper {
  background: #fff !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  box-shadow: 0 4px 16px rgba(0,0,0,.15) !important;
}
.leaflet-popup-tip { background: #fff !important; }

/* ── FILTER SECTIONS ── */
.filter-section {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

.filter-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.filter-row:last-child { margin-bottom: 0; }

.filter-label {
  color: var(--text-dim);
  font-size: 9px;
  letter-spacing: .14em;
  flex: 0 0 44px;
}

.filter-input {
  flex: 1;
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 4px 8px;
  border-radius: var(--radius);
  outline: none;
  transition: border-color .15s;
}
.filter-input:focus { border-color: var(--accent); }
.filter-input::placeholder { color: var(--text-meta); }

.filter-select {
  flex: 1;
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 4px 8px;
  border-radius: var(--radius);
  outline: none;
}
.filter-select:focus { border-color: var(--accent); }

.filter-value { color: var(--accent); font-size: 10px; min-width: 36px; font-weight: 700; }

input[type="range"] {
  flex: 1;
  accent-color: var(--accent3);
  cursor: pointer;
}

/* ── BUTTONS ── */
.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 3px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  letter-spacing: .08em;
  transition: all .12s;
}
.btn-ghost:hover  { border-color: var(--accent3); color: var(--accent3); }
.btn-ghost.active { border-color: var(--accent); color: var(--accent); background: rgba(0,0,0,.06); }

.btn-apply {
  flex: 1;
  background: var(--accent);
  border: 1px solid var(--accent);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 5px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  letter-spacing: .1em;
  font-weight: 700;
  transition: all .12s;
}
.btn-apply:hover { background: #333; border-color: #333; }

.btn-group { display: flex; gap: 4px; }

.btn-mode {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 9px;
  padding: 3px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  letter-spacing: .08em;
  transition: all .12s;
}
.btn-mode.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.region-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
}

.btn-region {
  background: #fff;
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 9px;
  padding: 5px 4px;
  border-radius: var(--radius);
  cursor: pointer;
  letter-spacing: .06em;
  transition: all .12s;
  text-align: center;
}
.btn-region:hover { border-color: var(--accent3); color: var(--accent3); }


/* ══ GRID PANEL ═══════════════════════════════ */
#grid-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

#grid-panel .panel-header {
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.grid-size-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.refresh-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 12px;
}

.grid-info {
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .1em;
  min-width: 80px;
  text-align: center;
}

/* ── LOADING ── */
#loading-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.loading-inner {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.loading-logo {
  color: var(--accent);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .3em;
  animation: pulse 1.4s ease-in-out infinite;
}

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

.loading-bar-wrap {
  width: 240px;
  height: 2px;
  background: var(--border);
  border-radius: 1px;
  overflow: hidden;
}

.loading-bar {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width .3s ease;
}

.loading-status {
  color: var(--text-dim);
  font-size: 10px;
  letter-spacing: .12em;
}

/* ── CAMERA GRID ── */
#camera-grid {
  flex: 1;
  overflow: hidden;
  padding: 4px;
  display: grid;
  grid-template-columns: repeat(5, 1fr); /* overridden by JS */
  gap: 3px;
  align-content: start;
}

#camera-grid::-webkit-scrollbar { width: 6px; }
#camera-grid::-webkit-scrollbar-track { background: var(--bg); }
#camera-grid::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }

/* ── CAMERA CELL ── */
.cam-cell {
  position: relative;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  overflow: hidden;
  aspect-ratio: 16/9;
  transition: border-color .15s;
}
.cam-cell:hover, .cam-cell.map-hover {
  border-color: var(--accent3);
  border-width: 1.5px;
  z-index: 2;
  box-shadow: 0 0 0 1.5px var(--accent3), 0 0 10px rgba(255,107,53,.6);
}
.cam-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #e8e8e8;
}



/* Always-visible status dot */
.cam-status {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 5px rgba(34,197,94,.6);
}
.cam-status.err { background: var(--danger); box-shadow: 0 0 5px rgba(204,0,0,.5); }

/* Error state */
.cam-cell.error .cam-img { opacity: .2; }
.cam-cell.error::after {
  content: '// FEED UNAVAILABLE';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-meta);
  font-size: 9px;
  letter-spacing: .1em;
}

#no-results {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.no-results-text {
  color: var(--text-meta);
  font-size: 11px;
  letter-spacing: .12em;
}

/* ══ MODAL ════════════════════════════════════ */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fade-in .1s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

#modal {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(1100px, 92vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 8px 40px rgba(0,0,0,.25);
  animation: slide-up .12s ease;
  overflow: hidden;
}

@keyframes slide-up {
  from { transform: translateY(8px); opacity: .6; }
  to   { transform: translateY(0);   opacity: 1; }
}

#modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg3);
  flex: 0 0 auto;
}

#modal-title {
  color: #000;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: .1em;
}

.modal-controls { display: flex; align-items: center; gap: 6px; }
.modal-meta { color: var(--text-dim); font-size: 10px; letter-spacing: .08em; }

.btn-close {
  background: rgba(204,0,0,.08);
  border: 1px solid rgba(204,0,0,.3);
  color: var(--danger);
  font-size: 12px;
  width: 24px;
  height: 24px;
  border-radius: var(--radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .12s;
  font-family: var(--font-mono);
}
.btn-close:hover { background: rgba(204,0,0,.2); border-color: var(--danger); }

#modal-img-wrap {
  position: relative;
  flex: 1;
  overflow: hidden;
  background: #111;
  min-height: 0;
}

#modal-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  max-height: calc(90vh - 90px);
}

.spinner {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,.6);
}
.spinner::after {
  content: '';
  width: 32px;
  height: 32px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#modal-timestamp {
  position: absolute;
  top: 8px;
  right: 10px;
  background: rgba(0,0,0,.6);
  color: #ddd;
  font-size: 9px;
  padding: 2px 7px;
  letter-spacing: .08em;
  font-family: var(--font-mono);
}

#modal-footer {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg3);
  flex: 0 0 auto;
}

/* ══ FULLSCREEN MODAL ════════════════════════ */

/* Native Fullscreen API */
#modal-overlay:fullscreen,
#modal-overlay:-webkit-full-screen {
  background: #000;
}
#modal-overlay:fullscreen #modal,
#modal-overlay:-webkit-full-screen #modal {
  width: 100vw !important;
  height: 100vh !important;
  max-height: 100vh !important;
  border-radius: 0 !important;
  border: none !important;
  box-shadow: none !important;
}
#modal-overlay:fullscreen #modal-img,
#modal-overlay:-webkit-full-screen #modal-img {
  max-height: calc(100vh - 88px);
}

/* CSS fallback fullscreen (iOS Safari / unsupported browsers) */
#modal-overlay.fs-active {
  background: #000;
  z-index: 9999;
}
#modal-overlay.fs-active #modal {
  width: 100vw !important;
  height: 100vh !important;
  max-height: 100vh !important;
  border-radius: 0 !important;
  border: none !important;
  box-shadow: none !important;
}
#modal-overlay.fs-active #modal-img {
  max-height: calc(100vh - 88px);
}

/* Fullscreen button — always visible, highlights when active */
#btn-fullscreen { font-size: 14px; padding: 2px 7px; }
#btn-fullscreen.active {
  border-color: var(--accent3);
  color: var(--accent3);
}

/* Swipe hint shown briefly when modal opens on touch devices */
#modal-img-wrap .swipe-hint {
  position: absolute;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,.5);
  color: rgba(255,255,255,.8);
  font-size: 9px;
  letter-spacing: .14em;
  padding: 3px 12px;
  border-radius: 10px;
  pointer-events: none;
  animation: hint-fade 2s ease forwards;
  white-space: nowrap;
}
@keyframes hint-fade {
  0%   { opacity: 0; }
  20%  { opacity: 1; }
  70%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ══ KEYBOARD HINT ══════════════════════════ */
.kbd-hint {
  position: fixed;
  bottom: 8px;
  right: 12px;
  color: var(--text-meta);
  font-size: 9px;
  letter-spacing: .08em;
  z-index: 50;
  pointer-events: none;
}

/* ══ RESPONSIVE ══════════════════════════════ */
@media (max-width: 900px) {
  :root { --sidebar-w: 220px; }
}

/* ── Mobile toggle button: hidden on desktop ── */
#btn-map-toggle { display: none; }

@media (max-width: 600px) {
  /* Header: compact — hide stats and subtitle */
  #btn-map-toggle  { display: inline-flex; }
  .header-center   { display: none; }
  .logo-sub        { display: none; }
  .header-right    { gap: 8px; }

  /* Stack sidebar above grid */
  #main {
    flex-direction: column;
    overflow: hidden;
  }

  /* Sidebar: hidden by default, shown via toggle */
  #sidebar {
    display: none;
    width: 100% !important;
    flex: 0 0 auto !important;
    max-height: 55vh;
    overflow-y: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  #sidebar.mobile-open { display: flex; }

  #map-container { height: 160px; }

  /* 4-column region grid on mobile */
  .region-grid { grid-template-columns: repeat(4, 1fr); }

  /* Hide the corner resize handle */
  #map-resizer { display: none; }

  /* Grid panel: fill remaining height, make scrollable */
  #grid-panel {
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }
  #camera-grid {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    align-content: start;
  }

  /* Compact panel header */
  #grid-panel .panel-header { flex-wrap: wrap; gap: 6px; padding: 6px 10px; }
  .refresh-controls { margin-left: 0; }

  /* Larger tap targets */
  .btn-ghost  { padding: 6px 10px; font-size: 11px; }
  .btn-region { padding: 8px 4px; }

  /* Modal: full-screen on mobile */
  #modal {
    width: 100vw !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
    border-left: none !important;
    border-right: none !important;
  }
  #modal-img { max-height: calc(100vh - 90px); }
}

/* ── corner brackets on camera cells ── */
.cam-cell::before {
  content: '';
  position: absolute;
  top: 4px; left: 4px;
  width: 8px; height: 8px;
  border-top: 1px solid rgba(0,0,0,.25);
  border-left: 1px solid rgba(0,0,0,.25);
  z-index: 1;
  pointer-events: none;
}
.cam-cell::after {
  content: '';
  position: absolute;
  bottom: 4px; right: 4px;
  width: 8px; height: 8px;
  border-bottom: 1px solid rgba(0,0,0,.25);
  border-right: 1px solid rgba(0,0,0,.25);
  z-index: 1;
  pointer-events: none;
}
/* override error state's ::after */
.cam-cell.error::after {
  content: '// FEED UNAVAILABLE';
  width: auto; height: auto;
  border: none;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-meta);
  font-size: 9px;
  letter-spacing: .1em;
  font-family: var(--font-mono);
}

/* draw-mode cursor */
#map.draw-mode { cursor: crosshair !important; }

/* selected camera highlight */
.cam-cell.selected { border-color: var(--accent3); box-shadow: 0 0 8px rgba(255,107,53,.3); }
