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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f5f5;
  overflow: hidden;
  height: 100vh;
}

#app {
  display: flex;
  height: 100vh;
  width: 100vw;
}

#map {
  flex: 1;
  min-width: 0;
}

/* Side Panel */
#panel {
  width: 380px;
  background: #fff;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 1000;
}

#panel-header {
  padding: 16px 20px;
  border-bottom: 1px solid #e0e0e0;
  background: #fafafa;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
}

#panel-title {
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0;
}

#back-btn {
  background: none;
  border: 1px solid #ccc;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  color: #333;
  white-space: nowrap;
  transition: background 0.15s;
}

#back-btn:hover {
  background: #f0f0f0;
}

#back-btn.hidden {
  display: none;
}

#panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

/* Loading spinner */
#loading-spinner {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: #888;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid #e0e0e0;
  border-top-color: #4a90d9;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}

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

/* Overview panel */
.overview-stats {
  margin-top: 12px;
}

.stat-card {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 14px 16px;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #888;
  margin-bottom: 4px;
}

.stat-value {
  font-size: 22px;
  font-weight: 700;
  color: #1a1a1a;
}

.instructions {
  color: #666;
  font-size: 14px;
  line-height: 1.5;
  margin-top: 16px;
  padding: 12px;
  background: #e8f4fd;
  border-radius: 8px;
  border-left: 3px solid #4a90d9;
}

/* Drill-down panel */
.region-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.region-summary .stat-card {
  margin-bottom: 0;
}

.zip-table-container {
  margin-top: 8px;
}

.zip-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.zip-table thead {
  position: sticky;
  top: 0;
  z-index: 1;
}

.zip-table th {
  background: #f0f0f0;
  padding: 10px 8px;
  text-align: left;
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: #555;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  border-bottom: 2px solid #ddd;
}

.zip-table th:hover {
  background: #e4e4e4;
}

.zip-table th.sorted-asc::after {
  content: ' \25B2';
  font-size: 9px;
}

.zip-table th.sorted-desc::after {
  content: ' \25BC';
  font-size: 9px;
}

.zip-table td {
  padding: 8px;
  border-bottom: 1px solid #eee;
  color: #333;
}

.zip-table tbody tr:hover {
  background: #f5f9ff;
}

.zip-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Toggle control */
.metric-toggle {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  padding: 8px;
  display: flex;
  gap: 4px;
}

.metric-toggle button {
  border: none;
  background: transparent;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  color: #555;
  transition: all 0.15s;
  font-weight: 500;
}

.metric-toggle button.active {
  background: #4a90d9;
  color: #fff;
}

.metric-toggle button:hover:not(.active) {
  background: #f0f0f0;
}

/* Legend */
.legend {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  padding: 12px 14px;
  line-height: 1.4;
  min-width: 160px;
}

.legend h4 {
  margin-bottom: 8px;
  font-size: 13px;
  color: #333;
  font-weight: 600;
}

.legend-scale {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #555;
}

.legend-color {
  width: 20px;
  height: 14px;
  border-radius: 2px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
}

/* ZIP3 labels on map */
.zip3-label {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  pointer-events: none !important;
}

.zip3-label span {
  font-size: 14px;
  font-weight: 800;
  color: #000;
  text-shadow: -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff, 1px 1px 0 #fff;
  white-space: nowrap;
  pointer-events: none;
}

/* Tooltip */
.region-tooltip {
  font-size: 13px;
  font-weight: 500;
  padding: 6px 10px;
}

/* Fiduciary stars */
.fiduciary-star {
  background: transparent !important;
  border: none !important;
  box-shadow: none !important;
  color: #d4a017;
  font-size: 22px;
  line-height: 20px;
  text-align: center;
  text-shadow: 0 0 3px rgba(0, 0, 0, 0.5);
  cursor: pointer;
}

.fiduciary-popup {
  font-size: 13px;
  line-height: 1.5;
  padding: 4px 6px;
}

.fiduciary-popup strong {
  font-size: 14px;
}

.fiduciary-toggle {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  margin-top: 8px;
}

.fiduciary-toggle label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

/* Responsive */
@media (max-width: 768px) {
  #app {
    flex-direction: column;
  }

  #map {
    flex: 1;
  }

  #panel {
    width: 100%;
    max-height: 45vh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.15);
  }

  #panel-header {
    padding: 12px 16px;
  }

  #panel-header::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: #ccc;
    border-radius: 2px;
    margin: 0 auto 8px;
  }

  #panel-content {
    padding: 12px 16px;
  }

  .region-summary {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }
}
