/* ==========================================================================
   OTP input — one box per character for verification codes.

   <input class="form-control" name="code" data-moey-otp="6" aria-label="Verification code">

   It stays ONE real input (invisible, over the boxes), so phones can fill the
   code from the SMS (autocomplete="one-time-code"), paste works, and forms,
   `required` and screen readers see a normal field.
   data-moey-otp="4"            how many characters (default: maxlength, or 6)
   data-moey-separator="3"      a dash after every 3
   data-moey-otp-type="alphanumeric"   letters too (shown in capitals)
   data-moey-mask               dots instead of the characters
   data-moey-autosubmit         send the form when it's full
   .otp-sm · .otp-lg · .form-filled · event moey:complete (detail.value)

   Countdown on any button — "Resend code" that waits:
   <button class="btn" data-moey-countdown="30">Resend code</button>
   ========================================================================== */

.otp {
  --mo-otp-size: 3rem;
  --mo-otp-gap: .5rem;
  --mo-otp-font-size: 1.375rem;
  position: relative;
  display: flex;
  gap: var(--mo-otp-gap);
  align-items: center;
  --mo-otp-sep-width: .625rem;
  /* Its natural width (moey.js sets the counts), shrinking to fit narrow screens */
  width: calc(var(--mo-otp-count, 6) * var(--mo-otp-size) + var(--mo-otp-seps, 0) * var(--mo-otp-sep-width) + (var(--mo-otp-count, 6) + var(--mo-otp-seps, 0) - 1) * var(--mo-otp-gap));
  max-width: 100%;
}
.otp-sm { --mo-otp-size: 2.5rem; --mo-otp-gap: .375rem; --mo-otp-font-size: 1.125rem; }
.otp-lg { --mo-otp-size: 3.5rem; --mo-otp-gap: .625rem; --mo-otp-font-size: 1.625rem; }
@media (width < 400px) { .otp { --mo-otp-gap: .375rem; --mo-otp-sep-width: .375rem; } }

/* The real input covers the boxes but is invisible: its own text, caret and selection */
.otp > input,
.otp > input.form-control {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0;
  font-size: 1px;
  color: transparent;
  letter-spacing: 0;
  caret-color: transparent;
  cursor: text;
  background: transparent;
  border: 0;
  outline: 0;
  box-shadow: none;
  -webkit-text-fill-color: transparent;
}
.otp > input::selection { background: transparent; }
.otp > input:autofill { -webkit-box-shadow: 0 0 0 100px transparent inset; transition: background-color 600000s; }
.otp > input:disabled { cursor: not-allowed; }

/* The boxes */
.otp-slot {
  position: relative;
  display: grid;
  flex: 1 1 0;
  place-items: center;
  min-width: 0;
  height: calc(var(--mo-otp-size) * 1.15);
  font-size: var(--mo-otp-font-size);
  font-weight: var(--mo-font-weight-semibold, 600);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--mo-fg-body);
  background: var(--mo-control-bg);
  border: var(--mo-border-width) solid var(--mo-control-border-color);
  border-radius: var(--mo-radius-control);
}
@media (prefers-reduced-motion: no-preference) {
  .otp-slot { transition: border-color var(--mo-duration), box-shadow var(--mo-duration), background-color var(--mo-duration); }
}
.otp:hover:not(:has(> input:disabled)) .otp-slot { border-color: color-mix(in oklab, var(--mo-control-border-color), var(--mo-fg-body) 15%); }
.otp-slot.is-active,
.otp:hover .otp-slot.is-active {
  z-index: 1;
  border-color: var(--mo-theme-bg);
  box-shadow: 0 0 0 var(--mo-focus-ring-width) var(--mo-theme-focus-ring);
}
/* A blinking caret in the empty box you're on */
.otp-slot.is-active:empty::after {
  width: 1.5px;
  height: 45%;
  content: "";
  background: var(--mo-fg-body);
  animation: mo-otp-caret 1.1s steps(1) infinite;
}
@keyframes mo-otp-caret { 50% { opacity: 0; } }
.otp-slot.is-placeholder { color: var(--mo-control-placeholder); font-weight: var(--mo-font-weight-normal); }

.otp-sep {
  flex-shrink: 0;
  width: var(--mo-otp-sep-width);
  height: 2px;
  background: var(--mo-border-color-strong);
  border-radius: 2px;
}

/* Filled (borderless) look */
.form-filled .otp-slot,
.otp.form-filled .otp-slot {
  background: var(--mo-control-fill);
  border-color: transparent;
}
.form-filled .otp-slot.is-active,
.otp.form-filled .otp-slot.is-active { background: var(--mo-bg-surface); border-color: var(--mo-theme-bg); }

/* States */
.otp:has(> input:disabled) .otp-slot { color: var(--mo-fg-3); background: var(--mo-bg-1); border-color: var(--mo-border-color); }
.otp:has(> input:is(.is-invalid, :user-invalid)) {
  --mo-theme-bg: var(--mo-red-bg);
  --mo-theme-focus-ring: var(--mo-red-focus-ring);
}
.otp:has(> input:is(.is-invalid, :user-invalid)) .otp-slot { border-color: var(--mo-red-bg); }
.otp:has(> input.is-valid) .otp-slot { border-color: var(--mo-green-bg); }
.otp:has(> input.is-invalid) { animation: mo-otp-shake .3s ease; }
@media (prefers-reduced-motion: reduce) { .otp:has(> input.is-invalid) { animation: none; } }
@keyframes mo-otp-shake { 25% { translate: -4px; } 75% { translate: 4px; } }
.field:has(.otp > input:is(.is-invalid, :user-invalid)) > .form-error,
.otp:has(> input:is(.is-invalid, :user-invalid)) ~ .form-error { display: flex; }
