@property --liquid-wave {
  syntax: '<length>';
  inherits: false;
  initial-value: 0px;
}

@property --liquid-strength {
  syntax: '<number>';
  inherits: false;
  initial-value: 1;
}

.liquid-capsule {
  position: absolute;
  top: var(--liquid-nav-inset, 0px);
  left: 0;
  height: 44px;
  background: var(--liquid-fill, var(--switch-knob, #000));
  box-shadow: 0 0 0 5px var(--liquid-fill, var(--switch-knob, #000));
  pointer-events: none;
}

.liquid-inverse {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--liquid-ink, var(--switch-track, #fff));
  clip-path: var(--liquid-label-clip, inset(50%));
  pointer-events: none;
}

.liquid-inverse::before { content: attr(data-label); }

:where(button, [data-liquid-control]) {
  position: relative;
  isolation: isolate;
}

:where(button, [data-liquid-control])::before {
  --glass-ink: var(--liquid-glass-ink, var(--liquid-fill, var(--switch-knob, #000)));
  content: '';
  position: absolute;
  z-index: -1;
  inset: 50% 0 auto;
  height: 44px;
  translate: 0 -50%;
  scale: 1 var(--liquid-height-scale, 1);
  border-radius: 999px;
  clip-path: var(--glass-rest, inset(1px 2px round 999px));
  background: linear-gradient(
    155deg,
    color-mix(in srgb, var(--glass-ink) 12%, transparent),
    color-mix(in srgb, var(--glass-ink) 3%, transparent) 55%,
    color-mix(in srgb, var(--glass-ink) 8%, transparent)
  );
  box-shadow: inset 0 0 5px color-mix(in srgb, var(--glass-ink) 12%, transparent);
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease;
}

:where(button, [data-liquid-control]):where(:focus-visible, :active)::before,
.theme-control:has(.theme-state:focus-visible, .theme-state:active) .theme-track::before {
  opacity: 1;
}

@media (hover: hover) {
  :where(button, [data-liquid-control]):hover::before,
  .theme-control:hover .theme-track::before {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: no-preference) {
  :where(button, [data-liquid-control]):focus-visible::before,
  .theme-control:has(.theme-state:focus-visible) .theme-track::before {
    animation: liquid-glass-shape 2.8s ease-in-out infinite;
  }

  :where(button, [data-liquid-control]):active::before,
  .theme-control:has(.theme-state:active) .theme-track::before {
    clip-path: var(--glass-crest, inset(1px 2px round 42% 58% 45% 55%));
  }

  .theme-indicator { animation: liquid-indicator-shape 8.2s ease-in-out infinite; }
}

@media (hover: hover) and (prefers-reduced-motion: no-preference) {
  :where(button, [data-liquid-control]):hover::before,
  .theme-control:hover .theme-track::before {
    animation: liquid-glass-shape 2.8s ease-in-out infinite;
  }
}

[data-liquid] > [aria-current]::before {
  opacity: 0;
  animation: none;
}

:is(button, [data-liquid-control]):not(:where(.theme-track, [data-liquid] > *)) {
  color: var(--liquid-ink, var(--switch-track, #fff));
  text-decoration: none;
}

:where(button, [data-liquid-control]):not(:where(.theme-track, [data-liquid] > *))::before {
  --control-fill: var(--liquid-fill, var(--switch-knob, #000));
  background: var(--control-fill);
  box-shadow: none;
  opacity: 1;
  transition: background-color 180ms ease;
}

:where(button, [data-liquid-control]):not(:where(.theme-track, [data-liquid] > *)):is(:hover, :focus-visible)::before {
  background-color: color-mix(in srgb, var(--control-fill) 88%, var(--shimmer-edge, #A79BBF));
}

@media (prefers-reduced-motion: no-preference) {
  :where(button, [data-liquid-control]):not(:where(.theme-track, [data-liquid] > *))::before {
    animation: liquid-glass-shape 8.2s ease-in-out infinite;
  }
}

@supports (clip-path: shape(from 0 0, line to 100% 100%, close)) {
  @media (prefers-reduced-motion: no-preference) {
    :where(button, [data-liquid-control]):not(:where(.theme-track, [data-liquid] > *))::before {
      --liquid-shoulder: min(27px, calc((100% - 4px) / 3));
      --liquid-drift: calc(var(--liquid-wave) * var(--liquid-strength));
      --liquid-vertical: calc(var(--liquid-drift) * 0.6);
      clip-path: shape(
        from 2px 22px,
        curve to calc(2px + var(--liquid-shoulder) + var(--liquid-drift)) 1px
          with 2px calc(8px - var(--liquid-vertical)) / calc(11px - var(--liquid-drift)) 1px,
        curve to 50% 1px with calc(50% - var(--liquid-shoulder) / 2) 1px / calc(50% - var(--liquid-shoulder) / 2) 1px,
        curve to calc(100% - 2px - var(--liquid-shoulder) + var(--liquid-drift)) 0
          with calc(50% + var(--liquid-shoulder) / 2) 1px / calc(50% + var(--liquid-shoulder) / 2) 0,
        curve to calc(100% - 2px) 22px
          with calc(100% - 10px - var(--liquid-drift)) 0 / calc(100% - 2px) calc(9px + var(--liquid-vertical)),
        curve to calc(100% - 2px - var(--liquid-shoulder) - var(--liquid-drift)) 44px
          with calc(100% - 2px) calc(36px + var(--liquid-vertical)) / calc(100% - 11px + var(--liquid-drift)) 44px,
        curve to 50% 44px with calc(50% + var(--liquid-shoulder) / 2) 44px / calc(50% + var(--liquid-shoulder) / 2) 44px,
        curve to calc(2px + var(--liquid-shoulder) - var(--liquid-drift)) 43px
          with calc(50% - var(--liquid-shoulder) / 2) 44px / calc(50% - var(--liquid-shoulder) / 2) 43px,
        curve to 2px 22px with calc(10px + var(--liquid-drift)) 43px / 2px calc(36px - var(--liquid-vertical)),
        close
      );
      animation: liquid-control-wave 8.2s ease-in-out infinite;
      transition: background-color 180ms ease, --liquid-strength 520ms ease;
    }

    :where(button, [data-liquid-control]):not(:where(.theme-track, [data-liquid] > *)):is(:hover, :focus-visible)::before {
      --liquid-strength: 1.65;
    }
  }
}

@keyframes liquid-control-wave {
  0%, 100% { --liquid-wave: 0px; }
  28% { --liquid-wave: 5px; }
  74% { --liquid-wave: -4px; }
}

@keyframes liquid-glass-shape {
  0%, 100% { clip-path: var(--glass-rest, inset(1px 2px round 999px)); }
  30% { clip-path: var(--glass-crest, inset(1px 2px round 40% 60% 43% 57% / 60% 40% 60% 40%)); }
  77% { clip-path: var(--glass-trough, inset(1px 2px round 60% 40% 57% 43% / 40% 60% 40% 60%)); }
}

@keyframes liquid-indicator-shape {
  0%, 100% { border-radius: 50%; }
  28% { border-radius: 45% 55% 47% 53% / 56% 44% 54% 46%; }
  74% { border-radius: 55% 45% 53% 47% / 44% 56% 46% 54%; }
}

@media (prefers-reduced-motion: reduce) {
  :is(button, [data-liquid-control])::before { transition: none; }
}
