/*
 * button.css — button and icon button.
 *
 * <button class="app-button" type="button" data-variant data-size disabled aria-disabled aria-busy aria-invalid>
 *   <span class="app-button-icon" aria-hidden="true">[icon]</span>
 *   <span class="app-button-label">Speichern</span>
 * </button>
 *
 * <button class="app-icon-button" type="button" data-variant data-size aria-label disabled aria-disabled aria-busy>
 *   <span class="app-icon-button-icon" aria-hidden="true">[icon]</span>
 * </button>
 *
 * <a class="app-button" href data-variant data-size>…</a>
 *
 * A link that looks like a button, for an action that leaves the page: find the way (a maps
 * address), call (tel:). It has no disabled or busy state; where it cannot go, it is not written.
 *
 * data-variant: primary | secondary | ghost | danger. data-size: regular | compact (the
 * density override, scoped to this control). Busy turns the icon slot into a spinner.
 *
 * disabled: the action does not apply now, and the screen shows why without help.
 * aria-disabled="true": unavailable for a reason the screen states in text (offline, a missing
 * right; sync.css .app-reason, linked by aria-describedby). It looks disabled but stays in the tab
 * order so the reason can be heard, and the wrapper ignores its clicks.
 */
@layer components {
    .app-button,
    .app-icon-button {
        position: relative;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: var(--app-space-inline);
        min-block-size: var(--app-control-height);
        border: var(--app-border-width) solid transparent;
        border-radius: var(--app-radius-button);
        background: var(--app-color-surface);
        color: var(--app-color-text);
        font-family: var(--app-font-text);
        font-size: var(--app-text-control-size);
        font-weight: var(--app-text-button-weight);
        line-height: var(--app-text-control-leading);
        white-space: nowrap;
        text-decoration: none;
        cursor: pointer;
        user-select: none;
        transition:
            background-color var(--app-duration-hover) var(--app-easing),
            border-color var(--app-duration-hover) var(--app-easing),
            color var(--app-duration-hover) var(--app-easing);
    }

    .app-button {
        max-inline-size: 100%;
        padding: var(--app-space-control-y) var(--app-space-control-x);
    }

    .app-icon-button {
        inline-size: var(--app-control-height);
        block-size: var(--app-control-height);
        padding: 0;
    }

    /* The account button: the avatar in a round button. */
    .app-icon-button:has(> .app-icon-button-icon > .app-avatar) {
        border-radius: var(--app-radius-round);
    }

    .app-button-icon,
    .app-icon-button-icon {
        display: inline-flex;
        flex: none;
        align-items: center;
        justify-content: center;
    }

    .app-button-icon:empty,
    .app-button-label:empty {
        display: none;
    }

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

    /* Variants */
    :is(.app-button, .app-icon-button)[data-variant="primary"] {
        background: var(--app-color-accent);
        color: var(--app-color-text-on-accent);
    }

    :is(.app-button, .app-icon-button)[data-variant="secondary"] {
        border-color: var(--app-color-line-control);
        background: var(--app-color-surface);
        color: var(--app-color-text);
    }

    :is(.app-button, .app-icon-button)[data-variant="ghost"] {
        background: transparent;
        color: var(--app-color-text);
    }

    /* A ghost button with words has no frame to say it is a button, so its words are in the
       accent text: blue means "act here". A ghost icon button keeps the text colour; its icon
       and its place in a bar already say it. */
    .app-button[data-variant="ghost"] {
        color: var(--app-color-accent-text);
    }

    :is(.app-button, .app-icon-button)[data-variant="danger"] {
        background: var(--app-color-danger);
        color: var(--app-color-text-on-accent);
    }

    @media (hover: hover) {
        :is(.app-button, .app-icon-button)[data-variant="primary"]:hover:not(:disabled, [aria-disabled="true"]) {
            background: var(--app-color-accent-hover);
        }

        :is(.app-button, .app-icon-button)[data-variant="secondary"]:hover:not(:disabled, [aria-disabled="true"], [aria-invalid="true"]) {
            border-color: var(--app-color-line-hover);
            background: var(--app-color-surface-hover);
        }

        :is(.app-button, .app-icon-button)[data-variant="ghost"]:hover:not(:disabled, [aria-disabled="true"]) {
            background: var(--app-color-surface-hover);
        }

        :is(.app-button, .app-icon-button)[data-variant="danger"]:hover:not(:disabled, [aria-disabled="true"]) {
            background: var(--app-color-danger-hover);
        }
    }

    /* Focus */
    :is(.app-button, .app-icon-button):focus-visible {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: var(--app-focus-ring-offset);
    }

    /* Invalid: the action cannot go ahead until something above it is fixed. */
    :is(.app-button, .app-icon-button)[aria-invalid="true"] {
        border-color: var(--app-color-danger-line);
    }

    :is(.app-button, .app-icon-button)[aria-invalid="true"]:focus-visible {
        outline-color: var(--app-color-danger-line);
    }

    /* Disabled */
    :is(.app-button, .app-icon-button):is(:disabled, [aria-disabled="true"]) {
        border-color: transparent;
        background: var(--app-color-surface-disabled);
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    :is(.app-button, .app-icon-button)[data-variant="ghost"]:is(:disabled, [aria-disabled="true"]) {
        background: transparent;
    }

    /* Busy: the icon slot becomes a spinner; the label stays. */
    :is(.app-button, .app-icon-button)[aria-busy="true"] {
        cursor: progress;
    }

    :is(.app-button, .app-icon-button)[aria-busy="true"] :is(.app-button-icon, .app-icon-button-icon) {
        display: inline-flex;
        inline-size: var(--app-icon-size);
        block-size: var(--app-icon-size);
        border: var(--app-border-width-strong) solid currentcolor;
        border-inline-end-color: transparent;
        border-radius: var(--app-radius-round);
        animation: app-spin var(--app-duration-busy) var(--app-easing-busy) infinite;
    }

    :is(.app-button, .app-icon-button)[aria-busy="true"] :is(.app-button-icon, .app-icon-button-icon) > * {
        display: none;
    }

    /* Touch: the hit area reaches the touch target whatever the visible height is. In the action
       bar of a narrow page the buttons are the page's main actions, used with gloves and one
       thumb: there they are 48 tall (the bar 72), and so are the buttons of a phone's dialog or
       drawer footer. */
    @media (pointer: coarse) {
        :is(.app-button, .app-icon-button)::after {
            position: absolute;
            inset: calc((var(--app-control-height) - var(--app-touch-target)) / 2);
            content: "";
        }

        @container app-region (width < 560px) {
            :is(.app-action-bar, .app-entry-actions, .app-agenda-actions) > :is(.app-button, .app-icon-button) {
                min-block-size: var(--app-action-height-touch);
            }

            :is(.app-action-bar, .app-entry-actions, .app-agenda-actions) > :is(.app-button, .app-icon-button)::after {
                inset: 0;
            }
        }

        @media (width <= 480px) {
            :is(.app-dialog-footer, .app-drawer-footer) > :is(.app-button, .app-icon-button) {
                min-block-size: var(--app-action-height-touch);
            }

            :is(.app-dialog-footer, .app-drawer-footer) > :is(.app-button, .app-icon-button)::after {
                inset: 0;
            }
        }
    }

    @keyframes app-spin {
        to {
            transform: rotate(1turn);
        }
    }

    /* Forced colours: the system greys a disabled button by itself, not an aria-disabled one. */
    @media (forced-colors: active) {
        :is(.app-button, .app-icon-button)[aria-disabled="true"] {
            border-color: var(--app-color-forced-disabled);
            color: var(--app-color-forced-disabled);
        }
    }
}
