/* CEM II / A-LL interactive widget
   Layout: a fixed 16:9 stage; every size is in cqw (1% of stage width) so the
   whole widget scales as one piece, like the Rive canvas did. */

@property --p {
  syntax: "<percentage>";
  inherits: false;
  initial-value: 0%;
}

.cem {
  --navy: #173671;
  --white: #ffffff;
  --orange: #f2a249;
  --orange-ink: #8a4d12;
  --coral: #d7735d;
  --coral-ink: #6e2416;
  --coral-soft: #fbd2b8;
  --blue: #3b72b6;
  --blue-soft: #7fa8e0;
  --green: #94cd96;
  --ul-a: #3b7ab0;
  --ul-b: #8bc79b;

  --font: "Public Sans", "Helvetica Neue", Arial, sans-serif;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-back: cubic-bezier(0.34, 1.56, 0.64, 1);
  /* After Effects "easy ease" */
  --easy: cubic-bezier(0.33, 0, 0.67, 1);
  /* Gentle start, long soft landing */
  --glide: cubic-bezier(0.45, 0, 0.15, 1);
  /* Damped springs sampled into linear(): one smooth overshoot, then settle */
  --spring: linear(0, 0.047, 0.163, 0.318, 0.487, 0.65, 0.795, 0.914, 1.005, 1.068, 1.107, 1.124, 1.125, 1.115, 1.098, 1.077, 1.056, 1.036, 1.019, 1.006, 0.996, 0.989, 0.985, 0.984, 0.985, 0.986, 0.989, 0.991, 0.994, 0.996, 0.998, 1, 1.001, 1.002, 1.002, 1.002, 1.002, 1.002, 1.001, 1.001, 1);
  --spring-soft: linear(0, 0.033, 0.114, 0.223, 0.342, 0.462, 0.574, 0.675, 0.762, 0.834, 0.891, 0.937, 0.97, 0.994, 1.011, 1.021, 1.026, 1.028, 1.028, 1.026, 1.023, 1.02, 1.016, 1.013, 1.01, 1.007, 1.005, 1.003, 1.002, 1.001, 1, 1, 0.999, 0.999, 0.999, 0.999, 0.999, 0.999, 0.999, 1, 1);
  --ease-in-back: cubic-bezier(0.36, 0, 0.66, -0.56);

  container-type: inline-size;
  width: 100%;
  max-width: 1200px;
  margin-inline: auto;
}

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

.cem__stage {
  position: relative;
  aspect-ratio: 16 / 9;
  max-width: 100%;
  overflow: hidden;
  background: var(--navy);
  color: var(--white);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

/* :where() keeps this reset at element specificity so component classes win */
:where(.cem) button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.cem button:focus-visible {
  outline: 0.3cqw solid var(--white);
  outline-offset: 0.5cqw;
  border-radius: 0.4cqw;
}

.ch {
  display: inline-block;
  white-space: pre;
}

/* ---------- Screens ---------- */

.screen {
  position: absolute;
  inset: 0;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.4s ease, visibility 0s linear 0.4s;
}

.screen.is-active {
  opacity: 1;
  visibility: visible;
  transition: opacity 0.4s ease 0.2s, visibility 0s;
}

/* Elements arrive at their own --t (seconds after the screen opens) */
.screen.is-active .enter {
  animation: enter 0.9s var(--ease) both;
  animation-delay: var(--t, 0.3s);
}

@keyframes enter {
  from {
    opacity: 0;
    transform: translateY(2.5cqw);
  }
}

/* Absolutely placed elements are centred on (--x, --y) */
.at {
  position: absolute;
  left: var(--x);
  top: var(--y);
  translate: -50% -50%;
}

/* ---------- Home: CEM II / A - LL ---------- */

.is-resetting [data-screen="home"] *,
.is-resetting [data-screen="home"] *::before,
.is-resetting [data-screen="home"] *::after {
  transition: none !important;
}

.code {
  position: absolute;
  left: 50%;
  top: 50.5%;
  translate: -50% -50%;
  display: flex;
  align-items: center;
  gap: 4.8cqw;
  font-size: 8.5cqw;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
}

.sep {
  font-weight: 500;
}

.part {
  position: relative;
}

.part__btn {
  position: relative;
  display: block;
  line-height: 1;
  z-index: 1;
}

.part__txt {
  display: inline-block;
}

/* Hover: the letters breathe, the bar underneath stays put */
.part__btn[aria-expanded="false"]:hover .part__txt {
  animation: breathe 2.2s var(--easy) infinite;
}

/* Grow, settle, then rest at normal size for the last third of the loop */
@keyframes breathe {
  0%,
  65%,
  100% {
    transform: none;
  }
  32% {
    transform: scale(1.06);
  }
}

/* Click: letters spread apart and settle back (CEM II, LL) */
.part.is-bump .ch {
  animation: spread 0.45s var(--easy);
}

@keyframes spread {
  50% {
    transform: translateX(calc(var(--o) * 0.55cqw));
  }
}

/* Click: A squashes and stretches instead */
.part--a.is-bump .ch {
  transform-origin: 50% 100%;
  animation: squash 0.45s var(--easy);
}

@keyframes squash {
  35% {
    transform: scale(1.1, 0.9);
  }
  70% {
    transform: scale(0.9, 1.1);
  }
}

/* Highlight behind the letters: a box for CEM II, circles for A and LL */
.part__btn::before {
  content: "";
  position: absolute;
  z-index: -1;
  left: 50%;
  top: 50%;
  translate: -50% -50%;
  transform: scale(0);
  transition: transform 0.4s var(--easy);
}

.part--cem .part__btn::before {
  width: calc(100% + 3cqw);
  height: calc(100% + 2.4cqw);
  border-radius: 0.6cqw;
  background: var(--orange);
  /* grows sideways out of a line in the middle */
  transform: scaleX(0);
}

.part--a .part__btn::before,
.part--ll .part__btn::before {
  width: var(--d);
  height: var(--d);
  border-radius: 50%;
}

.part--a .part__btn::before {
  background: var(--coral);
}

.part--ll .part__btn::before {
  background: var(--blue);
}

/* Shimmering underline */
.part__btn::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 1.5cqw);
  height: 1.9cqw;
  background: linear-gradient(90deg, var(--ul-a), var(--ul-b), var(--ul-a)) 0 0 / 200% 100%;
  animation: shimmer 2.4s linear infinite;
  transition: opacity 0.3s ease;
}

@keyframes shimmer {
  to {
    background-position: -200% 0;
  }
}

[data-open="cem"] .part--cem .part__btn::before,
[data-open="a"] .part--a .part__btn::before,
[data-open="ll"] .part--ll .part__btn::before {
  transform: none;
  transition: transform 0.4s var(--ease-back) 0.05s;
}

[data-open="cem"] .part--cem .part__btn::after,
[data-open="a"] .part--a .part__btn::after,
[data-open="ll"] .part--ll .part__btn::after {
  opacity: 0;
}

/* Callout + the arrow joining it to its highlight.
   --reach is how far the highlight sticks out above the letters, so the arrow
   tail sits on the box or circle. --d is the circle diameter. */
.part--cem { --reach: 1.2cqw; --tone: var(--orange); --ink: var(--orange-ink); }
.part--a   { --d: 10.4cqw; --reach: 0.95cqw; --tone: var(--coral); --ink: var(--coral-ink); }
.part--ll  { --d: 15cqw;   --reach: 3.25cqw; --tone: var(--blue);  --ink: var(--navy); }

.part__arrow {
  position: absolute;
  left: 50%;
  bottom: calc(100% + var(--reach));
  width: 1.6cqw;
  height: 4cqw;
  translate: -50% 0;
  overflow: visible;
  fill: none;
  stroke: var(--tone);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transform: scaleY(0);
  transform-origin: bottom;
  transition: transform 0.25s var(--easy);
}

/* Information boxes: the colour grows sideways from the middle with a small
   bounce, then the text fades in. Used by the callouts and the question boxes. */
.box {
  position: relative;
  isolation: isolate;
}

.box::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: inherit;
  background: var(--tone);
  transform: scaleX(0);
}

.tip {
  position: absolute;
  bottom: calc(100% + var(--reach) + 4cqw + 1.8cqw);
  width: 36cqw;
  padding: 2.2cqw 2.6cqw;
  border-radius: 0.6cqw;
  font-size: 2.7cqw;
  font-weight: 600;
  line-height: 1.38;
  white-space: normal;
  text-align: left;
  cursor: pointer;
  color: transparent;
  visibility: hidden;
  transition: color 0.15s ease, visibility 0s linear 0.3s;
}

.tip::before {
  transition: transform 0.25s var(--easy);
}

.tip em {
  font-style: normal;
  color: transparent;
  transition: color 0.15s ease;
}

.part--cem .tip { left: -6cqw; }
.part--a .tip   { left: calc(50% - 18cqw); }
.part--ll .tip  { right: -13cqw; }

[data-open="cem"] .part--cem .part__arrow,
[data-open="a"] .part--a .part__arrow,
[data-open="ll"] .part--ll .part__arrow {
  transform: none;
  transition-delay: 0.3s;
}

[data-open="cem"] .part--cem .tip,
[data-open="a"] .part--a .tip,
[data-open="ll"] .part--ll .tip {
  color: var(--white);
  visibility: visible;
  transition: color 0.25s ease 0.85s, visibility 0s;
}

[data-open="cem"] .part--cem .tip::before,
[data-open="a"] .part--a .tip::before,
[data-open="ll"] .part--ll .tip::before {
  transform: none;
  transition: transform 0.45s var(--ease-back) 0.45s;
}

[data-open="cem"] .part--cem .tip em,
[data-open="a"] .part--a .tip em,
[data-open="ll"] .part--ll .tip em {
  color: var(--ink);
  transition: color 0.25s ease 0.85s;
}

/* Question row: typed in letter by letter, then the arrow, then the button.
   --n (number of letters) is set by the script. */
.ask {
  --start: 1.15s;
  --step: 40ms;
  --typed: calc(var(--start) + var(--n, 20) * var(--step));
  position: absolute;
  left: 50%;
  top: 84%;
  translate: -50% -50%;
  display: flex;
  align-items: center;
  gap: 2cqw;
  width: max-content;
  font-size: 2.9cqw;
  font-weight: 500;
  white-space: nowrap;
  visibility: hidden;
}

.ask > * {
  flex: none;
}

.ask__arrow {
  width: 4.4cqw;
  height: 1.6cqw;
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ask--a  { color: var(--coral); }
.ask--ll { color: var(--blue-soft); }

.ask .ch,
.ask__arrow,
.ask .glow {
  opacity: 0;
}

[data-open="a"] .ask--a,
[data-open="ll"] .ask--ll {
  visibility: visible;
}

[data-open="a"] .ask--a .ch,
[data-open="ll"] .ask--ll .ch {
  animation: type 0.01s linear both;
  animation-delay: calc(var(--start) + var(--k) * var(--step));
}

@keyframes type {
  to {
    opacity: 1;
  }
}

[data-open="a"] .ask--a .ask__arrow,
[data-open="ll"] .ask--ll .ask__arrow {
  animation: slide-in 0.5s var(--easy) both;
  animation-delay: calc(var(--typed) + 0.15s);
}

@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateX(-1.5cqw);
  }
  to {
    opacity: 1;
  }
}

[data-open="a"] .ask--a .glow,
[data-open="ll"] .ask--ll .glow {
  animation: pop 0.5s var(--ease-back) both, pulse 1.6s var(--easy) infinite;
  animation-delay: calc(var(--typed) + 0.4s), calc(var(--typed) + 0.9s);
}

/* ---------- Glowing "next" button ---------- */

.glow {
  width: 3.6cqw;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 1.4cqw 0.4cqw rgba(176, 222, 150, 0.55);
  animation: pulse 1.6s var(--easy) infinite;
}

/* Continuous 100% ↔ 115% breathing with the glow swelling along */
@keyframes pulse {
  50% {
    scale: 1.15;
    box-shadow: 0 0 2.4cqw 0.9cqw rgba(176, 222, 150, 0.75);
  }
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(0);
  }
  to {
    opacity: 1;
  }
}

/* ---------- Detail screens: subject first, then it moves aside for the question ---------- */

/* A zero-size anchor at the subject's final centre. It starts in the middle of
   the stage (--shift) and slides over once the subject has finished arriving. */
.subject {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: 0;
  height: 0;
  will-change: transform;
}

.screen.is-active .subject {
  animation: aside 0.85s var(--glide) both;
  animation-delay: var(--t);
}

@keyframes aside {
  from {
    transform: translateX(var(--shift));
  }
}

.question {
  position: absolute;
  left: 75.6%;
  top: 19.8%;
  translate: -50% 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.qbox {
  --tone: var(--coral);
  width: 36cqw;
  padding: 2.6cqw 2.4cqw;
  border-radius: 0.5cqw;
  font-size: 2.75cqw;
  font-weight: 600;
  line-height: 1.38;
  text-align: center;
}

.screen.is-active .qbox {
  animation: ink-in 0.3s ease both;
  animation-delay: calc(var(--t) + 0.3s);
}

.screen.is-active .qbox::before {
  animation: box-x 0.8s var(--spring) both;
  animation-delay: var(--t);
}

@keyframes box-x {
  from {
    transform: scaleX(0);
  }
  to {
    transform: none;
  }
}

@keyframes ink-in {
  from {
    color: transparent;
  }
}

.qarrow {
  display: block;
  width: 1.6cqw;
  height: 5cqw;
  overflow: visible;
  fill: none;
  stroke: var(--coral);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.screen.is-active .qarrow {
  animation: grow-down 0.5s var(--easy) both;
  animation-delay: var(--t);
  transform-origin: top;
}

@keyframes grow-down {
  from {
    transform: scaleY(0);
  }
}

.question .glow {
  margin-top: 1.4cqw;
}

.screen.is-active .question .glow {
  animation: pop 0.5s var(--ease-back) both, pulse 1.6s var(--easy) infinite;
  animation-delay: var(--t), calc(var(--t) + 0.5s);
}

/* ---------- Pies ---------- */

.pie {
  --p: var(--val);
  aspect-ratio: 1;
  border-radius: 50%;
  background: conic-gradient(from 180deg, var(--coral) 0 var(--p), var(--green) 0);
}

.screen.is-active .pie {
  animation: pie-in 0.9s var(--spring) both, fade-in 0.25s ease both, fill 0.8s var(--easy) both;
  animation-delay: var(--t, 0.3s), var(--t, 0.3s), calc(var(--t, 0.3s) + 0.2s);
}

/* One continuous spring (the curve does the overshoot), with a quick fade */
@keyframes pie-in {
  from {
    transform: scale(0.4);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

.screen.is-active .bounce {
  animation: pie-in 0.8s var(--spring) both, fade-in 0.25s ease both;
  animation-delay: var(--t, 0.3s);
}

@keyframes fill {
  from {
    --p: 0%;
  }
}

.subject .pie {
  position: absolute;
  left: -12.9cqw;
  top: -12.9cqw;
  width: 25.8cqw;
}

.pie-title {
  position: absolute;
  left: 0;
  top: -20.3cqw;
  translate: -50% -50%;
  font-size: 5.4cqw;
  font-weight: 700;
  line-height: 1;
  color: var(--coral);
}

/* The A grows while rising from the top of the pie to its place */
.screen.is-active .pie-title {
  animation: title-in 0.9s var(--spring) both, fade-in 0.25s ease both;
  animation-delay: var(--t);
}

@keyframes title-in {
  from {
    transform: translateY(7cqw) scale(0.3);
  }
}

/* Labels sit left of the pie. Each one is placed by where its pointer tip
   touches the matching slice, measured from the pie centre. */
.tag {
  --r: 0.4cqw;   /* corner radius */
  --pw: 1.6cqw;  /* pointer width */
  --ph: 1.3cqw;  /* pointer height */
  position: absolute;
  isolation: isolate;
  padding: 1.05cqw 3.2cqw 1.05cqw 2.2cqw;
  font-size: 2.5cqw;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
}

/* Label and pointer are one shape: a single layer clipped to the outline
   (rounded corners approximated with two extra points each), so the
   pointer's edge is exactly the label's edge. The pointer is a right-angled
   wedge that carries the label's right edge on to the pie. */
.tag::before {
  content: "";
  position: absolute;
  z-index: -1;
  background: var(--tone);
}

/* Tip at (-8.9, -9.2) from the pie centre: on the edge of the green area */
.tag--clinker {
  --tone: var(--green);
  --b: calc(100% - var(--ph));
  right: 8.9cqw;
  bottom: 10.5cqw;
  transform-origin: 100% 100%;
}

.tag--clinker::before {
  inset: 0 0 calc(-1 * var(--ph)) 0;
  clip-path: polygon(
    0 var(--r),
    calc(var(--r) * 0.134) calc(var(--r) * 0.5),
    calc(var(--r) * 0.5) calc(var(--r) * 0.134),
    var(--r) 0,
    calc(100% - var(--r)) 0,
    calc(100% - var(--r) * 0.5) calc(var(--r) * 0.134),
    calc(100% - var(--r) * 0.134) calc(var(--r) * 0.5),
    100% var(--r),
    100% 100%,
    calc(100% - var(--pw)) var(--b),
    var(--r) var(--b),
    calc(var(--r) * 0.5) calc(var(--b) - var(--r) * 0.134),
    calc(var(--r) * 0.134) calc(var(--b) - var(--r) * 0.5),
    0 calc(var(--b) - var(--r))
  );
}

/* Tip at (-8.9, 9.2) from the pie centre: on the edge of the coral slice */
.tag--scm {
  --tone: var(--coral);
  display: grid;
  right: 8.9cqw;
  top: 10.5cqw;
  transform-origin: 100% 0;
}

.tag--scm::before {
  inset: calc(-1 * var(--ph)) 0 0 0;
  clip-path: polygon(
    0 calc(var(--ph) + var(--r)),
    calc(var(--r) * 0.134) calc(var(--ph) + var(--r) * 0.5),
    calc(var(--r) * 0.5) calc(var(--ph) + var(--r) * 0.134),
    var(--r) var(--ph),
    calc(100% - var(--pw)) var(--ph),
    100% 0,
    100% calc(100% - var(--r)),
    calc(100% - var(--r) * 0.134) calc(100% - var(--r) * 0.5),
    calc(100% - var(--r) * 0.5) calc(100% - var(--r) * 0.134),
    calc(100% - var(--r)) 100%,
    var(--r) 100%,
    calc(var(--r) * 0.5) calc(100% - var(--r) * 0.134),
    calc(var(--r) * 0.134) calc(100% - var(--r) * 0.5),
    0 calc(100% - var(--r))
  );
}

.tag--scm strong {
  font-size: 3.1cqw;
  font-weight: 700;
  color: var(--coral-soft);
}

.screen.is-active .tag {
  animation: pie-in 0.8s var(--spring) both, fade-in 0.25s ease both;
  animation-delay: var(--t);
}

.abc {
  display: grid;
  justify-items: center;
  gap: 2.6cqw;
}

.abc .pie {
  width: 20.4cqw;
}

.abc__letter {
  font-size: 5.9cqw;
  font-weight: 700;
  line-height: 1;
  color: var(--coral);
}

.abc__pct {
  font-size: 3.8cqw;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

/* The A pie and its letter carry over from the previous screen: they start
   where they were (offsets measured by the script) and shrink into place. */
.carry {
  --dx: 0cqw;
  --dy: 0cqw;
  --s: 1;
}

.screen.is-active .carry {
  animation: carry 0.9s var(--spring-soft) both;
  animation-delay: 0.1s;
}

@keyframes carry {
  from {
    transform: translate(var(--dx), var(--dy)) scale(var(--s));
  }
}

/* No cross-fade into the ABC screen, so the carried pie never doubles up */
.screen.is-active[data-screen="a-abc"] {
  transition: none;
}

[data-view="a-abc"] [data-screen="a-pie"] :is(.pie, .pie-title) {
  visibility: hidden;
}

/* ---------- Back arrow ---------- */

.back {
  position: absolute;
  left: 4%;
  top: 8.7%;
  width: 7cqw;
  height: 4cqw;
  display: grid;
  place-items: center;
  color: var(--ul-a);
  transition: translate 0.3s var(--ease), color 0.2s ease;
}

.back:hover {
  translate: -0.8cqw 0;
  color: var(--blue-soft);
}

.back svg {
  width: 100%;
  height: 1.8cqw;
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Jars ---------- */

.jar {
  position: absolute;
  left: var(--x);
  top: var(--y);
  height: var(--h, 18.6cqw);
  aspect-ratio: 414 / 841;
  margin: 0;
  translate: -50% 0;
  /* keep the big jar images on their own layer while they slide */
  will-change: transform;
}

.jar img {
  display: block;
  width: 100%;
  height: 100%;
}

.badge {
  position: absolute;
  top: 17%;
  right: 0;
  translate: 50% 0;
  min-width: calc(2.3em + 0.6cqw);
  height: calc(2.3em + 0.6cqw);
  padding-inline: 0.55em;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--c);
  color: var(--c-ink, var(--white));
  /* A border (not an outer shadow) so the colour runs under the white ring
     and no hairline of background shows between them */
  border: 0.3cqw solid var(--white);
  font-size: var(--badge, 1.9cqw);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
}

.screen.is-active .badge {
  animation: pop 0.5s var(--ease-back) both;
  animation-delay: calc(var(--t, 0.3s) + 0.5s);
}

/* Jars slide in from the right */
.screen.is-active .jar {
  animation: jar-in 0.9s var(--spring-soft) both, fade-in 0.3s ease both;
  animation-delay: var(--t, 0.3s);
}

@keyframes jar-in {
  from {
    transform: translateX(30cqw);
  }
}

/* The big limestone jar, centred on its .subject anchor */
.jar--hero {
  --h: 36.3cqw;
  --badge: 3.3cqw;
  left: -8.94cqw;
  top: -18.15cqw;
  translate: none;
}

/* ---------- Leaving: pies and jars pop out before going back ---------- */

.screen.is-leaving :is(.abc, .jar, .back) {
  animation: out 0.4s var(--ease-in-back) both, fade-out 0.4s ease-in both;
  animation-delay: calc(var(--i, 0) * 0.05s);
}

/* A small wind-up, then shrink away in one continuous move */
@keyframes out {
  to {
    transform: scale(0);
  }
}

@keyframes fade-out {
  60% {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  .cem *,
  .cem *::before,
  .cem *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    animation-delay: 0s !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0s !important;
  }
}
