/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

:host {
  --toggle-background-color: var(--button-background-color);
  --toggle-background-color-hover: var(--button-background-color-hover);
  --toggle-background-color-active: var(--button-background-color-active);
  --toggle-background-color-pressed: var(--color-accent-primary);
  --toggle-background-color-pressed-hover: var(--color-accent-primary-hover);
  --toggle-background-color-pressed-active: var(--color-accent-primary-active);
  --toggle-border-color: var(--border-color-interactive);
  --toggle-border-color-hover: var(--toggle-border-color);
  --toggle-border-color-active: var(--toggle-border-color);
  --toggle-border-radius: var(--border-radius-circle);
  --toggle-border-width: var(--border-width);
  --toggle-height: var(--size-item-small);
  --toggle-width: var(--size-item-large);
  --toggle-dot-background-color: var(--toggle-border-color);
  --toggle-dot-background-color-hover: var(--toggle-dot-background-color);
  --toggle-dot-background-color-active: var(--toggle-dot-background-color);
  --toggle-dot-background-color-on-pressed: var(--background-color-canvas);
  --toggle-dot-margin: 1px;
  --toggle-dot-height: calc(var(--toggle-height) - 2 * var(--toggle-dot-margin) - 2 * var(--toggle-border-width));
  --toggle-dot-width: var(--toggle-dot-height);
  --toggle-dot-transform-x: calc(var(--toggle-width) - 2 * var(--toggle-dot-margin) - 2px - var(--toggle-dot-width));
  --input-width: var(--toggle-width);
}

.toggle-button {
  appearance: none;
  padding: 0;
  border: var(--toggle-border-width) solid var(--toggle-border-color);
  height: var(--toggle-height);
  width: var(--toggle-width);
  border-radius: var(--toggle-border-radius);
  background-color: var(--toggle-background-color);
  box-sizing: border-box;

  &:focus-visible {
    outline: var(--focus-outline);
    outline-offset: var(--focus-outline-offset);
  }

  &:enabled:hover {
    background-color: var(--toggle-background-color-hover);
    border-color: var(--toggle-border-color);
  }

  &:enabled:hover:active {
    background-color: var(--toggle-background-color-active);
    border-color: var(--toggle-border-color);
  }

  &::before {
    display: block;
    content: "";
    background-color: var(--toggle-dot-background-color);
    height: var(--toggle-dot-height);
    width: var(--toggle-dot-width);
    margin: var(--toggle-dot-margin);
    border-radius: var(--toggle-border-radius);
    translate: 0;
  }
}

.toggle-button[aria-pressed="true"] {
  background-color: var(--toggle-background-color-pressed);
  border-color: transparent;

  &:enabled:hover {
    background-color: var(--toggle-background-color-pressed-hover);
    border-color: transparent;
  }

  &:enabled:hover:active {
    background-color: var(--toggle-background-color-pressed-active);
    border-color: transparent;
  }

  &::before {
    translate: var(--toggle-dot-transform-x);
    background-color: var(--toggle-dot-background-color-on-pressed);
  }

  &:enabled:hover::before,
  &:enabled:hover:active::before {
    background-color: var(--toggle-dot-background-color-on-pressed);
  }

  &:-moz-locale-dir(rtl)::before,
  &:dir(rtl)::before {
    translate: calc(-1 * var(--toggle-dot-transform-x));
  }
}

@media (prefers-reduced-motion: no-preference) {
  .toggle-button::before {
    transition: translate 100ms;
  }
}

@media (prefers-contrast) {
  .toggle-button:enabled:hover {
    border-color: var(--toggle-border-color-hover);
  }

  .toggle-button:enabled:hover:active {
    border-color: var(--toggle-border-color-active);
  }

  .toggle-button[aria-pressed="true"]:enabled {
    border-color: var(--toggle-border-color);
    position: relative;

    &:hover {
      border-color: var(--toggle-border-color-hover);

      &:active {
        background-color: var(--toggle-dot-background-color-active);
        border-color: var(--toggle-dot-background-color-hover);
      }
    }
  }

  .toggle-button:enabled:hover::before,
  .toggle-button:enabled:hover:active::before {
    background-color: var(--toggle-dot-background-color-hover);
  }
}

@media (forced-colors) {
  :host {
    --toggle-dot-background-color: var(--color-accent-primary);
    --toggle-dot-background-color-hover: var(--color-accent-primary-hover);
    --toggle-dot-background-color-active: var(--color-accent-primary-active);
    --toggle-dot-background-color-on-pressed: var(--button-background-color);
    --toggle-border-color-hover: var(--border-color-interactive-hover);
    --toggle-border-color-active: var(--border-color-interactive-active);
  }

  .toggle-button[aria-pressed="true"]:enabled::after {
    border: 1px solid var(--button-background-color);
    content: "";
    position: absolute;
    height: var(--toggle-height);
    width: var(--toggle-width);
    display: block;
    border-radius: var(--toggle-border-radius);
    inset: -2px;
  }

  .toggle-button[aria-pressed="true"]:enabled:hover:active::after {
    border-color: var(--toggle-border-color-active);
  }
}

@media not (forced-colors) {
  :host(:state(disabled)) .toggle-button {
    opacity: 0.4;
  }
}
