/*= Windows 10 - Top border of accent color at ESR #358 ======================*/
@include OS($win10) {
  :root[sizemode=normal]:is([tabsintitlebar], [customtitlebar]) {
    #navigator-toolbox {
      border-top: .5px solid rgb(47, 47, 47) !important;
    }
    &:-moz-window-inactive #navigator-toolbox {
      border-top-color: rgb(57, 57, 57) !important;
    }

    @media (-moz-windows-accent-color-in-titlebar) {
      #navigator-toolbox {
        @include AccentColor {
          border-top-color: $accentBorder !important;
        }
      }
    }
  }
}

/*= Windows 7, 8 - Tab Bar Background *****************************************/
@include OS($win7, $win8) {
  @include NotOption("userChrome.theme.transparent.frame") {
    /* Header Color */
    :root:not([lwtheme-image]):-moz-lwtheme {
      background-color: var(--lwt-accent-color) !important;

      &:-moz-window-inactive {
        background-color: var(--lwt-accent-color-inactive, var(--lwt-accent-color)) !important;
      }
    }
  }
}

//-- Mixin ---------------------------------------------------------------------
@mixin _os_winTabBarTextColor {
  /* background textcolor */
  color: var(--lwt-text-color) !important;
}

//------------------------------------------------------------------------------

@include OS($win7) {
  #TabsToolbar {
    &:-moz-lwtheme {
      @include _os_winTabBarTextColor;
    }

    /* Remove Aero */
    /* Original: radial-gradient(eclipse at bottom, rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.5) 80%, transparent) */
    background-image: unset !important;
  }
}
@include OS($win8) {
  #navigator-toolbox:-moz-lwtheme {
    @include _os_winTabBarTextColor;
  }
}

/*= Windows 7, 8 - Menu Bar ==================================================*/
@include OS($win7, $win8) {
  menupopup > menu,
  menupopup > menuitem,
  #context-navigation > menuitem {
    border: 1px solid transparent; /* Need reduce 2px at menu */
  }

  #main-menubar > menu[open="true"],
  #main-menubar > menu[_moz-menuactive="true"] {
    background-color: -moz-menuhover !important; /* Make to original */
  }
  :root:-moz-lwtheme #main-menubar > menu[open="true"],
  :root:-moz-lwtheme #main-menubar > menu[_moz-menuactive="true"] {
    color: inherit !important; /* Original: -moz-menubarhovertext */
    background-color: color-mix(in srgb, currentColor 20%, transparent) !important; /* Original: -moz-menuhover */
  }
}

/*= Windows - Native Menu ====================================================*/
//-- Mixin ---------------------------------------------------------------------
@mixin _os_menuEmulateActive($activeBg, $activeBd, $disabledBg, $disabledBd) {
  menu[_moz-menuactive="true"],
  menuitem[_moz-menuactive="true"] {
    background-color: $activeBg !important;
    border-color: $activeBd !important;

    &[disabled="true"] {
      background-color: $disabledBg !important;
      border-color: $disabledBd !important;
    }
  }
}
@mixin _os_menuEmulate($bgRate, $bdRate) {
  @include _os_menuEmulateActive(
    color-mix(in srgb, -moz-menuhover $bgRate, transparent),
    color-mix(in srgb, -moz-menuhover $bdRate, transparent),
    color-mix(in srgb, currentColor $bgRate, transparent),
    color-mix(in srgb, currentColor $bdRate, transparent)
  );
}

//------------------------------------------------------------------------------

@include WinNativeMenu() {
  @include OS($win) {
    // UI Density Menu Item Padding #649
    .customization-uidensity-menuitem > .menu-iconic-left {
      width: unset !important;
    }

    menulist > menupopup > menuitem[_moz-menuactive="true"],
    menulist > menupopup > menu[_moz-menuactive="true"] {
      background-color: highlight !important;
      color: highlighttext !important;
    }

    #PlacesToolbar menu,
    #PlacesToolbar menuitem,
    #BMB_bookmarksPopup menu,
    #BMB_bookmarksPopup menuitem {
      border: none !important; /* Remove border */

      &:not([disabled], :active)[_moz-menuactive="true"] {
        background-color: var(--button-hover-bgcolor) !important;
      }
    }
  }

  @include OS($win7) {
    @media (-moz-windows-classic: 0) {
      @include _os_menuEmulate(5%, 60%);

      /* Remove text shadow */
      :root:-moz-lwtheme #toolbar-menubar {
        text-shadow: unset !important;
        /* Original: 0 0 .5em white, 0 0 .5em white, 0 1px 0 rgba(255, 255, 255, .4) */
      }

      /* Remove Color */
      :root:-moz-lwtheme #main-menubar:not(:-moz-window-inactive) {
        background-color: unset !important; /* Original: rgba(255, 255, 255, .5) */
        color: unset !important; /* Original: black */
      }
    }
  }

  @include OS($win8) {
    @include _os_menuEmulate(17%, 80%);
  }

  @include OS($win10) {
    @include NativeMenuPopup {
      @include _os_menuEmulateActive(
        #91c9f7, // color-mix(in srgb, -moz-menuhover 40%, transparent)
        transparent,
        color-mix(in srgb, currentColor 9%, transparent),
        transparent
      );
    }
  }
}

@include OS($linux) {
  /*= Linux - Global Menubar Active Color ====================================*/
  #main-menubar > menu[open="true"],
  #main-menubar > menu[_moz-menuactive="true"] {
    color: inherit !important; /* Original: -moz-menubarhovertext */
    background-color: color-mix(in srgb, currentColor 20%, transparent) !important; /* Original: -moz-menuhover */
  }
}

/*= Linux - Light System Default Theme's Selected Tab ========================*/
//-- Mixin ---------------------------------------------------------------------
$_os_linuxDefaultShadow: 0 0 4px rgba(128, 128, 142, 0.5);
@mixin _os_linuxDefaultThemeSelectedTab {
  @include not_lwtheme {
    #tabbrowser-tabs:not([noshadowfortests])
      .tabbrowser-tab:is([visuallyselected], [multiselected])
      > .tab-stack
      > .tab-background {
        @content;
    }
  }
}

//------------------------------------------------------------------------------

@include OS($linux) {
  @include Light {
    /* Because of
      #TabsToolbar:not([brighttext]) #tabbrowser-tabs:not([noshadowfortests]) .tabbrowser-tab:is([visuallyselected], [multiselected]) > .tab-stack > .tab-background {
        border: 1px solid var(--tab-line-color, rgba(128,128,142,0.9));
        box-shadow: 0 0 4px rgba(128,128,142,0.5);
      }
     */
    @include _os_linuxDefaultThemeSelectedTab {
      box-shadow: $_os_linuxDefaultShadow !important;
    }
    #TabsToolbar:not([brighttext]) {
      @include _os_linuxDefaultThemeSelectedTab {
        box-shadow: 0 0 1px var(--tab-line-color, rgba(128, 128, 142, 0.9)), $_os_linuxDefaultShadow !important;
      }
    }
  }
}

/*= Titlebar Container Size at maximized #384 ================================*/
@include OS($win) {
  :root:is([tabsintitlebar], [customtitlebar]) {
    @include NotOption("userChrome.compatibility.os.windows_maximized") {
      #titlebar {
        // Prevent #476 FF v105 above
        /* -moz-default-appearance: -moz-window-titlebar */
        appearance: none !important;
      }
      @include OS($win7, $win8) {
        &[sizemode="maximized"] #titlebar {
          // Only win7, win8 #480
          padding-top: 8px; // Don't use margin-top for reserved fullscreen animation
        }
      }
    }
    @include Option("userChrome.compatibility.os.windows_maximized") {
      &[sizemode="maximized"] #titlebar {
        appearance: none !important;

        // Prevent side effect #370
        padding-top: 8px; // Don't use margin-top for reserved fullscreen animation
      }
    }
  }
}

/*= Windows - Classic Theme #734 =============================================*/
// It occurs by #titlebar { appearance: none !important; } above
// https://github.com/mozilla/gecko-dev/blob/4334cb91a3762ac497c50a1c7b86b8004690229e/browser/themes/windows/browser.css#L96-L109
// https://github.com/mozilla/gecko-dev/blob/4334cb91a3762ac497c50a1c7b86b8004690229e/browser/themes/windows/browser.css#L164-L174
// https://github.com/mozilla/gecko-dev/blob/4334cb91a3762ac497c50a1c7b86b8004690229e/browser/themes/windows/browser-aero.css#L71-L81
@include OS($win) {
  @include WinClassic {
    :root:is([tabsintitlebar], [customtitlebar])[sizemode="normal"]:not(:-moz-lwtheme) #navigator-toolbox {
      appearance: auto !important;
      -moz-default-appearance: -moz-window-titlebar !important;;
    }

    :root:is([tabsintitlebar], [customtitlebar])[sizemode="maximized"]:not(:-moz-lwtheme) #navigator-toolbox {
      appearance: auto !important;;
      -moz-default-appearance:-moz-window-titlebar !important;;
    }

    :root:is([tabsintitlebar], [customtitlebar]):not(:-moz-lwtheme) #pinned-tabs-container,
    :root:is([tabsintitlebar], [customtitlebar]):not(:-moz-lwtheme) #tabbrowser-arrowscrollbox {
      color: -moz-dialogtext !important;
    }

    :root:is([tabsintitlebar], [customtitlebar])[sizemode="normal"] #navigator-toolbox:-moz-lwtheme {
      background-image: linear-gradient(
        to bottom,
        ThreeDLightShadow 0, ThreeDLightShadow 1px,
        ThreeDHighlight 1px, ThreeDHighlight 2px,
        ActiveBorder 2px, ActiveBorder 4px, transparent 4px
      ) !important;
    }
  }
}

/*= MacOS Fullscreen clipping #1045 ==========================================*/
:root[sizemode="fullscreen"][macOSNativeFullscreen]:not([inDOMFullscreen="true"]) #TabsToolbar {
  min-height: var(--tab-min-height, fit-content) !important;
}
