@import url('https://fonts.googleapis.com/css2?family=Enriqueta:wght@400;500;600;700&display=swap');
@import 'normalize.css';

@property --sleep {
  inherits: true;
  syntax: '<number>';
  initial-value: 0;
}

*,
*:after,
*:before {
  box-sizing: border-box;
}

:root {
  --sleep-buffer: 200;
}

[data-theme='light'],
html {
  --strength: 0.15;
  color-scheme: light only;
}

@media (prefers-color-scheme: dark) {
  html {
    --strength: 1;
    color-scheme: dark only;
  }
}

[data-theme='dark'] {
  --strength: 1;
  color-scheme: dark only;
}

.word {
  white-space: nowrap;
}

header img {
  position: absolute;
  top: 0%;
  z-index: -1;
  left: 0;
  width: 80vh;
  min-width: 500px;
  translate: -30% 0%;
  aspect-ratio: 1;
  -o-object-fit: cover;
     object-fit: cover;
  filter: grayscale(1) blur(0px);
  -webkit-mask: linear-gradient(-45deg, #0000, #000, #0000),
    linear-gradient(-90deg, #0000, #000, #0000),
    radial-gradient(circle at center, #000, hsl(0 0% 100% / 0.5), #0000 70%);
          mask: linear-gradient(-45deg, #0000, #000, #0000),
    linear-gradient(-90deg, #0000, #000, #0000),
    radial-gradient(circle at center, #000, hsl(0 0% 100% / 0.5), #0000 70%);
  -webkit-mask-composite: source-in, xor;
          mask-composite: intersect;
}

body {
  display: flex;
  flex-direction: column;
  place-items: center;
  min-height: 100vh;
  font-family: 'Enriqueta', serif;
  overflow-x: hidden;
  background: linear-gradient(
    -30deg,
    transparent,
    hsl(0 0% 0% / var(--strength, 0.05)) 10% 15%,
    transparent 30% 50%,
    hsl(0 0% 0% / var(--strength, 0.05)) 70% 75%,
    transparent
  ), canvas;
  background-attachment: fixed;
}

body::before {
  --size: 40px;
  --line: color-mix(in lch, canvasText, transparent 85%);
  content: '';
  height: 100vh;
  width: 100vw;
  position: fixed;
  background: linear-gradient(
        90deg,
        var(--line) 1px,
        transparent 1px var(--size)
      )
      50% 50% / var(--size) var(--size),
    linear-gradient(var(--line) 1px, transparent 1px var(--size)) 50% 50% /
      var(--size) var(--size);
  -webkit-mask: linear-gradient(-25deg, transparent 30%, white);
          mask: linear-gradient(-25deg, transparent 30%, white);
  top: 0;
  transform-style: flat;
  pointer-events: none;
  z-index: -1;
}

main {
  width: 100%;
}

header {
  min-height: 10vh;
  display: flex;
  place-items: center;
  width: 58ch;
  text-align: right;
  max-width: calc(100% - 2rem);
  padding: 1rem;
  position: relative;
}

header h1 {
  width: auto;
  font-size: clamp(2.5rem, 4vw + 1rem, 8rem);
  font-weight: 400;
  margin: 0;
  text-wrap: balance;
  display: grid;
  translate: 0 -0.4em;
  line-height: 1;
  color: rgb(31 41 55);
}

header h1 span {
  font-size: 0.4em;
  font-weight: 600;
  font-family: 'SF Pro Text', 'SF Pro Icons', 'AOS Icons', 'Helvetica Neue',
    Helvetica, Arial, sans-serif, system-ui;
  color: rgb(20 184 166);
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme='light']) header img {
    filter: grayscale(1) blur(0);
  }
}

section:first-of-type p {
  max-width: calc(100vw - 2rem);
  width: 34ch;
  text-align: center;
  font-size: clamp(1.5rem, 2.25vw + 1rem, 6rem);
  font-weight: 400;
}

section:last-of-type {
  height: 100vh;
  display: grid;
  place-items: center;
  font-size: clamp(2.5rem, 6vw + 1rem, 8rem);
}

p {
  margin: 0;
}

.squisher {
  display: inline-block;
  position: relative;
  line-height: 0.85;
}

.squisher > [aria-hidden='true'] {
  position: absolute;
  inset: 0;
}

.squisher span:nth-of-type(1) {
  transform-origin: 50% 100%;
}

.squisher span:nth-of-type(2) {
  transform-origin: 50% 0;
}

.squisher span:nth-of-type(3) {
  color: transparent;
}

section:first-of-type div {
  position: sticky;
  height: 100vh;
  top: 0;
  display: grid;
  place-items: center;
}

footer {
  padding: 2rem;
}

.bear-link {
  color: canvasText;
  position: fixed;
  top: 1rem;
  left: 1rem;
  width: 48px;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  opacity: 0.95;
}

:where(.x-link, .bear-link):is(:hover, :focus-visible) {
  opacity: 1;
}

.bear-link svg {
  width: 75%;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

.char {
  display: inline-block;
}

div.tp-dfwv {
  position: fixed;
}

@supports (animation-timeline: scroll()) {
  [data-entity='character'] section:first-of-type {
    /* min-height: 200vh; */
    view-timeline: --section;
    height: calc(
      100vh + (var(--scroll-padding) * 2px) + (var(--sleep-buffer) * 1px) +
        (
          (
              var(--char-total) *
                (var(--pixels-per-character) * var(--delay-multiplier))
            ) * 1px
        )
    );
  }

  [data-entity='word'] section:first-of-type {
    /* min-height: 200vh; */
    view-timeline: --section;
    height: calc(
      100vh + (var(--scroll-padding) * 2px) + (var(--sleep-buffer) * 1px) +
        (
          (
              var(--word-total) *
                (var(--pixels-per-character) * var(--delay-multiplier))
            ) * 1px
        )
    );
  }

  [data-entity='character'] .char {
    /* To calculate a delay, use "sin" against the entire range of the start? */
    /* calc(sin((var(--index) / 12) * 45deg) * 0.475s); */
    --start: calc(
      100vh + (var(--scroll-padding) * 1px) +
        (
          (
              sin((var(--char-index) / var(--char-total)) * 45deg) *
                (
                  var(--char-total) *
                    (var(--pixels-per-character) * var(--delay-multiplier))
                )
            ) * 1px
        )
    );
    --end: calc(var(--start) + (var(--pixels-per-character) * 1px));
    -webkit-animation: drop both linear;
            animation: drop both linear;
    animation-timeline: --section;
    animation-range: var(--start) var(--end);
  }

  [data-entity='word'] .word {
    /* To calculate a delay, use "sin" against the entire range of the start? */
    /* calc(sin((var(--index) / 12) * 45deg) * 0.475s); */
    display: inline-block;
    --start: calc(
      100vh + (var(--scroll-padding) * 1px) +
        (
          (
              sin((var(--word-index) / var(--word-total)) * 45deg) *
                (
                  var(--word-total) *
                    (var(--pixels-per-character) * var(--delay-multiplier))
                )
            ) * 1px
        )
    );
    --end: calc(var(--start) + (var(--pixels-per-character) * 1px));
    -webkit-animation: drop both linear;
            animation: drop both linear;
    animation-timeline: --section;
    animation-range: var(--start) var(--end);
  }

  @-webkit-keyframes drop {
    0% {
      translate: calc(var(--x, 0) * 1%) calc(var(--y, 0) * 1%);
      filter: blur(calc(var(--blur, 6) * 1px));
      opacity: 0;
    }
  }

  @keyframes drop {
    0% {
      translate: calc(var(--x, 0) * 1%) calc(var(--y, 0) * 1%);
      filter: blur(calc(var(--blur, 6) * 1px));
      opacity: 0;
    }
  }

  section:has(.squisher) {
    view-timeline-name: --squisher;
  }

  .squisher span:nth-of-type(2) {
    --from: 1 1;
    --to: 1 0;
  }

  .squisher span:nth-of-type(1) {
    --from: 1 0;
    --to: 1 1;
  }

  .squisher span[aria-hidden='true'] {
    -webkit-animation: scale both ease-in-out;
            animation: scale both ease-in-out;
    animation-timeline: --squisher;
    animation-range: entry calc(100% - 10vh) entry 100%;
  }

  @-webkit-keyframes scale {
    to {
      scale: 10;
    }
  }

  @keyframes scale {
    to {
      scale: 10;
    }
  }

  @keyframes scale {
    0% {
      scale: var(--from);
    }

    100% {
      scale: var(--to);
    }
  }

  header {
    view-timeline-name: --header;
  }

  .header__content {
    translate: 0 calc(50vh - 50%);
  }

  header :where(img, h1) {
    -webkit-animation: fade-away both linear;
            animation: fade-away both linear;
    animation-timeline: --header;
    animation-range: exit 100% exit 150%;
  }

  body {
    timeline-scope: --section;
  }

  header img {
    --y: 0%;
    position: fixed;

    -webkit-animation: fade-away both linear;

            animation: fade-away both linear;
    animation-timeline: --section;
    animation-range: entry 90% entry 120%;
  }

  @-webkit-keyframes fade-away {
    to {
      transform: translateY(var(--y, -50%));
      opacity: 0;
      filter: grayscale(1) blur(10px);
    }
  }

  @keyframes fade-away {
    to {
      transform: translateY(var(--y, -50%));
      opacity: 0;
      filter: grayscale(1) blur(10px);
    }
  }

  .word:last-of-type {
    position: relative;
  }

  [data-entity='word'] .words {
    --sleep-start: calc(
      100vh + (var(--scroll-padding) * 1px) +
        (var(--pixels-per-character) * 1px) +
        (
          sin(45deg) *
            (
              var(--word-total) *
                (var(--pixels-per-character) * var(--delay-multiplier))
            ) * 1px
        )
    );
  }

  [data-entity='character'] .words {
    --sleep-start: calc(
      100vh + (var(--scroll-padding) * 1px) +
        (var(--pixels-per-character) * 1px) +
        (
          sin(45deg) *
            (
              var(--char-total) *
                (var(--pixels-per-character) * var(--delay-multiplier))
            ) * 1px
        )
    );
  }

  .words {
    -webkit-animation: sleep both steps(1);
            animation: sleep both steps(1);
    animation-timeline: --section;
    animation-range: var(--sleep-start)
      calc(var(--sleep-start) + (var(--sleep-buffer) * 1px));
  }

  @-webkit-keyframes sleep {
    100% {
      --sleep: 1;
    }
  }

  @keyframes sleep {
    100% {
      --sleep: 1;
    }
  }

  .zzz {
    transition: opacity 0.5s;
    opacity: var(--sleep);
  }

  [data-entity='character'] .word:not(:last-of-type),
  [data-entity='word'] .word:not(:last-of-type) .char {
    transition: opacity 0.5s;
    opacity: calc(1 - (var(--sleep) * 0.75));
  }

  .word:last-of-type {
    position: relative;
  }

  .z {
    content: 'Z';
    position: absolute;
    bottom: 2ex;
    font-size: 0.5em;
    color: red;
    left: 100%;
    font-weight: 900;
    font-family: sans-serif;
    transform-origin: 0 100%;
    -webkit-animation: snore 4s calc(var(--i) * (4 / 3) * -1s) ease-in-out infinite;
            animation: snore 4s calc(var(--i) * (4 / 3) * -1s) ease-in-out infinite;
  }

  .z:nth-of-type(1) {
    --r: 10deg;
  }

  .z:nth-of-type(2) {
    --r: -5deg;
  }

  .z:nth-of-type(3) {
    --r: -10deg;
  }

  @-webkit-keyframes snore {
    0% {
      scale: 0;
    }

    25% {
      scale: 1;
      translate: 0 -50%;
      rotate: var(--r, 20deg);
    }

    85% {
      opacity: 1;
    }

    100% {
      rotate: var(--r, 20deg);
      opacity: 0;
      scale: 1;
      translate: 0 -50%;
      transform: translateX(220%);
    }
  }

  @keyframes snore {
    0% {
      scale: 0;
    }

    25% {
      scale: 1;
      translate: 0 -50%;
      rotate: var(--r, 20deg);
    }

    85% {
      opacity: 1;
    }

    100% {
      rotate: var(--r, 20deg);
      opacity: 0;
      scale: 1;
      translate: 0 -50%;
      transform: translateX(220%);
    }
  }
}

::view-transition-old(root) {
  -webkit-animation: none;
          animation: none;
  z-index: -1;
}

::view-transition-new(root) {
  -webkit-animation: unclip 1s;
          animation: unclip 1s;
  -webkit-clip-path: polygon(-100vmax 100%, 100% 100%, 100% -100vmax);
          clip-path: polygon(-100vmax 100%, 100% 100%, 100% -100vmax);
}

@-webkit-keyframes unclip {
  0% {
    -webkit-clip-path: polygon(100% 100%, 100% 100%, 100% 100%);
            clip-path: polygon(100% 100%, 100% 100%, 100% 100%);
  }
}

@keyframes unclip {
  0% {
    -webkit-clip-path: polygon(100% 100%, 100% 100%, 100% 100%);
            clip-path: polygon(100% 100%, 100% 100%, 100% 100%);
  }
}