:root {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #17212b;
  background: #f4f7f8;
}

* { box-sizing: border-box; }
body { margin: 0; background: #f4f7f8; }
button { font: inherit; }

.topbar {
  background: #fff;
  border-bottom: 1px solid #dfe6e9;
  padding: 14px 16px 12px;
}
.topbar > div { max-width: 1180px; margin: 0 auto; }
.back-link { color: #335d70; text-decoration: none; font-size: 14px; }
.topbar h1 { margin: 5px 0 2px; font-size: clamp(20px, 3.8vw, 28px); }
.topbar p { margin: 0; color: #667680; font-size: 14px; }

.page-shell { max-width: 1180px; margin: 0 auto; padding: 12px; }
.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.metric-label {
  display: grid;
  gap: 2px;
}
.metric-label strong { font-size: 14px; }
.metric-label span { color: #667680; font-size: 12px; }
.refresh-button {
  border: 1px solid #d8e0e4;
  border-radius: 9px;
  padding: 9px 12px;
  cursor: pointer;
  background: #fff;
  white-space: nowrap;
}

.map-card {
  position: relative;
  overflow: hidden;
  background: #fff;
  border: 1px solid #dce3e6;
  border-radius: 14px;
}
#map { width: 100%; height: min(72vh, 720px); min-height: 480px; }
.map-message {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 900;
  padding: 8px 12px;
  background: rgba(255,255,255,.94);
  border-radius: 9px;
  box-shadow: 0 2px 10px rgba(0,0,0,.14);
  font-size: 14px;
}
.map-message[hidden] { display: none; }

.cddp-marker-wrap { background: transparent; border: 0; }
.cddp-marker {
  width: 58px;
  height: 54px;
  padding: 5px 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border-radius: 19px;
  border: 3px solid #fff;
  color: #fff;
  line-height: 1;
  font-weight: 800;
  box-shadow: 0 2px 7px rgba(0,0,0,.32);
  text-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.marker-water {
  font-size: 14px;
  line-height: 1;
}
.marker-rain {
  font-size: 10px;
  line-height: 1;
  font-weight: 750;
  white-space: nowrap;
  opacity: .98;
}
.cddp-marker.is-missing { background: #617889 !important; }

.legend {
  position: absolute;
  z-index: 800;
  right: 10px;
  bottom: 20px;
  max-width: min(280px, calc(100% - 20px));
  padding: 9px 10px;
  border-radius: 10px;
  background: rgba(255,255,255,.94);
  box-shadow: 0 2px 10px rgba(0,0,0,.17);
  font-size: 12px;
}
.legend-title { font-weight: 750; margin-bottom: 3px; }
.legend-note { color: #5d6b73; margin-bottom: 6px; line-height: 1.3; }
.legend-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 10px; }
.legend-item { display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 10px; }

.status-card {
  display: grid;
  gap: 3px;
  margin-top: 10px;
  background: #fff;
  border: 1px solid #dce3e6;
  border-radius: 12px;
  padding: 11px 13px;
  font-size: 14px;
}
.status-card span { color: #5d6b73; }
.source-note { font-size: 12px; }

.popup-title { font-size: 16px; font-weight: 800; margin-bottom: 2px; }
.popup-location { color: #60717a; margin-bottom: 8px; }
.popup-time { color: #52616a; font-size: 12px; margin-bottom: 8px; }
.popup-grid { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; min-width: 245px; }
.popup-grid .value { font-weight: 700; text-align: right; }
.popup-section { margin-top: 8px; padding-top: 7px; border-top: 1px solid #edf0f1; font-weight: 750; }
.popup-source { margin-top: 8px; color: #6b7880; font-size: 12px; }

@media (max-width: 640px) {
  .page-shell { padding: 8px; }
  .toolbar { align-items: stretch; }
  .metric-label { flex: 1; }
  .refresh-button { padding: 9px 10px; }
  #map { height: calc(100vh - 225px); min-height: 430px; }
  .legend { bottom: 12px; font-size: 11px; }
  .legend-grid { grid-template-columns: 1fr; }
}

/* On narrow screens keep the key outside the map and make station details scrollable. */
@media (max-width: 640px) {
  #map { height: min(72svh, 650px); min-height: 430px; }
  .legend {
    position: static;
    max-width: none;
    margin: 0;
    padding: 10px 12px;
    border-radius: 0;
    box-shadow: none;
    border-top: 1px solid #dce3e6;
  }
  .legend-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .leaflet-popup-content-wrapper {
    max-width: calc(100vw - 48px);
  }
  .leaflet-popup-content {
    width: auto !important;
    min-width: 0;
    max-height: min(56svh, 420px);
    overflow-y: auto;
    overscroll-behavior: contain;
    margin: 12px 16px;
  }
  .popup-grid { min-width: 0; grid-template-columns: minmax(0, 1fr) auto; }
}
