/*
  "Chaos -> order" story on the home page (BG and EN). Owned by the Senior Engineer (P2).
  Class contract: see README.md, "Home page motion".

  This file starts with the finished, static look: the tidy dashboard. That is what
  people see with reduced motion and in browsers without CSS scroll-driven animations
  (Firefox, older Safari). The scroll animation (flying papers -> dashboard) is below, only
  inside @supports (animation-timeline: view()) and prefers-reduced-motion: no-preference.
*/

.story { padding-block: var(--section-y); }
.story__scene { position: relative; }

.story__board {
  position: relative;
  display: grid;
  gap: var(--s-5);
  padding: clamp(1rem, 0.5rem + 2vw, 2rem);
  border: 1px solid var(--c-line-strong);
  border-radius: var(--radius-lg);
  background: linear-gradient(135deg, rgba(110, 168, 254, 0.1), rgba(167, 139, 250, 0.06)), var(--c-bg-raised);
  box-shadow: var(--shadow-lift);
}
.story__board-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-3); }
.story__board-title { font-weight: 750; font-size: var(--fs-md); }
.story__board-day { color: var(--c-text-muted); }
.story__board-status {
  display: inline-flex;
  align-items: center;
  gap: var(--s-2);
  margin-left: auto;
  padding: 0.2rem 0.75rem;
  border: 1px solid rgba(110, 168, 254, 0.35);
  border-radius: var(--radius-pill);
  background: rgba(110, 168, 254, 0.12);
  color: var(--c-accent-strong);
  font-size: var(--fs-sm);
  font-weight: 650;
}
.story__board-status::before { content: ""; width: 0.5rem; height: 0.5rem; border-radius: 50%; background: currentColor; }

.story__tiles { display: grid; gap: var(--s-3); grid-template-columns: repeat(2, minmax(0, 1fr)); }
.story__tile {
  display: grid;
  gap: var(--s-1);
  align-content: start;
  padding: var(--s-4);
  border: 1px solid var(--c-line);
  border-radius: var(--radius);
  background: var(--c-surface);
}
.story__tile-icon { width: 2.75rem; height: 2.75rem; object-fit: contain; margin-bottom: var(--s-2); }
.story__tile-label { color: var(--c-text-muted); font-size: var(--fs-sm); font-weight: 600; }
.story__tile-value { font-size: var(--fs-xl); font-weight: 750; line-height: var(--lh-tight); letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.story__tile-note { color: var(--c-text-faint); font-size: var(--fs-sm); line-height: var(--lh-snug); }
.story__board-note { color: var(--c-text-faint); font-size: var(--fs-sm); }

@media (min-width: 48rem) {
  .story__tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--s-4); }
  .story__tile { padding: var(--s-5); }
}

/* ---------- Scroll story (P2): the papers fly into their tiles ----------
  Only in browsers with CSS scroll-driven animations, and only without reduced motion.
  Everything else keeps the static board above. No JS.

  The "papers" are the tile icons themselves: each icon starts big, tilted and out of
  place (--fx, --fy, --fr, --fs) and flies home as you scroll, so it always lands exactly
  in its tile at every screen size. Then the tile's text fades in and its frame glows.
  When all six have landed, the status pill lights up. Only transform/opacity animate.

  Two modes:
  - default (phones, short screens): the story plays while the board scrolls through
    the screen (timeline = .story__scene, range "cover").
  - pinned (wide AND tall screens): the board stays put (sticky) while you scroll
    (timeline = .story__track, range "contain").
  Each animated element gets --a / --b: where its step starts and ends, 0-100 of the story.
*/
@supports (animation-timeline: view()) {
  @media (prefers-reduced-motion: no-preference) {
    .story__scene { container-type: inline-size; view-timeline: --story block; }

    .story__tile { position: relative; }
    .story__tile-icon {
      position: relative;
      z-index: 2; /* papers in flight pass over every tile */
      animation: story-fly linear both;
      animation-timing-function: cubic-bezier(0.3, 0.7, 0.2, 1);
    }
    .story__tile-label, .story__tile-value, .story__tile-note { animation: story-fade linear both; }
    .story__tile::after {
      content: "";
      position: absolute;
      inset: -1px;
      border: 1px solid var(--c-accent);
      border-radius: inherit;
      box-shadow: 0 0 1.5rem rgba(110, 168, 254, 0.35), inset 0 0 1.5rem rgba(110, 168, 254, 0.12);
      opacity: 0;
      pointer-events: none;
      animation: story-glow linear both;
    }
    .story__board::after {
      content: "";
      position: absolute;
      inset: -1px;
      border: 1px solid rgba(110, 168, 254, 0.5);
      border-radius: inherit;
      box-shadow: 0 0 3rem rgba(110, 168, 254, 0.25);
      opacity: 0;
      pointer-events: none;
      animation: story-glow linear both;
      --a: 84; --b: 100;
    }
    .story__board-status { animation: story-pop linear both; --a: 80; --b: 88; }

    /* All story animations run on the scroll timeline, over their own --a..--b slice. */
    .story__tile-icon, .story__tile-label, .story__tile-value, .story__tile-note,
    .story__tile::after, .story__board::after, .story__board-status {
      animation-timeline: --story;
      animation-range: cover calc(15% + var(--a) * 0.43%) cover calc(15% + var(--b) * 0.43%);
    }

    /* Order of landing (row by row) and where each step sits in the story. */
    .story__tile--bookings { --i: 0; }
    .story__tile--orders   { --i: 1; }
    .story__tile--messages { --i: 2; }
    .story__tile--invoices { --i: 3; }
    .story__tile--payments { --i: 4; }
    .story__tile--reviews  { --i: 5; }
    .story__tile-icon { --a: calc(var(--i) * 9); --b: calc(var(--i) * 9 + 30); }
    .story__tile-label, .story__tile-value, .story__tile-note { --a: calc(var(--i) * 9 + 26); --b: calc(var(--i) * 9 + 36); }
    .story__tile::after { --a: calc(var(--i) * 9 + 27); --b: calc(var(--i) * 9 + 45); }

    /* Where the papers lie before they fly: a messy pile across the board (2 columns). */
    .story__tile--bookings .story__tile-icon { --fx: 48cqw;  --fy: 38cqw;  --fr: -16deg; --fs: 2.1; }
    .story__tile--orders   .story__tile-icon { --fx: -42cqw; --fy: 72cqw;  --fr: 14deg;  --fs: 2.3; }
    .story__tile--messages .story__tile-icon { --fx: 54cqw;  --fy: -54cqw; --fr: -8deg;  --fs: 2;   }
    .story__tile--invoices .story__tile-icon { --fx: -38cqw; --fy: -32cqw; --fr: 10deg;  --fs: 2.2; }
    .story__tile--payments .story__tile-icon { --fx: 52cqw;  --fy: -10cqw; --fr: -20deg; --fs: 1.9; }
    .story__tile--reviews  .story__tile-icon { --fx: -36cqw; --fy: 20cqw;  --fr: 7deg;   --fs: 1.7; }

    @media (min-width: 48rem) {
      /* 3 columns */
      .story__tile--bookings .story__tile-icon { --fx: 51cqw;  --fy: 19cqw;  --fr: -14deg; --fs: 2.6; }
      .story__tile--orders   .story__tile-icon { --fx: -28cqw; --fy: 13cqw;  --fr: 12deg;  --fs: 2.8; }
      .story__tile--messages .story__tile-icon { --fx: -34cqw; --fy: -3cqw;  --fr: -8deg;  --fs: 2.4; }
      .story__tile--invoices .story__tile-icon { --fx: 77cqw;  --fy: -15cqw; --fr: 9deg;   --fs: 2.6; }
      .story__tile--payments .story__tile-icon { --fx: -14cqw; --fy: 11cqw;  --fr: -18deg; --fs: 2.4; }
      .story__tile--reviews  .story__tile-icon { --fx: -8cqw;  --fy: 15cqw;  --fr: 6deg;   --fs: 2.2; }
    }

    /* Pinned mode: the board stays on screen while the story plays. Needs room for the
       whole board below the header (the board is about 36rem tall on wide screens). */
    @media (min-width: 48rem) and (min-height: 44rem) {
      .story__scene { view-timeline: none; }
      .story__track { view-timeline: --story block; }
      .story__stage { display: flow-root; min-height: 230vh; min-height: 230svh; }
      .story__scene {
        position: sticky;
        top: calc(4.5rem + max(0rem, (100svh - 4.5rem - 36rem) / 2));
      }
      .story__tile-icon, .story__tile-label, .story__tile-value, .story__tile-note,
      .story__tile::after, .story__board::after, .story__board-status {
        animation-range: contain calc(var(--a) * 0.9%) contain calc(var(--b) * 0.9%);
      }
    }
  }
}

@keyframes story-fly {
  from { translate: var(--fx) var(--fy); rotate: var(--fr); scale: var(--fs); }
}
@keyframes story-fade { from { opacity: 0; } }
@keyframes story-glow { 0%, 100% { opacity: 0; } 35% { opacity: 1; } }
@keyframes story-pop  { from { opacity: 0.3; scale: 0.85; } }
