@use '@craftcms/sass/mixins';
@use 'cp';

.global-sidebar {
  --is-always-visible: true;
  position: sticky;
  inset-block-start: 0;
  display: grid;
  grid-template-columns: var(--global-sidebar-width);
  grid-template-rows: calc(var(--header-height) + 1px) minmax(0, 1fr) auto;
  grid-auto-flow: row;
  height: 100vh;
  padding: 0;
  -webkit-overflow-scrolling: touch;
  background-color: var(--gray-150);
  border-inline-end: 1px solid var(--border-hairline);
  width: var(--global-sidebar-width);
  isolation: isolate;
  z-index: 1;

  a {
    text-decoration: none;
  }

  & > a,
  *:not(.has-subnav) > a {
    &:not([href]) {
      cursor: default;
      background-color: transparent;
    }
  }

  @media only screen and (max-width: cp.$minFullUiWidth - calc(1rem/16)) {
    --is-always-visible: false;
  }
}

.global-sidebar__nav {
  padding-block: var(--s);
  padding-inline: 0;
  overflow: hidden auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  overscroll-behavior: contain;
}

.global-sidebar__footer {
  margin-block-start: auto;
  border-block-start: 1px solid var(--border-hairline);
}

.global-nav > ul {
  display: flex;
  flex-direction: column;
}

.global-nav > ul > li {
  position: relative;
}

.nav-icon {
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 1.25rem;
  height: 1rem;
  line-height: 0;

  --thumb-size: 1rem;

  > svg {
    @include mixins.svg-mask(currentColor);
    width: 100%;
    height: 100%;
  }
}

.nav-item {
  display: grid;
  grid-template-columns:
    var(--nav-item-gutter-width) minmax(0, 1fr) var(--nav-item-prefix-width)
    var(--nav-item-gutter-width);
  grid-template-areas: '. action trigger .' 'subnav subnav subnav subnav';
  align-items: center;
  width: 100%;
  padding-block: var(--xs);

  > .sidebar-action {
    grid-area: action;
  }

  &.sel {
    color: var(--nav-item-fg-active);
    background-color: var(--nav-item-bg-active);
  }
}

.nav-item--sub {
  padding-block: 0;
}

.nav-item__trigger {
  --icon-size: 10px;
  width: var(--nav-item-trigger-size);
  grid-area: trigger;
  display: flex;
  justify-content: center;
  z-index: 1;

  .menubtn {
    padding: 0;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: var(--nav-item-trigger-size);
    height: var(--nav-item-trigger-size);
    border: transparent;

    &:hover {
      background-color: var(--nav-item-bg-hover);
    }

    &[aria-expanded='true']:not(:hover) {
      background-color: transparent;
    }

    &[aria-expanded='true']::after {
      @include mixins.angle(up);
    }

    &::after {
      margin-inline-start: 0 !important;
    }
  }
}

.nav-item__subnav {
  --nav-item-prefix-ratio: 4/3;
  display: none;
  grid-area: subnav;
  gap: 1px;
  margin-block: 1px;
  flex-direction: column;

  &[data-state='expanded'] {
    display: flex;
  }
}

.badge {
  display: inline-flex;
  justify-content: center;
  text-align: center;
  font-size: calc(10rem / 16);
  line-height: 1rem;
  min-width: 1.1rem;
  height: 1rem;
  padding-block: 0;
  padding-inline: var(--xs);
  border-radius: var(--s);
  background-color: var(--nav-item-badge-bg);
  color: var(--nav-item-badge-fg);
}

.sidebar-actions {
  display: none;

  @media only screen and (min-width: cp.$minFullUiWidth) {
    display: block;
    padding-block: 10px;
    padding-inline: var(
      --nav-item-gutter-width
    ); // match padding of #system-info

    #sidebar-trigger {
      display: flex;
    }
  }
}

.sidebar-action {
  --thumb-size: 0.75rem;
  display: grid;
  grid-template-columns: var(--nav-item-prefix-width) minmax(0, 1fr) auto;
  width: 100%;
  align-items: center;
  position: relative;
  text-align: start;
  border-radius: var(--radius-md);
  color: currentcolor;
  cursor: pointer;
  min-height: 30px;
  font-weight: 500;

  // Active indicator
  &::before {
    content: '';
    position: absolute;
    inset-inline-start: 0;
    inset-block-start: 50%;
    transform: translateY(-50%) translateX(-150%);
    height: 95%;
    width: var(--nav-item-indicator-size);
    border-radius: var(--radius-sm);

    // No logical properties for transforms
    .rtl & {
      transform: translateY(-50%) translateX(150%);
    }
  }

  &:focus-visible,
  &:hover {
    color: var(--nav-item-fg-hover);
    background-color: var(--nav-item-bg-hover);
  }

  &[aria-current='page'],
  &[aria-current='true'] {
    &::before {
      background-color: var(--nav-item-fg-active);
    }
  }
}

.sidebar-action--sub {
  font-size: 0.9em;
  min-height: 26px;

  &::before {
    display: none;
  }

  .sidebar-action__prefix {
    position: relative;

    &::before {
      content: '';
      position: absolute;
      inset-inline-start: 50%;
      width: var(--nav-item-indicator-size);
      height: var(--nav-item-indicator-size);
      border-radius: 100%;
      inset-block-start: 50%;
      background-color: currentcolor;
      transform: translateY(-50%) translateX(-50%);

      // No logical properties for transforms
      .rtl & {
        transform: translateY(-50%) translateX(50%);
      }
    }
  }

  &[aria-current='page'],
  &[aria-current='true'] {
    --nav-item-indicator-size: 6px;
  }
}

.sidebar-action--job {
  align-items: start;

  .sidebar-action__label {
    flex-wrap: wrap;
    padding-block: calc((30px - var(--lh)) / 2);
    gap: var(--2xs);
  }

  .label {
    white-space: normal;
  }

  .progress-label {
    width: 100%;
    display: block;
    color: var(--gray-300);
    font-size: 11px;
    line-height: 1.5;
  }

  .nav-icon {
    display: block;
    position: relative;
    width: 16px;
    height: 16px;

    & > canvas {
      display: block;
      position: absolute;
      width: 16px;
      height: 16px;
    }
  }

  #job-icon-hover {
    opacity: 0;
  }

  &:hover {
    #job-icon-hover {
      opacity: 1;
    }

    #job-icon-static {
      opacity: 0;
    }
  }
}

.sidebar-action__prefix {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--nav-item-prefix-width);
  aspect-ratio: var(--nav-item-prefix-ratio);
  line-height: 0;
}

.sidebar-action__label {
  display: flex;
  gap: var(--s);
  align-items: center;
  padding-block: 0;
  padding-inline: var(--xs);
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  margin-inline-start: 7px; // magic number to align with system name

  [data-icon='external'] {
    display: inline-flex;
  }
}

.sidebar-action__badge {
  flex: 0 1 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-block: 0;
  padding-inline: var(--s);
  margin-inline-start: auto;
}

/**
States
 */
@media only screen and (min-width: cp.$minFullUiWidth) {
  [data-sidebar='collapsed'] {
    .global-sidebar {
      --global-sidebar-width: calc(50rem / 16);
    }

    .nav-item {
      grid-template-areas: '. action .' '. trigger .' 'subnav subnav subnav';
      grid-template-columns:
        var(--nav-item-gutter-width) var(--nav-item-prefix-width)
        var(--nav-item-gutter-width);
      justify-items: center;
    }

    .nav-item__indicator {
      inset-inline-end: -1px;
    }

    .sidebar-action {
      width: var(--nav-item-prefix-width);
      min-height: var(--nav-item-prefix-width);
    }

    .nav-item__trigger {
      .menubtn {
        width: 100%;
        height: var(--touch-target-size);
        padding-block: 2px var(--xs);
      }
    }

    .sidebar-action__label {
      display: none;
    }

    .sidebar-action__badge {
      z-index: 100;
      position: absolute;
      inset-inline-end: calc(var(--xs) * -1);
      inset-block-end: var(--xs);
      transform: translateY(25%);
      padding: 0;
    }

    .sidebar-action--sub {
      min-height: 26px;
    }

    .global-nav > ul > li + li {
      --separator-width: calc(28rem / 16);

      &::before {
        position: absolute;
        inset-block-start: 0;
        inset-inline-start: calc(
          (var(--global-sidebar-width) - var(--separator-width)) / 2
        );
        content: '';
        width: var(--separator-width);
        height: 1px;
        margin-block: 0;
        margin-inline: auto;
        background-color: var(--border-hairline);
      }
    }
  }

  [data-sidebar='expanded'] {
    #sidebar-toggle-icon {
      transform: rotate(180deg);
    }
  }
}
