/* KIRUPA scroll motion (prototype): the icon layer that /scroll_preview/motion.js builds.
   The layer is fixed behind the gutters for the moving presets and part of the page for "still".
   It sits above the page background (z-index 0, the first thing in <body>), so it shows in every
   style, including Minimal, which paints <body> itself; motion.js keeps every icon out of the
   content column and the rails, so nothing is ever drawn over the words. */

.kmotion {
  --kmotion-op: .22;
  --kmotion-op-k: 1;
  --kmotion-sat: .7;
  --kmotion-line: color-mix(in srgb, var(--primary, #0798ff) 55%, transparent);
  --kmotion-line-w: 1.5px;
  --kmotion-dash: none;
  --kmotion-cap: round;
  --kmotion-curve: 0;
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  contain: strict;
  opacity: 0;
  transition: opacity 1.2s ease;
}
.kmotion.is-ready {
  opacity: 1;
}
/* Still: the icons belong to the page and scroll with it, with no script running. */
.kmotion.is-page {
  position: absolute;
  top: 0;
  right: auto;
  bottom: auto;
  left: 0;
  width: 100%;
}
.kmotion[data-preset="off"] {
  display: none;
}

.kmotion-icon {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--s, 40px);
  height: var(--s, 40px);
  opacity: calc(var(--kmotion-op) * var(--k, 1) * var(--kmotion-op-k));
  transition: opacity .35s ease;
  will-change: transform;
}
.kmotion.is-page .kmotion-icon {
  will-change: auto;
}
.kmotion-icon.is-hidden,
.kmotion-icon.is-missing {
  opacity: 0;
}
.kmotion-icon[hidden] {
  display: none;
}
.kmotion-wander {
  display: block;
  width: 100%;
  height: 100%;
}
.kmotion-icon img {
  display: block;
  width: 100%;
  height: 100%;
  filter: saturate(var(--kmotion-sat));
}
html[data-twemoji="pixel"] .kmotion-icon img {
  image-rendering: pixelated;
}

.kmotion-bonds {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.kmotion[data-lines="off"] .kmotion-bonds {
  display: none;
}
.kmotion-bonds path {
  fill: none;
  stroke: var(--kmotion-line);
  stroke-width: var(--kmotion-line-w);
  stroke-dasharray: var(--kmotion-dash);
  stroke-linecap: var(--kmotion-cap);
}

/* ---------- Dancing (lively: while scrolling; drift: until the reader goes idle) ---------- */

.kmotion[data-dance] .kmotion-icon img {
  animation: var(--dance, kmotion-bob) var(--beat, 1.6s) ease-in-out var(--delay, 0s) infinite alternate paused;
}
.kmotion[data-dance="always"] .kmotion-wander {
  animation: kmotion-wander var(--wander, 9s) ease-in-out var(--delay, 0s) infinite alternate paused;
}
.kmotion[data-dance="scroll"].is-moving .kmotion-icon:not(.is-hidden) img,
.kmotion[data-dance="always"]:not(.is-idle) .kmotion-icon:not(.is-hidden) :is(img, .kmotion-wander) {
  animation-play-state: running;
}

@keyframes kmotion-wander {
  from { transform: translate(-10px, 5px) rotate(-4deg); }
  to { transform: translate(10px, -7px) rotate(4deg); }
}
@keyframes kmotion-bob {
  from { transform: translateY(0); }
  to { transform: translateY(-8px); }
}
@keyframes kmotion-sway {
  from { transform: rotate(-9deg); }
  to { transform: rotate(9deg); }
}
@keyframes kmotion-hop {
  from { transform: translateY(0) scale(1.05, .95); }
  to { transform: translateY(-12px) scale(.97, 1.03); }
}
@keyframes kmotion-jelly {
  0% { transform: scale(1, 1); }
  40% { transform: scale(1.12, .9); }
  70% { transform: scale(.92, 1.08); }
  100% { transform: scale(1.03, .97); }
}
@keyframes kmotion-shimmy {
  0% { transform: translateX(-4px) rotate(-3deg); }
  50% { transform: translateX(4px) rotate(3deg); }
  100% { transform: translateX(-2px) rotate(-1deg); }
}

/* ---------- Each style draws its lines its own way ---------- */

html.theme_dark .kmotion {
  --kmotion-op-k: 1.15;
  --kmotion-line: color-mix(in srgb, var(--primary, #0798ff) 62%, transparent);
}
/* Pixel (no style class) and Plakat: dotted lines on whole pixels. */
html:not(.style_nature):not(.style_scifi):not(.style_minimal):not(.style_ukiyoe) .kmotion {
  --kmotion-line-w: 2px;
  --kmotion-dash: 2 4;
  --kmotion-cap: butt;
}
html:not(.style_nature):not(.style_minimal):not(.style_ukiyoe) .kmotion-bonds path {
  shape-rendering: crispEdges;
}
/* Smooooth: soft, slightly curved, like vines or hyphae. */
html.style_nature .kmotion {
  --kmotion-line-w: 1.75px;
  --kmotion-curve: .14;
  --kmotion-sat: .75;
}
/* Sci-Fi: thin dashed traces in the frame color. */
html.style_scifi .kmotion {
  --kmotion-line: color-mix(in srgb, var(--primaryDarker, #0267af) 55%, transparent);
  --kmotion-line-w: 1px;
  --kmotion-dash: 6 3;
  --kmotion-cap: butt;
  --kmotion-sat: .8;
}
html.style_scifi.theme_dark .kmotion {
  --kmotion-line: color-mix(in srgb, var(--primaryLighter, #aadcff) 60%, transparent);
}
/* Minimal: hairlines in the text color; the art is already one ink. */
html.style_minimal .kmotion {
  --kmotion-line: color-mix(in srgb, var(--bodyText, #373d42) 34%, transparent);
  --kmotion-line-w: 1px;
  --kmotion-op-k: .9;
  --kmotion-sat: 1;
}
html.style_minimal.theme_dark .kmotion {
  --kmotion-op-k: 1;
}
/* Ukiyo-e: a light brushed ink curve. */
html.style_ukiyoe .kmotion {
  --kmotion-line: color-mix(in srgb, var(--bodyText, #2a2622) 42%, transparent);
  --kmotion-line-w: 1.25px;
  --kmotion-curve: .1;
}

/* ---------- Never move for readers who ask for less motion, never print, never fight contrast themes ---------- */

@media (prefers-reduced-motion: reduce) {
  .kmotion,
  .kmotion * {
    animation: none !important;
    transition: none !important;
  }
}
@media print {
  .kmotion {
    display: none !important;
  }
}
@media (forced-colors: active) {
  .kmotion {
    display: none !important;
  }
}
