/* 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 {

    /* Outer column — same edge-to-edge treatment as .asset-detail */
    .form-sheet {
        margin-inline: auto;
        max-inline-size: var(--main-width);
        padding-block: var(--block-space);
        padding-inline: var(--main-padding);
        text-align: start;
    }

    /* 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;
    }

    .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;
        }
    }

    /* 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;
        border-block-end: 1px solid var(--rule-soft);
        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 — replaces the old .fill-shade--error chunk. 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;
    }

    /* 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. */
    .form-danger {
        border-block-start: 1px dashed var(--rule);
        margin-block-start: 2.5rem;
        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;
        }
    }
}
