/* ============================================================================
   GLOBAL MODAL STYLES - SINGLE SOURCE OF TRUTH
   This file contains ALL modal styles for the Bay Brothers OS application.
   Do NOT add modal styles elsewhere - update this file instead.
   ============================================================================ */

/* ============ CRITICAL: BACKDROP CONFIGURATION ============ */
/* 
   IMPORTANT: Backdrops are DISABLED in JavaScript (modal-helpers.js)
   These rules are defensive fallbacks only.
*/
.modal-backdrop {
    /* CRITICAL: If a backdrop somehow gets created, make it completely invisible */
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
    display: none !important;
    z-index: -1 !important;
}

.modal-backdrop.show {
    opacity: 0 !important;
    visibility: hidden !important;
    display: none !important;
}

.modal-backdrop.fade {
    opacity: 0 !important;
}

/* ============ MODAL Z-INDEX HIERARCHY ============ */
.modal {
    z-index: 1060 !important;
}

.modal-dialog {
    z-index: 1061 !important;
}

.modal-content {
    z-index: 1062 !important;
    position: relative !important;
    pointer-events: auto !important;
}

/* Only apply z-index to Bootstrap modals, not custom expense-modal */
.modal .modal-header,
.modal .modal-body,
.modal .modal-footer {
    z-index: 1063;
    pointer-events: auto;
}

/* Dropdowns inside modals need even higher z-index */
.modal .dropdown-menu {
    z-index: 1070 !important;
}

.modal .address-suggestions-dropdown {
    z-index: 1071 !important;
}

/* ============ DESKTOP MODAL STYLES ============ */
@media (min-width: 992px) {
    .modal-dialog {
        margin: 1.75rem auto;
    }
    
    .modal-lg {
        max-width: 800px;
    }
    
    .modal-content {
        border-radius: 8px;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    }
}

/* ============ MOBILE MODAL STYLES ============ */
@media (max-width: 991.98px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-lg {
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 12px;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    }
    
    /* CRITICAL: Ensure modal body has extra padding for footer visibility */
    .modal-body {
        padding-bottom: 1.5rem !important;
    }
    
    /* CRITICAL: Ensure modal footer has solid background and good visibility */
    /* Use more specific selectors to avoid overriding custom modal styles */
    .modal:not(.expense-modal-backdrop) .modal-footer {
        background: var(--bs-modal-bg, #fff);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.1);
        border-top: 1px solid var(--bs-border-color);
        padding: 1rem;
        gap: 0.75rem;
        display: flex;
        flex-wrap: wrap;
    }
    
    /* Dark mode for modal footer */
    [data-bs-theme="dark"] .modal:not(.expense-modal-backdrop) .modal-footer {
        background: var(--bb-bg-secondary, #161b22);
        border-top-color: var(--bb-border-color, #30363d);
        box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.3);
    }
    
    /* CRITICAL: Make all modal footer buttons large and accessible on mobile */
    .modal:not(.expense-modal-backdrop) .modal-footer .btn {
        min-height: 50px;
        min-width: 48px;
        font-size: 1rem;
        font-weight: 600;
        padding: 0.75rem 1.5rem;
        touch-action: manipulation;
        /* Ensure buttons are always visible */
        position: relative;
        z-index: 10;
    }
    
    /* Dark mode for modal footer buttons */
    [data-bs-theme="dark"] .modal .modal-footer .btn-secondary {
        background-color: var(--bb-bg-tertiary, #21262d);
        border-color: var(--bb-border-color, #30363d);
        color: var(--bb-text-primary, #f0f6fc);
    }
    
    [data-bs-theme="dark"] .modal .modal-footer .btn-secondary:hover,
    [data-bs-theme="dark"] .modal .modal-footer .btn-secondary:focus {
        background-color: var(--bb-bg-secondary, #2c3139);
        border-color: var(--bb-border-dark, #484f58);
    }
    
    [data-bs-theme="dark"] .modal .modal-footer .btn-primary {
        background-color: var(--bb-primary, #4fa8c5);
        border-color: var(--bb-primary, #4fa8c5);
        color: #ffffff;
    }
    
    [data-bs-theme="dark"] .modal .modal-footer .btn-primary:hover,
    [data-bs-theme="dark"] .modal .modal-footer .btn-primary:focus {
        background-color: var(--bb-primary-light, #6bb8d1);
        border-color: var(--bb-primary-light, #6bb8d1);
    }
    
    [data-bs-theme="dark"] .modal .modal-footer .btn-primary:disabled {
        opacity: 0.5;
    }
    
    /* Make footer buttons stack or expand based on content */
    .modal .modal-footer .btn.flex-grow-1 {
        flex: 1;
    }
    
    /* Form controls in modals should be touch-friendly */
    .modal .form-control,
    .modal .form-select {
        min-height: 48px;
        font-size: 16px; /* Prevent iOS zoom */
    }
    
    .modal .btn {
        min-height: 48px;
        min-width: 48px;
    }
    
    .modal .btn-close {
        min-height: 48px;
        min-width: 48px;
        padding: 12px;
    }
}

/* ============ iOS SAFARI SPECIFIC FIXES ============ */
@supports (-webkit-touch-callout: none) {
    .modal {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
    }
    
    .modal-content {
        -webkit-transform: translate3d(0, 0, 0);
        transform: translate3d(0, 0, 0);
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
    }
    
    .modal-body {
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y;
    }
    
    /* Fix for iOS Safari input focus issues */
    .modal input,
    .modal textarea,
    .modal select {
        font-size: 16px !important; /* Prevent zoom */
        touch-action: manipulation;
    }
}

/* ============ MODAL BODY SCROLLING FIX - CRITICAL FOR ALL MODALS ============ */
/* This ensures modal bodies are scrollable on ALL devices, especially mobile */
/* IMPORTANT: Only target Bootstrap modals (.modal), NOT custom bottom sheets (.expense-modal) */
.modal .modal-body {
    /* Enable smooth scrolling */
    -webkit-overflow-scrolling: touch !important;
    overflow-y: auto !important;
    overscroll-behavior: contain !important;
    touch-action: pan-y !important;
    /* CRITICAL: Ensure pointer events work inside modal body */
    pointer-events: auto !important;
    /* Ensure body can scroll */
    position: relative !important;
}

/* CRITICAL: All interactive elements inside Bootstrap modal body must be touchable */
.modal .modal-body input,
.modal .modal-body textarea,
.modal .modal-body select,
.modal .modal-body button,
.modal .modal-body .btn,
.modal .modal-body .form-control,
.modal .modal-body .form-select,
.modal .modal-body a,
.modal .modal-body label,
.modal .modal-body .form-check,
.modal .modal-body .form-check-input {
    pointer-events: auto !important;
    touch-action: manipulation !important;
    -webkit-user-select: text !important;
    user-select: text !important;
}

/* Ensure autocomplete dropdowns work inside Bootstrap modals */
.modal .modal-body .address-autocomplete,
.modal .modal-body .dropdown-menu,
.modal .modal-body .address-suggestion-item {
    pointer-events: auto !important;
    touch-action: manipulation !important;
}

/* ============ FULLSCREEN MODALS ON MOBILE ============ */
@media (max-width: 767.98px) {
    .modal-fullscreen-md-down,
    .modal-fullscreen-sm-down,
    .modal-fullscreen {
        padding: 0 !important;
    }
    
    .modal-fullscreen-md-down .modal-dialog,
    .modal-fullscreen-sm-down .modal-dialog,
    .modal-fullscreen .modal-dialog {
        width: 100vw !important;
        max-width: 100vw !important;
        height: 100vh !important;
        max-height: 100vh !important;
        margin: 0 !important;
        padding: 0 !important;
    }
    
    .modal-fullscreen-md-down .modal-content,
    .modal-fullscreen-sm-down .modal-content,
    .modal-fullscreen .modal-content {
        width: 100% !important;
        height: 100vh !important;
        max-height: 100vh !important;
        min-height: 100vh !important;
        border: 0 !important;
        border-radius: 0 !important;
        display: flex !important;
        flex-direction: column !important;
    }
    
    .modal-fullscreen-md-down .modal-header,
    .modal-fullscreen-sm-down .modal-header,
    .modal-fullscreen .modal-header {
        flex-shrink: 0 !important;
        border-radius: 0 !important;
        padding: 1rem !important;
        padding-top: max(1rem, env(safe-area-inset-top)) !important;
    }
    
    .modal-fullscreen-md-down .modal-body,
    .modal-fullscreen-sm-down .modal-body,
    .modal-fullscreen .modal-body {
        flex: 1 1 auto !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        padding: 1rem !important;
        /* CRITICAL: Extra padding to prevent content hidden behind footer */
        /* Use unified variable for PWA-aware safe area */
        padding-bottom: calc(120px + var(--app-safe-bottom, env(safe-area-inset-bottom, 0px))) !important;
    }
    
    .modal-fullscreen-md-down .modal-footer,
    .modal-fullscreen-sm-down .modal-footer,
    .modal-fullscreen .modal-footer {
        flex-shrink: 0 !important;
        padding: 1rem !important;
        /* Use unified variable for PWA-aware safe area */
        padding-bottom: max(1rem, var(--app-safe-bottom, env(safe-area-inset-bottom, 0px))) !important;
        /* CRITICAL: Ensure footer has solid background */
        background: var(--bs-modal-bg, #fff) !important;
        backdrop-filter: blur(10px) !important;
        -webkit-backdrop-filter: blur(10px) !important;
        /* Enhanced shadow for better visibility */
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.2) !important;
        border-top: 1px solid var(--bs-border-color) !important;
    }
    
    /* Dark mode for fullscreen modal footer */
    [data-bs-theme="dark"] .modal-fullscreen-md-down .modal-footer,
    [data-bs-theme="dark"] .modal-fullscreen-sm-down .modal-footer,
    [data-bs-theme="dark"] .modal-fullscreen .modal-footer {
        background: var(--bb-bg-secondary, #161b22) !important;
        border-top-color: var(--bb-border-color, #30363d) !important;
        box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4) !important;
    }
    
    /* CRITICAL: Ensure submit buttons in modal footer are large and accessible */
    .modal-fullscreen-md-down .modal-footer .btn,
    .modal-fullscreen-sm-down .modal-footer .btn,
    .modal-fullscreen .modal-footer .btn {
        min-height: 50px !important;
        font-size: 1rem !important;
        font-weight: 600 !important;
        padding: 0.75rem 1.5rem !important;
        flex: 1 !important;
        /* Ensure buttons are always visible */
        position: relative !important;
        z-index: 1101 !important;
    }
    
    /* Dark mode button styles for fullscreen modals */
    [data-bs-theme="dark"] .modal-fullscreen-md-down .modal-footer .btn-secondary,
    [data-bs-theme="dark"] .modal-fullscreen-sm-down .modal-footer .btn-secondary,
    [data-bs-theme="dark"] .modal-fullscreen .modal-footer .btn-secondary {
        background-color: var(--bb-bg-tertiary, #21262d) !important;
        border-color: var(--bb-border-color, #30363d) !important;
        color: var(--bb-text-primary, #f0f6fc) !important;
    }
    
    [data-bs-theme="dark"] .modal-fullscreen-md-down .modal-footer .btn-secondary:hover,
    [data-bs-theme="dark"] .modal-fullscreen-sm-down .modal-footer .btn-secondary:hover,
    [data-bs-theme="dark"] .modal-fullscreen .modal-footer .btn-secondary:hover {
        background-color: var(--bb-bg-secondary, #2c3139) !important;
    }
    
    [data-bs-theme="dark"] .modal-fullscreen-md-down .modal-footer .btn-primary,
    [data-bs-theme="dark"] .modal-fullscreen-sm-down .modal-footer .btn-primary,
    [data-bs-theme="dark"] .modal-fullscreen .modal-footer .btn-primary {
        background-color: var(--bb-primary, #4fa8c5) !important;
        border-color: var(--bb-primary, #4fa8c5) !important;
        color: #ffffff !important;
    }
}

/* ============ DARK MODE SUPPORT ============ */
[data-bs-theme="dark"] .modal-content {
    background-color: var(--bb-bg-secondary, #161b22);
    color: var(--bb-text-primary, #f0f6fc);
    border-color: var(--bb-border-color, #30363d);
}

[data-bs-theme="dark"] .modal .modal-header {
    border-bottom-color: var(--bb-border-color, #30363d);
    background-color: var(--bb-bg-secondary, #161b22);
}

[data-bs-theme="dark"] .modal .modal-footer {
    border-top-color: var(--bb-border-color, #30363d);
    background-color: var(--bb-bg-secondary, #161b22);
}

/* Dark mode for form controls in modals */
[data-bs-theme="dark"] .modal .form-control,
[data-bs-theme="dark"] .modal .form-select {
    background-color: var(--bb-bg-tertiary, #21262d);
    border-color: var(--bb-border-color, #30363d);
    color: var(--bb-text-primary, #f0f6fc);
}

[data-bs-theme="dark"] .modal .form-control:focus,
[data-bs-theme="dark"] .modal .form-select:focus {
    border-color: var(--bb-primary, #4fa8c5);
    box-shadow: 0 0 0 0.2rem rgba(79, 168, 197, 0.25);
}

[data-bs-theme="dark"] .modal .form-control::placeholder {
    color: var(--bb-text-muted, #6e7681);
}

/* Dark mode for form labels in modals */
[data-bs-theme="dark"] .modal .form-label {
    color: var(--bb-text-primary, #f0f6fc);
}

/* Dark mode for input groups in modals */
[data-bs-theme="dark"] .modal .input-group-text {
    background-color: var(--bb-bg-tertiary, #21262d);
    border-color: var(--bb-border-color, #30363d);
    color: var(--bb-text-secondary, #8b949e);
}

/* Dark mode for modal buttons - Default button styling */
[data-bs-theme="dark"] .modal .btn-secondary {
    background-color: var(--bb-bg-tertiary, #21262d);
    border-color: var(--bb-border-color, #30363d);
    color: var(--bb-text-primary, #f0f6fc);
}

[data-bs-theme="dark"] .modal .btn-secondary:hover,
[data-bs-theme="dark"] .modal .btn-secondary:focus {
    background-color: var(--bb-bg-secondary, #2c3139);
    border-color: var(--bb-border-dark, #484f58);
}

[data-bs-theme="dark"] .modal .btn-outline-secondary {
    color: var(--bb-text-secondary, #8b949e);
    border-color: var(--bb-border-color, #30363d);
}

[data-bs-theme="dark"] .modal .btn-outline-secondary:hover,
[data-bs-theme="dark"] .modal .btn-outline-secondary:focus {
    background-color: var(--bb-bg-tertiary, #21262d);
    color: var(--bb-text-primary, #f0f6fc);
}

/* Dark mode for close button */
[data-bs-theme="dark"] .modal .btn-close {
    filter: invert(1) grayscale(100%) brightness(200%);
}

/* ============ MODAL ANIMATIONS ============ */
.modal.fade .modal-dialog {
    transition: transform 0.3s ease-out;
    transform: translate(0, -50px);
}

.modal.show .modal-dialog {
    transform: none;
}

/* ============ PREVENT BODY SCROLL WHEN MODAL OPEN ============ */
.modal-open {
    overflow: hidden !important;
}

@media (max-width: 991.98px) {
    .modal-open {
        position: fixed !important;
        width: 100% !important;
        overflow: hidden !important;
    }
}

/* ============ ACCESSIBILITY ============ */
.modal:focus {
    outline: none;
}

.btn-close:focus {
    outline: 2px solid var(--bs-primary);
    outline-offset: 2px;
}

/* ============ DROPDOWN MENUS IN MODALS ============ */
.modal .dropdown-menu {
    position: absolute;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    border: 1px solid var(--bs-border-color);
    margin-top: 4px;
}

.modal .dropdown-item {
    border-radius: 6px;
    margin: 2px 4px;
    padding: 0.75rem;
    transition: all 0.2s ease;
}

.modal .dropdown-item:hover {
    background-color: var(--bs-primary);
    color: white;
}

/* ============ ADDRESS AUTOCOMPLETE IN MODALS ============ */
.modal .address-autocomplete .dropdown-menu {
    z-index: 1071 !important;
    pointer-events: auto !important;
}

.modal .address-suggestion-item {
    pointer-events: auto !important;
    touch-action: manipulation !important;
}

/* ============ LOADING STATES ============ */
.modal-loading {
    pointer-events: none;
    opacity: 0.7;
}

.modal-loading .spinner-border {
    display: inline-block;
}

/* ============ ERROR HANDLING ============ */
.modal-error {
    border: 2px solid var(--bs-danger);
}

.modal-error .modal-header {
    background-color: rgba(220, 53, 69, 0.1);
}

/* ============ SUCCESS FEEDBACK ============ */
.modal-success {
    border: 2px solid var(--bs-success);
}

.modal-success .modal-header {
    background-color: rgba(25, 135, 84, 0.1);
}
