/* CSS Reset and Base Styles */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --primary-color: #08306b;
    --primary-light: #f7fbff;
    --bg-color: #f5f5f5;
    --panel-bg: #ffffff;
    --text-color: #333333;
    --text-muted: #666666;
    --border-color: #dddddd;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    --header-height: 60px;
    --timeline-height: 60px;
    --panel-width: 400px;
}

/* Global hidden class */
.hidden {
    display: none !important;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    color: var(--text-color);
    background-color: var(--bg-color);
}

#app {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Header */
#header {
    height: var(--header-height);
    background: var(--panel-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: var(--shadow);
    z-index: 100;
}

#header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--primary-color);
}

#header h1 a {
    color: inherit;
    text-decoration: none;
}

/* Search box */
.search-box {
    position: relative;
    flex: 1;
    max-width: 300px;
}

#search-input {
    width: 100%;
    padding: 8px 12px;
    font-size: 0.875rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: white;
}

#search-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(8, 48, 107, 0.1);
}

.search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: white;
    border: 1px solid var(--border-color);
    border-top: none;
    border-radius: 0 0 4px 4px;
    max-height: 300px;
    overflow-y: auto;
    z-index: 500;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.search-results.hidden {
    display: none;
}

.search-result-item {
    padding: 10px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.875rem;
    color: var(--text-color);
    transition: background-color 0.2s;
}

.search-result-item:hover {
    background: var(--primary-light);
}

.search-result-item:last-child {
    border-bottom: none;
}

.search-result-item.highlighted {
    background: var(--primary-light);
}

#dataset-selector {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

#dataset-selector label {
    font-size: 0.875rem;
    color: var(--text-muted);
}

#dataset-select {
    padding: 8px 12px;
    font-size: 0.875rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: white;
    cursor: pointer;
    min-width: 200px;
}

.link-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 12px;
    font-size: 0.875rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: #ffffff;
    color: var(--primary-color);
    text-decoration: none;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

.link-button:hover {
    background: var(--primary-light);
    border-color: #c9d7ee;
}

#per-capita-label {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.875rem;
    color: var(--text-muted);
    cursor: pointer;
}

#per-capita-toggle {
    width: 16px;
    height: 16px;
    accent-color: var(--primary-color);
}

#per-capita-toggle:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

#per-capita-toggle:disabled + span {
    color: #999999;
}

/* Main content */
#main {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* Remove browser focus outline on map polygon paths */
.leaflet-interactive:focus {
    outline: none;
}

/* Map container */
#map-container {
    flex: 1;
    position: relative;
}

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

/* Legend */
#legend {
    position: absolute;
    bottom: 30px;
    right: 20px;
    background: var(--panel-bg);
    padding: 12px 16px;
    border-radius: 4px;
    box-shadow: var(--shadow);
    z-index: 500;
    min-width: 150px;
    max-width: 240px;
}

#legend-title {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 8px;
    text-transform: uppercase;
}

#legend-note {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-bottom: 8px;
    line-height: 1.3;
}

#legend-scale {
    height: 12px;
    background: linear-gradient(to right,
        #f7fbff, #deebf7, #c6dbef, #9ecae1,
        #6baed6, #4292c6, #2171b5, #08519c, #08306b);
    border-radius: 2px;
    margin-bottom: 4px;
}

#legend-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-muted);
}

#legend-no-data {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border-color);
}

#legend-outliers {
    font-size: 0.65rem;
    color: var(--text-muted);
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px solid var(--border-color);
    line-height: 1.3;
    display: none;
}

/* Timeline controls */
#timeline-container {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    height: 52px;
    min-width: 420px;
    max-width: 640px;
    width: calc(50% - 40px);
    background: var(--panel-bg);
    border-radius: 8px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 15px;
    z-index: 500;
}

#play-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
}

#play-btn:hover {
    background: #0a4a8f;
}

#play-icon {
    font-size: 14px;
    margin-left: 2px;
}

#year-slider {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border-color);
    border-radius: 3px;
    outline: none;
}

#year-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    transition: transform 0.1s;
}

#year-slider::-webkit-slider-thumb:hover {
    transform: scale(1.1);
}

#year-slider::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border: none;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
}

#year-display {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--primary-color);
    min-width: 60px;
    text-align: center;
}

/* Detail panel */
#detail-panel {
    position: fixed;
    top: var(--header-height);
    right: 0;
    width: var(--panel-width);
    height: calc(100% - var(--header-height));
    background: var(--panel-bg);
    box-shadow: -2px 0 8px rgba(0, 0, 0, 0.15);
    z-index: 600;
    padding: 20px;
    overflow-y: auto;
    transform: translateX(0);
    transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

#detail-panel.hidden {
    display: block !important; /* override global .hidden so transform transition works */
    transform: translateX(100%);
    pointer-events: none;
}

#close-panel {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    font-size: 24px;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: 4px;
}

#close-panel:hover {
    background: var(--bg-color);
    color: var(--text-color);
}

#detail-name {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--primary-color);
    margin-bottom: 10px;
    padding-right: 40px;
}

#detail-value {
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 20px;
}

#detail-number {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-color);
}

#detail-unit {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-left: 6px;
}

.page {
    height: auto;
    min-height: 100%;
}

.page-content {
    flex: 1;
    padding: 24px;
    display: grid;
    gap: 20px;
    max-width: 900px;
    margin: 0 auto;
}

.card {
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 20px;
    box-shadow: var(--shadow);
}

.card h2 {
    font-size: 1.125rem;
    color: var(--primary-color);
    margin-bottom: 12px;
}

.attributions-list {
    list-style: none;
    display: grid;
    gap: 12px;
    font-size: 0.95rem;
    color: var(--text-color);
}

.attributions-list a {
    color: var(--primary-color);
}

.attributions-list a:hover {
    text-decoration: underline;
}

#chart-container {
    height: 250px;
    margin-top: 20px;
}

/* Loading overlay */
#loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#loading.hidden {
    display: none;
}

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

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

/* Error toast */
#error-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: #dc3545;
    color: white;
    padding: 12px 24px;
    border-radius: 4px;
    box-shadow: var(--shadow);
    z-index: 900;
    max-width: 400px;
}

#error-toast.hidden {
    display: none;
}

/* Leaflet tooltip styling */
.leaflet-tooltip {
    background: var(--panel-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 8px 12px;
    font-size: 0.875rem;
    box-shadow: var(--shadow);
}

.tooltip-name {
    font-weight: 600;
    color: var(--primary-color);
}

.tooltip-value {
    color: var(--text-muted);
}

.tooltip-note {
    margin-top: 4px;
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* Progress bar (timeline loading) */
#progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0%;
    background: var(--primary-color);
    z-index: 1001;
    pointer-events: none;
    transition: width 0.3s ease-out, opacity 0.3s ease;
    opacity: 0;
}

#progress-bar.loading {
    width: 80%;
    opacity: 1;
}

#progress-bar.complete {
    width: 100%;
    opacity: 0;
}

/* Council page */
#council-page {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow-y: auto;
    background: var(--bg-color);
}

#council-page.hidden {
    display: none;
}

/* Council header */
.council-header {
    background: var(--panel-bg);
    padding: 20px 40px;
    border-bottom: 1px solid var(--border-color);
    box-shadow: var(--shadow);
}

.breadcrumb {
    margin-bottom: 15px;
}

.breadcrumb a {
    color: var(--primary-color);
    text-decoration: none;
    font-size: 0.875rem;
    transition: opacity 0.2s;
}

.breadcrumb a:hover {
    opacity: 0.7;
}

.council-name {
    font-size: 2rem;
    font-weight: 600;
    color: var(--primary-color);
    margin: 10px 0 5px;
}

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

/* Council content */
.council-content {
    padding: 40px;
    max-width: 1400px;
    margin: 0 auto;
    width: 100%;
}

/* Dataset grid */
.dataset-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 30px;
}

@media (min-width: 1024px) {
    .dataset-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Dataset sections */
.dataset-section {
    background: var(--panel-bg);
    border-radius: 8px;
    padding: 30px;
    box-shadow: var(--shadow);
}

.dataset-section.no-data-section {
    opacity: 0.7;
}

.dataset-section h2 {
    font-size: 1.5rem;
    color: var(--primary-color);
    margin-bottom: 20px;
}

.chart-wrapper {
    height: 350px;
    position: relative;
    margin-bottom: 20px;
}

.chart-wrapper canvas {
    max-height: 100%;
}

/* Summary stats */
.chart-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    padding-top: 15px;
    border-top: 1px solid var(--border-color);
}

.chart-stats .stat label {
    font-size: 0.75rem;
    color: var(--text-muted);
    display: block;
    margin-bottom: 5px;
}

.chart-stats .stat span {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-color);
}

/* Error messages */
.dataset-section.error {
    background: #fff5f5;
    border-left: 4px solid #dc3545;
}

.error-message {
    color: #dc3545;
    font-size: 0.875rem;
}

.not-found-message,
.error-message-box {
    text-align: center;
    padding: 40px 20px;
}

.not-found-message h2,
.error-message-box h2 {
    font-size: 1.5rem;
    color: var(--primary-color);
    margin-bottom: 10px;
}

.not-found-message p,
.error-message-box p {
    color: var(--text-muted);
    margin-bottom: 15px;
}

.not-found-message code,
.error-message-box code {
    background: var(--bg-color);
    padding: 2px 6px;
    border-radius: 3px;
    font-family: monospace;
}

/* View details link in modal */
.view-details-link {
    display: inline-block;
    color: var(--primary-color);
    text-decoration: none;
    font-size: 0.875rem;
    margin-bottom: 15px;
    padding: 8px 12px;
    border: 1px solid var(--primary-color);
    border-radius: 4px;
    transition: all 0.2s;
}

.view-details-link:hover {
    background: var(--primary-color);
    color: white;
}

.loading-message {
    color: var(--text-muted);
    font-size: 0.875rem;
    text-align: center;
    padding: 20px;
}

.no-data {
    color: var(--text-muted);
    font-size: 0.875rem;
    text-align: center;
    padding: 40px 20px;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    :root {
        --panel-width: 100%;
    }

    #header {
        flex-wrap: wrap;
        gap: 8px;
        padding: 8px 12px;
    }

    #header h1 {
        font-size: 1rem;
        flex: 0 0 100%;
    }

    #search-container {
        flex: 1;
        min-width: 150px;
        max-width: none;
    }

    #dataset-selector {
        flex: 0 0 100%;
    }

    #dataset-select {
        flex: 1;
        min-width: 0;
        width: 100%;
    }

    #detail-panel {
        top: auto;
        bottom: 0;
        height: 65%;
        width: 100%;
        transform: translateY(100%);
        transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    }

    #detail-panel.hidden {
        display: block !important;
        transform: translateY(100%);
        pointer-events: none;
    }

    #detail-panel:not(.hidden) {
        transform: translateY(0);
    }

    #timeline-container {
        bottom: 16px;
        min-width: 0;
        width: calc(100% - 32px);
    }

    #legend {
        top: calc(var(--header-height) + 10px);
        right: 10px;
        bottom: auto;
        left: auto;
        min-width: auto;
        max-width: 160px;
    }

    /* Council page mobile */
    .council-header {
        padding: 15px 20px;
    }

    .council-name {
        font-size: 1.5rem;
    }

    .council-content {
        padding: 20px;
    }

    .dataset-grid {
        gap: 20px;
    }

    .dataset-section {
        padding: 20px;
    }

    .dataset-section h2 {
        font-size: 1.25rem;
        margin-bottom: 15px;
    }

    .chart-wrapper {
        height: 250px;
    }

    .chart-stats {
        grid-template-columns: 1fr;
    }
}
