/*
 * Field: label + control (input, select, textarea), optional hint and error message.
 * Error state is driven by aria-invalid="true" on the control; the message is linked with
 * aria-describedby and carries an icon plus an "Erro:" prefix, so color is never the only signal.
 * Variants: --checkbox (native checkbox inside its label, 44px row) and --search (sprite icon).
 */
@layer components {
  .field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    min-inline-size: 0;
    /* A form reads down one column; a control wider than this only adds eye travel. */
    max-inline-size: 40ch;
  }

  .field__label {
    color: var(--color-text);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
  }

  /* Required is spelled out in the label, not hinted by an asterisk or a color. */
  .field__required {
    color: var(--color-text-muted);
    font-weight: var(--weight-regular);
  }

  .field__hint {
    color: var(--color-text-muted);
    font-size: var(--text-sm);
  }

  .field__control {
    inline-size: 100%;
    min-block-size: calc(var(--space-8) + var(--space-3));
    padding: var(--space-2) var(--space-3);
    border: var(--border-width) solid var(--color-border-strong);
    border-radius: var(--radius-md);
    background: var(--color-surface);
    color: var(--color-text);
    font-size: var(--text-base);

    &::placeholder {
      color: var(--color-text-muted);
      opacity: 1;
    }

    &:hover:not(:disabled) {
      border-color: var(--color-text);
    }

    &:focus-visible {
      border-color: var(--color-action);
      outline: var(--focus-ring);
      outline-offset: var(--focus-offset);
    }

    &:disabled {
      border-color: var(--color-border);
      background: var(--color-surface-sunken);
      color: var(--color-text-muted);
    }

    &[aria-invalid="true"] {
      border-color: var(--color-danger);
      box-shadow: inset 0 0 0 var(--border-width) var(--color-danger);
    }
  }

  /* Multi-line control (textarea). */
  .field__control--multiline {
    min-block-size: calc(var(--space-16) + var(--space-8));
    line-height: var(--leading-relaxed);
    resize: vertical;
  }

  .field__error {
    display: flex;
    align-items: flex-start;
    gap: var(--space-1);
    color: var(--color-danger);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
  }

  .field__error-icon {
    flex: none;
    inline-size: var(--space-4);
    block-size: var(--space-4);
    margin-block-start: var(--space-1);
  }

  /* Checkbox: the label wraps the input, so the whole 44px row is the touch target. */
  .field--checkbox .field__option {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-block-size: calc(var(--space-8) + var(--space-3));
    color: var(--color-text);
    font-size: var(--text-base);
    font-weight: var(--weight-regular);
    cursor: pointer;

    &:has(:disabled) {
      color: var(--color-text-muted);
      cursor: not-allowed;
    }
  }

  .field__check {
    flex: none;
    inline-size: calc(var(--space-4) + var(--space-1));
    block-size: calc(var(--space-4) + var(--space-1));
    margin: 0;
    accent-color: var(--color-action);
    cursor: inherit;

    &[aria-invalid="true"] {
      outline: var(--border-width) solid var(--color-danger);
      outline-offset: var(--border-width);
    }

    /* After the error outline, so the focus ring wins on an invalid checkbox. */
    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: var(--focus-offset);
    }
  }

  /* Search: the label is visually hidden in the markup; the icon sits inside the control. */
  .field__wrap {
    position: relative;
  }

  .field--search .field__control {
    padding-inline-start: calc(var(--space-3) + var(--space-6));
  }

  .field__icon {
    position: absolute;
    inset-block-start: 50%;
    inset-inline-start: var(--space-3);
    inline-size: var(--space-4);
    block-size: var(--space-4);
    color: var(--color-text-muted);
    pointer-events: none;
    transform: translateY(-50%);
  }
}
