:root {
  --navy: #123b59;
  --navy-2: #0b2d45;
  --teal: #159d91;
  --teal-soft: #e6f5f3;
  --ink: #15344b;
  --muted: #617485;
  --line: #d7e1e7;
  --paper: #fff;
  --wash: #f3f7f9;
  --danger: #b93b4c;
  --shadow: 0 10px 30px rgba(14, 48, 71, .12);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; font-family: Inter, "Segoe UI", Arial, sans-serif; color: var(--ink); background: var(--wash); }
button, select, input { font: inherit; }
button { cursor: pointer; }

.topbar {
  height: 86px; display: flex; align-items: center; justify-content: space-between;
  padding: 0 34px; background: var(--navy); color: white; border-bottom: 4px solid var(--teal); z-index: 1000;
}
.brand { display: flex; gap: 14px; align-items: center; text-decoration: none; color: white; }
.brand-mark { display: grid; place-items: center; width: 52px; height: 52px; background: var(--teal); border-radius: 15px; font-size: 21px; font-weight: 900; }
.brand strong { display: block; font-size: 20px; letter-spacing: -.2px; }
.brand small { display: block; margin-top: 4px; font-size: 13px; color: #d7eef3; }
nav { display: flex; align-self: stretch; gap: 4px; }
.nav-link { border: 0; background: transparent; color: #f4fbfd; padding: 0 17px; font-weight: 750; border-radius: 10px; }
.nav-link:hover, .nav-link.active { background: rgba(255,255,255,.11); }

.view { display: none; }
.view.active { display: block; }
#view-explorer.active { height: calc(100vh - 86px); display: grid; grid-template-columns: 340px minmax(480px, 1fr) 292px; }

.sidebar, .analysis-panel { background: var(--paper); overflow-y: auto; padding: 28px 24px; z-index: 500; }
.sidebar { border-right: 1px solid var(--line); }
.analysis-panel { border-left: 1px solid var(--line); }
.eyebrow { color: var(--teal); text-transform: uppercase; letter-spacing: 1.8px; font-weight: 850; font-size: 12px; }
h1 { margin: 8px 0 24px; font-size: 30px; letter-spacing: -.7px; }
h2 { letter-spacing: -.35px; }
h3 { font-size: 14px; margin: 24px 0 10px; }
label:not(.check) { display: block; margin: 17px 0 7px; font-size: 13px; font-weight: 800; }
select, input { width: 100%; min-height: 46px; border: 1px solid #bfcfd8; border-radius: 9px; background: white; color: var(--ink); padding: 0 12px; }
select:focus, input:focus { outline: 3px solid rgba(21,157,145,.16); border-color: var(--teal); }
.search-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.primary { border: 0; border-radius: 9px; padding: 0 18px; background: #247fab; color: white; font-weight: 800; }
.layer-box { margin-top: 24px; padding: 15px; background: var(--wash); border: 1px solid var(--line); border-radius: 12px; }
.box-title { font-size: 13px; font-weight: 850; margin-bottom: 10px; }
.check { display: flex; gap: 9px; align-items: center; margin: 8px 0; color: #3f5667; font-size: 13px; }
.check input { width: 16px; min-height: 16px; accent-color: var(--teal); }
.selection-card { margin-top: 22px; padding: 16px; border-left: 4px solid var(--teal); background: var(--teal-soft); border-radius: 5px 11px 11px 5px; }
.selection-card.empty { border-left-color: #b6c5ce; background: #f5f7f8; }
.selection-kicker { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; font-weight: 800; }
.selection-card h2 { font-size: 19px; margin: 7px 0 3px; }
.selection-card .territory-parent { display: block; font-size: 12px; font-weight: 650; color: var(--muted); margin-bottom: 9px; }
.selection-card p { margin: 0; font-size: 13px; line-height: 1.45; color: #486071; }
.selection-value { font-size: 25px; font-weight: 900; color: var(--navy); margin: 8px 0 1px; }
.sidebar-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.quiet { border: 1px solid var(--line); background: white; color: var(--navy); border-radius: 8px; min-height: 38px; font-size: 12px; font-weight: 800; }

.map-stage { position: relative; min-width: 0; background: #edf4f6; }
#map { width: 100%; height: 100%; }
.loading { position: absolute; inset: 0; z-index: 900; display: grid; place-items: center; background: rgba(238,245,247,.95); font-weight: 850; }
.loading.error { color: var(--danger); padding: 40px; text-align: center; }
.map-title { position: absolute; top: 16px; left: 50%; transform: translateX(-50%); z-index: 450; background: rgba(255,255,255,.93); box-shadow: var(--shadow); border-radius: 8px; padding: 9px 14px; font-size: 13px; font-weight: 850; white-space: nowrap; }
.legend { position: absolute; left: 16px; bottom: 38px; z-index: 450; width: 205px; background: rgba(255,255,255,.96); padding: 13px; border-radius: 10px; box-shadow: var(--shadow); font-size: 12px; }
.legend-title { font-weight: 850; margin-bottom: 9px; }
.legend-row { display: flex; gap: 8px; align-items: center; margin: 5px 0; }
.legend-swatch { width: 22px; height: 13px; border-radius: 2px; flex: none; }
.map-source { position: absolute; z-index: 450; left: 50%; bottom: 7px; transform: translateX(-50%); background: rgba(255,255,255,.86); padding: 3px 8px; font-size: 9px; color: #506779; white-space: nowrap; }
.leaflet-tooltip.territory-label { background: transparent; border: 0; box-shadow: none; color: #244b62; font-size: 9px; font-weight: 800; text-shadow: 0 1px 0 white, 1px 0 0 white, 0 -1px 0 white, -1px 0 0 white; }
.popup-name { font-size: 16px; font-weight: 900; color: var(--navy); }
.popup-parent { display: block; color: var(--muted); margin: 2px 0 9px; }
.popup-value { padding-top: 8px; border-top: 1px solid var(--line); font-size: 18px; font-weight: 900; color: var(--teal); }

.panel-header { display: flex; justify-content: space-between; align-items: center; }
.panel-header button { border: 0; background: transparent; color: var(--muted); font-size: 27px; }
.analysis-panel h2 { margin: 8px 0 16px; font-size: 21px; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: var(--wash); padding: 11px; border-radius: 8px; }
.stat span { display: block; color: var(--muted); font-size: 10px; text-transform: uppercase; font-weight: 800; }
.stat strong { display: block; margin-top: 4px; font-size: 16px; }
.rank-row { display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; padding: 8px 0; border-bottom: 1px solid #e8eef1; align-items: center; font-size: 12px; }
.rank-row button { border: 0; text-align: left; background: none; color: var(--ink); padding: 0; font-weight: 700; }
.rank-row strong { color: var(--teal); }
.method-note { margin-top: 22px; padding: 12px; background: #fff9e9; border-radius: 8px; color: #6a5a2c; font-size: 11px; line-height: 1.45; }

.content-view { min-height: calc(100vh - 86px); padding: 38px max(4vw, 34px) 70px; }
.section-head { display: flex; align-items: end; justify-content: space-between; gap: 24px; margin-bottom: 24px; }
.section-head h1 { margin-bottom: 0; }
.section-head select { width: 280px; }
.profile-identity { padding: 18px 22px; background: var(--navy); color: white; border-radius: 14px; margin-bottom: 16px; }
.profile-identity h2 { display: inline; margin: 0; font-size: 25px; }
.profile-identity span { display: block; margin-top: 5px; color: #cae4ec; }
.kpi-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px; }
.kpi { background: white; border: 1px solid var(--line); padding: 16px; border-radius: 12px; }
.kpi span { display: block; color: var(--muted); font-size: 11px; min-height: 28px; }
.kpi strong { display: block; font-size: 22px; margin-top: 7px; }
.chart-grid, .compare-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.chart-card, .table-card, .profile-grid article, .method-grid article, .sources-table { background: white; border: 1px solid var(--line); border-radius: 13px; padding: 19px; }
.chart-card h2, .table-card h2, .profile-grid h2, .sources-table h2 { font-size: 17px; margin: 0 0 14px; }
.chart-card { min-height: 330px; }
.chart-card canvas { max-height: 255px; }
.inline-control { display: flex; align-items: center; gap: 8px; float: right; margin-top: -42px; }
.inline-control label { margin: 0; }
.inline-control select { min-height: 34px; width: 180px; font-size: 12px; }
.profile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.metric-bar { margin: 13px 0; }
.metric-bar div:first-child { display: flex; justify-content: space-between; font-size: 12px; font-weight: 750; }
.track { height: 7px; border-radius: 5px; background: #e6edef; margin-top: 6px; overflow: hidden; }
.track i { display: block; height: 100%; background: var(--teal); }
.lisa-card { display: grid; grid-template-columns: 1fr auto; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.cluster-pill { border-radius: 99px; padding: 4px 9px; font-size: 10px; font-weight: 850; background: #edf1f3; }

.compare-toolbar { display: grid; grid-template-columns: repeat(3, minmax(180px, 1fr)); gap: 12px; background: white; padding: 14px; border: 1px solid var(--line); border-radius: 12px; }
.compare-names { margin: 20px 0 0; display: flex; gap: 10px; flex-wrap: wrap; }
.territory-chip { background: var(--teal-soft); color: #116e68; padding: 7px 12px; border-radius: 99px; font-size: 12px; font-weight: 800; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th { text-align: left; color: var(--muted); font-size: 10px; text-transform: uppercase; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--line); }
td:not(:first-child), th:not(:first-child) { text-align: right; }
.method-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.method-grid h2 { font-size: 16px; margin: 0 0 9px; }
.method-grid p { margin: 0; color: #486071; font-size: 13px; line-height: 1.6; }
.sources-table { margin-top: 16px; }

@media (max-width: 1150px) {
  #view-explorer.active { grid-template-columns: 310px 1fr; }
  .analysis-panel { display: none; }
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .method-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 760px) {
  .topbar { height: auto; min-height: 80px; padding: 12px 16px; align-items: flex-start; }
  .brand small { display: none; }
  nav { overflow-x: auto; }
  .nav-link { padding: 13px 9px; font-size: 12px; white-space: nowrap; }
  #view-explorer.active { height: auto; display: block; }
  .sidebar { height: auto; }
  .map-stage { height: 68vh; }
  .map-title { max-width: 70%; overflow: hidden; text-overflow: ellipsis; }
  .map-source { display: none; }
  .content-view { padding: 24px 16px; }
  .section-head { display: block; }
  .section-head select { width: 100%; }
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .chart-grid, .profile-grid, .compare-layout, .method-grid { grid-template-columns: 1fr; }
  .compare-toolbar { grid-template-columns: 1fr; }
}

@media print {
  .topbar, .sidebar, .analysis-panel, .leaflet-control-container { display: none !important; }
  #view-explorer.active { display: block; height: 100vh; }
  .map-stage, #map { height: 100vh; }
  .map-title { top: 10px; }
}
