:root {
  color-scheme: light;
  --bg: #f6f7f2;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #667085;
  --line: #dde2dc;
  --working: #238636;
  --broken: #cf222e;
  --unknown: #7a8593;
  --accent: #0f766e;
  --amber: #d97706;
  --shadow: 0 14px 36px rgba(15, 23, 42, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input {
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 388px) minmax(0, 1fr);
  height: 100vh;
  min-height: 620px;
}

.sidebar {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 16px;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 22px;
}

.brand {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  max-width: 280px;
  margin: 0;
  font-size: 26px;
  line-height: 1.08;
}

.live-pill {
  flex: 0 0 auto;
  border: 1px solid #b6ddd7;
  border-radius: 999px;
  background: #e9f7f5;
  color: #115e59;
  padding: 6px 9px;
  font-size: 12px;
  font-weight: 800;
}

.region-controls,
.status-tabs {
  display: grid;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f5f6f1;
  padding: 4px;
}

.region-controls {
  grid-template-columns: repeat(3, 1fr);
}

.status-tabs {
  grid-template-columns: repeat(4, 1fr);
}

.segment,
.status-tab {
  min-width: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #475467;
  padding: 9px 8px;
  font-size: 13px;
  font-weight: 800;
}

.segment.is-active,
.status-tab.is-active {
  background: #17212b;
  color: #ffffff;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.stat-card {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  padding: 10px;
}

.stat-card span {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stat-card strong {
  display: block;
  margin-top: 4px;
  font-size: 22px;
  line-height: 1;
}

.stat-working strong {
  color: var(--working);
}

.stat-broken strong {
  color: var(--broken);
}

.stat-unknown strong {
  color: var(--unknown);
}

.search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px;
  gap: 8px;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  padding: 0 12px;
}

.search-box svg {
  width: 18px;
  color: var(--muted);
}

.search-box input {
  min-width: 0;
  width: 100%;
  border: 0;
  outline: 0;
  color: var(--ink);
}

.icon-button {
  display: inline-grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
}

.icon-button svg {
  width: 19px;
  height: 19px;
}

.icon-button:hover,
.icon-button.is-active {
  border-color: #f0b4b8;
  background: #fff1f2;
  color: var(--broken);
}

.location-list {
  display: flex;
  min-height: 0;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
}

.location-row {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  padding: 12px;
}

.location-row:hover,
.location-row.is-selected {
  border-color: #9ed1cc;
  background: #f1fbf9;
}

.location-row button {
  display: grid;
  min-width: 0;
  width: 100%;
  border: 0;
  background: transparent;
  padding: 0;
  text-align: left;
}

.location-title {
  overflow: hidden;
  color: var(--ink);
  font-size: 14px;
  font-weight: 850;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.location-meta {
  overflow: hidden;
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-label {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  flex: 0 0 auto;
  border-radius: 50%;
  margin-top: 4px;
}

.dot.working {
  background: var(--working);
}

.dot.broken {
  background: var(--broken);
}

.dot.unknown {
  background: var(--unknown);
}

.sidebar-footer {
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
}

.map-panel {
  position: relative;
  min-width: 0;
  background: #dfe6df;
}

#map {
  position: absolute;
  inset: 0;
}

.map-toolbar {
  position: absolute;
  z-index: 500;
  top: 18px;
  right: 18px;
  display: flex;
  gap: 8px;
  padding: 6px;
  border: 1px solid rgba(15, 23, 42, 0.1);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: var(--shadow);
}

.legend {
  position: absolute;
  z-index: 500;
  left: 18px;
  bottom: 18px;
  display: flex;
  gap: 12px;
  align-items: center;
  border: 1px solid rgba(15, 23, 42, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  padding: 10px 12px;
  color: #344054;
  font-size: 12px;
  font-weight: 800;
  box-shadow: var(--shadow);
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend .dot {
  margin-top: 0;
}

.leaflet-popup-content-wrapper {
  border-radius: 8px;
}

.popup-title {
  margin: 0 0 4px;
  font-weight: 850;
}

.popup-meta {
  margin: 0;
  color: #475467;
}

.popup-status {
  margin: 10px 0 0;
  font-weight: 850;
}

.popup-status.working {
  color: var(--working);
}

.popup-status.broken {
  color: var(--broken);
}

.popup-status.unknown {
  color: var(--unknown);
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(390px, 48vh) minmax(420px, 52vh);
    min-height: 100vh;
  }

  .sidebar {
    order: 2;
    border-top: 1px solid var(--line);
    border-right: 0;
    padding: 16px;
  }

  .map-panel {
    order: 1;
  }

  h1 {
    font-size: 22px;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .status-tabs {
    grid-template-columns: repeat(2, 1fr);
  }

  .legend {
    right: 12px;
    left: 12px;
    justify-content: center;
  }
}

/* === Phase 1: clustering + city search + nearest + UI states === */

.cluster-icon {
  background: transparent;
  border: 0;
}

.cluster-bubble {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 2px solid #ffffff;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  font-weight: 850;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.18);
}

.cluster-bubble.ok      { background: var(--working); }
.cluster-bubble.broken  { background: var(--broken); }
.cluster-bubble.unknown { background: var(--unknown); }

/* City search dropdown inside the existing .search-box */
.search-box {
  position: relative;
}

.city-suggestions {
  position: absolute;
  z-index: 800;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  display: flex;
  max-height: 240px;
  flex-direction: column;
  margin: 0;
  padding: 4px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  list-style: none;
  box-shadow: var(--shadow);
}

.city-suggestion {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}

.city-suggestion:hover,
.city-suggestion.is-active {
  background: #f1fbf9;
}

.city-name {
  font-weight: 800;
}

.city-meta {
  margin-left: 8px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.city-count {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

/* "Cerca de ti" panel */
.nearest-panel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f9fcfa;
  padding: 10px;
}

.nearest-title {
  margin: 0;
  color: var(--accent);
  font-size: 12px;
  font-weight: 850;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.nearest-empty {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.nearest-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.nearest-item button {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  border: 0;
  border-radius: 6px;
  background: #ffffff;
  padding: 6px 8px;
  text-align: left;
}

.nearest-item button:hover {
  background: #e7f6f3;
}

.nearest-info {
  display: flex;
  min-width: 0;
  flex-direction: column;
}

.nearest-name {
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.nearest-status {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

/* Empty / loading / error states */
.ui-states {
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: #fdfbf3;
  padding: 14px;
  color: #4b5563;
  font-size: 13px;
}

.ui-state p {
  margin: 0 0 6px;
}

.ui-state-error {
  border-color: rgba(207, 34, 46, 0.35);
  background: #fff5f5;
  color: #7f1d1d;
}

.ui-state-detail {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  word-break: break-word;
}

.ui-state-retry {
  margin-top: 8px;
  border: 1px solid var(--ink);
  border-radius: 6px;
  background: var(--ink);
  color: #ffffff;
  padding: 6px 12px;
  font-weight: 800;
}

.ui-state-retry:hover {
  background: #1f2937;
}
