:root {
  --bg: #fbfaf7;
  --paper: #ffffff;
  --ink: #1d2433;
  --ink2: #4a5468;
  --muted: #7a8396;
  --line: #e4e1da;
  --accent: #2e86de;
  --accent-ink: #ffffff;
  --good: #16a05a;
  --bad: #d64545;
  --stage: #fdfcf9;
  --grid: #e9edf3;
  --node-stroke: #1d2433;
  --t0: #f1efe9; --t1: #dbe9fb; --t2: #d9f2e3; --t3: #ece0f7; --t4: #fde2cf; --t5: #e5e5e5;
  --rule-bg: #fff6e0; --rule-line: #f0c45a;
  --shadow: 0 1px 2px rgba(20, 30, 50, .06), 0 4px 16px rgba(20, 30, 50, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151c; --paper: #1a1e27; --ink: #e8eaf0; --ink2: #b8bfcd; --muted: #8790a2; --line: #2c3240;
    --accent: #5aa2ec; --stage: #161a22; --grid: #232836; --node-stroke: #e8eaf0;
    --t0: #2a2f3b; --t1: #1f3550; --t2: #1d3d2c; --t3: #33264a; --t4: #4a3020; --t5: #33363d;
    --rule-bg: #2b2616; --rule-line: #8a6d1f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12151c; --paper: #1a1e27; --ink: #e8eaf0; --ink2: #b8bfcd; --muted: #8790a2; --line: #2c3240;
  --accent: #5aa2ec; --stage: #161a22; --grid: #232836; --node-stroke: #e8eaf0;
  --t0: #2a2f3b; --t1: #1f3550; --t2: #1d3d2c; --t3: #33264a; --t4: #4a3020; --t5: #33363d;
  --rule-bg: #2b2616; --rule-line: #8a6d1f;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 64px; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.75 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  word-break: keep-all; overflow-wrap: anywhere; -webkit-text-size-adjust: 100%;
}
i { font-family: "Times New Roman", "STIX Two Text", Georgia, serif; font-size: 1.08em; }
sub, sup { font-size: .72em; line-height: 0; }
a { color: var(--accent); }
b { font-weight: 650; }

/* 머리띠 */
.top {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--ink); text-decoration: none; font-size: 16px; }
.brand svg { width: 34px; height: 20px; }
.brand svg line { stroke: #e4572e; stroke-width: 3; stroke-linecap: round; }
.brand svg rect { fill: #2e86de; }
.brand svg circle { fill: #16a05a; }
.toc-btn { margin-left: auto; font: inherit; font-size: 14px; padding: 4px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--paper); color: var(--ink); cursor: pointer; }
.progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; }
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .1s; }

.toc { position: fixed; z-index: 19; top: 52px; right: 12px; display: none; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 8px 4px; max-height: calc(100vh - 70px); overflow: auto; }
.toc.open { display: block; }
.toc ol { margin: 0; padding: 0 12px 0 34px; font-size: 15px; line-height: 2; }
.toc a { color: var(--ink2); text-decoration: none; }
.toc a.cur { color: var(--accent); font-weight: 650; }
@media (min-width: 1240px) {
  .toc-btn { display: none; }
  .toc { display: block; left: max(16px, calc(50vw - 640px)); right: auto; top: 90px; width: 220px; border: none; box-shadow: none; background: transparent; }
}

main { max-width: 760px; margin: 0 auto; padding: 0 16px 80px; }
.hero { padding: 56px 0 24px; }
.kicker { color: var(--accent); font-weight: 650; letter-spacing: .02em; margin: 0 0 6px; font-size: 15px; }
h1 { font-size: clamp(36px, 7vw, 56px); line-height: 1.15; margin: 0 0 24px; letter-spacing: -.02em; }
.lead { font-size: 18px; color: var(--ink2); }
.howto { display: grid; gap: 8px; margin-top: 22px; font-size: 15px; color: var(--ink2); }
.howto .k { display: inline-block; min-width: 3.2em; font-weight: 700; color: var(--ink); }

.lesson { padding-top: 40px; border-top: 1px solid var(--line); margin-top: 40px; }
h2 { font-size: 26px; line-height: 1.35; margin: 0 0 14px; letter-spacing: -.01em; display: flex; align-items: baseline; gap: 12px; }
h2 .num { flex: none; display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; background: var(--ink); color: var(--bg); font-size: 17px; transform: translateY(-3px); }
.defs { padding-left: 1.2em; }
.defs li { margin: 4px 0; }
.rule { background: var(--rule-bg); border-left: 4px solid var(--rule-line); padding: 12px 16px; border-radius: 6px; margin: 18px 0; }
.note { color: var(--ink2); font-size: 15.5px; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.ix { font-family: "Times New Roman", Georgia, serif; font-style: italic; font-weight: 700; }
.ix.c0 { color: #e4572e; }

/* 위젯 */
.widget { margin: 20px -8px; background: var(--paper); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 12px; }
.widget.wide { margin-left: -8px; margin-right: -8px; }
.widget.wide svg.dg { max-width: none; }
@media (min-width: 900px) { .widget { margin-left: -40px; margin-right: -40px; } .widget.wide { margin-left: -90px; margin-right: -90px; } }
.w-tools { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; align-items: center; }
.btn { font: inherit; font-size: 14px; line-height: 1.3; padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); cursor: pointer; touch-action: manipulation; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.small { font-size: 13px; padding: 5px 10px; }
.btn.right { background: var(--good); border-color: var(--good); color: #fff; }
.btn.wrong { border-color: var(--bad); color: var(--bad); }
.sel { font-size: 14px; color: var(--ink2); display: inline-flex; gap: 6px; align-items: center; }
.sel select { font: inherit; padding: 5px 6px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); }
.w-stage { background: var(--stage); border-radius: 10px; border: 1px dashed var(--line); }
.w-msg { min-height: 0; max-height: 0; overflow: hidden; transition: max-height .2s, padding .2s; font-size: 14px; color: var(--ink); background: var(--rule-bg); border-radius: 8px; padding: 0 10px; margin-top: 6px; }
.w-msg.show { max-height: 120px; padding: 6px 10px; }
.w-cap { margin-top: 10px; font-size: 15.5px; }
.w-goals { margin-top: 10px; }
.goal-h { font-size: 13px; font-weight: 700; color: var(--muted); letter-spacing: .05em; }
.goals { list-style: none; padding: 0; margin: 4px 0 0; }
.goal { display: flex; gap: 10px; align-items: flex-start; padding: 6px 0; font-size: 15.5px; }
.goal .chk { flex: none; width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--line); margin-top: 4px; position: relative; transition: all .3s; }
.goal.done .chk { background: var(--good); border-color: var(--good); }
.goal.done .chk::after { content: ""; position: absolute; left: 5px; top: 1px; width: 5px; height: 10px; border: solid #fff; border-width: 0 2.5px 2.5px 0; transform: rotate(45deg); }
.goal.done > span:last-child { color: var(--ink2); }
.goal.just .chk { animation: pop .6s; }
@keyframes pop { 40% { transform: scale(1.5); } }

/* SVG 도식 */
svg.dg { display: block; width: 100%; max-width: 640px; margin: 0 auto; height: auto; touch-action: none; user-select: none; -webkit-user-select: none; color: var(--ink); }
.dg-edge, .dg-leg { fill: none; stroke-width: 3.2; stroke-linecap: round; }
.dg-hit { fill: none; stroke: transparent; stroke-width: 22; cursor: grab; pointer-events: stroke; }
.dg-hit.locked { cursor: default; }
.dg-tiphit { fill: transparent; cursor: crosshair; }
.dg-tiphit:hover { fill: color-mix(in srgb, var(--accent) 14%, transparent); }
.dg-tip { stroke: var(--paper); stroke-width: 1.5; }
.dg-node { cursor: grab; }
.dg-node > :first-child { stroke: var(--node-stroke); stroke-width: 2; }
.dg-node.tone0 > :first-child { fill: var(--t0); }
.dg-node.tone1 > :first-child { fill: var(--t1); }
.dg-node.tone2 > :first-child { fill: var(--t2); }
.dg-node.tone3 > :first-child { fill: var(--t3); }
.dg-node.tone4 > :first-child { fill: var(--t4); }
.dg-node.tone5 > :first-child { fill: var(--t5); stroke-dasharray: 4 3; }
.dg-node.copy > :first-child { fill: var(--node-stroke); stroke: none; }
.dg-hitnode { fill: transparent; stroke: none; }
.dg-node.tappable { cursor: pointer; }
.dg-node.sel > :first-child { stroke: var(--accent); stroke-width: 3.5; }
.dg-name { font: italic 700 19px "Times New Roman", Georgia, serif; fill: var(--ink); pointer-events: none; }
.dg-name.small { font-size: 14px; }
.dg-ix { font: italic 700 15px "Times New Roman", Georgia, serif; paint-order: stroke; stroke: var(--stage); stroke-width: 4px; pointer-events: none; }
.dg-dim { font: 600 11px system-ui, sans-serif; fill: var(--muted); paint-order: stroke; stroke: var(--stage); stroke-width: 3px; pointer-events: none; }
.dg-rubber { fill: none; stroke: var(--muted); stroke-width: 2.5; stroke-dasharray: 6 5; pointer-events: none; }
.dg-rubber.snap { stroke: var(--good); stroke-dasharray: none; stroke-width: 3.5; }
svg.dg.wiring .dg-tiphit { fill: color-mix(in srgb, var(--good) 10%, transparent); }

/* 읽어 주기 */
.ro { margin-top: 10px; }
.ro-formula { font-size: 20px; line-height: 1.9; overflow-x: auto; white-space: nowrap; padding: 2px 0; }
.ro-formula sub { font-size: .66em; }
.fac { font-family: "Times New Roman", Georgia, serif; font-style: italic; margin-right: 2px; }
.fac.delta { color: var(--ink2); }
.fac.res { font-weight: 700; }
.sigma { font-family: "Times New Roman", Georgia, serif; font-size: 1.25em; margin-right: 3px; }
.sigma sub { font-size: .55em; }
.eq { margin: 0 4px; }
.ro-desc { font-size: 15px; color: var(--ink2); margin: 2px 0 8px; }
.g-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; }
.g-row figure { margin: 0; }
.g-row figcaption { font-size: 16px; margin-bottom: 2px; }
.g-dot, .g-eq { font-size: 20px; color: var(--muted); }
.slices { display: flex; flex-wrap: wrap; gap: 8px; }
.grid { border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 14px; }
.grid td { min-width: 34px; height: 30px; text-align: center; border: 1px solid var(--grid); cursor: pointer; transition: box-shadow .15s; padding: 0 4px; }
.grid th.ax { font-weight: 400; font-size: 11px; color: var(--muted); padding: 0 4px; white-space: nowrap; }
.grid th.corner { text-align: left; }
.grid td.hl { box-shadow: inset 0 0 0 2.5px var(--ink); font-weight: 700; }
.grid.res td.hl { box-shadow: inset 0 0 0 3px #e4572e; }
.axnote { font-size: 12px; color: var(--muted); }
.hint { font-size: 13px; color: var(--muted); margin-top: 6px; }
.ro-expand { margin-top: 10px; font-family: "Times New Roman", Georgia, serif; font-size: 17px; overflow-x: auto; background: var(--bg); border-radius: 8px; padding: 6px 10px; }
.ro-expand:empty { display: none; }
.xline { white-space: nowrap; }
.xline .pad { display: inline-block; }
.ro-code { margin-top: 8px; font-size: 13px; overflow-x: auto; white-space: nowrap; }
.ro-code code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bg); border: 1px solid var(--line); padding: 3px 8px; border-radius: 6px; }

/* 퀴즈 */
.quiz { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 12px; margin: 16px 0; }
.qcard { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 10px; position: relative; }
.qnum { position: absolute; top: 8px; left: 12px; font-weight: 700; color: var(--muted); font-size: 13px; }
.qstage { background: var(--stage); border-radius: 8px; }
.qopts { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.qfb { font-size: 14px; margin-top: 6px; min-height: 1.4em; }
.qfb .f { display: block; font-size: 16px; overflow-x: auto; white-space: nowrap; }

/* 비용 */
.costlog { margin-top: 10px; font-size: 15px; }
.costlog .total { margin-top: 6px; font-size: 16px; }
table.cmp { border-collapse: collapse; margin: 10px 0 6px; font-size: 14px; font-variant-numeric: tabular-nums; }
table.cmp th, table.cmp td { border: 1px solid var(--line); padding: 4px 10px; text-align: right; }

/* 미분 항 */
.terms { display: grid; gap: 10px; margin-top: 10px; }
.term { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.term-h { font-size: 14px; color: var(--muted); }
.term-stage { max-width: 420px; }
.term-ro .ro-formula { font-size: 17px; }
.term-v { font-size: 15px; }
.termsum { margin-top: 10px; font-size: 16px; }
.termsum:empty { display: none; }

.refs li { margin: 8px 0; }
.foot { text-align: center; color: var(--muted); font-size: 13px; padding: 30px 16px 40px; border-top: 1px solid var(--line); }

@media (max-width: 520px) {
  body { font-size: 16px; }
  .widget { padding: 8px; margin-left: -8px; margin-right: -8px; }
  .ro-formula { font-size: 18px; }
  .grid td { min-width: 28px; height: 26px; font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
