/* Relief: neumorphic surfaces lit by one light.
   The page and every surface are the same colour (the plate). Depth exists
   only because one light, --mw-light-angle, hits the plate: every shadow
   offset is cos()/sin() of that angle, so turning it relights every surface
   in the scope at once.

   Each surface paints two layers with different jobs:
   - the edge: a 1.25 px pair (light toward the source, dark away from it),
     short and strong, so the boundary of a control reaches 3:1 against the
     plate on the side away from the light;
   - the volume: a wide soft pair (offset = depth, blur = depth x 1.9).
   Extruded and carved states are painted once each, on ::before and ::after;
   pressing a control crossfades them. box-shadow itself never animates.

   The angle convention is screen maths: 0deg = light from the right, 90deg =
   from below, 225deg (the default) = from the top left. */

@property --mw-light-angle { syntax: "<angle>"; inherits: true; initial-value: 225deg; }
/* Rotary (relief.js) writes these on the control; registered so a page can
   transition them. */
@property --mw-rotary-p { syntax: "<number>"; inherits: true; initial-value: 0; }
@property --mw-rotary-angle { syntax: "<angle>"; inherits: true; initial-value: 0deg; }

/* Defaults at zero specificity: any page or scope value wins. */
:where(:root) {
  --mw-relief-plate: #e3e6eb;
  --mw-relief-depth: 6px;
}

/* The recipe is declared on every relief surface, and on every element of a
   relief material scope (up to a nested scope of another material), so each
   element reads the angle, depth and plate it inherits at its own level: a
   dial scoped to one section relights only that section. */
.mw-relief,
[data-mw-material="relief"],
[data-mw-material="relief"] :where(:not([data-mw-material]:not([data-mw-material="relief"]), [data-mw-material]:not([data-mw-material="relief"]) *)) {
  /* Static 225deg light where CSS trig is missing (overridden below). */
  --mw-relief-lx: -0.7071;
  --mw-relief-ly: -0.7071;
  --mw-relief-contact: var(--ds-material-contact, 1.25px);
  --mw-relief-contact-blur: .8px;
  --mw-relief-blur-k: 1.9;
  --mw-relief-carve-k: var(--ds-material-carve, .78);
  --mw-relief-edge-hi: rgb(255 255 255 / .92);
  --mw-relief-edge-lo: rgb(28 32 40 / .58);
  --mw-relief-amb-hi: rgb(255 255 255 / .72);
  --mw-relief-amb-lo: rgb(28 32 40 / .2);
  --mw-relief-line: rgb(28 32 40 / .62);

  --mw-relief-edge:
    calc(var(--mw-relief-lx) * var(--mw-relief-contact)) calc(var(--mw-relief-ly) * var(--mw-relief-contact)) var(--mw-relief-contact-blur) var(--mw-relief-edge-hi),
    calc(var(--mw-relief-lx) * -1 * var(--mw-relief-contact)) calc(var(--mw-relief-ly) * -1 * var(--mw-relief-contact)) var(--mw-relief-contact-blur) var(--mw-relief-edge-lo);
  --mw-relief-out:
    calc(var(--mw-relief-lx) * var(--mw-relief-depth)) calc(var(--mw-relief-ly) * var(--mw-relief-depth)) calc(var(--mw-relief-depth) * var(--mw-relief-blur-k)) var(--mw-relief-amb-hi),
    calc(var(--mw-relief-lx) * -1 * var(--mw-relief-depth)) calc(var(--mw-relief-ly) * -1 * var(--mw-relief-depth)) calc(var(--mw-relief-depth) * var(--mw-relief-blur-k)) var(--mw-relief-amb-lo);
  /* Carved: the wall facing the light is in shadow, the far wall is lit. */
  --mw-relief-in:
    inset calc(var(--mw-relief-lx) * -1 * var(--mw-relief-contact)) calc(var(--mw-relief-ly) * -1 * var(--mw-relief-contact)) var(--mw-relief-contact-blur) var(--mw-relief-edge-lo),
    inset calc(var(--mw-relief-lx) * var(--mw-relief-contact)) calc(var(--mw-relief-ly) * var(--mw-relief-contact)) var(--mw-relief-contact-blur) var(--mw-relief-edge-hi),
    inset calc(var(--mw-relief-lx) * -1 * var(--mw-relief-depth) * var(--mw-relief-carve-k)) calc(var(--mw-relief-ly) * -1 * var(--mw-relief-depth) * var(--mw-relief-carve-k)) calc(var(--mw-relief-depth) * var(--mw-relief-blur-k)) var(--mw-relief-amb-lo),
    inset calc(var(--mw-relief-lx) * var(--mw-relief-depth) * var(--mw-relief-carve-k)) calc(var(--mw-relief-ly) * var(--mw-relief-depth) * var(--mw-relief-carve-k)) calc(var(--mw-relief-depth) * var(--mw-relief-blur-k)) var(--mw-relief-amb-hi);
}

@supports (width: calc(1px * cos(0deg))) {
  .mw-relief,
  [data-mw-material="relief"],
  [data-mw-material="relief"] :where(:not([data-mw-material]:not([data-mw-material="relief"]), [data-mw-material]:not([data-mw-material="relief"]) *)) {
    --mw-relief-lx: cos(var(--mw-light-angle));
    --mw-relief-ly: sin(var(--mw-light-angle));
  }
}

/* Shadow colours derived from the plate. Symmetric lightness offsets in OKLab
   need no branch for light or dark plates: on a light plate the dark edge
   carries the 3:1 boundary (white cannot), on a dark plate the light edge
   does (black cannot), and the wide highlight stays soft on dark plates,
   which keeps them from reading as glossy plastic. */
@supports (color: oklab(from red l a b)) {
  .mw-relief,
  [data-mw-material="relief"],
  [data-mw-material="relief"] :where(:not([data-mw-material]:not([data-mw-material="relief"]), [data-mw-material]:not([data-mw-material="relief"]) *)) {
    /* The boundary colour: whichever side reaches 3:1 on this plate. */
    --mw-relief-line: oklab(from var(--mw-relief-plate) calc(l + .4 - .8 * clamp(0, (l - .55) * 100, 1)) a b);
    --mw-relief-edge-hi: oklab(from var(--mw-relief-plate) min(1, calc(l + .38)) calc(a * .5) calc(b * .5));
    --mw-relief-edge-lo: oklab(from var(--mw-relief-plate) max(0, calc(l - .38)) calc(a * 1.2) calc(b * 1.2));
    --mw-relief-amb-hi: oklab(from var(--mw-relief-plate) min(1, calc(l + .12)) a b / .78);
    --mw-relief-amb-lo: oklab(from var(--mw-relief-plate) max(0, calc(l - .26)) calc(a * 1.3) calc(b * 1.3) / .55);
  }
}

/* ---- the surface ------------------------------------------------------------
   z-index: -1 inside an isolated stacking context paints the layers above the
   element's own background and below its text. The element keeps a real
   background (the same face colour) so contrast checks that read CSS see the
   colour the text sits on. */
.mw-relief {
  --mw-relief-face-c: var(--mw-relief-face, var(--mw-relief-plate));
  position: relative;
  isolation: isolate;
  border: 0;
  border-radius: var(--mw-relief-radius, 18px);
  background-color: var(--mw-relief-face-c);
}
.mw-relief::before,
.mw-relief::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  border-radius: inherit;
  background-color: var(--mw-relief-face-c);
  pointer-events: none;
  transition: opacity .42s var(--mw-relief-settle);
}
.mw-relief::before { box-shadow: var(--mw-relief-edge), var(--mw-relief-out); }
.mw-relief::after { box-shadow: var(--mw-relief-in); opacity: 0; }

/* Carved from the start: wells, grooves, input fields. */
.mw-relief--in::before { opacity: 0; }
.mw-relief--in::after { opacity: 1; }
/* Flat: the plate itself, for dense content where relief turns to wallpaper. */
.mw-relief--flat::before { box-shadow: none; }
.mw-relief--flat::after { opacity: 0; }

/* ---- the press ------------------------------------------------------------
   Extruded becomes carved under the finger: fast in (the material yields),
   slow out (it settles back). The pressed layer takes 8% of occlusion so the
   inversion is visible even at small depths. A toggle that is on stays carved
   and presses back out. */
.mw-press { cursor: pointer; transition: scale .42s var(--mw-relief-settle), translate .42s var(--mw-relief-settle); }
.mw-press:active { scale: .97; transition-duration: .13s; transition-timing-function: var(--mw-relief-yield); }
.mw-press:active::after,
.mw-press[aria-pressed="true"]::after,
.mw-press[aria-checked="true"]::after { background-color: color-mix(in oklab, var(--mw-relief-face-c), var(--mw-relief-edge-lo) calc(var(--ds-material-occlusion, .08) * 100%)); }
.mw-press:active,
.mw-press[aria-pressed="true"],
.mw-press[aria-checked="true"] { --mw-relief-carve-k: 1.02; }
.mw-press:active::before,
.mw-press[aria-pressed="true"]::before,
.mw-press[aria-checked="true"]::before { opacity: 0; }
.mw-press:active::after,
.mw-press[aria-pressed="true"]::after,
.mw-press[aria-checked="true"]::after { opacity: 1; }
.mw-press[aria-pressed="true"]:active::before,
.mw-press[aria-checked="true"]:active::before { opacity: 1; }
.mw-press[aria-pressed="true"]:active::after,
.mw-press[aria-checked="true"]:active::after { opacity: 0; }
.mw-press:active::before,
.mw-press:active::after { transition-duration: .13s; transition-timing-function: var(--mw-relief-yield); }

/* Hover leans the control a hair toward the light (fine pointers only). */
@media (hover: hover) and (pointer: fine) {
  .mw-press:hover { translate: calc(var(--mw-relief-lx) * .7px) calc(var(--mw-relief-ly) * .7px); }
}

/* Disabled is a flat plate with an engraved hatch, never opacity, so the label
   keeps its contrast. */
.mw-relief:disabled,
.mw-relief[aria-disabled="true"] { cursor: not-allowed; }
.mw-relief:disabled::after,
.mw-relief[aria-disabled="true"]::after { opacity: 0; }
.mw-relief:disabled::before,
.mw-relief[aria-disabled="true"]::before {
  opacity: 1;
  box-shadow: inset 0 0 0 1.5px var(--mw-relief-edge-lo);
  background-image: repeating-linear-gradient(45deg, color-mix(in oklab, var(--mw-relief-edge-lo) 45%, transparent) 0 1.5px, transparent 1.5px 8px);
}
.mw-relief:disabled:active,
.mw-relief[aria-disabled="true"]:active { scale: none; }

.mw-relief:focus-visible { outline: 3px solid var(--mw-relief-focus, currentColor); outline-offset: 4px; }

/* Shared motion tokens: overdamped springs (critically damped press, slightly
   overdamped release). Clay does not bounce. */
:where(:root) {
  --mw-relief-yield: linear(0, .2278 10%, .5378 20%, .7523 30%, .8754 40%, .9401 50%, .9723 60%, .9878 70%, .9951 80%, .9985 90%, 1);
  --mw-relief-settle: linear(0, .089 6%, .2569 13%, .4265 19%, .5713 25%, .6859 31%, .7732 38%, .838 44%, .8856 50%, .9202 56%, .9451 63%, .9631 69%, .976 75%, .9852 81%, .9919 88%, .9966 94%, 1);
}

/* Lite (Relief() sets it on crowded pages or low-memory devices): the widest
   raster, the soft highlight, is dropped; the edge pair stays. */
[data-mw-relief="lite"] .mw-relief,
[data-mw-relief="lite"] [data-mw-material="relief"] *,
[data-mw-material="relief"][data-mw-relief="lite"] * { --mw-relief-amb-hi: transparent; }

/* ---- rotary control and light dial ------------------------------------------ */
.mw-rotary { touch-action: none; cursor: grab; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
.mw-rotary[data-mw-dragging] { cursor: grabbing; }
.mw-rotary:focus-visible { outline: 3px solid var(--mw-relief-focus, currentColor); outline-offset: 5px; }

/* The light dial turns the light. A carved ring, an extruded ribbed body whose
   mark points at the source, and a carved cap with the angle. */
.mw-light-dial {
  --mw-light-dial-size: 104px;
  position: relative;
  display: grid;
  flex: none;
  place-items: center;
  width: var(--mw-light-dial-size);
  height: var(--mw-light-dial-size);
  border-radius: 50%;
}
.mw-light-dial > .mw-light-dial__ring,
.mw-light-dial > .mw-light-dial__body,
.mw-light-dial > .mw-light-dial__cap { position: absolute; border-radius: 50%; pointer-events: none; }
.mw-light-dial__ring { --mw-relief-depth: 4px; inset: 0; }
.mw-light-dial__body { --mw-relief-depth: 6px; inset: 11%; rotate: calc(var(--mw-light-angle) + 90deg); }
.mw-light-dial__ribs {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: repeating-conic-gradient(color-mix(in oklab, var(--mw-relief-edge-lo) 26%, transparent) 0 2deg, transparent 2deg 10deg);
  -webkit-mask: radial-gradient(closest-side, transparent 58%, #000 60%, #000 92%, transparent 94%);
  mask: radial-gradient(closest-side, transparent 58%, #000 60%, #000 92%, transparent 94%);
}
.mw-light-dial__mark {
  position: absolute;
  top: 7%;
  left: 50%;
  width: 6px;
  height: 18%;
  translate: -50% 0;
  border-radius: 3px;
  background: var(--mw-light-dial-mark, var(--mw-relief-focus, currentColor));
}
.mw-light-dial__cap {
  --mw-relief-depth: 3px;
  inset: 28%;
  display: grid;
  place-items: center;
}
.mw-light-dial__value {
  position: relative;
  font-size: calc(var(--mw-light-dial-size) * .14);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
  line-height: 1;
}
/* The light glides while it follows the dial (LightDial sets data-mw-light). */
[data-mw-light] { transition: --mw-light-angle .18s linear; }

/* ---- material preset ----------------------------------------------------------
   [data-mw-material="relief"] on <html> or a section turns its cards and
   panels (anything reading --mw-surface-*, such as .ds-card and .mw-surface)
   into relief, from the design system roles when one is set. Relief needs
   the surface and the page to be the same colour, so both come from
   --ds-background. A system tunes it with --ds-material-offset (depth,
   default 6px), --ds-material-angle (light, default 225deg),
   --ds-material-contact (edge offset, 1.25px), --ds-material-carve (how deep a
   carve reads against an extrusion, .78) and --ds-material-occlusion (the
   darkening of a pressed face, .08).
   Relief has no border: the 3:1 boundary is the contact pair, the first two
   layers of --mw-surface-shadow, so --mw-surface-edge is an empty border and
   --mw-surface-shadow-in is empty at rest. --mw-surface-shadow-carved (an
   addition) is the carved set, for wells, inputs and pressed states.
   Like surface.css, the scope values are recomputed below an element that
   switches data-mw-mode or data-mw-ds. */
[data-mw-material="relief"],
:where([data-mw-material="relief"] :is([data-mw-mode], [data-mw-ds])) {
  --mw-relief-plate: rgb(var(--ds-background-rgb, 227 230 235));
  --mw-relief-radius: var(--ds-radius-lg, 22px);
  --mw-relief-focus: var(--ds-focus, #3346c4);
  --mw-surface-tint: var(--mw-relief-plate);
  --mw-surface-bg: var(--mw-relief-plate);
  --mw-surface-bg-solid: var(--mw-relief-plate);
  --mw-surface-filter: none;
  --mw-surface-blur: 0px;
  --mw-surface-saturate: 1;
  --mw-surface-edge: 0 solid transparent;
  --mw-surface-edge-color: var(--mw-relief-line);
  --mw-surface-sheen: none;
  --mw-surface-texture: none;
  --mw-surface-radius: var(--ds-radius-lg, 22px);
  --mw-surface-ink: var(--ds-text, #262a31);
  --mw-surface-ink-muted: var(--ds-text-muted, #4c525c);
}
[data-mw-material="relief"] {
  --mw-light-angle: var(--ds-material-angle, 225deg);
  --mw-relief-depth: var(--ds-material-offset, 6px);
  background-color: var(--mw-relief-plate);
  color: var(--mw-surface-ink);
}
/* Shadow lists, declared per element so they follow the angle and depth
   each element inherits. */
[data-mw-material="relief"],
[data-mw-material="relief"] :where(:not([data-mw-material]:not([data-mw-material="relief"]), [data-mw-material]:not([data-mw-material="relief"]) *)) {
  --mw-surface-shadow: var(--mw-relief-edge), var(--mw-relief-out);
  --mw-surface-shadow-in: 0 0 #0000;
  --mw-surface-shadow-carved: var(--mw-relief-in);
}
/* Any element becomes a relief surface (the same rule as surface.css, so
   either sheet alone gives the same result). Use .mw-relief + .mw-press for
   controls that press. */
[data-mw-material="relief"] .mw-surface {
  border: var(--mw-surface-edge);
  border-radius: var(--mw-surface-radius);
  background: var(--mw-surface-sheen), var(--mw-surface-texture), var(--mw-surface-bg);
  color: var(--mw-surface-ink);
  box-shadow: var(--mw-surface-shadow-in), var(--mw-surface-shadow);
}

/* ---- reduced motion and forced colours ----------------------------------------
   The depth inversion stays under reduced motion: it is the feedback that a
   control was operated. Forced colours drop every shadow, so a plain outline
   keeps each surface's boundary. */
@media (prefers-reduced-motion: reduce) {
  .mw-relief::before,
  .mw-relief::after,
  .mw-press { transition-duration: 1ms !important; }
  [data-mw-light] { transition: none; }
}
@media (forced-colors: active) {
  .mw-relief,
  [data-mw-material="relief"] .mw-surface { outline: 1px solid CanvasText; outline-offset: -1px; }
}
