/* Smart TOC — Frontend Styles
   All colors driven by CSS custom properties set inline on .smart-toc
   ──────────────────────────────────────────────────────────────────── */

/* ── Custom Properties (fallbacks when inline vars not set) ──────── */
.smart-toc {
  --smart-toc-accent:       #0073aa;
  --smart-toc-bg:           #f9f9f9;
  --smart-toc-text:         #333333;
  --smart-toc-border:       #e5e5e5;
  --smart-toc-link-hover:   #005177;
  --smart-toc-active-bg:    rgba(0, 115, 170, 0.08);
  --smart-toc-radius:       6px;
  --smart-toc-font-size:    0.9rem;
  --smart-toc-transition:   0.2s ease;
}

/* ── Dark Mode (auto) ─────────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  .smart-toc {
    --smart-toc-bg:      #1e1e1e;
    --smart-toc-text:    #e0e0e0;
    --smart-toc-border:  #3a3a3a;
    --smart-toc-active-bg: rgba(0, 166, 255, 0.12);
  }
}

/* ── Container ────────────────────────────────────────────────────── */
.smart-toc {
  display: block;
  width: 100%;
  max-width: 100%;
  margin: 1.5rem 0;
  padding: 0;
  background: var(--smart-toc-bg);
  border: 1px solid var(--smart-toc-border);
  border-left: 3px solid var(--smart-toc-accent);
  border-radius: var(--smart-toc-radius);
  font-size: var(--smart-toc-font-size);
  color: var(--smart-toc-text);
  box-sizing: border-box;

  /* Prevent CLS: explicit min-height avoids layout shift */
  contain: layout style;
}

/* ── Header ───────────────────────────────────────────────────────── */
.smart-toc__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  cursor: default;
  user-select: none;
  gap: 0.5rem;
}

.smart-toc__title {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--smart-toc-text);
  letter-spacing: 0.01em;
}

/* ── Toggle Button ───────────────────────────────────────────────── */
.smart-toc__toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--smart-toc-border);
  border-radius: 4px;
  cursor: pointer;
  color: var(--smart-toc-text);
  transition: background var(--smart-toc-transition),
              border-color var(--smart-toc-transition);
}

.smart-toc__toggle:hover {
  background: var(--smart-toc-active-bg);
  border-color: var(--smart-toc-accent);
}

.smart-toc__toggle:focus-visible {
  outline: 2px solid var(--smart-toc-accent);
  outline-offset: 2px;
}

/* Chevron icon via CSS — no external icon font needed */
.smart-toc__toggle-icon {
  display: block;
  width: 10px;
  height: 10px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
  transition: transform var(--smart-toc-transition);
}

.smart-toc__toggle[aria-expanded="false"] .smart-toc__toggle-icon {
  transform: rotate(-135deg) translateY(-2px);
}

/* ── Body (collapsible) ───────────────────────────────────────────── */
.smart-toc__body {
  overflow: hidden;
  padding: 0 1rem 0.75rem;
  transition: max-height 0.3s ease, opacity 0.3s ease, padding 0.3s ease;
  max-height: 2000px; /* large enough for any TOC */
  opacity: 1;
}

.smart-toc--collapsible .smart-toc__body[hidden],
.smart-toc--collapsible .smart-toc__body.is-hidden {
  display: block !important; /* override [hidden] to allow transition */
  max-height: 0;
  opacity: 0;
  padding-bottom: 0;
  pointer-events: none;
}

/* ── Lists ────────────────────────────────────────────────────────── */
.smart-toc__list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.smart-toc__list--nested {
  padding-left: 1.25rem;
  margin-top: 0.25rem;
}

/* Numbered variant */
.smart-toc--numbered .smart-toc__list {
  list-style: none;
  counter-reset: smart-toc-counter;
}

.smart-toc--numbered .smart-toc__item > .smart-toc__link::before {
  counter-increment: smart-toc-counter;
  content: counters(smart-toc-counter, ".") ". ";
  color: var(--smart-toc-accent);
  font-weight: 500;
  margin-right: 0.15em;
}

/* ── Items & Links ────────────────────────────────────────────────── */
.smart-toc__item {
  margin: 0.1rem 0;
  line-height: 1.5;
}

.smart-toc__link {
  display: block;
  padding: 0.25rem 0.4rem;
  color: var(--smart-toc-text);
  text-decoration: none;
  border-radius: 3px;
  transition: color var(--smart-toc-transition),
              background var(--smart-toc-transition);
  word-break: break-word;
  hyphens: auto;
}

.smart-toc__link:hover {
  color: var(--smart-toc-accent);
  background: var(--smart-toc-active-bg);
  text-decoration: none;
}

/* Scroll Spy: active item */
.smart-toc__link.is-active {
  color: var(--smart-toc-accent);
  background: var(--smart-toc-active-bg);
  font-weight: 500;
}

/* Nested levels — visual indent cues */
.smart-toc__item--h3 > .smart-toc__link { font-size: 0.875rem; }
.smart-toc__item--h4 > .smart-toc__link { font-size: 0.825rem; opacity: 0.85; }

/* ── Back to Top Button ───────────────────────────────────────────── */
.smart-toc__back-to-top {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--smart-toc-accent);
  color: #fff;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  transform: translateY(8px);
}

.smart-toc__back-to-top:not([hidden]) {
  opacity: 1;
  transform: translateY(0);
}

.smart-toc__back-to-top:hover {
  filter: brightness(1.1);
}

.smart-toc__back-to-top:focus-visible {
  outline: 2px solid var(--smart-toc-accent);
  outline-offset: 3px;
}

/* ── Responsive ───────────────────────────────────────────────────── */

/* Mobile-collapsed: start collapsed on small screens */
@media (max-width: 600px) {
  .smart-toc--mobile-collapsed .smart-toc__body {
    max-height: 0;
    opacity: 0;
    padding-bottom: 0;
    pointer-events: none;
  }

  .smart-toc--mobile-collapsed .smart-toc__toggle {
    /* Indicate collapsed state on load */
  }

  .smart-toc--mobile-collapsed .smart-toc__toggle[aria-expanded="true"] .smart-toc__toggle-icon {
    transform: rotate(45deg) translateY(-2px);
  }

  .smart-toc--mobile-collapsed .smart-toc__toggle[aria-expanded="false"] .smart-toc__toggle-icon {
    transform: rotate(-135deg) translateY(-2px);
  }
}

/* ── Reduced Motion ───────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  .smart-toc__body,
  .smart-toc__toggle-icon,
  .smart-toc__link,
  .smart-toc__back-to-top {
    transition: none;
  }
}

/* ── Print ────────────────────────────────────────────────────────── */
@media print {
  .smart-toc__back-to-top { display: none !important; }
  .smart-toc__toggle      { display: none !important; }
  .smart-toc__body.is-hidden {
    max-height: none !important;
    opacity: 1 !important;
    pointer-events: auto !important;
  }
}
