:root {
  --sg-green: #006C35;
  --sg-green-dark: #004f26;
  --sg-green-soft: #e9f3ed;
  --ink: #14181c;
  --ink-soft: #4a5358;
  --muted: #8a9399;
  --line: #e6e8eb;
  --bg: #f5f6f7;
  --card: #ffffff;
  --accent: #d6a300;
  --lvl-1: #7ec97a;
  --lvl-2: #c2d247;
  --lvl-3: #f2b33d;
  --lvl-4: #ec7a3b;
  --lvl-5: #d93838;
  --shadow: 0 1px 2px rgba(20,24,28,.05), 0 8px 24px rgba(20,24,28,.06);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "ss01", "cv11";
}

a { color: var(--sg-green); }

/* Header */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark { display: grid; place-items: center; }
.brand-title {
  margin: 0;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: -0.01em;
}
.brand-sub {
  font-size: 13px;
  color: var(--ink-soft);
  margin-top: 2px;
}

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

.segment {
  display: inline-flex;
  background: var(--bg);
  padding: 4px;
  border-radius: 999px;
  border: 1px solid var(--line);
}
.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 7px 14px;
  border-radius: 999px;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.seg-btn[aria-selected="true"] {
  background: var(--sg-green);
  color: white;
  box-shadow: 0 2px 6px rgba(0,108,53,.25);
}
.seg-btn:hover:not([aria-selected="true"]) { color: var(--ink); }

.status {
  font-size: 12.5px;
  color: var(--muted);
  min-width: 100px;
  text-align: right;
}
.status.loading::before {
  content: "";
  display: inline-block;
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 50%;
  margin-right: 6px;
  animation: pulse 1.2s ease-in-out infinite;
}
.status.ready::before {
  content: "";
  display: inline-block;
  width: 8px; height: 8px;
  background: var(--sg-green);
  border-radius: 50%;
  margin-right: 6px;
}
@keyframes pulse { 0%,100% { opacity: .25 } 50% { opacity: 1 } }

/* Layout */
.grid {
  display: grid;
  grid-template-columns: 1fr 440px;
  gap: 16px;
  padding: 16px;
  height: calc(100vh - 65px);
  min-height: 600px;
}
@media (max-width: 1024px) {
  .grid { grid-template-columns: 1fr; height: auto; }
  .map-panel { height: 60vh; }
}

.panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  position: relative;
}
.map-panel { position: relative; }
#map { width: 100%; height: 100%; min-height: 420px; background: #eef2f3; }

/* Legend */
.legend {
  position: absolute;
  bottom: 16px;
  left: 16px;
  background: rgba(255,255,255,.96);
  backdrop-filter: blur(6px);
  padding: 12px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: 12px;
  z-index: 500;
  border: 1px solid var(--line);
}
.legend-title {
  font-weight: 700;
  font-size: 11px;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-bottom: 6px;
}
.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
  color: var(--ink-soft);
}
.dot {
  width: 12px; height: 12px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 0 0 1px rgba(0,0,0,.08);
}
.lvl-1 { background: var(--lvl-1); }
.lvl-2 { background: var(--lvl-2); }
.lvl-3 { background: var(--lvl-3); }
.lvl-4 { background: var(--lvl-4); }
.lvl-5 { background: var(--lvl-5); }

/* Side panel */
.side-panel {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.side-panel::-webkit-scrollbar { width: 6px; }
.side-panel::-webkit-scrollbar-thumb { background: #d0d4d8; border-radius: 99px; }

.side-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}
.side-title {
  font-weight: 800;
  font-size: 20px;
  letter-spacing: -0.01em;
}
.side-sub {
  font-size: 13px;
  color: var(--ink-soft);
  margin-top: 4px;
}
.pill-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: white;
  border-radius: 999px;
  padding: 6px 12px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
  transition: all .15s;
}
.pill-btn:hover { border-color: var(--sg-green); color: var(--sg-green); }

/* Stats */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.stat {
  background: var(--sg-green-soft);
  padding: 12px;
  border-radius: 10px;
  text-align: left;
}
.stat-val {
  font-weight: 800;
  font-size: 22px;
  color: var(--sg-green-dark);
  line-height: 1.1;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.stat-lab {
  font-size: 11px;
  color: var(--ink-soft);
  margin-top: 4px;
  text-transform: uppercase;
  letter-spacing: .03em;
  font-weight: 600;
}

/* Chart cards */
.chart-card {
  background: white;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
}
.chart-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}
.chart-head h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: -0.005em;
}
.muted { color: var(--muted); font-size: 12px; }
.chart-wrap {
  position: relative;
  height: 180px;
}
.heatmap-wrap { height: auto; }

/* Heatmap */
.heatmap {
  display: grid;
  grid-template-columns: 36px repeat(24, 1fr);
  gap: 2px;
  font-size: 10px;
}
.heatmap .hm-cell {
  aspect-ratio: 1;
  border-radius: 3px;
  background: #eef2f3;
  transition: transform .1s;
  cursor: default;
}
.heatmap .hm-cell:hover {
  transform: scale(1.15);
  outline: 2px solid var(--sg-green);
  outline-offset: 1px;
}
.heatmap .hm-label {
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 4px;
  font-weight: 600;
}
.heatmap .hm-hour-label {
  color: var(--muted);
  text-align: center;
  font-size: 9px;
  padding-bottom: 2px;
}

/* Leaflet custom markers */
.sensor-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 11px;
  border: 3px solid white;
  border-radius: 50%;
  box-shadow: 0 4px 14px rgba(0,0,0,.2);
  cursor: pointer;
  transition: transform .15s ease-out;
  font-variant-numeric: tabular-nums;
}
.sensor-marker:hover { transform: scale(1.15); z-index: 1000; }
.sensor-marker.active {
  transform: scale(1.2);
  outline: 3px solid rgba(255,255,255,.9);
  outline-offset: 2px;
}

.leaflet-popup-content-wrapper {
  border-radius: 12px;
  box-shadow: 0 12px 36px rgba(0,0,0,.18);
}
.leaflet-popup-content {
  margin: 14px 16px;
  font-family: inherit;
}
.popup-title {
  font-weight: 800;
  font-size: 15px;
  margin-bottom: 2px;
}
.popup-sub {
  font-size: 12px;
  color: var(--ink-soft);
  margin-bottom: 10px;
}
.popup-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 6px;
}
.popup-stat {
  text-align: center;
  background: var(--bg);
  border-radius: 6px;
  padding: 6px 4px;
}
.popup-stat b { display: block; font-size: 14px; font-weight: 700; }
.popup-stat span { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

/* Footer */
.foot {
  margin-top: auto;
  padding-top: 8px;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
}
.foot a { color: var(--ink-soft); text-decoration: none; border-bottom: 1px dotted var(--muted); }

@media (max-width: 640px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
    padding: 14px 16px;
  }

  .controls {
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
  }

  .segment {
    flex: 0 0 auto;
  }

  .seg-btn {
    padding-inline: 12px;
  }

  .status {
    flex: 1 1 120px;
    min-width: 0;
    text-align: left;
  }

  .grid {
    gap: 10px;
    padding: 10px;
  }

  .side-panel {
    padding: 14px;
  }

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

  .heatmap-wrap {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
  }

  .heatmap {
    min-width: 420px;
  }
}
