/* ==========================================================================
   OBSERVATORIO DEMOSCÓPICO ELECTORAL — ESTILOS GLOBALES
   Inspirado en los modelos de FiveThirtyEight, The Economist y Kiko Llaneras (El País)
   ========================================================================== */

:root {
  --bg-body: #0a0e17;
  --bg-card: #111827;
  --bg-card-elevated: #1a2333;
  --bg-hover: #223047;
  --border-subtle: #1f293d;
  --border-strong: #2e3c54;
  --text-main: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent-primary: #38bdf8;
  --accent-indigo: #818cf8;
  --accent-emerald: #10b981;
  --accent-amber: #f59e0b;
  --accent-rose: #f43f5e;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', Consolas, monospace;
}

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

body {
  background-color: var(--bg-body);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* TOPBAR LIMPIA */
.topbar {
  background: rgba(17, 24, 39, 0.96);
  border-bottom: 1px solid var(--border-strong);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar-inner {
  max-width: 1440px;
  margin: 0 auto;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-badge {
  width: 38px;
  height: 38px;
  border-radius: 9px;
  background: linear-gradient(135deg, #1d4ed8, #0ea5e9);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 15px;
  color: #fff;
  box-shadow: 0 4px 12px rgba(14, 165, 233, 0.3);
}

.brand-title {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.3px;
  color: #f8fafc;
}

.brand-subtitle {
  font-size: 12px;
  color: var(--text-secondary);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-pill {
  padding: 5px 11px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-pill .dot-live {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #10b981;
  box-shadow: 0 0 8px #10b981;
}

/* NAVEGACIÓN PRINCIPAL */
.subnav {
  background: #0d131f;
  border-bottom: 1px solid var(--border-subtle);
}

.subnav-inner {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  gap: 4px;
  overflow-x: auto;
}

.nav-tab {
  background: transparent;
  color: var(--text-secondary);
  border: none;
  border-bottom: 3px solid transparent;
  padding: 13px 16px;
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.nav-tab:hover {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.02);
}

.nav-tab.active {
  color: var(--accent-primary);
  border-bottom-color: var(--accent-primary);
  background: rgba(56, 189, 248, 0.05);
}

.tab-badge {
  background: rgba(245, 158, 11, 0.2);
  color: #fbbf24;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
}

/* CONTENEDOR PRINCIPAL */
.main-container {
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 24px 48px;
  flex: 1;
}

/* TOOLTIPS / BOTONES DE INFORMACIÓN DIVULGATIVA */
.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  font-family: var(--font-mono);
  cursor: pointer;
  margin-left: 6px;
  transition: all 0.15s ease;
  vertical-align: middle;
}

.info-btn:hover {
  color: #fff;
  border-color: var(--accent-primary);
  background: rgba(56, 189, 248, 0.2);
}

/* POP-UP MODAL EXPLICATIVO */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  opacity: 0;
  visibility: hidden;
  transition: all 0.2s ease;
}

.modal-overlay.open {
  opacity: 1;
  visibility: visible;
}

.modal-dialog {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  max-width: 700px;
  max-height: 85vh;
  overflow-y: auto;
  width: 100%;
  padding: 26px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
  transform: translateY(12px);
  transition: transform 0.2s ease;
}

.modal-body details {
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 12px 16px;
  margin-top: 14px;
}

.modal-body .katex {
  font-size: 1.08em;
  color: #f1f5f9;
}

.modal-body .katex-display {
  margin: 10px 0;
}

.modal-body summary {
  font-weight: 700;
  color: var(--accent-primary);
  cursor: pointer;
  outline: none;
  font-size: 13px;
}

.modal-body summary:hover {
  color: #7dd3fc;
}

.meta-link {
  color: var(--accent-primary);
  text-decoration: underline;
  text-underline-offset: 3px;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s ease;
}

.meta-link:hover {
  color: #7dd3fc;
}

.modal-overlay.open .modal-dialog {
  transform: translateY(0);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 14px;
}

.modal-title {
  font-size: 17px;
  font-weight: 800;
  color: #fff;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}

.modal-close:hover {
  color: #fff;
}

.modal-body {
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--text-secondary);
}

.modal-body p {
  margin-bottom: 10px;
}

.modal-body strong {
  color: var(--text-main);
}

.modal-footer {
  margin-top: 20px;
  text-align: right;
}

/* TARJETAS KPI DE PORTADA */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 22px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 16px;
  position: relative;
}

.kpi-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--text-secondary);
  text-transform: uppercase;
  display: flex;
  align-items: center;
}

.kpi-value {
  font-size: 20px;
  font-weight: 800;
  font-family: var(--font-mono);
  color: var(--text-main);
  margin: 4px 0 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#kpi-chances {
  font-size: 17px;
  letter-spacing: -0.4px;
}

.kpi-sub {
  font-size: 12px;
  color: var(--text-muted);
}

/* TABS Y PANELES */
.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

.grid-2col {
  display: grid;
  grid-template-columns: 1.05fr 0.95fr;
  gap: 20px;
}

.panel-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.panel-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
}

.panel-subtitle {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 3px;
}

/* HEMICICLO */
.hemiciclo-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 4px 0;
}

#hemiciclo-svg {
  width: 100%;
  max-width: 600px;
  height: auto;
}

.hemiciclo-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border-subtle);
}

.legend-chip,
.hemiciclo-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 4px 9px;
  font-size: 12px;
  transition: all 0.15s ease;
}

.legend-chip:hover,
.hemiciclo-legend-item:hover {
  border-color: var(--text-secondary);
  background: var(--bg-hover);
}

.legend-dot,
.legend-color-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.4);
}

/* PACTÓMETRO */
.pact-meter-box {
  background: #0c121d;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 14px;
}

.pact-meter-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-wrap: wrap;
  gap: 8px;
}

.pact-sum-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
}

.pact-sum-number {
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 800;
  margin-left: 6px;
  color: #fff;
}

.pact-status-pill {
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 6px;
  background: rgba(244, 63, 94, 0.16);
  color: #fb7185;
  border: 1px solid rgba(244, 63, 94, 0.35);
  transition: all 0.2s ease;
}

.pact-status-pill.ok,
.pact-status-pill.majority-reached {
  background: rgba(16, 185, 129, 0.2) !important;
  color: #34d399 !important;
  border-color: rgba(16, 185, 129, 0.5) !important;
  box-shadow: 0 0 10px rgba(16, 185, 129, 0.2);
}

.pact-bar-track {
  position: relative;
  height: 26px;
  background: #1a2333;
  border-radius: 6px;
  overflow: hidden;
  display: flex;
}

.pact-bar-segments {
  display: flex;
  width: 100%;
  height: 100%;
}

.pact-bar-segment,
.pact-segment {
  height: 100%;
  transition: width 0.25s ease;
}

.pact-majority-line {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 2px;
  background: #fbbf24;
  z-index: 5;
  box-shadow: 0 0 6px #fbbf24;
}

.pact-party-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.pact-chip,
.party-toggle-btn {
  background: var(--bg-card-elevated);
  border: 1.5px solid var(--border-strong);
  color: var(--text-main);
  border-radius: 8px;
  padding: 6px 11px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  transition: all 0.15s ease;
  user-select: none;
}

.pact-chip:hover,
.party-toggle-btn:hover {
  border-color: var(--text-secondary);
  background: var(--bg-hover);
  transform: translateY(-1px);
}

.pact-chip.selected,
.party-toggle-btn.selected {
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.pact-chip-dot,
.party-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}

.pact-chip-seats {
  font-family: var(--font-mono);
  font-size: 11.5px;
  font-weight: 700;
  color: var(--text-secondary);
}

.pact-chip.selected .pact-chip-seats {
  color: #fff;
}

.presets-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.preset-row {
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.preset-row:hover {
  border-color: var(--accent-primary);
  background: var(--bg-hover);
}

.preset-info-title {
  font-weight: 700;
  font-size: 13px;
  color: #f8fafc;
}

.preset-info-sub {
  font-size: 11px;
  color: var(--text-secondary);
}

.preset-prob-badge {
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 5px;
  min-width: 60px;
  text-align: center;
}

.prob-high { background: rgba(16, 185, 129, 0.18); color: #34d399; }
.prob-mid { background: rgba(245, 158, 11, 0.18); color: #fbbf24; }
.prob-low { background: rgba(148, 163, 184, 0.14); color: #94a3b8; }

/* TABLAS LIMPIAS */
.table-responsive {
  width: 100%;
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  text-align: left;
  padding: 10px 12px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  background: #0d131f;
  border-bottom: 1px solid var(--border-strong);
  white-space: nowrap;
}

.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: middle;
}

.data-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.02);
}

.data-table th.num,
.data-table td.num {
  text-align: right;
  font-family: var(--font-mono);
}

.data-table th.center,
.data-table td.center {
  text-align: center;
}

.party-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
}

.party-color-bar {
  width: 4px;
  height: 20px;
  border-radius: 2px;
  flex-shrink: 0;
}

.delta-pos { color: #34d399; font-weight: 700; }
.delta-neg { color: #fb7185; font-weight: 700; }
.delta-zero { color: var(--text-muted); }

/* PESTAÑA MAPA TERRITORIAL (ESTILO NOCHE ELECTORAL) */
.map-layout {
  display: grid;
  grid-template-columns: 1.15fr 0.85fr;
  gap: 20px;
  align-items: start;
  position: relative;
}

.map-card-wrapper {
  background: #0c121d;
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

#spain-map-svg {
  width: 100%;
  max-width: 720px;
  height: auto;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.4));
}

.map-region-path {
  cursor: pointer;
  stroke: #0a0e17;
  stroke-width: 0.7;
  transition: all 0.15s ease;
}

.map-region-path:hover {
  filter: brightness(1.25);
  stroke: #ffffff;
  stroke-width: 1.5;
}

.map-region-path.active {
  filter: brightness(1.35);
  stroke: var(--accent-primary);
  stroke-width: 2.0;
}

.plaza-marker {
  cursor: pointer;
  stroke: #ffffff;
  stroke-width: 1;
  transition: all 0.15s ease;
}

.plaza-marker:hover {
  filter: brightness(1.3);
  stroke: var(--accent-primary);
  stroke-width: 1.8;
}

.plaza-marker.active {
  stroke: var(--accent-primary);
  stroke-width: 2.2;
}

/* FLOATING TOOLTIP PARA EL MAPA */
.map-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(13, 19, 31, 0.95);
  border: 1px solid var(--accent-primary);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 12px;
  line-height: 1.5;
  color: #f8fafc;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.7);
  z-index: 200;
  display: none;
  backdrop-filter: blur(8px);
  min-width: 170px;
}

.map-tooltip-line {
  margin-bottom: 2px;
}

.map-tooltip-line strong {
  color: #fff;
}

/* PANEL LATERAL DE LA PROVINCIA SELECCIONADA */
.prov-inspector-card {
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  border-radius: 12px;
  padding: 20px;
}

.prov-inspector-header {
  border-bottom: 1px solid var(--border-subtle);
  padding-bottom: 12px;
  margin-bottom: 14px;
}

.twin-charts-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 12px 0;
}

.twin-chart-box {
  background: #0c121d;
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.chart-box-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-bottom: 6px;
  text-align: center;
}

#prov-pie-svg, #prov-arc-svg {
  width: 100%;
  max-width: 160px;
  height: 125px;
}

/* PESTAÑA BAILE DE ESCAÑOS */
.battle-filter-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.filter-btn {
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text-secondary);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.filter-btn.active {
  background: var(--accent-primary);
  color: #0a0e17;
  font-weight: 700;
  border-color: var(--accent-primary);
}

/* PESTAÑA HISTÓRICA / COMPARADOR DE VALIDACIÓN */
.hist-selector-box {
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.election-select {
  background: var(--bg-card);
  color: #fff;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  outline: none;
}

.election-select:focus {
  border-color: var(--accent-primary);
}

.grouped-bar-chart-wrapper {
  background: #0c121d;
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 20px;
}

#hist-bars-svg,
#baile-bars-svg,
#llaneras-chart-svg,
#weights-lollipop-svg,
#bias-bars-svg,
#timeline-svg {
  width: 100%;
  height: auto;
  display: block;
}

/* HEMEROTECA DE ENCUESTAS */
.polls-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: 14px;
}

.poll-card {
  background: var(--bg-card-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 16px;
  transition: all 0.15s ease;
}

.poll-card:hover {
  border-color: var(--accent-primary);
}

.poll-card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 8px;
}

.poll-card-title {
  font-size: 14px;
  font-weight: 700;
  color: #fff;
}

.poll-card-meta {
  font-size: 12px;
  color: var(--text-secondary);
}

.poll-compare-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  font-size: 12px;
}

/* FOOTER */
.footer {
  border-top: 1px solid var(--border-subtle);
  background: #080b12;
  padding: 16px 24px;
  font-size: 12px;
  color: var(--text-muted);
}

.footer-inner {
  max-width: 1440px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

/* RESPONSIVE */
@media (max-width: 1100px) {
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
  .grid-2col, .map-layout { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .kpi-row { grid-template-columns: 1fr; }
  .polls-grid { grid-template-columns: 1fr; }
}
