/* ── Reset & tokens ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0B1120;
  --surface:     #131c2e;
  --surface-2:   #1a263d;
  --border:      #243047;
  --accent:      #3B82F6;
  --accent-dim:  #1d4ed8;
  --live-green:  #10B981;
  --live-amber:  #F59E0B;
  --live-red:    #EF4444;
  --text:        #e2e8f0;
  --text-muted:  #64748b;
  --text-dim:    #94a3b8;
  --sidebar-w:   330px;
  --radius:      8px;
  --font:        -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

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

/* ── Login page ──────────────────────────────────────────────────────────── */
body.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  overflow: auto;
  background: var(--bg);
}

.login-card {
  width: 100%;
  max-width: 360px;
  padding: 48px 36px 40px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-icon svg {
  width: 48px;
  height: 48px;
  color: var(--accent);
}

.login-card h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.login-tagline {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: -8px;
}

.login-error {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius);
  color: #fca5a5;
  font-size: 0.85rem;
  padding: 10px 14px;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-card input[type="password"] {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
}

.login-card input[type="password"]:focus {
  border-color: var(--accent);
}

.login-card button[type="submit"] {
  padding: 12px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}

.login-card button[type="submit"]:hover { background: var(--accent-dim); }

/* ── Map page layout ─────────────────────────────────────────────────────── */
body.map-page {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  height: 100%;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 10;
  transition: margin-left 0.3s ease;
}

body.sidebar-collapsed #sidebar {
  margin-left: calc(-1 * var(--sidebar-w));
}

/* ── Sidebar tab (collapse handle) ───────────────────────────────────────── */
.sidebar-tab {
  position: fixed;
  left: var(--sidebar-w);
  top: 50%;
  transform: translateY(-50%);
  z-index: 20;
  width: 18px;
  height: 52px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 6px 6px 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: left 0.3s ease, color 0.15s, background 0.15s;
  padding: 0;
}

.sidebar-tab:hover {
  background: var(--surface-2);
  color: var(--text);
}

body.sidebar-collapsed .sidebar-tab {
  left: 0;
}

#map {
  flex: 1;
  height: 100%;
  z-index: 1;
}

/* ── Sidebar header ──────────────────────────────────────────────────────── */
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px 16px 16px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--text);
}

.brand-icon {
  width: 18px;
  height: 18px;
  color: var(--accent);
  flex-shrink: 0;
}

.logout-link {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 4px 8px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
.logout-link:hover { color: var(--text); background: var(--surface-2); }

/* ── Live status bar ─────────────────────────────────────────────────────── */
.fix-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
  flex-shrink: 0;
}
.fix-dot--online  { background: var(--live-green); animation: pulse-dot 2s infinite; }
.fix-dot--stale   { background: var(--live-amber); }
.fix-dot--offline { background: var(--text-muted); }

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.35; }
}


/* ── Stats grid ──────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1px;
  background: var(--border);
  border-bottom: 1px solid var(--border);
}

.stat-card {
  background: var(--surface);
  padding: 12px 14px;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}
.stat-label--row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.follow-inline {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  user-select: none;
}
.follow-inline input[type="checkbox"] {
  width: 13px;
  height: 13px;
  accent-color: var(--live-green);
  cursor: pointer;
}
.follow-inline:hover { color: var(--text); }

.stat-value {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-left: 10px;
}
.stat-value--wrap {
  white-space: normal;
  overflow: visible;
  text-overflow: unset;
}
.trip-section {
  margin-top: auto;
}
.dist-grid {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 4px;
  padding-left: 10px;
}
.dist-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
}
.dist-row:hover { background: rgba(255,255,255,0.05); }
.dist-row--active { background: rgba(255,255,255,0.08); }
.dist-period {
  font-size: 0.78rem;
  color: #ffffff;
}
.dist-val {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  text-align: right;
}

/* ── Section label ───────────────────────────────────────────────────────── */
.section-label {
  padding: 14px 16px 8px;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ── Quick filters ───────────────────────────────────────────────────────── */
.quick-filters {
  display: flex;
  gap: 6px;
  padding: 0 16px 12px;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 5px 10px;
  font-size: 0.78rem;
  font-weight: 500;
  font-family: var(--font);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.15s;
}

.filter-btn:hover { border-color: var(--accent); color: var(--text); }
.filter-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ── Date inputs ─────────────────────────────────────────────────────────── */
.date-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 16px 10px;
}

.date-row input[type="date"] {
  flex: 1;
  min-width: 0;
  padding: 7px 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.78rem;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
  color-scheme: dark;
}

.date-row input[type="date"]:focus { border-color: var(--accent); }
.date-sep { font-size: 0.8rem; color: var(--text-muted); flex-shrink: 0; }

.apply-btn {
  width: calc(100% - 32px);
  margin: 0 16px 16px;
  padding: 9px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}
.apply-btn:hover { background: var(--accent-dim); }

/* ── Follow toggle ───────────────────────────────────────────────────────── */
.follow-label {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 16px;
  font-size: 0.82rem;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}

.follow-label input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: var(--live-green);
  cursor: pointer;
  flex-shrink: 0;
}

.follow-label:hover span { color: var(--text); }

/* ── No data message ─────────────────────────────────────────────────────── */
.no-data {
  margin: 0 16px;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.82rem;
  color: var(--text-muted);
  text-align: center;
}

.hidden { display: none !important; }

/* ── Spinner overlay ─────────────────────────────────────────────────────── */
.spinner {
  position: fixed;
  inset: 0;
  background: rgba(11, 17, 32, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(2px);
}

.spinner-ring {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

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

/* ── Leaflet overrides ───────────────────────────────────────────────────── */
.leaflet-container { background: #0d1623; }

.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--surface-2) !important; }

.leaflet-control-attribution {
  background: rgba(11, 17, 32, 0.75) !important;
  color: var(--text-muted) !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--text-dim) !important; }

/* ── Live marker ─────────────────────────────────────────────────────────── */
.live-marker-icon { position: relative; width: 48px; height: 48px; }

.live-marker-pulse {
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  background: rgba(16, 185, 129, 0.2);
  animation: live-pulse 2s ease-out infinite;
}

.live-van {
  position: absolute;
  inset: 0;
  width: 48px;
  height: 48px;
  filter: drop-shadow(0 0 3px rgba(0,0,0,0.9)) drop-shadow(0 2px 4px rgba(0,0,0,0.6));
  transition: transform 0.4s ease;
}

@keyframes live-pulse {
  0%   { transform: scale(0.6); opacity: 1; }
  100% { transform: scale(2.2); opacity: 0; }
}

/* ── Stationary dot marker ───────────────────────────────────────────────── */
.live-stationary-icon { position: relative; width: 20px; height: 20px; }
.live-stationary-dot {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: #3B82F6;
  box-shadow: 0 0 0 2px white, 0 2px 4px rgba(0,0,0,0.4);
}
.live-stationary-pulse {
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  background: rgba(59, 130, 246, 0.3);
  animation: live-pulse 2s ease-out infinite;
}

/* ── Leaflet popup ───────────────────────────────────────────────────────── */
.leaflet-popup-content-wrapper {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5) !important;
  font-family: var(--font) !important;
  font-size: 0.82rem !important;
}

.leaflet-popup-tip { background: var(--surface) !important; }
.leaflet-popup-close-button { color: var(--text-muted) !important; }
.popup-time { color: var(--text-muted); font-size: 0.75rem; margin-bottom: 4px; }
@keyframes blink-colon { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
.blink-colon { animation: blink-colon 1s step-end infinite; }
.popup-row  { display: flex; justify-content: space-between; gap: 16px; }
.popup-row span { color: var(--text-dim); }
.popup-row strong { color: var(--text); }

/* ── Photo pins — SVG teardrop, styled inline in app.js ─────────────────── */

/* ── Photo popup thumbnail ───────────────────────────────────────────────── */
.photo-thumb {
  display: block;
  max-width: 180px;
  width: 100%;
  border-radius: 4px;
  margin-top: 6px;
  cursor: pointer;
  transition: opacity 0.15s;
}
.photo-thumb:hover { opacity: 0.85; }

.photo-caption {
  font-size: 0.82rem;
  color: var(--text-dim);
  line-height: 1.4;
  margin: 4px 0 6px;
  font-style: italic;
}

.popup-tap-hint {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 4px;
  text-align: center;
}

/* ── Admin bar ───────────────────────────────────────────────────────────── */
.admin-bar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
}

.admin-upload-link {
  display: block;
  width: 100%;
  padding: 8px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 500;
  text-decoration: none;
  text-align: center;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.admin-upload-link:hover {
  border-color: var(--accent);
  color: var(--text);
  background: var(--surface);
}

.db-size-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  padding: 0 2px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.db-size-label { text-transform: uppercase; letter-spacing: 0.04em; }
.db-size-bar-track {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin-top: 6px;
  overflow: hidden;
}
.db-size-bar-fill {
  height: 100%;
  border-radius: 2px;
  background: var(--live-green);
  transition: width 0.4s ease;
}

.popup-add-photo {
  display: block;
  margin-top: 8px;
  padding: 5px 10px;
  background: rgba(59, 130, 246, 0.1);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: 5px;
  color: #93c5fd;
  font-size: 0.75rem;
  text-align: center;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s;
}
.popup-add-photo:hover {
  background: rgba(59, 130, 246, 0.25);
  border-color: rgba(59, 130, 246, 0.6);
}

.photo-delete-btn {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 5px 10px;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 5px;
  color: #fca5a5;
  font-size: 0.75rem;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.photo-delete-btn:hover {
  background: rgba(239, 68, 68, 0.25);
  border-color: rgba(239, 68, 68, 0.6);
}

/* ── Lightbox ────────────────────────────────────────────────────────────── */
#lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#lightbox.hidden { display: none !important; }

#lightboxImg {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.8);
  display: block;
}

#lightboxClose {
  position: absolute;
  top: 16px;
  right: 20px;
  background: none;
  border: none;
  color: #fff;
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
  opacity: 0.75;
  padding: 4px 8px;
  border-radius: 4px;
  font-family: var(--font);
  transition: opacity 0.15s, background 0.15s;
}
#lightboxClose:hover { opacity: 1; background: rgba(255, 255, 255, 0.12); }

/* ── Mobile ──────────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
  body.map-page { flex-direction: column; }

  #sidebar {
    width: 100%;
    min-width: unset;
    margin-left: 0;
    height: auto;
    max-height: 45vh;
    border-right: none;
    border-bottom: 1px solid var(--border);
    flex-direction: row;
    flex-wrap: wrap;
    overflow-y: auto;
    transition: max-height 0.3s ease;
  }

  body.sidebar-collapsed #sidebar {
    width: 100%;
    min-width: unset;
    margin-left: 0;
    max-height: 0;
    border-bottom: none;
  }

  .sidebar-tab {
    position: relative;
    left: auto !important;
    top: auto;
    transform: none;
    width: 100%;
    height: 18px;
    border-radius: 0 0 6px 6px;
    border-left: 1px solid var(--border);
    border-top: none;
    border-right: 1px solid var(--border);
    writing-mode: horizontal-tb;
    flex-shrink: 0;
  }

  .sidebar-header { width: 100%; }
  .stats-grid { width: 100%; }
  .admin-bar { width: 100%; }
  .section-label, .quick-filters, .date-row, .apply-btn, .no-data { width: 100%; }
  .trip-section { margin-top: 0; width: 100%; }

  #map { flex: 1; min-height: 55vh; }
}
