/* ==========================================================================
   1. 變數定義與三種主題配色系統 (亮色 / 暗色 / 純黑高對比)
   ========================================================================== */
:root,
[data-theme="light"] {
  --page-bg: #e8eaed;
  --container-bg: #ffffff;
  --container-shadow: 0 12px 36px rgba(0, 0, 0, 0.08);
  --text-primary: #1f2328;
  --text-secondary: #59636e;
  --text-muted: #768390;
  --border-color: #1f2328;
  --divider-subtle: #d0d7de;
  --hover-bg: rgba(31, 35, 40, 0.05);
  --active-bg: rgba(31, 35, 40, 0.1);
  --ripple-color: rgba(31, 35, 40, 0.16);
  --code-bg: #f6f8fa;
  --blockquote-bg: #f6f8fa;
  --accent-color: #0969da;
  --zoom-overlay: rgba(255, 255, 255, 0.94);
  --scrollbar-thumb: rgba(31, 35, 40, 0.28);
  --scrollbar-thumb-hover: rgba(31, 35, 40, 0.52);
  --scrollbar-thumb-active: rgba(31, 35, 40, 0.78);
}

[data-theme="dark"] {
  --page-bg: #121417;
  --container-bg: #1c2026;
  --container-shadow: 0 14px 40px rgba(0, 0, 0, 0.45);
  --text-primary: #e6edf3;
  --text-secondary: #9198a1;
  --text-muted: #7d8590;
  --border-color: #8b949e;
  --divider-subtle: #30363d;
  --hover-bg: rgba(230, 237, 243, 0.07);
  --active-bg: rgba(230, 237, 243, 0.14);
  --ripple-color: rgba(230, 237, 243, 0.22);
  --code-bg: #262c34;
  --blockquote-bg: #22272e;
  --accent-color: #58a6ff;
  --zoom-overlay: rgba(28, 32, 38, 0.94);
  --scrollbar-thumb: rgba(230, 237, 243, 0.26);
  --scrollbar-thumb-hover: rgba(230, 237, 243, 0.48);
  --scrollbar-thumb-active: rgba(230, 237, 243, 0.72);
}

[data-theme="oled"] {
  --page-bg: #000000;
  --container-bg: #000000;
  --container-shadow: 0 0 0 1px #333333; /* 純黑模式下提供微弱輪廓區隔視窗 */
  --text-primary: #ffffff;
  --text-secondary: #d4d4d4;
  --text-muted: #a3a3a3;
  --border-color: #ffffff;
  --divider-subtle: #404040;
  --hover-bg: rgba(255, 255, 255, 0.12);
  --active-bg: rgba(255, 255, 255, 0.22);
  --ripple-color: rgba(255, 255, 255, 0.32);
  --code-bg: #141414;
  --blockquote-bg: #0f0f0f;
  --accent-color: #79c0ff;
  --zoom-overlay: rgba(0, 0, 0, 0.96);
  --scrollbar-thumb: rgba(255, 255, 255, 0.42);
  --scrollbar-thumb-hover: rgba(255, 255, 255, 0.75);
  --scrollbar-thumb-active: #ffffff;
}

/* ==========================================================================
   2. 基礎重置與全域排版
   ========================================================================== */
*,
*::before,
*::after {
  box-sizing: border-box;
  border-radius: 0; /* 全站無圓角設計規範 */
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
  overflow-x: hidden;
  overflow-y: auto; /* 僅在實際內容超出視窗高度時才允許垂直捲動 */
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  min-height: 100dvh; /* 適配現代行動瀏覽器動態視窗高度，避免網址列導致溢出 */
  display: flex;
  flex-direction: column; /* 建立 Flex 上下文，徹底防止子元素 margin-top/bottom 穿透撐高 body */
  background-color: var(--page-bg);
  color: var(--text-primary);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-weight: 300;
  font-size: 16px;
  line-height: 1.7;
  transition: background-color 0.25s ease, color 0.25s ease;
  -webkit-font-smoothing: antialiased;
}

/* 保持比例鋪滿視窗的背景圖片 */
.bg-layer {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.bg-layer.is-loaded {
  opacity: 1;
  visibility: visible;
}

/* 未載入完成或清空來源時隱藏 img，防止瀏覽器閃現原生破損圖片圖示 */
.bg-layer:not(.is-loaded) img {
  display: none;
}

.bg-layer img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  display: block;
}

/* ==========================================================================
   2.5 全站與內部容器自訂細矩形捲動條（隱藏預設槽位，跨瀏覽器相容）
   ========================================================================== */
html,
body,
.markdown-body pre,
.table-wrapper {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) transparent;
}

::-webkit-scrollbar {
  width: 5px;
  height: 5px;
  background: transparent;
}

::-webkit-scrollbar-track,
::-webkit-scrollbar-track-piece,
::-webkit-scrollbar-button,
::-webkit-scrollbar-corner {
  background: transparent;
  border: none;
  display: none;
}

::-webkit-scrollbar-thumb {
  background-color: var(--scrollbar-thumb);
  border-radius: 0;
  border: none;
}

::-webkit-scrollbar-thumb:hover {
  background-color: var(--scrollbar-thumb-hover);
}

::-webkit-scrollbar-thumb:active {
  background-color: var(--scrollbar-thumb-active);
}

/* ==========================================================================
   3. 主容器 (.container) 佈局與響應式斷點
   ========================================================================== */
.container {
  box-sizing: border-box;
  width: 100%;
  max-width: 640px;
  margin: 48px auto;
  padding: 28px 24px;
  background-color: var(--container-bg);
  box-shadow: var(--container-shadow);
  border: none;
  transition: background-color 0.25s ease, box-shadow 0.25s ease;
}

@media (max-width: 640px) {
  .container {
    margin: 0 auto;
    flex: 1;
    min-height: auto;
    box-shadow: none;
    padding: 28px 24px;
  }
}

/* ==========================================================================
   4. 頁頭：部落格標題（主題切換器）與描述
   ========================================================================== */
.site-header {
  text-align: center;
  margin-bottom: 18px;
}

.brand-switcher {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 6px 4px;
  cursor: pointer;
  user-select: none;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.brand-switcher:hover {
  background-color: var(--hover-bg);
}

.brand-switcher:active {
  background-color: var(--active-bg);
  transform: scale(0.995);
}

.brand-switcher:focus-visible {
  outline: 2px solid var(--border-color);
  outline-offset: 2px;
}

/* 部落格 Logo 圖片（SVG / WebP）：寬度不超容器，高度自適應，最大高度不超過標題文字高度 (2.6rem) 的 2 倍 */
.blog-logo {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 5.2rem; /* 2rem * 1.3 (標題單行高) * 2 = 5.2rem */
  object-fit: contain;
  margin: 0 auto;
}

.blog-logo[hidden] {
  display: none;
}

.blog-title {
  margin: 0;
  width: 100%;
  font-size: 2rem; /* 32px，單行文字高度為 2.6rem */
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: 0.04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

.blog-title[hidden] {
  display: none;
}

.blog-description {
  margin: 6px 0 0 0;
  font-size: 1rem;
  font-weight: 300;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}

/* ==========================================================================
   5. 社群媒體列與漣漪動畫基礎樣式
   ========================================================================== */
.social-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: nowrap;
  height: 36px;
  line-height: 36px;
  margin-bottom: 20px;
  padding: 0 2px;
}

.social-label {
  font-size: 0.95rem;
  color: var(--text-secondary);
  white-space: nowrap;
  flex-shrink: 0;
}

.social-links {
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  flex: 1;
  max-width: 240px;
  margin-left: auto;
}

.social-btn {
  position: relative;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  background-color: transparent;
  border: none;
  color: var(--text-primary);
  text-decoration: none;
  font-size: 1.25rem;
  cursor: pointer;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.social-btn:hover {
  background-color: var(--hover-bg);
}

.social-btn:active {
  background-color: var(--active-bg);
}

/* 圖示庫載入失敗時的降級文字 */
.icon-fallback {
  display: none;
  font-size: 0.75rem;
  font-weight: 500;
}

.icons-failed .social-btn i,
.icons-failed .meta-icon {
  display: none;
}

.icons-failed .icon-fallback {
  display: inline;
}

/* 游標中心擴散漣漪動畫 */
.ripple-target {
  position: relative;
  overflow: hidden;
}

.ripple-wave {
  position: absolute;
  border-radius: 50%; /* 漣漪波紋本身保持圓形擴散，受容器 overflow:hidden 裁切不超出邊界 */
  background-color: var(--ripple-color);
  transform: scale(0);
  opacity: 0.75;
  animation: ripple-expand 0.75s cubic-bezier(0.2, 0, 0, 1) forwards;
  pointer-events: none;
}

@keyframes ripple-expand {
  0% {
    transform: scale(0);
    opacity: 0.75;
  }
  55% {
    transform: scale(2.2);
    opacity: 0.55;
  }
  100% {
    transform: scale(2.6);
    opacity: 0;
  }
}

/* ==========================================================================
   6. 文章列表與文章卡片
   ========================================================================== */
.article-list {
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  list-style: none;
}

.article-card {
  display: block;
  width: 100%;
  padding: 14px 10px;
  margin: 0;
  background-color: transparent;
  border: none;
  border-top: 2px solid var(--border-color);
  color: var(--text-primary);
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.article-card:last-child {
  border-bottom: 2px solid var(--border-color);
}

.article-card:hover {
  background-color: var(--hover-bg);
}

.article-card:active {
  background-color: var(--active-bg);
}

.card-title {
  margin: 0 0 6px 0;
  font-size: 1.125rem;
  font-weight: 700;
  line-height: 1.4;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.meta-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 48%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta-item i {
  font-size: 0.95rem;
  flex-shrink: 0;
}

/* ==========================================================================
   7. 文章閱讀視圖與導覽按鈕
   ========================================================================== */
.article-view {
  border-top: 2px solid var(--border-color);
  border-bottom: 2px solid var(--border-color);
  padding: 20px 0;
}

.article-header {
  padding-bottom: 14px;
}

.article-main-title {
  margin: 0 0 10px 0;
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.35;
  overflow-wrap: break-word;
}

.article-nav-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 12px 0 22px 0;
  padding: 10px 0;
  border-top: 1px solid var(--divider-subtle);
  border-bottom: 1px solid var(--divider-subtle);
}

.nav-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 34px;
  padding: 0 8px;
  background-color: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  cursor: pointer;
  transition: background-color 0.2s ease, opacity 0.2s ease;
}

.nav-btn:hover:not(:disabled) {
  background-color: var(--hover-bg);
}

.nav-btn:active:not(:disabled) {
  background-color: var(--active-bg);
}

.nav-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* ==========================================================================
   8. Markdown 與 LaTeX 文章內文排版
   ========================================================================== */
.markdown-body {
  font-size: 1rem;
  font-weight: 300;
  line-height: 1.8;
  color: var(--text-primary);
  /* 除程式碼區塊外，內文皆可自動換行 */
  overflow-wrap: break-word;
}

/* 標題層級：避免文章內出現多個 h1 影響 SEO，章節主樣式下移至 h2，h3 及 h4-h6 依序遞減 */
.markdown-body h1,
.markdown-body h2 {
  font-size: 1.45rem;
  font-weight: 700;
  margin: 1.6em 0 0.7em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--divider-subtle);
  line-height: 1.35;
}

.markdown-body h3 {
  font-size: 1.25rem;
  font-weight: 700;
  margin: 1.5em 0 0.6em;
  line-height: 1.4;
}

.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  font-size: 1.1rem;
  font-weight: 500;
  margin: 1.3em 0 0.5em;
  line-height: 1.45;
}

.markdown-body p {
  margin: 0 0 1.1em 0;
}

.markdown-body a {
  color: var(--accent-color);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.markdown-body a:hover {
  background-color: var(--hover-bg);
}

.markdown-body ul,
.markdown-body ol {
  margin: 0 0 1.1em 0;
  padding-left: 1.5em;
}

.markdown-body blockquote {
  margin: 1.2em 0;
  padding: 10px 16px;
  border-left: 3px solid var(--border-color);
  background-color: var(--blockquote-bg);
  color: var(--text-secondary);
}

.markdown-body blockquote p:last-child {
  margin-bottom: 0;
}

/* 程式碼區塊與行內程式碼（不強制斷字換行，超出可水平捲動） */
.markdown-body code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.875em;
  padding: 0.15em 0.35em;
  background-color: var(--code-bg);
  overflow-wrap: normal;
}

.markdown-body pre {
  margin: 1.2em 0;
  padding: 14px;
  background-color: var(--code-bg);
  overflow-x: auto;
  line-height: 1.5;
  overflow-wrap: normal;
}

.markdown-body pre code {
  padding: 0;
  background-color: transparent;
  white-space: pre;
}

/* 圖片樣式：自適應容器寬度，無懸停效果（僅保留游標切換），點擊可觸發燈箱 */
.markdown-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.2em auto;
  cursor: zoom-in;
}

/* 表格樣式：未超寬時依內容自適應居中，超寬時儲存格內自動換行且絕不超過容器 */
.table-wrapper {
  width: 100%;
  max-width: 100%;
  margin: 1.2em 0;
  overflow-x: hidden;
}

.markdown-body table {
  width: auto;
  max-width: 100%;
  margin-left: auto;
  margin-right: auto;
  border-collapse: collapse;
  font-size: 0.925rem;
  table-layout: auto;
}

.markdown-body th,
.markdown-body td {
  padding: 8px 12px;
  border: 1px solid var(--divider-subtle);
  text-align: left;
  white-space: normal;
  overflow-wrap: anywhere;
}

.markdown-body th {
  font-weight: 500;
  background-color: var(--code-bg);
}

.markdown-body td code,
.markdown-body th code {
  white-space: normal;
  overflow-wrap: anywhere;
}

/* KaTeX 公式超出寬度時允許水平捲動，避免撐破版面 */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

/* ==========================================================================
   9. 頁尾與狀態提示
   ========================================================================== */
.status-message {
  padding: 36px 12px;
  text-align: center;
  color: var(--text-secondary);
  border-top: 2px solid var(--border-color);
  border-bottom: 2px solid var(--border-color);
}

.site-footer {
  margin-top: 24px;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.copyright {
  margin: 0 0 6px 0;
}

.footer-links {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.footer-link {
  color: var(--text-secondary);
  text-decoration: none;
  padding: 2px 4px;
  transition: color 0.2s ease, background-color 0.2s ease;
}

.footer-link:hover {
  color: var(--text-primary);
  background-color: var(--hover-bg);
  text-decoration: underline;
}

.footer-link:active {
  background-color: var(--active-bg);
}

/* ==========================================================================
   10. 降級簡易燈箱（當 medium-zoom 外部 CDN 無法載入時自動啟用）
   ========================================================================== */
.medium-zoom-overlay {
  background-color: var(--zoom-overlay) !important;
  z-index: 9998;
}

.medium-zoom-image--opened {
  z-index: 9999;
}

   .fallback-lightbox {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background-color: var(--zoom-overlay);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  cursor: zoom-out;
}

.fallback-lightbox img {
  max-width: 95vw;
  max-height: 95vh;
  object-fit: contain;
}