/* =========================================================================
   Global Modal — Mobile bottom-sheet style, theme-aware (light & dark)
   Applies to ALL Bootstrap modals across frontend + backend.
   ========================================================================= */

/* Backdrop — reset weird overrides from index.php, use a proper dark scrim
   Bootstrap 5.0.2 uses .modal-backdrop z-index:1040 and .modal z-index:1050,
   so keep backdrop strictly BELOW modal. */
.modal-backdrop,
.modal-backdrop.show{
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    background: #000 !important;
    opacity: .55 !important;
    z-index: 1040 !important;
}

/* Modal shell — transparent; backdrop handles scrim */
.modal{
    background: transparent !important;
    padding: 0 !important;
    z-index: 1055;
}

/* Dialog — bottom sheet */
.modal .modal-dialog{
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    top: auto !important;
    margin: 0 auto !important;
    width: 100% !important;
    max-width: 680px !important;
    min-height: 0 !important;
    padding: 0 !important;
    transform: translateY(100%);
    transition: transform .32s cubic-bezier(.22,.9,.28,1);
}
.modal.show .modal-dialog{
    transform: translateY(0);
}

.modal .modal-dialog.modal-lg,
.modal .modal-dialog.modal-xl{
    max-width: 820px !important;
}

/* Content — sheet card: rounded top only, theme colors */
.modal .modal-content{
    position: relative;
    background: var(--mu-surface, #ffffff) !important;
    color: var(--mu-text, #0f172a) !important;
    border: 0 !important;
    border-radius: 20px 20px 0 0 !important;
    box-shadow: 0 -12px 40px rgba(0,0,0,.25) !important;
    max-height: 88vh;
    display: flex;
    flex-direction: column;
    overflow: hidden !important;
}

/* Drag handle indicator — hidden (was causing visual gap above header) */
.modal .modal-content::before{
    content: none;
}

/* Header — theme-aware; fills the rounded top edge flush */
.modal .modal-header{
    background: transparent;
    color: var(--mu-text, #0f172a);
    border-bottom: 1px solid var(--mu-border, #e6e8ec);
    padding: 14px 20px;
    border-top-left-radius: 20px !important;
    border-top-right-radius: 20px !important;
    border-bottom-left-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    margin: 0 !important;
    flex: none;
}
.modal .modal-header .modal-title{
    color: inherit;
    font-weight: 800;
}
.modal .modal-header .btn-close{
    filter: none;
}
/* Dark mode — invert bootstrap's default dark X icon */
:root[data-theme="dark"] .modal .modal-header .btn-close{
    filter: invert(1) grayscale(1) brightness(2);
}
/* Keep the white close icon intact on gradient/dark custom headers */
.modal .modal-header .btn-close.btn-close-white,
:root[data-theme="dark"] .modal .modal-header .btn-close.btn-close-white{
    filter: invert(1) grayscale(100%) brightness(200%);
}

/* Body — scrollable within the sheet */
.modal .modal-body{
    background: var(--mu-surface, #ffffff);
    color: var(--mu-text, #0f172a);
    padding: 18px 20px;
    overflow-y: auto;
    flex: 1 1 auto;
    -webkit-overflow-scrolling: touch;
}

/* Footer — sticky feel at bottom of sheet */
.modal .modal-footer{
    background: var(--mu-surface-2, #f6f7f9);
    border-top: 1px solid var(--mu-border, #e6e8ec);
    padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
}

.modal .modal-body textarea,
.modal .modal-body textarea.form-control{
    border-radius: 0 !important;
    resize: vertical;
}

/* Form controls inside modals — pick up theme colors */
.modal .modal-body .form-control,
.modal .modal-body .form-select,
.modal .modal-body textarea,
.modal .modal-body input[type="text"],
.modal .modal-body input[type="number"],
.modal .modal-body input[type="email"],
.modal .modal-body input[type="password"],
.modal .modal-body input[type="search"],
.modal .modal-body input[type="tel"],
.modal .modal-body input[type="url"]{
    background: var(--mu-surface-2, #f6f7f9);
    color: var(--mu-text, #0f172a);
    border: 1px solid var(--mu-border, #e6e8ec);
}
.modal .modal-body .form-control::placeholder,
.modal .modal-body textarea::placeholder{
    color: var(--mu-text-subtle, #8a93a4);
}
.modal .modal-body .form-control:focus,
.modal .modal-body .form-select:focus,
.modal .modal-body textarea:focus{
    background: var(--mu-surface, #fff);
    color: var(--mu-text, #0f172a);
    border-color: var(--mu-primary, #4f46e5);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--mu-primary, #4f46e5) 15%, transparent);
}
.modal .modal-body .form-label{
    color: var(--mu-text, #0f172a);
    font-weight: 600;
}
.modal .modal-body .form-text,
.modal .modal-body small{
    color: var(--mu-text-muted, #5b6472);
}
.modal .modal-body hr{
    border-color: var(--mu-border, #e6e8ec);
}
.modal .modal-body code{
    background: var(--mu-surface-3, #eef0f3);
    color: var(--mu-text, #0f172a);
    padding: 2px 6px;
    border-radius: 6px;
}

/* Tables inside modals */
.modal .modal-body .table{
    color: var(--mu-text, #0f172a);
}
.modal .modal-body .table > :not(caption) > * > *{
    background: transparent;
    border-bottom-color: var(--mu-border, #e6e8ec);
    color: inherit;
}

/* Desktop: leave a little breathing room from edges, centered horizontally
   but still anchored to bottom for consistent mobile-like feel */
@media (min-width: 768px){
    .modal .modal-dialog{
        bottom: 20px !important;
        border-radius: 20px;
    }
    .modal .modal-content{
        border-radius: 20px !important;
        border: 0 !important;
        box-shadow: 0 20px 50px rgba(0,0,0,.35) !important;
    }
}

/* Fullscreen modals — keep behavior but themed */
.modal-fullscreen .modal-content,
.modal-fullscreen-sm-down .modal-content,
.modal-fullscreen-md-down .modal-content,
.modal-fullscreen-lg-down .modal-content{
    border-radius: 0 !important;
    max-height: 100vh;
}
.modal-fullscreen .modal-content::before{ display: none; }

/* When body has .modal-open, prevent background scroll on iOS */
body.modal-open{
    overflow: hidden;
    position: relative;
}


/* =========================================================================
   v2 strengthen — beat admin-pro.css's body.ap-admin-body .modal-content
   Specificity: 0,2,1 (body.ap-admin-body + .modal-content). We match it
   with body + .modal .modal-content (0,2,1) and add !important so layout
   props (max-height, flex, overflow) survive in admin pages too.
   ========================================================================= */

body .modal .modal-content,
body.ap-admin-body .modal .modal-content{
    max-height: 88vh !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
}
body .modal .modal-body,
body.ap-admin-body .modal .modal-body{
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    flex: 1 1 auto !important;
    min-height: 0 !important;
}
body .modal .modal-header,
body .modal .modal-footer,
body.ap-admin-body .modal .modal-header,
body.ap-admin-body .modal .modal-footer{
    flex: 0 0 auto !important;
}

/* Mobile bottom-sheet polish — drag handle, larger touch targets, safe-area */
@media (max-width: 767.98px){
    .modal .modal-content{
        max-height: 92vh !important;
        border-radius: 22px 22px 0 0 !important;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    /* Visible drag handle for the sheet */
    .modal .modal-content::before{
        content: "" !important;
        display: block !important;
        position: absolute !important;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 44px;
        height: 4px;
        border-radius: 999px;
        background: var(--mu-border-strong, #cdd1d8);
        opacity: .7;
        z-index: 2;
        pointer-events: none;
    }
    .modal .modal-header{
        padding-top: 22px !important;     /* leave room for the drag handle */
        padding-bottom: 12px !important;
    }
    .modal .modal-header .modal-title{
        font-size: 1rem !important;
        line-height: 1.35;
    }
    /* Larger close button for thumb-tap */
    .modal .modal-header .btn-close{
        width: 36px; height: 36px;
        padding: 0 !important;
        border-radius: 10px;
        background-color: var(--mu-surface-2, #f6f7f9) !important;
        background-size: 12px 12px;
    }
    /* Footer — full-width primary action on mobile */
    .modal .modal-footer{
        flex-direction: column-reverse;
        align-items: stretch;
        padding: 14px 18px calc(14px + env(safe-area-inset-bottom, 0px)) !important;
    }
    .modal .modal-footer .btn{
        width: 100%;
        padding: 12px 18px !important;
        font-size: .95rem !important;
        border-radius: 12px !important;
    }
    /* Body padding: tighter horizontal, comfy vertical */
    .modal .modal-body{
        padding: 16px 18px !important;
    }
    /* Form controls: touch-friendly height */
    .modal .modal-body .form-control,
    .modal .modal-body .form-select,
    .modal .modal-body input[type="text"],
    .modal .modal-body input[type="number"],
    .modal .modal-body input[type="email"],
    .modal .modal-body input[type="password"]{
        min-height: 44px;
        font-size: 16px !important; /* prevent iOS auto-zoom */
        border-radius: 10px !important;
    }
    .modal .modal-body textarea,
    .modal .modal-body textarea.form-control{
        font-size: 16px !important;
        border-radius: 10px !important;
    }
    .modal .modal-body .form-label{
        font-size: .85rem !important;
        margin-bottom: 4px !important;
    }
}

/* Tighten Bootstrap's grid gutters inside modals on mobile so 3-column form
   rows stack cleanly without horizontal overflow. */
@media (max-width: 575.98px){
    .modal .modal-body .row{ --bs-gutter-x: .75rem; }
    .modal .modal-body .col-md-3,
    .modal .modal-body .col-md-4,
    .modal .modal-body .col-md-6,
    .modal .modal-body .col-md-8{
        flex: 0 0 auto;
        width: 100%;
    }
}

/* Keep the desktop sheet feel (anchored bottom) but cap height nicely */
@media (min-width: 768px){
    .modal .modal-content{
        max-height: 86vh !important;
    }
}

/* Body lock — prevent iOS rubber-band when sheet is open */
body.modal-open{
    overflow: hidden !important;
    overscroll-behavior: contain !important;
}

/* ========================================================== */
/* Global fix: .btn-close.btn-close-white visibility on mobile */
/* ปุ่มปิด X บน modal ที่มี header สีเข้ม → วงกลมขาว + X สีหลัก */
/* ========================================================== */
.modal-header .btn-close.btn-close-white,
.modal-header .btn-close-white{
    width: 32px; height: 32px; border-radius: 50%;
    background-color: #fff !important;
    background-image: none !important;
    opacity: 1 !important;
    position: relative;
    box-shadow: 0 4px 12px rgba(0,0,0,.18);
    transition: transform .15s, box-shadow .2s;
    filter: none !important;
}
.modal-header .btn-close.btn-close-white::before,
.modal-header .btn-close-white::before{
    content: "\f00d";
    font-family: "Font Awesome 6 Free", "Font Awesome 5 Free", FontAwesome;
    font-weight: 900;
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    color: var(--main, #6366f1);
    font-size: .95rem;
    line-height: 1;
}
.modal-header .btn-close.btn-close-white:hover,
.modal-header .btn-close-white:hover{ transform: rotate(90deg) scale(1.05); }
.modal-header .btn-close.btn-close-white:focus,
.modal-header .btn-close-white:focus{ box-shadow: 0 0 0 3px rgba(255,255,255,.45); }

/* Ensure SweetAlert is always above admin v5-modal stack */
.swal2-container { z-index: 20000 !important; }
.swal2-backdrop-show { z-index: 19999 !important; }
