/*
 * segmented.css — one of a few, side by side: a setting (Hell · System · Dunkel) or a view
 * (Liste · Kalender, Tag · Woche). It is a radio group, so it always has a value, the arrow keys
 * move it and a form sends it.
 *
 * <div class="app-segmented" role="radiogroup" aria-labelledby="id-label" data-size="regular | compact" data-fill data-label="hidden">
 *   <span class="app-segmented-label" id="id-label">Darstellung</span>
 *   <div class="app-segmented-track">
 *     <label class="app-segment">
 *       <input class="app-segment-input" type="radio" name value checked disabled>
 *       <span class="app-segment-label">Hell</span>
 *     </label>
 *   </div>
 * </div>
 *
 * Short, where the room is a menu's row: each segment shows a mark, an icon or a code of two or
 * three letters, and keeps its label for assistive technology and as its tooltip.
 *
 *     <label class="app-segment" title="Dunkel">
 *       <input class="app-segment-input" type="radio" name value>
 *       <span class="app-segment-mark" aria-hidden="true">[icon: moon] or DE</span>
 *       <span class="app-segment-label">Dunkel</span>
 *     </label>
 *
 * The segments share one frame and the width of the widest; data-fill stretches the frame over
 * the line (a phone's settings). The chosen segment looks like a pressed toggle chip: an accent
 * ring, the accent wash, accent text. Each native radio covers its segment, unseen, as on a
 * choice. data-label="hidden" keeps the label for assistive technology only, where the place
 * names the choice (a view switch in a page header). The frame is a ring over the segments, not
 * a border; in a contrast theme, an outline. Between two segments there is one line: a divider,
 * or the chosen segment's ring, which covers the dividers on both its sides.
 */
@layer components {
    .app-segmented {
        display: inline-flex;
        flex-direction: column;
        gap: var(--app-space-label);
        max-inline-size: 100%;
        min-inline-size: 0;
    }

    .app-segmented[data-fill] {
        display: flex;
    }

    .app-segmented-label {
        color: var(--app-color-text-muted);
        font-size: var(--app-text-label-size);
        font-weight: var(--app-text-label-weight);
        line-height: var(--app-text-label-leading);
    }

    .app-segmented[data-label="hidden"] .app-segmented-label {
        position: absolute;
        inline-size: var(--app-border-width);
        block-size: var(--app-border-width);
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    /* The frame is a ring, not a border (it would push the segments off the 4px rhythm), drawn
       above the segments so a hovered one does not cover it; the chosen one's ring is above it. */
    .app-segmented-track {
        position: relative;
        display: inline-grid;
        grid-auto-columns: minmax(0, 1fr);
        grid-auto-flow: column;
        min-block-size: var(--app-control-height);
        overflow: clip;
        border-radius: var(--app-radius-field);
        background: var(--app-color-surface);
    }

    .app-segmented-track::after {
        position: absolute;
        z-index: 1;
        inset: 0;
        border-radius: inherit;
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line-control);
        content: "";
        pointer-events: none;
    }

    .app-segmented[data-fill] .app-segmented-track {
        display: grid;
    }

    .app-segment {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        min-inline-size: 0;
        padding: var(--app-space-control-y) var(--app-space-control-x);
        color: var(--app-color-text);
        font-family: var(--app-font-text);
        font-size: var(--app-text-control-size);
        font-weight: var(--app-text-segment-weight);
        line-height: var(--app-text-control-leading);
        cursor: pointer;
        transition: background-color var(--app-duration-hover) var(--app-easing);
    }

    /* The ends round with the frame, so a chosen end's ring follows its corners. */
    .app-segment:first-child {
        border-start-start-radius: var(--app-radius-field);
        border-end-start-radius: var(--app-radius-field);
    }

    .app-segment:last-child {
        border-start-end-radius: var(--app-radius-field);
        border-end-end-radius: var(--app-radius-field);
    }

    /* A divider on the segment's start edge, out of the layout; a border, so a contrast theme
       keeps it. */
    .app-segment + .app-segment::before {
        position: absolute;
        inset-block: 0;
        inset-inline-start: 0;
        border-inline-start: var(--app-border-width) solid var(--app-color-line-control);
        content: "";
        pointer-events: none;
    }

    /* The chosen segment's ring: over its own divider and, a line further, the next one's. */
    .app-segment:has(.app-segment-input:checked)::after {
        position: absolute;
        inset-block: 0;
        inset-inline: 0 calc(-1 * var(--app-border-width));
        border-radius: inherit;
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-accent);
        content: "";
        pointer-events: none;
    }

    .app-segment:last-child:has(.app-segment-input:checked)::after {
        inset-inline-end: 0;
    }

    .app-segment-label {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-segment-mark {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-block-size: var(--app-text-control-leading);
    }

    .app-segment-mark + .app-segment-label {
        position: absolute;
        inline-size: var(--app-border-width);
        block-size: var(--app-border-width);
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    /* Over the whole segment, so the hit area is the segment and the browser keeps the keys. */
    .app-segment-input {
        position: absolute;
        inset: 0;
        inline-size: 100%;
        block-size: 100%;
        margin: 0;
        opacity: 0;
        cursor: inherit;
    }

    @media (hover: hover) {
        .app-segment:hover:not(:has(.app-segment-input:checked, .app-segment-input:disabled)) {
            background: var(--app-color-surface-hover);
        }
    }

    .app-segment:has(.app-segment-input:checked) {
        z-index: 2;
        background: var(--app-color-accent-soft);
        color: var(--app-color-accent-text);
    }

    .app-segment:has(.app-segment-input:focus-visible) {
        z-index: 3;
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: calc(-1 * var(--app-focus-ring-width));
    }

    .app-segment:has(.app-segment-input:disabled) {
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    /* The whole control off: the frame of a disabled field. */
    .app-segmented:not(:has(.app-segment-input:enabled)) .app-segmented-track {
        background: var(--app-color-surface-disabled);
    }

    .app-segmented:not(:has(.app-segment-input:enabled)) .app-segmented-track::after {
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line);
    }

    .app-segmented:not(:has(.app-segment-input:enabled)) .app-segment:has(.app-segment-input:checked) {
        background: var(--app-color-surface-muted);
        color: var(--app-color-text-disabled);
    }

    .app-segmented:not(:has(.app-segment-input:enabled)) .app-segment:has(.app-segment-input:checked)::after {
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line-strong);
    }

    /* Forced colours (a Windows contrast theme): the frame and the dividers are borders and stay;
       the chosen segment is the system highlight. */
    @media (forced-colors: active) {
        .app-segmented-track {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
        .app-segment:has(.app-segment-input:checked) {
            forced-color-adjust: none;
            background: var(--app-color-forced-current);
            color: var(--app-color-forced-current-text);
        }
        .app-segment:has(.app-segment-input:disabled) {
            color: var(--app-color-forced-disabled);
        }
    }
}
