/* 레이아웃과 조판. 값은 theme.css의 변수를 사용합니다. */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-padding-top: 1.5rem;
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: var(--text-size);
  font-weight: var(--weight-body);
  line-height: var(--line-height);
  font-optical-sizing: auto;
  font-kerning: normal;
  word-break: keep-all;          /* 한글 어절 단위 줄바꿈 */
  overflow-wrap: break-word;
  text-rendering: optimizeLegibility;
}

@media (max-width: 40rem) {
  body { font-size: var(--text-size-mobile); }
}

a { color: var(--color-accent); text-underline-offset: 0.18em; text-decoration-thickness: 1px; }
a:hover { color: var(--color-accent-hover); }

:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
  border-radius: 2px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: 1rem; top: -3rem;
  z-index: 100;
  padding: 0.5rem 0.9rem;
  background: var(--color-text);
  color: var(--color-bg);
  font-family: var(--font-ui);
  font-size: var(--ui-size);
  border-radius: var(--radius);
}
.skip-link:focus { top: 0.75rem; color: var(--color-bg); }

/* ── 전체 배치 ──────────────────────────────────────────── */

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
}

.main {
  min-width: 0;
  padding: 3.5rem var(--page-gutter) 5rem;
}
.main:focus { outline: none; }

.note, .pager { max-width: var(--measure); margin-inline: auto; }

@media (min-width: 75rem) {
  .layout { grid-template-columns: var(--sidebar-width) minmax(0, 1fr) var(--toc-width); }
  .toc-inline { display: none; }
}
@media (max-width: 74.99rem) {
  .toc-side { display: none; }
}

/* ── 상단 막대(좁은 화면 전용) ─────────────────────────────── */

.topbar { display: none; }

@media (max-width: 56rem) {
  .topbar {
    position: sticky; top: 0; z-index: 30;
    display: flex; align-items: center; gap: 0.75rem;
    height: 3.25rem;
    padding: 0 1rem;
    background: color-mix(in srgb, var(--color-bg) 94%, transparent);
    backdrop-filter: blur(6px);
    border-bottom: 1px solid var(--color-border);
    font-family: var(--font-ui);
  }
  .topbar-brand { color: var(--color-text); text-decoration: none; font-size: 1rem; }
  .nav-toggle {
    display: inline-flex; align-items: center; gap: 0.4rem;
    padding: 0.35rem 0.6rem;
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius);
    background: var(--color-bg);
    color: var(--color-text);
    font: inherit; font-size: var(--ui-size);
    cursor: pointer;
  }
  html { scroll-padding-top: 4.5rem; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .main { padding-top: 2rem; }
}

/* ── 사이드바(과목별 탐색) ──────────────────────────────── */

.sidebar {
  position: sticky; top: 0;
  height: 100vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--color-bg-sunken);
  border-right: 1px solid var(--color-border);
  font-family: var(--font-ui);
  font-size: var(--ui-size);
  line-height: 1.5;
  word-break: keep-all;
}
.sidebar-inner { padding: 1.5rem 1rem 3rem 1.25rem; }

.brand {
  display: block;
  margin: 0 0 1.25rem 0.5rem;
  color: var(--color-text);
  font-size: 1.05rem;
  letter-spacing: -0.01em;
  text-decoration: none;
}

.nav-group + .nav-group { margin-top: 1.5rem; }
.nav-heading {
  margin: 0 0 0.35rem 0.5rem;
  color: var(--color-text-faint);
  font-size: var(--small-size);
  font-weight: var(--weight-body);
  letter-spacing: 0.04em;
}
.nav-list { list-style: none; margin: 0; padding: 0; }
.nav-list a {
  display: block;
  padding: 0.32rem 0.5rem;
  border-left: 2px solid transparent;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--color-text-muted);
  text-decoration: none;
}
.nav-list a:hover { color: var(--color-text); background: color-mix(in srgb, var(--color-border) 45%, transparent); }
.nav-list a[aria-current="page"] {
  color: var(--color-accent);
  border-left-color: var(--color-accent);
  background: var(--color-accent-soft);
}

@media (max-width: 56rem) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 50;
    width: min(20rem, 86vw);
    height: auto;
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.2s ease, visibility 0s linear 0.2s;
    box-shadow: 0 0 0 1px var(--color-border);
  }
  .nav-open .sidebar {
    transform: none;
    visibility: visible;
    transition: transform 0.2s ease;
  }
  .nav-backdrop {
    position: fixed; inset: 0; z-index: 40;
    background: rgb(20 24 28 / 0.28);
  }
  .nav-open { overflow: hidden; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none !important; }
}

/* ── 검색 ─────────────────────────────────────────────── */

.search { position: relative; margin-bottom: 1.75rem; }
.search-input {
  width: 100%;
  padding: 0.5rem 2rem 0.5rem 0.7rem;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: var(--color-bg);
  color: var(--color-text);
  font: inherit;
}
.search-input::placeholder { color: var(--color-text-faint); }
.search-input:focus { outline: 2px solid var(--color-focus); outline-offset: 0; border-color: transparent; }
.search-input::-webkit-search-cancel-button { display: none; }
.search-key {
  position: absolute; right: 0.55rem; top: 0.5rem;
  padding: 0 0.35rem;
  border: 1px solid var(--color-border-strong);
  border-radius: 3px;
  color: var(--color-text-faint);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  line-height: 1.5;
}
.search-input:focus ~ .search-key, .search-input:not(:placeholder-shown) ~ .search-key { display: none; }
.search-status { margin: 0.4rem 0.2rem 0; color: var(--color-text-faint); font-size: var(--small-size); }
.search-status:empty { display: none; }
.search-results {
  list-style: none;
  margin: 0.4rem 0 0; padding: 0;
  border-top: 1px solid var(--color-border);
}
.search-results a {
  display: block;
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--color-border);
  color: var(--color-text);
  text-decoration: none;
}
.search-results a:hover, .search-results a:focus-visible { background: var(--color-bg); outline-offset: -2px; }
.search-result-title { display: block; color: var(--color-accent); }
.search-result-subject { color: var(--color-text-faint); font-size: 0.78rem; margin-left: 0.3em; }
.search-result-excerpt { display: block; margin-top: 0.15rem; color: var(--color-text-muted); font-size: 0.8rem; line-height: 1.5; }
.search-results mark { background: var(--color-mark); color: inherit; padding: 0 1px; }

/* ── 문서 머리 ────────────────────────────────────────────── */

.note-header { margin-bottom: 2.5rem; }
.note-subject {
  margin: 0 0 0.6rem;
  color: var(--color-accent);
  font-family: var(--font-ui);
  font-size: var(--small-size);
  letter-spacing: 0.05em;
}
.note-header h1 {
  margin: 0;
  font-size: clamp(1.85rem, 1.3rem + 2vw, 2.45rem);
  font-weight: var(--weight-heading);
  line-height: 1.25;
  letter-spacing: -0.015em;
}
.note-lede { margin: 0.9rem 0 0; color: var(--color-text-muted); font-size: 1.04em; }
.note-meta { margin: 0.6rem 0 0; color: var(--color-text-faint); font-family: var(--font-ui); font-size: var(--small-size); }

/* ── 목차 ──────────────────────────────────────────────── */

.toc-list, .toc-list ol { list-style: none; margin: 0; padding: 0; }
.toc-list ol { padding-left: 0.85rem; }
.toc-list a {
  display: block;
  padding: 0.2rem 0 0.2rem 0.75rem;
  border-left: 1px solid var(--color-border);
  color: var(--color-text-muted);
  text-decoration: none;
  line-height: 1.45;
}
.toc-list a:hover { color: var(--color-text); }
.toc-list a[aria-current="true"] { color: var(--color-accent); border-left-color: var(--color-accent); }

.toc-side {
  position: sticky; top: 0;
  align-self: start;
  max-height: 100vh;
  overflow-y: auto;
  padding: 3.6rem 1.25rem 2rem 0;
  font-family: var(--font-ui);
  font-size: var(--small-size);
  word-break: keep-all;
}
.toc-title { margin: 0 0 0.6rem 0.75rem; color: var(--color-text-faint); letter-spacing: 0.04em; }

.toc-inline {
  margin: -1rem 0 2.25rem;
  padding: 0.25rem 0.9rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-family: var(--font-ui);
  font-size: var(--ui-size);
}
.toc-inline > summary { cursor: pointer; padding: 0.4rem 0; color: var(--color-text-muted); }
.toc-inline .toc-list { padding-bottom: 0.6rem; }

/* ── 본문 조판 ──────────────────────────────────────────── */

.prose > :first-child { margin-top: 0; }
.prose p { margin: 0 0 var(--paragraph-gap); }

.prose h2, .prose h3, .prose h4 {
  position: relative;
  font-weight: var(--weight-heading);
  line-height: var(--heading-line-height);
  letter-spacing: -0.01em;
  scroll-margin-top: 1.5rem;
}
.prose h2 {
  margin: 2.2em 0 0.8em;
  padding-top: 0.8em;
  border-top: 1px solid var(--color-border);
  font-size: 1.5em;
}
.prose h3 { margin: 2.2em 0 0.6em; font-size: 1.22em; }
.prose h4 {
  margin: 1.8em 0 0.4em;
  color: var(--color-text-muted);
  font-size: 1em;
  font-family: var(--font-ui);
  letter-spacing: 0.02em;
}
.prose h2 + h3 { margin-top: 1em; }

.heading-anchor {
  margin-left: 0.4em;
  color: var(--color-text-faint);
  font-family: var(--font-ui);
  font-weight: var(--weight-body);
  text-decoration: none;
  opacity: 0;
}
:is(h2, h3, h4):hover .heading-anchor, .heading-anchor:focus { opacity: 1; }

/* 굵은 글씨 대신 색으로만 구분합니다. */
.prose strong, .prose b { font-weight: inherit; color: var(--color-strong); }
.prose em { font-style: italic; }

.prose hr { margin: 2.5em 0; border: 0; border-top: 1px solid var(--color-border); }

.prose blockquote {
  margin: var(--block-gap) 0;
  padding: 0 0 0 1.1em;
  border-left: 2px solid var(--color-border-strong);
  color: var(--color-text-muted);
}

.prose img { display: block; max-width: 100%; height: auto; margin: 0.8em 0; }
.prose figure { margin: var(--block-gap) 0; }
.prose figcaption { margin-top: 0.4em; color: var(--color-text-muted); font-family: var(--font-ui); font-size: var(--small-size); }

/* 코드 */
.prose code, .prose kbd {
  font-family: var(--font-mono);
  font-size: 0.86em;
}
.prose :not(pre) > code {
  padding: 0.1em 0.3em;
  background: var(--color-bg-sunken);
  border: 1px solid var(--color-border);
  border-radius: 3px;
  word-break: break-all;
}
.prose pre {
  margin: var(--block-gap) 0;
  padding: 0.9em 1.1em;
  overflow-x: auto;
  background: var(--color-bg-sunken);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.84rem;
  line-height: 1.65;
  word-break: normal;
  tab-size: 2;
}
.prose pre code { font-size: inherit; }

/* 표 */
.table-wrap { margin: var(--block-gap) 0; overflow-x: auto; }
.prose table { border-collapse: collapse; min-width: 50%; font-size: 0.95em; }
.prose th, .prose td { padding: 0.45em 0.9em; border-bottom: 1px solid var(--color-border); text-align: left; vertical-align: top; }
.prose th { font-weight: var(--weight-heading); border-bottom-color: var(--color-border-strong); font-family: var(--font-ui); font-size: 0.92em; color: var(--color-text-muted); }

/* ── 목록 ──────────────────────────────────────────────── */

.prose ul, .prose ol {
  margin: 0.4em 0 var(--paragraph-gap);
  padding-left: var(--list-indent);
}
.prose li { margin: var(--list-item-gap) 0; padding-left: 0.15em; }
.prose li::marker { color: var(--color-marker); }
.prose ol > li::marker { font-family: var(--font-ui); font-size: 0.92em; font-variant-numeric: tabular-nums; }

/* 단계별 기호: • ◦ ▪ – / 1. a. i. */
.prose ul { list-style-type: disc; }
.prose ul ul { list-style-type: circle; }
.prose ul ul ul { list-style-type: square; }
.prose ul ul ul ul { list-style-type: "–  "; }
.prose ol { list-style-type: decimal; }
.prose ol ol { list-style-type: lower-alpha; }
.prose ol ol ol { list-style-type: lower-roman; }
.prose ol ol ol ol { list-style-type: decimal; }

/* 하위 목록: 위아래 간격을 줄이고 세로 안내선으로 계층을 표시합니다. */
.prose li > ul, .prose li > ol {
  margin: var(--list-item-gap) 0 0.2em;
  padding-left: calc(var(--list-indent) - 1px);
  border-left: 1px solid var(--color-list-guide);
}

/* 한 항목 안의 여러 문단 */
.prose li > p { margin: 0.4em 0; }
.prose li > p:first-child { margin-top: 0; }
.prose li > p:last-child { margin-bottom: 0; }
.prose li > :is(.callout, .fold, pre, .table-wrap, blockquote, img, figure) { margin: 0.7em 0; }
.prose li > :last-child { margin-bottom: 0; }

/* 체크리스트 */
.prose ul.task-list-container { list-style: none; padding-left: var(--list-indent); }
.prose li.task-list-item { position: relative; }
.prose .task-list-item-checkbox {
  position: absolute;
  left: calc(-1 * var(--list-indent) + 0.1em);
  top: 0.42em;
  width: 0.95em; height: 0.95em;
  margin: 0;
  appearance: none;
  border: 1.5px solid var(--color-marker);
  border-radius: 2px;
  background: var(--color-bg);
}
.prose .task-list-item-checkbox:checked {
  border-color: var(--color-accent);
  background: var(--color-accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.2l2.3 2.3 4.7-5' fill='none' stroke='white' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
}
.prose .task-list-item-checkbox:checked + label { color: var(--color-text-muted); }

/* ── 수식 ──────────────────────────────────────────────── */
/* 수식 내부 글꼴은 MathJax가 정하므로 여기서는 크기·여백·넘침만 다룹니다. */

/* MathJax가 조판을 마칠 때까지 TeX 원문을 숨깁니다(mathjax-config.js). */
.math-loading .math { visibility: hidden; }

mjx-container[jax="CHTML"] { font-size: calc(var(--math-scale) * 100%); }

.math-display { margin: 0.9em 0; }
/* 좌우 여백은 바깥 div에 둡니다. MathJax가 줄바꿈 폭을 잴 때 수식 상자 자체의 안쪽 여백은 빼지 않기 때문입니다. */
.math-display > mjx-container[display] { margin: 0; padding: 0.35em 0; }
/* 긴 수식은 MathJax가 줄바꿈하고, 줄바꿈할 수 없는 행렬 등은 수식 영역만 가로로 스크롤합니다. */
.math-display > mjx-container[display] {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  /* 넘칠 때만 가장자리에 옅은 그림자가 생겨 스크롤할 수 있음을 알려 줍니다. */
  background:
    linear-gradient(to right, var(--math-bg, var(--color-bg)) 60%, transparent) left / 1.5em 100% no-repeat local,
    linear-gradient(to left, var(--math-bg, var(--color-bg)) 60%, transparent) right / 1.5em 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgb(0 0 0 / 0.12), transparent) left / 0.6em 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgb(0 0 0 / 0.12), transparent) right / 0.6em 100% no-repeat scroll;
}

/* 목록·callout 안의 독립 수식은 왼쪽에 맞춰 문단 흐름을 유지합니다. */
.prose li .math-display { padding-left: 1em; }
.prose li .math-display > mjx-container[display]:not([width="full"]) {
  text-align: left;
  justify-content: left;
}

/* 화면낭독기 전용 MathML은 화면에 그리지 않습니다. */
mjx-assistive-mml {
  position: absolute !important;
  top: 0; left: 0;
  clip: rect(1px, 1px, 1px, 1px);
  padding: 1px 0 0 0 !important;
  border: 0 !important;
  display: block !important;
  width: auto !important;
  overflow: hidden !important;
  user-select: none;
}
mjx-container { position: relative; }
mjx-container[display] > mjx-assistive-mml { width: 100% !important; }

/* ── callout(정의·정리·예시·주의…) ─────────────────────────── */

.callout {
  --c: var(--callout-note);
  --c-bg: var(--callout-note-bg);
  margin: var(--block-gap) 0;
  padding: 0.75em 1.1em 0.85em;
  border-left: 3px solid var(--c);
  background: var(--c-bg);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.callout { --math-bg: var(--c-bg); }
.callout-definition { --c: var(--callout-definition); --c-bg: var(--callout-definition-bg); }
.callout-theorem    { --c: var(--callout-theorem);    --c-bg: var(--callout-theorem-bg); }
.callout-lemma      { --c: var(--callout-lemma);      --c-bg: var(--callout-lemma-bg); }
.callout-example    { --c: var(--callout-example);    --c-bg: var(--callout-example-bg); }
.callout-warning    { --c: var(--callout-warning);    --c-bg: var(--callout-warning-bg); }

.callout-title { margin: 0 0 0.35em !important; line-height: 1.5; }
.callout-label {
  color: var(--c);
  font-family: var(--font-ui);
  font-size: var(--small-size);
  letter-spacing: 0.06em;
}
.callout-name { margin-left: 0.35em; color: var(--color-text); }
.callout-body > :last-child { margin-bottom: 0; }
.callout-body > :first-child { margin-top: 0; }
.callout .math-display { margin: 0.6em 0; }

/* ── 접이식 증명·풀이 ──────────────────────────────────────── */

.fold {
  margin: var(--block-gap) 0;
  border: 1px solid var(--fold-border);
  border-radius: var(--radius);
  background: var(--fold-bg);
}
.fold > summary {
  display: flex; align-items: baseline; gap: 0.45em;
  padding: 0.55em 1em;
  cursor: pointer;
  list-style: none;
  border-radius: var(--radius);
}
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary::before {
  content: "";
  flex: none;
  width: 0.45em; height: 0.45em;
  margin-right: 0.15em;
  border-right: 1.5px solid var(--color-marker);
  border-bottom: 1.5px solid var(--color-marker);
  transform: translateY(-0.12em) rotate(-45deg);
  transition: transform 0.15s ease;
}
.fold[open] > summary::before { transform: translateY(-0.2em) rotate(45deg); }
.fold > summary:hover .fold-label { color: var(--color-accent); }
.fold-label {
  color: var(--color-text-muted);
  font-family: var(--font-ui);
  font-size: var(--small-size);
  letter-spacing: 0.06em;
}
.fold-name { color: var(--color-text); }
.fold-body { padding: 0.2em 1.15em 0.9em; border-top: 1px solid var(--fold-border); background: var(--color-bg); border-radius: 0 0 var(--radius) var(--radius); }
.fold-body > :first-child { margin-top: 0.7em; }
.fold-body > :last-child { margin-bottom: 0; }
/* 증명 끝 표시 ∎ */
.fold-proof > .fold-body::after {
  content: "∎";
  display: block;
  text-align: right;
  color: var(--color-text-muted);
  line-height: 1;
  margin-top: -0.9em;
}
@media (prefers-reduced-motion: reduce) { .fold > summary::before { transition: none; } }

/* ── 이전·다음 ──────────────────────────────────────────── */

.pager {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1rem;
  margin-top: 4rem; padding-top: 1.5rem;
  border-top: 1px solid var(--color-border);
  font-family: var(--font-ui);
}
.pager a { display: block; padding: 0.3rem 0; text-decoration: none; line-height: 1.45; word-break: keep-all; }
.pager-next { grid-column: 2; text-align: right; }
.pager-label { display: block; margin-bottom: 0.15rem; color: var(--color-text-faint); font-size: var(--small-size); }

/* ── 인쇄 ─────────────────────────────────────────────── */

@media print {
  .sidebar, .topbar, .toc-side, .toc-inline, .pager, .skip-link, .heading-anchor { display: none !important; }
  .layout { display: block; }
  .main { padding: 0; }
  body { background: #fff; font-size: 11pt; }
  .math-display > mjx-container[display] { overflow: visible; }
}
