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

:root {
  /* MDA — Madinah Region Development Authority identity */
  --bg: #FFFFFF;            /* pure white canvas */
  --surface: #FFFFFF;       /* panels / toolbar */
  --surface-hover: #F2F2F2; /* card grey hover */
  --border: #D9D9D9;        /* faint borders */
  --text: #000000;          /* body text */
  --text-muted: #6F7271;    /* muted supporting */
  --accent: #006A5B;        /* MDA brand green */
  --accent-dim: #00705C;    /* bright green (hover) */
  --brand-deep: #004438;    /* heading deep green */
  --brand-dark: #005448;    /* dark teal sections */
  --gold: #8D7632;          /* premium gold accent */
  --cluster-sm: #006A5B;
  --cluster-md: #8D7632;
  --cluster-lg: #004438;
  --point: #006A5B;
  --sidebar-w: 340px;
  --font: "29LT Azer", "Tajawal", "Segoe UI", system-ui, sans-serif;
}

html, body { height: 100%; width: 100%; font-family: var(--font); background: var(--bg); color: var(--text); overflow: hidden; }

/* ── Toolbar ── */
#toolbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 12px 24px;
  background: var(--surface); border-bottom: 2px solid var(--accent);
}
#toolbar h1 { font-size: 17px; font-weight: 800; white-space: nowrap; color: var(--brand-deep); }
#toolbarControls { display: flex; align-items: center; gap: 12px; flex: 1; flex-wrap: wrap; }

.search-group { flex: 1; min-width: 180px; max-width: 360px; }
#searchInput {
  width: 100%; padding: 8px 12px; border-radius: 4px;
  border: 1px solid var(--border); background: #FFFFFF; color: var(--text);
  font-family: var(--font); font-size: 13px; outline: none; transition: border-color 0.2s;
}
#searchInput:focus { border: 2px solid var(--accent); }
#searchInput::placeholder { color: var(--text-muted); opacity: 0.8; }

.filter-group { display: flex; align-items: center; gap: 6px; }
.filter-group label { font-size: 13px; font-weight: 700; color: var(--brand-deep); white-space: nowrap; }
.filter-group select {
  padding: 7px 10px; border-radius: 4px;
  border: 1px solid var(--border); background: #FFFFFF; color: var(--text);
  font-family: var(--font); font-size: 13px; max-width: 200px; cursor: pointer;
}
.filter-group select:focus { border: 2px solid var(--accent); outline: none; }

#busToggle, #mosqueToggle {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 36px; padding: 0; border-radius: 0;
  border: 2px solid var(--accent); background: #FFFFFF; color: var(--accent);
  font-size: 18px; line-height: 1; cursor: pointer;
  transition: all 0.15s;
}
#busToggle:hover, #mosqueToggle:hover {
  background: var(--accent); color: #FFFFFF;
}
#busToggle.active, #mosqueToggle.active {
  background: var(--accent); color: #FFFFFF;
  border-color: var(--accent);
}

#counter[hidden] { display: none; }

/* ── Map ── (leaves room for the always-visible district panel on the right) */
#map { position: fixed; top: 46px; bottom: 0; left: 0; right: 400px; }

/* ── Sidebar ── */
#sidebar {
  position: fixed; top: 50px; right: 0; bottom: 0;
  width: var(--sidebar-w); background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 20px; overflow-y: auto; z-index: 13;
  transition: transform 0.25s ease;
}
#sidebar.hidden { transform: translateX(100%); }

#closeSidebar {
  position: absolute; top: 10px; left: 10px;
  background: none; border: none; color: var(--text-muted);
  font-size: 22px; cursor: pointer; line-height: 1;
}
#closeSidebar:hover { color: var(--text); }

#sidebar h2 { font-size: 18px; margin-bottom: 16px; line-height: 1.4; color: var(--brand-deep); font-weight: 800; }

.sidebar-tag {
  display: inline-block; padding: 4px 10px;
  border-radius: 12px; font-size: 11px; font-weight: 600;
  margin-bottom: 12px;
  background: rgba(0,106,91,0.12); color: var(--accent);
}

.info-row {
  display: flex; flex-direction: column; gap: 4px;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.info-row:last-child { border-bottom: none; }
.info-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.info-value { font-size: 15px; line-height: 1.5; font-weight: 500; }

/* ── Cluster Panel ── */
#clusterPanel {
  position: fixed; top: 50px; right: 0; bottom: 0;
  width: 380px; background: var(--surface);
  border-left: 1px solid var(--border);
  z-index: 13;
  display: flex; flex-direction: column;
  transition: transform 0.25s ease;
}
#clusterPanel.hidden { transform: translateX(100%); }

#closeClusterPanel {
  position: absolute; top: 10px; left: 10px;
  background: none; border: none; color: var(--text-muted);
  font-size: 22px; cursor: pointer; line-height: 1; z-index: 1;
}
#closeClusterPanel:hover { color: var(--text); }

.cluster-header {
  display: flex; align-items: center; gap: 10px;
  padding: 18px 20px 12px;
  border-bottom: 1px solid var(--border);
}
.cluster-header h2 { font-size: 16px; font-weight: 800; color: var(--brand-deep); }
.cluster-count-badge {
  padding: 3px 10px; border-radius: 12px;
  background: var(--accent); color: var(--bg);
  font-size: 12px; font-weight: 700;
}

.cluster-tabs {
  display: flex; gap: 0;
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
}
.cluster-tab {
  flex: 1; padding: 10px 4px;
  background: none; border: none; cursor: pointer;
  color: var(--text-muted); font-size: 13px;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
}
.cluster-tab:hover { color: var(--text); }
.cluster-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

#clusterList {
  flex: 1; overflow-y: auto; padding: 8px;
}

.cluster-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 8px;
  cursor: pointer; transition: background 0.15s;
}
.cluster-item:hover { background: var(--surface-hover); }
.cluster-item-dot {
  width: 10px; height: 10px; border-radius: 50%;
  flex-shrink: 0;
}
.cluster-item-content { flex: 1; min-width: 0; }
.cluster-item-name {
  font-size: 14px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cluster-item-activity {
  font-size: 12px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 2px;
}

.cluster-category {
  margin-bottom: 4px;
}
.cluster-category-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; cursor: pointer;
  border-radius: 8px;
  user-select: none;
}
.cluster-category-header:hover { background: var(--surface-hover); }
.cluster-category-dot {
  width: 12px; height: 12px; border-radius: 50%;
  flex-shrink: 0;
}
.cluster-category-name {
  flex: 1; font-size: 14px; font-weight: 600;
}
.cluster-category-count {
  background: var(--bg); color: var(--text-muted);
  padding: 2px 8px; border-radius: 10px;
  font-size: 11px;
}
.cluster-category-arrow {
  color: var(--text-muted); font-size: 11px;
  transition: transform 0.15s;
}
.cluster-category.open .cluster-category-arrow { transform: rotate(90deg); }
.cluster-category-items {
  display: none; padding-right: 18px;
}
.cluster-category.open .cluster-category-items { display: block; }

/* ── Style Switcher ── */
#styleSwitcher {
  position: fixed; bottom: 24px; left: 24px; z-index: 10;
  display: flex; gap: 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
}
.style-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 10px; border-radius: 8px;
  border: 2px solid transparent; background: none;
  color: var(--text-muted); cursor: pointer;
  transition: all 0.2s;
}
.style-btn:hover { background: var(--surface-hover); color: var(--text); }
.style-btn.active { border-color: var(--accent); color: var(--accent); background: rgba(0,106,91,0.10); }
.style-icon { font-size: 20px; line-height: 1; }
.style-label { font-size: 10px; white-space: nowrap; }

/* ── MapLibre popup ── */
.maplibregl-popup-content {
  background: #FFFFFF !important; color: var(--text) !important;
  border-radius: 8px !important; padding: 10px 14px !important;
  font-family: var(--font) !important;
  font-size: 13px !important; box-shadow: 0 4px 16px rgba(0,0,0,.12) !important;
  border: 1px solid var(--border) !important;
  direction: rtl !important;
  max-width: 280px !important;
  line-height: 1.55 !important;
  word-wrap: break-word !important;
}
.maplibregl-popup-content strong { font-weight: 800; }
.maplibregl-popup-tip { border-top-color: #FFFFFF !important; }
.maplibregl-popup-close-button { color: var(--text-muted) !important; font-size: 16px !important; }

/* District name popup (on hover) */
.district-popup .maplibregl-popup-content {
  background: var(--accent) !important;
  color: #FFFFFF !important;
  padding: 6px 14px !important;
  border-radius: 0 !important;
  border: none !important;
  box-shadow: 0 4px 14px rgba(0,106,91,.40) !important;
  pointer-events: none;
}
.district-popup .maplibregl-popup-tip {
  border-top-color: var(--accent) !important;
  border-bottom-color: var(--accent) !important;
}
.district-popup-name {
  font-size: 14px; font-weight: 700; white-space: nowrap;
}

/* ── District Statistics Panel (LIGHT MODE) ── */
#districtPanel {
  /* MDA light tokens scoped to this panel */
  --lp-bg: #FFFFFF;
  --lp-surface: #F2F2F2;
  --lp-text: #000000;
  --lp-muted: #6F7271;
  --lp-border: #D9D9D9;
  --lp-green: #006A5B;

  position: fixed; top: 46px; right: 0; bottom: 0;
  width: 400px; background: var(--lp-bg);
  color: var(--lp-text);
  border-left: 1px solid var(--lp-border);
  z-index: 12;
  display: flex; flex-direction: column;
  box-shadow: -8px 0 24px rgba(15,23,42,.08);
}

.dp-empty-state {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  height: 100%; padding: 40px 28px;
  color: var(--lp-muted);
}
.dp-empty-icon { font-size: 48px; margin-bottom: 16px; opacity: 0.55; }
.dp-empty-text { font-size: 14px; line-height: 1.7; max-width: 280px; }

.district-panel-header {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--lp-border);
  background: linear-gradient(135deg, rgba(0,108,53,0.10), transparent);
}
.district-eyebrow {
  font-size: 12px; color: var(--lp-green); font-weight: 700; margin-bottom: 4px;
}
.district-panel-header h2 { font-size: 22px; font-weight: 800; line-height: 1.3; color: var(--lp-text); }

#districtPanelBody {
  flex: 1; overflow-y: auto; padding: 16px 20px 32px;
}

.dp-section { margin-bottom: 24px; }
.dp-section-title {
  font-size: 13px; color: var(--lp-muted);
  letter-spacing: 0.3px;
  margin-bottom: 10px; font-weight: 700;
}
.dp-hint {
  font-size: 11px; color: var(--lp-muted);
  margin: -4px 0 10px; line-height: 1.5;
}

/* Headline stat blocks */
.dp-headline {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 22px;
}
.dp-head-cell {
  background: rgba(0,108,53,0.07);
  border: 1px solid rgba(0,108,53,0.20);
  border-radius: 10px;
  padding: 12px 8px;
  text-align: center;
  display: flex; flex-direction: column; justify-content: center;
}
.dp-head-num {
  font-size: 18px; font-weight: 800; color: var(--lp-green);
  line-height: 1.2;
}
.dp-head-lbl {
  font-size: 11px; color: var(--lp-muted); margin-top: 4px; line-height: 1.4;
}

/* Density rows (establishments per 1,000 residents) */
.dp-dense {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; background: var(--lp-surface);
  border: 1px solid var(--lp-border); border-radius: 8px;
  margin-bottom: 6px;
}
.dp-dense-main { flex: 1; min-width: 0; }
.dp-dense-name {
  font-size: 13px; font-weight: 600; color: var(--lp-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dp-dense-sub { font-size: 11px; color: var(--lp-muted); margin-top: 2px; }
.dp-dense-rate { text-align: left; flex-shrink: 0; }
.dp-dense-num { font-size: 18px; font-weight: 800; color: var(--lp-text); display: block; }
.dp-dense-unit { font-size: 10px; color: var(--lp-muted); }

/* KPI grid (population, totals) */
.dp-kpi-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
}
.dp-kpi {
  background: var(--lp-surface); border-radius: 10px; padding: 12px;
  border: 1px solid var(--lp-border);
}
.dp-kpi-value { font-size: 20px; font-weight: 800; color: var(--lp-text); }
.dp-kpi-label { font-size: 11px; color: var(--lp-muted); margin-top: 2px; }
.dp-kpi.wide { grid-column: 1 / -1; }
.dp-kpi.accent { background: rgba(0,108,53,0.08); border-color: rgba(0,108,53,0.25); }
.dp-kpi.accent .dp-kpi-value { color: var(--lp-green); }

/* Ratio cards */
.dp-ratio {
  display: flex; align-items: baseline; gap: 8px;
  padding: 10px 12px; background: var(--lp-surface);
  border-radius: 8px; margin-bottom: 6px;
  border: 1px solid var(--lp-border);
}
.dp-ratio-num { font-size: 18px; font-weight: 800; color: var(--lp-green); }
.dp-ratio-text { font-size: 13px; color: var(--lp-text); line-height: 1.5; }

/* Activity breakdown bars */
.dp-act {
  margin-bottom: 10px;
}
.dp-act-top {
  display: flex; align-items: center; gap: 8px; margin-bottom: 4px;
}
.dp-act-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.dp-act-name {
  flex: 1; font-size: 13px; color: var(--lp-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dp-act-count { font-size: 13px; font-weight: 800; color: var(--lp-text); }
.dp-act-pct { font-size: 11px; color: var(--lp-muted); min-width: 38px; text-align: left; }
.dp-act-bar {
  height: 6px; background: var(--lp-border); border-radius: 3px; overflow: hidden;
}
.dp-act-bar-fill { height: 100%; border-radius: 3px; }

.dp-empty {
  padding: 16px; text-align: center;
  color: var(--lp-muted); font-size: 13px;
}

/* ── Responsive ── */
@media (max-width: 768px) {
  #toolbar { flex-direction: column; align-items: stretch; gap: 6px; padding: 8px 10px; }
  #toolbar h1 { font-size: 13px; line-height: 1.2; }
  #toolbarControls { gap: 6px; flex-wrap: wrap; }
  .search-group { flex: 1 1 100%; min-width: 0; max-width: none; }
  .filter-group { flex: 1; min-width: 0; }
  .filter-group label { font-size: 11px; }
  .filter-group select { font-size: 12px; padding: 6px 8px; min-width: 0; max-width: 100%; flex: 1; }
  #busToggle, #mosqueToggle { width: 32px; height: 32px; font-size: 16px; }

  /* Map fills the space above the collapsed sidebar peek */
  #map { top: 110px; right: 0; bottom: var(--mobile-dp-h, 76px); }

  #sidebar, #clusterPanel {
    top: auto; bottom: 0; right: 0; left: 0;
    width: 100%; max-height: 65vh;
    border-left: none; border-top: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transition: transform 0.25s ease;
    z-index: 13;
  }
  #sidebar.hidden, #clusterPanel.hidden { transform: translateY(100%); }

  /* District panel: compact peek by default, expands when content is loaded */
  #districtPanel {
    top: auto; bottom: 0; right: 0; left: 0;
    width: 100%; height: 76px;
    border-left: none; border-top: 2px solid var(--lp-green);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 24px rgba(15,23,42,.10);
    transition: height 0.28s cubic-bezier(.2,.7,.2,1);
    overflow: hidden;
  }
  #districtPanel.expanded { height: 62vh; }

  /* Add a drag handle to communicate it's interactive */
  #districtPanel::before {
    content: ""; position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
    width: 44px; height: 4px; border-radius: 999px; background: var(--lp-border);
    pointer-events: none;
  }
  #districtPanel.expanded::before { background: var(--lp-green); }

  .district-panel-header { padding: 16px 16px 10px; padding-top: 18px; cursor: pointer; }
  .district-panel-header h2 { font-size: 16px; }
  .district-eyebrow { font-size: 11px; }
  #districtPanelBody { padding: 12px 16px 24px; }

  /* In collapsed state, hide the body content for a clean peek */
  #districtPanel:not(.expanded) #districtPanelBody { display: none; }
  /* And lift the empty-state icon hint into the header area */
  #districtPanel:not(.expanded) .district-panel-header { padding-bottom: 14px; }

  #styleSwitcher { bottom: calc(var(--mobile-dp-h, 76px) + 12px); left: 12px; padding: 4px; gap: 4px; transition: bottom 0.28s; }
  #districtPanel.expanded ~ #styleSwitcher,
  body:has(#districtPanel.expanded) #styleSwitcher { bottom: calc(62vh + 12px); }
  .style-btn { padding: 4px 8px; }
  .style-icon { font-size: 16px; }
  .style-label { font-size: 9px; }
}
