/* ==========================================================================
   textArea.css

   Default generated DOM (EDIT, mandatory, validation enabled):

   <div class="ui-text-area-wrapper ui-text-area-width-full">
     <label class="ui-text-area-label" for="notes">Notes</label>
     <textarea id="notes" name="notes" class="ui-text-area-field ui-text-area-resize-vertical" placeholder="" rows="4"
               autocomplete="off" spellcheck="true" dir="auto" aria-invalid="false" aria-required="true"
               aria-describedby="notes-limit"></textarea>
     <div class="ui-text-area-footer">
       [<div id="notes-help" class="ui-text-area-help">…</div>]                      (help: left, on the counter's line)
       <div id="notes-error" class="ui-text-area-error" hidden></div>                 (error: own line below)
       <div id="notes-counter" class="ui-text-area-counter" aria-hidden="true">1000 characters left</div>
     </div>
     <span id="notes-limit" class="ui-text-area-visually-hidden">Maximum 1000 characters.</span>
     <span id="notes-announcer" class="ui-text-area-visually-hidden" aria-live="polite"></span>
   </div>

   Conditional structures:
   - mandatory indicator in the label: span.ui-text-area-optional (texts.optional) or span.ui-text-area-mandatory "*" (aria-hidden);
   - help: div#{id}-help.ui-text-area-help, first in the footer, on the same line as the counter (in aria-describedby);
   - no counter/limit/announcer without validation.enabled; no counter with showCounter:false;
   - counter warning (fewer than counterWarningThreshold characters left): .ui-text-area-counter-warning;
   - autoGrow: .ui-text-area-auto-grow; the component sets --ui-text-area-height and --ui-text-area-overflow inline;
   - VIEW: p.ui-text-area-label + p.ui-text-area-view-value (line breaks preserved); INVISIBLE: no markup; INACTIVE: textarea[disabled].

   The counter is visual (aria-hidden); screen readers get the limit through aria-describedby and one polite
   announcement when the warning zone is entered and when the limit is reached. Errors are not live regions.
   The 768px media query is a literal because custom properties cannot be used in media queries.

   Variables left: --ui-text-area-height and --ui-text-area-overflow, written by the component on the field while it
   grows with its text (autoResize).

   STYLE GROUPS. The rules are in three parts; a class can appear in more than one part, each time with
   different properties. Values are design tokens written directly in the rules (no component variables).
     1. APPEARANCE  colours, borders, corners, type, motion. Change freely: only the look changes.
     2. DIMENSIONS  sizes, spacing, widths. Change with care: the component keeps working, its size changes.
     3. STRUCTURE   layout, behaviour, states, accessibility (focus ring, hidden text). Do not change.
   Text uses the typography roles of design-system.css (--font-ui, --fw-ui, --fw-ui-strong, …), never --font-sans
   or --fw-* directly, so a change of a role reaches every component.
   To restyle, change a value in part 1 or 2 (client/local-styling marks app values [APP]), or write the same
   selector with the new value in a stylesheet that loads later, in @layer overrides.
   ui-catalog source and client/local-styling use this file with the app look; client/showcase-styling keeps
   the original values, which the lines marked [APP] name in brackets (library / original).
   Borders use physical sides (top, right, bottom, left): a value set for one side only does not mirror in
   right-to-left languages.
   ========================================================================== */

@layer components {

    /* ======================================================================
       1. APPEARANCE — change freely
       ====================================================================== */
    /* component font and normal weight for every text of the field; the typed text needs them explicitly
       (form fields do not inherit the page font). Label, "*", error and the counter at the limit are --fw-ui-strong. */
    .ui-text-area-wrapper, .ui-text-area-field { font-family: var(--font-ui); font-weight: var(--fw-ui); }
    .ui-text-area-label { color: var(--c-text); font-size: var(--fs-label); font-weight: var(--fw-ui-strong); line-height: var(--lh-compact); }
    .ui-text-area-optional { color: var(--c-muted); font-size: var(--fs-label); font-weight: var(--fw-ui); }
    .ui-text-area-mandatory { color: var(--c-danger-text); font-weight: var(--fw-ui-strong); }

    /* the field: border, corners, background and text, at rest */
    .ui-text-area-field {
        border-top: 0 solid var(--c-border);   /* [APP] as TextInput: bottom line only (library: var(--border-thin) on every side) */
        border-right: 0 solid var(--c-border);
        border-bottom: var(--border-thin) solid var(--c-border);
        border-left: 0 solid var(--c-border);
        border-top-left-radius: 0;   /* [APP] square corners (library: var(--radius-control)) */
        border-top-right-radius: 0;
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;
        background-color: var(--c-surface);
        color: var(--c-text);
        font-size: var(--fs-control);
        line-height: var(--lh-body);
        transition: border-color var(--transition-1);
    }

    /* the same field while typing, with an invalid value (keep it clearly "error") and disabled */
    .ui-text-area-field:focus { border-color: var(--c-accent); }
    .ui-text-area-field[aria-invalid="true"] { border-color: var(--c-danger); }
    .ui-text-area-field:disabled { background-color: var(--c-surface-muted); color: var(--c-muted); -webkit-text-fill-color: var(--c-muted); }

    /* DISABLED LABEL — the label and the "*" of a disabled field use the secondary text colour.
       Disabled = state INACTIVE, or inside a disabled fieldset (e.g. ContentGroup disabled:true); :has() looks for a
       disabled field inside the wrapper. "(Optional)" and the help text are already --c-muted; the error stays red.
       To change the colour, edit these two lines (a plain .ui-text-area-label rule elsewhere cannot override them). */
    .ui-text-area-wrapper:has(.ui-text-area-field:disabled) .ui-text-area-label { color: var(--c-muted); }
    .ui-text-area-wrapper:has(.ui-text-area-field:disabled) .ui-text-area-mandatory { color: var(--c-muted); }

    .ui-text-area-help { color: var(--c-muted); font-size: var(--fs-label); line-height: var(--lh-body); }
    .ui-text-area-error { color: var(--c-danger-text); font-size: var(--fs-label); font-weight: var(--fw-ui-strong); line-height: var(--lh-body); }
    .ui-text-area-counter { color: var(--c-muted); font-size: var(--fs-label); line-height: var(--lh-body); }
    .ui-text-area-counter-warning { color: var(--c-danger-text); font-weight: var(--fw-ui-strong); }
    .ui-text-area-view-value { color: var(--c-text); font-size: var(--fs-control); line-height: var(--lh-body); }

    /* ======================================================================
       2. DIMENSIONS — change with care
       ====================================================================== */
    .ui-text-area-wrapper { gap: var(--gap-tight); margin-block-end: var(--gap-section); }        /* label, field, help, footer */
    .ui-text-area-label { gap: var(--gap-inline-items); }                                       /* label to "(Optional)" */
    /* one line at least; rows / autoResize set the real height */
    .ui-text-area-field { min-block-size: var(--control-height); padding: var(--padding-control); }
    .ui-text-area-footer { gap: var(--gap-tight) var(--gap-inline-items); }                     /* help to counter; line to the error below */

    /* width option */
    .ui-text-area-width-small { inline-size: var(--percentage-s); }
    .ui-text-area-width-medium { inline-size: var(--percentage-m); }
    .ui-text-area-width-large { inline-size: var(--percentage-l); }
    .ui-text-area-width-full { inline-size: var(--percentage-full); }

    /* ======================================================================
       3. STRUCTURE — do not change
       ====================================================================== */
    .ui-text-area-wrapper { display:grid; min-inline-size:0; max-inline-size:var(--percentage-full); }
    .ui-text-area-label { display:flex; align-items:center; margin:0; }

    .ui-text-area-field { box-sizing:border-box; display:block; inline-size:100%; min-inline-size:0;
      block-size:var(--ui-text-area-height,auto); overflow-y:var(--ui-text-area-overflow,auto); overflow-wrap:anywhere; }
    .ui-text-area-field:focus { outline:0; }
    .ui-text-area-field:focus-visible { outline:var(--focus-outline); outline-offset:var(--focus-offset); }
    .ui-text-area-resize-none { resize:none; }
    .ui-text-area-resize-vertical { resize:vertical; }

    /* footer: help (left, takes the free room and wraps) and counter (right) on one line; the error on its own line below */
    .ui-text-area-footer { display:flex; flex-wrap:wrap; align-items:flex-start; justify-content:space-between; }
    .ui-text-area-footer:has(.ui-text-area-error[hidden]):not(:has(.ui-text-area-counter, .ui-text-area-help)) { display:none; }
    .ui-text-area-footer > .ui-text-area-help { flex:1 1 0; min-inline-size:0; }
    .ui-text-area-footer > .ui-text-area-error { order:1; flex-basis:100%; }
    .ui-text-area-counter { margin-inline-start:auto; white-space:nowrap; }

    .ui-text-area-view-value { margin:0; white-space:pre-wrap; overflow-wrap:anywhere; }
    .ui-text-area-visually-hidden { position:absolute; inline-size:1px; block-size:1px; margin:-1px; padding:0; overflow:hidden; clip-path:inset(50%); white-space:nowrap; border:0; }

    @media (max-width:768px) { .ui-text-area-wrapper { max-inline-size:100%; } }

    /* Label on the left: labelPosition START, or INHERIT inside an element with the class ui-labels-start.
       Applies only while the nearest container (e.g. Form, ContentGroup, CollapsiblePanel) is at least 36rem wide;
       narrower, the label returns on top. 36rem is a literal because custom properties cannot be used in container queries.
       Label column width: --form-label-width (design-system.css); gap between label and field: --gap-toolbar. */
    @container (min-width:36rem) {
        :is(.ui-labels-start .ui-text-area-wrapper:not(.ui-text-area-label-top), .ui-text-area-wrapper.ui-text-area-label-start) { grid-template-columns:var(--form-label-width) minmax(0,1fr); column-gap:var(--gap-toolbar); }
        /* label on the left with width SMALL / MEDIUM / LARGE: the row keeps the full width and the option sizes the field
           column (a share of the row, never more than the room beside the label) */
        :is(.ui-labels-start .ui-text-area-wrapper:not(.ui-text-area-label-top), .ui-text-area-wrapper.ui-text-area-label-start).ui-text-area-width-small { inline-size:var(--percentage-full); grid-template-columns:var(--form-label-width) minmax(0,min(var(--percentage-s), 100% - var(--form-label-width) - var(--gap-toolbar))); }
        :is(.ui-labels-start .ui-text-area-wrapper:not(.ui-text-area-label-top), .ui-text-area-wrapper.ui-text-area-label-start).ui-text-area-width-medium { inline-size:var(--percentage-full); grid-template-columns:var(--form-label-width) minmax(0,min(var(--percentage-m), 100% - var(--form-label-width) - var(--gap-toolbar))); }
        :is(.ui-labels-start .ui-text-area-wrapper:not(.ui-text-area-label-top), .ui-text-area-wrapper.ui-text-area-label-start).ui-text-area-width-large { inline-size:var(--percentage-full); grid-template-columns:var(--form-label-width) minmax(0,min(var(--percentage-l), 100% - var(--form-label-width) - var(--gap-toolbar))); }
        :is(.ui-labels-start .ui-text-area-wrapper:not(.ui-text-area-label-top), .ui-text-area-wrapper.ui-text-area-label-start) > * { grid-column:2; }
        :is(.ui-labels-start .ui-text-area-wrapper:not(.ui-text-area-label-top), .ui-text-area-wrapper.ui-text-area-label-start) > .ui-text-area-label { grid-column:1; float:inline-start; margin:0; align-self:start; margin-block-start:calc(( var(--control-height) - 1lh ) / 2); }
    }
}
