:root {
  --bg: #0f1222;
  --panel: #161a2e;
  --text: #e9edf5;
  --muted: #9aa3b2;
  --accent: #6aa6ff;
  --accent2: #7ad0c9;
  --border: #2a3152;
  --success: #7ad0c9;
  --warning: #ffd86e;
  --error: #ff6b6b;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  --shadow-light: 0 2px 8px rgba(0, 0, 0, 0.2);
}

* {
  box-sizing: border-box;
}

html,
body,
#app {
  height: 100%;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, sans-serif;
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: 420px 1fr;
  background: var(--bg);
  position: relative;
}

/* Sidebar */
#sidebar {
  background: var(--panel);
  color: var(--text);
  padding: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

#sidebar h1 {
  margin: 0 0 16px 0;
  font-size: 20px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  padding: 20px 20px 0;
}

/* Sections */
section {
  padding: 20px;
  border-bottom: 1px solid var(--border);
}
section:last-child {
  border-bottom: none;
}

section h2 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: flex;
  align-items: center;
  gap: 8px;
}
section h3 {
  margin: 0 0 12px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}

/* Controls */
.controls {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}
.controls label {
  display: block;
  margin-bottom: 16px;
  font-size: 13px;
  color: var(--text);
}
.controls select,
.controls input[type="text"] {
  width: 100%;
  margin-top: 6px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.controls select:focus,
.controls input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(106, 166, 255, 0.2);
}

/* Range Inputs */
.range-inputs {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
}
.range-inputs input[type="range"] {
  flex: 1;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  -webkit-appearance: none;
}
.range-inputs input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.2s;
}
.range-inputs input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.1);
}
.range-inputs input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: none;
  transition: transform 0.2s;
}
.range-inputs input[type="range"]::-moz-range-thumb:hover {
  transform: scale(1.1);
}
.range-inputs span {
  font-size: 12px;
  color: var(--muted);
  min-width: 30px;
  text-align: center;
}

/* Toggles */
.toggle-group {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}
.toggle-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}
.toggle-item input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

/* Actions */
.actions {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.button-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.action-btn {
  background: linear-gradient(135deg, var(--accent), #5a8fd8);
  color: white;
  border: none;
  padding: 12px 16px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  text-align: left;
}
.action-btn:hover {
  background: linear-gradient(135deg, #5a8fd8, var(--accent));
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}
.action-btn:active {
  transform: translateY(0);
}

/* Details */
#details {
  flex-shrink: 0;
  padding: 16px 20px;
}
#selectedInfo {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  padding: 12px;
  border-radius: 8px;
  font-size: 13px;
  white-space: pre-wrap;
  color: var(--text);
  min-height: 60px;
}

/* Map */
#map {
  height: 100%;
  background: var(--bg);
  position: relative;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}
.modal-content {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: white;
  border-radius: 12px 12px 0 0;
}
.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: background-color 0.2s;
}
.close-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}
.modal-body {
  padding: 20px;
  color: var(--text);
}
.modal-body h4 {
  margin: 20px 0 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
}
.modal-body h4:first-child {
  margin-top: 0;
}
.modal-body p {
  margin: 0 0 12px 0;
  line-height: 1.5;
  color: var(--text);
}
.modal-body ul {
  margin: 0 0 12px 0;
  padding-left: 20px;
}
.modal-body li {
  margin-bottom: 4px;
  line-height: 1.4;
  color: var(--text);
}

/* ===================== */
/*  Analytics Dashboard  */
/* ===================== */

/* Full-screen-ish, centered, scrollable */
.analytics-panel {
  position: fixed;
  inset: 24px 24px; /* top/right/bottom/left in one line */
  left: 0; /* ensure both sides exist */
  right: 0;
  transform: none; /* kill translate hack */
  width: min(1400px, 100dvw - 48px);
  height: calc(100dvh - 48px);
  margin: 0 auto;
  background: rgba(22, 26, 46, 0.95);
  color: #e9edf5;
  padding: 20px;
  border-radius: 16px;
  border: 1px solid var(--border);
  font-size: 12px;
  z-index: 1000;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  display: none; /* toggled by JS */
  flex-direction: column;
  backdrop-filter: blur(4px);
  overflow: auto;
}

/* Mobile tweaks */
@media (max-width: 768px) {
  .analytics-panel {
    inset: 4px; /* tight margins all around */
    width: calc(100dvw - 8px);
    height: calc(100dvh - 8px);
    border-radius: 8px;
    padding: 12px;
    margin: 0 auto;
  }
}
@media (max-width: 480px) {
  .analytics-panel {
    inset: 2px;
    width: calc(100dvw - 4px);
    height: calc(100dvh - 4px);
    border-radius: 6px;
    padding: 8px;
    margin: 0 auto;
  }
}

.analytics-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  color: white;
  border-radius: 12px;
  margin-bottom: 16px;
}

/* KPI strip */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.kpi {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.kpi .label {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.04em;
}
.kpi .value {
  font-size: 20px;
  font-weight: 700;
}

/* Single centered column of large charts */
.analytics-grid {
  display: grid;
  grid-template-columns: 1fr; /* single column */
  gap: 20px;
  min-height: 0;
  justify-items: center; /* center each card */
}

.chart-container {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  width: 100%;
  max-width: 1200px; /* nice readable width */
  display: flex;
  flex-direction: column;
  align-items: center;
}
.chart-container h4 {
  margin: 0 0 12px 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  text-align: center;
}
/* Big canvases that stay inside the card */
.chart-container canvas {
  width: 100% !important;
  height: 520px !important; /* tall, readable */
  max-height: none;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 8px;
}
::-webkit-scrollbar-track {
  background: var(--bg);
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}

/* Focus */
*:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Print */
@media print {
  #sidebar {
    display: none;
  }
  #app {
    grid-template-columns: 1fr;
  }
  #map {
    height: 100vh;
  }
}

/* MapLibre tweaks */
.maplibregl-canvas {
  border-radius: 0;
}
.maplibregl-ctrl-group {
  background: var(--panel) !important;
  border: 1px solid var(--border) !important;
}
.maplibregl-ctrl-group button {
  background: var(--panel) !important;
  color: var(--text) !important;
  border: none !important;
}
.maplibregl-ctrl-group button:hover {
  background: var(--accent) !important;
  color: white !important;
}
.maplibregl-ctrl-attrib {
  background: rgba(22, 26, 46, 0.8) !important;
  color: var(--text) !important;
  font-size: 11px !important;
}
.maplibregl-ctrl-attrib a {
  color: var(--accent) !important;
}

/* Responsive */
@media (max-width: 1024px) {
  .kpi-row {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  #sidebar {
    height: auto;
    max-height: 50vh;
    overflow-y: auto;
  }
  .toggle-group {
    flex-direction: column;
    gap: 8px;
  }
  .button-group {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .action-btn {
    flex: 1;
    min-width: 120px;
  }

  .map-legend {
    position: absolute;
    bottom: 20px;
    right: 20px;
    background: rgba(22, 26, 46, 0.9);
    color: #e9edf5;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    font-size: 12px;
    z-index: 1000;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    max-width: 220px;
  }
  .legend-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    font-weight: 600;
    color: #6aa6ff;
    margin-bottom: 8px;
  }
  .legend-toggle {
    background: none;
    border: 1px solid var(--border);
    color: #e9edf5;
    width: 22px;
    height: 22px;
    border-radius: 4px;
    cursor: pointer;
    line-height: 20px;
    padding: 0;
  }
  .legend-list {
    display: grid;
    grid-auto-rows: min-content;
    gap: 4px;
  }
  .legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
  }
  .legend-swatch {
    width: 12px;
    height: 12px;
    border-radius: 2px;
  }

  /* Compact on tablets/phones */
  @media (max-width: 768px) {
    .map-legend {
      bottom: 8px;
      right: 8px;
      padding: 8px;
      font-size: 11px;
      max-width: 180px;
    }
    .legend-item {
      gap: 6px;
    }
    .legend-swatch {
      width: 10px;
      height: 10px;
    }
    /* Show fewer items automatically; keep it minimal */
    .legend-list .legend-item:nth-of-type(n + 6) {
      display: none;
    } /* keep first 5 */
  }

  /* Phone */
  @media (max-width: 480px) {
    .map-legend {
      bottom: 6px;
      right: 6px;
      padding: 6px;
      font-size: 10px;
      max-width: 160px;
    }
    .legend-list .legend-item:nth-of-type(n + 5) {
      display: none;
    } /* keep first 4 */
  }

  /* Collapsed state: show just the header bar */
  .map-legend.collapsed .legend-list {
    display: none;
  }

  /* Analytics Panel Mobile Improvements */
  .analytics-panel {
    top: 4px;
    left: 50%;
    transform: translateX(-50%);
    width: calc(100vw - 8px);
    height: calc(100vh - 8px);
    border-radius: 8px;
    padding: 12px;
  }

  .analytics-header {
    padding: 12px 16px;
    margin-bottom: 12px;
  }

  .analytics-header h3 {
    font-size: 16px !important;
  }

  .kpi-row {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin-bottom: 12px;
  }

  .kpi {
    padding: 8px 10px;
  }

  .kpi .value {
    font-size: 16px;
  }

  .analytics-grid {
    gap: 12px;
  }

  .chart-container {
    padding: 12px;
    border-radius: 8px;
  }

  .chart-container h4 {
    font-size: 14px;
    margin-bottom: 8px;
  }

  .chart-container canvas {
    height: 300px !important;
  }
}

@media (max-width: 480px) {
  .analytics-panel {
    top: 2px;
    left: 50%;
    transform: translateX(-50%);
    width: calc(100vw - 4px);
    height: calc(100vh - 4px);
    border-radius: 6px;
    padding: 8px;
  }

  .analytics-header {
    padding: 8px 12px;
    margin-bottom: 8px;
  }

  .analytics-header h3 {
    font-size: 14px !important;
  }

  .kpi-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .kpi {
    padding: 6px 8px;
  }

  .kpi .value {
    font-size: 14px;
  }

  .analytics-grid {
    gap: 8px;
  }

  .chart-container {
    padding: 8px;
  }

  .chart-container h4 {
    font-size: 12px;
  }

  .chart-container canvas {
    height: 250px !important;
  }

  /* Touch-friendly improvements for mobile */
  .analytics-panel {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  /* Make close button easier to tap on mobile */
  .close-btn {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
  }

  /* Ensure charts are properly sized on mobile */
  .chart-container canvas {
    max-width: 100% !important;
    max-height: 250px !important;
  }
}
