/*
   AgentFactory Dialog System
   One authoritative dialog layer for Bootstrap and custom AgentFactory overlays.
   Goals: centered dialogs, professional sizing, no visible dialog scrollbars,
   no clipped/blank work-order overlays, and safe body scroll locking.
*/
:root {
    --af-dialog-safe-x: clamp(14px, 2.6vw, 36px);
    --af-dialog-safe-y: clamp(14px, 2.6vh, 34px);
    --af-dialog-max-h: calc(100dvh - (var(--af-dialog-safe-y) * 2));
    --af-dialog-standard-w: min(980px, calc(100vw - (var(--af-dialog-safe-x) * 2)));
    --af-dialog-wide-w: min(1180px, calc(100vw - (var(--af-dialog-safe-x) * 2)));
    --af-dialog-xl-w: min(1380px, calc(100vw - (var(--af-dialog-safe-x) * 2)));
    --af-dialog-radius: 26px;
    --af-dialog-border: rgba(148, 163, 184, .24);
    --af-dialog-shadow: 0 32px 96px rgba(2, 6, 23, .55);
}

html.af-dialog-open,
body.af-dialog-open,
body.modal-open {
    overflow: hidden !important;
    padding-right: 0 !important;
}

/* Hide visual scrollbars while preserving wheel/touch access for rare overflow. */
.modal-body,
.af-dialog__body,
.agent-studio-dialog-message,
.editor-dialog-message,
.af-qs-gallery-grid,
.af-prompt-enhancer-body,
.af-lowcode-modal-card,
.af-social-modal-card,
.modal-panel,
.imgstudio-confirm-body,
.af-candidate-body,
.af-runpod-launch-body {
    scrollbar-width: none;
    -ms-overflow-style: none;
}
.modal-body::-webkit-scrollbar,
.af-dialog__body::-webkit-scrollbar,
.agent-studio-dialog-message::-webkit-scrollbar,
.editor-dialog-message::-webkit-scrollbar,
.af-qs-gallery-grid::-webkit-scrollbar,
.af-prompt-enhancer-body::-webkit-scrollbar,
.af-lowcode-modal-card::-webkit-scrollbar,
.af-social-modal-card::-webkit-scrollbar,
.modal-panel::-webkit-scrollbar,
.imgstudio-confirm-body::-webkit-scrollbar,
.af-candidate-body::-webkit-scrollbar,
.af-runpod-launch-body::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
    display: none !important;
}

/* Bootstrap modals: centered, large, no page-level scrollbars. */
.modal,
.modal.show,
.modal-open .modal {
    overflow: hidden !important;
}
.modal.show {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    padding: var(--af-dialog-safe-y) var(--af-dialog-safe-x) !important;
}
.modal-dialog {
    width: var(--af-dialog-standard-w) !important;
    max-width: var(--af-dialog-standard-w) !important;
    max-height: var(--af-dialog-max-h) !important;
    margin: 0 auto !important;
    display: flex !important;
    align-items: stretch !important;
    pointer-events: auto;
}
.modal-dialog-centered {
    min-height: 0 !important;
}
.modal-dialog.modal-sm,
.modal-sm {
    width: min(660px, calc(100vw - (var(--af-dialog-safe-x) * 2))) !important;
    max-width: min(660px, calc(100vw - (var(--af-dialog-safe-x) * 2))) !important;
}
.modal-dialog.modal-lg,
.modal-lg {
    width: var(--af-dialog-wide-w) !important;
    max-width: var(--af-dialog-wide-w) !important;
}
.modal-dialog.modal-xl,
.modal-xl {
    width: var(--af-dialog-xl-w) !important;
    max-width: var(--af-dialog-xl-w) !important;
}
.modal-content {
    max-height: var(--af-dialog-max-h) !important;
    min-height: auto;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    border-radius: var(--af-dialog-radius) !important;
    box-shadow: var(--af-dialog-shadow) !important;
}
.modal-header,
.modal-footer {
    flex: 0 0 auto !important;
}
.modal-body {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow: auto !important;
    overflow-wrap: anywhere;
    word-break: normal;
}
.modal-dialog-scrollable {
    height: auto !important;
}
.modal-dialog-scrollable .modal-content {
    max-height: var(--af-dialog-max-h) !important;
}
.modal-dialog-scrollable .modal-body {
    overflow: auto !important;
}
.modal-backdrop {
    z-index: 2040 !important;
}
.modal {
    z-index: 2050 !important;
}

/* Custom overlay families: center, use full safe viewport, never clip the visible card. */
.agent-studio-dialog-overlay,
.af-dialog-backdrop,
.af-rerun-modal,
.editor-dialog-overlay,
.af-qs-modal,
.af-runpod-launch-overlay,
.af-prompt-enhancer-overlay,
.modal-imgstudio,
.imgstudio-confirm-overlay,
.af-social-modal,
.af-lowcode-modal-backdrop {
    align-items: center !important;
    justify-content: center !important;
    padding: var(--af-dialog-safe-y) var(--af-dialog-safe-x) !important;
    overflow: hidden !important;
    box-sizing: border-box !important;
}
.af-dialog-backdrop:not([hidden]),
.af-rerun-modal.is-open,
.editor-dialog-overlay:not(.editor-dialog-hidden),
.af-qs-modal.af-qs-open,
.af-runpod-launch-overlay.is-open,
.af-prompt-enhancer-overlay.is-open,
.modal-imgstudio.open,
.imgstudio-confirm-overlay.open,
.af-social-modal.open {
    display: flex !important;
}

/* Dialog cards: predictable width/height, professional rounded shell. */
.agent-studio-dialog-card,
.af-dialog,
.af-rerun-modal__card,
.editor-dialog,
.af-qs-modal-card,
.af-runpod-launch-card,
.af-prompt-enhancer-card,
.modal-panel,
.imgstudio-confirm-card,
.af-social-modal-card,
.af-lowcode-modal-card {
    box-sizing: border-box !important;
    width: var(--af-dialog-standard-w) !important;
    max-width: var(--af-dialog-standard-w) !important;
    max-height: var(--af-dialog-max-h) !important;
    overflow: hidden !important;
    border-radius: var(--af-dialog-radius) !important;
}
.af-dialog,
.af-rerun-modal__card,
.editor-dialog,
.agent-studio-dialog-card,
.imgstudio-confirm-card {
    min-height: auto;
}
.af-qs-modal-card,
.af-prompt-enhancer-card,
.modal-panel.large-preview,
.af-social-modal-card.wide,
.af-lowcode-modal-card,
.af-workorder-draft-card,
.af-runtime-monitor-card,
.af-print-preview-card {
    width: var(--af-dialog-wide-w) !important;
    max-width: var(--af-dialog-wide-w) !important;
}
.modal-panel.large-preview,
.af-print-preview-card {
    width: var(--af-dialog-xl-w) !important;
    max-width: var(--af-dialog-xl-w) !important;
}

/* Make custom cards internally layout like professional modal shells. */
.agent-studio-dialog-card,
.af-dialog,
.editor-dialog,
.af-qs-modal-card,
.af-runpod-launch-card,
.af-prompt-enhancer-card,
.af-lowcode-modal-card,
.af-social-modal-card,
.modal-panel,
.imgstudio-confirm-card {
    display: flex !important;
    flex-direction: column !important;
}
.agent-studio-dialog-head,
.agent-studio-dialog-actions,
.af-dialog__header,
.af-dialog__footer,
.editor-dialog-actions,
.af-qs-modal-head,
.af-qs-toolbar,
.af-runpod-launch-head,
.af-prompt-enhancer-head,
.af-prompt-enhancer-foot,
.af-lowcode-modal-head,
.af-lowcode-modal-actions,
.imgstudio-confirm-top,
.imgstudio-confirm-actions {
    flex: 0 0 auto !important;
}
.agent-studio-dialog-message,
.af-dialog__body,
.editor-dialog-message,
.af-qs-gallery-grid,
.af-runpod-launch-body,
.af-prompt-enhancer-body,
.af-lowcode-modal-card,
.af-social-modal-card,
.modal-panel,
.imgstudio-confirm-body {
    min-height: 0 !important;
    overflow: auto !important;
    overflow-wrap: anywhere;
    word-break: normal;
}

/* Prevent full-screen WorkOrder launch overlay from blanking the page forever. */
.af-runpod-launch-overlay {
    z-index: 2060 !important;
}
.af-runpod-launch-card {
    width: min(720px, calc(100vw - (var(--af-dialog-safe-x) * 2))) !important;
    max-width: min(720px, calc(100vw - (var(--af-dialog-safe-x) * 2))) !important;
    min-height: 0 !important;
}
.af-runpod-launch-body {
    max-height: calc(var(--af-dialog-max-h) - 112px) !important;
}

/* Quick Start gallery: use width first, then hidden-scroll inner body only when unavoidable. */
.af-qs-gallery-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    max-height: calc(var(--af-dialog-max-h) - 160px) !important;
}

/* Drawers are not centered modals, but still must not show ugly scrollbars. */
.af-candidate-drawer {
    max-height: 100dvh !important;
    overflow: hidden !important;
}
.af-candidate-body {
    overflow: auto !important;
    min-height: 0 !important;
}

/* Waiting cards are non-blocking corner toasts; never blank the page. */
.af-wait-overlay {
    overflow: hidden !important;
    align-items: center !important;
    justify-content: center !important;
    padding: var(--af-dialog-safe-y) var(--af-dialog-safe-x) !important;
}
.af-wait-card {
    width: min(820px, calc(100vw - (var(--af-dialog-safe-x) * 2))) !important;
    max-width: min(820px, calc(100vw - (var(--af-dialog-safe-x) * 2))) !important;
    max-height: var(--af-dialog-max-h) !important;
    overflow: hidden !important;
    scrollbar-width: none;
    -ms-overflow-style: none;
}
.af-wait-card::-webkit-scrollbar { width:0 !important; height:0 !important; display:none !important; }

.af-waiting-backdrop {
    pointer-events: none !important;
    background: transparent !important;
    backdrop-filter: none !important;
}
.af-waiting-card {
    pointer-events: auto !important;
    max-height: min(420px, calc(100dvh - 110px)) !important;
    overflow: hidden !important;
}

@media (max-height: 760px) {
    :root {
        --af-dialog-safe-y: 10px;
    }
    .modal-header,
    .modal-footer,
    .agent-studio-dialog-head,
    .agent-studio-dialog-actions,
    .af-dialog__header,
    .af-dialog__footer,
    .af-qs-modal-head,
    .af-qs-toolbar,
    .af-prompt-enhancer-head,
    .af-prompt-enhancer-foot,
    .af-lowcode-modal-head,
    .af-lowcode-modal-actions {
        padding-top: .78rem !important;
        padding-bottom: .78rem !important;
    }
    .agent-studio-dialog-icon,
    .af-runpod-launch-dot {
        transform: scale(.92);
    }
}
@media (max-width: 991.98px) {
    .af-qs-gallery-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}
@media (max-width: 640px) {
    :root {
        --af-dialog-safe-x: 12px;
        --af-dialog-safe-y: 12px;
        --af-dialog-radius: 22px;
    }
    .modal-dialog,
    .modal-dialog.modal-sm,
    .modal-dialog.modal-lg,
    .modal-dialog.modal-xl,
    .agent-studio-dialog-card,
    .af-dialog,
    .af-rerun-modal__card,
    .editor-dialog,
    .af-qs-modal-card,
    .af-runpod-launch-card,
    .af-prompt-enhancer-card,
    .modal-panel,
    .imgstudio-confirm-card,
    .af-social-modal-card,
    .af-lowcode-modal-card {
        width: calc(100vw - 24px) !important;
        max-width: calc(100vw - 24px) !important;
    }
    .af-qs-gallery-grid {
        grid-template-columns: 1fr;
    }
    .agent-studio-dialog-actions,
    .af-dialog__footer,
    .editor-dialog-actions,
    .imgstudio-confirm-actions,
    .af-lowcode-modal-actions {
        flex-wrap: wrap;
    }
}
