@use "govuk-frontend/dist/govuk/base";
@use "govuk-frontend/dist/govuk/objects/width-container";

$govuk-header-link-underline-thickness: 3px;

$toggle-content-spacing: 8px;
$toggle-padding: 15px;
$block-link-spacing: base.govuk-spacing(3);
$logo-height: 30px;
$toggle-border-height: 3px;

@mixin underline-hover-focus($type) {
  display: block;
   @if $type == 'focus' {
      background-color: base.govuk-functional-colour(focus-text);
      @media (forced-colors: active) {
        background-color: linktext;
      }
   } @else if $type == 'hover' {
      background-color: base.govuk-colour("white");
      @media (forced-colors: active) {
        background-color: linktext;
      }
   }
}

@mixin link-underline-hover {
  &:hover {
    text-decoration: underline;
    text-decoration-thickness: $govuk-header-link-underline-thickness;

    @if base.$govuk-link-underline-offset {
      text-underline-offset: base.$govuk-link-underline-offset;
    }
  }
}
 
.one-login-header {
  @include base.govuk-font($size: 16);
  color: base.govuk-functional-colour(inverse-text);
  position: relative;
  background: base.govuk-functional-colour(brand);
}

.one-login-header__container {
  display: flex;
  position: relative;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  border-bottom: 1px solid transparent;
}

.one-login-header__logo {
  min-width: max-content;
  max-width: 33.33%;
  // this strange non-standard spacing came from the Design System
  padding-top:16px;
  padding-bottom:12px;
  box-sizing: border-box;

  @include base.govuk-media-query ($from: desktop) {
    width: 33.33%;
    padding-right: $toggle-padding;
  }
}

.one-login-header__logotype {
  display: inline-block;
  position: relative;
  top: -3px;
  margin-bottom: 2px;
  // this is in place to ensure the GOVUK logo doesn't overflow when text magnification is used
  max-width: 80vw;
  fill: currentcolor;
  vertical-align: sub;

  // Prevent readability backplate from obscuring underline in Windows High
  // Contrast Mode
  @media (forced-colors: active) {
    forced-color-adjust: none;
    color: linktext;
  }
}

.one-login-header__link--homepage {
  // Font size needs to be set on the link so that the box sizing is correct
  // in Firefox
  @include base.govuk-font($size: false, $weight: bold);
  display: inline;
  margin-right: base.govuk-spacing(2);
  font-size: 30px; // We don't have a mixin that produces 30px font size
  line-height: 1;

  @include base.govuk-media-query($from: tablet) {  
    &:focus {
      // Replicate the focus box shadow but without the -2px y-offset of the first yellow shadow
      // This is to stop the logo getting cut off by the box shadow when focused on above a product name
      box-shadow: 0 0 base.govuk-functional-colour(focus);
    }
  }

  &:link,
  &:visited {
    text-decoration: none;
  }

  &:hover,
  &:active {
    // Negate the added border
    margin-bottom: $govuk-header-link-underline-thickness * -1;
    border-bottom: $govuk-header-link-underline-thickness solid currentColor;
  }

  // Remove any borders that show when focused and hovered.
  &:focus {
    margin-bottom: 0;
    border-bottom: 0;
  }
}

.one-login-header__link,
.one-login-header__nav__link {
  &:link,
  &:visited {
    @include base.govuk-typography-common;
    @include base.govuk-link-style-inverse;
    text-decoration: none;
    @include link-underline-hover;

    &:focus {
      @include base.govuk-focused-text;
    }
  }
}

.one-login-header__link {
  word-spacing:-.375rem;

  &>* {
    word-spacing:0;
  }

  &:not(:focus){
    background-color: base.govuk-functional-colour(brand);
  }
}

.one-login-header__nav {
  @include base.govuk-media-query ($until: tablet) {
    width: 100%;

    &.one-login-header__nav--open {
      border-top: 1px solid transparent;
    }
  }

  @include base.govuk-media-query ($from: tablet) {
    max-width: 66%;
  } 
}

.one-login-header__nav__button {
  border: 0 none;
  cursor: pointer;
  background-color: transparent;
  padding: 0;
  margin: 0;
  @include base.govuk-font($size: 16);
  color: base.govuk-functional-colour(link);

  @include base.govuk-media-query ($until: tablet) {
    @include link-underline-hover;
  }

  &:focus {
    @include base.govuk-focused-text;
    outline: none;

    @media (forced-colors: active) {
      outline: 3px solid transparent;
    }
  }

  @include base.govuk-media-query ($from: tablet) {
    color: base.govuk-functional-colour(inverse-text);
    position: relative;
    padding: $block-link-spacing 0;

    &:focus {
      box-shadow: none;

      &:after,
      &:hover:after {
        @include underline-hover-focus('focus');
      }
    }

    &:hover {
      text-decoration: none;

      &:after {
        @include underline-hover-focus('hover');
      }
    }
  }
}

.one-login-header__nav__link,
.one-login-header__nav__button {
  font-weight: bold;

  @include base.govuk-media-query ($from: tablet) {
    position: relative;
    padding: $block-link-spacing 0;

    &:after {
      content: '';
      display: none;
      position: absolute;
      height: $toggle-border-height;
      width: calc(100% - $block-link-spacing*2);
      bottom: 0;
      left: $block-link-spacing;
    }
  }
}

.one-login-header__nav__link {
  @include base.govuk-media-query ($from: tablet) {
    // this degree of specificity is only needed in order to overrule DS rules
    &:link:link,
    &:link:visited,
    &:visited:link,
    &:visited:visited {
      &:hover,
      &:active {
        text-decoration: none;

        &:after {
          @include underline-hover-focus('hover');
        }
      }

      &:focus {
        box-shadow: none;

        &:after {
          @include underline-hover-focus('focus');
        }
      }
    }
  }

  @include base.govuk-media-query ($until: tablet) {
    &:link:link,
    &:link:visited,
    &:visited:link,
    &:visited:visited {
      color: base.govuk-functional-colour(brand);
      &:focus {
        color: base.govuk-functional-colour(focus-text);
      }
    }
  }
}

.one-login-header__nav__form {
  margin: 0;
  padding: 0;
  display: inline;
}

.one-login-header__nav__text {
  @include base.govuk-media-query ($from: tablet) {
    display: flex;
    align-items: center;
    padding: 0 $block-link-spacing;

    &--sign-out {
      height: $logo-height;
      border-left: 1px solid base.govuk-colour("white");
    }
  }
}

.one-login-header__nav__list {
  margin: 0;
  padding: 0;
  list-style: none;

  @include base.govuk-media-query ($from: tablet) {
    display: flex;
    align-items: center;
  }

  @include base.govuk-media-query ($until: tablet) {
    background-color: base.govuk-colour("white");
    color: base.govuk-functional-colour(brand);
    margin-right: -(base.govuk-spacing(3));
    margin-left: -(base.govuk-spacing(3));
    padding: 0 base.govuk-spacing(3);
  }
}

// if JS is unavailable, the nav links are expanded and the toggle button is hidden
.toggle-enabled .one-login-header__nav__list {
  @include base.govuk-media-query ($until: tablet) {
    display: none;
  }

  @include base.govuk-media-query ($from: tablet) {
    display: flex;
  } 
}
.one-login-header__nav--open .one-login-header__nav__list {
  @include base.govuk-media-query ($until: tablet) {
    display: block;
  }
}

.one-login-header__nav__list-item {
  @include base.govuk-media-query ($from: tablet) {
    border-left: none;
    margin-right: 0;
    padding: 0;
    display: flex;
    align-items: center;
  }

  @include base.govuk-media-query ($until: tablet) {
    padding: base.govuk-spacing(3) 0;

    &:not(:last-child) {
      border-bottom: 1px solid base.govuk-colour("black", $variant: 'tint-80');
    }
  }
}

.cross-service-header__logo {
  margin-right: $toggle-content-spacing;
}

.cross-service-header__logo--nav {
  @include base.govuk-media-query ($until: tablet) {
    display: none;
  }
}

.cross-service-header__toggle {
  display: none;
  @include base.govuk-font($size: 16, $weight: bold);
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
  border: 0;
  margin: 0;
  background: none;
  color: inherit;
  padding: $toggle-padding $toggle-padding calc($toggle-padding + 1px) $toggle-padding;
  margin-right: -(base.govuk-spacing(3));

  @mixin toggle-button-focus {
    color: base.govuk-functional-colour(focus-text);
    background-color: base.govuk-functional-colour(focus);
    outline: 3px solid transparent;
  }

  &:focus {
    @include toggle-button-focus;

    &.cross-service-header__toggle--open {
      @include toggle-button-focus;
    }

    // apply focus style on :focus-visible for browsers which support :focus-visible
    &-visible {
      @include toggle-button-focus;

      &.cross-service-header__toggle--open {
        @include toggle-button-focus;
      }
    }
  }

  &.cross-service-header__toggle--open {
    background-color: base.govuk-functional-colour(inverse-text);
    color: base.govuk-functional-colour(brand);
  }
}

.toggle-enabled .cross-service-header__toggle {
  display: inline;

  @include base.govuk-media-query ($from: tablet) {
    display: none;
  }
}

.cross-service-header__toggle-content-wrapper {
  position: relative;
  display: flex;
  align-items: center;

  &:after {
    content: "";
    display: inline-block;
    margin-left: $toggle-content-spacing;
    top: 50%;
    width: 0.6rem;
    height: 0.6rem;
    // this is in place to ensure the caret renders as intended when text magnification is useds
    min-width: 0.6rem;
    transform: translateY(-20%) rotate(135deg);
    border-top: 0.15rem solid;
    border-right: 0.15rem solid;
    .cross-service-header__toggle--open & {
      transform: translateY(30%) rotate(-45deg);
    }
  }

  &:before {
    content: "";
    display: inline-block;
    position: absolute;
    width: 100%;
    height: $toggle-border-height;
    background-color: transparent;
    // the underline that appears on hover/focus should be 1px from the bottom of the container
    bottom: calc(1px - $toggle-padding);
  
    .cross-service-header__toggle:hover & {
      background-color: base.govuk-colour("white");
    }
        
    .cross-service-header__toggle--open &,
    .cross-service-header__toggle--open:hover & {
      background-color: base.govuk-functional-colour(brand);
    }

    .cross-service-header__toggle:focus &,
    .cross-service-header__toggle:focus-visible & {
      background-color: base.govuk-functional-colour(focus-text);
      &:hover {
        background-color: base.govuk-functional-colour(focus-text);
      }
    }
  }
}

.one-login-header__logo-dot {
  fill: #00ffe0;
  
    // Override Dot colour when printing
    @include base.govuk-media-query($media-type: print) {
      fill: currentcolor;
    }

    // Override Dot colour on forced colours mode
    @media (forced-colors: active) {
      fill: currentcolor;
    }

    // Override Dot colour on focus
    :focus & {
      fill: currentcolor;
    }
}
