/* ===================================================
 * TRENJ - Mobile Optimization for Chalets Page
 * تحسينات تجربة الموبايل لصفحة الشاليهات
 * =================================================== */

/* ===== 1. Auto-hide Header on Scroll + Compact Header ===== */
@media (max-width: 768px) {
    /* Fixed header on mobile — out of layout flow, no white gap */
    .header {
        position: fixed !important;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
        transition: transform 0.3s ease, box-shadow 0.3s ease !important;
        will-change: transform;
    }
    .header.header-hidden {
        transform: translateY(-100%);
        box-shadow: none;
        /* pointer-events: none; -- removed: was blocking taps on the user-menu button */
    }
    .header.header-visible {
        transform: translateY(0);
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }

    /* Compensate for fixed header — chalets page uses full viewport */
    .airbnb-page-wrapper {
        padding-top: 0;
    }
    .main-container {
        height: calc(100vh - 52px) !important;
    }
    
    /* Compact header on chalets page */
    .header .header-content {
        flex-direction: row !important;
        gap: 0 !important;
        padding: 0.5rem 1rem !important;
    }
    
    /* Hide main nav on mobile - replaced by bottom nav */
    .header .main-nav {
        display: none !important;
    }
    
    .header .logo-text {
        font-size: 1.1rem !important;
    }
    
    .header .logo-img {
        height: 28px !important;
    }
}

/* ===== 2. Hide Map by Default on Mobile + Floating Map Button ===== */
@media (max-width: 950px) {
    .map-section {
        display: none !important;
        position: fixed;
        top: 0;
        left: 0;
        width: 100% !important;
        height: 100vh !important;
        z-index: 2000;
        background: white;
    }
    .map-section.mobile-map-open {
        display: block !important;
    }
    
    .listings-section {
        width: 100% !important;
        min-width: 100% !important;
    }
    
    .main-container {
        flex-direction: column;
    }

    /* زر إغلاق الخريطة - شريط سفلي */
    .mobile-map-close {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2100;
        background: #222;
        color: white;
        border: none;
        border-radius: 0;
        width: 100%;
        height: 52px;
        display: none;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        font-weight: 600;
        gap: 8px;
        cursor: pointer;
        letter-spacing: 0.3px;
    }
    .mobile-map-close.visible {
        display: flex;
    }

    /* الزر العائم للخريطة */
    .floating-map-btn {
        position: fixed;
        bottom: 80px; /* فوق الـ bottom nav */
        left: 50%;
        transform: translateX(-50%);
        z-index: 1100;
        background: #222;
        color: white;
        border: none;
        border-radius: 25px;
        padding: 12px 24px;
        font-size: 14px;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 8px;
        box-shadow: 0 4px 16px rgba(0,0,0,0.3);
        cursor: pointer;
        font-family: inherit;
        transition: all 0.3s ease;
    }
    .floating-map-btn:active {
        transform: translateX(-50%) scale(0.95);
    }
    .floating-map-btn i {
        font-size: 16px;
    }
    .floating-map-btn.hidden {
        display: none;
    }
}

@media (min-width: 951px) {
    .floating-map-btn,
    .mobile-map-close {
        display: none !important;
    }
}

/* ===== 3. New Header Layout ===== */

/* الصف الأول: العداد + زر الفلاتر */
.header-top-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.filters-sheet-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 10px;
    padding: 8px 14px;
    font-size: 13px;
    font-weight: 600;
    color: #222;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}
.filters-sheet-btn:hover {
    border-color: #222;
}
.filters-sheet-btn:active {
    background: #f7f7f7;
}
.filters-sheet-btn i {
    font-size: 14px;
    color: #717171;
}

.active-filters-badge {
    background: #FF385C;
    color: white;
    border-radius: 50%;
    min-width: 18px;
    height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 700;
    padding: 0 4px;
}

/* الصف الثاني: فلاتر سريعة */
.filters-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

.quick-filter-pill {
    display: flex;
    align-items: center;
    gap: 5px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 25px;
    padding: 7px 14px;
    font-size: 13px;
    font-weight: 500;
    color: #222;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    transition: all 0.2s;
    flex-shrink: 0;
}
.quick-filter-pill:hover {
    border-color: #222;
}
.quick-filter-pill:active {
    background: #f7f7f7;
}
.quick-filter-pill i {
    font-size: 12px;
    color: #717171;
}
.quick-filter-pill.active {
    background: #222;
    color: white;
    border-color: #222;
}
.quick-filter-pill.active i {
    color: white;
}
.quick-filter-pill.located {
    background: #E8F5E9;
    border-color: #4CAF50;
    color: #2E7D32;
}
.quick-filter-pill.located i {
    color: #4CAF50;
}

@media (max-width: 768px) {
    .filters-row {
        flex-wrap: nowrap !important;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        padding-bottom: 2px;
        gap: 6px !important;
    }
    .filters-row::-webkit-scrollbar {
        display: none;
    }
    
    .quick-filter-pill {
        font-size: 12px !important;
        padding: 6px 12px !important;
    }
    
    /* Hide desktop-only pills on mobile */
    .quick-filter-pill.desktop-only {
        display: none !important;
    }
}

/* ===== 4. Bottom Sheet for Filters (Advanced Filters) ===== */
@media (max-width: 768px) {
    /* Prevent body scroll when sheet is open */
    body.filter-sheet-open {
        overflow: hidden !important;
        position: fixed !important;
        width: 100% !important;
        height: 100% !important;
        touch-action: none !important;
    }
    
    .filter-bottom-sheet-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        /* Use dvh with fallback for real mobile browsers */
        height: 100vh;
        height: 100dvh;
        width: 100%;
        background: rgba(0,0,0,0.5);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
        z-index: 3000;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
        /* Prevent touch passthrough */
        touch-action: none;
        -webkit-tap-highlight-color: transparent;
    }
    .filter-bottom-sheet-overlay.open {
        opacity: 1;
        visibility: visible;
    }
    
    .filter-bottom-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        /* Use dvh for accurate mobile height, with fallback */
        max-height: 80vh;
        max-height: 80dvh;
        max-height: calc(80 * var(--dvh, 1vh));
        background: white;
        border-radius: 20px 20px 0 0;
        z-index: 3100;
        transform: translateY(100%);
        transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
        display: flex;
        flex-direction: column;
        /* Prevent overscroll bounce */
        overscroll-behavior: contain;
        /* Hardware acceleration */
        will-change: transform;
        -webkit-transform: translateY(100%);
        /* Safe area for notch devices */
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    .filter-bottom-sheet-overlay.open .filter-bottom-sheet {
        transform: translateY(0);
        -webkit-transform: translateY(0);
    }
    
    .filter-sheet-handle {
        position: relative;
        width: 40px;
        height: 5px;
        background: #ddd;
        border-radius: 3px;
        margin: 12px auto 8px;
        flex-shrink: 0;
        /* Make handle area tappable for drag */
        cursor: grab;
        touch-action: none;
    }
    /* Larger invisible touch area */
    .filter-sheet-handle::before {
        content: '';
        display: block;
        width: 120px;
        height: 40px;
        position: absolute;
        top: -18px;
        left: 50%;
        transform: translateX(-50%);
    }
    
    .filter-sheet-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 20px 16px;
        border-bottom: 1px solid #eee;
    }
    .filter-sheet-header h3 {
        font-size: 18px;
        font-weight: 700;
        color: #222;
        margin: 0;
    }
    .filter-sheet-close {
        background: #f0f0f0;
        border: none;
        border-radius: 50%;
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        font-size: 16px;
        color: #222;
    }
    .btn-apply-header {
        background: linear-gradient(135deg, #7CB342, #558B2F);
        color: white;
        border: none;
        border-radius: 8px;
        padding: 8px 16px;
        font-size: 14px;
        font-weight: 700;
        cursor: pointer;
        font-family: inherit;
        display: flex;
        align-items: center;
        gap: 4px;
        -webkit-tap-highlight-color: transparent;
        transition: background 0.15s;
        box-shadow: 0 2px 8px rgba(85, 139, 47, 0.35);
    }
    .btn-apply-header:active {
        background: linear-gradient(135deg, #558B2F, #33691E);
    }
    .btn-apply-header #filterResultCount {
        font-weight: 800;
    }
    
    .filter-sheet-body {
        padding: 20px;
        padding-bottom: 10px;
        overflow-y: auto;
        overflow-x: hidden;
        /* Modern scroll behavior */
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-y: contain;
        scroll-behavior: smooth;
        flex: 1;
        min-height: 0; /* Fix flexbox overflow */
        /* Allow vertical panning only */
        touch-action: pan-y;
        /* Scrollbar styling */
        scrollbar-width: thin;
        scrollbar-color: #ddd transparent;
    }
    .filter-sheet-body::-webkit-scrollbar {
        width: 4px;
    }
    .filter-sheet-body::-webkit-scrollbar-track {
        background: transparent;
    }
    .filter-sheet-body::-webkit-scrollbar-thumb {
        background: #ddd;
        border-radius: 2px;
    }
    
    .filter-sheet-section {
        margin-bottom: 24px;
    }
    .filter-sheet-section label {
        display: block;
        font-size: 14px;
        font-weight: 600;
        color: #222;
        margin-bottom: 10px;
    }
    
    /* === Price Double Range Slider (Booking-style) === */
    .price-slider-display {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        margin-bottom: 20px;
    }
    .price-slider-val {
        background: #f7f7f7;
        border: 1.5px solid #ddd;
        border-radius: 10px;
        padding: 8px 16px;
        font-size: 15px;
        font-weight: 700;
        color: #222;
        min-width: 90px;
        text-align: center;
        transition: border-color 0.2s;
    }
    .price-slider-val.active {
        border-color: #222;
    }
    .price-slider-sep {
        color: #b0b0b0;
        font-size: 16px;
    }

    .price-slider-container {
        position: relative;
        width: 100%;
        height: 40px;
        display: flex;
        align-items: center;
        direction: ltr; /* force LTR so range inputs work correctly */
    }
    .price-slider-track {
        position: absolute;
        width: 100%;
        height: 4px;
        background: #ddd;
        border-radius: 2px;
        pointer-events: none;
    }
    .price-slider-range {
        position: absolute;
        height: 100%;
        background: #222;
        border-radius: 2px;
        pointer-events: none;
        transition: left 0.05s, width 0.05s;
    }

    /* Native range inputs overlaid */
    .price-slider-thumb {
        position: absolute;
        width: 100%;
        height: 40px;
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
        background: transparent;
        pointer-events: none;
        margin: 0;
        padding: 0;
        outline: none;
    }

    /* Webkit (Chrome, Safari, Edge) */
    .price-slider-thumb::-webkit-slider-runnable-track {
        height: 4px;
        background: transparent;
    }
    .price-slider-thumb::-webkit-slider-thumb {
        -webkit-appearance: none;
        /* Larger touch target for mobile */
        width: 32px;
        height: 32px;
        border-radius: 50%;
        background: white;
        border: 2.5px solid #222;
        box-shadow: 0 2px 8px rgba(0,0,0,0.18);
        cursor: pointer;
        pointer-events: auto;
        margin-top: -14px;
        transition: transform 0.15s, box-shadow 0.15s;
        /* Prevent double-tap zoom */
        touch-action: manipulation;
    }
    .price-slider-thumb::-webkit-slider-thumb:active {
        transform: scale(1.2);
        box-shadow: 0 4px 14px rgba(0,0,0,0.3);
    }

    /* Firefox */
    .price-slider-thumb::-moz-range-track {
        height: 4px;
        background: transparent;
        border: none;
    }
    .price-slider-thumb::-moz-range-thumb {
        width: 24px;
        height: 24px;
        border-radius: 50%;
        background: white;
        border: 2px solid #222;
        box-shadow: 0 2px 6px rgba(0,0,0,0.15);
        cursor: pointer;
        pointer-events: auto;
    }
    .price-slider-thumb::-moz-range-thumb:active {
        transform: scale(1.15);
    }

    .price-slider-limits {
        display: flex;
        justify-content: space-between;
        font-size: 11px;
        color: #999;
        margin-top: 2px;
        padding: 0 2px;
        direction: ltr; /* match slider LTR direction */
    }
    
    .filter-chip-group {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    .filter-chip {
        /* Larger touch target */
        padding: 10px 18px;
        border: 1.5px solid #ddd;
        border-radius: 25px;
        font-size: 14px;
        cursor: pointer;
        background: white;
        color: #222;
        transition: all 0.2s ease;
        font-family: inherit;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        user-select: none;
        -webkit-user-select: none;
    }
    .filter-chip:active {
        transform: scale(0.95);
    }
    .filter-chip.active {
        background: #222;
        color: white;
        border-color: #222;
    }
    
    .filter-sheet-actions {
        padding: 14px 20px;
        padding-bottom: calc(14px + env(safe-area-inset-bottom));
        border-top: 1px solid #eee;
        display: flex;
        gap: 12px;
        flex-shrink: 0;
        background: white;
    }
    .filter-sheet-actions .btn-apply {
        flex: 1;
        padding: 14px;
        background: #222;
        color: white;
        border: none;
        border-radius: 12px;
        font-size: 15px;
        font-weight: 700;
        cursor: pointer;
        font-family: inherit;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        -webkit-tap-highlight-color: transparent;
    }
    .filter-sheet-actions .btn-apply:active {
        background: #000;
    }
    .filter-sheet-actions .btn-apply #filterResultCount {
        font-weight: 800;
    }
    .filter-sheet-actions .btn-reset {
        padding: 14px 20px;
        background: none;
        border: 1px solid #ddd;
        border-radius: 12px;
        font-size: 14px;
        font-weight: 600;
        color: #222;
        cursor: pointer;
        font-family: inherit;
    }
}

/* ===== 5. Mobile Card Improvements ===== */
@media (max-width: 768px) {
    .listings-grid {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
        padding: 12px !important;
    }
    
    .chalet-image-container {
        height: 230px !important; /* fixed on mobile for uniformity */
        padding-bottom: 0 !important;
        aspect-ratio: unset !important;
        border-radius: 14px !important;
    }
    
    .chalet-image-container img {
        width: 100%;
        height: 100%;
        object-fit: cover;
    }
    
    .chalet-info {
        padding: 10px 4px 6px !important;
    }
    
    /* New card layout */
    .card-row-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 8px;
        margin-bottom: 3px;
    }
    
    .card-name {
        font-size: 15px !important;
        font-weight: 600;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        flex: 1;
    }
    
    .card-rating {
        font-size: 13px;
        flex-shrink: 0;
    }
    
    .card-location {
        font-size: 13px !important;
        color: #717171 !important;
        margin-bottom: 5px;
    }
    
    .card-features {
        display: flex;
        align-items: center;
        gap: 14px;
        margin-bottom: 5px;
    }
    
    .card-feat {
        font-size: 13px;
        color: #717171;
    }
    
    .card-feat i {
        font-size: 12px;
        color: #484848;
    }
    
    .card-price {
        margin-top: 2px;
    }
    
    .card-price-amount {
        font-size: 16px !important;
        font-weight: 600;
    }
    
    .card-price-currency {
        font-size: 14px;
    }
    
    .card-price-per {
        font-size: 13px;
        color: #717171;
    }
    
    /* Hide old classes (no longer used) */
    .chalet-description,
    .chalet-dates,
    .distance-badge {
        display: none !important;
    }
    
    .chalet-card {
        width: 100% !important;
        max-width: none !important;
        margin: 0 !important;
        border-radius: 14px;
        box-shadow: none !important;
    }
    
    /* Remove hover effect on mobile */
    .chalet-card:hover {
        transform: none !important;
        box-shadow: none !important;
    }
    
    /* Better tap feedback */
    .chalet-card:active {
        transform: scale(0.98) !important;
        transition: transform 0.1s !important;
    }
    
    /* Show nav arrows and dots always on mobile (no hover) */
    .slider-nav {
        opacity: 1 !important;
        width: 30px !important;
        height: 30px !important;
    }
    
    .slider-dots {
        opacity: 1 !important;
    }
}

/* ===== 6. Bottom Navigation Bar ===== */
@media (max-width: 768px) {
    .mobile-bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 64px;
        background: white;
        border-top: 1px solid #eee;
        display: flex;
        align-items: center;
        justify-content: space-around;
        z-index: 1500;
        padding: 0 8px;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    
    .bottom-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        text-decoration: none;
        color: #717171;
        font-size: 10px;
        font-weight: 500;
        padding: 6px 12px;
        border-radius: 8px;
        transition: color 0.2s;
        -webkit-tap-highlight-color: transparent;
        border: none;
        background: none;
        cursor: pointer;
        font-family: inherit;
    }
    .bottom-nav-item i {
        font-size: 20px;
        line-height: 1;
    }
    .bottom-nav-item.active {
        color: #FF385C;
    }
    .bottom-nav-item:active {
        color: #FF385C;
    }
    
    /* Add bottom padding to page content to avoid nav overlap */
    .airbnb-page-wrapper {
        padding-bottom: 70px !important;
    }
    
    /* Adjust floating map button position */
    .floating-map-btn {
        bottom: 80px !important;
    }
    
    /* Adjust sync indicator */
    .sync-indicator {
        bottom: 140px !important;
    }
    
    /* Hide the old floating map button */
    .floating-map-button {
        display: none !important;
    }
}

@media (min-width: 769px) {
    .mobile-bottom-nav {
        display: none !important;
    }
}

/* ===== 7. Listings Header Mobile Fix ===== */
@media (max-width: 768px) {
    .listings-header {
        padding: 10px 12px !important;
        position: sticky;
        top: 0;
        z-index: 100;
        transition: transform 0.25s ease, opacity 0.25s ease;
        will-change: transform, opacity;
    }
    
    /* Hide filter bar — GPU only, no layout reflow */
    .listings-header.filters-hidden {
        transform: translateY(-100%);
        opacity: 0;
        pointer-events: none;
    }
    .listings-header.filters-visible {
        transform: translateY(0);
        opacity: 1;
        pointer-events: auto;
    }
    
    .header-top-row {
        margin-bottom: 8px;
    }
    
    .results-count {
        font-size: 13px !important;
        margin-bottom: 0 !important;
    }
    
    .filters-sheet-btn {
        padding: 6px 12px;
        font-size: 12px;
    }
}

/* ===== 8. Skeleton Loading Mobile ===== */
@media (max-width: 768px) {
    .skeleton-card {
        border-radius: 14px;
    }
    .skeleton-image {
        height: 0;
        padding-bottom: 75%; /* 4:3 ratio */
        border-radius: 14px;
    }
}

/* ===== 9. Map Toggle Button Mobile ===== */
@media (max-width: 950px) {
    .map-toggle {
        display: none !important;
    }
}

/* ===== 10. (Removed - handled by .desktop-only class) ===== */

/* ===== 11. Smooth transitions ===== */
@media (max-width: 768px) {
    * {
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Prevent text selection on interactive elements */
    .filter-button,
    .bottom-nav-item,
    .floating-map-btn,
    .mobile-filter-trigger,
    .filter-chip {
        -webkit-user-select: none;
        user-select: none;
    }
}

/* ===== 12. Leaflet Map Mobile Fixes ===== */
@media (max-width: 950px) {
    .map-section.mobile-map-open #map {
        width: 100% !important;
        height: 100vh !important;
    }
    
    .map-section.mobile-map-open .map-toggle {
        display: none !important;
    }
    
    /* Price markers smaller on mobile */
    .price-marker-content {
        font-size: 12px !important;
        padding: 4px 10px !important;
    }
}
