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

:root {
  --bg: #f0f2f5;
  --card: #ffffff;
  --text: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --rosso: #ef4444;
  --arancione: #f97316;
  --giallo: #eab308;
  --verde: #22c55e;
  --bianco: #94a3b8;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 40px rgba(0,0,0,0.12);
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
}

/* Visibile solo agli screen reader / crawler, non a schermo */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* TOP BAR */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 1000;
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
}

.topbar-row:not(:last-child) {
  border-bottom: 1px solid var(--border);
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-right: 8px;
  flex-shrink: 0;
  text-decoration: none;
  color: inherit;
  cursor: pointer;
}

.brand-icon {
  width: 34px; height: 34px;
  object-fit: contain;
}

.brand-text {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.3px;
  white-space: nowrap;
}

.search-container {
  flex: 1;
  max-width: 480px;
  position: relative;
}

.search-container input {
  width: 100%;
  padding: 10px 90px 10px 40px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
  background: var(--bg);
  outline: none;
  transition: all 0.2s;
}

.search-container input:focus {
  border-color: var(--accent);
  background: white;
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

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

.search-icon {
  position: absolute; left: 13px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.search-actions {
  position: absolute; right: 5px; top: 50%;
  transform: translateY(-50%);
  display: flex; gap: 4px;
}

.btn-search, .btn-locate {
  width: 32px; height: 32px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.btn-search {
  background: var(--accent);
  color: white;
}
.btn-search:hover { background: var(--accent-hover); }

.btn-locate {
  background: transparent;
  color: var(--text-secondary);
}
.btn-locate:hover { background: var(--bg); color: var(--accent); }
.btn-locate.active { color: var(--accent); }

.filter-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.filter-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
  flex-shrink: 0;
}

.filters {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.filter-chip {
  padding: 7px 14px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  background: white;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  color: var(--text-secondary);
  transition: all 0.15s;
  white-space: nowrap;
}

.filter-chip:hover { border-color: #cbd5e1; background: var(--bg); }
.filter-chip.active {
  background: #eff6ff;
  border-color: var(--accent);
  color: var(--accent);
}

.color-chip {
  padding: 7px 14px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  background: white;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  color: var(--text-secondary);
  transition: all 0.15s;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 6px;
}

.color-chip:hover { border-color: #cbd5e1; background: var(--bg); }

.color-chip.active {
  border-color: var(--chip-color);
  background: color-mix(in srgb, var(--chip-color) 10%, white);
  color: var(--chip-color);
}

.color-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--chip-color);
}

.last-update {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 5px;
}

/* MAIN LAYOUT */
.main {
  display: flex;
  height: calc(100vh - var(--footer-h, 72px));
  padding-top: var(--topbar-h, 105px);
}

#map { flex: 1; }

/* SIDEBAR RIGHT */
.sidebar {
  width: 400px;
  min-width: 400px;
  background: var(--card);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-header h2 {
  font-size: 14px;
  font-weight: 600;
}

.sidebar-sort {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

.sidebar-count {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg);
  padding: 3px 10px;
  border-radius: 12px;
}

.sidebar-hint {
  padding: 12px 20px;
  background: #fffbeb;
  border-bottom: 1px solid #fef3c7;
  font-size: 12px;
  color: #92400e;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-hint svg { flex-shrink: 0; }

.page-footer {
  height: var(--footer-h, 72px);
  background: var(--card);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 4px;
  padding: 0 16px;
  text-align: center;
}

.footer-about {
  max-width: 760px;
  line-height: 1.4;
}

.footer-about strong { font-weight: 600; color: var(--text-secondary); }

.footer-links {
  display: flex;
  align-items: center;
  gap: 8px;
}

.page-footer .footer-sep { color: var(--border); }

.page-footer a {
  color: var(--text-secondary);
  text-decoration: none;
}

.page-footer a:hover {
  color: var(--accent);
  text-decoration: underline;
}

.ps-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
}

.ps-list::-webkit-scrollbar { width: 5px; }
.ps-list::-webkit-scrollbar-track { background: transparent; }
.ps-list::-webkit-scrollbar-thumb { background: #e2e8f0; border-radius: 3px; }

.ps-card {
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 0;
  border-radius: var(--radius-sm);
  margin-bottom: 6px;
  cursor: pointer;
  transition: all 0.15s;
  border: 1.5px solid transparent;
  background: white;
  overflow: visible;
  position: relative;
}

.ps-card:hover {
  border-color: var(--border);
  box-shadow: var(--shadow);
}

.ps-card.highlighted {
  border-color: var(--accent);
  background: #f8faff;
}

.ps-rank-bar {
  width: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  background: var(--bg);
  flex-shrink: 0;
}

.ps-body {
  flex: 1;
  padding: 12px 14px;
  min-width: 0;
}

.ps-name {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ps-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.ps-times {
  display: flex;
  gap: 12px;
  align-items: center;
}

.ps-time-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}

.ps-time-item svg { flex-shrink: 0; }

.ps-time-item strong {
  font-weight: 600;
  color: var(--text);
}

.ps-total {
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 70px;
  border-left: 1px solid var(--bg);
  flex-shrink: 0;
}

.ps-total-value {
  font-size: 22px;
  font-weight: 700;
  line-height: 1;
}

.ps-total-label {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

.wait-low { color: #16a34a !important; }
.wait-med { color: #ca8a04 !important; }
.wait-high { color: #ea580c !important; }
.wait-crit { color: #dc2626 !important; }

.ps-services {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  position: relative;
  z-index: 5;
}

.ps-card:hover {
  z-index: 10;
}

.svc-icon {
  width: 18px;
  height: 18px;
  opacity: 0.18;
  cursor: help;
  transition: opacity 0.15s;
  flex-shrink: 0;
}

.svc-icon.active {
  opacity: 0.7;
}

.svc-wrap {
  position: relative;
  display: inline-flex;
}

.svc-tooltip {
  position: fixed;
  padding: 6px 10px;
  background: var(--text);
  color: white;
  font-size: 11px;
  font-weight: 500;
  border-radius: 6px;
  white-space: normal;
  max-width: calc(100vw - 16px);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 9999;
}

/* TOAST */
.toast-container {
  position: fixed;
  top: calc(var(--topbar-h, 105px) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 3000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: max-content;
  max-width: 90vw;
  pointer-events: none;
}

.toast {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.35;
  max-width: 360px;
  pointer-events: auto;
  animation: toast-in 0.25s ease;
}

.toast.info { border-left-color: var(--accent); }
.toast.warning { border-left-color: var(--giallo); }
.toast.error { border-left-color: var(--rosso); }
.toast.hide { animation: toast-out 0.25s ease forwards; }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes toast-out {
  from { opacity: 1; transform: translateY(0); }
  to { opacity: 0; transform: translateY(-10px); }
}

/* POPUP */
.leaflet-popup-content-wrapper {
  border-radius: var(--radius) !important;
  box-shadow: var(--shadow-lg) !important;
  padding: 0 !important;
}

.leaflet-popup-content { margin: 0 !important; min-width: 340px !important; }
.leaflet-popup-tip { box-shadow: none !important; }

.popup-inner { padding: 18px 20px; font-family: 'Inter', sans-serif; }
.popup-name { font-size: 15px; font-weight: 700; margin-bottom: 3px; letter-spacing: -0.2px; }
.popup-address { font-size: 12px; color: var(--text-secondary); margin-bottom: 16px; }

.popup-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0 3px;
}

.popup-table th {
  font-size: 9px;
  font-weight: 600;
  color: var(--text-muted);
  text-align: right;
  padding: 0 8px 4px 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.popup-table th:first-child {
  text-align: left;
  padding-left: 8px;
}

.popup-table td {
  padding: 7px 8px 7px 10px;
  font-size: 13px;
  text-align: right;
  background: var(--bg);
}

.popup-table td:first-child {
  text-align: left;
  padding-left: 8px;
  border-radius: 6px 0 0 6px;
}

.popup-table td:last-child {
  border-radius: 0 6px 6px 0;
}

.popup-color-cell {
  display: flex;
  align-items: center;
  gap: 7px;
}

.popup-color-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 0 2px rgba(0,0,0,0.06);
}

.popup-color-name {
  font-weight: 600;
  font-size: 12px;
  color: var(--text);
}

.popup-wait-val {
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
}

.popup-people-val {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.popup-update {
  margin-top: 12px;
  font-size: 10px;
  color: var(--text-muted);
  text-align: center;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.popup-travel {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
  font-family: 'Inter', sans-serif;
  display: flex;
  align-items: center;
  gap: 6px;
}

.popup-travel strong { color: var(--text); }

/* LOADING */
.loading-bar {
  position: fixed;
  top: var(--topbar-h, 105px); left: 0; right: 0;
  height: 3px;
  z-index: 1001;
  background: var(--border);
  overflow: hidden;
  display: none;
}

.loading-bar.active { display: block; }

.loading-bar::after {
  content: '';
  position: absolute;
  top: 0; left: -40%;
  width: 40%;
  height: 100%;
  background: var(--accent);
  animation: loading 1s ease-in-out infinite;
}

@keyframes loading {
  0% { left: -40%; }
  100% { left: 100%; }
}

.no-location-msg {
  text-align: center;
  padding: 32px 24px;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.6;
}

.no-location-msg svg { margin-bottom: 8px; opacity: 0.4; }

.emergency-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 1002;
  background: #fef2f2;
  border-bottom: 1px solid #fecaca;
  padding: 8px 20px;
  font-size: 12px;
  color: #991b1b;
  display: flex;
  align-items: center;
  gap: 8px;
  line-height: 1.4;
}

.emergency-banner strong { font-weight: 700; }

.emergency-banner .emergency-close {
  margin-left: auto;
  background: none;
  border: none;
  color: #991b1b;
  cursor: pointer;
  padding: 4px;
  font-size: 16px;
  line-height: 1;
  opacity: 0.5;
  flex-shrink: 0;
}

.emergency-banner .emergency-close:hover { opacity: 1; }

@media (max-width: 900px) {
  .topbar-row {
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 8px;
  }
  .topbar-brand { order: 0; }
  .last-update {
    order: 1;
    margin-left: auto;
  }
  .search-container {
    flex: 1 1 100%;
    max-width: 100%;
    order: 2;
  }
  /* Ogni gruppo di filtri su una riga intera; i chip scorrono in orizzontale */
  .filter-section {
    flex: 1 1 100%;
    min-width: 0;
  }
  .filters {
    flex: 1;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .filters::-webkit-scrollbar { display: none; }
  /* Su mobile la pagina scorre in modo naturale (niente app-shell a scroll interno) */
  html, body { height: auto; overflow: visible; }
  .main {
    flex-direction: column;
    height: auto;
  }
  #map { height: 44vh; min-height: 300px; flex: none; }
  .sidebar {
    width: 100%;
    min-width: 100%;
    flex: none;
    border-left: none;
    border-top: 1px solid var(--border);
    overflow: visible;
  }
  .sidebar-header { padding: 12px 16px 10px; }
  .sidebar-hint { padding: 10px 16px; }
  .ps-list { padding: 6px 8px; overflow: visible; flex: none; }
  .page-footer { height: auto; padding: 14px 16px; }
  .ps-card { margin-bottom: 4px; }
  .ps-body { padding: 10px 10px; }
  .ps-name { font-size: 12px; }
  .ps-total { min-width: 60px; padding: 10px 10px; }
  .ps-total-value { font-size: 20px; }

  .leaflet-popup-content { min-width: 280px !important; }
  .popup-inner { padding: 14px 14px; }
  .popup-name { font-size: 13px; }
  .popup-table td { padding: 6px 6px 6px 8px; }
  .popup-table th { padding: 0 6px 4px 8px; }
}

@media (max-width: 480px) {
  #map { height: 42vh; min-height: 280px; }
  .search-container input { padding: 9px 80px 9px 36px; font-size: 13px; }
  .filter-chip { padding: 6px 12px; font-size: 11px; }
  .ps-rank-bar { width: 28px; font-size: 11px; }
  .ps-meta { font-size: 10px; }
  .ps-time-item { font-size: 11px; }
  .ps-services { gap: 4px; }
  .svc-icon { width: 16px; height: 16px; }
}
