/* ==========================================================================
   Misk — Table of Contents (TOC)
   Uses theme CSS variables for full visual consistency.
   Dual layout: .misk-toc--above  |  .misk-toc--floating (via FAB icon)
   ========================================================================== */

/* ---------- Container ---------- */
.misk-toc {
  margin-bottom: 1.25rem;
  border: 1px solid var(--misk-border-color);
  border-radius: var(--misk-block-radius);
  background: var(--misk-bg-white-color);
  box-shadow: var(--misk-block-shadow);
  font-family: var(--misk-font-family);
  font-size: 0.9rem;
  line-height: 1.75;
  overflow: hidden;
}

/* ---------- Header ---------- */
.misk-toc__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--misk-border-color);
  cursor: pointer;
  user-select: none;
}

.misk-toc__title {
  font-weight: 700;
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.misk-toc__title-icon {
  display: inline-flex;
  color: var(--misk-theme-color);
}

.misk-toc__toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  border-radius: var(--misk-block-radius);
  color: var(--misk-text-color-muted, #6b7280);
  cursor: pointer;
  transition: background .2s, color .2s;
}
.misk-toc__toggle:hover {
  background: var(--misk-placeholder-bg);
}

.misk-toc__icon {
  transition: transform .25s ease;
}
.misk-toc__toggle[aria-expanded="false"] .misk-toc__icon {
  transform: rotate(-90deg);
}
[dir="rtl"] .misk-toc__toggle[aria-expanded="false"] .misk-toc__icon {
  transform: rotate(90deg);
}

/* ---------- Body ---------- */
.misk-toc__body {
  position: relative;
  max-height: 600px;
  overflow: hidden;
  transition: max-height .35s ease, opacity .25s ease;
}
.misk-toc.is-collapsed .misk-toc__body {
  max-height: 0;
  opacity: 0;
  pointer-events: none;
}

/* ---------- Progress bar ---------- */
.misk-toc__progress {
  position: absolute;
  top: 0;
  inset-inline-start: 0;
  width: 3px;
  height: 100%;
  background: var(--misk-border-color);
  z-index: 1;
}
.misk-toc__progress-bar {
  width: 100%;
  height: 0%;
  background: var(--misk-theme-color);
  border-radius: 0 0 3px 3px;
  transition: height .15s ease-out;
}

/* ---------- List ---------- */
.misk-toc__list {
  list-style: none;
  margin: 0;
  padding: 10px 14px 10px 10px;
  counter-reset: toc-counter;
}
[dir="rtl"] .misk-toc__list {
  padding: 10px 10px 10px 14px;
}

.misk-toc__item {
  counter-increment: toc-counter;
  margin: 0;
  padding: 0;
}

/* ---------- Links ---------- */
.misk-toc__link {
  display: flex;
  align-items: baseline;
  gap: 6px;
  padding: 5px 10px;
  color: inherit;
  text-decoration: none;
  border-radius: var(--misk-block-radius);
  transition: background .2s, color .15s;
  position: relative;
}

.misk-toc__link::before {
  content: counter(toc-counter);
  flex-shrink: 0;
  min-width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7em;
  font-weight: 700;
  border-radius: 50%;
  background: var(--misk-placeholder-bg);
  color: var(--misk-text-color-muted, #6b7280);
  transition: background .2s, color .2s;
}

.misk-toc__link:hover {
  background: var(--misk-placeholder-bg);
  text-decoration: none;
  color: var(--misk-theme-color);
}
.misk-toc__link:hover::before {
  background: var(--misk-theme-color);
  color: #fff;
}

/* Active state */
.misk-toc__link.is-active {
  background: var(--misk-placeholder-bg);
  color: var(--misk-theme-color);
  font-weight: 600;
}
.misk-toc__link.is-active::before {
  background: var(--misk-theme-color);
  color: #fff;
}

/* Read / passed state */
.misk-toc__link.is-read {
  color: var(--misk-text-color-muted, #94a3b8);
}
.misk-toc__link.is-read::before {
  background: transparent;
  color: var(--misk-text-color-muted, #94a3b8);
  content: "\2713";
  font-size: 0.75em;
}

/* ---------- Depth indentation ---------- */
.misk-toc__item--l1 .misk-toc__link {
  padding-inline-start: 28px;
  font-size: 0.88em;
}
.misk-toc__item--l1 .misk-toc__link::before {
  width: 16px;
  height: 16px;
  font-size: 0.65em;
}
.misk-toc__item--l2 .misk-toc__link {
  padding-inline-start: 46px;
  font-size: 0.84em;
}
.misk-toc__item--l2 .misk-toc__link::before {
  width: 14px;
  height: 14px;
  font-size: 0.6em;
  border-radius: 3px;
}

/* ---------- Above-content variant ---------- */
.misk-toc--above {
  max-width: 680px;
}

/* ==========================================================================
   Floating FAB + Panel
   ========================================================================== */

/* --- FAB (Floating Action Button) --- */
.misk-toc-fab {
  position: fixed;
  bottom: 85px;
  inset-inline-start: 18px;
  z-index: 1040;
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: var(--misk-theme-color);
  color: #fff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .25);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .2s ease, box-shadow .2s ease, background .2s ease;
}
.misk-toc-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
}
.misk-toc-fab:active {
  transform: scale(0.96);
}

/* Icon toggle: list ↔ close */
.misk-toc-fab__icon {
  position: absolute;
  transition: opacity .2s ease, transform .2s ease;
}
.misk-toc-fab__icon--close {
  opacity: 0;
  transform: rotate(-90deg);
}
.misk-toc-fab[aria-expanded="true"] .misk-toc-fab__icon--list {
  opacity: 0;
  transform: rotate(90deg);
}
.misk-toc-fab[aria-expanded="true"] .misk-toc-fab__icon--close {
  opacity: 1;
  transform: rotate(0deg);
}

/* Progress ring around FAB */
.misk-toc-fab::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: #fff;
  opacity: 0.5;
  transition: opacity .3s;
  pointer-events: none;
}
.misk-toc-fab:not([aria-expanded="true"])::after {
  animation: misk-toc-fab-pulse 2s ease-in-out infinite;
}
@keyframes misk-toc-fab-pulse {
  0%, 100% { opacity: 0; }
  50%      { opacity: 0.4; }
}

/* --- Panel --- */
.misk-toc-panel {
  position: fixed;
  bottom: 145px;
  inset-inline-start: 18px;
  z-index: 1039;
  width: 320px;
  max-height: calc(100vh - 140px);
  pointer-events: none;
  opacity: 0;
  transform: translateY(16px) scale(0.96);
  transition: opacity .25s ease, transform .25s ease;
}
.misk-toc-panel.is-open {
  pointer-events: auto;
  opacity: 1;
  transform: translateY(0) scale(1);
}

/* Floating variant of the TOC container */
.misk-toc--floating {
  margin-bottom: 0;
  border: 1px solid var(--misk-border-color);
  border-radius: calc(var(--misk-block-radius) * 2);
  background: var(--misk-bg-white-color);
  box-shadow: 0 8px 32px rgba(0, 0, 0, .15);
  overflow: hidden;
}
.misk-toc--floating .misk-toc__header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--misk-border-color);
}
.misk-toc--floating .misk-toc__body {
  max-height: calc(100vh - 260px);
  overflow-y: auto;
}
.misk-toc--floating .misk-toc__list {
  padding: 8px 10px;
}
.misk-toc--floating .misk-toc__link {
  padding: 4px 8px;
  font-size: 0.85rem;
}

/* Floating panel scrollbar */
.misk-toc--floating .misk-toc__body::-webkit-scrollbar {
  width: 4px;
}
.misk-toc--floating .misk-toc__body::-webkit-scrollbar-track {
  background: transparent;
}
.misk-toc--floating .misk-toc__body::-webkit-scrollbar-thumb {
  background: var(--misk-border-color);
  border-radius: 4px;
}

/* --- Overlay behind panel (mobile) --- */
.misk-toc-overlay {
  position: fixed;
  inset: 0;
  z-index: 1038;
  background: rgba(0, 0, 0, .3);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
.misk-toc-overlay.is-active {
  opacity: 1;
  pointer-events: auto;
}

/* ---------- Responsive ---------- */
@media (max-width: 576px) {
  .misk-toc-fab {
    bottom: 76px;
    inset-inline-start: 14px;
    width: 44px;
    height: 44px;
  }
  .misk-toc-panel {
    inset-inline-start: 12px;
    inset-inline-end: 12px;
    bottom: 132px;
    width: auto;
    max-height: 60vh;
  }
  .misk-toc--floating .misk-toc__body {
    max-height: calc(60vh - 60px);
  }
}

/* ---------- Print ---------- */
@media print {
  .misk-toc {
    break-inside: avoid;
    border: 1px solid #ccc;
    box-shadow: none;
  }
  .misk-toc__toggle,
  .misk-toc__progress {
    display: none;
  }
  .misk-toc-fab,
  .misk-toc-panel,
  .misk-toc-overlay {
    display: none;
  }
}

/* ---------- Smooth scroll anchor offset ---------- */
[id] {
  scroll-margin-top: 80px;
}
