/* ── Variables ─────────────────────────────────────────── */
:root {
  --psd-blue:         #003087;
  --psd-blue-dark:    #001f5c;
  --psd-blue-mid:     #0050b3;
  --psd-blue-light:   #dde8f5;
  --psd-orange:       #f05a28;
  --psd-orange-light: #fff0eb;
  --psd-orange-dark:  #c4441a;

  --gray-50:  #f8f9fa;
  --gray-100: #f1f3f5;
  --gray-200: #e9ecef;
  --gray-300: #dee2e6;
  --gray-500: #adb5bd;
  --gray-700: #495057;
  --gray-900: #212529;

  --sidebar-width: 380px;
  --header-height: 65px;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,.12);
  --shadow-md: 0 4px 16px rgba(0,0,0,.16);
}

/* ── Reset ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; color: var(--gray-900); background: var(--gray-100); }

/* ── Header ────────────────────────────────────────────── */
.app-header {
  height: var(--header-height);
  background: var(--psd-blue);
  color: white;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 6px rgba(0,0,0,.25);
  position: relative;
  z-index: 1000;
}
.header-inner { display: flex; align-items: center; gap: 14px; }
.header-logo {
  font-size: 22px;
  font-weight: 800;
  letter-spacing: 1px;
  color: var(--psd-orange);
  border: 2.5px solid var(--psd-orange);
  padding: 2px 8px;
  border-radius: 4px;
  line-height: 1;
}
.header-titles h1 { font-size: 16px; font-weight: 700; line-height: 1.2; margin-top: 6px; }
.header-titles p  { font-size: 12px; opacity: .75; }

/* ── Main layout ───────────────────────────────────────── */
.app-main {
  display: flex;
  height: calc(100vh - var(--header-height));
  overflow: hidden;
}

/* ── Map ───────────────────────────────────────────────── */
.map-container {
  flex: 1;
  position: relative;
  min-width: 0;
}
#map {
  width: 100%;
  height: 100%;
}
.map-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(255,255,255,.85);
  font-size: 14px;
  color: var(--gray-700);
  z-index: 500;
}
.map-loading.hidden { display: none; }

/* ── Sidebar ───────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: white;
  border-left: 1px solid var(--gray-200);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: -2px 0 8px rgba(0,0,0,.08);
}
.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 0;
}

/* ── Sidebar: district list ────────────────────────────── */
.sidebar-header {
  padding: 16px;
  background: var(--psd-blue);
  color: white;
  position: sticky;
  top: 0;
  z-index: 10;
}
.sidebar-header h2 { font-size: 16px; font-weight: 700; }
.sidebar-header .subtitle { font-size: 12px; opacity: .8; margin-top: 3px; }

.district-list { padding: 12px; display: flex; flex-direction: column; gap: 8px; }

.district-card {
  background: white;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color .15s, box-shadow .15s, background .15s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.district-card:hover {
  border-color: var(--psd-orange);
  background: var(--psd-orange-light);
  box-shadow: var(--shadow);
}
.district-card.active {
  border-color: var(--psd-orange);
  background: var(--psd-orange-light);
}
.district-name {
  font-weight: 600;
  font-size: 14px;
  color: var(--psd-blue);
}
.district-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  font-size: 11px;
  color: var(--gray-700);
  white-space: nowrap;
}
.district-meta .votantes-badge {
  background: var(--psd-blue-light);
  color: var(--psd-blue);
  padding: 2px 7px;
  border-radius: 10px;
  font-weight: 600;
}
.district-no-data {
  font-size: 11px;
  color: var(--gray-500);
  font-style: italic;
}

/* ── Sidebar: district results ─────────────────────────── */
.results-header {
  padding: 12px 16px;
  background: var(--psd-blue);
  color: white;
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
}
.btn-back {
  background: rgba(255,255,255,.15);
  border: 1px solid rgba(255,255,255,.35);
  color: white;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: background .15s;
}
.btn-back:hover { background: rgba(255,255,255,.28); }
.results-header h2 { font-size: 15px; font-weight: 700; }

.results-section {
  padding: 14px 14px 0;
}
.results-section:last-child { padding-bottom: 16px; }

.section-group-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .6px;
  text-transform: uppercase;
  color: var(--gray-500);
  padding: 10px 0 6px;
  border-bottom: 1px solid var(--gray-200);
  margin-bottom: 10px;
}

/* ── Organ card ────────────────────────────────────────── */
.organ-card {
  background: white;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius);
  margin-bottom: 10px;
  overflow: hidden;
}
.organ-card-header {
  background: var(--gray-50);
  padding: 9px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-bottom: 1px solid var(--gray-200);
}
.organ-name {
  font-weight: 600;
  font-size: 13px;
  color: var(--gray-900);
  line-height: 1.3;
}
.organ-level-badge {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .4px;
  padding: 2px 7px;
  border-radius: 10px;
  white-space: nowrap;
}
.organ-level-badge.distrital {
  background: var(--psd-blue-light);
  color: var(--psd-blue);
}
.organ-level-badge.seccao {
  background: var(--psd-orange-light);
  color: var(--psd-orange-dark);
}

.organ-stats {
  padding: 8px 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  border-bottom: 1px solid var(--gray-100);
}
.stat-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
}
.stat-label { color: var(--gray-500); }
.stat-value { font-weight: 600; color: var(--gray-900); }
.stat-pct   { color: var(--gray-500); font-weight: 400; margin-left: 2px; }

.organ-lists { padding: 8px 12px 10px; }
.list-row { margin-bottom: 7px; }
.list-row:last-child { margin-bottom: 0; }
.list-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 3px;
  font-size: 12px;
}
.list-desc   { font-weight: 600; color: var(--gray-900); }
.list-values { color: var(--gray-700); }
.list-values .pct { font-weight: 700; margin-left: 4px; }

.progress-track {
  height: 7px;
  background: var(--gray-200);
  border-radius: 4px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  border-radius: 4px;
  transition: width .4s ease;
}

/* List colours (cycle through) */
.list-color-1 { background: var(--psd-blue); }
.list-color-2 { background: var(--psd-orange); }
.list-color-3 { background: #2e7d32; }
.list-color-4 { background: #7b1fa2; }
.list-color-5 { background: #c62828; }

/* ── Accordion (sections) ──────────────────────────────── */
.accordion { border: 1.5px solid var(--gray-200); border-radius: var(--radius); margin-bottom: 8px; overflow: hidden; }

.accordion-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--gray-50);
  cursor: pointer;
  user-select: none;
  transition: background .15s;
  border: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--gray-900);
}
.accordion-header:hover { background: var(--psd-blue-light); }
.accordion-header.open  { background: var(--psd-blue-light); color: var(--psd-blue); }

.accordion-icon {
  font-size: 11px;
  color: var(--gray-500);
  transition: transform .2s;
  flex-shrink: 0;
}
.accordion-header.open .accordion-icon { transform: rotate(90deg); color: var(--psd-blue); }

.accordion-body {
  padding: 10px;
  background: white;
  border-top: 1px solid var(--gray-200);
  display: none;
}
.accordion-body.open { display: block; }

.no-data-msg { font-size: 12px; color: var(--gray-500); font-style: italic; padding: 4px; }

/* ── Spinner ───────────────────────────────────────────── */
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--psd-orange);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
.loading-placeholder { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; height: 200px; color: var(--gray-700); }

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

/* ── Leaflet tooltip ───────────────────────────────────── */
.district-tooltip {
  background: var(--psd-blue-dark);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 600;
  padding: 4px 10px;
  box-shadow: var(--shadow);
}
.district-tooltip::before { display: none; }

/* ── Map district style (Leaflet popup) ─────────────────── */
.leaflet-container { background: #e8eef4; }

/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 700px) {
  .app-main { flex-direction: column; }
  .sidebar { width: 100%; min-width: 0; height: 45vh; border-left: none; border-top: 1px solid var(--gray-200); }
  .map-container { height: 55vh; }
}
