:root {
  --bg: #faf8f4;
  --paper: #fffef9;
  --ink: #1a1612;
  --ink-soft: #3d3528;
  --ink-faint: #7a6e60;
  --rule: #d4cabb;
  --rule-light: #e8e2d6;
  --accent: #8b3a1c;
  --accent-soft: #c4623a;
  --accent-pale: #f5ede7;
  --def-bg: #f7f3ee;
  --thm-bg: #f0ede8;
  --thm-border: #9b4e2a;
  --tag-bg: #1a1612;
  --tag-fg: #faf8f4;
  --en-bg: #f0f4f8;
  --en-border: #4a6fa5;
  --en-accent: #2c4a7c;
  --en-pale: #e8eef5;
  --pr-bg: #4a3a6b;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; scroll-behavior: smooth; overflow-x: hidden; }

body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Noto Serif KR', 'Libre Baskerville', Georgia, serif;
  line-height: 1.85;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ── HEADER ── */
header {
  background: var(--ink);
  color: var(--bg);
  padding: 3.5rem 2rem 3rem;
  text-align: center;
  position: relative;
  overflow: hidden;
}
header::before {
  content: '';
  position: absolute; inset: 0;
  background: repeating-linear-gradient(90deg, transparent, transparent 60px, rgba(255,255,255,.03) 60px, rgba(255,255,255,.03) 61px),
              repeating-linear-gradient(0deg, transparent, transparent 60px, rgba(255,255,255,.03) 60px, rgba(255,255,255,.03) 61px);
}
header .subtitle {
  font-family: 'JetBrains Mono', monospace;
  font-size: .72rem;
  letter-spacing: .22em;
  color: var(--accent-soft);
  text-transform: uppercase;
  margin-bottom: .9rem;
  position: relative;
}
header h1 {
  font-family: 'Libre Baskerville', Georgia, serif;
  font-size: clamp(1.6rem, 4vw, 2.6rem);
  font-weight: 700;
  letter-spacing: -.02em;
  line-height: 1.2;
  position: relative;
}
header h1 span {
  display: block;
  font-size: .65em;
  font-weight: 400;
  font-style: italic;
  color: rgba(250,248,244,.55);
  margin-top: .35rem;
}
header .header-desc {
  position: relative;
  margin-top: 1.5rem;
  font-size: .88rem;
  color: rgba(250,248,244,.6);
  max-width: 520px;
  margin-left: auto;
  margin-right: auto;
  line-height: 1.7;
}
header .chapter-tag {
  display: inline-block;
  margin-top: 1.4rem;
  padding: .28rem .9rem;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 2px;
  font-size: .75rem;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: .12em;
  color: rgba(250,248,244,.65);
  position: relative;
}
.lang-badge {
  display: inline-flex;
  gap: .5rem;
  margin-top: 1rem;
  position: relative;
}
.lang-badge span {
  padding: .2rem .6rem;
  border-radius: 2px;
  font-size: .7rem;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: .08em;
}
.lang-badge .ko { background: rgba(139,58,28,.7); color: #fff; }
.lang-badge .en { background: rgba(44,74,124,.7); color: #fff; }

/* ── LANG TOGGLE ── */
.lang-toggle-bar {
  background: var(--def-bg);
  border-bottom: 1px solid var(--rule-light);
  padding: .7rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  font-size: .8rem;
  font-family: 'JetBrains Mono', monospace;
  color: var(--ink-faint);
  position: sticky;
  top: 0;
  z-index: 100;
}
.lang-toggle-bar label { display: flex; align-items: center; gap: .4rem; cursor: pointer; }
.lang-toggle-bar input[type=checkbox] { accent-color: var(--en-border); cursor: pointer; }
.lang-toggle-bar .sep { color: var(--rule); }

/* ── LAYOUT ── */
main { max-width: 1100px; margin: 0 auto; padding: 2.5rem 1.5rem 5rem; }

/* ── SECTION DIVIDER ── */
.section-title {
  margin: 3.5rem 0 1.5rem;
  display: flex;
  align-items: center;
  gap: .9rem;
}
.section-title::after { content: ''; flex: 1; height: 1px; background: var(--rule); }
.section-title h2 {
  font-size: .72rem;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: var(--ink-faint);
  white-space: nowrap;
}

/* ── CARD BASE ── */
.card {
  background: var(--paper);
  border: 1px solid var(--rule-light);
  border-radius: 3px;
  margin-bottom: 1.6rem;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0,0,0,.05);
  transition: box-shadow .2s;
}
.card:hover { box-shadow: 0 3px 12px rgba(0,0,0,.09); }

/* ── CARD HEAD ── */
.ex-card .card-head,
.pr-card .card-head {
  display: flex;
  align-items: center;
  gap: .8rem;
  padding: .75rem 1.2rem .6rem;
  background: var(--thm-bg);
  border-bottom: 1px solid var(--rule-light);
}
.ex-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: .68rem;
  font-weight: 500;
  padding: .18rem .55rem;
  background: var(--accent);
  color: #fff;
  border-radius: 2px;
  letter-spacing: .06em;
  white-space: nowrap;
}
/* 유제 배지: ex-num과 같은 형태, 차분한 teal 톤으로 색상만 구분 */
.yu-num {
  background: #3f7a6a;
}
.pr-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: .68rem;
  font-weight: 500;
  padding: .18rem .55rem;
  background: var(--pr-bg);
  color: #fff;
  border-radius: 2px;
  letter-spacing: .06em;
  white-space: nowrap;
}
.ex-title {
  font-family: 'Libre Baskerville', Georgia, serif;
  font-size: .95rem;
  font-weight: 700;
  color: var(--accent);
}
.pr-card .ex-title { color: var(--pr-bg); }
.ref-link {
  margin-left: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: .65rem;
  letter-spacing: .06em;
  color: var(--en-accent);
  text-decoration: none;
  border: 1px solid var(--en-border);
  padding: .15rem .5rem;
  border-radius: 2px;
  white-space: nowrap;
  transition: all .15s;
}
.ref-link:hover { background: var(--en-border); color: #fff; }

/* ── BILINGUAL BODY ── */
.card-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  align-items: start;
  gap: 0;
}
@media (max-width: 720px) {
  .card-body { grid-template-columns: 1fr; }
}

.ko-panel {
  padding: 1.1rem 1.3rem 1.2rem;
  border-right: 1px solid var(--rule-light);
  min-width: 0;
  overflow: hidden;
}
.en-panel {
  padding: 1.1rem 1.3rem 1.2rem;
  background: #fcfdff;
  transition: opacity .2s;
  min-width: 0;
  overflow: hidden;
}
@media (max-width: 720px) {
  .ko-panel { border-right: none; border-bottom: 1px solid var(--rule-light); }
}

body.hide-en .en-panel { display: none; }
body.hide-en .card-body { grid-template-columns: 1fr; }
body.hide-ko .ko-panel { display: none; }
body.hide-ko .card-body { grid-template-columns: 1fr; }

/* ── PANEL LABELS ── */
.panel-label {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  font-family: 'JetBrains Mono', monospace;
  font-size: .62rem;
  letter-spacing: .14em;
  text-transform: uppercase;
  padding: .14rem .45rem;
  border-radius: 2px;
  margin-bottom: .7rem;
}
.panel-label.ko-label { background: rgba(139,58,28,.12); color: var(--accent); }
.panel-label.en-label { background: rgba(44,74,124,.12); color: var(--en-accent); }

/* ── FORMAL BLOCK (KO) ── */
.formal {
  background: var(--accent-pale);
  border-left: 3px solid var(--accent);
  border-radius: 0 2px 2px 0;
  padding: .85rem 1.1rem;
  margin: .5rem 0 .7rem;
  font-size: .94rem;
}
.formal p { margin-bottom: .4rem; }
.formal p:last-child { margin-bottom: 0; }

/* ── EN FORMAL BLOCK ── */
.en-formal {
  background: var(--en-pale);
  border-left: 3px solid var(--en-border);
  border-radius: 0 2px 2px 0;
  padding: .85rem 1.1rem;
  margin: .5rem 0 .7rem;
  font-size: .91rem;
  font-family: 'Libre Baskerville', Georgia, serif;
  line-height: 1.8;
  color: var(--ink-soft);
}
.en-formal p { margin-bottom: .4rem; }
.en-formal p:last-child { margin-bottom: 0; }

/* ── FORMAL BOX (보기/힌트) ── */
.formal-box {
  border: 1px solid var(--rule);
  padding: .6rem .9rem;
  margin: .5rem 0;
  border-radius: 4px;
}

/* ── DISPLAY MATH ── */
.math-display {
  text-align: center;
  padding: .65rem .5rem;
  margin: .5rem 0;
  overflow-x: auto;
  font-size: 1.02rem;
  max-width: 100%;
}

/* ── FORMAL TABLES ── */
.formal-table {
  border-collapse: collapse;
  font-family: 'JetBrains Mono', monospace;
  font-size: .8rem;
  margin: .4rem 0;
}
.formal-table caption {
  font-family: 'JetBrains Mono', monospace;
  font-size: .7rem;
  color: var(--ink-faint);
  caption-side: bottom;
  padding-top: .3rem;
}
.formal-table th, .formal-table td {
  border: 1px solid var(--rule);
  padding: .25rem .6rem;
  text-align: center;
}
.formal-table th { background: var(--thm-bg); font-weight: 700; }
.table-pair { display: flex; flex-wrap: wrap; gap: 1.2rem; align-items: flex-start; }

/* ── PROOF BLOCK ── */
.proof-toggle {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  font-family: 'JetBrains Mono', monospace;
  font-size: .68rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--ink-faint);
  cursor: pointer;
  border: 1px solid var(--rule);
  padding: .22rem .6rem;
  border-radius: 2px;
  background: none;
  margin-top: .55rem;
  transition: all .15s;
}
.proof-toggle:hover { border-color: var(--accent); color: var(--accent); }
.proof-toggle .arrow { transition: transform .2s; display: inline-block; }
.proof-toggle.open .arrow { transform: rotate(90deg); }
.proof-content {
  display: none;
  margin-top: .75rem;
  padding: .85rem 1rem;
  background: #f5f2ec;
  border-left: 2px solid var(--rule);
  font-size: .88rem;
  color: var(--ink-soft);
  line-height: 1.8;
  overflow-x: auto;
}
.proof-content.visible { display: block; }
.proof-content p { margin-bottom: .5rem; }
.qed { text-align: right; font-size: 1.1rem; color: var(--ink-faint); margin-top: .4rem; }

.en-proof-content {
  display: none;
  margin-top: .75rem;
  padding: .85rem 1rem;
  background: #edf1f7;
  border-left: 2px solid #a3b8d4;
  font-size: .86rem;
  color: #3a4e65;
  line-height: 1.8;
  font-family: 'Libre Baskerville', Georgia, serif;
  overflow-x: auto;
}
.en-proof-content.visible { display: block; }
.en-proof-content p { margin-bottom: .5rem; }

/* ── TOC ── */
.toc {
  background: var(--def-bg);
  border: 1px solid var(--rule-light);
  border-radius: 3px;
  padding: 1.3rem 1.5rem;
  margin-bottom: 2.5rem;
}
.toc h3 {
  font-size: .72rem;
  font-family: 'JetBrains Mono', monospace;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: .9rem;
}
.toc ol {
  list-style: none;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .25rem .5rem;
}
@media (max-width: 560px) { .toc ol { grid-template-columns: 1fr; } }
.toc ol li a {
  font-size: .83rem;
  color: var(--ink-soft);
  text-decoration: none;
  display: flex;
  gap: .5rem;
  align-items: baseline;
  transition: color .15s;
}
.toc ol li a:hover { color: var(--accent); }
.toc ol li a .toc-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: .67rem;
  color: var(--ink-faint);
  min-width: 3rem;
}

/* ── UTILITY ── */
p { margin-bottom: .5rem; }
p:last-child { margin-bottom: 0; }
strong { color: var(--ink); font-weight: 700; }
.en-panel strong { color: var(--en-accent); }

/* ── CHAPTER CARDS GRID (HUB) ── */
.chapter-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.8rem;
}
@media (max-width: 640px) {
  .chapter-grid { grid-template-columns: 1fr; }
}

.chapter-card {
  background: var(--paper);
  border: 1px solid var(--rule-light);
  border-radius: 3px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0,0,0,.05);
  transition: box-shadow .25s, transform .25s;
  display: flex;
  flex-direction: column;
}
.chapter-card:hover {
  box-shadow: 0 6px 20px rgba(0,0,0,.1);
  transform: translateY(-2px);
}
.chapter-card .card-top {
  padding: 1.8rem 1.5rem 1.4rem;
  flex: 1;
}
.chapter-card .ch-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: .65rem;
  font-weight: 500;
  padding: .2rem .6rem;
  background: var(--tag-bg);
  color: var(--tag-fg);
  border-radius: 2px;
  letter-spacing: .08em;
  display: inline-block;
  margin-bottom: 1rem;
}
.chapter-card h3 {
  font-family: 'Noto Serif KR', serif;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: .3rem;
  line-height: 1.4;
}
.chapter-card .ch-en-title {
  font-family: 'Libre Baskerville', Georgia, serif;
  font-size: .92rem;
  font-style: italic;
  color: var(--ink-faint);
  margin-bottom: 1rem;
}
.chapter-card .ch-desc {
  font-size: .84rem;
  color: var(--ink-soft);
  line-height: 1.7;
}
.chapter-card .card-actions {
  display: flex;
  border-top: 1px solid var(--rule-light);
}
.chapter-card .card-actions a {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  padding: .85rem .5rem;
  font-family: 'JetBrains Mono', monospace;
  font-size: .75rem;
  letter-spacing: .06em;
  text-decoration: none;
  background: var(--accent-pale);
  color: var(--accent);
  transition: background .15s, color .15s;
}
.chapter-card .card-actions a:hover {
  background: var(--accent);
  color: #fff;
}

/* ── INTRO TEXT ── */
.intro-text {
  text-align: center;
  max-width: 620px;
  margin: 0 auto 2.5rem;
  font-size: .9rem;
  color: var(--ink-soft);
  line-height: 1.8;
}

/* ── SITE NAV ── */
.site-nav {
  background: var(--ink);
  padding: .6rem 1.5rem;
  display: flex;
  gap: 1.5rem;
  align-items: center;
  font-family: 'JetBrains Mono', monospace;
  font-size: .75rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.site-nav a { color: #ccc; text-decoration: none; letter-spacing: .05em; transition: color .15s; }
.site-nav a:hover { color: #fff; }
.site-nav a.active { color: var(--accent-soft); }
.nav-sep { color: #555; }

/* ── SKIP LINK ── */
.skip-link {
  position: absolute; left: -9999px; top: auto;
  padding: .5rem 1rem; background: var(--accent); color: #fff;
  font-size: .85rem; z-index: 1000; border-radius: 0 0 4px 0;
}
.skip-link:focus { left: 0; top: 0; }

/* ── FOOTER ── */
.site-footer {
  background: var(--ink);
  color: #aaa;
  padding: 2.5rem 1.5rem;
  text-align: center;
  font-size: .8rem;
  margin-top: 3rem;
}
.site-footer a { color: #ccc; text-decoration: none; margin: 0 .8rem; transition: color .15s; }
.site-footer a:hover { color: #fff; }
.site-footer .footer-copy { margin-top: 1rem; font-size: .72rem; color: #666; }
.site-footer .footer-desc { margin-top: .5rem; font-size: .7rem; color: #555; }

@media (max-width: 600px) {
  .ko-panel, .en-panel { padding: .85rem .95rem .95rem; }
  header { padding: 2.5rem 1.2rem 2.2rem; }
  .site-nav { gap: 1rem; font-size: .68rem; flex-wrap: wrap; }
  .chapter-card .card-top { padding: 1.4rem 1.2rem 1.1rem; }
}

/* ── SCROLL TO TOP ── */
.scroll-top {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transition: opacity .3s, visibility .3s, transform .2s;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}
.scroll-top.visible { opacity: .85; visibility: visible; }
.scroll-top:hover { opacity: 1; transform: translateY(-2px); }

/* ── RESPONSIVE MATH OVERFLOW FIX (2026-07-19) ──
   좀은 폭에서 긴 display 수식이 패널 경계를 뚫거나 잘리는 문제:
   (1) display 수식은 가로 스크롤, (2) 900px 이하에서는 한·영 패널을 세로로 스택 */
mjx-container[jax="CHTML"][display="true"] {
  display: block;
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  padding: 3px 2px;
  scrollbar-width: thin;
}
@media (max-width: 900px) {
  .card-body { grid-template-columns: 1fr; }
  .ko-panel { border-right: none; border-bottom: 1px solid var(--rule-light); }
}
