/* ===== Reset & Layout ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

html, body, #map {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ===== Sidebar ===== */
#sidebar {
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 320px;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.95);
  color: #1a1a2e;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #d1d5db;
  backdrop-filter: blur(10px);
  box-shadow: 2px 0 12px rgba(0,0,0,0.06);
}

.sidebar-header {
  padding: 20px 20px 12px;
  border-bottom: 1px solid #e5e7eb;
}
.sidebar-header h1 {
  font-size: 1.2rem;
  color: #111827;
  margin-bottom: 4px;
}
.sidebar-header .subtitle {
  font-size: 0.8rem;
  color: #6b7280;
}

.sidebar-section {
  padding: 14px 20px;
  border-bottom: 1px solid #f3f4f6;
}
.sidebar-section label {
  display: block;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6b7280;
  margin-bottom: 8px;
  font-weight: 600;
}

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid #e5e7eb;
  margin-top: auto;
  font-size: 0.7rem;
  color: #9ca3af;
}
.sidebar-footer a { color: #6b7280; }

/* ===== Stats bar ===== */
.stats-bar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  padding: 12px 20px !important;
}
.stat {
  background: #f3f4f6;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  color: #4b5563;
}
.stat-1 { background: #d1fae5; color: #065f46; }
.stat-2 { background: #dbeafe; color: #1e40af; }
.stat-3 { background: #fef3c7; color: #92400e; }
.stat-4 { background: #fee2e2; color: #991b1b; }

/* ===== Search ===== */
#search {
  width: 100%;
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid #d1d5db;
  background: #f9fafb;
  color: #1f2937;
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
#search:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* ===== Filter chips ===== */
.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  padding: 5px 12px;
  border-radius: 14px;
  border: 1px solid #d1d5db;
  background: #f9fafb;
  color: #4b5563;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
  font-weight: 500;
}
.chip:hover { border-color: #9ca3af; background: #f3f4f6; }
.chip.active { background: #e5e7eb; border-color: #9ca3af; color: #111827; font-weight: 600; }
.chip-1.active, .chip-1:hover { background: #d1fae5; border-color: #34d399; color: #065f46; }
.chip-2.active, .chip-2:hover { background: #dbeafe; border-color: #60a5fa; color: #1e40af; }
.chip-3.active, .chip-3:hover { background: #fef3c7; border-color: #fbbf24; color: #92400e; }
.chip-4.active, .chip-4:hover { background: #fee2e2; border-color: #f87171; color: #991b1b; }
.chip-null.active, .chip-null:hover { background: #f3f4f6; border-color: #9ca3af; color: #4b5563; }

/* ===== Selects ===== */
select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #d1d5db;
  background: #f9fafb;
  color: #1f2937;
  font-size: 0.85rem;
  outline: none;
  cursor: pointer;
  appearance: auto;
}
select:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* ===== Map ===== */
#map {
  z-index: 1;
}

/* ===== Legend ===== */
#legend {
  position: absolute;
  bottom: 30px;
  right: 20px;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.93);
  padding: 12px 16px;
  border-radius: 8px;
  border: 1px solid #d1d5db;
  font-size: 0.75rem;
  color: #4b5563;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
}
.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.15);
}
.dot-1    { background: #16a34a; }
.dot-2    { background: #2563eb; }
.dot-3    { background: #d97706; }
.dot-4    { background: #dc2626; }
.dot-null { background: #9ca3af; }

/* ===== Popup ===== */
.leaflet-popup-content-wrapper {
  background: #fff;
  color: #1f2937;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
  box-shadow: 0 4px 16px rgba(0,0,0,0.1);
}
.leaflet-popup-tip { background: #fff; }
.leaflet-popup-close-button { color: #6b7280 !important; }

.popup-grade {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: 700;
  margin-bottom: 6px;
}
.popup-grade-1 { background: #d1fae5; color: #065f46; }
.popup-grade-2 { background: #dbeafe; color: #1e40af; }
.popup-grade-3 { background: #fef3c7; color: #92400e; }
.popup-grade-4 { background: #fee2e2; color: #991b1b; }
.popup-grade-null { background: #f3f4f6; color: #6b7280; }
.popup-name {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 4px;
  color: #111827;
}
.popup-meta { font-size: 0.75rem; color: #6b7280; }
.popup-meta span { margin-right: 12px; }
.popup-subgrades {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid #e5e7eb;
}
.popup-subgrade {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  padding: 2px 0;
}
.popup-subgrade span:first-child { color: #6b7280; }
.subgrade-badge {
  font-size: 0.65rem;
  padding: 1px 6px;
  border-radius: 8px;
  font-weight: 600;
}
.subgrade-1 { background: #d1fae5; color: #065f46; }
.subgrade-2 { background: #dbeafe; color: #1e40af; }
.subgrade-3 { background: #fef3c7; color: #92400e; }
.subgrade-4 { background: #fee2e2; color: #991b1b; }

/* ===== Address section ===== */
.address-form {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}
.address-form input {
  flex: 1;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid #d1d5db;
  background: #f9fafb;
  color: #1f2937;
  font-size: 0.8rem;
  outline: none;
}
.address-form input:focus {
  border-color: #8b5cf6;
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.15);
}
.btn-add {
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid #8b5cf6;
  background: #f5f3ff;
  color: #7c3aed;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.btn-add:hover:not(:disabled) {
  background: #8b5cf6;
  color: #fff;
}
.btn-add:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#address-geocode-status {
  font-size: 0.7rem;
  color: #8b5cf6;
  margin-bottom: 8px;
  min-height: 16px;
}

.address-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 260px;
  overflow-y: auto;
}
.address-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  margin-bottom: 4px;
  background: #f9fafb;
  border: 1px solid #f3f4f6;
  font-size: 0.78rem;
  transition: background 0.1s;
}
.address-item:hover { background: #f5f3ff; }
.address-item-label {
  flex: 1;
  color: #1f2937;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.address-item-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}
.address-item-actions button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.85rem;
  padding: 2px 4px;
  border-radius: 4px;
  transition: background 0.1s;
  line-height: 1;
}
.address-item-actions button:hover { background: #ede9fe; }

/* Address popup distances */
.popup-distances {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #e5e7eb;
}
.popup-distances h4 {
  font-size: 0.75rem;
  color: #6b7280;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.distance-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 3px 0;
  font-size: 0.73rem;
  gap: 8px;
}
.distance-row .school-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #1f2937;
}
.distance-row .school-dist {
  flex-shrink: 0;
  font-weight: 600;
  color: #4b5563;
}
.distance-row .school-grade {
  font-size: 0.6rem;
  padding: 1px 5px;
  border-radius: 6px;
  font-weight: 600;
  flex-shrink: 0;
}

.distance-limit {
  margin-top: 6px;
}
.distance-limit label {
  font-size: 0.7rem;
  color: #6b7280;
  margin-right: 6px;
}
.distance-limit select {
  font-size: 0.7rem;
  padding: 2px 6px;
}

.legend-house {
  font-size: 0.9rem;
}
@media (max-width: 768px) {
  #sidebar {
    width: 100%;
    height: 40%;
    bottom: auto;
    overflow-y: auto;
  }
  #legend {
    bottom: 10px;
    right: 10px;
    font-size: 0.65rem;
    padding: 8px 12px;
  }
}

/* ===== Cluster icons ===== */
.custom-cluster {
  background: transparent !important;
  border: none !important;
}
.cluster-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.8rem;
  color: #fff;
}
.cluster-small {
  background: rgba(59, 130, 246, 0.85);
  width: 36px;
  height: 36px;
  font-size: 0.75rem;
}
.cluster-medium {
  background: rgba(59, 130, 246, 0.88);
  width: 44px;
  height: 44px;
  font-size: 0.85rem;
}
.cluster-large {
  background: rgba(37, 99, 235, 0.9);
  width: 52px;
  height: 52px;
  font-size: 0.95rem;
}
