:root {
    --primary-h: 220;
    --primary-s: 90%;
    --primary-l: 60%;
    --primary: hsl(var(--primary-h), var(--primary-s), var(--primary-l));
    
    --bg-h: 225;
    --bg-s: 20%;
    --bg-l: 8%;
    --bg: hsl(var(--bg-h), var(--bg-s), var(--bg-l));
    
    --surface-h: 225;
    --surface-s: 15%;
    --surface-l: 15%;
    --surface: hsl(var(--surface-h), var(--surface-s), var(--surface-l));
    
    --text: #f0f0f0;
    --text-dim: #a0a0a0;
    
    --glass: rgba(30, 35, 45, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    
    --accent: #58a6ff;
    --accent-glow: rgba(88, 166, 255, 0.15);
    --danger: #ff7b72;
    --success: #3fb950;
    --warning: #d29922;
    --purple: #bc8cff;
    
    --sidebar-width: 380px;
    --header-height: 70px;

    /* Category colors */
    --color-bc: #4fc3f7;
    --color-sc: #ff8a65;
    --color-st: #81c784;
    --color-oc: #ba68c8;
}

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

body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background-color: var(--bg);
    color: var(--text);
    overflow: hidden; /* Prevent body scroll */
    height: 100vh;
}

h1, h2, h3 {
    font-family: 'Outfit', sans-serif;
    font-weight: 600;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}
::-webkit-scrollbar-track {
    background: transparent;
}
::-webkit-scrollbar-thumb {
    background: var(--glass-border);
    border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.2);
}

.glass {
    background: var(--glass);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
}

/* ============================== */
/* Header & Tab Navigation        */
/* ============================== */
.app-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    z-index: 1000;
    position: relative;
    gap: 1.5rem;
}

.app-header h1 {
    font-size: 1.35rem;
    background: linear-gradient(90deg, #fff, var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    white-space: nowrap;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
    font-size: 0.85rem;
    color: var(--text-dim);
    white-space: nowrap;
}

/* Tab Navigation */
.tab-navigation {
    display: flex;
    gap: 4px;
    background: rgba(0, 0, 0, 0.25);
    padding: 4px;
    border-radius: 12px;
    border: 1px solid var(--glass-border);
}

.tab-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 18px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    font-family: 'Inter', sans-serif;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
}

.tab-btn:hover {
    color: var(--text);
    background: rgba(255, 255, 255, 0.05);
}

.tab-btn.active {
    color: #fff;
    background: linear-gradient(135deg, rgba(88, 166, 255, 0.25), rgba(88, 166, 255, 0.1));
    box-shadow: 0 0 12px var(--accent-glow), inset 0 1px 0 rgba(255,255,255,0.1);
}

.tab-btn svg {
    flex-shrink: 0;
}

/* ============================== */
/* Layout                         */
/* ============================== */
.app-container {
    height: calc(100vh - var(--header-height));
    position: relative;
}

.tab-content {
    display: none;
    height: 100%;
}

.tab-content.active {
    display: flex;
}

/* Map tab keeps flex row */
#map-tab {
    flex-direction: row;
}

/* Charts / Analysis tabs — scrollable dashboard */
#charts-tab,
#analysis-tab {
    display: none;
    overflow: hidden;
}

#charts-tab.active,
#analysis-tab.active {
    display: block;
    overflow-y: auto;
}

/* ============================== */
/* Charts Dashboard Grid          */
/* ============================== */
.charts-dashboard {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    padding: 24px;
    max-width: 1600px;
    margin: 0 auto;
}

.chart-card {
    border-radius: 16px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: box-shadow 0.3s ease;
}

.chart-card:hover {
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(88, 166, 255, 0.1);
}

.chart-card.full-width {
    grid-column: 1 / -1;
}

.chart-header {
    padding: 20px 24px 0;
}

.chart-header h3 {
    font-size: 1.1rem;
    color: var(--text);
    margin-bottom: 2px;
}

.chart-subtitle {
    font-size: 0.8rem;
    color: var(--text-dim);
    margin-bottom: 0;
}

.chart-header-with-select {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
}

.chart-container {
    width: 100%;
    height: 400px;
    padding: 8px;
}

.chart-container.tall {
    height: 520px;
}

.chart-container.extra-tall {
    height: 700px;
}

/* Chart Select Dropdown */
.chart-select {
    padding: 6px 12px;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    color: var(--text);
    font-family: 'Inter', sans-serif;
    font-size: 0.82rem;
    cursor: pointer;
    outline: none;
    min-width: 140px;
    transition: border-color 0.2s;
}

.chart-select:focus {
    border-color: var(--accent);
}

.chart-select option {
    background: #1e232d;
    color: var(--text);
}

/* Comparison selects */
.comparison-selects {
    display: flex;
    align-items: center;
    gap: 10px;
}

.vs-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

/* ============================== */
/* Sidebar (Map Tab)              */
/* ============================== */
.sidebar {
    width: var(--sidebar-width);
    height: 100%;
    z-index: 999;
    display: flex;
    flex-direction: column;
}

.sidebar-content {
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.sidebar h2 { font-size: 1.15rem; margin-bottom: 0.75rem; }

/* Category Chips */
.category-chips { display: flex; gap: 6px; margin-bottom: 12px; }
.cat-chip {
    flex: 1; display: flex; align-items: center; justify-content: center; gap: 5px;
    padding: 7px 6px; border: 1px solid var(--glass-border); border-radius: 8px;
    background: rgba(0,0,0,0.2); color: var(--text-dim); font-family: 'Inter', sans-serif;
    font-size: 0.78rem; font-weight: 600; cursor: pointer; transition: all 0.25s;
}
.cat-chip:hover { background: rgba(255,255,255,0.05); color: var(--text); }
.cat-chip.active { border-color: currentColor; background: rgba(255,255,255,0.08); color: var(--text); box-shadow: 0 0 12px rgba(88,166,255,0.15); }
.cat-chip.active[data-cat="BC"] { color: var(--color-bc); border-color: var(--color-bc); }
.cat-chip.active[data-cat="SC"] { color: var(--color-sc); border-color: var(--color-sc); }
.cat-chip.active[data-cat="ST"] { color: var(--color-st); border-color: var(--color-st); }
.cat-chip.active[data-cat="OC"] { color: var(--color-oc); border-color: var(--color-oc); }
.cat-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.cat-chip-count { font-size: 0.65rem; opacity: 0.6; margin-left: 2px; }

/* Search Box */
.search-box { position: relative; margin-bottom: 8px; }
.search-box input {
    width: 100%; padding: 0.65rem 1rem 0.65rem 2.3rem; background: rgba(0,0,0,0.2);
    border: 1px solid var(--glass-border); border-radius: 8px; color: white;
    font-family: inherit; font-size: 0.85rem; transition: all 0.2s;
}
.search-box input:focus { outline: none; border-color: var(--accent); background: rgba(0,0,0,0.3); }
.search-icon { position: absolute; left: 0.75rem; top: 12px; width: 1rem; height: 1rem; color: var(--text-dim); pointer-events: none; }

/* Autocomplete Dropdown */
.autocomplete-dropdown {
    position: absolute; top: 100%; left: 0; right: 0; z-index: 1050;
    background: rgba(22, 27, 36, 0.97); backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border); border-top: none; border-radius: 0 0 8px 8px;
    max-height: 220px; overflow-y: auto; box-shadow: 0 12px 36px rgba(0,0,0,0.5);
}
.autocomplete-item {
    padding: 8px 12px; cursor: pointer; display: flex; align-items: center; gap: 8px;
    font-size: 0.82rem; transition: background 0.15s; border-bottom: 1px solid rgba(255,255,255,0.03);
}
.autocomplete-item:hover { background: rgba(88,166,255,0.1); }
.autocomplete-item .ac-cat { font-size: 0.65rem; padding: 1px 5px; border-radius: 3px; font-weight: 600; flex-shrink: 0; }
.autocomplete-item .ac-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Selection Tags Bar */
.selection-tags-bar { margin-bottom: 8px; }
.tags-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.tags-count { font-size: 0.75rem; color: var(--text-dim); font-weight: 500; }
.tags-scroll { display: flex; flex-wrap: wrap; gap: 4px; max-height: 56px; overflow-y: auto; }
.selection-tag {
    display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px 2px 6px;
    border-radius: 12px; font-size: 0.7rem; background: rgba(88,166,255,0.12);
    border: 1px solid rgba(88,166,255,0.2); color: var(--text); animation: tagIn 0.2s ease;
}
@keyframes tagIn { from { opacity: 0; transform: scale(0.85); } to { opacity: 1; transform: scale(1); } }
.selection-tag .tag-x { cursor: pointer; opacity: 0.5; font-size: 0.65rem; padding: 0 2px; transition: opacity 0.15s; }
.selection-tag .tag-x:hover { opacity: 1; }

.text-btn { background: none; border: none; color: var(--accent); font-size: 0.75rem; cursor: pointer; font-weight: 500; transition: opacity 0.2s; }
.text-btn:hover { opacity: 0.8; text-decoration: underline; }

/* Tree Structure */
.tree-container { flex: 1; overflow-y: auto; padding-right: 0.5rem; }
.tree-node { margin-bottom: 2px; }
.tree-header {
    display: flex; align-items: center; padding: 0.35rem 0.5rem; border-radius: 6px;
    cursor: pointer; transition: background 0.2s;
}
.tree-header:hover { background: rgba(255, 255, 255, 0.05); }
.tree-header.selected-highlight { background: rgba(88,166,255,0.08); }
.tree-toggle { width: 14px; height: 14px; margin-right: 6px; display: flex; align-items: center; justify-content: center; transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1); user-select: none; font-size: 0.65rem; }
.tree-node.expanded > .tree-header .tree-toggle { transform: rotate(90deg); }
.tree-checkbox { margin-right: 8px; cursor: pointer; width: 15px; height: 15px; accent-color: var(--accent); flex-shrink: 0; }
.tree-label { flex: 1; font-size: 0.85rem; user-select: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tree-pop-count { font-size: 0.65rem; color: var(--text-dim); margin-left: auto; padding-left: 6px; flex-shrink: 0; font-variant-numeric: tabular-nums; }
.tree-cat-dot { width: 6px; height: 6px; border-radius: 50%; margin-right: 6px; flex-shrink: 0; }
.tree-children { display: none; padding-left: 20px; border-left: 1px solid rgba(255,255,255,0.06); margin-left: 12px; }
.tree-node.expanded > .tree-children { display: block; animation: fadeSlide 0.2s ease; }
@keyframes fadeSlide { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }

/* ============================== */
/* Map View                       */
/* ============================== */
.map-view { flex: 1; position: relative; }
#map { width: 100%; height: 100%; background: var(--bg); }

/* Selection Title Overlay */
.map-selection-title {
    position: absolute; top: 16px; right: 20px;
    z-index: 1000; font-family: 'Outfit', sans-serif; font-size: 1.5rem; font-weight: 700;
    color: #fff; text-align: right; pointer-events: none; opacity: 0.4;
    text-shadow: 0 2px 12px rgba(0,0,0,0.7), 0 0 30px rgba(88,166,255,0.3);
    max-width: 60%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    transition: opacity 0.3s;
}

/* Map Toolbar */
.map-toolbar { position: absolute; top: 12px; left: 12px; z-index: 1000; display: flex; flex-direction: column; gap: 2px; border-radius: 10px; padding: 4px; }
.map-tool-btn { width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; border: none; background: transparent; color: var(--text-dim); cursor: pointer; border-radius: 8px; transition: all 0.2s; }
.map-tool-btn:hover { color: var(--text); background: rgba(255,255,255,0.08); }
.map-tool-btn.active { color: var(--accent); background: rgba(88,166,255,0.1); }

/* Gradient Legend */
.legend-panel { position: absolute; bottom: 60px; right: 16px; z-index: 1000; padding: 14px 16px; border-radius: 12px; min-width: 200px; }
.legend-title { font-size: 0.72rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600; margin-bottom: 10px; }
.legend-gradient { height: 12px; border-radius: 6px; margin-bottom: 6px; }
.legend-labels { display: flex; justify-content: space-between; font-size: 0.7rem; color: var(--text-dim); }

/* Rich Hover Panel */
.hover-panel { position: absolute; top: 16px; left: 60px; right: auto; z-index: 1001; padding: 16px 20px; border-radius: 14px; min-width: 280px; max-width: 340px; pointer-events: none; transition: opacity 0.2s; }
.hover-district-name { font-family: 'Outfit', sans-serif; font-size: 1.15rem; font-weight: 700; color: var(--accent); margin-bottom: 10px; padding-bottom: 8px; border-bottom: 1px solid var(--glass-border); }
.hover-body { font-size: 0.82rem; }
.hover-row { display: flex; justify-content: space-between; align-items: center; padding: 3px 0; }
.hover-row .label { color: var(--text-dim); }
.hover-row .value { font-weight: 600; font-variant-numeric: tabular-nums; }
.hover-mini-bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; margin: 10px 0 6px; gap: 1px; }
.hover-mini-bar > div { height: 100%; transition: width 0.3s ease; }
.hover-mini-legend { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 2px; }
.hover-mini-legend span { display: flex; align-items: center; gap: 3px; font-size: 0.68rem; color: var(--text-dim); }
.hover-mini-legend .dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.hover-rank { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--glass-border); font-size: 0.78rem; color: var(--accent); font-weight: 600; }

/* Bottom Summary Bar */
.bottom-summary { position: absolute; bottom: 0; left: 0; right: 0; z-index: 1000; display: flex; align-items: center; padding: 10px 20px; gap: 12px; font-size: 0.8rem; border-top: 1px solid var(--glass-border); border-radius: 0; min-height: 44px; }
.summary-item { display: flex; align-items: center; gap: 5px; white-space: nowrap; }
.summary-icon { font-size: 0.9rem; }
.summary-divider { width: 1px; height: 20px; background: var(--glass-border); }
.summary-flex { flex: 1; }
.mini-rank-bar { display: flex; gap: 2px; align-items: flex-end; height: 24px; }
.mini-rank-col { width: 6px; border-radius: 2px 2px 0 0; transition: height 0.4s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s; cursor: pointer; position: relative; }
.mini-rank-col:hover { opacity: 0.8; }

/* Detail Drawer */
.detail-drawer { position: absolute; top: 0; right: 0; width: 360px; height: 100%; z-index: 1002; transform: translateX(100%); transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1); display: flex; flex-direction: column; overflow-y: auto; padding: 20px; border-left: 1px solid var(--glass-border); }
.detail-drawer.open { transform: translateX(0); }
.drawer-close { position: absolute; top: 14px; right: 14px; width: 30px; height: 30px; border: none; background: rgba(255,255,255,0.06); color: var(--text-dim); border-radius: 50%; cursor: pointer; font-size: 0.9rem; display: flex; align-items: center; justify-content: center; transition: all 0.2s; z-index: 1; }
.drawer-close:hover { background: rgba(255,255,255,0.12); color: var(--text); }
.drawer-header { margin-bottom: 16px; padding-right: 36px; }
.drawer-header h3 { font-size: 1.25rem; color: var(--accent); margin-bottom: 4px; }
.drawer-total { font-size: 0.8rem; color: var(--text-dim); }
.drawer-section { margin-bottom: 18px; }
.drawer-section h4 { font-size: 0.78rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 10px; font-weight: 600; }
.drawer-donut { width: 100%; height: 180px; }
.drawer-table-wrap { max-height: 220px; overflow-y: auto; }
.drawer-table { width: 100%; border-collapse: collapse; font-size: 0.78rem; }
.drawer-table th { text-align: left; color: var(--text-dim); font-weight: 600; padding: 5px 8px; border-bottom: 1px solid var(--glass-border); font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; }
.drawer-table td { padding: 5px 8px; border-bottom: 1px solid rgba(255,255,255,0.03); }
.drawer-table tr:hover td { background: rgba(255,255,255,0.03); }
.drawer-table .pop-bar { height: 4px; border-radius: 2px; background: var(--accent); display: inline-block; vertical-align: middle; margin-right: 6px; transition: width 0.3s; }

/* District Labels */
.district-label { background: transparent !important; border: none !important; box-shadow: none !important; font-size: 9px !important; font-weight: 600 !important; color: rgba(255,255,255,0.5) !important; text-align: center !important; white-space: nowrap !important; pointer-events: none !important; text-shadow: 0 1px 3px rgba(0,0,0,0.7) !important; }

/* Utility */
.hidden { display: none !important; }
.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,0.1); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Leaflet Overrides */
.leaflet-container { font-family: inherit !important; }
.leaflet-bar { border: none !important; box-shadow: 0 4px 15px rgba(0,0,0,0.3) !important; }
.leaflet-bar a { background: var(--glass) !important; backdrop-filter: blur(10px) !important; color: var(--text) !important; border-bottom: 1px solid var(--glass-border) !important; }
.leaflet-bar a:hover { background: rgba(255, 255, 255, 0.1) !important; }
.custom-tooltip { background: var(--glass) !important; backdrop-filter: blur(10px) !important; border: 1px solid var(--glass-border) !important; color: var(--text) !important; border-radius: 8px !important; box-shadow: 0 8px 32px rgba(0,0,0,0.5) !important; padding: 8px 12px !important; font-size: 0.9rem !important; }
.tooltip-district { font-weight: 700; font-family: 'Outfit', sans-serif; color: var(--accent); margin-bottom: 4px; border-bottom: 1px solid var(--glass-border); padding-bottom: 4px; }
.tooltip-val { display: flex; justify-content: space-between; gap: 12px; margin-top: 4px; }
.tooltip-label { color: var(--text-dim); }

/* Responsive */
@media (max-width: 1100px) {
    .charts-dashboard { grid-template-columns: 1fr; }
    .chart-card.full-width { grid-column: 1; }
    .detail-drawer { width: 300px; }
}
@media (max-width: 768px) {
    .app-header { padding: 0 1rem; gap: 0.5rem; }
    .app-header h1 { font-size: 1rem; }
    .tab-btn { padding: 6px 10px; font-size: 0.75rem; }
    .tab-btn svg { display: none; }
    .sidebar { width: 280px; }
    .charts-dashboard { padding: 12px; gap: 12px; }
    .chart-container { height: 320px; }
    .chart-container.tall { height: 400px; }
    .chart-container.extra-tall { height: 500px; }
    .detail-drawer { width: 100%; }
    .bottom-summary { font-size: 0.7rem; padding: 8px 12px; }
}

