/* Form field with a floating label (CSS only, same technique as Bootstrap).

   <div class="field">
     <input class="field__input" id="email" name="email" type="email" placeholder=" "
            aria-describedby="email-error">
     <label class="field__label" for="email">Email address</label>
     <p class="field__error" id="email-error"></p>
   </div>

   - The input comes before its label, and needs placeholder=" " so CSS can tell when it is empty.
   - Error state: set aria-invalid="true" on the input and put the message in .field__error
     (JS while typing, PHP after submit). An empty .field__error takes no space.
   - Works the same for <select> (label always floated) and <textarea>. */

.field {
  position: relative;
  display: grid;
  align-content: start; /* in a row beside a taller field (one showing its error) it stays its own height, not stretched */
  gap: var(--sp-1);
}

.field__input {
  width: 100%;
  min-height: var(--field-h); /* also holds selects to the same height */
  padding: var(--sp-6) var(--sp-4) var(--sp-2);
  font-size: var(--fs-body);                    /* 16px: phones do not zoom */
  line-height: var(--lh-body);
  color: var(--c-text);
  background-color: var(--c-page);
  border: var(--border-1) solid var(--c-line);
  border-radius: var(--radius-1);
  transition: border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
}

/* Fields show the ring on any focus, mouse included, as Bootstrap does. */
.field__input:focus {
  border-color: var(--c-focus-border);
  outline: var(--border-1) solid transparent;
  box-shadow: var(--focus-ring);
}

textarea.field__input {
  min-height: var(--sp-24);
  resize: vertical;
}

/* Our own chevron in place of the browser's, which sits almost on the right border (Chrome, Safari, Firefox):
   16px in from the edge, like the text on the left. */
select.field__input {
  padding-right: var(--sp-10);
  appearance: none;
  background-image: var(--icon-chevron-down);
  background-repeat: no-repeat;
  background-position: right var(--sp-4) center;
  background-size: var(--sp-3) var(--sp-3);
}

.field__label {
  position: absolute;
  top: 0;
  left: 0;
  padding: var(--sp-4);
  border: var(--border-1) solid transparent; /* same box as the input, so the text lines up */
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  color: var(--c-text-muted);
  pointer-events: none;
  transform-origin: 0 0;
  transition: transform var(--duration-fast) var(--ease);
}

/* Float the label up when the field has focus, has content, is autofilled, or is a select. */
.field__input:focus ~ .field__label,
.field__input:not(:placeholder-shown) ~ .field__label,
.field__input:-webkit-autofill ~ .field__label,
select.field__input ~ .field__label {
  /* Shrinking also shrinks the label's left padding, so nudge it right to stay in line with the typed text. */
  transform: translate(calc(var(--sp-4) * 0.2), calc(-1 * var(--sp-2))) scale(0.8);
}

.field__input[aria-invalid="true"] {
  border-color: var(--c-error);
}

/* A field in error keeps its red border and gets a red ring, as in Bootstrap. */
.field__input[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 var(--sp-1) var(--c-error-ring);
}

.field__error {
  font-size: var(--fs-small);
  color: var(--c-error);
}

.field__error:empty {
  display: none;
}

.field__input:disabled {
  color: var(--c-text-muted);
  background-color: var(--c-surface);
  cursor: not-allowed;
}

/* --- A code in boxes, one per digit (the code generator pages): site.js turns a .field__input with data-digits="6" into
   this. It stays one real field (so phones and password managers can fill in the code, pasting works and screen readers
   hear one field), laid over the boxes with its own text and caret hidden; the boxes show what is typed, and the box for
   the next digit has the focus ring. Without JavaScript it is an ordinary field. The label sits above, centred. --- */
.field--digits {
  justify-items: center;
  gap: var(--sp-2);
}

.field.field--digits .field__label {
  position: static;
  order: -1;
  padding: 0;
  border: 0;
  font-size: var(--fs-small);
  transform: none;
}

.digits {
  position: relative;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, var(--sp-12)); /* one column per box, 48px at most */
  gap: var(--sp-2);
}

.field.field--digits .field__input {
  position: absolute;
  inset: 0;
  min-height: 0;
  padding: 0;
  color: transparent;
  caret-color: transparent;
  background-color: transparent;
  border: 0;
  box-shadow: none;
}

.field--digits .field__input::selection {
  background-color: transparent;
}

.digits__box {
  display: grid;
  place-items: center;
  height: var(--field-h);
  font-size: var(--fs-h4);
  font-variant-numeric: tabular-nums;
  color: var(--c-text);
  background-color: var(--c-page);
  border: var(--border-1) solid var(--c-line);
  border-radius: var(--radius-1);
  pointer-events: none; /* clicks go to the real field under them */
  transition: border-color var(--duration-fast) var(--ease), box-shadow var(--duration-fast) var(--ease);
}

.digits__box--on {
  border-color: var(--c-focus-border);
  box-shadow: var(--focus-ring);
}

.digits:has([aria-invalid="true"]) .digits__box {
  border-color: var(--c-error);
}

.digits:has([aria-invalid="true"]) .digits__box--on {
  box-shadow: 0 0 0 var(--sp-1) var(--c-error-ring);
}

@media (min-width: 576px) {
  .digits {
    gap: var(--sp-3);
  }
}
