/* ---------- 테마 변수 ---------- */
:root,
:root[data-theme="light"] {
  --bg:            #ffffff;
  --bg-soft:       #f6f7f9;
  --bg-sunken:     #eef0f4;
  --surface:       #ffffff;
  --border:        #e3e6ec;
  --border-strong: #c9cfda;
  --text:          #1c2028;
  --text-soft:     #5b6474;
  --text-faint:    #8b95a6;
  --accent:        #2f6fed;
  --accent-soft:   #e8efff;
  --code-bg:       #f4f6f9;
  --shadow:        0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .06);
}

:root[data-theme="dark"] {
  --bg:            #14171d;
  --bg-soft:       #191d25;
  --bg-sunken:     #1f242d;
  --surface:       #1b1f27;
  --border:        #2b313c;
  --border-strong: #3b4351;
  --text:          #e4e8ef;
  --text-soft:     #a6b0c0;
  --text-faint:    #7b8494;
  --accent:        #6f9dff;
  --accent-soft:   #23304d;
  --code-bg:       #1e232c;
  --shadow:        0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo",
               "Noto Sans KR", "Malgun Gothic", Segoe UI, Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.7;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- 상단 바 ---------- */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 56px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

.topbar-left,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand { font-size: 20px; line-height: 1; }

.select-wrap { position: relative; min-width: 0; }

.select-wrap::after {
  content: "";
  position: absolute;
  right: 12px; top: 50%;
  width: 7px; height: 7px;
  margin-top: -5px;
  border-right: 2px solid var(--text-faint);
  border-bottom: 2px solid var(--text-faint);
  transform: rotate(45deg);
  pointer-events: none;
}

#doc-select {
  appearance: none;
  max-width: 46vw;
  min-width: 200px;
  padding: 7px 34px 7px 12px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  text-overflow: ellipsis;
}

#doc-select:hover { border-color: var(--border-strong); }
#doc-select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* 라디오형 테마 아이콘 */
.theme-toggle {
  display: flex;
  align-items: center;
  gap: 2px;
  margin: 0;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg-soft);
}

.theme-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.theme-toggle label {
  display: grid;
  place-items: center;
  width: 30px; height: 26px;
  border-radius: 7px;
  cursor: pointer;
  color: var(--text-faint);
  transition: background .15s ease, color .15s ease;
}

.theme-toggle label:hover { color: var(--text); }

.theme-toggle svg {
  width: 16px; height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
}

.theme-toggle input:checked + label {
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .12);
}

.theme-toggle input:focus-visible + label {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.doc-meta {
  font-size: 12px;
  color: var(--text-faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: none;
  place-items: center;
  width: 34px; height: 30px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-soft);
  color: var(--text-soft);
  cursor: pointer;
}

.icon-btn svg {
  width: 18px; height: 18px;
  fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round;
}

/* ---------- 좌우 분할 레이아웃 ---------- */
.layout {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  min-height: 0;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg-soft);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-head {
  padding: 16px 18px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-head h2 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-faint);
}

#toc-filter {
  width: 100%;
  padding: 7px 10px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

#toc-filter:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.toc {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px 10px 40px;
  overscroll-behavior: contain;
}

.toc a {
  display: block;
  padding: 5px 10px 5px 12px;
  margin: 1px 0;
  border-left: 2px solid transparent;
  border-radius: 0 6px 6px 0;
  color: var(--text-soft);
  font-size: 13.5px;
  line-height: 1.45;
  text-decoration: none;
  overflow-wrap: anywhere;
}

.toc a:hover { background: var(--bg-sunken); color: var(--text); }

.toc a.lvl-1 { font-weight: 700; color: var(--text); font-size: 14px; margin-top: 10px; }
.toc a.lvl-2 { padding-left: 24px; }
.toc a.lvl-3 { padding-left: 38px; font-size: 13px; }
.toc a.lvl-4 { padding-left: 52px; font-size: 12.5px; color: var(--text-faint); }

/* 지금 보고 있는 섹션 밖의 항목은 흐리게 */
.toc a:not(.in-section) { color: var(--text-faint); }
.toc a:not(.in-section):hover { color: var(--text); }
.toc a.lvl-1:not(.in-section) { color: var(--text-soft); }

.toc a.active {
  color: var(--accent);
  background: var(--accent-soft);
  border-left-color: var(--accent);
  font-weight: 600;
}

.toc a[hidden] { display: none; }

.toc-empty {
  padding: 12px;
  font-size: 13px;
  color: var(--text-faint);
}

.content {
  position: relative; /* 제목의 offsetTop 기준이 되도록 */
  overflow-y: auto;
  min-width: 0;
}

.markdown-body {
  max-width: 860px;
  margin: 0 auto;
  padding: 40px 40px 64px;
}

/* 한 번에 한 섹션(최상위 제목 단위)만 보여준다 */
.doc-section[hidden] { display: none; }
.doc-section > :first-child { margin-top: 0; }

.section-nav {
  display: flex;
  gap: 12px;
  margin-top: 56px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

.section-nav[hidden] { display: none; }

.section-nav-btn {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  padding: 12px 16px;
  font: inherit;
  text-align: left;
  color: var(--text);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: border-color .15s ease, background .15s ease;
}

.section-nav-btn:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.section-nav-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.section-nav-btn[hidden] { display: none; }
.section-nav-btn.next { text-align: right; margin-left: auto; }

.section-nav-btn .dir {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-faint);
}

.section-nav-btn .label {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.placeholder { color: var(--text-faint); }

.scrim {
  display: none;
  position: fixed;
  inset: 56px 0 0;
  background: rgba(0, 0, 0, .4);
  z-index: 19;
}

/* ---------- 마크다운 본문 ---------- */
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  line-height: 1.35;
  font-weight: 700;
  scroll-margin-top: 24px;
}

.markdown-body h1 {
  margin: 2.2em 0 .7em;
  padding-bottom: .3em;
  font-size: 1.9em;
  border-bottom: 1px solid var(--border);
}

.markdown-body h1:first-child { margin-top: 0; }

.markdown-body h2 {
  margin: 2em 0 .6em;
  font-size: 1.42em;
}

.markdown-body h3 { margin: 1.7em 0 .5em; font-size: 1.16em; }
.markdown-body h4 { margin: 1.5em 0 .5em; font-size: 1.02em; color: var(--text-soft); }

.markdown-body p { margin: 0 0 1.05em; }

.markdown-body a { color: var(--accent); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }

.markdown-body ul,
.markdown-body ol { margin: 0 0 1.05em; padding-left: 1.5em; }
.markdown-body li { margin: .3em 0; }
.markdown-body li > ul,
.markdown-body li > ol { margin-bottom: .2em; }

.markdown-body blockquote {
  margin: 1.4em 0;
  padding: .9em 1.2em;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: var(--accent-soft);
  color: var(--text);
}

.markdown-body blockquote p:last-child { margin-bottom: 0; }

.markdown-body hr {
  height: 1px;
  margin: 2.6em 0;
  border: 0;
  background: var(--border);
}

.markdown-body table {
  width: 100%;
  margin: 1.2em 0 1.6em;
  border-collapse: collapse;
  font-size: .93em;
}

.markdown-body th,
.markdown-body td {
  padding: 9px 13px;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: var(--bg-sunken);
  font-weight: 600;
  white-space: nowrap;
}

.markdown-body tbody tr:nth-child(even) { background: var(--bg-soft); }

/* 넓은 표는 가로 스크롤 */
.table-scroll { overflow-x: auto; margin: 1.2em 0 1.6em; }
.table-scroll table { margin: 0; }

.markdown-body code {
  padding: .16em .4em;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: .875em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
}

.markdown-body pre {
  position: relative;
  margin: 1.2em 0 1.6em;
  padding: 0;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.markdown-body pre code {
  display: block;
  padding: 14px 16px;
  overflow-x: auto;
  background: transparent;
  border: 0;
  border-radius: 0;
  font-size: .855em;
  line-height: 1.6;
}

/* 문장형 블록은 줄바꿈 */
.markdown-body pre code.wrap {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding-right: 68px;
}

.copy-btn {
  position: absolute;
  top: 8px; right: 8px;
  padding: 3px 9px;
  font: inherit;
  font-size: 11.5px;
  color: var(--text-soft);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s ease;
}

.markdown-body pre:hover .copy-btn,
.copy-btn:focus-visible { opacity: 1; }

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

/* ---------- 반응형 ---------- */
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }

  .icon-btn { display: grid; }

  .sidebar {
    position: fixed;
    top: 56px; bottom: 0; left: 0;
    width: 300px;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open { transform: translateX(0); }
  .scrim { display: block; }

  .markdown-body { padding: 24px 20px 50vh; }
  #doc-select { min-width: 140px; max-width: 42vw; }
  .doc-meta { display: none; }
}
