:host(:not(:is([hydrated], .hydrated))) {
  @apply not-hydrated;
}


mds-text {
  &:not(:is([hydrated], .hydrated)) {
    @apply not-hydrated;
  }
}


/**
 * @prop --mds-input-range-thumb-background-disabled: Sets the thumb background color when the component is disabled
 * @prop --mds-input-range-thumb-background: Sets the thumb background color
 * @prop --mds-input-range-thumb-cursor: Sets the thumb cursor
 * @prop --mds-input-range-thumb-size: Sets the thumb width and height of the component
 * @prop --mds-input-range-track-background-disabled: Sets the track background color when the component is disabled
 * @prop --mds-input-range-track-background: Sets the track background color
 * @prop --mds-input-range-track-progress-background-disabled: Sets the track progress background color when the component is disabled
 * @prop --mds-input-range-track-progress-background: Sets the track progress background color
 * @prop --mds-input-range-track-size: Sets the track width and height
 */

:host {
  --mds-input-range-transition-duration: var(--duration-300, 300ms);
  --private-input-range-transition-duration: var(--mds-input-range-transition-duration);
  --mds-input-range-thumb-background-disabled: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-range-thumb-background: rgb(var(--magma-surface-overlay, var(--surface-neutral-overlay, 255 255 255)));
  --mds-input-range-thumb-cursor: col-resize;
  --mds-input-range-thumb-size: --spacing(400);
  --mds-input-range-track-background-disabled: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-range-track-background: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-range-track-progress-background-disabled: rgb(var(--magma-border-strong, var(--border-neutral-strong, 140 140 140)));
  --mds-input-range-track-progress-background: rgb(var(--magma-accent-emphasis-hover, var(--variant-primary-03, 0 73 211)));
  --mds-input-range-track-size: --spacing(600);

  /* private properties */
  --private-border-width: calc(
    calc(var(--mds-input-range-track-size) - var(--mds-input-range-thumb-size)) / 2
  );
  --private-thumb-size: calc(var(--mds-input-range-thumb-size) + var(--private-border-width) * 2);

  /* the shadow of the tip of a value, as the one of mds-tooltip; high contrast trades it for a
   * glow (css/mds-input-range-pref-contrast.css) */
  --private-input-range-tip-shadow: drop-shadow(
      0 --spacing(250) --spacing(200) rgb(var(--magma-shadow-ink, var(--tone-neutral-01, 5 5 5)) / 0.04)
    )
    drop-shadow(0 --spacing(100) --spacing(75) rgb(var(--magma-shadow-ink, var(--tone-neutral-01, 5 5 5)) / 0.1))
    drop-shadow(0 0 1px rgb(var(--magma-shadow-ink, var(--tone-neutral-01, 5 5 5)) / 0.1));

  display: grid;
  gap: --spacing(200);

  /* one column as wide as the host, never as its content: an implicit auto column grew to the
   * min-content of the label and of the native input, and the parts overflowed a narrower host
   * (#1314) */
  grid-template-columns: minmax(0, 1fr);
  transition-duration: var(--private-input-range-transition-duration);
  transition-property: color;
  transition-timing-function: var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1));
}

.header {
  display: flex;
  gap: --spacing(200);
}

/* truncate on the host gave no ellipsis: the text sits in a block of the shadow root of
 * mds-text, which truncates it itself (truncate="word") */

.label {
  min-width: 0;
}

/* a value is data, as an <option>: its text names its thumb, and stays out of the label (#1261) */

::slotted(mds-input-range-value) {
  display: none;
}

.value {
  margin-left: auto;
}

.range {
  align-items: center;
  display: flex;
  height: var(--private-thumb-size);
  position: relative;
}

.track {
  box-sizing: border-box;
  position: absolute;
  width: 100%;
}

.field {
  appearance: none;
  background-color: transparent;
  border-radius: var(--magma-radius-full, var(--radius-full, 1000px));
  flex-grow: 1;
  height: --spacing(400);
  margin: 0;

  /* a native range input is 129 px wide in Chromium, and the automatic minimum of a flex item
   * kept it so in a narrower track */
  min-width: 0;

  /* the ring of the focus is on the thumb: the outline of the browser would go round the track */
  outline: none;
  z-index: 20;
}

/* the ring of the focus goes round the thumb, not round the whole track as the outline of the
 * input did: with several thumbs, each has its own (#1261) */

.field:focus-visible::-webkit-slider-thumb {
  @apply focus-bounce--focused;
}

.field::-webkit-slider-thumb {
  @apply focus-bounce;

  appearance: none;
  background-color: var(--mds-input-range-thumb-background);
  border-color: var(--mds-input-range-track-progress-background);
  border-radius: var(--mds-input-range-thumb-size);
  border-style: solid;
  border-width: var(--private-border-width);
  cursor: var(--mds-input-range-thumb-cursor);
  height: var(--private-thumb-size);
  line-height: 0;
  transform: translate(0, -50%);
  -webkit-transition-property: background-color, border;
  transition-property: background-color, border;
  width: var(--private-thumb-size);
}

.field::-webkit-slider-runnable-track {
  appearance: none;
  border: 0;
  height: 0;
  width: 100%;
}

.track-progress {
  background-color: var(--mds-input-range-track-progress-background);
  display: flex;
  height: var(--mds-input-range-track-size);
  transition-duration: var(--private-input-range-transition-duration);
  transition-property: background-color;

  /*
    Align fill end with thumb center.
    Thumb center travels across (100% - thumbSize); at 0% and 100% it's inset by thumbSize/2.
    We use track size as the thumb's outer size (matches current thumb calc).
  */
  width: calc(
    (100% - var(--mds-input-range-track-size)) * (var(--mds-input-range-progress, 0) * 0.01) +
      (var(--mds-input-range-track-size) / 2)
  );
}

/* several thumbs: the fill runs from the centre of the first to the centre of the last */

.track-progress--between {
  margin-left: calc(
    (100% - var(--mds-input-range-track-size)) *
      (var(--private-input-range-progress-start, 0) * 0.01) +
      (var(--mds-input-range-track-size) / 2)
  );
  width: calc(
    (100% - var(--mds-input-range-track-size)) *
      ((var(--mds-input-range-progress, 0) - var(--private-input-range-progress-start, 0)) * 0.01)
  );
}

/* Several thumbs (#1261): a native input each, all as wide as the track and stacked on it, so
 * that a value sits at the same place on every input. An input lets the pointer through to the
 * track, which moves the nearest thumb; its thumb catches the pointer and drags itself. */

.range--several .field {
  inset-inline: 0;
  pointer-events: none;
  position: absolute;
}

.range--several .field::-webkit-slider-thumb {
  pointer-events: auto;
}

/* Two thumbs on one value lie on each other (a thumb is --spacing(600) wide, a step can be far
 * less): the one with the focus lies on top, and the keys move it alone. Without the focus, a
 * thumb held by its neighbours at its value lies under them, so that the one on top can move:
 * of two thumbs on `max`, the first, which has room before it. */

.range--several .field--held {
  z-index: 10;
}

.range--several .field:focus {
  z-index: 30;
}

/* The value of a thumb in a tip over it, one thumb or several: the header holds the values at its
 * end, far from the thumb that moves. It shows while the input of its thumb is under the pointer,
 * pressed (a drag keeps it, wherever the pointer goes) or focused from the keyboard, and follows
 * the thumb at once. The input of the one value takes the whole track, so its tip shows with the
 * pointer anywhere on the track; the inputs of several thumbs let the pointer through but on the
 * thumb (.range--several), so each tip shows with the pointer on its thumb.
 *
 * Nothing is measured: the tip is a strip as wide as the track, over it, and a container, so that
 * its parts know that width (100cqi). Its centre is the centre of the thumb, from the same value
 * the fill takes (--private-input-range-tip-position, 0 to 100). The arrow stays on that centre;
 * the bubble is centred on it, and slides back inside the track near its ends. It looks like
 * mds-tooltip: overlay surface, radius, drop shadow.
 *
 * The bubble lies on the line of the header, its value on the line of the values of the header:
 * the header is as tall as its capitals (mds-text trims the label) and ends --spacing(200) over
 * the track (the gap of the host), so a bubble padded by --spacing(100) ends --spacing(100) over
 * the track. It covers what of the header lies under it whole: a tip set higher covered the top
 * of the letters and left their foot out. The arrow fills the --spacing(100) left, down to the
 * thumb. */

.tip {
  --private-input-range-tip-centre: calc(
    (100cqi - var(--mds-input-range-track-size)) *
      (var(--private-input-range-tip-position, 0) * 0.01) + (var(--mds-input-range-track-size) / 2)
  );

  bottom: 100%;
  container-type: inline-size;
  filter: var(--private-input-range-tip-shadow);
  height: 0;
  inset-inline: 0;
  opacity: 0;
  pointer-events: none;
  position: absolute;
  transition-duration: var(--private-input-range-transition-duration);
  transition-property: opacity;
  transition-timing-function: var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1));
  z-index: 40;
}

.field:hover:not(:disabled) + .tip,
.field:active:not(:disabled) + .tip,
.field:focus-visible + .tip {
  opacity: 1;
}

.tip-bubble {
  background-color: rgb(var(--magma-surface-overlay, var(--surface-neutral-overlay, 255 255 255)));
  border-radius: var(--magma-radius-3xs, var(--radius-squircle-3xs, 6px));
  bottom: --spacing(100);
  box-sizing: border-box;
  color: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));
  corner-shape: var(--magma-corner-shape, squircle);
  display: flex;
  left: var(--private-input-range-tip-centre);
  padding-block: --spacing(100);
  padding-inline: --spacing(200);
  position: absolute;

  /* centred on the thumb, inside the track: its left edge from 0 to the track less the bubble */
  translate: clamp(
      calc(var(--private-input-range-tip-centre) * -1),
      -50%,
      calc(100cqi - 100% - var(--private-input-range-tip-centre))
    )
    0;
  white-space: nowrap;

  /* as wide as its value: an absolute box near the end of the track would wrap it */
  width: max-content;
}

/* the arrow of mds-tooltip is --spacing(225) tall: here it has --spacing(100), and a triangle
 * drawn in CSS keeps its point where a squeezed copy of that shape would round it off */

.tip-arrow {
  background-color: rgb(var(--magma-surface-overlay, var(--surface-neutral-overlay, 255 255 255)));
  bottom: 0;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  height: --spacing(100);
  left: var(--private-input-range-tip-centre);
  position: absolute;
  translate: -50% 0;
  width: --spacing(250);
}

.track-total {
  background-color: var(--mds-input-range-track-background);
  border-radius: var(--mds-input-range-track-size);
  display: flex;
  height: var(--mds-input-range-track-size);
  overflow: hidden;
  transition-duration: var(--private-input-range-transition-duration);
  transition-property: background-color;
  transition-timing-function: var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1));
  width: 100%;
}

.contrast-area {
  border-radius: var(--mds-input-range-track-size);
  margin-left: --spacing(100);
  margin-right: --spacing(100);
}

/* Motion, held back.
 *
 * Every component zeroes the private durations its own base CSS reads, which composes
 * with the public knobs - but that only reaches the declarations that read them. An audit
 * of the 52 sheets found 21 transitions in 15 components that did not: the host's own
 * transition in mds-button and mds-table, the focus ring of the input family, the tab
 * slider, the close button of mds-note. This reset reaches the whole shadow tree, which is
 * what the preference promises, and keeps reaching it when a declaration is added.
 *
 * Transitions only, on purpose. A keyframe animation is usually a loader, and a loader has
 * to keep saying "working": mds-spinner slows to 8s under reduced motion instead of
 * freezing, and a duration of nearly zero on an infinite animation would spin it out of
 * control instead of stopping it.
 *
 * 0.01ms rather than 0s, so a component that waits for its own transitionend still gets
 * one: mds-modal, mds-push-notification and mds-tree-item all listen for it, and a
 * transition of zero never fires it. */

/* stylelint-disable time-min-milliseconds -- the floor is there to keep a transition
   perceivable; the point of these two is that it is not. */

:host([pref-animation='reduce']),
:host([pref-animation='reduce']) *,
:host([pref-animation='reduce']) *::before,
:host([pref-animation='reduce']) *::after {
  transition-duration: 0.01ms !important;
}

@media (prefers-reduced-motion: reduce) {
  :host([pref-animation='system']),
  :host([pref-animation='system']) *,
  :host([pref-animation='system']) *::before,
  :host([pref-animation='system']) *::after,
  :host(:not([pref-animation])),
  :host(:not([pref-animation])) *,
  :host(:not([pref-animation])) *::before,
  :host(:not([pref-animation])) *::after {
    transition-duration: 0.01ms !important;
  }
}

/* stylelint-enable time-min-milliseconds */


/* Animation preference. The component mirrors the resolved mode from <html>
 * onto its host as pref-animation (see subscribePreference in @common/preference);
 * these plain shadow selectors then zero the private duration(s) the base CSS
 * reads. `reduce` always stops; `system` and no controller follow the OS via
 * @media; `no-preference` keeps the base value. Customization composes (the base
 * sets --private from the public token, this only overrides it when reduced). */

:host([pref-animation='reduce']) {
  --private-input-range-transition-duration: 0s;
}

@media (prefers-reduced-motion: reduce) {
  :host([pref-animation='system']),
  :host(:not([pref-animation])) {
    --private-input-range-transition-duration: 0s;
  }
}

/* Theme preference. The component reflects the resolved theme mode and scheme
 * from <html> onto its host as pref-mode + pref-theme-scheme (see
 * subscribePreference in @common/preference); these plain shadow selectors refine
 * for dark. `dark` is the explicit choice; `system` and no controller follow the
 * OS via @media; the scheme axis gates it. Cross-browser, no :host-context.
 *
 * In dark the progress is accent-emphasis, a light blue, and the thumb sits on it inside a ring of
 * the same colour: it takes the ink of that fill, text-on-emphasis, as the knob of an
 * mds-input-switch that is on (#1141). 10.19:1 on the progress, 3.16:1 on the empty track.
 * It used to take accent-emphasis, the colour of its ring and of the progress, so the three
 * were one shape (#1073); then text-default, light like the progress, 1.61:1 on it. */

:host([pref-mode='dark']:not([pref-theme-scheme='light'])),
:host([pref-theme-scheme='dark']) {
  --mds-input-range-thumb-background: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
  --mds-input-range-track-progress-background: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
}

@media (prefers-color-scheme: dark) {
  :host([pref-mode='system']:not([pref-theme-scheme='light'])),
  :host(:not([pref-mode]):not([pref-theme-scheme='light'])) {
    --mds-input-range-thumb-background: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
    --mds-input-range-track-progress-background: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
  }
}

/* Contrast preference. The component reflects the resolved mode from <html> onto
 * its host as pref-contrast (see subscribePreference in @common/preference); these
 * plain shadow selectors refine for high contrast. `more` is the explicit choice;
 * `system` and no controller follow the OS via @media. Cross-browser, no :host-context.
 * The tip of a value takes the glow of mds-tooltip in high contrast: a centred glow is an
 * outline, not a projected shadow, so it names --magma-text-default, which flips with the mode. */

:host([pref-contrast='more']) {
  --private-input-range-tip-shadow: drop-shadow(
      0 0 --spacing(100) rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)) / 0.05)
    )
    drop-shadow(0 0 --spacing(100) rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)) / 0.3))
    drop-shadow(0 0 --spacing(100) rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)) / 0.3));

  color: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));
}

@media (prefers-contrast: more) {
  :host([pref-contrast='system']),
  :host(:not([pref-contrast])) {
    --private-input-range-tip-shadow: drop-shadow(
        0 0 --spacing(100) rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)) / 0.05)
      )
      drop-shadow(0 0 --spacing(100) rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)) / 0.3))
      drop-shadow(0 0 --spacing(100) rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)) / 0.3));

    color: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));
  }
}


/* after the pref-* sheets, whose selectors weigh the same: a disabled range keeps its colours in
 * dark too */
/* :disabled also covers a disabled fieldset around the range, which sets no attribute */

:host([disabled]:not([disabled='false'])),
:host(:disabled:not([disabled='false'])) {
  --mds-input-range-thumb-background: var(--mds-input-range-thumb-background-disabled);
  --mds-input-range-track-background: var(--mds-input-range-track-background-disabled);
  --mds-input-range-track-progress-background: var(
    --mds-input-range-track-progress-background-disabled
  );
}

:host([disabled]:not([disabled='false'])) .field::-webkit-slider-thumb,
:host(:disabled:not([disabled='false'])) .field::-webkit-slider-thumb {
  cursor: not-allowed;
}

