/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #2D5A3D;
    --primary-dark: #1B4332;
    --primary-light: #4A7C5E;
    --accent: #8FB07A;
    --accent-light: #C5D5A0;
    --accent-muted: #D4E4C4;
    --bg-warm: #F5F0E8;
    --bg-cream: #FAF7F2;
    --surface: #FFFFFF;
    --surface-warm: #FBF9F5;
    --text-primary: #2C3319;
    --text-secondary: #5C6B4F;
    --text-muted: #8A9474;
    --border: #DED8CA;
    --border-light: #EAE5DB;
    --error: #C1433E;
    --success: #3D8B5E;
    --header-height: 52px;
    --tab-bar-height: 48px;
    --direction-bar-height: 48px;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-full: 999px;
    --shadow-warm: rgba(44, 51, 25, 0.08);
    --shadow-soft: 0 1px 3px rgba(44, 51, 25, 0.06);
    --shadow-md: 0 4px 12px rgba(44, 51, 25, 0.1);
    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s ease;
}

body {
    font-family: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-warm);
    color: var(--text-primary);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Header */
.header {
    height: var(--header-height);
    background: var(--surface);
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 24px;
    z-index: 100;
}

.logo {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: 'Sora', 'DM Sans', sans-serif;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
    text-decoration: none;
    flex-shrink: 0;
    letter-spacing: -0.02em;
}

.logo-icon {
    width: 22px;
    height: 22px;
    stroke: var(--accent);
    flex-shrink: 0;
}

/* View toggle */
.view-toggle {
    display: flex;
    gap: 2px;
    margin-left: auto;
    background: var(--bg-warm);
    border-radius: var(--radius-sm);
    padding: 3px;
}

.view-toggle-link {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 14px;
    border-radius: 6px;
    text-decoration: none;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--text-muted);
    transition: all 0.2s ease;
}

.view-toggle-link:hover {
    color: var(--text-primary);
}

.view-toggle-link.active {
    background-color: var(--surface);
    color: var(--text-primary);
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.view-toggle-link svg {
    flex-shrink: 0;
}

/* Header search */
.header-search {
    flex: 0 1 280px;
    margin-left: 16px;
}

.header-search input {
    width: 100%;
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.9rem;
    font-family: inherit;
    background: var(--surface-warm);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.header-search input::placeholder {
    color: var(--text-muted);
}

.header-search input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(143, 176, 122, 0.15);
}

/* Tab bar (horizontal dataset selector) */
.tab-bar {
    height: var(--tab-bar-height);
    background: var(--bg-cream);
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    padding: 0 16px;
    z-index: 90;
    flex-shrink: 0;
}

.tab-bar-scroll {
    display: flex;
    gap: 4px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 6px 0;
    flex: 1;
    min-width: 0;
}

.tab-bar-scroll::-webkit-scrollbar {
    display: none;
}

.tab-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 6px 12px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-family: 'DM Sans', sans-serif;
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.tab-btn:hover {
    color: var(--text-primary);
    background: rgba(45, 90, 61, 0.06);
}

.tab-btn.active {
    background: var(--primary);
    color: #ffffff;
    box-shadow: 0 1px 4px rgba(45, 90, 61, 0.2);
}

.tab-btn svg {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
    opacity: 0.7;
    overflow: visible;
}

.tab-btn.active svg {
    opacity: 1;
}

/* Main content - full map layout */
.map-full {
    flex: 1;
    position: relative;
    overflow: hidden;
}

/* Map container */
.map-container {
    width: 100%;
    height: 100%;
    position: relative;
}

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

/* Map legend overlay */
.map-legend-overlay {
    position: absolute;
    bottom: 40px;
    left: 12px;
    z-index: 10;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    box-shadow: 0 4px 16px var(--shadow-warm);
    max-width: 220px;
    max-height: 50vh;
    overflow-y: auto;
}

.map-legend-overlay h3 {
    font-family: 'Sora', 'DM Sans', sans-serif;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 10px;
    font-weight: 600;
}

/* Legend */
.legend {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.legend-color {
    width: 24px;
    height: 16px;
    border-radius: 3px;
    flex-shrink: 0;
}

.legend-section-title {
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Map info overlay */
.map-info-overlay {
    position: absolute;
    top: 12px;
    right: 60px;
    z-index: 10;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    box-shadow: 0 4px 16px var(--shadow-warm);
    max-width: 320px;
    max-height: 70vh;
    overflow-y: auto;
    display: none;
}

.map-info-overlay.visible {
    display: block;
}

.map-info-overlay h3 {
    font-family: 'Sora', 'DM Sans', sans-serif;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 10px;
    font-weight: 600;
}

/* Info panel */
.info-panel {
    font-size: 0.88rem;
}

.info-placeholder {
    color: var(--text-muted);
    font-style: italic;
}

.info-row {
    display: flex;
    justify-content: space-between;
    padding: 5px 0;
    border-bottom: 1px solid var(--border-light);
}

.info-row:last-child {
    border-bottom: none;
}

.info-label {
    color: var(--text-muted);
}

.info-value {
    font-weight: 600;
    color: var(--text-primary);
}

.info-section-header {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-top: 12px;
    margin-bottom: 4px;
    padding-top: 8px;
    border-top: 1px solid var(--border-light);
}

.info-section-header:first-of-type {
    margin-top: 8px;
    padding-top: 0;
    border-top: none;
}

.info-granularity {
    font-weight: 400;
    font-size: 0.73rem;
    color: var(--text-muted);
    font-style: italic;
    text-transform: none;
}

.info-note {
    font-size: 0.8rem;
    font-style: italic;
    color: var(--text-muted);
    border-bottom: none;
}

.info-badge {
    display: inline-block;
    font-size: 0.65rem;
    font-weight: 600;
    padding: 1px 6px;
    border-radius: 4px;
    background: var(--surface-warm);
    border: 1px solid var(--border-light);
    color: var(--text-muted);
    text-transform: none;
    letter-spacing: 0;
    vertical-align: middle;
}

/* Demographics visual indicators */
.demo-nationality-bar {
    display: flex;
    height: 10px;
    border-radius: 5px;
    overflow: hidden;
    margin: 4px 0 2px;
}

.demo-nationality-bar .bar-esp {
    background: #6366f1;
    transition: width 0.3s ease;
}

.demo-nationality-bar .bar-ext {
    background: #f59e0b;
    transition: width 0.3s ease;
}

.demo-nationality-legend {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-muted);
    padding: 0;
    margin-bottom: 4px;
}

.demo-nationality-legend span {
    display: flex;
    align-items: center;
    gap: 4px;
}

.demo-nationality-legend .dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
}

.dot-esp { background: #6366f1; }
.dot-ext { background: #f59e0b; }

.demo-pct-label {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 1px 6px;
    border-radius: 4px;
    color: white;
}

.demo-pct-low { background: #22c55e; }
.demo-pct-mid { background: #f59e0b; }
.demo-pct-high { background: #ef4444; }

/* Age distribution horizontal bars */
.demo-age-bars {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 4px 0;
    border-bottom: 1px solid var(--border-light);
}

.demo-age-row {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.78rem;
}

.demo-age-label {
    width: 52px;
    color: var(--text-muted);
    text-align: right;
    flex-shrink: 0;
    font-size: 0.72rem;
}

.demo-age-bar-track {
    flex: 1;
    height: 14px;
    background: var(--accent-muted);
    border-radius: 3px;
    overflow: hidden;
    position: relative;
}

.demo-age-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
    min-width: 2px;
}

.demo-age-pct {
    width: 38px;
    text-align: right;
    font-weight: 600;
    color: var(--text-primary);
    flex-shrink: 0;
    font-size: 0.72rem;
}

/* Origin breakdown section */
.demo-origin-section {
    margin-top: 4px;
    padding-top: 10px;
    border-top: 1px solid var(--border-light);
}

.demo-origin-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 8px;
}

.demo-origin-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-primary);
}

.demo-origin-total {
    font-size: 0.72rem;
    color: var(--text-muted);
}

/* Horizontal bars per continent */
.demo-origin-bars {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 8px;
}

.demo-origin-row {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.78rem;
}

.demo-origin-label {
    width: 58px;
    color: var(--text-muted);
    text-align: right;
    flex-shrink: 0;
    font-size: 0.74rem;
}

.demo-origin-track {
    flex: 1;
    height: 14px;
    background: var(--accent-muted);
    border-radius: 3px;
    overflow: hidden;
    position: relative;
}

.demo-origin-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
    min-width: 2px;
}

.demo-origin-pct {
    width: 38px;
    text-align: right;
    font-weight: 600;
    color: var(--text-primary);
    flex-shrink: 0;
    font-size: 0.74rem;
}

/* Top countries as pill tags */
.demo-top-countries {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 2px 0 2px;
}

.demo-top-countries .country-pill {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 0.74rem;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--surface-warm, #f8f7f4);
    border: 1px solid var(--border-light, #e5e2dc);
}

.demo-top-countries .country-name {
    color: var(--text-primary);
    font-weight: 500;
}

.demo-top-countries .country-pct {
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.7rem;
}

/* Population trend charts */
.pop-trend-municipal-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0 4px;
    border-top: 1px solid var(--border-light, #e5e2dc);
    margin-top: 6px;
}

.pop-trend-muni-name {
    font-size: 0.82rem;
    font-weight: 700;
    color: var(--text-primary);
}

.pop-trend-muni-badge {
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    padding: 2px 7px;
    border-radius: 4px;
    background: rgba(99, 102, 241, 0.1);
    color: #6366f1;
}

.pop-trend-sec-badge {
    background: rgba(34, 197, 94, 0.1);
    color: #16a34a;
}

.pop-trend-section {
    padding: 6px 0 4px;
}

.pop-trend-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 2px;
}

.pop-trend-title {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-primary);
}

.pop-trend-subtitle {
    font-size: 0.68rem;
    color: var(--text-muted);
    font-style: italic;
}

.pop-trend-change {
    font-size: 0.75rem;
    font-weight: 700;
    padding: 1px 6px;
    border-radius: 4px;
}

.pop-trend-change.positive { color: #22c55e; background: rgba(34, 197, 94, 0.1); }
.pop-trend-change.negative { color: #ef4444; background: rgba(239, 68, 68, 0.1); }
.pop-trend-change.neutral  { color: #f59e0b; background: rgba(245, 158, 11, 0.1); }

/* Chart container */
.pop-chart-wrap {
    position: relative;
}

.pop-chart-svg {
    width: 100%;
    height: 110px;
    display: block;
}

.pop-chart-svg .spark-area { opacity: 0.12; }
.pop-chart-svg .spark-line { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.pop-chart-svg .spark-year { font-size: 8px; fill: #94a3b8; font-family: 'DM Sans', sans-serif; }
.pop-chart-svg .spark-hover-dot { transition: r 0.1s; }
.pop-chart-svg .spark-hover-zone { cursor: crosshair; }

.pop-chart-tooltip {
    position: absolute;
    top: -4px;
    transform: translateX(-50%);
    background: var(--text-primary, #1e293b);
    color: white;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 3px 8px;
    border-radius: 5px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 10;
}

.pop-trend-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 1px;
}

.pop-trend-legend {
    display: flex;
    justify-content: space-between;
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.pop-trend-legend .dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 3px;
    vertical-align: middle;
}

.data-granularity {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-style: italic;
    display: block;
    margin-left: 28px;
    margin-top: -4px;
}

/* MapLibre controls — subtle */
.maplibregl-ctrl-attrib {
    font-size: 10px !important;
    opacity: 0.45;
    transition: opacity 0.2s;
    background: transparent !important;
}

.maplibregl-ctrl-attrib:hover {
    opacity: 0.85;
}

.maplibregl-ctrl-scale {
    font-size: 9px !important;
    border-color: rgba(0,0,0,0.2) !important;
    background: rgba(255,255,255,0.5) !important;
    padding: 1px 5px !important;
    line-height: 1.2 !important;
    opacity: 0.5;
    transition: opacity 0.2s;
}

.maplibregl-ctrl-scale:hover {
    opacity: 0.9;
}

.maplibregl-ctrl-bottom-left,
.maplibregl-ctrl-bottom-right {
    opacity: 0.7;
    transition: opacity 0.2s;
}

.maplibregl-ctrl-bottom-left:hover,
.maplibregl-ctrl-bottom-right:hover {
    opacity: 1;
}

/* MapLibre popup styling */
.maplibregl-popup-content {
    padding: 12px 16px;
    border-radius: var(--radius-md);
    box-shadow: 0 4px 16px var(--shadow-warm);
    border: 1px solid var(--border-light);
    font-family: 'DM Sans', sans-serif;
    color: var(--text-primary);
}

.maplibregl-popup-close-button {
    font-size: 18px;
    padding: 4px 8px;
    color: var(--text-muted);
}

/* Loading state */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(245, 240, 232, 0.92);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.loading-text {
    margin-top: 16px;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* Responsive adjustments */
@media (max-width: 900px) {
    .tab-bar {
        padding: 0 8px;
    }

    .tab-btn {
        padding: 6px 10px;
        font-size: 0.8rem;
    }

    .header-search {
        display: none;
    }

    .view-toggle-link span {
        display: none;
    }
}

@media (max-width: 600px) {
    .tab-btn span.tab-label {
        display: none;
    }

    .tab-btn {
        padding: 6px 10px;
    }

    .map-info-overlay {
        right: 8px;
        left: 8px;
        max-width: none;
        top: auto;
        bottom: 12px;
        max-height: 40vh;
    }

    .map-legend-overlay {
        bottom: 12px;
        left: 8px;
        max-width: 180px;
    }
}

/* Ocio Nocturno heatmap/dots toggle */
.ocio-toggle-container {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 5;
    display: inline-flex;
    gap: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 2px;
    box-shadow: 0 1px 4px var(--shadow-warm);
    height: auto;
    width: auto;
}

.ocio-toggle-btn {
    font-family: 'DM Sans', sans-serif;
    font-size: 0.75rem;
    font-weight: 500;
    padding: 4px 10px;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.15s ease;
    line-height: 1.2;
    white-space: nowrap;
    height: auto;
    flex: none;
}

.ocio-toggle-btn:hover {
    background: var(--bg);
    color: var(--text);
}

.ocio-toggle-btn.active {
    background: var(--accent);
    color: #fff;
}

/* Hover highlight for features */
.maplibregl-canvas-container {
    cursor: pointer;
}
