/* ── evolucio.css ─────────────────────────────────────────────────────────────
   Estils per a la vista evolutiva d'estadístiques (heatmap, sparklines,
   selecció múltiple, cicle de vida). Tokens compatibles amb tokens.css.

   La taula usa les classes `results-table estadistiques-table evolucio-table`
   i hereta tots els estils globals de `results.css` (sticky `thead th`,
   `sortable-th-inner`, `.sort-btn`, zebra, etc.) i de `estadistiques.css`.
   Aquí només definim tokens cromàtics i ajustos específics de la vista.
   ──────────────────────────────────────────────────────────────────────────── */

/* ── Tokens específics de la vista evolutiva ───────────────────────────────── */
:root {
  --trend-up: #15803d;
  --trend-flat: #94a3b8;
  --trend-down: #b91c1c;

  --life-active: #276749;
  --life-pre: #4a7fd4;
  --life-post: #94a3b8;

  --heat-0: transparent;
  --heat-1: rgba(39, 103, 73, 0.02);
  --heat-2: rgba(39, 103, 73, 0.05);
  --heat-3: rgba(39, 103, 73, 0.09);
  --heat-4: rgba(39, 103, 73, 0.14);
  --heat-5: rgba(39, 103, 73, 0.20);
}

/* ── Shell de la vista ─────────────────────────────────────────────────────── */

#section-estadistiques.section-estadistiques--evolucio.main {
  max-width: 100%;
  margin-left: 0;
  margin-right: 0;
}

#section-estadistiques .evolucio-results-header {
  flex-direction: column;
  align-items: stretch;
  align-content: stretch;
  gap: 0;
}

#section-estadistiques .evolucio-results-header__top {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  width: 100%;
  min-width: 0;
}

#section-estadistiques .evolucio-results-header .evolucio-legend {
  width: 100%;
  box-sizing: border-box;
  margin: 0;
}

#section-estadistiques .evolucio-results-header #evolucioSelBarMount {
  width: 100%;
  box-sizing: border-box;
}

/* ── Llegenda ──────────────────────────────────────────────────────────────── */

.evolucio-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  padding: 8px 18px;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border-subtle, var(--line));
}

.evolucio-legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.evolucio-legend-swatch {
  width: 14px;
  height: 8px;
  border-radius: 2px;
  display: inline-block;
}

.evolucio-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

/* ── Barra de selecció ─────────────────────────────────────────────────────── */

.evolucio-selection-bar-wrap {
  padding: 0 18px;
}

.evolucio-selection-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  margin-bottom: 0;
  background: linear-gradient(
    to right,
    rgba(var(--color-primary-rgb), 0.07),
    rgba(var(--color-primary-rgb), 0.02)
  );
  border: 1px solid rgba(var(--color-primary-rgb), 0.18);
  border-radius: 10px;
  flex-wrap: wrap;
  transition: opacity 0.2s, max-height 0.2s;
}

.evolucio-selection-bar[hidden] {
  display: none;
}

.evolucio-sel-count {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-primary);
  white-space: nowrap;
}

.evolucio-sel-count strong {
  font-size: 16px;
  font-feature-settings: 'tnum';
}

.evolucio-sel-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border: 1px solid var(--line-strong, rgba(31, 42, 55, 0.18));
  background: var(--card);
  border-radius: 6px;
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  color: var(--fg, var(--ink));
  transition: all 0.15s;
}

.evolucio-sel-toggle:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.evolucio-sel-toggle.active {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}

.evolucio-sel-btn {
  padding: 5px 10px;
  border: 1px solid var(--line-strong, rgba(31, 42, 55, 0.18));
  background: var(--card);
  border-radius: 6px;
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  color: var(--muted);
  transition: all 0.15s;
}

.evolucio-sel-btn:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}

.evolucio-sel-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  flex: 1;
  min-width: 0;
}

.evolucio-sel-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 4px 3px 10px;
  background: var(--card);
  border: 1px solid rgba(var(--color-primary-rgb), 0.25);
  border-radius: 14px;
  font-size: 12px;
  color: var(--fg, var(--ink));
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.evolucio-sel-chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: none;
  background: rgba(31, 42, 55, 0.08);
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  line-height: 1;
  padding: 0;
  flex-shrink: 0;
}

.evolucio-sel-chip-remove:hover {
  background: var(--trend-down);
  color: #fff;
}

/* ── Taula evolutiva — ajustos específics (el gruix ve de results.css) ────── */

/* Columna de selecció / desselecció */
table.evolucio-table th.evo-col-check,
table.evolucio-table td.evo-col-check {
  width: 32px;
  padding: 6px 4px 6px 10px;
  text-align: center;
}

table.evolucio-table th.evo-col-check {
  cursor: default;
}

table.evolucio-table input.evo-cobla-check {
  width: 15px;
  height: 15px;
  cursor: pointer;
  accent-color: var(--color-primary);
  margin: 0;
  vertical-align: middle;
}

table.evolucio-table th.evo-col-check .evo-deselect-visible-btn {
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--font-xl);
  line-height: 1;
  font-weight: 800;
}

/* Nom de cobla */
table.evolucio-table th.evo-col-cobla {
  padding-left: 14px;
}

/* Columnes any (heatmap): amplada fixa per mantenir alineació de la taula */
table.evolucio-table th.evo-year,
table.evolucio-table td.evo-year {
  text-align: right;
  width: 44px;
  min-width: 44px;
  max-width: 44px;
  font-feature-settings: 'tnum';
  font-size: 12px;
  padding-left: 2px;
  padding-right: 5px;
}

table.evolucio-table th.evo-year.is-focal-header {
  background: rgba(var(--color-primary-rgb), 0.10);
  color: var(--color-primary);
}

/* Parcial: text inferior petit a la capçalera de l'any */
table.evolucio-table th.evo-year .evo-year-partial {
  display: block;
  font-size: 9px;
  font-weight: 400;
  color: var(--muted);
  letter-spacing: 0;
  text-transform: none;
  line-height: 1.1;
  margin-top: 1px;
}

table.evolucio-table th.evo-col-spark {
  width: 110px;
  text-align: center;
}

table.evolucio-table th.evo-col-spark-life {
  width: 130px;
  text-align: center;
}

table.evolucio-table th.evo-col-total {
  width: 80px;
  text-align: right;
  padding-right: 14px;
}

table.evolucio-table th.evo-col-range-total {
  width: 80px;
  text-align: right;
  padding-right: 8px;
}

table.evolucio-table td.evo-col-range-total {
  text-align: right;
  padding-right: 8px;
  font-weight: 500;
  color: var(--fg, var(--ink));
  font-feature-settings: 'tnum';
}

table.evolucio-table th.evo-col-delta {
  width: 70px;
  text-align: right;
}

/* Creació / dissolució: alçada sobre 4 xifres; capçalera amb &shy; i trencament manual */
table.evolucio-table th.evo-col-life-year,
table.evolucio-table td.evo-col-life-year {
  width: 4.5ch;
  min-width: 4.25ch;
  max-width: 6rem;
  text-align: right;
  padding-right: 8px;
  font-feature-settings: 'tnum';
  font-size: 12px;
}

table.evolucio-table th.evo-col-life-year {
  white-space: normal;
  line-height: 1.15;
  hyphens: manual;
  word-break: normal;
  overflow-wrap: anywhere;
}

table.evolucio-table td.evo-col-life-year {
  color: var(--fg, var(--ink));
  font-weight: 500;
}

table.evolucio-table td.evo-col-life-year.is-current {
  color: var(--color-primary);
  font-weight: 600;
}

table.evolucio-table td.evo-col-life-year.is-old {
  color: var(--muted);
}

/* Població i comarca: sense amplada forçada; el redimensionament de columna ho ajusta */
table.evolucio-table th.evo-col-town,
table.evolucio-table td.evo-col-town,
table.evolucio-table th.evo-col-county,
table.evolucio-table td.evo-col-county {
  font-size: 12px;
  min-width: 0;
  max-width: none;
  overflow-wrap: anywhere;
  word-break: break-word;
}

table.evolucio-table td.evo-col-county {
  color: var(--muted);
}

/* Cel·la nom */
td.evo-col-cobla {
  padding-left: 14px;
  font-weight: 500;
  color: var(--fg, var(--ink));
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
  position: relative;
}

td.evo-col-cobla .evo-life-marker {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}

td.evo-col-cobla .evo-life-marker.active {
  background: var(--life-active);
}

td.evo-col-cobla .evo-life-marker.pre {
  background: var(--life-pre);
}

td.evo-col-cobla .evo-life-marker.post {
  background: var(--life-post);
}

/* Cel·les any (heatmap) */
td.evo-year {
  text-align: center;
  font-size: 12px;
  color: var(--fg, var(--ink));
  position: relative;
}

td.evo-year.heat-0 { color: #cbd5e1; }
td.evo-year.heat-1 { background: var(--heat-1); }
td.evo-year.heat-2 { background: var(--heat-2); }
td.evo-year.heat-3 { background: var(--heat-3); }
td.evo-year.heat-4 { background: var(--heat-4); font-weight: 500; }
td.evo-year.heat-5 { background: var(--heat-5); font-weight: 600; }

td.evo-year.life-pre { color: var(--life-pre); opacity: 0.6; }
td.evo-year.life-post { color: var(--life-post); }

/* Sparkline */
td.evo-col-spark { text-align: center; padding: 4px 6px; }
td.evo-col-spark svg { display: inline-block; vertical-align: middle; }

/* Sparkline vida sencera */
td.evo-col-spark-life { text-align: center; padding: 4px 6px; position: relative; }
td.evo-col-spark-life svg { display: inline-block; vertical-align: middle; }
td.evo-col-spark-life .life-range {
  display: block;
  font-size: 10px;
  color: var(--muted);
  font-feature-settings: 'tnum';
  margin-top: 1px;
  line-height: 1;
}

/* Total i delta */
td.evo-col-total { text-align: right; padding-right: 14px; font-weight: 600; color: var(--fg, var(--ink)); }
td.evo-col-delta { text-align: right; font-weight: 600; font-size: 12px; }
td.evo-col-delta.up { color: var(--trend-up); }
td.evo-col-delta.down { color: var(--trend-down); }
td.evo-col-delta.flat { color: var(--muted); }
td.evo-col-delta .arrow { margin-right: 1px; font-size: 10px; }

/* Fila seleccionada */
table.evolucio-table tbody tr.evo-is-selected { background: rgba(var(--color-primary-rgb), 0.10) !important; }
table.evolucio-table tbody tr.evo-is-selected:hover { background: rgba(var(--color-primary-rgb), 0.15) !important; }
table.evolucio-table tbody tr.evo-is-selected td.evo-col-cobla { border-left: 3px solid var(--color-primary); padding-left: 11px; }

/* Fila sticky resum */
table.evolucio-table tfoot { position: sticky; bottom: 0; z-index: 2; }
table.evolucio-table tfoot tr { background: #ffffff; font-weight: 600; box-shadow: 0 -1px 0 rgba(var(--color-primary-rgb), 0.3); }
table.evolucio-table tfoot td { border-bottom: none; border-top: 2px solid var(--color-primary); padding: 8px 6px; color: var(--fg, var(--ink)); }
table.evolucio-table tfoot td.evo-col-cobla { padding-left: 14px; font-weight: 700; color: var(--color-primary); }
table.evolucio-table tfoot td.evo-year { font-weight: 600; }
table.evolucio-table tfoot td.evo-col-total { font-weight: 700; }

/* ── Tooltip sparkline ─────────────────────────────────────────────────────── */

.evolucio-tooltip {
  position: fixed;
  background: var(--fg, #111827);
  color: #fff;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  pointer-events: none;
  z-index: 100;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transition: opacity 0.1s;
}

.evolucio-tooltip.visible { opacity: 1; }
.evolucio-tooltip .yr { font-weight: 600; color: var(--color-accent, #52c4a8); margin-right: 6px; }

/* ── Sidebar: rang temporal (dos inputs en una línia) ──────────────────────── */

.evolucio-range-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.evolucio-range-input-wrap {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
}

.evolucio-range-input-wrap > .input {
  width: 100%;
  padding-right: 32px;
}

.evolucio-range-input-wrap > .input-clear {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
}

.evolucio-range-sep {
  color: var(--muted);
  font-weight: 600;
  flex-shrink: 0;
}

.sidebar .evolucio-range-input-wrap > .input:hover:not(:focus) {
  background-color: var(--filter-option-hover-bg);
}

/* ── Sidebar: slider de navegació anual ────────────────────────────────────── */

.evolucio-range-slider-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.evolucio-range-slider-row input[type='range'] {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--line-strong, rgba(31, 42, 55, 0.18));
  border-radius: 2px;
  outline: none;
}

.evolucio-range-slider-row input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  background: var(--color-primary);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
  transition: transform 0.1s;
}

.evolucio-range-slider-row input[type='range']::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

.evolucio-range-slider-row input[type='range']::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--color-primary);
  border-radius: 50%;
  cursor: pointer;
  border: none;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.evolucio-year-step {
  width: 26px;
  height: 26px;
  border: 1px solid var(--line-strong, rgba(31, 42, 55, 0.18));
  background: var(--card);
  color: var(--fg, var(--ink));
  border-radius: 6px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: all 0.1s;
  user-select: none;
}

.evolucio-year-step:hover:not(:disabled) {
  border-color: var(--color-primary);
  color: var(--color-primary);
  background: rgba(var(--color-primary-rgb), 0.05);
}

.evolucio-year-step:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  color: var(--muted);
}

/* ── Responsive ────────────────────────────────────────────────────────────── */

@media (max-width: 900px) {
  table.evolucio-table th.evo-col-county,
  table.evolucio-table td.evo-col-county {
    display: none;
  }
}

@media (max-width: 700px) {
  table.evolucio-table th.evo-col-town,
  table.evolucio-table td.evo-col-town,
  table.evolucio-table th.evo-col-life-year,
  table.evolucio-table td.evo-col-life-year {
    display: none;
  }
}
