/* =====================================================================
   OSE · Field
   ---------------------------------------------------------------------
   The wrapper around one form control (.input, .select, .dropdown):
   label (+ required marker), optional help text, error message, and the
   vertical rhythm between them. <fieldset class="field field--group">
   groups checkboxes / radios (.check) under a legend.

   Markup
     <div class="field">
       <label class="field__label" for="phone">Số điện thoại<span
         class="field__required" aria-hidden="true">*</span></label>
       <input class="input" id="phone" name="phone" type="tel" required
              aria-describedby="phone-help phone-error" aria-invalid="true">
       <p class="field__help" id="phone-help">OSE sẽ gọi lại vào số này.</p>
       <p class="field__error" id="phone-error">Vui lòng nhập số điện thoại.</p>
     </div>

     <fieldset class="field field--group" aria-describedby="way-error">
       <legend class="field__label field__legend">Hình thức liên hệ</legend>
       <div class="field__options field__options--inline">
         <label class="check">…</label> …
       </div>
       <p class="field__error" id="way-error" hidden>…</p>
     </fieldset>

   States
     default
     required   the * marker is decorative (aria-hidden); the control
                carries required / aria-required.
     optional   <span class="field__optional">(không bắt buộc)</span>
     error      the control gets aria-invalid="true" and lists the
                .field__error id in aria-describedby; when valid, hide the
                message with [hidden] (or remove it) and drop aria-invalid.
     disabled   the control is :disabled or aria-disabled="true", or the
                <fieldset> is disabled: label and help text dim with it.
   Tokens   --field-* only.
   JS       none.
   ===================================================================== */

@layer components {
  .field {
    display: grid;
    gap: var(--field-gap);
    align-content: start;
    min-inline-size: 0;
  }

  /* <fieldset>: drop the UA border, padding and min-content width. */
  .field--group {
    margin: 0;
    padding: 0;
    border: 0;
  }

  .field__label {
    color: var(--field-label-color);
    font-family: var(--field-label-font-family);
    font-size: var(--field-label-font-size);
    font-weight: var(--field-label-font-weight);
    line-height: var(--field-label-leading);
  }

  /* A floated legend is not the "rendered legend", so it becomes an
     ordinary grid item and gets the same gap as a label. */
  .field__legend {
    float: left;
    inline-size: 100%;
    padding: 0;
  }

  .field__required {
    margin-inline-start: var(--field-required-gap);
    color: var(--field-required-color);
  }

  .field__optional {
    color: var(--field-help-color);
    font-family: var(--field-optional-font-family);
    font-weight: var(--field-optional-font-weight);
  }

  .field__help {
    color: var(--field-help-color);
    font-size: var(--field-help-font-size);
    line-height: var(--field-help-leading);
  }

  .field__error {
    display: flex;
    align-items: flex-start;
    gap: var(--field-error-gap);
    color: var(--field-error-color);
    font-size: var(--field-error-font-size);
    line-height: var(--field-error-leading);
  }

  /* Icon box is one line tall, so the icon centres on the first line. */
  .field__error::before {
    content: "";
    flex: none;
    inline-size: var(--field-error-icon-size);
    block-size: calc(var(--field-error-font-size) * var(--field-error-leading));
    background-color: currentColor;
    -webkit-mask: var(--field-error-icon) center / contain no-repeat;
    mask: var(--field-error-icon) center / contain no-repeat;
  }

  /* Options inside a group: stacked rows, or inline on one line. */
  .field__options {
    display: grid;
    gap: var(--field-options-gap);
  }

  .field__options--inline {
    display: flex;
    flex-wrap: wrap;
    column-gap: var(--field-options-gap-inline);
    row-gap: var(--field-options-gap);
  }

  /* Disabled: the label and help text dim with their control. */
  .field:has(.input:disabled, .input[aria-disabled="true"]) .field__label,
  .field--group:disabled .field__label {
    color: var(--field-label-color-disabled);
  }

  .field:has(.input:disabled, .input[aria-disabled="true"]) .field__help,
  .field--group:disabled .field__help {
    color: var(--field-help-color-disabled);
  }

  .field:has(.input:disabled, .input[aria-disabled="true"]) .field__required,
  .field--group:disabled .field__required,
  .field:has(.input:disabled, .input[aria-disabled="true"]) .field__optional,
  .field--group:disabled .field__optional {
    color: inherit;
  }

  /* Forced colours: masks paint with background-color, which the UA
     would reset to Canvas; paint the icon in the text colour instead. */
  @media (forced-colors: active) {
    .field__error::before {
      forced-color-adjust: none;
      background-color: CanvasText;
    }
  }
}
