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


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


/**
 * @prop --mds-input-switch-animation-timing-adjust: Multiplier used to fine-tune animation timing
 * @prop --mds-input-switch-animation-timing-function: Easing function applied to switch animations
 * @prop --mds-input-switch-box-color-enabled-checked: Background color of the switch box when enabled and checked
 * @prop --mds-input-switch-box-color-enabled-unchecked: Background color of the switch box when enabled and unchecked
 * @prop --mds-input-switch-box-color-disabled-checked: Background color of the switch box when disabled and checked
 * @prop --mds-input-switch-box-color-disabled-unchecked: Background color of the switch box when disabled and unchecked
 * @prop --mds-input-switch-box-padding-lg: Padding of the switch container (large size)
 * @prop --mds-input-switch-box-padding-md: Padding of the switch container (medium size)
 * @prop --mds-input-switch-box-padding-sm: Padding of the switch container (small size)
 * @prop --mds-input-switch-box-padding: Effective padding used for the switch box
 * @prop --mds-input-switch-duration: Duration of switch state transitions
 * @prop --mds-input-switch-icon-color-checked-disabled: Icon color when checked and disabled
 * @prop --mds-input-switch-icon-color-checked: Icon color when checked
 * @prop --mds-input-switch-icon-color-indeterminate-disabled: Icon color for indeterminate state when disabled
 * @prop --mds-input-switch-icon-color-indeterminate: Icon color for indeterminate state
 * @prop --mds-input-switch-icon-color-unchecked-disabled: Icon color when unchecked and disabled
 * @prop --mds-input-switch-icon-color-unchecked: Icon color when unchecked
 * @prop --mds-input-switch-icon-explicit-color: Explicitly forced icon color
 * @prop --mds-input-switch-toggle-color-enabled-checked: Toggle color when enabled and checked
 * @prop --mds-input-switch-toggle-color-enabled-unchecked: Toggle color when enabled and unchecked
 * @prop --mds-input-switch-toggle-color-disabled-checked: Toggle color when disabled and checked
 * @prop --mds-input-switch-toggle-color-disabled-unchecked: Toggle color when disabled and unchecked
 * @prop --mds-input-switch-toggle-container-size: Computed size of the toggle container
 * @prop --mds-input-switch-toggle-size-lg: Toggle size for large variant
 * @prop --mds-input-switch-toggle-size-md: Toggle size for medium variant
 * @prop --mds-input-switch-toggle-size-sm: Toggle size for small variant
 * @prop --mds-input-switch-toggle-size: Effective toggle size currently in use
 */

/* ❌ Not supported
@property --mds-input-switch-animation-timing-function {
  inherits: false;
  initial-value: var(--ease-in-out-quad);
  syntax: "<easing-function>";
}

@property --mds-input-switch-box-shadow {
  inherits: false;
  initial-value: <missing value>;
  syntax: "<box-shadow>";
}
*/

:host {
  --mds-input-switch-animation-timing-adjust: 1.4;
  --mds-input-switch-animation-timing-function: var(--ease-in-out-quad, cubic-bezier(0.455, 0.03, 0.515, 0.955));
  --mds-input-switch-box-color-enabled-checked: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
  --mds-input-switch-box-color-enabled-unchecked: rgb(var(--magma-border-strong, var(--border-neutral-strong, 140 140 140)));
  --mds-input-switch-box-color-disabled-checked: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-switch-box-color-disabled-unchecked: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-switch-box-padding-lg: --spacing(100);
  --mds-input-switch-box-padding-md: --spacing(100);
  --mds-input-switch-box-padding-sm: --spacing(100);
  --mds-input-switch-box-padding: var(--mds-input-switch-box-padding-md, --spacing(100));
  --mds-input-switch-duration: var(--duration-300, 300ms);
  --mds-input-switch-icon-color-checked-disabled: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
  --mds-input-switch-icon-color-checked: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
  --mds-input-switch-icon-color-indeterminate-disabled: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
  --mds-input-switch-icon-color-indeterminate: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
  --mds-input-switch-icon-color-unchecked-disabled: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
  --mds-input-switch-icon-color-unchecked: rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99)));
  --mds-input-switch-icon-explicit-color: var(--mds-input-switch-icon-color-checked, rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255))));
  --mds-input-switch-toggle-color-enabled-checked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
  --mds-input-switch-toggle-color-enabled-unchecked: rgb(var(--magma-wash-soft, var(--tone-neutral-10, 237 237 237)));
  --mds-input-switch-toggle-color-disabled-checked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
  --mds-input-switch-toggle-color-disabled-unchecked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
  --mds-input-switch-toggle-container-size: calc(
    calc(var(--mds-input-switch-toggle-size-md, --spacing(600)) * 2) +
      calc(var(--mds-input-switch-box-padding-md, --spacing(100)) * 2)
  );
  --mds-input-switch-toggle-size-lg: --spacing(800);
  --mds-input-switch-toggle-size-md: --spacing(600);
  --mds-input-switch-toggle-size-sm: --spacing(400);
  --mds-input-switch-toggle-size: var(--mds-input-switch-toggle-size-md, --spacing(600));

  align-items: flex-start;
  display: inline-flex;
  gap: --spacing(200);
  position: relative;
}

/* visually hidden but focusable: the native input is the control keyboard and screen
   reader users reach, the labels only draw it and toggle it on click. The host is its
   containing block: positioned by a farther ancestor it sat at the unscrolled place of a switch
   in a scrolled box, and the click, which focuses it, scrolled the page there (#1050) */

.field {
  height: 0;
  margin: 0;
  opacity: 0;
  pointer-events: none;
  position: absolute;
  width: 0;
}

.label-icon {
  @apply focus-bounce;

  align-items: baseline;
  border-radius: var(--magma-radius-3xs, var(--radius-squircle-3xs, 6px));
  corner-shape: var(--magma-corner-shape, squircle);
  cursor: pointer;
  display: flex;
  position: relative;
  transition-property: outline-offset, opacity;
}

.field:focus-visible + .label-icon {
  @apply focus-bounce--focused;
}

.label-text {
  cursor: pointer;
  display: flex;
}

.label-text--empty {
  display: none;
}

:host([type='switch']) .label-text {
  align-items: center;
  align-self: center;
}

.icon-explicit {
  fill: var(--mds-input-switch-icon-explicit-color, rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255))));
  height: var(--mds-input-switch-toggle-size, --spacing(600));
  width: var(--mds-input-switch-toggle-size, --spacing(600));
}

.icon-typography-checked,
.icon-typography-unchecked {
  align-items: center;
  display: inline-flex;
}

.icon-typography-checked {
  left: 0;
  position: absolute;
  top: 0;
}

.switch-container {
  @apply focus-bounce;

  align-self: stretch;
  border-radius: var(--magma-radius-full, var(--radius-full, 1000px));
  box-sizing: border-box;
  display: flex;
  flex-shrink: 0;
  transition-property: width, outline-offset, opacity;
  width: var(--mds-input-switch-toggle-container-size, calc( calc(--spacing(600) * 2) + calc(--spacing(100) * 2) ));
}

.field:focus-visible + .switch-container {
  @apply focus-bounce--focused;
}

.switch {
  background-color: var(--mds-input-switch-box-color-enabled-unchecked, rgb(var(--magma-border-strong, var(--border-neutral-strong, 140 140 140))));
  border-radius: var(--magma-radius-full, var(--radius-full, 1000px));
  box-sizing: border-box;
  cursor: pointer;
  display: inline-flex;
  flex-shrink: 0;
  height: min-content;
  padding: var(--mds-input-switch-box-padding, --spacing(100));
  transition-duration: var(--mds-input-switch-duration, var(--duration-300, 300ms));
  transition-property:
    background-color, border-color, box-shadow, color, fill, height, margin, opacity, padding,
    transform, width;
  transition-timing-function: var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1));
  width: var(--mds-input-switch-toggle-container-size, calc( calc(--spacing(600) * 2) + calc(--spacing(100) * 2) ));
}

/* The knob starts in the unchecked colour: it used to take the checked one until the first
   change, because only the rule of a toggled switch (there to keep the uncheck animation from
   running on load) painted it unchecked. In light the two are near (white and wash-soft); in
   dark a switch that was never touched showed the knob of a checked one. */

.switch-toggle {
  align-items: center;
  animation-duration: calc(var(--mds-input-switch-duration, var(--duration-300, 300ms)) * 1.5);
  animation-fill-mode: forwards;
  animation-iteration-count: 1;
  animation-timing-function: var(--mds-input-switch-animation-timing-function);
  background-color: var(--mds-input-switch-toggle-color-enabled-unchecked, rgb(var(--magma-wash-soft, var(--tone-neutral-10, 237 237 237))));
  border-radius: var(--magma-radius-full, var(--radius-full, 1000px));
  box-shadow: var(--shadow-sm, 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1));
  display: flex;
  height: var(--mds-input-switch-toggle-size, --spacing(600));
  justify-content: center;
  transition-duration: var(--duration-200, 200ms);
  transition-property:
    background-color, border-color, box-shadow, color, fill, height, margin, opacity, padding, width;
  transition-timing-function: var(--ease-linear);
  width: var(--mds-input-switch-toggle-size, --spacing(600));
}

.field:checked + .switch-container .switch {
  background-color: var(--mds-input-switch-box-color-enabled-checked, rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255))));
}

.field:checked + .switch-container .switch-toggle {
  background-color: var(--mds-input-switch-toggle-color-enabled-checked, rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255))));
  transform: translateX(100%);
}

/* The knob rests in the place of its state, and slides there only on a change of checked. Its
   place used to be the last frame of the slide, which ran again every time the switch appeared,
   on load and when shown again after being hidden (#924). The transitions above leave out
   transform: a transition outranks an animation, and would cut the slide short. */

.field:checked + .switch-container--sliding .switch-toggle {
  animation-name: check;
}

.field:not(:checked) + .switch-container--sliding .switch-toggle {
  animation-name: uncheck;
}

.field:disabled + .switch-container .switch {
  background-color: var(--mds-input-switch-box-color-disabled-unchecked, rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208))));
}

.field:checked:disabled + .switch-container .switch {
  background-color: var(--mds-input-switch-box-color-disabled-checked, rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208))));
}

.field:disabled + .switch-container .switch-toggle {
  background-color: var(--mds-input-switch-toggle-color-disabled-unchecked, rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255))));
}

.field:checked:disabled + .switch-container .switch-toggle {
  background-color: var(--mds-input-switch-toggle-color-disabled-checked, rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255))));
}

@keyframes check {
  0% {
    transform: translateX(0);
    width: var(--mds-input-switch-toggle-size, --spacing(600));
  }

  50% {
    transform: translateX(0);
    width: calc(
      var(--mds-input-switch-toggle-size, --spacing(600)) * var(--mds-input-switch-animation-timing-adjust, 1.4)
    );
  }

  100% {
    transform: translateX(100%);
    width: var(--mds-input-switch-toggle-size, --spacing(600));
  }
}

@keyframes uncheck {
  0% {
    transform: translateX(100%);
    width: var(--mds-input-switch-toggle-size, --spacing(600));
  }

  50% {
    transform: translateX(0);
    width: calc(
      var(--mds-input-switch-toggle-size, --spacing(600)) * var(--mds-input-switch-animation-timing-adjust, 1.4)
    );
  }

  100% {
    transform: translateX(0);
    width: var(--mds-input-switch-toggle-size, --spacing(600));
  }
}

/* :disabled also covers a disabled fieldset around the switch, which sets no attribute */

:host([disabled]),
:host(:disabled) {
  pointer-events: none;
}

.icon-checked,
.icon-unchecked {
  @apply font-normal;

  transition-duration: var(--duration-200, 200ms);
  transition-property: fill, transform;
  transition-timing-function: var(--ease-out-expo, cubic-bezier(0.19, 1, 0.22, 1));
}

.icon-unchecked {
  fill: var(--mds-input-switch-icon-color-unchecked, rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99))));
  opacity: 1;
  transform: scale(1);
  transition-duration: var(--mds-input-switch-duration, var(--duration-300, 300ms));
}

.icon-checked {
  fill: var(--mds-input-switch-icon-color-checked, rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255))));
  transform: scale(0);
  transition-duration: var(--mds-input-switch-duration, var(--duration-300, 300ms));
}

.field:disabled + .label-icon {
  cursor: default;
}

.field:disabled + .label-icon + .label-text {
  cursor: default;
}

.field:checked + .label-icon .icon-unchecked {
  opacity: 0;
  transform: scale(1.5);
  transition-duration: calc(var(--mds-input-switch-duration, var(--duration-300, 300ms)) * 2);
}

.field:checked + .label-icon .icon-checked {
  transform: scale(1);
  transition-duration: calc(var(--mds-input-switch-duration, var(--duration-300, 300ms)) * 1.5);
}

.field:disabled + .label-icon .icon-unchecked {
  fill: var(--mds-input-switch-icon-color-unchecked-disabled, rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119))));
}

.field:disabled + .label-icon .icon-checked {
  fill: var(--mds-input-switch-icon-color-checked-disabled, rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119))));
}

.field:disabled:indeterminate + .label-icon .icon-checked {
  fill: var(--mds-input-switch-icon-color-indeterminate-disabled, rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119))));
}

.field:indeterminate + .label-icon .icon-checked {
  fill: var(--mds-input-switch-icon-color-indeterminate, rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119))));
  transform: scale(1);
}

:host([explicit]:not([disabled]):not([checked])) {
  --mds-input-switch-icon-explicit-color: var(--mds-input-switch-icon-color-unchecked, rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99))));
}

:host([explicit][checked]:not([disabled])) {
  --mds-input-switch-icon-explicit-color: var(--mds-input-switch-icon-color-checked, rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255))));
}

:host([explicit][checked][disabled]) {
  --mds-input-switch-icon-explicit-color: var(--mds-input-switch-icon-color-checked-disabled, rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119))));
}

:host([explicit][disabled]:not([checked])) {
  --mds-input-switch-icon-explicit-color: var(--mds-input-switch-icon-color-unchecked-disabled, rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119))));
}

/* 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.
 *
 * Unchecked in dark: border-strong (#ababab) made the off track almost as loud as
 * the on one, so it drops to border-default. The knob on it is light, as in light mode:
 * text-default, 4.90:1 on the track. It used to take the fill ink, which in dark is the
 * page colour: 3.35:1, but a dark dot on a grey track reads as a hole, not as a handle.
 * Checked, the knob stays the fill ink: the track is accent-emphasis, a light blue in
 * dark, where the dark knob gets 10.19:1 and a light one would get 1.61:1, so the knob
 * changes colour with the state, as in Material 3. Disabled, the unchecked knob is
 * text-subtle: still light, 3.06:1 on wash-strong (3.56:1 before the palette of #1459), below
 * the enabled one. It was text-disabled, which the palette balanced in #1459 solves darker on
 * the lighter dark surfaces (#9a9a9a): 1.89:1 on the track and under its glyph.
 * On the light off knob, the glyph of `explicit` takes the colour of the track, as if cut
 * out of the knob: border-default at 4.90:1, wash-strong at 3.06:1 when disabled. The text
 * roles it used are light in dark (text-subtle 1.46:1, text-disabled on itself 1:1). */

:host([pref-mode='dark']:not([pref-theme-scheme='light'])),
:host([pref-theme-scheme='dark']) {
  --mds-input-switch-box-color-enabled-checked: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
  --mds-input-switch-box-color-enabled-unchecked: rgb(var(--magma-border-default, var(--border-neutral-default, 183 183 183)));
  --mds-input-switch-box-color-disabled-checked: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-switch-box-color-disabled-unchecked: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-switch-toggle-color-enabled-checked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
  --mds-input-switch-toggle-color-enabled-unchecked: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));
  --mds-input-switch-toggle-color-disabled-checked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
  --mds-input-switch-toggle-color-disabled-unchecked: rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99)));
  --mds-input-switch-icon-color-checked: rgb(var(--magma-accent-border, var(--variant-primary-06, 129 171 251)));
  --mds-input-switch-icon-color-unchecked: rgb(var(--magma-border-default, var(--border-neutral-default, 183 183 183)));
  --mds-input-switch-icon-color-unchecked-disabled: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
  --mds-input-switch-icon-color-indeterminate-disabled: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
  --mds-input-switch-icon-color-indeterminate: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
}

@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-switch-box-color-enabled-checked: rgb(var(--magma-accent-emphasis, var(--variant-primary-04, 11 96 255)));
    --mds-input-switch-box-color-enabled-unchecked: rgb(var(--magma-border-default, var(--border-neutral-default, 183 183 183)));
    --mds-input-switch-box-color-disabled-checked: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
    --mds-input-switch-box-color-disabled-unchecked: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
    --mds-input-switch-toggle-color-enabled-checked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
    --mds-input-switch-toggle-color-enabled-unchecked: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));
    --mds-input-switch-toggle-color-disabled-checked: rgb(var(--magma-text-on-emphasis, var(--tone-neutral-seed, 255 255 255)));
    --mds-input-switch-toggle-color-disabled-unchecked: rgb(var(--magma-text-subtle, var(--text-neutral-subtle, 99 99 99)));
    --mds-input-switch-icon-color-checked: rgb(var(--magma-accent-border, var(--variant-primary-06, 129 171 251)));
    --mds-input-switch-icon-color-unchecked: rgb(var(--magma-border-default, var(--border-neutral-default, 183 183 183)));
    --mds-input-switch-icon-color-unchecked-disabled: rgb(var(--magma-wash-strong, var(--tone-neutral-08, 208 208 208)));
    --mds-input-switch-icon-color-indeterminate-disabled: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
    --mds-input-switch-icon-color-indeterminate: rgb(var(--magma-text-disabled, var(--text-neutral-disabled, 119 119 119)));
  }
}

:host([size='sm']) {
  --mds-input-switch-box-padding: var(--mds-input-switch-box-padding-sm, --spacing(100));
  --mds-input-switch-toggle-size: var(--mds-input-switch-toggle-size-sm, --spacing(400));
  --mds-input-switch-toggle-container-size: calc(
    calc(var(--mds-input-switch-toggle-size-sm, --spacing(400)) * 2) +
      calc(var(--mds-input-switch-box-padding-sm, --spacing(100)) * 2)
  );
}

:host([size='md']) {
  --mds-input-switch-box-padding: var(--mds-input-switch-box-padding-md, --spacing(100));
  --mds-input-switch-toggle-size: var(--mds-input-switch-toggle-size-md, --spacing(600));
  --mds-input-switch-toggle-container-size: calc(
    calc(var(--mds-input-switch-toggle-size-md, --spacing(600)) * 2) +
      calc(var(--mds-input-switch-box-padding-md, --spacing(100)) * 2)
  );
}

:host([size='lg']) {
  --mds-input-switch-box-padding: var(--mds-input-switch-box-padding-lg, --spacing(100));
  --mds-input-switch-toggle-size: var(--mds-input-switch-toggle-size-lg, --spacing(800));
  --mds-input-switch-toggle-container-size: calc(
    calc(var(--mds-input-switch-toggle-size-lg, --spacing(800)) * 2) +
      calc(var(--mds-input-switch-box-padding-lg, --spacing(100)) * 2)
  );
}

