/**
 * Mobile Modal & Overlay Optimization
 * 
 * Goals:
 * - Full-screen modals on mobile for better focus
 * - Slide-up animation (native app feel)
 * - Swipe-to-dismiss gesture
 * - Sticky header/footer
 * - Better backdrop (darker, prevents interaction)
 * - Touch-friendly close button
 * - Keyboard-aware (input fields visible)
 * 
 * Phase: 3.6 - Modal Optimization
 * Date: December 30, 2025
 */

/* ========================================================================
   MOBILE MODALS (< 768px)
   ======================================================================== */

@media (max-width: 767px) {
    
    /* ====================================================================
       1. FULL-SCREEN MODALS
       ==================================================================== */
    
    /* Modal dialog full-screen */
    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
        width: 100% !important;
        height: 100vh !important;
        display: flex !important;
        align-items: stretch !important;
    }
    
    /* Modal content fills screen */
    .modal-content {
        width: 100%;
        height: 100vh;
        border-radius: 0 !important;
        border: none !important;
        display: flex;
        flex-direction: column;
    }
    
    /* Slide-up animation */
    .modal.fade .modal-dialog {
        transform: translateY(100%);
        transition: transform 0.3s ease-out;
    }
    
    .modal.show .modal-dialog {
        transform: translateY(0);
    }
    
    /* ====================================================================
       2. STICKY HEADER - Always visible
       ==================================================================== */
    
    .modal-header {
        position: sticky;
        top: 0;
        z-index: 1055;
        background: white;
        border-bottom: 2px solid #e5e7eb;
        padding: 16px 20px;
        min-height: 64px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        flex-shrink: 0;
    }
    
    /* Modal title */
    .modal-title {
        font-size: 20px;
        font-weight: 700;
        color: #111827;
        margin: 0;
        flex: 1;
        line-height: 1.3;
    }
    
    /* Close button */
    .modal-header .btn-close {
        min-width: 44px;
        min-height: 44px;
        padding: 12px;
        margin: 0;
        background-size: 20px;
        opacity: 0.8;
        border-radius: 8px;
        flex-shrink: 0;
    }
    
    .modal-header .btn-close:hover {
        opacity: 1;
        background-color: #f3f4f6;
    }
    
    /* Alternative close button (X icon) */
    .modal-close-button {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: none;
        background: transparent;
        color: #6b7280;
        font-size: 24px;
        cursor: pointer;
        border-radius: 8px;
        transition: all 0.2s ease;
    }
    
    .modal-close-button:hover,
    .modal-close-button:active {
        background-color: #f3f4f6;
        color: #111827;
    }
    
    /* ====================================================================
       3. SCROLLABLE BODY - Main content area
       ==================================================================== */
    
    .modal-body {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 20px;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    
    /* Prevent body scroll when modal open */
    body.modal-open {
        overflow: hidden !important;
        position: fixed;
        width: 100%;
    }
    
    /* ====================================================================
       4. STICKY FOOTER - Action buttons always visible
       ==================================================================== */
    
    .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 1055;
        background: white;
        border-top: 2px solid #e5e7eb;
        padding: 16px 20px;
        display: flex;
        flex-direction: column-reverse;
        gap: 12px;
        flex-shrink: 0;
        box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
    }
    
    /* Footer buttons full-width, stacked */
    .modal-footer .btn {
        width: 100%;
        min-height: 52px;
        font-size: 16px;
        font-weight: 600;
        margin: 0;
    }
    
    /* Primary button on top (due to column-reverse) */
    .modal-footer .btn-primary {
        order: -1;
    }
    
    /* ====================================================================
       5. BACKDROP - Darker, prevents interaction
       ==================================================================== */
    
    .modal-backdrop {
        background-color: rgba(0, 0, 0, 0.75) !important;
    }
    
    .modal-backdrop.fade {
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .modal-backdrop.show {
        opacity: 1;
    }
    
    /* ====================================================================
       6. SWIPE-TO-DISMISS - Gesture support
       ==================================================================== */
    
    /* Swipe indicator */
    .modal-swipe-indicator {
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 5px;
        background: #d1d5db;
        border-radius: 3px;
        z-index: 1056;
    }
    
    /* Touch feedback during swipe */
    .modal-dialog.swiping {
        transition: none !important;
    }
    
    /* ====================================================================
       7. KEYBOARD-AWARE - Input visibility
       ==================================================================== */
    
    /* When keyboard open, reduce modal height */
    .modal-dialog.keyboard-open {
        height: 50vh !important;
    }
    
    .modal-dialog.keyboard-open .modal-content {
        height: 50vh;
    }
    
    /* Focused input scrolls into view */
    .modal-body input:focus,
    .modal-body textarea:focus,
    .modal-body select:focus {
        scroll-margin: 20px;
    }
    
    /* ====================================================================
       8. MODAL SIZES - Full-screen variants
       ==================================================================== */
    
    /* Small modal (75% height) */
    .modal-sm .modal-dialog {
        height: 75vh !important;
    }
    
    .modal-sm .modal-content {
        height: 75vh;
        border-radius: 20px 20px 0 0 !important;
    }
    
    /* Animate from bottom */
    .modal-sm.fade .modal-dialog {
        transform: translateY(100%);
    }
    
    .modal-sm.show .modal-dialog {
        transform: translateY(25vh);
    }
    
    /* Large modal (full-screen) */
    .modal-lg .modal-dialog,
    .modal-xl .modal-dialog {
        height: 100vh !important;
    }
    
    /* ====================================================================
       9. SPECIFIC MODAL TYPES
       ==================================================================== */
    
    /* Create Lead Modal */
    #createLeadModal .modal-content {
        background: linear-gradient(180deg, #f9fafb 0%, white 100px);
    }
    
    /* Create Quote Modal */
    #createQuoteModal .modal-body {
        padding: 16px;
    }
    
    #createQuoteModal .quote-line-item {
        margin-bottom: 16px;
        padding: 16px;
        background: #f9fafb;
        border-radius: 10px;
    }
    
    /* Log Interaction Modal */
    #logInteractionModal textarea {
        min-height: 160px;
    }
    
    /* Appointment Modal */
    #createAppointmentModal .modal-body {
        padding: 20px;
    }
    
    #createAppointmentModal input[type="datetime-local"] {
        min-height: 52px;
        font-size: 16px;
    }
    
    /* Email Compose Modal */
    #composeEmailModal .modal-body {
        padding: 0;
    }
    
    #composeEmailModal textarea {
        border: none;
        border-radius: 0;
        min-height: 300px;
        padding: 20px;
        font-size: 16px;
        line-height: 1.6;
    }
    
    #composeEmailModal .email-toolbar {
        padding: 12px 20px;
        border-bottom: 1px solid #e5e7eb;
        background: #f9fafb;
        position: sticky;
        top: 64px;
        z-index: 1050;
    }
    
    /* Confirmation Modal (alerts) */
    .modal-dialog.modal-confirm {
        height: auto !important;
        align-items: flex-end !important;
    }
    
    .modal-confirm .modal-content {
        height: auto;
        border-radius: 20px 20px 0 0 !important;
    }
    
    .modal-confirm .modal-body {
        padding: 32px 24px;
        text-align: center;
    }
    
    .modal-confirm .modal-body i {
        font-size: 64px;
        margin-bottom: 20px;
        display: block;
    }
    
    .modal-confirm .modal-body h4 {
        font-size: 22px;
        font-weight: 700;
        margin-bottom: 12px;
    }
    
    .modal-confirm .modal-body p {
        font-size: 16px;
        color: #6b7280;
        margin-bottom: 0;
    }
    
    /* ====================================================================
       10. LOADING STATES
       ==================================================================== */
    
    /* Modal loading overlay */
    .modal-loading-overlay {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(255, 255, 255, 0.95);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 1060;
        flex-direction: column;
        gap: 16px;
    }
    
    .modal-loading-overlay .spinner {
        width: 48px;
        height: 48px;
        border: 4px solid #e5e7eb;
        border-top-color: #2563eb;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }
    
    .modal-loading-overlay p {
        font-size: 15px;
        color: #6b7280;
        margin: 0;
    }
    
    @keyframes spin {
        to { transform: rotate(360deg); }
    }
    
    /* ====================================================================
       11. MODAL TRANSITIONS - Smooth
       ==================================================================== */
    
    /* Fade-in */
    .modal.fade {
        transition: opacity 0.3s ease;
    }
    
    /* Scale-in (for small modals) */
    .modal.scale-in .modal-dialog {
        transform: scale(0.9) translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    .modal.scale-in.show .modal-dialog {
        transform: scale(1) translateY(0);
    }
    
    /* ====================================================================
       12. FORM MODALS - Optimized layout
       ==================================================================== */
    
    /* Form groups in modals */
    .modal-body .form-group,
    .modal-body .mb-3 {
        margin-bottom: 20px;
    }
    
    /* Last form group no margin */
    .modal-body .form-group:last-child,
    .modal-body .mb-3:last-child {
        margin-bottom: 0;
    }
    
    /* Form labels in modals */
    .modal-body label {
        font-size: 15px;
        font-weight: 600;
        margin-bottom: 8px;
        color: #374151;
    }
    
    /* Form sections */
    .modal-body .form-section {
        padding: 20px 0;
        border-bottom: 1px solid #e5e7eb;
    }
    
    .modal-body .form-section:last-child {
        border-bottom: none;
        padding-bottom: 0;
    }
    
    .modal-body .form-section-title {
        font-size: 17px;
        font-weight: 700;
        color: #111827;
        margin-bottom: 16px;
    }
    
    /* ====================================================================
       13. ACCESSIBILITY
       ==================================================================== */
    
    /* Focus trap indicator */
    .modal-dialog:focus {
        outline: none;
    }
    
    /* Screen reader only */
    .modal-sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border-width: 0;
    }
    
    /* High contrast mode */
    @media (prefers-contrast: high) {
        .modal-content {
            border: 3px solid #000;
        }
        
        .modal-header,
        .modal-footer {
            border-color: #000;
        }
    }
    
    /* Reduced motion */
    @media (prefers-reduced-motion: reduce) {
        .modal.fade .modal-dialog,
        .modal-backdrop.fade {
            transition: none !important;
        }
    }
    
    /* ====================================================================
       14. MULTI-STEP MODALS - Progress indicator
       ==================================================================== */
    
    .modal-steps {
        display: flex;
        justify-content: space-between;
        padding: 16px 20px;
        background: #f9fafb;
        border-bottom: 1px solid #e5e7eb;
    }
    
    .modal-step {
        flex: 1;
        text-align: center;
        position: relative;
        font-size: 13px;
        color: #9ca3af;
        font-weight: 600;
    }
    
    .modal-step.active {
        color: #2563eb;
    }
    
    .modal-step.completed {
        color: #10b981;
    }
    
    .modal-step::before {
        content: '';
        display: block;
        width: 32px;
        height: 32px;
        margin: 0 auto 8px;
        border-radius: 50%;
        background: #e5e7eb;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 700;
    }
    
    .modal-step.active::before {
        background: #2563eb;
        color: white;
    }
    
    .modal-step.completed::before {
        background: #10b981;
        color: white;
        content: '✓';
    }
    
    /* Step connector line */
    .modal-step:not(:last-child)::after {
        content: '';
        position: absolute;
        top: 16px;
        left: 50%;
        width: 100%;
        height: 2px;
        background: #e5e7eb;
        z-index: -1;
    }
    
    .modal-step.completed:not(:last-child)::after {
        background: #10b981;
    }
    
    /* ====================================================================
       15. MODAL ALERTS - Error/Success states
       ==================================================================== */
    
    .modal-alert {
        padding: 16px;
        border-radius: 10px;
        margin-bottom: 20px;
        display: flex;
        align-items: flex-start;
        gap: 12px;
    }
    
    .modal-alert i {
        font-size: 20px;
        flex-shrink: 0;
    }
    
    .modal-alert-content {
        flex: 1;
    }
    
    .modal-alert-title {
        font-size: 15px;
        font-weight: 700;
        margin-bottom: 4px;
    }
    
    .modal-alert-message {
        font-size: 14px;
        line-height: 1.5;
        margin: 0;
    }
    
    /* Alert variants */
    .modal-alert-success {
        background: #d1fae5;
        color: #065f46;
    }
    
    .modal-alert-error {
        background: #fee2e2;
        color: #991b1b;
    }
    
    .modal-alert-warning {
        background: #fef3c7;
        color: #92400e;
    }
    
    .modal-alert-info {
        background: #dbeafe;
        color: #1e40af;
    }
}

/* ========================================================================
   TABLET MODALS (768px - 991px)
   ======================================================================== */

@media (min-width: 768px) and (max-width: 991px) {
    
    /* Slightly larger modals on tablet */
    .modal-dialog {
        margin: 1.75rem auto;
        max-width: 600px;
    }
    
    .modal-content {
        border-radius: 16px;
    }
    
    /* Not full-screen on tablet */
    .modal-dialog {
        height: auto !important;
    }
    
    .modal-content {
        height: auto;
    }
}

/* ========================================================================
   DESKTOP - Standard Bootstrap modals
   ======================================================================== */

@media (min-width: 992px) {
    
    /* Desktop modals use standard behavior */
    .modal-dialog {
        margin: 1.75rem auto;
    }
    
    .modal-sm { max-width: 400px; }
    .modal-lg { max-width: 800px; }
    .modal-xl { max-width: 1140px; }
    
    /* Footer buttons inline on desktop */
    .modal-footer {
        flex-direction: row;
        justify-content: flex-end;
    }
    
    .modal-footer .btn {
        width: auto;
        min-width: 120px;
    }
}
