/* ─────────────────────────────────────────────────────────────────────────────
   Liquid Glass — web implementation
   Requires tokens.css.

   The material is FIVE stacked layers, not one blur. Removing any of them is
   what makes an imitation look flat:
     1  frost + saturation        (the element's own backdrop-filter)
     2  rim refraction / lensing  (::before — a masked ring with its own filter)
     3  specular highlight        (background-image on the element: top-edge
                                   light, pointer glow, optional dim)
     4  tint                      (::after — hue mapped onto backdrop luminance)
     5  shadow                    (box-shadow, adaptive by what's underneath)

   Layer 3 lives in the element's own background stack rather than a pseudo
   element, so ::after is free to carry the full-area tint blend. Putting the
   tint on ::before instead only colours the rim, which reads as a coloured
   outline around a dark button — not as stained glass.

   Children must sit above layers 2 and 4, hence `.lg > *`.
   ───────────────────────────────────────────────────────────────────────────── */

/* ═══ 1. The surface ═════════════════════════════════════════════════════ */

.lg {
  position: relative;
  isolation: isolate;
  border-radius: var(--lg-radius);
  color: var(--lg-fg);
  background-color: rgb(var(--lg-veil-rgb) / var(--lg-veil-a));

  /* Layer 3, topmost first: pointer glow · specular · optional dim.
     The light source sits above, so the top edge is bright and the bottom
     carries a weaker bounce. */
  background-image:
    radial-gradient(
      140% 120% at var(--lg-px, 50%) var(--lg-py, 50%),
      rgb(255 255 255 / var(--lg-glow)) 0%,
      transparent 62%
    ),
    linear-gradient(
      180deg,
      rgb(255 255 255 / calc(var(--lg-specular) * 0.45)) 0%,
      transparent 38%,
      transparent 62%,
      rgb(255 255 255 / calc(var(--lg-specular) * 0.14)) 100%
    ),
    linear-gradient(rgb(0 0 0 / var(--lg-dim, 0)), rgb(0 0 0 / var(--lg-dim, 0)));

  -webkit-backdrop-filter: blur(var(--lg-blur)) saturate(var(--lg-sat)) brightness(var(--lg-bright));
  backdrop-filter: blur(var(--lg-blur)) saturate(var(--lg-sat)) brightness(var(--lg-bright));

  box-shadow:
    inset 0 1px 0 0 rgb(255 255 255 / var(--lg-specular)),
    inset 0 -1px 0 0 rgb(255 255 255 / calc(var(--lg-specular) * 0.35)),
    inset 0 0 0 0.5px rgb(255 255 255 / var(--lg-rim)),
    0 var(--lg-shadow-y) var(--lg-shadow-blur) rgb(0 0 0 / var(--lg-shadow-a));

  transition:
    background-color var(--dur-fast) var(--ease-out),
    box-shadow var(--dur-fast) var(--ease-out),
    transform var(--dur-instant) var(--ease-spring),
    -webkit-backdrop-filter var(--dur-base) var(--ease-out),
    backdrop-filter var(--dur-base) var(--ease-out);
}

.lg > * { position: relative; z-index: 2; }

/* ═══ 2. Rim refraction ══════════════════════════════════════════════════
   Real glass bends light most at its boundary. A uniform blur has no edge, so
   it reads as fog rather than as an object. This ring carries a second, harder
   filter concentrated in the outer 1–2px, which is what produces the sense of
   a physical lens sitting on the page. */

.lg::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  padding: var(--lg-lens-w);
  pointer-events: none;

  -webkit-backdrop-filter: blur(calc(var(--lg-blur) * 0.4)) saturate(220%) brightness(var(--lg-lens-bright));
  backdrop-filter: blur(calc(var(--lg-blur) * 0.4)) saturate(220%) brightness(var(--lg-lens-bright));

  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
}

/* ═══ Variants ═══════════════════════════════════════════════════════════ */

/* REGULAR — the default. Legible over anything, at any size. ~90% of uses. */
.lg-regular { /* inherits the defaults above */ }

/* CLEAR — no adaptive behavior, permanently more transparent. Valid ONLY when
   all three hold: over media-rich content; the content layer tolerates a
   dimming layer; what sits on the glass is bold and bright.
   Pair with .lg-dim on the element behind, or use .lg-clear--dimmed. */
.lg-clear {
  --lg-blur: 8px;
  --lg-sat: 150%;
  --lg-veil-a: 0.06;
  --lg-specular: 0.42;
  --lg-rim: 0.30;
  --lg-shadow-a: 0.18;
}

/* The dimming layer clear glass requires. ~35% black when the media is bright;
   skip it when the media is already dark. Localized (inset to the element)
   keeps surrounding media at full vibrancy. */
.lg-clear--dimmed { --lg-dim: 0.35; }

/* ═══ Size behaviour ═════════════════════════════════════════════════════
   Size changes the MATERIAL, not just the dimensions. A larger surface
   simulates thicker glass: deeper shadow, stronger lensing, softer scatter —
   and it does NOT flip light/dark, because a big surface changing appearance
   mid-scroll is distracting. */

.lg-sm {                      /* buttons, toolbars, tab bars — thin, flips */
  --lg-blur: 20px;
  --lg-shadow-y: 4px;
  --lg-shadow-blur: 16px;
  --lg-shadow-a: 0.10;
}

.lg-lg {                      /* menus, sidebars, sheets — thick, never flips */
  --lg-blur: 40px;
  --lg-veil-a: 0.28;
  --lg-lens-w: 2px;
  --lg-lens-bright: 1.35;
  --lg-shadow-y: 18px;
  --lg-shadow-blur: 56px;
  --lg-shadow-a: 0.22;
}

/* ═══ Adaptive light/dark ════════════════════════════════════════════════
   Apple samples the backdrop continuously. The browser cannot, so the flip is
   driven by declared zones — mark a section `data-lg-bg="dark"` and
   liquid-glass.js sets `data-lg-scheme` on any small glass overlapping it.
   Applied only to .lg-sm: large surfaces adapt but must not flip. */

.lg-sm[data-lg-scheme="dark"] {
  --lg-veil-rgb: 20 20 22;
  --lg-veil-a: 0.34;
  --lg-bright: 0.88;
  --lg-specular: 0.20;
  --lg-rim: 0.14;
  --lg-shadow-a: 0.42;
  --lg-fg: rgb(255 255 255 / 0.95);
}

.lg-sm[data-lg-scheme="light"] {
  --lg-veil-rgb: 255 255 255;
  --lg-veil-a: 0.20;
  --lg-bright: 1.06;
  --lg-specular: 0.60;
  --lg-rim: 0.24;
  --lg-shadow-a: 0.12;
  --lg-fg: rgb(0 0 0 / 0.92);
}

/* Shadow deepens over text and lightens over a flat light background — the
   separation is strongest exactly where it is needed. Set by JS, or by hand. */
.lg[data-lg-over="text"]  { --lg-shadow-a: 0.28; }
.lg[data-lg-over="flat"]  { --lg-shadow-a: 0.07; }
.lg[data-lg-over="media"] { --lg-shadow-a: 0.34; }

/* ═══ 4. Tinting ═════════════════════════════════════════════════════════
   ONE tinted element per view: the primary action. Apple maps a colour to a
   range of tones driven by the brightness underneath — stained glass, not
   paint. `mix-blend-mode: color` reproduces this exactly: it keeps the
   backdrop's luminance and applies only the tint's hue and saturation, so the
   button stays legible over anything. A solid fill is the tell of a bad
   imitation, so the flat layer underneath stays deliberately weak. */

/* The two knobs trade character against colour identity, and the balance is not
   obvious — it was set by rendering candidates over a bright gradient, a dark
   band and a photo, then comparing:

     a=.30 blend=.90  reads as flat paint; barely tracks the backdrop
     a=.22 blend=.55  beautifully glassy, but the blue goes grey over orange —
                      Apple's rule is that tint shifts with the backdrop
                      "without deviating too much from the intended colour"
     a=.34 blend=.68  keeps the colour, still tracks the backdrop, and the
                      white label survives a bright backdrop      ← chosen

   Note also that Chrome composites a backdrop-filtered surface outside the
   element's own blending group, so an effective blend lands weaker than the
   raw opacity suggests. Over very bright media, lower --lg-tint-blend and
   switch --lg-fg to a dark label rather than pushing the tint harder. */
.lg-tint {
  --lg-tint-rgb: var(--accent-rgb);
  --lg-tint-a: 0.34;
  --lg-tint-blend: 0.68;
  background-color: rgb(var(--lg-tint-rgb) / var(--lg-tint-a));
  --lg-fg: #fff;
}

/* Full-area blend — NOT on ::before, which is only the rim. `color` keeps the
   backdrop's luminance and applies the tint's hue and saturation, so the
   control stays legible over both a bright photo and a dark one. */
.lg-tint::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  border-radius: inherit;
  pointer-events: none;
  background-color: rgb(var(--lg-tint-rgb));
  mix-blend-mode: color;
  opacity: var(--lg-tint-blend);
}

/* Tinted TEXT or SYMBOL, for the rare case a label needs emphasis without the
   whole control being primary. Prefer tinting the background instead. */
.lg-tint-label { color: rgb(var(--accent-rgb)); }

/* ═══ Glass on glass — the fix, not the sin ══════════════════════════════
   Never stack two translucent panes. For something sitting ON a glass bar,
   use a fill so it reads as part of the same surface. */

.lg-inset {
  background-color: var(--fill-tertiary);
  background-image: none;
  border-radius: max(0px, calc(var(--radius-outer, var(--radius-control)) - var(--pad, 0px)));
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  box-shadow: none;
}
.lg-inset::before, .lg-inset::after { content: none; }

/* ═══ Interaction ════════════════════════════════════════════════════════
   Flex and energize on contact; the resting state stays quiet. */

.lg-interactive {
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  min-height: var(--hit-min);
}
.lg-interactive:hover  { --lg-glow: 0.10; }
.lg-interactive:active {
  --lg-glow: 0.26;
  transform: scale(0.97);
}
.lg-interactive:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

/* Lift into glass on interaction — for controls that live in the CONTENT
   layer (slider knob, toggle). At rest they are solid; glass appears only
   while being touched, then goes away. */
.lg-lift {
  background-color: var(--bg-primary);
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  transition: all var(--dur-fast) var(--ease-spring);
}
.lg-lift:active, .lg-lift[data-lg-active="true"] {
  background-color: rgb(var(--lg-veil-rgb) / var(--lg-veil-a));
  -webkit-backdrop-filter: blur(var(--lg-blur)) saturate(var(--lg-sat));
  backdrop-filter: blur(var(--lg-blur)) saturate(var(--lg-sat));
}

/* Materialize by modulating refraction, not by fading. A cross-fade breaks
   the illusion — a lens does not dissolve. */
@keyframes lg-materialize {
  from { opacity: 0; --lg-blur: 0px; --lg-specular: 0; transform: scale(0.94); }
  to   { opacity: 1; transform: scale(1); }
}
.lg-enter { animation: lg-materialize var(--dur-base) var(--ease-glass) both; }

/* Morph: a trigger inflates into the surface it opens, so the relationship
   between tap and result stays visible. Set transform-origin to the trigger. */
.lg-morph {
  transform-origin: var(--lg-origin-x, 50%) var(--lg-origin-y, 0%);
  animation: lg-morph-in var(--dur-base) var(--ease-glass) both;
}
@keyframes lg-morph-in {
  from { opacity: 0; transform: scale(0.6, 0.35); border-radius: var(--radius-capsule); }
  to   { opacity: 1; transform: scale(1, 1); }
}

/* ═══ 5. Scroll edge effect ══════════════════════════════════════════════
   Replaces hard dividers. NOT an overlay and NOT decoration: it dissolves
   content into the background as it scrolls under floating UI, lifting the
   glass above it. One per view; never stack soft and hard.

   Markup:
     <div class="lg-edge lg-edge--soft" aria-hidden="true">
       <i></i><i></i><i></i><i></i><i></i>
     </div>
   Progressive blur — stacked layers of increasing strength, each masked to a
   band — because a single blur produces a visible seam. */

.lg-edge {
  position: absolute;
  inset-inline: 0;
  top: 0;
  height: var(--lg-edge-h, 120px);
  pointer-events: none;
  z-index: 40;
}
.lg-edge > i {
  position: absolute;
  inset: 0;
  display: block;
}
.lg-edge--soft > i:nth-child(1) { -webkit-backdrop-filter: blur(1px);  backdrop-filter: blur(1px);
  -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 20%, transparent 40%);
          mask-image: linear-gradient(to bottom, #000 0%, #000 20%, transparent 40%); }
.lg-edge--soft > i:nth-child(2) { -webkit-backdrop-filter: blur(3px);  backdrop-filter: blur(3px);
  -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 15%, transparent 32%);
          mask-image: linear-gradient(to bottom, #000 0%, #000 15%, transparent 32%); }
.lg-edge--soft > i:nth-child(3) { -webkit-backdrop-filter: blur(8px);  backdrop-filter: blur(8px);
  -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 10%, transparent 24%);
          mask-image: linear-gradient(to bottom, #000 0%, #000 10%, transparent 24%); }
.lg-edge--soft > i:nth-child(4) { -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  -webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 6%, transparent 16%);
          mask-image: linear-gradient(to bottom, #000 0%, #000 6%, transparent 16%); }
.lg-edge--soft > i:nth-child(5) { -webkit-backdrop-filter: blur(28px); backdrop-filter: blur(28px);
  -webkit-mask-image: linear-gradient(to bottom, #000 0%, transparent 9%);
          mask-image: linear-gradient(to bottom, #000 0%, transparent 9%); }

/* HARD — uniform across the full height of the bar and any pinned accessory
   view. Mostly desktop; use when a pinned header needs extra separation. */
.lg-edge--hard {
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  backdrop-filter: blur(24px) saturate(180%);
  background: rgb(var(--lg-veil-rgb) / calc(var(--lg-veil-a) * 1.6));
  border-bottom: 0.5px solid var(--separator);
}

/* When darker content scrolls under, the effect dims rather than fades. */
[data-lg-scheme="dark"] .lg-edge--soft > i {
  background: linear-gradient(to bottom, rgb(0 0 0 / 0.28), transparent);
}

/* Bottom edge, for a floating bar anchored to the bottom of the viewport. */
.lg-edge--bottom { top: auto; bottom: 0; transform: scaleY(-1); }

/* ═══ Background extension ═══════════════════════════════════════════════
   Content appears to expand behind a sidebar or inspector — mirrored and
   blurred — so the layout reads edge-to-edge while the visual stays centred.
   Layer text and controls ABOVE it to avoid distortion. */

.lg-extend {
  position: absolute;
  inset-block: 0;
  width: var(--lg-extend-w, 320px);
  pointer-events: none;
  -webkit-backdrop-filter: blur(40px) saturate(160%);
  backdrop-filter: blur(40px) saturate(160%);
}

/* ═══ Accessibility ══════════════════════════════════════════════════════
   On Apple platforms these come free. Here they do not, and glass without
   them is unusable for the people who need them. */

@media (prefers-reduced-transparency: reduce) {
  /* `.lg-sm[data-lg-scheme]` is listed explicitly because the adaptive flip
     above has attribute-level specificity and would otherwise keep its
     inverted --lg-fg here: an opaque LIGHT bar still carrying its flipped
     WHITE label. Same trick in the increased-contrast block below. */
  .lg,
  .lg-clear,
  .lg-sm[data-lg-scheme],
  .lg-lift:active,
  .lg-lift[data-lg-active="true"] {
    background-color: rgb(var(--lg-solid-rgb) / 0.94);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
    --lg-fg: var(--label);
  }
  .lg::before { content: none; }              /* the lens has nothing to bend */

  /* `:nth-child(n)` is here to MATCH SPECIFICITY, not to select anything new.
     The progressive-blur layers are defined at `.lg-edge--soft > i:nth-child(1)`
     — (0,2,1). A fallback written as `.lg-edge > i` is (0,1,1) and quietly
     loses, so the blur survives with the setting on. Media queries add no
     specificity of their own; this is the second bug of exactly this shape in
     this file, so when you write an accessibility override, count the selector
     you are overriding first. */
  .lg-edge--soft > i:nth-child(n),
  .lg-edge--hard,
  .lg-extend { -webkit-backdrop-filter: none; backdrop-filter: none; }
  .lg-tint { background-color: rgb(var(--lg-tint-rgb) / 0.96); }
  .lg-tint::after { content: none; }          /* opaque already carries the colour */
}

@media (prefers-contrast: more) {
  .lg,
  .lg-sm[data-lg-scheme],
  .lg-lift:active,
  .lg-lift[data-lg-active="true"] {
    background-color: var(--bg-primary);
    background-image: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    box-shadow: inset 0 0 0 2px var(--label), 0 2px 8px rgb(0 0 0 / 0.30);
    --lg-fg: var(--label);
  }
  .lg::before, .lg::after { content: none; }
  .lg-tint {
    background-color: rgb(var(--accent-hc-rgb));
    --lg-fg: #fff;
  }
}

@media (prefers-reduced-motion: reduce) {
  .lg,
  .lg-interactive:active,
  .lg-lift { transition-duration: 1ms; transform: none; }
  .lg-enter, .lg-morph { animation: lg-fade var(--dur-fast) linear both; }
  @keyframes lg-fade { from { opacity: 0 } to { opacity: 1 } }
}

/* No backdrop-filter at all (older browsers, some embedded webviews):
   fall back to an opaque surface rather than an unreadable transparent one. */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .lg { background-color: rgb(var(--lg-solid-rgb) / 0.96); }
  .lg::before { content: none; }
  .lg-edge > i { display: none; }
}

/* ═══ Optional: true refraction (highest fidelity tier) ══════════════════
   Adds real geometric displacement at the rim via an SVG filter — the closest
   a browser gets to Apple's lensing. Requires assets/lens-filter.html to be
   inlined in the document. Support is uneven (Chromium yes, WebKit largely
   no), so it is opt-in and purely additive: without it the ring above still
   carries the effect. */
@supports (backdrop-filter: url(#lg-lens)) {
  .lg-refract::before {
    -webkit-backdrop-filter: url(#lg-lens) blur(calc(var(--lg-blur) * 0.4)) saturate(220%);
    backdrop-filter: url(#lg-lens) blur(calc(var(--lg-blur) * 0.4)) saturate(220%);
  }
}
