/* ---------- design tokens ---------- */
:root {
  --bg: #fafaf9;
  --bg-elevated: #ffffff;
  --bg-sidebar: #f4f3f1;
  --bg-code: #f1efec;
  --bg-hover: #ece9e4;
  --text: #1e1e1c;
  --text-muted: #63625c;
  --text-faint: #8a8880;
  --border: #e2dfd8;
  --border-strong: #cfccc2;
  --accent: #7a4b2f;
  --accent-soft: #efe6dd;
  --link: #6a3f27;
  --code-text: #5b4636;
  --source-bg: #f6f1e9;
  --source-border: #d8c9ac;
  --diagram-bg: #fbfaf8;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", "Pretendard", "Noto Sans KR", sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  --sidebar-w: 268px;
  --toc-w: 236px;
  --topbar-h: 52px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161513;
    --bg-elevated: #1c1b18;
    --bg-sidebar: #1a1917;
    --bg-code: #221f1b;
    --bg-hover: #2a2723;
    --text: #eae7e0;
    --text-muted: #a8a49a;
    --text-faint: #756f64;
    --border: #322f29;
    --border-strong: #423e36;
    --accent: #d3a077;
    --accent-soft: #2c241c;
    --link: #e0b083;
    --code-text: #e2c8a8;
    --source-bg: #23201a;
    --source-border: #4a3f2c;
    --diagram-bg: #1c1a17;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 16.5px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- topbar ---------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  z-index: 40;
}
.nav-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text);
  cursor: pointer;
  padding: 4px 6px;
}
.brand {
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  letter-spacing: -0.01em;
}
.brand:hover { text-decoration: none; opacity: 0.8; }
.topbar-sub {
  color: var(--text-faint);
  font-size: 13px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
}

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--toc-w);
  padding-top: var(--topbar-h);
  min-height: 100vh;
}

.sidebar {
  grid-column: 1;
  border-right: 1px solid var(--border);
  background: var(--bg-sidebar);
  padding: 20px 12px 60px;
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
}

.content-area {
  grid-column: 2;
  padding: 32px 40px 100px;
  min-width: 0;
  max-width: 860px;
}

.toc {
  grid-column: 3;
  padding: 32px 16px;
  position: sticky;
  top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  font-size: 13px;
}

.nav-scrim { display: none; }

/* ---------- sidebar nav tree ---------- */
.nav-group { margin-bottom: 6px; }
.nav-group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-faint);
  padding: 10px 10px 6px;
}
.nav-item {
  display: block;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 14.5px;
  line-height: 1.4;
}
.nav-item:hover {
  background: var(--bg-hover);
  text-decoration: none;
  color: var(--text);
}
.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
.nav-item.top {
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
}
.nav-item.disabled {
  color: var(--text-faint);
  cursor: default;
}
.nav-item.disabled:hover { background: none; }

/* ---------- breadcrumb ---------- */
.breadcrumb {
  font-size: 13px;
  color: var(--text-faint);
  margin-bottom: 18px;
}
.breadcrumb a { color: var(--text-faint); }
.breadcrumb a:hover { color: var(--link); }

/* ---------- article typography ---------- */
.page h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0 0 6px;
  line-height: 1.3;
}
.page .page-kicker {
  font-size: 13px;
  color: var(--accent);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}
.page h2 {
  font-size: 21px;
  font-weight: 700;
  margin: 40px 0 14px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  scroll-margin-top: calc(var(--topbar-h) + 16px);
}
.page article > h2:first-of-type,
.page h1 + h2 { border-top: none; }
.page h3 {
  font-size: 17px;
  font-weight: 700;
  margin: 26px 0 10px;
  scroll-margin-top: calc(var(--topbar-h) + 16px);
}
.page h4 {
  font-size: 15px;
  font-weight: 700;
  margin: 20px 0 8px;
  color: var(--text-muted);
}
.page p { margin: 0 0 16px; }
.page ul, .page ol { margin: 0 0 16px; padding-left: 22px; }
.page li { margin-bottom: 6px; }
.page blockquote {
  margin: 0 0 16px;
  padding: 4px 16px;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
}
.page hr { border: none; border-top: 1px solid var(--border); margin: 32px 0; }
.page strong { color: var(--text); font-weight: 700; }

/* inline code / pdf citation badges */
.page code {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: var(--bg-code);
  color: var(--code-text);
  padding: 0.12em 0.4em;
  border-radius: 4px;
}
.page code.cite-ref {
  background: var(--source-bg);
  border: 1px solid var(--source-border);
  color: var(--accent);
  font-weight: 600;
}

/* fenced code blocks: plain code vs. ascii diagrams */
.page pre {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 0 0 18px;
}
.page pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 0.84em;
  color: var(--text);
  white-space: pre;
}
.page pre.diagram-block {
  background: var(--diagram-bg);
  border: 1px dashed var(--border-strong);
}
.page pre.diagram-block code {
  color: var(--text-muted);
}

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

/* katex overflow safety */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

/* ---------- source box (auto-wrapped "출처" section) ---------- */
.source-box {
  margin: 28px 0;
  padding: 16px 18px;
  background: var(--source-bg);
  border: 1px solid var(--source-border);
  border-left: 4px solid var(--accent);
  border-radius: 6px;
  font-size: 14px;
}
.source-box .source-label {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--accent);
  margin-bottom: 8px;
}
.source-box p, .source-box li { margin-bottom: 6px; }
.source-box ul { padding-left: 18px; margin-bottom: 0; }

/* ---------- flow diagram widget (site-authored, not from markdown) ---------- */
.flow-diagram {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin: 20px 0 28px;
}
.flow-diagram.vertical { flex-direction: column; align-items: stretch; }
.flow-step {
  flex: 1 1 140px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13.5px;
  text-align: center;
  box-shadow: var(--shadow);
}
.flow-step a { color: var(--text); font-weight: 600; }
.flow-step.current {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.flow-step.current a { color: var(--accent); }
.flow-step .flow-step-sub {
  display: block;
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 2px;
  font-weight: 400;
}
.flow-arrow {
  color: var(--text-faint);
  font-size: 16px;
  flex: 0 0 auto;
}
.flow-diagram.vertical .flow-arrow { text-align: center; }

/* ---------- concept cards (section index pages) ---------- */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 14px;
  margin: 20px 0 28px;
}
.concept-card {
  display: block;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: var(--shadow);
}
.concept-card:hover {
  border-color: var(--accent);
  text-decoration: none;
}
.concept-card .cc-index {
  font-size: 11px;
  color: var(--text-faint);
  font-weight: 700;
  letter-spacing: 0.04em;
}
.concept-card .cc-title {
  font-size: 15.5px;
  font-weight: 700;
  margin: 4px 0 4px;
}
.concept-card .cc-desc {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ---------- prev/next footer nav ---------- */
.pagenav {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 48px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}
.pagenav-link {
  flex: 1 1 0;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  min-width: 0;
}
.pagenav-link:hover { border-color: var(--accent); text-decoration: none; }
.pagenav-link.next { text-align: right; }
.pagenav-dir {
  display: block;
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 3px;
}
.pagenav-title { font-size: 14px; font-weight: 600; }

/* ---------- home page ---------- */
.hero {
  margin-bottom: 8px;
}
.hero-eyebrow {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
}
.hero h1 { font-size: 32px; margin-bottom: 12px; }
.hero-desc {
  font-size: 16px;
  color: var(--text-muted);
  max-width: 640px;
  margin-bottom: 28px;
}
.home-nav-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin: 24px 0 40px;
}
.home-nav-card {
  padding: 20px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  color: var(--text);
}
.home-nav-card:hover { border-color: var(--accent); text-decoration: none; }
.home-nav-card h3 { margin: 0 0 6px; font-size: 16px; }
.home-nav-card p { margin: 0; font-size: 13px; color: var(--text-muted); }
.section-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  margin: 40px 0 14px;
}

/* ---------- table of contents (right rail) ---------- */
.toc-title {
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-faint);
  margin-bottom: 10px;
}
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list li { margin-bottom: 6px; }
.toc-list a {
  color: var(--text-muted);
  display: block;
  line-height: 1.4;
  border-left: 2px solid transparent;
  padding-left: 10px;
}
.toc-list a:hover { color: var(--link); text-decoration: none; }
.toc-list.lvl3 a { padding-left: 22px; font-size: 12.5px; }

/* ---------- placeholder (research) ---------- */
.placeholder-box {
  padding: 40px 24px;
  text-align: center;
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  color: var(--text-muted);
  background: var(--bg-elevated);
}

/* ---------- loading / error ---------- */
.state-msg {
  color: var(--text-faint);
  padding: 40px 0;
  font-size: 14px;
}

/* ---------- tag pills (문제 태그, 태그별 보기) ---------- */
.tag-pill {
  display: inline-block;
  padding: 3px 10px;
  margin: 0 6px 6px 0;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12.5px;
  font-weight: 600;
}
a.tag-pill:hover { text-decoration: none; opacity: 0.8; }
.tag-cloud { margin: 20px 0 28px; }
.tag-problem-list { list-style: none; margin: 0; padding: 0; }
.tag-problem-list li {
  padding: 10px 14px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elevated);
}
.tag-problem-list .tpl-title { font-weight: 700; margin-right: 8px; }
.tag-problem-list .tpl-chapter { color: var(--text-faint); font-size: 12.5px; }

/* ---------- embedded progress dashboard ----------
 * app.js의 renderProgress()가 progress_dashboard.html의 body를 #qm-dashboard-root
 * 안에 그대로 이식하고, 그 <style>도 (:root/body 등만 #qm-dashboard-root 기준으로
 * 다시 스코프해) 같이 주입한다 — 자세한 이유는 app.js의 scopeDashboardCss 주석 참고.
 * 대시보드 자신의 .wrap이 max-width:960px로 이미 스스로 중앙 정렬되므로, 여기서는
 * 그걸 가리지 않도록 바깥 여백만 얇게 둔다. */
body.progress-page .layout {
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}
body.progress-page .toc { display: none; }
body.progress-page .content-area {
  max-width: none;
  padding: 12px 8px 24px;
}

/* ---------- responsive ---------- */
@media (max-width: 1180px) {
  .layout { grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
  .toc { display: none; }
}

@media (max-width: 900px) {
  .nav-toggle { display: inline-block; }
  .topbar-sub { display: none; }
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    left: 0;
    top: var(--topbar-h);
    width: 82vw;
    max-width: 320px;
    height: calc(100vh - var(--topbar-h));
    transform: translateX(-100%);
    transition: transform 0.18s ease;
    z-index: 50;
    box-shadow: 4px 0 16px rgba(0,0,0,0.15);
  }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open .nav-scrim {
    display: block;
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    background: rgba(0,0,0,0.35);
    z-index: 45;
  }
  .content-area { padding: 24px 20px 80px; }
  .home-nav-grid { grid-template-columns: 1fr; }
}
