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

:root {
  --sidebar-width: 300px;
  --color-bg: #f0f2f5;
  --color-sidebar: #ffffff;
  --color-text: #1a1a2e;
  --color-text-muted: #6b7280;
  --color-primary: #2563eb;
  --color-border: #e5e7eb;
  --color-bus: #2563eb;
  --color-trolleybus: #dc2626;
  --color-other: #7c3aed;
  --color-hover: #f3f4f6;
  --color-active: #dbeafe;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.15);
  --radius: 8px;
}

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

/* ===== Layout ===== */
#map {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100%;
  background: var(--color-sidebar);
  box-shadow: var(--shadow-md);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  clip-path: inset(0 0 0 0);
  transition: clip-path 0.3s ease;
}

#sidebar.collapsed {
  clip-path: inset(0 100% 0 0);
  pointer-events: none;
}

#sidebar .sidebar-header {
  position: relative;
}

#collapse-sidebar {
  position: absolute;
  top: 50%;
  right: 12px;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
  z-index: 1;
}

#collapse-sidebar:hover {
  background: var(--color-hover);
  color: var(--color-text);
}

/* Expand button (visible only when collapsed, desktop only) */
#expand-sidebar {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1100;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius);
  background: var(--color-sidebar);
  box-shadow: var(--shadow-md);
  font-size: 1.2rem;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  color: var(--color-text);
  transition: background 0.15s;
}

#expand-sidebar:hover {
  background: var(--color-hover);
}

/* ===== Sidebar Header ===== */
.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--color-border);
}

.sidebar-header h1 {
  font-size: 1.25rem;
  font-weight: 700;
}

.subtitle {
  font-size: 0.8rem;
  color: var(--color-text-muted);
  margin-top: 2px;
}

/* ===== Search ===== */
.search-box {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  gap: 8px;
}

#route-search {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.2s;
}

#route-search:focus {
  border-color: var(--color-primary);
}

#clear-search {
  background: none;
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 4px 8px;
  border-radius: 4px;
  display: none;
}

#clear-search:hover {
  background: var(--color-hover);
}

/* ===== Feature Filters ===== */
.feature-filters {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--color-border);
  align-items: center;
}

.feature-filters-label {
  font-size: 0.7rem;
  color: var(--color-text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  width: 100%;
  text-align: center;
  margin-bottom: 2px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  transition: opacity 0.2s;
}

.feature-item.dimmed {
  opacity: 0.35;
}

/* ===== Legend ===== */
.legend {
  display: flex;
  gap: 16px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  justify-content: center;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background 0.2s;
  user-select: none;
}

.legend-item:hover {
  background: var(--color-hover);
}

.legend-item.dimmed {
  opacity: 0.35;
}

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

.legend-dot.bus {
  background: var(--color-bus);
}

.legend-dot.trolleybus {
  background: var(--color-trolleybus);
}

.legend-dot.other {
  background: var(--color-other);
}

/* ===== Route List ===== */
.filter-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.filter-section h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  padding: 12px 16px 8px;
}

.route-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 8px;
}

.route-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.875rem;
  user-select: none;
  transition: background 0.15s;
  min-width: 0;
}

.route-item:hover {
  background: var(--color-hover);
}

.route-item.active {
  background: var(--color-active);
  font-weight: 600;
}

.route-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 11px;
  background: var(--color-primary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.route-badge.trolleybus {
  background: var(--color-trolleybus);
}

.route-badge.other {
  background: var(--color-other);
}

.route-badge.unassigned {
  background: #9ca3af;
  color: #fff;
  font-style: italic;
  font-weight: 500;
}

.route-badge.unassigned:hover {
  background: #6b7280;
}

.route-count {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--color-text-muted);
}

.route-gtfs-prefix {
  font-size: 0.7rem;
  color: var(--color-text-muted);
  background: var(--color-hover);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 500;
  font-family: 'SF Mono', 'Fira Code', monospace;
  letter-spacing: 0.02em;
}

/* ===== Status Bar ===== */
.status-bar {
  display: flex;
  justify-content: space-between;
  padding: 10px 16px;
  border-top: 1px solid var(--color-border);
  font-size: 0.75rem;
  color: var(--color-text-muted);
  background: var(--color-bg);
}

#last-update {
  display: inline-flex;
  align-items: center;
}

.update-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-text-muted);
  margin-right: 4px;
  flex-shrink: 0;
}

.update-dot.active {
  background: #22c55e;
  animation: pulse-dot 1.8s ease-in-out infinite;
}

.update-dot.reconnecting {
  background: #f59e0b;
  animation: pulse-dot 1.8s ease-in-out infinite;
}

.update-dot.disconnected {
  background: #9ca3af;
}

/* ===== Toggle Sidebar (mobile) ===== */
#toggle-sidebar {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1100;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius);
  background: var(--color-sidebar);
  box-shadow: var(--shadow-md);
  font-size: 1.25rem;
  cursor: pointer;
}

/* ===== Custom Markers ===== */
.leaflet-map-pane {
  transition: none !important;
}

.leaflet-overlay-pane svg {
  pointer-events: none;
}

.vehicle-marker-group {
  cursor: pointer;
  pointer-events: none;
}

.vehicle-marker-shape {
  pointer-events: all;
  transition: stroke-width 0.15s ease, fill 0.3s ease;
}

.vehicle-marker-group:hover .vehicle-marker-shape {
  stroke-width: 3.5;
}

.vehicle-marker-text {
  user-select: none;
  -webkit-user-select: none;
  pointer-events: none;
}

/* ===== Popup ===== */
.leaflet-popup-content-wrapper {
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 0;
  overflow: hidden;
}

.leaflet-popup-content {
  margin: 0;
  font-size: 0.85rem;
  line-height: 1.5;
  min-width: 260px;
}

.leaflet-popup-tip {
  box-shadow: var(--shadow-sm);
}

.popup-header {
  padding: 12px 14px 10px;
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.popup-header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.popup-route-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 24px;
  padding: 0 10px;
  border-radius: 12px;
  color: #fff;
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
}

.popup-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.popup-status-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.popup-status-dot.active {
  background: #22c55e;
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5);
  animation: pulse-dot 1.8s ease-in-out infinite;
}

.popup-status-dot.inactive {
  background: #9ca3af;
}

@keyframes pulse-dot {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.5);
    transform: scale(1);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(34, 197, 94, 0);
    transform: scale(1.15);
  }
}

.popup-deviation {
  display: inline-flex;
  align-items: center;
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1;
}

.popup-deviation.late {
  color: #dc2626;
}

.popup-deviation.early {
  color: #2563eb;
}

.popup-deviation.on-time {
  color: #15803d;
}

.popup-body {
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.popup-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.popup-section div {
  font-size: 0.8rem;
  line-height: 1.4;
}

.popup-section strong {
  color: var(--color-text-muted);
  font-weight: 500;
}

.popup-features {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.popup-feature {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--color-hover);
  font-size: 0.95rem;
  cursor: default;
  transition: background 0.15s;
}

.popup-feature:hover {
  background: var(--color-active);
}

.popup-gtfs-trip-id {
  font-family: 'SF Mono', 'Fira Code', 'Courier New', monospace;
  font-size: 0.85em;
  background: var(--color-hover);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
  letter-spacing: 0.03em;
}

.popup-inactive-status {
  color: var(--color-text-muted);
  font-style: italic;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  #sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    width: 280px;
  }

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

  /* Collapse doesn't apply on mobile — use open/close instead */
  #sidebar.collapsed {
    clip-path: inset(0 0 0 0);
    pointer-events: auto;
  }

  #collapse-sidebar {
    display: none;
  }

  #expand-sidebar {
    display: none !important;
  }

  #toggle-sidebar {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
