/* ===== Tokens ===== */
:root {
  --bg: #f7f6f2;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --border: #e2dfd6;
  --text: #1d1c1a;
  --text-2: #55524b;
  --text-3: #8a867c;
  --accent: #3b4cca;
  --accent-soft: #e6e9fb;
  --tip: #1f7a55;
  --tip-soft: #e3f3ea;
  --warn: #a5560b;
  --warn-soft: #fbeedd;
  --interview: #7a3fb8;
  --interview-soft: #f1e8fb;
  --appier: #c2335b;
  --appier-soft: #fbe6ec;
  --code-bg: #1e1f24;
  --code-inline: #eceae3;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 10px;
  --sidebar-w: 280px;
  --font: "Inter", "Noto Sans TC", system-ui, -apple-system, "PingFang TC", "Microsoft JhengHei", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131417; --surface: #1b1c20; --surface-2: #23252a; --border: #2f3137;
    --text: #e9e7e2; --text-2: #b2afa7; --text-3: #7d7a73;
    --accent: #8f9bff; --accent-soft: #23284a;
    --tip: #5fd19e; --tip-soft: #15281f; --warn: #f0a75a; --warn-soft: #2d2114;
    --interview: #c49bf5; --interview-soft: #261b33; --appier: #f37a9b; --appier-soft: #321821;
    --code-bg: #0f1013; --code-inline: #2a2c32;
    --shadow: 0 1px 2px rgba(0,0,0,.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #131417; --surface: #1b1c20; --surface-2: #23252a; --border: #2f3137;
  --text: #e9e7e2; --text-2: #b2afa7; --text-3: #7d7a73;
  --accent: #8f9bff; --accent-soft: #23284a;
  --tip: #5fd19e; --tip-soft: #15281f; --warn: #f0a75a; --warn-soft: #2d2114;
  --interview: #c49bf5; --interview-soft: #261b33; --appier: #f37a9b; --appier-soft: #321821;
  --code-bg: #0f1013; --code-inline: #2a2c32;
  --shadow: 0 1px 2px rgba(0,0,0,.3);
  color-scheme: dark;
}

/* ===== Base ===== */
* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 72px; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 16px; line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img, svg { max-width: 100%; }
hr { border: 0; border-top: 1px solid var(--border); margin: 2.5rem 0; }

/* ===== Layout ===== */
.layout { display: flex; min-height: 100vh; }
#sidebar {
  position: sticky; top: 0; height: 100vh; width: var(--sidebar-w); flex-shrink: 0;
  overflow-y: auto; background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 14px 40px; font-size: 14px;
}
.content { flex: 1; min-width: 0; padding: 40px 48px 120px; max-width: 980px; margin: 0 auto; }
.topbar { display: none; }

/* sidebar */
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; color: var(--text); font-weight: 700; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 13px; font-weight: 800; }
.brand small { display: block; font-weight: 500; color: var(--text-3); font-size: 12px; line-height: 1.2; }
.search-box { position: relative; margin: 0 4px 14px; }
.search-box input {
  width: 100%; padding: 8px 10px 8px 32px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: inherit; font-size: 14px;
}
.search-box input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.search-box svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 14px; height: 14px; color: var(--text-3); }
.progress-mini { margin: 0 8px 16px; font-size: 12px; color: var(--text-3); }
.progress-bar { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.progress-bar > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .3s; }
.nav-group { margin-top: 14px; }
.nav-group-title { padding: 0 8px 4px; font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--text-3); }
.nav-link {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px;
  color: var(--text-2); line-height: 1.4;
}
.nav-link:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-num { font-family: var(--mono); font-size: 11px; color: var(--text-3); width: 20px; flex-shrink: 0; }
.nav-link.active .nav-num { color: var(--accent); }
.nav-done { margin-left: auto; color: var(--tip); font-size: 12px; }
.sidebar-foot { margin: 24px 8px 0; display: flex; gap: 8px; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-2);
  border-radius: 8px; padding: 6px 10px; font: inherit; font-size: 13px; cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--text-3); }

/* search results */
.search-results {
  display: none; position: absolute; left: 0; right: 0; top: calc(100% + 6px); z-index: 50;
  max-height: 70vh; overflow-y: auto; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 10px 40px rgba(0,0,0,.18); padding: 6px;
}
.search-results.open { display: block; }
.search-hit { display: block; padding: 8px 10px; border-radius: 6px; color: var(--text); }
.search-hit:hover, .search-hit.sel { background: var(--surface-2); text-decoration: none; }
.search-hit b { display: block; font-size: 13px; }
.search-hit small { display: block; font-size: 12px; color: var(--text-3); line-height: 1.5; }
.search-hit mark { background: var(--accent-soft); color: var(--accent); border-radius: 2px; }
.search-empty { padding: 10px; color: var(--text-3); font-size: 13px; }

/* ===== Chapter header ===== */
.chapter-head { margin-bottom: 28px; }
.eyebrow { margin: 0 0 6px; font-family: var(--mono); font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); }
h1 { font-size: 2.1rem; line-height: 1.3; margin: 0 0 12px; letter-spacing: -.01em; }
.lede { font-size: 1.08rem; color: var(--text-2); margin: 0 0 16px; }
.meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 13px; color: var(--text-3); }
.done-btn { margin-left: auto; }
.done-btn.is-done { background: var(--tip-soft); color: var(--tip); border-color: var(--tip); }

/* TOC */
.toc {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 18px; margin: 0 0 36px; font-size: 14px;
}
.toc:empty { display: none; }
.toc-title { font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-3); margin-bottom: 6px; }
.toc ol { margin: 0; padding-left: 20px; columns: 2; column-gap: 28px; }
.toc li { break-inside: avoid; line-height: 1.6; padding: 1px 0; }
.toc a { color: var(--text-2); }
.toc a:hover { color: var(--accent); }

/* ===== Typography in article ===== */
article h2 {
  font-size: 1.55rem; line-height: 1.35; margin: 3.2rem 0 1rem; padding-top: 1.2rem;
  border-top: 1px solid var(--border); letter-spacing: -.005em;
}
article section:first-child > h2 { border-top: 0; margin-top: 0; padding-top: 0; }
article h3 { font-size: 1.2rem; margin: 2.2rem 0 .6rem; }
article h4 { font-size: 1.02rem; margin: 1.6rem 0 .4rem; color: var(--text-2); }
article p { margin: .7rem 0; }
article ul, article ol { padding-left: 1.4rem; margin: .6rem 0; }
article li { margin: .25rem 0; }
article li > ul, article li > ol { margin: .2rem 0; }
strong { font-weight: 700; }
.anchor { opacity: 0; margin-left: 6px; font-size: .8em; color: var(--text-3); }
h2:hover .anchor, h3:hover .anchor { opacity: 1; }

/* inline code & pre */
code { font-family: var(--mono); font-size: .88em; }
:not(pre) > code { background: var(--code-inline); padding: .12em .38em; border-radius: 4px; word-break: break-word; }
pre {
  position: relative; margin: 1rem 0; border-radius: var(--radius); background: var(--code-bg);
  overflow-x: auto; font-size: 13.5px; line-height: 1.6;
}
pre > code { display: block; padding: 16px 18px; color: #e6e6e6; background: transparent !important; }
pre code.hljs { padding: 16px 18px; }
.copy-btn {
  position: absolute; top: 8px; right: 8px; font: 12px var(--font); padding: 3px 8px; border-radius: 6px;
  border: 1px solid #3a3c44; background: #2a2c33; color: #c9c9c9; cursor: pointer; opacity: 0; transition: opacity .15s;
}
pre:hover .copy-btn { opacity: 1; }
.code-label { display: block; font: 600 11px var(--mono); color: #8b8f9a; padding: 10px 18px 0; text-transform: uppercase; letter-spacing: .06em; }

/* tables */
.table-wrap { overflow-x: auto; margin: 1rem 0; border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: 14px; background: var(--surface); }
th, td { padding: 9px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
th { background: var(--surface-2); font-weight: 700; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }

/* callouts */
.callout {
  border-left: 4px solid var(--accent); background: var(--accent-soft); border-radius: 0 var(--radius) var(--radius) 0;
  padding: 12px 16px; margin: 1.2rem 0; font-size: 15px;
}
.callout > :first-child { margin-top: 0; }
.callout > :last-child { margin-bottom: 0; }
.callout-title { display: block; font-weight: 700; margin-bottom: 4px; font-size: 14px; }
.callout.tip { border-color: var(--tip); background: var(--tip-soft); }
.callout.tip .callout-title { color: var(--tip); }
.callout.warn { border-color: var(--warn); background: var(--warn-soft); }
.callout.warn .callout-title { color: var(--warn); }
.callout.interview { border-color: var(--interview); background: var(--interview-soft); }
.callout.interview .callout-title { color: var(--interview); }
.callout.appier { border-color: var(--appier); background: var(--appier-soft); }
.callout.appier .callout-title { color: var(--appier); }
.callout pre { margin: .6rem 0; }

/* badges */
.badge {
  display: inline-block; font-size: 11px; font-weight: 700; padding: 1px 8px; border-radius: 99px;
  vertical-align: middle; line-height: 1.7; letter-spacing: .02em; border: 1px solid transparent;
}
.badge.basic { background: var(--tip-soft); color: var(--tip); }
.badge.mid { background: var(--warn-soft); color: var(--warn); }
.badge.hard { background: var(--appier-soft); color: var(--appier); }
.badge.freq { background: var(--interview-soft); color: var(--interview); }
h2 .badge, h3 .badge { margin-left: 8px; font-size: 11px; }

/* Q&A */
.qa-list { margin: 1rem 0; }
details.qa {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin: .6rem 0; box-shadow: var(--shadow);
}
details.qa > summary {
  cursor: pointer; padding: 12px 16px 12px 44px; font-weight: 600; list-style: none; position: relative; line-height: 1.55;
}
details.qa > summary::-webkit-details-marker { display: none; }
details.qa > summary::before {
  content: "Q"; position: absolute; left: 14px; top: 12px; width: 20px; height: 20px; border-radius: 6px;
  background: var(--interview-soft); color: var(--interview); font: 700 12px/20px var(--mono); text-align: center;
}
details.qa[open] > summary { border-bottom: 1px solid var(--border); }
details.qa > .answer { padding: 6px 18px 14px; }
details.qa > .answer > :first-child { margin-top: .6rem; }
.qa-know {
  display: inline-flex; align-items: center; gap: 6px; float: right; font-size: 12px; font-weight: 500; color: var(--text-3);
  margin: -2px 0 0 10px;
}
.qa-know input { accent-color: var(--tip); }
details.qa.known > summary::before { content: "✓"; background: var(--tip-soft); color: var(--tip); }

/* grids & cards */
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 14px; margin: 1rem 0; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 14px; margin: 1rem 0; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.card > :first-child { margin-top: 0; }
.card > :last-child { margin-bottom: 0; }
.card h4 { margin: 0 0 6px; color: var(--text); }
a.card { color: var(--text); display: block; transition: border-color .15s, transform .15s; }
a.card:hover { text-decoration: none; border-color: var(--accent); transform: translateY(-1px); }
.card .card-num { font: 600 12px var(--mono); color: var(--accent); }
.card p { color: var(--text-2); font-size: 14px; margin: 4px 0 0; }

/* mermaid diagrams */
.mermaid {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin: 1.2rem 0; text-align: center; overflow-x: auto;
}
.mermaid:not([data-processed]) { color: transparent; min-height: 80px; }
figure { margin: 1.2rem 0; }
figcaption { font-size: 13px; color: var(--text-3); text-align: center; margin-top: 6px; }

/* key-value term list */
dl.terms { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 1rem 0; }
dl.terms dt { font-weight: 700; font-family: var(--mono); font-size: 14px; }
dl.terms dd { margin: 0; color: var(--text-2); }

/* steps */
ol.steps { counter-reset: step; list-style: none; padding-left: 0; }
ol.steps > li { counter-increment: step; position: relative; padding-left: 40px; margin: .9rem 0; }
ol.steps > li::before {
  content: counter(step); position: absolute; left: 0; top: 2px; width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #fff; font: 700 13px/26px var(--mono); text-align: center;
}

/* pager */
.pager { display: flex; justify-content: space-between; gap: 14px; margin-top: 64px; }
.pager a { flex: 1; border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; background: var(--surface); color: var(--text); }
.pager a:hover { border-color: var(--accent); text-decoration: none; }
.pager small { display: block; color: var(--text-3); font-size: 12px; }
.pager .next { text-align: right; }

/* back to top */
.to-top {
  position: fixed; right: 20px; bottom: 20px; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-2); cursor: pointer;
  box-shadow: var(--shadow); display: none; font-size: 18px;
}
.to-top.show { display: block; }

/* ===== Responsive ===== */
@media (max-width: 960px) {
  .layout { display: block; }
  #sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 40; transform: translateX(-100%); transition: transform .2s;
    box-shadow: 0 0 40px rgba(0,0,0,.25); width: min(300px, 86vw);
  }
  body.nav-open #sidebar { transform: none; }
  body.nav-open::after { content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .topbar {
    display: flex; align-items: center; gap: 10px; position: sticky; top: 0; z-index: 20;
    background: var(--surface); border-bottom: 1px solid var(--border); padding: 10px 16px; font-weight: 700; font-size: 15px;
  }
  .content { padding: 24px 16px 100px; }
  .search-results { right: 0; }
  .toc ol { columns: 1; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  h1 { font-size: 1.7rem; }
  article h2 { font-size: 1.35rem; }
  dl.terms { grid-template-columns: 1fr; }
  dl.terms dd { margin-bottom: 8px; }
  .pager { flex-direction: column; }
}
@media print {
  #sidebar, .topbar, .to-top, .copy-btn, .done-btn, .pager { display: none !important; }
  details.qa { break-inside: avoid; }
  .content { max-width: none; padding: 0; }
}
