:root {
    --rm-primary: #e23126;
    --rm-radius-container: 8px;
    --rm-radius-map: 8px;
}

.race-map-wrapper.map-style-dark {
    --rm-overlay-bg: var(--rm-overlay-bg-dark);
    --rm-overlay-text: var(--rm-overlay-text-dark);
    --rm-overlay-blur: var(--rm-overlay-blur-dark);
}

.rundrm-header-container,
header,
.site-header,
#masthead,
.elementor-location-header {
    z-index: 999999 !important;
    position: relative;
}

.leaflet-container .leaflet-control-attribution {
    display: none;
}

.race-map-wrapper {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
    margin-bottom: 30px;
    position: relative;
    border-radius: var(--rm-radius-container);
    display: block;
}

.race-map-wrapper:focus {
    outline: none;
}

.race-map-header-overlay {
    width: 250px;
    max-width: 100%;
}

.race-map-header-overlay.hidden {
    display: none;
}

.race-map-select-overlay {
    width: 100%;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: 600;
    border: none;
    border-radius: 6px;
    background-color: var(--rm-overlay-bg);
    color: var(--rm-overlay-text);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
    backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    -webkit-backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-hyphens: none;
    hyphens: none;
    transition: color 0.2s;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.race-map-select-overlay option {
    white-space: normal;
    word-break: normal;
    -webkit-hyphens: none !important;
    hyphens: none !important;
}

.race-map-select-overlay:hover {
    color: var(--rm-primary);
}

.race-map-container {
    position: relative;
    width: 100%;
    padding: 0;
}

.race-map-canvas {
    width: 100%;
    height: 100dvh;
    /* Default to 100 dynamic viewport height */
    /* Default to 100 dynamic viewport height */
    background: #222;
    z-index: 1;
    display: block;
    border-radius: var(--rm-radius-map);
    overflow: hidden;
}

.race-map-controls-overlay {
    position: absolute;
    bottom: 15px;
    left: 50%;
    transform: translateX(-50%);
    width: 50%;
    max-width: 800px;
    min-width: 320px;
    padding: 10px 15px;
    background: var(--rm-overlay-bg);
    display: flex;
    align-items: center;
    gap: 15px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    -webkit-backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    z-index: 90;
    pointer-events: auto;
}

.race-map-controls-overlay button {
    transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.race-map-controls-overlay button:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-2px);
}

.race-map-controls-overlay button:active {
    transform: translateY(0);
}

.race-play-btn {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
    color: var(--rm-overlay-text);
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.2s ease;
}

.race-play-btn:hover {
    border-color: var(--rm-primary);
    color: var(--rm-primary);
}

.race-play-btn svg {
    fill: currentColor;
    width: 16px;
    height: 16px;
    filter: drop-shadow(0px 2px 3px rgba(0, 0, 0, 0.4));
}

.race-play-btn.locked {
    color: var(--rm-primary);
    border-color: var(--rm-primary);
    background: rgba(226, 49, 38, 0.1);
}

.race-timeline-wrapper {
    flex-grow: 1;
    height: 100%;
    position: relative;
}

.race-elevation-canvas {
    width: 100%;
    height: 60px;
    display: block;
    background: transparent;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.race-elevation-canvas:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    transform: translateY(-2px) scale(1.02);
}



/* --- INLINE STATS UNDER ELEVATION CANVAS --- */
.race-map-stats-inline {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    margin-top: 6px;
    font-size: 13px;
    font-weight: 400;
    color: var(--rm-overlay-text);
    pointer-events: none;
    line-height: 1;
}

.race-stat-box-inline {
    padding: 3px 0;
    white-space: nowrap;
}

.race-stat-box-inline+.race-stat-box-inline::before {
    content: '|';
    margin-right: 8px;
    color: rgba(255, 255, 255, 0.4);
    text-shadow: 1px 0 0 rgba(0, 0, 0, 0.6);
    font-weight: 400;
}

.race-map-zoom-container {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 15px;
    z-index: 90;
    display: flex;
    pointer-events: auto;
}

.race-info-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 320px;
    max-width: calc(100% - 30px);
    max-height: calc(100% - 30px);
    overflow-y: auto;
    background: var(--rm-overlay-bg);
    color: var(--rm-overlay-text);
    padding: 10px 15px;
    border-radius: 6px;
    z-index: 9999;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    -webkit-backdrop-filter: blur(var(--rm-overlay-blur, 12px));
}

.race-info-overlay.collapsed {
    transform: translate(-50%, -48%) scale(0.95);
    opacity: 0;
    pointer-events: none;
}

.race-info-overlay.expanded {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
    pointer-events: auto;
}

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

.race-info-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 15px;
    padding: 10px 0;
}

.race-desc-title {
    display: block;
    font-weight: bold;
    font-size: 15px;
    margin: 0;
    color: var(--rm-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.race-desc-text {
    font-size: 13px;
    line-height: 1.4;
    color: var(--rm-overlay-text);
    opacity: 0.9;
    display: none;
    white-space: pre-wrap;
}

.race-info-overlay.expanded .race-desc-text {
    display: block;
}

.race-toggle-btn {
    background: rgba(128, 128, 128, 0.2);
    border: none;
    padding: 4px 10px;
    color: var(--rm-overlay-text);
    font-size: 11px;
    cursor: pointer;
    border-radius: 4px;
    white-space: nowrap;
    transition: all 0.2s;
}

.race-toggle-btn:hover {
    background: rgba(128, 128, 128, 0.3);
    color: var(--rm-primary);
}

/* --- UPDATED MARKER CSS --- */
.race-marker-container {
    pointer-events: none !important;
    width: 0 !important;
    height: 0 !important;
    overflow: visible;
}

.race-pin-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    pointer-events: none !important;
    transition: opacity 0.3s ease;
    position: absolute;
    bottom: 0;
    left: 0;
    transform: translateX(-50%);
    width: max-content;
}

.race-pin-wrapper:hover {
    transform: translateX(-50%) scale(1.1);
    z-index: 5000 !important;
}

.race-pin-emoji {
    font-size: 24px;
    line-height: 30px;
    z-index: 20;
    /* Ensure emoji is ON TOP of line */
    background: var(--rm-emoji-bg, rgba(255, 255, 255, 0.65));
    backdrop-filter: blur(var(--rm-emoji-blur, 8px));
    -webkit-backdrop-filter: blur(var(--rm-emoji-blur, 8px));
    border-radius: 15px;
    min-width: 30px;
    height: 30px;
    padding: 0 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid rgba(255, 255, 255, 0.4);
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15), 0 2px 5px rgba(0, 0, 0, 0.1);
    color: var(--rm-emoji-text, #111111);
    pointer-events: auto !important;
    cursor: help;
    white-space: nowrap;
    position: relative;
}

.race-pin-line {
    width: 2px;
    background: #fff;
    box-shadow: 0 0 3px rgba(0, 0, 0, 0.8);
    flex-grow: 1;
    z-index: 10;
    /* Lower z-index than emoji */
    position: relative;
}

.race-km-marker {
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid #666;
    border-radius: 50%;
    text-align: center;
    color: #333;
    font-weight: bold;
    font-size: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    line-height: 1;
    padding: 0;
    padding-top: 2.5px;
    box-sizing: border-box;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}

.race-km-marker.active {
    background: var(--rm-primary);
    border: 2px solid #ffffff;
    color: #ffffff;
    transform: scale(1.15);
    z-index: 3000 !important;
}

.race-km-marker.active span {
    transform: translateY(-0.5px) !important;
}


/* Scrubbing Mode */
.race-map-wrapper.scrubbing .race-pin-wrapper,
.race-map-wrapper.scrubbing .race-km-marker {
    opacity: 0.5;
}

.race-map-tooltip {
    background: rgba(0, 0, 0, 0.8);
    border: none;
    color: #fff;
    font-weight: bold;
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 4px;
}

.leaflet-tooltip-left:before {
    border-left-color: rgba(0, 0, 0, 0.8);
}

.leaflet-tooltip-right:before {
    border-right-color: rgba(0, 0, 0, 0.8);
}

.leaflet-tooltip-top:before {
    border-top-color: rgba(0, 0, 0, 0.8);
}

.leaflet-tooltip-bottom:before {
    border-bottom-color: rgba(0, 0, 0, 0.8);
}



.race-download-btn {
    background-color: var(--rm-primary);
    color: #fff !important;
    text-decoration: none;
    padding: 10px 20px;
    border-radius: 4px;
    font-size: 0.9rem;
    font-weight: bold;
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.race-download-btn:hover {
    background-color: #c02015;
}

.race-download-btn svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

.race-map-loader {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 60;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(20, 20, 20, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 30px 40px;
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
}

.race-map-wrapper.loading .race-map-loader {
    display: flex;
}

.race-loader-content {
    position: relative;
    width: 60px;
    height: 50px;
    display: flex;
    justify-content: center;
}

.race-loader-pin {
    width: 30px;
    height: 30px;
    border-radius: 50% 50% 50% 0;
    background: var(--rm-primary);
    position: absolute;
    transform: rotate(-45deg);
    top: 0;
    animation: race-pin-bounce 1.2s infinite cubic-bezier(0.28, 0.84, 0.42, 1);
    box-shadow: inset -4px -4px 6px rgba(0,0,0,0.2);
}

.race-loader-pin::after {
    content: '';
    width: 14px;
    height: 14px;
    background: #fff;
    position: absolute;
    border-radius: 50%;
    top: 8px;
    left: 8px;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}

.race-loader-shadow {
    width: 20px;
    height: 6px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    position: absolute;
    bottom: 0px;
    animation: race-shadow-scale 1.2s infinite cubic-bezier(0.28, 0.84, 0.42, 1);
    filter: blur(2px);
}

.race-loader-text {
    margin-top: 15px;
    color: #fff;
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    animation: race-text-pulse 1.5s infinite;
}

@keyframes race-pin-bounce {
    0%, 100% { transform: rotate(-45deg) translate(0, 0); }
    50% { transform: rotate(-45deg) translate(15px, -15px); }
}

@keyframes race-shadow-scale {
    0%, 100% { transform: scale(1); opacity: 0.6; }
    50% { transform: scale(0.5); opacity: 0.2; }
}

@keyframes race-text-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.race-download-btn-small {
    /* Inherits from .race-control-btn but adds specific tweaks if needed */
    display: flex;
    /* Make sure it uses flexbox */
}

.race-download-btn-small:hover {
    color: var(--rm-primary);
    /* Use primary color on hover like other buttons usually do or keep grey */
}

/* --- MAP TOOLS (Top Right) --- */
.race-map-tools {
    position: absolute;
    top: var(--rm-tools-margin-top, 15px);
    right: 15px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 90;
    align-items: flex-end;
    pointer-events: none;
}

.race-map-tools>* {
    pointer-events: auto;
}

.race-tools-toggle {
    display: none;
}

.race-control-group {
    background: var(--rm-overlay-bg);
    padding: 4px;
    border-radius: 6px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    gap: 4px;
    align-items: center;
    backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    -webkit-backdrop-filter: blur(var(--rm-overlay-blur, 12px));
}

/* Layer Buttons */
.race-layer-btn {
    background: none;
    border: none;
    padding: 6px 10px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    border-radius: 4px;
    color: var(--rm-overlay-text);
    transition: all 0.2s;
}

.race-layer-btn.active {
    background: var(--rm-primary);
    color: #fff;
}

.race-layer-btn:hover:not(.active) {
    background: rgba(128, 128, 128, 0.2);
    color: var(--rm-primary);
}

/* Vertical Zoom Control */
.race-zoom-control {
    display: flex;
    flex-direction: column;
    padding: 4px;
    gap: 0;
    background: var(--rm-overlay-bg);
    border-radius: 6px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
    width: fit-content;
    backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    -webkit-backdrop-filter: blur(var(--rm-overlay-blur, 12px));
}

.zoom-btn {
    width: 24px;
    height: 24px;
    background: transparent;
    border: none;
    font-size: 18px;
    font-weight: bold;
    color: var(--rm-overlay-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.zoom-btn:hover {
    background: rgba(128, 128, 128, 0.2);
    color: var(--rm-primary);
}

.zoom-btn.in {
    border-radius: 0;
}

.zoom-btn.center {
    border-radius: 6px 6px 0 0;
}

.zoom-btn.out {
    border-radius: 0 0 6px 6px;
}

/* Vertical Slider */
.zoom-slider {
    -webkit-appearance: slider-vertical;
    appearance: slider-vertical;
    width: 8px;
    height: 80px;
    background: #ddd;
    border-radius: 3px;
    outline: none;
    margin: 5px auto;
}

/* Fallback */
@supports not (-webkit-appearance: slider-vertical) {
    .zoom-slider {
        writing-mode: bt-lr;
        -webkit-appearance: none;
        appearance: none;
        width: 80px;
        height: 8px;
        transform: rotate(270deg);
        margin: 40px -36px;
    }
}

.race-control-btn {
    background: var(--rm-overlay-bg);
    border: none;
    padding: 6px 12px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    border-radius: 6px;
    color: var(--rm-overlay-text);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: all 0.2s;
    backdrop-filter: blur(var(--rm-overlay-blur, 12px));
    -webkit-backdrop-filter: blur(var(--rm-overlay-blur, 12px));
}

.race-control-btn:hover {
    background: rgba(128, 128, 128, 0.2);
    color: var(--rm-primary);
}

/* Override Leaflet controls z-index to be lower than sticky headers */
.leaflet-top,
.leaflet-bottom {
    z-index: 40 !important;
}

/* Fullscreen overrrides */
.race-map-wrapper:fullscreen {
    border-radius: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
}

.race-map-wrapper:fullscreen .race-map-canvas {
    border-radius: 0 !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
}

.race-map-wrapper:-webkit-full-screen {
    border-radius: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
    height: 100vh !important;
}

.race-map-wrapper:-webkit-full-screen .race-map-canvas {
    border-radius: 0 !important;
    height: 100vh !important;
}

/* Photo Popup Overlay */
.race-photo-popup {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999999;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.race-photo-popup.hidden {
    opacity: 0;
    pointer-events: none;
}

.race-photo-popup-content {
    position: relative;
    max-width: 90%;
    max-height: 90%;
    background: #000;
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.race-photo-popup-content img {
    max-width: 100%;
    max-height: 90vh;
    display: block;
    object-fit: contain;
}

.race-photo-caption {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 15px 20px;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    color: #fff;
    font-size: 14px;
    text-align: center;
    box-sizing: border-box;
    font-weight: 500;
    z-index: 10;
}

.close-photo {
    position: absolute;
    top: 10px;
    right: 15px;
    color: var(--rm-primary);
    font-size: 26px;
    font-weight: bold;
    cursor: pointer;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    z-index: 10;
    line-height: 1;
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.2s ease;
}

.close-photo:hover {
    background: rgba(0, 0, 0, 0.85);
    transform: scale(1.05);
}

/* --- ACCESSIBILITY / FOCUS STATES --- */
.race-map-wrapper button:focus-visible,
.race-map-wrapper a:focus-visible,
.race-map-wrapper select:focus-visible,
.race-map-wrapper input[type="range"]:focus-visible {
    outline: 2px solid var(--rm-primary);
    outline-offset: 2px;
}

@media (max-width: 768px) {
    .race-map-left-panel {
        right: 15px;
    }

    .race-map-header-overlay {
        width: 100%;
    }

    .race-map-tools> :not(.race-tools-toggle) {
        transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1), opacity 0.3s;
    }

    .race-tools-toggle {
        display: flex;
        position: relative;
        width: 46px;
        height: 46px;
        justify-content: center;
        align-items: center;
        padding: 0;
        border-radius: 50% !important;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3) !important;
        align-self: flex-end;
        flex-shrink: 0;
        margin-top: 4px;
    }

    .race-map-tools.is-hidden> :not(.race-tools-toggle) {
        transform: translateX(120%);
        opacity: 0;
        pointer-events: none;
    }

    .race-control-group {
        flex-wrap: nowrap;
        width: auto;
        justify-content: flex-end;
    }

    .race-map-controls-overlay {
        bottom: 15px;
        left: 15px;
        right: 15px;
        transform: none;
        width: auto;
        padding: 8px 10px;
    }


    button[id^="fullscreen-"] {
        display: none !important;
    }

    .race-map-stats-inline {
        justify-content: flex-start;
        font-size: 11px;
        gap: 5px;
        flex-wrap: wrap;
        /* Let it wrap if really tight */
    }

    .race-stat-box-inline+.race-stat-box-inline::before {
        margin-right: 5px;
    }
}