:root {
  --motion-ink: var(--pink);
  --motion-edge: var(--cyan);
  --motion-spring: cubic-bezier(.2, 1.28, .38, 1);
}

:root[data-scene="music"] {
  --motion-ink: var(--cyan);
  --motion-edge: var(--violet);
}

:root[data-scene="lab"] {
  --motion-ink: var(--mint);
  --motion-edge: var(--gold);
}

body { transition: background-color .42s ease, color .32s ease; }

/* A soft scan behind the controls, with a separate sweep for scene changes. */
html[data-motion="playing"][data-boot="ready"] #desktop::after {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  height: 22vh;
  height: 22dvh;
  z-index: -1;
  pointer-events: none;
  background: linear-gradient(transparent, color-mix(in srgb, var(--motion-ink) 12%, transparent), transparent);
  animation: ambient-scan 8s linear infinite;
}

#scene-fx {
  position: fixed;
  inset: 0;
  z-index: 8;
  overflow: hidden;
  pointer-events: none;
}

#scene-fx::before, #scene-fx::after {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0;
}

#scene-fx::before {
  background: linear-gradient(107deg,
    transparent 14%, color-mix(in srgb, var(--motion-ink) 24%, transparent) 41%,
    var(--motion-edge) 49%, transparent 50%, var(--motion-ink) 52%, transparent 54%);
}

#scene-fx::after {
  inset: 12px;
  border: 1px solid var(--motion-ink);
  box-shadow: inset 0 0 38px color-mix(in srgb, var(--motion-edge) 32%, transparent);
}

#scene-fx-label {
  position: absolute;
  top: 42%;
  left: 0;
  right: 0;
  width: max-content;
  margin: 0 auto;
  max-width: 92vw;
  padding: .14em .65em;
  translate: 0 -50%;
  color: var(--text);
  background: var(--bg-0);
  background: color-mix(in srgb, var(--bg-0) 90%, transparent);
  border-inline: 3px solid var(--motion-ink);
  box-shadow: 0 0 34px color-mix(in srgb, var(--motion-edge) 33%, transparent);
  font-size: clamp(19px, 3.7vw, 56px);
  font-weight: 700;
  letter-spacing: .07em;
  white-space: nowrap;
  opacity: 0;
}

.scene-enter #scene-fx::before { animation: scene-sweep .88s cubic-bezier(.24,.7,.25,1); }
.scene-enter #scene-fx::after { animation: scene-frame .9s ease-out; }
.scene-enter #scene-fx-label { animation: scene-stamp 1s cubic-bezier(.18,.7,.25,1); }
.theme-enter #scene-fx::after { animation: scene-frame .58s ease-out; }
.scene-enter :is(.path-mark, #scene-label) { animation: signal-in .6s var(--motion-spring); }
.scene-enter .scene-rail button[aria-current="true"] span { animation: index-hit .66s var(--motion-spring); }

@keyframes ambient-scan {
  from { transform: translateY(-100%); }
  to { transform: translateY(100vh); transform: translateY(100dvh); }
}

@keyframes scene-sweep {
  0% { transform: translateX(-115%); opacity: .1; }
  30% { opacity: .62; }
  72% { opacity: .26; }
  100% { transform: translateX(115%); opacity: 0; }
}

@keyframes scene-frame {
  0% { opacity: 0; transform: scale(.96); }
  25% { opacity: .76; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.025); }
}

@keyframes scene-stamp {
  0% { opacity: 0; transform: skewX(-18deg) scale(1.3); clip-path: inset(42% 0 43%); }
  24% { opacity: 1; transform: skewX(0) scale(1); clip-path: inset(0); }
  57% { opacity: 1; transform: scale(1); clip-path: inset(0); }
  100% { opacity: 0; transform: translateX(22px) scale(.96); clip-path: inset(46% 0 46%); }
}

@keyframes signal-in {
  0% { opacity: .2; translate: -18px 0; }
  45% { opacity: 1; translate: 3px 0; }
  100% { translate: 0 0; }
}

@keyframes index-hit {
  0% { transform: scale(.55) rotate(-18deg); }
  46% { transform: scale(1.35) rotate(5deg); }
  100% { transform: none; }
}

/* Boot, command output and photo pages each keep their own rhythm. */
.boot-enter .topbar { animation: chrome-in .64s var(--motion-spring) backwards; }
.boot-enter .terminal-path { animation: signal-in .6s .08s var(--motion-spring) backwards; }
.boot-enter .profile-head { animation: chrome-in .69s .12s var(--motion-spring) backwards; }
.boot-enter .scene-rail { animation: chrome-in .7s .23s var(--motion-spring) backwards; }
.boot-enter .bottom-bar { animation: chrome-in .67s .29s var(--motion-spring) backwards; }

@keyframes chrome-in {
  0% { opacity: 0; translate: 0 -23px; }
  55% { opacity: 1; translate: 0 4px; }
  100% { opacity: 1; translate: 0 0; }
}

.out-enter > * {
  animation: terminal-line .62s var(--motion-spring) backwards;
}
.out-enter > :nth-child(2) { animation-delay: .07s; }
.out-enter > :nth-child(3) { animation-delay: .14s; }
.out-enter > :nth-child(n+4) { animation-delay: .2s; }

@keyframes terminal-line {
  0% { opacity: 0; transform: translate(-17px, 10px) skewX(-6deg); clip-path: inset(0 96% 0 0); }
  37% { opacity: 1; clip-path: inset(0 12% 0 0); }
  65% { transform: translate(3px, 0); clip-path: inset(0); }
  100% { opacity: 1; transform: none; clip-path: inset(0); }
}

.photo-tile { overflow: hidden; overflow: clip; }
.photo-visual { transition: transform .34s var(--motion-spring), filter .32s ease; }
.photo-tile-label { transition: opacity .24s ease, translate .28s var(--motion-spring); translate: 0 7px; }
.photo-tile:is(:hover, :focus-visible) .photo-tile-label { translate: 0 0; }
.photo-grid { transition: opacity .2s ease; }
.photo-tile:nth-child(2) { --photo-delay: 32ms; }
.photo-tile:nth-child(3) { --photo-delay: 64ms; }
.photo-tile:nth-child(4) { --photo-delay: 96ms; }
.photos-enter .photo-visual { animation: photo-unfold 440ms var(--motion-spring) var(--photo-delay, 0ms) backwards; }
.photos-enter #photo-counter { animation: signal-in 440ms var(--motion-spring); }

/* Each clipping window moves while its canvas moves in the opposite direction.
   The photo stays in place as the scan travels, without JS drawing any frames. */
.photo-layer--image { --scan-from: -100%; --scan-to: 0%; }
.photo-layer--image canvas { --scan-from: 100%; }
.photo-layer--glyphs { --scan-from: 0%; --scan-to: 100%; }
.photo-layer--glyphs canvas { --scan-to: -100%; }
.photos-enter[data-photo-mode="photo"] :is(.photo-layer, .photo-layer canvas) {
  animation: photo-scan 420ms cubic-bezier(.36, .13, .42, .92) calc(130ms + var(--photo-delay, 0ms)) backwards;
}
@keyframes photo-scan {
  from { transform: translateY(var(--scan-from)); }
  to { transform: translateY(var(--scan-to)); }
}

@keyframes photo-unfold {
  0% { opacity: .68; transform: translateY(10px) scale(.94); }
  56% { opacity: 1; transform: translateY(-2px) scale(1.016); }
  100% { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: no-preference) {
  /* Match the gallery's character-to-image scan on every entrance. */
  html[data-motion="playing"] .album-enter {
    animation: photo-unfold 440ms var(--motion-spring) backwards;
  }
  html[data-motion="playing"] .album-enter.album-scanning .album-scan-glyphs {
    display: block;
    transform: translateY(100%);
  }
  .album-scan-image { --scan-from: -100%; --scan-to: 0%; }
  .album-scan-image > * { --scan-from: 100%; }
  .album-scan-glyphs { --scan-from: 0%; --scan-to: 100%; }
  .album-scan-glyphs > canvas { --scan-to: -100%; }
  html[data-motion="playing"] .album-enter :is(.album-scan-image, .album-scan-image > *, .album-scan-glyphs, .album-scan-glyphs > canvas) {
    animation: photo-scan 420ms cubic-bezier(.36, .13, .42, .92) 130ms backwards;
  }
  html[data-motion="playing"] .album-enter .album-artwork::before,
  html[data-motion="playing"] .album-enter .album-artwork::after {
    animation: album-disc-in 550ms ease-out backwards;
  }
  @keyframes album-disc-in {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  html[data-motion="playing"][data-boot="ready"] .album-showcase[data-loading="false"] .album-artwork { animation: sleeve-float 5.6s ease-in-out infinite; }
  @keyframes sleeve-float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
  }
  html[data-motion="playing"] .album-print[data-painted] .album-moving-ink { display: block; }
  html[data-motion="playing"] .album-print[data-painted] img { opacity: 0; }
}

/* Controls answer immediately while their surrounding effects finish. */
.terminal { transition: border-color .18s ease, box-shadow .18s ease; }
:is(.topbar nav, .quick-commands, .photo-tools, .photo-playback, .scene-rail, .dialog-bar, .lightbox-controls, .lightbox-zoom, .synth-pads) :is(button, select) {
  transition: background-color .23s ease, color .23s ease, box-shadow .25s ease, transform .25s var(--motion-spring);
}

@media (hover: hover) {
  html[data-motion="playing"] :is(.topbar nav, .quick-commands, .photo-tools, .photo-playback, .scene-rail, .dialog-bar, .lightbox-controls, .lightbox-zoom) :is(button, select):not(:disabled):hover {
    background-color: var(--soft);
    background-color: color-mix(in srgb, var(--motion-ink) 14%, var(--bg-0));
    box-shadow: inset 0 -2px var(--motion-ink), 0 0 18px color-mix(in srgb, var(--motion-ink) 17%, transparent);
    transform: translateY(-2px);
  }
  html[data-motion="playing"] .photo-tile:hover .photo-visual {
    transform: scale(1.055);
    filter: saturate(1.16);
  }
}

html[data-motion="playing"] :is(.topbar nav, .quick-commands, .photo-tools, .photo-playback, .scene-rail, .dialog-bar, .lightbox-controls, .lightbox-zoom, .synth-pads) button:not(:disabled):active {
  transform: translateY(2px) scale(.88);
}

.terminal-caret { box-shadow: 0 0 12px var(--motion-ink); }
.command-entry:focus-within::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: linear-gradient(100deg, transparent 25%, color-mix(in srgb, var(--motion-ink) 13%, transparent) 51%, transparent 68%);
  animation: input-scan 4.5s ease-in-out infinite;
}

@keyframes input-scan {
  0%, 45% { opacity: 0; transform: translateX(-100%); }
  57% { opacity: 1; }
  78%, 100% { opacity: 0; transform: translateX(100%); }
}

.ui-burst {
  position: fixed;
  z-index: 40;
  width: 0;
  height: 0;
  pointer-events: none;
  color: var(--motion-ink);
  font-size: 15px;
  font-weight: 700;
  animation: burst-life .74s ease-out both;
}

.ui-burst::before {
  content: "";
  position: absolute;
  inset: -17px;
  border: 2px solid var(--motion-ink);
  border-radius: 50%;
  box-shadow: 0 0 15px var(--motion-edge);
  animation: burst-ring .74s cubic-bezier(.16,.65,.28,1) both;
}

.ui-burst > span {
  position: absolute;
  translate: -50% -50%;
  text-shadow: 0 0 9px var(--motion-ink);
  animation: burst-glyph .74s cubic-bezier(.16,.69,.26,1) both;
}
.ui-burst > span:nth-child(even) { color: var(--motion-edge); }

@keyframes burst-life {
  0%, 30% { opacity: 1; }
  100% { opacity: 0; }
}

@keyframes burst-ring {
  from { transform: scale(.2); opacity: 1; }
  to { transform: scale(3.5); opacity: 0; }
}

@keyframes burst-glyph {
  from { transform: scale(.5); }
  to { transform: translate(var(--burst-x), var(--burst-y)) rotate(var(--burst-turn)) scale(1.3); }
}

/* Dialog animation leaves the lightbox image's pan and zoom transform alone. */
#file-dialog[open] { animation: file-window-in .58s var(--motion-spring); }
#lightbox[open] { animation: lightbox-in .58s cubic-bezier(.18,.77,.29,1); }
#lightbox[open]::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 8;
  pointer-events: none;
  opacity: 0;
  background: linear-gradient(107deg, transparent 22%, color-mix(in srgb, var(--motion-ink) 25%, transparent) 48%, var(--motion-edge) 51%, transparent 54%);
  animation: scene-sweep .74s ease-out;
}
dialog[open]::backdrop { animation: backdrop-in .45s ease; }
.dialog-content-enter > * { animation: detail-in .52s .09s var(--motion-spring) backwards; }
.original-enter #lightbox-image { animation: original-in .49s ease-out; }
.synth-pads button.is-playing { animation: pad-hit .24s var(--motion-spring); }
#toast.visible { animation: toast-in .5s var(--motion-spring); }

@keyframes file-window-in {
  0% { opacity: .15; scale: .91; translate: 0 21px; }
  55% { opacity: 1; scale: 1.017; translate: 0 -3px; }
  100% { opacity: 1; scale: 1; translate: 0 0; }
}

@keyframes lightbox-in {
  from { opacity: .35; }
  to { opacity: 1; }
}

@keyframes backdrop-in { from { opacity: 0; } to { opacity: 1; } }

@keyframes detail-in {
  from { opacity: .28; translate: -10px 4px; }
  to { opacity: 1; translate: 0 0; }
}

@keyframes original-in {
  from { opacity: .3; clip-path: inset(0 95% 0 0); }
  to { opacity: 1; clip-path: inset(0); }
}

@keyframes pad-hit {
  0% { transform: scale(.87); }
  48% { transform: scale(1.1); }
  100% { transform: none; }
}

@keyframes toast-in {
  0% { opacity: 0; scale: .78; translate: 0 18px; }
  58% { opacity: 1; scale: 1.03; translate: 0 -3px; }
  100% { opacity: 1; scale: 1; translate: 0 0; }
}

@supports (overlay: auto) and (transition-behavior: allow-discrete) {
  :is(#file-dialog, #lightbox) {
    transition: opacity .22s ease, display .22s allow-discrete, overlay .22s allow-discrete;
  }
  :is(#file-dialog, #lightbox):not([open]) { opacity: 0; pointer-events: none; }
  :is(#file-dialog, #lightbox):not([open])::backdrop { pointer-events: none; }
}
