/**
 * DTB Table of Contents Styles
 * Clean, modern, premium UI/UX
 */

/* =========================================
   BASE NAV WIDGET (Common to Sidebar & Mobile)
   ========================================= */
.dtb-toc {
  background: #ffffff;
  border-radius: 12px;
  padding: 16px;
  box-shadow:
    0 4px 20px rgba(0, 0, 0, 0.04),
    0 1px 3px rgba(0, 0, 0, 0.02);
  border: 1px solid var(--gray-200, #e2e8f0);
  margin-bottom: 30px;
  font-family: var(
    --font-sans,
    system-ui,
    -apple-system,
    blinkmacsystemfont,
    "Segoe UI",
    roboto,
    "Helvetica Neue",
    arial,
    sans-serif
  );
  position: relative;
  overflow: hidden;
}

/* Header & Title */
.dtb-toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--gray-100, #f1f5f9);
}

.dtb-toc-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--gray-800, #1e293b);
  display: flex;
  align-items: center;
  gap: 8px;
}
.dtb-toc-title::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%233b82f6' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='8' y1='6' x2='21' y2='6'%3E%3C/line%3E%3Cline x1='8' y1='12' x2='21' y2='12'%3E%3C/line%3E%3Cline x1='8' y1='18' x2='21' y2='18'%3E%3C/line%3E%3Cline x1='3' y1='6' x2='3.01' y2='6'%3E%3C/line%3E%3Cline x1='3' y1='12' x2='3.01' y2='12'%3E%3C/line%3E%3Cline x1='3' y1='18' x2='3.01' y2='18'%3E%3C/line%3E%3C/svg%3E");
  background-size: contain;
  background-repeat: no-repeat;
}

/* Reading Time (shown below title in TOC header) */
.dtb-toc-header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.dtb-toc-reading-time {
  font-size: 0.75rem;
  color: var(--gray-400, #94a3b8);
  font-weight: 400;
  letter-spacing: 0.01em;
}

/* Expand All Button */
.dtb-toc-toggle-all {
  background: transparent;
  border: none;
  color: var(--gray-400, #94a3b8);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}
.dtb-toc-toggle-all:hover {
  background: var(--gray-100, #f1f5f9);
  color: var(--gray-700, #334155);
}
.dtb-toc-toggle-all.expanded svg {
  transform: rotate(180deg);
}
.dtb-toc-toggle-all svg {
  transition: transform 0.3s ease;
}

/* Reading Progress Bar built into TOC */
.dtb-toc-progress-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: var(--gray-100, #f1f5f9);
}
.dtb-toc-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--primary, #2563eb);
  transition: width 0.1s linear;
}

/* =========================================
   LIST STRUCTURE & SPACING
   ========================================= */
.dtb-toc-list,
.dtb-toc-nested-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.dtb-toc-item {
  margin-bottom: 2px;
  position: relative;
}

/* Link Wrapper for Flex Layout */
.dtb-toc-link-wrapper {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 6px 10px;
  border-radius: 6px;
  transition: all 0.2s ease;
  border-left: 3px solid transparent;
}

.dtb-toc-item > .dtb-toc-link-wrapper:hover {
  background: var(--gray-50, #f8fafc);
}

/* Active State */
.dtb-toc-item.is-active > .dtb-toc-link-wrapper {
  background: rgba(37, 99, 235, 0.04);
  border-left-color: var(--primary, #2563eb);
}
.dtb-toc-item.is-active > .dtb-toc-link-wrapper .dtb-toc-text {
  color: var(--primary, #2563eb);
  font-weight: 600;
}

/* Typography & Links */
.dtb-toc-link {
  text-decoration: none;
  color: var(--gray-600, #475569);
  flex-grow: 1;
  line-height: 1.4;
  padding: 2px 0;
  display: block;
  outline: none;
}
.dtb-toc-item.dtb-toc-level-2 > .dtb-toc-link-wrapper .dtb-toc-link {
  font-size: 0.9rem;
  font-weight: 500;
}
.dtb-toc-item[class*="dtb-toc-level-3"] > .dtb-toc-link-wrapper .dtb-toc-link,
.dtb-toc-item[class*="dtb-toc-level-4"] > .dtb-toc-link-wrapper .dtb-toc-link {
  font-size: 0.85rem;
  font-weight: 400;
}

/* Child Toggle Button */
.dtb-toc-toggle-child {
  background: transparent;
  border: none;
  color: var(--gray-400, #94a3b8);
  cursor: pointer;
  padding: 4px;
  margin-left: 6px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}
.dtb-toc-toggle-child:hover {
  background: var(--gray-200, #e2e8f0);
  color: var(--gray-700, #334155);
}
.dtb-toc-toggle-child svg {
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.dtb-toc-toggle-child[aria-expanded="true"] svg {
  transform: rotate(180deg);
}

/* Nested List Styling */
.dtb-toc-nested-list {
  padding-left: 14px;
  margin-top: 2px;
  margin-bottom: 4px;
  border-left: 1px dashed var(--gray-200, #e2e8f0);
  margin-left: 12px;

  /* Animation defaults (JS will handle exact heights) */
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition:
    max-height 0.3s ease,
    opacity 0.3s ease,
    margin 0.3s ease;
}

/* Open state for nested list */
.dtb-toc-nested-list.is-open {
  max-height: 2000px; /* arbitrary large value, JS will refine */
  opacity: 1;
}

/* View More Button */
.dtb-toc-view-more-container {
  text-align: center;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--gray-200, #e2e8f0);
}
.dtb-toc-view-more {
  background: transparent;
  border: none;
  color: var(--primary, #2563eb);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 20px;
  transition: background 0.2s ease;
}
.dtb-toc-view-more:hover {
  background: rgba(37, 99, 235, 0.08);
}

/* Hidden H2 Items */
.dtb-toc-hidden-h2 {
  display: none;
}
.dtb-toc.show-all-sections .dtb-toc-hidden-h2 {
  display: block;
  animation: fadeIn 0.4s ease forwards;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Back to Top */
.dtb-toc-footer {
  display: flex;
  justify-content: center;
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--gray-100, #f1f5f9);
}
.dtb-toc-back-top {
  font-size: 0.8rem;
  color: var(--gray-500, #64748b);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: color 0.2s;
  font-weight: 500;
}
.dtb-toc-back-top:hover {
  color: var(--gray-800, #1e293b);
}

/* =========================================
   DESKTOP SPECIFIC (Sticky Sidebar)
   ========================================= */
@media (min-width: 993px) {
  .sidebar-widget#toc-widget {
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
  }

  /* Assuming #toc-widget is inside a sidebar, make it sticky */
  .sidebar-widget#toc-widget .dtb-toc {
    position: sticky;
    top: 90px; /* Offset for site header */
    max-height: calc(100vh - 120px);
    overflow-y: auto;

    /* Custom scrollbar for TOC */
    scrollbar-width: thin;
    scrollbar-color: var(--gray-300, #cbd5e1) transparent;
  }
  .sidebar-widget#toc-widget .dtb-toc::-webkit-scrollbar {
    width: 4px;
  }
  .sidebar-widget#toc-widget .dtb-toc::-webkit-scrollbar-track {
    background: transparent;
  }
  .sidebar-widget#toc-widget .dtb-toc::-webkit-scrollbar-thumb {
    background-color: var(--gray-300, #cbd5e1);
    border-radius: 4px;
  }

  /* Hide the mobile TOC container on desktop */
  .dtb-mobile-toc-container {
    display: none !important;
  }
}

/* =========================================
   MOBILE SPECIFIC (In-Content Block)
   ========================================= */
@media (max-width: 992px) {
  /* Hide the sidebar TOC entirely on smaller screens */
  .sidebar-widget#toc-widget {
    display: none !important;
  }

  /* Style the mobile TOC injected into content */
  .dtb-mobile-toc-container {
    display: block;
    margin: 30px 0;
    clear: both;
  }

  /* By default on mobile, the entire TOC list could be collapsed to save space */
  .dtb-mobile-toc-container .dtb-toc-list {
    display: none;
  }
  .dtb-mobile-toc-container .dtb-toc.is-expanded .dtb-toc-list {
    display: block;
    animation: fadeIn 0.3s ease;
  }

  /* Make the title clickable to expand/collapse entire TOC on mobile */
  .dtb-mobile-toc-container .dtb-toc-header {
    cursor: pointer;
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
  }
  .dtb-mobile-toc-container .dtb-toc.is-expanded .dtb-toc-header {
    margin-bottom: 14px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--gray-100, #f1f5f9);
  }

  /* Add an arrow to the header for mobile */
  .dtb-mobile-toc-container .dtb-toc-title::after {
    content: "";
    display: inline-block;
    width: 16px;
    height: 16px;
    margin-left: auto;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-size: contain;
    background-repeat: no-repeat;
    transition: transform 0.3s ease;
  }
  .dtb-mobile-toc-container .dtb-toc.is-expanded .dtb-toc-title::after {
    transform: rotate(180deg);
  }

  /* Hide the expand all top button on mobile intro state to avoid confusion */
  .dtb-mobile-toc-container .dtb-toc-toggle-all {
    display: none;
  }
}
