/**
 * Leaf TOC — Frontend Stylesheet
 * Version: 1.0.0
 * License: GPL-2.0-or-later
 */

/* =============================================
   CSS CUSTOM PROPERTIES
   ============================================= */

.leaf-toc-wrapper {
  --ltoc-bg:           #f9f8f5;
  --ltoc-border:       rgba(0,0,0,.09);
  --ltoc-shadow:       0 2px 12px rgba(0,0,0,.07);
  --ltoc-title-color:  #1c1b19;
  --ltoc-link-color:   #28251d;
  --ltoc-link-hover:   #01696f;
  --ltoc-link-active:  #01696f;
  --ltoc-active-bg:    rgba(1,105,111,.08);
  --ltoc-active-bar:   #01696f;
  --ltoc-radius:       8px;
  --ltoc-font-size:    0.875rem;
  --ltoc-indent:       1rem;
  --ltoc-transition:   180ms cubic-bezier(0.16, 1, 0.3, 1);
  --ltoc-header-bg:    transparent;
  --ltoc-divider:      rgba(0,0,0,.07);
}

/* Auto dark mode */
@media (prefers-color-scheme: dark) {
  .leaf-toc-auto-dark .leaf-toc-wrapper {
    --ltoc-bg:          #1c1b19;
    --ltoc-border:      rgba(255,255,255,.09);
    --ltoc-shadow:      0 2px 12px rgba(0,0,0,.25);
    --ltoc-title-color: #cdccca;
    --ltoc-link-color:  #b5b4b2;
    --ltoc-link-hover:  #4f98a3;
    --ltoc-link-active: #4f98a3;
    --ltoc-active-bg:   rgba(79,152,163,.12);
    --ltoc-active-bar:  #4f98a3;
    --ltoc-divider:     rgba(255,255,255,.07);
  }
}

/* Forced dark mode */
.leaf-toc-dark .leaf-toc-wrapper {
  --ltoc-bg:          #1c1b19;
  --ltoc-border:      rgba(255,255,255,.09);
  --ltoc-shadow:      0 2px 12px rgba(0,0,0,.25);
  --ltoc-title-color: #cdccca;
  --ltoc-link-color:  #b5b4b2;
  --ltoc-link-hover:  #4f98a3;
  --ltoc-link-active: #4f98a3;
  --ltoc-active-bg:   rgba(79,152,163,.12);
  --ltoc-active-bar:  #4f98a3;
  --ltoc-divider:     rgba(255,255,255,.07);
}

/* =============================================
   BASE WRAPPER
   ============================================= */

.leaf-toc-wrapper {
  background: var(--ltoc-bg);
  border: 1px solid var(--ltoc-border);
  border-radius: var(--ltoc-radius);
  box-shadow: var(--ltoc-shadow);
  max-width: 300px;
  margin: 1.5em 0 1.75em;
  font-size: var(--ltoc-font-size);
  line-height: 1.5;
  overflow: hidden;
  contain: layout style;
}

.leaf-toc-wrapper *,
.leaf-toc-wrapper *::before,
.leaf-toc-wrapper *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* =============================================
   HEADER
   ============================================= */

.leaf-toc-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--ltoc-divider);
  background: var(--ltoc-header-bg);
}

.leaf-toc-title {
  flex: 1;
  font-size: 0.8125rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--ltoc-title-color);
  cursor: default;
  user-select: none;
}

.leaf-toc-reading-time {
  font-size: 0.75rem;
  color: var(--ltoc-link-color);
  opacity: 0.65;
  white-space: nowrap;
  flex-shrink: 0;
}

/* =============================================
   TOGGLE BUTTON
   ============================================= */

.leaf-toc-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  border-radius: 4px;
  color: var(--ltoc-link-color);
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
  transition: background var(--ltoc-transition), color var(--ltoc-transition);
}

.leaf-toc-toggle:hover,
.leaf-toc-toggle:focus-visible {
  background: var(--ltoc-active-bg);
  color: var(--ltoc-link-active);
  outline: none;
}

.leaf-toc-toggle:focus-visible {
  outline: 2px solid var(--ltoc-active-bar);
  outline-offset: 1px;
}

.leaf-toc-toggle-icon {
  transition: transform var(--ltoc-transition);
  pointer-events: none;
}

.leaf-toc-wrapper.leaf-toc-collapsed .leaf-toc-toggle-icon {
  transform: rotate(-90deg);
}

/* =============================================
   CONTENT AREA
   ============================================= */

.leaf-toc-content {
  padding: 0.625rem 1rem 0.875rem;
  overflow: hidden;
  transition: max-height var(--ltoc-transition), opacity var(--ltoc-transition);
}

.leaf-toc-content[hidden] {
  display: none;
}

/* =============================================
   TOC LIST
   ============================================= */

.leaf-toc-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.leaf-toc-list.leaf-toc-depth-0 > .leaf-toc-item {
  margin-block: 0.125rem;
}

.leaf-toc-list.leaf-toc-depth-1,
.leaf-toc-list.leaf-toc-depth-2,
.leaf-toc-list.leaf-toc-depth-3 {
  padding-left: var(--ltoc-indent);
  margin-top: 0.125rem;
  border-left: 1px solid var(--ltoc-divider);
  margin-left: 0.25rem;
}

.leaf-toc-item {
  position: relative;
}

/* =============================================
   TOC LINKS
   ============================================= */

.leaf-toc-link {
  display: block;
  padding: 0.1875rem 0.375rem;
  color: var(--ltoc-link-color);
  text-decoration: none;
  border-radius: 4px;
  transition: color var(--ltoc-transition), background var(--ltoc-transition);
  word-break: break-word;
}

.leaf-toc-link:hover {
  color: var(--ltoc-link-hover);
  text-decoration: none;
  background: var(--ltoc-active-bg);
}

.leaf-toc-link:focus-visible {
  outline: 2px solid var(--ltoc-active-bar);
  outline-offset: 1px;
  color: var(--ltoc-link-active);
}

/* Active / scroll-spy highlighted link */
.leaf-toc-link.is-active {
  color: var(--ltoc-link-active);
  background: var(--ltoc-active-bg);
  font-weight: 500;
}

.leaf-toc-link.is-active::before {
  content: '';
  position: absolute;
  left: -1rem;
  top: 50%;
  transform: translateY(-50%);
  width: 2px;
  height: 60%;
  background: var(--ltoc-active-bar);
  border-radius: 2px;
}

/* =============================================
   STICKY MODE
   ============================================= */

.leaf-toc-sticky {
  position: sticky;
  top: 80px;
  z-index: 100;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--ltoc-border) transparent;
}

.leaf-toc-sticky::-webkit-scrollbar {
  width: 4px;
}
.leaf-toc-sticky::-webkit-scrollbar-track {
  background: transparent;
}
.leaf-toc-sticky::-webkit-scrollbar-thumb {
  background: var(--ltoc-border);
  border-radius: 4px;
}

/* =============================================
   READING PROGRESS BAR
   ============================================= */

#leaf-toc-progress {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  z-index: 9999;
  background: transparent;
  pointer-events: none;
}

#leaf-toc-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--leaf-toc-progress-color, #01696f);
  transition: width 100ms linear;
  transform-origin: left center;
  will-change: width;
}

/* =============================================
   COPY LINK — HEADING ANCHOR
   ============================================= */

.leaf-toc-anchor-wrap {
  position: relative;
}

.leaf-toc-copy-btn {
  position: absolute;
  left: -2rem;
  top: 50%;
  transform: translateY(-50%);
  width: 1.5rem;
  height: 1.5rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--ltoc-link-color);
  opacity: 0;
  cursor: pointer;
  border-radius: 4px;
  transition: opacity var(--ltoc-transition), background var(--ltoc-transition);
  padding: 0;
}

.leaf-toc-anchor-wrap:hover .leaf-toc-copy-btn,
.leaf-toc-anchor-wrap:focus-within .leaf-toc-copy-btn {
  opacity: 1;
}

.leaf-toc-copy-btn:hover {
  background: var(--ltoc-active-bg);
  color: var(--ltoc-link-active);
}

.leaf-toc-copy-btn:focus-visible {
  opacity: 1;
  outline: 2px solid var(--ltoc-active-bar);
  outline-offset: 1px;
}

.leaf-toc-copy-tooltip {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 6px);
  transform: translateX(-50%);
  background: var(--ltoc-title-color);
  color: var(--ltoc-bg);
  font-size: 0.6875rem;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--ltoc-transition);
}

.leaf-toc-copy-btn.copied .leaf-toc-copy-tooltip {
  opacity: 1;
}

/* =============================================
   PRESETS
   ============================================= */

/* Minimal */
.leaf-toc-preset-minimal {
  --ltoc-bg: transparent;
  border: none;
  box-shadow: none;
  border-left: 2px solid var(--ltoc-active-bar);
  border-radius: 0;
  padding-left: 0.75rem;
}
.leaf-toc-preset-minimal .leaf-toc-header {
  padding-left: 0;
  border-bottom-color: var(--ltoc-divider);
}
.leaf-toc-preset-minimal .leaf-toc-content {
  padding-left: 0;
}

/* Boxed */
.leaf-toc-preset-boxed {
  border: 2px solid var(--ltoc-active-bar);
  box-shadow: none;
}
.leaf-toc-preset-boxed .leaf-toc-header {
  background: var(--ltoc-active-bg);
}
.leaf-toc-preset-boxed .leaf-toc-title {
  color: var(--ltoc-active-bar);
}

/* Clean */
.leaf-toc-preset-clean {
  box-shadow: none;
  border-color: var(--ltoc-divider);
}

/* Elegant */
.leaf-toc-preset-elegant {
  --ltoc-header-bg: var(--ltoc-active-bg);
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,.09);
}
.leaf-toc-preset-elegant .leaf-toc-title {
  color: var(--ltoc-active-bar);
}

/* Compact */
.leaf-toc-preset-compact {
  --ltoc-font-size: 0.8125rem;
  --ltoc-indent: 0.75rem;
}
.leaf-toc-preset-compact .leaf-toc-header {
  padding: 0.5rem 0.75rem;
}
.leaf-toc-preset-compact .leaf-toc-content {
  padding: 0.375rem 0.75rem 0.5rem;
}
.leaf-toc-preset-compact .leaf-toc-link {
  padding: 0.125rem 0.25rem;
}

/* =============================================
   RTL SUPPORT
   ============================================= */

.leaf-toc-rtl .leaf-toc-list.leaf-toc-depth-1,
.leaf-toc-rtl .leaf-toc-list.leaf-toc-depth-2,
.leaf-toc-rtl .leaf-toc-list.leaf-toc-depth-3 {
  padding-left: 0;
  padding-right: var(--ltoc-indent);
  border-left: none;
  border-right: 1px solid var(--ltoc-divider);
  margin-left: 0;
  margin-right: 0.25rem;
}

.leaf-toc-rtl .leaf-toc-link.is-active::before {
  left: auto;
  right: -1rem;
}

.leaf-toc-rtl .leaf-toc-copy-btn {
  left: auto;
  right: -2rem;
}

/* =============================================
   REDUCED MOTION
   ============================================= */

@media (prefers-reduced-motion: reduce) {
  .leaf-toc-toggle-icon,
  .leaf-toc-link,
  .leaf-toc-copy-btn,
  .leaf-toc-content,
  #leaf-toc-progress-bar {
    transition: none !important;
  }
}

/* =============================================
   MOBILE — RESPONSIVE
   ============================================= */

@media (max-width: 600px) {
  .leaf-toc-wrapper {
    max-width: 100% !important;
    margin-inline: 0;
  }

  .leaf-toc-sticky {
    position: relative;
    top: auto;
    max-height: none;
    overflow: visible;
  }

  /* Auto-collapse on mobile */
  .leaf-toc-wrapper:not(.leaf-toc-collapsible) .leaf-toc-header {
    cursor: pointer;
  }
}

/* =============================================
   PRINT
   ============================================= */

@media print {
  .leaf-toc-wrapper {
    break-inside: avoid;
    box-shadow: none;
    border: 1px solid #ccc;
  }
  .leaf-toc-content[hidden] {
    display: block !important;
  }
  #leaf-toc-progress {
    display: none;
  }
  .leaf-toc-copy-btn {
    display: none;
  }
}
