/* =========================================================================
   EvAU Madrid — estilos
   ========================================================================= */

:root {
  --bg:         #f6f7f9;
  --surface:    #ffffff;
  --surface-2:  #f0f2f5;
  --border:     #e3e6ec;
  --text:       #1a1d23;
  --text-soft:  #5a6275;
  --text-faint: #8a92a6;
  --accent:     #b32d2e;
  --accent-soft:#fbe9e9;
  --good:       #1a8a4a;
  --bad:        #c14242;
  --shadow:     0 1px 2px rgba(20, 30, 50, .04), 0 4px 12px rgba(20, 30, 50, .06);
  --radius:     8px;
  --radius-sm:  6px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:         #0e1116;
    --surface:    #161a22;
    --surface-2:  #1f2531;
    --border:     #2a313e;
    --text:       #e8ebf2;
    --text-soft:  #a8b0c2;
    --text-faint: #6f7889;
    --accent:     #ff5e5f;
    --accent-soft:#3b1d1e;
    --good:       #4ec47e;
    --bad:        #ff6b6b;
    --shadow:     0 1px 2px rgba(0, 0, 0, .4), 0 6px 18px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, system-ui, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* ---- Topbar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -.01em;
}
.brand h1::before {
  content: "▮";
  color: var(--accent);
  margin-right: 6px;
}
.meta {
  margin: 2px 0 0;
  color: var(--text-soft);
  font-size: 12px;
}
.theme-hint {
  font-size: 12px;
  color: var(--text-faint);
}

/* ---- Filtros ---- */
.filters {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  align-items: center;
}
.filters input,
.filters select,
.filters button {
  font: inherit;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-sm);
}
.filters input:focus-visible,
.filters select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}

/* Label oculto visualmente pero accesible para lectores de pantalla. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.filters input[type="search"] {
  flex: 1 1 280px;
  min-width: 220px;
}
.filters select { min-width: 130px; cursor: pointer; }

button {
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
  padding: 0 14px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  height: 34px;
}
button:not(.ghost):hover { filter: brightness(1.05); }
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button.ghost {
  background: transparent;
  color: var(--text-soft);
  border: 1px solid var(--border);
}
button.ghost:hover { color: var(--text); border-color: var(--text-faint); }

/* ---- Layout principal ---- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(420px, 38%);
  gap: 16px;
  padding: 16px 20px;
  min-height: 0;
}

/* ---- Tabla ranking ---- */
.ranking {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  box-shadow: var(--shadow);
}
.ranking-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.ranking-head span { font-weight: 600; }
.ranking-head small { color: var(--text-soft); }
.table-wrap {
  flex: 1;
  overflow: auto;
  min-height: 0;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
thead th {
  position: sticky;
  top: 0;
  z-index: 2;          /* tapa los iconos con opacity de las filas que pasan por debajo */
  background: var(--surface);
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: .03em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  /* Refuerzo visual: una sombrita inferior fina para que la cabecera se
     "despegue" del contenido al hacer scroll. */
  box-shadow: 0 1px 0 var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
thead th[data-sort]::after {
  content: "";
  display: inline-block;
  margin-left: 4px;
  border: 4px solid transparent;
  vertical-align: middle;
  opacity: .4;
}
thead th.active.asc::after  { border-bottom-color: var(--text-soft); margin-bottom: 4px; opacity: .9; }
thead th.active.desc::after { border-top-color:    var(--text-soft); margin-top: 4px;    opacity: .9; }
thead th.num { text-align: right; }
thead th.rank { width: 42px; text-align: right; cursor: default; }

tbody tr {
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .12s;
}
tbody tr:hover { background: var(--surface-2); }
tbody tr:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  background: var(--surface-2);
}
tbody tr.selected { background: var(--accent-soft); }
thead th[data-sort]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
tbody td {
  padding: 8px 10px;
  vertical-align: middle;
}
tbody td.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody td.rank { color: var(--text-faint); text-align: right; font-variant-numeric: tabular-nums; }
tbody td.center-name { font-weight: 500; }
tbody td.center-name .center-name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 20px;          /* iguala la altura de los botones */
}
.row-actions {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
tbody td.center-name .center-name-text {
  min-width: 0;
  line-height: 1.35;          /* tighter para que el centro óptico coincida */
}
tbody td.center-name .add-compare {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px; height: 20px;
  border-radius: 4px;
  border: 1px solid var(--border);
  color: var(--text-soft);
  font-size: 14px; font-weight: 700;
  background: var(--surface);
}
tbody td.center-name .add-compare.on {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
tbody td.center-name .add-compare:hover { color: var(--accent); border-color: var(--accent); }
tbody td.center-name .add-compare.on:hover { color: #fff; }

/* Enlace a OpenStreetMap (icono pin como acción de fila). */
.map-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: 20px;
  height: 20px;
  color: var(--accent);
  opacity: .7;
  text-decoration: none;
  border-radius: 4px;
  border: 1px solid transparent;
  line-height: 0;
  transition: opacity .12s, background .12s, border-color .12s;
}
.map-link:hover {
  opacity: 1;
  background: var(--accent-soft);
  border-color: var(--accent);
}
.map-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  opacity: 1;
}
/* En la fila seleccionada el fondo ya es accent-soft → diferenciamos. */
tbody tr.selected .map-link:hover { background: rgba(255, 255, 255, .35); }

/* Variante con texto (panel de detalle): chip más ancha, el texto en color
   normal para que sea legible y el icono mantiene el rojo. */
.map-link-text {
  width: auto;
  height: auto;
  padding: 4px 8px;
  color: var(--text);
  line-height: normal;
  opacity: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.map-link-text:hover { background: var(--accent-soft); border-color: var(--accent); }
.map-link-text svg { color: var(--accent); }
.map-link-text span { font-size: 12px; }

.delta { font-size: 11px; color: var(--text-faint); margin-left: 4px; }
.delta.good { color: var(--good); }
.delta.bad  { color: var(--bad); }

/* Δ de posición en el ranking global */
tbody td.rank-delta-cell {
  text-align: center;
  padding-right: 6px;
  padding-left: 6px;
}
.rank-delta {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 20px;
  min-width: 36px;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
  white-space: nowrap;
  padding: 0 6px;
  border-radius: 4px;
  background: var(--surface-2);
  border: 1px solid transparent;
  vertical-align: middle;
}
.rank-delta.up   { color: var(--good); background: color-mix(in srgb, var(--good) 14%, var(--surface-2)); }
.rank-delta.down { color: var(--bad);  background: color-mix(in srgb, var(--bad)  14%, var(--surface-2)); }
.rank-delta.same { color: var(--text-faint); }
.rank-delta.new  { color: var(--accent); background: var(--accent-soft); }
.rank-delta.none { color: var(--text-faint); background: transparent; font-weight: 400; }

.empty-row td {
  text-align: center;
  padding: 40px 10px;
  color: var(--text-faint);
}

/* ---- Detalle ---- */
.detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  overflow: auto;
  box-shadow: var(--shadow);
  min-height: 380px;
  position: relative;
}
.detail-close {
  display: none;  /* sólo visible en móvil */
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
  padding: 0;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 22px;
  line-height: 1;
  z-index: 1;
  /* Sombra suave para despegarlo del contenido (gráfico, tabla...) */
  box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
}
.detail-close:hover { background: var(--accent-soft); border-color: var(--accent); }
.detail-backdrop {
  display: none;  /* sólo visible en móvil */
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 22, .55);
  backdrop-filter: blur(2px);
  z-index: 55;
}
.detail-empty {
  color: var(--text-soft);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
  text-align: center;
}
.detail-empty p { max-width: 32ch; line-height: 1.5; }
.detail-empty .hint { font-size: 12px; color: var(--text-faint); margin-top: 4px; }

.detail h2 {
  margin: 0 0 4px;
  font-size: 16px;
  letter-spacing: -.01em;
}
.detail .center-meta {
  font-size: 12px;
  color: var(--text-soft);
  margin-bottom: 14px;
  display: flex; flex-wrap: wrap; gap: 4px 12px;
}
.detail .center-meta strong { color: var(--text); font-weight: 500; }
.metric-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  background: var(--surface-2);
  width: fit-content;
}
.metric-tabs button {
  background: transparent;
  color: var(--text-soft);
  border: none;
  padding: 4px 10px;
  height: 26px;
  font-size: 12px;
  border-radius: 4px;
  font-weight: 500;
}
.metric-tabs button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.chart-host {
  position: relative;
  height: 260px;
  margin-bottom: 16px;
}

.detail-table-wrap {
  margin-top: 8px;
  /* Scroll horizontal interno si las 6 columnas no caben en el panel
     (típicamente en móvil) — los headers no se cortan. */
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.detail table {
  font-size: 12px;
  margin-top: 0;
  width: 100%;
}
.detail table thead th {
  position: static;
  text-transform: none;
  letter-spacing: 0;
  cursor: default;
  font-size: 11px;
  white-space: nowrap;
  vertical-align: bottom;
  box-shadow: none;        /* anula la sombra inferior de la cabecera del ranking */
}
.detail table thead th, .detail table tbody td { padding: 6px 8px; }

.add-compare-cta {
  margin-top: 8px;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  font-size: 12px;
  height: 30px;
  padding: 0 10px;
}
.add-compare-cta.on {
  background: var(--accent);
  color: #fff;
}

/* ---- Compare bar (footer flotante) ---- */
.compare-bar {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
  display: flex;
  gap: 8px;
  align-items: center;
  z-index: 50;
  font-size: 13px;
}
.compare-bar.hidden { display: none; }
.compare-bar button { height: 30px; padding: 0 12px; font-size: 12px; }

/* ---- Compare dialog ---- */
dialog.compare-dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(90vw, 920px);
  max-height: 86vh;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(10, 14, 22, .55); backdrop-filter: blur(2px); }
dialog header {
  display: flex; justify-content: space-between; align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
dialog header h2 {
  margin: 0;
  font-size: 16px;
  /* Si el título es largo, que no empuje al botón fuera de pantalla. */
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
dialog header button {
  flex-shrink: 0;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 22px;
  line-height: 1;
  width: 32px; height: 32px; padding: 0;
  border-radius: 50%;
}
dialog header button:hover { background: var(--accent-soft); border-color: var(--accent); }
.compare-dialog .metric-tabs { margin: 14px 16px 8px; }
.compare-dialog .chart-host { height: 480px; padding: 0 16px 16px; }

/* Fallback si Chart.js no carga (CDN caído / sin red). */
.chart-fallback {
  margin: 0;
  padding: 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-soft);
  font-size: 13px;
  text-align: center;
}

/* =========================================================================
   RESPONSIVE
   - 1024px : panel detalle a una columna (debajo de la tabla, no encima)
   - 720px  : ocultar columnas secundarias, detalle como bottom-sheet,
              filtros apilados, compare-bar/dialog adaptados
   ========================================================================= */

@media (max-width: 1024px) {
  .layout {
    /* `minmax(0, 1fr)` en lugar de `1fr` para que la columna NO se ensanche
       hasta acomodar la tabla (que en móvil puede ser más ancha que el
       viewport). Sin esto, la página gana scroll horizontal y position:fixed
       se calcula sobre el viewport extendido — el botón × del bottom sheet
       queda fuera de pantalla. */
    grid-template-columns: minmax(0, 1fr);
    /* Sin grid-template-rows, las dos filas del grid son `auto` y el ranking
       se expande para mostrar todas las filas, sin scroll interno y con la
       página creciendo sin límite. */
    grid-template-rows: minmax(320px, 1fr) auto;
    padding: 12px;
    gap: 12px;
  }
  /* Defensivo: los grid items necesitan poder encogerse por debajo del tamaño
     de su contenido para que la tabla scrolee dentro de .table-wrap en lugar
     de empujar al padre. */
  .ranking, .detail { min-width: 0; }
  .topbar { padding: 12px 14px; }
  .filters { padding: 10px 14px; }
  /* En tablet ya no necesitamos min-height grande del detalle (no hay 2 cols
     que igualar); deja que se ajuste al contenido. */
  .detail { min-height: 0; }
}

@media (max-width: 720px) {
  body { font-size: 13px; }
  .topbar { padding: 10px 12px; }
  .brand h1 { font-size: 16px; }
  .theme-hint { display: none; }

  /* Filtros: cada control ocupa toda la fila si es necesario. */
  .filters {
    padding: 8px 10px;
    gap: 6px;
  }
  .filters input[type="search"] { flex: 1 1 100%; min-width: 0; }
  .filters select { flex: 1 1 calc(50% - 3px); min-width: 0; }
  .filters #reset { flex: 1 1 100%; }

  /* Layout de columna única. La tabla manda. */
  .layout { padding: 8px; gap: 8px; }

  /* Ocultamos columnas que también aparecen en el detalle del centro:
     Municipio (4), Área (5), Titularidad (6).
     Pos | Δ | Centro | Métrica | vs CM siguen visibles. */
  thead th:nth-child(4),
  thead th:nth-child(5),
  thead th:nth-child(6),
  tbody td:nth-child(4),
  tbody td:nth-child(5),
  tbody td:nth-child(6) {
    display: none;
  }
  thead th, tbody td { padding: 8px 4px; }

  /* Forzamos table-layout fijo para que la columna CENTRO absorba el sobrante
     y los nombres muy largos rompan dentro del ancho disponible (en lugar de
     hacer crecer la tabla más allá del viewport). */
  #table { table-layout: fixed; width: 100%; }
  thead th:nth-child(1), tbody td:nth-child(1) { width: 34px; }   /* Pos. */
  thead th:nth-child(2), tbody td:nth-child(2) { width: 56px; }   /* Δ */
  /* nth-child(3) = Centro → absorbe el resto, no le ponemos width */
  thead th:nth-child(7), tbody td:nth-child(7) { width: 64px; }   /* Métrica */
  thead th:nth-child(8), tbody td:nth-child(8) { width: 78px; }   /* vs CM */

  /* Los headers "Nota media aptos", "Nº presentados"... no caben en una sola
     línea con anchos pequeños — permitimos que rompan, los centramos
     verticalmente y quitamos uppercase + letter-spacing para hacerlos más
     compactos. */
  thead th {
    white-space: normal;
    line-height: 1.2;
    vertical-align: middle;
    text-transform: none;
    letter-spacing: 0;
    font-size: 12px;
    font-weight: 600;
  }

  /* Permitimos romper dentro de palabras (las "CPR INF-PRI-SEC..." son más
     largas que cualquier columna razonable a este ancho). */
  tbody td.center-name .center-name-text {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* El panel de detalle pasa a ser un bottom-sheet deslizable. */
  .detail {
    position: fixed;
    inset: auto 0 0 0;
    height: 85vh;
    margin: 0;
    border: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    transform: translateY(105%);
    transition: transform .25s ease-out;
    z-index: 60;
    min-height: 0;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, .25);
  }
  .detail.open { transform: translateY(0); }
  .detail-empty { display: none; }
  .detail-close { display: block; }
  .detail-backdrop:not([hidden]) { display: block; }

  /* El gráfico del detalle más bajo para que quepa en pantallas cortas. */
  .chart-host { height: 220px; }

  /* Tabla del detalle: compacta y con scroll horizontal interno. Los headers
     mantienen white-space: nowrap para que no se rompan a media palabra. */
  .detail-table-wrap {
    /* Sangrado al borde del panel: aprovecha todo el ancho disponible. */
    margin-left: -16px;
    margin-right: -16px;
    padding-left: 16px;
    padding-right: 16px;
  }
  .detail table { font-size: 11px; }
  .detail table thead th,
  .detail table tbody td { padding: 6px 6px; }

  /* Tabs de métrica del detalle: con 4 botones tipo "Nota media aptos" /
     "Nº presentados" no hay forma de que quepan en una pantalla de 360 px
     sin romper feo. Solución: el contenedor ocupa exactamente el ancho del
     panel y los tabs scrollean horizontalmente dentro. `width: 100%` (no
     max-width) anula el `width: fit-content` del rule base, que con flex y
     flex-shrink:0 hacía crecer al contenedor con sus hijos. */
  .detail-metric-tabs {
    width: 100%;
    box-sizing: border-box;
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
  }
  .detail-metric-tabs button {
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* Compare-bar más compacta: si no cabe en una línea, los botones envuelven
     debajo del texto en lugar de salirse. */
  .compare-bar {
    bottom: 12px;
    padding: 8px 10px;
    font-size: 12px;
    max-width: calc(100vw - 16px);
    flex-wrap: wrap;
    justify-content: center;
    border-radius: 12px;
  }
  .compare-bar button { height: 30px; font-size: 12px; padding: 0 12px; }

  /* Compare dialog ocupa toda la pantalla. */
  dialog.compare-dialog {
    width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    border: none;
  }
  .compare-dialog .chart-host { height: calc(100vh - 180px); padding: 0 8px 8px; }
  .compare-dialog .metric-tabs {
    margin: 10px 8px 4px;
    overflow-x: auto;
    flex-wrap: nowrap;
    width: calc(100% - 16px);
  }
  .compare-dialog .metric-tabs button { white-space: nowrap; }

  .ranking-head { padding: 8px 12px; font-size: 13px; }
}

@media (max-width: 1024px) and (min-width: 721px) {
  /* En tablet, el panel está debajo (no es bottom-sheet) y siempre visible. */
  .detail { order: 1; }
}

/* En móviles estrechos ocultamos también vs CM. */
@media (max-width: 480px) {
  thead th:nth-child(8),
  tbody td:nth-child(8) { display: none; }
}
