:root {
  --bg: #f6efe4;
  --bg-2: #efe5d6;
  --card: #fffaf2;
  --ink: #3b2a22;
  --ink-2: #6e5a4c;
  --line: #e2d4c0;
  --accent: #c0603e;
  --accent-2: #a54e30;
  --accent-ink: #fffaf2;
  --sage: #8fa07c;
  --sage-soft: #e3e8da;
  --mustard: #d4a23c;
  --warn-bg: #fbecd2;
  --warn-ink: #7a4f12;
  --shadow: 0 1px 2px rgba(80, 50, 30, .06), 0 6px 20px rgba(80, 50, 30, .08);
  --radius: 16px;
  --font-head: 'Fraunces', Georgia, serif;
  --font-body: 'Nunito Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #221a16; --bg-2: #2b221d; --card: #2f2620; --ink: #f3e7d8; --ink-2: #c8b6a3; --line: #4a3c32;
    --accent: #e07a55; --accent-2: #f08f6a; --accent-ink: #221a16; --sage-soft: #34392e; --warn-bg: #3f3220; --warn-ink: #f0cf91;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #221a16; --bg-2: #2b221d; --card: #2f2620; --ink: #f3e7d8; --ink-2: #c8b6a3; --line: #4a3c32;
  --accent: #e07a55; --accent-2: #f08f6a; --accent-ink: #221a16; --sage-soft: #34392e; --warn-bg: #3f3220; --warn-ink: #f0cf91;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--font-body); font-size: 1rem; line-height: 1.5;
  min-height: 100vh; min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}
h1, h2, h3 { font-family: var(--font-head); font-weight: 650; line-height: 1.2; margin: 0 0 .5em; letter-spacing: -.01em; }
h1 { font-size: clamp(1.8rem, 5vw, 2.6rem); }
h2 { font-size: clamp(1.35rem, 3.6vw, 1.75rem); }
h3 { font-size: 1.15rem; }
p { margin: 0 0 .75em; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select { font: inherit; color: inherit; }
a { color: var(--accent-2); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; background: none; border: 0; padding: 4px; font-family: var(--font-head); font-size: 1.2rem; font-weight: 650; }
.brand img { border-radius: 8px; }
.topbar-actions { display: flex; gap: 6px; }
.icon-btn {
  min-width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--card);
  display: inline-flex; align-items: center; justify-content: center; font-weight: 700; padding: 0 10px; gap: 6px;
}
.icon-btn svg { width: 22px; height: 22px; }

main { max-width: 1180px; margin: 0 auto; padding: 16px 16px 120px; outline: none; }

/* ---------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 10px 20px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--card); font-weight: 700; text-decoration: none; color: var(--ink);
  transition: transform .12s ease, background .2s;
}
.btn:active { transform: scale(.97); }
.btn svg { width: 20px; height: 20px; flex: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 38px; padding: 6px 14px; font-size: .92rem; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; }

/* ---------- home */
.hero { display: grid; gap: 24px; align-items: center; padding: 12px 0 24px; }
.hero-art { border-radius: 24px; overflow: hidden; background: var(--bg-2); aspect-ratio: 4 / 3; position: relative; }
.hero-art svg { width: 100%; height: 100%; display: block; }
.hero p.lead { font-size: 1.15rem; color: var(--ink-2); max-width: 36em; }
.home-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-top: 8px; }
.home-card {
  text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px;
  box-shadow: var(--shadow); display: flex; gap: 14px; align-items: flex-start;
}
.home-card svg { width: 30px; height: 30px; flex: none; color: var(--accent); }
.home-card strong { display: block; font-size: 1.08rem; margin-bottom: 2px; }
.home-card span { color: var(--ink-2); font-size: .95rem; }
@media (min-width: 860px) { .hero { grid-template-columns: 1.1fr 1fr; padding-top: 32px; } }

/* ---------- wizard */
.wizard { max-width: 760px; margin: 0 auto; }
.progress { height: 8px; border-radius: 99px; background: var(--bg-2); overflow: hidden; margin: 6px 0 4px; }
.progress > i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--mustard)); border-radius: 99px; transition: width .35s ease; }
.q-count { color: var(--ink-2); font-size: .92rem; margin-bottom: 14px; }
.q-title { margin-bottom: 6px; }
.q-help { color: var(--ink-2); margin-bottom: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.cards.compact { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
.opt {
  position: relative; text-align: left; background: var(--card); border: 2px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; min-height: 64px; display: flex; flex-direction: column; gap: 2px; transition: border-color .15s, transform .12s;
}
.opt:active { transform: scale(.98); }
.opt strong { font-size: 1.04rem; }
.opt span { color: var(--ink-2); font-size: .92rem; }
.opt.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--card)); }
.opt.selected::after {
  content: ''; position: absolute; top: 10px; right: 10px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent) url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12l5 5 9-10"/></svg>') center/14px no-repeat;
}
.opt[disabled] { opacity: .5; }
.opt .why { color: var(--warn-ink); font-size: .85rem; }
.opt .badge { align-self: flex-start; margin-top: 6px; }
.badge { display: inline-block; font-size: .78rem; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--sage-soft); color: var(--ink); }
.wizard-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; display: flex; justify-content: space-between; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border-top: 1px solid var(--line);
}
.wizard-nav .inner { max-width: 760px; margin: 0 auto; width: 100%; display: flex; justify-content: space-between; gap: 10px; }

.rope-card { display: grid; grid-template-columns: 64px 1fr; gap: 12px; align-items: start; }
.rope-swatch { width: 64px; height: 64px; border-radius: 12px; background-size: 64px 64px; border: 1px solid var(--line); }
.rope-more { grid-column: 1 / -1; font-size: .92rem; color: var(--ink); background: var(--bg-2); border-radius: 12px; padding: 10px 12px; margin-top: 6px; }
.rope-more dt { font-weight: 700; }
.rope-more dd { margin: 0 0 6px; color: var(--ink-2); }
.linkish { background: none; border: 0; padding: 6px 0 0; color: var(--accent-2); font-weight: 700; text-align: left; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 44px; padding: 8px 16px; border-radius: 99px; border: 2px solid var(--line); background: var(--card); font-weight: 700;
}
.chip.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--card)); }
.chip.common { border-style: dashed; }

.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 10px; }
.sw { display: flex; flex-direction: column; align-items: center; gap: 4px; background: none; border: 0; padding: 4px; font-size: .78rem; color: var(--ink-2); }
.sw i { width: 48px; height: 48px; border-radius: 50%; border: 3px solid var(--card); box-shadow: 0 0 0 1px var(--line); position: relative; display: grid; place-items: center; font-style: normal; font-weight: 800; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.5); }
.sw.selected i { box-shadow: 0 0 0 3px var(--accent); }
.chosen { display: flex; gap: 10px; flex-wrap: wrap; margin: 4px 0 16px; align-items: center; }
.chosen .c { display: inline-flex; align-items: center; gap: 6px; padding: 4px 6px 4px 4px; border-radius: 99px; background: var(--card); border: 1px solid var(--line); font-size: .9rem; }
.chosen .c i { width: 26px; height: 26px; border-radius: 50%; display: inline-block; box-shadow: inset 0 0 0 1px rgba(0,0,0,.15); }
.chosen .c button { border: 0; background: none; padding: 2px 6px; font-weight: 800; color: var(--ink-2); }
.custom-color { display: inline-flex; align-items: center; gap: 8px; }
.custom-color input[type=color] { width: 48px; height: 48px; border: 0; padding: 0; background: none; border-radius: 50%; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field label { font-weight: 700; }
.field input[type=number], .field select, .field input[type=text] {
  min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); padding: 8px 12px; max-width: 260px;
}
.inline-fields { display: flex; gap: 14px; flex-wrap: wrap; }
.slider-wrap input[type=range] { width: 100%; accent-color: var(--accent); height: 40px; }
.slider-labels { display: flex; justify-content: space-between; color: var(--ink-2); font-size: .92rem; }

/* ---------- loading */
.loading { display: grid; place-items: center; min-height: 60vh; text-align: center; gap: 18px; }
.loader { width: 90px; height: 90px; }
.loader path { stroke-dasharray: 220; stroke-dashoffset: 220; animation: draw 1.6s ease-in-out infinite; }
@keyframes draw { 50% { stroke-dashoffset: 0; } 100% { stroke-dashoffset: -220; } }
@media (prefers-reduced-motion: reduce) { .loader path { animation: none; stroke-dashoffset: 0; } }

/* ---------- result */
.result { display: grid; gap: 18px; }
@media (min-width: 980px) { .result { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); align-items: start; } .result .stage-col { position: sticky; top: 76px; } }
.stage { position: relative; border-radius: 22px; overflow: hidden; background: var(--bg-2); height: min(68vh, 760px); min-height: 340px; box-shadow: var(--shadow); touch-action: none; }
.stage.fs { position: fixed; inset: 0; height: 100%; border-radius: 0; z-index: 50; }
.viewer-canvas { width: 100%; height: 100%; display: block; }
.stage-top { position: absolute; top: 10px; left: 10px; right: 10px; display: flex; justify-content: space-between; gap: 8px; pointer-events: none; }
.stage-top > * { pointer-events: auto; }
.seg { display: inline-flex; background: color-mix(in srgb, var(--card) 88%, transparent); border-radius: 99px; padding: 3px; border: 1px solid var(--line); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.seg button { border: 0; background: none; padding: 7px 12px; border-radius: 99px; font-weight: 700; font-size: .88rem; min-height: 36px; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.stage-hint { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); font-size: .85rem; background: color-mix(in srgb, var(--card) 85%, transparent); padding: 4px 12px; border-radius: 99px; color: var(--ink-2); pointer-events: none; transition: opacity .6s; white-space: nowrap; }
.stage .icon-btn { background: color-mix(in srgb, var(--card) 88%, transparent); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 12px; }
@media (min-width: 560px) { .actions { grid-template-columns: repeat(4, 1fr); } }
.actions .btn { padding: 10px 12px; }
.title-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.seed { font-family: ui-monospace, Menlo, monospace; font-size: .9rem; background: var(--bg-2); border: 1px solid var(--line); border-radius: 99px; padding: 4px 12px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 6px 0 14px; overflow-x: auto; scrollbar-width: none; }
.tabs button { border: 0; background: none; padding: 12px 14px; font-weight: 700; color: var(--ink-2); border-bottom: 3px solid transparent; white-space: nowrap; min-height: 48px; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.fact { background: var(--bg-2); border-radius: 12px; padding: 10px 12px; }
.fact b { display: block; font-size: 1.15rem; font-family: var(--font-head); }
.fact span { color: var(--ink-2); font-size: .85rem; }
.warn { background: var(--warn-bg); color: var(--warn-ink); border-radius: 12px; padding: 10px 14px; margin-bottom: 10px; }
.cut-table { width: 100%; border-collapse: collapse; }
.cut-table td, .cut-table th { padding: 10px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.cut-table th { font-size: .85rem; color: var(--ink-2); }
.dot { display: inline-block; width: 18px; height: 18px; border-radius: 50%; vertical-align: -3px; margin-right: 6px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.18); }
.big-n { font-family: var(--font-head); font-size: 1.2rem; }
.muted { color: var(--ink-2); font-size: .92rem; }
.step { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.step-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.step-num { width: 36px; height: 36px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; flex: none; }
.step h3 { margin: 0; flex: 1; }
.step ol, .step ul { margin: 0 0 8px; padding-left: 1.2em; }
.step li { margin-bottom: 6px; }
.step .tip { background: var(--sage-soft); border-radius: 10px; padding: 8px 12px; font-size: .95rem; margin-top: 6px; }
.diagram-wrap { background: var(--bg-2); border-radius: 12px; padding: 8px; margin: 6px 0 12px; max-height: 420px; overflow: auto; }
.diagram { width: 100%; height: auto; max-height: 400px; display: block; }
.step.done { opacity: .6; }
.step-check { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--ink-2); }
.step-check input { width: 22px; height: 22px; accent-color: var(--accent); }
.knot-links { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

/* ---------- sheets / toast */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(30, 20, 15, .45); z-index: 60; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--card); width: 100%; max-width: 560px; border-radius: 22px 22px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  max-height: 88vh; overflow: auto; animation: up .25s ease;
}
@media (min-width: 700px) { .sheet-backdrop { align-items: center; } .sheet { border-radius: 22px; } }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-list { display: grid; gap: 8px; }
.sheet-item { display: flex; gap: 14px; align-items: center; text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 14px; padding: 12px 14px; min-height: 60px; }
.sheet-item svg { width: 26px; height: 26px; color: var(--accent); flex: none; }
.sheet-item strong { display: block; }
.sheet-item span { color: var(--ink-2); font-size: .9rem; }
.bar { height: 8px; background: var(--bg-2); border-radius: 99px; overflow: hidden; margin-top: 10px; }
.bar i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
#toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 99px; z-index: 80; transition: all .25s; pointer-events: none; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- lessons & library */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.lesson-card { text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.lesson-card strong { display: block; font-size: 1.05rem; }
.lesson-card span { color: var(--ink-2); font-size: .92rem; }
.level { color: var(--mustard); letter-spacing: 2px; font-size: .9rem; }
.lesson { display: grid; gap: 16px; }
@media (min-width: 900px) { .lesson { grid-template-columns: 1.2fr 1fr; align-items: start; } }
.lesson .stage { height: min(60vh, 620px); }
.lesson-step { font-size: 1.2rem; min-height: 4.5em; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.lesson-step .n { color: var(--accent); font-weight: 800; font-size: .95rem; display: block; margin-bottom: 4px; }
.player { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.player input[type=range] { flex: 1; accent-color: var(--accent); height: 40px; }
.player .icon-btn { width: 52px; height: 52px; border-radius: 50%; }
.player .icon-btn.play { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.lib-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.lib-card img { width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--bg-2); display: block; cursor: pointer; }
.lib-card .body { padding: 12px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.lib-card .name { font-weight: 700; }
.lib-card .row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: auto; }
.empty { text-align: center; padding: 40px 16px; color: var(--ink-2); }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }

/* ---------- print */
#print { display: none; }
@media print {
  body { background: #fff; color: #222; }
  body > *:not(#print) { display: none !important; }
  #print { display: block; font-size: 11pt; }
  #print h1 { font-size: 22pt; }
  #print img.hero { max-width: 100%; max-height: 120mm; display: block; margin: 0 auto 6mm; }
  #print .pstep { margin-bottom: 6mm; border-top: 1px solid #ddd; padding-top: 3mm; }
  #print .pstep h3 { break-after: avoid; }
  #print .diagram { max-height: 70mm; width: auto; max-width: 100%; break-inside: avoid; display: block; }
  #print table { width: 100%; border-collapse: collapse; }
  #print td, #print th { border-bottom: 1px solid #ddd; padding: 2mm; text-align: left; }
  @page { margin: 14mm; }
}
.rope-card strong, .rope-card > div > span { display: block; }

/* ---------- demo pictures in the questions */
.cards.with-media { grid-template-columns: repeat(auto-fill, minmax(min(46%, 210px), 1fr)); }
.opt.has-media { padding: 0 0 12px; overflow: hidden; }
.opt.has-media > strong, .opt.has-media > span { padding: 0 12px; }
.opt.has-media > strong { margin-top: 8px; }
.opt-media { position: relative; aspect-ratio: 4 / 3; background: #efe6d8; overflow: hidden; border-bottom: 1px solid var(--line); }
.opt-media img { display: block; width: 100%; height: 100%; object-fit: cover; opacity: 1; transition: opacity .3s; }
.opt-media img:not([src]) { opacity: 0; }
.opt-media.loading::before {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 20%, rgba(255,255,255,.55) 50%, transparent 80%) #efe6d8;
  background-size: 220% 100%; animation: shimmer 1.3s linear infinite;
}
@keyframes shimmer { from { background-position: 120% 0; } to { background-position: -120% 0; } }
@media (prefers-reduced-motion: reduce) { .opt-media.loading::before { animation: none; } }
.opt-media.sketch { background: #f6efe3; display: flex; align-items: center; justify-content: center; }
.opt-media.sketch svg { width: 100%; height: 100%; }
.opt-media.paper { align-items: stretch; padding: 10px 36px 10px 12px; font-size: .8rem; line-height: 1.35; color: #4a3a2e; text-align: left; }
.opt-media .instr p { margin: 0 0 6px; }
.opt-media .lesson-chip { display: inline-block; background: #c0603e; color: #fff; border-radius: 99px; padding: 2px 8px; font-size: .74rem; font-weight: 700; }
.opt.selected.has-media::after { top: 8px; right: 8px; box-shadow: 0 0 0 3px #fff; }
.opt[disabled] .opt-media { filter: grayscale(.8); }
.q-preview { margin: 0 0 18px; max-width: 460px; border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); background: var(--card); }
.q-preview .opt-media { border-bottom: 1px solid var(--line); }
.q-preview figcaption { padding: 8px 12px; color: var(--ink-2); font-size: .88rem; }
.chip.thick { display: inline-flex; align-items: center; gap: 8px; }
.chip .tbar { display: inline-block; width: 22px; border-radius: 99px; background: #c9a27a; }
.stage .icon-btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.stage-left { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; }
