@use "sass:map" as sassmap;
@use "../../../styles/tokens/breakpoint" as breakpoint;
@use "../../../styles/tools/convert";
@use "../../../styles/tokens/color";

.documentation-sidebar {
  --sidebar-width: 280px;

  position: relative;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  gap: 0;
  min-height: 100vh;

  &__toggle {
    margin: 0 !important;
    position: fixed;
    left: 16px;
    top: 16px;
    z-index: 50;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: #fff;
    border: 1px solid var(--color-border-subtle);
    display: none;
    align-items: center;
    justify-content: center;

    &:focus-visible {
      outline: 2px solid #005ea5;
      outline-offset: 2px;
    }
  }

  &__toggle-bar {
    display: block;
    width: 20px;
    height: 2px;
    background: #111;
    position: relative;
  }
  &__toggle-bar + &__toggle-bar {
    margin-top: 4px;
  }

  &__sidebar {
    position: sticky;
    top: 0;
    align-self: start;
    height: 100vh;
    overflow: auto;
    background: var(--color-surface-subtle);
    padding: 16px;
  }

  &__sidebar-title {
    font-weight: 700;
    font-size: 1.1rem;
    margin-bottom: 16px;
    color: #333;
  }

  &__nav {
    margin: 0;
    padding: 0;
    margin-bottom: convert.to-rem(20px);
  }

  &__list {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  &__sub-list {
    list-style: none;
    margin: 0;
    padding: 0;
    position: relative;

    // Vertical line for expanded dropdowns - positioned based on parent level
    &::before {
      content: "";
      position: absolute;
      left: 8px;
      top: 0;
      bottom: 0;
      width: 1.5px;
      background: var(--color-border-subtle);
    }
  }

  // Nested sub-lists get additional offset
  &__sub-list &__sub-list::before {
    left: 28px; // 8px + 20px
  }

  &__sub-list &__sub-list &__sub-list::before {
    left: 48px; // 28px + 20px
  }

  &__list-item {
    margin: 0;
    padding: 0;
  }

  &__item {
    margin: 0;
    padding: 0;
  }

  &__dropdown-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 8px 12px;
    border: none;
    background: none;
    border-radius: convert.to-rem(5px);
    color: var(--color-text-default);
    text-decoration: none;
    font-size: 0.9rem;
    cursor: pointer;
    text-align: left;
    font-weight: 600;

    &:hover {
      font-weight: 700;
    }

    &--active {
      color: var(--color-text-inverse);
      background: var(--color-surface-secondary);
    }

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

  &__dropdown-icon {
    display: inline-block;
    width: 16px;
    height: 16px;
    font-weight: bold;
    text-align: center;
    line-height: 1;
    color: #666;
  }

  &__link {
    display: block;
    padding: 8px 12px;
    border-radius: convert.to-rem(5px);
    color: #666;
    text-decoration: none;
    font-size: 0.9rem;

    &:hover {
      color: var(--color-text-default);
    }

    &:hover &--active {
      color: var(--color-text-inverse);
    }

    &--active {
      color: var(--color-text-inverse) !important;
      background: var(--color-surface-secondary);
    }

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

  &__content {
    padding: 16px;
    background: #fff;
    min-height: 100vh;
    overflow: auto;
  }

  &__backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    border: 0;
    margin: 0;
    padding: 0;
    z-index: 40;
    display: none;
  }

  &__version {
    font-size: convert.to-rem(14px);
    padding-left: convert.to-rem(15px);
  }

  &__search-clear {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    border: none;
    background: none;
    color: #999;
    cursor: pointer;
    padding: 4px 8px;
    font-size: 1rem;
    line-height: 1;

    &:hover {
      color: var(--color-text-default);
    }

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

  &__search-results {
    margin-bottom: 16px;
    border: 1px solid var(--color-border-subtle);
    border-radius: convert.to-rem(5px);
    background: white;
    max-height: 400px;
    overflow-y: auto;
  }

  &__search-list {
    list-style: none;
    margin: 0;
    padding: 0;

    li {
      margin: 0;
      padding: 0;
      border-bottom: 1px solid var(--color-border-subtle);

      &:last-child {
        border-bottom: none;
      }
    }
  }

  &__search-result-link {
    display: block;
    padding: 8px 12px;
    color: #666;
    text-decoration: none;
    font-size: 0.9rem;
    transition: background-color 0.2s ease;

    &:hover {
      background-color: var(--color-surface-subtle);
      color: var(--color-text-default);
    }

    &--active {
      background-color: var(--color-surface-subtle);
      color: var(--color-text-default);
      font-weight: 600;
    }

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

  // Mobile behavior - below lg breakpoint (992px)
  @include breakpoint.get(lg, downfrom) {
    grid-template-columns: 1fr;

    &__toggle {
      display: inline-flex;
    }

    &__sidebar {
      position: fixed;
      z-index: 60;
      left: 0;
      top: 0;
      width: min(86vw, var(--sidebar-width));
      transform: translateX(-100%);
      transition: transform 160ms ease-out;
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
    }

    &__content {
      padding-top: 70px !important;
      min-height: 100vh;
    }

    &--open &__sidebar {
      transform: translateX(0%);
    }

    &--open &__backdrop {
      display: block;
    }
  }
}
