/* ==========================================================
   《從時空到控制》 書籍網站樣式
   ========================================================== */

:root {
  --bg: #fdfcf9;
  --fg: #2b2b2b;
  --fg-soft: #5a5a5a;
  --accent: #1a5fb4;
  --accent-soft: #e8f0fb;
  --border: #e2ddd3;
  --math-bg: #f4f6fa;
  --math-border: #c3d2e8;
  --concept-bg: #f4faf4;
  --concept-border: #bfdcbf;
  --warn-bg: #fdf6e3;
  --warn-border: #e6d9a8;
  --code-bg: #f0efe9;
  max-width: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1f22;
    --fg: #e4e2dd;
    --fg-soft: #a8a5a0;
    --accent: #7fb0f0;
    --accent-soft: #263242;
    --border: #3a3b3f;
    --math-bg: #24282f;
    --math-border: #3d4a5e;
    --concept-bg: #232a23;
    --concept-border: #3c4f3c;
    --warn-bg: #2c2820;
    --warn-border: #55492e;
    --code-bg: #2a2b2e;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: "Noto Serif TC", "Songti TC", "PMingLiU", serif;
  font-size: 17px;
  line-height: 1.9;
}

/* ---------- 版面 ---------- */
.wrap {
  max-width: 46rem;
  margin: 0 auto;
  padding: 2rem 1.25rem 5rem;
}

header.site {
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.25rem;
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  display: flex;
  align-items: baseline;
  gap: 1rem;
  flex-wrap: wrap;
}
header.site .book-title {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--fg);
  text-decoration: none;
}

/* ---------- 標題 ---------- */
h1, h2, h3 {
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  line-height: 1.4;
}
h1 { font-size: 1.9rem; margin: 2.5rem 0 .5rem; }
h1 .part-label {
  display: block;
  font-size: .95rem;
  color: var(--accent);
  letter-spacing: .15em;
  margin-bottom: .4rem;
}
h2 {
  font-size: 1.4rem;
  margin-top: 3rem;
  padding-bottom: .35rem;
  border-bottom: 2px solid var(--border);
}
h3 { font-size: 1.15rem; margin-top: 2.2rem; }

p { margin: 1rem 0; }
.subtitle { color: var(--fg-soft); font-size: 1.05rem; margin-top: 0; }

a { color: var(--accent); }

em.term { font-style: normal; font-weight: 700; }
.en { color: var(--fg-soft); font-size: .88em; }

/* ---------- 概念框 / 數學框 ---------- */
.box {
  border: 1px solid;
  border-radius: 8px;
  padding: 1rem 1.25rem;
  margin: 1.5rem 0;
}
.box > .box-label {
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  font-weight: 700;
  font-size: .85rem;
  letter-spacing: .1em;
  display: block;
  margin-bottom: .5rem;
}
.box.concept { background: var(--concept-bg); border-color: var(--concept-border); }
.box.concept > .box-label { color: #2e7d32; }
.box.warn { background: var(--warn-bg); border-color: var(--warn-border); }
.box.warn > .box-label { color: #9a7b1a; }

details.math {
  background: var(--math-bg);
  border: 1px solid var(--math-border);
  border-radius: 8px;
  padding: .75rem 1.25rem;
  margin: 1.5rem 0;
}
details.math > summary {
  cursor: pointer;
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  font-weight: 700;
  font-size: .95rem;
  color: var(--accent);
  user-select: none;
}
details.math[open] > summary { margin-bottom: .5rem; }

/* ---------- 數學式 ---------- */
mjx-container[display="true"] {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  min-width: 0 !important;  /* 蓋掉 MathJax 對帶編號公式寫死的 min-width，否則長公式會撐寬整頁 */
  padding: .25rem 0;
}

/* ---------- 表格 ---------- */
.table-scroll { overflow-x: auto; }
table {
  border-collapse: collapse;
  margin: 1.5rem 0;
  font-size: .95rem;
  min-width: 60%;
}
th, td {
  border: 1px solid var(--border);
  padding: .5rem .9rem;
  text-align: left;
}
th {
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  background: var(--accent-soft);
}

/* ---------- 圖 ---------- */
figure {
  margin: 2rem 0;
  text-align: center;
}
figure svg { max-width: 100%; height: auto; }
figure video, figure img { max-width: 100%; height: auto; display: block; margin: 0 auto; }
figure img.mirror { transform: scaleX(-1); }
.modes-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0.5rem 1rem; margin: 2rem 0; }
.modes-grid figure { margin: 0; }
figcaption {
  font-size: .88rem;
  color: var(--fg-soft);
  margin-top: .5rem;
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
}

/* ---------- 左側欄目錄 ---------- */
.layout { display: flex; align-items: flex-start; }
.sidebar {
  position: sticky;
  top: 0;
  flex: 0 0 250px;
  max-height: 100vh;
  overflow-y: auto;
  padding: 1.5rem 1.25rem 2rem;
  border-right: 1px solid var(--border);
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  font-size: .88rem;
  line-height: 1.7;
}
.sidebar ol { list-style: none; margin: 0; padding: 0; }
.sidebar ol.sb-parts > li { margin: .55rem 0; color: var(--fg-soft); }
.sidebar ol.sb-parts > li.pending { opacity: .55; }
.sidebar a { color: var(--fg); text-decoration: none; }
.sidebar a:hover { color: var(--accent); }
.sidebar ol.sb-secs {
  margin: .3rem 0 .5rem;
  padding-left: 1rem;
  border-left: 2px solid var(--border);
}
.sidebar ol.sb-secs a { color: var(--fg-soft); }
.sidebar ol.sb-secs a:hover { color: var(--accent); }
.layout > .wrap { flex: 1; min-width: 0; }
/* 手機：頁首固定在上方，按選單鈕從左側滑出側欄 */
.menu-btn, .sb-close, .menu-backdrop { display: none; }
@media (max-width: 900px) {
  header.site {
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--bg);
    align-items: center;
    padding: .6rem 1rem;
  }
  .menu-btn {
    display: inline-flex;
    align-items: center;
    font: inherit;
    font-size: .9rem;
    color: var(--fg);
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: .45rem .6rem;
    cursor: pointer;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 110;
    width: min(82vw, 300px);
    max-height: none;
    background: var(--bg);
    overscroll-behavior: contain;
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform .25s ease, visibility 0s linear .25s;
  }
  body.menu-open .sidebar { transform: none; visibility: visible; transition: transform .25s ease; }
  .sb-close {
    display: block;
    margin: -.4rem -.25rem 0 auto;
    font-size: 1.6rem;
    line-height: 1;
    color: var(--fg-soft);
    background: none;
    border: 0;
    padding: 0 .25rem;
    cursor: pointer;
  }
  .menu-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 100;
    background: rgba(0, 0, 0, .4);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s;
  }
  body.menu-open .menu-backdrop { opacity: 1; pointer-events: auto; }
  body.menu-open { overflow: hidden; }
  h1[id], h2[id], h3[id] { scroll-margin-top: 4.5rem; }
}

/* ---------- 首頁目錄 ---------- */
ol.toc {
  list-style: none;
  padding: 0;
  counter-reset: part;
}
ol.toc > li {
  border: 1px solid var(--border);
  border-radius: 10px;
  margin: 1rem 0;
  padding: 1.1rem 1.4rem;
  counter-increment: part;
}
ol.toc > li::before {
  content: "第 " counter(part, cjk-ideographic) " 部";
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  font-size: .82rem;
  color: var(--accent);
  letter-spacing: .12em;
  display: block;
}
ol.toc .part-title {
  font-family: "Noto Sans TC", "PingFang TC", sans-serif;
  font-size: 1.2rem;
  font-weight: 700;
  margin: .15rem 0 .3rem;
}
ol.toc .part-title a { text-decoration: none; }
ol.toc .part-desc { color: var(--fg-soft); font-size: .95rem; margin: 0; }
ol.toc li.pending { opacity: .55; }
.status-tag {
  font-family: "Noto Sans TC", sans-serif;
  font-size: .75rem;
  border: 1px solid var(--border);
  border-radius: 99px;
  padding: .1rem .6rem;
  color: var(--fg-soft);
  margin-left: .5rem;
  vertical-align: middle;
}
.status-tag.ready { color: #2e7d32; border-color: var(--concept-border); }

/* ---------- 章末導覽 ---------- */
nav.chapter-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 4rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
  font-family: "Noto Sans TC", sans-serif;
  font-size: .95rem;
}

footer.site {
  text-align: center;
  color: var(--fg-soft);
  font-size: .85rem;
  padding: 2rem 0 3rem;
  font-family: "Noto Sans TC", sans-serif;
}

/* ---------- 習題 ---------- */
ol.exercises > li { margin: 1rem 0; }

/* ---------- SVG 圖內的 MathJax 標籤 ---------- */
figure svg foreignObject { overflow: visible; }
.fo-math {
  line-height: 1;
  white-space: nowrap;
  font-family: "Noto Serif TC", "Songti TC", serif;
}
.fo-math mjx-container { margin: 0 !important; }
