/**
 * IM ACF OpenStreetMap - Frontend Styles
 *
 * Styles for frontend map display.
 * Uses aggressive selectors to override Bootstrap/theme CSS conflicts.
 *
 * @package IM_ACF_OpenStreetMap
 * @since 1.0.5
 */

/* ==========================================================================
   CRITICAL: Leaflet Core Layout Fixes
   These rules MUST override any Bootstrap/theme CSS that breaks Leaflet
   ========================================================================== */

/* The map container - .im-osm-map gets .leaflet-container class added by Leaflet */
.im-osm-map-container {
    position: relative !important;
    width: 100% !important;
    margin: 0 0 1em !important;
    display: block !important;
}

/* Main map element - before and after Leaflet initialization */
.im-osm-map {
    width: 100% !important;
    min-height: 200px !important;
    background-color: #f0f0f0 !important;
    border-radius: 4px !important;
    overflow: hidden !important;
    position: relative !important;
    display: block !important;
}

/* When Leaflet adds .leaflet-container class directly to .im-osm-map */
.im-osm-map.leaflet-container {
    position: relative !important;
    overflow: hidden !important;
    background-color: transparent !important;
}

/* All Leaflet panes must be absolutely positioned */
.im-osm-map.leaflet-container .leaflet-pane,
.im-osm-map .leaflet-pane {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
}

/* The map pane that holds tiles */
.im-osm-map.leaflet-container .leaflet-map-pane,
.im-osm-map .leaflet-map-pane {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
    z-index: 0 !important;
}

/* Tile pane */
.im-osm-map.leaflet-container .leaflet-tile-pane,
.im-osm-map .leaflet-tile-pane {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
}

/* Tile layer container */
.im-osm-map.leaflet-container .leaflet-layer,
.im-osm-map .leaflet-layer {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
}

/* Tile container */
.im-osm-map.leaflet-container .leaflet-tile-container,
.im-osm-map .leaflet-tile-container {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
}

/* CRITICAL: Individual tiles - must have fixed 256px size and be positioned absolutely */
.im-osm-map.leaflet-container .leaflet-tile,
.im-osm-map .leaflet-tile {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
    width: 256px !important;
    height: 256px !important;
    max-width: none !important;
    max-height: none !important;
    min-width: 0 !important;
    min-height: 0 !important;
    display: block !important;
    border: none !important;
    padding: 0 !important;
    margin: 0 !important;
    box-sizing: border-box !important;
    /* Prevent any img rules from Bootstrap */
    object-fit: fill !important;
}

/* All images inside the map container - override Bootstrap img rules */
.im-osm-map.leaflet-container img,
.im-osm-map img {
    max-width: none !important;
    max-height: none !important;
    border: none !important;
    padding: 0 !important;
    margin: 0 !important;
}

/* Marker icons */
.im-osm-map.leaflet-container .leaflet-marker-icon,
.im-osm-map.leaflet-container .leaflet-marker-shadow,
.im-osm-map .leaflet-marker-icon,
.im-osm-map .leaflet-marker-shadow {
    position: absolute !important;
    display: block !important;
    max-width: none !important;
    max-height: none !important;
}

/* Control container */
.im-osm-map.leaflet-container .leaflet-control-container,
.im-osm-map .leaflet-control-container {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    pointer-events: none !important;
    z-index: 3000 !important;
}

.im-osm-map.leaflet-container .leaflet-control-container > div,
.im-osm-map .leaflet-control-container > div {
    position: absolute !important;
    pointer-events: auto !important;
}

/* Zoom controls - standard Leaflet bar appearance */
.im-osm-map.leaflet-container .leaflet-bar,
.im-osm-map .leaflet-bar {
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.65) !important;
    border-radius: 4px !important;
    border: none !important;
    background: transparent !important;
}

.im-osm-map.leaflet-container .leaflet-bar a,
.im-osm-map .leaflet-bar a {
    display: block !important;
    width: 26px !important;
    height: 26px !important;
    line-height: 26px !important;
    text-align: center !important;
    text-decoration: none !important;
    color: #000 !important;
    background-color: #fff !important;
    border-bottom: 1px solid #ccc !important;
    font: bold 18px/26px 'Lucida Console', Monaco, monospace !important;
}

.im-osm-map.leaflet-container .leaflet-bar a:first-child,
.im-osm-map .leaflet-bar a:first-child {
    border-top-left-radius: 4px !important;
    border-top-right-radius: 4px !important;
}

.im-osm-map.leaflet-container .leaflet-bar a:last-child,
.im-osm-map .leaflet-bar a:last-child {
    border-bottom-left-radius: 4px !important;
    border-bottom-right-radius: 4px !important;
    border-bottom: none !important;
}

.im-osm-map.leaflet-container .leaflet-bar a:hover,
.im-osm-map .leaflet-bar a:hover {
    background-color: #f4f4f4 !important;
}

.im-osm-map.leaflet-container .leaflet-control-zoom,
.im-osm-map .leaflet-control-zoom {
    position: relative !important;
    pointer-events: auto !important;
    margin: 10px !important;
}

/* ==========================================================================
   Additional Leaflet UI Styles
   ========================================================================== */

.im-osm-map .leaflet-popup-content-wrapper {
    border-radius: 6px;
}

.im-osm-map .leaflet-popup-content {
    margin: 10px 14px;
    line-height: 1.5;
}

/* Attribution styling */
.im-osm-map .leaflet-control-attribution {
    font-size: 11px !important;
    background: rgba(255, 255, 255, 0.9) !important;
    padding: 2px 6px !important;
    position: relative !important;
    display: inline-block !important;
    white-space: nowrap !important;
    border-radius: 3px !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2) !important;
}

.im-osm-map .leaflet-control-attribution a {
    color: #0073aa;
    text-decoration: none;
}

.im-osm-map .leaflet-control-attribution a:hover {
    text-decoration: underline;
}

/* ==========================================================================
   Address Display
   ========================================================================== */

.im-osm-map-address {
    margin-top: 8px;
    padding: 8px 12px;
    background: #f5f5f5;
    border-radius: 4px;
    font-size: 14px;
    color: #333;
    display: block;
}

.im-osm-map-address-text {
    display: block;
}

.im-osm-map-address-text::before {
    content: "📍 ";
}

/* ==========================================================================
   Fallback & Error States
   ========================================================================== */

.im-osm-fallback {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 200px;
    background: #f5f5f5;
    color: #666;
    font-style: italic;
    border-radius: 4px;
}

.im-osm-map.im-osm-error {
    background: #fef7f7 !important;
    border: 1px solid #f0c0c0 !important;
}

.im-osm-map.im-osm-error .im-osm-fallback {
    background: transparent;
    color: #a00;
}

/* ==========================================================================
   Loading State
   ========================================================================== */

.im-osm-map:not(.im-osm-initialized):not(.im-osm-error)::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 30px;
    height: 30px;
    margin: -15px 0 0 -15px;
    border: 3px solid #ddd;
    border-top-color: #0073aa;
    border-radius: 50%;
    animation: im-osm-spin 0.8s linear infinite;
    z-index: 10;
}

@keyframes im-osm-spin {
    to {
        transform: rotate(360deg);
    }
}

/* ==========================================================================
   Responsive Adjustments
   ========================================================================== */

@media screen and (max-width: 600px) {
    .im-osm-map {
        min-height: 250px !important;
    }

    .im-osm-map-address {
        font-size: 13px;
        padding: 6px 10px;
    }

    .im-osm-map .leaflet-control-attribution {
        font-size: 9px !important;
    }
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    .im-osm-map-container {
        page-break-inside: avoid;
    }

    .im-osm-map {
        border: 1px solid #ccc !important;
    }

    .im-osm-map .leaflet-control-zoom {
        display: none !important;
    }
}

/* ==========================================================================
   Accessibility
   ========================================================================== */

.im-osm-map:focus-within {
    outline: 2px solid #0073aa;
    outline-offset: 2px;
}

/* Screen reader only text */
.im-osm-sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
}

/* ==========================================================================
   Dark Mode Support
   ========================================================================== */

@media (prefers-color-scheme: dark) {
    .im-osm-map-address {
        background: #2c2c2c;
        color: #e0e0e0;
    }

    .im-osm-fallback {
        background: #2c2c2c;
        color: #999;
    }

    .im-osm-map .leaflet-control-attribution {
        background: rgba(30, 30, 30, 0.8) !important;
        color: #ccc;
    }

    .im-osm-map .leaflet-control-attribution a {
        color: #6eb4e8;
    }
}

/* ==========================================================================
   Emergency overrides: extra-high specificity to defeat stubborn theme rules
   ========================================================================== */

html body .im-osm-map.leaflet-container .leaflet-tile,
html body .im-osm-map .leaflet-tile {
    position: absolute !important;
    width: 256px !important;
    height: 256px !important;
    max-width: none !important;
    max-height: none !important;
    transform-origin: 0 0 !important;
    image-rendering: auto !important;
    backface-visibility: hidden !important;
}

html body .im-osm-map.leaflet-container .leaflet-tile-container,
html body .im-osm-map .leaflet-tile-container {
    position: absolute !important;
    will-change: transform !important;
}

/* Ensure Bootstrap img rules don't collapse tiles */
html body .im-osm-map.leaflet-container img.leaflet-tile,
html body .im-osm-map img.leaflet-tile {
    max-width: none !important;
    width: auto !important;
    height: auto !important;
}

/* Ensure default marker size matches Leaflet (25x41) */
html body .im-osm-map .leaflet-marker-icon,
html body .im-osm-map .leaflet-marker-shadow {
    width: 25px !important;
    height: 41px !important;
    max-width: none !important;
    max-height: none !important;
}

html body .im-osm-map .leaflet-marker-shadow {
    width: 41px !important;
    height: 41px !important;
}


