/* Layers above the page: dialogs, drawers, popovers, menus, tooltips, toasts, command palette. */

/* ── Dialogs ─────────────────────────────────────────────────────────────── */

dialog {
    max-width: none;
    max-height: none;
    padding: 0;
    border: 0;
    background: transparent;
    color: inherit;
}

dialog::backdrop {
    background: transparent;
}

/* The dialog element covers the viewport and is the scrim; the surface is the visible box. */
dialog.modal[open],
dialog.drawer[open],
dialog.palette[open] {
    position: fixed;
    inset: 0;
    display: flex;
    width: 100vw;
    height: 100dvh;
    background: var(--overlay);
    animation: fade-in var(--duration-normal) var(--ease-out);
}

dialog.modal[open] {
    align-items: flex-start;
    justify-content: center;
    padding: min(12vh, 7rem) var(--space-4) var(--space-4);
    overflow-y: auto;
}

dialog.modal--confirm[open] {
    padding-top: min(22vh, 12rem);
}

.dialog__surface {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-height: calc(100dvh - min(12vh, 7rem) - var(--space-8));
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    background: var(--surface-raised);
    box-shadow: var(--shadow-lg);
    animation: pop-in var(--duration-slow) var(--ease-spring);
}

.modal--sm .dialog__surface {
    max-width: 26rem;
}

.modal--md .dialog__surface {
    max-width: 34rem;
}

.modal--lg .dialog__surface {
    max-width: 48rem;
}

.modal--xl .dialog__surface {
    max-width: 68rem;
}

dialog.is-closing {
    animation: fade-out var(--duration-normal) var(--ease-out) forwards;
}

dialog.modal.is-closing .dialog__surface {
    animation: pop-out var(--duration-normal) var(--ease-out) forwards;
}

@keyframes fade-out {
    to {
        opacity: 0;
    }
}

@keyframes pop-out {
    to {
        opacity: 0;
        transform: scale(0.97) translateY(4px);
    }
}

.dialog__header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-4);
    padding: var(--space-5) var(--space-5) 0 var(--space-6);
}

.dialog__heading {
    min-width: 0;
    padding-top: 0.125rem;
}

.dialog__title {
    font-size: var(--text-lg);
    font-weight: 650;
}

.dialog__description {
    margin-top: var(--space-1);
    color: var(--text-muted);
}

.dialog__close {
    flex: none;
    margin: -0.25rem -0.25rem 0 0;
}

.dialog__body {
    flex: 1;
    min-height: 0;
    padding: var(--space-4) var(--space-6) var(--space-6);
    overflow-y: auto;
}

.dialog__body:empty {
    padding-bottom: var(--space-2);
}

.dialog__message {
    color: var(--text-secondary);
    font-size: var(--text-md);
}

.dialog__footer {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-6);
    border-top: 1px solid var(--border-subtle);
    border-radius: 0 0 var(--radius-xl) var(--radius-xl);
    background: var(--surface-subtle);
}

/* Drawer */

dialog.drawer[open] {
    justify-content: flex-end;
}

dialog.drawer--left[open] {
    justify-content: flex-start;
}

.drawer .dialog__surface {
    max-height: none;
    height: calc(100dvh - var(--space-4));
    margin: var(--space-2);
    border-radius: var(--radius-lg);
    animation: drawer-in var(--duration-slow) var(--ease-out);
}

.drawer--left .dialog__surface {
    animation-name: drawer-in-left;
}

.drawer--sm .dialog__surface {
    max-width: 24rem;
}

.drawer--md .dialog__surface {
    max-width: 32rem;
}

.drawer--lg .dialog__surface {
    max-width: 46rem;
}

.drawer .dialog__footer {
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

dialog.drawer.is-closing .dialog__surface {
    animation: drawer-out var(--duration-normal) var(--ease-in-out) forwards;
}

dialog.drawer--left.is-closing .dialog__surface {
    animation-name: drawer-out-left;
}

/* A non-modal drawer leaves the page usable: no scrim, clicks pass through beside it. */
dialog.drawer:not(:modal)[open] {
    z-index: var(--z-drawer);
    background: transparent;
    pointer-events: none;
}

dialog.drawer:not(:modal) .dialog__surface {
    pointer-events: auto;
}

@keyframes drawer-in {
    from {
        opacity: 0.4;
        transform: translateX(2.5rem);
    }
}

@keyframes drawer-in-left {
    from {
        opacity: 0.4;
        transform: translateX(-2.5rem);
    }
}

@keyframes drawer-out {
    to {
        opacity: 0;
        transform: translateX(2.5rem);
    }
}

@keyframes drawer-out-left {
    to {
        opacity: 0;
        transform: translateX(-2.5rem);
    }
}

/* ── Popovers and menus ──────────────────────────────────────────────────── */

.popover {
    position: fixed;
    z-index: var(--z-popover);
    display: flex;
    flex-direction: column;
    min-width: 9rem;
    max-width: min(26rem, calc(100vw - 1rem));
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface-raised);
    box-shadow: var(--shadow-md);
    transform-origin: top left;
    animation: popover-in var(--duration-normal) var(--ease-out);
}

.popover[data-side="top"] {
    transform-origin: bottom left;
}

.popover.is-closing {
    animation: popover-out var(--duration-fast) var(--ease-out) forwards;
}

@keyframes popover-in {
    from {
        opacity: 0;
        transform: scale(0.96) translateY(-3px);
    }
}

@keyframes popover-out {
    to {
        opacity: 0;
        transform: scale(0.97);
    }
}

.menu {
    display: flex;
    flex-direction: column;
    gap: 1px;
    padding: 0.25rem;
    overflow-y: auto;
    outline: none;
}

.menu__item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    min-height: 1.875rem;
    padding: 0.25rem 0.5rem;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text);
    text-align: left;
    text-decoration: none;
    cursor: pointer;
}

.menu__item .icon {
    color: var(--text-muted);
}

.menu__item:hover {
    text-decoration: none;
}

.menu__item:focus,
.menu__item:focus-visible {
    outline: none;
    background: var(--surface-hover);
}

.menu__item:disabled,
.menu__item[aria-disabled="true"] {
    color: var(--text-disabled);
    cursor: not-allowed;
}

.menu__item--danger,
.menu__item--danger .icon {
    color: var(--danger);
}

.menu__item--danger:focus {
    background: var(--danger-soft);
}

.menu__check {
    display: inline-flex;
    width: 0.875rem;
    color: var(--accent-text);
}

.menu__check .icon {
    color: inherit;
}

.menu__text {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    line-height: 1.35;
}

.menu__description {
    color: var(--text-muted);
    font-size: var(--text-sm);
}

.menu__shortcut {
    color: var(--text-muted);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
}

.menu__separator {
    height: 1px;
    margin: 0.25rem -0.25rem;
    background: var(--border-subtle);
}

.menu__heading {
    padding: 0.375rem 0.5rem 0.125rem;
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.menu__custom {
    padding: 0.25rem 0.5rem;
}

.user-card {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 0.25rem 0;
}

.user-card__text {
    display: flex;
    flex-direction: column;
    min-width: 0;
    line-height: 1.35;
}

.user-card__name {
    font-weight: 600;
}

.user-card__mail {
    color: var(--text-muted);
    font-size: var(--text-sm);
}

.user-theme {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    padding: 0.125rem 0 0.25rem;
}

.user-theme__label {
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

/* ── Tooltip ─────────────────────────────────────────────────────────────── */

.tooltip {
    position: fixed;
    z-index: var(--z-toast);
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    max-width: 18rem;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    background: light-dark(#1f1e1b, #f1efea);
    color: light-dark(#fff, #171614);
    font-size: var(--text-sm);
    font-weight: 500;
    box-shadow: var(--shadow-sm);
    pointer-events: none;
    animation: fade-in var(--duration-fast) var(--ease-out);
}

.tooltip__kbd {
    font-family: var(--font-sans);
    font-size: var(--text-xs);
    opacity: 0.65;
}

/* ── Toasts ──────────────────────────────────────────────────────────────── */

.toasts {
    position: fixed;
    right: var(--space-5);
    bottom: var(--space-5);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    width: min(24rem, calc(100vw - 2rem));
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: 0.625rem;
    padding: 0.625rem 0.5rem 0.625rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface-raised);
    box-shadow: var(--shadow-md);
    pointer-events: auto;
    animation: toast-in var(--duration-slow) var(--ease-spring);
}

.toast.is-closing {
    animation: toast-out var(--duration-normal) var(--ease-in-out) forwards;
}

@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateY(0.75rem) scale(0.96);
    }
}

@keyframes toast-out {
    to {
        opacity: 0;
        transform: translateX(1.5rem);
    }
}

.toast__icon {
    display: inline-flex;
    margin-top: 0.125rem;
    color: var(--info);
}

.toast--success .toast__icon {
    color: var(--success);
}

.toast--error .toast__icon {
    color: var(--danger);
}

.toast--warning .toast__icon {
    color: var(--warning);
}

.toast__content {
    flex: 1;
    min-width: 0;
}

.toast__message {
    font-weight: 550;
}

.toast__description {
    margin-top: 0.125rem;
    color: var(--text-muted);
}

.toast__action {
    flex: none;
    padding: 0.125rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: transparent;
    font-size: var(--text-sm);
    font-weight: 600;
}

.toast__action:hover {
    background: var(--surface-hover);
}

.toast__close {
    display: inline-flex;
    flex: none;
    padding: 0.1875rem;
    border: 0;
    border-radius: var(--radius-xs);
    background: transparent;
    color: var(--text-muted);
}

.toast__close:hover {
    background: var(--surface-hover);
    color: var(--text);
}

/* ── Command palette ─────────────────────────────────────────────────────── */

dialog.palette[open] {
    align-items: flex-start;
    justify-content: center;
    padding: min(16vh, 9rem) var(--space-4) var(--space-4);
}

.palette__surface {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 38rem;
    max-height: min(30rem, calc(100dvh - 12rem));
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    background: var(--surface-raised);
    box-shadow: var(--shadow-lg);
    animation: pop-in var(--duration-normal) var(--ease-out);
}

dialog.palette.is-closing .palette__surface {
    animation: pop-out var(--duration-fast) var(--ease-out) forwards;
}

.palette__head {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    height: 3.25rem;
    padding: 0 var(--space-4);
    border-bottom: 1px solid var(--border-subtle);
    color: var(--text-muted);
}

.palette__input {
    flex: 1;
    min-width: 0;
    height: 100%;
    padding: 0;
    border: 0;
    outline: none;
    background: transparent;
    color: var(--text);
    font-size: var(--text-lg);
}

.palette__input::placeholder {
    color: var(--text-muted);
}

.palette__busy {
    display: inline-flex;
}

.palette__list {
    flex: 1;
    padding: var(--space-2);
    overflow-y: auto;
    scroll-padding: var(--space-8) 0;
}

.palette__group {
    padding: var(--space-2) 0.625rem 0.25rem;
    color: var(--text-muted);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.palette__item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: 2.375rem;
    padding: 0.25rem 0.625rem;
    border-radius: var(--radius-md);
    cursor: pointer;
}

.palette__item-icon {
    display: grid;
    flex: none;
    place-items: center;
    width: 1.625rem;
    height: 1.625rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-subtle);
    color: var(--text-secondary);
}

.palette__item-text {
    display: flex;
    flex: 1;
    align-items: baseline;
    gap: 0.625rem;
    min-width: 0;
}

.palette__item-label {
    overflow: hidden;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.palette__item-description {
    overflow: hidden;
    color: var(--text-muted);
    font-size: var(--text-sm);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.palette__item-enter {
    color: var(--text-muted);
    opacity: 0;
}

.palette__item.is-active {
    background: var(--surface-hover);
}

.palette__item.is-active .palette__item-icon {
    border-color: var(--accent-border);
    background: var(--accent-soft);
    color: var(--accent-text);
}

.palette__item.is-active .palette__item-enter {
    opacity: 1;
}

.palette__empty {
    padding: var(--space-8);
    color: var(--text-muted);
    text-align: center;
}

.palette__foot {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    height: 2.25rem;
    padding: 0 var(--space-4);
    border-top: 1px solid var(--border-subtle);
    background: var(--surface-subtle);
    color: var(--text-muted);
    font-size: var(--text-sm);
}

.palette__foot span {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
}

.palette__foot-hint {
    margin-left: auto;
}

@media (max-width: 760px) {
    .palette__foot-hint {
        display: none !important;
    }
}
