/* Black Friday: character-by-character typing of the BLACK|FRIDAY wordmark.
 *
 * One animation, shared by every wordmark that types:
 *   - homepage course cards (css/home-blackfriday-cards.css positions the
 *     layer over each card's badge),
 *   - the /off hero (public/css/off-blackfriday.css styles the wordmark
 *     itself; nothing here changes it).
 * js/blackfriday-typing.js builds the markup and adds .is-typing once the
 * wordmark is on screen; this file is only the reveal, so both places type at
 * exactly the same speed with the same cursor.
 *
 * Markup (built by the script):
 *   .bf-type                         root, gets .is-typing
 *     <box>                          the page's own black / red box
 *       .bf-type__text[--lead]       one word; --lead carries the opening cursor
 *         .bf-type__ch (--i: n)      one letter, hidden until its turn
 *
 * Sequence: cursor -> B L A C K -> (beat for the space) -> F R I D A Y ->
 * cursor blinks three times and goes; the full wordmark stays. The /off
 * hero loops instead (.bf-type--loop, below): typed the same way, held, erased
 * letter by letter, a short pause, typed again. Letters are
 * hidden with opacity, so every one already holds its final place: the
 * wordmark's width never changes and nothing shifts while it types.
 */

.bf-type {
  --bf-type-start: 420ms; /* opening cursor, then the first letter */
  --bf-type-step: 115ms;  /* one letter (or the space beat) */
  /* Loop only (read by js/blackfriday-typing.js): */
  --bf-type-hold: 1500ms;      /* full wordmark before erasing */
  --bf-type-erase-step: 80ms;  /* one letter erased (or the space beat) */
  --bf-type-gap: 400ms;        /* empty, before typing again */
}

.bf-type__text {
  position: relative;
}

.bf-type__ch {
  position: relative;
  opacity: 0;
}

.bf-type.is-typing .bf-type__ch {
  animation: bf-type-in 170ms cubic-bezier(.2, .7, .3, 1) calc(var(--bf-type-start) + var(--i) * var(--bf-type-step)) both;
}

/* Cursor: a thin bar in the box's own text colour, so it reads on both the
   black/white and the red box. Opening one before B, then after each letter
   until the next appears. 1.5px on the small card badges, thicker on the
   large hero wordmark. */
.bf-type__text--lead::before,
.bf-type__ch::after {
  position: absolute;
  top: .1em;
  bottom: .1em;
  width: max(1.5px, .08em);
  border-radius: 1px;
  background: currentColor;
  content: "";
  opacity: 0;
  pointer-events: none;
}

.bf-type__text--lead::before {
  left: -.12em;
}

/* Straddles the letter's trailing spacing and the next letter's start (not
   shown yet), clear of the glyph just typed. */
.bf-type__ch::after {
  right: -1px;
}

.bf-type.is-typing .bf-type__text--lead::before {
  animation: bf-type-caret var(--bf-type-start) step-end;
}

.bf-type.is-typing .bf-type__ch::after {
  animation: bf-type-caret calc(var(--hold, 1) * var(--bf-type-step)) step-end calc(var(--bf-type-start) + var(--i) * var(--bf-type-step));
}

.bf-type.is-typing .bf-type__ch--last::after {
  animation: bf-type-blink 1s step-end calc(var(--bf-type-start) + var(--i) * var(--bf-type-step)) 3;
}

/* Loop mode (.bf-type--loop: the /off hero, for as long as the
   Black Friday campaign renders it).
   The script steps through the letters itself: .is-on shows a letter (same
   fade as above), and .is-caret marks where the cursor sits. The cursor stays
   solid while it moves (each move restarts the blink) and blinks while the
   wordmark waits full or empty, like a text cursor. */
.bf-type--loop .bf-type__ch {
  transition: opacity 90ms ease-in;
}

.bf-type--loop .bf-type__ch.is-on {
  opacity: 1;
  transition: opacity 170ms cubic-bezier(.2, .7, .3, 1);
}

.bf-type--loop .bf-type__text--lead.is-caret::before,
.bf-type--loop .bf-type__ch.is-caret::after {
  animation: bf-type-blink 1s step-end infinite;
}

@keyframes bf-type-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes bf-type-caret {
  0%, 100% { opacity: 1; }
}

@keyframes bf-type-blink {
  0% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

/* The script does not build the markup for these users; if the setting
   changes on an open page, show the full wordmark at once with no cursor. */
@media (prefers-reduced-motion: reduce) {
  .bf-type .bf-type__ch,
  .bf-type.is-typing .bf-type__ch {
    opacity: 1;
    animation: none;
    transition: none;
  }

  .bf-type.is-typing .bf-type__text--lead::before,
  .bf-type.is-typing .bf-type__ch::after,
  .bf-type--loop .bf-type__text--lead.is-caret::before,
  .bf-type--loop .bf-type__ch.is-caret::after {
    animation: none;
  }
}
