/* tilt-card: a card with depth (float, foil, peel). The turn itself is effect.js's (it writes the card's transform and
   --_tx, --_ty, --_on, --_gx, --_gy, --_f); this file holds the knobs and draws what the turn shows: the glare, the
   foil's sheen, the lifted corner, the layers that ride above the card, the shadow that shifts under it. The card's
   own look stays the site's. Every rule sits in :where(), so any rule of the page wins. */

:where([data-fx~="tilt-card"]) {
  /* knobs */
  --tilt-max: 7;                 /* the largest turn in degrees, times the site's --fx-energy (foil turns 60% of it) */
  --tilt-glare: 0.5;             /* 0 to 1: how bright the glare and the sheen are; 0 for none */
  --tilt-depth: 9;               /* px a data-tilt-depth="1" layer shifts at the full turn (2 and 3 shift twice and three times) */
  --tilt-scale: 1.015;           /* float, foil: the card's size while the pointer is on it */
  --tilt-perspective: 900px;     /* float, foil: nearer is more dramatic */
  --tilt-shadow: 0.35;           /* float, foil: 0 to 1, the soft shadow that shifts under the card; 0 for a flat look */
  --tilt-rest: 1200;             /* float, foil: ms after the pointer stops until the card lies flat again (its words read sharp); 0 keeps it turned */
  --tilt-fold: 44;               /* peel: the lifted corner's size in px */
  --tilt-corner: auto;           /* peel: auto (the corner nearest the pointer), or tl, tr, bl, br */
  --tilt-stiffness: 170;         /* the spring's pull (times the site's --fx-pace, squared) */
  --tilt-damping: 20;            /* the spring's brake */
  --tilt-c1: var(--c-brand-light, #cfd8ff);   /* foil: the sheen's three colors */
  --tilt-c2: var(--c-accent, #e6682a);
  --tilt-c3: var(--c-brand, #2456e6);
  position: relative;
  isolation: isolate;
}

/* The layers that ride above the card: they shift with the turn, further the higher they are. */
:where([data-fx~="tilt-card"]) :where([data-tilt-depth]) {
  translate: calc(var(--_tx, 0) * var(--_n, 1) * var(--tilt-depth) * 1px) calc(var(--_ty, 0) * var(--_n, 1) * var(--tilt-depth) * 1px);
}
:where([data-tilt-depth="2"]) { --_n: 2; }
:where([data-tilt-depth="3"]) { --_n: 3; }

/* float, foil: while it turns, a soft shadow under the card shifts away from the pointer (not with --tilt-shadow: 0). */
:where([data-fx~="tilt-card"][data-tilt-on][data-tilt-shadow]) {
  filter: drop-shadow(calc(var(--_tx, 0) * -12px) calc(var(--_ty, 0) * -12px + 10px) 16px color-mix(in srgb, var(--c-ink, #000) calc(var(--tilt-shadow) * var(--_on, 0) * 55%), transparent));
}

/* The glare: light where the pointer is, a little shade at the far corner. */
:where(.fx-tilt__glare, .fx-tilt__fold) { position: absolute; z-index: 5; pointer-events: none; }
:where(.fx-tilt__glare) {
  inset: 0; border-radius: inherit; opacity: var(--_on, 0);
  background:
    radial-gradient(circle farthest-corner at var(--_gx, 50%) var(--_gy, 10%), color-mix(in srgb, var(--c-paper, #fff) calc(var(--tilt-glare) * 34%), transparent), transparent 62%),
    radial-gradient(circle farthest-corner at calc(100% - var(--_gx, 50%)) calc(100% - var(--_gy, 10%)), color-mix(in srgb, var(--c-ink, #000) calc(var(--tilt-glare) * 16%), transparent), transparent 58%);
}
/* The keyboard's focus shows the glare, still. */
:where([data-fx~="tilt-card"]:is(:focus-visible, :has(:focus-visible))) { --_on: 1; }

/* foil: a band of the palette's colors that slides across the card with the turn; a quiet band at rest. */
:where([data-fx~="tilt-card"][data-fx-preset="foil"]) > :where(.fx-tilt__glare) {
  opacity: calc(0.4 + 0.6 * var(--_on, 0));
  background: linear-gradient(112deg, transparent 22%,
    color-mix(in srgb, var(--tilt-c1) calc(var(--tilt-glare) * 44%), transparent) 36%,
    color-mix(in srgb, var(--tilt-c2) calc(var(--tilt-glare) * 38%), transparent) 47%,
    color-mix(in srgb, var(--tilt-c3) calc(var(--tilt-glare) * 34%), transparent) 58%, transparent 72%);
  background-size: 300% 300%;
  background-position: calc(100% - var(--_gx, 34%)) calc(100% - var(--_gy, 34%));
}

/* peel: the corner is cut off the card along the fold (the polygon reaches past the card so its shadow stays), and
   the corner's back lies folded over it. --_f is the fold's size in px. */
:where([data-fx~="tilt-card"][data-fx-preset="peel"][data-tilt-peel]:is([data-tilt-corner="tr"], :not([data-tilt-corner]))) {
  clip-path: polygon(-60px -60px, calc(100% - var(--_f) * 1px - 60px) -60px, calc(100% + 60px) calc(var(--_f) * 1px + 60px), calc(100% + 60px) calc(100% + 60px), -60px calc(100% + 60px));
}
:where([data-fx~="tilt-card"][data-fx-preset="peel"][data-tilt-peel][data-tilt-corner="tl"]) {
  clip-path: polygon(calc(var(--_f) * 1px + 60px) -60px, calc(100% + 60px) -60px, calc(100% + 60px) calc(100% + 60px), -60px calc(100% + 60px), -60px calc(var(--_f) * 1px + 60px));
}
:where([data-fx~="tilt-card"][data-fx-preset="peel"][data-tilt-peel][data-tilt-corner="br"]) {
  clip-path: polygon(-60px -60px, calc(100% + 60px) -60px, calc(100% + 60px) calc(100% - var(--_f) * 1px - 60px), calc(100% - var(--_f) * 1px - 60px) calc(100% + 60px), -60px calc(100% + 60px));
}
:where([data-fx~="tilt-card"][data-fx-preset="peel"][data-tilt-peel][data-tilt-corner="bl"]) {
  clip-path: polygon(-60px -60px, calc(100% + 60px) -60px, calc(100% + 60px) calc(100% + 60px), calc(var(--_f) * 1px + 60px) calc(100% + 60px), -60px calc(100% - var(--_f) * 1px - 60px));
}
:where(.fx-tilt__fold) {
  --_back: color-mix(in srgb, var(--fx-surface) 80%, var(--fx-surface-on));
  --_lit: color-mix(in srgb, var(--fx-surface) 93%, var(--fx-surface-on));
  --_line: color-mix(in srgb, var(--fx-surface) 55%, var(--fx-surface-on));
  top: 0; right: 0; width: max(0px, calc(var(--_f, 0) * 1px - 2 * var(--_bw, 0px))); aspect-ratio: 1;
  clip-path: polygon(0 0, 100% 100%, 0 100%);
  background: linear-gradient(to top right, var(--_back) 0%, var(--_lit) 38%, var(--_back) 47%, var(--_line) 50%);
}
:where([data-tilt-corner="tl"]) > :where(.fx-tilt__fold) { right: auto; left: 0; clip-path: polygon(100% 0, 100% 100%, 0 100%); background-image: linear-gradient(to top left, var(--_back) 0%, var(--_lit) 38%, var(--_back) 47%, var(--_line) 50%); }
:where([data-tilt-corner="br"]) > :where(.fx-tilt__fold) { top: auto; bottom: 0; clip-path: polygon(0 0, 100% 0, 0 100%); background-image: linear-gradient(to bottom right, var(--_back) 0%, var(--_lit) 38%, var(--_back) 47%, var(--_line) 50%); }
:where([data-tilt-corner="bl"]) > :where(.fx-tilt__fold) { top: auto; bottom: 0; right: auto; left: 0; clip-path: polygon(0 0, 100% 0, 100% 100%); background-image: linear-gradient(to bottom left, var(--_back) 0%, var(--_lit) 38%, var(--_back) 47%, var(--_line) 50%); }
/* The keyboard's focus lifts the top right corner a little, still. */
:where([data-fx~="tilt-card"][data-fx-preset="peel"]:is(:focus-visible, :has(:focus-visible)):not([data-tilt-on])) {
  --_f: 24;
  clip-path: polygon(-60px -60px, calc(100% - var(--_f) * 1px - 60px) -60px, calc(100% + 60px) calc(var(--_f) * 1px + 60px), calc(100% + 60px) calc(100% + 60px), -60px calc(100% + 60px));
}

/* A touch screen has no pointer to follow: float and foil tilt a little as the card crosses the screen, and their
   light crosses the card with it (a scroll-driven animation, where the browser has one). */
@media (hover: none) and (prefers-reduced-motion: no-preference) {
  @supports (animation-timeline: view()) {
    :where([data-fx~="tilt-card"]:not([data-fx-preset="peel"])) {
      animation: fx-tilt-scroll linear both;
      animation-timeline: view();
      animation-range: cover 0% cover 100%;
    }
    :where([data-fx~="tilt-card"]:not([data-fx-preset="peel"])) > :where(.fx-tilt__glare) {
      opacity: 0.85;
      background: linear-gradient(180deg, transparent 30%, color-mix(in srgb, var(--c-paper, #fff) calc(var(--tilt-glare) * 26%), transparent) 50%, transparent 70%);
      background-size: 100% 300%;
      animation: fx-tilt-light linear both;
      animation-timeline: view();
      animation-range: cover 0% cover 100%;
    }
    :where([data-fx~="tilt-card"][data-fx-preset="foil"]) > :where(.fx-tilt__glare) {
      background: linear-gradient(168deg, transparent 30%,
        color-mix(in srgb, var(--tilt-c1) calc(var(--tilt-glare) * 44%), transparent) 42%,
        color-mix(in srgb, var(--tilt-c2) calc(var(--tilt-glare) * 38%), transparent) 50%,
        color-mix(in srgb, var(--tilt-c3) calc(var(--tilt-glare) * 34%), transparent) 58%, transparent 70%);
      background-size: 100% 300%;
    }
  }
}
/* it leans as it comes onto the screen and as it leaves, and lies flat (its words sharp) through the middle */
@keyframes fx-tilt-scroll {
  0% { transform: perspective(900px) rotateX(calc(var(--tilt-max) * 0.6deg)); }
  28%, 72% { transform: none; }
  100% { transform: perspective(900px) rotateX(calc(var(--tilt-max) * -0.6deg)); }
}
@keyframes fx-tilt-light {
  from { background-position: 50% 100%; }
  to { background-position: 50% 0%; }
}

/* Reduced motion: nothing turns. A hover or the focus shows the glare, or lifts the corner, still. */
@media (prefers-reduced-motion: reduce) {
  :where([data-fx~="tilt-card"]:is(:hover, :focus-visible, :has(:focus-visible))) { --_on: 1; }
  :where([data-fx~="tilt-card"][data-fx-preset="peel"]:is(:hover, :focus-visible, :has(:focus-visible))) {
    --_f: 24;
    clip-path: polygon(-60px -60px, calc(100% - var(--_f) * 1px - 60px) -60px, calc(100% + 60px) calc(var(--_f) * 1px + 60px), calc(100% + 60px) calc(100% + 60px), -60px calc(100% + 60px));
  }
}
/* Windows' high contrast: no glare and no fold. */
@media (forced-colors: active) {
  :where(.fx-tilt__glare, .fx-tilt__fold) { display: none; }
  :where([data-fx~="tilt-card"]) { clip-path: none; filter: none; }
}
