/* ==========================================================================
   Level 2 교재 리더 — 목차(왼쪽) · 본문 · 이 문서(오른쪽)
   토큰은 style.css의 것을 그대로 쓴다.
   ========================================================================== */

body.reader { font-size: 16px; }
.rd-navwrap { max-width: none; }
.side-btn { display: none; }
.rd-muted { color: var(--muted); font-size: 14.5px; }

.rd {
  display: grid;
  grid-template-columns: 272px minmax(0, 1fr) 228px;
  gap: 0;
  max-width: 1480px;
  margin: 0 auto;
  min-height: calc(100vh - var(--nav-h));
}

/* ---------- 왼쪽: 교재 목차 ---------- */
.rd-side {
  position: sticky;
  top: var(--nav-h);
  align-self: start;
  height: calc(100vh - var(--nav-h));
  overflow-y: auto;
  border-right: 1px solid var(--line);
  overscroll-behavior: contain;
}
.rd-side-in { padding: 20px 14px 40px 20px; }
.side-top { display: grid; gap: 2px; margin-bottom: 12px; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
.side-link, .side-stage > summary {
  display: block;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 14px;
  color: var(--ink-2);
  text-decoration: none;
  line-height: 1.45;
}
.side-link:hover, .side-stage > summary:hover { background: var(--bg-sunk); color: var(--ink); }
.side-link.current {
  background: var(--accent-soft);
  color: var(--ink);
  font-weight: 650;
  box-shadow: inset 2px 0 0 var(--accent);
}
.side-stage { margin: 1px 0; }
.side-stage > summary {
  list-style: none;
  cursor: pointer;
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) 14px;
  gap: 6px;
  align-items: start;
  font-weight: 600;
}
.side-stage > summary::-webkit-details-marker { display: none; }
.side-stage > summary::after {
  content: "";
  width: 7px; height: 7px;
  margin-top: 7px;
  border-right: 1.6px solid var(--muted);
  border-bottom: 1.6px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform .15s;
}
.side-stage[open] > summary::after { transform: rotate(45deg); }
.side-k { font-family: var(--mono); font-size: 11.5px; font-weight: 700; color: var(--accent); padding-top: 2px; }
.side-stage ul { list-style: none; margin: 2px 0 8px 46px; display: grid; gap: 1px; }
.side-stage ul .side-link { font-size: 13.5px; padding: 5px 10px; }
.side-st { font-size: 10.5px; font-weight: 700; color: var(--warn); margin-left: 4px; white-space: nowrap; }

/* ---------- 본문 ---------- */
.rd-main { min-width: 0; padding: 30px 48px 80px; }
.rd-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  max-width: 820px;
  margin: 0 auto 18px;
  font-size: 13.5px;
  color: var(--muted);
}
.crumb { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.crumb a { color: var(--muted); text-decoration: none; }
.crumb a:hover { color: var(--accent); }
.crumb .sep { opacity: .5; }
.rd-meta .gh { color: var(--accent); font-weight: 650; text-decoration: none; white-space: nowrap; }
.rd-meta .gh:hover { text-decoration: underline; text-underline-offset: 3px; }

.md { max-width: 820px; margin: 0 auto; outline: none; color: var(--ink); }
.md > *:first-child { margin-top: 0; }
.md h1 { font-size: clamp(1.8rem, 3.6vw, 2.3rem); font-weight: 830; letter-spacing: -0.03em; margin: 0 0 22px; line-height: 1.25; }
.md h2 {
  font-size: 1.5rem;
  font-weight: 800;
  margin: 52px 0 16px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  letter-spacing: -0.025em;
}
.md h3 { font-size: 1.2rem; font-weight: 760; margin: 34px 0 12px; }
.md h4 { font-size: 1.04rem; font-weight: 740; margin: 26px 0 10px; }
.md h1, .md h2, .md h3, .md h4 { position: relative; scroll-margin-top: calc(var(--nav-h) + 18px); }
.hd-anchor {
  margin-left: 8px;
  font-weight: 500;
  color: var(--muted);
  text-decoration: none;
  opacity: 0;
  transition: opacity .15s;
}
.md :is(h2, h3, h4):hover .hd-anchor, .hd-anchor:focus-visible { opacity: 1; }
.md p { margin: 0 0 1.05em; line-height: 1.8; }
.md ul, .md ol { margin: 0 0 1.1em; padding-left: 1.45em; }
.md li { margin: .3em 0; line-height: 1.75; }
.md li > ul, .md li > ol { margin: .3em 0 .2em; }
.md li::marker { color: var(--muted); }
.md a { color: var(--accent); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px; }
.md a:hover { text-decoration-thickness: 2px; }
.md a.ext::after { content: " ↗"; font-size: .85em; }
.md strong { color: var(--ink); }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 36px 0; }
.md hr + h2 { border-top: 0; padding-top: 0; margin-top: 0; }
.md img { max-width: 100%; height: auto; border-radius: 10px; border: 1px solid var(--line); }
.md blockquote {
  margin: 0 0 1.2em;
  padding: 12px 18px;
  border-left: 3px solid var(--accent);
  border-radius: 0 10px 10px 0;
  background: var(--bg-sunk);
  color: var(--ink-2);
}
.md blockquote p:last-child { margin-bottom: 0; }
.md blockquote > :first-child { margin-top: 0; }

/* 표 — 모바일에서는 표만 가로로 스크롤 */
.md .tbl-wrap {
  margin: 0 0 1.4em;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev);
  -webkit-overflow-scrolling: touch;
}
.md table { border-collapse: collapse; width: 100%; font-size: 14.5px; }
.md th, .md td { padding: 10px 14px; text-align: left; vertical-align: top; line-height: 1.6; }
.md thead th { background: var(--bg-sunk); font-size: 13px; color: var(--muted); font-weight: 700; border-bottom: 1px solid var(--line); white-space: nowrap; }
.md tbody tr + tr td { border-top: 1px solid var(--line); }
.md td code { white-space: nowrap; }

/* 코드 */
.md :not(pre) > code { white-space: normal; }
.code-wrap {
  position: relative;
  margin: 0 0 1.3em;
  border-radius: 12px;
  background: var(--code-bg);
  border: 1px solid color-mix(in srgb, var(--code-bg) 80%, var(--line));
  overflow: hidden;
}
.code-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px 6px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, .07);
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--code-dim);
}
.copy-btn {
  font-family: var(--font);
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, .14);
  background: transparent;
  color: var(--code-ink);
  cursor: pointer;
}
.copy-btn:hover { background: rgba(255, 255, 255, .08); }
.copy-btn.done { color: #6ee7b7; border-color: rgba(110, 231, 183, .4); }
.md pre {
  margin: 0;
  padding: 14px 16px 16px;
  overflow-x: auto;
  font-size: 13.4px;
  line-height: 1.65;
  color: var(--code-ink);
  tab-size: 4;
}
.md pre code { font-family: var(--mono); background: none; border: 0; padding: 0; font-size: inherit; white-space: pre; }

/* highlight.js 토큰 — 코드 배경이 두 테마 모두 어두우므로 한 벌 */
.hljs-comment, .hljs-quote { color: #7f8f8c; font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-section, .hljs-doctag { color: #f0a6ca; }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-attribute, .hljs-meta .hljs-string { color: #a7e3a1; }
.hljs-number, .hljs-symbol, .hljs-bullet, .hljs-link { color: #f7c27b; }
.hljs-title, .hljs-title.function_, .hljs-title.class_, .hljs-name { color: #8fc5ff; }
.hljs-built_in, .hljs-type, .hljs-class .hljs-title { color: #7fdccf; }
.hljs-attr, .hljs-variable, .hljs-template-variable, .hljs-params { color: #ffd59a; }
.hljs-meta, .hljs-meta .hljs-keyword { color: #c3b4ff; }
.hljs-deletion { color: #ff9b9b; }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }

/* mermaid */
.md .mermaid-wrap {
  margin: 0 0 1.4em;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev);
  overflow-x: auto;
  text-align: center;
}
.md .mermaid-wrap svg { max-width: 100%; height: auto; }
.md .mermaid-src { display: none; }

/* details */
.md details {
  margin: 0 0 1.2em;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev);
  padding: 0 18px;
}
.md details > summary {
  cursor: pointer;
  padding: 12px 0;
  font-weight: 650;
  color: var(--ink);
}
.md details[open] > summary { border-bottom: 1px dashed var(--line); margin-bottom: 14px; }
.md details > :last-child { margin-bottom: 14px; }

/* 404 */
.rd-404 { padding: 28px; border: 1px dashed var(--line-strong); border-radius: 14px; }
.rd-404 h1 { font-size: 1.6rem; margin-bottom: 10px; }

/* 이전·다음 */
.rd-pager {
  max-width: 820px;
  margin: 56px auto 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.rd-pager a {
  display: block;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev);
  text-decoration: none;
  transition: border-color .15s;
}
.rd-pager a:hover { border-color: var(--accent); }
.rd-pager span { display: block; font-size: 12.5px; color: var(--muted); }
.rd-pager b { display: block; font-size: 15px; font-weight: 650; color: var(--ink); margin-top: 2px; }
.rd-pager .next { text-align: right; grid-column: 2; }

/* ---------- 오른쪽: 이 문서 ---------- */
.rd-toc {
  position: sticky;
  top: var(--nav-h);
  align-self: start;
  max-height: calc(100vh - var(--nav-h));
  overflow-y: auto;
}
.rd-toc-in { padding: 30px 20px 40px 8px; font-size: 13px; }
.rd-toc-in > b { display: block; font-size: 12px; font-weight: 700; color: var(--muted); letter-spacing: .04em; margin-bottom: 10px; }
.rd-toc ol { list-style: none; display: grid; gap: 1px; border-left: 1px solid var(--line); }
.rd-toc li a {
  display: block;
  padding: 4px 10px;
  margin-left: -1px;
  border-left: 2px solid transparent;
  color: var(--muted);
  text-decoration: none;
  line-height: 1.45;
}
.rd-toc li.h3 a { padding-left: 22px; font-size: 12.5px; }
.rd-toc li a:hover { color: var(--ink); }
.rd-toc li a.active { color: var(--accent); border-left-color: var(--accent); font-weight: 650; }

/* ---------- 반응형 ---------- */
@media (max-width: 1240px) {
  .rd { grid-template-columns: 260px minmax(0, 1fr); }
  .rd-toc { display: none; }
}

@media (max-width: 1020px) {
  .side-btn { display: inline-grid; }
  .rd { grid-template-columns: minmax(0, 1fr); }
  .rd-side {
    position: fixed;
    z-index: 60;
    top: var(--nav-h);
    left: 0;
    bottom: 0;
    height: auto;
    width: min(320px, 86vw);
    background: var(--bg);
    box-shadow: var(--shadow);
    transform: translateX(-102%);
    transition: transform .2s ease;
  }
  .rd-side.open { transform: none; }
  .rd-scrim {
    position: fixed;
    inset: var(--nav-h) 0 0 0;
    z-index: 55;
    background: rgba(5, 10, 10, .35);
  }
  .rd-main { padding: 24px var(--gutter) 64px; }
}

@media (max-width: 640px) {
  .md h2 { font-size: 1.32rem; margin-top: 40px; }
  .md h3 { font-size: 1.1rem; }
  .md pre { font-size: 12.6px; }
  .rd-pager { grid-template-columns: minmax(0, 1fr); }
  .rd-pager .next { grid-column: auto; }
  .brand .brand-text { display: none; }
}

@media print {
  .rd { display: block; }
  .rd-side, .rd-toc, .rd-pager, .rd-meta .gh, .copy-btn, .side-btn, .rd-scrim { display: none !important; }
  .rd-main { padding: 0; }
  .md pre { white-space: pre-wrap; }
  .code-wrap { break-inside: avoid; }
  .md details { border: 0; padding: 0; }
}
