.mud-dialog.fd-dialog {
    border-radius: 12px;
    overflow: hidden;
    padding: 0 !important;
    margin: 0 !important;
    /* Keep dialog height within the viewport so only the dialog content scrolls. */
    max-height: calc(100dvh - 48px);
}

/* MudBlazor pads the title/action areas even when they are unused. */
.mud-dialog.fd-dialog > .mud-dialog-title,
.mud-dialog.fd-dialog > .mud-dialog-actions {
    padding: 0 !important;
    margin: 0 !important;
}

/* app.css forces dialog content to overflow: visible, which would push long forms
   outside the dialog. Form dialogs scroll their own body instead. */
.mud-dialog-content.fd-content {
    padding: 0 !important;
    margin: 0 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    /* Prevent wheel/touch scroll from chaining to the page behind the dialog. */
    overscroll-behavior: contain;
    max-height: calc(100dvh - 48px);
}

/* Lock background page scroll while a form dialog is open (MudBlazor 7.8 does not
   always keep body.scroll-locked reliably for tall dialogs). */
html:has(.mud-dialog.fd-dialog),
body:has(.mud-dialog.fd-dialog) {
    overflow: hidden !important;
    overscroll-behavior: none;
}

.mud-dialog-container:has(.mud-dialog.fd-dialog) {
    overscroll-behavior: none;
    touch-action: none;
}

.mud-dialog-container:has(.mud-dialog.fd-dialog) .mud-dialog.fd-dialog {
    touch-action: auto;
}

.fd-shell {
    margin: 0;
    padding: 0;
}

.fd-header {
    position: sticky;
    top: 0;
    z-index: 3;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 12px 10px 24px;
    margin: 0;
    color: var(--text-color-white);
    border-top-left-radius: 12px;
    border-top-right-radius: 12px;
}

.fd-header .fd-header-title {
    color: var(--text-color-white);
    font-weight: bold;
}

.fd-header .fd-header-close,
.fd-header .fd-header-close .mud-icon-root,
.fd-header .fd-header-action,
.fd-header .fd-header-action .mud-icon-root {
    color: var(--text-color-white) !important;
}

.fd-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.fd-body {
    padding: 24px;
}

.fd-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
}

@media (max-width: 600px) {
    .fd-body {
        padding: 16px;
    }

    .fd-header {
        padding-left: 16px;
    }
}

/* Application dialogs include MessageComponent. Keep one fixed size so switching
   tabs does not resize or move the window. The tab content scrolls inside. */
.mud-dialog.fd-dialog:has(.custom-message-papper),
.mud-dialog.fd-dialog:has(.fd-stable) {
    display: flex;
    flex-direction: column;
    width: calc(100vw - 160px) !important;
    max-width: calc(100vw - 160px) !important;
    height: calc(100dvh - 96px);
    min-height: calc(100dvh - 96px);
    max-height: calc(100dvh - 96px);
    overflow: hidden;
}

.mud-dialog-content.fd-content:has(.custom-message-papper),
.mud-dialog-content.fd-content:has(.fd-stable) {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    height: 100%;
    min-height: 0;
    max-height: none;
    overflow: hidden !important;
}

.fd-shell:has(.custom-message-papper),
.fd-shell:has(.fd-stable) {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    height: 100%;
    overflow: hidden;
}

.fd-body:has(.fd-stable) {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
}

.fd-body:has(.fd-stable) > .fd-stable {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.fd-body:has(> .custom-message-papper),
.fd-body:has(.fd-stable) > .fd-stable:has(> .custom-message-papper) {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    padding-bottom: 0;
}

.fd-body:has(> .custom-message-papper) > :not(.custom-message-papper),
.fd-stable:has(> .custom-message-papper) > :not(.custom-message-papper) {
    flex: 1 1 auto;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-bottom: 12px;
}

.fd-body > .custom-message-papper,
.fd-stable > .custom-message-papper {
    position: relative;
    flex: 0 0 auto;
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    align-items: flex-end;
    gap: 12px;
    width: auto;
    align-self: stretch;
    margin: 0 -24px;
    padding: 0 12px;
    background: #fff;
    border-top: 1px solid #e5e7eb;
    z-index: 40;
}

.mud-dialog.fd-dialog .main-container,
.mud-dialog.fd-dialog .add-message-container {
    position: relative;
    right: auto;
    bottom: auto;
    flex: 0 0 auto;
    width: min(400px, 100%);
    max-width: 100%;
    border: none;
    border-radius: 10px 10px 0 0;
    box-shadow: none;
}

.fd-body > .custom-message-papper:has(.tab-slide-custom),
.fd-body > .custom-message-papper:has(.message-detail),
.fd-body > .custom-message-papper:has(.add-message-container),
.fd-stable > .custom-message-papper:has(.tab-slide-custom),
.fd-stable > .custom-message-papper:has(.message-detail),
.fd-stable > .custom-message-papper:has(.add-message-container) {
    height: 56px;
}

.mud-dialog.fd-dialog .custom-message-papper:has(.tab-slide-custom) .main-container,
.mud-dialog.fd-dialog .custom-message-papper:has(.message-detail) .main-container,
.mud-dialog.fd-dialog .custom-message-papper:has(.add-message-container) .main-container,
.mud-dialog.fd-dialog .add-message-container {
    position: absolute;
    right: 12px;
    bottom: 0;
    display: flex;
    flex-direction: column;
    width: min(400px, calc(100% - 24px));
    min-height: min(480px, 64vh);
    max-height: min(680px, 76vh);
    overflow: hidden;
    border: 1px solid #e5e7eb;
    border-bottom: none;
    box-shadow: 0 -10px 28px rgba(15, 23, 42, 0.16);
    background: #fff;
    z-index: 40;
}

.mud-dialog.fd-dialog .add-message-container {
    right: calc(12px + min(400px, 100%) + 12px);
    width: min(400px, calc(100% - 36px));
    min-height: 0;
    height: auto;
    max-height: min(680px, 78vh);
}

.mud-dialog.fd-dialog .compose-message {
    display: flex;
    flex-direction: column;
    height: auto;
    max-height: none;
}

.mud-dialog.fd-dialog .compose-body {
    flex: 0 0 auto;
}

.mud-dialog.fd-dialog .message-input textarea {
    min-height: 280px;
}

.mud-dialog.fd-dialog .scroll-area {
    height: auto;
    min-height: 340px;
    max-height: min(520px, 58vh);
    flex: 1 1 auto;
}

.mud-dialog.fd-dialog .scroll-area-empty {
    min-height: 220px;
    max-height: none;
}

.mud-dialog.fd-dialog .message-detail-content {
    height: auto;
    min-height: 300px;
    max-height: min(460px, 54vh);
}

.fd-body:has(.tab-slide-custom) > :not(.custom-message-papper),
.fd-body:has(.message-detail) > :not(.custom-message-papper),
.fd-body:has(.add-message-container) > :not(.custom-message-papper) {
    scroll-padding-bottom: 24px;
}

@media (max-width: 1100px) {
    .mud-dialog.fd-dialog .add-message-container {
        right: 12px;
        bottom: min(500px, 66vh);
        width: min(400px, calc(100% - 24px));
    }
}

@media (max-width: 600px) {
    .fd-body > .custom-message-papper,
    .fd-stable > .custom-message-papper {
        margin-left: -16px;
        margin-right: -16px;
        padding-left: 8px;
        padding-right: 8px;
    }
}

/* Agent application dialog. Keep the message panel inside the window. */
.fd-stable > .custom-message-papper {
    margin-left: 0;
    margin-right: 0;
    padding: 0 16px 8px;
}

.mud-dialog.fd-dialog .fd-stable > .custom-message-papper:has(.tab-slide-custom) .main-container,
.mud-dialog.fd-dialog .fd-stable > .custom-message-papper:has(.message-detail) .main-container,
.mud-dialog.fd-dialog .fd-stable > .custom-message-papper:has(.add-message-container) .main-container {
    right: 16px;
    bottom: 8px;
    height: min(560px, 72vh);
    min-height: min(560px, 72vh);
    max-height: min(560px, 72vh);
}
