/* Homepage motion layer. Loaded only on the homepage (all locales).
   home-motion.js adds .mo to <html>, decorative elements, and the .in / .live state classes.
   Everything that hides or moves content sits inside the no-preference query, so
   reduced-motion visitors get the full page with static visuals. */

/* ---------- static decorations (shown to everyone) ---------- */
.mo-progress { position: fixed; inset: 0 0 auto 0; height: 3px; z-index: 50; pointer-events: none;
  background: linear-gradient(90deg, var(--accent), var(--angle), var(--height));
  transform: scaleX(var(--sp, 0)); transform-origin: 0 50%; }
[dir="rtl"] .mo-progress { transform-origin: 100% 50%; }

.mo .intro { position: relative; isolation: isolate; }
.mo-bg { position: absolute; inset: -20px 0 -10px; overflow: hidden; z-index: -1; pointer-events: none; }
.mo-bg svg { position: absolute; width: var(--s); height: var(--s); left: var(--x); top: var(--y); opacity: .55; }
.mo-bg path { fill: var(--c); stroke: var(--c2); stroke-width: 1.5; stroke-linejoin: round; }

.mo .gsec > h2::after { content: ""; display: block; width: 64px; height: 4px; margin-top: 12px; border-radius: 4px;
  background: linear-gradient(90deg, var(--accent), var(--height)); transform-origin: 0 50%; }
[dir="rtl"] .gsec > h2::after { transform-origin: 100% 50%; }

/* TOC scroll-spy */
.mo .toc a { position: relative; transition: color .2s; }
.mo .toc a.on { color: var(--height); font-weight: 700; }

/* timeline progress line */
.mo .timeline { position: relative; }
.mo .timeline::before { content: ""; position: absolute; left: -2px; top: 0; width: 2px; height: 100%;
  background: linear-gradient(var(--accent), var(--height)); transform: scaleY(var(--p, 0)); transform-origin: 50% 0; }
.mo[dir="rtl"] .timeline::before { left: auto; right: -2px; }
.mo .timeline li::before { transition: background .3s, transform .3s, box-shadow .3s; }
.mo .timeline li.on::before { background: var(--accent); box-shadow: 0 0 0 5px var(--accent-soft); }

/* stepper connector */
.mo .stepper li::after { content: ""; position: absolute; left: 31px; top: 49px; bottom: -11px; width: 2px;
  background: linear-gradient(var(--accent), transparent); opacity: .35; }
.mo .stepper li:last-child::after { display: none; }
.mo[dir="rtl"] .stepper li::after { left: auto; right: 31px; }

/* what-is: flipping pieces */
.mo .flip { transform-box: view-box; stroke-width: 1.5; stroke-linejoin: round; }
.mo .flip.f1 { transform-origin: 95px 130px; fill: rgba(14, 165, 233, .38); stroke: var(--rect); }
.mo .flip.f2 { transform-origin: 255px 130px; fill: rgba(15, 157, 118, .36); stroke: var(--angle); }

/* unit: counted squares */
.mo .cells rect { rx: 3; }
.mo .cells .c-in { fill: var(--accent); fill-opacity: .2; }
.mo .cells .c-out { fill: var(--rect); fill-opacity: .07; }

/* formula cards: pointer tilt + glare */
.mo .fitem { position: relative; transform-style: preserve-3d; }
.mo .fitem::after { content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; opacity: 0;
  background: radial-gradient(260px circle at var(--gx, 50%) var(--gy, 0%), var(--accent-soft), transparent 70%);
  transition: opacity .3s; }
.mo .fitem:hover::after { opacity: 1; }

/* calculator mode cards: rotating gradient rim */
@property --ang { syntax: "<angle>"; inherits: false; initial-value: 0deg; }
.mo .cmode { position: relative; isolation: isolate; }
.mo .cmode::before { content: ""; position: absolute; inset: -1px; border-radius: inherit; padding: 1.5px; z-index: -1;
  background: conic-gradient(from var(--ang), var(--accent), var(--angle), var(--height), var(--accent));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0); -webkit-mask-composite: xor; mask-composite: exclude;
  opacity: 0; transition: opacity .3s; }
.mo .cmode:hover::before, .mo .cmode:focus-visible::before { opacity: 1; }

/* FAQ plus turns into a cross */
.mo .faq summary::after { content: "+" !important; transition: transform .3s; }
.mo .faq details[open] summary::after { transform: translateY(-50%) rotate(45deg); }
.mo .faq details { transition: border-color .2s, box-shadow .2s; }
.mo .faq details[open] { border-color: var(--accent); }

/* ---------- motion ---------- */
@media (prefers-reduced-motion: no-preference) {
  /* scroll reveal */
  .mo .rv { transition: opacity .7s ease var(--d, 0s), translate .8s cubic-bezier(.2, .7, .2, 1) var(--d, 0s),
    transform .25s ease, border-color .2s, box-shadow .25s; }
  .mo .rv:not(.in) { opacity: 0; translate: 0 26px; }

  .mo .gsec > h2::after { transform: scaleX(0); transition: transform .9s cubic-bezier(.2, .7, .2, 1) .15s; }
  .mo .gsec > h2.in::after { transform: scaleX(1); }

  .mo-bg svg { animation: drift var(--t) ease-in-out var(--dl) infinite alternate; }
  @keyframes drift {
    from { transform: translate(0, 0) rotate(0deg); }
    to { transform: translate(var(--dx), var(--dy)) rotate(var(--r)); }
  }

  /* SVG draw-in */
  .mo svg .draw { stroke-dasharray: 1 1; stroke-dashoffset: 1; transition: stroke-dashoffset 1.3s cubic-bezier(.6, .1, .2, 1) var(--sd, 0s), fill-opacity .8s ease calc(var(--sd, 0s) + .8s); fill-opacity: 0; }
  .mo .in svg .draw, .mo svg.in .draw { stroke-dashoffset: 0; fill-opacity: 1; }
  .mo svg .fade { opacity: 0; transition: opacity .6s ease calc(var(--sd, 0s) + 1s); }
  .mo .in svg .fade, .mo svg.in .fade { opacity: 1; }

  /* marching height lines (only while on screen) */
  .mo .d-h { animation: ants20 1.4s linear infinite paused; }
  .mo .m-h { animation: ants14 1.2s linear infinite paused; }
  .mo .d-box, .mo .d-circ { animation: ants22 2.4s linear infinite paused; }
  .mo .live .d-h, .mo .live .m-h, .mo .live .d-box, .mo .live .d-circ { animation-play-state: running; }
  @keyframes ants20 { to { stroke-dashoffset: -20; } }
  @keyframes ants14 { to { stroke-dashoffset: -14; } }
  @keyframes ants22 { to { stroke-dashoffset: -22; } }

  /* vertices breathe */
  .mo .d-dot { transform-box: fill-box; transform-origin: center; animation: dot 2.6s ease-in-out infinite paused; }
  .mo .live .d-dot { animation-play-state: running; }
  .mo .d-dot:nth-of-type(2n) { animation-delay: -.9s; }
  @keyframes dot { 50% { transform: scale(1.35); } }
  .mo .d-in { transform-box: fill-box; transform-origin: center; animation: incircle 3.2s ease-in-out infinite paused; }
  .mo .live .d-in { animation-play-state: running; }
  @keyframes incircle { 50% { fill-opacity: .55; } }

  /* what-is: pieces flip into the triangle and back */
  .mo .flip { animation: flip 7s cubic-bezier(.65, 0, .35, 1) infinite paused; }
  .mo .flip.f2 { animation-delay: .35s; }
  .mo .live .flip { animation-play-state: running; }
  @keyframes flip {
    0%, 14% { transform: rotate(0deg); }
    36%, 64% { transform: rotate(180deg); }
    86%, 100% { transform: rotate(0deg); }
  }

  /* unit: squares count in, hold, reset */
  .mo .cells rect { transform-box: fill-box; transform-origin: center; animation: cell 6s ease infinite paused; animation-delay: calc(var(--i) * 70ms); }
  .mo .live .cells rect { animation-play-state: running; }
  @keyframes cell {
    0% { opacity: 0; transform: scale(.4); }
    10%, 78% { opacity: 1; transform: scale(1); }
    92%, 100% { opacity: 0; transform: scale(.4); }
  }

  /* in words: each word lights up its symbol in turn */
  .mo .wm-words span, .mo .wm-syms span { transition: none; animation: wmw 7.5s ease infinite paused; animation-delay: calc(var(--i) * 1.2s); }
  .mo .wm-syms span { animation-name: wms; display: inline-block; }
  .mo .wm-arrows i { overflow: visible; }
  .mo .wm-arrows i::before { content: ""; position: absolute; left: -3px; top: 0; width: 8px; height: 8px; border-radius: 50%;
    background: var(--accent); opacity: 0; animation: wma 7.5s ease infinite paused; animation-delay: calc(var(--i) * 1.2s); }
  .mo .live .wm-words span, .mo .live .wm-syms span, .mo .live .wm-arrows i::before { animation-play-state: running; }
  @keyframes wmw { 0%, 14%, 100% { background: var(--panel-2); border-color: var(--line); }
    3%, 10% { background: var(--accent-soft); border-color: var(--accent); } }
  @keyframes wma { 0% { opacity: 0; transform: translateY(0); } 4% { opacity: 1; } 9% { opacity: 1; transform: translateY(20px); } 11%, 100% { opacity: 0; transform: translateY(20px); } }
  @keyframes wms { 0%, 8%, 20%, 100% { transform: scale(1); text-shadow: none; } 11% { transform: scale(1.35); text-shadow: 0 0 18px var(--accent-soft); } }

  /* steppers: badges pop with a ring */
  .mo .stepper li.in::before { animation: pop .6s cubic-bezier(.3, 1.6, .5, 1) calc(var(--d, 0s) + .2s) both, ring 2.4s ease-out calc(var(--d, 0s) + .8s) 1; }
  @keyframes pop { from { transform: scale(0); } to { transform: scale(1); } }
  @keyframes ring { from { box-shadow: 0 0 0 0 var(--accent-soft); } 60% { box-shadow: 0 0 0 12px rgba(0, 0, 0, 0); } to { box-shadow: 0 0 0 0 rgba(0, 0, 0, 0); } }
  .mo .stepper li::after { transform: scaleY(0); transform-origin: 50% 0; transition: transform .8s ease calc(var(--d, 0s) + .5s); }
  .mo .stepper li.in::after { transform: scaleY(1); }

  /* decision chart: a pulse runs down each branch to its formula */
  .mo .flow-col .arrow::before { content: ""; position: absolute; left: -3px; top: -4px; width: 8px; height: 8px; border-radius: 50%;
    background: var(--height); opacity: 0; animation: drop 6s ease-in infinite paused; animation-delay: calc(var(--i) * 1.5s); }
  .mo .flow-col .a, .mo .flow-col .q { animation: glow 6s ease infinite paused; animation-delay: calc(var(--i) * 1.5s + .45s); }
  .mo .flow-col .q { animation-name: glowq; animation-delay: calc(var(--i) * 1.5s); }
  .mo .flow-start { animation: breathe 3s ease-in-out infinite paused; }
  .mo .live .flow-col .arrow::before, .mo .live .flow-col .a, .mo .live .flow-col .q, .mo .live .flow-start { animation-play-state: running; }
  @keyframes drop { 0% { opacity: 0; transform: translateY(0); } 2% { opacity: 1; } 8% { opacity: 1; transform: translateY(22px); } 10%, 100% { opacity: 0; transform: translateY(22px); } }
  @keyframes glow { 0%, 22%, 100% { box-shadow: none; transform: none; } 4%, 14% { box-shadow: 0 0 0 3px var(--accent), 0 10px 24px -10px var(--accent); transform: translateY(-2px); } }
  @keyframes glowq { 0%, 14%, 100% { border-color: var(--line); } 2%, 10% { border-color: var(--height); } }
  @keyframes breathe { 50% { transform: scale(1.05); } }

  /* class ladder: bars rise from the floor */
  .mo .ladder li { clip-path: inset(100% 0 0 0 round 12px 12px 4px 4px); transition: clip-path .9s cubic-bezier(.2, .7, .2, 1) var(--d, 0s); }
  .mo .ladder.in li { clip-path: inset(0 0 0 0 round 12px 12px 4px 4px); }
  .mo .ladder li:nth-child(5) { background: linear-gradient(120deg, var(--accent), var(--accent-2), var(--accent)); background-size: 200% 100%; animation: sheen 4s linear infinite; }
  @keyframes sheen { to { background-position: -200% 0; } }

  /* formula mini diagrams breathe on hover */
  .mo .fitem:hover .m-fill { animation: fillpulse 1.6s ease-in-out infinite; }
  @keyframes fillpulse { 50% { fill-opacity: .45; } }
  .mo .fitem .n { transition: transform .4s cubic-bezier(.3, 1.6, .5, 1), background .3s, color .3s; }
  .mo .fitem:hover .n { transform: rotate(360deg) scale(1.15); background: var(--accent); color: #fff; }

  .mo .cmode::before { animation: spin 3s linear infinite; }
  @keyframes spin { to { --ang: 360deg; } }
  .mo .cmode:hover { transform: translateY(-4px); }

  /* tables: rows slide in */
  .mo .table-wrap.rv tbody tr { transition: opacity .5s ease, translate .6s cubic-bezier(.2, .7, .2, 1); transition-delay: calc(var(--i, 0) * 45ms + .2s); }
  .mo .table-wrap.rv:not(.in) tbody tr { opacity: 0; translate: 0 10px; }
  .mo .guide tbody tr { transition: background .2s; }
  .mo .guide tbody tr:hover { background: var(--accent-soft); }

  /* facts list: rows cascade */
  .mo .facts dt, .mo .facts dd { transition: opacity .5s ease, translate .6s ease; transition-delay: calc(var(--i, 0) * 80ms + .25s); }
  .mo .facts:not(.in) dt, .mo .facts:not(.in) dd { opacity: 0; translate: 0 8px; }

  /* worked examples: lines type in */
  .mo .example ol li { transition: opacity .45s ease, translate .5s ease; transition-delay: calc(var(--i, 0) * 120ms + .3s); }
  .mo .example:not(.in) ol li { opacity: 0; translate: 0 8px; }

  /* FAQ answers slide open */
  .mo .faq details[open] .ans { animation: faqin .35s ease; }
  @keyframes faqin { from { opacity: 0; transform: translateY(-6px); } }
}

@media (max-width: 820px) {
  .mo .ladder li { clip-path: none !important; }
  .mo-bg svg:nth-child(n+5) { display: none; }
}
