/* Orbit
   A container lit by a light: a beam that travels around its border, a light
   under the pointer, or a gradient ring that turns.

   <div class="orbit">Any content</div>

   CSS only, no extra elements. Configure it with the custom properties in the
   first rule, set on the element itself. The light under the pointer also needs
   pointer.js, which tells the stylesheet where the pointer is. */

/* The clock: it runs from 0 to 1 once per cycle. The beam's place on the border,
   the ring's angle and the fill all read it, so they keep step and stop together. */
@property --orbit-t { syntax: '<number>'; inherits: true; initial-value: 0 }
/* Fades the light in and out when it shows only on hover. */
@property --orbit-lit { syntax: '<number>'; inherits: true; initial-value: 1 }

.orbit {
  /* Shape */
  --orbit-width: 1.5px; /* border width, which is also the thickness of the light on it */
  --orbit-radius: 16px; /* corner radius */
  --orbit-length: 180px; /* length of the beam along the border, or width of the light under the pointer */

  /* Colour */
  --orbit-from: #7a2fd6; /* colour at the trailing side of the beam, or the left of the container */
  --orbit-to: #e0457b; /* colour at the leading side of the beam, or the right of the container */
  --orbit-line: light-dark(#1f17261f, #f1edf724); /* border colour away from the light */
  --orbit-face: light-dark(#fff, #17131f); /* background of the container; it must be opaque */

  /* Strength of the light in each place, 0 to 1 */
  --orbit-border: 1; /* on the border */
  --orbit-glow: 0.12; /* inside the container */
  --orbit-outer: 0; /* outside it; pointer and ring lights only */
  --orbit-reach: 24px; /* how far the light outside reaches */
  --orbit-fill: 0.16; /* of the fill, with data-fill */

  /* Motion */
  --orbit-cycle: 6000ms; /* time for one lap, one turn of the ring, or one pass of the fill */
  --orbit-fade: 320ms; /* fade-in and fade-out time when the light shows only on hover */

  /* The light is visible by default. Declared here as well as registered above,
     for a browser without registered properties. */
  --orbit-lit: 1;
  /* Where the pointer is, from 0 to 1 across and down. pointer.js writes these. */
  --pointer-x: 0.5;
  --pointer-y: 0;

  /* The covers that keep the light off the border and the inside. Both are laid
     over the whole container at the lesser of the two, and each place then gets
     only what it still lacks, so no light leaks where the two meet. */
  --_both: min(1 - var(--orbit-border), 1 - var(--orbit-glow));
  --_ring: calc((1 - var(--orbit-border) - var(--_both)) / max(0.001, 1 - var(--_both)));
  --_inside: calc((1 - var(--orbit-glow) - var(--_both)) / max(0.001, 1 - var(--_both)));
  --_cover:
    linear-gradient(color-mix(in srgb, var(--orbit-face) calc(var(--_inside) * 100%), transparent) 0 0) padding-box,
    linear-gradient(color-mix(in srgb, var(--orbit-face) calc(var(--_both) * 100%), transparent) 0 0) border-box;
  /* The reach counts only when there is light outside to reach anywhere. */
  --_reach: calc(var(--orbit-reach) * min(1, var(--orbit-outer) * 1000));

  position: relative;
  isolation: isolate;
  box-sizing: border-box;
  border-radius: var(--orbit-radius);
  background: var(--orbit-face);
  /* Cuts the light at the container's edge, and keeps it from adding to the
     scrolling area of whatever the container is in. */
  overflow: clip;
  transition: --orbit-lit var(--orbit-fade) ease;
  animation: orbit-clock var(--orbit-cycle) linear infinite;
}
@keyframes orbit-clock {
  to { --orbit-t: 1 }
}
.orbit[data-direction='reverse'] { animation-direction: reverse }

/* The light. As a beam it is a soft round spot, as wide as the beam is long,
   whose centre rides the border. The part of it over the border is the beam, and
   because it is round the beam bends with the corners. On square corners the
   path is still rounded a little, so the spot turns the corner instead of flipping. */
.orbit::before {
  content: '';
  position: absolute;
  z-index: -2;
  top: 0;
  left: 0;
  width: var(--orbit-length);
  aspect-ratio: 1;
  background: linear-gradient(to right in oklab, var(--orbit-from) 20%, var(--orbit-to) 80%);
  -webkit-mask: radial-gradient(closest-side, #000 12%, transparent);
  mask: radial-gradient(closest-side, #000 12%, transparent);
  opacity: var(--orbit-lit);
  offset-path: inset(calc(var(--orbit-width) / 2) round max(var(--orbit-radius) - var(--orbit-width) / 2, var(--orbit-length) / 8));
  offset-anchor: 50% 50%;
  offset-distance: calc(var(--orbit-t) * 100%);
}
/* Anticlockwise: the colours are mirrored so the leading colour still leads. */
.orbit[data-direction='reverse']::before {
  background: linear-gradient(to left in oklab, var(--orbit-from) 20%, var(--orbit-to) 80%);
}

/* The face: the container's background again, over the light, letting through
   the share each strength asks for. The border's own line is drawn on top, so it
   shows whatever the covers do. The border is drawn inside the container's edge
   and takes no room. */
.orbit::after {
  content: '';
  position: absolute;
  z-index: -1;
  inset: 0;
  border: var(--orbit-width) solid color-mix(in srgb, var(--orbit-face) calc(var(--_ring) * 100%), transparent);
  border-radius: inherit;
  background: var(--_cover);
  outline: var(--orbit-width) solid var(--orbit-line);
  outline-offset: calc(-1 * var(--orbit-width));
}

/* ── Pointer and ring: the light is a layer that does not move. It covers the
   container and, with a light outside, a margin around it as wide as the reach.
   The light is painted to half the reach past the container's edge and the layer
   is blurred, so it fades to nothing at the reach with no edge of its own. A
   mask, applied after the blur, keeps it whole over the container and lets
   through outside only the share --orbit-outer asks for. */
/* The margin takes the reach as it is given: this property does not take a calculation. */
.orbit[data-light] { overflow-clip-margin: var(--orbit-reach) }
.orbit[data-light]::before {
  --_region:
    linear-gradient(#000 0 0) content-box,
    linear-gradient(rgb(0 0 0 / var(--orbit-outer)) 0 0) border-box;
  inset: calc(-1 * var(--_reach));
  box-sizing: border-box;
  width: auto;
  aspect-ratio: auto;
  border: calc(var(--_reach) / 2) solid transparent;
  padding: calc(var(--_reach) / 2);
  border-radius: calc(var(--orbit-radius) + var(--_reach));
  filter: blur(calc(var(--_reach) * 0.22));
  offset-path: none;
  -webkit-mask: var(--_region);
  mask: var(--_region);
}

/* Ring: the whole border is lit, by a gradient that turns. */
.orbit[data-light='ring']::before {
  background: conic-gradient(from calc(var(--orbit-t) * 360deg) in oklab, var(--orbit-from), var(--orbit-to), var(--orbit-from)) padding-box;
}

/* Pointer: a round light where the pointer is. It shows while the pointer is on
   the container, which pointer.js reports, or while it has the focus, and fades
   out where the pointer left it. */
.orbit[data-light='pointer'] {
  --orbit-lit: 0;
  /* A sideways finger movement places the light. Scrolling up and down and pinch-zoom stay the page's;
     without this the browser takes a sideways touch for its own and the light is lost. */
  touch-action: pan-y pinch-zoom;
}
.orbit[data-light='pointer']::before {
  --_x: calc(var(--_reach) + var(--pointer-x) * (100% - 2 * var(--_reach)));
  --_y: calc(var(--_reach) + var(--pointer-y) * (100% - 2 * var(--_reach)));
  --_spot: radial-gradient(calc(var(--orbit-length) / 2) circle at var(--_x) var(--_y), #000 12%, transparent);
  background: linear-gradient(to right in oklab, var(--orbit-from), var(--orbit-to)) padding-box;
  -webkit-mask: var(--_spot), var(--_region);
  -webkit-mask-composite: source-in, source-over;
  mask: var(--_spot), var(--_region);
  mask-composite: intersect, add;
}
.orbit[data-light='pointer']:is([data-pointer], :focus-within, .is-lit) { --orbit-lit: 1 }

/* None: no light at all, for a container that only carries a fill. */
.orbit[data-light='none']::before { display: none }

/* ── Fill: something moving inside the container, over its face. */
/* Flow: the two colours drift across the face without a seam. */
.orbit[data-fill='flow']::after {
  --_f: color-mix(in srgb, var(--orbit-from) calc(var(--orbit-fill) * 100%), transparent);
  --_t: color-mix(in srgb, var(--orbit-to) calc(var(--orbit-fill) * 100%), transparent);
  background:
    linear-gradient(100deg in oklab, var(--_f), var(--_t), var(--_f)) calc(var(--orbit-t) * 200%) 0 / 200% 100% repeat-x padding-box,
    var(--_cover);
}
/* Sweep: a band of light crosses the face in the first part of the cycle, and the rest of it is a pause. */
.orbit[data-fill='sweep']::after {
  --_s: color-mix(in srgb, var(--orbit-to) calc(var(--orbit-fill) * 100%), transparent);
  background:
    linear-gradient(105deg, transparent 38%, var(--_s) 50%, transparent 62%) calc(100% - min(1, var(--orbit-t) / 0.55) * 100%) 0 / 300% 100% no-repeat padding-box,
    var(--_cover);
}

/* ── Plays only on hover, on press and on focus, and holds its place in between.
   The light under the pointer has nothing to play unless there is a fill. */
.orbit[data-flow='hover'] { --orbit-lit: 0 }
.orbit:is([data-flow='hover'], [data-light='pointer']) { animation-play-state: paused }
@media (hover: hover) {
  .orbit[data-flow='hover']:hover { --orbit-lit: 1 }
  .orbit:is([data-flow='hover'], [data-light='pointer']):hover { animation-play-state: running }
}
.orbit[data-flow='hover']:is(:active, :focus-within, .is-lit) { --orbit-lit: 1 }
.orbit:is([data-flow='hover'], [data-light='pointer']):is(:active, :focus-within, .is-lit, [data-pointer]) { animation-play-state: running }

/* Held where it is, from script or markup. This comes last and wins over hover and focus. */
.orbit.orbit.orbit[data-paused] { animation-play-state: paused }

/* Without path support the beam is not drawn, and the border stays plain. */
@supports not (offset-path: inset(0 round 1px)) {
  .orbit:not([data-light])::before { display: none }
}
/* Reduced motion: the clock stops. The beam rests on the top edge, the ring does
   not turn and the fill stays where it is. The light under the pointer still follows it. */
@media (prefers-reduced-motion: reduce) {
  .orbit { --orbit-t: 0.08; animation: none }
}
@media (forced-colors: active) {
  .orbit { background: Canvas; outline: var(--orbit-width) solid CanvasText; outline-offset: calc(-1 * var(--orbit-width)) }
  .orbit::before,
  .orbit::after { display: none }
}
