:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --border: #2b303b;
  --text: #e6e8ec;
  --muted: #8b93a3;
  --accent: #4f9cff;
  --danger: #ff6b6b;
  --picker-width: 340px;
  --highlight: #ffc83d;
  --ticker-height: 38px;
  /* Traffic alert categories (ticker badges and map diamonds). */
  --alert-major: #ff5c5c;
  --alert-incident: #ff9f43;
  --alert-closure: #e56bd6;
  --alert-weather: #4dd0e1;
  --alert-event: #6bd88a;
  --alert-workzone: #f5d547;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

button, select, input {
  font: inherit;
  color: inherit;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
}
button { cursor: pointer; }
/* Touch screens keep :hover on the last button tapped, which would look like it's selected. */
@media (hover: hover) {
  button:hover { border-color: var(--accent); }
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { display: flex; align-items: center; gap: 8px; margin: 0; font-size: 18px; }
/* The favicon; its background matches the bar, so just the camera shows. */
.topbar .logo { display: block; flex-shrink: 0; }
.controls { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.controls label { display: flex; gap: 6px; align-items: center; color: var(--muted); }
.dashboards { margin-right: auto; }
.dashboards select { max-width: 260px; }

body { display: flex; flex-direction: column; }

.layout {
  display: flex;
  flex: 1;
  min-height: 0;
}

.picker {
  width: var(--picker-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
.picker.hidden { display: none; }

.filters { padding: 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--border); }
.filters input[type="search"] { width: 100%; }
.filters .row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filters .row select { min-width: 0; }
.check { display: flex; gap: 6px; align-items: center; color: var(--muted); }
.check input { padding: 0; }

.status { margin: 0; padding: 8px 12px; color: var(--muted); font-size: 12px; }
.attribution { margin: 0; padding: 6px 12px; color: var(--muted); font-size: 11px; border-top: 1px solid var(--border); }
.attribution a { color: inherit; }

.camera-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.camera-list li {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.camera-list li:hover { background: var(--panel-2); }
.camera-list li.selected { background: rgba(79, 156, 255, 0.12); }
.camera-list input { margin-top: 3px; padding: 0; }
.camera-list .desc { display: block; }
.camera-list .meta { display: block; color: var(--muted); font-size: 12px; }

.grid {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: grid;
  gap: 12px;
  align-content: start;
  /* Rows must size to their tiles. Without this, tiles (overflow: hidden) get
     squeezed to fit the viewport and the 4:3 video is cropped. */
  grid-auto-rows: max-content;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
}
.grid[data-columns="auto"] { justify-content: center; }
.grid.with-map { justify-content: end; } /* keep the map in the top-right corner */
.grid[data-columns="1"] { grid-template-columns: repeat(1, 1fr); }
.grid[data-columns="2"] { grid-template-columns: repeat(2, 1fr); }
.grid[data-columns="3"] { grid-template-columns: repeat(3, 1fr); }
.grid[data-columns="4"] { grid-template-columns: repeat(4, 1fr); }
.grid[data-columns="5"] { grid-template-columns: repeat(5, 1fr); }
.grid[data-columns="6"] { grid-template-columns: repeat(6, 1fr); }

.empty { grid-column: 1 / -1; color: var(--muted); text-align: center; margin-top: 15vh; }

.tile {
  margin: 0;
  align-self: start; /* its own height, which fitGrid measures, not its row's */
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.tile .media {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #000;
}
.tile video, .tile img, .viewer video, .viewer img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}
.tile .media { cursor: zoom-in; }
.tile.highlight { border-color: var(--highlight); box-shadow: 0 0 0 2px var(--highlight); }

.tile .overlay, .viewer .overlay {
  position: absolute;
  inset: auto 0 0 0;
  padding: 4px 8px;
  font-size: 12px;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
}
.tile .overlay.error, .viewer .overlay.error { color: var(--danger); }
.tile figcaption {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  min-height: 36px; /* same height whether or not the buttons are showing */
  padding: 6px 8px;
  border-top: 1px solid var(--border);
}
.tile .label { display: flex; flex-direction: column; min-width: 0; }
.tile .title, .tile .location { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .title { font-weight: 600; }
.tile .location { color: var(--muted); font-size: 12px; }
.tile .actions { display: flex; gap: 4px; flex-shrink: 0; }
.tile .actions button { padding: 2px 7px; font-size: 12px; }
.tile .actions .icon { display: block; width: 1.15em; height: 1.15em; }
.tile .actions [data-act="remove"]:hover { border-color: var(--danger); color: var(--danger); }
/* On devices with a mouse, show the tile buttons only on hover or keyboard focus,
   so the title gets the full width. Touch devices always show them. */
@media (hover: hover) {
  .tile .actions { display: none; }
  .tile:hover .actions, .tile:focus-within .actions { display: flex; }
}

.viewer {
  width: min(92vw, calc((90vh - 60px) * 4 / 3));
  max-width: none;
  padding: 0;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.viewer::backdrop { background: rgba(0, 0, 0, 0.75); }
.viewer header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
}
.viewer-heading { min-width: 0; }
.viewer-title { margin: 0; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.viewer-meta { color: var(--muted); font-size: 12px; }
.viewer .actions { display: flex; gap: 4px; flex-shrink: 0; }
.viewer .actions .icon { display: block; width: 1.15em; height: 1.15em; }
.viewer .actions [data-act="add"]:hover { border-color: var(--accent); color: var(--accent); }
.viewer .actions [data-act="remove"]:hover { border-color: var(--danger); color: var(--danger); }
.viewer .actions button:disabled { opacity: 0.35; cursor: default; border-color: var(--border); }
.viewer .media { position: relative; aspect-ratio: 4 / 3; background: #000; }

/* Fills the grid's top-right cells (placed by app.js), with the tiles around it. */
.map-panel {
  display: flex;
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel);
}
.map-panel.hidden { display: none; }
.map { flex: 1; background: #0b0d10; }

/* Leaflet controls and tooltips, restyled for the dark UI. */
.map.leaflet-container { font: inherit; font-size: 12px; }
/* Invert the light USGS basemap; rotating hue back keeps water blue and roads red. */
.map .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.85) contrast(0.9); }
.map .leaflet-bar a { background: var(--panel-2); color: var(--text); border-color: var(--border); }
.map .leaflet-bar a:hover { background: var(--border); }
.map .overview-control.hidden { display: none; }
.map .overview-control a { display: flex; align-items: center; justify-content: center; }
.map .overview-control svg { width: 16px; height: 16px; }
.map .leaflet-control-attribution { background: rgba(15, 17, 21, 0.8); color: var(--muted); }
.map .leaflet-control-attribution a { color: var(--text); }
.map .leaflet-tooltip {
  white-space: normal;
  width: max-content;
  max-width: 220px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--highlight);
  box-shadow: none;
}
/* Tooltips sit centered above their icon (app.js offsets them clear of it). */
.map .leaflet-tooltip-top::before { border-top-color: var(--highlight); }
/* Cameras not on the dashboard: grey tooltips and popups. */
.map .other-tooltip { border-color: var(--muted); }
.map .other-tooltip.leaflet-tooltip-top::before { border-top-color: var(--muted); }
.map .tooltip-note { color: var(--muted); }

/* Popups pinned by clicking an icon, also centered above it. */
.map .leaflet-popup { text-align: left; }
.map .leaflet-popup-content-wrapper {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--highlight);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
}
.map .leaflet-popup-tip { background: var(--highlight); box-shadow: none; }
.map .other-popup .leaflet-popup-content-wrapper { border-color: var(--muted); }
.map .other-popup .leaflet-popup-tip { background: var(--muted); }
.map .leaflet-popup-content { margin: 10px 26px 10px 12px; font-size: 12px; line-height: 1.4; }
.map .leaflet-container a.leaflet-popup-close-button,
.map a.leaflet-popup-close-button { color: var(--muted); }
.map a.leaflet-popup-close-button:hover { color: var(--text); }
.popup-camera + .popup-camera { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
.popup-camera button { display: block; margin-top: 6px; }
.popup-meta { color: var(--muted); }
.popup-message { margin: 6px 0 0; }

/* ---------- traffic alerts ---------- */

.alert-major { --alert: var(--alert-major); }
.alert-incident { --alert: var(--alert-incident); }
.alert-closure { --alert: var(--alert-closure); }
.alert-weather { --alert: var(--alert-weather); }
.alert-event { --alert: var(--alert-event); }
.alert-workzone { --alert: var(--alert-workzone); }

.ticker {
  display: flex;
  align-items: stretch;
  height: var(--ticker-height);
  flex-shrink: 0;
  background: var(--panel);
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.ticker-label {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 12px;
  font-weight: 600;
  white-space: nowrap;
  border-right: 1px solid var(--border);
  cursor: help;
}
.ticker-count { color: var(--muted); font-weight: 400; }
.ticker-viewport {
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.ticker-viewport::-webkit-scrollbar { display: none; }
.ticker-track { display: flex; height: 100%; width: max-content; }
.ticker-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  white-space: nowrap;
  border-right: 1px solid var(--border);
  cursor: pointer;
}
.ticker-item:hover, .ticker-item.highlight { background: var(--panel-2); }
.ticker-item.highlight { box-shadow: inset 0 0 0 2px var(--highlight); }
.ticker-badge {
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  color: #0f1115;
  background: var(--alert);
}
.ticker-route { font-weight: 600; }
.ticker-empty { display: flex; align-items: center; padding: 0 16px; color: var(--muted); }

/* Map markers: diamonds, so they read differently from the round camera markers. Leaflet
   positions the outer element with a transform, so all styling is on the inner span. */
.alert-marker { background: none; border: 0; }
.alert-marker span {
  display: block;
  width: 12px;
  height: 12px;
  margin: 2px;
  background: var(--alert);
  border: 2px solid #0f1115;
  transform: rotate(45deg);
  transition: transform 0.12s;
}
.alert-marker.highlight span {
  transform: rotate(45deg) scale(1.6);
  border-color: var(--highlight);
}

@media (max-width: 760px) {
  body { height: auto; }
  .layout { flex-direction: column; height: auto; }
  .ticker { position: sticky; bottom: 0; }
  .picker { width: 100%; max-height: 50vh; border-right: 0; border-bottom: 1px solid var(--border); }
  .grid { grid-template-columns: 1fr !important; overflow: visible; }
  /* Enlarged view: name and details get the full width; the buttons go on a row below. */
  .viewer header { flex-wrap: wrap; gap: 8px; }
  .viewer-heading { flex: 1 1 100%; }
  .viewer .actions { margin-left: auto; }

  /* Map view: the map replaces the cameras, filling the screen between header and ticker. */
  body.map-view { height: 100vh; height: 100dvh; }
  .map-view .layout { flex: 1; min-height: 0; }
  .map-view .picker, .map-view .grid > :not(.map-panel) { display: none; }
  .map-view .grid { flex: 1; display: flex; padding: 0; }
  .map-view .map-panel { flex: 1; border: 0; border-radius: 0; }
  .map-view #toggle-map { border-color: var(--accent); }
}
