/*
 * field.css — text field and select.
 *
 * <div class="app-field">
 *   <label class="app-field-label" for="id">Kundenname</label>
 *   <input class="app-field-control" id="id" type="text" aria-invalid aria-describedby disabled>
 *   <p class="app-field-hint" id="id-hint">…</p>
 *   <p class="app-field-error" id="id-error">…</p>
 * </div>
 *
 * type: text | email | tel | search | password | date | time | datetime-local. Date and time are
 * typed as the browser lets (its fields for day, month, year), in a group with a button that
 * opens the picker:
 *
 * <div class="app-field-group" data-picker="date | time | datetime">
 *   <input class="app-field-control" type="date" …>
 *   <button class="app-field-picker" type="button" aria-label="Datum wählen" aria-haspopup="dialog" aria-expanded="false">[icon]</button>
 *   <template class="app-picker-template" lang="de-AT">
 *     <div class="app-picker" role="dialog" aria-label="Datum wählen" data-kind="date">
 *       <div class="app-picker-calendar">
 *         <div class="app-picker-head"><p class="app-picker-month" aria-live="polite"></p>[icon buttons: data-step="-1", "1"]</div>
 *         <div class="app-picker-grid" role="grid"></div>
 *         <div class="app-picker-foot">[button: data-today]</div>
 *       </div>
 *       <ul class="app-picker-times" role="listbox" aria-label="Uhrzeiten"></ul>   (time, datetime)
 *     </div>
 *   </template>
 * </div>
 *
 * On a fine pointer fsm-ui.js opens the template as a popover under the field: a calendar of
 * the month (weeks from the locale's first day, today ringed, the chosen day in the accent), a
 * list of times in the field's step, or both side by side. On touch the button opens the
 * system's picker (the phone's wheel). A pick writes the ISO value as typing would.
 *
 * A text area is the same field with <textarea class="app-field-control" rows data-rows="3|5|8">
 * in place of the input. It grows with its text, a line at a time, from its rows up to 12 lines;
 * where the browser cannot size it to its content it keeps its rows and a vertical grip.
 *
 * A value with a unit (a number, an amount) puts the input in a group; the group is the frame,
 * drawn as a ring inside it, so the input in it is a full control high:
 *
 * <div class="app-field-group">
 *   <input class="app-field-control" id inputmode="decimal" data-align="end" aria-describedby="id-unit id-hint id-error">
 *   <span class="app-field-unit" id="id-unit">Std.</span>
 * </div>
 *
 * The select keeps the same field frame and puts this in place of the input:
 *
 * <div class="app-select">
 *   <button class="app-select-trigger" type="button" role="combobox" aria-haspopup="listbox"
 *           aria-expanded aria-controls aria-labelledby aria-activedescendant aria-invalid disabled>
 *     <span class="app-select-value" data-placeholder>Offen</span>
 *     <span class="app-select-chevron" aria-hidden="true">[icon]</span>
 *   </button>
 *   <ul class="app-select-list" role="listbox" aria-labelledby>
 *     <li class="app-select-option" role="option" aria-selected data-active aria-disabled>
 *       <span class="app-select-option-label">Offen</span>
 *       <span class="app-select-option-check" aria-hidden="true">[icon]</span>
 *     </li>
 *   </ul>
 * </div>
 *
 * Hint and error are always in the DOM. An empty one takes no space. The error shows while the
 * control is invalid and replaces the hint.
 */
@layer components {
    .app-field {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-label);
        min-inline-size: 0;
    }

    .app-field-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-field-hint,
    .app-field-error {
        font-size: var(--app-text-help-size);
        line-height: var(--app-text-help-leading);
    }

    .app-field-hint {
        color: var(--app-color-text-subtle);
    }

    .app-field-error {
        display: none;
        color: var(--app-color-danger-text);
    }

    .app-field-hint:empty {
        display: none;
    }

    .app-field:has([aria-invalid="true"]) .app-field-error:not(:empty) {
        display: block;
    }

    .app-field:has([aria-invalid="true"]):has(.app-field-error:not(:empty)) .app-field-hint {
        display: none;
    }

    /* The control box, shared by the input and the select trigger. */
    .app-field-control,
    .app-select-trigger {
        inline-size: 100%;
        min-block-size: var(--app-control-height);
        padding: var(--app-space-control-y) var(--app-space-field-x);
        border: var(--app-border-width) solid var(--app-color-line-control);
        border-radius: var(--app-radius-field);
        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-body-weight);
        line-height: var(--app-text-control-leading);
        appearance: none;
        transition: border-color var(--app-duration-hover) var(--app-easing);
    }

    .app-field-control::placeholder {
        color: var(--app-color-text-subtle);
        opacity: 1;
    }

    @media (hover: hover) {
        :is(.app-field-control, .app-select-trigger):hover:not(:disabled, [readonly], [aria-invalid="true"]) {
            border-color: var(--app-color-line-hover);
        }
    }

    :is(.app-field-control, .app-select-trigger):focus-visible {
        border-color: var(--app-color-focus);
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: 0;
    }

    :is(.app-field-control, .app-select-trigger)[aria-invalid="true"] {
        border-color: var(--app-color-danger-line);
    }

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

    .app-field-control[readonly] {
        background: var(--app-color-surface-muted);
    }

    :is(.app-field-control, .app-select-trigger):disabled {
        border-color: var(--app-color-line);
        background: var(--app-color-surface-disabled);
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    .app-field:has(:is(.app-field-control, .app-select-trigger):disabled) .app-field-label {
        color: var(--app-color-text-disabled);
    }

    /* Date and time: the browser's fields for typing, in the field's frame and alignment; the
       picker is the group's button, so the browser's own indicator goes. */
    .app-field-control::-webkit-date-and-time-value {
        text-align: start;
    }

    .app-field-control::-webkit-datetime-edit {
        padding: 0;
    }

    .app-field-control::-webkit-calendar-picker-indicator {
        cursor: pointer;
    }

    .app-field-group[data-picker] {
        padding-inline: var(--app-space-field-x) var(--app-space-picker-button);
    }

    .app-field-group[data-picker] > .app-field-control::-webkit-calendar-picker-indicator {
        display: none;
    }

    .app-field-picker {
        display: inline-flex;
        flex: none;
        align-items: center;
        justify-content: center;
        inline-size: var(--app-control-height-compact);
        block-size: var(--app-control-height-compact);
        padding: 0;
        border: 0;
        border-radius: var(--app-radius-option);
        background: transparent;
        color: var(--app-color-text-subtle);
        cursor: pointer;
        transition:
            background-color var(--app-duration-hover) var(--app-easing),
            color var(--app-duration-hover) var(--app-easing);
    }

    @media (hover: hover) {
        .app-field-picker:hover:not(:disabled) {
            background: var(--app-color-surface-hover);
            color: var(--app-color-text);
        }
    }

    .app-field-picker[aria-expanded="true"] {
        background: var(--app-color-surface-hover);
        color: var(--app-color-text);
    }

    .app-field-picker:focus-visible {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: 0;
    }

    .app-field-picker:disabled {
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    /* The picker: in the top layer under its field (fsm-ui.js places it, above the field where
       the screen ends below), on the raised surface as the select's list. */
    .app-picker {
        position: fixed;
        inset: auto;
        z-index: var(--app-z-select);
        display: grid;
        grid-auto-flow: column;
        align-items: stretch;
        gap: var(--app-space-picker);
        margin: 0;
        padding: var(--app-space-picker);
        border: 0;
        border-radius: var(--app-radius-popover);
        background: var(--app-color-surface-raised);
        color: var(--app-color-text);
        box-shadow:
            0 0 0 var(--app-border-width) var(--app-color-line),
            var(--app-shadow-popover);
    }

    .app-picker[data-kind="time"] {
        padding: var(--app-space-list-inset);
    }

    .app-picker-calendar {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-picker-gap);
    }

    .app-picker-head {
        display: flex;
        align-items: center;
        gap: var(--app-space-list-inset);
        min-block-size: var(--app-control-height);
        padding-inline: var(--app-space-option-x) 0;
    }

    .app-picker-month {
        flex: 1;
        font-size: var(--app-text-control-size);
        font-weight: var(--app-text-strong-weight);
        line-height: var(--app-text-control-leading);
    }

    .app-picker-grid,
    .app-picker-week {
        display: grid;
    }

    .app-picker-week {
        grid-template-columns: repeat(7, var(--app-picker-day));
    }

    .app-picker-weekday {
        display: grid;
        place-items: center;
        block-size: var(--app-picker-weekday);
        color: var(--app-color-text-subtle);
        font-size: var(--app-text-label-size);
        font-weight: var(--app-text-label-weight);
        line-height: var(--app-text-label-leading);
    }

    .app-picker-day {
        display: grid;
        place-items: center;
        inline-size: var(--app-picker-day);
        block-size: var(--app-picker-day);
        padding: 0;
        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);
        cursor: pointer;
        transition: background-color var(--app-duration-hover) var(--app-easing);
    }

    @media (hover: hover) {
        :not([aria-selected="true"]) > .app-picker-day:hover:not([aria-disabled="true"]) {
            background: var(--app-color-surface-hover);
        }
    }

    /* Today is ringed; the chosen day is the accent, whatever else it is. */
    .app-picker-day[aria-current="date"] {
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line-strong);
        font-weight: var(--app-text-strong-weight);
    }

    [aria-selected="true"] > .app-picker-day {
        background: var(--app-color-accent);
        box-shadow: none;
        color: var(--app-color-text-on-accent);
        font-weight: var(--app-text-strong-weight);
    }

    .app-picker-day:focus-visible {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: var(--app-border-width);
    }

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

    .app-picker-foot {
        display: flex;
    }

    .app-picker-times {
        max-block-size: var(--app-select-list-height);
        overflow-y: auto;
        overscroll-behavior: contain;
        scrollbar-color: var(--app-color-line-strong) transparent;
        scrollbar-width: thin;
    }

    /* Beside a calendar the list is as high as the calendar and scrolls within it. */
    .app-picker[data-kind="datetime"] .app-picker-times {
        inline-size: var(--app-picker-times);
        max-block-size: none;
        contain: size;
        padding-inline: var(--app-space-picker) 0;
        border-inline-start: var(--app-border-width) solid var(--app-color-line);
    }

    /* Narrower than a calendar and the times beside it: the times under it, three rows high. */
    @media (width <= 480px) {
        .app-picker[data-kind="datetime"] {
            grid-auto-flow: row;
        }

        .app-picker[data-kind="datetime"] .app-picker-times {
            inline-size: auto;
            max-block-size: calc(3 * var(--app-control-height));
            contain: none;
            padding-block: var(--app-space-picker-gap) 0;
            padding-inline: 0;
            border-block-start: var(--app-border-width) solid var(--app-color-line);
            border-inline-start: 0;
        }
    }

    .app-picker-times .app-select-option {
        justify-content: flex-start;
        font-variant-numeric: tabular-nums;
    }

    .app-picker-times .app-select-option:focus-visible {
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: calc(-1 * var(--app-focus-ring-width));
    }

    @media (prefers-reduced-motion: reduce) {
        :is(.app-field-picker, .app-picker-day) {
            transition: none;
        }
    }

    /* Text area */
    textarea.app-field-control {
        display: block;
        min-block-size: calc(3 * var(--app-text-control-leading) + 2 * var(--app-space-area-y) + 2 * var(--app-border-width));
        max-block-size: calc(12 * var(--app-text-control-leading) + 2 * var(--app-space-area-y) + 2 * var(--app-border-width));
        padding: var(--app-space-area-y) var(--app-space-field-x);
        resize: none;
        field-sizing: content;
    }

    textarea.app-field-control[data-rows="5"] {
        min-block-size: calc(5 * var(--app-text-control-leading) + 2 * var(--app-space-area-y) + 2 * var(--app-border-width));
    }

    textarea.app-field-control[data-rows="8"] {
        min-block-size: calc(8 * var(--app-text-control-leading) + 2 * var(--app-space-area-y) + 2 * var(--app-border-width));
    }

    @supports not (field-sizing: content) {
        textarea.app-field-control {
            resize: vertical;
        }
    }

    /* A value with a unit: the group is the frame, the input inside it has none. */
    .app-field-group {
        display: flex;
        align-items: center;
        gap: var(--app-space-inline);
        min-inline-size: 0;
        min-block-size: var(--app-control-height);
        padding: 0 var(--app-space-field-x);
        border-radius: var(--app-radius-field);
        background: var(--app-color-surface);
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line-control);
        transition: box-shadow var(--app-duration-hover) var(--app-easing);
    }

    .app-field-group > .app-field-control {
        flex: 1;
        min-inline-size: 0;
        min-block-size: var(--app-control-height);
        padding: var(--app-space-control-y) 0;
        border: 0;
        border-radius: 0;
        background: transparent;
    }

    .app-field-group > .app-field-control:focus-visible {
        outline: none;
    }

    .app-field-control[data-align="end"] {
        font-variant-numeric: tabular-nums;
        text-align: end;
    }

    .app-field-unit {
        flex: none;
        color: var(--app-color-text-subtle);
        font-size: var(--app-text-control-size);
        line-height: var(--app-text-control-leading);
    }

    @media (hover: hover) {
        .app-field-group:hover:not(:has(:disabled, [readonly], [aria-invalid="true"])) {
            box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line-hover);
        }
    }

    .app-field-group:has(> .app-field-control:focus-visible) {
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-focus);
        outline: var(--app-focus-ring-width) solid var(--app-color-focus);
        outline-offset: 0;
    }

    .app-field-group:has(> [aria-invalid="true"]) {
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-danger-line);
        outline-color: var(--app-color-danger-line);
    }

    .app-field-group:has(> [readonly]) {
        background: var(--app-color-surface-muted);
    }

    .app-field-group:has(> :disabled) {
        box-shadow: inset 0 0 0 var(--app-border-width) var(--app-color-line);
        background: var(--app-color-surface-disabled);
        color: var(--app-color-text-disabled);
        cursor: not-allowed;
    }

    /* Select */
    .app-select {
        position: relative;
        min-inline-size: 0;
    }

    .app-select-trigger {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--app-space-inline);
        text-align: start;
        cursor: pointer;
    }

    .app-select-value {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-select-value[data-placeholder="true"] {
        color: var(--app-color-text-subtle);
    }

    .app-select-trigger:disabled .app-select-value {
        color: var(--app-color-text-disabled);
    }

    .app-select-chevron {
        display: inline-flex;
        flex: none;
        color: var(--app-color-text-subtle);
        transition: transform var(--app-duration-overlay) var(--app-easing);
    }

    .app-select-trigger[aria-expanded="true"] .app-select-chevron {
        transform: rotate(180deg);
    }

    .app-select-list {
        position: absolute;
        inset-block-start: calc(100% + var(--app-space-popover-offset));
        inset-inline: 0;
        z-index: var(--app-z-select);
        display: none;
        max-block-size: var(--app-select-list-height);
        overflow-y: auto;
        padding: var(--app-space-list-inset);
        border-radius: var(--app-radius-popover);
        background: var(--app-color-surface);
        /* The outline is a ring outside the box, not a border, so the list is options plus
           insets: on the 4px grid. */
        box-shadow:
            0 0 0 var(--app-border-width) var(--app-color-line),
            var(--app-shadow-popover);
    }

    .app-select-trigger[aria-expanded="true"] + .app-select-list {
        display: block;
    }

    /* An open list in the top layer (fsm-ui.js shows it as a popover where the browser can
       anchor): under its field and as wide, flipped above it where the screen ends below, and
       never cut off by the scrolling body of a dialog or drawer. The combobox's popup too. */
    .app-select,
    .app-combobox {
        anchor-scope: --app-field-list;
    }

    .app-select-trigger,
    .app-combobox > .app-field-group {
        anchor-name: --app-field-list;
    }

    @supports (anchor-scope: all) {
        :is(.app-select-list, .app-combobox-popup):popover-open {
            position: fixed;
            position-anchor: --app-field-list;
            inset: calc(anchor(end) + var(--app-space-popover-offset)) auto auto anchor(start);
            inline-size: anchor-size(inline);
            block-size: auto;
            margin: 0;
            border: 0;
            color: var(--app-color-text);
            position-try-fallbacks: flip-block;
            position-visibility: anchors-visible;
        }
    }

    .app-select-option {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: var(--app-space-inline);
        min-block-size: var(--app-control-height);
        padding: var(--app-space-control-y) var(--app-space-option-x);
        border-radius: var(--app-radius-option);
        color: var(--app-color-text);
        cursor: pointer;
    }

    .app-select-option-check {
        display: inline-flex;
        flex: none;
        color: var(--app-color-accent);
        visibility: hidden;
    }

    .app-select-option[aria-selected="true"] {
        color: var(--app-color-accent-text);
        font-weight: var(--app-text-strong-weight);
    }

    .app-select-option[aria-selected="true"] .app-select-option-check {
        visibility: visible;
    }

    .app-select-option[data-active="true"] {
        background: var(--app-color-surface-hover);
    }

    @media (hover: hover) {
        .app-select-option:hover:not([aria-disabled="true"]) {
            background: var(--app-color-surface-hover);
        }
    }

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

    /* 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-field-group {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
        :is(.app-select-list, .app-picker) {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
        [aria-selected="true"] > .app-picker-day {
            forced-color-adjust: none;
            background: var(--app-color-forced-current);
            color: var(--app-color-forced-current-text);
        }
        .app-picker-day[aria-current="date"] {
            outline: var(--app-border-width) solid var(--app-color-forced-line);
            outline-offset: calc(-1 * var(--app-border-width));
        }
        .app-select-option[data-active="true"] {
            forced-color-adjust: none;
            background: var(--app-color-forced-current);
            color: var(--app-color-forced-current-text);
        }

        /* A muted second line takes the highlight's text colour too. */
        .app-select-option[data-active="true"] * {
            color: inherit;
        }
    }
}
