:root {
  --bg: #FFF8EE;
  --paper: #FFFFFF;
  --ink: #2A2833;
  --muted: #6E6A78;
  --line: #ECE3D5;
  --soft: #F6F0E6;
  --blue: #3A7BD5;
  --blue-soft: #E4EEFB;
  --orange: #E8752A;
  --orange-soft: #FDEBDB;
  --green: #1F9D64;
  --green-soft: #DEF3E8;
  --red: #E5484D;
  --red-soft: #FDE4E4;
  --purple: #7B55D9;
  --purple-soft: #ECE5FB;
  --yellow: #F5B82E;
  --yellow-soft: #FFF3CF;
  --teal: #0F8F9A;
  --teal-soft: #DDF2F3;
  --pink: #D6457A;
  --pink-soft: #FBE3EC;
  --accent: var(--blue);
  --accent-soft: var(--blue-soft);
  --radius: 18px;
  --shadow: 0 1px 2px rgba(80, 55, 20, .05), 0 8px 24px rgba(80, 55, 20, .07);
  --font-title: "Jua", "Noto Sans KR", system-ui, sans-serif;
  --font-body: "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scrollbar-gutter: stable; }
body {
  /* 글자에 쓰는 진한 강조색. 페이지가 body에서 정한 --accent를 따르도록 body에 둔다(:root에 두면 파랑으로 굳는다) */
  --accent-ink: color-mix(in srgb, var(--accent) 80%, #000);
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
b, strong { font-weight: 700; }
[hidden] { display: none !important; }

/* ── 머리말 ─────────────────────────────── */
.topbar {
  max-width: 1320px;
  margin: 0 auto;
  padding: 18px 20px 14px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.back {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--paper);
  border: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
  font-weight: 700;
  font-size: 15px;
}
.back:hover { border-color: var(--accent); color: var(--accent); }
.page-title { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.page-title h1 {
  margin: 0;
  font-family: var(--font-title);
  font-weight: 400;
  font-size: clamp(26px, 3vw, 34px);
  line-height: 1.2;
}
.chip {
  display: inline-block;
  padding: 3px 11px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

/* ── 배치 ─────────────────────────────── */
.layout {
  max-width: 1320px;
  margin: 0 auto;
  padding: 0 20px 48px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 370px;
  gap: 20px;
  align-items: start;
}
@media (max-width: 980px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 520px) {
  .topbar, .layout { padding-left: 14px; padding-right: 14px; }
}

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 18px 20px;
  min-width: 0;
}
.card h2 {
  margin: 0 0 12px;
  font-family: var(--font-title);
  font-weight: 400;
  font-size: 20px;
  line-height: 1.3;
  display: flex;
  align-items: center;
  gap: 8px;
}
.card h3 {
  margin: 16px 0 8px;
  font-size: 15px;
  font-weight: 700;
}
.num {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-family: var(--font-title);
  flex: none;
}
.panel { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.stage { padding: 0; overflow: hidden; }
@media (min-width: 981px) {
  .stage { position: sticky; top: 12px; }
}
.stage-lead { margin: 0; padding: 16px 20px 4px; color: var(--muted); }
.stage svg.board { display: block; width: 100%; height: auto; max-height: 62vh; touch-action: none; }
.stage-bar {
  display: flex;
  align-items: center;
  gap: 12px 14px;
  padding: 12px 20px 16px;
  flex-wrap: wrap;
  border-top: 1px solid var(--line);
}
.stage-bar input[type=range] { flex: 1 1 180px; }
.spacer { flex: 1 1 auto; }
.bar-break { flex-basis: 100%; height: 0; }       /* 조작 막대 안에서 줄 바꾸기 */
.bar-label { font-size: 14px; font-weight: 700; color: var(--muted); white-space: nowrap; }
/* 여러 활동의 조작 막대를 한 칸에 겹쳐 두고 하나만 보인다: 활동을 바꿔도 무대 높이가 그대로다 */
.bars { display: grid; }
.bars > .stage-bar { grid-area: 1 / 1; align-content: flex-start; }
.bars > .off { visibility: hidden; }
.board text { font-family: var(--font-title); }
.lead-strong { color: var(--ink); font-weight: 700; }
/* 끌어서 옮기는 손잡이(KM.pointer): 끄는 동안 무대에 .dragging이 붙는다 */
.board .handle { cursor: grab; outline: none; }
.board .handle:focus-visible .ring { stroke: var(--ink); stroke-width: 4; }
.board .handle.locked { cursor: not-allowed; }
.board.dragging, .board.dragging .handle { cursor: grabbing; }
/* 지금 자리를 알려 주는 점(.pin)과 성공 폭죽(KM.burst) */
.board .pin { animation: pinPulse 1.4s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
@keyframes pinPulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.25); } }
.burst circle { animation: burst .9s cubic-bezier(.2, .8, .3, 1) forwards; }
@keyframes burst { from { transform: translate(0, 0); opacity: 1; } to { transform: translate(var(--dx), var(--dy)); opacity: 0; } }
/* 움직임을 줄이면 폭죽은 띄우지 않는다(애니메이션만 끄면 점들이 가운데 뭉친 채 남는다) */
@media (prefers-reduced-motion: reduce) {
  .board .pin { animation: none; }
  .burst { display: none; }
}
/* 화면이 낮으면 무대가 붙어 있을 때 아래쪽 조작 막대가 가려지므로 붙이지 않는다(더 높게 잡을 페이지는 따로 정한다) */
@media (min-width: 981px) and (max-height: 820px) {
  .stage { position: static; }
}
.canvas-wrap {
  position: relative;
  width: 100%;
  height: clamp(320px, 64vh, 640px);
  background: linear-gradient(#F7FAFE, #EEF3F9);
  touch-action: none;
}
.canvas-wrap canvas { display: block; width: 100%; height: 100%; }
@media (max-width: 980px) {
  .canvas-wrap { height: clamp(300px, 56vh, 520px); }
}
.hint {
  position: absolute;
  left: 12px;
  bottom: 12px;
  font-size: 13px;
  color: var(--muted);
  background: rgba(255, 255, 255, .88);
  padding: 4px 10px;
  border-radius: 999px;
  pointer-events: none;
}

/* ── 버튼 · 입력 ─────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 18px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-family: var(--font-title);
  font-size: 18px;
  cursor: pointer;
  transition: transform .08s, filter .15s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: var(--soft); color: var(--ink); }
.btn.small { min-height: 38px; font-size: 16px; padding: 6px 14px; }
.btn:disabled { opacity: .45; cursor: default; filter: none; transform: none; }
/* 글자가 바뀌는 버튼: 모든 글자를 겹쳐 두고 하나만 보여 폭이 변하지 않는다 (KM.swap) */
.swap { display: inline-grid; place-items: center; }
.swap > span { grid-area: 1 / 1; }
.swap > span:not(.on) { visibility: hidden; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  min-width: 46px;
  min-height: 42px;
  padding: 6px 12px;
  border-radius: 12px;
  border: 2px solid var(--line);
  background: #fff;
  cursor: pointer;
  font-weight: 700;
  line-height: 1.25;
  transition: background .12s, border-color .12s;
}
.seg button:hover { border-color: var(--accent); }
.seg button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.seg button:disabled { opacity: .4; cursor: default; }
.seg.fill button { flex: 1 1 0; }
.seg.grid2 { display: grid; grid-template-columns: 1fr 1fr; }
.seg button small { display: block; font-weight: 400; font-size: 12px; opacity: .85; }

.field-label { margin: 14px 0 6px; font-size: 14px; font-weight: 700; color: var(--muted); }

.btn:focus-visible, .seg button:focus-visible, .stepper-btn:focus-visible, .back:focus-visible, .toggle input:focus-visible, .reveal:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 2px;
}

input[type=range] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 30px;
  background: transparent;
  cursor: pointer;
  margin: 0;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 99px;
  background: linear-gradient(to right, var(--accent) var(--fill, 0%), #E7DFD2 var(--fill, 0%));
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -9px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--accent);
  box-shadow: 0 2px 6px rgba(0, 0, 0, .15);
}
input[type=range]::-moz-range-track { height: 8px; border-radius: 99px; background: #E7DFD2; }
input[type=range]::-moz-range-progress { height: 8px; border-radius: 99px; background: var(--accent); }
input[type=range]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--accent);
}
input[type=range]:focus-visible { outline: none; }
input[type=range]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 5px color-mix(in srgb, var(--accent) 30%, transparent); }
/* 쓸 수 없는 막대: 움직일 수 있을 것처럼 보이지 않게 흐리고 회색으로 */
input[type=range]:disabled { opacity: .4; cursor: not-allowed; filter: grayscale(1); }

.toggles { display: grid; gap: 10px; }
.toggle { display: flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.toggle input {
  appearance: none;
  -webkit-appearance: none;
  width: 42px;
  height: 24px;
  border-radius: 99px;
  background: #DDD5C8;
  position: relative;
  cursor: pointer;
  transition: background .15s;
  flex: none;
  margin: 0;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
}
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(18px); }
.toggle input:disabled { opacity: .45; cursor: default; }
.toggle:has(input:disabled) { color: var(--muted); cursor: default; }
.swatch { width: 14px; height: 14px; border-radius: 4px; display: inline-block; flex: none; }

/* ── 글 상자 ─────────────────────────────── */
.muted { color: var(--muted); }
.small { font-size: 14px; }
p { margin: 0 0 8px; }
.callout {
  border-radius: 14px;
  padding: 12px 14px;
  background: var(--yellow-soft);
  border: 1px solid #F1D98F;
  font-size: 15px;
}
.ctx .callout { margin-top: 12px; }
/* 자리는 그대로 두고 숨긴다(자리 잡기로 크기를 맞춘 칸) */
.vis-off { visibility: hidden; }
.callout .tag {
  display: inline-block;
  margin-right: 6px;
  padding: 0 8px;
  border-radius: 99px;
  background: var(--yellow);
  color: #5A3E00;
  font-family: var(--font-title);
  font-size: 14px;
}

.formula { display: grid; gap: 6px; font-size: 16px; }
.formula .line {
  padding: 7px 11px;
  border-radius: 10px;
  background: var(--soft);
  animation: pop .35s ease;
}
/* 아직 보여 주지 않은 줄은 빈 점선 칸으로 자리만 차지한다(outline이라 크기는 그대로) */
.formula .line.pending { color: transparent; background: transparent; border-color: transparent; outline: 1.5px dashed #E3D9C8; outline-offset: -1.5px; animation: none; }
.formula .line.pending * { visibility: hidden; }
.formula .result {
  background: var(--accent-soft);
  border: 2px solid var(--accent);
  font-weight: 700;
}
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; }
.stat {
  background: var(--soft);
  border-radius: 12px;
  padding: 8px 10px;
  text-align: center;
  border: 0;
}
.stat .v { display: block; font-family: var(--font-title); font-size: 28px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat .k { display: block; font-size: 13px; color: var(--muted); }
.reveal { cursor: pointer; transition: background .15s; }
.reveal:hover { background: var(--accent-soft); }
.reveal .v { color: var(--accent); }

table.data { width: 100%; border-collapse: collapse; font-size: 15px; font-variant-numeric: tabular-nums; table-layout: fixed; }
table.data th, table.data td { padding: 6px 6px; text-align: center; border-bottom: 1px solid var(--line); }
table.data th { font-size: 13px; color: var(--muted); font-weight: 700; }
table.data tr.now td { background: var(--accent-soft); }
table.data tr.rule td { font-weight: 700; color: var(--accent); background: var(--accent-soft); }
/* 설명 카드의 표(table.data.sum): 첫 칸은 이름, 아직 모르는 칸(.q)은 흐리게 */
table.sum td:first-child { text-align: left; font-weight: 700; }
table.sum td.q { color: #C9C1B4; }

/* ── 설명 카드의 도우미(KM.card) ─────────────── */
/* 알아낸 것(KM.card.found) */
.formula.found .line { font-size: 14px; padding: 5px 10px; }
/* 앞에서 가져오는 것: 다른 단계·페이지로 가는 알약 */
.carry {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0 0 10px;
  padding: 3px 12px;
  border-radius: 999px;
  border: 1.5px dashed var(--accent);
  background: var(--accent-soft);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  line-height: 1.5;
  text-align: left;
  cursor: pointer;
}
a.carry { text-decoration: none; }
.carry:hover { border-style: solid; }
.carry:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }
/* 식 사다리(KM.card.ladder): 한 줄씩 쌓인다(지금 줄만 까닭을 함께) */
.ladder { display: grid; gap: 6px; margin-top: 4px; }
.rung { padding: 7px 12px; border-radius: 10px; background: var(--soft); border: 1.5px solid transparent; }
.rung .k { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; font-weight: 700; color: var(--muted); }
.rung .ok { color: var(--green); }
.rung .q { color: #C9C1B4; }
.rung .eq { margin-top: 3px; font-size: 15px; line-height: 1.65; }
.rung .why { margin-top: 4px; font-size: 13px; line-height: 1.55; color: var(--muted); }
.rung.todo { background: transparent; border: 1.5px dashed #E3D9C8; }
.rung.now { background: var(--accent-soft); border: 1.5px solid color-mix(in srgb, var(--accent) 45%, transparent); }
.rung.key:not(.todo) .eq { font-weight: 700; color: var(--accent); font-size: 16px; }

/* ── 도전 문제 ─────────────────────────────── */
/* 탭마다 도전 문제를 같은 칸에 겹쳐 두고 지금 탭의 것만 보인다(.vis-off) */
.quiz-stack { display: grid; }
.quiz-stack > * { grid-area: 1 / 1; min-width: 0; }
.quiz-q { font-size: 17px; margin-bottom: 10px; }
.quiz-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.quiz-input {
  width: 120px;
  min-height: 44px;
  padding: 6px 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
  font-size: 18px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  background: #fff;
}
.quiz-input:focus { outline: none; border-color: var(--accent); }
.quiz-unit { display: inline-block; min-width: 2.4em; font-weight: 700; }
/* 결과 칸은 늘 자리를 차지한다. 비어 있을 때는 안내 글을 보여 준다 */
.quiz-feedback { margin-top: 10px; border-radius: 12px; font-size: 15px; padding: 10px 12px; background: var(--soft); }
.quiz-feedback:empty::before { content: "답을 쓰고 확인을 눌러 보세요."; color: var(--muted); }
.quiz-feedback.ok { background: var(--green-soft); color: #11663F; }
.quiz-feedback.no { background: var(--red-soft); color: #A1262B; }
.quiz-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; gap: 8px; }

/* ── 탭 · 증감 버튼 · 분수 ─────────────────────────────── */
.stage-tabs { padding: 14px 20px 0; }
.stepper { display: inline-flex; align-items: center; gap: 6px; }
.stepper-label { font-weight: 700; font-size: 14px; color: var(--muted); margin-right: 2px; }
.stepper-unit { font-size: 14px; color: var(--muted); }
.stepper-btn {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: #fff;
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}
.stepper-btn:hover { border-color: var(--accent); color: var(--accent); }
.stepper-btn:disabled { opacity: .35; cursor: default; border-color: var(--line); color: inherit; }
.stepper-value { min-width: 2ch; text-align: center; font-family: var(--font-title); font-size: 22px; font-variant-numeric: tabular-nums; }
/* layout 'after': 이름 값 [− | +]. 두 단추를 한 덩어리로 붙이고, 값은 이름 바로 뒤에 */
.stepper.after { gap: 8px; }
.stepper.after .stepper-value { text-align: left; }
.stepper-pair { display: inline-flex; border: 2px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.stepper-pair .stepper-btn { width: 38px; height: 34px; border: 0; border-radius: 0; }
.stepper-pair .stepper-btn + .stepper-btn { box-shadow: inset 2px 0 0 var(--line); }
.stepper-pair .stepper-btn:hover:not(:disabled) { background: var(--accent-soft); }

.frac {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  vertical-align: middle;
  line-height: 1.15;
  margin: 0 .12em;
  font-size: .9em;
}
.frac > span { padding: 0 .2em; }
.frac > span:first-child { border-bottom: 2px solid currentColor; }
.mixed { display: inline-flex; align-items: center; }
.quiz-input.wide { width: 170px; text-align: left; }
.quiz-input::placeholder { color: #B5AEA2; font-size: 15px; }

/* ── 고르는 도전 문제 (KM.choiceQuiz) ─────────────────────────────── */
.choice-grid { display: grid; grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr)); gap: 8px; }
.choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 48px;
  padding: 8px 10px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: #fff;
  font-size: 15px;
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
  cursor: pointer;
  transition: border-color .12s, background .12s;
}
.choice:hover { border-color: var(--accent); }
.choice:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }
.choice svg { display: block; max-width: 100%; height: auto; }
.choice[aria-disabled="true"] { cursor: default; }
.choice[aria-disabled="true"]:hover { border-color: var(--line); }
.choice.wrong, .choice.wrong:hover { border-color: #F0B3B5; background: var(--red-soft); color: #A1262B; opacity: .7; }
.choice.right, .choice.right:hover { border-color: var(--green); background: var(--green-soft); color: #11663F; opacity: 1; }
.choice-quiz .quiz-feedback:empty::before { content: "알맞은 것을 골라 보세요."; }
/* 문제 안의 큰 식, 그림, ○× */
.quiz-big { font-family: var(--font-title); font-size: 26px; margin: 6px 0 2px; }
.q-pic { display: flex; justify-content: center; margin-top: 8px; }
.q-pic svg { display: block; max-width: 100%; height: auto; }
.ox { font-family: var(--font-title); font-size: 22px; font-weight: 400; }
.ox-q { margin-top: 8px; padding: 10px 12px; border-radius: 12px; background: var(--soft); font-weight: 700; font-size: 17px; }

/* 진도 지우기(주소 끝 ?reset, 테스트용)를 했다고 위쪽 가운데에 잠깐 알린다 */
.km-toast {
  position: fixed; top: 16px; left: 50%; z-index: 2000; translate: -50% 0; padding: 10px 20px; border-radius: 999px;
  background: var(--ink); color: #fff; font-weight: 700; font-size: 16px; box-shadow: 0 8px 24px rgba(42, 40, 51, .25);
  pointer-events: none; animation: kmToast 2.6s ease forwards;
}
@keyframes kmToast { 0% { opacity: 0; translate: -50% -8px; } 8%, 85% { opacity: 1; translate: -50% 0; } 100% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .km-toast { animation: none; } }
