:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary: #64748b;
    --secondary-hover: #475569;
    --bg-glass: rgba(255, 255, 255, 0.85);
    --bg-glass-dark: rgba(15, 23, 42, 0.85);
    --border-glass: rgba(255, 255, 255, 0.3);
    --text-main: #1e293b;
    --text-muted: #64748b;
    --radius-lg: 16px;
    --radius-md: 8px;
    --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    --panel-width: 320px;
    --font-inter: 'Inter', system-ui, -apple-system, sans-serif;
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    font-family: var(--font-inter);
    color: var(--text-main);
    overflow: hidden;
}

#map {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 100%;
}

/* Glass Panels */
:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary: #64748b;
    --secondary-hover: #475569;
    --bg-glass: rgba(255, 255, 255, 0.85);
    --bg-glass-dark: rgba(15, 23, 42, 0.85);
    --border-glass: rgba(255, 255, 255, 0.3);
    --text-main: #1e293b;
    --text-muted: #64748b;
    --radius-lg: 16px;
    --radius-md: 8px;
    --shadow-lg: 0 10px 25px -5px rgba(0,0,0,0.1), 0 8px 10px -6px rgba(0,0,0,0.1);
    --panel-width: 320px;
    --collapsed-handle: 44px;
    --font-inter: 'Inter', system-ui, -apple-system, sans-serif;
}

.glass-panel {
    position: absolute;
    top: 20px;
    left: 20px;
    width: var(--panel-width);
    height: calc(100vh - 40px);
    background: var(--bg-glass);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--border-glass);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s;
    overflow: visible;
}

.glass-panel.collapsed {
    transform: translateX(calc(-100% - 20px));
}

.glass-panel.collapsed .panel-content,
.glass-panel.collapsed .header-text,
.glass-panel.collapsed .panel-footer {
    visibility: hidden;
    pointer-events: none;
}

.glass-panel.collapsed .panel-header,
.glass-panel.collapsed #configToggle {
    visibility: visible;
    pointer-events: auto;
}

.panel-header {
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    position: relative;
    padding-right: 64px;
}

.header-text {
    flex: 1;
}

.panel-header h2 {
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    letter-spacing: -0.025em;
}

#mapSubtitle {
    margin: 2px 0 0;
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 400;
}

.panel-content {
    flex: 1;
    overflow-y: auto;
    padding: 0 15px;
}

.panel-content.scrollable::-webkit-scrollbar {
    width: 4px;
}

.panel-content.scrollable::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.1);
    border-radius: 4px;
}

.panel-footer {
    padding: 12px 15px 15px;
    border-top: 1px solid rgba(0, 0, 0, 0.05);
}

/* Sections */
.config-section {
    padding: 12px 0;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.config-section:last-child {
    border-bottom: none;
}

.config-section h3 {
    margin: 0 0 15px;
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Form Controls */
.control-row {
    margin-bottom: 12px;
}

.control-row label {
    display: block;
    font-size: 0.85rem;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text-main);
}

select, input[type="text"], input[type="url"], input[type="range"] {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: var(--radius-md);
    background: white;
    font-family: inherit;
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s;
}

select:focus, input:focus {
    border-color: var(--primary);
}

input[type="range"] {
    padding: 0;
    height: 6px;
    background: #e2e8f0;
    border: none;
    appearance: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 16px;
    height: 16px;
    background: var(--primary);
    border-radius: 50%;
    cursor: pointer;
}

/* Tabs */
.tab-container {
    margin-bottom: 15px;
}

.tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 12px;
}

.tab-btn {
    flex: 1;
    padding: 6px;
    font-size: 0.8rem;
    font-weight: 500;
    border: 1px solid rgba(0,0,0,0.05);
    background: rgba(0,0,0,0.02);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
}

.tabs-small {
    display: flex;
    gap: 5px;
    margin-bottom: 15px;
    background: rgba(0,0,0,0.03);
    padding: 3px;
    border-radius: var(--radius-md);
}

.tab-btn-small {
    flex: 1;
    padding: 5px;
    font-size: 0.7rem;
    font-weight: 600;
    border: none;
    background: transparent;
    border-radius: calc(var(--radius-md) - 2px);
    cursor: pointer;
    color: var(--text-muted);
}

.tab-btn-small.active {
    background: white;
    color: var(--primary);
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.tab-btn-small:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

/* Drop Zone */
.drop-zone {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 24px;
    border: 2px dashed rgba(0,0,0,0.1);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s;
    background: rgba(0,0,0,0.01);
}

.drop-zone:hover {
    border-color: var(--primary);
    background: rgba(37, 99, 235, 0.05);
}

.drop-zone i {
    font-size: 1.5rem;
    color: var(--primary);
    margin-bottom: 10px;
}

.drop-zone span {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.file-input {
    display: none;
}

.input-group {
    display: flex;
    gap: 8px;
}

.small-btn {
    padding: 8px 12px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85rem;
}

.help-text {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 8px;
}

.help-text a {
    color: var(--primary);
    text-decoration: none;
}

/* Buttons */
.primary-btn {
    width: 100%;
    padding: 12px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

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

.secondary-btn {
    flex: 1;
    padding: 10px;
    background: white;
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: var(--radius-md);
    font-weight: 500;
    font-size: 0.85rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.secondary-btn:hover {
    background: rgba(0,0,0,0.02);
}

.button-group {
    display: flex;
    gap: 10px;
}

.icon-button {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 8px;
    color: var(--text-muted);
    transition: color 0.2s;
}

.icon-button:hover {
    color: var(--text-main);
}

/* Desktop sidebar handle: keep toggle reachable while panel is fully off-canvas */
#configPanel #configToggle {
    position: absolute;
    top: 50%;
    right: -40px;
    transform: translateY(-50%);
    width: 36px;
    height: 36px;
    border-radius: 999px;
    background: var(--bg-glass);
    border: 1px solid rgba(0, 0, 0, 0.08);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    z-index: 2;
}

#configPanel #configToggle:hover {
    background: rgba(255, 255, 255, 0.95);
}

/* Legend (Bottom Right) */
.legend-container {
    position: absolute;
    bottom: 30px;
    right: 30px;
    background: transparent;
    padding: 0;
    z-index: 900;
    transition: all 0.3s;
    pointer-events: none; /* Let clicks pass through to map if not on a cell */
}

.legend-container * {
    pointer-events: auto;
}

.legend-header-minimal {
    display: none; /* Removed as per user request */
}

.legend-title {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.icon-button-small {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 4px;
    color: var(--text-muted);
    font-size: 0.7rem;
}

.legend-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    padding-top: 35px;
}

.bivariate-grid-wrapper {
    position: relative;
    padding: 20px;
}

.bivariate-legend {
    display: grid;
    /* Grid columns set dynamically by JS */
    gap: 2px;
    width: 100px;
    height: 100px;
    transform: rotate(-45deg);
}

.legend-cell {
    width: 100%;
    height: 100%;
    cursor: pointer;
    transition: transform 0.2s;
}

.legend-cell:hover, .legend-cell-active {
    transform: scale(1.15);
    z-index: 10;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.legend-cell-active {
    outline: 2px solid var(--text-main);
}

.legend-cell-hover {
    outline: 2px solid var(--primary);
    transform: scale(1.15);
    z-index: 20;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
}

.axis-label-x, .axis-label-y {
    font-size: 0.65rem;
    font-weight: 700;
    color: var(--text-main);
    text-shadow: 0 0 4px white, 0 0 8px white;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    pointer-events: none;
    position: absolute;
    width: 120px;
    text-align: center;
}

/* Y Axis along the top-left edge */
.axis-label-y {
    left: -40px;
    top: 50px;
    transform: rotate(-45deg);
}

/* X Axis along the top-right edge */
.axis-label-x {
    right: -40px;
    top: 50px;
    transform: rotate(45deg);
}

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

.info-badge {
    padding: 4px 8px;
    background: rgba(37, 99, 235, 0.1);
    color: var(--primary);
    font-size: 0.75rem;
    font-weight: 500;
    border-radius: 4px;
    margin-top: 8px;
    display: inline-block;
}

.file-status {
    margin-top: 10px;
    font-size: 0.75rem;
}

.file-status.success { color: #16a34a; }
.file-status.error { color: #dc2626; }
.file-status.loading { color: var(--primary); }

/* GitHub Banner */
#githubBanner {
    position: fixed;
    top: 14px;
    right: 14px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(0, 0, 0, 0.72);
    color: white;
    border-radius: 999px;
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 600;
    z-index: 2100;
    box-shadow: 0 6px 18px rgba(0,0,0,0.35);
    transition: transform 0.2s ease, opacity 0.2s ease;
}

#githubBanner:hover {
    transform: translateY(-2px);
    opacity: 0.95;
}

#githubBanner i {
    font-size: 1rem;
}

/* Toast */
.toast-container {
    position: fixed;
    top: 24px;
    right: 24px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.toast {
    background: white;
    padding: 12px 20px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 12px;
    border-left: 4px solid var(--primary);
    min-width: 250px;
}

.toast-error { border-left-color: #dc2626; }
.toast-success { border-left-color: #16a34a; }

/* Popups */
.maplibregl-popup-content {
    border-radius: var(--radius-md) !important;
    padding: 15px !important;
    box-shadow: var(--shadow-lg) !important;
}

.popup-title {
    font-weight: 700;
    margin-bottom: 10px;
    font-size: 0.95rem;
}

.popup-variable {
    font-size: 0.85rem;
    margin-bottom: 4px;
}

.popup-variable strong {
    color: var(--primary);
}

/* Responsive design for tablets and mobile */
@media (max-width: 768px) {
    :root {
        --panel-width: 280px;
    }

    #githubBanner {
        display: none !important;
    }

    .glass-panel {
        top: 10px;
        left: 10px;
        height: auto;
        max-height: calc(100vh - 80px); /* Leave room for legend */
        transition: transform 0.3s ease;
        overflow: hidden;
    }

    .glass-panel.collapsed {
        transform: translateY(calc(-100% + 90px));
        left: 10px;
        right: 10px;
        width: auto;
    }

    .glass-panel.collapsed .panel-content {
        visibility: hidden;
        pointer-events: none;
    }

    /* Keep the toggle area visible and operable on small collapsed mobile panels */
    .glass-panel.collapsed .panel-header {
        padding-bottom: 0;
    }

    .glass-panel.collapsed #configToggle {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        z-index: 11;
    }

    /* Move toggle to the bottom of the header on mobile to ensure it's visible when collapsed */
    .panel-header {
        position: relative;
        padding-bottom: 40px;
    }

    #configPanel #configToggle {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        top: auto;
        transform: none;
        width: 100%;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0,0,0,0.02);
        border: none;
        border-top: 1px solid rgba(0,0,0,0.05);
        border-radius: 0 0 var(--radius-lg) var(--radius-lg);
        box-shadow: none;
    }

    #configToggle i {
        transform: rotate(90deg);
        transition: transform 0.3s ease;
    }

    .glass-panel.collapsed #configToggle i {
        transform: rotate(-90deg);
    }

    .legend-container {
        bottom: 10px;
        right: 10px;
        transform: scale(0.85);
        transform-origin: bottom right;
    }
}

@media (max-width: 480px) {
    .glass-panel {
        left: 8px;
        right: 8px;
        width: auto;
    }

    .panel-header {
        padding: 10px 15px;
    }

    .panel-header h2 {
        font-size: 0.9rem;
    }

    #mapSubtitle {
        font-size: 0.65rem;
    }

    .legend-container {
        bottom: 5px;
        right: 5px;
        transform: scale(0.75);
    }
}

/* Custom Color Picker UI */
.custom-palette-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-top: 10px;
}

.color-picker-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.color-picker-item label {
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--text-muted);
}

.color-picker-item input[type="color"] {
    width: 100%;
    height: 32px;
    padding: 0;
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: 4px;
    cursor: pointer;
    background: none;
}
