/* Title dock: see README.md. The script (title-dock.js) writes the keyframes
   and sets --td-d (the scroll it takes), --td-fade and each line's --td-anim. */

/* The header's name is hidden from first paint on a page that has a name to
   dock (td-soon, set by the <head> snippet), and while the dock is on. */
.td-soon:has(.td-name) .td-target,
.td-js .td-target { opacity: 0; }

/* ⚠ Headroom. While it docks, Safari paints each line on its own layer cut
   to the line's box, and glyphs that reach outside a tight line-height (an
   italic's ascenders: the d of "Wild" on Emily-Rose's site) were sliced off.
   Padding with a matching negative margin grows the box without moving
   anything. Always on, not only under .td-on, because the script measures
   with .td-on off and the box must be the same both ways. Set
   --td-headroom: 0 on a line whose own background matters (gradient text
   clipped to the box) and pad it yourself. Pictures are left alone. */
.td-name > :not(.td-skip):not(img):not(svg):not(picture):not([data-td-to]) {
  padding-top: var(--td-headroom, 0.2em);
  margin-top: calc(-1 * var(--td-headroom, 0.2em));
  /* An italic's last letter leans past the line's end (Emily-Rose's d,
     sliced vertically when the title was right-aligned): room there too. A
     background on the line (an underline) grows by this; size it to
     calc(100% - var(--td-headroom-end)). */
  padding-right: var(--td-headroom-end, 0.3em);
  margin-right: calc(-1 * var(--td-headroom-end, 0.3em));
}

@supports (animation-timeline: scroll()) {
  /* The name travels above the header. ⚠ Any ancestor of .td-name with its own
     stacking layer (z-index on a positioned box, opacity < 1, transform, filter)
     traps it below the header: give those z-index:auto under .td-on, and give
     their other children a z-index of their own (README, step 4). */
  .td-on .td-name { pointer-events: none; }
  /* ⚠ Only what docks rides above the header, not the whole .td-name block.
     Lifting the block (the old rule) made anything left behind scroll OVER the
     header's pills: a long name that doesn't dock on a phone, a .td-skip part.
     .td-name is no stacking layer; each moving piece takes the z-index itself.
     position is :where() (specificity 0) so a piece a site already positions
     (absolute) keeps its own. */
  :where(.td-on .td-name:not(.td-pieces) > :not(.td-skip), .td-on .td-name.td-pieces [data-td-to]) { position: relative; }
  .td-on .td-name:not(.td-pieces) > :not(.td-skip),
  .td-on .td-name.td-pieces [data-td-to] { z-index: var(--td-z, 60); }

  /* ⚠ Every shorthand beside an animation-timeline carries a var(): CSS
     minifiers (esbuild, Lightning CSS) otherwise fold animation-timeline into
     it, browsers reject the result, and the whole rule silently disappears. */
  /* Text and logo lines are the name's children; in pieces mode (script adds
     .td-pieces) every [data-td-to] piece moves, and a piece inside a piece
     gets --td-hide: none, because it fades with its parent. */
  .td-on .td-name:not(.td-pieces) > :not(.td-skip),
  .td-on .td-name.td-pieces [data-td-to] {
    transform-origin: 0 0;
    animation: var(--td-anim) var(--td-ease, linear) both, var(--td-hide, td-out) var(--td-ease, linear) both;
    animation-timeline: scroll(root), scroll(root);
    animation-range: 0px var(--td-d), calc(var(--td-d) - var(--td-fade, 6px) * .5 - 50000px) calc(var(--td-d) - var(--td-fade, 6px) * .5 + 50000px);
  }
  /* Parts of the big name the header doesn't have (a year, a tagline) fade
     over the first 40% of the journey. */
  .td-on .td-skip {
    animation: td-hide var(--td-ease, linear) both;
    animation-timeline: scroll(root);
    animation-range: 0px calc(var(--td-d) * .4);
  }
  .td-on .td-target {
    animation: td-in var(--td-ease, linear) both;
    animation-timeline: scroll(root);
    animation-range: calc(var(--td-d) - var(--td-fade, 6px) * .5 - 50000px) calc(var(--td-d) - var(--td-fade, 6px) * .5 + 50000px);
  }
  /* A phone menu opened near the top waits for the name to dock, then fades in. */
  .td-on .td-menu { transition: opacity .25s ease; }
  .td-wait { opacity: 0 !important; pointer-events: none; }
}

/* The handover: the name holds still in register over the last --td-fade px, and the
   two swap at once in the middle of that. A longer crossfade dimmed the name, and showed
   two whenever they were a hair apart (Joseph, Dec 2026).
   ⚠ The swap is the MIDDLE of a 100000px range, so both animations are always running.
   Safari hands an animation to its scrolling thread only once it has started: one that
   starts at the swap is late, so no name showed for a few frames on the way down, and
   two on the way up.
   Opacity, not visibility: visibility runs on the main thread, and on a fast flick the
   name rode on up with the page until the main thread caught up. */
@keyframes td-out { 0%, 49.9999% { opacity: 1; } 50%, 100% { opacity: 0; } }
@keyframes td-in { 0%, 49.9999% { opacity: 0; } 50%, 100% { opacity: 1; } }
/* Plain fades, for .td-skip and for a site's own parts. */
@keyframes td-hide { from { opacity: 1; } to { opacity: 0; } }
@keyframes td-show { from { opacity: 0; } to { opacity: 1; } }
