/* 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/. */

.toggle-button {
  --button-background-color: color-mix(in srgb, currentColor 7%, transparent);
  --button-background-color-hover: color-mix(
    in srgb,
    currentColor 14%,
    transparent
  );
  --button-background-color-active: color-mix(
    in srgb,
    currentColor 21%,
    transparent
  );
  --color-accent-primary: light-dark(#0060df, #0df);
  --color-accent-primary-hover: light-dark(#0250bb, #80ebff);
  --color-accent-primary-active: light-dark(#054096, #aaf2ff);
  --border-radius-circle: 9999px;
  --border-width: 1px;
  --size-item-small: 16px;
  --size-item-large: 32px;
  --color-canvas: light-dark(white, #1c1b22);
  --background-color-canvas: var(--color-canvas);
  --border-color-interactive: light-dark(#8f8f9d, #f9f9fa);
  --border-color-interactive-hover: var(--border-color-interactive);
  --border-color-interactive-active: var(--border-color-interactive);
  --focus-outline-offset: 2px;

  @media (forced-colors: active) {
    --color-accent-primary: ButtonText;
    --color-accent-primary-hover: SelectedItem;
    --color-accent-primary-active: SelectedItem;
    --button-background-color: ButtonFace;
    --border-color-interactive: ButtonText;
    --border-color-interactive-hover: SelectedItem;
    --border-color-interactive-active: ButtonText;
    --color-canvas: ButtonText;
    --background-color-canvas: Canvas;
  }
}

/*
    The original file is located at:
    https://hg.mozilla.org/mozilla-central/file/168f6caa3b160c12413f9e9ad9df1db49c6b4cdf/toolkit/content/widgets/moz-toggle/moz-toggle.css

    The original file is licensed under the Mozilla Public License, v. 2.0.
    This file is a modified version of the original file.

    The pseudo-selector :host has been replaced by .toggle-button.

    TODO: check from times to times if the original file has been updated (and
    in such a case don't forget to change the revision in the above link).
*/

.toggle-button {
  --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) - 4 * var(--toggle-dot-margin) - var(--toggle-dot-width)
  );
  --input-width: var(--toggle-width);

  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) {
  .toggle-button {
    --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);
  }
}
