@use "sass:map";
@use "../../../styles/tools/convert";
@use "../../../styles/tokens/color";
@use "../../../styles/tokens/tag-colors";
@use "../../../styles/tokens/base";
@use "../../../styles/tokens/breakpoint";
@use "../../../styles/tools/generate";
@use "../../../styles/tokens/space";
@use "../../../styles/tools/visibility" as visibility;
@use "../../../styles/typography/config" as typographyConfig;
@use "../../Container/styles/mixins" as container;
@use "../../Accordion/styles/mixins" as accordion;
@use "../../Forms/TextInput/styles/mixins" as input;
@use "../../Forms/TextInput/styles/config" as inputConfig;
@use "../../Tag/styles/mixins" as tag;
@use "../../Tag/styles/config" as tagConfig;
@use "../../Button/styles/mixins" as button;
@use "../../../styles/typography/mixins" as typography;

@use "./config";

@mixin globals {
  .is-highlighted {
    background-color: var(--color-surface-accent);
  }

  .sr-only {
    @include visibility.visually-hidden;
  }

  .hide {
    display: none !important;
  }

  .mt-none {
    margin-top: 0 !important;
  }

  .no-underline {
    text-decoration: none !important;
  }

  ul {
    @include typography.list-unstyled();
    margin-bottom: convert.to-rem(30px);
  }

  h3 {
    margin: 0 0 convert.to-rem(15px) 0;
    font-size: convert.to-rem(16px);
    line-height: 1.25;
    font-weight: 400;
    color: var(--color-text-default);
  }

  a {
    color: var(--color-text-default);
    font-weight: 700;
    font-size: inherit;
    text-decoration: underline;
    text-underline-offset: convert.to-rem(2px);
    text-decoration-thickness: auto;

    &:hover,
    &:focus,
    &:active {
      color: var(--color-text-accent);
    }

    &:focus-visible {
      outline: 2px solid var(--color-border-accent);
      outline-offset: 2px;
    }
  }

  a[href],
  area[href],
  input,
  select,
  textarea,
  button,
  [type="button"],
  iframe,
  [tabindex]:not([tabindex="-1"]),
  [contentEditable="true"] {
    @include base.focusring;
  }

  hr {
    margin-top: convert.to-rem(30px);
    margin-bottom: convert.to-rem(30px);
    border: 0;
    border-top: 1px solid var(--color-border-subtle);
    width: 100%;

    @include breakpoint.get("xs", "down") {
      margin-top: convert.to-rem(15px);
      margin-bottom: convert.to-rem(15px);
    }
  }

  * {
    box-sizing: border-box;
    letter-spacing: convert.to-rem(-0.1px);
    line-height: convert.to-rem(18px);
  }

  ::selection {
    background-color: base.$selection-background;
    color: base.$text-color;
  }
}

@mixin light-vars {
  @each $name, $value in color.$colors-light {
    --color-#{$name}: #{$value};
  }

  // Megamenu ships as a standalone bundle and cannot rely on global tag token variables.
  @each $name, $value in tag-colors.$tag-colors-light {
    --#{$name}: #{$value};
  }
}

@mixin dark-vars {
  @each $name, $value in color.$colors-dark {
    --color-#{$name}: #{$value};
  }

  // Keep tag colors available in dark mode as well.
  @each $name, $value in tag-colors.$tag-colors-dark {
    --#{$name}: #{$value};
  }
}

@mixin base {
  top: 0;
  left: 0;
  width: 100%;
  z-index: 11;
  font-size: 1rem;
  color: var(--color-text-default);
  background-color: var(--color-surface-primary);
  box-sizing: border-box;
  font-family: #{base.$font-family};

  // Scroll hide functionality
  &.is-sticky {
    position: fixed;

    body.has-modal & {
      padding-right: var(--ods-modal-scrollbar-width, 0px);
    }

    #{config.$class-root}__main {
      position: sticky;
      top: 0;
      z-index: 12;
    }

    // Only apply scroll behavior on larger screens where top is visible
    @include breakpoint.get("lg") {
      #{config.$class-root}__top[data-hide-when-sticky="true"] {
        transition:
          transform 0.2s ease-in-out,
          opacity 0.2s ease-in-out;

        &.is-hidden {
          transform: translateY(-100%);
          opacity: 0;
          pointer-events: none;
        }
      }

      #{config.$class-root}__main {
        transition: transform 0.2s ease-in-out;
      }

      // When top is hidden, main should move up
      #{config.$class-root}__top[data-hide-when-sticky="true"].is-hidden
        ~ #{config.$class-root}__main {
        transform: translateY(-#{convert.to-rem(40px)});
      }

      // When top is hidden, reduce the overall container height by the top bar height
      #{config.$class-root}__top[data-hide-when-sticky="true"].is-hidden {
        height: #{convert.to-rem(40px)};
        overflow: hidden;
      }
    }

    // Adjust container height when top is hidden
    &.top-hidden {
      @include breakpoint.get("lg") {
        height: #{convert.to-rem(80px)} !important;
      }
    }
  }
}

@mixin nav {
  display: flex;
  flex: 1;
  list-style: none;
  margin: 0;
  padding: 0;
  max-width: none;
  align-items: center;
  gap: convert.to-rem(30px);

  a {
    text-decoration: none;
  }

  @include breakpoint.get("md", "down") {
    gap: convert.to-rem(20px);
  }
}

@mixin nav-vertical {
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  padding: convert.to-rem(10px) 0;
  gap: convert.to-rem(15px);

  & > #{config.$class-root}__nav-item {
    > * {
      &.is-active,
      &:hover,
      &:focus,
      &:active {
        box-shadow: none !important;
      }
    }
  }
}

@mixin nav-space-small {
  gap: convert.to-rem(15px);
}

@mixin nav-mobile-locale {
  padding: convert.to-rem(15px) 0 !important;
  margin-bottom: 0px !important;
}

@mixin nav-item {
  display: flex;
  height: 100%;
}

@mixin nav-item-lead {
  margin-right: convert.to-rem(10px);
}

@mixin nav-dropdown {
  position: absolute;
  top: calc(100% + 1px);
  left: 0;
  width: 100%;
  background-color: var(--color-surface-primary);
  padding: convert.to-rem(20px) 0;
  display: none;
  opacity: 0;
  z-index: 15;

  &.is-spaced {
    padding-top: convert.to-rem(50px);
    padding-bottom: convert.to-rem(50px);
  }
}

@mixin nav-logo {
  display: flex;
  align-items: center;

  > svg,
  > img {
    width: convert.to-rem(40px);
    height: convert.to-rem(40px);

    & + * {
      margin-left: convert.to-rem(20px);
    }
  }

  > * {
    font-size: convert.to-rem(34px);
    line-height: convert.to-rem(40px);
  }

  @include breakpoint.get("md", "down") {
    > svg,
    > img {
      width: convert.to-rem(30px);
      height: convert.to-rem(30px);

      & + * {
        margin-left: convert.to-rem(10px);
      }
    }

    > * {
      font-size: convert.to-rem(20px);
      line-height: convert.to-rem(30px);
    }
  }
}

@mixin nav-link {
  display: flex;
  font-weight: 700;
  font-size: convert.to-rem(16px) !important;
  align-items: center;
  color: var(--color-text-default);
  text-decoration: none;

  &:hover,
  &:focus,
  &:active,
  &.is-active {
    color: var(--color-text-default) !important;
    box-shadow: config.$active-line;
  }
}

@mixin nav-link-locale {
  &:hover,
  &:focus,
  &:active,
  &.is-active {
    box-shadow: none;
    text-decoration: none !important;
  }

  &.is-active {
    color: var(--color-border-accent);
  }
}

@mixin nav-link-no-line {
  &:hover,
  &:focus,
  &:active,
  &.is-active {
    color: var(--color-text-accent) !important;
    box-shadow: none;
  }
}

@mixin divider {
  margin-top: convert.to-rem(30px);
  margin-bottom: convert.to-rem(30px);
  border: 0;
  border-top: 1px solid var(--color-border-subtle);

  @include breakpoint.get("xs", "down") {
    margin-top: convert.to-rem(15px);
    margin-bottom: convert.to-rem(15px);
  }
}

@mixin nav-cart-icon {
  position: relative;

  &:after {
    content: attr(data-items-in-cart);
    position: absolute;
    right: -25%;
    top: -25%;
    width: convert.to-rem(18px);
    height: convert.to-rem(18px);
    background-color: var(--color-icon-information);
    border-radius: 50%;
    font-size: convert.to-rem(14px);
    color: color.$white;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

@mixin nav-button {
  position: relative;
  display: flex;
  font-size: convert.to-rem(16px);
  font-weight: 700;
  align-items: center;
  color: var(--color-text-default);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;

  &:hover,
  &:focus,
  &:active,
  &.is-active {
    box-shadow: config.$active-line;
  }

  &:focus-visible {
    outline: 2px solid var(--color-border-accent);
    outline-offset: 2px;
  }

  &--toggle {
    .megamenu__nav-button-icon-close {
      display: none;
    }

    &.is-active {
      .megamenu__nav-button-icon-open {
        display: none;
      }

      .megamenu__nav-button-icon-close {
        display: inline-flex;
      }
    }
  }
}

@mixin nav-button-indicator {
  padding: 0 convert.to-rem(20px) 0 0;

  &:after {
    content: "";
    position: absolute;
    right: 0;
    bottom: convert.to-rem(36px);
    width: 0;
    height: 0;
    margin-left: 0.5em;
    border-color: currentcolor transparent transparent;
    border-style: solid;
    border-width: 7px 7px 0;
    transition: transform 0.1s ease-out;
  }

  &.is-active:after {
    transform: rotate(180deg);
  }
}

@mixin nav-icon {
  display: flex;
  margin-right: convert.to-rem(5px);
}

@mixin nav-icon-no-gap-md-down {
  @include breakpoint.get("md", "down") {
    margin-right: 0;
  }
}

@mixin nav-spacer {
  display: flex;
  flex: 1 0 1px;
  visibility: hidden;
}

@mixin subnav {
  margin: 0 0 convert.to-rem(30px) 0;

  &:last-child {
    margin-bottom: 0;
  }
}

@mixin subnav-item {
  display: flex;
  gap: convert.to-rem(10px);
  margin-bottom: convert.to-rem(10px) !important;

  &:last-child {
    margin-bottom: 0 !important;
  }
}

@mixin top {
  display: flex;
  border-bottom: 1px solid var(--color-border-subtle);
  min-height: convert.to-rem(40px);
}

@mixin main {
  display: flex;
  border-bottom: 1px solid var(--color-border-subtle);
  min-height: convert.to-rem(80px);

  @include breakpoint.get("md", "down") {
    min-height: convert.to-rem(60px);
  }
}

@mixin submain {
  display: flex;
  border-bottom: 1px solid var(--color-border-subtle);
  padding: convert.to-rem(20px) 0;
}

@mixin mobile {
  position: fixed;
  z-index: 100;
  width: 0;
  height: 0;
  top: 0;
  right: 0;
  overflow: hidden;
  visibility: hidden;
  transition:
    visibility 0s linear 0.2s,
    width 0s linear 0.2s,
    height 0s linear 0.2s;

  &:after {
    content: "";
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: color-mix(
      in srgb,
      var(--color-surface-contrast) 75%,
      transparent
    );
    opacity: 0;
    transition: opacity 0.2s ease-out;
  }

  &.is-active {
    visibility: visible;
    width: 100vw;
    height: 100vh;
    transition-delay: 0s;

    &.is-active #{config.$class-root}__mobile-wrapper {
      transform: translateX(0);
    }

    &:after {
      opacity: 1;
    }
  }

  #{config.$class-root}__container {
    @include breakpoint.get("md") {
      padding-left: convert.to-rem(20px);
      padding-right: convert.to-rem(20px);
    }
  }
}

@mixin mobile-overlay {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: transparent;
  border: none;
  appearance: none;
  -webkit-appearance: none;
}

@mixin mobile-wrapper {
  position: absolute;
  width: convert.to-rem(380px);
  max-width: 90vw;
  height: 100%;
  top: 0;
  right: 0;
  overflow-x: inherit;
  overflow-y: auto;
  transform: translateX(100%);
  transition: transform 0.2s ease-out;
  background-color: var(--color-surface-primary);

  @include breakpoint.get("xs", "down") {
    width: 100%;
    max-width: 100%;
  }
}

@mixin mobile-header {
  display: flex;
  height: convert.to-rem(80px);
  border-bottom: 1px solid var(--color-border-subtle);

  @include breakpoint.get("md", "down") {
    height: convert.to-rem(60px);
  }
}

@mixin mobile-body {
  padding: convert.to-rem(15px) 0 convert.to-rem(30px);
}

@mixin mobile-account {
  text-align: center;
  padding-top: convert.to-rem(10px);
}

@mixin mobile-account-avatar {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin-bottom: convert.to-rem(12px);
}

@mixin mobile-account-name {
  @include generate.css-map(typographyConfig.$caption, "large");
  margin-top: 0;
  margin-bottom: convert.to-rem(5px);
  font-weight: 700;
}

@mixin mobile-account-phone {
  font-size: convert.to-rem(16px);
  line-height: convert.to-rem(18px);
  margin-top: 0;
  margin-bottom: convert.to-rem(5px);
}

@mixin mobile-account-logout {
  display: inline-block;
  text-decoration: underline;
  text-underline-offset: convert.to-rem(2px);
  text-decoration-thickness: auto;
  margin-bottom: convert.to-rem(20px);
  font-weight: 400 !important;
}

@mixin mobile-account-cta {
  @include button.base();
  @include button.layout();
  @include button.size("default");
  @include button.color("primary");
  font-size: convert.to-rem(18px) !important;
  color: color.$white !important;
  width: 100%;
  justify-content: center;
  margin-bottom: convert.to-rem(40px);
  text-decoration: none !important;

  &:hover {
    color: var(--color-text-inverse) !important;
    background-color: var(--color-fill-contrast) !important;
    border-color: transparent !important;
  }

  &:active {
    color: var(--color-fill-contrast) !important;
    background-color: transparent !important;
    border-color: var(--color-border-contrast) !important;
  }
}

@mixin mobile-account-links {
  margin-top: convert.to-rem(30px);
}

@mixin mobile-account-link {
  text-decoration: none !important;

  &:hover,
  &:focus,
  &:active {
    text-decoration: underline !important;
    text-underline-offset: convert.to-rem(2px);
    text-decoration-thickness: auto;
  }
}

@mixin container {
  @include container.base;
  display: flex;
  flex-direction: column;
  flex: 1;
}

@mixin grid {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: convert.to-rem(20px);

  @include breakpoint.get("md", "down") {
    grid-auto-flow: row;
    grid-auto-columns: 100%;
  }
}

@mixin card {
  border-radius: convert.to-rem(20px);
  padding: convert.to-rem(30px);

  a:hover,
  a:focus {
    text-decoration: underline;
    text-underline-offset: convert.to-rem(2px);
    text-decoration-thickness: auto;
  }

  @include breakpoint.get("md", "down") {
    padding: convert.to-rem(15px);

    &:not(.is-highlighted) {
      padding: 0;
    }
  }
}

@mixin text-large {
  font-size: convert.to-rem(28px) !important;
  line-height: convert.to-rem(32px);
  font-weight: 700;
}

@mixin caption {
  @include generate.css-map(typographyConfig.$caption, "default");
  margin-top: 0;
  margin-bottom: convert.to-rem(20px);
}

@mixin caption-large {
  @include generate.css-map(typographyConfig.$caption, "large");
  margin-top: 0;
}

@mixin icon {
  fill: currentColor;
  display: inline-block;
  vertical-align: middle;

  &__use {
    display: inline;

    // Light theme (default) - show only light version
    &--light {
      display: inline;
    }

    &--dark {
      display: none;
    }
  }

  // Dark theme context - show only dark version
  .is-dark &,
  [data-theme="dark"] &,
  .dark & {
    &__use {
      &--light {
        display: none;
      }

      &--dark {
        display: inline;
      }
    }
  }
}

@mixin overlay {
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  height: 0;
  background-color: color-mix(
    in srgb,
    var(--color-surface-contrast) 75%,
    transparent
  );
  border: none;
  appearance: none;
  -webkit-appearance: none;
  z-index: 10;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 0.15s ease-out,
    visibility 0.15s ease-out;
  outline: none;

  &.is-active {
    pointer-events: auto;
    opacity: 1;
    visibility: visible;
    height: 100vh;
  }

  @include breakpoint.get("md", "down") {
    display: none;
  }
}

@mixin spacer {
  height: convert.to-rem(118px);

  @include breakpoint.get("md", "down") {
    height: convert.to-rem(58px);
  }
}

@mixin spacer-small {
  height: convert.to-rem(78px);

  @include breakpoint.get("md", "down") {
    height: convert.to-rem(58px);
  }
}

@mixin accordion {
  @include accordion.base;
  margin-bottom: 0px !important;

  a {
    text-decoration: none !important;
  }
}

@mixin accordion-item {
  @include accordion.item;
  border-top: none !important;
  border-bottom: 1px solid var(--color-border-subtle) !important;
}

@mixin accordion-button {
  @include accordion.button-base;
  padding: convert.to-rem(15px) 0;
  background: none;
  border: none;
  font-weight: 700;
  font-size: convert.to-rem(18px);
  color: var(--color-text-default);

  &[aria-expanded="true"] {
    border-bottom: 2px solid var(--color-border-accent);
  }
}

@mixin accordion-title {
  @include accordion.title;
  font-size: convert.to-rem(18px);
  font-weight: 700;
}

@mixin accordion-icon {
  @include accordion.icon;
  transform: rotate(90deg);

  [aria-expanded="true"] > &,
  &.is-active {
    @include accordion.icon-active;
    transform: rotate(270deg);
  }
}

@mixin accordion-body {
  @include accordion.body;
  display: none;
  margin-bottom: 0;
  margin-top: 0;
  padding: convert.to-rem(20px) 0 convert.to-rem(30px) 0;

  &.is-visible {
    display: block;
  }

  h3 {
    margin-top: convert.to-rem(20px);

    &:first-child {
      margin-top: 0;
    }
  }
}

@mixin input {
  @include input.base();
  @include input.states();
  max-width: unset !important;
  margin-bottom: convert.to-rem(30px);

  &:focus-visible {
    outline-width: 2px !important;
    outline-offset: -2px !important;
  }

  @each $size, $values in inputConfig.$sizes {
    &#{generate.variant-name($size)} {
      @include input.size($size);
    }
  }

  @each $width, $value in inputConfig.$widths {
    &#{generate.variant-name($width)} {
      @include input.width($width);
    }
  }
}

@mixin input-wrapper {
  position: relative;
  display: inline-block;
  width: 100%;
  margin-bottom: convert.to-rem(30px);

  &--search-icon-with-placeholder {
    isolation: isolate;

    &::before {
      content: "";
      position: absolute;
      top: 50%;
      left: convert.to-rem(8px);
      width: convert.to-rem(20px);
      height: convert.to-rem(20px);
      transform: translateY(-50%);
      background-image: inputConfig.$background-image-search-icon;
      background-repeat: no-repeat;
      pointer-events: none;
      z-index: 2;
    }

    #{config.$class-root}__input {
      background-image: none !important;
      padding-left: convert.to-rem(40px);
      margin-bottom: 0;
    }
  }

  &--search-responsive {
    @include breakpoint.get("lg") {
      width: 50%;
    }
  }
}

@mixin tag {
  @include tag.base;
  text-decoration: none !important;

  @each $name, $props in tagConfig.$sizes {
    &#{generate.variant-name($name)} {
      @include tag.size($name);
    }
  }

  @each $variant, $props in tagConfig.$colors {
    &#{generate.variant-name($variant)} {
      @include tag.color($variant);
    }
  }

  // Interactive styles for button and anchor tags
  &:not(&--transparent):where(button) {
    @include tag.button();
    z-index: 2;
  }

  &:not(&--transparent):where(a) {
    @include tag.button();
    z-index: 2;
  }

  &:not(&--transparent) {
    @include tag.buttton-disabled();
  }
}

@mixin tag-transparent {
  @include tag.color-transparent();

  &:where(button) {
    @include tag.button-transparent();
    z-index: 2;
  }

  &:where(a) {
    @include tag.button-transparent();
    z-index: 2;
    text-decoration: none;

    &:hover,
    &:focus,
    &:active,
    &.is-active {
      text-decoration: none !important;
    }

    &:hover,
    &:focus {
      color: inherit !important;
    }

    &:active,
    &.is-active {
      color: color.$white !important;
    }
  }

  @include tag.button-transparent-disabled();
}

@mixin tag-orange {
  @include tag.color("orange");

  &:where(button) {
    @include tag.button();
    z-index: 2;
  }

  &:where(a) {
    @include tag.button();
    z-index: 2;
    text-decoration: none !important;
  }

  @include tag.buttton-disabled();
}

@mixin list-gap-small {
  display: flex;
  flex-direction: column;
  gap: convert.to-rem(10px);
}

@mixin hide-md-down {
  @include breakpoint.get("md", "down") {
    display: none !important;
  }
}

@mixin hide-lg-up {
  @include breakpoint.get("lg") {
    display: none !important;
  }
}
