/*
 * form.css — the layout of a form: sections, and where a field puts its label; and the form's
 * read-only twin, the description list.
 *
 * <div class="app-form" data-labels="top | side">
 *   <div class="app-notice">…</div>   (optional: what happened to the form as a whole, first)
 *   <section class="app-form-section" aria-labelledby>
 *     <div class="app-form-section-header">
 *       <h2 class="app-form-section-title" id>Kunde</h2>
 *       <p class="app-form-section-description">…</p>
 *       <div class="app-form-section-actions">[a switch, a button]</div>   (optional)
 *     </div>
 *     <div class="app-columns">
 *       <div class="app-column" data-span="6">[field]</div>
 *     </div>
 *   </section>
 * </div>
 *
 * The fields of a section sit on the column grid (layout.css); a field never knows how wide it
 * is. A form is at most 800 wide (--app-form-width), at the start of its region: on a wide
 * screen a field a whole row long would leave its label far from its end, and a person reads a
 * form down, not across. What stands beside it (a record's side column) takes the rest. Sections are divided by a line and the section space. The description is always written
 * and takes no space when empty.
 *
 * data-labels="side" puts every label in a fixed track of its own beside the control, once the
 * region is 560 wide. The track has the same width everywhere, so labels and controls make two
 * straight edges down the form and across sections. A side-label field takes the whole row,
 * whatever its span: the form is at most 800 wide, and a label track beside a part of a row
 * would leave the control too narrow for its value or its error. So a side-label form reads
 * down, one field a row, as settings do. Below 560 every label goes on top and the spans apply.
 * A field, a radio group, a lone checkbox and a segmented control all follow the same two
 * tracks. A segmented control keeps its own width in a form, its segments as wide as the
 * widest: stretched over a row, two or three words would float in a long bar.
 *
 * <dl class="app-columns app-description-list" data-labels="top | side">
 *   <div class="app-column app-description-item" data-span="6">
 *     <dt class="app-description-term">Kunde</dt>
 *     <dd class="app-description-value">Muster GmbH</dd>
 *   </div>
 * </dl>
 *
 * The description list shows what a form edits, on the same grid: same spans, same label
 * track, same thresholds. Switching a page between viewing and editing keeps every value on
 * its line. A value that is not there is written as a dash by the screen; CSS writes nothing.
 */
@layer components {
    .app-form {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-section);
        min-inline-size: 0;
        max-inline-size: var(--app-form-width);
    }

    .app-form-section {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-field);
        min-inline-size: 0;
    }

    /* The dividing line is an inset shadow: it takes no room, so the section stays on the grid. */
    .app-form-section + .app-form-section {
        padding-block: var(--app-space-section) 0;
        box-shadow: inset 0 var(--app-border-width) 0 var(--app-color-line);
    }

    .app-form-section-header {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-section-header);
    }

    /* With actions the heading is two columns: title and description, and at the end what
       applies to the whole section (Eigene Angaben), level with the title. */
    .app-form-section-header:has(> .app-form-section-actions) {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        column-gap: var(--app-space-inline);
        align-items: start;
    }

    .app-form-section-header > .app-form-section-actions {
        display: flex;
        grid-area: 1 / 2 / span 2;
        gap: var(--app-space-actions);
    }

    .app-form-section-title {
        color: var(--app-color-text);
        font-family: var(--app-font-display);
        font-size: var(--app-text-heading-size);
        font-weight: var(--app-text-heading-weight);
        line-height: var(--app-text-heading-leading);
    }

    .app-form-section-description {
        color: var(--app-color-text-muted);
        font-size: var(--app-text-body-size);
        line-height: var(--app-text-body-leading);
    }

    .app-form-section-description:empty {
        display: none;
    }

    .app-form .app-column > .app-segmented:not([data-fill]) {
        justify-self: start;
    }

    .app-description-item {
        display: flex;
        flex-direction: column;
        gap: var(--app-space-term);
    }

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

    .app-description-value {
        min-inline-size: 0;
        color: var(--app-color-text);
        font-size: var(--app-text-body-size);
        font-weight: var(--app-text-value-weight);
        line-height: var(--app-text-body-leading);
        overflow-wrap: anywhere;
        hyphens: auto;
        text-wrap: pretty;
    }

    @container app-region (width >= 560px) {
        .app-description-list[data-labels="side"] .app-description-item {
            display: grid;
            grid-template-columns: var(--app-form-label-width) minmax(0, 1fr);
            align-items: baseline;
            gap: var(--app-space-form-label);
        }

        .app-form[data-labels="side"] :is(.app-field, .app-choice-group),
        .app-form[data-labels="side"] .app-column:has(> .app-choice) {
            display: grid;
            grid-template-columns: var(--app-form-label-width) minmax(0, 1fr);
            align-items: start;
            gap: var(--app-space-label) var(--app-space-form-label);
        }

        .app-form[data-labels="side"] :is(.app-field, .app-choice-group) > *,
        .app-form[data-labels="side"] .app-column > .app-choice {
            grid-column: 2;
        }

        /* The label sits on the middle of the control's first line. */
        .app-form[data-labels="side"] .app-field > .app-field-label {
            display: flex;
            grid-row: 1;
            grid-column: 1;
            align-items: center;
            min-block-size: var(--app-control-height);
        }

        .app-form[data-labels="side"] .app-segmented:not([data-label="hidden"]) {
            display: grid;
            grid-template-columns: var(--app-form-label-width) auto;
            align-items: center;
            gap: var(--app-space-label) var(--app-space-form-label);
        }

        .app-form[data-labels="side"] .app-choice-group > .app-choice-group-label {
            display: flex;
            grid-row: 1;
            grid-column: 1;
            align-items: center;
            min-block-size: var(--app-choice-size);
        }
    }

    @container app-region (width >= 560px) {
        :is(.app-form, .app-description-list)[data-labels="side"] .app-column {
            grid-column: 1 / -1;
        }
    }
}
