/* Reset and base */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, sans-serif;
}

/* ── App shell: sidebar + map side-by-side ── */
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Controls sidebar ── */
.controls-panel {
  width: 280px;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-right: 1px solid #dee2e6;
  overflow-y: auto;
  z-index: 1000;
}

.controls-header {
  padding: 1rem 1rem 0.5rem;
  border-bottom: 1px solid #e9ecef;
  background: #f8f9fa;
}

.app-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0 0 0.1rem;
  color: #212529;
}

.app-subtitle {
  font-size: 0.75rem;
  color: #6c757d;
  margin: 0;
}

.breadcrumb-nav {
  padding: 0.5rem 1rem;
  background: #edf7dc;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.controls-body {
  padding: 1rem;
  flex: 1;
}

.control-group {
  margin-bottom: 1rem;
}

.control-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6c757d;
  margin-bottom: 0.4rem;
}

/* ── Toggle button groups ── */
.btn-toggle-group {
  display: flex;
  gap: 4px;
}

.btn-toggle-group--wrap {
  flex-wrap: wrap;
}

.btn-toggle {
  flex: 1;
  padding: 0.3rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 500;
  line-height: 1.3;
  border: 1.5px solid #c0d890;
  border-radius: 5px;
  background: #f8fafc;
  color: #495057;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
  white-space: nowrap;
  text-align: center;
}

.btn-toggle-group--wrap .btn-toggle {
  flex: 0 0 auto;  /* don't stretch when wrapping */
}

.btn-toggle:hover {
  background: #edf7dc;
  border-color: #8cc040;
  color: #4a7a12;
}

.btn-toggle.active {
  background: #5a8c18;
  border-color: #5a8c18;
  color: #fff;
  font-weight: 600;
}

/* ── Indicator sub-groups ── */
.indicator-group {
  margin-bottom: 0.6rem;
}

.indicator-group-label {
  display: block;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #adb5bd;
  margin-bottom: 0.3rem;
}

/* ── Country filter chip ── */
.country-filter-bar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 1rem;
  background: #edf7dc;
  border-bottom: 1px solid #b5d97a;
  font-size: 0.85rem;
  font-weight: 500;
  color: #4a7a12;
}

.country-filter-flag { font-size: 1.1rem; line-height: 1; }

.country-filter-clear {
  margin-left: auto;
  background: none;
  border: none;
  cursor: pointer;
  color: #6c757d;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.1rem;
}
.country-filter-clear:hover { color: #dc3545; }

/* ── CCAA filter chip (sub-level of country selection) ── */
.admin1-filter-bar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 1rem 0.35rem 1.5rem;
  background: #f2f8e8;
  border-bottom: 1px solid #c5df9a;
  font-size: 0.8rem;
  color: #4a7a12;
}

.admin1-filter-label {
  font-weight: 600;
  flex-shrink: 0;
}

.admin1-filter-name { flex: 1; }

.admin1-filter-clear {
  background: none;
  border: none;
  cursor: pointer;
  color: #6c757d;
  font-size: 0.9rem;
  padding: 0 0.1rem;
}
.admin1-filter-clear:hover { color: #dc3545; }

/* ── City filter chip (sub-level of CCAA selection) ── */
.city-filter-bar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 1rem 0.35rem 2.5rem;
  background: #eaf5df;
  border-bottom: 1px solid #b0d080;
  font-size: 0.8rem;
  color: #3d6614;
}
.city-filter-label { font-weight: 600; flex-shrink: 0; }
.city-filter-name  { flex: 1; }
.city-filter-clear {
  background: none; border: none; cursor: pointer;
  color: #6c757d; font-size: 0.9rem; padding: 0 0.1rem;
}
.city-filter-clear:hover { color: #dc3545; }

/* Clickable country name cells in the countries table */
.col-country-clickable {
  cursor: pointer;
}
.col-country-clickable:hover {
  color: #4a7a12;
  text-decoration: underline;
}

.comparison-notice {
  padding: 0.75rem 1rem;
  background: #fff3cd;
  border-top: 1px solid #ffc107;
}

/* ── Legend ── */
.legend-container {
  padding: 0.75rem 1rem;
  border-top: 1px solid #e9ecef;
}

.legend-title {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6c757d;
  margin-bottom: 0.4rem;
}

.legend-gradient {
  height: 10px;
  border-radius: 4px;
  margin-bottom: 0.25rem;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: #6c757d;
}

/* ── Data summary ── */
.data-summary {
  padding: 0.75rem 1rem;
  border-top: 1px solid #e9ecef;
  display: flex;
  gap: 1rem;
}

.summary-stat {
  display: flex;
  flex-direction: column;
}

.summary-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  color: #6c757d;
}

.summary-value {
  font-size: 0.9rem;
  font-weight: 600;
  color: #212529;
}

/* ── Context header (breadcrumb + KPI boxes) ── */
.context-header {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.5rem 0.75rem;
  background: #223510;
  border-bottom: 1px solid #172309;
}

.context-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 0.9rem;
  color: #c8e080;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.context-title strong {
  color: #fff;
  font-weight: 700;
}

.ctx-sep {
  color: #6a8540;
  margin: 0 0.15rem;
}

.context-kpis {
  display: flex;
  gap: 0.45rem;
  overflow-x: auto;
  scrollbar-width: none;
}
.context-kpis::-webkit-scrollbar { display: none; }

.kpi-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 0.2rem 0.7rem;
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.13);
  border-radius: 6px;
  min-width: 86px;
  flex-shrink: 0;
  transition: background 0.12s;
}
.kpi-box:hover {
  background: rgba(255,255,255,0.12);
}

.kpi-label {
  font-size: 0.59rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #9ab855;
  line-height: 1.5;
}

.kpi-value {
  font-size: 0.9rem;
  font-weight: 700;
  color: #f0f8e8;
  line-height: 1.25;
}

/* ── Map area (flex column: map pane on top, countries table below) ── */
.map-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* Map pane — left: Leaflet map; right: year chart */
.map-pane {
  flex: 0 0 55%;
  display: flex;
  flex-direction: row;
  overflow: hidden;
}

/* Left half: contains the map and all drill-down overlays */
.map-half {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

#world-map {
  width: 100%;
  height: 100%;
}

/* Right half: year-by-year bar chart — 40% of the map pane width */
.chart-half {
  flex: 0 0 40%;
  display: flex;
  flex-direction: column;
  border-left: 1px solid #dee2e6;
  background: #fff;
  overflow: hidden;
}

#year-chart {
  flex: 1;
  min-height: 0;
}

/* Countries data table pane — toolbar fixed, scroll area below */
.table-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-top: 2px solid #dee2e6;
}

.table-pane-toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0.75rem;
  background: #f8f9fa;
  border-bottom: 1px solid #dee2e6;
}

/* ── Listing tab bar (replaces the old table-pane title + sidebar Show/Thematic controls) ── */
.listing-tabs {
  display: flex;
  gap: 2px;
  align-items: center;
  flex-wrap: wrap;
}

.listing-tab {
  padding: 0.2rem 0.65rem;
  font-size: 0.72rem;
  font-weight: 600;
  border: 1px solid #dee2e6;
  border-radius: 4px;
  background: #fff;
  color: #495057;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.1s, color 0.1s, border-color 0.1s;
}

.listing-tab:hover:not(.active) {
  background: #edf7dc;
  border-color: #a9d96c;
  color: #3d6614;
}

.listing-tab.active {
  background: #74b320;
  border-color: #5a8f18;
  color: #fff;
}

.table-scroll-area {
  flex: 1;
  overflow: auto;
}

.countries-table {
  margin-bottom: 0;
  white-space: nowrap;
  font-size: 0.8rem;
}

.countries-table thead th {
  position: sticky;
  top: 0;
  background: #f8f9fa;
  z-index: 2;
  border-bottom: 2px solid #dee2e6 !important;
  font-size: 0.72rem;
  padding: 0.35rem 0.5rem;
}

.countries-table tbody td {
  padding: 0.25rem 0.5rem;
}

.countries-table .col-active {
  background-color: #edf7dc;
}

.countries-table thead th.col-active {
  background-color: #d0e8a0;
  color: #4a7a12;
}

.countries-table thead th.col-sortable {
  cursor: pointer;
  user-select: none;
}

.countries-table thead th.col-sortable:not(.col-active):hover {
  background: #edf7dc;
  color: #4a7a12;
}

.countries-table .col-rank {
  color: #adb5bd;
  width: 2.5rem;
  text-align: right;
}

/* ── Loading overlay ── */
.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.92);
  z-index: 2000;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #dee2e6;
  border-top-color: #74b320;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 1rem;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-message {
  color: #6c757d;
  font-size: 0.9rem;
}

/* ── Error overlay ── */
.error-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.95);
  z-index: 2000;
}

.error-content {
  text-align: center;
  padding: 2rem;
  max-width: 400px;
}

.error-content h3 { color: #dc3545; }
.error-content code {
  background: #f8f9fa;
  padding: 0.2em 0.4em;
  border-radius: 4px;
  font-size: 0.85em;
}

/* ── Spain panel ── */
.spain-panel {
  display: flex;
  height: 100%;
}

#spain-map {
  flex: 1;
  height: 100%;
}

.orgs-panel {
  width: 400px;
  border-left: 1px solid #dee2e6;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.orgs-title, .panel-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #e9ecef;
  margin: 0;
}

.orgs-filters {
  padding: 0.5rem 1rem;
  border-bottom: 1px solid #e9ecef;
}

.orgs-table-wrapper, .cities-table-wrapper {
  flex: 1;
  overflow-y: auto;
}

/* ── Cities panel ── */
.cities-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.cities-table-wrapper {
  padding: 0 1rem;
}

/* ── Leaf selection (terminal node in the exploration tree) ── */
.leaf-back-row td {
  cursor: pointer;
  color: #4a7a12;
  font-size: 0.72rem;
  padding: 0.3rem 0.5rem !important;
  border-bottom: 1px solid #dee2e6;
}
.leaf-back-row:hover td { background: #edf7dc; }

.row-leaf-selected td {
  background-color: #fffbeb !important;
  border-top: 2px solid #f59e0b;
  border-bottom: 2px solid #f59e0b;
}

/* Sortable table header */
th[data-sortable] {
  cursor: pointer;
  user-select: none;
}

th[data-sortable]:hover { background: #f8f9fa; }
