/**
 * Tocbot 自定义样式
 * 与博客主题保持一致的目录样式
 */

/* ===== ToC 右侧居中（固定定位） ===== */
/* 清理可能影响定位的 overflow 限制 */
.main, .content, .post-content, .container, .page-single, .post-single, main, article, header {
  overflow: visible !important;
}

/* 桌面端：隐藏左侧内嵌目录，显示右侧固定目录（首帧即位于右侧） */
@media (min-width: 1200px) {
  .toc { display: none !important; }
  .toc-sidebar { display: block !important; visibility: visible !important; opacity: 1 !important; }
}

/* 右侧目录固定在视窗右侧垂直居中 */
aside.toc-sidebar,
#toc-sidebar {
  position: fixed !important;
  top: 50% !important;
  right: 20px !important; /* JS 拖拽后会写入 left 并清理 right */
  transform: translateY(-50%) !important;
  z-index: 1000 !important;
  width: 350px !important;
  height: auto !important;
  max-height: 80vh !important;
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
  pointer-events: auto !important;
  padding: 0 !important;
  margin: 0 !important;
}

/* 目录侧边栏容器（内部可交互） */
.toc-sidebar {
  position: fixed !important;
  top: 50% !important;
  right: 20px !important;
  width: 350px !important;
  height: auto !important;
  max-height: 80vh !important;
  z-index: 1000 !important;
  pointer-events: auto !important;
  transform: translateY(-50%) !important;
  padding: 0 !important;
  margin: 0 !important;
  display: block !important;
  visibility: visible !important;
  opacity: 1 !important;
}

/* 目录容器 - 居中显示 */
.toc-container {
  position: relative !important;
  width: 100% !important;
  max-width: 520px !important;
  min-width: 220px !important;
  max-height: 80vh !important;
  min-height: 200px !important;
  resize: both !important; /* 宽高均可拉伸 */
  background: rgba(255, 255, 255, 0.98) !important;
  border: 1px solid rgba(0, 0, 0, 0.08) !important;
  border-radius: 16px !important;
  padding: 0 !important;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.08),
    0 2px 8px rgba(0, 0, 0, 0.04) !important;
  backdrop-filter: blur(20px) !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
  transform: translateX(0) !important;
  display: block !important;
  /* 确保容器在父元素中居中 */
  margin: 0 auto !important;
}

.toc-container.toc-loaded {
  opacity: 1;
  visibility: visible;
  transform: translateX(0);
}

.toc-container:hover {
  box-shadow: 
    0 12px 48px rgba(0, 0, 0, 0.12),
    0 4px 16px rgba(0, 0, 0, 0.08);
  transform: translateX(-5px) scale(1.02);
}

/* 目录头部 */
.toc-header {
  padding: 1rem 1.5rem 0.5rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  margin-bottom: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: move;
}

/* 目录标题 */
.toc-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--primary);
}

.toc-icon {
  font-size: 1rem;
}

.toc-title-text {
  font-size: 0.875rem;
}

/* 收缩按钮 */
.toc-toggle-btn {
  width: 24px;
  height: 24px;
  border: none;
  background: rgba(0, 0, 0, 0.05);
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  color: var(--primary);
  font-size: 12px;
  font-weight: bold;
}

.toc-toggle-btn:hover {
  background: rgba(0, 0, 0, 0.1);
  transform: scale(1.1);
}

.toc-toggle-btn:active {
  transform: scale(0.95);
}

/* 收缩状态 */
.toc-container.collapsed {
  min-width: 48px;
  max-width: 48px;
  padding: 0;
}

.toc-container.collapsed .toc-header {
  padding: 0.75rem 0.5rem;
  border-bottom: none;
  margin-bottom: 0;
  justify-content: center;
}

.toc-container.collapsed .toc-title {
  justify-content: center;
}

.toc-container.collapsed .toc-icon,
.toc-container.collapsed .toc-title-text {
  display: none;
}

.toc-container.collapsed .toc-content {
  display: none;
}

.toc-container.collapsed .toc-toggle-btn {
  margin: 0;
}

/* 收缩按钮图标动画 */
.toc-toggle-btn .toggle-icon {
  transition: transform 0.3s ease;
}

.toc-container.collapsed .toc-toggle-btn .toggle-icon {
  transform: rotate(180deg);
}

/* 目录内容容器 */
.toc-content {
  padding: 0;
  max-height: calc(80vh - 80px);
  overflow-y: auto;
  overflow-x: hidden;
}

/* 目录列表容器 */
.toc-list {
  padding: 0.5rem 0 1rem;
}

/* 自定义滚动条 */
.toc-content::-webkit-scrollbar {
  width: 4px;
}

.toc-content::-webkit-scrollbar-track {
  background: transparent;
}

.toc-content::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

.toc-content::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* 重置 Tocbot 默认样式 */
.toc-list ol,
.toc-list ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.toc-list li {
  margin: 0;
  padding: 0;
}

/* 目录链接样式 */
.toc-link {
  display: block;
  padding: 0.5rem 1.5rem;
  color: var(--content);
  text-decoration: none;
  font-size: 0.875rem;
  line-height: 1.4;
  border-left: 3px solid transparent;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toc-link:hover {
  color: var(--primary);
  background: rgba(0, 122, 204, 0.05);
  border-left-color: rgba(0, 122, 204, 0.3);
  transform: translateX(2px);
}

/* 活动链接样式 */
.toc-link.is-active-link {
  color: var(--primary);
  background: linear-gradient(90deg, rgba(0, 122, 204, 0.1), transparent);
  border-left-color: var(--primary);
  font-weight: 600;
  transform: translateX(4px);
  position: relative;
}

/* 点击动画效果 */
.toc-link.toc-link-clicked {
  animation: tocLinkClick 0.3s ease;
}

@keyframes tocLinkClick {
  0% { transform: translateX(4px) scale(1); }
  50% { transform: translateX(6px) scale(1.05); }
  100% { transform: translateX(4px) scale(1); }
}

/* 高亮动画效果 */
.toc-link.toc-highlight {
  animation: tocHighlight 1s ease;
}

@keyframes tocHighlight {
  0% { 
    background: linear-gradient(90deg, rgba(0, 122, 204, 0.1), transparent);
    box-shadow: none;
  }
  50% { 
    background: linear-gradient(90deg, rgba(0, 122, 204, 0.2), rgba(0, 122, 204, 0.05));
    box-shadow: 0 0 10px rgba(0, 122, 204, 0.3);
  }
  100% { 
    background: linear-gradient(90deg, rgba(0, 122, 204, 0.1), transparent);
    box-shadow: none;
  }
}

.toc-link.is-active-link::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 3px;
  height: 100%;
  background: var(--primary);
  animation: activeIndicator 0.3s ease;
}

@keyframes activeIndicator {
  from {
    height: 0;
    top: 50%;
  }
  to {
    height: 100%;
    top: 0;
  }
}

/* 层级缩进 */
.toc-list .toc-list {
  padding-left: 0;
}

.toc-list .toc-list .toc-link {
  padding-left: 2rem;
  font-size: 0.8125rem;
  opacity: 0.8;
}

.toc-list .toc-list .toc-list .toc-link {
  padding-left: 2.5rem;
  font-size: 0.75rem;
  opacity: 0.7;
}

.toc-list .toc-list .toc-list .toc-list .toc-link {
  padding-left: 3rem;
  font-size: 0.75rem;
  opacity: 0.6;
}

/* 层级指示器 */
.toc-list .toc-list .toc-link::before {
  content: '└';
  position: absolute;
  left: 1.2rem;
  color: rgba(0, 0, 0, 0.3);
  font-size: 0.75rem;
}

.toc-list .toc-list .toc-list .toc-link::before {
  left: 1.7rem;
}

.toc-list .toc-list .toc-list .toc-list .toc-link::before {
  left: 2.2rem;
}

/* 折叠功能样式 */
.is-collapsible {
  position: relative;
}

.is-collapsible::after {
  content: '▼';
  position: absolute;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.75rem;
  color: rgba(0, 0, 0, 0.5);
  transition: transform 0.2s ease;
  cursor: pointer;
}

.is-collapsed::after {
  transform: translateY(-50%) rotate(-90deg);
}

.is-collapsed + .toc-list {
  display: none;
}

/* 主内容区域适配 */
@media (min-width: 1200px) {
  .post-content { margin-right: 380px; }
}

/* 响应式设计 */
@media (max-width: 1400px) {
  .toc-sidebar {
    width: 300px;
    right: 15px;
  }
  
  .toc-container {
    max-width: 280px;
    min-width: 250px;
  }
  
  .post-content {
    margin-right: 330px !important;
  }
}

/* 移动端隐藏阈值调整为 1200px 以下 */
@media (max-width: 1199px) {
  .toc-sidebar { display: none !important; }
  .post-content { margin-right: 0 !important; }
}

@media (max-width: 768px) {
  .toc-container {
    position: relative;
    top: auto;
    right: auto;
    transform: none;
    max-width: 100%;
    min-width: auto;
    margin: 1rem 0;
    border-radius: 8px;
  }
}

/* 提示工具样式 */
.toc-tooltip {
  position: absolute;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  font-size: 0.75rem;
  white-space: nowrap;
  z-index: 1000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}

/* 收缩动画优化 */
.toc-container {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.toc-container.collapsed {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.toc-list {
  transition: opacity 0.2s ease, transform 0.3s ease;
  transform-origin: top center;
}

.toc-container.collapsed .toc-list {
  opacity: 0;
  transform: scaleY(0);
}

/* 收缩状态下的悬停效果 */
.toc-container.collapsed:hover {
  transform: translateX(-5px) scale(1.05);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .toc-container {
    background: rgba(30, 30, 30, 0.98);
    border-color: rgba(255, 255, 255, 0.1);
  }
  
  .toc-title {
    color: var(--primary);
    border-bottom-color: rgba(255, 255, 255, 0.1);
  }
  
  .toc-link {
    color: rgba(255, 255, 255, 0.8);
  }
  
  .toc-link:hover {
    background: rgba(0, 122, 204, 0.1);
    color: var(--primary);
  }
  
  .toc-link.is-active-link {
    color: var(--primary);
    background: linear-gradient(90deg, rgba(0, 122, 204, 0.15), transparent);
  }
  
  .toc-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
  }
}

/* 打印样式 */
@media print {
  .toc-container {
    display: none !important;
  }
}

/* 无障碍访问 */
.toc-link:focus {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* 加载动画 */
.toc-container:not(.toc-loaded) {
  animation: tocLoading 1s ease-in-out infinite alternate;
}

@keyframes tocLoading {
  from {
    opacity: 0.3;
  }
  to {
    opacity: 0.7;
  }
}

/* 跳转提示 */
.toc-container::after {
  content: '📍 点击跳转到章节';
  position: absolute;
  bottom: -35px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 15px;
  font-size: 0.75rem;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
  z-index: 101;
}

.toc-container:hover::after {
  opacity: 1;
}

/* ===== Mobile TOC ===== */
@media (max-width: 1199px) {
  .mobile-toc-fab {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 1100;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 10px 14px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.2);
    font-size: 14px;
    line-height: 1;
  }

  .mobile-toc-drawer {
    position: fixed;
    inset: 0;
    display: none;
    z-index: 1099;
  }

  .mobile-toc-drawer.open { display: block; }

  .mobile-toc-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.45);
    opacity: 0;
    transition: opacity .2s ease;
  }

  .mobile-toc-drawer.open .mobile-toc-backdrop { opacity: 1; }

  .mobile-toc-panel {
    position: absolute;
    right: 0;
    top: 0;
    height: 100%;
    width: min(86%, 360px);
    background: var(--theme);
    color: var(--content);
    border-left: 1px solid rgba(0,0,0,0.08);
    transform: translateX(100%);
    transition: transform .25s ease;
    display: flex;
    flex-direction: column;
  }

  .mobile-toc-drawer.open .mobile-toc-panel { transform: translateX(0); }

  .mobile-toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    border-bottom: 1px solid rgba(0,0,0,0.08);
  }

  .mobile-toc-title { font-size: 16px; margin: 0; }

  .mobile-toc-close {
    background: transparent; border: none; font-size: 18px; line-height: 1; color: var(--content);
  }

  .mobile-toc-content {
    padding: 8px 0;
    overflow: auto;
    height: 100%;
  }

  .mobile-toc-list { padding: 0 8px 24px; }

  /* 隐藏桌面侧边 TOC */
  .toc-sidebar { display: none !important; }
}

@media (prefers-color-scheme: dark) {
  .mobile-toc-panel { border-left-color: rgba(255,255,255,0.1); }
  .mobile-toc-header { border-bottom-color: rgba(255,255,255,0.1); }
}
