:root {
    /* STITCH DESIGN SYSTEM: HYBRID */

    /* Brand Colors */
    --primary: #3b82f6;
    /* Royal Blue */
    --primary-glow: rgba(59, 130, 246, 0.4);
    --accent-rose: #f43f5e;
    --accent-emerald: #10b981;

    /* Sidebar (Dark Glass) */
    --sb-bg: #020617;
    /* Deepest Slate */
    --sb-glass: rgba(15, 23, 42, 0.90);
    /* Increased opacity for better contrast */
    --sb-border: rgba(255, 255, 255, 0.08);
    --sb-text: #f8fafc;
    /* White-ish */
    --sb-muted: #94a3b8;
    /* Muted Slate */

    /* Map (Light & Clean) */
    --map-bg: #e2e8f0;
    --map-overlay-bg: rgba(255, 255, 255, 0.95);
    --map-text: #0f172a;
    /* Dark Slate */

    /* UI Elements */
    --radius: 12px;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-glow: 0 0 15px rgba(59, 130, 246, 0.2);

    --font-main: 'Inter', sans-serif;

    /* Legacy Var Support (Mapping to new system) */
    --bg-card: var(--map-overlay-bg);
    /* For Leaflet popups */
    --text-main: var(--map-text);
    /* For Leaflet popups */
    --text-muted: #64748b;
    /* For Leaflet popups */
    --primary-color: var(--primary);
    --radius-xl: var(--radius);
    --glass-border: #cbd5e1;
    /* For Map elements */
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-main);
    color: var(--sb-text);
    background: var(--sb-bg);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

#app-container {
    display: flex;
    height: 100%;
    width: 100%;
    background: var(--sb-bg);
}

/* --- Sidebar: Premium Dark Glass --- */
#sidebar {
    width: 360px;
    /* Slightly wider for better breathing room */
    background: var(--sb-glass);
    border-right: 1px solid var(--sb-border);
    padding: 32px 24px;
    display: flex;
    flex-direction: column;
    z-index: 1001;
    /* Above map */
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    box-shadow: 10px 0 30px rgba(0, 0, 0, 0.3);
    /* Deep shadow over map */
}

.sidebar-header {
    margin-bottom: 32px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--sb-border);
    flex-shrink: 0;
}

.logo-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    text-align: center;
}

.sidebar-logo {
    width: 120px;
    height: auto;
    filter: drop-shadow(0 0 15px rgba(255, 255, 255, 0.4));
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.sidebar-logo:hover {
    transform: scale(1.05) rotate(2deg);
    filter: drop-shadow(0 0 20px rgba(255, 255, 255, 0.6));
}

.sidebar-header h1 {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: white;
    background: linear-gradient(135deg, #fff 0%, #cbd5e1 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 4px;
}

.subtitle {
    font-size: 10px;
    font-weight: 600;
    color: var(--primary);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    line-height: 1.4;
    margin-top: 4px;
}

/* --- Controls --- */
.control-panel {
    flex-shrink: 0;
}

.control-group {
    margin-bottom: 32px;
}

.control-group label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    margin-bottom: 12px;
    color: var(--sb-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.layer-toggles {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.layer-btn {
    padding: 10px 12px;
    border: 1px solid var(--sb-border);
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    color: var(--sb-muted);
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
}

.layer-btn:hover {
    background: rgba(255, 255, 255, 0.08);
    color: white;
    border-color: rgba(255, 255, 255, 0.2);
}

.layer-btn.active {
    background: rgba(59, 130, 246, 0.15);
    color: #60a5fa;
    border-color: var(--primary);
    box-shadow: var(--shadow-glow);
}

/* New Persistent Overlay State */
.layer-btn.overlay-active {
    background: #d946ef !important;
    color: white !important;
    border-color: #f472b6 !important;
    box-shadow: 0 0 15px rgba(217, 70, 239, 0.4);
}

.layer-btn.warning.active {
    background: rgba(244, 63, 94, 0.15);
    color: #fb7185;
    border-color: var(--accent-rose);
    box-shadow: 0 0 15px rgba(244, 63, 94, 0.2);
}

/* --- Filters --- */
.filter-toggles {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.checkbox-btn {
    display: flex;
    align-items: center;
    cursor: pointer;
    font-size: 13px;
    color: var(--sb-muted);
    transition: color 0.2s;
    user-select: none;
}

.checkbox-btn:hover {
    color: white;
}

.checkbox-btn input {
    margin-right: 8px;
    accent-color: var(--primary);
    width: 14px;
    height: 14px;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--sb-border);
    border-radius: 3px;
    cursor: pointer;
}

/* --- Insights Panel --- */
#insights-panel {
    flex-grow: 1;
    overflow-y: auto;
    padding-right: 4px;
}

/* Scrollbar */
#insights-panel::-webkit-scrollbar {
    width: 4px;
}

#insights-panel::-webkit-scrollbar-track {
    background: transparent;
}

#insights-panel::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

#insights-panel h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--sb-border);
    color: white;
    display: flex;
    align-items: center;
}

#insights-panel h3::before {
    content: '';
    display: inline-block;
    width: 3px;
    height: 14px;
    background: var(--primary);
    margin-right: 10px;
    border-radius: 2px;
    box-shadow: 0 0 8px var(--primary);
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    margin-bottom: 32px;
}

.stat-card {
    background: rgba(255, 255, 255, 0.03);
    padding: 16px;
    border-radius: var(--radius);
    border: 1px solid var(--sb-border);
    position: relative;
    overflow: hidden;
}

.stat-card:hover {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.15);
}

.stat-card .label {
    font-size: 10px;
    color: var(--sb-muted);
    font-weight: 700;
    text-transform: uppercase;
    display: block;
    margin-bottom: 6px;
    letter-spacing: 0.05em;
}

.stat-card .value {
    font-size: 24px;
    font-weight: 700;
    color: white;
}

/* Rankings */
#rankings-container h4 {
    font-size: 11px;
    font-weight: 700;
    color: var(--sb-muted);
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.styled-list {
    list-style: none;
}

.styled-list li {
    padding: 10px 0;
    border-bottom: 1px solid var(--sb-border);
    font-size: 13px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.2s;
}

.styled-list li:hover {
    background: rgba(255, 255, 255, 0.03);
}

.styled-list li span {
    color: #cbd5e1 !important;
}

.styled-list li strong {
    color: white !important;
}

.styled-list li span[style*="background"] {
    background: rgba(59, 130, 246, 0.2) !important;
    color: #93c5fd !important;
    border: 1px solid rgba(59, 130, 246, 0.3);
}

.sidebar-footer {
    padding-top: 24px;
    border-top: 1px solid var(--sb-border);
    color: var(--sb-muted);
    font-size: 11px;
    text-align: center;
}

/* --- MAP AREA (Light Mode) --- */
#map-wrapper {
    flex-grow: 1;
    position: relative;
    background: var(--map-bg);
    overflow: hidden;
}

#map {
    height: 100%;
    width: 100%;
    z-index: 1;
    /* KEY FIX: No filter = Light Tiles */
    filter: none;
}

/* Popups (Light) */
.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
    background: white;
    color: var(--map-text);
    border: 1px solid #cbd5e1;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    border-radius: 8px;
    font-family: var(--font-main);
}

.leaflet-popup-content-wrapper {
    padding: 0;
}

.leaflet-popup-content {
    margin: 12px;
}

.leaflet-popup-content strong {
    color: #0f172a !important;
    /* Slate 900 */
    font-weight: 600;
}

.leaflet-popup-content span {
    color: #64748b !important;
    /* Slate 500 */
}

.leaflet-popup-content hr {
    border-color: #f1f5f9 !important;
}

/* Legend (Light) */
.info.legend {
    background: rgba(255, 255, 255, 0.95) !important;
    color: #475569 !important;
    border: 1px solid #cbd5e1;
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1) !important;
    border-radius: 8px;
    padding: 12px;
}

.info.legend strong {
    color: #0f172a !important;
    font-weight: 600;
}

/* Overlays */
#loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(4px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    transition: opacity 0.3s ease;
}

#loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid #e2e8f0;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 16px;
}

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

/* Skeleton */
.skeleton {
    height: 70px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: var(--radius);
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* --- Responsive Optimizations --- */

/* Utility for mobile only elements */
.mobile-only {
    display: none;
}

/* Tablet (1024px and below) */
@media (max-width: 1024px) {
    #sidebar {
        width: 320px;
        padding: 24px 16px;
    }

    .sidebar-header h1 {
        font-size: 20px;
    }
}

/* Mobile (768px and below) */
@media (max-width: 768px) {
    .mobile-only {
        display: flex;
    }

    #app-container {
        flex-direction: column;
    }

    #sidebar {
        width: 100%;
        height: 75vh;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2000;
        border-right: none;
        border-top: 1px solid var(--sb-border);
        border-radius: 20px 20px 0 0;
        padding: 40px 20px 40px;
        /* Increased bottom padding */
        transform: translateY(100%);
        transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
        box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.4);
        overflow-y: auto;
        /* Enable scrolling */
        -webkit-overflow-scrolling: touch;
        /* Smooth momentum scroll */
    }

    #sidebar.open {
        transform: translateY(0);
    }

    /* Pull handle for the drawer */
    #sidebar::after {
        content: '';
        position: absolute;
        top: 12px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: var(--sb-muted);
        opacity: 0.3;
        border-radius: 2px;
    }

    #sidebar-close {
        position: absolute;
        top: 16px;
        right: 16px;
        background: transparent;
        border: none;
        color: white;
        font-size: 28px;
        cursor: pointer;
        line-height: 1;
        opacity: 0.6;
    }

    #mobile-toggle {
        position: absolute;
        bottom: 24px;
        left: 50%;
        transform: translateX(-50%);
        z-index: 1000;
        background: var(--sb-bg);
        color: white;
        border: 1px solid var(--sb-border);
        padding: 12px 20px;
        border-radius: 30px;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
        display: flex;
        align-items: center;
        gap: 10px;
        font-weight: 600;
        font-size: 14px;
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
    }

    #mobile-toggle svg {
        opacity: 0.8;
    }

    /* Small Adjustments */
    .layer-toggles {
        grid-template-columns: 1fr 1fr;
    }

    .sidebar-logo {
        width: 100px;
    }

    #insights-panel {
        flex-grow: 0;
        /* Don't grow to fill space if parent scrolls */
        overflow-y: visible;
        /* Let it expand into parent scroll */
        padding-right: 0;
        height: auto;
    }

    .stats-grid {
        margin-bottom: 20px;
    }

    /* Ensure map isn't obscured by controls or browser UI */
    .leaflet-bottom.leaflet-left,
    .leaflet-bottom.leaflet-right {
        bottom: 90px;
        /* Stay clear of mobile toggle and browser bars */
    }

    .leaflet-bottom.leaflet-left {
        left: 10px;
    }

    .leaflet-bottom.leaflet-right {
        right: 10px;
    }

    .legend-container {
        display: flex;
        flex-direction: row;
        gap: 24px;
        align-items: flex-start;
    }

    .legend-section.donor-legend {
        border-left: 1px solid #e2e8f0;
        padding-left: 20px;
    }
}

/* Fix for very small devices */
@media (max-width: 400px) {
    .layer-btn {
        padding: 8px 6px;
        font-size: 11px;
    }

    .sidebar-header h1 {
        font-size: 18px;
    }
}