@import '../config/import';

:root {
  --layout-sidebar-left-display: block;
  --layout-sidebar-right-display: block;
  --layout-sidebar-left-width: 100%;
  --layout-sidebar-right-width: 100%;
  --layout-sidebar-left-row-end: initial;
  --layout-content-width: 100%;
  --layout-drawer-width: 300px;
  --layout-gap: var(--su-2);
  --layout-padding: 0;
  --layout: 100%;

  @media (min-width: $breakpoint-m) {
    --layout-sidebar-left-width: 2fr;
    --layout-padding: var(--su-2);
    --layout: var(--layout-content-width);
  }

  @media (min-width: $breakpoint-l) {
    --layout-sidebar-left-width: 240px;
    --layout-content-width: 1fr;
    --layout-gap: var(--su-4);
    --layout-padding: var(--su-4);
  }
}

.crayons-layout {
  font-size: var(--fs-base); // todo: drop it when ready
  width: 100%;
  max-width: var(--site-width);
  margin: 0 auto;
  display: grid;
  gap: var(--layout-gap);
  grid-template-columns: var(--layout);
  padding: var(--layout-padding);

  &__sidebar-left {
    display: var(--layout-sidebar-left-display);
    grid-row-end: var(--layout-sidebar-left-row-end);
    width: var(--layout-sidebar-left-width);
  }

  &__sidebar-right {
    display: var(--layout-sidebar-right-display);
    width: var(--layout-sidebar-right-width);
  }

  &__content {
    // Hack preventing content exceeding its container - it works for scenarios where we have
    // `pre` tag having wide (overflowing) text. And the container itself is part of a layout
    // built using CSS `grid`. Without this, the grid layout wouldn't stick to its defined
    // widths...
    min-width: 0;
  }

  &__content__inner {
    margin: 0 auto;
    width: 100%;
  }

  &--limited-xl {
    --site-width: #{$breakpoint-xl};
  }

  &--limited-l {
    --site-width: #{$breakpoint-l};
  }

  &--limited-m {
    --site-width: #{$breakpoint-m};
  }

  &--limited-s {
    --site-width: #{$breakpoint-s};
  }

  &--limited-xs {
    --site-width: #{$breakpoint-xs};
  }

  &--limited-inner-l {
    .crayons-layout__content__inner {
      max-width: $breakpoint-l;
    }
  }

  &--limited-inner-m {
    .crayons-layout__content__inner {
      max-width: $breakpoint-m;
    }
  }

  &--limited-inner-s {
    .crayons-layout__content__inner {
      max-width: $breakpoint-s;
    }
  }
}

.crayons-layout--2-cols {
  @media (min-width: $breakpoint-m) {
    --layout: var(--layout-sidebar-left-width) var(--layout-content-width);
    --layout-content-width: 5fr;
  }

  @media (min-width: $breakpoint-l) {
    --layout-sidebar-left-width: 240px;
    --layout-content-width: 1fr;
    --layout-gap: var(--su-4);
  }

  &.crayons-layout--limited-inner {
    .crayons-layout__content__inner {
      max-width: $breakpoint-m;
      margin: 0 auto;
      width: 100%;
    }
  }

  &--inverted {
    .crayons-layout__sidebar-right {
      order: -1;
    }
    @media (min-width: $breakpoint-m) {
      --layout: var(--layout-content-width) var(--layout-sidebar-left-width);
      .crayons-layout__sidebar-right {
        order: 1;
      }
    }
  }

  &--1-2 {
    @media (min-width: $breakpoint-l) {
      --layout-sidebar-left-width: 1fr;
      --layout-content-width: 2fr;
    }
  }
}

.crayons-layout--3-cols {
  @media (min-width: $breakpoint-m) {
    --layout: var(--layout-sidebar-left-width) var(--layout-content-width);
    --layout-sidebar-left-row-end: span 2;
    --layout-sidebar-left-width: 2fr;
    --layout-sidebar-right-width: 5fr;
    --layout-content-width: 5fr;
  }

  @media (min-width: $breakpoint-l) {
    --layout-sidebar-left-width: 240px;
    --layout-sidebar-right-width: 1fr;
    --layout-sidebar-left-row-end: initial;
    --layout-content-width: 2fr;
    --layout: var(--layout-sidebar-left-width) var(--layout-content-width)
      var(--layout-sidebar-right-width);
  }

  &--drop-right-left {
    --layout-sidebar-left-display: none;
    --layout-sidebar-right-display: none;
    --layout-sidebar-left-width: var(--layout-drawer-width);
    --layout-sidebar-right-width: var(--layout-drawer-width);

    @media (min-width: $breakpoint-m) {
      --layout-sidebar-left-width: 2fr;
      --layout-sidebar-left-row-end: initial;
      --layout-sidebar-left-display: block;
    }

    @media (min-width: $breakpoint-l) {
      --layout-sidebar-right-display: block;
      --layout-sidebar-right-width: 1fr;
      --layout-sidebar-left-width: 240px;
    }
  }
}

.crayons-layout--article {
  --layout-gap: 0;
  @media (min-width: $breakpoint-m) {
    --layout-gap: var(--su-2);
    --layout-sidebar-left-width: var(--su-9);
    --layout: var(--layout-sidebar-left-width) 1fr;
    --layout-sidebar-left-row-end: span 2;
  }

  @media (min-width: $breakpoint-l) {
    --layout-gap: var(--su-4);
    --layout-sidebar-right-width: 3fr;
    --layout-sidebar-left-row-end: initial;
    --layout-content-width: 7fr;
    --layout: var(--layout-sidebar-left-width) var(--layout-content-width)
      var(--layout-sidebar-right-width);
  }
}

// Unlike the main app, in the admin area we only break into 2 side-by-side columns at large breakpoint,
// to allow more real estate for data tables
.crayons-layout--admin-2-cols {
  @media (min-width: $breakpoint-l) {
    --layout: var(--layout-sidebar-left-width) var(--layout-content-width);
    --layout-sidebar-left-width: 240px;
    --layout-content-width: 1fr;
    --layout-gap: var(--su-4);
  }
}

// Temporary solution for sidebar on mobile
.crayons-layout__sidebar-left,
.crayons-layout__sidebar-right {
  .sidebar-bg {
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow: hidden;
    position: fixed;
    background: rgba(0, 0, 0, 0.5);
    animation: fade-in var(--transition-props);
    display: none;
  }

  &.swiped-in {
    z-index: var(--z-drawer);
    display: block;
    position: fixed;
    top: 0;
    bottom: 0;
    -webkit-overflow-scrolling: touch;

    .sidebar-bg {
      display: block;
    }

    .side-bar {
      position: relative;
      background: var(--body-bg);
      padding: 16px;
      align-content: start;
      height: 100%;
      overflow-y: auto;
    }
  }
}

.crayons-layout__sidebar-left {
  &.swiped-in {
    left: 0;
  }
}
.crayons-layout__sidebar-right {
  &.swiped-in {
    right: 0;
  }
}

// Zen Mode
// It's here because it affects the layout.
.zen-mode {
  --site-width: 1024px;
  --header-height: 0;
  .crayons-layout--3-cols {
    --layout: var(--layout-sidebar-left-width) 1fr;
    @media (min-width: $breakpoint-m) {
      --layout: var(--layout-sidebar-left-width) 1fr;
    }
  }
  .crayons-header,
  .crayons-footer,
  .crayons-article-sticky,
  .crayons-layout__sidebar-right {
    display: none;
  }
}

// Used for bigger sections paddings (posts for example)
// It's here because it affects the layout.
:root {
  --content-padding-x: var(--su-3);
  --content-padding-y: var(--su-3);

  @media (min-width: $breakpoint-s) {
    --content-padding-x: var(--su-5);
    --content-padding-y: var(--su-5);
  }

  @media (min-width: $breakpoint-m) {
    --content-padding-x: var(--su-8);
    --content-padding-y: var(--su-7);
  }

  @media (min-width: $breakpoint-l) {
    --content-padding-x: var(--su-9);
  }
}

.text-padding {
  padding: var(--content-padding-y) var(--content-padding-x);
}

.crayons-page-header {
  --layout-padding: var(--su-3);

  @media (min-width: $breakpoint-l) {
    --layout-padding: var(--su-4);
  }

  padding: var(--layout-padding);
  max-width: var(--site-width);
  width: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;

  @media (min-width: $breakpoint-m) {
    min-height: unset;
  }
}

.crayons-layout--header-inside {
  --header-span: 1 / span 1;
  &.crayons-layout--2-cols {
    --header-span: 1 / span 2;
  }
  grid-template-rows: min-content 1fr;
  row-gap: 0;

  @media (min-width: $breakpoint-m) {
    row-gap: var(--layout-gap);

    .crayons-page-header {
      --layout-padding: 0;
      grid-column: var(--header-span);
    }
  }
}

// .layout-sidebar-visible .crayons-layout should shift layout to the right
body[data-side-nav-visible='true']:not(.hidden-shell) {
  @media (min-width: $breakpoint-m) {
    .crayons-side-nav {
      display: block; /* Show sidebar on larger screens */
    }
  }

  .crayons-layout, .crayons-header__container {
    @media (min-width: $breakpoint-m) {
      max-width: calc(var(--site-width) - 50px);
      width: calc(100% - 50px);
      margin-left: 50px; /* Adjust this value to match the sidebar width */
    }
    @media (min-width: $breakpoint-l) {
      &.crayons-layout--limited-l {
        margin-left: auto;
      }
    }
    @media (min-width: $breakpoint-2xl) {
      max-width: var(--site-width);
      width: 100%;
      margin-left: auto; /* Reset margin on larger screens */
      transform: translateX(20px);
    }
  }
  .crayons-layout--full {
    @media (min-width: $breakpoint-m) {
      max-width: calc(2000px - 100px);
      width: calc(100% - 100px);
      margin-left: 50px;
    }
  }
}

.crayons-side-nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 50px; /* Adjust width as needed */
  height: 100vh; /* Full height */
  background-color: var(--base-0); /* Light background color */
  border-right: 1px solid var(--base-10); /* Optional border for separation */
  z-index: 1000; /* Ensure it stays above other content */
  display: none; /* Initially hidden */
  overflow: visible; /* Allow pop-out cards to extend beyond bounds */
  
  /* Scrollable container for sidebar items */
  .crayons-side-nav__scrollable {
    height: calc(100vh - 82px); /* Full height minus menu space */
    overflow-y: auto; /* Default - only show scrollbar when needed */
    overflow-x: hidden;
    
    /* Custom scrollbar styling - respect system preferences */
    &::-webkit-scrollbar {
      width: 4px;
    }
    
    &::-webkit-scrollbar-track {
      background: transparent;
    }
    
    &::-webkit-scrollbar-thumb {
      background: transparent; /* Start transparent */
      border-radius: 2px;
      transition: background-color 0.2s ease;
    }
    
    &::-webkit-scrollbar-thumb:hover {
      background: var(--base-50);
    }
    
    /* Show scrollbar thumb only when hovering over the container */
    &:hover::-webkit-scrollbar-thumb {
      background: var(--base-30);
    }
    
    /* For Firefox and other browsers that support scrollbar-width */
    scrollbar-width: thin;
    scrollbar-color: transparent transparent;
    
    /* Show scrollbar on hover for better UX */
    &:hover {
      scrollbar-color: var(--base-30) transparent;
    }
  }
  
  img {
    max-width: 100%; /* Ensure images are responsive */
    height: auto; /* Maintain aspect ratio */
    border-radius: 8px; /* Optional: rounded corners for images */
    aspect-ratio: 1 / 1; /* Maintain square aspect ratio */
    border: 1px solid var(--base-30); /* Optional border for images */
  }
  a.crayons-side-nav-link {
    display: block; /* Make links block-level for easier clicking */
    padding: 5px 4px; /* Space between links */
    text-align: center; /* Center align images */
    line-height: 0;
    overflow: visible;
    &:first-child {
      padding-top: 7px; /* Add space above the first link */
      padding-bottom: 8px; /* Add space below the first link */
      box-shadow: 0 1px 1px var(--header-shadow);
    }
    &:hover {
      background-color: var(--base-10); /* Change background on hover */
    }
    &.active {
      background-color: var(--base-20); /* Highlight active link */
    }
  }

  .crayons-side-nav__item {
    position: relative;
    overflow: visible; /* Allow pop-out cards to extend beyond bounds */
    z-index: 1001; /* Ensure it appears above other content */
    .profile-preview-card__content {
      overflow: hidden;
      &--list {
        top: 0px !important;
        height: calc(100vh - 10px);
        padding: 0;
        overflow-y: auto;
        .crayons-card__header {
          padding: var(--su-1) var(--su-4);
        }
        .crayons-link {
          line-height: 1.3em;
          &:hover {
            background-color: var(--base-10);
          }
          .crayons-side-nav__item-icon {
            height: 30px;
            width: 30px;
            margin-right: 8px;
          }
          .crayons-side-nav__item-cover {
            border-radius: 2px;
            border: 0;
          }
        }
        .crayons-side-nav__item-text {
          font-weight: 600;
        }
      }
      .crayons-side-nav__item-cover--main {
        border-radius: 0px;
        width: calc(100% + 32px);
        max-width: calc(100% + 32px);
        margin-left: -16px;
      }
    }
    &.hovered {
      z-index: 2000; /* Ensure it appears above other content */
      .profile-preview-card__content {
        display: block; /* Show sidebar when hovered */
        border-top: 0px;
        h3 a {
          color: var(--base-100);
        }
      }
    }
    &.not-hovered {
      .profile-preview-card__content {
        display: none; /* Hide sidebar when not hovered */
      }
    }
    &--add {
      position: absolute;
      bottom: 41px;
      left: 0;
      width: 100%;
      padding: 8px 0;
      border-top: 1px solid var(--base-20);
      z-index: 2000;
      background-color: var(--base-0);
      cursor: pointer;
      svg {
        margin: 0 auto;
        display: block;
      }
    }
    &--menu {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      padding: 8px 0;
      border-top: 1px solid var(--base-20);
      z-index: 2000;
      background-color: var(--base-0);
      cursor: pointer;
      svg {
        width: 100%;
      }
    }
  }

}
