/**
 * Global Modal Z-Index Fix
 * 
 * Ensures ALL modals across the entire webapp appear above navbar and are fully functional.
 * 
 * Z-Index Hierarchy:
 * - Navbar: 10000
 * - Modal Backdrop (with blur): 10001 (above navbar, below modal)
 * - Modal Content: 10002 (above backdrop and navbar)
 * 
 * CRITICAL: Backdrop blur must be BELOW modal content, not over it.
 * 
 * This file must load after Bootstrap CSS but before other custom CSS.
 * This is a GLOBAL solution that works everywhere.
 */

/* ============================================================================
   MODAL BACKDROP - ABOVE NAVBAR, BELOW MODAL
   ============================================================================ */

.modal-backdrop {
    z-index: 10001 !important; /* Above navbar (10000), below modal (10002) */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    background-color: rgba(0, 0, 0, 0.5) !important;
    backdrop-filter: blur(2px) !important;
    -webkit-backdrop-filter: blur(2px) !important;
    /* Ensure backdrop is always below modal content */
    pointer-events: auto !important;
}

.modal-backdrop.show {
    opacity: 1 !important;
    display: block !important;
}

.modal-backdrop.fade {
    opacity: 0;
    transition: opacity 0.15s linear;
}

.modal-backdrop.fade.show {
    opacity: 0.5;
}

/* ============================================================================
   MODAL CONTAINER - ABOVE BACKDROP AND NAVBAR
   ============================================================================ */

.modal {
    z-index: 10002 !important; /* Above backdrop (10001) and navbar (10000) */
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    display: none !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    outline: 0 !important;
    /* Ensure modal is always above backdrop blur */
    pointer-events: none !important; /* Allow clicks to pass through to backdrop when closed */
}

.modal.show {
    pointer-events: auto !important; /* Enable clicks when modal is shown */
}

.modal.show {
    display: block !important;
}

.modal.fade {
    opacity: 0;
    transition: opacity 0.15s linear;
}

.modal.fade.show {
    opacity: 1;
}

/* ============================================================================
   MODAL DIALOG - CENTERING AND LAYOUT
   ============================================================================ */

.modal-dialog {
    position: relative !important;
    width: auto !important;
    margin: 1.75rem auto !important;
    max-width: 500px !important;
    pointer-events: none !important; /* Dialog itself doesn't capture clicks */
    z-index: inherit !important;
}

/* When modal is shown, dialog should allow pointer events */
.modal.show .modal-dialog {
    pointer-events: auto !important;
}

.modal-dialog-centered {
    display: flex !important;
    align-items: center !important;
    min-height: calc(100% - 3.5rem) !important;
}

.modal-dialog-centered::before {
    display: block !important;
    height: calc(100vh - 3.5rem) !important;
    content: "" !important;
}

/* Modal sizes */
.modal-dialog.modal-sm {
    max-width: 300px !important;
}

.modal-dialog.modal-lg {
    max-width: 800px !important;
}

.modal-dialog.modal-xl {
    max-width: 1140px !important;
}

/* ============================================================================
   MODAL CONTENT - ENABLE POINTER EVENTS AND ENSURE ABOVE BACKDROP
   ============================================================================ */

.modal-content {
    position: relative !important;
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    pointer-events: auto !important; /* Content is always clickable */
    background-color: #fff !important;
    background-clip: padding-box !important;
    border: 1px solid rgba(0, 0, 0, 0.2) !important;
    border-radius: 0.3rem !important;
    box-shadow: 0 0.25rem 0.5rem rgba(0, 0, 0, 0.5) !important;
    outline: 0 !important;
    z-index: inherit !important;
    /* Ensure content is above backdrop blur */
    transform: translateZ(0) !important; /* Create new stacking context */
    will-change: transform !important;
}

/* ============================================================================
   MODAL HEADER, BODY, FOOTER
   ============================================================================ */

.modal-header {
    display: flex !important;
    align-items: flex-start !important;
    justify-content: space-between !important;
    padding: 1rem 1rem !important;
    border-bottom: 1px solid #dee2e6 !important;
    border-top-left-radius: calc(0.3rem - 1px) !important;
    border-top-right-radius: calc(0.3rem - 1px) !important;
}

.modal-body {
    position: relative !important;
    flex: 1 1 auto !important;
    padding: 1rem !important;
}

.modal-footer {
    display: flex !important;
    flex-wrap: wrap !important;
    align-items: center !important;
    justify-content: flex-end !important;
    padding: 0.75rem !important;
    border-top: 1px solid #dee2e6 !important;
    border-bottom-right-radius: calc(0.3rem - 1px) !important;
    border-bottom-left-radius: calc(0.3rem - 1px) !important;
}

/* ============================================================================
   MODAL CLOSE BUTTON
   ============================================================================ */

.btn-close {
    box-sizing: content-box !important;
    width: 1em !important;
    height: 1em !important;
    padding: 0.25em 0.25em !important;
    color: #000 !important;
    background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000'%3e%3cpath d='M.293.293a1 1 0 011.414 0L8 6.586 14.293.293a1 1 0 111.414 1.414L9.414 8l6.293 6.293a1 1 0 01-1.414 1.414L8 9.414l-6.293 6.293a1 1 0 01-1.414-1.414L6.586 8 .293 1.707a1 1 0 010-1.414z'/%3e%3c/svg%3e") center/1em auto no-repeat !important;
    border: 0 !important;
    border-radius: 0.25rem !important;
    opacity: 0.5 !important;
    cursor: pointer !important;
    pointer-events: auto !important;
}

.btn-close:hover {
    color: #000 !important;
    text-decoration: none !important;
    opacity: 0.75 !important;
}

.btn-close:focus {
    outline: 0 !important;
    box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25) !important;
    opacity: 1 !important;
}

.btn-close-white {
    filter: invert(1) grayscale(100%) brightness(200%) !important;
}

/* ============================================================================
   RESPONSIVE MODAL FIXES
   ============================================================================ */

@media (max-width: 575.98px) {
    .modal-dialog {
        margin: 0.5rem !important;
        max-width: calc(100% - 1rem) !important;
    }
    
    .modal-dialog-centered {
        min-height: calc(100% - 1rem) !important;
    }
    
    .modal-dialog-centered::before {
        height: calc(100vh - 1rem) !important;
    }
}

/* ============================================================================
   DARK MODE SUPPORT
   ============================================================================ */

[data-theme="dark"] .modal-content {
    background-color: #1a1a1a !important;
    border-color: rgba(255, 255, 255, 0.2) !important;
    color: #fff !important;
}

[data-theme="dark"] .modal-header {
    border-bottom-color: rgba(255, 255, 255, 0.2) !important;
}

[data-theme="dark"] .modal-footer {
    border-top-color: rgba(255, 255, 255, 0.2) !important;
}

[data-theme="dark"] .btn-close {
    filter: invert(1) grayscale(100%) brightness(200%) !important;
}

/* ============================================================================
   ENSURE MODAL IS ALWAYS CLICKABLE AND ABOVE BACKDROP
   ============================================================================ */

.modal.show {
    z-index: 10002 !important; /* Above backdrop (10001) */
    pointer-events: auto !important;
}

.modal.show .modal-dialog {
    pointer-events: auto !important;
}

.modal.show .modal-content {
    pointer-events: auto !important;
    /* Ensure content is visually above backdrop blur */
    position: relative !important;
    z-index: 1 !important; /* Within modal's stacking context */
}

.modal.show .modal-content * {
    pointer-events: auto !important;
}

/* Ensure all interactive elements are clickable */
.modal.show button,
.modal.show a,
.modal.show input,
.modal.show textarea,
.modal.show select,
.modal.show form,
.modal.show [role="button"] {
    pointer-events: auto !important;
    z-index: inherit !important;
}

/* Prevent body scroll when modal is open */
body.modal-open {
    overflow: hidden !important;
    padding-right: 0 !important;
}

/* ============================================================================
   GLOBAL MODAL FIXES - WORK EVERYWHERE
   ============================================================================ */

/* Ensure modals moved to body are properly positioned */
body > .modal {
    z-index: 10002 !important;
    position: fixed !important;
}

body > .modal.show {
    z-index: 10002 !important;
    display: block !important;
}

body > .modal-backdrop {
    z-index: 10001 !important;
    position: fixed !important;
}

body > .modal-backdrop.show {
    z-index: 10001 !important;
    opacity: 0.5 !important;
}

/* Prevent parent containers from creating stacking contexts that trap modals */
.account-container,
.profile-layout,
.profile-main,
.admin-content,
.site-main {
    position: relative !important;
    z-index: auto !important;
    /* Don't create stacking context */
    isolation: auto !important;
}

/* Ensure backdrop is always below modal, even when both are in body */
body > .modal-backdrop + .modal,
body > .modal-backdrop ~ .modal {
    z-index: 10002 !important;
}

/* Multiple modals support */
.modal-backdrop.show ~ .modal {
    z-index: 10003 !important;
}

.modal-backdrop.show ~ .modal .modal-backdrop.show {
    z-index: 10004 !important;
}

