#toc-nav {
  position: fixed;
  top: 100px;
  /* Bám lề phải, nhưng không bao giờ tràn ra ngoài hay đè nội dung.
     Ưu tiên nằm cạnh cột nội dung (rộng ~960px); nếu không đủ chỗ thì
     cách mép phải 20px. */
  right: max(20px, calc((100vw - 960px) / 2 - 220px));
  width: 200px;
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  background: #fff;
  border: 1px solid #e8eaed;
  border-radius: 12px;
  padding: 14px 0 10px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.07);
  font-family: 'Open Sans', sans-serif;
  font-size: 0.78rem;
  z-index: 100;
  opacity: 0;
  transform: translateX(10px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  scrollbar-width: thin;
  scrollbar-color: #e0e0e0 transparent;
}

#toc-nav.visible {
  opacity: 1;
  transform: translateX(0);
}

/* Ẩn khi màn hình không đủ rộng */
@media (max-width: 1300px) {
  #toc-nav { display: none; }
}

#toc-header {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 800;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #888;
  padding: 0 14px 10px;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 6px;
}

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

#toc-list li { margin: 0; }

#toc-list a {
  display: block;
  padding: 5px 14px;
  color: #555;
  text-decoration: none;
  line-height: 1.45;
  border-left: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#toc-list a:hover {
  color: #008AFF;
  background: #f5f9ff;
}

/* H3 indent */
#toc-list li.toc-h3 a {
  padding-left: 24px;
  font-size: 0.73rem;
  color: #777;
}

/* Active — đang đọc section này */
#toc-list a.active {
  color: #008AFF;
  border-left-color: #008AFF;
  background: #f0f7ff;
  font-weight: 700;
}

/* ---- Dark mode ---- */
body.dark-mode #toc-nav {
  background: #161b22;
  border-color: #30363d;
  box-shadow: 0 2px 12px rgba(0,0,0,0.4);
}
body.dark-mode #toc-header { color: #8b949e; border-bottom-color: #30363d; }
body.dark-mode #toc-list a { color: #adbac7; }
body.dark-mode #toc-list li.toc-h3 a { color: #8b949e; }
body.dark-mode #toc-list a:hover { color: #58a6ff; background: #21262d; }
body.dark-mode #toc-list a.active {
  color: #58a6ff;
  border-left-color: #58a6ff;
  background: #1c2c44;
}
