/* =============================================================
   Modal / popup shell — shared standard.

   Structure this layer assumes (standard Bootstrap markup):

     .modal > .modal-dialog > .modal-content   <- the shell: owns the
                                                  radius, border and
                                                  shadow, and CLIPS
       .modal-header                              everything to it
       .modal-body        <- content; scrolls when it needs to
       .modal-footer

   The shell is what rounds the corners, so the shell is what must
   clip. A scrollable child cannot round its own scrollbar: the
   scrollbar is painted outside the child's border-radius, so it
   runs square across the shell's curve and appears to bleed out of
   the modal. Clipping on .modal-content is the only thing that
   keeps the scrollbar inside the rounded boundary, which is why the
   one modal in the app that already got this right
   (.dg-details-modal in Views/WorkforceMobility/Index.cshtml) pairs
   its radius with overflow:hidden. That pairing is now the default
   for every modal.
   ============================================================= */

/* Shared backdrop only: keep the dialog and its controls sharp. Existing
   overlay visibility, dimming, stacking and open/close transitions stay owned
   by each modal implementation. */
:root {
    --synx-modal-backdrop-blur: 6px;
}

body :is(.modal-backdrop, .fg-modal-backdrop, .figma-confirm-backdrop,
    .doc-modal-overlay, .rp-modal, .fg-onboarding-modal, .onboard-modal,
    .modal-scrim),
dialog::backdrop {
    -webkit-backdrop-filter: blur(var(--synx-modal-backdrop-blur)) !important;
    backdrop-filter: blur(var(--synx-modal-backdrop-blur)) !important;
}

/* Bootstrap otherwise applies opacity to the entire backdrop, weakening the
   blur itself. Keep its fade and put the dimming in the background colour. */
body .modal-backdrop {
    --bs-backdrop-opacity: 1 !important;
    background-color: rgb(15 23 42 / 42%);
}

@media (prefers-reduced-transparency: reduce) {
    body :is(.modal-backdrop, .fg-modal-backdrop, .figma-confirm-backdrop,
        .doc-modal-overlay, .rp-modal, .fg-onboarding-modal, .onboard-modal,
        .modal-scrim),
    dialog::backdrop {
        -webkit-backdrop-filter: none !important;
        backdrop-filter: none !important;
    }
}

.modal-content {
    border-radius: var(--modal-radius);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    /* Clip scrollbars, backgrounds, sticky headers and table edges
       to the rounded shell. Without this, a horizontal scrollbar on
       any descendant renders square-cornered over the curve. */
    overflow: hidden;
}

/* Escape hatch for the few modals whose content must escape the
   shell (custom dropdown menus, popovers, date pickers rendered
   inline rather than in a portal). Put it on .modal-content. */
.modal-content.modal-content-overflow-visible {
    overflow: visible;
}

/* Header / footer never scroll — only the body does. */
.modal-header,
.modal-footer,
.fg-modal-header,
.fg-modal-footer {
    flex: 0 0 auto;
}

/* ---------- The heading stays put, on EVERY modal ----------
   flex:0 0 auto above only pins the header if something caps the shell's
   height — otherwise the shell just grows and the whole .modal scrolls,
   taking the heading with it.

   Bootstrap caps the height for ONE variant, .modal-dialog-scrollable.
   Most modals in this app are a plain .modal-dialog, so for them the rule
   above had nothing to act against and the heading scrolled away. That is
   why the lock looked unimplemented: nothing was overriding it, it simply
   never applied outside that one variant.

   Capping here is all it takes. .modal-content is already a flex column
   (Bootstrap), the header and footer are fixed-size, so the body becomes
   the flexible part and takes the scrolling. */
.modal-content {
    max-height: calc(100vh - var(--bs-modal-margin, 0.5rem) * 2);
    /* dvh so mobile browser chrome does not push the footer out of reach.
       Browsers without dvh keep the vh line above. */
    max-height: calc(100dvh - var(--bs-modal-margin, 0.5rem) * 2);
}

.modal-body {
    overflow-y: auto;
    /* A flex item will not shrink below its content without this, and an
       unshrinkable body cannot scroll. */
    min-height: 0;
}

/* ---- Modals whose header and body live inside a <form> ----
   Pinning relies on an unbroken flex chain: .modal-content is a flex column,
   the header and footer are fixed-size, and the body takes what is left and
   scrolls. A <form> wrapped around all three - which 14 modals in this app do,
   so the inputs post together - sits in that chain as a plain block. The body
   is then no longer a flex item of the capped column, so it never gets a
   bounded height to scroll inside, the content pushes the shell past its cap
   and the whole dialog scrolls, heading and all.

   Making the form carry the column through restores it, and costs nothing for
   a form whose content is short. Direct children only: a form inside the body
   is the body's business, not the shell's. */
.modal-content > form {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
}

/* Escape hatch for a body that must not clip or scroll — an inline date
   picker or custom dropdown that renders into the body rather than a
   portal. Pair it with .modal-content-overflow-visible on the shell. */
.modal-body.modal-body-overflow-visible {
    overflow: visible;
}

.modal-header {
    padding: var(--modal-padding-y) var(--modal-padding-x);
    border-bottom: 1px solid var(--border-color);
}

.modal-body {
    padding: var(--modal-padding-y) var(--modal-padding-x);
}

.modal-footer {
    padding: var(--space-4) var(--modal-padding-x);
    gap: var(--space-2);
    border-top: 1px solid var(--border-color);
}

.modal-title {
    font-size: var(--fs-xl);
    font-weight: var(--fw-semibold);
    line-height: var(--lh-tight);
    margin: 0;
}

/* ---------- Scrollable modal bodies ----------
   Bootstrap's own .modal-dialog-scrollable already scrolls the body;
   .fg-modal-scroll is the equivalent for a modal that sizes itself
   and wants its own capped scroll region. Both sit inside the
   clipped shell, so their scrollbars stay within the rounded edge. */

.modal-dialog-scrollable .modal-content {
    overflow: hidden;
}

.fg-modal-scroll {
    overflow: auto;
    overscroll-behavior: contain;
    min-height: 0;
}

/* Wide content (tables, grids) keeps real horizontal scrolling —
   the scroll container is inset by the body's padding, so its
   scrollbar is nowhere near the shell's corners. */
.modal-body > .table-responsive,
.modal-body > .fg-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* A scroll region that is flush to the shell (zero body padding)
   still scrolls; the shell's clip keeps its scrollbar inside the
   curve rather than cutting across it. */
.modal-body.fg-modal-body-flush {
    padding: 0;
}

/* ---------- The hand-rolled shells ----------
   Not every popup in the app is Bootstrap markup: .fg-modal (used in
   Expenses and ~120 places besides), .review-expense-modal and
   .figma-confirm-dialog are their own shells with their own radius.
   They are popups, so they answer to this layer too - otherwise
   "shared modal styling" only ever means the Bootstrap half.

   .fg-modal in particular set a radius AND scrolled itself, with no
   clip, which is the exact combination that squares off a corner. */
.fg-modal,
.review-expense-modal,
.figma-confirm-dialog {
    border-radius: var(--modal-radius);
    overflow: hidden;
}

/* It still has to scroll - the clip above would otherwise trap the
   overflow. Move the scrolling to an inner region that is inset from
   the shell, so the bar never reaches the curve in the first place. */
.fg-modal {
    display: flex;
    flex-direction: column;
}

    .fg-modal > .fg-modal-body {
        overflow-y: auto;
        overscroll-behavior: contain;
        min-height: 0;
    }

/* ---------- Scrollbars inside a popup ----------
   Clipping the shell stops a scrollbar painting outside the curve, but
   a bar that runs the full height still arrives at the rounded corner
   and gets sliced there, which is what reads as "one side is square".
   Inset the track instead: the margin ends the bar short of both
   corners, so the curve is clean and the bar looks deliberate.

   The margin is a little over the shell radius so the track clears the
   whole arc, not just the tangent point. */
.modal-body,
.fg-modal-scroll,
.fg-modal > .fg-modal-body,
.review-modal-body,
.fg-onboarding-modal-body,
.onboard-modal .panel-body,
.figma-confirm-dialog,
.rp-modal-dialog {
    /* Firefox */
    scrollbar-width: thin;
    scrollbar-color: var(--border-color-strong) transparent;
}

    .modal-body::-webkit-scrollbar,
    .fg-modal-scroll::-webkit-scrollbar,
    .fg-modal > .fg-modal-body::-webkit-scrollbar,
    .review-modal-body::-webkit-scrollbar,
    .fg-onboarding-modal-body::-webkit-scrollbar,
    .onboard-modal .panel-body::-webkit-scrollbar,
    .figma-confirm-dialog::-webkit-scrollbar,
    .rp-modal-dialog::-webkit-scrollbar {
        width: 12px;
        height: 12px;
    }

    .modal-body::-webkit-scrollbar-track,
    .fg-modal-scroll::-webkit-scrollbar-track,
    .fg-modal > .fg-modal-body::-webkit-scrollbar-track,
    .review-modal-body::-webkit-scrollbar-track,
    .fg-onboarding-modal-body::-webkit-scrollbar-track,
    .onboard-modal .panel-body::-webkit-scrollbar-track,
    .figma-confirm-dialog::-webkit-scrollbar-track,
    .rp-modal-dialog::-webkit-scrollbar-track {
        background: transparent;
        margin: calc(var(--modal-radius) + 4px) 0;
    }

    /* Transparent border + background-clip keeps the visible thumb
       narrow while the hit target stays a comfortable 12px. */
    .modal-body::-webkit-scrollbar-thumb,
    .fg-modal-scroll::-webkit-scrollbar-thumb,
    .fg-modal > .fg-modal-body::-webkit-scrollbar-thumb,
    .review-modal-body::-webkit-scrollbar-thumb,
    .fg-onboarding-modal-body::-webkit-scrollbar-thumb,
    .onboard-modal .panel-body::-webkit-scrollbar-thumb,
    .figma-confirm-dialog::-webkit-scrollbar-thumb,
    .rp-modal-dialog::-webkit-scrollbar-thumb {
        background-color: var(--border-color-strong);
        border-radius: 999px;
        border: 4px solid transparent;
        background-clip: content-box;
    }

        .modal-body::-webkit-scrollbar-thumb:hover,
        .fg-modal-scroll::-webkit-scrollbar-thumb:hover,
        .fg-modal > .fg-modal-body::-webkit-scrollbar-thumb:hover,
        .review-modal-body::-webkit-scrollbar-thumb:hover,
        .fg-onboarding-modal-body::-webkit-scrollbar-thumb:hover,
        .onboard-modal .panel-body::-webkit-scrollbar-thumb:hover,
        .figma-confirm-dialog::-webkit-scrollbar-thumb:hover,
        .rp-modal-dialog::-webkit-scrollbar-thumb:hover {
            background-color: var(--text-muted);
        }

/* ---------- Shells that scroll themselves ----------
   .rp-modal-dialog (Resource Planning) has no body element: the header,
   the content and the footer are all siblings inside the scrolling
   dialog, so there is nothing to cap and nothing to pin against, and the
   heading scrolls away with the content.

   It cannot take the clip-and-scroll-the-body treatment without new
   markup, so it gets the other way round: the dialog keeps scrolling and
   the header and footer stick to its scrollport. Sticky works here
   precisely because the dialog is the scroll container and no ancestor
   between them clips. The scrollbar above is inset, so the corners stay
   clean without needing overflow:hidden.

   --rp-modal-pad is the dialog's own padding, published by the page so
   the sticky offsets and the full-bleed negative margins line up with it. */
.rp-modal-header,
.rp-modal-footer {
    position: sticky;
    z-index: 2;
    background: var(--theme-surface, #fff);
}

.rp-modal-header {
    /* Negative top cancels the dialog's padding so the header sits flush
       to the top edge instead of sticking a padding's width down it. */
    top: calc(var(--rp-modal-pad, 26px) * -1);
    margin: calc(var(--rp-modal-pad, 26px) * -1) calc(var(--rp-modal-pad, 26px) * -1) 20px;
    padding: var(--rp-modal-pad, 26px) var(--rp-modal-pad, 26px) 16px;
    border-bottom: 1px solid var(--border-color);
}

.rp-modal-footer {
    bottom: calc(var(--rp-modal-pad, 26px) * -1);
    margin: 22px calc(var(--rp-modal-pad, 26px) * -1) calc(var(--rp-modal-pad, 26px) * -1);
    padding: 16px var(--rp-modal-pad, 26px) var(--rp-modal-pad, 26px);
    border-top: 1px solid var(--border-color);
}

    /* A horizontal bar needs the same treatment on the other axis. */
    .modal-body::-webkit-scrollbar-track:horizontal,
    .fg-modal-scroll::-webkit-scrollbar-track:horizontal {
        margin: 0 calc(var(--modal-radius) + 4px);
    }

/* ---------- Typography inside a popup ----------
   Body copy in a popup is body copy. Pages had been dialling single
   modals up to 700-900 across labels, inputs, buttons and even
   placeholders, which makes everything shout and nothing read as
   emphasis. Weight belongs on the title and on genuine emphasis. */
.modal-body,
.fg-modal-body {
    font-weight: var(--fw-normal);
}

    .modal-body .form-label,
    .fg-modal-body .form-label {
        font-weight: var(--fw-medium);
    }

    .modal-body .form-control,
    .modal-body .form-select,
    .fg-modal-body .form-control,
    .fg-modal-body .form-select {
        font-weight: var(--fw-normal);
    }

    .modal-body .form-check-label,
    .fg-modal-body .form-check-label {
        font-weight: var(--fw-normal);
    }

/* ---------- Responsive ---------- */
@media (max-width: 575.98px) {
    .modal-header,
    .modal-body {
        padding: var(--space-4);
    }

    .modal-footer {
        padding: var(--space-3) var(--space-4);
    }

    /* Keep footer actions usable rather than squeezed onto one line. */
    .modal-footer > .btn {
        flex: 1 1 auto;
    }
}
