/* ===========================================================================
   Godot 2D 강좌 사이트 스타일
   대상 화면 3종을 명시적으로 다룬다.
     1) 폴드 커버(접음)   ~ 400~520px  : 서랍 목차, 1단, 압축된 타이포
     2) 폴드 메인(펼침)   ~ 520~900px  : 넓은 본문, 2단 카드, 서랍 목차(넓게)
     3) 데스크톱/DeX      ~ 900px 이상 : 고정 사이드바 + 본문
   접었다 펴는 동안 레이아웃이 튀지 않도록 고정폭(px) 컨테이너를 쓰지 않는다.
   =========================================================================== */

:root {
  --fs: 16.5px;

  --bg: #ffffff;
  --bg-2: #f5f7fa;
  --bg-3: #eceff4;
  --fg: #1a1f27;
  --fg-2: #5a6472;
  --fg-3: #8b95a4;
  --line: #e2e7ee;
  --line-2: #cfd6e0;
  --accent: #3c8ce7;
  --accent-fg: #1f6fd0;
  --accent-bg: #eaf3fd;
  --ok: #2f9e6b;
  --warn: #d1741a;
  --warn-bg: #fdf3e7;
  --danger: #d2453f;
  --danger-bg: #fdeceb;
  --code-bg: #f4f6f9;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 6px 20px rgba(16, 24, 40, .06);

  --t-kw: #a13ba8;
  --t-ty: #1f7a8c;
  --t-st: #227d4a;
  --t-cm: #8b95a4;
  --t-nu: #b3641b;
  --t-fn: #2f6fd0;
  --t-an: #a1662f;

  --top-h: 52px;
  --side-w: 268px;
}

:root[data-theme='dark'],
:root[data-theme='auto'] {
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0e1116;
    --bg-2: #151a21;
    --bg-3: #1c222b;
    --fg: #e5eaf1;
    --fg-2: #9aa5b4;
    --fg-3: #6d7887;
    --line: #232a34;
    --line-2: #313a47;
    --accent: #62a8f0;
    --accent-fg: #8cc0f7;
    --accent-bg: #14263a;
    --ok: #4cc38a;
    --warn: #e0a35e;
    --warn-bg: #2a2015;
    --danger: #f0736c;
    --danger-bg: #2c1717;
    --code-bg: #141a22;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 28px rgba(0, 0, 0, .35);

    --t-kw: #d78ade;
    --t-ty: #5fc8d8;
    --t-st: #7fd39b;
    --t-cm: #6d7887;
    --t-nu: #e0a35e;
    --t-fn: #78b4f5;
    --t-an: #d8a06a;
  }
}

:root[data-theme='dark'] {
  --bg: #0e1116;
  --bg-2: #151a21;
  --bg-3: #1c222b;
  --fg: #e5eaf1;
  --fg-2: #9aa5b4;
  --fg-3: #6d7887;
  --line: #232a34;
  --line-2: #313a47;
  --accent: #62a8f0;
  --accent-fg: #8cc0f7;
  --accent-bg: #14263a;
  --ok: #4cc38a;
  --warn: #e0a35e;
  --warn-bg: #2a2015;
  --danger: #f0736c;
  --danger-bg: #2c1717;
  --code-bg: #141a22;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 28px rgba(0, 0, 0, .35);
  --t-kw: #d78ade;
  --t-ty: #5fc8d8;
  --t-st: #7fd39b;
  --t-cm: #6d7887;
  --t-nu: #e0a35e;
  --t-fn: #78b4f5;
  --t-an: #d8a06a;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scroll-padding-top: calc(var(--top-h) + 12px); }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: var(--fs)/1.75 'Pretendard', -apple-system, BlinkMacSystemFont, 'Apple SD Gothic Neo',
    'Noto Sans KR', 'Malgun Gothic', system-ui, sans-serif;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

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

.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; z-index: 100; background: var(--bg); padding: 8px 12px; border-radius: 8px; }

/* ───────────────────────────── 상단 바 ───────────────────────────── */

.top {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 6px;
  height: calc(var(--top-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}

.icon-btn {
  flex: none;
  display: grid; place-items: center;
  width: 38px; height: 38px;
  border: 0; border-radius: 10px;
  background: transparent; color: var(--fg-2);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-3); color: var(--fg); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.brand { display: flex; align-items: center; gap: 8px; min-width: 0; color: var(--fg); font-weight: 700; }
.brand:hover { text-decoration: none; }
.brand-mark {
  flex: none; display: grid; place-items: center;
  width: 26px; height: 26px; border-radius: 7px;
  background: linear-gradient(140deg, #4b8bf4, #2f6fd0);
  color: #fff; font-size: .72rem; font-weight: 800; letter-spacing: -.02em;
}
.brand-txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .95rem; }
.top-right { margin-left: auto; display: flex; gap: 2px; }

/* ───────────────────────────── 레이아웃 ───────────────────────────── */

.app { display: block; }

main {
  min-width: 0;
  padding: 18px max(14px, env(safe-area-inset-right)) 72px max(14px, env(safe-area-inset-left));
}

.lesson, .home { max-width: 46rem; margin: 0 auto; }

/* 서랍(커버·펼침 공통, 데스크톱에서 해제) */
.side {
  position: fixed; z-index: 45;
  top: calc(var(--top-h) + env(safe-area-inset-top));
  bottom: 0; left: 0;
  width: min(86vw, 320px);
  background: var(--bg-2);
  border-right: 1px solid var(--line);
  overflow-y: auto; overscroll-behavior: contain;
  transform: translateX(-102%);
  transition: transform .22s cubic-bezier(.32, .72, 0, 1);
  padding-bottom: 40px;
}
body.nav-open .side { transform: none; }
.scrim {
  position: fixed; inset: calc(var(--top-h) + env(safe-area-inset-top)) 0 0 0;
  z-index: 44; background: rgba(6, 10, 16, .45);
  opacity: 0; transition: opacity .22s;
}
body.nav-open .scrim { opacity: 1; }
@media (prefers-reduced-motion: reduce) { .side, .scrim { transition: none; } }

/* ───────────────────────────── 목차 ───────────────────────────── */

.toc { padding: 14px 12px 0; font-size: .9rem; }
.toc-home {
  display: block; padding: 8px 10px; margin-bottom: 10px;
  border: 1px solid var(--line-2); border-radius: 10px;
  color: var(--fg-2); font-weight: 600; text-align: center;
}
.toc-home:hover { text-decoration: none; background: var(--bg-3); color: var(--fg); }
.toc-part {
  display: flex; align-items: center; gap: 6px;
  margin: 16px 0 6px; padding: 0 6px;
  font-size: .74rem; font-weight: 800; letter-spacing: .04em;
  color: var(--fg-3); text-transform: uppercase;
}
.toc-part-id {
  display: inline-grid; place-items: center;
  min-width: 22px; height: 18px; padding: 0 5px;
  border-radius: 5px; background: var(--bg-3); color: var(--fg-2);
  font-size: .68rem;
}
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc li a {
  display: flex; align-items: baseline; gap: 8px;
  padding: 7px 8px; border-radius: 8px;
  color: var(--fg-2); line-height: 1.45;
}
.toc li a:hover { background: var(--bg-3); color: var(--fg); text-decoration: none; }
.toc li a b { flex: none; width: 1.5em; text-align: right; color: var(--fg-3); font-variant-numeric: tabular-nums; font-weight: 600; }
.toc li a span { flex: 1; min-width: 0; }
.toc li.on a { background: var(--accent-bg); color: var(--accent-fg); font-weight: 700; }
.toc li.on a b { color: var(--accent-fg); }
.toc li.soon a { opacity: .55; }
.done-dot { flex: none; width: 7px; height: 7px; border-radius: 50%; background: transparent; }
.is-done .done-dot, li.is-done > a .done-dot { background: var(--ok); }

/* ───────────────────────────── 홈 ───────────────────────────── */

.hero { padding: 8px 0 22px; border-bottom: 1px solid var(--line); margin-bottom: 22px; }
.kicker { margin: 0 0 8px; color: var(--accent-fg); font-size: .8rem; font-weight: 700; letter-spacing: .04em; }
.hero h1 { margin: 0 0 8px; font-size: 1.72rem; line-height: 1.28; letter-spacing: -.02em; }
.hero-sub { margin: 0 0 14px; color: var(--fg-2); font-size: 1.02rem; }
.hero-desc { margin: 0 0 18px; color: var(--fg-2); font-size: .95rem; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 18px; }
.cta {
  display: inline-block; padding: 11px 18px; border-radius: 10px;
  background: var(--accent-fg); color: #fff; font-weight: 700; font-size: .95rem;
}
.cta:hover { text-decoration: none; filter: brightness(1.06); }
.cta.ghost { background: transparent; color: var(--fg); border: 1px solid var(--line-2); }

.progress { display: flex; align-items: center; gap: 10px; font-size: .82rem; color: var(--fg-2); }
.bar { flex: 1; height: 6px; border-radius: 99px; background: var(--bg-3); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--ok); transition: width .3s; }

.notice {
  margin: 0 0 26px; padding: 12px 14px;
  border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: var(--radius); background: var(--bg-2);
  font-size: .88rem; color: var(--fg-2);
}
.notice b { color: var(--fg); }

.part { margin: 0 0 30px; }
.part-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.part-head h2 { margin: 0; font-size: 1.14rem; letter-spacing: -.01em; }
.pid {
  display: inline-grid; place-items: center; min-width: 30px; height: 22px; padding: 0 7px;
  border-radius: 6px; background: var(--accent-bg); color: var(--accent-fg);
  font-size: .72rem; font-weight: 800;
}
.part-count { margin-left: auto; color: var(--fg-3); font-size: .8rem; }
.part-goal { margin: 0 0 12px; color: var(--fg-2); font-size: .89rem; }

.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.cards a {
  position: relative;
  display: grid; grid-template-columns: 2.1rem 1fr auto; align-items: baseline;
  gap: 4px 10px;
  padding: 12px 13px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg-2); color: var(--fg);
}
.cards a:hover { text-decoration: none; border-color: var(--accent); background: var(--bg-3); }
.cards .n { grid-row: 1 / 3; font-size: 1.1rem; font-weight: 800; color: var(--fg-3); font-variant-numeric: tabular-nums; }
.cards .t { font-weight: 700; font-size: .97rem; }
.cards .s { grid-column: 2 / 4; color: var(--fg-2); font-size: .85rem; line-height: 1.55; }
.cards .done-dot { align-self: center; }
.capstone .part-head h2 { color: var(--fg-2); }

/* ───────────────────────────── 강의 본문 ───────────────────────────── */

.crumb { display: flex; align-items: center; gap: 7px; color: var(--fg-3); font-size: .82rem; margin-bottom: 8px; }
.lesson h1 { margin: 0 0 10px; font-size: 1.55rem; line-height: 1.32; letter-spacing: -.02em; }
.lnum {
  display: inline-block; margin-right: 8px; padding: 2px 8px;
  border-radius: 6px; background: var(--accent-fg); color: #fff;
  font-size: .78rem; font-weight: 800; vertical-align: .18em;
}
.lead { margin: 0 0 16px; color: var(--fg-2); font-size: 1rem; }

.inpage {
  margin: 0 0 24px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg-2); font-size: .88rem;
}
.inpage > summary { padding: 10px 14px; cursor: pointer; font-weight: 700; color: var(--fg-2); }
.inpage ol { margin: 0; padding: 0 16px 12px 34px; color: var(--fg-2); }
.inpage li { margin: 3px 0; }

.md h2 {
  margin: 34px 0 12px; padding-top: 14px; border-top: 1px solid var(--line);
  font-size: 1.25rem; line-height: 1.4; letter-spacing: -.015em;
}
.md h3 { margin: 24px 0 8px; font-size: 1.06rem; line-height: 1.45; }
.md h4 { margin: 20px 0 6px; font-size: .97rem; color: var(--fg-2); }
.md p { margin: 0 0 14px; }
.md ul, .md ol { margin: 0 0 14px; padding-left: 1.35em; }
.md li { margin: 5px 0; }
.md li > ul, .md li > ol { margin: 5px 0; }
.md strong { font-weight: 700; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 28px 0; }
.md blockquote {
  margin: 0 0 16px; padding: 2px 0 2px 14px;
  border-left: 3px solid var(--line-2); color: var(--fg-2);
}
.md img { max-width: 100%; height: auto; border-radius: 10px; }
.anchor { margin-left: .4em; color: var(--fg-3); opacity: 0; font-weight: 400; text-decoration: none; }
.md h2:hover .anchor, .md h3:hover .anchor { opacity: 1; }

.md :not(pre) > code {
  padding: .12em .38em; border-radius: 5px;
  background: var(--bg-3); border: 1px solid var(--line);
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .86em; word-break: break-all;
}

.code {
  position: relative; margin: 0 0 18px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--code-bg); overflow: hidden;
}
.code-lang {
  position: absolute; top: 0; right: 0;
  padding: 3px 9px; border-radius: 0 var(--radius) 0 8px;
  background: var(--bg-3); color: var(--fg-3);
  font-size: .68rem; font-weight: 700; letter-spacing: .05em; text-transform: uppercase;
}
.code pre { margin: 0; padding: 14px 14px; overflow-x: auto; overscroll-behavior-x: contain; }
.code code {
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .82rem; line-height: 1.65; white-space: pre; tab-size: 4;
}
.t-kw { color: var(--t-kw); font-weight: 600; }
.t-ty { color: var(--t-ty); }
.t-st { color: var(--t-st); }
.t-cm { color: var(--t-cm); font-style: italic; }
.t-nu { color: var(--t-nu); }
.t-fn { color: var(--t-fn); }
.t-an { color: var(--t-an); }

.table-wrap { overflow-x: auto; margin: 0 0 18px; border: 1px solid var(--line); border-radius: var(--radius); }
.md table { border-collapse: collapse; width: 100%; font-size: .87rem; }
.md th, .md td { padding: 9px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.md th { background: var(--bg-2); font-weight: 700; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }

/* ───────────────────────────── 콜아웃 ───────────────────────────── */

.cb {
  margin: 0 0 18px; padding: 13px 15px;
  border: 1px solid var(--line); border-left: 3px solid var(--line-2);
  border-radius: var(--radius); background: var(--bg-2);
}
.cb > :last-child { margin-bottom: 0; }
.cb-h { margin: 0 0 7px; font-size: .8rem; font-weight: 800; letter-spacing: .03em; color: var(--fg-3); }
.cb-concept { border-left-color: var(--accent); background: var(--accent-bg); }
.cb-concept .cb-h { color: var(--accent-fg); }
.cb-analogy { border-left-color: #8b5cf6; }
.cb-analogy .cb-h { color: #8b5cf6; }
.cb-goal { border-left-color: var(--ok); }
.cb-goal .cb-h { color: var(--ok); }
.cb-warn { border-left-color: var(--warn); background: var(--warn-bg); }
.cb-warn .cb-h { color: var(--warn); }
.cb-err { border-left-color: var(--danger); background: var(--danger-bg); }
.cb-err .cb-h { color: var(--danger); }
.cb-try { border-left-color: var(--accent); border-style: dashed; }
.cb-try .cb-h { color: var(--accent-fg); }
.cb-check { border-left-color: var(--ok); background: var(--bg-2); }
.cb-check .cb-h { color: var(--ok); }

details.cb-deep { padding: 0; border-left-color: var(--fg-3); background: var(--bg-2); }
details.cb-deep > summary {
  padding: 11px 15px; cursor: pointer;
  font-size: .88rem; font-weight: 700; color: var(--fg-2);
  list-style: none;
}
details.cb-deep > summary::-webkit-details-marker { display: none; }
details.cb-deep > summary::before { content: '▸ '; color: var(--fg-3); }
details.cb-deep[open] > summary::before { content: '▾ '; }
details.cb-deep[open] > summary { border-bottom: 1px solid var(--line); }
details.cb-deep > :not(summary) { margin-left: 15px; margin-right: 15px; }
details.cb-deep > :nth-child(2) { margin-top: 12px; }
details.cb-deep > :last-child { margin-bottom: 12px; }

.cb-roles { border-left-color: #d18f2c; padding: 13px 15px; }
.cb-roles ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.cb-roles li { margin: 0; padding-left: 0; font-size: .9rem; }
.cb-roles li strong:first-child {
  display: inline-block; min-width: 4.6em; margin-right: 6px; padding: 1px 7px;
  border-radius: 5px; background: var(--bg-3); color: var(--fg-2);
  font-size: .76rem; font-weight: 800; text-align: center; vertical-align: .1em;
}

/* 체크리스트 */
.md li:has(> input[type='checkbox']) { list-style: none; margin-left: -1.2em; }
.md input[type='checkbox'] {
  width: 1.05em; height: 1.05em; margin-right: .45em;
  vertical-align: -.15em; accent-color: var(--ok); cursor: pointer;
}
.md li.ck-on { color: var(--fg-3); text-decoration: line-through; text-decoration-color: var(--line-2); }

/* ───────────────────────────── 하단 ───────────────────────────── */

.done-bar { margin: 34px 0 18px; }
.done-btn {
  width: 100%; padding: 12px; border: 1px solid var(--line-2); border-radius: 10px;
  background: var(--bg-2); color: var(--fg-2); font: inherit; font-size: .92rem; font-weight: 700;
  cursor: pointer;
}
.done-btn:hover { background: var(--bg-3); }
.done-btn.on { border-color: var(--ok); color: var(--ok); background: transparent; }

.pager { display: grid; gap: 8px; margin-top: 10px; }
.pg {
  display: block; padding: 12px 14px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg-2); color: var(--fg);
}
.pg:hover { text-decoration: none; border-color: var(--accent); }
.pg span { display: block; font-size: .76rem; color: var(--fg-3); margin-bottom: 2px; }
.pg b { font-size: .93rem; }
.pg.next { text-align: right; }
.pg.empty { display: none; }

/* ===========================================================================
   2) 폴드 메인 화면(펼침) — 대략 520px 이상
   =========================================================================== */
@media (min-width: 520px) {
  :root { --fs: 17px; --top-h: 56px; }
  main { padding-left: max(22px, env(safe-area-inset-left)); padding-right: max(22px, env(safe-area-inset-right)); padding-top: 24px; }
  .hero h1 { font-size: 2rem; }
  .lesson h1 { font-size: 1.75rem; }
  .cards { grid-template-columns: 1fr 1fr; }
  .cards .s { font-size: .84rem; }
  .pager { grid-template-columns: 1fr 1fr; }
  .pg.empty { display: block; border: 0; background: transparent; }
  .code code { font-size: .84rem; }
  .cb-roles ul { grid-template-columns: 1fr; }
}

/* 펼친 화면은 가로가 넉넉하지만 세로가 짧다(거의 정사각).
   상단 바를 더 낮추고 제목 여백을 줄여 한 화면에 더 담는다. */
@media (min-width: 520px) and (max-height: 900px) {
  :root { --top-h: 50px; }
  .md h2 { margin-top: 28px; }
}

/* ===========================================================================
   3) 데스크톱 · DeX · 폴드 가로 — 900px 이상: 사이드바 고정
   =========================================================================== */
@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: var(--side-w) minmax(0, 1fr); }
  .side {
    position: sticky; top: calc(var(--top-h) + env(safe-area-inset-top));
    height: calc(100dvh - var(--top-h) - env(safe-area-inset-top));
    width: auto; transform: none; transition: none;
    background: var(--bg);
  }
  .nav-toggle { display: none; }
  .scrim { display: none !important; }
  main { padding: 30px 32px 96px; }
  .lesson, .home { max-width: 47rem; margin: 0; }
}

@media (min-width: 1280px) {
  :root { --side-w: 300px; }
  main { padding: 34px 56px 110px; }
  .cards { grid-template-columns: repeat(2, 1fr); }
}

/* 접었을 때 커버 화면은 폭이 아주 좁다. 한 줄이라도 더 읽히게 압축한다. */
@media (max-width: 400px) {
  :root { --fs: 16px; }
  main { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .hero h1 { font-size: 1.55rem; }
  .lesson h1 { font-size: 1.42rem; }
  .code pre { padding: 12px 11px; }
  .code code { font-size: .78rem; }
  .cb { padding: 11px 12px; }
  .brand-txt { font-size: .88rem; }
}

/* 폴드를 가로로 눕히거나 플렉스 모드로 접었을 때(세로가 매우 짧음) */
@media (max-height: 480px) {
  :root { --top-h: 46px; }
  .hero { padding-top: 0; }
}
