/**
 * Global viewport-safe Bootstrap modals — EnjazSmart
 *
 * Keep normal Bootstrap widths (sm / lg / xl) and centered placement.
 * Cap height to the viewport; scroll .modal-body so header + footer stay visible.
 * Works when a <form> wraps header / body / footer.
 *
 * Note: dashboard.js adds .modal-fullscreen-sm-down to dialogs. Do NOT treat that
 * class as always-fullscreen — Bootstrap only expands it below the sm breakpoint.
 */

.modal {
    --es-modal-gap: 1rem;
}

/* Always center horizontally */
.modal .modal-dialog:not(.modal-fullscreen) {
    margin-left: auto;
    margin-right: auto;
    margin-top: var(--es-modal-gap);
    margin-bottom: var(--es-modal-gap);
    max-height: calc(100vh - (var(--es-modal-gap) * 2));
    max-height: calc(100dvh - (var(--es-modal-gap) * 2));
}

/*
 * Bootstrap sets .modal-dialog-scrollable { height: 100% } which forces a
 * full-viewport panel. Size to content up to the viewport instead.
 */
.modal .modal-dialog.modal-dialog-scrollable:not(.modal-fullscreen) {
    height: auto;
    max-height: calc(100vh - (var(--es-modal-gap) * 2));
    max-height: calc(100dvh - (var(--es-modal-gap) * 2));
}

.modal .modal-dialog.modal-dialog-centered:not(.modal-fullscreen) {
    min-height: calc(100% - (var(--es-modal-gap) * 2));
    align-items: center;
}

.modal .modal-dialog:not(.modal-fullscreen) .modal-content {
    max-height: calc(100vh - (var(--es-modal-gap) * 2));
    max-height: calc(100dvh - (var(--es-modal-gap) * 2));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: auto;
    width: 100%;
}

/* Form between content and sections must participate in the flex height chain */
.modal .modal-content > form {
    display: flex;
    flex-direction: column;
    min-height: 0;
    max-height: inherit;
    overflow: hidden;
    flex: 0 1 auto;
}

.modal .modal-header,
.modal .modal-footer {
    flex-shrink: 0;
}

.modal .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

/* Nested wide tables must scroll horizontally inside the modal body */
.modal .table-responsive,
.modal .card-datatable {
    overflow-x: auto;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
}

.modal .table-responsive .table th,
.modal .table-responsive .table td,
.modal .card-datatable .table th,
.modal .card-datatable .table td {
    white-space: nowrap;
    word-break: normal;
}

.modal .table-responsive .table:not(.dataTable) {
    width: max-content;
    min-width: 100%;
}

/* Equal-height card fields must not stretch inside scrollable modal bodies */
.modal .es-worker-field,
.modal .es-entity-field {
    height: auto;
}

/* Mobile: keep centered with edge padding (fullscreen-*-down handled by Bootstrap) */
@media (max-width: 575.98px) {
    .modal {
        --es-modal-gap: 0.5rem;
    }

    .modal .modal-dialog:not(.modal-fullscreen):not(.modal-fullscreen-sm-down) {
        margin-left: auto;
        margin-right: auto;
        width: calc(100% - (var(--es-modal-gap) * 2));
    }
}

/* True fullscreen only — never match modal-fullscreen-*-down here */
.modal .modal-dialog.modal-fullscreen {
    max-height: 100%;
    height: 100%;
    margin: 0;
    width: 100%;
}

.modal .modal-dialog.modal-fullscreen .modal-content {
    max-height: 100%;
    height: 100%;
}
