/*
 * A text field with a floating label (`shared/_text_field`), and the error
 * line under it (`shared/_error_message`).
 *
 * The label is the input's following sibling and sits inside the field until
 * there is something to make room for: focus, or a value. The input carries an
 * empty placeholder so `:placeholder-shown` can tell the two apart.
 *
 * A square box with an ink outline on the field ground, the one ground lighter
 * than paper (tasks/redesign, D2). 16px text so iOS does not zoom on focus;
 * the focus ring from base.css plus a green border; a brick border and
 * label, and aria-invalid, when the form object reports an error. A select
 * in the same box centres its text and draws its own chevron.
 *
 * `.field--compact` is a caption over a box at a row's height: the screener's
 * rows, the valuation model, the alert drawer, the price chart's settings.
 *
 * `.field--trap` is the honeypot the e-mail forms carry (`shared/_bot_trap`):
 * moved off the page rather than `.sr-only`, because a screen reader reads out
 * what `.sr-only` hides, and a person must never be asked to fill this in.
 */
@layer components {
  .field {
    position: relative;
  }

  /* `:where()`, so a field that sizes its box to its own row (a compact
     field, a page's state border) does so whatever the files' order. */
  :where(.field__input) {
    display: block;
    inline-size: 100%;
    min-block-size: 3.25rem;
    padding: 1.375rem var(--space-sm) 0.375rem;
    border: var(--rule-width) solid var(--color-ink);
    border-radius: var(--radius-control);
    background-color: var(--color-field);
    color: var(--color-ink);
    font-size: var(--text-input);
    line-height: var(--leading-input);
    appearance: none;
    transition: border-color var(--duration-fast);

    &:focus {
      border-color: var(--color-accent);
    }

    &[aria-invalid="true"] {
      border-color: var(--color-loss);
    }

    &:is(textarea) {
      min-block-size: 8rem;
    }

    /* A select has no floating label to make room for, so its text sits in
       the middle of the box; `appearance: none` took the native arrow, so a
       chevron is drawn in its place from two gradients, in the ink. */
    &:is(select) {
      padding-block: 0;
      padding-inline-end: calc(var(--space-sm) + 1.25rem);
      background-image:
        linear-gradient(45deg, transparent 50%, var(--color-ink) 50%),
        linear-gradient(135deg, var(--color-ink) 50%, transparent 50%);
      background-position:
        right calc(var(--space-sm) + 0.3125rem) center,
        right var(--space-sm) center;
      background-size: 0.3125rem 0.3125rem;
      background-repeat: no-repeat;
      cursor: pointer;
    }
  }

  .field__label {
    position: absolute;
    inset-block-start: 0.4375rem;
    inset-inline-start: var(--space-sm);
    color: var(--color-ink-muted);
    font-size: var(--text-meta-xs);
    line-height: var(--leading-meta-xs);
    pointer-events: none;
    transition: all var(--duration-fast);

    .field__input:placeholder-shown:not(:focus) + & {
      inset-block-start: 0.9375rem;
      font-size: var(--text-input);
      line-height: var(--leading-input);
    }

    .field__input:focus + & {
      color: var(--color-accent-strong);
    }

    .field__input[aria-invalid="true"] + & {
      color: var(--color-loss);
    }
  }

  /* A compact field: a caption above a box at a row's height, for a small
     setting beside other content, where a floating label has no room. The
     figure is in the data face; the box keeps the control boundary until it
     is hovered or focused.

       <label class="field field--compact">
         <span class="field__caption">days</span>
         <input class="field__input" type="number" value="14">
       </label>

     The screener's rows, the valuation model and the alert drawer use it
     too; a page adds only its own state (the screener's filtering border,
     a Custom choice that opens a further field), which `:where()` lets win
     whatever the files' order. */
  :where(.field--compact) {
    display: grid;
    align-content: start;
    gap: var(--space-3xs);
    min-inline-size: 0;

    & > .field__input {
      min-block-size: var(--control-height-sm);
      padding-block: 0;
      padding-inline: var(--space-xs);
      border-color: var(--color-rule-control);
      font-family: var(--font-data);
      font-variant-numeric: tabular-nums;

      &:hover {
        border-color: var(--color-ink);
      }

      &:focus {
        border-color: var(--color-accent);
      }

      /* The chevron keeps its room at the narrower inset. */
      &:is(select) {
        padding-inline-end: calc(var(--space-xs) + 1.25rem);
        background-position:
          right calc(var(--space-xs) + 0.3125rem) center,
          right var(--space-xs) center;
      }

      @media (pointer: coarse) {
        min-block-size: var(--target-min);
      }
    }
  }

  .field__caption {
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
  }

  /* Rendered even when empty, at a fixed height, so the field below it does
     not jump when an error appears. */
  .field-error {
    min-block-size: 1.25rem;
    margin-block: var(--space-2xs);
    padding-inline: var(--space-sm);
    color: var(--color-loss);
    font-size: var(--text-meta);
    line-height: var(--leading-meta);
  }

  .field--trap {
    position: absolute;
    inset-inline-start: -100vw;
    inline-size: 1px;
    block-size: 1px;
    overflow: hidden;
  }
}
