/* Keycode: a one-time-code input whose digits drop into place. */

.keycode {
  box-sizing: border-box;
  width: min(520px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: clamp(24px, 6vw, 40px);
  background: var(--kc-surface);
  border-radius: var(--kc-radius);
  box-shadow: var(--kc-shadow);
  font-family: var(--kc-font);
  color: var(--kc-ink);
  text-align: center;
}

.keycode__icon {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background: var(--kc-icon-bg);
  color: var(--kc-accent-text);
}

.keycode__title {
  margin: 0;
  font-size: 28px;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.keycode__lead {
  margin: 0;
  font-size: 15px;
  color: var(--kc-muted);
}

.keycode__lead strong { color: var(--kc-ink); font-weight: 500; }

.keycode__form {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.keycode__fieldset {
  min-width: 0;
  margin: 8px 0 0;
  padding: 0;
  border: 0;
}

.keycode__boxes {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 62px));
  gap: clamp(6px, 2vw, 10px);
  justify-content: center;
}

.keycode__box {
  position: relative;
  aspect-ratio: 62 / 76;
  border: 1.5px solid var(--kc-line);
  border-radius: var(--kc-box-radius);
  background: var(--kc-surface);
  transition: border-color 120ms linear, box-shadow 120ms linear;
}

.keycode__box[data-filled] { border-color: var(--kc-ink); }

.keycode__box:focus-within {
  border-color: var(--kc-accent);
  box-shadow: 0 0 0 4px var(--kc-ring);
}

.keycode[data-state="error"] .keycode__box { border-color: var(--kc-error); }
.keycode[data-state="success"] .keycode__box { border-color: var(--kc-success); }

/* The real input fills the box; its own text is hidden and drawn by .keycode__digit. */
.keycode__input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: inherit;
  background: transparent;
  color: transparent;
  caret-color: transparent;
  font-family: var(--kc-mono);
  font-size: 32px;
  text-align: center;
  outline: none;
  cursor: text;
}

.keycode__input::selection { background: transparent; }

.keycode__digit {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  font-family: var(--kc-mono);
  font-size: clamp(22px, 6vw, 32px);
  font-weight: 600;
  color: var(--kc-ink);
}

/* Two fading copies above the digit while it drops. */
.keycode__digit::before,
.keycode__digit::after {
  content: attr(data-digit);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: 0;
}

.keycode__digit::before { color: var(--kc-trail-near); transform: translateY(-14px); }
.keycode__digit::after { color: var(--kc-trail); transform: translateY(-28px); }

.keycode__digit--drop { animation: keycode-drop var(--kc-drop) var(--kc-spring) both; }
.keycode__digit--drop::before { animation: keycode-trail var(--kc-drop) linear both; }
.keycode__digit--drop::after { animation: keycode-trail var(--kc-drop) linear both; }

/* A pasted code drops in one digit after another (set by the script). */
.keycode__digit--drop,
.keycode__digit--drop::before,
.keycode__digit--drop::after { animation-delay: var(--kc-delay, 0ms); }

@keyframes keycode-drop {
  from { opacity: 0; transform: translateY(-28px); }
  40% { opacity: 1; }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes keycode-trail {
  from { opacity: 0.9; }
  to { opacity: 0; }
}

/* A caret in the empty box that has focus. */
.keycode__box:not([data-filled]):focus-within::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 2px;
  height: 40%;
  background: var(--kc-accent);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.keycode__boxes--shake { animation: keycode-shake var(--kc-shake) ease-in-out both; }

@keyframes keycode-shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-8px); }
  30% { transform: translateX(8px); }
  45% { transform: translateX(-6px); }
  60% { transform: translateX(6px); }
  75% { transform: translateX(-3px); }
  90% { transform: translateX(3px); }
}

.keycode__message {
  min-height: 20px;
  margin: 0;
  font-size: 14px;
  font-weight: 500;
}

.keycode[data-state="error"] .keycode__message { color: var(--kc-error); }
.keycode[data-state="success"] .keycode__message { color: var(--kc-success); }

.keycode__hint {
  margin: 0;
  font-size: 14px;
  color: var(--kc-muted);
}

.keycode__resend {
  padding: 2px;
  border: 0;
  background: none;
  color: var(--kc-accent-text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.keycode__verify {
  width: 100%;
  height: 48px;
  border: 0;
  border-radius: var(--kc-button-radius);
  background: var(--kc-ink);
  color: var(--kc-surface);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--kc-fade) linear, color var(--kc-fade) linear;
}

.keycode__verify:disabled {
  background: var(--kc-line);
  color: var(--kc-muted);
  cursor: default;
}

.keycode__resend:focus-visible,
.keycode__verify:focus-visible {
  outline: 2px solid var(--kc-focus);
  outline-offset: 2px;
}

.keycode__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Reduced motion: digits fade in, no trail, no shake. */
@media (prefers-reduced-motion: reduce) {
  .keycode__digit--drop { animation: keycode-fade var(--kc-fade) linear both; animation-delay: 0ms; }
  .keycode__digit--drop::before,
  .keycode__digit--drop::after { animation: none; }
  .keycode__boxes--shake { animation: none; }
}

@keyframes keycode-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
