/*
 * dialog.css
 *
 * <dialog class="app-dialog" data-size="s | m" aria-labelledby open>
 *   <header class="app-dialog-header">
 *     <h2 class="app-dialog-title" id>Auftrag stornieren</h2>
 *     <button class="app-icon-button" data-variant="ghost" data-size="compact" aria-label="Schließen">…</button>
 *   </header>
 *   <div class="app-dialog-body">…</div>
 *   <footer class="app-dialog-footer">[buttons]</footer>
 * </dialog>
 *
 * The wrapper opens it with showModal(), which brings the backdrop, the focus trap and Escape.
 * A specimen in Storybook carries the open attribute only. Two sizes and no more: s (400) for a
 * question with at most one field, m (560) for a short task of a few fields. What needs more
 * room is a page. On a phone (a viewport up to 480) it is a sheet from the bottom as tall as
 * its content: its actions sit under the thumb and it clears the home indicator.
 */
@layer components {
    .app-dialog {
        position: fixed;
        inset: 0;
        z-index: var(--app-z-modal);
        inline-size: min(var(--app-dialog-width-m), calc(100% - 2 * var(--app-space-page)));
        max-inline-size: none;
        max-block-size: calc(var(--app-page-height) - 2 * var(--app-space-page));
        padding: var(--app-space-dialog) 0;
        border: 0;
        border-radius: var(--app-radius-card);
        background: var(--app-color-surface-raised);
        color: var(--app-color-text);
        box-shadow: var(--app-shadow-dialog);
    }

    .app-dialog[open] {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-dialog-gap);
    }

    .app-dialog[data-size="s"] {
        inline-size: min(var(--app-dialog-width-s), calc(100% - 2 * var(--app-space-page)));
    }

    .app-dialog::backdrop {
        background: var(--app-color-scrim);
    }

    /* Header, body and footer are shared with the drawer. */
    :is(.app-dialog-header, .app-drawer-header) {
        display: flex;
        flex: none;
        align-items: center;
        justify-content: space-between;
        gap: var(--app-space-inline);
        padding: 0 var(--app-space-dialog);
    }

    :is(.app-dialog-title, .app-drawer-title) {
        min-inline-size: 0;
        font-family: var(--app-font-display);
        font-size: var(--app-text-heading-size);
        font-weight: var(--app-text-heading-weight);
        line-height: var(--app-text-heading-leading);
    }

    /* The body scrolls. Its small vertical inset keeps a focus ring inside the scroll box. */
    :is(.app-dialog-body, .app-drawer-body) {
        flex: 1 1 auto;
        min-block-size: 0;
        overflow-y: auto;
        padding: var(--app-space-focus-room) var(--app-space-dialog);
        overscroll-behavior: contain;
    }

    :is(.app-dialog-footer, .app-drawer-footer) {
        display: flex;
        flex: none;
        flex-wrap: wrap;
        justify-content: flex-end;
        gap: var(--app-space-actions);
        padding: 0 var(--app-space-dialog);
    }

    :is(.app-dialog-footer, .app-drawer-footer):empty {
        display: none;
    }

    /* On a phone the dialog is a sheet from the bottom and the actions share the width.
       Viewport, --app-breakpoint-480. */
    @media (width <= 480px) {
        .app-dialog,
        .app-dialog[data-size] {
            inset-block: auto 0;
            inset-inline: 0;
            inline-size: 100%;
            max-block-size: calc(var(--app-page-height) - var(--app-space-section));
            margin: 0;
            padding-block: var(--app-space-dialog) calc(var(--app-space-dialog) + var(--app-space-safe-bottom));
            border-end-start-radius: 0;
            border-end-end-radius: 0;
        }

        :is(.app-dialog-footer, .app-drawer-footer) > * {
            flex: 1 1 0;
        }

        /* The sheet rises above the keyboard, its footer on it (shell.css). */
        :root[data-keyboard] :is(.app-dialog, .app-dialog[data-size]) {
            inset-block-end: var(--app-space-keyboard);
            max-block-size: calc(var(--app-page-height) - var(--app-space-keyboard) - var(--app-space-section));
            padding-block: var(--app-space-dialog);
        }
    }

    /* Forced colours (a Windows contrast theme): shadows and backgrounds are gone, so frames
       are outlines and a chosen state is the system highlight. */
    @media (forced-colors: active) {
        .app-dialog {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
    }
}
