:root {
  --primary: #722F37;
  --primary-hover: #5B252C;
  --secondary: #D4AF37;
  --accent: #A63A50;
  --bg-dark: #1A1A24;
  --panel-bg: rgba(255, 255, 255, 0.96);
  --panel-border: rgba(226, 232, 240, 0.9);
  --text-main: #1E293B;
  --text-muted: #64748B;
  --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-dark);
  color: var(--text-main);
}

#app {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
}

/* Header */
.top-nav {
  height: 60px;
  background: rgba(255, 255, 255, 0.98);
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  z-index: 1000;
  box-shadow: 0 2px 4px rgba(0,0,0,0.04);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.brand-link {
  display: flex;
  align-items: center;
  text-decoration: none;
}

.brand-logo-img {
  width: 34px;
  height: 34px;
  display: block;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(114, 47, 55, 0.2);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.brand-logo-img:hover {
  transform: scale(1.06);
  box-shadow: 0 3px 8px rgba(114, 47, 55, 0.35);
}

.brand-icon {
  font-size: 1.8rem;
}

.brand-title {
  display: flex;
  align-items: baseline;
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--primary);
  letter-spacing: 0.2px;
  line-height: 1.15;
}

.brand-exp {
  color: #D97706; /* Champagne gold accent */
  font-size: 1.05rem;
  font-weight: 800;
  vertical-align: super;
  margin-right: 0.15rem;
}

.brand-ai {
  color: #0284C7; /* High-tech Cyan/Blue accent */
  font-size: 0.8rem;
  font-weight: 700;
  background: rgba(2, 132, 199, 0.1);
  padding: 0.05rem 0.3rem;
  border-radius: 4px;
  border: 1px solid rgba(2, 132, 199, 0.3);
  letter-spacing: 0.5px;
  margin-left: 0.1rem;
}

.brand-subtitle {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-weight: 500;
  letter-spacing: 0.2px;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.basemap-selector-wrap {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: #F1F5F9;
  border: 1px solid #CBD5E1;
  border-radius: 6px;
  padding: 0.25rem 0.5rem;
  transition: all 0.2s;
}

.basemap-selector-wrap:hover {
  border-color: var(--primary);
}

.basemap-label {
  font-size: 0.9rem;
  line-height: 1;
}

.basemap-select {
  border: none;
  background: transparent;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-main);
  outline: none;
  cursor: pointer;
  padding: 0.15rem 0;
}

.lang-btn {
  background: #F1F5F9;
  border: 1px solid #CBD5E1;
  border-radius: 6px;
  padding: 0.35rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-main);
  transition: all 0.2s;
}

.lang-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.btn-link {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none;
  transition: all 0.2s;
}

.btn-github {
  background: #24292F;
  color: white;
}

.btn-github:hover {
  background: #000;
}

.btn-atlas {
  background: #3B82F6;
  color: white;
}

.btn-atlas:hover {
  background: #2563EB;
}

.btn-grapes {
  background: #7C2D12;
  color: #FEF3C7;
  border: 1px solid #9A3412;
}

.btn-grapes:hover {
  background: #9A3412;
  color: #FFFFFF;
}

/* Topnav Layer Toggles (Wine Regions & World Climate) */
.btn-toggle-layer {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  background: #F1F5F9;
  border: 1px solid #CBD5E1;
  color: var(--text-main);
  transition: all 0.2s ease;
  user-select: none;
}

.btn-toggle-layer:hover {
  background: #E2E8F0;
  border-color: #94A3B8;
}

.btn-toggle-regions.active {
  background: linear-gradient(135deg, #722F37 0%, #5B252C 100%);
  color: #FFFFFF;
  border-color: #722F37;
  box-shadow: 0 0 0 2px rgba(114, 47, 55, 0.25);
}

.btn-toggle-climate.active {
  background: linear-gradient(135deg, #0284C7 0%, #0369A1 100%);
  color: #FFFFFF;
  border-color: #0284C7;
  box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.25);
}

/* Export PDF Button (A4 Landscape) */
.btn-export-pdf {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  background: #1E293B;
  border: 1px solid #334155;
  color: #F8FAFC;
  transition: all 0.2s ease;
  user-select: none;
}

.btn-export-pdf:hover {
  background: #0F172A;
  border-color: var(--primary);
  color: #FFFFFF;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
}

.btn-export-pdf:active {
  transform: translateY(1px);
}

/* Main Workspace */
.main-content {
  flex: 1;
  position: relative;
  display: flex;
}

#map {
  flex: 1;
  width: 100%;
  height: 100%;
}

/* Sidebar Drawer */
.sidebar {
  position: absolute;
  top: 1rem;
  left: 1rem;
  bottom: 1rem;
  width: 410px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  backdrop-filter: blur(12px);
  z-index: 500;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  overflow: visible;
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar.collapsed {
  transform: translateX(calc(-100% - 1.25rem));
}

/* Sidebar Toggle Tab */
.sidebar-toggle {
  position: absolute;
  top: 1.1rem;
  right: -36px;
  width: 36px;
  height: 42px;
  background: #FFFFFF;
  border: 1px solid var(--panel-border);
  border-left: none;
  border-radius: 0 8px 8px 0;
  box-shadow: 4px 2px 8px rgba(0,0,0,0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 501;
  color: var(--primary);
  font-size: 0.85rem;
  font-weight: 700;
  outline: none;
  transition: all 0.2s ease;
  user-select: none;
}

.sidebar-toggle:hover {
  background: #F8FAFC;
  color: var(--primary-hover);
  width: 38px;
}

.sidebar.collapsed .sidebar-toggle .toggle-icon {
  transform: rotate(180deg);
}

.toggle-icon {
  display: inline-block;
  transition: transform 0.35s ease;
}

.sidebar-header {
  padding: 1rem;
  border-bottom: 1px solid var(--panel-border);
  background: #FFFFFF;
}

.search-box {
  position: relative;
  margin-bottom: 0.75rem;
}

.search-input {
  width: 100%;
  padding: 0.65rem 0.75rem 0.65rem 2.2rem;
  border: 1px solid #CBD5E1;
  border-radius: 8px;
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.2s;
}

.search-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(114, 47, 55, 0.1);
}

.search-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 0.9rem;
}

.filters-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem;
}

.filter-select {
  flex: 1;
  min-width: 0;
  padding: 0.4rem 0.5rem;
  border: 1px solid #CBD5E1;
  border-radius: 6px;
  font-size: 0.75rem;
  background: white;
  color: var(--text-main);
  outline: none;
}

.regions-counter {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.55rem 0.85rem;
  background: #F8FAFC;
  border-bottom: 1px solid var(--panel-border);
  gap: 0.5rem;
}

.counter-text-wrap {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
}

.count-visible-pill {
  font-size: 0.68rem;
  padding: 0.1rem 0.45rem;
  border-radius: 10px;
  background: #E2E8F0;
  color: #475569;
  font-weight: 700;
  transition: all 0.2s ease;
}

.vis-btn-group {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.btn-vis-action {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.22rem 0.5rem;
  font-size: 0.7rem;
  font-weight: 600;
  border-radius: 5px;
  border: 1px solid #CBD5E1;
  background: #FFFFFF;
  color: #475569;
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
}

.btn-vis-action:hover {
  background: #F1F5F9;
  border-color: var(--primary);
  color: var(--primary);
}

.regions-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.region-card {
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  background: white;
  border: 1px solid #E2E8F0;
  margin-bottom: 0.5rem;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.region-card:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}

.region-card.selected {
  border-color: var(--primary);
  background: #FFF5F5;
}

.region-card-main {
  flex: 1;
  min-width: 0;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.btn-card-visibility {
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.3rem 0.35rem;
  cursor: pointer;
  color: #64748B;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  transition: all 0.15s ease;
  user-select: none;
  opacity: 0.85;
  flex-shrink: 0;
}

.btn-card-visibility:hover {
  background: #F1F5F9;
  border-color: #CBD5E1;
  color: var(--primary);
  opacity: 1;
}

.region-card.region-hidden {
  opacity: 0.6;
  background: #F8FAFC;
  border-style: dashed;
}

.region-card.region-hidden:hover {
  opacity: 0.9;
}

.region-card.region-hidden .btn-card-visibility {
  opacity: 0.45;
  filter: grayscale(1);
}

.card-title {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--text-main);
  margin-bottom: 0.2rem;
}

.card-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.card-badge {
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 700;
  color: white;
}

/* Detail Popup / Info Card */
.detail-panel {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 440px;
  max-width: calc(100vw - 2rem);
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  backdrop-filter: blur(14px);
  z-index: 500;
  box-shadow: var(--shadow-lg);
  padding: 1.5rem;
  display: none;
}

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

.detail-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s;
}

.detail-close:hover {
  color: var(--primary);
}

.detail-header {
  margin-bottom: 1rem;
}

.detail-badge {
  display: inline-block;
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 700;
  color: white;
  margin-bottom: 0.45rem;
  letter-spacing: 0.5px;
}

.detail-title {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1.25;
}

.detail-region {
  font-size: 0.95rem;
  color: #334155;
  font-weight: 700;
  margin-top: 0.35rem;
  line-height: 1.35;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.detail-body {
  font-size: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.info-label {
  font-weight: 700;
  color: var(--text-muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.info-value {
  color: var(--text-main);
  line-height: 1.4;
}

/* Climate Detail Item */
.detail-climate-wrap {
  margin: 0.15rem 0;
}

.detail-climate-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.6rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  color: #1E293B;
  background: #F1F5F9;
  border: 1.5px solid #CBD5E1;
  transition: all 0.2s ease;
}

.detail-climate-desc {
  font-size: 0.74rem;
  color: var(--text-muted);
  line-height: 1.35;
  margin-top: 0.15rem;
}

/* Municipios Dropdown & Chips */
.munis-dropdown {
  margin-top: 0.4rem;
  background: #F8FAFC;
  border: 1px solid #E2E8F0;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.2s ease;
}

.munis-dropdown[open] {
  border-color: #CBD5E1;
  background: #FFFFFF;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}

.munis-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.45rem 0.65rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  cursor: pointer;
  user-select: none;
  list-style: none;
}

.munis-summary::-webkit-details-marker {
  display: none;
}

.summary-arrow {
  font-size: 0.7rem;
  transition: transform 0.2s ease;
  color: var(--text-muted);
}

.munis-dropdown[open] .summary-arrow {
  transform: rotate(180deg);
  color: var(--primary);
}

.munis-container {
  padding: 0.5rem 0.65rem;
  border-top: 1px solid #E2E8F0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.munis-search-input {
  width: 100%;
  padding: 0.35rem 0.5rem;
  font-size: 0.72rem;
  border: 1px solid #CBD5E1;
  border-radius: 5px;
  outline: none;
  transition: border-color 0.2s;
}

.munis-search-input:focus {
  border-color: var(--primary);
}

.munis-chips {
  max-height: 160px;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  padding-right: 0.2rem;
}

.munis-chips::-webkit-scrollbar {
  width: 4px;
}

.munis-chips::-webkit-scrollbar-thumb {
  background: #CBD5E1;
  border-radius: 2px;
}

.muni-chip {
  background: #F1F5F9;
  border: 1px solid #E2E8F0;
  border-radius: 4px;
  padding: 0.15rem 0.4rem;
  font-size: 0.7rem;
  color: #334155;
  line-height: 1.3;
  transition: background 0.15s, border-color 0.15s;
}

.muni-chip:hover {
  background: #FFF5F5;
  border-color: var(--primary);
  color: var(--primary);
}

.munis-empty {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-style: italic;
  padding: 0.3rem 0;
}

.detail-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.btn-action {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.6rem;
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  border: none;
  transition: all 0.2s;
}

.btn-pliego {
  background: var(--primary);
  color: white;
}

.btn-pliego:hover {
  background: var(--primary-hover);
}

/* MapLibre Popup Override */
.maplibregl-popup-content {
  border-radius: 8px !important;
  padding: 0.75rem 1rem !important;
  font-size: 0.85rem;
  box-shadow: var(--shadow-lg) !important;
}

/* Floating Climate Layer Map Legend */
.climate-map-legend {
  position: absolute;
  bottom: 2rem;
  right: 3.5rem;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  padding: 0.75rem 1rem;
  z-index: 500;
  min-width: 250px;
  font-size: 0.75rem;
}

.climate-legend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.5rem;
  padding-bottom: 0.35rem;
  border-bottom: 1px solid #E2E8F0;
}

.climate-legend-title {
  font-weight: 700;
  color: var(--text-main);
  font-size: 0.8rem;
}

.climate-legend-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: 0 0.2rem;
}

.climate-legend-close:hover {
  color: var(--primary);
}

.climate-legend-body {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.clim-leg-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #334155;
  font-weight: 500;
  font-size: 0.74rem;
}

.clim-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .climate-map-legend {
    right: 1rem;
    bottom: calc(45vh + 1.5rem);
    min-width: 210px;
    padding: 0.5rem 0.75rem;
  }
  .sidebar {
    width: calc(100vw - 2rem);
    height: 45vh;
    top: auto;
    bottom: 1rem;
  }
  .sidebar.collapsed {
    transform: translateY(calc(100% + 1.25rem));
  }
  .sidebar-toggle {
    top: -36px;
    right: 1rem;
    width: 44px;
    height: 36px;
    border-left: 1px solid var(--panel-border);
    border-bottom: none;
    border-radius: 8px 8px 0 0;
  }
  .sidebar-toggle .toggle-icon {
    transform: rotate(90deg);
  }
  .sidebar.collapsed .sidebar-toggle .toggle-icon {
    transform: rotate(270deg);
  }
  .detail-panel {
    width: calc(100vw - 2rem);
    right: 1rem;
    left: 1rem;
  }
  .brand-subtitle {
    display: none;
  }
}
