/* ==========================================================================
   Floating labels — the label sits inside the field like a placeholder and
   moves up out of the way when you type, so it never disappears.

   <div class="form-floating">
     <input class="form-control" id="email" type="email">
     <label for="email">Email</label>
   </div>

   Works with inputs, textareas and Moey dropdowns (.form-select), inside an
   .input-group, with .form-adorn icons, .form-filled and validation.
   The label goes AFTER the field. moey.js adds placeholder=" " when there is none
   (the CSS needs one to tell an empty field from a filled one); a real
   placeholder shows as a hint once the field has focus.
   ========================================================================== */

.form-floating {
  --mo-floating-height: 3.25rem;                     /* 52px: room for the label and the value */
  --mo-floating-scale: .8;
  position: relative;
  display: block;
  min-width: 0;
}

/* The field: taller, with the value sitting below the label */
.form-floating > :is(.form-control, .form-select) {
  min-height: var(--mo-floating-height);
  padding-top: 1.5rem;
  padding-bottom: .375rem;
  line-height: 1.25;
}
.form-floating > textarea.form-control { min-height: calc(var(--mo-floating-height) * 1.75); padding-top: 1.625rem; line-height: 1.5; }
.form-floating > .form-control::placeholder { color: transparent; }
.form-floating > .form-control:focus::placeholder { color: var(--mo-control-placeholder); }
@media (prefers-reduced-motion: no-preference) {
  .form-floating > .form-control::placeholder { transition: color var(--mo-duration); }
}

/* The label: resting in the middle, like a placeholder */
.form-floating > label {
  position: absolute;
  top: 0;
  inset-inline-start: calc(var(--mo-border-width) + var(--mo-control-padding-x));   /* where the value starts */
  z-index: 2;
  max-width: calc(100% - 2 * var(--mo-control-padding-x));
  height: var(--mo-floating-height);
  padding: 0;
  margin: 0;
  overflow: hidden;
  font-size: var(--mo-control-font-size);
  line-height: var(--mo-floating-height);
  color: var(--mo-control-placeholder);
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
  transform-origin: 0 0;
}
.form-floating > label:dir(rtl) { transform-origin: 100% 0; }
@media (prefers-reduced-motion: no-preference) {
  .form-floating > label { transition: translate var(--mo-duration) ease, scale var(--mo-duration) ease, color var(--mo-duration); }
}

/* …and floated: focused, filled, autofilled, or a field that always shows something */
.form-floating > :is(.form-control:focus, .form-control:not(:placeholder-shown), .form-control:autofill,
  .form-control:is([type="date"], [type="time"], [type="datetime-local"], [type="month"], [type="week"], [type="file"], [type="color"]),
  select.form-select, button.form-select, .menu) ~ label {
  max-width: calc((100% - 2 * var(--mo-control-padding-x)) / var(--mo-floating-scale));
  color: var(--mo-fg-3);
  translate: 0 -.625rem;
  scale: var(--mo-floating-scale);
}
.form-floating > .form-control:focus ~ label,
.form-floating > button.form-select[aria-expanded="true"] ~ label { color: var(--mo-theme-fg); }

/* A Moey dropdown still on its "Choose…" placeholder counts as empty */
.form-floating:has(> button.form-select:not([aria-expanded="true"]) .form-select-value.is-placeholder) > label {
  max-width: calc(100% - 2 * var(--mo-control-padding-x) - 1.5rem);
  color: var(--mo-control-placeholder);
  translate: none;
  scale: none;
}
.form-floating > button.form-select:not([aria-expanded="true"]) .form-select-value.is-placeholder { color: transparent; }
.form-floating > button.form-select { align-items: flex-end; }

/* Textareas: the floated label gets a backdrop so scrolled text doesn't run under it */
.form-floating > textarea.form-control:is(:focus, :not(:placeholder-shown)) ~ label {
  /* the backdrop spans the field; sizes are divided by the scale so they come out right after it */
  inset-inline-start: var(--mo-border-width);
  width: calc((100% - 2 * var(--mo-border-width) - .75rem) / var(--mo-floating-scale));
  max-width: none;
  height: auto;
  padding: calc(.4375rem / var(--mo-floating-scale)) calc(var(--mo-control-padding-x) / var(--mo-floating-scale)) calc(.1875rem / var(--mo-floating-scale));
  margin-top: var(--mo-border-width);
  line-height: 1.25;
  background: var(--mo-control-bg);
  border-start-start-radius: var(--mo-radius-control);
  translate: none;
}
.form-filled .form-floating > textarea.form-control:focus ~ label,
.form-floating > textarea.form-control.form-filled:focus ~ label { background: var(--mo-bg-surface); }
.form-filled .form-floating > textarea.form-control:not(:focus):not(:placeholder-shown) ~ label,
.form-floating > textarea.form-control.form-filled:not(:focus):not(:placeholder-shown) ~ label { background: transparent; }

/* With .form-adorn icons: the label starts after the icon */
.form-floating.form-adorn:has(> .adorn-start) > label { inset-inline-start: calc(var(--mo-border-width) + var(--mo-control-padding-x) + 1.625rem); max-width: calc(100% - 2 * var(--mo-control-padding-x) - 1.625rem); }
.form-floating.form-adorn > :is(.adorn-start, .adorn-end) { top: calc(var(--mo-floating-height) / 2); }

/* States */
.form-floating > :is(.form-control, .form-select):disabled ~ label { color: var(--mo-fg-3); opacity: .7; }
.form-floating:has(> :is(.form-control, .form-select, .form-select-native):is(.is-invalid, :user-invalid)) > label { color: var(--mo-red-fg); }
.form-floating > :required ~ label::after,
.form-floating > .form-select-native:required ~ label::after { content: " *"; color: var(--mo-red-fg); }

/* Inside an input group: one piece of the row */
.input-group > .form-floating { flex: 1 1 auto; width: 1%; }
.input-group:has(> .form-floating) { min-height: var(--mo-floating-height, 3.25rem); }
.input-group > .form-floating > :is(.form-control, .form-select) {
  height: 100%;
  background-color: transparent;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}
.input-group:has(> .form-floating > .form-control:focus) {
  border-color: var(--mo-theme-bg);
  box-shadow: 0 0 0 var(--mo-focus-ring-width) var(--mo-theme-focus-ring);
}
.input-group > .form-floating + :is(.form-control, .form-floating) { border-inline-start: var(--mo-border-width) solid var(--mo-ig-line); }
