/* The public page as a scroll story. Read this with public/site/index.html.

   How it works, in plain words:
   - Each section marked .scene is made tall (two to three screens). Inside it, the .stage sticks to the screen
     while you scroll through the scene, so the page seems to stop and play instead of moving down.
   - Everything on a stage is tied to how far you've scrolled through its scene (CSS "scroll-driven animations":
     animation-timeline). Scroll down and it plays forwards; scroll up and it plays backwards.
   - Most things follow one rhythm: in fast, then a slow push towards you, then out. The keyframes do that by
     spending a short stretch of scroll on the entrance (eased out, so it lands softly), a long stretch on a small
     scale-up, and a short stretch on the exit.

   No JavaScript: the website's security rules allow none, and none is needed.
   Only browsers that support scroll-driven animations get this, and never when the device asks for reduced
   motion. Everyone else gets the still page from site.css, with every word in place. */

@media (prefers-reduced-motion: no-preference) {
  @supports (animation-timeline: view()) {

    /* ------------------------------------------------------------ The frame */

    /* A thin line under the top bar that fills as you read. Black: cinnabar always means AI. */
    .site-top::after {
      content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px;
      background: var(--color-primary); transform-origin: 0 50%; transform: scaleX(0);
      animation: progress linear both; animation-timeline: scroll(root);
    }
    @keyframes progress { to { transform: scaleX(1); } }

    :root { --fit: 1; }
    main { gap: 0; padding-top: 0; }

    /* A scene: tall, and the full width of the window so things can fly in from its edges. Every scene names its
       timeline --scene; whatever is inside follows the nearest one. */
    .scene {
      view-timeline: --scene block;
      height: 280vh;
      width: 100vw;
      margin-left: calc(50% - 50vw);
    }

    /* The stage: one screen, held in place while its scene scrolls past. */
    .stage {
      position: sticky; top: 0;
      overflow: clip;
      display: grid; align-content: center;
      /* Fit to the screen: the stage is laid out as if the screen were bigger by 1 / --fit, then scaled down by
         --fit, so a short or narrow screen shows the whole scene, smaller, instead of cutting it off. --fit is set
         by screen size at the end of this file. Inside a stage, sizes use cqw and cqh (the stage's width and
         height) instead of vw and vh, so every movement shrinks with it. */
      width: calc(100vw / var(--fit));
      height: calc(100vh / var(--fit)); height: calc(100svh / var(--fit));
      scale: var(--fit);
      transform-origin: 0 0;
      container-type: size;
      --pad-top: 88px; --pad-bottom: var(--space-xl);
      padding: calc(var(--pad-top) / var(--fit)) max(var(--space-lg), calc((100vw / var(--fit) - 1200px) / 2)) calc(var(--pad-bottom) / var(--fit));
    }

    /* The rhythm used by most things: in fast, slow push towards you, out.
       The scroll range is set where it's used: from halfway into the scene to halfway out of it. */
    .stage > * {
      animation-timeline: --scene;
      animation-range: entry 50% exit 50%;
      animation-fill-mode: both;
      animation-duration: auto;
    }

    @keyframes in-close-out {
      0%   { opacity: 0; transform: translateY(45cqh) scale(0.9); animation-timing-function: var(--ease-out); }
      16%  { opacity: 1; transform: translateY(0) scale(0.96); animation-timing-function: linear; }
      76%  { opacity: 1; transform: translateY(0) scale(1.05); animation-timing-function: var(--ease-in); }
      100% { opacity: 0; transform: translateY(-75cqh) scale(1.05); }
    }
    @keyframes words-in-out {
      0%   { opacity: 0; transform: translateY(12cqh); animation-timing-function: var(--ease-out); }
      14%  { opacity: 1; transform: translateY(0); animation-timing-function: linear; }
      78%  { opacity: 1; transform: translateY(-2cqh); animation-timing-function: var(--ease-in); }
      100% { opacity: 0; transform: translateY(-30cqh); }
    }

    /* ------------------------------------------------------------ 1. The hero */

    /* At rest it is the page as it always was: the promise and the buttons on the left, the real product on the
       right, all of it readable before you touch anything (on phones, stacked). Motion starts only when you scroll:
       the words step back to the left, the product glides to the middle and comes slowly closer, then rises away,
       the same way every scene leaves. Opacity, position and a little scale only: no tilt, no flourish. */
    .scene-hero { height: 230vh; }
    .scene-hero .hero {
      /* The grid columns and gap come from site.css, unchanged. */
      --content: min(100cqw - 48px, 1200px);
      --to-middle: calc(0.205 * (var(--content) - 56px) + 28px); /* from the right column's centre to the page's */
      align-content: center;
    }
    .scene-hero .hero-words { animation-name: hero-words; animation-range: contain 0% contain 22%; }
    .scene-hero .preview { animation-name: hero-product; animation-range: contain 0% exit 40%; }
    @keyframes hero-words {
      to { opacity: 0; transform: translateX(-10cqw); }
    }
    @keyframes hero-product {
      0%   { transform: none; animation-timing-function: var(--ease-out); }
      30%  { transform: translateX(calc(-1 * var(--to-middle))) scale(1.45); animation-timing-function: linear; }
      72%  { transform: translateX(calc(-1 * var(--to-middle))) scale(1.6); opacity: 1; animation-timing-function: var(--ease-in); }
      100% { transform: translateX(calc(-1 * var(--to-middle))) translateY(-95cqh) scale(1.6); opacity: 0; }
    }

    /* ------------------------------------------------------------ 2. Five AIs, one workspace */

    /* Each person's AI flies in from its own corner, drifts slowly towards the middle, and is pulled into one
       block: the Stoa mark, which then lifts away. Everything shares one spot on the stage. */
    .scene-ais .ais { display: grid; grid-template-areas: "all"; grid-template-rows: minmax(0, 1fr); align-content: stretch; justify-items: center; align-items: center; }
    .scene-ais .ais > * { grid-area: all; }
    .scene-ais h2 { align-self: start; max-width: 18em; text-align: center; font-size: clamp(2rem, 1.2rem + 2.6cqw, 3.25rem); animation-name: words-in-out; }
    .scene-ais > .stage > .quiet { align-self: end; animation-name: words-in-out; }

    .ai-cloud { display: grid; grid-template-areas: "all"; place-items: center; animation: none; }
    .ai-chip {
      grid-area: all;
      animation: gather linear both; animation-timeline: --scene; animation-range: entry 50% exit 50%;
    }
    /* Where each one starts: spread round the middle. */
    .ai-chip:nth-child(1) { --x: -30cqw; --y: -12cqh; }
    .ai-chip:nth-child(2) { --x: 27cqw;  --y: -17cqh; }
    .ai-chip:nth-child(3) { --x: -24cqw; --y: 15cqh; }
    .ai-chip:nth-child(4) { --x: 29cqw;  --y: 13cqh; }
    .ai-chip:nth-child(5) { --x: 2cqw;   --y: 24cqh; }
    @keyframes gather {
      0%   { opacity: 0; transform: translate(calc(var(--x) * 2.2), calc(var(--y) * 2.2)) scale(0.7); animation-timing-function: var(--ease-out); }
      18%  { opacity: 1; transform: translate(var(--x), var(--y)) scale(1); animation-timing-function: linear; }
      58%  { opacity: 1; transform: translate(calc(var(--x) * 0.6), calc(var(--y) * 0.6)) scale(1.06); animation-timing-function: var(--ease-in); }
      74%  { opacity: 0; transform: translate(0, 0) scale(0.4); }
      100% { opacity: 0; transform: translate(0, 0) scale(0.4); }
    }

    .ai-hub {
      flex-direction: column; text-align: center;
      animation-name: hub;
    }
    .ai-hub .mark { width: 72px; height: 86px; }
    @keyframes hub {
      0%, 62% { opacity: 0; transform: scale(0.5); animation-timing-function: var(--ease-out); }
      78%     { opacity: 1; transform: scale(1); animation-timing-function: linear; }
      88%     { opacity: 1; transform: scale(1.04); animation-timing-function: var(--ease-in); }
      100%    { opacity: 0; transform: translateY(-60cqh) scale(1.04); }
    }

    /* ------------------------------------------------------------ 3. The handoff */

    /* The heading slides in from the left; the channel rises in; its three messages arrive one after another as you
       scroll; then the channel comes slowly closer and everything slides away. */
    .scene-handoff .handoff { align-content: center; }
    .scene-handoff h2, .scene-handoff .handoff > .quiet { animation-name: from-left; }
    .scene-handoff .convo { animation-name: in-close-out; transform-origin: 50% 40%; }
    @keyframes from-left {
      0%   { opacity: 0; transform: translateX(-35cqw); animation-timing-function: var(--ease-out); }
      15%  { opacity: 1; transform: translateX(0); animation-timing-function: linear; }
      80%  { opacity: 1; transform: translateX(1cqw); animation-timing-function: var(--ease-in); }
      100% { opacity: 0; transform: translateX(-45cqw); }
    }

    .scene-handoff .thread-item { animation-timeline: --scene; animation-range: entry 50% exit 50%; animation-fill-mode: both; animation-duration: auto; animation-timing-function: linear; }
    .scene-handoff .thread-item:nth-child(1) { animation-name: message-1; }
    .scene-handoff .thread-item:nth-child(2) { animation-name: message-2; }
    .scene-handoff .thread-item:nth-child(3) { animation-name: message-3; }
    @keyframes message-1 { 0%, 18% { opacity: 0; transform: translateY(18px); } 26%, 100% { opacity: 1; transform: none; } }
    @keyframes message-2 { 0%, 32% { opacity: 0; transform: translateY(18px); } 40%, 100% { opacity: 1; transform: none; } }
    @keyframes message-3 { 0%, 46% { opacity: 0; transform: translateY(18px); } 54%, 100% { opacity: 1; transform: none; } }

    /* ------------------------------------------------------------ 4. Three steps, sideways */

    /* The page stops, and the three steps slide past from right to left as you scroll down: in fast, a slow pan
       from the first card to the third, then out to the left. */
    .scene-steps { height: 340vh; }
    .scene-steps .steps {
      --card: min(60cqw, 520px);
      --gap: 32px;
      grid-template-columns: 100%; /* one column, the stage's full width: the row of cards runs off its edge */
      justify-items: start; gap: var(--space-2xl);
      padding-left: 0; padding-right: 0;
    }
    .scene-steps h2 {
      justify-self: center; width: min(100% - 2 * var(--space-lg), 1200px);
      animation-name: words-in-out;
    }
    .scene-steps ol {
      display: flex; gap: var(--gap); width: max-content;
      animation-name: pan;
    }
    .scene-steps li { flex: none; width: var(--card); min-height: 300px; padding: var(--space-xl); box-shadow: var(--shadow-float); }
    .scene-steps h3 { font-size: var(--text-xl); }
    @keyframes pan {
      0%   { opacity: 0; transform: translateX(100cqw); animation-timing-function: var(--ease-out); }
      16%  { opacity: 1; transform: translateX(calc(50cqw - var(--card) / 2)); animation-timing-function: linear; }
      80%  { opacity: 1; transform: translateX(calc(50cqw - var(--card) * 2.5 - var(--gap) * 2)); animation-timing-function: var(--ease-in); }
      100% { opacity: 0; transform: translateX(calc(-3 * var(--card) - 2 * var(--gap))); }
    }

    /* ------------------------------------------------------------ 5. What AIs can and can't see */

    /* The two lists slide in from opposite sides, meet in the middle, come slowly closer, then lift away. */
    .scene-see .see { grid-template-columns: repeat(2, minmax(0, 480px)); justify-content: center; }
    .scene-see .see > .card:nth-of-type(1) { animation-name: from-side; --from: -55cqw; --tilt: -4deg; }
    .scene-see .see > .card:nth-of-type(2) { animation-name: from-side; --from: 55cqw; --tilt: 4deg; }
    @keyframes from-side {
      0%   { opacity: 0; transform: translateX(var(--from)) rotate(var(--tilt)); animation-timing-function: var(--ease-out); }
      18%  { opacity: 1; transform: translateX(0) rotate(0); animation-timing-function: linear; }
      76%  { opacity: 1; transform: scale(1.05); animation-timing-function: var(--ease-in); }
      100% { opacity: 0; transform: translateY(-70cqh) scale(1.05); }
    }

    /* ------------------------------------------------------------ 6. The last word */

    /* Not held: it rises into place as it comes into view, and the mark's side line draws itself down. */
    .install {
      margin-top: var(--space-4xl);
      animation: rise linear both; animation-timeline: view(); animation-range: entry 0% cover 35%;
    }
    @keyframes rise {
      from { opacity: 0; transform: translateY(90px) scale(0.94); animation-timing-function: var(--ease-out); }
      to   { opacity: 1; transform: none; }
    }
    .install-mark .mark-line { animation: draw-down linear both; animation-timeline: view(); animation-range: entry 30% cover 45%; }
    .install-mark .mark-top  { animation: light-up linear both; animation-timeline: view(); animation-range: entry 20% cover 35%; }
    @keyframes draw-down { from { clip-path: inset(0 0 100% 0); } to { clip-path: inset(0); } }
    @keyframes light-up  { from { opacity: 0; } to { opacity: 1; } }

    /* ------------------------------------------------------------ Phones and narrow windows */

    @media (max-width: 960px) {
      .scene { height: 240vh; }
      .scene-steps { height: 300vh; }
      .stage { --pad-top: 76px; --pad-bottom: var(--space-lg); padding-left: var(--space-md); padding-right: var(--space-md); }
      .scene-hero { height: 200vh; }
      .scene-hero .hero { align-content: start; --pad-top: 84px; }
      .scene-hero .hero-words { animation-name: hero-words-up; }
      .scene-hero .preview { animation-name: hero-product-up; }
      .scene-handoff .handoff { grid-template-columns: minmax(0, 1fr); gap: var(--space-md); align-content: center; }
      .scene-steps .steps { --card: 78cqw; --gap: 16px; }
      .scene-steps li { min-height: 0; padding: var(--space-lg); }
      .scene-see .see { grid-template-columns: minmax(0, 1fr); }
      .ai-chip:nth-child(1) { --x: -18cqw; --y: -14cqh; }
      .ai-chip:nth-child(2) { --x: 16cqw;  --y: -6cqh; }
      .ai-chip:nth-child(3) { --x: -14cqw; --y: 4cqh; }
      .ai-chip:nth-child(4) { --x: 14cqw;  --y: 14cqh; }
      .ai-chip:nth-child(5) { --x: -6cqw;  --y: 23cqh; }
    }

    @keyframes hero-words-up {
      to { opacity: 0; transform: translateY(-8cqh); }
    }
    @keyframes hero-product-up {
      0%   { transform: none; animation-timing-function: var(--ease-out); }
      30%  { transform: translateY(-32cqh) scale(1.03); animation-timing-function: linear; }
      72%  { transform: translateY(-32cqh) scale(1.08); opacity: 1; animation-timing-function: var(--ease-in); }
      100% { transform: translateY(-110cqh) scale(1.08); opacity: 0; }
    }

    /* Phones: the stage stops above the Sign up bar held at the bottom, and the busier scenes are set a little
       smaller, so every line still fits on a short screen. */
    @media (max-width: 640px) {
      .stage { --pad-bottom: calc(76px + env(safe-area-inset-bottom) + var(--space-sm)); }
      .scene-ais .ais { gap: var(--space-sm); }
      .scene-ais h2, .scene-handoff h2, .scene-steps h2 { font-size: clamp(1.5rem, 1rem + 2.6cqw, 2rem); }
      .ai-chip { font-size: var(--text-sm); padding: 4px 12px 4px 4px; }
      .ai-chip .face { width: 26px; height: 26px; }

      .scene-handoff .handoff > .quiet { display: none; }
      .scene-handoff .card-h { padding: var(--space-xs) var(--space-md); }
      .scene-handoff .thread { padding: var(--space-sm) var(--space-md) 0; }
      .scene-handoff .thread-item { grid-template-columns: 2.4rem 26px minmax(0, 1fr); gap: var(--space-xs); }
      .scene-handoff .thread-item .face { width: 26px; height: 26px; }
      .scene-handoff .thread-link { padding-bottom: var(--space-sm); }
      .scene-handoff .thread-body { font-size: var(--text-sm); line-height: 1.45; }

      .scene-steps li { gap: var(--space-xs); }
      .scene-steps li p { font-size: var(--text-sm); }

      .scene-see .see { gap: var(--space-sm); align-content: center; }
      .scene-see .see > .card { padding: var(--space-md); }
      .scene-see .see h3 { font-size: var(--text-md); margin-bottom: var(--space-2xs); }
      .scene-see .see li { padding: 6px 0; font-size: var(--text-sm); }
    }

    /* ------------------------------------------------------------ Fit to the screen */

    /* How much each stage is scaled down so a whole scene fits: by the screen's height, and on phones also by its
       width. The scenes are drawn for about 820 px of height on a phone and 760 px on a wider screen; below that,
       everything shrinks a step at a time, never below 60%. */
    @media (max-width: 960px) {
      @media (max-height: 820px) { :root { --fit: 0.92; } }
      @media (max-height: 740px) { :root { --fit: 0.85; } }
      @media (max-height: 680px) { :root { --fit: 0.8; } }
      @media (max-height: 620px) { :root { --fit: 0.74; } }
      @media (max-height: 560px) { :root { --fit: 0.68; } }
      @media (max-height: 500px) { :root { --fit: 0.62; } }
      @media (max-height: 460px) { :root { --fit: 0.6; } }
    }
    @media (max-width: 360px) and (min-height: 741px) { :root { --fit: 0.88; } }
    @media (min-width: 961px) {
      @media (max-height: 760px) { :root { --fit: 0.9; } }
      @media (max-height: 660px) { :root { --fit: 0.82; } }
      @media (max-height: 560px) { :root { --fit: 0.72; } }
    }
  }
}
