/**
 * MLD Saved Searches Mobile Modal Fix
 *
 * Fixes the issue where the Save button is below the screen
 * and cannot be scrolled to on mobile devices
 *
 * @package MLS_Listings_Display
 * @since 4.5.0
 */

/* Fix modal positioning and scrolling on mobile devices */
@media (max-width: 768px) {
    /* Ensure modal overlay allows scrolling */
    .bme-modal-overlay {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    }

    /* Fix modal content positioning */
    .bme-modal-content {
        position: relative !important;
        margin: 20px auto !important;
        margin-bottom: 20px !important;
        width: calc(100% - 40px) !important;
        max-width: 100% !important;
        max-height: none !important; /* Remove height restriction */
        min-height: auto !important;
    }

    /* Ensure body can scroll when modal is open */
    body.bme-modal-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }

    /* Make modal body scrollable if needed */
    .bme-modal-body {
        max-height: calc(100vh - 250px); /* Account for header and footer */
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Ensure form actions are always visible */
    .bme-form-actions {
        position: sticky;
        bottom: 0;
        background: white;
        padding: 16px;
        margin: 0 -28px -28px -28px; /* Compensate for modal body padding */
        border-top: 1px solid #e2e8f0;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    }

    /* Full width buttons on mobile */
    .bme-form-actions .bme-btn {
        width: 100%;
        padding: 14px 20px;
    }

    /* Adjust modal header for mobile */
    .bme-modal-header {
        padding: 20px;
        position: sticky;
        top: 0;
        z-index: 10;
    }

    /* Reduce form spacing on mobile */
    .bme-form-group {
        margin-bottom: 18px;
    }
}

/* For very small screens */
@media (max-width: 480px) {
    .bme-modal-content {
        margin: 10px !important;
        width: calc(100% - 20px) !important;
        border-radius: 12px;
    }

    .bme-modal-body {
        padding: 20px;
        max-height: calc(100vh - 220px);
    }

    /* Smaller text on very small screens */
    .bme-modal-header h3 {
        font-size: 20px;
    }

    .bme-modal-subtitle {
        font-size: 13px;
    }

    .bme-form-group label {
        font-size: 14px;
    }

    .bme-form-control,
    .bme-form-group input[type="text"],
    .bme-form-group textarea,
    .bme-form-group select {
        font-size: 14px;
        padding: 10px 14px;
    }
}

/* Alternative fix using flexbox for better mobile support */
@media (max-width: 768px) {
    .bme-modal-overlay {
        display: flex;
        align-items: flex-start;
        justify-content: center;
        padding: 20px 0;
        min-height: 100vh;
    }

    .bme-modal-content {
        display: flex;
        flex-direction: column;
        max-height: calc(100vh - 40px);
        margin: 0 20px !important;
    }

    .bme-modal-header {
        flex-shrink: 0;
    }

    .bme-modal-body {
        flex: 1 1 auto;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 20px;
    }

    .bme-form-actions {
        flex-shrink: 0;
        position: relative;
        margin: 0;
        padding: 20px;
        background: white;
        border-top: 2px solid #e2e8f0;
    }
}

/* iOS specific fixes for better scrolling */
@supports (-webkit-touch-callout: none) {
    /* iOS only */
    .bme-modal-overlay {
        height: -webkit-fill-available;
        min-height: -webkit-fill-available;
    }

    .bme-modal-content {
        max-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 40px);
    }
}

/* Fix for landscape mode on mobile */
@media (max-width: 768px) and (orientation: landscape) {
    .bme-modal-body {
        max-height: calc(100vh - 200px);
    }

    .bme-form-group {
        margin-bottom: 12px;
    }

    .bme-modal-header {
        padding: 15px 20px;
    }

    .bme-modal-header h3 {
        font-size: 18px;
    }
}