/**
 * HouzezHub OSM Mobile UI/UX Enhancements (v1.2.19+)
 * Bottom nav, bottom sheet filters, infinite scroll, geolocation, responsive design
 * ENHANCED: Fixed search field visibility, improved filter dropdown z-index
 * @version 1.0.0
 */

/* ============================================================
   MOBILE SEARCH FIELD & HEADER FIXES
   ============================================================ */

@media (max-width: 1023px) {
    /* Ensure search header is above all content */
    .rci-header {
        position: relative;
        z-index: 1040 !important;
        flex-shrink: 0;
    }

    /* Search field must be fully visible on mobile */
    .rci-header-search-bar {
        width: 100%;
        z-index: 1040;
    }

    .osm-filters-panel-wrapper {
        z-index: 1050;
    }

    .osm-advanced-filters-dropdown {
        z-index: 2000;
        position: fixed;
        left: 8px;
        right: 8px;
        width: auto;
        max-width: calc(100vw - 16px);
        top: auto;
    }

    /* Hide primary search input label on mobile (already visible) */
    .rci-header-search-bar .form-control::placeholder {
        text-align: left;
        color: #999;
    }

    /* Adjust filter buttons for mobile */
    .filter-group-actions {
        flex-wrap: wrap;
        justify-content: flex-end;
    }

    .filter-group-actions .btn {
        padding: 8px 12px;
        font-size: 13px;
        height: auto;
    }

    /* Ensure mobile content is below search header */
    .rci-main-content-wrapper {
        margin-top: 0;
    }

    /* --- Mobile Button Layout Fix --- */
    .rci-header-search-bar .filters-inner-wrap {
        flex-wrap: wrap;
        gap: 8px;
    }

    .rci-header-search-bar .filter-group-vector-search {
        order: 1;
        flex-basis: 100%;
    }

    .rci-header-search-bar .filter-group-actions {
        order: 2;
        flex-basis: 100%;
        justify-content: flex-start;
    }

    .rci-header-search-bar .filter-group-actions #osm-apply-main-filters-button,
    .rci-header-search-bar .filter-group-actions #osm-reset-map-view-button {
        display: block;
        width: auto;
        flex-grow: 1;
    }


}

@media (max-width: 1023px) {
    body.osm-map-fullscreen-active {
        --rci-bottom-nav-height-mobile: 56px;
    }

    .realtor-ca-inspired-interface {
        padding-bottom: 56px;
        margin-top: 64px; /* push below theme header on mobile so search bar stays visible */
    }

    /* Fixed bottom view toggler toolbar */
    .rci-mobile-view-toggler {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: white;
        border-top: 1px solid #e0e0e0;
        display: flex;
        justify-content: space-around;
        align-items: center;
        z-index: 1000;
        padding-bottom: max(0px, env(safe-area-inset-bottom));
        box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
    }

    .rci-view-toggle-btn {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 100%;
        cursor: pointer;
        border: none;
        background: none;
        color: #666;
        font-size: 11px;
        transition: all 0.2s;
        user-select: none;
        padding: 8px 0;
        text-decoration: none;
    }

    .rci-view-toggle-btn:hover {
        background: #f5f5f5;
        color: #333;
    }

    .rci-view-toggle-btn.active {
        color: #0066cc;
        background: #f0f5ff;
    }

    .rci-view-toggle-btn i {
        font-size: 20px;
        margin-bottom: 4px;
        display: block;
    }

    /* Safe area for notched devices */
    @supports (padding: max(0px)) {
        .rci-mobile-view-toggler {
            padding-bottom: max(0px, env(safe-area-inset-bottom));
        }
    }

    /* ============================================================
       TASK 19: MOBILE FILTER BOTTOM SHEET
       ============================================================ */

    .osm-filters-bottom-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: #fdfdfd;
        border-radius: 18px 18px 0 0;
        max-height: 72vh;
        overflow-y: auto;
        z-index: 2000;
        display: none;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 0 -8px 20px rgba(0, 0, 0, 0.18);
        border: 1px solid #e6e6e6;
    }

    .osm-filters-bottom-sheet.is-open {
        display: block;
        transform: translateY(0);
    }

    .osm-bottom-sheet-header {
        position: sticky;
        top: 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 14px 16px;
        border-bottom: 1px solid #ededed;
        background: #fdfdfd;
        border-radius: 18px 18px 0 0;
        z-index: 10;
        gap: 12px;
    }

    .osm-bottom-sheet-header-title {
        font-weight: 600;
        font-size: 15px;
        color: #111;
    }

    .osm-bottom-sheet-close-btn {
        width: 32px;
        height: 32px;
        border: none;
        background: none;
        cursor: pointer;
        font-size: 20px;
        color: #666;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: color 0.2s;
    }

    .osm-bottom-sheet-close-btn:active {
        color: #0066cc;
    }

    /* Scroll area styling */
    .osm-filters-bottom-sheet {
        overflow: hidden;
    }
    .osm-filters-bottom-sheet-content {
        overflow-y: auto;
        max-height: calc(72vh - 130px);
        padding: 12px 16px 100px;
    }
    .osm-filters-bottom-sheet-content::-webkit-scrollbar {
        width: 6px;
    }
    .osm-filters-bottom-sheet-content::-webkit-scrollbar-track {
        background: transparent;
    }
    .osm-filters-bottom-sheet-content::-webkit-scrollbar-thumb {
        background: #c8c8c8;
        border-radius: 10px;
    }
    .osm-filters-bottom-sheet-content::-webkit-scrollbar-thumb:hover {
        background: #a8a8a8;
    }

    .osm-bottom-sheet-drag-handle {
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: #d0d0d0;
        border-radius: 2px;
    }

    .osm-bottom-sheet-content {
        padding: 16px;
    }

    /* Carded filter sections + pill inputs */
    .osm-bottom-sheet-content .filter-group {
        background: #fff;
        border: 1px solid #ededed;
        border-radius: 14px;
        padding: 12px;
        margin-bottom: 12px;
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
    }

    .osm-bottom-sheet-content .filter-group label {
        font-weight: 600;
        color: #111;
        margin-bottom: 8px;
        display: block;
    }

    .osm-bottom-sheet-content .form-control,
    .osm-bottom-sheet-content select,
    .osm-bottom-sheet-content input[type="text"],
    .osm-bottom-sheet-content input[type="number"] {
        background: #fafafa;
        border: 1px solid #e2e2e2;
        border-radius: 12px;
        height: 44px;
        font-size: 14px;
        box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.03);
    }

    .osm-bottom-sheet-content .btn,
    .osm-bottom-sheet-content .btn-primary,
    .osm-bottom-sheet-content .btn-secondary {
        border-radius: 999px;
        border: 1px solid #e4e4e4;
        background: #fff;
        color: #333;
        padding: 10px 14px;
        font-weight: 600;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    }

    .osm-bottom-sheet-content .btn-primary {
        background: #111;
        color: #fff;
        border-color: #111;
    }

    .osm-bottom-sheet-content .btn-secondary {
        background: #f5f5f5;
        color: #333;
    }

    .osm-bottom-sheet-content .btn:active {
        transform: translateY(1px);
    }

    .osm-bottom-sheet-content .houzez-range-field,
    .osm-bottom-sheet-content .range-slider {
        margin-top: 6px;
    }

    .osm-bottom-sheet-footer {
        position: sticky;
        bottom: 0;
        display: flex;
        gap: 12px;
        padding: 16px;
        border-top: 1px solid #e0e0e0;
        background: #fdfdfd;
        z-index: 10;
        box-shadow: 0 -6px 12px rgba(0, 0, 0, 0.05);
    }

    .osm-bottom-sheet-footer button {
        flex: 1;
        padding: 12px;
        border: 1px solid #e0e0e0;
        border-radius: 999px;
        font-weight: 700;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .osm-bottom-sheet-footer .btn-primary {
        background: #111;
        color: white;
        border-color: #111;
    }

    .osm-bottom-sheet-footer .btn-primary:active {
        background: #000;
    }

    .osm-bottom-sheet-footer .btn-secondary {
        background: #f3f3f3;
        color: #333;
    }

    .osm-bottom-sheet-footer .btn-secondary:active {
        background: #e5e5e5;
    }

    /* Backdrop for bottom sheet */
    .osm-bottom-sheet-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1999;
        display: none;
        opacity: 0;
        transition: opacity 0.3s;
    }

    .osm-bottom-sheet-backdrop.is-open {
        display: block;
        opacity: 1;
    }

    /* ============================================================
       TASK 20: MOBILE INFINITE SCROLL & PULL-TO-REFRESH
       ============================================================ */

    /* Infinite scroll loading indicator */
    .osm-infinite-scroll-loader {
        padding: 16px;
        text-align: center;
        display: none;
    }

    .osm-infinite-scroll-loader.is-loading {
        display: block;
    }

    .osm-infinite-scroll-spinner {
        display: inline-block;
        width: 24px;
        height: 24px;
        border: 3px solid #f0f0f0;
        border-top-color: #0066cc;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }

    @keyframes spin {
        to {
            transform: rotate(360deg);
        }
    }

    .osm-infinite-scroll-text {
        margin-top: 8px;
        font-size: 12px;
        color: #999;
    }

    /* Pull-to-refresh */
    .osm-pull-to-refresh {
        position: relative;
        overflow: hidden;
    }

    .osm-pull-to-refresh-header {
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #999;
        font-size: 12px;
        transform: translateY(-100%);
        transition: transform 0.3s;
        gap: 8px;
    }

    .osm-pull-to-refresh-header.is-pulling {
        transform: translateY(0);
    }

    .osm-pull-to-refresh-icon {
        font-size: 16px;
        transition: transform 0.3s;
    }

    .osm-pull-to-refresh-header.is-pulling .osm-pull-to-refresh-icon {
        transform: rotate(180deg);
    }

    /* Back to top button */
    .osm-back-to-top {
        position: fixed;
        right: 16px;
        bottom: 72px;
        width: 48px;
        height: 48px;
        background: #0066cc;
        color: white;
        border: none;
        border-radius: 24px;
        cursor: pointer;
        display: none;
        align-items: center;
        justify-content: center;
        font-size: 18px;
        box-shadow: 0 4px 12px rgba(0, 102, 204, 0.4);
        transition: all 0.2s;
        z-index: 500;
    }

    .osm-back-to-top.is-visible {
        display: flex;
    }

    .osm-back-to-top:active {
        transform: scale(0.95);
        box-shadow: 0 2px 8px rgba(0, 102, 204, 0.3);
    }

    /* ============================================================
       TASK 21: MOBILE PROPERTY INFOBOX (BOTTOM SHEET)
       ============================================================ */

    .osm-property-infobox-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        border-radius: 16px 16px 0 0;
        max-height: 85vh;
        overflow-y: auto;
        z-index: 2000;
        display: none;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.15);
    }

    .osm-property-infobox-sheet.is-open {
        display: block;
        transform: translateY(0);
    }

    .osm-property-infobox-gallery {
        position: relative;
        height: 240px;
        background: #f0f0f0;
        overflow: hidden;
    }

    .osm-property-infobox-image {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }

    .osm-property-infobox-gallery-nav {
        position: absolute;
        bottom: 12px;
        left: 50%;
        transform: translateX(-50%);
        display: flex;
        gap: 6px;
    }

    .osm-property-infobox-gallery-dot {
        width: 6px;
        height: 6px;
        background: rgba(255, 255, 255, 0.5);
        border-radius: 50%;
        cursor: pointer;
        transition: all 0.2s;
    }

    .osm-property-infobox-gallery-dot.is-active {
        background: white;
        width: 12px;
        border-radius: 3px;
    }

    .osm-property-infobox-close {
        position: absolute;
        top: 12px;
        right: 12px;
        width: 32px;
        height: 32px;
        background: rgba(0, 0, 0, 0.4);
        border: none;
        border-radius: 50%;
        color: white;
        cursor: pointer;
        font-size: 18px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: background 0.2s;
        z-index: 10;
    }

    .osm-property-infobox-close:active {
        background: rgba(0, 0, 0, 0.6);
    }

    .osm-property-infobox-content {
        padding: 16px;
    }

    .osm-property-infobox-price {
        font-size: 24px;
        font-weight: 700;
        color: #0066cc;
        margin-bottom: 8px;
    }

    .osm-property-infobox-address {
        font-size: 14px;
        color: #666;
        margin-bottom: 12px;
        line-height: 1.4;
    }

    .osm-property-infobox-meta {
        display: flex;
        gap: 16px;
        margin-bottom: 16px;
        padding-bottom: 16px;
        border-bottom: 1px solid #f0f0f0;
    }

    .osm-property-infobox-meta-item {
        flex: 1;
        text-align: center;
    }

    .osm-property-infobox-meta-label {
        font-size: 11px;
        color: #999;
        text-transform: uppercase;
        margin-bottom: 4px;
    }

    .osm-property-infobox-meta-value {
        font-size: 16px;
        font-weight: 600;
        color: #333;
    }

    .osm-property-infobox-actions {
        display: flex;
        gap: 8px;
        margin-top: 16px;
    }

    .osm-property-infobox-actions button {
        flex: 1;
        padding: 12px;
        border: 1px solid #ddd;
        background: white;
        border-radius: 6px;
        font-weight: 600;
        font-size: 13px;
        cursor: pointer;
        transition: all 0.2s;
    }

    .osm-property-infobox-actions button:active {
        background: #f0f0f0;
    }

    .osm-property-infobox-actions .btn-primary {
        background: #0066cc;
        color: white;
        border-color: #0066cc;
    }

    .osm-property-infobox-actions .btn-primary:active {
        background: #0052a3;
    }

    /* ============================================================
       TASK 22: MOBILE GEOLOCATION "NEAR ME"
       ============================================================ */

    .osm-geolocation-btn {
        display: flex;
        align-items: center;
        gap: 8px;
        width: 100%;
        padding: 12px;
        background: #0066cc;
        color: white;
        border: none;
        border-radius: 6px;
        font-weight: 600;
        font-size: 14px;
        cursor: pointer;
        margin-bottom: 12px;
        transition: all 0.2s;
    }

    .osm-geolocation-btn:active {
        background: #0052a3;
    }

    .osm-geolocation-btn.is-loading::before {
        content: '';
        display: inline-block;
        width: 14px;
        height: 14px;
        border: 2px solid rgba(255, 255, 255, 0.3);
        border-top-color: white;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }

    .osm-distance-badge {
        display: inline-block;
        background: #f0f0f0;
        color: #666;
        font-size: 11px;
        padding: 4px 8px;
        border-radius: 4px;
        margin-top: 4px;
    }

    .osm-user-location-marker {
        width: 30px;
        height: 30px;
        background: #0066cc;
        border: 3px solid white;
        border-radius: 50%;
        position: relative;
        box-shadow: 0 2px 8px rgba(0, 102, 204, 0.4);
    }

    .osm-user-location-marker::after {
        content: '';
        position: absolute;
        top: 50%;
        left: 50%;
        width: 6px;
        height: 6px;
        background: white;
        border-radius: 50%;
        transform: translate(-50%, -50%);
    }

    /* ============================================================
       TASK 23: MOBILE RESPONSIVE FONTS & TOUCH TARGETS
       ============================================================ */

    @media (max-width: 1023px) {
        body.osm-map-fullscreen-active {
            font-size: 16px;
            line-height: 1.6;
        }

        /* Ensure 48x48px touch targets */
        button,
        a.btn,
        .osm-bottom-nav-item,
        .property-list-item {
            min-height: 48px;
            min-width: 48px;
        }

        /* Increased padding for touch targets */
        input[type='text'],
        input[type='number'],
        input[type='email'],
        select,
        textarea {
            padding: 12px;
            font-size: 16px;
            min-height: 44px;
        }

        /* Heading sizes for mobile */
        h1 {
            font-size: 24px;
        }

        h2 {
            font-size: 20px;
        }

        h3 {
            font-size: 18px;
        }

        /* Increased line-height for readability */
        p,
        li {
            line-height: 1.6;
        }

        /* Safe area padding for notched devices */
        .osm-bottom-sheet-content {
            padding-bottom: max(16px, env(safe-area-inset-bottom));
        }

        .osm-mobile-bottom-nav {
            padding-bottom: max(0px, env(safe-area-inset-bottom));
        }
    }

    /* ============================================================
       GENERAL MOBILE RESPONSIVE
       ============================================================ */

    .property-list-item {
        padding: 12px;
        border-bottom: 1px solid #f0f0f0;
    }

    .property-list-item:active {
        background: #f5f5f5;
    }

    /* Hide desktop-only elements */
    .desktop-only {
        display: none;
    }
}

/* ============================================================
   TASK 25: UNIFIED LOADING STATES & SKELETON SCREENS
   ============================================================ */

/* Skeleton screen component */
.osm-skeleton {
    background: linear-gradient(
        90deg,
        #f0f0f0 25%,
        #e0e0e0 50%,
        #f0f0f0 75%
    );
    background-size: 200% 100%;
    animation: shimmer 2s infinite;
}

@keyframes shimmer {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

.osm-skeleton-item {
    background: white;
    padding: 12px;
    margin-bottom: 8px;
    border-radius: 6px;
}

.osm-skeleton-image {
    width: 100%;
    height: 160px;
    border-radius: 6px;
    margin-bottom: 12px;
}

.osm-skeleton-title {
    height: 16px;
    border-radius: 4px;
    margin-bottom: 8px;
}

.osm-skeleton-text {
    height: 12px;
    border-radius: 4px;
    margin-bottom: 6px;
}

.osm-skeleton-text:last-child {
    margin-bottom: 0;
}

/* Prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
    .osm-skeleton,
    .osm-bottom-sheet-header,
    .osm-mobile-bottom-nav,
    .osm-back-to-top,
    .property-list-item {
        animation: none;
        transition: none;
    }

    .osm-filters-bottom-sheet,
    .osm-property-infobox-sheet,
    .osm-filters-bottom-sheet.is-open,
    .osm-property-infobox-sheet.is-open {
        transform: none;
    }
}
