/* Control Room — form sheets (create / edit screens)
/* --------------------------------------------------------------------------
/* The form IS the document. Labels read as datasheet headers (mono, uppercase,
/* tight letterspacing); fields read as cells with a bottom rule (like filling
/* in a printed form). One borderless display-type textarea at the top acts as
/* the primary entry — the "handwriting" on the printed sheet.
/* --------------------------------------------------------------------------
/* Templates opt in via <div class="form-sheet"> … .form-hero … .form-section
/* … .form-row … .form-actions. Reuses .asset-section, .section-head,
/* .section-marker, and .btn--subtle* atoms from atoms.css / asset_detail.css.
/* --------------------------------------------------------------------------- */

@layer components {

    /* .form-sheet — container layout via page-wrapper group in atoms.css */

    /* Narrow variant — login / public auth forms and any single-purpose
       sheet that wants the same datasheet treatment but reads as a
       standalone document, not an application page. ~46ch keeps fields
       comfortably one-handed on mobile and prevents over-stretched inputs
       on wide screens. */
    .form-sheet--narrow {
        max-inline-size: 46ch;
    }

    /* Stacked variant — profile / settings pages where two or three
       form-sheets sit on the same page. Each sheet has its own hero and
       sections; the page-level vertical rhythm is owned by .form-sheets
       not by .form-sheet's bottom margin. */
    .form-sheets {
        display: grid;
        gap: 2.5rem;
    }

    /* Flush variant — drops the outer padding so the form sits inside a
       container that already owns its own padding (e.g. inside a
       <dialog class="popup"> or a turbo-frame partial). Use sparingly:
       only when the host element supplies the inline padding. */
    .form-sheet--flush {
        padding-block: 0;
        padding-inline: 0;
    }

    /* Top block — callsign + chip row, title textarea, meta, right-rail
       actions. Same border + stripe treatment as .asset-hero so create /
       edit sits inside the same visual family as detail. Status-driven
       via data-state (warning / critical) when editing a tinted asset. */
    .form-hero {
        background: var(--surface);
        border-block-end: 1px solid var(--card-color, var(--rule));
        border-block-start: 1px solid var(--card-color, var(--rule));
        box-shadow: inset 3px 0 0 var(--card-color, var(--rule));
        display: grid;
        gap: 1.25rem;
        grid-template-columns: 1fr auto;
        margin-block-end: 1.75rem;
        padding: 1.25rem;
        position: relative;
    }

    .form-hero[data-state="warning"]  { background: var(--surface-warn); }
    .form-hero[data-state="critical"] { background: var(--surface-crit); }

    .form-hero__main {
        display: grid;
        gap: 0.4rem;
        min-inline-size: 0;
    }

    .form-hero__ids {
        align-items: baseline;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .form-hero__chip {
        color: var(--ink-soft);
        font-family: var(--font-data);
        font-size: 0.625rem;
        letter-spacing: 0.12em;
        text-transform: uppercase;
    }

    /* Primary title entry — borderless textarea typeset as the hero display.
       Autoresize controller grows it with content; Enter submits. */
    .form-hero__title {
        margin: 0.2rem 0 0;
    }

    .form-hero__title .autoresize__wrapper {
        display: block;
    }

    .form-hero__title textarea {
        background: transparent;
        border: 0;
        color: var(--ink);
        font-family: var(--font-display);
        font-size: clamp(1.5rem, 3vw, 2.25rem);
        font-weight: 700;
        inline-size: 100%;
        letter-spacing: -0.02em;
        line-height: 1.05;
        margin: 0;
        min-block-size: 1.2em;
        outline: 0;
        padding: 0;
        resize: none;
    }

    .form-hero__title textarea::placeholder {
        color: var(--ink-mute);
        opacity: 1;
    }

    /* Optional secondary input below the title — same borderless treatment
       in a smaller body register. Used by the route editor for the
       description so name + description sit inside the hero block. */
    .form-hero__description {
        background: transparent;
        border: 0;
        color: var(--ink-soft);
        font-family: var(--font-body);
        font-size: var(--text-small);
        inline-size: 100%;
        line-height: 1.45;
        margin: 0;
        outline: 0;
        padding: 0;
        resize: vertical;
    }

    .form-hero__description::placeholder {
        color: var(--ink-mute);
        opacity: 1;
    }

    .form-hero__meta {
        color: var(--ink-faint);
        display: flex;
        flex-wrap: wrap;
        font-family: var(--font-data);
        font-size: 0.625rem;
        gap: 0 1ch;
        letter-spacing: 0.1em;
        margin-block-start: 0.25rem;
        text-transform: uppercase;
    }

    .form-hero__side {
        align-items: flex-end;
        display: flex;
        flex-direction: column;
        gap: 0.625rem;
        min-inline-size: 10rem;
        text-align: end;
    }

    .form-hero__actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.4rem;
        justify-content: flex-end;
    }

    /* Mobile: collapse the side column below the main column */
    @media (max-width: 639px) {
        .form-hero {
            grid-template-columns: 1fr;
        }
        .form-hero__side {
            align-items: stretch;
            text-align: start;
        }
        .form-hero__actions {
            justify-content: flex-start;
        }
    }

    /* Centered variant — login / standalone hero with no right-rail
       actions. The hero behaves as a brand block: logomark, eyebrow,
       title, optional subtitle. Used by login.html and any other public
       form. The standard .form-hero chrome (1px top+bottom + 3px stripe)
       is too heavy for a public marketing surface — flatten to a single
       hairline rule below the hero so the brand block reads as a header,
       not as a card. */
    .form-hero--centered {
        background: transparent;
        border: 0;
        box-shadow: none;
        gap: 0.4rem;
        grid-template-columns: 1fr;
        justify-items: center;
        margin-block-end: 1.25rem;
        padding-block: 1.5rem 1.25rem;
        padding-inline: 0;
        text-align: center;
    }

    .form-hero--centered .form-hero__main {
        justify-items: center;
        text-align: center;
    }

    .form-hero--centered .form-hero__brand {
        align-items: center;
        color: var(--ink);
        display: inline-flex;
        gap: 0.5ch;
        margin-block-end: 0.5rem;
    }

    .form-hero--centered .form-hero__brand svg {
        block-size: 2rem;
        inline-size: auto;
    }

    /* Datasheet eyebrow above the H1. Per DESIGN.md "Login screen
       specifics", the public hero is in marketing mode (no callsign);
       the eyebrow carries the report-spine identity instead. */
    .form-hero__eyebrow {
        color: var(--accent);
        font-family: var(--font-data);
        font-size: 0.6875rem;
        font-weight: 700;
        letter-spacing: 0.18em;
        margin-block-end: 0.25rem;
        text-transform: uppercase;
    }

    .form-hero--centered .form-hero__title {
        font-family: var(--font-display);
        font-size: clamp(1.5rem, 3vw, 2.25rem);
        font-weight: 700;
        letter-spacing: -0.02em;
        line-height: 1.05;
        margin: 0;
    }

    .form-hero--centered .form-hero__subtitle {
        color: var(--ink-soft);
        font-family: var(--font-body);
        font-size: 0.9375rem;
        margin-block-start: 0.5rem;
    }

    /* Wrapper for a group of rows — pairs with .section-head above.
       Reuses .asset-section spacing; kept as a form-scoped alias for
       clarity in templates. */
    .form-section {
        margin-block-end: 1.75rem;
    }

    /* Field row — mono uppercase label on the left, control on the right,
       with a hairline rule underneath. Label column fixed at 16ch on wide;
       collapses to stacked on mobile. */
    .form-row {
        align-items: baseline;
        display: grid;
        gap: 0.4rem 1rem;
        grid-template-columns: 1fr;
        padding-block: 0.85rem;
    }

    .form-row:last-child {
        border-block-end: 0;
    }

    @media (min-width: 60ch) {
        .form-row {
            grid-template-columns: 16ch 1fr;
        }
    }

    /* Row variant — label column sized wider (for long labels like
       "Machine body" / "ISO 10816 class"). */
    .form-row--wide {
        @media (min-width: 60ch) {
            grid-template-columns: 22ch 1fr;
        }
    }

    /* Row variant — "block" fields (body-layout-editor, file upload,
       rich text etc.) want a single column regardless of viewport. */
    .form-row--block {
        grid-template-columns: 1fr !important;
    }

    .form-label {
        color: var(--ink-soft);
        font-family: var(--font-data);
        font-size: 0.625rem;
        font-weight: 700;
        letter-spacing: 0.14em;
        text-transform: uppercase;
    }

    .form-label--required::after {
        color: var(--accent);
        content: " *";
    }

    .form-field {
        min-inline-size: 0;
    }

    /* Input treatment — flatten the global .input pill inside a form sheet.
       Cell-like, bottom-rule only, no radius, tight padding. Focus darkens
       the bottom rule (no outline ring — the rule change is the tell). */
    .form-sheet .input,
    .form-field .input {
        background: transparent;
        border: 0;
        border-block-end: 1px solid var(--rule);
        border-radius: 0;
        color: var(--ink);
        font-family: var(--font-body);
        font-size: 0.9375rem;
        inline-size: 100%;
        line-height: 1.4;
        padding: 0.3rem 0.1rem;
        transition: border-color 120ms ease-out;
    }

    .form-sheet .input:focus,
    .form-field .input:focus,
    .form-sheet .input:focus-visible,
    .form-field .input:focus-visible {
        border-block-end-color: var(--ink);
        box-shadow: inset 0 -1px 0 0 var(--ink);
        outline: 0;
    }

    /* Select keeps its chevron but picks up the cell chrome.
       Long option labels (ISO 10816 class, equipment type) would otherwise
       spill past the chevron on narrow viewports — clip cleanly with an
       ellipsis and widen the chevron rail so text never kisses the arrow. */
    .form-sheet select.input,
    .form-field select.input {
        background-position: center right 0.5em;
        block-size: auto;
        overflow: hidden;
        padding-inline-end: 2em;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Textarea grows naturally inside a form-sheet — cap the min height */
    .form-sheet .input--textarea,
    .form-field .input--textarea {
        min-block-size: 2.4em;
        padding-block: 0.3rem;
    }

    /* iOS / touch: bump input font-size to 16px on focus so Safari does
       not zoom the viewport. The global .input has the same guard; my
       compact override above dropped it, so it needs restoring. */
    @media (hover: none) {
        .form-sheet .input,
        .form-field .input {
            font-size: max(16px, 0.9375rem);
        }
    }

    /* Help caption — subtle mono line beneath a field */
    .form-help {
        color: var(--ink-faint);
        display: block;
        font-family: var(--font-data);
        font-size: 0.625rem;
        letter-spacing: 0.08em;
        margin-block-start: 0.45rem;
        text-transform: uppercase;
    }

    /* Inline error — mono, red,
       no background box. Sits directly under the field it belongs to. */
    .form-field__error {
        color: var(--status-crit-fg);
        display: block;
        font-family: var(--font-data);
        font-size: 0.6875rem;
        letter-spacing: 0.06em;
        margin-block-start: 0.4rem;
    }

    .form-field__error p {
        margin: 0;
    }

    /* Action bar — footer for long forms. 2px rule-strong above, same as
       .section-head bottom rule. Primary action left, cancel beside it,
       optional danger pushed right via .form-actions__spacer. */
    .form-actions {
        align-items: center;
        border-block-start: 2px solid var(--rule-strong);
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-block-start: 2rem;
        padding-block-start: 1rem;
    }

    .form-actions__spacer {
        margin-inline-start: auto;
    }

    /* Block variant — full-width primary CTA. Used on narrow public
       sheets (login, password reset) where a single decisive action
       reads better as a full-row button than as a left-justified chip. */
    .form-actions--block {
        border-block-start: 0;
        flex-direction: column;
        margin-block-start: 1.25rem;
        padding-block-start: 0;
    }

    .form-actions--block > .btn--subtle {
        inline-size: 100%;
        justify-content: center;
        padding-block: 0.55rem;
    }

    /* Danger section — edit-only. Dashed rule above it signals the zone
       shift; delete action rendered via the existing _delete_dialog partial
       so no new button plumbing is needed. iOS-notched safe-area inset
       guarantees the destructive action doesn't sit under the home bar
       on narrow screens. */
    .form-danger {
        border-block-start: 1px dashed var(--rule);
        margin-block-start: 2.5rem;
        padding-block-end: max(0.5rem, env(safe-area-inset-bottom));
        padding-block-start: 1rem;
    }

    .form-danger__heading {
        color: var(--status-crit-fg);
        font-family: var(--font-data);
        font-size: 0.6875rem;
        font-weight: 700;
        letter-spacing: 0.16em;
        margin: 0 0 0.5rem;
        text-transform: uppercase;
    }

    .form-danger__body {
        color: var(--ink-soft);
        font-size: 0.8125rem;
        margin-block-end: 0.75rem;
    }

    /* Photo block — compact preview + replace/remove on asset_edit.
       Replaces the old .asset-edit__photo-* hierarchy (still kept in
       asset_detail.css until the other call sites migrate). */
    .form-photo {
        align-items: center;
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .form-photo__preview {
        background: var(--surface-sunken);
        border: 1px solid var(--rule);
        display: block;
        flex: 0 0 auto;
        inline-size: 5.5rem;
        overflow: hidden;
    }

    .form-photo__preview img {
        aspect-ratio: 1;
        display: block;
        inline-size: 100%;
        object-fit: cover;
    }

    .form-photo__actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.4rem;
    }

    /* Kbd placement inside submit/cancel buttons — half-tone, nudged baseline */
    .form-actions .btn--subtle kbd,
    .form-hero__actions .btn--subtle kbd {
        align-self: center;
        margin-inline-start: 0.35ch;
    }

    /* Body-layout editor — the DE / NDE / custom-input / add-button row is
       defined in asset_chart.css as a non-wrapping flex strip. Inside a
       form-sheet we let it wrap so narrow viewports don't punt controls
       off-canvas. forms.css loads after asset_chart.css alphabetically so
       this override wins without !important. */
    .form-sheet .body-layout-editor__actions {
        flex-wrap: wrap;
        row-gap: 0.5rem;
    }

    /* Narrow-viewport polish — reclaim horizontal room on phones. */
    @media (max-width: 479px) {
        .form-hero {
            padding: 1rem 0.9rem;
        }
        .form-hero__side {
            align-items: flex-start;
        }
        .form-hero__actions .btn--subtle {
            /* Stretch buttons across the row so the primary tap target
               reads like a native mobile CTA instead of a narrow chip. */
            flex: 1 1 auto;
            justify-content: center;
        }
    }

    /* Radio group — segmented mono pills used by the profile unit
       picker and any short choice list. Built on .btn--subtle so the
       active state reuses the same hover invert as the rest of the
       button family. The actual <input type="radio"> stays
       screen-reader-only inside the <label>. */
    .form-radio-group {
        display: flex;
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    .form-radio-group__option {
        --btn-padding: 0.18rem 0.6rem;
        cursor: pointer;
        font-family: var(--font-data);
        font-size: 0.75rem;
        letter-spacing: 0.04em;
    }

    .form-radio-group__option:has(input:checked),
    .form-radio-group__option--active {
        --btn-background: var(--ink);
        --btn-border-color: var(--ink);
        --btn-color: var(--color-ink-inverted, #fff);
        /* Inset rule keeps the checked state legible against the
           hover-invert (which uses the same ink-on-inverted swap). The
           rule shows on hover too — that's intentional — but the
           checked option keeps it persistent. */
        box-shadow: inset 0 -2px 0 0 var(--accent);
    }

    .form-radio-group__option:focus-within {
        outline: 2px solid var(--accent);
        outline-offset: 1px;
    }

    /* Top-of-form banner — sits between the hero and the first
       .form-section, mono and inline-rule like the rest of the
       datasheet. */
    .form-banner {
        border-block-end: 1px solid var(--card-color, var(--rule));
        border-block-start: 1px solid var(--card-color, var(--rule));
        box-shadow: inset 3px 0 0 var(--card-color, var(--rule));
        color: var(--ink);
        display: block;
        font-family: var(--font-body);
        font-size: 0.875rem;
        margin-block-end: 1.5rem;
        padding: 0.65rem 0.9rem;
    }

    .form-banner--error {
        --card-color: var(--status-crit-fg);
        background: var(--surface-crit, color-mix(in oklch, var(--surface) 92%, var(--status-crit-fg)));
    }

    .form-banner__label {
        color: var(--status-crit-fg);
        font-family: var(--font-data);
        font-size: 0.625rem;
        font-weight: 700;
        letter-spacing: 0.14em;
        margin-inline-end: 0.5rem;
        text-transform: uppercase;
    }

    /* Selection list — public surfaces where the user picks a target
       (installation chooser, future "switch tenant" flows). Datasheet
       row treatment: hairline rule between rows, mono callsign on the
       left, body-face name in the middle, chevron on the right.
       Each row uses the .form-row hairline + a hover invert so the
       selectable affordance is unambiguous. */
    .select-list {
        border-block-end: 1px solid var(--rule);
        border-block-start: 1px solid var(--rule);
        list-style: none;
        margin: 0;
        padding: 0;
    }

    .select-list__item {
        border-block-end: 1px solid var(--rule-soft);
        margin: 0;
        padding: 0;
    }

    .select-list__item:last-child {
        border-block-end: 0;
    }

    .select-list__link {
        align-items: baseline;
        color: var(--ink);
        display: flex;
        font-family: var(--font-body);
        font-size: 0.9375rem;
        font-weight: 500;
        gap: 0.9rem;
        padding: 0.85rem 0.4rem;
        text-decoration: none;
        transition: background-color 100ms ease-out, color 100ms ease-out;
    }

    @media (any-hover: hover) {
        .select-list__link:hover {
            background-color: color-mix(in oklch, var(--surface) 88%, var(--ink));
            color: var(--ink);
        }
    }

    .select-list__link:focus-visible {
        background-color: color-mix(in oklch, var(--surface) 88%, var(--ink));
        outline: 2px solid var(--accent);
        outline-offset: -2px;
    }

    .select-list__name {
        flex: 1 1 auto;
        min-inline-size: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .select-list__arrow {
        block-size: 0.85em;
        color: var(--ink-faint);
        flex: 0 0 auto;
        inline-size: 0.85em;
    }

    .select-list__empty {
        color: var(--ink-soft);
        padding: 1.25rem 0.4rem;
        text-align: center;
    }
}
