/* Material surfaces. A preset is chosen with data-mw-material on any element
   (usually <html> or a section) and sets the surface variables that every card
   and panel inside reads. .mw-surface applies them to any element.

   Presets: flat | paper | glass | liquid-glass | clay | brutal.
   (relief and cell live in their own stylesheets and set the same variables.)

   Output variables, set by each preset:
     --mw-surface-bg          fill colour (may be translucent)
     --mw-surface-bg-solid    opaque equivalent: used under prefers-reduced-transparency
                              and where backdrop-filter is missing
     --mw-surface-tint        the opaque base colour of the material
     --mw-surface-filter      backdrop-filter value, or none
     --mw-surface-blur, --mw-surface-saturate   the same filter as a length and a number
     --mw-surface-edge        border shorthand; --mw-surface-edge-color is its colour
     --mw-surface-sheen       highlight gradient (a background-image layer), or none
     --mw-surface-texture     extra background-image layers (paper grain), or none
     --mw-surface-shadow      outer shadows; --mw-surface-shadow-in inner shadows at
                              rest (0 0 #0000 when empty). Join them as
                              "shadow-in, shadow". Where the material keeps the
                              system's card shadow, --mw-surface-shadow may be none,
                              which voids the joined list: correct, since those
                              materials have no inner shadow
     --mw-surface-radius      corner radius
     --mw-surface-ink         text colour, at least 4.5:1 on the composited surface
     --mw-surface-ink-muted   secondary text (the same as ink on glass: secondary
                              text over an unknown backdrop would need a far denser tint)

   Every value derives from the --ds-* roles of a design system, with fallbacks
   that look right when no system is set. The system's card is its signature
   (a top rule, a left rule, a scrap shadow): edge, shadow, radius and fill come
   from --ds-card-border, --ds-card-shadow / --ds-shadow-card, --ds-card-radius
   and --ds-card-bg, and a material only adds what it brings. flat adds nothing;
   paper adds grain; brutal adds its thick edge and hard offset only where the
   system has no card border or shadow; glass, liquid-glass and clay replace
   fill, filter and shadow (reading --ds-material-* first) and keep the
   system's edge and radius when it has them. Material parameters a system may set:
   --ds-material-tint (glass alpha of one sheet), --ds-material-floor (the
   solved minimum alpha for text, see below), --ds-material-blur,
   --ds-material-saturate, --ds-material-grain, and for brutal
   --ds-material-border (frame width), --ds-material-edge (frame colour),
   --ds-material-offset and --ds-material-shadow (offset-shadow colour). A page can tune glass with --mw-glass-tint (colour),
   --mw-glass-ink, --mw-glass-alpha, --mw-glass-floor, --mw-glass-blur and
   --mw-glass-saturate on the element that carries data-mw-material.

   Variables are computed where they are declared and inherited from there, so
   an element below the material that switches data-mw-mode or data-mw-ds, or
   asks for another depth with data-mw-depth, recomputes them (the :where()
   selectors below). A nested data-mw-material wins on its own element; put
   data-mw-mode on that same element when a nested section also switches mode.

   Glass depth (the stacked-translucency recipe). One sheet has one recipe,
   alpha a and blur b. data-mw-depth="2" or "3" on a surface paints it as two
   or three sheets stacked: alpha 1 - (1 - a)^N, blur b * sqrt(N) (Gaussian blurs
   compose in quadrature). Text never drops below the floor: the alpha of any
   surface that carries text is max(alpha, floor), where the floor is the
   smallest alpha at which the ink keeps 4.6:1 over pure black and pure white,
   that is, over any backdrop. Siblings that overlap composite for real: each
   backdrop-filter sees the sheets painted below it. No JavaScript is needed:
   CSS can stack and floor; it cannot measure colours, so the floor is a token.
   A design system should ship it solved (--ds-material-floor); without one,
   the floor follows the tint's lightness: 0.68 for light glass, 0.78 for dark
   glass, which holds for any ink with at least 10:1 on the tint. */

/* ---- flat: nothing but the system's card ------------------------------------ */
[data-mw-material="flat"],
:where([data-mw-material="flat"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-surface-tint: var(--ds-card-bg, var(--ds-surface, #fff));
  --mw-surface-bg: var(--mw-surface-tint);
  --mw-surface-bg-solid: var(--mw-surface-tint);
  --mw-surface-filter: none;
  --mw-surface-blur: 0px;
  --mw-surface-saturate: 1;
  --mw-surface-edge-color: var(--ds-line, rgb(0 0 0 / .12));
  --mw-surface-edge: var(--ds-card-border, 1px solid var(--mw-surface-edge-color));
  --mw-surface-sheen: none;
  --mw-surface-texture: none;
  --mw-surface-shadow: var(--ds-card-shadow, var(--ds-shadow-card, 0 0 #0000));
  --mw-surface-shadow-in: 0 0 #0000;
  --mw-surface-radius: var(--ds-card-radius, var(--ds-radius-md, 12px));
  --mw-surface-ink: var(--ds-card-fg, var(--ds-text, #15141a));
  --mw-surface-ink-muted: var(--ds-text-muted, #55535c);
}

/* ---- paper: hairline edge and a very light grain ---------------------------- */
[data-mw-material="paper"],
:where([data-mw-material="paper"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-surface-tint: var(--ds-card-bg, rgb(var(--ds-surface-rgb, 250 247 240)));
  --mw-surface-bg: var(--mw-surface-tint);
  --mw-surface-bg-solid: var(--mw-surface-tint);
  --mw-surface-filter: none;
  --mw-surface-blur: 0px;
  --mw-surface-saturate: 1;
  --mw-surface-edge-color: var(--ds-line, rgb(60 48 30 / .16));
  --mw-surface-edge: var(--ds-card-border, 1px solid var(--mw-surface-edge-color));
  --mw-surface-sheen: none;
  --mw-surface-texture: none;
  /* Without a system: a sheet resting on the page, a 1 px contact line, no float. */
  --mw-surface-shadow: var(--ds-card-shadow, var(--ds-shadow-card, 0 1px 0 rgb(var(--ds-text-rgb, 40 32 20) / .06)));
  --mw-surface-shadow-in: 0 0 #0000;
  --mw-surface-radius: var(--ds-card-radius, var(--ds-radius-md, 6px));
  --mw-surface-ink: var(--ds-card-fg, var(--ds-text, #1d1a16));
  --mw-surface-ink-muted: var(--ds-text-muted, #5a554c);
}

/* ---- glass and liquid-glass: translucent tint, blur, edge, sheen ------------ */
/* Static tier: browsers without relative colours get the opaque tint (legible,
   not translucent); the tier below replaces it where they work. */
:is([data-mw-material="glass"], [data-mw-material="liquid-glass"]),
:where(:is([data-mw-material="glass"], [data-mw-material="liquid-glass"]) :is([data-mw-mode], [data-mw-ds])) {
  --mw-surface-tint: var(--mw-glass-tint, rgb(var(--ds-surface-rgb, 255 255 255)));
  --mw-surface-bg: var(--mw-surface-tint);
  --mw-surface-bg-solid: var(--mw-surface-tint);
  /* One sheet's alpha, read by the depth rules below. */
  --mw-glass-a1: var(--mw-glass-alpha, var(--ds-material-tint, .5));
  /* The floor as tokens: `l` is the tint's OKLCH lightness, resolved inside each
     relative colour below (dark tint 0.78, light tint 0.68, ramp between). */
  --mw-glass-f: var(--mw-glass-floor, var(--ds-material-floor, calc(.78 - .1 * clamp(0, (l - .45) * 5, 1))));
  --mw-surface-filter: blur(var(--mw-surface-blur)) saturate(var(--mw-surface-saturate));
  --mw-surface-edge-color: rgb(255 255 255 / .4);
  --mw-surface-edge: var(--ds-card-border, 1px solid var(--mw-surface-edge-color));
  --mw-surface-sheen: linear-gradient(180deg, rgb(255 255 255 / .14), rgb(255 255 255 / 0) 42%);
  --mw-surface-texture: none;
  /* Glass floats: shadows fall straight down. */
  --mw-surface-shadow: var(--ds-shadow-raised, 0 1px 2px rgb(16 12 40 / .08), 0 14px 34px -12px rgb(16 12 40 / .26));
  --mw-surface-shadow-in: inset 0 1px 0 rgb(255 255 255 / .5), inset 0 -1px 0 rgb(255 255 255 / .1);
  --mw-surface-ink: var(--mw-glass-ink, var(--ds-text, #17141f));
  --mw-surface-ink-muted: var(--mw-surface-ink);
  --mw-lens-rim-bg: transparent;
  --mw-lens-core-bg: var(--mw-surface-tint);
}
[data-mw-material="glass"],
:where([data-mw-material="glass"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-glass-b1: var(--mw-glass-blur, var(--ds-material-blur, 8px));
  --mw-surface-blur: var(--mw-glass-b1);
  --mw-surface-saturate: var(--mw-glass-saturate, var(--ds-material-saturate, 1.3));
  --mw-surface-radius: var(--ds-card-radius, var(--ds-radius-lg, 22px));
}
/* Liquid glass is the denser frost of the lens material; with LiquidLens on a
   surface, the rim refracts and the core keeps this frost. */
[data-mw-material="liquid-glass"],
:where([data-mw-material="liquid-glass"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-glass-b1: var(--mw-glass-blur, var(--ds-material-blur, 18px));
  --mw-surface-blur: var(--mw-glass-b1);
  --mw-surface-saturate: var(--mw-glass-saturate, var(--ds-material-saturate, 1.8));
  --mw-surface-radius: var(--ds-card-radius, var(--ds-radius-lg, 24px));
}

@supports (color: oklch(from red l c h / max(.5, calc(.7 - .1 * clamp(0, (l - .45) * 5, 1))))) {
  :is([data-mw-material="glass"], [data-mw-material="liquid-glass"]),
  :where(:is([data-mw-material="glass"], [data-mw-material="liquid-glass"]) :is([data-mw-mode], [data-mw-ds])) {
    --mw-surface-bg: oklch(from var(--mw-surface-tint) l c h / max(var(--mw-glass-a1), var(--mw-glass-f)));
    /* Edge, sheen and specular follow the tint: bright on light glass, faint on dark. */
    --mw-surface-edge-color: oklch(from var(--mw-surface-tint) 1 0 0 / calc(.12 + .3 * clamp(0, (l - .45) * 5, 1)));
    --mw-surface-sheen: linear-gradient(180deg, oklch(from var(--mw-surface-tint) 1 0 0 / calc(.05 + .13 * clamp(0, (l - .45) * 5, 1))), rgb(255 255 255 / 0) 42%);
    --mw-surface-shadow-in:
      inset 0 1px 0 oklch(from var(--mw-surface-tint) 1 0 0 / calc(.16 + .4 * clamp(0, (l - .45) * 5, 1))),
      inset 0 -1px 0 oklch(from var(--mw-surface-tint) 1 0 0 / calc(.05 + .07 * clamp(0, (l - .45) * 5, 1)));
    /* LiquidLens split: a thin tint at the rim lets the bent backdrop show; the
       core layer is solved so rim and core together reach the same alpha as the
       plain surface, max(alpha, floor). */
    --mw-lens-rim-bg: oklch(from var(--mw-surface-tint) l c h / var(--mw-lens-rim-alpha, .22));
    --mw-lens-core-bg: oklch(from var(--mw-surface-tint) l c h / calc(1 - (1 - max(var(--mw-glass-a1), var(--mw-glass-f))) / (1 - var(--mw-lens-rim-alpha, .22))));
  }
  /* Depth: two and three sheets of the same recipe. */
  :where(:is([data-mw-material="glass"], [data-mw-material="liquid-glass"])) [data-mw-depth="2"] {
    --mw-surface-bg: oklch(from var(--mw-surface-tint) l c h / max(calc(var(--mw-glass-a1) * (2 - var(--mw-glass-a1))), var(--mw-glass-f)));
  }
  :where(:is([data-mw-material="glass"], [data-mw-material="liquid-glass"])) [data-mw-depth="3"] {
    --mw-surface-bg: oklch(from var(--mw-surface-tint) l c h / max(calc(1 - (1 - var(--mw-glass-a1)) * (1 - var(--mw-glass-a1)) * (1 - var(--mw-glass-a1))), var(--mw-glass-f)));
  }
}
:where(:is([data-mw-material="glass"], [data-mw-material="liquid-glass"])) [data-mw-depth="2"] {
  --mw-surface-blur: calc(var(--mw-glass-b1) * 1.4142);
  --mw-surface-filter: blur(var(--mw-surface-blur)) saturate(var(--mw-surface-saturate));
}
:where(:is([data-mw-material="glass"], [data-mw-material="liquid-glass"])) [data-mw-depth="3"] {
  --mw-surface-blur: calc(var(--mw-glass-b1) * 1.7321);
  --mw-surface-filter: blur(var(--mw-surface-blur)) saturate(var(--mw-surface-saturate));
}

/* ---- clay: soft outer and inner shadows, rounded, pastel -------------------- */
[data-mw-material="clay"],
:where([data-mw-material="clay"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-surface-tint: var(--mw-clay-tint, rgb(var(--ds-surface-rgb, 241 235 255)));
  --mw-surface-bg: var(--mw-surface-tint);
  --mw-surface-bg-solid: var(--mw-surface-tint);
  --mw-surface-filter: none;
  --mw-surface-blur: 0px;
  --mw-surface-saturate: 1;
  --mw-surface-edge-color: transparent;
  --mw-surface-edge: var(--ds-card-border, 0 solid transparent);
  /* Light from above: a soft glow on the top half, never under the text. */
  --mw-surface-sheen: radial-gradient(120% 70% at 30% 0%, rgb(255 255 255 / .38), rgb(255 255 255 / 0) 62%);
  --mw-surface-texture: none;
  --mw-clay-shade: rgb(70 48 140 / .3);
  --mw-surface-shadow: 0 22px 38px -16px var(--mw-clay-shade), 0 8px 14px -8px var(--mw-clay-shade);
  --mw-surface-shadow-in: inset 0 -9px 16px -8px rgb(40 20 90 / .16), inset 0 9px 14px -6px rgb(255 255 255 / .7);
  --mw-surface-radius: var(--ds-card-radius, max(var(--ds-radius-lg, 28px), 22px));
  --mw-surface-ink: var(--ds-text, #2a2547);
  --mw-surface-ink-muted: var(--ds-text-muted, #514b72);
}
@supports (color: color-mix(in oklab, red 50%, blue)) {
  /* The shade takes the system's accent, so a lilac card casts a lilac shadow. */
  [data-mw-material="clay"],
  :where([data-mw-material="clay"] :is([data-mw-mode], [data-mw-ds])) {
    --mw-clay-shade: color-mix(in srgb, color-mix(in oklab, var(--ds-accent, #8b7cf6) 55%, #1b1430) 34%, transparent);
  }
}

@supports (color: oklch(from red l c h / max(.5, calc(.7 - .1 * clamp(0, (l - .45) * 5, 1))))) {
  /* Clay on a dark system: the light from above stays faint, so it never
     washes out light text. */
  [data-mw-material="clay"],
  :where([data-mw-material="clay"] :is([data-mw-mode], [data-mw-ds])) {
    --mw-surface-sheen: radial-gradient(120% 70% at 30% 0%, oklch(from var(--mw-surface-tint) 1 0 0 / calc(.05 + .33 * clamp(0, (l - .45) * 5, 1))), rgb(255 255 255 / 0) 62%);
    --mw-surface-shadow-in:
      inset 0 -9px 16px -8px rgb(20 10 50 / .16),
      inset 0 9px 14px -6px oklch(from var(--mw-surface-tint) 1 0 0 / calc(.08 + .62 * clamp(0, (l - .45) * 5, 1)));
  }
}

/* ---- brutal: thick border, hard offset shadow ------------------------------- */
[data-mw-material="brutal"],
:where([data-mw-material="brutal"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-surface-tint: var(--ds-card-bg, rgb(var(--ds-surface-rgb, 255 253 242)));
  --mw-surface-bg: var(--mw-surface-tint);
  --mw-surface-bg-solid: var(--mw-surface-tint);
  --mw-surface-filter: none;
  --mw-surface-blur: 0px;
  --mw-surface-saturate: 1;
  /* Frame and offset colours come from the system's material when it sets
     them (a dark system's text is light, which would paint a white frame). */
  --mw-surface-edge-color: var(--ds-material-edge, var(--ds-text, #0b0b0b));
  --mw-surface-edge: var(--ds-card-border, var(--ds-material-border, var(--ds-border-width, 3px)) solid var(--mw-surface-edge-color));
  --mw-surface-sheen: none;
  --mw-surface-texture: none;
  --mw-surface-shadow: var(--ds-card-shadow, var(--ds-shadow-card, var(--ds-material-offset, 6px) var(--ds-material-offset, 6px) 0 0 var(--ds-material-shadow, var(--ds-text, #0b0b0b))));
  --mw-surface-shadow-in: 0 0 #0000;
  --mw-surface-radius: var(--ds-card-radius, var(--ds-radius-sm, 0px));
  --mw-surface-ink: var(--ds-card-fg, var(--ds-text, #0b0b0b));
  --mw-surface-ink-muted: var(--ds-text-muted, #3a3a3a);
}

/* ---- paper grain (needs relative colours) ----------------------------------- */
/* Grey fractal noise under a veil of the paper colour at 1 - grain. The veil
   colour is corrected so the average stays exactly the paper colour: the
   grain shifts single pixels by about 1 % and never moves the tone the text
   was measured against. */
@supports (color: rgb(from red calc((r - 1) / .9) g b / calc(1 - .1))) {
  [data-mw-material="paper"],
  :where([data-mw-material="paper"] :is([data-mw-mode], [data-mw-ds])) {
    --mw-surface-texture:
      linear-gradient(rgb(from var(--mw-surface-tint) calc((r - 127.5 * var(--ds-material-grain, .035)) / (1 - var(--ds-material-grain, .035))) calc((g - 127.5 * var(--ds-material-grain, .035)) / (1 - var(--ds-material-grain, .035))) calc((b - 127.5 * var(--ds-material-grain, .035)) / (1 - var(--ds-material-grain, .035))) / calc(1 - var(--ds-material-grain, .035))) 0 0),
      url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='g' x='0' y='0' width='100%25' height='100%25' color-interpolation-filters='sRGB'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='.33 .33 .33 0 0 .33 .33 .33 0 0 .33 .33 .33 0 0 0 0 0 0 1'/%3E%3CfeComponentTransfer%3E%3CfeFuncR type='linear' slope='3' intercept='-1'/%3E%3CfeFuncG type='linear' slope='3' intercept='-1'/%3E%3CfeFuncB type='linear' slope='3' intercept='-1'/%3E%3C/feComponentTransfer%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)'/%3E%3C/svg%3E");
  }
}

/* ---- fallbacks: reduced transparency, no backdrop-filter -------------------- */
@media (prefers-reduced-transparency: reduce) {
  [data-mw-material],
  [data-mw-material] [data-mw-depth],
  :where([data-mw-material] :is([data-mw-mode], [data-mw-ds])) {
    --mw-surface-bg: var(--mw-surface-bg-solid);
    --mw-surface-filter: none;
    --mw-lens-rim-bg: var(--mw-surface-bg-solid);
    --mw-lens-core-bg: var(--mw-surface-bg-solid);
  }
}
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  [data-mw-material],
  [data-mw-material] [data-mw-depth],
  :where([data-mw-material] :is([data-mw-mode], [data-mw-ds])) {
    --mw-surface-bg: var(--mw-surface-bg-solid);
    --mw-surface-filter: none;
  }
}

/* ---- the utility ------------------------------------------------------------ */
/* Any element becomes a surface. Outside a material it is the system's card
   (or a white card with a hairline when no system is set). */
:where(.mw-surface) {
  position: relative;
  isolation: isolate;
}
.mw-surface {
  border: var(--mw-surface-edge, var(--ds-card-border, 1px solid var(--ds-line, rgb(0 0 0 / .12))));
  border-radius: var(--mw-surface-radius, var(--ds-card-radius, 16px));
  background: var(--mw-surface-sheen, none), var(--mw-surface-texture, none), var(--mw-surface-bg, var(--ds-card-bg, var(--ds-surface, #fff)));
  color: var(--mw-surface-ink, var(--ds-card-fg, var(--ds-text)));
  box-shadow: var(--mw-surface-shadow-in, 0 0 #0000), var(--mw-surface-shadow, 0 0 #0000);
  -webkit-backdrop-filter: var(--mw-surface-filter, none);
  backdrop-filter: var(--mw-surface-filter, none);
}
/* Liquid glass: a 1 px gradient ring for the thickness of the glass, lit from
   the top left. It sits on the border, so it never crosses text. */
:where([data-mw-material="liquid-glass"]) .mw-surface::after {
  content: "";
  position: absolute;
  inset: -1px;
  padding: 1px;
  border-radius: inherit;
  background: linear-gradient(155deg, var(--mw-surface-edge-color) 0%, rgb(255 255 255 / 0) 34%, rgb(255 255 255 / 0) 62%, var(--mw-surface-edge-color) 100%);
  -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 exclude, linear-gradient(#000 0 0);
  pointer-events: none;
}
@media (prefers-reduced-transparency: reduce) {
  :where([data-mw-material="liquid-glass"]) .mw-surface::after { content: none; }
}
/* Forced colours drop backgrounds and shadows: keep the boundary. */
@media (forced-colors: active) {
  .mw-surface { border: 1px solid CanvasText; }
}
