/* A form field whose label rests inside the control like a placeholder and moves onto its top
   border, which it cuts: the upper half of the label's background matches what is behind the
   field (--field-surface, white unless a container sets it), the lower half the control. */
.ui-field {
  --field-label-fill: #fff;
  position: relative;
  display: grid;
  gap: 6px;
}

.ui-field .ui-input input,
.ui-field .ui-select,
.ui-field .ui-search-select__summary {
  height: 48px;
  border-color: var(--color-border-control);
  border-radius: var(--radius-default);
  background-color: #fff;
  padding: 12px 16px;
  font-size: 16px;
  line-height: 24px;
  transition: background-color 160ms ease, border-color 160ms ease,
    box-shadow 160ms ease;
}

.ui-field .ui-input input::placeholder {
  color: var(--color-neutral-500);
  opacity: 1;
}

.ui-field .ui-input--with-trailing input {
  padding-right: 52px;
}

.ui-field .ui-input__trailing {
  right: 4px;
}

/* A native select draws the chevron of a search select instead of its own arrow. */
.ui-field .ui-select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%234a5565'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M5.22 8.22a.75.75 0 0 1 1.06 0L10 11.94l3.72-3.72a.75.75 0 1 1 1.06 1.06l-4.25 4.25a.75.75 0 0 1-1.06 0L5.22 9.28a.75.75 0 0 1 0-1.06Z'/%3E%3C/svg%3E");
  background-position: right 16px center;
  background-repeat: no-repeat;
  background-size: 20px;
  padding-right: 44px;
}

.ui-field .ui-input input:hover,
.ui-field .ui-select:hover,
.ui-field .ui-search-select__summary:hover {
  border-color: var(--color-neutral-400);
}

/* Focus darkens the border; there is no ring around the field. */
.ui-field .ui-input input:focus,
.ui-field .ui-select:focus,
.ui-field .ui-search-select__summary:focus-visible,
.ui-field .ui-search-select details[open] .ui-search-select__summary {
  border-color: var(--color-neutral-600);
  outline: none;
  background-color: #fff;
  box-shadow: none;
}

/* A control is invalid by its class from the server or by aria-invalid from a script;
   :where() gives the attribute the class's weight, so focus still turns the field white.
   The pointer over it leaves it red; focus has its own rule below. */
.ui-field .ui-input--invalid input,
.ui-field .ui-input--invalid input:hover:not(:focus),
.ui-field .ui-input input:where([aria-invalid="true"]),
.ui-field .ui-input input:where([aria-invalid="true"]):hover:not(:focus),
.ui-field .ui-search-select__summary--invalid,
.ui-field .ui-search-select__summary--invalid:hover:not(:focus-visible) {
  border-color: var(--color-red-300);
  background-color: var(--color-red-50);
}

.ui-field .ui-input--invalid input:focus,
.ui-field .ui-input input:where([aria-invalid="true"]):focus,
.ui-field .ui-search-select__summary--invalid:focus-visible,
.ui-field .ui-search-select details[open] .ui-search-select__summary--invalid {
  border-color: var(--color-red-500);
}

/* Autofill paints its own background; the inset shadow covers it with the control's white. */
.ui-field .ui-input input:-webkit-autofill,
.ui-field .ui-input input:-webkit-autofill:hover,
.ui-field .ui-input input:-webkit-autofill:focus {
  -webkit-box-shadow: 0 0 0 1000px #fff inset;
  -webkit-text-fill-color: var(--color-neutral-900);
}

.ui-field .ui-search-select__value--placeholder {
  visibility: hidden;
}

/* A select holds hidden and search inputs, so the rules for typed values name the text input. */
.ui-field:has(.ui-input--invalid input:not(:focus)),
.ui-field:has(.ui-input input:where([aria-invalid="true"]):not(:focus)),
.ui-field:has(.ui-search-select__summary--invalid) {
  --field-label-fill: var(--color-red-50);
}

.ui-field:has(details[open]),
.ui-field:has(.ui-search-select__summary:focus-visible) {
  --field-label-fill: #fff;
}

.ui-field .ui-label {
  position: absolute;
  top: 24px;
  left: 12px;
  z-index: 2;
  max-width: calc(100% - 24px);
  margin: 0;
  padding: 0 4px;
  overflow: hidden;
  color: var(--color-neutral-500);
  font-size: 16px;
  font-weight: 400;
  line-height: 24px;
  white-space: nowrap;
  text-overflow: ellipsis;
  pointer-events: none;
  transform: translateY(-50%);
  transform-origin: left center;
  transition: top 160ms ease, transform 160ms ease, color 160ms ease;
}

/* A native select always shows a value, so its label stays on the border. */
.ui-field:has(.ui-input input:focus) .ui-label,
.ui-field:has(.ui-input input:not(:placeholder-shown)) .ui-label,
.ui-field:has(.ui-select) .ui-label,
.ui-field:has(details[open]) .ui-label,
.ui-field:has(.ui-search-select__summary:focus-visible) .ui-label,
.ui-field:has(.ui-search-select__value:not(.ui-search-select__value--placeholder)) .ui-label {
  top: 0;
  background: linear-gradient(var(--field-surface, #fff) 50%, var(--field-label-fill) 50%);
  transform: translateY(-50%) scale(0.75);
}

/* Autofill may paint a value before :placeholder-shown changes; separate rules keep an
   unsupported pseudo-class from dropping the others. */
.ui-field:has(.ui-input input:autofill) .ui-label {
  top: 0;
  background: linear-gradient(var(--field-surface, #fff) 50%, var(--field-label-fill) 50%);
  transform: translateY(-50%) scale(0.75);
}

.ui-field:has(.ui-input input:-webkit-autofill) .ui-label {
  top: 0;
  background: linear-gradient(var(--field-surface, #fff) 50%, var(--field-label-fill) 50%);
  transform: translateY(-50%) scale(0.75);
}

.ui-field:has(.ui-input input:focus) .ui-label,
.ui-field:has(.ui-select:focus) .ui-label,
.ui-field:has(details[open]) .ui-label,
.ui-field:has(.ui-search-select__summary:focus-visible) .ui-label {
  color: var(--color-neutral-600);
}

/* An invalid field's label stays red while the field is focused or open, as weighty as the
   rules above that grey a focused label. */
.ui-field:has(.ui-input--invalid) .ui-label,
.ui-field:has(.ui-input--invalid input:focus) .ui-label,
.ui-field:has(.ui-input :where(input[aria-invalid="true"])) .ui-label,
.ui-field:has(.ui-input input:where([aria-invalid="true"]):focus) .ui-label,
.ui-field:has(.ui-search-select__summary--invalid) .ui-label,
.ui-field:has(.ui-search-select__summary--invalid:focus-visible) .ui-label,
.ui-field:has(details[open] .ui-search-select__summary--invalid) .ui-label {
  color: var(--color-red-500);
}

/* The field's own gap already sets its error off the control. */
.ui-field .ui-field-error {
  margin-top: 0;
}

@media (prefers-reduced-motion: reduce) {
  .ui-field .ui-label {
    transition: none;
  }
}

.ui-field .ui-search-select__error {
  margin-top: 6px;
}
