/* ==========================================================================
   Organization Portal popups: one responsive side-sheet system for every edit panel.
   Loaded after org-portal.css and et-popups.css; overrides earlier modal sizing (including inline
   max-width styles) so no edit panel can collapse to a narrow column again.

   - Edit panels (.modal-card, Bootstrap .modal-dialog) dock to the inline end, full height, and take a
     width that suits their content: sm 560px, default 760px, lg 1040px, xl 1280px, never wider than the
     viewport. Below 720px they take the full screen.
   - Confirmations (.confirm-dialog / role=alertdialog) stay centred and compact.
   - Header and footer never scroll away; the body scrolls (layout rules in et-popups.css).
   ========================================================================== */

:root {
    --org-sheet-sm: 560px;
    --org-sheet-md: 760px;
    --org-sheet-lg: 1040px;
    --org-sheet-xl: 1280px;
    --org-sheet-gap: 24px;
}

/* ---------- overlay ---------- */
.modal-backdrop,
.modal.show,
.modal.d-block {
    position: fixed !important;
    inset: 0 !important;
    display: flex !important;
    flex-direction: row;
    align-items: stretch !important;
    justify-content: flex-end !important; /* inline end: right in LTR, left in RTL */
    padding: 0 !important;
    margin: 0 !important;
    background: rgba(11, 25, 44, 0.5) !important;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    z-index: 100050 !important;
    overflow: hidden !important;
    animation: org-sheet-fade 160ms ease-out;
}

/* ---------- side sheet ---------- */
.modal-backdrop > .modal-card,
.modal > .modal-dialog {
    position: relative;
    box-sizing: border-box;
    width: min(var(--org-sheet-md), 100vw) !important;
    max-width: 100vw !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    min-height: 0 !important;
    margin: 0 !important;
    border-radius: 0 !important;
    border: 0 !important;
    border-inline-start: 1px solid var(--rg-border, #e2e8f0) !important;
    box-shadow: -24px 0 48px -16px rgba(11, 25, 44, 0.35) !important;
    animation: org-sheet-in 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
}

[dir="rtl"] .modal-backdrop > .modal-card,
[dir="rtl"] .modal > .modal-dialog {
    box-shadow: 24px 0 48px -16px rgba(11, 25, 44, 0.35) !important;
    animation-name: org-sheet-in-rtl;
}

.modal-backdrop > .modal-card.sm,
.modal > .modal-dialog.modal-sm { width: min(var(--org-sheet-sm), 100vw) !important; }

.modal-backdrop > .modal-card.lg,
.modal > .modal-dialog.modal-lg { width: min(var(--org-sheet-lg), calc(100vw - var(--org-sheet-gap))) !important; }

.modal-backdrop > .modal-card.xl,
.modal > .modal-dialog.modal-xl { width: min(var(--org-sheet-xl), calc(100vw - var(--org-sheet-gap))) !important; }

/* Bootstrap dialogs: the dialog is the sheet, its content fills it. */
.modal > .modal-dialog { display: flex !important; flex-direction: column; min-height: 100dvh !important; }
.modal > .modal-dialog.modal-dialog-centered { align-items: stretch !important; }
.modal > .modal-dialog > .modal-content {
    flex: 1 1 auto;
    height: 100% !important;
    max-height: 100dvh !important;
    border: 0 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
}

/* Card surface (the sheet itself for .modal-card). */
.modal-backdrop > .modal-card,
.modal > .modal-dialog > .modal-content {
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    background: var(--rg-surface, #ffffff) !important;
}

/* ---------- sections ---------- */
.modal-backdrop > .modal-card :is(.modal-head, .modal-header),
.modal > .modal-dialog :is(.modal-head, .modal-header) {
    position: sticky;
    top: 0;
    z-index: 2;
    flex: 0 0 auto;
    gap: 12px;
    min-height: 64px;
    padding: 16px 24px !important;
    background: var(--rg-surface, #ffffff);
    border-bottom: 1px solid var(--rg-border, #e2e8f0);
}

.modal-backdrop > .modal-card :is(.modal-body),
.modal > .modal-dialog :is(.modal-body) {
    padding: 24px !important;
    overflow-y: auto !important;
    overscroll-behavior: contain;
}

.modal-backdrop > .modal-card :is(.modal-foot, .modal-footer),
.modal > .modal-dialog :is(.modal-foot, .modal-footer) {
    position: sticky;
    bottom: 0;
    z-index: 2;
    flex: 0 0 auto;
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
    padding: 14px 24px !important;
    background: var(--rg-surface-muted, #f8fafc);
    border-top: 1px solid var(--rg-border, #e2e8f0);
}

.modal-backdrop > .modal-card .modal-title,
.modal > .modal-dialog .modal-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--rg-heading, #0b192c);
    margin: 0;
}

/* Forms inside a wide sheet use two columns where there is room. */
.modal-backdrop > .modal-card:not(.sm) .modal-body .form-row,
.modal > .modal-dialog:not(.modal-sm) .modal-body .form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 16px;
}

/* ---------- confirmations stay centred ---------- */
.modal-backdrop:has(> .modal-card.confirm-dialog),
.modal-backdrop:has(> [role="alertdialog"]) {
    justify-content: center !important;
    align-items: center !important;
    padding: 16px !important;
}

.modal-backdrop > .modal-card.confirm-dialog,
.modal-backdrop > [role="alertdialog"].modal-card {
    width: min(480px, calc(100vw - 32px)) !important;
    height: auto !important;
    max-height: min(80dvh, 520px) !important;
    border: 1px solid var(--rg-border, #e2e8f0) !important;
    border-radius: 16px !important;
    box-shadow: 0 24px 48px -12px rgba(11, 25, 44, 0.45) !important;
    animation: org-dialog-in 160ms ease-out;
}

.modal-backdrop > .modal-card.confirm-dialog :is(.modal-head, .modal-foot) { position: static; }

/* ---------- small screens: full-screen sheets ---------- */
@media (max-width: 720px) {
    .modal-backdrop > .modal-card:not(.confirm-dialog),
    .modal > .modal-dialog { width: 100vw !important; border-inline-start: 0 !important; }

    .modal-backdrop > .modal-card :is(.modal-head, .modal-header, .modal-body, .modal-foot, .modal-footer),
    .modal > .modal-dialog :is(.modal-head, .modal-header, .modal-body, .modal-foot, .modal-footer) {
        padding-inline: 16px !important;
    }

    .modal-backdrop > .modal-card :is(.modal-foot, .modal-footer) > .btn { flex: 1 1 auto; }
}

@keyframes org-sheet-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes org-sheet-in { from { transform: translateX(32px); opacity: 0.6; } to { transform: none; opacity: 1; } }
@keyframes org-sheet-in-rtl { from { transform: translateX(-32px); opacity: 0.6; } to { transform: none; opacity: 1; } }
@keyframes org-dialog-in { from { transform: scale(0.97); opacity: 0; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
    .modal-backdrop, .modal, .modal-backdrop > .modal-card, .modal > .modal-dialog { animation: none !important; }
}

/* ---------- dark theme ---------- */
html[data-theme="dark"] .modal-backdrop > .modal-card,
html.dark .modal-backdrop > .modal-card,
html[data-theme="dark"] .modal > .modal-dialog > .modal-content,
html.dark .modal > .modal-dialog > .modal-content,
html[data-theme="dark"] .modal-backdrop > .modal-card :is(.modal-head, .modal-header),
html.dark .modal-backdrop > .modal-card :is(.modal-head, .modal-header) {
    background: var(--rg-surface, #0f1b2d) !important;
}
