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

:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --surface2: #f2efeb;
  --surface3: #e8e4df;
  --border: #d8d2ca;
  --text: #2c2420;
  --text-dim: #6b5f57;
  --text-muted: #a09690;
  --tier-poor: #9ca3af;
  --tier-fair: #f59e0b;
  --tier-good: #f97316;
  --tier-great: #ef4444;
  --panel-width: 360px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: linear-gradient(135deg, #fff8f0 0%, #ffe8d6 50%, #ffd6ba 100%);
  border-bottom: 1px solid var(--border);
  z-index: 1000;
  flex-shrink: 0;
}

.header-left h1 {
  font-size: 1.4rem;
  font-weight: 700;
  background: linear-gradient(90deg, #e63946, #f77f00, #fcbf49);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-left .subtitle {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-left: 4px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.summary-bar {
  font-size: 0.8rem;
  color: var(--text-dim);
  display: flex;
  gap: 12px;
}

.summary-stat {
  display: flex;
  align-items: center;
  gap: 4px;
}

.summary-stat .value {
  font-weight: 600;
  color: var(--text);
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.update-time {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.refresh-btn, .view-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: background 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.refresh-btn:hover, .view-btn:hover {
  background: var(--surface2);
}

.view-btn-inactive {
  opacity: 0.5;
}

.view-btn-inactive:hover {
  opacity: 0.8;
}

.refresh-btn {
  font-size: 1.1rem;
  padding: 2px 8px;
}

/* View Mode Button Group */
.view-mode-group {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.view-mode-btn {
  background: var(--surface);
  color: var(--text-dim);
  border: none;
  border-right: 1px solid var(--border);
  padding: 4px 10px;
  cursor: pointer;
  font-size: 0.75rem;
  transition: background 0.2s, color 0.2s;
}

.view-mode-btn:last-child {
  border-right: none;
}

.view-mode-btn:hover {
  background: var(--surface2);
  color: var(--text);
}

.view-mode-btn.active {
  background: linear-gradient(135deg, #f97316, #ef4444);
  color: #ffffff;
  font-weight: 600;
}

/* Legend Bar */
.legend-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 6px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 0.75rem;
  flex-shrink: 0;
  z-index: 999;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-dim);
}

.legend-title {
  font-weight: 600;
  color: var(--text);
  font-size: 0.8rem;
  margin-right: 4px;
}

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

.legend-gradient {
  position: relative;
  flex: 1;
  max-width: 200px;
  margin-left: auto;
}

.gradient-strip {
  height: 8px;
  border-radius: 4px;
  background: linear-gradient(90deg, #e0e0e0, #fff9c4, #ffe082, #ffb74d, #ff7043, #ef5350, #e53935, #d32f2f, #c2185b, #880e4f);
}

.gradient-label-left, .gradient-label-right {
  position: absolute;
  top: 10px;
  font-size: 0.65rem;
  color: var(--text-muted);
}

.gradient-label-left { left: 0; }
.gradient-label-right { right: 0; }

/* Main Layout */
.app-body {
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}

#map {
  flex: 1;
  z-index: 1;
}

/* Leaflet overrides for light theme */
.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}

.leaflet-control-attribution {
  background: rgba(255, 255, 255, 0.85) !important;
  color: var(--text-muted) !important;
  font-size: 0.6rem !important;
}

.leaflet-control-attribution a {
  color: var(--text-dim) !important;
}

/* Custom marker tooltip */
.city-tooltip {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 8px !important;
  padding: 8px 12px !important;
  font-size: 0.8rem !important;
  box-shadow: 0 4px 16px rgba(0,0,0,0.1) !important;
}

.city-tooltip::before {
  border-top-color: var(--border) !important;
}

.tooltip-name { font-weight: 600; font-size: 0.9rem; }
.tooltip-score { margin-top: 2px; }
.tooltip-time { color: var(--text-dim); font-size: 0.75rem; }

/* Detail Panel */
.detail-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: var(--panel-width);
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--border);
  z-index: 500;
  transform: translateX(100%);
  transition: transform 0.3s ease;
  overflow-y: auto;
  box-shadow: -4px 0 20px rgba(0,0,0,0.08);
}

.detail-panel.open {
  transform: translateX(0);
}

.panel-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.5rem;
  cursor: pointer;
  z-index: 10;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.panel-close:hover {
  background: var(--surface2);
  color: var(--text);
}

/* Panel Content */
.panel-header {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.panel-city-name {
  font-size: 1.5rem;
  font-weight: 700;
}

.panel-city-en {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.panel-province {
  color: var(--text-muted);
  font-size: 0.75rem;
  margin-top: 2px;
}

.score-circle {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  margin: 20px auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 4px solid;
  position: relative;
}

.score-number {
  font-size: 2.5rem;
  font-weight: 800;
  line-height: 1;
}

.score-label {
  font-size: 0.8rem;
  margin-top: 2px;
}

.tier-badge {
  display: inline-block;
  padding: 4px 16px;
  border-radius: 20px;
  font-size: 0.9rem;
  font-weight: 600;
  margin-top: 10px;
}

.sunset-time-display {
  text-align: center;
  padding: 12px;
  margin: 12px 20px;
  background: var(--surface2);
  border-radius: 10px;
}

.sunset-time-label {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.sunset-time-value {
  font-size: 1.8rem;
  font-weight: 700;
  background: linear-gradient(90deg, #e63946, #f77f00);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.color-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px;
  margin: 0 20px;
}

.color-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--border);
}

.color-name {
  font-size: 0.9rem;
}

.color-name-en {
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* Sub-scores */
.subscores-section {
  padding: 16px 20px;
}

.subscores-title {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 12px;
  font-weight: 600;
}

.subscore-row {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  gap: 8px;
}

.subscore-label {
  width: 70px;
  font-size: 0.75rem;
  color: var(--text-dim);
  flex-shrink: 0;
}

.subscore-bar-bg {
  flex: 1;
  height: 8px;
  background: var(--surface2);
  border-radius: 4px;
  overflow: hidden;
}

.subscore-bar-fill {
  height: 100%;
  border-radius: 4px;
  transition: width 0.6s ease;
}

.subscore-value {
  width: 30px;
  text-align: right;
  font-size: 0.75rem;
  font-weight: 600;
}

/* Raw data */
.raw-data-section {
  padding: 0 20px 20px;
}

.raw-data-toggle {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.75rem;
  text-align: center;
}

.raw-data-toggle:hover {
  background: var(--surface3);
}

.raw-data-table {
  margin-top: 10px;
  display: none;
}

.raw-data-table.open {
  display: block;
}

.raw-data-table table {
  width: 100%;
  font-size: 0.72rem;
  border-collapse: collapse;
}

.raw-data-table th, .raw-data-table td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.raw-data-table th {
  color: var(--text-dim);
  font-weight: 500;
}

/* Loading Overlay */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(247, 245, 242, 0.95);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.5s;
}

.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 4px solid var(--surface3);
  border-top: 4px solid #ef4444;
  border-right: 4px solid #f97316;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

.loading-overlay p {
  margin-top: 16px;
  color: var(--text-dim);
  font-size: 0.9rem;
}

/* Pulse animation for Great tier markers */
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.6); }
  70% { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Contour region soft edges */
.contour-region {
  filter: blur(10px);
}

/* Contour tooltip */
.contour-tooltip {
  background: var(--surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: 6px !important;
  padding: 4px 10px !important;
  font-size: 0.8rem !important;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1) !important;
}

/* History Section */
.history-section {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

.history-title {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-bottom: 12px;
  font-weight: 600;
}

.history-chart {
  min-height: 100px;
}

.history-bars {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 100px;
  padding-bottom: 24px;
  position: relative;
}

.history-bar-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  justify-content: flex-end;
  position: relative;
}

.history-bar {
  width: 100%;
  min-height: 2px;
  border-radius: 2px 2px 0 0;
  transition: height 0.4s ease;
}

.history-bar-score {
  font-size: 0.55rem;
  color: var(--text-dim);
  margin-top: 2px;
  line-height: 1;
}

.history-bar-date {
  font-size: 0.5rem;
  color: var(--text-muted);
  position: absolute;
  bottom: -18px;
  white-space: nowrap;
}

.history-loading, .history-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 20px 0;
}

/* Responsive */
@media (max-width: 768px) {
  .app-header {
    flex-direction: column;
    gap: 2px;
    padding: 4px 8px;
  }

  .app-header h1 { font-size: 16px; }
  .app-header .subtitle { display: none; }
  .header-controls { gap: 4px; }
  .header-controls button { font-size: 11px; padding: 2px 6px; }

  .header-right {
    width: 100%;
    justify-content: space-between;
  }

  .summary-bar {
    display: none;
  }

  .legend-bar {
    gap: 4px;
    padding: 2px 8px;
    overflow-x: auto;
    font-size: 11px;
  }

  .legend-gradient {
    display: none;
  }

  .detail-panel {
    width: 100%;
    height: 50vh;
    top: auto;
    bottom: 0;
    transform: translateY(100%);
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
  }

  .detail-panel.open {
    transform: translateY(0);
  }
}
