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

:root {
    --bg: #0f1117;
    --panel: #1a1d27;
    --card: #232734;
    --border: #2d3244;
    --text: #e8eaf0;
    --text-dim: #8b90a5;
    --accent: #4f8cff;
    --green: #34d399;
    --yellow: #fbbf24;
    --red: #f87171;
    --orange: #fb923c;
}

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

/* ── Generic page container ── */

.page {
    position: fixed;
    inset: 0;
    background: var(--bg);
    z-index: 1100;
    overflow-y: auto;
}

.page.hidden { display: none; }

/* ── Overlay (pre-race countdown) ── */

.overlay {
    position: fixed;
    inset: 0;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1100;
    text-align: center;
}

.overlay.hidden { display: none; pointer-events: none; }

.overlay-content h1 {
    font-size: 2.5rem;
    margin-bottom: 0.5rem;
}

.overlay-content p {
    font-size: 1.2rem;
    color: var(--text-dim);
    margin-bottom: 2rem;
}

.countdown {
    font-size: 3rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--accent);
}

.overlay-weather {
    margin-top: 1.5rem;
    font-size: 0.9rem;
    color: var(--text-dim);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.overlay-weather .weather-desc {
    color: var(--text);
    font-weight: 600;
}

/* ── Reset button ── */

.btn-reset {
    margin-top: 2.5rem;
    background: none;
    border: 1px solid var(--border);
    color: var(--text-dim);
    border-radius: 8px;
    padding: 0.5rem 1.2rem;
    font-size: 0.8rem;
    cursor: pointer;
    transition: color 0.2s, border-color 0.2s;
}

.btn-reset:hover {
    color: var(--red);
    border-color: var(--red);
}

.btn-reset-small {
    margin-top: 0;
    margin-left: auto;
    padding: 0.3rem 0.7rem;
    font-size: 0.7rem;
    flex-shrink: 0;
}

/* ── Setup page ── */

.setup-container {
    max-width: 480px;
    margin: 0 auto;
    padding: 3rem 1.5rem;
}

.setup-container h1 {
    font-size: 2rem;
    margin-bottom: 0.25rem;
}

.setup-subtitle {
    color: var(--text-dim);
    margin-bottom: 2rem;
    font-size: 1rem;
}

.race-form {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.form-group label {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-dim);
}

.form-group input[type="text"],
.form-group input[type="datetime-local"] {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    color: var(--text);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}

.form-group input:focus {
    border-color: var(--accent);
}

.file-upload {
    position: relative;
    background: var(--card);
    border: 2px dashed var(--border);
    border-radius: 10px;
    padding: 1.5rem;
    text-align: center;
    cursor: pointer;
    transition: border-color 0.2s;
}

.file-upload:hover {
    border-color: var(--accent);
}

.file-upload input[type="file"] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}

.file-upload-label {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-dim);
    font-size: 0.9rem;
}

.file-icon {
    font-size: 2rem;
}

.form-error {
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid rgba(248, 113, 113, 0.3);
    color: var(--red);
    padding: 0.6rem 0.8rem;
    border-radius: 8px;
    font-size: 0.85rem;
}

.form-error.hidden { display: none; }

.btn-primary {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 0.85rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.2s;
}

.btn-primary:hover {
    opacity: 0.9;
}

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

/* ── Report page ── */

.report-container {
    max-width: 600px;
    margin: 0 auto;
    padding: 2.5rem 1.5rem;
}

.report-container h1 {
    font-size: 1.8rem;
    margin-bottom: 1.5rem;
}

.report-content {
    margin-bottom: 2rem;
}

.report-note {
    color: var(--text-dim);
    text-align: center;
    padding: 2rem;
}

.report-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

.report-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1rem;
}

.report-card-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-dim);
    margin-bottom: 0.25rem;
}

.report-card-value {
    font-size: 1.3rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.report-section {
    margin-bottom: 1.5rem;
}

.report-section h3 {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text);
}

/* HR Zones */

.hr-zones {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.hr-zone-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.8rem;
}

.hr-zone-label {
    width: 120px;
    flex-shrink: 0;
    color: var(--text-dim);
}

.hr-zone-bar-bg {
    flex: 1;
    height: 8px;
    background: var(--card);
    border-radius: 4px;
    overflow: hidden;
}

.hr-zone-bar {
    height: 100%;
    border-radius: 4px;
    transition: width 0.3s;
}

.hr-zone-pct {
    width: 45px;
    text-align: right;
    font-variant-numeric: tabular-nums;
    color: var(--text-dim);
}

/* Splits table */

.splits-table {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    max-height: 300px;
    overflow-y: auto;
}

.splits-header {
    display: flex;
    justify-content: space-between;
    padding: 0.6rem 1rem;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-dim);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--card);
}

.splits-row {
    display: flex;
    justify-content: space-between;
    padding: 0.45rem 1rem;
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
    border-bottom: 1px solid rgba(45, 50, 68, 0.3);
}

.splits-row:last-child {
    border-bottom: none;
}

/* ── Race history ── */

.race-history-section {
    margin-top: 2.5rem;
    border-top: 1px solid var(--border);
    padding-top: 1.5rem;
}

.race-history-section.hidden { display: none; }

.race-history-section h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text);
}

.race-history-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.history-item {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.75rem 1rem;
}

.history-name {
    font-weight: 600;
    font-size: 0.95rem;
    margin-bottom: 0.2rem;
}

.history-meta {
    font-size: 0.8rem;
    color: var(--text-dim);
}

.history-report-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.history-report-link:hover {
    text-decoration: underline;
}

/* ── Main layout ── */

.main {
    display: flex;
    height: 100vh;
}

#map-container {
    flex: 1 1 60%;
    min-width: 0;
}

#map {
    width: 100%;
    height: 100%;
}

#stats-panel {
    flex: 0 0 380px;
    background: var(--panel);
    border-left: 1px solid var(--border);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.stats-fixed {
    flex-shrink: 0;
    padding: 1.25rem 1.25rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.stats-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 1.25rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* ── Runner header ── */

.runner-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);
}

.runner-avatar {
    font-size: 2.5rem;
    line-height: 1;
}

.runner-info h1 {
    font-size: 1.4rem;
    font-weight: 700;
}

.runner-info p {
    font-size: 0.8rem;
    color: var(--text-dim);
}

/* ── Stats grid ── */

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.stat-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.stat-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-dim);
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.stat-sub {
    font-size: 0.75rem;
    color: var(--text-dim);
}

/* Heart rate colors */
.hr-green  .stat-value { color: var(--green); }
.hr-yellow .stat-value { color: var(--yellow); }
.hr-red    .stat-value { color: var(--red); }

/* ── Progress bar ── */

.progress-bar-container {
    position: relative;
    background: var(--card);
    border-radius: 8px;
    height: 28px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), #6c5ce7);
    border-radius: 8px;
    transition: width 1s ease;
}

.progress-text {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 600;
}

/* ── Checkpoints ── */

.checkpoints h2 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.checkpoint-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.checkpoint {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.6rem;
    border-radius: 6px;
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
}

.checkpoint.passed {
    background: rgba(52, 211, 153, 0.08);
    color: var(--green);
}

.checkpoint.next {
    background: rgba(79, 140, 255, 0.12);
    color: var(--accent);
    font-weight: 600;
}

.checkpoint.upcoming {
    color: var(--text-dim);
}

.checkpoint .km {
    width: 3rem;
    font-weight: 600;
}

.checkpoint .eta {
    flex: 1;
    text-align: right;
}

.checkpoint .diff {
    width: 4rem;
    text-align: right;
    font-size: 0.7rem;
    font-weight: 600;
}

.diff-ahead { color: var(--green); }
.diff-behind { color: var(--orange); }

.checkpoint .status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
}

.checkpoint.passed .status-dot  { background: var(--green); }
.checkpoint.next .status-dot    { background: var(--accent); }
.checkpoint.upcoming .status-dot { background: var(--border); }

/* ── Pulsing runner marker ── */

@keyframes pulse {
    0%   { transform: scale(1);   opacity: 1; }
    50%  { transform: scale(1.8); opacity: 0.4; }
    100% { transform: scale(2.5); opacity: 0; }
}

.runner-marker {
    width: 18px;
    height: 18px;
    background: var(--accent);
    border: 3px solid #fff;
    border-radius: 50%;
    position: relative;
    box-shadow: 0 0 8px rgba(79, 140, 255, 0.6);
}

.runner-marker::after {
    content: "";
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    background: var(--accent);
    animation: pulse 2s infinite;
    z-index: -1;
}

/* ── Km markers on map ── */

.km-marker {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 700;
    color: #fff;
    text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

.km-marker-major {
    width: 22px;
    height: 22px;
    background: var(--accent);
    border: 2px solid #fff;
    border-radius: 50%;
}

.km-marker-minor {
    width: 14px;
    height: 14px;
    background: rgba(79, 140, 255, 0.5);
    border: 1px solid rgba(255,255,255,0.5);
    border-radius: 50%;
    font-size: 8px;
}

/* ── Mobile responsive ── */

@media (max-width: 768px) {
    .main {
        flex-direction: column;
    }

    #map-container {
        flex: 0 0 40vh;
    }

    #stats-panel {
        flex: 1;
        border-left: none;
        border-top: 1px solid var(--border);
    }

    .stat-value {
        font-size: 1.25rem;
    }

    .chart-container {
        height: 140px;
    }

    .setup-container {
        padding: 2rem 1rem;
    }

    .report-container {
        padding: 2rem 1rem;
    }

    .report-grid {
        grid-template-columns: 1fr;
    }
}

/* ── Re-center button ── */

#map-container {
    position: relative;
}

.center-btn {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 800;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 20px;
    padding: 8px 20px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
    transition: opacity 0.2s;
}

.center-btn:hover {
    opacity: 0.9;
}

.center-btn.hidden {
    display: none;
}

/* ── HA connection status banner ── */

.ha-status {
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    font-size: 0.78rem;
    display: none;
}

.ha-status.ha-error {
    display: block;
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid rgba(248, 113, 113, 0.3);
    color: var(--red);
}

.ha-status.ha-ok {
    display: none;
}

/* ── Weather card (floating on map) ── */

.weather-bar {
    display: none;
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 800;
    flex-direction: column;
    gap: 0.2rem;
    padding: 0.55rem 0.75rem;
    background: rgba(26, 29, 39, 0.88);
    backdrop-filter: blur(8px);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.75rem;
    color: var(--text-dim);
    pointer-events: none;
    min-width: 130px;
}

.weather-bar.visible {
    display: flex;
}

.weather-main {
    display: flex;
    align-items: center;
    gap: 0.45rem;
}

.weather-icon {
    font-size: 1.3rem;
    line-height: 1;
}

.weather-temp {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text);
}

.weather-desc {
    font-size: 0.7rem;
    color: var(--text-dim);
}

.weather-details {
    display: flex;
    gap: 0.6rem;
    font-size: 0.68rem;
    color: var(--text-dim);
    padding-top: 0.15rem;
    border-top: 1px solid rgba(45, 50, 68, 0.5);
    margin-top: 0.15rem;
}

.weather-details span {
    display: flex;
    align-items: center;
    gap: 0.2rem;
}

.weather-detail-icon {
    font-size: 0.75rem;
    opacity: 0.7;
}

/* ── Pace chart ── */

.pace-chart-section h2 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.chart-container {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.5rem;
    height: 180px;
}

/* ── Last update indicator ── */

.last-update {
    font-size: 0.72rem;
    color: var(--text-dim);
    text-align: right;
}

.last-update.stale {
    color: var(--orange);
}

/* ── Leaflet overrides ── */

.leaflet-container {
    background: #1a1d27;
}
