/**
 * Table of Contents (ToC) Styles
 * Responsive styling for inline (narrow screens) and fixed right margin (wide screens)
 * Includes collapsible functionality with direction-based animations
 */

/* ToC Container Base Styles */
#toc {
  display: none; /* Hidden by default, shown by JavaScript when content is available */
  font-size: 0.9em;
  line-height: 1.4;
  transition: all 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  overflow: hidden;
  will-change: transform, opacity, max-height, max-width;
}

.toc-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin-bottom: 0.5rem;
  transition: all 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.toc-title {
  margin: 0;
  font-size: 1.1em;
  font-weight: 600;
  color: #333;
  border-bottom: 2px solid #0052d9;
  padding-bottom: 0.25rem;
  flex-grow: 1;
  cursor: pointer;
  transition: all 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  border-radius: 3px;
  padding: 0.25rem 0.25rem 0.25rem 0;
  margin: -0.25rem -0.25rem -0.25rem 0;
  will-change: transform, background-color, color, font-weight;
}

.toc-title:hover {
  color: #0052d9;
  background-color: rgba(0, 82, 217, 0.05);
}

.toc-title:active {
  background-color: rgba(0, 82, 217, 0.1);
  color: #003d99;
  transform: translateY(1px);
}

.toc-title:focus {
  outline: none;
}

/* Manually expanded state (clicked) - bold and blue */
#toc.manually-expanded .toc-title {
  font-weight: 700;
  color: #0052d9;
}

.toc-content {
  transition: all 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  overflow: hidden;
  will-change: transform, opacity, max-height, max-width;
  /* Default expanded state */
  opacity: 1;
  transform: scale(1);
}

/* ToC List Styles */
.toc-list,
.toc-list-nested {
  list-style: none;
  padding: 0;
  margin: 0;
}

.toc-list {
  margin-top: 0.5rem;
}

.toc-list-nested {
  margin-top: 0.25rem;
  margin-left: 1rem;
  border-left: 2px solid #e8e8e8;
  padding-left: 0.75rem;
}

/* Enhanced nesting for deeper levels */
.toc-list-nested .toc-list-nested {
  margin-left: 0.75rem;
  border-left: 1px solid #f0f0f0;
  padding-left: 0.5rem;
}

.toc-list-nested .toc-list-nested .toc-list-nested {
  margin-left: 0.5rem;
  border-left: 1px dotted #f5f5f5;
  padding-left: 0.375rem;
}

.toc-list-nested .toc-list-nested .toc-list-nested .toc-list-nested {
  margin-left: 0.375rem;
  border-left: 1px dotted #f8f8f8;
  padding-left: 0.25rem;
}

.toc-list li {
  margin: 0.25rem 0;
}

.toc-list a {
  display: block;
  color: #0052d9;
  text-decoration: none;
  padding: 0.125rem 0;
  border-radius: 3px;
  transition: all 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  word-wrap: break-word;
  hyphens: auto;
}

.toc-list a:hover {
  color: #003d99;
  background-color: rgba(0, 82, 217, 0.05);
  text-decoration: none;
}

.toc-list a:visited {
  color: #0052d9;
}

/* Font size hierarchy for different heading levels */
.toc-list > li > a {
  font-weight: 600; /* h2 - main sections */
  font-size: 1em;
}

.toc-list-nested > li > a {
  font-weight: 500; /* h3 - subsections */
  font-size: 0.95em;
}

.toc-list-nested .toc-list-nested > li > a {
  font-weight: 400; /* h4 - sub-subsections */
  font-size: 0.9em;
}

.toc-list-nested .toc-list-nested .toc-list-nested > li > a {
  font-weight: 400; /* h5 - deep subsections */
  font-size: 0.85em;
  opacity: 0.9;
}

.toc-list-nested .toc-list-nested .toc-list-nested .toc-list-nested > li > a {
  font-weight: 300; /* h6 - very deep subsections */
  font-size: 0.8em;
  opacity: 0.8;
}

/* Narrow Screen Styles (Default) - ToC appears inline within content */
@media (max-width: 1023px) {
  #toc {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 6px;
    padding: 1rem;
    margin: 1.5rem 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    transform-origin: top center;
    /* Default expanded state for narrow screens */
    max-height: none;
    transform: translateY(0) scale(1);
    opacity: 1;
  }

  .toc-title {
    color: #495057;
    /* Default expanded state */
    text-align: left;
    padding: 0.25rem 0.25rem 0.25rem 0;
    margin: -0.25rem -0.25rem -0.25rem 0;
    writing-mode: horizontal-tb;
    text-orientation: mixed;
    width: auto;
    height: auto;
    border-bottom: 2px solid #0052d9;
    border-left: none;
    transform: translateY(0) scale(1);
  }

  .toc-header {
    margin-bottom: 0.5rem;
    width: auto;
    justify-content: flex-start;
    flex-direction: row;
    align-items: center;
    transform: translateY(0) scale(1);
  }

  .toc-content {
    /* Default expanded state */
    max-height: none;
    opacity: 1;
    transform: translateY(0) scale(1);
    margin-top: 0.5rem;
    position: static;
    width: auto;
    overflow: visible;
  }
  /* Collapsed state - consistent inward collapse animation */
  #toc.collapsed {
    max-height: 60px;
    transform: translateY(0) scale(0.95);
    padding: 0.5rem 1rem;
    display: flex;
    align-items: center;
    opacity: 0.9;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  }

  #toc.collapsed .toc-header {
    margin-bottom: 0;
    width: 100%;
    justify-content: flex-start;
    transform: translateY(0) scale(1);
  }

  #toc.collapsed .toc-title {
    margin: 0;
    text-align: left;
    padding: 0.25rem 0;
    transform: translateY(0) scale(1);
    opacity: 1;
  }

  #toc.collapsed .toc-content {
    max-height: 0;
    margin-top: 0;
    opacity: 0;
    transform: translateY(-10px) scale(0.9);
    position: absolute;
    width: 0;
    overflow: hidden;
  }
}

/* Wide Screen Styles - ToC appears in fixed right margin */
@media (min-width: 1024px) {
  #toc {
    position: fixed;
    top: 120px;
    right: 30px;
    width: 280px;
    max-height: calc(100vh - 150px);
    overflow-y: auto;
    background: white;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    padding: 1.25rem;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 100;
    transform-origin: right center;
    /* Default expanded state for wide screens */
    transform: translateX(0) scale(1);
    opacity: 1;
  }

  .toc-header {
    margin-bottom: 0.5rem;
    /* Default expanded state */
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    height: auto;
    margin-top: 0;
    width: auto;
    transform: translateX(0) scale(1);
  }

  .toc-title {
    /* Default expanded state */
    writing-mode: horizontal-tb;
    text-orientation: mixed;
    white-space: normal;
    overflow: visible;
    width: auto;
    height: auto;
    margin: -0.25rem -0.25rem -0.25rem 0;
    border-bottom: 2px solid #0052d9;
    border-left: none;
    padding-bottom: 0.25rem;
    padding-left: 0;
    display: block;
    text-align: left;
    transform: translateX(0) scale(1);
  }

  .toc-content {
    /* Default expanded state */
    max-width: none;
    opacity: 1;
    transform: translateX(0) scale(1);
    overflow: visible;
  }
  /* Collapsed state - consistent inward collapse animation */
  #toc.collapsed {
    width: 45px;
    overflow: hidden;
    padding: 0.5rem 0;
    text-align: center;
    /* Keep the right edge aligned with expanded state */
    right: 30px;
    transform: translateX(0) scale(0.95);
    opacity: 0.9;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  #toc.collapsed .toc-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: auto;
    margin-bottom: 0;
    margin-top: 0.5rem;
    position: relative;
    width: 100%;
    transform: translateX(0) scale(1);
  }

  #toc.collapsed .toc-content {
    max-width: 0;
    opacity: 0;
    transform: translateX(10px) scale(0.9);
    overflow: hidden;
  }

  #toc.collapsed .toc-title {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    white-space: nowrap;
    overflow: hidden;
    width: 16px;
    height: 100px;
    margin: 0 auto;
    border-bottom: none;
    border-left: 2px solid #0052d9;
    padding-bottom: 0;
    padding-left: 0.15rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transform: translateX(0) scale(1);
    opacity: 1;
  }

  /* Custom scrollbar for ToC */
  #toc::-webkit-scrollbar {
    width: 6px;
  }

  #toc::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
  }

  #toc::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
  }

  #toc::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
  }

  /* Ensure main content doesn't interfere with ToC positioning */
  .post-content {
    /* Main content maintains its original width and centering */
    max-width: 800px;
    margin: 0 auto;
  }
}

/* Extra Wide Screen Adjustments */
@media (min-width: 1400px) {
  #toc {
    right: calc((100vw - 800px) / 2 - 320px);
    width: 300px;
  }

  #toc.collapsed {
    right: calc((100vw - 800px) / 2 - 320px);
  }
}

/* Handle very wide screens where ToC might be too far */
@media (min-width: 1600px) {
  #toc {
    right: calc((100vw - 1200px) / 2 + 50px);
  }

  #toc.collapsed {
    right: calc((100vw - 1200px) / 2 + 50px);
  }
}

/* Hide ToC on small screens to avoid clutter */
@media (max-width: 767px) {
  #toc {
    font-size: 0.85em;
  }

  .toc-list-nested {
    margin-left: 0.75rem;
    padding-left: 0.5rem;
  }
}

/* Print styles - hide ToC in print */
@media print {
  #toc {
    display: none !important;
  }
}
