/* ============================================
   TOC — Table of Contents (v2, optimized)
   ============================================ */

:root {
  --toc-accent: var(--primary, #1e40af);
  --toc-accent-soft: var(--primary-soft, #dbeafe);
  --toc-accent-grad: var(--secondary, #0891b2);
  --toc-bg: var(--surface, #fff);
  --toc-bg-soft: var(--surface-soft, #f1f5f9);
  --toc-bg-floating: rgba(255, 255, 255, .9);
  --toc-border: var(--border, #e2e8f0);
  --toc-text: var(--text, #0f172a);
  --toc-text-soft: var(--text-soft, #334155);
  --toc-muted: var(--muted, #64748b);
  --toc-focus: var(--toc-accent);
  --toc-radius: 10px;
  --toc-radius-sm: 6px;
  --toc-shadow-sm: 0 1px 3px rgba(15, 23, 42, .04), 0 2px 8px rgba(15, 23, 42, .03);
  --toc-shadow-md: 0 4px 16px rgba(15, 23, 42, .08);
  --toc-transition: 200ms cubic-bezier(.4, 0, .2, 1);
  --toc-header-offset: 90px;
  --toc-accent: #00ffe0; 
  --toc-accent-soft: #003d38;
  --toc-accent-grad: #ff2e88;

  --toc-text: #ffffff;
  --toc-text-soft: #e0e0e0;
  --toc-muted: #b0b0b0; 

  --toc-bg: #0a0a0a; 
  --toc-bg-soft: #151515;
  --toc-bg-floating: rgba(10, 10, 10, .92);
  --toc-border: #2a2a2a;
  --toc-focus: #00ffe0;
}

/* Global anchor offset — respected by native scroll-to-anchor too */
:where(h2[id], h3[id]) {
  scroll-margin-top: var(--toc-header-offset);
}

/* ===== Inline TOC ===== */
.article-toc {
  margin: 24px 0 32px;
  padding: 0;
  border: 1px solid var(--toc-border);
  border-left: 4px solid var(--toc-accent);
  border-radius: var(--toc-radius);
  background: var(--toc-bg);
  box-shadow: var(--toc-shadow-sm);
  font-family: inherit;
  overflow: hidden;
  /* Skip rendering offscreen content for perf on long pages */
  contain: layout style;
}

.article-toc[open] .toc-body {
  content-visibility: auto;
  contain-intrinsic-size: auto 400px;
}

.article-toc > summary {
  list-style: none;
  cursor: pointer;
  padding: 14px 20px;
  background: var(--toc-bg-soft);
  border-bottom: 1px solid var(--toc-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: .95em;
  font-weight: 600;
  color: var(--toc-accent);
  user-select: none;
  transition: background-color var(--toc-transition);
}

.article-toc > summary:hover { background: var(--toc-accent-soft); }

.article-toc > summary:focus-visible {
  outline: 2px solid var(--toc-focus);
  outline-offset: -2px;
}

.article-toc > summary::-webkit-details-marker,
.article-toc > summary::marker { display: none; }

.article-toc .toc-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  letter-spacing: .02em;
  text-transform: uppercase;
  font-size: .8em;
}

.article-toc .toc-count {
  font-size: .75em;
  font-weight: 500;
  background: var(--toc-bg);
  color: var(--toc-accent);
  padding: 2px 9px;
  border-radius: 10px;
  border: 1px solid var(--toc-border);
  font-variant-numeric: tabular-nums;
}

.article-toc .toc-chev {
  display: inline-block;
  font-size: .7em;
  opacity: .7;
  margin-left: 6px;
  transition: transform var(--toc-transition);
  /* GPU layer */
  will-change: transform;
}

.article-toc[open] > summary .toc-chev { transform: rotate(180deg); }

.article-toc .toc-body {
  padding: 14px 22px 18px;
  background: var(--toc-bg);
}

.article-toc ol.toc-list {
  list-style: none;
  counter-reset: toc-h2;
  margin: 0;
  padding: 0;
}

.article-toc ol.toc-list > li {
  counter-increment: toc-h2;
  padding: 6px 0 6px 34px;
  position: relative;
  line-height: 1.5;
}

.article-toc ol.toc-list > li::before {
  content: counter(toc-h2, decimal-leading-zero);
  position: absolute;
  left: 0;
  top: 6px;
  font-size: .72em;
  font-weight: 700;
  color: var(--toc-accent);
  background: var(--toc-accent-soft);
  padding: 3px 7px;
  border-radius: 4px;
  min-width: 20px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.article-toc ol.toc-list > li > a,
.article-toc ul.toc-sublist > li > a {
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: color var(--toc-transition), border-color var(--toc-transition);
}

.article-toc ol.toc-list > li > a {
  color: var(--toc-text);
  font-weight: 500;
  font-size: .92em;
}

.article-toc a:hover,
.article-toc a.is-active {
  color: var(--toc-accent);
  border-bottom-color: var(--toc-accent);
}

.article-toc a:focus-visible {
  outline: 2px solid var(--toc-focus);
  outline-offset: 2px;
  border-radius: 2px;
}

.article-toc ul.toc-sublist {
  list-style: none;
  margin: 6px 0 4px;
  padding: 0 0 0 4px;
  border-left: 2px solid var(--toc-border);
}

.article-toc ul.toc-sublist > li {
  padding: 3px 0 3px 14px;
  position: relative;
  font-size: .86em;
  line-height: 1.45;
}

.article-toc ul.toc-sublist > li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 13px;
  width: 6px;
  height: 6px;
  background: var(--toc-accent);
  border-radius: 50%;
  opacity: .5;
}

.article-toc ul.toc-sublist > li > a {
  color: var(--toc-text-soft);
}

/* ===== Floating Right-side TOC ===== */
.toc-floating {
  position: fixed;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 90;
  display: none;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  max-height: 70vh;
  pointer-events: none;
  /* Promote to its own layer — avoids repainting article on scroll */
  will-change: transform;
  contain: layout style;
}

.toc-floating.is-ready { display: flex; }

.toc-floating-track {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  background: var(--toc-bg-floating);
  border: 1px solid var(--toc-border);
  border-radius: 14px;
  box-shadow: var(--toc-shadow-md);
  pointer-events: auto;
  max-height: 70vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  scrollbar-gutter: stable;
}

@supports ((backdrop-filter: blur(10px)) or (-webkit-backdrop-filter: blur(10px))) {
  .toc-floating-track {
    background: rgba(255, 255, 255, .75);
    backdrop-filter: blur(10px) saturate(160%);
    -webkit-backdrop-filter: blur(10px) saturate(160%);
  }
  [data-theme="dark"] .toc-floating-track,
  :root:not([data-theme="light"]) .toc-floating-track {
    background: rgba(30, 41, 59, .7);
  }
}

.toc-floating-track::-webkit-scrollbar { display: none; }

.toc-floating-item {
  display: grid;
  grid-template-columns: auto 0fr;
  align-items: center;
  gap: 0;
  text-decoration: none;
  color: var(--toc-muted);
  font-size: 12px;
  line-height: 1.3;
  padding: 4px 0;
  position: relative;
  transition: color var(--toc-transition), grid-template-columns var(--toc-transition), gap var(--toc-transition);
}

.toc-floating-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--toc-border);
  flex-shrink: 0;
  transition: background-color var(--toc-transition), transform var(--toc-transition), box-shadow var(--toc-transition);
  /* hint compositor */
  will-change: transform;
}

.toc-floating-label {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  min-width: 0;
  max-width: 180px;
  opacity: 0;
  transition: opacity var(--toc-transition);
}

.toc-floating:hover .toc-floating-item,
.toc-floating-item.is-active {
  grid-template-columns: auto 1fr;
  gap: 10px;
}

.toc-floating:hover .toc-floating-label,
.toc-floating-item.is-active .toc-floating-label {
  opacity: 1;
}

.toc-floating-item:hover { color: var(--toc-accent); }

.toc-floating-item:hover .toc-floating-dot {
  background: var(--toc-accent);
  transform: scale(1.3);
}

.toc-floating-item.is-active {
  color: var(--toc-accent);
  font-weight: 600;
}

.toc-floating-item.is-active .toc-floating-dot {
  background: var(--toc-accent);
  transform: scale(1.4);
  box-shadow: 0 0 0 3px var(--toc-accent-soft);
}

.toc-floating-item:focus-visible {
  outline: 2px solid var(--toc-focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ===== Reading progress ring ===== */
.toc-floating-progress {
  position: absolute;
  left: -22px;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 60%;
  background: var(--toc-border);
  border-radius: 2px;
  overflow: hidden;
  pointer-events: none;
}

.toc-floating-progress-bar {
  width: 100%;
  height: 0%;
  background: linear-gradient(180deg, var(--toc-accent), var(--toc-accent-grad));
  border-radius: 2px;
  /* transform-based animation if you ever swap to scaleY — for now height is fine but no transition needed in RAF loop */
  will-change: height;
}

/* ===== Responsive ===== */
@media (max-width: 1280px) {
  .toc-floating { right: 10px; }
  .toc-floating-label { max-width: 140px; }
}

@media (max-width: 1024px) {
  .toc-floating { display: none !important; }
}

@media (max-width: 600px) {
  .article-toc { margin: 18px 0 24px; border-radius: 8px; }
  .article-toc > summary { padding: 12px 14px; font-size: .88em; }
  .article-toc .toc-body { padding: 12px 16px 14px; }
  .article-toc ol.toc-list > li { padding-left: 30px; font-size: .95em; }
  .article-toc ol.toc-list > li::before { font-size: .68em; padding: 2px 6px; }
  .article-toc ul.toc-sublist > li { font-size: .82em; }
}

/* ===== Dark mode (unified) ===== */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --toc-bg: #1a1a1a;
    --toc-bg-soft: rgba(87, 44, 72, .15);
    --toc-bg-floating: rgba(26, 26, 26, .9);
    --toc-border: rgba(87, 44, 72, .4);
    --toc-text: #e8e8e8;
    --toc-text-soft: #bbb;
    --toc-muted: #999;
  }
}

[data-theme="dark"] {
  --toc-bg: #1a1a1a;
  --toc-bg-soft: rgba(87, 44, 72, .15);
  --toc-bg-floating: rgba(26, 26, 26, .9);
  --toc-border: rgba(87, 44, 72, .4);
  --toc-text: #e8e8e8;
  --toc-text-soft: #bbb;
  --toc-muted: #999;
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .article-toc,
  .article-toc *,
  .toc-floating,
  .toc-floating * {
    transition: none !important;
    animation: none !important;
  }
}
