@use 'sass:math';
@use "variables";
@use "global";

// Modern Sidenav Component Styles (v2.0+)
.sidenav-container {
  position: relative;
  z-index: 997;
}

.sidenav-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background: var(--mm-overlay-background, rgba(0, 0, 0, 0.5));
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  z-index: 998;
  cursor: pointer;
  
  &.show {
    opacity: 1;
    visibility: visible;
  }
}

.sidenav-link {
  display: flex;
  align-items: center;
  padding: 0.75rem 1rem;
  color: var(--mm-text-primary, rgba(0, 0, 0, 0.87));
  text-decoration: none;
  transition: background-color 0.2s ease, color 0.2s ease;
  cursor: pointer;
  min-height: 48px;
  
  &:hover:not(.disabled) {
    background: var(--mm-border-color, rgba(0, 0, 0, 0.12));
    color: var(--mm-text-primary, rgba(0, 0, 0, 0.87));
    text-decoration: none;
  }
  
  &.active {
    background: var(--mm-primary-color-light, rgba(38, 166, 154, 0.1));
    color: var(--mm-primary-color, #26a69a);
    
    .sidenav-icon {
      color: var(--mm-primary-color, #26a69a);
    }
  }
  
  &.disabled {
    color: var(--mm-text-disabled, rgba(0, 0, 0, 0.38));
    cursor: not-allowed;
    
    .sidenav-icon {
      color: var(--mm-text-disabled, rgba(0, 0, 0, 0.38));
    }
  }
}

.sidenav-icon {
  margin-right: 1rem;
  font-size: 1.5rem;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--mm-text-secondary, rgba(0, 0, 0, 0.6));
  flex-shrink: 0;
}

.sidenav-text {
  font-size: 0.875rem;
  font-weight: 500;
  line-height: 1.5;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidenav-divider {
  height: 1px;
  background: var(--mm-divider-color, rgba(0, 0, 0, 0.12));
  margin: 0.5rem 0;
}

.sidenav-subheader {
  padding: 1rem 1rem 0.5rem 1rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--mm-text-secondary, rgba(0, 0, 0, 0.6));
  text-transform: uppercase;
  letter-spacing: 0.5px;
  line-height: 1.5;
}

.sidenav-content {
  padding: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

// Legacy Sidenav Styles (maintain backward compatibility)
ul.sidenav.right-aligned li > a:not(.btn):not(.btn-large):not(.btn-flat):not(.btn-floating) i.material-icons {
  margin: 0;
}
.sidenav {
  position: fixed;
  width: variables.$sidenav-width;
  left: 0;
  top: 0;
  margin: 0;
  transform: translateX(-100%);
  height: 100%;
  padding-bottom: 0;
  background-color: var(--mm-surface-color, variables.$sidenav-bg-color);
  z-index: 999;
  overflow-y: auto;
  overflow-x: hidden;
  will-change: transform;
  backface-visibility: hidden;
  transform: translateX(-105%);
  transition: transform 0.3s ease, left 0.3s ease, right 0.3s ease;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
  scrollbar-color: var(--mm-scrollbar-thumb-color, rgba(128, 128, 128, 0.3)) transparent;

  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: var(--mm-scrollbar-thumb-color, rgba(128, 128, 128, 0.3));
    border-radius: 2px;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: var(--mm-scrollbar-thumb-hover-color, rgba(128, 128, 128, 0.5));
  }

  @extend .z-depth-1 !optional;
  
  // New component position variants
  &.sidenav-left {
    left: 0;
  }
  
  &.sidenav-right {
    right: 0;
    left: auto;
    transform: translateX(100%);
  }
  
  // New component mode variants
  &.sidenav-overlay {
    position: fixed;
  }
  
  &.sidenav-push {
    position: relative;
    box-shadow: var(--mm-border-color, rgba(0, 0, 0, 0.12)) 1px 0 0 0;
  }
  
  // New component state variants
  &.closed {
    &.sidenav-left {
      transform: translateX(-100%);
    }
    
    &.sidenav-right {
      transform: translateX(100%);
    }
  }
  
  &.open {
    transform: translateX(0);
  }

  // Right Align
  &.right-aligned {
    right: 0;
    transform: translateX(105%);
    left: auto;
    transform: translateX(100%);
  }

  .collapsible {
    margin: 0;
  }


  li {
    float: none;
    line-height: variables.$sidenav-line-height;

    &.active { background-color: rgba(0,0,0,.05); }
  }

  // Style non btn anchors
  li > a:not(.btn):not(.btn-large):not(.btn-flat):not(.btn-floating) {
    &:hover { background-color: var(--mm-border-color, rgba(0,0,0,.05));}

    color: var(--mm-text-primary, variables.$sidenav-font-color);
    display: block;
    font-size: variables.$sidenav-font-size;
    font-weight: 500;
    height: variables.$sidenav-item-height;
    line-height: variables.$sidenav-line-height;
    padding: 0 (variables.$sidenav-padding * 2);
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;

    & > i,
    & > [class^="mdi-"], li > a > [class*="mdi-"],
    & > i.material-icons {
      float: left;
      height: variables.$sidenav-item-height;
      line-height: variables.$sidenav-line-height;
      margin: 0 (variables.$sidenav-padding * 2) 0 0;
      width: math.div(variables.$sidenav-item-height, 2);
      color: var(--mm-text-secondary, rgba(0,0,0,.54));
      user-select: none;
    }
  }

  // Active menu item styling
  li.active > a:not(.btn):not(.btn-large):not(.btn-flat):not(.btn-floating) {
    color: var(--mm-nav-active-text, #fff);
    background-color: var(--mm-primary-color, #26a69a);

    & > i,
    & > i.material-icons {
      color: var(--mm-nav-active-text, #fff);
    }
  }

  // Stlye btn anchors
  li > .btn, li > .btn-large, li > .btn-flat, li > .btn-floating {
    margin: 10px (variables.$sidenav-padding * 2);
  }

  .divider {
    margin: math.div(variables.$sidenav-padding, 2) 0 0 0;
  }

  .subheader {
    &:hover {
      background-color: transparent;
    }

    cursor: initial;
    pointer-events: none;
    color: var(--mm-text-secondary, rgba(0,0,0,.54));
    font-size: variables.$sidenav-font-size;
    font-weight: 500;
    line-height: variables.$sidenav-line-height;
    padding: 0 (variables.$sidenav-padding * 2);
    height: variables.$sidenav-item-height;
    display: flex;
    align-items: center;
  }

  .user-view {
    position: relative;
    padding: (variables.$sidenav-padding * 2) (variables.$sidenav-padding * 2) 0;
    margin-bottom: math.div(variables.$sidenav-padding, 2);

    & > a {
      &:hover { background-color: transparent; }
      height: auto;
      padding: 0;
    }

    .background {
      overflow: hidden;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: -1;
    }

    .circle, .name, .email {
      display: block;
    }

    .circle {
      height: 64px;
      width: 64px;
    }

    .name,
    .email {
      font-size: variables.$sidenav-font-size;
      line-height: math.div(variables.$sidenav-line-height, 2);
    }

    .name {
      margin-top: 16px;
      font-weight: 500;
    }

    .email {
      padding-bottom: 16px;
      font-weight: 400;
    }
  }
}


// Touch interaction
.drag-target {
  // Right Align
  &.right-aligned {
    right: 0;
  }

  height: 100%;
  width: 10px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 998;
}


// Fixed Sidenav shown
.sidenav.sidenav-fixed {
  // Right Align
  &.right-aligned {
    right: 0;
    left: auto;
  }

  left: 0;
  transform: translateX(0);
  position: fixed;
}

// Fixed Sidenav hide on smaller
@media #{variables.$medium-and-down} {
  .sidenav {
    &.sidenav-fixed {
      transform: translateX(-105%);

      &.right-aligned {
        transform: translateX(105%);
      }
    }

    > a {
      padding: 0 variables.$sidenav-padding;
    }

    .user-view {
      padding: variables.$sidenav-padding variables.$sidenav-padding 0;
    }
  }
}


.sidenav .collapsible-body > ul:not(.collapsible) > li.active,
.sidenav.sidenav-fixed .collapsible-body > ul:not(.collapsible) > li.active {
  background-color: variables.$primary-color;
  a {
    color: var(--mm-nav-active-text, #fff);
  }
}
.sidenav .collapsible-body {
  padding: 0;
}


.sidenav-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  background-color: var(--mm-overlay-background, rgba(0,0,0,.5));
  z-index: 997;
  display: none;
  transition: opacity 0.3s ease;
}

// Ensure overlay sidenav doesn't affect main content layout
.sidenav-overlay {
  pointer-events: auto;
}

// Hamburger button styles (legacy - external button)
.sidenav-hamburger {
  background: var(--mm-surface-color, #fff);
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: background-color 0.2s ease, box-shadow 0.2s ease;

  &:hover {
    background: var(--mm-border-color, rgba(0, 0, 0, 0.05));
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  }

  svg {
    fill: var(--mm-text-primary, rgba(0, 0, 0, 0.87));
  }
}

// Hamburger item inside sidenav
.sidenav-hamburger-item {
  list-style: none;

  &:hover {
    background: var(--mm-border-color, rgba(0, 0, 0, 0.05));
  }

  svg {
    fill: var(--mm-text-secondary, rgba(0, 0, 0, 0.6));
  }
}

// Collapsed sidenav state
.sidenav.sidenav-collapsed {
  width: 60px !important;

  .sidenav-item-text {
    display: none;
  }

  li > a:not(.btn):not(.btn-large):not(.btn-flat):not(.btn-floating) {
    padding: 0 18px;
    justify-content: center;

    & > i,
    & > i.material-icons {
      margin: 0;
      float: none;
    }
  }

  .subheader {
    display: none;
  }

  .sidenav-subitem {
    padding: 8px 16px !important;
    justify-content: center;

    span {
      display: none;
    }
  }
}

// Expand/collapse toggle button
.sidenav-expand-toggle {
  &:hover {
    background: var(--mm-border-color, rgba(0, 0, 0, 0.05));
  }

  svg {
    fill: var(--mm-text-secondary, rgba(0, 0, 0, 0.6));
    transition: transform 0.2s ease;
  }
}

// Submenu item styles
.sidenav-subitem {
  list-style: none;
  transition: background-color 0.2s ease;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;

  &:hover {
    background: var(--mm-border-color, rgba(0, 0, 0, 0.05));
  }

  &.selected {
    background-color: var(--mm-primary-color-light, rgba(38, 166, 154, 0.15));

    svg {
      fill: var(--mm-primary-color, #26a69a);
    }

    i.material-icons {
      color: var(--mm-primary-color, #26a69a);
    }
  }

  svg {
    fill: var(--mm-text-secondary, rgba(0, 0, 0, 0.6));
    transition: fill 0.2s ease;
  }

  i.material-icons {
    color: var(--mm-text-secondary, rgba(0, 0, 0, 0.6));
  }
}

// Menu item with submenu indicator
.sidenav li.has-submenu {
  &.active > a {
    background: var(--mm-primary-color-light, rgba(38, 166, 154, 0.1));
    color: var(--mm-primary-color, #26a69a);
  }
}

// Dark theme support
[data-theme="dark"] {
  .sidenav-hamburger {
    background: var(--mm-surface-color, #1e1e1e);

    svg {
      fill: var(--mm-text-primary, rgba(255, 255, 255, 0.87));
    }
  }

  .sidenav {
    background-color: var(--mm-surface-color, #1e1e1e);

    li > a:not(.btn):not(.btn-large):not(.btn-flat):not(.btn-floating) {
      color: var(--mm-text-primary, rgba(255, 255, 255, 0.87));

      &:hover {
        background-color: var(--mm-border-color, rgba(255, 255, 255, 0.05));
      }

      & > i,
      & > i.material-icons {
        color: var(--mm-text-secondary, rgba(255, 255, 255, 0.6));
      }
    }

    li.active {
      background-color: var(--mm-primary-color, #26a69a);

      & > a:not(.btn):not(.btn-large):not(.btn-flat):not(.btn-floating) {
        color: #000;

        & > i,
        & > i.material-icons {
          color: #000;
        }
      }
    }

    .collapsible-body > ul:not(.collapsible) > li.active a {
      color: #000;

      i,
      i.material-icons {
        color: #000;
      }
    }

    .subheader {
      color: var(--mm-text-secondary, rgba(255, 255, 255, 0.6));
    }
  }

  .sidenav-expand-toggle svg {
    fill: var(--mm-text-secondary, rgba(255, 255, 255, 0.6));
  }

  .sidenav-subitem {
    &:hover {
      background: var(--mm-border-color, rgba(255, 255, 255, 0.05));
    }

    svg {
      fill: var(--mm-text-secondary, rgba(255, 255, 255, 0.6));
    }

    i.material-icons {
      color: var(--mm-text-secondary, rgba(255, 255, 255, 0.6));
    }

    &.selected {
      background-color: rgba(38, 166, 154, 0.2);
      color: var(--mm-text-primary, rgba(255, 255, 255, 0.87));

      svg {
        fill: var(--mm-primary-color, #26a69a);
      }

      i.material-icons {
        color: var(--mm-primary-color, #26a69a);
      }
    }
  }
}

// Footer item positioning
.sidenav-footer-item {
  margin-top: auto;
}

// Animation for smooth width transitions
.sidenav {
  transition: transform 0.3s ease, width 0.3s ease;
}
