/*
 * menu.css — the actions behind a button: the "more" of a row or a page header.
 *
 * <div class="app-menu">
 *   <button class="app-icon-button" type="button" data-variant="ghost" data-size="regular"
 *           aria-label="Weitere Aktionen" aria-busy="false" popovertarget="id">[icon: more]</button>
 *   <div class="app-menu-list" id="id" popover>
 *     <ul class="app-menu-items" role="list">
 *       <li><button class="app-menu-item" type="button" data-tone="neutral | danger" disabled>
 *         <span class="app-menu-item-icon" aria-hidden="true">[icon]</span>
 *         <span class="app-menu-item-label">Duplizieren</span>
 *       </button></li>
 *       <li><a class="app-menu-item" href data-tone="neutral">…</a></li>
 *     </ul>
 *     <ul class="app-menu-items" role="list">…a second group, below a line…</ul>
 *     <ul class="app-menu-items" role="list">
 *       <li class="app-menu-setting">[a menu of its own, by its text]</li>   a setting, in place
 *     </ul>
 *   </div>
 * </div>
 *
 * The button is an icon button (the "more" of a row, the account's avatar), or a quiet button
 * with its text and a chevron, for a menu whose button says what is chosen (the language):
 *
 *   <button class="app-button" type="button" data-variant="ghost" data-size="compact"
 *           aria-label="Sprache: Deutsch" popovertarget="id">
 *     <span class="app-button-icon" aria-hidden="true">[icon: globe]</span>
 *     <span class="app-button-label">Deutsch<span class="app-menu-chevron" aria-hidden="true">[icon: chevron-down]</span></span>
 *   </button>
 *
 * A menu of choices marks the one in force with aria-current and a check at its end:
 *
 *   <li><button class="app-menu-item" type="button" data-tone="neutral" aria-current="true" lang="de">
 *     <span class="app-menu-item-icon" aria-hidden="true"></span>
 *     <span class="app-menu-item-label">Deutsch</span>
 *     <span class="app-menu-item-check" aria-hidden="true">[icon: check]</span>
 *   </button></li>
 *
 * A setting the person changes in passing has a row of its own: the language, a menu of its
 * own at the start, which opens over this one and holds any number of languages, each by its
 * own name. It changes at once. Light or dark is no row here: it is an icon button in the
 * shell's bar, left of the account, which turns on to the next each press (the device's, light,
 * dark) and names the one in force. Where there is no bar, the entry screens write both at the
 * panel's foot (entry.css), the language at the start and light or dark at the end.
 *
 * A list of actions, not an ARIA menu: Tab and Enter, like the rest of a page. The browser's
 * popover does the rest: open from the button, close on Escape and on a click outside, focus
 * back on the button, the expanded state for assistive technology. A followed item closes it
 * (fsm-ui.js).
 *
 * Where it opens. From a viewport of 560, beside its button, end edges aligned, flipping to
 * stay on screen (anchor positioning). Below 560, or where the browser cannot anchor, it is a
 * sheet floating at the bottom of the screen over a scrim, clear of the home indicator: under
 * the thumb, and never off screen.
 */
@layer components {
    .app-menu {
        display: inline-flex;
        anchor-scope: --app-menu;
    }

    .app-menu > [popovertarget] {
        anchor-name: --app-menu;
    }

    /* The sheet. */
    .app-menu-list {
        position: fixed;
        inset: auto var(--app-space-overlay-edge) calc(var(--app-space-overlay-edge) + var(--app-space-safe-bottom));
        inline-size: auto;
        max-inline-size: none;
        margin: 0;
        padding: var(--app-space-list-inset);
        border: 0;
        border-radius: var(--app-radius-popover);
        background: var(--app-color-surface-raised);
        color: var(--app-color-text);
        box-shadow: var(--app-shadow-popover);
    }

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

    .app-menu-items {
        display: flex;
        flex-direction: column;
    }

    .app-menu-items + .app-menu-items {
        padding-block: var(--app-space-list-inset) 0;
        box-shadow: inset 0 var(--app-border-width) 0 var(--app-color-line);
    }

    /* A setting's row: a menu by its text at the start; an icon button, where there is one, at the end. */
    .app-menu-setting {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--app-space-inline);
        padding-block: var(--app-space-list-inset) 0;
    }

    /* A quiet menu button with its text: the chevron says it opens, so it needs no accent, and
       it reads as the menu's items do, its icon subtle. */
    .app-menu > .app-button[data-variant="ghost"] {
        color: var(--app-color-text);
    }

    .app-menu > .app-button[data-variant="ghost"] .app-button-icon {
        color: var(--app-color-text-subtle);
    }

    /* A menu button's chevron, after its text: it opens. */
    .app-button-label:has(> .app-menu-chevron) {
        display: inline-flex;
        align-items: center;
        gap: var(--app-space-chevron);
    }

    .app-menu-chevron {
        display: inline-flex;
        color: var(--app-color-text-subtle);
    }

    .app-menu-item {
        display: flex;
        align-items: center;
        gap: var(--app-space-inline);
        inline-size: 100%;
        min-block-size: var(--app-control-height);
        padding: var(--app-space-control-y) var(--app-space-option-x);
        border: 0;
        border-radius: var(--app-radius-option);
        background: transparent;
        color: var(--app-color-text);
        font-family: var(--app-font-text);
        font-size: var(--app-text-control-size);
        line-height: var(--app-text-control-leading);
        text-align: start;
        text-decoration: none;
        white-space: nowrap;
        cursor: pointer;
    }

    .app-menu-item-icon {
        display: inline-flex;
        flex: none;
        color: var(--app-color-text-subtle);
    }

    .app-menu-item-icon:empty {
        display: none;
    }

    .app-menu-item-label {
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* The choice in force: a check at the end, in the accent. */
    .app-menu-item-check {
        display: none;
        flex: none;
        color: var(--app-color-accent-text);
    }

    .app-menu-item:has(> .app-menu-item-check) > .app-menu-item-label {
        flex: 1;
    }

    .app-menu-item[aria-current="true"] .app-menu-item-check {
        display: inline-flex;
    }

    .app-menu-item[data-tone="danger"],
    .app-menu-item[data-tone="danger"] .app-menu-item-icon {
        color: var(--app-color-danger-text);
    }

    @media (hover: hover) {
        .app-menu-item:hover:not(:disabled) {
            background: var(--app-color-surface-hover);
        }
    }

    .app-menu-item:focus-visible {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: calc(-1 * var(--app-focus-ring-width));
    }

    .app-menu-item:disabled,
    .app-menu-item:disabled .app-menu-item-icon {
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    @media (pointer: coarse) {
        .app-menu-item {
            min-block-size: var(--app-touch-target);
        }
    }

    /* Beside its button. */
    @supports (anchor-scope: all) {
        @media (width >= 560px) {
            .app-menu-list {
                position-anchor: --app-menu;
                inset: calc(anchor(end) + var(--app-space-popover-offset)) anchor(end) auto auto;
                min-inline-size: var(--app-menu-width-min);
                max-inline-size: var(--app-menu-width-max);
                padding: var(--app-space-list-inset);
                border-radius: var(--app-radius-popover);
                box-shadow:
                    0 0 0 var(--app-border-width) var(--app-color-line),
                    var(--app-shadow-popover);
                position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
            }

            .app-menu-list::backdrop {
                background: transparent;
            }
        }
    }

    /* 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-menu-list {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
        .app-menu-items + .app-menu-items {
            border-block-start: var(--app-border-width) solid var(--app-color-forced-line);
        }
    }
}
