/* ─────────────────────────────────────────────────────────
   GanadoMap Chile — Estilos
   Paleta agrícola: verdes profundos, tierra, paja dorada
───────────────────────────────────────────────────────── */

@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&family=Instrument+Serif:ital@0;1&display=swap');

/* ── Tokens ─────────────────────────────────────────────── */
:root,
[data-theme='light'] {
  --text-xs: clamp(0.72rem, 0.68rem + 0.2vw, 0.8rem);
  --text-sm: clamp(0.8rem, 0.75rem + 0.25vw, 0.9rem);
  --text-base: clamp(0.9rem, 0.85rem + 0.25vw, 1rem);
  --text-lg: clamp(1rem, 0.9rem + 0.5vw, 1.2rem);
  --text-xl: clamp(1.2rem, 1rem + 0.8vw, 1.5rem);
  --text-2xl: clamp(1.5rem, 1.2rem + 1.2vw, 2rem);

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;

  /* Paleta agrícola chilenera */
  --color-bg: #f5f3ee;
  --color-surface: #faf9f6;
  --color-surface-2: #fdfcfa;
  --color-surface-offset: #edeae2;
  --color-surface-dynamic: #e4e0d6;
  --color-divider: #d8d4c9;
  --color-border: #ccc8bc;

  --color-text: #22201a;
  --color-text-muted: #6b6960;
  --color-text-faint: #b0ae a8;

  --color-primary: #2d6e3e;
  --color-primary-hover: #1f5030;
  --color-primary-active: #143d22;
  --color-primary-highlight: #c3dcc8;

  --color-accent: #b8860b;
  --color-accent-hover: #9a7009;

  --color-warning: #a0522d;
  --color-success: #2d6e3e;
  --color-error: #8b1a1a;

  --radius-sm: 0.35rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-full: 9999px;

  --transition: 160ms cubic-bezier(0.16, 1, 0.3, 1);
  --shadow-sm: 0 1px 3px rgba(30,25,15,0.08);
  --shadow-md: 0 4px 12px rgba(30,25,15,0.10);
  --shadow-lg: 0 8px 24px rgba(30,25,15,0.14);

  --font-display: 'Instrument Serif', Georgia, serif;
  --font-body: 'Plus Jakarta Sans', system-ui, sans-serif;

  --sidebar-w: 260px;
  --detail-w: 290px;
}

[data-theme='dark'] {
  --color-bg: #141210;
  --color-surface: #1a1815;
  --color-surface-2: #1f1d1a;
  --color-surface-offset: #1d1b18;
  --color-surface-dynamic: #272420;
  --color-divider: #2a2822;
  --color-border: #3a3830;
  --color-text: #d6d3cc;
  --color-text-muted: #7a7872;
  --color-text-faint: #525048;
  --color-primary: #5aad72;
  --color-primary-hover: #3d9257;
  --color-primary-active: #2d7040;
  --color-primary-highlight: #243b2b;
  --color-accent: #d4a017;
  --color-accent-hover: #c08c0e;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.25);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.35);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.45);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --color-bg: #141210;
    --color-surface: #1a1815;
    --color-surface-2: #1f1d1a;
    --color-surface-offset: #1d1b18;
    --color-surface-dynamic: #272420;
    --color-divider: #2a2822;
    --color-border: #3a3830;
    --color-text: #d6d3cc;
    --color-text-muted: #7a7872;
    --color-text-faint: #525048;
    --color-primary: #5aad72;
    --color-primary-hover: #3d9257;
    --color-primary-highlight: #243b2b;
    --color-accent: #d4a017;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.25);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.35);
  }
}

/* ── Base ────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 14px;
  scroll-behavior: smooth;
  -webkit-font-smoothing: antialiased;
}

body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

a { color: var(--color-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Header ─────────────────────────────────────────────── */
.site-header {
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  max-width: 100%;
  padding: var(--space-3) var(--space-5);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
}

.logo-wrap {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.logo-icon { flex-shrink: 0; }

.site-title {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-style: italic;
  color: var(--color-primary);
  line-height: 1.1;
}

.site-subtitle {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  letter-spacing: 0.03em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.data-badge {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  background: var(--color-surface-offset);
  border: 1px solid var(--color-border);
  padding: 3px var(--space-3);
  border-radius: var(--radius-full);
  letter-spacing: 0.02em;
}

.theme-btn {
  width: 32px; height: 32px;
  border-radius: var(--radius-md);
  background: var(--color-surface-offset);
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}
.theme-btn:hover { background: var(--color-surface-dynamic); color: var(--color-text); }

/* ── Layout ─────────────────────────────────────────────── */
.app-layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr var(--detail-w);
  flex: 1;
  overflow: hidden;
  height: calc(100dvh - 57px);
}

/* ── Sidebar ────────────────────────────────────────────── */
.sidebar {
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  padding: var(--space-4) 0;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.filter-section {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-divider);
}
.filter-section:last-child { border-bottom: none; }

.filter-title {
  font-size: var(--text-xs);
  font-weight: 700;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: var(--space-3);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

/* Radio group */
.radio-group { display: flex; flex-direction: column; gap: var(--space-1); }

.radio-opt {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  transition: background var(--transition), color var(--transition);
  border: 1px solid transparent;
}
.radio-opt:hover { background: var(--color-surface-offset); color: var(--color-text); }
.radio-opt.active {
  background: var(--color-primary-highlight);
  color: var(--color-primary);
  border-color: var(--color-primary-highlight);
  font-weight: 600;
}
.radio-opt input { display: none; }
.radio-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  border: 2px solid currentColor;
  flex-shrink: 0;
  transition: background var(--transition);
}
.radio-opt.active .radio-dot { background: var(--color-primary); border-color: var(--color-primary); }

/* Breed chips */
.breed-grid {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

.breed-chip {
  font-size: var(--text-xs);
  font-weight: 600;
  padding: 4px var(--space-3);
  border-radius: var(--radius-full);
  border: 1.5px solid var(--color-border);
  cursor: pointer;
  background: var(--color-surface-offset);
  color: var(--color-text-muted);
  transition: all var(--transition);
}
.breed-chip:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}
.breed-chip.active {
  background: var(--chip-color, var(--color-primary));
  color: var(--chip-text, white);
  border-color: var(--chip-color, var(--color-primary));
}
.breed-chip[data-breed="all"].active {
  background: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
}

/* Climate buttons */
.climate-filters, .adapt-filters {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.climate-btn, .adapt-btn {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  background: transparent;
  transition: all var(--transition);
  text-align: left;
}
.climate-btn:hover, .adapt-btn:hover {
  background: var(--color-surface-offset);
  color: var(--color-text);
}
.climate-btn.active, .adapt-btn.active {
  background: var(--color-primary-highlight);
  color: var(--color-primary);
  border-color: var(--color-primary-highlight);
  font-weight: 600;
}
.climate-icon { font-size: 1em; }

/* Fuentes */
.sources-section { padding-bottom: var(--space-6); }
.sources-list { display: flex; flex-direction: column; gap: var(--space-2); }

.source-link {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  background: var(--color-surface-offset);
  transition: all var(--transition);
  text-decoration: none;
}
.source-link:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
  text-decoration: none;
}

.source-tag {
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}
.source-tag.odepa { background: #1a4e2e; color: white; }
.source-tag.inia  { background: #7b3f00; color: white; }
.source-tag.ine   { background: #1a237e; color: white; }

/* ── Mapa ────────────────────────────────────────────────── */
.map-container {
  display: flex;
  flex-direction: column;
  background: var(--color-bg);
  overflow: hidden;
  position: relative;
}

.map-header {
  padding: var(--space-3) var(--space-5);
  border-bottom: 1px solid var(--color-divider);
  background: var(--color-surface);
}

.map-title-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-2);
}

.map-title {
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--color-text);
}

.map-year {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  white-space: nowrap;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  align-items: center;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.legend-dot {
  width: 12px; height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}

.legend-label {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.map-wrapper {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-4);
  position: relative;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

#chile-map {
  height: auto;
  min-height: 820px;
  width: 260px;
  flex-shrink: 0;
  filter: drop-shadow(0 4px 16px rgba(0,0,0,0.12));
}

/* Regiones SVG */
.region {
  fill: var(--color-surface-offset);
  stroke: rgba(255,255,255,0.5);
  stroke-width: 0.8;
  transition: fill 0.3s ease, opacity 0.25s ease, stroke-width 0.15s ease;
}

.region-group:hover .region,
.region-group.selected .region {
  stroke: white;
  stroke-width: 1.8;
  filter: brightness(1.1);
}

.region-group.selected .region {
  stroke: white;
  stroke-width: 2.5;
  filter: brightness(1.15) drop-shadow(0 0 4px rgba(255,255,255,0.4));
}

.region-label {
  fill: rgba(255,255,255,0.92);
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  font-family: var(--font-body);
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
  font-size: 6.5px;
}

/* Tooltip */
.map-tooltip {
  display: none;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: var(--space-3) var(--space-4);
  min-width: 220px;
  max-width: 280px;
  z-index: 50;
  pointer-events: all;
}

.tooltip-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-2);
  font-weight: 700;
  font-size: var(--text-sm);
  color: var(--color-text);
  border-bottom: 1px solid var(--color-divider);
  padding-bottom: var(--space-2);
}

.tooltip-close {
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: 12px;
  padding: 2px;
  border-radius: 4px;
  transition: color var(--transition);
}
.tooltip-close:hover { color: var(--color-text); }

.tt-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-xs);
  padding: 2px 0;
  color: var(--color-text-muted);
}
.tt-row strong { color: var(--color-text); font-weight: 600; }

.tt-hint {
  margin-top: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-primary);
  font-style: italic;
  text-align: right;
}

/* ── Panel Detalle ──────────────────────────────────────── */
.detail-panel {
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  overflow-y: auto;
  padding: var(--space-4);
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.detail-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  height: 100%;
  text-align: center;
  color: var(--color-text-faint);
  font-size: var(--text-sm);
  padding: var(--space-8);
}

.detail-content { display: block; }

.detail-region-header {
  margin-bottom: var(--space-4);
  padding-bottom: var(--space-3);
  border-bottom: 2px solid var(--color-primary-highlight);
}

.detail-region-header h3 {
  font-family: var(--font-display);
  font-size: var(--text-xl);
  font-style: italic;
  color: var(--color-text);
  margin-bottom: var(--space-1);
}

.climate-tag {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-primary);
  background: var(--color-primary-highlight);
  padding: 3px var(--space-3);
  border-radius: var(--radius-full);
}

/* Stats grid */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}

.stat-card {
  background: var(--color-surface-offset);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-2);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-val {
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--color-primary);
  line-height: 1.2;
}

.stat-lbl {
  font-size: 9px;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  line-height: 1.2;
}

/* Breed bars */
.section-label {
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.breed-bars-section,
.system-section,
.adapt-section {
  margin-bottom: var(--space-4);
}

.breed-bars { display: flex; flex-direction: column; gap: var(--space-1); }

.breed-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.breed-name {
  font-size: var(--text-xs);
  font-weight: 600;
  width: 90px;
  flex-shrink: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.breed-bar-wrap {
  flex: 1;
  background: var(--color-surface-offset);
  border-radius: var(--radius-full);
  height: 8px;
  overflow: hidden;
}

.breed-bar {
  height: 100%;
  border-radius: var(--radius-full);
  transition: width 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

.breed-pct {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  width: 28px;
  text-align: right;
}

/* Sistema productivo */
.system-label-text {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-bottom: var(--space-2);
}

.system-bar {
  display: flex;
  height: 28px;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--color-border);
}

.system-seg {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  color: white;
  letter-spacing: 0.02em;
  transition: width 0.5s ease;
  white-space: nowrap;
  overflow: hidden;
}

.system-seg span {
  padding: 0 var(--space-2);
  overflow: hidden;
  text-overflow: ellipsis;
}

.system-seg.pastoreo { background: #4a7c59; }
.system-seg.intensivo { background: #2c5f8a; }

/* Adaptabilidad */
.adapt-tags { display: flex; flex-wrap: wrap; gap: var(--space-1); }

.adapt-tag {
  font-size: var(--text-xs);
  font-weight: 600;
  padding: 4px var(--space-3);
  border-radius: var(--radius-full);
  border: 1px solid;
}
.tag-sequia { background: #fff3cd; border-color: #ffc107; color: #856404; }
.tag-frio   { background: #cfe2ff; border-color: #0d6efd; color: #084298; }
.tag-calor  { background: #f8d7da; border-color: #dc3545; color: #842029; }
.tag-doble  { background: #d1e7dd; border-color: #198754; color: #0f5132; }

/* Fuente */
.source-note {
  background: var(--color-surface-offset);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
}

.region-desc {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  line-height: 1.55;
  margin-bottom: var(--space-2);
}

.source-text {
  font-size: 10px;
  color: var(--color-text-muted);
  font-style: italic;
}

/* ── Modal ──────────────────────────────────────────────── */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(4px);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg);
  max-width: 900px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-6);
  border-bottom: 1px solid var(--color-divider);
}

.modal-header h3 {
  font-family: var(--font-display);
  font-style: italic;
  font-size: var(--text-xl);
}

.modal-close {
  cursor: pointer;
  color: var(--color-text-muted);
  font-size: 16px;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-md);
  background: var(--color-surface-offset);
  border: 1px solid var(--color-border);
  transition: all var(--transition);
}
.modal-close:hover { background: var(--color-surface-dynamic); color: var(--color-text); }

.modal-body {
  overflow-y: auto;
  padding: var(--space-4) var(--space-6);
}

/* Tabla comparativa */
.comparison-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-xs);
}

.comparison-table th {
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-bottom: 2px solid var(--color-border);
  color: var(--color-text-muted);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 9px;
  white-space: nowrap;
  background: var(--color-surface-offset);
}

.comparison-table td {
  padding: var(--space-3);
  border-bottom: 1px solid var(--color-divider);
  color: var(--color-text);
  vertical-align: middle;
}

.comparison-table tr:hover td { background: var(--color-surface-offset); }

.breed-swatch {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 3px;
  margin-right: var(--space-2);
  vertical-align: middle;
}

.table-source {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  margin-top: var(--space-3);
  font-style: italic;
}

/* FAB */
.fab-compare {
  position: fixed;
  bottom: var(--space-5);
  right: var(--space-5);
  z-index: 90;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--color-primary);
  color: white;
  font-family: var(--font-body);
  font-size: var(--text-xs);
  font-weight: 700;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  border: none;
  letter-spacing: 0.02em;
  transition: background var(--transition), box-shadow var(--transition), transform var(--transition);
}
.fab-compare:hover {
  background: var(--color-primary-hover);
  box-shadow: var(--shadow-lg);
  transform: translateY(-1px);
}

/* ── Scrollbars ─────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 99px; }

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 1100px) {
  :root { --detail-w: 250px; --sidebar-w: 230px; }
}

@media (max-width: 860px) {
  .app-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr auto;
    height: auto;
  }
  .sidebar, .detail-panel {
    height: auto;
    max-height: 300px;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
    border-left: none;
  }
  .map-wrapper { min-height: 400px; }
  .fab-compare { bottom: var(--space-3); right: var(--space-3); }
  .data-badge { display: none; }
}
