/* =========================================================
   Tokens
   ========================================================= */
:root {
    --bg:        #0f172a;
    --surface:   rgba(15, 23, 42, 0.72);
    --glass:     rgba(15, 23, 42, 0.60);
    --border:    rgba(148, 163, 184, 0.12);
    --text:      #e2e8f0;
    --text-dim:  #94a3b8;
    --accent:    #3b82f6;
    --accent2:   #f43f5e;
    --radius:    12px;
    --radius-sm: 8px;
    --blur:      20px;
    --font:      "Inter", "Noto Sans JP", system-ui, sans-serif;
}

/* =========================================================
   Reset
   ========================================================= */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); overflow: hidden; }

/* =========================================================
   Glass mixin via class
   ========================================================= */
.glass, #top-bar, #legend, #timeline, #panel, #tooltip {
    background: var(--glass);
    backdrop-filter: blur(var(--blur));
    -webkit-backdrop-filter: blur(var(--blur));
    border: 1px solid var(--border);
}

/* =========================================================
   Map (full screen)
   ========================================================= */
#map {
    position: fixed;
    inset: 0;
    z-index: 0;
}

/* =========================================================
   Top bar
   ========================================================= */
#top-bar {
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 1000;
    border-radius: var(--radius);
    padding: 10px 14px;
    display: flex;
    align-items: center;
    gap: 14px;
}
#logo {
    font-size: 17px;
    font-weight: 700;
    color: var(--accent2);
    letter-spacing: -0.5px;
    white-space: nowrap;
}
#controls {
    display: flex;
    gap: 8px;
    align-items: center;
}

/* Segmented control */
.seg-group {
    display: flex;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 8px;
    padding: 2px;
}
.seg {
    padding: 5px 12px;
    border: none;
    background: transparent;
    color: var(--text-dim);
    font-size: 12px;
    font-weight: 500;
    font-family: var(--font);
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s ease;
    white-space: nowrap;
}
.seg:hover { color: var(--text); }
.seg.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 1px 4px rgba(59, 130, 246, 0.4);
}
#unit-toggle .seg.active {
    background: rgba(255, 255, 255, 0.12);
    color: #fff;
    box-shadow: none;
}

/* =========================================================
   Legend
   ========================================================= */
#legend {
    position: fixed;
    bottom: 72px;
    left: 12px;
    z-index: 800;
    border-radius: var(--radius-sm);
    padding: 8px 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    color: var(--text-dim);
}
#legend-gradient {
    width: 120px;
    height: 6px;
    border-radius: 3px;
    background: linear-gradient(to right, #1e40af, #3b82f6, #fbbf24, #f43f5e);
}
#legend-title {
    font-size: 10px;
    opacity: 0.6;
}

/* =========================================================
   Timeline
   ========================================================= */
#timeline {
    position: fixed;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    border-radius: 24px;
    padding: 6px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 360px;
    max-width: 560px;
    width: 50vw;
}
#play-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: var(--accent);
    color: #fff;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
}
#play-btn:hover { background: #2563eb; transform: scale(1.05); }
#play-btn.playing { background: var(--accent2); }
#play-btn.playing #play-icon { display: none; }
#play-btn.playing #pause-icon { display: block; }
#play-btn:not(.playing) #play-icon { display: block; }
#play-btn:not(.playing) #pause-icon { display: none; }

#timeline-slider {
    flex: 1;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: rgba(255,255,255,0.1);
    border-radius: 2px;
    outline: none;
    cursor: pointer;
}
#timeline-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid #fff;
    cursor: pointer;
    box-shadow: 0 0 6px rgba(59, 130, 246, 0.5);
}
#timeline-slider::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid #fff;
    cursor: pointer;
}
#timeline-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    min-width: 100px;
    text-align: center;
}

/* =========================================================
   Panel
   ========================================================= */
#panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 380px;
    z-index: 1100;
    border-radius: 0;
    border-left: 1px solid var(--border);
    border-top: none;
    border-bottom: none;
    border-right: none;
    display: flex;
    flex-direction: column;
    transform: translateX(0);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
}
#panel.hidden { transform: translateX(100%); pointer-events: none; }
#panel-drag-handle { display: none; }

#panel-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    padding: 20px 20px 12px;
    flex-shrink: 0;
}
#ward-name {
    font-size: 20px;
    font-weight: 700;
    color: #fff;
    letter-spacing: -0.3px;
}
#back-to-wards {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-top: 6px;
    background: none;
    border: none;
    color: var(--accent);
    font-size: 12px;
    font-family: var(--font);
    cursor: pointer;
    padding: 0;
}
#back-to-wards:hover { text-decoration: underline; }
#back-to-wards.hidden { display: none; }
#panel-close {
    background: rgba(255,255,255,0.06);
    border: none;
    color: var(--text-dim);
    width: 30px;
    height: 30px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.15s;
}
#panel-close:hover { background: rgba(255,255,255,0.12); color: #fff; }

#panel-content {
    flex: 1;
    overflow-y: auto;
    padding: 0 20px 20px;
}

/* Summary cards */
#ward-summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 16px;
}
.stat-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 12px;
}
.stat-card .label {
    font-size: 11px;
    color: var(--text-dim);
    margin-bottom: 4px;
    font-weight: 500;
}
.stat-card .value {
    font-size: 17px;
    font-weight: 700;
    color: var(--accent);
}
.stat-card.highlight .value { color: var(--accent2); }

/* Type bars */
#ward-types { margin-bottom: 16px; }
.type-bar-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    font-size: 12px;
}
.type-bar-label { width: 80px; color: var(--text-dim); text-align: right; font-weight: 500; }
.type-bar-wrap {
    flex: 1;
    height: 6px;
    background: rgba(255,255,255,0.06);
    border-radius: 3px;
    overflow: hidden;
}
.type-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.4s ease;
}
.type-bar-count { width: 50px; text-align: right; color: var(--text-dim); font-weight: 500; }

/* District section */
#district-section {
    border-top: 1px solid var(--border);
    padding-top: 14px;
}
#district-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}
#district-header h3 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-dim);
}
#district-sort {
    padding: 4px 8px;
    background: rgba(255,255,255,0.06);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 6px;
    font-size: 11px;
    font-family: var(--font);
    cursor: pointer;
}

#district-list { display: flex; flex-direction: column; gap: 6px; }
.district-item {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    cursor: default;
    transition: all 0.15s;
}
.district-item:hover {
    background: rgba(255, 255, 255, 0.07);
    border-color: var(--accent);
}
.district-name {
    font-size: 13px;
    font-weight: 600;
    color: #fff;
    margin-bottom: 4px;
}
.district-stats {
    display: flex;
    gap: 14px;
    font-size: 11px;
    color: var(--text-dim);
}
.district-stats span { white-space: nowrap; }
.district-stats .val { color: var(--accent); font-weight: 600; }

/* =========================================================
   Tooltip
   ========================================================= */
#tooltip {
    position: fixed;
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    pointer-events: none;
    z-index: 2000;
    font-size: 12px;
    max-width: 220px;
}
#tooltip.hidden { display: none; }
#tooltip .tt-name { font-weight: 600; color: #fff; margin-bottom: 3px; font-size: 13px; }
#tooltip .tt-row { color: var(--text-dim); line-height: 1.6; }
#tooltip .tt-val { color: var(--accent); font-weight: 600; }

/* =========================================================
   Leaflet overrides
   ========================================================= */
.leaflet-control-zoom a {
    background: var(--glass) !important;
    backdrop-filter: blur(var(--blur)) !important;
    -webkit-backdrop-filter: blur(var(--blur)) !important;
    color: var(--text) !important;
    border-color: var(--border) !important;
    width: 32px !important;
    height: 32px !important;
    line-height: 32px !important;
    font-size: 16px !important;
}
.leaflet-control-zoom { border: none !important; border-radius: var(--radius-sm) !important; overflow: hidden; }
.leaflet-control-attribution {
    background: rgba(15, 23, 42, 0.6) !important;
    backdrop-filter: blur(10px) !important;
    color: #475569 !important;
    font-size: 10px !important;
    border-radius: 4px 0 0 0 !important;
}
.leaflet-control-attribution a { color: #64748b !important; }

/* scrollbar */
#panel-content::-webkit-scrollbar { width: 4px; }
#panel-content::-webkit-scrollbar-track { background: transparent; }
#panel-content::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.1); border-radius: 2px; }

/* =========================================================
   Mobile (max-width: 768px)
   ========================================================= */
@media (max-width: 768px) {
    #top-bar {
        top: 8px;
        left: 8px;
        right: 8px;
        padding: 8px 10px;
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }
    #logo { font-size: 15px; }
    #controls {
        width: 100%;
        gap: 6px;
        flex-wrap: wrap;
    }
    .seg { padding: 4px 9px; font-size: 11px; }

    /* Panel → bottom sheet */
    #panel {
        top: auto;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        max-height: 50vh;
        border-radius: var(--radius) var(--radius) 0 0;
        border-left: none;
        border-top: 1px solid var(--border);
        transform: translateY(0);
    }
    #panel.hidden { transform: translateY(100%); }
    #panel-drag-handle {
        display: block;
        width: 36px;
        height: 4px;
        background: rgba(255,255,255,0.15);
        border-radius: 2px;
        margin: 8px auto 0;
    }
    #panel-header { padding: 12px 16px 8px; }
    #panel-content { padding: 0 16px 16px; }
    #ward-name { font-size: 17px; }

    /* Timeline */
    #timeline {
        min-width: unset;
        max-width: unset;
        width: auto;
        left: 8px;
        right: 8px;
        transform: none;
        bottom: 8px;
        padding: 6px 12px;
        gap: 8px;
    }
    #play-btn { width: 28px; height: 28px; }
    #timeline-label { font-size: 11px; min-width: 80px; }

    /* Legend */
    #legend { bottom: 56px; left: 8px; padding: 6px 8px; }

    /* Tooltip disabled on mobile */
    #tooltip { display: none !important; }
}
