/* ============================================================
   General Nephrology — site/src/style.css
   純 CSS, 無外部資源。分區: tokens / base / layout / nav / note /
   components / search / print
   ============================================================ */

/* ------------------------------------------------------------
   1) tokens (亮色為預設)
   ------------------------------------------------------------ */
:root {
  color-scheme: light;

  /* 字型 */
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", 微軟正黑體, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", "Courier New", monospace;

  /* 色彩 */
  --bg: #ffffff;            /* 頁面背景 */
  --surface: #f4f6f8;       /* 表面 (hover / 次層底) */
  --text: #1d2d36;          /* 主要文字 (對白 >= 12:1) */
  --muted: #55666f;         /* 次要文字 (對白 ≈ 6:1, AA) */
  --border: #dbe3e9;
  --border-strong: #b7c5ce;
  --primary: #0f766e;       /* 主色 teal-700 */
  --link: #0f766e;          /* 連結 (≈5.5:1, AA) */
  --link-hover: #0a5750;
  --strong: #16423d;        /* <strong> 深青強調 */
  --accent-soft: #e1f2ef;   /* 強調淺底 (chips/is-active) */
  --mark-bg: #d3efe9;
  --mark-text: #0c4a43;
  --code-bg: #f4f6f8;
  --code-text: #12433d;
  --table-head: #eff3f5;
  --zebra: #f7f9fa;         /* 表格斑馬紋 */
  --row-hover: #eef4f3;
  --scroll-thumb: #c7d2d9;
  --shade-bg: rgba(255, 255, 255, 0.85); /* topbar 半透明 */
  --scrim-bg: rgba(10, 24, 30, 0.45);    /* 抽屜遮罩 */
  --overlay-bg: rgba(10, 24, 30, 0.5);   /* 搜尋 modal 背層 */
  --lightbox-bg: rgba(6, 14, 18, 0.9);

  /* 形狀 / 陰影 / 間距 */
  --radius-s: 6px;
  --radius: 10px;
  --radius-l: 14px;
  --shadow-1: 0 1px 2px rgba(18, 38, 48, 0.08), 0 2px 10px rgba(18, 38, 48, 0.06);
  --shadow-2: 0 12px 40px rgba(8, 22, 28, 0.22);

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;

  --ring: #0f766e;
  --topbar-h: 56px;
  --note-w: 760px;
}

/* 暗色 A: 跟隨系統, 除非手動選了亮色 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d151c;
    --surface: #141f28;
    --text: #d9e3e9;
    --muted: #8ea5b0;
    --border: #253341;
    --border-strong: #354a58;
    --primary: #2dd4bf;
    --link: #2dd4bf;
    --link-hover: #7ee8db;
    --strong: #b9e6df;
    --accent-soft: rgba(45, 212, 191, 0.13);
    --mark-bg: rgba(45, 212, 191, 0.16);
    --mark-text: #a9f0e6;
    --code-bg: #161f28;
    --code-text: #8ad3c8;
    --table-head: #16222c;
    --zebra: #111a22;
    --row-hover: #1a2530;
    --scroll-thumb: #2c3a46;
    --shade-bg: rgba(13, 21, 28, 0.85);
    --scrim-bg: rgba(2, 8, 12, 0.6);
    --overlay-bg: rgba(2, 8, 12, 0.6);
    --lightbox-bg: rgba(4, 10, 14, 0.92);
    --shadow-1: 0 1px 3px rgba(0, 0, 0, 0.5);
    --shadow-2: 0 12px 40px rgba(0, 0, 0, 0.55);
    --ring: #2dd4bf;
  }
}

/* 暗色 B: 手動切換 (html[data-theme="dark"]) */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d151c;
  --surface: #141f28;
  --text: #d9e3e9;
  --muted: #8ea5b0;
  --border: #253341;
  --border-strong: #354a58;
  --primary: #2dd4bf;
  --link: #2dd4bf;
  --link-hover: #7ee8db;
  --strong: #b9e6df;
  --accent-soft: rgba(45, 212, 191, 0.13);
  --mark-bg: rgba(45, 212, 191, 0.16);
  --mark-text: #a9f0e6;
  --code-bg: #161f28;
  --code-text: #8ad3c8;
  --table-head: #16222c;
  --zebra: #111a22;
  --row-hover: #1a2530;
  --scroll-thumb: #2c3a46;
  --shade-bg: rgba(13, 21, 28, 0.85);
  --scrim-bg: rgba(2, 8, 12, 0.6);
  --overlay-bg: rgba(2, 8, 12, 0.6);
  --lightbox-bg: rgba(4, 10, 14, 0.92);
  --shadow-1: 0 1px 3px rgba(0, 0, 0, 0.5);
  --shadow-2: 0 12px 40px rgba(0, 0, 0, 0.55);
  --ring: #2dd4bf;
}

/* ------------------------------------------------------------
   2) base
   ------------------------------------------------------------ */
*,
*::before,
*::after { box-sizing: border-box; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  background: var(--bg);
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: 600;
  line-height: 1.35;
  letter-spacing: -0.011em;
  color: var(--text);
}

a {
  color: var(--link);
  text-decoration: none;
}
a:hover { color: var(--link-hover); }

img { max-width: 100%; height: auto; }

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

code, kbd, pre, samp {
  font-family: var(--font-mono);
}

kbd {
  font-size: 0.72em;
  line-height: 1;
  padding: 3px 6px;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: var(--radius-s);
  white-space: nowrap;
}

::placeholder { color: var(--muted); opacity: 1; }

::selection {
  color: var(--text);
  background: rgba(15, 118, 110, 0.22);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) ::selection { background: rgba(45, 212, 191, 0.25); }
}
:root[data-theme="dark"] ::selection { background: rgba(45, 212, 191, 0.25); }

/* 互動元素聚焦外框 */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--ring, var(--link));
  outline-offset: 2px;
  border-radius: 2px;
}
input:focus-visible { outline-offset: 0; border-radius: var(--radius-s); }

/* hidden 一律不可見 */
[hidden] { display: none !important; }

/* 細捲軸 (頁內捲動區) */
.sidebar::-webkit-scrollbar,
.toc::-webkit-scrollbar,
.search-results::-webkit-scrollbar,
pre::-webkit-scrollbar,
.table-wrap::-webkit-scrollbar { width: 10px; height: 10px; }
.sidebar::-webkit-scrollbar-thumb,
.toc::-webkit-scrollbar-thumb,
.search-results::-webkit-scrollbar-thumb,
pre::-webkit-scrollbar-thumb,
.table-wrap::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border: 3px solid transparent;
  background-clip: content-box;
  border-radius: 6px;
}
.sidebar::-webkit-scrollbar-track,
.toc::-webkit-scrollbar-track { background: transparent; }
.sidebar, .toc, .search-results, pre, .table-wrap {
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) transparent;
}

/* ------------------------------------------------------------
   3) layout: topbar 下方三欄 (sidebar | content | toc)
   ------------------------------------------------------------ */
.layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 240px;
  column-gap: 0;
  align-items: start;
}
.layout > * { min-width: 0; }

/* 兩側欄: sticky + 各自捲動 */
.sidebar {
  position: sticky;
  top: calc(var(--topbar-h) + env(safe-area-inset-top));
  height: calc(100vh - var(--topbar-h) - env(safe-area-inset-top));
  height: calc(100dvh - var(--topbar-h) - env(safe-area-inset-top));
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bg);
  border-inline-end: 1px solid var(--border);
  padding-block: var(--space-4) var(--space-6);
}

.toc {
  position: sticky;
  top: calc(var(--topbar-h) + env(safe-area-inset-top));
  height: calc(100vh - var(--topbar-h) - env(safe-area-inset-top));
  height: calc(100dvh - var(--topbar-h) - env(safe-area-inset-top));
  overflow-y: auto;
  overscroll-behavior: contain;
  background: transparent;
  padding: 24px 12px var(--space-7) 20px;
  font-size: 0.84rem;
}

.content {
  padding: 30px clamp(18px, 4vw, 48px) var(--space-7);
  padding-left: max(clamp(18px, 4vw, 48px), env(safe-area-inset-left));
  padding-right: max(clamp(18px, 4vw, 48px), env(safe-area-inset-right));
}

/* 平板 700–1099px: 隱藏 toc, 剩兩欄 */
@media (max-width: 1099px) {
  .toc { display: none; }
  .layout { grid-template-columns: 280px minmax(0, 1fr); }
}

/* ---------- 手機 <700px 在「nav」區 (抽屜) 覆寫 ---------- */

/* ------------------------------------------------------------
   4) nav: topbar / 品牌按鈕 / 側欄導覽樹 / 手機抽屜 / scrim
   ------------------------------------------------------------ */
.topbar {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 10px;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: var(--shade-bg);
  -webkit-backdrop-filter: saturate(1.4) blur(8px);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--border);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: 0;
  border-radius: var(--radius-s);
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  margin-right: auto;
  color: var(--text);
}
.brand:hover { color: var(--text); }
.brand-mark { font-size: 19px; color: var(--primary); line-height: 1; }
.brand-text {
  font-weight: 650;
  font-size: 0.95rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 搜尋觸發按鈕 (topbar 與首頁 .search-hero 共用) */
.search-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  flex: 0 1 auto;
  min-width: 0;
  padding: 0 10px 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.875rem;
  cursor: pointer;
}
.search-trigger:hover { border-color: var(--border-strong); color: var(--text); }
.search-icon { font-size: 15px; line-height: 1; flex: 0 0 auto; }
.search-label {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.topbar .search-trigger kbd { margin-left: 10px; }

/* 首頁中央大搜尋框 */
.search-trigger.search-hero {
  width: min(440px, 100%);
  height: 44px;
  margin: var(--space-5) auto 0;
  justify-content: flex-start;
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 0.9375rem;
}
.search-trigger.search-hero:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-1);
  color: var(--text);
}
.search-trigger.search-hero .search-label { flex: 1 1 auto; text-align: start; }
.search-trigger.search-hero .search-icon { color: var(--primary); }

/* --- 導覽樹 --- */
.nav-tree {
  font-size: 0.875rem;
  padding: var(--space-1) 10px 0;
}
.nav-home { margin: 0 0 6px; }
.nav-home .nav-link { font-weight: 550; color: var(--text); }

.nav-group { margin: 2px 0; }
.nav-group > summary {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: var(--radius-s);
  cursor: pointer;
  list-style: none;
  font-weight: 600;
  font-size: 0.845rem;
  color: var(--text);
  user-select: none;
}
.nav-group > summary::-webkit-details-marker { display: none; }
.nav-group > summary::before {
  content: "▸";
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.85em;
  transition: transform 0.15s ease;
}
.nav-group[open] > summary::before { transform: rotate(90deg); }
.nav-group > summary:hover { background: var(--surface); }
.nav-group > summary > span:first-child { min-width: 0; overflow-wrap: anywhere; }

.count {
  flex: 0 0 auto;
  margin-left: auto;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.nav-list {
  list-style: none;
  margin: 2px 0 8px;
  padding-inline-start: 14px;
  border-inline-start: 1px solid var(--border);
}
.nav-link {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  padding: 4px 8px;
  border-radius: var(--radius-s);
  color: var(--muted);
  line-height: 1.5;
  overflow-wrap: anywhere;
}
.nav-link:hover { background: var(--surface); color: var(--text); }
.nav-link.is-active {
  background: var(--accent-soft);
  color: var(--link);
  font-weight: 600;
}

/* --- scrim (抽屜半透明遮罩) --- */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: var(--scrim-bg);
}

/* --- 手機抽屜 <700px --- */
@media (max-width: 699px) {
  .layout { display: block; }
  .content {
    padding: 18px max(14px, env(safe-area-inset-right)) max(56px, env(safe-area-inset-bottom));
    padding-left: max(14px, env(safe-area-inset-left));
  }

  .sidebar {
    position: fixed;
    inset-inline-start: 0;
    top: calc(var(--topbar-h) + env(safe-area-inset-top));
    bottom: 0;
    height: auto;
    width: min(86vw, 320px);
    z-index: 80;
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--bg);
    border-inline-end: 1px solid var(--border);
    box-shadow: none;
    transform: translateX(-104%);
    visibility: hidden;
    transition: transform 0.28s cubic-bezier(0.2, 0.7, 0.3, 1), visibility 0s linear 0.3s;
  }
  body.nav-open .sidebar {
    transform: translateX(0);
    visibility: visible;
    box-shadow: var(--shadow-2);
    transition: transform 0.28s cubic-bezier(0.2, 0.7, 0.3, 1);
  }
  body.nav-open { overflow: hidden; }
}

@media (max-width: 1099px) {
  .content { padding-top: 24px; }
}

/* 桌機不需要漢堡鈕 */
@media (min-width: 700px) {
  #menu-btn { display: none; }
}

/* 手機上 topbar 搜尋只留圖示 */
@media (max-width: 699px) {
  .topbar .search-trigger .search-label,
  .topbar .search-trigger kbd { display: none; }
  .brand-text { font-size: 0.92rem; }
}

/* ------------------------------------------------------------
   5) note: 文章頁 (breadcrumb / header / 內文排版 / backlinks / pager)
   ------------------------------------------------------------ */
.note {
  max-width: var(--note-w);
  margin-inline: 0;
}

.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px;
  margin: 0 0 14px;
  font-size: 0.82rem;
  color: var(--muted);
}
.breadcrumb a { color: var(--muted); }
.breadcrumb a:hover { color: var(--link); text-decoration: underline; }
.breadcrumb .sep { margin-inline: 6px; color: var(--muted); }
.breadcrumb [aria-current="page"] { color: var(--text); }

.note-header { margin: 0 0 26px; }
.note-title {
  margin: 0 0 10px;
  font-size: clamp(1.55rem, 2.4vw + 1rem, 2.05rem);
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.015em;
}
.note-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
}
.chip {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--link);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.5;
}
.meta-item {
  color: var(--muted);
  font-size: 0.8125rem;
}
a.meta-item { color: var(--link); }
a.meta-item:hover { text-decoration: underline; }

/* --- 內文 --- */
.note-body {
  max-width: var(--note-w);
  font-size: 1.0625rem;
  line-height: 1.78;
  color: var(--text);
  overflow-wrap: break-word;
}
.note-body > :first-child { margin-top: 0; }
.note-body > :last-child { margin-bottom: 0; }

.note-body p { margin: 0 0 1em; }

.note-body :is(h1, h2, h3, h4, h5, h6) {
  scroll-margin-top: calc(var(--topbar-h) + env(safe-area-inset-top) + 14px);
}
.note-body h2 {
  margin: 2em 0 0.7em;
  padding-bottom: 0.32em;
  font-size: 1.34rem;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
}
.note-body :is(h3, h4) {
  margin: 1.6em 0 0.55em;
  font-size: 1.145rem;
  font-weight: 650;
}
.note-body h4 { font-size: 1.02rem; }
.note-body > h2:first-child,
.note-body > :is(h2, h3):first-child { margin-top: 0; }

/* 重點粗体: 深青強調 */
.note-body strong {
  color: var(--strong);
  font-weight: 700;
}
.note-body h2 strong, .note-body h3 strong { color: inherit; }

/* 標題錨點: hover/focus 標題才顯示 */
.note-body a.anchor {
  margin-inline-start: 0.45em;
  color: var(--muted);
  font-weight: 400;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.note-body :is(h2, h3, h4, h5, h6):hover > a.anchor,
.note-body :is(h2, h3, h4, h5, h6):focus-within > a.anchor {
  opacity: 1;
}
.note-body a.anchor:hover { color: var(--link); }

/* 內文連結 */
.note-body a { color: var(--link); }
.note-body a:hover { color: var(--link-hover); }
.note-body :is(p, li, td, th, blockquote, dd, figcaption) > a,
.note-body .callout a {
  text-decoration: underline;
  text-underline-offset: 0.2em;
  text-decoration-thickness: 1px;
  text-decoration-color: var(--border-strong);
}
.note-body :is(p, li, td, th, blockquote, dd, figcaption) > a:hover,
.note-body .callout a:hover { text-decoration-color: currentColor; }

/* wikilink: 主色底線; 未建立 → 灰色虛線 */
.note-body a.wikilink {
  text-decoration: underline;
  text-underline-offset: 0.2em;
  text-decoration-thickness: 1px;
}
.note-body a.wikilink.is-broken {
  color: var(--muted);
  text-decoration-style: dashed;
  text-decoration-color: var(--muted);
}
.note-body a.wikilink.is-broken:hover { color: var(--link); text-decoration-color: currentColor; }

/* --- 巢狀清單: • ◦ ▪ --- */
.note-body ul,
.note-body ol {
  margin: 0.25em 0 1em;
  padding-inline-start: 1.35em;
}
.note-body ul li + li,
.note-body ol li + li { margin-top: 0.32em; }
.note-body li > p { margin: 0; }
.note-body li > :is(ul, ol) { margin: 0.35em 0 0.15em; }
.note-body li > :is(ul, ol):last-child { margin-bottom: 0.15em; }

.note-body ul { list-style: none; }
.note-body ul > li { position: relative; }
.note-body ul > li::before {
  content: "•";
  position: absolute;
  inset-inline-start: -0.8em;
  color: var(--link);
  font-weight: 700;
}
.note-body ul ul > li::before {
  content: "◦";
  color: var(--muted);
  font-weight: 400;
  inset-inline-start: -1em;
}
.note-body ul ul ul > li::before {
  content: "▪";
  color: var(--border-strong);
  font-weight: 400;
  font-size: 0.8em;
  inset-inline-start: -1.05em;
}
.note-body ol > li { padding-inline-start: 0.15em; }
.note-body ol > li::marker { color: var(--link); font-weight: 600; }

/* --- 引用 --- */
.note-body blockquote {
  margin: 1.1em 0 1.25em;
  padding: 0.1em 0 0.1em 1em;
  border-inline-start: 3px solid var(--border-strong);
  color: var(--muted);
}
.note-body blockquote > :first-child { margin-top: 0; }
.note-body blockquote > :last-child { margin-bottom: 0; }
.note-body blockquote p + p { margin-top: 0.6em; }

/* --- callout (note / tip / warning / danger) --- */
.callout {
  margin: 1.1em 0 1.25em;
  padding: 12px 14px;
  border-radius: var(--radius-s);
  border-inline-start: 3px solid var(--co-bar, var(--link));
  background: var(--co-bg, var(--accent-soft));
  font-size: 0.97em;
  line-height: 1.7;
}
.callout > :last-child { margin-bottom: 0; }
.callout > :first-child { margin-top: 0; }
.callout-title {
  margin-bottom: 6px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--co-title, var(--co-bar, var(--link)));
}
.callout > .callout-title:not(:first-child) { margin-top: 8px; }

.callout[data-type="note"] {
  --co-bar: var(--primary);
  --co-bg: var(--accent-soft);
}
.callout[data-type="tip"] {
  --co-bar: #047857;
  --co-bg: #e8f6ef;
  --co-title: #047857;
}
.callout[data-type="warning"] {
  --co-bar: #b45309;
  --co-bg: #fdf5e7;
  --co-title: #b45309;
}
.callout[data-type="danger"] {
  --co-bar: #b91c1c;
  --co-bg: #fdeeee;
  --co-title: #b91c1c;
}
:root[data-theme="dark"] .callout[data-type="note"] {
  --co-bg: rgba(45, 212, 191, 0.1);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .callout[data-type="note"] { --co-bg: rgba(45, 212, 191, 0.1); }
}
:root[data-theme="dark"] .callout[data-type="tip"] {
  --co-bar: #34d399;
  --co-bg: rgba(52, 221, 169, 0.12);
  --co-title: #34d399;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .callout[data-type="tip"] {
    --co-bar: #34d399;
    --co-bg: rgba(52, 221, 169, 0.12);
    --co-title: #34d399;
  }
}
:root[data-theme="dark"] .callout[data-type="warning"] {
  --co-bar: #fbbf24;
  --co-bg: rgba(251, 191, 36, 0.12);
  --co-title: #fbbf24;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .callout[data-type="warning"] {
    --co-bar: #fbbf24;
    --co-bg: rgba(251, 191, 36, 0.12);
    --co-title: #fbbf24;
  }
}
:root[data-theme="dark"] .callout[data-type="danger"] {
  --co-bar: #f87171;
  --co-bg: rgba(248, 113, 113, 0.12);
  --co-title: #f87171;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .callout[data-type="danger"] {
    --co-bar: #f87171;
    --co-bg: rgba(248, 113, 113, 0.12);
    --co-title: #f87171;
  }
}

/* --- callout data-type 別名 fallback ---
   info/quote/abstract → note 色; success/example → tip 色;
   question/todo → warning 色 (其余未知型別走 .callout 預設) */
.callout[data-type="info"],
.callout[data-type="quote"],
.callout[data-type="abstract"] {
  --co-bar: var(--primary);
  --co-bg: var(--accent-soft);
  --co-title: var(--primary);
}
.callout[data-type="success"],
.callout[data-type="example"] {
  --co-bar: #047857;
  --co-bg: #e8f6ef;
  --co-title: #047857;
}
.callout[data-type="question"],
.callout[data-type="todo"] {
  --co-bar: #b45309;
  --co-bg: #fdf5e7;
  --co-title: #b45309;
}
/* 別名的暗色 (success/example、question/todo 需明亮對比) */
:root[data-theme="dark"] .callout[data-type="success"],
:root[data-theme="dark"] .callout[data-type="example"] {
  --co-bar: #34d399;
  --co-bg: rgba(52, 221, 169, 0.12);
  --co-title: #34d399;
}
:root[data-theme="dark"] .callout[data-type="question"],
:root[data-theme="dark"] .callout[data-type="todo"] {
  --co-bar: #fbbf24;
  --co-bg: rgba(251, 191, 36, 0.12);
  --co-title: #fbbf24;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .callout[data-type="success"],
  :root:not([data-theme="light"]) .callout[data-type="example"] {
    --co-bar: #34d399;
    --co-bg: rgba(52, 221, 169, 0.12);
    --co-title: #34d399;
  }
  :root:not([data-theme="light"]) .callout[data-type="question"],
  :root:not([data-theme="light"]) .callout[data-type="todo"] {
    --co-bar: #fbbf24;
    --co-bg: rgba(251, 191, 36, 0.12);
    --co-title: #fbbf24;
  }
}

/* --- 表格: 橫向捲動 + 斑馬紋 --- */
.note-body .table-wrap {
  margin: 1.1em 0 1.4em;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  -webkit-overflow-scrolling: touch;
}
.note-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  line-height: 1.55;
}
.note-body th,
.note-body td {
  padding: 9px 12px;
  text-align: left;
  vertical-align: top;
}
.note-body thead th {
  background: var(--table-head);
  border-bottom: 1px solid var(--border-strong);
  font-weight: 650;
  font-size: 0.85rem;
  white-space: nowrap;
}
.note-body tbody td { border-top: 1px solid var(--border); }
.note-body tbody tr:nth-child(even) { background: var(--zebra); }
.note-body tbody tr:hover { background: var(--row-hover); }
.note-body caption { caption-side: bottom; padding-top: 8px; color: var(--muted); font-size: 0.8rem; }

/* --- 程式碼 --- */
.note-body pre {
  margin: 0.3em 0 1.25em;
  padding: 14px 16px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  font-size: 0.84rem;
  line-height: 1.65;
}
.note-body pre code {
  padding: 0;
  background: none;
  border: 0;
  border-radius: 0;
  font-size: inherit;
  color: var(--text);
}
.note-body code {
  padding: 0.12em 0.38em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--code-text);
  font-size: 0.88em;
  overflow-wrap: anywhere;
}

/* KaTeX (inline 數學) — 縮小 KaTeX 預設 1.21em, 貼近內文 */
.note-body .katex { font-size: 1.06em; }

/* --- 圖片 --- */
.note-body img {
  height: auto;
  margin: 0.4em 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: zoom-in;
}
.lightbox img { cursor: zoom-out; border: 0; }

/* --- 其他 --- */
.note-body hr {
  margin: 1.9em 0;
  border: 0;
  border-top: 1px solid var(--border);
}
.note-body mark {
  padding: 0 0.18em;
  border-radius: 3px;
  background: var(--mark-bg);
  color: var(--mark-text);
}
.note-body .katex-display {
  overflow-x: auto;
  padding: 4px 0;
}

/* --- 反向連結 --- */
.backlinks {
  margin-top: 40px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.backlinks h2 {
  margin: 0 0 10px;
  padding: 0;
  border: 0;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.backlinks ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.backlinks li + li { margin-top: 2px; }
.backlinks a { color: var(--link); }
.backlinks a:hover { text-decoration: underline; }

/* --- 上下篇 (兩欄卡片; 手機疊一欄) --- */
.pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 40px 0 8px;
}
.pager:empty { display: none; }
.pager-prev,
.pager-next {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  text-decoration: none;
  box-shadow: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}
.pager-prev:hover,
.pager-next:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-1);
  transform: translateY(-1px);
}
.pager-label { color: var(--muted); font-size: 0.75rem; }
.pager-title { color: var(--strong); font-weight: 650; font-size: 0.95rem; }
.pager-prev:hover .pager-title, .pager-next:hover .pager-title { color: var(--link-hover); }
.pager-next { text-align: end; align-items: flex-end; }
@media (max-width: 699px) {
  .pager { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------------
   6) components: toc 清單 / skip-link / 進度條 / footer / 首頁 / 燈箱
   ------------------------------------------------------------ */
.toc-title {
  margin-bottom: 10px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--muted);
}
.toc-list ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
.toc-link {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  padding: 3px 9px;
  border-radius: var(--radius-s);
  color: var(--muted);
  line-height: 1.55;
  overflow-wrap: anywhere;
}
.toc-link:hover { background: var(--surface); color: var(--text); }
.toc-link.is-active {
  background: var(--accent-soft);
  color: var(--link);
  font-weight: 600;
}
.toc-h2 {
  font-weight: 600;
  font-size: 0.875rem;
  margin-top: 4px;
}
.toc-h3 { padding-left: 16px; }

/* skip-link (預設隱形, focus 跳出) */
.skip-link {
  position: fixed;
  top: -60px;
  left: 12px;
  z-index: 130;
  padding: 9px 14px;
  border-radius: var(--radius-s);
  background: var(--primary);
  color: #ffffff;
  font-size: 0.85rem;
  font-weight: 600;
  text-decoration: none;
}
.skip-link:focus,
.skip-link:focus-visible { top: 10px; color: #ffffff; }

/* WCAG 對比修法 (dev/reports/contrast.md §三.1): 暗色主題下 skip-link
   白字對 var(--primary) (#2dd4bf) 僅 1.87:1, 改深青黑 #062f2b = 7.78:1;
   新增規則, 亮色主題維持白字 */
:root[data-theme="dark"] .skip-link,
:root[data-theme="dark"] .skip-link:focus,
:root[data-theme="dark"] .skip-link:focus-visible {
  color: #062f2b;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .skip-link,
  :root:not([data-theme="light"]) .skip-link:focus,
  :root:not([data-theme="light"]) .skip-link:focus-visible {
    color: #062f2b;
  }
}

/* 閱讀進度條: JS 設定 body/documentElement 的 --p (0~1) */
.progress {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 120;
  width: calc(var(--p, 0) * 100%);
  height: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--primary);
  pointer-events: none;
}

/* site footer */
.site-footer {
  margin-top: 48px;
  padding: 16px 0 4px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8rem;
}

/* --- 首頁 --- */
.home-hero {
  padding: clamp(28px, 6vw, 64px) 8px 12px;
  text-align: center;
}
.home-hero h1,
.home-hero .home-title {
  margin: 0;
  font-size: clamp(1.9rem, 4.6vw, 2.65rem);
  font-weight: 750;
  letter-spacing: -0.02em;
  line-height: 1.25;
}
.note-body .home-hero .home-sub {
  margin: 10px auto 0;
  max-width: 620px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.7;
}
/* 首頁: 重複的「首頁」標題區 — meta/麵包屑隱藏, h1 視覺隱藏保可存取 */
.note:has(.home-hero) .note-title {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.note:has(.home-hero) .note-header { margin: 0; }
.note:has(.home-hero) .note-meta,
.note:has(.home-hero) .breadcrumb { display: none; }
.home-section { margin-top: 34px; }
.home-section > h2 {
  margin: 0 0 14px;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
  font-size: 1.15rem;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  text-decoration: none;
  box-shadow: none;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
}
.card:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-1);
  transform: translateY(-2px);
}
.card-title {
  color: var(--text);
  font-size: 0.97rem;
  font-weight: 650;
  line-height: 1.4;
}
.card:hover .card-title { color: var(--link); }
.card-desc {
  flex: 1 1 auto;
  color: var(--muted);
  font-size: 0.83rem;
  line-height: 1.55;
}
.card-count {
  align-self: flex-start;
  margin-top: 7px;
  padding: 1px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--link);
  font-size: 0.73rem;
  font-weight: 600;
}

/* --- 燈箱 --- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 110;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 3vh, 28px);
  background: var(--lightbox-bg);
  cursor: zoom-out;
}
.lightbox img {
  max-width: 95vw;
  max-height: 95vh;
  border-radius: var(--radius-s);
  box-shadow: var(--shadow-2);
  background: transparent;
}

/* ------------------------------------------------------------
   7) search: 全螢幕 modal + 面板 + 結果清單
   ------------------------------------------------------------ */
.search-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: clamp(12px, 10vh, 96px) 16px var(--space-5);
  padding-top: max(clamp(12px, 10vh, 96px), env(safe-area-inset-top));
  background: var(--overlay-bg);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.search-panel {
  display: flex;
  flex-direction: column;
  width: min(640px, 100%);
  max-height: 80vh;
  overflow: hidden;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-l);
  box-shadow: var(--shadow-2);
}

.search-input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}
.search-input-row .search-icon { color: var(--muted); font-size: 17px; }
#search-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 42px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}
#search-input:focus { outline: none; }
#search-input:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: -2px;
  border-radius: var(--radius-s);
}
#search-input::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; }

.search-results {
  flex: 1 1 auto;
  margin: 0;
  padding: 8px;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  max-height: min(56vh, 480px);
  min-height: 44px;
}
.search-results:empty { padding: 0; min-height: 0; }

.search-results li > a.result {
  display: block;
  padding: 10px 12px;
  border-radius: var(--radius-s);
  color: inherit;
  text-decoration: none;
}
.search-results li > a.result:hover { background: var(--surface); }
.search-results li.is-selected > a.result,
.search-results li.is-selected > a.result:hover {
  background: var(--accent-soft);
}
.result-title {
  display: block;
  color: var(--link);
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.4;
}
.result-path {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.74rem;
}
.result-snippet {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.6;
}
.result-snippet mark {
  padding: 0 0.15em;
  border-radius: 3px;
  background: var(--mark-bg);
  color: var(--mark-text);
}
li.is-selected .result-title { color: var(--link-hover); }
:root[data-theme="dark"] li.is-selected .result-title { color: var(--link-hover); }

.search-foot {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 9px 14px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.75rem;
}

/* 手機: modal 接近全螢幕、貼頂 */
@media (max-width: 699px) {
  .search-modal {
    padding: max(6px, env(safe-area-inset-top)) 6px max(6px, env(safe-area-inset-bottom));
  }
  .search-panel { max-height: calc(100dvh - 24px); }
  .search-results { max-height: min(62vh, 520px); }
}

/* ------------------------------------------------------------ 
   動效偏好: 關閉 transition (僅針對有宣告 transition 的元素)
   ------------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .sidebar,
  body.nav-open .sidebar,
  .card,
  .pager-prev,
  .pager-next,
  .note-body a.anchor,
  .nav-group > summary::before {
    transition: none;
  }
}

/* ------------------------------------------------------------
   8) print: 只留內文 (黑字白底), 隱藏導覽/互動元件
   ------------------------------------------------------------ */
@media print {
  @page { margin: 14mm; }

  .topbar,
  .sidebar,
  .toc,
  .scrim,
  .search-modal,
  .lightbox,
  .progress,
  .skip-link,
  .pager,
  .search-hero {
    display: none;
  }

  .layout { display: block; }
  .layout > * { min-width: 0; }
  .content { padding: 0; }
  .content .note,
  .note,
  .note-body {
    max-width: none;
  }
  .site-footer { margin-top: 24px; border-color: #cccccc; }

  /* 強制亮色 token (涵蓋 dark 三種情境, 之後宣告優先) */
  :root,
  :root[data-theme="dark"],
  :root:not([data-theme="light"]) {
    color-scheme: light;
    --bg: #ffffff;
    --surface: #ffffff;
    --text: #000000;
    --muted: #333333;
    --border: #bbbbbb;
    --border-strong: #999999;
    --primary: #000000;
    --link: #000000;
    --link-hover: #000000;
    --strong: #000000;
    --accent-soft: #ffffff;
    --mark-bg: #eeeeee;
    --mark-text: #000000;
    --code-bg: #f2f2f2;
    --code-text: #000000;
    --table-head: #f0f0f0;
    --zebra: #fafafa;
    --row-hover: #fafafa;
    --shadow-1: none;
    --shadow-2: none;
    --ring: #000000;
  }

  html, body { background: #ffffff; color: #000000; }

  a { color: #000000; }
  .note-body a { text-decoration: underline; }
  .note-body a.anchor { display: none; }
  .callout,
  .note-body blockquote { break-inside: avoid; page-break-inside: avoid; }
  .note-body pre { border-color: #cccccc; }
  .note-body .table-wrap { border-color: #999999; }
  thead { display: table-header-group; }
  img, .note-body img, .note-body pre { max-width: 100%; page-break-inside: avoid; }
  h2, h3 { page-break-after: avoid; }
}

/* ============================================================
   追加批次: .toast / .search-recent-* / 寬表格 (sticky 首欄 + scroll
   shadow) / pre 細捲軸補強 / 列印 pre 換行
   ============================================================ */

/* ---------- 1) .toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 70; /* 高於 .study-panel/.back-top (60), 低於 .search-modal (100) */
  max-width: min(480px, calc(100vw - 32px));
  padding: 10px 18px;
  border-radius: 999px;
  background: #22303a;
  color: #eef4f7;
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: var(--shadow-2);
  font-size: 0.85rem;
  line-height: 1.5;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: opacity 0.22s ease, transform 0.22s ease, visibility 0.22s;
}
.toast.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
}
/* 暗色主題: 淺底深字 */
:root[data-theme="dark"] .toast {
  background: #e7eef2;
  color: #102229;
  border-color: rgba(255, 255, 255, 0.6);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .toast {
    background: #e7eef2;
    color: #102229;
    border-color: rgba(255, 255, 255, 0.6);
  }
}
/* 手機: 避開 safe-area 並高於 .back-top (44px + 16px 邊距) */
@media (max-width: 699px) {
  .toast { bottom: calc(72px + env(safe-area-inset-bottom)); }
}
@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}

/* ---------- 2) 搜尋「最近搜尋」清單 ---------- */
/* 中和既有 li/a.result 樣式 (以較高專一性, 不動既有規則) */
.search-results li.search-recent-title,
.search-results li:has(> .search-recent-item),
.search-results li:has(> .search-recent-clear) {
  padding: 0;
  background: transparent;
}
.search-results li.search-recent-title {
  margin-top: 2px;
  padding: 6px 14px 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.search-results li:has(> .search-recent-item) { margin-top: 3px; }
.search-results .search-recent-item {
  display: block;
  width: 100%;
  box-sizing: border-box;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.875rem;
  line-height: 1.5;
  text-align: start;
  white-space: normal;
  overflow-wrap: anywhere;
  cursor: pointer;
}
.search-results .search-recent-item:hover {
  background: var(--surface);
  border-color: var(--border-strong);
}
.search-results .search-recent-item:focus-visible {
  outline: 2px solid var(--ring, var(--link));
  outline-offset: 2px;
}
.search-results li:has(> .search-recent-clear) { text-align: end; }
.search-results .search-recent-clear {
  display: inline-block;
  margin-top: 2px;
  padding: 4px 10px;
  border: 0;
  border-radius: var(--radius-s);
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
}
.search-results .search-recent-clear:hover {
  color: var(--link);
  background: var(--surface);
}
.search-results .search-recent-clear:focus-visible {
  outline: 2px solid var(--ring, var(--link));
  outline-offset: 2px;
}

/* ---------- 3) 寬表格: scroll-shadow 提示 + 手機 sticky 首欄 ---------- */
/* 斑馬紋改半透明 (加成覆寫, 視覺與原 #f7f9fa/#111a22 幾乎相同),
   讓 background-attachment local/scroll 的捲動陰影不被不透明列蓋住 */
.note-body .table-wrap tbody tr:nth-child(even) {
  background: rgba(18, 38, 48, 0.035);
}
:root[data-theme="dark"] .note-body .table-wrap tbody tr:nth-child(even) {
  background: rgba(240, 248, 250, 0.05);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .note-body .table-wrap tbody tr:nth-child(even) {
    background: rgba(240, 248, 250, 0.05);
  }
}
.note-body .table-wrap {
  --sc-color: rgba(18, 38, 48, 0.18);
  background:
    /* local: 蓋住該側陰影 (未捲到底時貼邊, 捲動時隨內容滑走) */
    linear-gradient(to right, var(--bg) 40%, rgba(0, 0, 0, 0)) 0 0 / 2.4em 100% local,
    linear-gradient(to left, var(--bg) 40%, rgba(0, 0, 0, 0)) 100% 0 / 2.4em 100% local,
    /* scroll: 固定在視窗邊緣的陰影, 只在內容超出時可見 */
    radial-gradient(farthest-side at 0 50%, var(--sc-color), rgba(0, 0, 0, 0)) 0 0 / 12px 100% scroll,
    radial-gradient(farthest-side at 100% 50%, var(--sc-color), rgba(0, 0, 0, 0)) 100% 0 / 12px 100% scroll;
  background-repeat: no-repeat;
}
:root[data-theme="dark"] .note-body .table-wrap {
  --sc-color: rgba(240, 248, 250, 0.16);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .note-body .table-wrap {
    --sc-color: rgba(240, 248, 250, 0.16);
  }
}
/* 手機: th 不換行 (全域已有 nowrap)、td 可換行、首欄 sticky */
@media (max-width: 699px) {
  .note-body table td { min-width: 8em; }
  .note-body table th:first-child,
  .note-body table td:first-child {
    position: sticky;
    left: 0;
    z-index: 1;
    box-shadow: inset -1px 0 0 var(--border-strong);
  }
  .note-body table th:first-child { background: var(--table-head); }
  .note-body table td:first-child { background: var(--bg); }
  .note-body table tbody tr:nth-child(even) td:first-child { background: var(--zebra); }
}

/* ---------- 4) pre: 細捲軸補強 (webkit 部分既有) + 列印換行 ---------- */
.note-body pre {
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) transparent;
}
@media print {
  pre { white-space: pre-wrap; }
  .toast { display: none; }
}
