/* The code entry (docs/motion-design.md §6.3, code-input.js): /signin's six-digit code as one tray that changes
   shape (the pill, the six cells, Verifying, Verified, the error), and /account's Use code morph (.otp-pm).
   signin.html and account.html link it after app.css. The website's pages don't load theme.css, so the values it
   uses are copied here with the token they come from; the website is dark only today (the light values are
   iOS's, and the motion lab's Light). code-input.js writes the moving values through the CSSOM: the tray's size
   (--tw, --th, px), its spread (--s), each cell's --o and --blur, the ring's place (--rp, in cells) and look
   (--ro, --rb, --rr). Classes are otp-…; no style attributes. */

/* The dark values as plain colors: what the website (dark only) shows in every browser, including those without
   light-dark() (Safari before 17.5, Chrome before 123), where a light-dark() value would leave the tray, the
   cells, the error red and the Verified green transparent. */
.otp, .otp-pm {
  --otp-tray: #111114; /* --card */
  --otp-tray-shadow: 0 0 0 1px #ffffff14, inset 0 1px 0 #ffffff0d, 0 12px 32px -10px #000000cc;
  --otp-cell: #1f1f23; /* the cells and the inner pill */
  --otp-ink: #ededee; /* --text: digits, labels, icons, the ring */
  --otp-caret: #86b7ff; /* --accent: the caret is the mark's dot */
  --otp-err: #ffb4a8; /* --danger-text */
  --otp-err-cell: #3a2224;
  --otp-done: #22c55e; /* --done-fill */
  --otp-on-done: #04210f; /* --on-done */
  --otp-shine: #ffffff59; /* --done-shine */
  --otp-focus: #86b7ff87;
  --otp-ease-out: cubic-bezier(.22, 1, .36, 1); /* --ease-out */
  --otp-ease-in: cubic-bezier(.55, 0, 1, .45); /* --ease-in */
  --otp-ease-in-out: cubic-bezier(.65, 0, .35, 1); /* --ease-in-out */
  --otp-font: var(--sans, var(--font, 'Geist', ui-sans-serif, -apple-system, 'Segoe UI', sans-serif));
}
/* Where light-dark() is understood, both themes (the page's color-scheme picks: dark on the website, the motion
   lab's Light, iOS's values). */
@supports (color: light-dark(#000, #fff)) {
  .otp, .otp-pm {
    --otp-tray: light-dark(#ffffff, #111114);
    --otp-tray-shadow: 0 0 0 1px light-dark(#0000000f, #ffffff14), inset 0 1px 0 light-dark(#ffffff00, #ffffff0d),
      0 1px 1px light-dark(#00000014, #00000000), 0 8px 24px -6px light-dark(#00000014, #00000000), 0 12px 32px -10px light-dark(#00000000, #000000cc);
    --otp-cell: light-dark(#f0f0f3, #1f1f23);
    --otp-ink: light-dark(#18181b, #ededee);
    --otp-caret: light-dark(#2563eb, #86b7ff);
    --otp-err: light-dark(#b3261e, #ffb4a8);
    --otp-err-cell: light-dark(#fbe3e1, #3a2224);
    --otp-done: light-dark(#15803d, #22c55e);
    --otp-on-done: light-dark(#ffffff, #04210f);
    --otp-shine: light-dark(#ffffff8c, #ffffff59);
    --otp-focus: light-dark(#2563eb87, #86b7ff87);
  }
}

/* ------------------------------------------------------------ the tray (a capsule 60 tall, padding 6) */
.otp {
  --cw: clamp(38px, 11.5vw, 44px); --ch: 48px; --gap: clamp(4px, 1.4vw, 6px); --pitch: calc(var(--cw) + var(--gap));
  position: relative; height: 60px; margin: 4px 0; color: var(--otp-ink); font-family: var(--otp-font);
}
.otp-tray {
  position: absolute; left: 50%; top: 50%; width: calc(var(--tw, 168) * 1px); height: calc(var(--th, 60) * 1px); translate: -50% -50%;
  border-radius: 999px; background: var(--otp-tray); box-shadow: var(--otp-tray-shadow); pointer-events: none;
}
/* Keyboard focus (code-input.js sets is-kbd when the field was reached with the keyboard). */
.otp.is-kbd:has(input:focus) .otp-tray { outline: 2px solid var(--otp-focus); outline-offset: 3px; }

/* The input: a transparent layer over the whole row (the tray is the input), 16 px so iPhone Safari doesn't zoom. */
.otp > input.otp-input, .otp > input.otp-input:focus {
  position: absolute; inset: 0; z-index: 2; width: 100%; height: 100%; margin: 0; padding: 0; border: 0; border-radius: 0;
  background: transparent; color: transparent; caret-color: transparent; opacity: 0; font-size: 16px; box-shadow: none; outline: none; cursor: text;
}

/* ------------------------------------------------------------ the cells (44×48, 6 apart; phones: they shrink to fit 320 px) */
.otp-cells {
  position: absolute; left: 50%; top: 50%; width: calc(var(--pitch) * (var(--n, 6) - 1) + var(--cw)); height: var(--ch); translate: -50% -50%;
  pointer-events: none;
}
.otp-cell {
  position: absolute; left: calc(50% - var(--cw) / 2); top: 0; display: grid; place-items: center; width: var(--cw); height: var(--ch);
  border-radius: 12px; background: var(--otp-cell);
  translate: calc((var(--i) - var(--mid, 2.5)) * var(--pitch) * var(--s, 1)) 0; filter: blur(calc(var(--blur, 0) * 1px)); opacity: var(--o, 1);
  transition: background-color 120ms var(--otp-ease-out);
}
/* The end cells are concentric with the tray (30 − 6): 24 on their outer corners. */
.otp-cell[data-edge="first"] { border-radius: 24px 12px 12px 24px; }
.otp-cell[data-edge="last"] { border-radius: 12px 24px 24px 12px; }
.otp-digit { font: 600 22px/1 var(--otp-font); font-variant-numeric: tabular-nums; color: var(--otp-ink); transition: color 120ms var(--otp-ease-out); }
/* A digit rises in from 8 px below out of a 3 px blur; one that goes drops 15 px into one. */
.otp-digit.is-in { animation: otp-in 220ms var(--otp-ease-out) both; }
.otp-digit.is-out { animation: otp-out 90ms var(--otp-ease-in) forwards; }
@keyframes otp-in { from { translate: 0 8px; filter: blur(3px); opacity: 0; } }
@keyframes otp-out { to { translate: 0 15px; filter: blur(3px); opacity: 0; } }
.otp.is-error .otp-cell { background: var(--otp-err-cell); }
.otp.is-error .otp-digit { color: var(--otp-err); }

/* The ring: a 2 px stroke in the ink exactly on the active cell's edge, with its radii; the caret (1.5×20, the
   accent) in its middle while the cell is empty, blinking hard on and off every 530 ms. */
.otp-ring {
  position: absolute; left: calc(50% - var(--cw) / 2); top: 0; width: var(--cw); height: var(--ch); box-sizing: border-box;
  border: 2px solid var(--otp-ink); border-radius: var(--rr, 24px 12px 12px 24px);
  translate: calc((var(--rp, 0) - var(--mid, 2.5)) * var(--pitch) * var(--s, 1)) 0; opacity: var(--ro, 0); filter: blur(calc(var(--rb, 0) * 1px));
}
.otp-caret { position: absolute; left: 50%; top: 50%; width: 1.5px; height: 20px; translate: -50% -50%; border-radius: 1px; background: var(--otp-caret); opacity: 0; }
.otp-ring.is-caret .otp-caret { opacity: 1; animation: otp-blink 1.06s steps(1, end) infinite; }
@keyframes otp-blink { 50% { opacity: 0; } }

/* ------------------------------------------------------------ the inner pill: "Enter code", "Verifying", "Verified" */
.otp-label {
  position: absolute; left: 50%; top: 50%; z-index: 1; translate: -50% -50%; display: inline-flex; align-items: center; gap: 10px;
  height: 48px; padding: 0 24px; border-radius: 24px; background-color: var(--otp-cell); color: var(--otp-ink);
  font: 600 16px/1 var(--otp-font); letter-spacing: -.01em; white-space: pre; overflow: hidden; pointer-events: none; opacity: 0;
  transition: background-color 160ms var(--otp-ease-out), color 120ms var(--otp-ease-out);
  /* Never outside the tray: while the tray is the small blob (collapsing, then springing open as Verifying) the
     inner pill is cut to it, 6 px in, so the spinner and the letters come out of the blob (ref. 3). */
  clip-path: inset(max(0px, calc(50% - var(--th, 60) * .5px + 6px)) max(0px, calc(50% - var(--tw, 168) * .5px + 6px)) round 24px);
}
.otp-label[data-icon="lock"] { gap: 12px; }
.otp-label.is-shown { opacity: 1; }
/* Opening: the lock and the words blur away (to 4 px) as the inner pill melts into the tray, in 60 ms. */
.otp-label.is-leaving { animation: otp-melt 60ms var(--otp-ease-in) forwards; }
@keyframes otp-melt { to { opacity: 0; filter: blur(4px); background-color: transparent; } }
/* Arriving (Verifying): the pill's fill fades in with its letters. */
.otp-label.is-arriving { animation: otp-pill-in 160ms var(--otp-ease-out) both; }
@keyframes otp-pill-in { from { background-color: transparent; } }
.otp-label.is-done { background-color: var(--otp-done); color: var(--otp-on-done); }
/* The done sweep (§5.11): one "/" band of light, a third of the pill wide with a bright core, crosses the green
   pill once at a steady pace (its centre from −0.16 to 1.16 of the width: inside the fill for ~150 of its 200 ms,
   as ref. 3's 8.38 → 8.54 s). */
.otp-label::after, .otp-pm::after {
  content: ''; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; opacity: 0;
  background: linear-gradient(105deg, transparent 40%, color-mix(in srgb, var(--otp-shine) 50%, transparent) 46%, var(--otp-shine) 50%,
    color-mix(in srgb, var(--otp-shine) 50%, transparent) 54%, transparent 60%) 160% 0 / 160% 100% no-repeat;
}
.otp-label.is-sweep::after, .otp-pm.otp-pm-sweep::after { animation: otp-sweep 200ms linear; }
@keyframes otp-sweep { from { opacity: 1; background-position: 160% 0; } to { opacity: 1; background-position: -60% 0; } }

/* Its icon: the lock, the mark spinner (16 px, turning in 1.1 s), the check; one blurs out while the next comes in. */
.otp-icon { display: inline-grid; flex: none; place-items: center; min-width: 14px; height: 16px; }
.otp-icon:empty { display: none; }
.otp-ic { grid-area: 1 / 1; display: grid; place-items: center; }
.otp-ic svg { display: block; }
.otp-ic.is-in { animation: otp-ic-in 80ms var(--otp-ease-out) 40ms both; }
.otp-ic.is-out { animation: otp-ic-out 80ms var(--otp-ease-in) forwards; }
@keyframes otp-ic-in { from { opacity: 0; filter: blur(3px); } }
@keyframes otp-ic-out { to { opacity: 0; filter: blur(3px); } }
.otp-spin { display: grid; place-items: center; animation: otp-turn 1.1s linear infinite; }
.otp-spin .otp-dot { fill: var(--otp-caret); }
.is-done .otp-spin .otp-dot, .otp-pm-done .otp-spin .otp-dot { fill: currentColor; }
@keyframes otp-turn { to { rotate: 360deg; } }

/* Its letters: each arrives out of a 4 px blur (120 ms, 12 ms apart); the ones that go blur away where they were. */
.otp-word { position: relative; display: inline-block; white-space: pre; }
.otp-ch { display: inline-block; }
.otp-ch.is-in { animation: otp-ch-in var(--in, 120ms) var(--otp-ease-out) calc(var(--at, 50ms) + var(--k, 0) * var(--stagger, 12ms)) both; }
.otp-chs-out { position: absolute; left: var(--x, 0px); top: 0; white-space: pre; pointer-events: none; }
.otp-chs-out > .otp-ch { animation: otp-ch-out 90ms var(--otp-ease-in) forwards; }
@keyframes otp-ch-in { from { opacity: 0; filter: blur(4px); } }
@keyframes otp-ch-out { to { opacity: 0; filter: blur(.2em); } }

/* The page's own words for screen readers (its "Code" label, the state). */
.otp-sr { position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }

/* ------------------------------------------------------------ /account's Use code */
.otp-pm { position: relative; overflow: hidden; white-space: nowrap; transition: background-color 160ms var(--otp-ease-out), color 120ms var(--otp-ease-out); }
.otp-pm .otp-icon { height: 14px; min-width: 14px; }
.otp-pm .otp-spin svg { width: 14px; height: 14px; }
.otp-pm.otp-pm-done, .otp-pm.otp-pm-done:hover, .otp-pm.otp-pm-done:disabled { background: var(--otp-done); border-color: transparent; color: var(--otp-on-done); opacity: 1; }
.otp-pm:disabled { opacity: 1; cursor: default; }

/* ------------------------------------------------------------ less motion: cross-fades at the final size, a steady caret */
@media (prefers-reduced-motion: reduce) {
  .otp-digit.is-in, .otp-digit.is-out, .otp-ch.is-in, .otp-chs-out > .otp-ch, .otp-ic.is-in, .otp-ic.is-out, .otp-label.is-leaving, .otp-label.is-arriving { animation: none; }
  .otp-chs-out, .otp-ic.is-out { display: none; }
  .otp-label.is-sweep::after, .otp-pm.otp-pm-sweep::after { animation: none; }
  .otp-ring.is-caret .otp-caret { animation: none; }
  .otp-label { transition: opacity 120ms linear, background-color 120ms linear, color 120ms linear; }
  .otp-spin { animation-duration: 1.6s; }
}
html[data-motion="reduce"] :is(.otp-digit.is-in, .otp-digit.is-out, .otp-ch.is-in, .otp-chs-out > .otp-ch, .otp-ic.is-in, .otp-ic.is-out, .otp-label.is-leaving, .otp-label.is-arriving) { animation: none; }
html[data-motion="reduce"] :is(.otp-chs-out, .otp-ic.is-out) { display: none; }
html[data-motion="reduce"] :is(.otp-label.is-sweep, .otp-pm.otp-pm-sweep)::after { animation: none; }
html[data-motion="reduce"] .otp-ring.is-caret .otp-caret { animation: none; }
html[data-motion="reduce"] .otp-label { transition: opacity 120ms linear, background-color 120ms linear, color 120ms linear; }
html[data-motion="reduce"] .otp-spin { animation-duration: 1.6s; }
