/**
 * WAGI Smart TOC 樣式（左欄目錄、不浮動，隨頁面捲動）
 */

/* ========================================
   變數定義
   ======================================== */
:root {
  --wagi-toc-bg: #ffffff;
  --wagi-toc-text: #333333;
  --wagi-toc-text-light: #666666;
  --wagi-toc-border: #e8e8e8;
  --wagi-toc-active: #5a8164;
  --wagi-toc-active-bg: #e8f5e9;
  --wagi-toc-accent: #9b9b9b;
  --wagi-toc-accent-hover: #5a8164;
  --wagi-toc-underline: #5a8164;
  --wagi-toc-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  --wagi-toc-width: 260px;
  --wagi-toc-transition: 0.3s ease;
}

/* ========================================
   版面：左欄目錄 + 中欄文章 + 右欄其他文章（三欄，不浮動）
   外層突破主題容器，內層置中並限制寬度 → 文章不擠壓、三欄不貼邊
   ======================================== */
.wagi-toc-wrapper {
  display: flex;
  justify-content: center;
  flex-direction: column;
  gap: 42px;
  margin: 36px auto 118px;
  width: 100vw;
  max-width: 100vw;
  align-items: center;
  margin-left: calc(50% - 50vw);
  margin-right: calc(50% - 50vw);
  padding-left: 24px;
  padding-right: 24px;
  box-sizing: border-box;
  padding-left: 24px;
  padding-right: 24px;
  box-sizing: border-box;
}

.wagi-toc-inner {
  display: flex;
  gap: 32px;
  max-width: 1280px;
  width: 100%;
  align-items: flex-start;
  position: relative;
  /* Tri-State Logic Requirement */
}

.wagi-toc-article {
  flex: 1;
  min-width: 0;
}

.wagi-breadcrumb {
  display: flex;
  width: 100%;
  max-width: 1280px;
  gap: 0.5rem;
  list-style: none;
  margin: 0;
  padding: 0;
  background-color: var(--bg-color-light);
  border-radius: var(--border-radius-md);
  color: var(--wagi-toc-text);
}

/* ========================================
   單篇文章：標題、特色圖片、內文排版
   ======================================== */
.single-post__title {
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.4;
  margin: 0 0 24px 0;
  color: #222;
}

.wagi-toc-article .single-post__thumbnail {
  width: 100%;
  position: relative;
  margin-bottom: 40px;
  border-radius: 8px;
  overflow: hidden;
}

.wagi-toc-article .single-post__thumbnail img {
  width: 100%;
  height: auto;
  display: block;
}

/* 內文排版 */
.wagi-toc-article h2 {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.4;
  margin: 48px 0 16px;
  color: #222;
}

.wagi-toc-article h3 {
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.4;
  margin: 32px 0 12px;
  color: #333;
}

.wagi-toc-article p {
  font-size: 1rem;
  line-height: 1.8;
  margin: 0 0 20px;
  color: #444;
}

.wagi-toc-article img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 24px 0;
}

.wagi-toc-article ul,
.wagi-toc-article ol {
  margin: 0 0 20px;
  padding-left: 1.5em;
  line-height: 1.8;
  color: #444;
}

.wagi-toc-article li {
  margin-bottom: 8px;
}

.wagi-toc-article blockquote {
  margin: 24px 0;
  padding: 16px 24px;
  border-left: 4px solid var(--wagi-toc-underline);
  background: #f9f9f9;
  color: #555;
  font-style: italic;
}

@media (max-width: 992px) {
  .single-post__title {
    font-size: 1.4rem;
    margin-bottom: 20px;
  }

  .single-post__thumbnail {
    margin-bottom: 28px;
  }

  .wagi-toc-article h2 {
    font-size: 1.3rem;
    margin: 36px 0 12px;
  }

  .wagi-toc-article h3 {
    font-size: 1.125rem;
    margin: 24px 0 10px;
  }

  .wagi-toc-article p {
    font-size: 0.95rem;
    line-height: 1.75;
  }
}

/* ========================================
   右欄：其他文章（同標籤文章列表，設計稿樣式）
   ======================================== */
.wagi-related-posts {
  flex-shrink: 0;
  width: var(--wagi-toc-width);
  position: relative;
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
}

/* 大於 992px 才套用置頂相關樣式 */
@media (min-width: 993px) {
  .wagi-toc-sidebar.is-sticky,
  .wagi-related-posts.is-sticky {
    position: fixed !important;
    top: 100px !important;
    z-index: 900;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
  }

  .wagi-toc-sidebar.is-frozen,
  .wagi-related-posts.is-frozen {
    position: absolute !important;
    z-index: 900;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
  }
}

/* 與本文目錄標題相同文字樣式（提高優先級以覆蓋主題對 h3 的樣式） */
.wagi-toc-wrapper .wagi-related-posts .wagi-related-title,
.wagi-toc-wrapper .wagi-related-posts h3.wagi-related-title {
  font-weight: 600 !important;
  color: var(--wagi-toc-text) !important;
  font-size: 16px !important;
  line-height: 1.3 !important;
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif !important;
  margin: 0 0 12px 0 !important;
  padding: 0 0 10px 0 !important;
  border-bottom: 14px solid var(--wagi-toc-underline);
}

.wagi-related-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.wagi-related-item {
  margin: 0;
  margin-left: 28px;
  padding: 0;
  border-bottom: 1px solid #000;
}

.wagi-related-item:last-child {
  border-bottom: none;
}

.wagi-related-link {
  display: block;
  padding: 12px 0;
  color: var(--wagi-toc-text);
  text-decoration: none;
  transition:
    color var(--wagi-toc-transition),
    background var(--wagi-toc-transition),
    padding var(--wagi-toc-transition) !important;
  line-height: 1.4;
  word-break: break-word;
}

.wagi-related-link:hover {
  color: var(--wagi-toc-accent);
  background: var(--wagi-toc-active-bg);
  padding: 12px;
}

.wagi-related-empty {
  padding: 12px 0;
  color: var(--wagi-toc-text-light);
  font-size: 14px;
}

.wagi-related-more {
  display: inline-block;
  margin-top: 16px;
  margin-left: 1.5rem;
  padding: 8px 28px;
  background: var(--wagi-toc-accent);
  color: #000;
  text-align: center;
  text-decoration: none;
  font-size: 15px;
  font-weight: 500;
  border-radius: 2px;
  transition:
    opacity var(--wagi-toc-transition),
    background var(--wagi-toc-transition);
}

.wagi-related-more:hover {
  opacity: 0.95;
  background: var(--wagi-toc-accent-hover, #5a8164);
  color: #fff;
}

/* ========================================
   桌面版 - 左欄目錄（不浮動，隨頁面捲動）
   ======================================== */
.wagi-toc {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, sans-serif;
  font-size: 14px;
  overflow: hidden;
  transition: opacity var(--wagi-toc-transition);
}

/* 左欄目錄：在文件流中，不設 position: fixed */
.wagi-toc.wagi-toc-sidebar {
  flex-shrink: 0;
  width: var(--wagi-toc-width);
  position: relative;
  background: var(--wagi-toc-bg);
}

/* 目錄標題列 - 設計稿：標題 + 綠色下劃線 */
.wagi-toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 0 10px 0;
  margin-bottom: 12px;
  border-bottom: 14px solid var(--wagi-toc-underline);
  cursor: pointer;
  user-select: none;
}

/* 桌面版左欄隱藏收合按鈕 */
.wagi-toc.wagi-toc-sidebar .wagi-toc-toggle {
  display: none;
}

.wagi-toc-title {
  font-weight: 600;
  color: var(--wagi-toc-text);
  font-size: 14px;
}

.wagi-toc-toggle {
  background: none;
  border: none;
  padding: 4px;
  cursor: pointer;
  color: var(--wagi-toc-text-light);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color var(--wagi-toc-transition);
}

.wagi-toc-toggle:hover {
  color: var(--wagi-toc-text);
}

/* 行動版專用關閉鈕（桌面不顯示） */
.wagi-toc-mobile-close {
  display: none;
}

.wagi-toc-icon-collapse {
  display: none;
}

.wagi-toc.is-collapsed .wagi-toc-icon-expand {
  display: none;
}

.wagi-toc.is-collapsed .wagi-toc-icon-collapse {
  display: block;
}

/* 目錄內容區 */
.wagi-toc-content {
  overflow-y: auto;
  padding: 0;
  transition:
    max-height var(--wagi-toc-transition),
    opacity var(--wagi-toc-transition);
}

.wagi-toc.wagi-toc-sidebar .wagi-toc-content {
  max-height: none;
  margin-left: 28px;
}

.wagi-toc.is-collapsed .wagi-toc-content {
  max-height: 0;
  opacity: 0;
  padding: 0;
  overflow: hidden;
}

/* 目錄列表 - 設計稿：項與項之間淺灰分隔線 */
.wagi-toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.wagi-toc-item {
  margin: 0;
  padding: 0;
  border-bottom: 1px solid #000;
}

.wagi-toc-item:last-child {
  border-bottom: none;
}

.wagi-toc-link {
  display: block;
  padding: 12px 0;
  color: var(--wagi-toc-text);
  text-decoration: none;
  transition: all var(--wagi-toc-transition);
  line-height: 1.4;
  word-break: break-word;
}

.wagi-toc-link:hover {
  color: var(--wagi-toc-accent);
}
.wagi-toc-link.is-active {
  color: #d8d8d8;
  font-weight: 500;
}

/* 標題層級縮排 */
.wagi-toc-level-1 .wagi-toc-link {
  padding-left: 0;
  font-weight: 500;
}

.wagi-toc-level-2 .wagi-toc-link {
}

.wagi-toc-level-3 .wagi-toc-link {
  font-size: 13px;
}

/* 捲軸樣式 */
.wagi-toc-content::-webkit-scrollbar {
  width: 4px;
}

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

.wagi-toc-content::-webkit-scrollbar-thumb {
  background: var(--wagi-toc-border);
  border-radius: 2px;
}

.wagi-toc-content::-webkit-scrollbar-thumb:hover {
  background: var(--wagi-toc-text-light);
}

/* ========================================
   行動版：左上角書籤式觸發（收合目錄與其他文章）
   ======================================== */
.wagi-toc-mobile-trigger {
  display: none;
  position: fixed;
  top: 28rem;
  left: 0;
  width: 44px;
  min-height: 100px;
  background: var(--wagi-toc-bg);
  border: none;
  border-left: none;
  border-radius: 0 12px 12px 0;
  box-shadow: 2px 2px 12px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  z-index: 998;
  align-items: center;
  justify-content: center;
  padding: 16px 10px;
  font-size: 15px;
  font-weight: 600;
  color: var(--wagi-toc-text);
  transition:
    box-shadow var(--wagi-toc-transition),
    transform var(--wagi-toc-transition);
  writing-mode: vertical-rl;
  letter-spacing: 0.1em;
}

.wagi-toc-mobile-trigger:hover {
  box-shadow: 3px 3px 16px rgba(0, 0, 0, 0.16);
}

.wagi-toc-mobile-trigger:focus {
  outline: 2px solid var(--wagi-toc-accent);
  outline-offset: 2px;
}

.wagi-toc-mobile-trigger .wagi-toc-mobile-trigger-text {
  display: block;
}

.wagi-toc-mobile-trigger svg {
  display: none;
}

/* ========================================
   響應式設計
   ======================================== */
@media (max-width: 1200px) {
  .wagi-toc.wagi-toc-sidebar {
    width: 220px;
  }

  .wagi-related-posts {
    width: 220px;
  }
}

@media (max-width: 992px) {
  .wagi-toc-inner {
    flex-direction: column;
  }

  .wagi-toc.wagi-toc-sidebar {
    width: 100%;
    order: -1;
  }

  /* 行動版：其他文章收合進書籤抽屜，版面中不顯示（由 JS 移入抽屜） */
  .wagi-toc-inner > .wagi-related-posts {
    display: none;
  }

  /* 行動版：左側滑出面板（約 2/3 寬、全高，白底） */
  .wagi-toc.wagi-toc-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 999;
    width: 66.666%;
    max-width: 400px;
    height: 100%;
    border-radius: 0 0 0 0;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
    border: none;
    border-right: 1px solid var(--wagi-toc-border);
    transform: translateX(-100%);
    opacity: 1;
    visibility: hidden;
    transition:
      transform var(--wagi-toc-transition),
      visibility var(--wagi-toc-transition);
    display: flex;
    flex-direction: column;
    background: var(--wagi-toc-bg);
  }

  .wagi-toc.is-mobile-open {
    transform: translateX(0);
    visibility: visible;
  }

  /* 行動版右上角 X 關閉鈕 */
  .wagi-toc-mobile-close {
    display: none;
    position: absolute;
    top: 16px;
    right: 16px;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--wagi-toc-text-light);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    transition:
      color var(--wagi-toc-transition),
      background var(--wagi-toc-transition);
    z-index: 2;
  }

  .wagi-toc-mobile-close:hover {
    color: var(--wagi-toc-text);
    background: rgba(0, 0, 0, 0.06);
  }

  .wagi-toc.is-mobile-open .wagi-toc-mobile-close {
    display: flex;
  }

  .wagi-toc.is-mobile-open .wagi-toc-toggle {
    display: none;
  }

  /* 行動版面板：不設 padding，改由各區塊自己留白 → 本文目錄與相關文章從同一左緣開始 */
  .wagi-toc.is-mobile-open {
    padding-left: 22px;
    padding-right: 16px;
    box-sizing: border-box;
    border-bottom: 8px solid var(--wagi-toc-underline);
    border-top: 8px solid var(--wagi-toc-underline);
  }

  .wagi-toc.is-mobile-open .wagi-toc-mobile-close {
    right: 20px;
  }

  /* 本文目錄標題：上方多留空白，視覺較舒爽 */
  .wagi-toc.is-mobile-open .wagi-toc-header {
    padding-top: 72px;
    padding-right: 48px;
    padding-bottom: 10px;
    padding-left: 0;
    margin-bottom: 12px;
    border-bottom: 4px solid var(--wagi-toc-underline);
  }

  /* 本文目錄區：約五行高度，左 20px 與標題對齊 */
  .wagi-toc.is-mobile-open .wagi-toc-content {
    flex: 0 0 auto;
    min-height: 0;
    max-height: 12rem;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding-left: 0;
    padding-right: 12px;
    margin-left: 0;
  }
  .wagi-toc.is-mobile-open .wagi-related-item {
    margin-left: 0;
  }

  /* 行動版 TOC 層級縮排調整：Level 2 不縮排（視覺上與下方區塊對齊）、Level 3 縮 12px */
  .wagi-toc.is-mobile-open .wagi-toc-link {
    margin-left: 0;
  }
  .wagi-toc.is-mobile-open .wagi-toc-link.is-active {
    margin-left: 0;
  }

  .wagi-toc.is-mobile-open .wagi-toc-level-2 .wagi-toc-link {
    padding-left: 0;
    margin-left: 12px;
  }

  .wagi-toc.is-mobile-open .wagi-toc-level-3 .wagi-toc-link {
    padding-left: 24px;
  }

  /* 目錄區內頁滾軸：淺灰軌道、綠色滑塊 */
  .wagi-toc.is-mobile-open .wagi-toc-content::-webkit-scrollbar {
    width: 8px;
  }

  .wagi-toc.is-mobile-open .wagi-toc-content::-webkit-scrollbar-track {
    background: #f0f0f0;
    border-radius: 4px;
  }

  .wagi-toc.is-mobile-open .wagi-toc-content::-webkit-scrollbar-thumb {
    background: var(--wagi-toc-accent);
    border-radius: 4px;
  }

  .wagi-toc.is-mobile-open .wagi-toc-content::-webkit-scrollbar-thumb:hover {
    background: var(--wagi-toc-accent-hover);
  }

  /* 相關文章區：與本文目錄同一留白，無滾軸（以「更多文章」按鈕為主） */
  .wagi-toc .wagi-related-posts.wagi-in-drawer {
    width: 100%;
    min-width: 0;
    padding: 12px 20px 24px 0;
    border-top: 1px solid var(--wagi-toc-border);
    margin: 0;
    flex-shrink: 0;
    overflow: visible;
    box-sizing: border-box;
  }

  /* 相關文章標題列：與本文目錄同一起點、同粗橫線寬（用負 margin 讓標題列滿寬） */
  .wagi-toc .wagi-related-posts.wagi-in-drawer .wagi-related-title {
    margin: 0;
    padding: 0 0 10px 0 !important;
    font-size: 16px !important;
    font-weight: 600 !important;
    box-sizing: content-box;
    border-bottom: 4px solid var(--wagi-toc-underline);
  }

  /* 列表與按鈕：已在區塊 padding 內，不需額外 margin */
  /* 列表與按鈕：已在區塊 padding 內，不需額外 margin */
  /* 行動版：縮小列表項目高度 (padding 12px -> 8px)，節省垂直空間 */
  .wagi-toc .wagi-related-posts.wagi-in-drawer .wagi-related-link {
    padding: 8px 0;
  }

  .wagi-toc .wagi-related-posts.wagi-in-drawer .wagi-related-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-left: 0;
    margin-right: 0;
  }

  /* 行動版：只顯示前 3 篇文章，隱藏第 4 篇起 */
  .wagi-toc
    .wagi-related-posts.wagi-in-drawer
    .wagi-related-item:nth-child(n + 4) {
    display: none;
  }

  /* 行動版：第 3 項（視覺最後一項）移除底線 */
  .wagi-toc .wagi-related-posts.wagi-in-drawer .wagi-related-item:nth-child(3) {
    border-bottom: none;
  }

  /* 更多文章按鈕：行動版向右移動 1rem，且緊貼上方列表，並略微上移 */
  .wagi-toc .wagi-related-posts.wagi-in-drawer .wagi-related-more {
    margin-left: 0;
    margin-right: 0;
    margin-top: 12px;
  }

  /* 顯示行動版書籤觸發 */
  .wagi-toc-mobile-trigger {
    display: flex;
  }

  /* 行動版開啟時隱藏書籤觸發 */
  body.wagi-toc-mobile-open .wagi-toc-mobile-trigger {
    opacity: 0;
    visibility: hidden;
    transform: translateX(-20px);
  }
}

/* 行動版遮罩（模糊 + 變暗） */
.wagi-toc-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 998;
  opacity: 0;
  transition: opacity var(--wagi-toc-transition);
}

.wagi-toc-overlay.is-visible {
  display: block;
  opacity: 1;
}

/* ========================================
   無障礙與列印
   ======================================== */
@media (prefers-reduced-motion: reduce) {
  .wagi-toc,
  .wagi-toc-content,
  .wagi-toc-link,
  .wagi-toc-mobile-trigger {
    transition: none;
  }
}

@media print {
  .wagi-toc,
  .wagi-toc-mobile-trigger {
    display: none !important;
  }
}
