/* Timeline: dark section, label + Prev / Next on top, then one
   swipeable track with the heading followed by staggered entries. The
   dotted line runs from the year badge to the middle of the track;
   every second entry is flipped (info below, feature above). */

.timeline {
  --theme-timeline-badge: 2.25rem;
  --theme-timeline-item-width: clamp(22rem, 38vw, 36rem);
  --theme-timeline-gap: clamp(2rem, 3vw, 2.5rem);
  --theme-timeline-list-gap: clamp(1.5rem, 3vw, 3rem);

  /* The header is fixed over the section when it is scrolled to the top: the content
     starts below it. */
  position: relative;
  padding-block: calc(var(--theme-header-height) + clamp(0.5rem, 1.2vw, 1.25rem))
    clamp(4rem, 7vw, 6.5rem);
  overflow: hidden;
  isolation: isolate;
}

/* From desktop up the section fills the screen: the entries grow with its height and
   sit in the middle of it. */

@media (width > 768px) {
  .timeline {
    display: flex;
    flex-direction: column;
    min-height: 100svh;
    --theme-timeline-item-height: clamp(
      26rem,
      calc(100svh - 18.5rem - var(--theme-header-height)),
      46rem
    );
  }
}

.timeline__head {
  position: relative;
  z-index: 1;
  align-items: baseline;
}

.timeline__label {
  grid-column: 1 / span 6;
}

.timeline__controls {
  display: flex;
  grid-column: 7 / -1;
  gap: clamp(1rem, 1.9vw, 1.75rem);
  justify-content: end;
  font-size: var(--theme-fs-xs);
}

.timeline__controls button {
  line-height: 1.9;
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 0.3em;
  transition: text-decoration-color 0.3s var(--theme-ease-signature);
}

/* The underline shows what a click would do: Next at rest and over the right half
   of the track, Prev over the left half, the button itself when the pointer is on it
   (data-hover, set by main.js). It fades between them. */

.timeline:not([data-hover]) [data-timeline-next]:not(:disabled),
.timeline[data-hover='right'] [data-timeline-next]:not(:disabled),
.timeline[data-hover='left'] [data-timeline-prev]:not(:disabled) {
  text-decoration-color: currentcolor;
}

.timeline__controls button:disabled {
  opacity: 0.4;
  cursor: default;
}

.timeline__stage {
  position: relative;
  z-index: 1;
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  margin-block-start: clamp(2rem, 4vw, 4rem);
}

.timeline__track {
  display: flex;
  align-items: center;
  gap: clamp(2rem, 5vw, 5rem);
  padding-inline: var(--theme-margin);
  position: relative;
  overflow-x: auto;
  scrollbar-width: none;
  cursor: grab;
  overscroll-behavior-x: contain;
}

.timeline__track::-webkit-scrollbar {
  display: none;
}

.timeline__track.is-dragging {
  cursor: grabbing;
  user-select: none;
}

.timeline__list {
  position: relative;
  display: flex;
  flex: none;
  gap: var(--theme-timeline-list-gap);
  list-style: none;
}

.timeline-item {
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: var(--theme-timeline-gap);
  width: var(--theme-timeline-item-width);
  height: var(--theme-timeline-item-height, clamp(26rem, 33vw, 31rem));
}

.timeline-item--flip .timeline-item__info {
  grid-row: 2;
  align-content: end;
  align-items: end;
}

.timeline-item--flip .timeline-item__feature {
  grid-row: 1;
  align-items: start;
}

.timeline-item__info {
  position: relative;
  z-index: 2;
  display: grid;
  grid-template-columns: var(--theme-timeline-badge) 1fr;
  column-gap: clamp(0.75rem, 1.2vw, 1.1rem);
  align-items: start;
  align-content: start;
  font-size: var(--theme-fs-xs);
  line-height: var(--theme-lh-snug);
}

/* Dotted line and dot, from the badge to the middle of the entry. */
.timeline-item__info::before {
  content: '';
  position: absolute;
  inset-inline-start: calc(var(--theme-timeline-badge) / 2);
  inset-block: var(--theme-timeline-badge) calc(var(--theme-timeline-gap) / -2);
  border-inline-start: 1px dotted var(--theme-color-text-inverse);
}

.timeline-item__info::after {
  content: '';
  position: absolute;
  inset-inline-start: calc(var(--theme-timeline-badge) / 2 - 0.3rem);
  inset-block-end: calc(var(--theme-timeline-gap) / -2 - 0.3rem);
  width: 0.6rem;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--theme-color-text-inverse);
  transition:
    background-color 0.5s var(--theme-ease-signature),
    scale 0.5s var(--theme-ease-signature);
}

.timeline-item--flip .timeline-item__info::before {
  inset-block: calc(var(--theme-timeline-gap) / -2) var(--theme-timeline-badge);
}

.timeline-item--flip .timeline-item__info::after {
  inset-block: calc(var(--theme-timeline-gap) / -2 - 0.3rem) auto;
}

.timeline-item__year {
  display: grid;
  grid-row: 1 / span 2;
  place-items: center;
  width: var(--theme-timeline-badge);
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--theme-color-primary);

  /* Whole pixel size and a little larger, medium weight: four digits stay sharp. */
  font-size: 0.6875rem;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  line-height: 1;
}

.timeline-item__name {
  align-self: center;
  min-height: var(--theme-timeline-badge);
  font-size: var(--theme-fs-md);
  font-weight: 400;
  letter-spacing: 0;
  line-height: var(--theme-timeline-badge);
}

.timeline-item__text {
  grid-column: 2;
  max-width: 17em;
}

.timeline-item--flip .timeline-item__year {
  align-self: end;
}

.timeline-item__feature {
  display: flex;
  gap: clamp(1rem, 2vw, 1.75rem);
  align-items: end;
}

.timeline-item__media {
  flex: none;
  width: 46%;
  aspect-ratio: 200 / 115;
}

.timeline-item__media img {
  pointer-events: none;
}

.timeline-item__headline {
  color: var(--theme-color-primary);
  font-size: var(--theme-fs-8xl);
  line-height: var(--theme-lh-tight);
  letter-spacing: var(--theme-tracking-heading);
}

/* From desktop up the picture or video fills the whole cell of its entry and the large
   title sits over its lower left, on a dark fade. Entries without a picture keep the
   title on its own. */

@media (width > 768px) {
  .timeline-item__feature:has(.timeline-item__media) {
    position: relative;
    align-items: end;
  }

  /* The frame runs up to the line through the dots (half the gap between the two rows)
     and across to the dotted line of the next entry (the gap between the entries plus
     half a badge). The last entry has nothing next to it. */
  .timeline-item__media {
    position: absolute;
    inset: calc(var(--theme-timeline-gap) / -2)
      calc(-1 * (var(--theme-timeline-list-gap) + var(--theme-timeline-badge) / 2)) 0 0;
    width: auto;
    aspect-ratio: auto;
  }

  .timeline-item--flip .timeline-item__media {
    inset-block: 0 calc(var(--theme-timeline-gap) / -2);
  }

  .timeline-item:last-child .timeline-item__media {
    inset-inline-end: 0;
  }

  .timeline-item__media::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
      to top,
      color-mix(in srgb, var(--theme-color-inverse) 92%, transparent),
      transparent 78%
    );
    pointer-events: none;
  }

  .timeline-item__feature:has(.timeline-item__media) .timeline-item__headline {
    position: relative;
    z-index: 1;
    max-width: 90%;
    padding: 0 clamp(1rem, 1.6vw, 1.5rem) clamp(0.9rem, 1.4vw, 1.25rem);
  }
}

/* Scroll-linked motion (main.js writes --theme-timeline-place, the entry's place in the track
   from -1 to 1, and --theme-timeline-in, how much of it shows right of the year block, on
   every entry). Entries that slide away or come in are dimmer, the pictures drift against
   the scroll inside their frames, and the dot of the entry in front lights up. */

@media (prefers-reduced-motion: no-preference) {
  .timeline-item {
    opacity: calc((0.35 + 0.65 * var(--theme-timeline-in, 1)) * var(--theme-timeline-fade, 1));
  }

  .timeline-item__media :is(img, video) {
    scale: 1.22;
    translate: calc(var(--theme-timeline-place, 0) * -7%) 0;
  }
}

.timeline-item__media video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.timeline-item.is-active .timeline-item__info::after {
  background: var(--theme-color-primary);
  scale: 1.5;
}

/* The line through the dots, in the middle between the upper and the lower row. */

.timeline__list::before {
  content: '';
  position: absolute;
  transform-origin: left;
  inset-inline: calc(var(--theme-timeline-badge) / 2) 0;
  inset-block-start: 50%;
  border-block-start: 1px dotted var(--theme-color-text-inverse);
}

/* The year of the entry in front, under the label in the header. */

.timeline__count {
  grid-column: 1 / span 6;
  grid-row: 2;
  margin-block-start: clamp(0.75rem, 1.6vw, 1.5rem);
}

.timeline__reels {
  display: flex;
  height: 1em;
  overflow: hidden;
  color: var(--theme-color-primary);
  font-size: var(--theme-fs-8xl);
  font-variant-numeric: tabular-nums;
  letter-spacing: var(--theme-tracking-heading);
  line-height: 1;
}

.timeline__digit {
  display: block;
  height: 1em;
  overflow: hidden;
}

.timeline__reel {
  display: flex;
  flex-direction: column;
  transition: translate 1s var(--theme-ease-signature);
  transition-delay: calc(var(--theme-timeline-order, 0) * 60ms);
  translate: 0 calc(var(--theme-timeline-digit, 0) * -1em);
}

.timeline__reel span {
  display: block;
  height: 1em;
}

@media (width <= 768px) {
  .timeline {
    --theme-timeline-item-width: 78vw;
  }

  .timeline__reels {
    font-size: var(--theme-fs-5xl);
  }

  .timeline__list::before {
    display: none;
  }

  .timeline__label {
    grid-column: 1 / span 6;
  }

  .timeline__track {
    align-items: start;
    padding-inline-start: var(--theme-margin);
    gap: var(--theme-space-sm);
  }

  .timeline-item {
    --theme-timeline-gap: var(--theme-space-xs);

    height: auto;
  }

  .timeline-item--flip .timeline-item__info,
  .timeline-item--flip .timeline-item__feature {
    grid-row: auto;
  }

  .timeline-item--flip .timeline-item__info {
    align-items: start;
  }

  .timeline-item__info::before,
  .timeline-item__info::after {
    display: none;
  }

  .timeline-item__feature {
    flex-direction: column;
    align-items: start;
  }

  .timeline-item__media {
    width: 100%;
  }

  .timeline-item__headline {
    font-size: var(--theme-fs-5xl);
  }
}

/* Entrance, like passing through time: when the timeline comes into view the line through
   the dots is drawn from left to right and the entries follow it one by one. Each entry
   starts on the line: its dot appears, the dotted line grows out of it, and when that
   reaches the end the badge with the year pops in, then the text and the picture. The
   others do the same as they slide in. main.js sets is-drawn on the section and is-in
   (with a delay) on each entry. */

@media (prefers-reduced-motion: no-preference) {
  .timeline__list::before {
    transition: scale 2.4s var(--theme-ease-signature) 0.1s;
  }

  .js-reveal .timeline:not(.is-drawn) .timeline__list::before {
    scale: 0 1;
  }

  .js-reveal
    .timeline-item:not(.is-in)
    :is(
      .timeline-item__year,
      .timeline-item__name,
      .timeline-item__text,
      .timeline-item__headline,
      .timeline-item__media
    ),
  .js-reveal .timeline-item:not(.is-in) .timeline-item__info::before,
  .js-reveal .timeline-item:not(.is-in) .timeline-item__info::after {
    opacity: 0;
  }

  /* 1. The dot on the line. */
  .timeline-item.is-in .timeline-item__info::after {
    animation: timeline-pop 0.5s var(--theme-ease-signature) var(--theme-timeline-delay, 0s)
      backwards;
  }

  /* 2. The dotted line grows out of the dot, up for the entries above the line and down
     for the flipped ones. */
  .timeline-item.is-in .timeline-item__info::before {
    transform-origin: bottom;
    animation: timeline-draw 0.8s cubic-bezier(0.65, 0, 0.25, 1)
      calc(var(--theme-timeline-delay, 0s) + 0.3s) backwards;
  }

  .timeline-item--flip.is-in .timeline-item__info::before {
    transform-origin: top;
  }

  /* 3. The badge with the year, once the line has arrived. */
  .timeline-item.is-in .timeline-item__year {
    animation: timeline-badge 0.6s var(--theme-ease-signature)
      calc(var(--theme-timeline-delay, 0s) + 1s) backwards;
  }

  /* 4. Then the text and the picture. */
  .timeline-item.is-in .timeline-item__name {
    animation: timeline-rise 0.9s var(--theme-ease-signature)
      calc(var(--theme-timeline-delay, 0s) + 1.15s) backwards;
  }

  .timeline-item.is-in :is(.timeline-item__text, .timeline-item__headline) {
    animation: timeline-rise 0.9s var(--theme-ease-signature)
      calc(var(--theme-timeline-delay, 0s) + 1.3s) backwards;
  }

  .timeline-item.is-in .timeline-item__media {
    animation: timeline-wipe 1.8s cubic-bezier(0.65, 0, 0.25, 1)
      calc(var(--theme-timeline-delay, 0s) + 1s) backwards;
  }

  @keyframes timeline-rise {
    from {
      opacity: 0;
      translate: 0 0.9rem;
    }
  }

  @keyframes timeline-badge {
    from {
      opacity: 0;
      transform: scale(0.4);
    }
  }

  @keyframes timeline-draw {
    from {
      transform: scaleY(0);
    }
  }

  @keyframes timeline-pop {
    from {
      opacity: 0;
      transform: scale(0);
    }
  }

  @keyframes timeline-wipe {
    0% {
      opacity: 0;
      clip-path: inset(0 100% 0 0);
    }

    60% {
      opacity: 1;
    }

    /* The end is written out: animating to clip-path: none is not interpolated, it
       flips half way (the same as in reveal.css). */
    100% {
      opacity: 1;
      clip-path: inset(0 0 0 0);
    }
  }
}

/* What is still to come: the last entries, each fainter than the one before (the fade is
   set per entry in the template), with an open dot and badge instead of a filled one. */

.timeline-item--future {
  opacity: var(--theme-timeline-fade, 0.5);
}

.timeline-item--future .timeline-item__year {
  border: 1px solid var(--theme-color-primary);
  background: transparent;
  color: var(--theme-color-primary);
}

.timeline-item--future .timeline-item__info::after {
  border: 1px solid var(--theme-color-text-inverse);
  background: transparent;
}

/* Backdrop: the picture or video of the entry in front, dimmed and blurred behind
   everything, fading from one entry to the next. */

.timeline__backdrop {
  position: absolute;
  inset: 0;
  z-index: -1;
}

.timeline__backdrop::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    to bottom,
    var(--theme-color-inverse),
    transparent 30%,
    transparent 70%,
    var(--theme-color-inverse)
  );
}

.timeline__bg {
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity 1.4s var(--theme-ease-signature);
}

.timeline__bg.is-active {
  opacity: 0.32;
}

.timeline__bg :is(img, video) {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: blur(2.25rem) saturate(0.85);
  scale: 1.15;
}

.timeline__bg iframe {
  width: 100%;
  height: 100%;
  border: 0;
  filter: blur(2.25rem);
  scale: 1.3;
}

@media (prefers-reduced-motion: reduce) {
  .timeline__bg {
    transition: none;
  }
}
