/* File: static/css/course.css */

/* ================= 공통 변수/기본 ================= */
:root {
  --navbar-height: 100px; /* 네비바 높이에 맞게 조정 */
}
html { scroll-behavior: smooth; }           /* 앵커 이동 부드럽게 */

/* 페이지 가로 스크롤 방지 (안전장치) */
html, body { max-width: 100%; overflow-x: hidden; }

/* ================= 레이아웃 ================= */
/* 좌측 TOC / 우측 본문 그리드 */
.course-layout {
  display: grid;
  grid-template-columns: clamp(200px, 22vw, 280px) minmax(0, 1fr);
  gap: 1.5rem;
}

/* TOC 박스 (본문과 함께 스크롤: 비고정) */
.course-toc,
.course-toc .toc-box {
  position: static;         /* ← 고정 해제 (sticky/fixed 사용 안 함) */
  top: auto;
  align-self: auto;
  max-height: none;
  overflow: visible;
  padding-right: 0;
  z-index: auto;
}

/* TOC 스타일 */
.course-toc .toc-box {
  border: 1px solid var(--gh-border, #d0d7de);
  border-radius: 8px;
  padding: .75rem;
  background: var(--gh-soft, #f6f8fa);
}
.course-toc .toc-title {
  font-weight: 600;
  margin-bottom: .5rem;
}

/* 본문 컬럼 설정 */
.course-toc, .course-doc { min-width: 0; }  /* grid 자식이 부모 폭을 밀지 않도록 */
.course-doc { min-height: 60vh; }
.course-header h1 { margin-top: .25rem; }

/* ================= GitHub 스타일 보완 ================= */
.markdown-body h1, .markdown-body h2 {
  border-bottom: 1px solid var(--gh-border, #d0d7de);
  padding-bottom: .3rem;
}

/* 헤딩 앵커 점프 시 네비바에 가려지지 않게 */
.markdown-body h1, .markdown-body h2, .markdown-body h3,
.markdown-body h4, .markdown-body h5, .markdown-body h6 {
  scroll-margin-top: calc(var(--navbar-height) + 12px);
}

/* 헤딩 옆 앵커 아이콘: 호버 시만 표시 */
.markdown-body a.headerlink {
  color: var(--gh-link, #0969da);
  margin-left: .25em;
  text-decoration: none;
  opacity: 0;
}
/* Markdown 렌더러가 기본으로 넣는 번호 제거 */
.markdown-body h1::before,
.markdown-body h2::before,
.markdown-body h3::before,
.markdown-body h4::before,
.markdown-body h5::before,
.markdown-body h6::before {
  content: none !important;
}
/* ================= 가로 스크롤/콘텐츠 안전 장치 ================= */
/* 긴 제목/URL 줄바꿈 허용 */
.course-toc, .course-toc * { overflow-wrap: anywhere; word-break: break-word; }
.markdown-body { overflow-wrap: anywhere; word-break: break-word; }

/* 미디어가 컨테이너를 넘지 않게 */
.markdown-body img, .markdown-body video, .markdown-body svg, .markdown-body canvas {
  max-width: 100%;
  height: auto;
}

/* === GitHub-like Table Set ============================ */
:root{
  --gh-border: #d0d7de;
  --gh-bg: #ffffff;
  --gh-soft: #f6f8fa;
  --gh-soft-2: #eef2f6;
  --gh-text: #24292f;
}

/* 표 기본 스타일 */
.markdown-body table {
  border-collapse: collapse;
  border-spacing: 0;
  width: auto;                 /* ← 100% 대신 auto로 변경 */
  max-width: 100%;             /* 컨테이너 넘어가면 줄어듦 */
  margin: 1rem 0;
  border: 1px solid var(--gh-border);
  border-radius: 6px;
  overflow-x: auto;            /* 긴 표만 스크롤 */
  display: inline-table;       /* ← block → inline-table로 변경 */
}

/* 표 안쪽 컨테이너 여백 */
.markdown-body table > thead,
.markdown-body table > tbody,
.markdown-body table > tfoot{
  background: var(--gh-bg);
}

/* 표 셀 */
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--gh-border);
  padding: .55rem .8rem;
  vertical-align: top;
  line-height: 1.5;
}

/* 헤더 */
.markdown-body thead th{
  position: sticky;               /* 긴 표 스크롤 시 헤더 고정 */
  top: 0;
  background: var(--gh-soft);
  font-weight: 600;
  z-index: 1;
}

/* 줄무늬 + hover */
.markdown-body tbody tr:nth-child(even){
  background: #fafbfc;
}
.markdown-body tbody tr:hover{
  background: var(--gh-soft-2);
}

/* 캡션 */
.markdown-body table caption{
  caption-side: top;
  text-align: left;
  font-weight: 600;
  color: #57606a;
  padding: .25rem 0 .5rem;
}

/* 코드/숫자 정렬 */
.markdown-body td code,
.markdown-body th code{
  background: var(--gh-soft);
  padding: .1rem .25rem;
  border-radius: 4px;
}
.markdown-body td.num,
.markdown-body th.num{
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* 작은 테이블(공간 절약) */
.markdown-body table.table-sm th,
.markdown-body table.table-sm td{
  padding: .35rem .6rem;
}

/* 긴 표가 화면을 넘을 때만 스크롤 */
.markdown-body .table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* 작은 화면에서 스크롤 그림자(스크롤 가능한 느낌) */
.markdown-body table{
  background:
    linear-gradient(to right, var(--gh-bg), var(--gh-bg)) padding-box,
    linear-gradient(to right, rgba(0,0,0,.08), rgba(0,0,0,0)) left/12px 100%,
    linear-gradient(to left,  rgba(0,0,0,.08), rgba(0,0,0,0)) right/12px 100%;
  background-repeat: no-repeat;
}

/* 다크 모드(선택): prefers-color-scheme 따라 자동 전환 */
@media (prefers-color-scheme: dark){
  :root{
    --gh-border: #3d444d;
    --gh-bg: #0d1117;
    --gh-soft: #161b22;
    --gh-soft-2: #1f2630;
    --gh-text: #c9d1d9;
  }
  .markdown-body table{ border-color: var(--gh-border); }
  .markdown-body th, .markdown-body td{ border-color: var(--gh-border); }
}



/* ================= 코드 블록 ================= */
.markdown-body pre {
  background: var(--gh-code-bg, #f6f8fa);
  border: 1px solid var(--gh-border, #d0d7de);
  border-radius: 8px;
  padding: 1rem;           /* 내부 패딩 */
  margin: 1.25rem 0;       /* 위/아래 여백 */
  line-height: 1.5;
  max-width: 100%;
  overflow-x: auto;        /* 내부만 가로 스크롤 */
}

/* 인라인 코드가 길 때도 줄바꿈 허용 */
.markdown-body p code,
.markdown-body li code,
.markdown-body td code {
  white-space: break-spaces;
  word-break: break-word;
}

/* MathJax 수식 폭 제한 */
.markdown-body .mjx-container { max-width: 100%; overflow-x: auto; }
.markdown-body .mjx-container[display="inline"] { display: inline-block; }

/* 코드 복사 버튼 */
.code-block { position: relative; }
.copy-btn {
  position: absolute; top: .5rem; right: .5rem;
  background: #f6f8fa;
  border: 1px solid #d0d7de;
  border-radius: 6px;
  font-size: .8rem;
  padding: .2rem .5rem;
  cursor: pointer;
  color: #24292f;
  transition: background .2s;
}
.copy-btn:hover { background: #eaeef2; }

/* ================= 반응형 (모바일: 1컬럼) ================= */
@media (max-width: 992px) {
  .course-layout { grid-template-columns: 1fr; }
  .course-toc { order: 2; }
  .course-doc { order: 1; }
}


/* ===== 강제 해제: TOC 고정(sticky/fixed) 죽이기 ===== */
/* .course-layout .course-toc,
.course-layout .course-toc .toc-box {
  position: static !important;
  top: auto !important;
  bottom: auto !important;
  left: auto !important;
  right: auto !important;
  align-self: auto !important;
  max-height: none !important;
  overflow: visible !important;
  padding-right: 0 !important;
  z-index: auto !important;
} */

/* ▼ 새 규칙: 네비바 바로 아래에 TOC를 고정(sticky) */
.course-layout.sticky-toc .course-toc {
  position: sticky;
  top: calc(var(--navbar-height) + 12px); /* fixed-top 네비바 아래에 착! */
  align-self: start;
  max-height: calc(100vh - var(--navbar-height) - 24px);
  overflow: auto;               /* 긴 목차 스크롤 */
  z-index: 1020;                /* 네비바(보통 1030)보단 낮게 */
  padding-right: .25rem;        /* 스크롤바 여백 */
}

.course-layout:not(.sticky-toc) .course-toc,
.course-layout:not(.sticky-toc) .course-toc .toc-box { position: static; }


.course-layout.sticky-toc .course-toc {
  position: sticky !important;
  top: calc(var(--navbar-height) + 12px) !important;
  max-height: calc(100vh - var(--navbar-height) - 24px);
  overflow: auto;
}


/* 모바일(<=992px)엔 본문 아래로 보내고 고정 해제 */
@media (max-width: 992px) {
  .course-layout.sticky-toc .course-toc {
    position: static;
    top: auto;
    max-height: none;
    overflow: visible;
  }
}



/* 모든 카운터 초기화 */
.markdown-body {
  counter-reset: h1;
}

.markdown-body h1 {
  counter-reset: h2;
}

.markdown-body h2 {
  counter-reset: h3;
}

.markdown-body h3 {
  counter-reset: h4;
}

.markdown-body h4 {
  counter-reset: h5;
}

.markdown-body h5 {
  counter-reset: h6;
}

/* 번호 붙이기 */
.markdown-body h1::before {
  counter-increment: h1;
  content: counter(h1) ". ";
  font-weight: 600;
  margin-right: .25em;
}

.markdown-body h2::before {
  counter-increment: h2;
  content: counter(h1) "." counter(h2) " ";
  font-weight: 600;
  margin-right: .25em;
}

.markdown-body h3::before {
  counter-increment: h3;
  content: counter(h1) "." counter(h2) "." counter(h3) " ";
  margin-right: .25em;
}

.markdown-body h4::before {
  counter-increment: h4;
  content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " ";
  margin-right: .25em;
}

/* h3 이하 항목 숨기기 (기본) */
.course-toc ul ul ul {
  display: none;
}

/* 펼친 상태 */
.course-toc ul ul ul.show {
  display: block;
}

/* 토글 버튼 스타일 */
.toc-toggle {
  font-size: 0.8rem;
  margin-left: 5px;
  cursor: pointer;
  color: #0969da;
}


/* === h1: 챕터 제목 === */
.markdown-body h1 {
  border: none;
  padding: .6rem 1rem;
  margin: 2rem 0 1.5rem;
  font-weight: 700;
  font-size: 2rem;
  line-height: 1.4;

  border-left: 8px solid #023e8a; /* 진한 네이비 블루 */
  background: #f0f4f8;           /* 차분한 블루 그레이 배경 */
  border-radius: 6px;
}

/* === h2: 섹션 제목 === */
.markdown-body h2 {
  border: none;
  padding: .4rem .75rem;
  margin: 1.5rem 0 .75rem;
  font-weight: 600;
  font-size: 1.5rem;
  line-height: 1.4;

  border-left: 6px solid #0366d6; /* GitHub 블루 */
  background: #f9fafb;           /* 밝은 회색 */
  border-radius: 4px;
}

/* === h3: 하위 섹션 ===
   → 좌측 라인은 빼고, 글자색/크기로만 구분 */
.markdown-body h3 {
  border: none;
  margin: 1.25rem 0 .5rem;
  font-weight: 600;
  font-size: 1.25rem;
  line-height: 1.4;
  color: #222; /* 본문보다 약간 진한 톤 */
}

/* === h4: 소주제 ===
   → 밑줄만 얇게 */
.markdown-body h4 {
  border: none;
  border-bottom: 1px solid #e1e4e8;
  padding-bottom: .25rem;
  margin: 1rem 0 .5rem;
  font-weight: 600;
  font-size: 1.1rem;
  line-height: 1.4;
}

/* === h5: 더 작은 소제목 ===
   → 점선 밑줄 + 살짝 연한 글자색 */
.markdown-body h5 {
  border: none;
  border-bottom: 1px dashed #e5e7eb;
  padding-bottom: .2rem;
  margin: .75rem 0 .4rem;
  font-weight: 500;
  font-size: 1rem;
  line-height: 1.4;
  color: #444;
}

/* === h6: 최소 강조 ===
   → 색만 살짝 연하게 */
.markdown-body h6 {
  border: none;
  margin: .5rem 0 .3rem;
  font-weight: 500;
  font-size: .95rem;
  line-height: 1.4;
  color: #666;
}

/* TOC 플로팅 버튼 */
.toc-fab {
  position: fixed;
  bottom: 50px;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: #0366d6;
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  text-decoration: none;
  box-shadow: 0 4px 8px rgba(0,0,0,0.25);
  z-index: 5000;
  transition: transform 0.5s, background 0.2s;
}
.toc-fab:hover {
  transform: translateY(-30px);
  background: #024b9c;
}
.toc-fab span {
  font-size: 0.7rem;
  margin-top: 2px;
}

#toc {
  scroll-margin-top: calc(var(--navbar-height) + 200px);
}

@media (max-width: 992px) {
  .toc-fab {
    display: none;
  }
}