/* Version: 2.02 (no gold preset: coloured buttons are liquid-gold, glass.css) | 2.01 (dark ink on the solid yellow glass) | 2.00 (Liquid Glass: Martin's glass bench) | 1.02 (a disabled glass button keeps its glass)
   "Liquid Glass" - the glass button of Martin's glass bench (glass-bench-prototype.html) for every element with
   class ios-glass. scripts/ios-glass.js sets the values (--lg-*) from liquid-glass-presets.js for the element's
   preset and state; this file only draws. The element must be positioned (relative, absolute or fixed).
   Layers, bottom to top (the same order as on the bench):
     - its own backdrop (--lg-backdrop): in Chrome an SVG filter of its own (frost, bend at the rim, blur,
       saturation, brightness); elsewhere blur, saturation and brightness;
     - its own background: the white and dark tint, the shade at the sides, the shine at the top and the light at
       the bottom (white, the press colour on a press, fading back), the see-through colour fill;
     - ::before: the rim glint and the rim light (an image per size and light angle) and the light from the
       finger: they add light (plus-lighter);
     - ::after: the light inside (white core, the colour round it, from one point): it adds light and may breathe.
   The word and the icon get a shadow when it is bright behind them (--lg-word-ink, 0 when dark).
   Only transform and colours animate; nothing above a glass element ever gets opacity, filter, mask or clip-path. */

/* the bottom light: white at rest, the press colour on a press, then a fade */
@property --lg-lit {
  syntax: '<color>';
  inherits: true;
  initial-value: rgb(255 255 255 / 62%);
}

@property --ios-finger-spread {
  syntax: '<length>';
  inherits: true;
  initial-value: 0px;
}

/* the breath of the light inside: 0 -> 1 -> 0 */
@property --lg-breath-phase {
  syntax: '<number>';
  inherits: true;
  initial-value: 0;
}

:root {
  --lg-lit-rest: rgb(255 255 255 / 62%);
  --ios-finger-reach: 96px; /* the light from the finger spreads to 1.6 x the height */
  --ios-finger-in: 0.22s;
  --ios-finger-out: 0.4s;
  --ios-press-in: 0.36s;
  --ios-press-out: 0.5s;
  --ios-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ios-disabled-content: 0.4; /* a disabled glass button: its icon and words fade, the glass stays */
}

.ios-glass {
  /* the starting values until scripts/ios-glass.js paints the element */
  --lg-u: 0.32px;
  --lg-white-tint: 0;
  --lg-dark-tint: 0.12;
  --lg-shade: 1;
  --lg-top-shine: 1;
  --lg-bottom-light: 1;
  --lg-drop-shadow: 0.3;
  --lg-shadow-blur: 24px;
  --lg-shadow-y: 8px;
  --lg-inner-shadow: 0.4;
  --lg-fade: 2.5s;
  --lg-finger-light: 0;
  --lg-press-scale: 1;
  --lg-press-colour: #ffffff;
  --lg-tint-colour: #ffffff;
  --lg-tint-end: #ffffff;
  --lg-tint-angle: 0deg;
  --lg-colour-tint: 0;
  --lg-glow: 0;
  --lg-glow-reach: 0;
  --lg-core: 0;
  --lg-light-x: 50%;
  --lg-light-y: 110%;
  --lg-spread: 70%;
  --lg-tint-rim: 0;
  --lg-tint-rim-width: 1px;
  --lg-halo: 0;
  --lg-halo-size: 0px;
  --lg-breath: 0;
  --lg-breath-time: 2.8s;
  --lg-word-ink: transparent;
  --lg-word-blur: 8px;
  --lg-word-y: 2px;
  --lg-lit: var(--lg-lit-rest);
  --ios-finger-spread: 0px;
  isolation: isolate;
  border: 0;
  backdrop-filter: var(--lg-backdrop, blur(4px) saturate(135%));
  -webkit-backdrop-filter: var(--lg-backdrop, blur(4px) saturate(135%));
  background:
    linear-gradient(rgb(255 255 255 / var(--lg-white-tint)), rgb(255 255 255 / var(--lg-white-tint))),
    linear-gradient(rgb(0 0 0 / var(--lg-dark-tint)), rgb(0 0 0 / var(--lg-dark-tint))),
    linear-gradient(90deg,
      rgb(0 0 0 / calc(6% * var(--lg-shade))) 0%,
      transparent 7%,
      transparent 93%,
      rgb(0 0 0 / calc(8% * var(--lg-shade))) 100%),
    linear-gradient(180deg,
      rgb(255 255 255 / calc(14% * var(--lg-top-shine))) 0%,
      rgb(255 255 255 / calc(5% * var(--lg-top-shine))) 5%,
      rgb(0 0 0 / calc(4% * var(--lg-shade))) 10%,
      rgb(0 0 0 / calc(7% * var(--lg-shade))) 20%,
      rgb(0 0 0 / calc(6% * var(--lg-shade))) 35%,
      rgb(0 0 0 / calc(4% * var(--lg-shade))) 50%,
      rgb(0 0 0 / calc(1% * var(--lg-shade))) 65%,
      color-mix(in srgb, var(--lg-lit) clamp(0%, calc(8% * var(--lg-bottom-light)), 100%), transparent) 80%,
      color-mix(in srgb, var(--lg-lit) clamp(0%, calc(16% * var(--lg-bottom-light)), 100%), transparent) 90%,
      color-mix(in srgb, var(--lg-lit) clamp(0%, calc(29% * var(--lg-bottom-light)), 100%), transparent) 96%,
      color-mix(in srgb, var(--lg-lit) clamp(0%, calc(48% * var(--lg-bottom-light)), 100%), transparent) 100%),
    linear-gradient(var(--lg-tint-angle),
      color-mix(in srgb, var(--lg-tint-colour) calc(var(--lg-colour-tint) * 100%), transparent),
      color-mix(in srgb, var(--lg-tint-end) calc(var(--lg-colour-tint) * 100%), transparent));
  box-shadow:
    inset 0 calc(-6 * var(--lg-u)) calc(2 * var(--lg-u)) calc(-3 * var(--lg-u))
      color-mix(in srgb, var(--lg-lit) clamp(0%, calc(88% * var(--lg-bottom-light)), 100%), transparent),
    inset 0 calc(4 * var(--lg-u)) calc(3 * var(--lg-u)) calc(-2 * var(--lg-u)) rgb(255 255 255 / calc(26% * var(--lg-top-shine))),
    inset 0 0 0 calc(4 * var(--lg-u)) rgb(0 0 0 / calc(9% * var(--lg-inner-shadow))),
    inset 0 calc(-14 * var(--lg-u)) calc(14 * var(--lg-u)) calc(-2 * var(--lg-u))
      color-mix(in srgb, var(--lg-lit) clamp(0%, calc(26% * var(--lg-bottom-light)), 100%), transparent),
    inset 0 0 calc(10 * var(--lg-u)) calc(6 * var(--lg-u)) rgb(255 255 255 / calc(5% * var(--lg-top-shine))),
    inset 0 0 0 var(--lg-tint-rim-width) color-mix(in srgb, var(--lg-tint-colour) calc(min(var(--lg-tint-rim), 1) * 100%), transparent),
    inset 0 0 calc(var(--lg-tint-rim-width) * 4) color-mix(in srgb, var(--lg-tint-end) calc(var(--lg-tint-rim) * 45%), transparent),
    0 0 0 max(1px, calc(1.5 * var(--lg-u))) rgb(0 0 0 / calc(28% * var(--lg-inner-shadow))),
    0 0 var(--lg-halo-size) calc(var(--lg-halo-size) * 0.3) color-mix(in srgb, var(--lg-tint-end) calc(var(--lg-halo) * 75%), transparent),
    0 var(--lg-shadow-y) var(--lg-shadow-blur) rgb(0 0 0 / var(--lg-drop-shadow));
  text-shadow:
    0 var(--lg-word-y) var(--lg-word-blur) var(--lg-word-ink),
    0 0 calc(var(--lg-word-blur) * 0.35) var(--lg-word-ink);
  transition:
    transform var(--ios-press-out) var(--ios-spring),
    --lg-lit var(--lg-fade) ease-out,
    --ios-finger-spread var(--ios-finger-out) ease-out;
}

/* the icon stands above the lights and gets the word shadow too (a filter on a child of the glass, never a parent) */
.ios-glass > .icon,
.ios-glass > img {
  position: relative;
  z-index: 1;
  filter: drop-shadow(0 var(--lg-word-y) calc(var(--lg-word-blur) * 0.5) var(--lg-word-ink));
}

/* the rim glint, the rim light and the light from the finger: they only add light */
.ios-glass::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    radial-gradient(circle at var(--ios-finger-x, 50%) var(--ios-finger-y, 50%),
      rgb(255 255 255 / calc(55% * var(--lg-finger-light))) 0,
      rgb(255 255 255 / calc(16% * var(--lg-finger-light))) calc(var(--ios-finger-spread) * 0.55),
      transparent var(--ios-finger-spread)),
    var(--lg-glint, none) center / 100% 100% no-repeat;
  mix-blend-mode: plus-lighter;
  pointer-events: none;
}

/* the light inside: white in its core, the colour round it, from one point; it adds light and may breathe */
.ios-glass::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: radial-gradient(ellipse var(--lg-spread) calc(var(--lg-spread) * 2) at var(--lg-light-x) var(--lg-light-y),
    color-mix(in srgb, #ffffff calc(var(--lg-core) * 100%), var(--lg-tint-colour)) 0%,
    var(--lg-tint-colour) calc(15% + 20% * var(--lg-glow-reach)),
    color-mix(in srgb, var(--lg-tint-end) 45%, transparent) calc(40% + 25% * var(--lg-glow-reach)),
    transparent 100%);
  opacity: calc(var(--lg-glow) * (1 - var(--lg-breath) * var(--lg-breath-phase)));
  mix-blend-mode: plus-lighter;
  pointer-events: none;
}

.ios-glass.is-lg-breathing {
  animation: lg-breathe var(--lg-breath-time) ease-in-out infinite;
}

@keyframes lg-breathe {
  50% {
    --lg-breath-phase: 1;
  }
}

/* the press: the press colour at the bottom and the finger light come at once (the fade back is the transition
   above); the glass may grow with a spring (--lg-press-scale, 1 = it does not grow) */
.ios-glass.is-ios-pressed {
  --lg-lit: var(--lg-press-colour);
  --ios-finger-spread: var(--ios-finger-reach);
  transform: scale(var(--lg-press-scale));
  transition:
    transform var(--ios-press-in) var(--ios-spring),
    --lg-lit 0s,
    --ios-finger-spread var(--ios-finger-in) cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* a disabled glass button, or a button that holds glass, must not fade as a whole (opacity on it would switch the
   glass off: base.css fades every disabled button); only what lies on the glass fades */
.ios-glass:disabled,
button:disabled:has(.ios-glass) {
  opacity: 1;
}

.ios-glass:disabled > *,
button:disabled:has(.ios-glass) > :not(.ios-glass),
button:disabled .ios-glass > * {
  opacity: var(--ios-disabled-content);
}
