/* 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([hidden]) {
  display: none !important;
}

:host {
  display: inline-block;
  height: fit-content;
  width: fit-content;
}

:host([type="split"]) {
  --button-split-separator-height: 50%;

  display: flex;
  justify-content: flex-start;
  gap: var(--button-gap-split);

  button {
    min-width: fit-content;
  }

  #main-button {
    padding-inline-end: 0;
  }

  #chevron-button {
    padding-inline-start: 0;
  }

  .button-background {
    height: 100%;
  }

  #main-button .button-background {
    border-start-end-radius: 0;
    border-end-end-radius: 0;
    border-inline-end-width: var(--button-border-width-split);
  }

  #chevron-button .button-background {
    border-start-start-radius: 0;
    border-end-start-radius: 0;
    border-inline-start-width: var(--button-border-width-split);

    &::before {
      content: "";
      position: absolute;
      inset-block-start: 50%;
      inset-inline-start: 0;
      transform: translateY(-50%);
      background-color: var(--button-split-separator-background-color);
      /* This element is stylized like a border, so border tokens are appropriate. */
      /* stylelint-disable-next-line stylelint-plugin-mozilla/use-design-tokens */
      width: var(--border-width);
      height: var(--button-split-separator-height);
    }
  }
}

:host([type="split"][size="small"]) {
  --button-split-separator-height: 64%;
}

:host([type="split"][size="small"]) .button-background img {
  width: var(--icon-size-xsmall);
  height: var(--icon-size-xsmall);
}

:host([type="split"]:hover) #chevron-button,
:host([type="split"]) #chevron-button:focus-visible,
:host([type="split"]) :focus-visible ~ #chevron-button,
:host([type="split"]) button:is([aria-expanded="true"], [aria-pressed="true"]) {
  .button-background::before {
    background-color: transparent;
  }
}

:host([type="split"]) button:focus-visible {
  z-index: 1;
}

button {
  appearance: none;
  background: transparent;
  border: none;
  /* HTML button gets `font: -moz-button` from UA styles,
   * but we want it to match the root font styling. */
  font: inherit;
  /* Overridden on button-background, but still need to unset to not get the
   * UA styles */
  color: inherit;
  width: 100%;
  height: 100%;

  &:focus-visible {
    outline: none;
  }

  padding-inline-start: var(--button-outer-padding-inline-start, var(--button-outer-padding-inline, 0));
  padding-inline-end: var(--button-outer-padding-inline-end, var(--button-outer-padding-inline, 0));
  padding-block-start: var(--button-outer-padding-block-start, var(--button-outer-padding-block, 0));
  padding-block-end: var(--button-outer-padding-block-end, var(--button-outer-padding-block, 0));
}

.button-background {
  box-sizing: border-box;
  min-height: var(--button-min-height);
  border: var(--button-border);
  border-radius: var(--button-border-radius);
  background-color: var(--button-background-color);
  color: var(--button-text-color);
  padding: var(--button-padding);
  font-weight: var(--button-font-weight);
  /* Ensure font-size isn't overridden by widget styling (e.g. in forms.css) */
  font-size: var(--button-font-size);
  display: flex;
  justify-content: var(--button-content-alignment);
  align-items: center;
  position: relative;

  &[size="small"] {
    min-height: var(--button-min-height-small);
    padding: var(--button-padding-block-small) var(--button-padding-inline-small);
    font-size: var(--button-font-size-small);
  }

  &[size="large"] {
    min-height: var(--button-min-height-large);
    padding: var(--button-padding-block-large) var(--button-padding-inline-large);
  }

  &.badged::after {
    content: "";
    position: absolute;
    height: var(--button-badge-size);
    width: var(--button-badge-size);
    inset-block-start: var(--space-xxsmall);
    inset-inline-end: var(--space-xxsmall);
    background-color: var(--button-badge-background-color);
    border-radius: var(--border-radius-circle);
  }

  @media not ((prefers-contrast) or (forced-colors)) {
    &.badged::after {
      background-color: var(--button-attention-dot-color, var(--button-badge-background-color));
    }
  }

  button:hover > & {
    background-color: var(--button-background-color-hover);
    border-color: var(--button-border-color-hover);
    color: var(--button-text-color-hover);
  }

  button:hover:active:not(:disabled) > & {
    background-color: var(--button-background-color-active);
    border-color: var(--button-border-color-active);
    color: var(--button-text-color-active);
  }

  button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
    background-color: var(--button-background-color-selected);
    border-color: var(--button-border-color-selected);
    color: var(--button-text-color-selected);
  }

  button:disabled > & {
    background-color: var(--button-background-color-disabled);
    border-color: var(--button-border-color-disabled);
    color: var(--button-text-color-disabled);
    opacity: var(--button-opacity-disabled);
  }

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

  &[type="primary"] {
    background-color: var(--button-background-color-primary);
    border-color: var(--button-border-color-primary);
    color: var(--button-text-color-primary);

    button:hover > & {
      background-color: var(--button-background-color-primary-hover);
      border-color: var(--button-border-color-primary-hover);
      color: var(--button-text-color-primary-hover);
    }

    button:hover:active:not(:disabled) > & {
      background-color: var(--button-background-color-primary-active);
      border-color: var(--button-border-color-primary-active);
      color: var(--button-text-color-primary-active);
    }

    button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
      background-color: var(--button-background-color-primary-selected);
      border-color: var(--button-border-color-primary-selected);
      color: var(--button-text-color-primary-selected);
    }

    button:disabled > & {
      background-color: var(--button-background-color-primary-disabled);
      border-color: var(--button-border-color-primary-disabled);
      color: var(--button-text-color-primary-disabled);
    }
  }

  &[type="destructive"] {
    background-color: var(--button-background-color-destructive);
    border-color: var(--button-border-color-destructive);
    color: var(--button-text-color-destructive);

    button:hover > & {
      background-color: var(--button-background-color-destructive-hover);
      border-color: var(--button-border-color-destructive-hover);
      color: var(--button-text-color-destructive-hover);
    }

    button:hover:active:not(:disabled) > & {
      background-color: var(--button-background-color-destructive-active);
      border-color: var(--button-border-color-destructive-active);
      color: var(--button-text-color-destructive-active);
    }

    button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
      background-color: var(--button-background-color-destructive-selected);
      border-color: var(--button-border-color-destructive-selected);
      color: var(--button-text-color-destructive-selected);
    }

    button:disabled > & {
      background-color: var(--button-background-color-destructive-disabled);
      border-color: var(--button-border-color-destructive-disabled);
      color: var(--button-text-color-destructive-disabled);
    }
  }

  &[type~="ghost"] {
    background-color: var(--button-background-color-ghost);
    border-color: var(--button-border-color-ghost);
    color: var(--button-text-color-ghost);

    button:hover > & {
      background-color: var(--button-background-color-ghost-hover);
      border-color: var(--button-border-color-ghost-hover);
      color: var(--button-text-color-ghost-hover);
    }

    button:hover:active:not(:disabled) > & {
      background-color: var(--button-background-color-ghost-active);
      border-color: var(--button-border-color-ghost-active);
      color: var(--button-text-color-ghost-active);
    }

    button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
      background-color: var(--button-background-color-ghost-selected);
      border-color: var(--button-border-color-ghost-selected);
      color: var(--button-text-color-ghost-selected);
    }

    button:disabled > & {
      background-color: var(--button-background-color-ghost-disabled);
      border-color: var(--button-border-color-ghost-disabled);
      color: var(--button-text-color-ghost-disabled);
    }
  }

  &[type~="toolbar"] {
    --button-outer-padding-block: 4px;
    --button-outer-padding-inline: var(--toolbarbutton-padding-outer);
    --button-size-icon: var(--tab-min-height, var(--button-min-height));
    --focus-outline-offset: -2px;

    background-color: var(--button-background-color-toolbar);
    border-color: var(--button-border-color-toolbar);
    color: var(--button-text-color-toolbar);

    button:hover > & {
      background-color: var(--button-background-color-toolbar-hover);
      border-color: var(--button-border-color-toolbar-hover);
      color: var(--button-text-color-toolbar-hover);
    }

    button:hover:active:not(:disabled) > & {
      background-color: var(--button-background-color-toolbar-active);
      border-color: var(--button-border-color-toolbar-active);
      color: var(--button-text-color-toolbar-active);
    }

    button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
      background-color: var(--button-background-color-toolbar-selected);
      border-color: var(--button-border-color-toolbar-selected);
      color: var(--button-text-color-toolbar-selected);
    }

    button:disabled > & {
      background-color: var(--button-background-color-toolbar-disabled);
      border-color: var(--button-border-color-toolbar-disabled);
      color: var(--button-text-color-toolbar-disabled);
    }
  }

  &[type~="menuitem"] {
    --button-font-weight: normal;
    --button-content-alignment: inline-start;
    --button-padding: var(--panel-menuitem-padding);
    --button-border-radius: var(--panel-menuitem-border-radius);
    --focus-outline-offset: -2px;

    background-color: var(--button-background-color-menu);
    border-color: var(--button-border-color-menu);
    color: var(--button-text-color-menu);
    /* We want the button to span the width of the content area. */
    /* stylelint-disable-next-line stylelint-plugin-mozilla/use-design-tokens */
    width: -moz-available;
    margin: var(--panel-menuitem-margin);

    button:hover > & {
      background-color: var(--button-background-color-menu-hover);
      border-color: var(--button-border-color-menu-hover);
      color: var(--button-text-color-menu-hover);
    }

    button:hover:active:not(:disabled) > & {
      background-color: var(--button-background-color-menu-active);
      border-color: var(--button-border-color-menu-active);
      color: var(--button-text-color-menu-active);
    }

    button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
      background-color: var(--button-background-color-menu-selected);
      border-color: var(--button-border-color-menu-selected);
      color: var(--button-text-color-menu-selected);
    }

    button:disabled > & {
      background-color: var(--button-background-color-menu-disabled);
      border-color: var(--button-border-color-menu-disabled);
      color: var(--button-text-color-menu-disabled);
    }
  }

  &[type~="muted"] {
    background-color: var(--button-background-color-muted);
    border-color: var(--button-border-color-muted);
    color: var(--button-text-color-muted);

    button:hover > & {
      background-color: var(--button-background-color-muted-hover);
      border-color: var(--button-border-color-muted-hover);
      color: var(--button-text-color-muted-hover);
    }

    button:hover:active:not(:disabled) > & {
      background-color: var(--button-background-color-muted-active);
      border-color: var(--button-border-color-muted-active);
      color: var(--button-text-color-muted-active);
    }

    button:is([aria-expanded="true"], [aria-pressed="true"]):not(:hover, :disabled) > & {
      background-color: var(--button-background-color-muted-selected);
      border-color: var(--button-border-color-muted-selected);
      color: var(--button-text-color-muted-selected);
    }

    button:disabled > & {
      background-color: var(--button-background-color-muted-disabled);
      border-color: var(--button-border-color-muted-disabled);
      color: var(--button-text-color-muted-disabled);
    }
  }

  &.labelled {
    gap: var(--space-small);
  }

  &[type~="icon"]:not(.labelled) {
    background-size: var(--icon-size);
    background-position: center;
    background-repeat: no-repeat;
  }

  /* Small subtree */
  /* stylelint-disable-next-line stylelint-plugin-mozilla/no-has-selector */
  &[type~="icon"]:not(.labelled),
  &:not(.labelled):has(img) {
    width: var(--button-size-icon);
    height: var(--button-size-icon);
    padding: var(--button-padding-icon);

    &[size="small"] {
      width: var(--button-size-icon-small);
      height: var(--button-size-icon-small);
    }

    &[size="large"] {
      width: var(--button-size-icon-large);
      height: var(--button-size-icon-large);
    }
  }

  img,
  &[type~="icon"]:not(.labelled) {
    -moz-context-properties: fill, fill-opacity, stroke;
    fill: var(--button-icon-fill);
    stroke: var(--button-icon-stroke);
  }

  img {
    content: var(--button-icon-content);
    width: var(--icon-size);
    height: var(--icon-size);
    pointer-events: none;
  }
}
