/* ==========================================
   Statistics Module — CESU, Regional, National
   ========================================== */

/* KPI Cards */
.stats-kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 1rem;
    margin-bottom: 2rem;
}

.stats-kpi-card {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    padding: 1.25rem 1rem;
    text-align: center;
}

.stats-kpi-card__value {
    display: block;
    font-size: 2rem;
    font-weight: 700;
    line-height: 1.2;
    color: var(--color-primary, #1e3a5f);
}

.stats-kpi-card__label {
    display: block;
    font-size: 0.85rem;
    color: #64748b;
    margin-top: 0.25rem;
}

/* Year-over-year variation */
.stats-kpi-card__var {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 600;
    margin-top: 0.35rem;
    padding: 0.15rem 0.5rem;
    border-radius: 10px;
}

.stats-var--up {
    color: #15803d;
    background: #dcfce7;
}

.stats-var--down {
    color: #b91c1c;
    background: #fee2e2;
}

/* 5-column KPI variant */
.stats-kpi-grid--5 {
    grid-template-columns: repeat(5, 1fr);
}

/* Year Selector */
.stats-year-selector {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.stats-year-selector label {
    font-weight: 600;
    font-size: 0.9rem;
    color: #334155;
}

.stats-year-selector select {
    padding: 0.4rem 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 0.9rem;
    background: #fff;
}

/* Chart Containers */
.stats-chart-container {
    position: relative;
    margin-bottom: 2rem;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    padding: 1.5rem;
}

.stats-chart-container h3 {
    margin: 0 0 1rem;
    font-size: 1.1rem;
    color: #1e293b;
}

.stats-chart-wrapper {
    position: relative;
    height: 300px;
}

.stats-chart-wrapper--tall {
    height: 400px;
}

/* Two-column layout for chart + data */
.stats-columns {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

/* Map + sidebar layout */
.stats-map-layout {
    display: grid;
    grid-template-columns: 1.5fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.statistics-map {
    position: relative;
}

.statistics-map svg,
.statistics-map object {
    width: 100%;
    height: auto;
    max-height: 500px;
}

/* Choropleth levels */
.choropleth-level-0 .st0,
.choropleth-level-0 path {
    fill: #d9f0fa;
    transition: fill 0.3s ease;
}

.choropleth-level-1 .st0,
.choropleth-level-1 path {
    fill: #8bcde6;
    transition: fill 0.3s ease;
}

.choropleth-level-2 .st0,
.choropleth-level-2 path {
    fill: #3ea8cc;
    transition: fill 0.3s ease;
}

.choropleth-level-3 .st0,
.choropleth-level-3 path {
    fill: #0080aa;
    transition: fill 0.3s ease;
}

.choropleth-level-4 .st0,
.choropleth-level-4 path {
    fill: #005f85;
    transition: fill 0.3s ease;
}

.choropleth-no-data .st0,
.choropleth-no-data path {
    fill: #f0f0f0;
}

/* Hover state */
.choropleth-level-0:hover .st0,
.choropleth-level-0:hover path,
.choropleth-level-1:hover .st0,
.choropleth-level-1:hover path,
.choropleth-level-2:hover .st0,
.choropleth-level-2:hover path,
.choropleth-level-3:hover .st0,
.choropleth-level-3:hover path,
.choropleth-level-4:hover .st0,
.choropleth-level-4:hover path {
    opacity: 0.8;
    stroke: #1e293b;
    stroke-width: 1.5px;
}

/* Legend */
.stats-legend {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem;
    font-size: 0.8rem;
    color: #64748b;
}

.stats-legend__item {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.stats-legend__color {
    width: 16px;
    height: 12px;
    border-radius: 2px;
    display: inline-block;
}

/* Ranking sidebar */
.stats-ranking {
    list-style: none;
    padding: 0;
    margin: 0;
}

.stats-ranking li {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem 0;
    border-bottom: 1px solid #f1f5f9;
    font-size: 0.9rem;
}

.stats-ranking li:last-child {
    border-bottom: none;
}

.stats-ranking__rank {
    font-weight: 700;
    color: #2563eb;
    min-width: 1.5rem;
}

.stats-ranking__name {
    flex: 1;
    margin: 0 0.5rem;
}

.stats-ranking__value {
    font-weight: 600;
    color: #1e293b;
}

/* Regional table */
.stats-table-container {
    overflow-x: auto;
    margin-bottom: 2rem;
}

.stats-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.stats-table th,
.stats-table td {
    padding: 0.6rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid #e2e8f0;
}

.stats-table th {
    background: #f8fafc;
    font-weight: 600;
    color: #334155;
    white-space: nowrap;
    position: sticky;
    top: 0;
}

.stats-table td {
    color: #475569;
}

.stats-table td:first-child {
    font-weight: 500;
    color: #1e293b;
}

.stats-table tr:hover td {
    background: #f1f5f9;
}

.stats-table .text-right {
    text-align: right;
}

/* Accessible details/summary */
.stats-detail-toggle {
    margin-top: 1.5rem;
}

.stats-detail-toggle summary {
    cursor: pointer;
    font-weight: 600;
    color: #2563eb;
    padding: 0.5rem 0;
    font-size: 0.95rem;
}

.stats-detail-toggle summary:hover {
    text-decoration: underline;
}

/* Source footer */
.stats-source {
    margin-top: 2rem;
    padding-top: 1rem;
    border-top: 1px solid #e2e8f0;
    font-size: 0.8rem;
    color: #94a3b8;
}

/* Metric selector */
.stats-metric-selector {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.stats-metric-selector select {
    padding: 0.4rem 0.75rem;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 0.9rem;
    background: #fff;
}

/* Loading overlay */
.stats-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    color: #94a3b8;
    font-size: 0.95rem;
}

.stats-loading::before {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid #e2e8f0;
    border-top-color: #2563eb;
    border-radius: 50%;
    animation: stats-spin 0.6s linear infinite;
    margin-right: 0.75rem;
}

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

/* Section within CESU/RESU single pages */
.stats-section {
    margin-top: 2rem;
    padding-top: 2rem;
    border-top: 2px solid #e2e8f0;
}

.stats-section h2 {
    font-size: 1.5rem;
    margin: 0 0 1.5rem;
    color: #1e293b;
}

/* Subnav */
.stats-subnav {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
    margin-top: 1.5rem;
}

.stats-subnav a {
    color: #2563eb;
    text-decoration: none;
    font-weight: 500;
}

.stats-subnav a:hover {
    text-decoration: underline;
}

.stats-subnav__current {
    color: var(--color-primary, #1e3a5f);
}

/* Subtitle */
.stats-subtitle {
    color: #64748b;
    margin: 0.5rem 0 0;
    font-size: 0.95rem;
}

/* Responsive */
@media (max-width: 768px) {
    .stats-columns,
    .stats-map-layout {
        grid-template-columns: 1fr;
    }

    .stats-kpi-grid,
    .stats-kpi-grid--5 {
        grid-template-columns: repeat(2, 1fr);
    }

    .stats-chart-wrapper {
        height: 250px;
    }

    .stats-year-selector {
        flex-wrap: wrap;
    }

    .stats-legend {
        flex-wrap: wrap;
    }
}

@media (max-width: 480px) {
    .stats-kpi-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }

    .stats-kpi-card {
        padding: 0.75rem 0.5rem;
    }

    .stats-kpi-card__value {
        font-size: 1.5rem;
    }
}

/* Print */
@media print {
    .stats-year-selector select,
    .stats-metric-selector select {
        border: none;
        appearance: none;
    }

    .stats-chart-container {
        border: 1px solid #ccc;
        break-inside: avoid;
    }

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

    .choropleth-level-0 .st0,
    .choropleth-level-0 path { fill: #d9d9d9 !important; }
    .choropleth-level-1 .st0,
    .choropleth-level-1 path { fill: #b3b3b3 !important; }
    .choropleth-level-2 .st0,
    .choropleth-level-2 path { fill: #8c8c8c !important; }
    .choropleth-level-3 .st0,
    .choropleth-level-3 path { fill: #666 !important; }
    .choropleth-level-4 .st0,
    .choropleth-level-4 path { fill: #333 !important; }
}
