/* ============================================================
   PropertyPulse — Interactive Map Page Styles
   Full-screen Leaflet map with controls panel
   ============================================================ */

/* === FULL-SCREEN MAP LAYOUT === */

.map-page {
    position: relative;
    width: 100%;
    height: calc(100vh - var(--header-height));
    margin-top: var(--header-height);
    overflow: hidden;
}

.map-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
}

/* === LEAFLET GLOBAL CSS ISOLATION ===
   The global reset (*, img, svg rules in main.css) breaks Leaflet.
   Re-declare ALL critical Leaflet layout rules here so the map
   renders correctly even if the CDN stylesheet fails to load. */

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
    position: absolute;
    left: 0;
    top: 0;
}

.leaflet-container {
    overflow: hidden;
    -webkit-tap-highlight-color: transparent;
    font-size: 12px;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    -webkit-user-drag: none;
}

.leaflet-container img {
    max-width: none !important;
    max-height: none !important;
    display: block !important;
}

.leaflet-container img.leaflet-tile {
    outline: none;
}

.leaflet-tile-pane {
    box-sizing: content-box !important;
    z-index: 200;
}

.leaflet-tile-container {
    box-sizing: content-box !important;
}

.leaflet-tile {
    box-sizing: content-box !important;
    width: 256px !important;
    height: 256px !important;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    filter: inherit;
    visibility: hidden;
}

.leaflet-tile-loaded {
    visibility: inherit !important;
}

/* CRITICAL: transform-origin must be 0 0 for Leaflet's translate3d positioning */
.leaflet-zoom-animated {
    -webkit-transform-origin: 0 0;
    transform-origin: 0 0;
}

.leaflet-zoom-anim .leaflet-zoom-animated {
    will-change: transform;
    -webkit-transition: -webkit-transform 0.25s cubic-bezier(0,0,0.25,1);
    transition: transform 0.25s cubic-bezier(0,0,0.25,1);
}

/* Pane z-index stack */
.leaflet-pane          { z-index: 400; }
.leaflet-overlay-pane  { z-index: 400; }
.leaflet-shadow-pane   { z-index: 500; }
.leaflet-marker-pane   { z-index: 600; }
.leaflet-tooltip-pane  { z-index: 650; }
.leaflet-popup-pane    { z-index: 700; }

.leaflet-map-pane canvas { z-index: 100; }
.leaflet-map-pane svg    { z-index: 200; }

.leaflet-container .leaflet-overlay-pane svg,
.leaflet-container .leaflet-marker-pane img,
.leaflet-container .leaflet-shadow-pane img,
.leaflet-container .leaflet-tile-pane img,
.leaflet-container img.leaflet-image-layer {
    max-width: none !important;
    max-height: none !important;
    width: auto;
    padding: 0;
}

/* Control positioning */
.leaflet-control {
    position: relative;
    z-index: 800;
    pointer-events: auto;
}

.leaflet-top,
.leaflet-bottom {
    position: absolute;
    z-index: 1000;
    pointer-events: none;
}

.leaflet-top    { top: 0; }
.leaflet-right  { right: 0; }
.leaflet-bottom { bottom: 0; }
.leaflet-left   { left: 0; }

.leaflet-control { float: left; clear: both; }
.leaflet-right .leaflet-control { float: right; }
.leaflet-top .leaflet-control { margin-top: 10px; }
.leaflet-bottom .leaflet-control { margin-bottom: 10px; }
.leaflet-left .leaflet-control { margin-left: 10px; }
.leaflet-right .leaflet-control { margin-right: 10px; }

/* Cursors */
.leaflet-interactive { cursor: pointer; }
.leaflet-grab { cursor: -webkit-grab; cursor: grab; }
.leaflet-dragging .leaflet-grab,
.leaflet-dragging .leaflet-grab .leaflet-interactive,
.leaflet-dragging .leaflet-marker-draggable {
    cursor: move;
    cursor: -webkit-grabbing;
    cursor: grabbing;
}

/* Popup fade */
.leaflet-fade-anim .leaflet-popup { opacity: 0; transition: opacity 0.2s linear; }
.leaflet-fade-anim .leaflet-map-pane .leaflet-popup { opacity: 1; }

.leaflet-container .leaflet-control-container,
.leaflet-container .leaflet-control-container * {
    box-sizing: border-box;
}

/* Attribution */
.leaflet-container .leaflet-control-attribution {
    background: rgba(255,255,255,0.7);
    margin: 0;
    font-size: 11px;
}


/* === CONTROLS PANEL === */

.map-controls {
    position: absolute;
    top: var(--space-4);
    left: var(--space-4);
    z-index: 10;
    width: 320px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    overflow: hidden;
    transition: width var(--transition-base);
}

.map-controls--collapsed .map-controls__body,
.map-controls--collapsed .map-legend {
    display: none;
}

.map-controls--collapsed {
    width: auto;
}

.map-controls__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--border);
}

.map-controls__title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
    font-weight: 700;
}

.map-controls__title svg {
    width: 18px;
    height: 18px;
    color: var(--accent);
}

.map-controls__toggle {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    color: var(--text-tertiary);
    transition: all var(--transition-fast);
}

.map-controls__toggle:hover {
    background: var(--bg-card-hover);
    color: var(--text-primary);
}

.map-controls__toggle svg {
    width: 18px;
    height: 18px;
}

.map-controls__body {
    padding: var(--space-4) var(--space-5);
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    max-height: calc(100vh - var(--header-height) - 200px);
    overflow-y: auto;
    overflow-x: hidden;
}


/* === CONTROL GROUPS === */

.map-control-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.map-control-label {
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-tertiary);
}


/* === MAP SEARCH === */

.map-search-wrap {
    display: flex;
    align-items: center;
    gap: 0;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    transition: border-color var(--transition-fast);
}

.map-search-wrap:focus-within {
    border-color: var(--accent);
}

.map-search-input {
    flex: 1;
    border: none;
    outline: none;
    background: none;
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    font-family: var(--font-sans);
    color: var(--text-primary);
}

.map-search-input::placeholder {
    color: var(--text-tertiary);
}

.map-search-btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-tertiary);
    transition: color var(--transition-fast);
    flex-shrink: 0;
}

.map-search-btn:hover {
    color: var(--accent);
}

.map-search-btn svg {
    width: 16px;
    height: 16px;
}

.map-search-btn--loading {
    pointer-events: none;
    animation: map-search-spin 0.8s linear infinite;
}

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


/* === RADIO BUTTON GROUP === */

.map-radio-group {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
}

.map-radio {
    padding: var(--space-1) var(--space-3);
    font-size: var(--text-xs);
    font-weight: 500;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    background: var(--bg-section-alt);
    border: 1px solid transparent;
    transition: all var(--transition-fast);
    cursor: pointer;
}

.map-radio:hover {
    color: var(--text-primary);
    border-color: var(--border);
}

.map-radio--active {
    background: var(--pulse-emerald-glow);
    color: var(--accent);
    border-color: var(--accent);
    font-weight: 600;
}


/* === PRICE RANGE INPUTS === */

.map-price-range {
    display: flex;
    align-items: center;
    gap: var(--space-2);
}

.map-price-select {
    flex: 1 1 0;
    min-width: 0;
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    font-family: var(--font-sans);
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    outline: none;
    transition: border-color var(--transition-fast);
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2394a3b8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: var(--space-7);
    cursor: pointer;
}

.map-price-select:focus {
    border-color: var(--accent);
}

.map-price-select option {
    background: var(--bg-card);
    color: var(--text-primary);
}

.map-price-sep {
    color: var(--text-tertiary);
    font-size: var(--text-sm);
}


/* === LAYER TOGGLES === */

.map-layer-toggles {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.map-layer-toggle {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-sm);
    color: var(--text-secondary);
    cursor: pointer;
}

.map-layer-toggle input {
    display: none;
}

.map-layer-toggle__check {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-fast);
    flex-shrink: 0;
}

.map-layer-toggle input:checked + .map-layer-toggle__check {
    background: var(--accent);
    border-color: var(--accent);
}

.map-layer-toggle input:checked + .map-layer-toggle__check::after {
    content: '';
    width: 10px;
    height: 6px;
    border-left: 2px solid #fff;
    border-bottom: 2px solid #fff;
    transform: rotate(-45deg) translateY(-1px);
}


/* === APPLY BUTTON === */

.map-apply-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-3);
    background: var(--accent);
    color: #fff;
    font-size: var(--text-sm);
    font-weight: 600;
    border-radius: var(--radius-md);
    transition: all var(--transition-fast);
    width: 100%;
}

.map-apply-btn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.map-apply-btn svg {
    width: 16px;
    height: 16px;
}


/* === LEGEND === */

.map-legend {
    padding: var(--space-3) var(--space-5) var(--space-4);
    border-top: 1px solid var(--border);
}

.map-legend__title {
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-tertiary);
    margin-bottom: var(--space-2);
}

.map-legend__items {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-1) var(--space-4);
}

.map-legend__item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-xs);
    color: var(--text-secondary);
}

.map-legend__swatch {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    flex-shrink: 0;
}


/* === STATS BAR === */

.map-stats {
    position: absolute;
    bottom: var(--space-4);
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-5);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-secondary);
    white-space: nowrap;
}

.map-stats__item {
    font-variant-numeric: tabular-nums;
}

.map-stats__sep {
    color: var(--text-tertiary);
    opacity: 0.5;
}


/* === LOADING OVERLAY === */

.map-loading {
    position: absolute;
    top: var(--space-4);
    right: var(--space-4);
    z-index: 10;
    display: none;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-md);
    font-size: var(--text-sm);
    color: var(--text-secondary);
}

.map-loading--visible {
    display: flex;
}

.map-loading__spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: mapSpin 0.7s linear infinite;
}

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


/* === LEAFLET CUSTOM MARKER STYLES === */

.map-cluster-icon,
.map-pin-icon {
    background: none !important;
    border: none !important;
}

.map-cluster {
    border-radius: 50%;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    font-family: var(--font-sans);
    text-align: center;
    border: 2px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    transition: transform var(--transition-fast);
}

.map-cluster:hover {
    transform: scale(1.15);
    cursor: pointer;
}

.map-house {
    display: block;
    filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.5));
    transition: transform var(--transition-fast);
}

.map-pin-icon:hover .map-house {
    transform: scale(1.3);
}


/* === LEAFLET POPUP OVERRIDES === */

.leaflet-popup-content-wrapper {
    background: var(--bg-card) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border) !important;
    border-radius: var(--radius-lg) !important;
    box-shadow: var(--shadow-xl) !important;
    padding: 0 !important;
    font-family: var(--font-sans) !important;
}

.leaflet-popup-content {
    margin: 0 !important;
    font-size: var(--text-sm) !important;
    line-height: 1.5 !important;
}

.leaflet-popup-tip {
    background: var(--bg-card) !important;
    border: 1px solid var(--border) !important;
    box-shadow: none !important;
}

.leaflet-popup-close-button {
    color: var(--text-tertiary) !important;
    font-size: 18px !important;
    width: 28px !important;
    height: 28px !important;
    line-height: 28px !important;
}

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


/* === MAP POPUP CONTENT === */

.map-popup {
    padding: var(--space-4);
    min-width: 180px;
}

.map-popup__title {
    font-weight: 700;
    font-size: var(--text-sm);
    margin-bottom: var(--space-2);
    display: block;
    color: var(--text-primary);
}

.map-popup__title--link {
    text-decoration: none;
    transition: color var(--transition-fast);
}

.map-popup__title--link:hover {
    color: var(--accent);
}

.map-popup__price {
    font-size: var(--text-lg);
    font-weight: 800;
    letter-spacing: -0.02em;
    color: var(--accent);
    margin-bottom: var(--space-2);
}

.map-popup__stat,
.map-popup__meta,
.map-popup__date {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    margin-bottom: var(--space-1);
}

.map-popup__link {
    display: inline-block;
    margin-top: var(--space-2);
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--accent);
    text-decoration: none;
}

.map-popup__link:hover {
    text-decoration: underline;
}


/* === LEAFLET ZOOM CONTROLS === */

.leaflet-control-zoom {
    border: 1px solid var(--border) !important;
    border-radius: var(--radius-md) !important;
    box-shadow: var(--shadow-md) !important;
    overflow: hidden;
}

.leaflet-control-zoom a {
    background: var(--bg-card) !important;
    color: var(--text-primary) !important;
    border-bottom: 1px solid var(--border) !important;
    width: 36px !important;
    height: 36px !important;
    line-height: 36px !important;
    font-size: 18px !important;
}

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

.leaflet-control-zoom a:last-child {
    border-bottom: none !important;
}


/* === MOBILE RESPONSIVE === */

/* === THEMED SCROLLBARS === */

.map-controls ::-webkit-scrollbar {
    width: 6px;
}

.map-controls ::-webkit-scrollbar-track {
    background: transparent;
}

.map-controls ::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

.map-controls ::-webkit-scrollbar-thumb:hover {
    background: var(--border-hover);
}

.map-controls * {
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}


/* === MOBILE RESPONSIVE === */

@media (max-width: 768px) {
    .map-controls {
        width: calc(100% - var(--space-8));
        left: var(--space-4);
        top: var(--space-4);
    }

    .map-controls--collapsed {
        width: auto;
    }

    .map-controls__body {
        max-height: 50vh;
    }

    .map-stats {
        bottom: var(--space-3);
        font-size: var(--text-xs);
        padding: var(--space-2) var(--space-3);
    }

    .map-legend__items {
        grid-template-columns: 1fr 1fr 1fr;
    }
}
