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

:root {
    --bg-primary: #0f1117;
    --bg-secondary: #1a1d27;
    --bg-tertiary: #242836;
    --text-primary: #e4e6f0;
    --text-secondary: #9398a8;
    --accent: #4da6ff;
    --accent-dim: #2d6aa0;
    --danger: #ff6b6b;
    --gradient-cold: #1a3a5c;
    --gradient-hot: #ff4444;
    --border: #2e3348;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header */
#header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 20px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    z-index: 1000;
    flex-shrink: 0;
}

#header h1 {
    font-size: 1.15rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--text-primary);
}

#header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

#header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

#header-nav {
    display: flex;
    align-items: center;
}

#header-nav a {
    font-size: 0.9rem;
    color: var(--text-secondary);
    text-decoration: none;
}

#header-nav a:hover {
    color: var(--accent);
}

#stats {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

#stat-events {
    font-weight: 700;
    color: var(--accent);
}

.stat-sep {
    margin: 0 8px;
    opacity: 0.4;
}

.github-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 999px;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    background: var(--bg-tertiary);
    text-decoration: none;
    transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
}

.github-link:hover {
    background: var(--accent-dim);
    border-color: var(--accent);
    color: var(--text-primary);
    transform: translateY(-1px);
}

.github-icon {
    width: 18px;
    height: 18px;
}

/* Map */
#map-container {
    flex: 1;
    position: relative;
    min-height: 0;
}

#map {
    width: 100%;
    height: 100%;
    background: var(--bg-primary);
}

.leaflet-container {
    background: #0a0d14 !important;
}

.leaflet-control-zoom a {
    background: var(--bg-secondary) !important;
    color: var(--text-primary) !important;
    border-color: var(--border) !important;
}

.leaflet-control-zoom a:hover {
    background: var(--bg-tertiary) !important;
}

.leaflet-control-attribution {
    background: rgba(15, 17, 23, 0.8) !important;
    color: var(--text-secondary) !important;
    font-size: 10px !important;
}

.leaflet-control-attribution a {
    color: var(--accent-dim) !important;
}

#map-overlay {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 800;
    pointer-events: none;
}

#current-period {
    background: rgba(15, 17, 23, 0.85);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 16px;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    backdrop-filter: blur(8px);
    display: none;
}

/* Controls panel */
#controls-container {
    flex-shrink: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    padding: 12px 20px 14px;
}

/* Timeline chart + brush */
#timeline-wrapper {
    position: relative;
    height: 150px;
    margin-bottom: 10px;
}

#timeline-chart {
    width: 100%;
    height: 100%;
    cursor: pointer;
}

#brush-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
}

.brush-handle {
    position: absolute;
    top: 0;
    width: 12px;
    height: 100%;
    background: var(--accent);
    opacity: 0.9;
    cursor: ew-resize;
    pointer-events: all;
    border-radius: 3px;
    box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.35);
    transition: opacity 0.15s, box-shadow 0.15s, transform 0.1s;
}

.brush-handle::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2px;
    height: 14px;
    border-radius: 999px;
    background: var(--bg-secondary);
    box-shadow:
        -3px 0 0 var(--bg-secondary),
         3px 0 0 var(--bg-secondary);
    transform: translate(-50%, -50%);
}

.brush-handle:hover,
.brush-handle.active {
    opacity: 1;
    box-shadow: 0 0 0 3px rgba(77, 166, 255, 0.45);
    transform: translateX(0);
}

#brush-selection {
    position: absolute;
    top: 0;
    height: 100%;
    background: rgba(77, 166, 255, 0.08);
    border-top: 2px solid var(--accent);
    border-bottom: 2px solid var(--accent);
    pointer-events: all;
    cursor: grab;
}

#brush-selection:active {
    cursor: grabbing;
}

/* Animation controls row */
#controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

#animation-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

#animation-controls button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

#animation-controls button:hover {
    background: var(--accent-dim);
    border-color: var(--accent);
}

#speed-control {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 8px;
}

#speed-control label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

#speed-slider {
    width: 80px;
    accent-color: var(--accent);
}

#range-display {
    font-size: 0.85rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 6px;
}

#range-start, #range-end {
    font-weight: 600;
    color: var(--text-primary);
    font-variant-numeric: tabular-nums;
}

.range-arrow {
    color: var(--accent-dim);
}

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

#range-event-count {
    font-weight: 600;
    color: var(--accent);
}

.hidden {
    display: none !important;
}

/* Loading overlay */
#loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 17, 23, 0.92);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.4s;
}

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

#loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

#loading-text {
    margin-top: 14px;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

/* Responsive */
@media (max-width: 768px) {
    #header {
        flex-direction: column;
        gap: 4px;
        padding: 8px 12px;
    }

    #controls {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    #controls-container {
        padding: 10px 12px;
    }

    #timeline-wrapper {
        height: 100px;
    }
}

/* Event popup (detail view) */
.event-popup .leaflet-popup-content-wrapper {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.event-popup .leaflet-popup-tip {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
}

.event-popup .leaflet-popup-content {
    margin: 10px 14px;
    font-size: 0.82rem;
    line-height: 1.6;
}

.event-popup .leaflet-popup-content b {
    color: var(--accent);
}

.event-popup .leaflet-popup-content code {
    font-size: 0.75rem;
    background: var(--bg-tertiary);
    padding: 1px 5px;
    border-radius: 3px;
    word-break: break-all;
}

.event-popup .leaflet-popup-close-button {
    color: var(--text-secondary) !important;
}

.event-popup .leaflet-popup-close-button:hover {
    color: var(--text-primary) !important;
}

/* Info overlay (dataset) */
#info-overlay {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
}

#info-overlay.hidden {
    display: none !important;
}

.info-overlay-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.65);
    cursor: pointer;
}

.info-overlay-panel {
    position: relative;
    width: 100%;
    max-width: 640px;
    max-height: 85vh;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.info-overlay-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.info-overlay-header h2 {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.info-overlay-close {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-size: 1.5rem;
    line-height: 1;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.info-overlay-close:hover {
    background: var(--border);
    color: var(--text-primary);
}

.info-overlay-content {
    overflow-y: auto;
    padding: 20px;
    font-size: 0.9rem;
    line-height: 1.6;
}

.info-overlay-content p {
    margin: 0 0 12px;
    color: var(--text-secondary);
}

.info-overlay-content p.info-lead {
    color: var(--text-primary);
    font-size: 1rem;
}

.info-overlay-content h3 {
    font-size: 1.05rem;
    margin: 22px 0 10px;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border);
    padding-bottom: 6px;
}

.info-overlay-content h3:first-child {
    margin-top: 0;
}

.info-overlay-content ul {
    margin: 8px 0 16px 20px;
    color: var(--text-secondary);
}

.info-overlay-content li {
    margin-bottom: 4px;
}

.info-overlay-content .info-table {
    width: 100%;
    border-collapse: collapse;
    margin: 12px 0 16px;
    font-size: 0.85rem;
}

.info-overlay-content .info-table th,
.info-overlay-content .info-table td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    color: var(--text-secondary);
}

.info-overlay-content .info-table th {
    color: var(--text-primary);
    font-weight: 600;
}

.info-overlay-content .info-ref {
    margin: 12px 0;
    padding: 12px 14px;
    background: var(--bg-tertiary);
    border-radius: 8px;
    border-left: 3px solid var(--accent);
    font-size: 0.85rem;
}

.info-overlay-content a {
    color: var(--text-primary);
}

.info-overlay-content a:hover {
    text-decoration: underline;
}

.info-overlay-content .info-ref a {
    color: var(--text-primary);
}

.info-overlay-content .info-ref-title {
    font-weight: 600;
    color: var(--text-primary);
}

.info-overlay-content .info-ref-meta {
    display: block;
    margin-top: 4px;
    color: var(--text-secondary);
    font-size: 0.8rem;
}
