/*
 * hyd-desktop-effects.css - wallpaper effects, sticker motion and the rotating wallpaper layer.
 *
 * The CSS half of three features whose logic is in JS: hyd-desktop-effects.js (wallpaper effects; it sets
 * data-wallpaper-effect, data-wallpaper-renderer, data-wallpaper-motion and data-desktop-motion on the html
 * element), hyd-sticker-turntables.js (souvenir stickers turned through pre-rendered frames) and
 * hyd-wallpaper-rotation.js (the cross-fading wallpaper layer). Effects touch only the wallpaper: interface
 * surfaces and text never animate here.
 *
 * Loaded after hyd-stickers.css and the desktop sheets. Motion is switched off by prefers-reduced-motion and by
 * body.motion-off; !important appears only where a rule must beat the per-element animation declarations.
 */

/* Wallpaper-only effects: interface surfaces and text remain completely still. */
#hyd-wallpaper-effect {
  position: fixed;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  z-index: -1;
  pointer-events: none;
}
#hyd-wallpaper-effect[hidden] {
  display: none;
}

/* Dreamy effect, CSS renderer: a slow drift of the wallpaper layer (body::before, whose image is
   var(--scene)). The canvas and WebGL renderers draw into #hyd-wallpaper-effect instead. */
html[data-wallpaper-effect='dreamy'][data-wallpaper-renderer='css'] body::before {
  background-image:
    repeating-linear-gradient(0deg,#00000005 0 1px,transparent 1px 4px),var(--scene);
  animation: wallpaper-soft-drift 24s ease-in-out infinite alternate;
  animation-play-state: running;
  will-change: transform;
}

/* The script sets data-wallpaper-motion to paused when the effect must not run. !important wins
   over the running state set above. */
html[data-wallpaper-motion='paused'] body::before {
  animation-play-state: paused !important;
}
@keyframes wallpaper-soft-drift {
  from {
    transform: scale(1.01) translate(-0.4%, 0.2%);
  }
  to {
    transform: scale(1.035) translate(0.4%, -0.2%);
  }
}

/* Personalise dialog: the Picture effect fieldset (original, infrared, MacPaint, ASCII). */
#desktop-personalise .wallpaper-effect-options {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 15px;
  margin: 10px 0 14px;
  padding: 10px;
  border: 1px solid var(--paper-edge);
  border-radius: 2px;
}
#desktop-personalise .wallpaper-effect-options legend {
  font-weight: 700;
  padding: 0 5px;
}
#desktop-personalise .wallpaper-effect-options label {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}
#desktop-personalise .wallpaper-effect-options small {
  flex-basis: 100%;
  color: var(--paper-muted);
  font: 11px/1.4 var(--desktop-font);
}

/* Sticker orbit: a sticker circles a small box (--sticker-orbit-x and --sticker-orbit-y set the
   size, in px); hover, drag and rotate pause it. */
#world #home .desktop-sticker.illustrated-sticker[data-motion='orbit'] .desktop-sticker-art {
  animation: sticker-orbit 9s linear infinite;
}
#world #home .desktop-sticker.illustrated-sticker .desktop-sticker-art {
  animation-delay: var(--sticker-delay,0s);
}
#world
  #home
  .desktop-sticker.illustrated-sticker:is(:hover, .is-dragging, .is-rotating)
  .desktop-sticker-art {
  animation-play-state: paused;
}
#world #home .sticker-preview-art[data-motion='orbit'] {
  animation: sticker-orbit 9s linear infinite;
}
#world #home .desktop-sticker[data-default-sticker='true'] {
  --sticker-orbit-x: 4px;
  --sticker-orbit-y: 4px;
}
@keyframes sticker-orbit {
  0%,
  100% {
    translate: var(--sticker-orbit-x,12px) 0;
  }
  25% {
    translate: 0 var(--sticker-orbit-y,8px);
  }
  50% {
    translate: calc(-1 * var(--sticker-orbit-x,12px)) 0;
  }
  75% {
    translate: 0 calc(-1 * var(--sticker-orbit-y,8px));
  }
}

/* Pause every sticker animation while the tab is hidden, the OS asks for reduced motion or motion
   is off (the script sets data-desktop-motion). !important beats the running animations above. */
html[data-desktop-motion='paused']
  #world
  #home
  :is(.desktop-sticker-art, .sticker-preview-art, .sticker-turntable),
body.motion-off #world #home :is(.desktop-sticker-art, .sticker-preview-art, .sticker-turntable) {
  animation-play-state: paused !important;
}

/* Reduced motion: no wallpaper drift and no sticker animation. !important overrides the animation
   set by the effect and sticker rules above. */
@media (prefers-reduced-motion: reduce) {
  html[data-wallpaper-effect] body::before {
    animation: none !important;
    will-change: auto;
  }
  #world #home :is(.desktop-sticker-art, .sticker-preview-art, .sticker-turntable) {
    animation: none !important;
  }
}

/* 48 views rendered around each model's world Z axis: no rotation of the flat image. */
#world #home :is(.desktop-sticker-art, .sticker-preview-art) {
  position: relative;
}
#world #home .sticker-turntable {
  display: none;
  position: absolute;
  inset: 0;
  background-size: 800% 600%;
  background-repeat: no-repeat;
  pointer-events: none;
}

/* A turntable runs as soon as data-turn-state is ready (the frame sheet has loaded) and hides the
   flat image; while loading or failed the flat image stays visible. */
#world #home [data-motion='turn'][data-turn-state='ready'] > .sticker-turntable {
  display: block;
  animation: sticker-turntable var(--sticker-turn-duration,4s) steps(1,end) infinite;
  animation-delay: var(--sticker-delay,0s);
}
#world #home [data-motion='turn'][data-turn-state='ready'] > img {
  visibility: hidden;
}
#world
  #home
  .desktop-sticker.illustrated-sticker:is(:hover, .is-dragging, .is-rotating)
  .sticker-turntable {
  animation-play-state: paused;
}

/* Turntable frames: 48 steps, one per rendered view, on a sheet of 8 columns by 6 rows (x steps of
   100/7 percent, y steps of 20 percent). The 100 percent keyframe returns to the first view for a
   seamless loop; the animation uses steps(1,end) so each frame is held. */
@keyframes sticker-turntable {
  0.000000% {
    background-position: 0% 0%;
  }
  2.083333% {
    background-position: 14.285714% 0%;
  }
  4.166667% {
    background-position: 28.571429% 0%;
  }
  6.250000% {
    background-position: 42.857143% 0%;
  }
  8.333333% {
    background-position: 57.142857% 0%;
  }
  10.416667% {
    background-position: 71.428571% 0%;
  }
  12.500000% {
    background-position: 85.714286% 0%;
  }
  14.583333% {
    background-position: 100% 0%;
  }
  16.666667% {
    background-position: 0% 20%;
  }
  18.750000% {
    background-position: 14.285714% 20%;
  }
  20.833333% {
    background-position: 28.571429% 20%;
  }
  22.916667% {
    background-position: 42.857143% 20%;
  }
  25.000000% {
    background-position: 57.142857% 20%;
  }
  27.083333% {
    background-position: 71.428571% 20%;
  }
  29.166667% {
    background-position: 85.714286% 20%;
  }
  31.250000% {
    background-position: 100% 20%;
  }
  33.333333% {
    background-position: 0% 40%;
  }
  35.416667% {
    background-position: 14.285714% 40%;
  }
  37.500000% {
    background-position: 28.571429% 40%;
  }
  39.583333% {
    background-position: 42.857143% 40%;
  }
  41.666667% {
    background-position: 57.142857% 40%;
  }
  43.750000% {
    background-position: 71.428571% 40%;
  }
  45.833333% {
    background-position: 85.714286% 40%;
  }
  47.916667% {
    background-position: 100% 40%;
  }
  50.000000% {
    background-position: 0% 60%;
  }
  52.083333% {
    background-position: 14.285714% 60%;
  }
  54.166667% {
    background-position: 28.571429% 60%;
  }
  56.250000% {
    background-position: 42.857143% 60%;
  }
  58.333333% {
    background-position: 57.142857% 60%;
  }
  60.416667% {
    background-position: 71.428571% 60%;
  }
  62.500000% {
    background-position: 85.714286% 60%;
  }
  64.583333% {
    background-position: 100% 60%;
  }
  66.666667% {
    background-position: 0% 80%;
  }
  68.750000% {
    background-position: 14.285714% 80%;
  }
  70.833333% {
    background-position: 28.571429% 80%;
  }
  72.916667% {
    background-position: 42.857143% 80%;
  }
  75.000000% {
    background-position: 57.142857% 80%;
  }
  77.083333% {
    background-position: 71.428571% 80%;
  }
  79.166667% {
    background-position: 85.714286% 80%;
  }
  81.250000% {
    background-position: 100% 80%;
  }
  83.333333% {
    background-position: 0% 100%;
  }
  85.416667% {
    background-position: 14.285714% 100%;
  }
  87.500000% {
    background-position: 28.571429% 100%;
  }
  89.583333% {
    background-position: 42.857143% 100%;
  }
  91.666667% {
    background-position: 57.142857% 100%;
  }
  93.750000% {
    background-position: 71.428571% 100%;
  }
  95.833333% {
    background-position: 85.714286% 100%;
  }
  97.916667% {
    background-position: 100% 100%;
  }
  100% {
    background-position: 0% 0%;
  }
}

/* Reduced motion: show the still image instead of the turntable. !important beats the ready-state
   rules above. */
@media (prefers-reduced-motion: reduce) {
  #world #home [data-motion='turn'] > .sticker-turntable {
    display: none !important;
    animation: none !important;
  }
  #world #home [data-motion='turn'] > img {
    visibility: visible !important;
  }
}

/* Keep the tray preview in its flex slot; old collage offsets must not clip it. */
#world #home #desk-stickers .sticker-board > .sticker-preview-art {
  inset: auto;
  align-self: center;
  transform: none;
}

/* The souvenirs sit directly on the photograph; controls appear only on selection. */
/* !important group: the themed window and button frames from the desktop sheets must not draw a
   background, border or shadow around the art. */
html[data-theme] #world #home .desktop-sticker.illustrated-sticker {
  background: none !important;
  border: 0 !important;
  border-radius: 0;
  box-shadow: none !important;
}

/* Souvenir stickers (images whose path contains hyd-) get a fixed art size: 124 px by default
   (--starter-art-size), 98 px on mid-size or short desktops, 94 px up to 1100 px wide and 76 px on
   phones. */
#world #home .desktop-sticker.illustrated-sticker[data-souvenir-size='true']:has(img[src*='hyd-']) {
  width: var(--starter-art-size,124px);
  height: var(--starter-art-size,124px);
  filter: drop-shadow(0 3px 3px #211e2b55);
}
@media (min-width: 621px) and (max-width: 1350px), (min-width: 621px) and (max-height: 780px) {
  #world
    #home
    .desktop-sticker.illustrated-sticker[data-souvenir-size='true']:has(img[src*='hyd-']) {
    width: 98px;
    height: 98px;
  }
}
@media (min-width: 621px) and (max-width: 1100px) {
  #world
    #home
    .desktop-sticker.illustrated-sticker[data-souvenir-size='true']:has(img[src*='hyd-']) {
    width: 94px;
    height: 94px;
  }
}
@media (max-width: 620px) {
  #world
    #home
    .desktop-sticker.illustrated-sticker[data-souvenir-size='true']:has(img[src*='hyd-']) {
    width: 76px;
    height: 76px;
  }
}

/* Blend adjacent rendered Z-axis views at display cadence, including the loop seam. */
/* !important stops the stepped CSS animation; hyd-sticker-turntables.js then drives the frames and
   cross-fades the next view (.sticker-turntable-next). */
#world #home [data-smooth-turn='true'] > .sticker-turntable {
  animation: none !important;
}
#world #home .sticker-turntable-next {
  opacity: 0;
}

/* Edited frames are prepared once; only opacity animates during a change. */
#hyd-wallpaper-rotation {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
}
#hyd-wallpaper-rotation > img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.8s linear;
}

/* Personalise dialog: the selected wallpapers, one card each (a slot button plus its controls). */
#wallpaper-selected {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0 18px;
}
.wallpaper-selected-item {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  min-width: 0;
  padding: 7px;
  border: 1px solid var(--paper-edge);
  border-radius: 3px;
}
.wallpaper-selected-item > [data-wallpaper-slot] {
  grid-column: 1/-1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  text-align: left;
  padding: 5px;
  font: 12px/1.3 var(--desktop-font);
}
.wallpaper-selected-item img {
  width: 64px;
  height: 48px;
  flex: none;
  object-fit: cover;
}
.wallpaper-selected-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.wallpaper-selected-item > [aria-pressed='true'] {
  outline: 2px solid var(--paper-accent,#6396b5);
  outline-offset: 1px;
}
.wallpaper-selected-item > button {
  min-height: 34px;
}
.wallpaper-rotation-help {
  font: 12px/1.5 var(--desktop-font);
  margin: 6px 0;
}

/* No cross-fade when motion is off or reduced. */
body.motion-off #hyd-wallpaper-rotation > img {
  transition: none;
}
@media (prefers-reduced-motion: reduce) {
  #hyd-wallpaper-rotation > img {
    transition: none;
  }
}

/* Phone: the selected-wallpaper cards stack in one column with 44 px buttons. */
@media (max-width: 620px) {
  #wallpaper-selected {
    grid-template-columns: 1fr;
  }
  .wallpaper-selected-item {
    grid-template-columns: minmax(0, 1fr) auto auto;
  }
  .wallpaper-selected-item > [data-wallpaper-slot] {
    grid-column: auto;
  }
  .wallpaper-selected-item > button {
    min-height: 44px;
  }
}
