/**
 * Mobile Responsiveness Enhancement
 * Ensures all pages fit perfectly on mobile, tablet, and desktop views
 * Breakpoints: 320px, 480px, 768px, 1024px, 1200px
 */

/* ===== GLOBAL MOBILE FIXES ===== */

/* Ensure body has no horizontal overflow */
body, html {
    width: 100%;
    overflow-x: hidden;
}

/* Container responsive widths */
.container {
    width: 100%;
    padding-left: 1rem;
    padding-right: 1rem;
    margin-left: auto;
    margin-right: auto;
}

/* Extra small devices (320px - 479px) */
@media (max-width: 479px) {
    /* Typography scaling for small screens */
    h1 {
        font-size: 1.5rem !important;
    }
    
    h2 {
        font-size: 1.25rem !important;
    }
    
    h3 {
        font-size: 1.1rem !important;
    }
    
    h4, h5, h6 {
        font-size: 1rem !important;
    }
    
    body {
        font-size: 14px;
    }
    
    /* Navbar adjustments */
    .navbar {
        padding: 0.75rem 0 !important;
    }
    
    .navbar-brand {
        font-size: 1.1rem !important;
    }
    
    .navbar-brand div {
        width: 32px !important;
        height: 32px !important;
    }
    
    /* Navigation spacing */
    .navbar-nav {
        gap: 0 !important;
    }
    
    .nav-link {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.9rem;
    }
    
    /* Button sizing */
    .btn {
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }
    
    .btn-lg {
        padding: 0.75rem 1.5rem;
        font-size: 1rem;
    }
    
    /* Form controls */
    .form-control, .form-select {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 0.75rem 0.75rem;
    }
    
    /* Card spacing */
    .card {
        margin-bottom: 1rem;
        border-radius: 12px;
    }
    
    /* Remove horizontal margins on columns */
    .row > * {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
    
    /* Images responsive */
    img {
        max-width: 100%;
        height: auto;
    }
    
    /* Tables scrollable on mobile */
    .table-responsive {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    table {
        width: 100%;
        font-size: 0.85rem;
    }
    
    /* Modals full screen on mobile */
    .modal-dialog {
        margin: 0;
        width: 100%;
        max-width: 100%;
    }
    
    /* Hero sections */
    .hero, .hero-section {
        padding: 2rem 1rem !important;
        min-height: auto;
    }
    
    /* Grid adjustments */
    .grid-container, .grid-2, .grid-3, .grid-4 {
        grid-template-columns: 1fr !important;
        gap: 1rem;
    }
    
    /* Sticky elements */
    .sticky-top {
        position: static !important;
    }
    
    /* Remove gap utilities on mobile */
    .gap-4, .gap-5 {
        gap: 1rem !important;
    }
    
    /* Flexbox wrapping */
    .flex-wrap {
        flex-wrap: wrap;
    }
}

/* Small devices (480px - 767px) */
@media (min-width: 480px) and (max-width: 767px) {
    .container {
        padding-left: 1.5rem;
        padding-right: 1.5rem;
    }
    
    h1 {
        font-size: 1.75rem;
    }
    
    h2 {
        font-size: 1.5rem;
    }
    
    h3 {
        font-size: 1.25rem;
    }
    
    /* Navbar */
    .navbar-nav {
        gap: 0.5rem;
    }
    
    .nav-link {
        padding: 0.5rem 1rem;
    }
    
    /* Cards */
    .card {
        margin-bottom: 1.25rem;
    }
    
    /* Grid for tablets */
    .grid-container, .grid-3 {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    .grid-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    /* Two column layout on tablet */
    .row > .col-md-6 {
        flex: 0 0 100%;
        max-width: 100%;
    }
    
    /* Side-by-side on tablet if space permits */
    .row > .col-md-6.responsive-pair {
        flex: 0 0 calc(50% - 0.5rem);
        max-width: calc(50% - 0.5rem);
    }
}

/* Medium devices (768px - 1023px) - tablets */
@media (min-width: 768px) and (max-width: 1023px) {
    .container {
        max-width: 750px;
    }
    
    .grid-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    /* Sticky sidebars should be non-sticky on tablet */
    .sticky-top-card {
        position: relative !important;
        top: auto !important;
    }
    
    /* Reduce spacing */
    .py-5 {
        padding-top: 2rem !important;
        padding-bottom: 2rem !important;
    }
}

/* Large devices (1024px - 1199px) */
@media (min-width: 1024px) and (max-width: 1199px) {
    .container {
        max-width: 960px;
    }
    
    /* Adjust grid */
    .grid-3 {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .grid-4 {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Extra large devices (1200px+) */
@media (min-width: 1200px) {
    .container {
        max-width: 1200px;
    }
}

/* ===== SPECIFIC COMPONENT MOBILE FIXES ===== */

/* Listing cards responsive */
@media (max-width: 767px) {
    .listing-card {
        margin-bottom: 1rem !important;
    }
    
    .listing-card-image {
        height: 200px !important;
    }
    
    .listing-card-title {
        font-size: 1rem;
    }
    
    .listing-price {
        font-size: 1.25rem;
    }
    
    .listing-details {
        display: flex;
        gap: 0.5rem;
        flex-wrap: wrap;
        font-size: 0.85rem;
    }

    .filter-bar-container {
        position: static;
        top: auto;
        padding-top: 0.75rem;
        padding-bottom: 0.75rem;
    }

    .filter-scroll-wrapper {
        flex-wrap: wrap;
        width: 100%;
        gap: 0.5rem;
    }

    #listingsFilterForm {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.75rem;
    }

    .filter-pill {
        width: 100%;
        min-height: 44px;
        white-space: normal;
    }

    .filter-pill input,
    .filter-pill select,
    #listingsFilterSubmit,
    #sortListings {
        width: 100% !important;
        min-width: 0;
    }

    #all-listings-container {
        display: block !important;
    }

    .masonry-column {
        width: 100% !important;
        gap: 1rem;
    }

    .listing-pin-card,
    .premium-secondary-card {
        border-radius: 16px !important;
    }

    .listing-pin-card .card-body {
        padding: 0.95rem !important;
    }

    .property-title,
    .property-location-text {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: unset !important;
    }

    .property-features,
    .premium-chip-row {
        gap: 0.6rem;
        flex-wrap: wrap;
        justify-content: flex-start;
    }

    .feature-item,
    .feature-chip {
        min-width: calc(50% - 0.3rem);
        justify-content: center;
    }

    .compare-checkbox-wrapper {
        top: 0.75rem;
        right: 0.75rem;
        width: 40px;
        height: 40px;
    }

    .compare-toggle-text {
        display: none;
    }

    #compare-action-bar {
        width: calc(100% - 1rem) !important;
        max-width: none;
        padding: 0.75rem 0.9rem !important;
        border-radius: 16px !important;
    }

    #compare-action-bar .btn {
        min-width: 0;
        flex: 1 1 48%;
    }
}

/* Hero gallery on mobile */
@media (max-width: 768px) {
    .hero-gallery {
        grid-template-columns: 1fr !important;
        grid-template-rows: auto auto auto;
        height: auto;
        max-height: 400px;
    }
    
    .hero-gallery .main-img {
        grid-column: 1 !important;
        grid-row: 1 !important;
        height: 250px;
    }
    
    .hero-gallery .side-img-3,
    .hero-gallery .side-img-4 {
        display: none !important;
    }
    
    .hero-gallery .img-wrap {
        height: 100px;
    }
}

/* Navigation on mobile */
@media (max-width: 991px) {
    .navbar-collapse {
        padding-top: 1rem;
    }
    
    .navbar-nav {
        flex-direction: column;
    }
    
    .nav-link {
        padding: 0.5rem 0 !important;
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    
    .nav-link:last-child {
        border-bottom: none;
    }
    
    /* Language dropdown on mobile */
    .dropdown-menu {
        position: static;
        float: none;
        width: 100%;
        margin-top: 0.5rem;
        box-shadow: none;
        border: 1px solid rgba(0, 0, 0, 0.1);
    }
}

/* Forms on mobile */
@media (max-width: 767px) {
    .form-group, .mb-3 {
        margin-bottom: 1.5rem !important;
    }
    
    label {
        font-size: 0.9rem;
        margin-bottom: 0.5rem;
    }
    
    /* Ensure forms are full width */
    .form-control, .form-select, .input-group {
        width: 100%;
    }
    
    /* Button groups */
    .btn-group, .btn-group-vertical {
        display: flex;
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group > .btn,
    .btn-group-vertical > .btn {
        width: 100%;
        margin-bottom: 0.5rem;
    }
}

/* Footer on mobile */
@media (max-width: 767px) {
    footer {
        border-top-left-radius: 20px !important;
        border-top-right-radius: 20px !important;
        padding-top: 2rem !important;
    }
    
    .row > .col-lg-4,
    .row > .col-md-6 {
        margin-bottom: 2rem;
    }
    
    .row > .col-lg-4:last-child,
    .row > .col-md-6:last-child {
        margin-bottom: 0;
    }
}

/* Maps on mobile */
@media (max-width: 768px) {
    #map {
        height: 300px !important;
        border-radius: 12px;
    }
    
    .map-controls {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .map-controls button {
        width: 100%;
        padding: 0.75rem;
    }
}

/* Sticky cards on mobile - make them static */
@media (max-width: 991px) {
    .sticky-top-card {
        position: relative !important;
        top: auto !important;
    }
    
    .sticky-top {
        position: static !important;
    }
}

/* Ensure all images are responsive */
img, picture, video {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Fix overflow on small screens */
.overflow-auto {
    overflow-x: auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Touch-friendly spacing */
@media (max-width: 767px) {
    button, a.btn, .btn {
        min-height: 44px; /* Touch target minimum */
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 44px;
        min-height: 44px;
        cursor: pointer;
    }
}

/* Prevent text selection issues on mobile */
@media (max-width: 767px) {
    body {
        -webkit-user-select: none;
        user-select: none;
    }
    
    input, textarea, button, a.btn, label {
        -webkit-user-select: text;
        user-select: text;
    }
}

/* Fix common layout issues */
@media (max-width: 767px) {
    .d-none-mobile {
        display: none !important;
    }
    
    .d-block-mobile {
        display: block !important;
    }
    
    .text-center-mobile {
        text-align: center;
    }
    
    /* Prevent margin collapse issues */
    .container {
        display: block;
    }
    
    /* Ensure flex containers work on mobile */
    .d-flex {
        display: flex;
        flex-wrap: wrap;
    }
}

/* Performance optimization for mobile */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Dark mode - ensure readability on mobile */
@media (prefers-color-scheme: dark) {
    body {
        background: #0f172a;
        color: #f8fafc;
    }
    
    .card {
        background: #1e293b;
        border-color: #334155;
    }
}

/* Print styles - ensure mobile pages print correctly */
@media print {
    body {
        width: 100%;
        margin: 0;
        padding: 0;
    }
    
    .navbar, footer, .sticky-top, .no-print {
        display: none !important;
    }
}
