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

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-surface: #1e2a4a;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b0;
  --text-muted: #6a6a7a;
  --accent: #00b4d8;
  --accent-dim: #0077b6;
  --accent-glow: rgba(0, 180, 216, 0.2);
  --border: #2a2a4a;
  --danger: #e74c3c;
  --success: #2ecc71;
  --warning: #f39c12;
  --sidebar-width: 340px;
  --player-height: 45%;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

/* Sidebar */
#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100%;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 1000;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-header h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 12px;
  letter-spacing: 0.5px;
}

#search-input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  transition: border-color 0.2s;
}

#search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

#search-input::placeholder {
  color: var(--text-muted);
}

.filter-row {
  display: flex;
  gap: 6px;
  margin-top: 10px;
}

.filter-row select {
  flex: 1;
  padding: 6px 4px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-secondary);
  font-size: 11px;
  cursor: pointer;
  outline: none;
}

.filter-row select:focus {
  border-color: var(--accent);
}

#camera-count {
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Camera list */
#camera-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
}

#camera-list li {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

#camera-list li:hover {
  background: var(--bg-surface);
}

#camera-list li.active {
  background: var(--bg-tertiary);
  border-left: 3px solid var(--accent);
}

.cam-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.cam-meta {
  font-size: 11px;
  color: var(--text-muted);
}

.cam-status {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

.cam-status.enabled {
  background: var(--success);
}

.cam-status.disabled {
  background: var(--danger);
}

/* Map */
#map {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  right: 0;
  bottom: 0;
  z-index: 1;
  background: #0e0e12;
}

/* Player panel */
#player-panel {
  position: fixed;
  bottom: 0;
  left: var(--sidebar-width);
  right: 0;
  height: var(--player-height);
  background: var(--bg-primary);
  border-top: 2px solid var(--accent);
  z-index: 500;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
}

#player-panel.hidden {
  transform: translateY(100%);
  pointer-events: none;
}

.player-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#player-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
}

#player-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 22px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
  transition: color 0.15s;
}

#player-close:hover {
  color: var(--danger);
}

#player-container {
  flex: 1;
  min-height: 0;
  background: #000;
  position: relative;
  overflow: hidden;
}

#video-player {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#player-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 16px;
  background: rgba(0, 0, 0, 0.8);
}

#player-error.hidden {
  display: none;
}

/* Leaflet marker overrides */
.camera-marker {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, 0.5);
  transition: transform 0.15s;
}

.camera-marker:hover {
  transform: scale(1.6);
}

.camera-marker.enabled {
  background: var(--accent);
  box-shadow: 0 0 6px var(--accent-glow);
}

.camera-marker.disabled {
  background: var(--text-muted);
}

.camera-marker.active {
  background: var(--warning);
  border-color: #fff;
  transform: scale(1.8);
  box-shadow: 0 0 10px rgba(243, 156, 18, 0.5);
}

/* Layer toggles */
.layer-toggles {
  margin-top: 8px;
  display: flex;
  gap: 10px;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
}

.toggle-label input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

/* Event markers (diamond shape) */
.event-marker {
  width: 10px;
  height: 10px;
  transform: rotate(45deg);
  border: 1.5px solid;
  border-radius: 2px;
  transition: transform 0.15s;
}

.event-marker:hover {
  transform: rotate(45deg) scale(1.6);
}

/* Event popups */
.event-popup-container .leaflet-popup-content-wrapper {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.event-popup-container .leaflet-popup-tip {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
}

.event-popup-container .leaflet-popup-close-button {
  color: var(--text-muted);
}

.event-popup-container .leaflet-popup-close-button:hover {
  color: var(--text-primary);
}

.event-popup {
  font-size: 13px;
  line-height: 1.4;
}

.event-popup-title {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 4px;
  color: var(--text-primary);
}

.event-popup-type {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: capitalize;
}

.event-popup-desc {
  margin-bottom: 4px;
  color: var(--text-secondary);
}

.event-popup-lanes {
  font-size: 12px;
  color: var(--warning);
  margin-bottom: 2px;
}

.event-popup-comment {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
  margin-bottom: 4px;
}

.event-popup-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
  border-top: 1px solid var(--border);
  padding-top: 4px;
}

/* Scrollbar */
#camera-list::-webkit-scrollbar {
  width: 6px;
}

#camera-list::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

#camera-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

#camera-list::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* Loading state */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  color: var(--text-muted);
}

.loading::after {
  content: "";
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-left: 10px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Grid page */
.page-nav {
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

.page-nav a {
  color: var(--accent);
  text-decoration: none;
}

.page-nav a:hover {
  text-decoration: underline;
}

.page-nav-current {
  color: var(--text-primary);
}

#grid-main {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  right: 0;
  bottom: 0;
  background: var(--bg-primary);
  padding: 10px;
  overflow-y: auto;
}

#grid-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  font-size: 15px;
}

#grid-empty.hidden {
  display: none;
}

#video-grid {
  display: grid;
  gap: 10px;
  height: 100%;
}

.grid-tile {
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
  border-radius: 6px;
  overflow: hidden;
  min-height: 0;
}

.grid-tile-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--text-secondary);
}

.grid-tile-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.grid-tile-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
}

.grid-tile-close:hover {
  color: var(--danger);
}

.grid-tile-body {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #000;
}

.grid-tile-body video {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.grid-tile-error {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--danger);
  background: rgba(0, 0, 0, 0.8);
  font-size: 13px;
}

.grid-tile-error.hidden {
  display: none;
}

#camera-list li.no-stream {
  opacity: 0.45;
  cursor: default;
}

/* Data freshness indicator */
.data-status {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-muted);
}

.data-status.stale {
  color: var(--warning);
  font-weight: 600;
}

/* Full closure emphasis */
.event-marker.closure {
  width: 14px;
  height: 14px;
  border-width: 2px;
  animation: closure-pulse 1.5s ease-out infinite;
}

@keyframes closure-pulse {
  0% { box-shadow: 0 0 0 0 rgba(255, 0, 110, 0.6); }
  70% { box-shadow: 0 0 0 10px rgba(255, 0, 110, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 0, 110, 0); }
}

.event-popup-closure {
  color: #ff006e;
  font-weight: 700;
  font-size: 12px;
  margin-bottom: 4px;
  letter-spacing: 0.5px;
}

/* Road conditions legend */
#conditions-legend {
  position: fixed;
  right: 10px;
  bottom: 28px;
  z-index: 400;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 11px;
  color: var(--text-secondary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.legend-title {
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 5px;
  font-size: 12px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 6px;
  line-height: 1.7;
}

.legend-swatch {
  display: inline-block;
  width: 18px;
  height: 4px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-group + .legend-group {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* Mobile drawer controls (hidden on desktop) */
#sidebar-toggle,
#sidebar-backdrop {
  display: none;
}

/* Mobile layout */
@media (max-width: 768px) {
  #sidebar {
    width: min(85vw, 340px);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.5);
  }

  body.sidebar-open #sidebar {
    transform: translateX(0);
  }

  #sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 1100;
    width: 42px;
    height: 42px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 20px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  }

  body.sidebar-open #sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 900;
    background: rgba(0, 0, 0, 0.5);
  }

  #map,
  #grid-main,
  #player-panel {
    left: 0;
  }

  /* Keep Leaflet zoom controls clear of the drawer toggle */
  .leaflet-top.leaflet-left {
    top: 52px;
  }

  /* 16px inputs prevent iOS auto-zoom on focus */
  #search-input,
  .filter-row select {
    font-size: 16px;
  }

  .filter-row {
    flex-direction: column;
  }

  #camera-list li {
    padding: 12px 16px;
  }

  #conditions-legend {
    font-size: 10px;
    padding: 6px 8px;
    bottom: 24px;
  }

  #conditions-legend .legend-title {
    font-size: 11px;
  }

  /* Grid page: scroll tiles vertically instead of squeezing them */
  #video-grid {
    height: auto;
  }

  .grid-tile {
    height: 240px;
  }

  .grid-tile-close {
    font-size: 22px;
    padding: 4px 8px;
  }
}
