:root {
    --sidebar-width: 360px;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --school-color: #ef4444;
    --flat-color: #3b82f6;
    --bg: #f8fafc;
    --card-bg: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-secondary: #64748b;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    display: flex;
    height: 100vh;
    overflow: hidden;
    color: var(--text);
}

/* Sidebar */
#sidebar {
    width: var(--sidebar-width);
    background: var(--card-bg);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 20px;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
}

.sidebar-header h1 {
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 4px;
}

.sidebar-header .subtitle {
    font-size: 12px;
    opacity: 0.85;
}

.filters {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.filters h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.filter-group {
    margin-bottom: 12px;
}

.filter-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 4px;
    color: var(--text-secondary);
}

.filter-group select,
.filter-group input {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
    background: var(--bg);
    color: var(--text);
}

.range-inputs {
    display: flex;
    align-items: center;
    gap: 8px;
}

.range-inputs input {
    flex: 1;
}

.range-inputs span {
    color: var(--text-secondary);
}

.btn-primary, .btn-secondary {
    width: 100%;
    padding: 10px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-primary {
    background: var(--primary);
    color: white;
    margin-top: 8px;
}

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

.btn-secondary {
    background: var(--bg);
    color: var(--text-secondary);
    margin-top: 6px;
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: #e2e8f0;
}

/* Stats Panel */
.stats-panel {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.stats-panel h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.stat-row {
    display: flex;
    justify-content: space-between;
    padding: 4px 0;
    font-size: 13px;
}

.stat-label { color: var(--text-secondary); }
.stat-value { font-weight: 600; }

/* School List */
.school-list {
    padding: 16px 20px;
    flex: 1;
    overflow-y: auto;
}

.school-list h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.school-item {
    display: flex;
    align-items: center;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s;
    margin-bottom: 2px;
}

.school-item:hover {
    background: var(--bg);
}

.school-item.active {
    background: #eff6ff;
    border: 1px solid #bfdbfe;
}
/* School markers (map pins) */
.school-marker {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #dc2626;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 700;
    border: 2px solid #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
    cursor: pointer;
}

.school-marker.regular {
    width: 14px;
    height: 14px;
    background: #94a3b8;
    border: 2px solid #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    font-size: 0;
}

.school-marker.active-selected {
    width: 18px;
    height: 18px;
    background: #f59e0b;
    border: 2px solid #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
    font-size: 0;
}

/* Live mode - regular school dot in viewport */
.school-marker.live-regular {
    width: 16px;
    height: 16px;
    background: #f97316;
    border: 2px solid #fff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    font-size: 0;
}

/* Live mode toggle button */
.live-btn {
    display: block;
    padding: 6px 14px;
    border: 2px solid #475569;
    border-radius: 6px;
    background: #fff;
    color: #475569;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

.live-btn:hover {
    background: #f1f5f9;
}

.live-btn.active {
    background: #10b981;
    border-color: #10b981;
    color: #fff;
    box-shadow: 0 0 8px rgba(16, 185, 129, 0.4);
}

.live-mode-control {
    margin: 10px !important;
}

/* Live mode viewport counter */
.live-counter {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    background: rgba(15, 23, 42, 0.85);
    color: #e2e8f0;
    padding: 4px 14px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
    pointer-events: none;
    backdrop-filter: blur(4px);
}

/* School rank badge (sidebar list) */
.school-rank {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--school-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
    margin-right: 10px;
}

.school-info {
    flex: 1;
    min-width: 0;
}

.school-name {
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.school-tags {
    display: flex;
    gap: 4px;
    margin-top: 2px;
    flex-wrap: wrap;
}

.school-tag {
    font-size: 10px;
    padding: 1px 5px;
    border-radius: 3px;
    background: #fef2f2;
    color: #991b1b;
    font-weight: 500;
}

.school-flat-count {
    font-size: 11px;
    color: var(--text-secondary);
    margin-left: 8px;
    white-space: nowrap;
}

/* Map */
#map {
    flex: 1;
    height: 100vh;
}

/* Custom Leaflet markers */
.school-marker {
    background: var(--school-color);
    border: 2px solid white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 10px;
    font-weight: 700;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

.flat-marker {
    background: var(--flat-color);
    border: 2px solid white;
    border-radius: 4px;
    width: 16px;
    height: 16px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.2);
    cursor: pointer;
    transition: transform 0.2s;
}

.flat-marker:hover {
    transform: scale(1.3);
}

.flat-marker.highlighted {
    background: #f59e0b;
    width: 20px;
    height: 20px;
    z-index: 1000 !important;
}

/* Popup */
.leaflet-popup-content {
    min-width: 200px;
}

.popup-title {
    font-weight: 700;
    font-size: 14px;
    margin-bottom: 6px;
}

.popup-row {
    font-size: 12px;
    color: #64748b;
    margin-bottom: 3px;
}

.popup-price {
    font-size: 16px;
    font-weight: 700;
    color: var(--primary);
}

.popup-school-tag {
    display: inline-block;
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 4px;
    background: #fef2f2;
    color: #991b1b;
    margin: 2px 2px 2px 0;
}

/* Modal */
.modal {
    position: fixed;
    z-index: 10000;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background: white;
    border-radius: 12px;
    padding: 24px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    font-size: 24px;
    cursor: pointer;
    color: #94a3b8;
}

.modal-close:hover { color: #1e293b; }

.detail-title {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 16px;
}

.detail-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.detail-item {
    padding: 8px;
    background: #f8fafc;
    border-radius: 6px;
}

.detail-item .label {
    font-size: 11px;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.detail-item .value {
    font-size: 16px;
    font-weight: 600;
    margin-top: 2px;
}

.detail-schools {
    margin-top: 16px;
}

.detail-schools h4 {
    font-size: 14px;
    margin-bottom: 8px;
    color: #64748b;
}

.detail-school-item {
    display: flex;
    justify-content: space-between;
    padding: 6px 8px;
    background: #f8fafc;
    border-radius: 4px;
    margin-bottom: 4px;
    font-size: 13px;
}

.distance-badge {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 600;
}

.distance-badge.within-1km {
    background: #dcfce7;
    color: #166534;
}

.distance-badge.within-2km {
    background: #fef3c7;
    color: #92400e;
}

/* Loading overlay */
#loading {
    position: fixed;
    top: 0;
    left: var(--sidebar-width);
    right: 0;
    bottom: 0;
    background: rgba(255,255,255,0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    font-size: 16px;
    color: var(--primary);
    font-weight: 500;
}

/* Zoom hint for flats */
.flat-hint {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    background: rgba(15, 23, 42, 0.85);
    color: #e2e8f0;
    padding: 8px 18px;
    border-radius: 14px;
    font-size: 13px;
    font-weight: 500;
    pointer-events: none;
    backdrop-filter: blur(4px);
    white-space: nowrap;
}

/* P1 Registration Section */
.p1-registration {
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}

.p1-registration h3 {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

#p1-year-badge {
    display: inline-block;
    background: var(--primary);
    color: white;
    font-size: 11px;
    padding: 1px 6px;
    border-radius: 4px;
    vertical-align: middle;
}

.p1-summary-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin-bottom: 10px;
}

.p1-summary-card {
    padding: 8px;
    background: var(--bg);
    border-radius: 6px;
    text-align: center;
}

.p1-summary-card .p1-num {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
}

.p1-summary-card .p1-label {
    font-size: 10px;
    color: var(--text-secondary);
    text-transform: uppercase;
}

.p1-school-item {
    padding: 10px;
    border-radius: 6px;
    margin-bottom: 4px;
    background: var(--bg);
    cursor: pointer;
    transition: background 0.2s;
}

.p1-school-item:hover {
    background: #e2e8f0;
}

.p1-school-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
}

.p1-school-name {
    font-size: 13px;
    font-weight: 600;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.p1-ballot-badge {
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: 600;
    margin-left: 6px;
    white-space: nowrap;
}

.p1-ballot-badge.balloted {
    background: #fef2f2;
    color: #991b1b;
}

.p1-ballot-badge.no-ballot {
    background: #dcfce7;
    color: #166534;
}

.p1-phase-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
    font-size: 11px;
}

.p1-phase-label {
    width: 55px;
    font-weight: 600;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.p1-bar-container {
    flex: 1;
    height: 14px;
    background: #e2e8f0;
    border-radius: 3px;
    overflow: hidden;
    position: relative;
}

.p1-bar-vacancy {
    height: 100%;
    background: #93c5fd;
    border-radius: 3px;
    min-width: 2px;
}

.p1-bar-applicants {
    height: 100%;
    background: #fca5a5;
    border-radius: 3px;
    min-width: 2px;
    position: absolute;
    top: 0;
    left: 0;
}

.p1-bar-numbers {
    width: 65px;
    text-align: right;
    font-size: 10px;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.p1-expand-icon {
    font-size: 10px;
    color: var(--text-secondary);
    transition: transform 0.2s;
}

.p1-phases-detail {
    display: none;
}

.p1-school-item.expanded .p1-phases-detail {
    display: block;
}

.p1-school-item.expanded .p1-expand-icon {
    transform: rotate(90deg);
}

/* ─── Mobile sidebar toggle ─────────────────────────────────── */
.sidebar-toggle {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 10001;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: none;
    background: var(--primary);
    color: white;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
    transition: background 0.2s;
}
.sidebar-toggle:hover {
    background: var(--primary-dark);
}

@media (max-width: 768px) {
    .sidebar-toggle {
        display: block;
    }

    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        width: 85vw;
        max-width: 360px;
        height: 100vh;
        z-index: 10000;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        box-shadow: none;
    }

    #sidebar.sidebar-open {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0,0,0,0.2);
    }

    #sidebar.sidebar-open + #map ~ .sidebar-toggle,
    .sidebar-open ~ #map ~ .sidebar-toggle {
        /* no-op — toggle always visible */
    }

    /* Backdrop overlay when sidebar is open */
    #sidebar.sidebar-open::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.35);
        z-index: -1;
    }

    #map {
        width: 100vw;
        height: 100vh;
    }

    /* Move toggle to top-right area when sidebar is open */
    .sidebar-open ~ #map ~ .sidebar-toggle,
    #sidebar.sidebar-open ~ .sidebar-toggle {
        left: calc(85vw + 12px);
    }
    #sidebar.sidebar-open ~ #map ~ .sidebar-toggle,
    .sidebar-open ~ .sidebar-toggle {
        left: calc(85vw + 12px);
    }

    #loading {
        left: 0;
    }
}
