/* =============================================================
 * Knotrix v2 — Polished Commercial Stylesheet
 * System font stack (no external CDN), cohesive design system,
 * per-chapter palette theming, responsive 320px–1920px.
 * ============================================================= */

/* ---------- Reset & base ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; width: 100%; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans,
               Ubuntu, Cantarell, "Helvetica Neue", Arial, sans-serif;
  font-feature-settings: "kern", "liga", "clig", "calt";
  -webkit-font-smoothing: antialiased;
  -moz-os-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  background: #f4ead7;
  color: #2a1f12;
  position: fixed;
  inset: 0;
}

:root {
  /* Warm workshop defaults (overridden per theme) */
  --bg-0: #f4ead7;
  --bg-1: #e7d5b0;
  --bg-grad-0: #fff7e3;
  --bg-grad-1: #ebd8b0;
  --ink: #2a1f12;
  --ink-soft: #5a4628;
  --ink-mute: #9c8664;
  --paper: #fcf5e5;
  --paper-2: #f0e1c3;
  --line: rgba(70,45,15,0.14);
  --line-strong: rgba(70,45,15,0.28);
  --accent: #b8782b;
  --accent-2: #3e7662;
  --danger: #b83c2c;
  --success: #3e8e5a;
  --shadow-sm: 0 2px 6px -2px rgba(40,25,8,0.18), 0 1px 2px rgba(40,25,8,0.10);
  --shadow:    0 10px 28px -14px rgba(40,25,8,0.45), 0 3px 8px -4px rgba(40,25,8,0.18);
  --shadow-lg: 0 22px 50px -18px rgba(40,25,8,0.55), 0 8px 16px -8px rgba(40,25,8,0.25);
  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 24px;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Times New Roman", serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

/* Body background layers: radial warmth + paper grain */
body::before {
  content: '';
  position: fixed;
  inset: -40px;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(ellipse at 18% 8%,  var(--bg-grad-0) 0%, transparent 55%),
    radial-gradient(ellipse at 85% 92%, rgba(184,120,43,0.22) 0%, transparent 55%),
    linear-gradient(160deg, var(--bg-0) 0%, var(--bg-1) 100%);
}
body::after {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: 0.5;
  background-image:
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.45  0 0 0 0 0.32  0 0 0 0 0.15  0 0 0 0.08 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
  background-size: 200px 200px;
  mix-blend-mode: multiply;
}

/* Background art: subtle repeating peg-constellation motif behind the whole app.
   Loaded from assets/bg-pattern.svg — scattered nodes + thin connecting lines,
   low-opacity + multiply-blended so it reads as texture, not artwork, and
   tints correctly under every chapter theme. */
body::before {
  background-image:
    url("../assets/bg-pattern.svg"),
    radial-gradient(ellipse at 18% 8%,  var(--bg-grad-0) 0%, transparent 55%),
    radial-gradient(ellipse at 85% 92%, rgba(184,120,43,0.22) 0%, transparent 55%),
    linear-gradient(160deg, var(--bg-0) 0%, var(--bg-1) 100%);
  background-size: 180px 180px, auto, auto, auto;
  background-repeat: repeat, no-repeat, no-repeat, no-repeat;
}

/* ---------- Themes ---------- */
body[data-theme="craft"] {
  --bg-0:#f4e4c3; --bg-1:#e3cc9a;
  --bg-grad-0:#fbf0d4; --bg-grad-1:#e8d3a0;
  --ink:#2d1e0c; --ink-soft:#5e4422; --ink-mute:#957b4f;
  --paper:#f8ecd1; --paper-2:#e8d3a4;
  --line:rgba(80,50,15,0.16); --line-strong:rgba(80,50,15,0.32);
  --accent:#a65c1e; --accent-2:#7a3f18;
}
body[data-theme="lab"] {
  --bg-0:#1a2633; --bg-1:#0f1924;
  --bg-grad-0:rgba(80,130,160,0.15); --bg-grad-1:transparent;
  --ink:#eaf0f6; --ink-soft:#9fb2c7; --ink-mute:#6b8099;
  --paper:#213344; --paper-2:#2b4259;
  --line:rgba(180,210,240,0.12); --line-strong:rgba(180,210,240,0.25);
  --accent:#3fa98a; --accent-2:#d9a24e;
  --shadow-sm:0 2px 8px -2px rgba(0,0,0,0.55), 0 1px 2px rgba(0,0,0,0.4);
  --shadow:   0 14px 32px -14px rgba(0,0,0,0.75), 0 4px 10px -4px rgba(0,0,0,0.5);
  --shadow-lg:0 28px 60px -18px rgba(0,0,0,0.85), 0 10px 20px -8px rgba(0,0,0,0.55);
}
body[data-theme="blueprint"] {
  --bg-0:#14263d; --bg-1:#0b1727;
  --bg-grad-0:rgba(120,170,230,0.12); --bg-grad-1:transparent;
  --ink:#e6edf7; --ink-soft:#9db6d2; --ink-mute:#6b87aa;
  --paper:#1b3252; --paper-2:#234269;
  --line:rgba(220,230,250,0.12); --line-strong:rgba(220,230,250,0.25);
  --accent:#e8b14e; --accent-2:#6ba0e2;
  --shadow-sm:0 2px 8px -2px rgba(0,0,0,0.55), 0 1px 2px rgba(0,0,0,0.4);
  --shadow:   0 14px 32px -14px rgba(0,0,0,0.75), 0 4px 10px -4px rgba(0,0,0,0.55);
  --shadow-lg:0 28px 60px -18px rgba(0,0,0,0.85), 0 10px 20px -8px rgba(0,0,0,0.6);
}
body[data-theme="master"] {
  --bg-0:#141014; --bg-1:#080609;
  --bg-grad-0:rgba(217,162,78,0.10); --bg-grad-1:transparent;
  --ink:#f4e6c9; --ink-soft:#a99370; --ink-mute:#6a5940;
  --paper:#1c1818; --paper-2:#25201d;
  --line:rgba(240,220,180,0.10); --line-strong:rgba(240,220,180,0.22);
  --accent:#d9a24e; --accent-2:#8a3a2c;
  --shadow-sm:0 2px 10px -2px rgba(0,0,0,0.75), 0 1px 3px rgba(0,0,0,0.5);
  --shadow:   0 16px 40px -16px rgba(0,0,0,0.9), 0 4px 12px -4px rgba(0,0,0,0.6);
  --shadow-lg:0 32px 70px -20px rgba(0,0,0,0.95), 0 10px 22px -8px rgba(0,0,0,0.7);
}

/* Dark themes: grain blends softly rather than multiplies */
body[data-theme="lab"]::after,
body[data-theme="blueprint"]::after {
  background-image:
    linear-gradient(rgba(255,255,255,0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.035) 1px, transparent 1px);
  background-size: 32px 32px;
  mix-blend-mode: normal;
  opacity: 0.5;
}
body[data-theme="master"]::after {
  background-image:
    radial-gradient(circle at 50% 50%, rgba(217,162,78,0.06) 0, transparent 65%),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='240' height='240'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.85  0 0 0 0 0.70  0 0 0 0 0.40  0 0 0 0.06 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
  background-size: auto, 240px 240px;
  mix-blend-mode: normal;
  opacity: 1;
}

/* ---------- App shell ---------- */
#app {
  position: relative;
  width: 100%;
  height: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px calc(env(safe-area-inset-bottom, 0px) + 10px);
  display: flex;
  flex-direction: column;
  z-index: 1;
}

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px calc(env(safe-area-inset-bottom, 0px) + 14px);
  display: none;
  flex-direction: column;
  align-items: center;
  opacity: 0;
  z-index: 2;
  overflow: hidden;
}
.screen.active {
  display: flex;
  opacity: 1;
  animation: screenIn 0.38s cubic-bezier(.2,.9,.25,1.1);
}
.screen.scrollable { overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }
@keyframes screenIn {
  0%   { opacity: 0; transform: translateY(8px) scale(0.985); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- Typography ---------- */
.title {
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: clamp(2.4rem, 9vw, 3.2rem);
  letter-spacing: -0.5px;
  color: var(--ink);
  line-height: 1.05;
}
.subtitle {
  font-size: 0.78rem;
  letter-spacing: 3.5px;
  text-transform: uppercase;
  color: var(--ink-mute);
  margin-top: 4px;
  font-weight: 500;
}
h2 {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1.3rem;
  color: var(--ink);
  letter-spacing: 0.2px;
}
h3 {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 1rem;
  color: var(--ink);
  margin-bottom: 3px;
}
p { color: var(--ink-soft); font-size: 0.85rem; line-height: 1.55; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.95rem;
  letter-spacing: 0.2px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 13px 22px;
  border-radius: var(--radius);
  cursor: pointer;
  box-shadow: var(--shadow-soft);
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  min-height: 48px;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; pointer-events: none; }
.btn.block { width: 100%; max-width: 360px; }
.btn.btn-primary {
  background: linear-gradient(180deg, #3a2c14 0%, #1f180b 100%);
  color: #f8ecd1;
  border-color: transparent;
  box-shadow: 0 6px 16px -6px rgba(40,25,8,0.55), inset 0 1px 0 rgba(255,240,200,0.12);
}
body[data-theme="lab"] .btn.btn-primary,
body[data-theme="blueprint"] .btn.btn-primary,
body[data-theme="master"] .btn.btn-primary {
  background: linear-gradient(180deg, #d9a24e 0%, #a67528 100%);
  color: #1a1206;
  box-shadow: 0 8px 18px -8px rgba(217,162,78,0.55), inset 0 1px 0 rgba(255,240,200,0.3);
}
.btn.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 40%, transparent);
}
@media (hover:hover) {
  .btn:hover { transform: translateY(-1px); box-shadow: var(--shadow); }
  .btn:hover:active { transform: translateY(0) scale(0.97); }
}

.btn-icon {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink-soft);
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.08s ease, background 0.15s ease, color 0.15s ease, opacity 0.2s ease, border-color 0.15s ease;
  padding: 0;
}
.btn-icon svg { width: 20px; height: 20px; display: block; }
.btn-icon:active { transform: scale(0.92); }
.btn-icon:disabled, .btn-icon.disabled { opacity: 0.35; pointer-events: none; }
.btn-icon.btn-sm { width: 40px; height: 40px; border-radius: 10px; }
.btn-icon.btn-sm svg { width: 18px; height: 18px; }
@media (hover:hover) {
  .btn-icon:hover { background: var(--paper-2); color: var(--ink); }
}
.btn-ic { display: inline-flex; width: 18px; height: 18px; }
.btn-ic svg { width: 100%; height: 100%; }

/* ---------- Loading ---------- */
#screen-loading { justify-content: center; }
.load-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.load-knot {
  width: 110px; height: 110px;
  filter: drop-shadow(0 8px 22px rgba(184,120,43,0.35));
}
.load-bar {
  width: min(62vw, 240px);
  height: 4px;
  background: var(--line);
  border-radius: 10px;
  overflow: hidden;
  margin-top: 6px;
}
.load-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 10px;
  transition: width 0.12s linear;
}
.load-text {
  font-size: 0.72rem;
  color: var(--ink-mute);
  letter-spacing: 1.6px;
  text-transform: uppercase;
  margin-top: 2px;
  min-height: 1em;
}

/* ---------- Menu ---------- */
#screen-menu { justify-content: center; }
.menu-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 360px;
}
.menu-hero {
  width: 200px; height: 170px;
  display: flex; align-items: center; justify-content: center;
  animation: heroBob 5s ease-in-out infinite;
  filter: drop-shadow(0 12px 28px rgba(120,70,20,0.22));
}
.menu-knot-svg { width: 100%; height: 100%; }
@keyframes heroBob {
  0%,100% { transform: translateY(0) rotate(-1.5deg); }
  50%     { transform: translateY(-10px) rotate(1.5deg); }
}
.menu-meta {
  display: flex; gap: 10px; margin-top: 4px;
  font-size: 0.72rem;
  color: var(--ink-mute);
  letter-spacing: 0.6px;
}
.menu-meta span {
  background: var(--paper);
  padding: 5px 12px;
  border-radius: 20px;
  border: 1px solid var(--line);
  font-weight: 500;
}
.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  margin-top: 16px;
}
.menu-row {
  display: flex; gap: 10px; margin-top: 8px;
}

/* ---------- Sub-screen top bars ---------- */
.sub-top {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
  padding-top: 4px;
  flex-shrink: 0;
}
.sub-top .spacer { width: 40px; height: 40px; display: block; }

/* ---------- Tutorial ---------- */
.tutorial-body {
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding-bottom: 24px;
}
.tut-hero {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-soft);
  display: flex;
  justify-content: center;
}
.tut-svg { width: 100%; max-width: 280px; height: auto; }
.tut-svg .tut-peg.p1 { animation: pegFloat 3s ease-in-out infinite; }
.tut-svg .tut-peg.p4 { animation: pegFloat 3s ease-in-out infinite 0.4s; }
@keyframes pegFloat {
  0%,100% { transform: translateY(0); }
  50%     { transform: translateY(-4px); }
}
.tut-steps { display: flex; flex-direction: column; gap: 10px; }
.tut-step {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  gap: 14px;
  box-shadow: var(--shadow-sm);
  align-items: flex-start;
}
.tut-n {
  flex: 0 0 34px;
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 0.95rem;
  margin-top: 1px;
}
body[data-theme="lab"] .tut-n,
body[data-theme="blueprint"] .tut-n,
body[data-theme="master"] .tut-n {
  background: var(--accent);
  color: #1a1206;
}
.kbd {
  display: inline-block;
  padding: 1px 7px;
  border: 1px solid var(--line);
  border-radius: 5px;
  font-size: 0.72rem;
  background: var(--paper-2);
  font-family: var(--font-mono);
  font-weight: 600;
}

/* ---------- Settings ---------- */
.settings-body { width: 100%; max-width: 480px; display: flex; flex-direction: column; gap: 14px; padding-bottom: 24px; }
.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.card.danger-card { border-color: color-mix(in srgb, var(--danger) 25%, var(--line)); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row-label { font-weight: 600; font-size: 0.95rem; color: var(--ink); }
.row-desc { font-size: 0.75rem; color: var(--ink-soft); margin-top: 2px; }

/* Toggle switch */
.toggle {
  position: relative;
  width: 48px; height: 28px;
  border-radius: 20px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  cursor: pointer;
  padding: 0;
  flex: 0 0 auto;
  transition: background 0.2s ease, border-color 0.2s ease;
}
.toggle .knob {
  position: absolute;
  top: 2px; left: 2px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--ink);
  opacity: 0.75;
  transition: left 0.2s cubic-bezier(.3,1.3,.4,1), background 0.2s ease;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
body[data-theme="warm"] .toggle .knob,
body[data-theme="craft"] .toggle .knob { background: #fff; opacity: 1; }
.toggle.on { background: var(--accent); border-color: transparent; }
.toggle.on .knob { left: 22px; background: #fff; opacity: 1; }

/* Slider */
.slider {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 4px; border-radius: 8px;
  background: var(--paper-2);
  outline: none;
  margin-top: 10px;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--paper);
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}
.slider::-moz-range-thumb {
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--paper);
  cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}

.legal { font-size: 0.7rem; color: var(--ink-mute); text-align: center; margin-top: 8px; letter-spacing: 0.5px; }

/* ---------- Chapters ---------- */
.chapters-grid {
  width: 100%;
  max-width: 540px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  padding-bottom: 24px;
}
.chapter-card {
  appearance: none;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 16px 18px;
  display: flex;
  gap: 16px;
  align-items: center;
  cursor: pointer;
  text-align: left;
  color: var(--ink);
  font-family: inherit;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  position: relative;
  overflow: hidden;
  width: 100%;
}
.chapter-card:active { transform: scale(0.985); }
.chapter-card:disabled { opacity: 0.45; cursor: default; }
@media (hover:hover) { .chapter-card:hover { box-shadow: var(--shadow); transform: translateY(-2px); } }
.chapter-art {
  flex: 0 0 72px;
  width: 72px; height: 72px;
  border-radius: 18px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center;
  color: var(--accent);
}
.chapter-art svg { width: 44px; height: 44px; }
.chapter-pal-warm .chapter-art    { background: #f2dfb4; color: #b8782b; }
.chapter-pal-craft .chapter-art   { background: #e9ceaa; color: #a65c1e; }
.chapter-pal-lab .chapter-art     { background: #274058; color: #3fa98a; }
.chapter-pal-blueprint .chapter-art{background: #27456f; color: #e8b14e; }
.chapter-pal-master .chapter-art  { background: #252019; color: #d9a24e; }
.chapter-meta { flex: 1; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.chapter-num { font-size: 0.68rem; letter-spacing: 2.2px; text-transform: uppercase; color: var(--ink-mute); font-weight: 600; }
.chapter-name { font-family: var(--font-serif); font-weight: 600; font-size: 1.2rem; color: var(--ink); }
.chapter-sub { font-size: 0.76rem; color: var(--ink-soft); }
.chapter-stats {
  display: flex; gap: 14px; margin-top: 6px; flex-wrap: wrap;
  font-size: 0.74rem; color: var(--ink-soft);
}
.chapter-lock {
  position: absolute; top: 14px; right: 14px;
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--paper-2);
  border: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center;
  color: var(--ink-mute);
}
.chapter-lock svg { width: 15px; height: 15px; }

/* ---------- Level Select ---------- */
.levelselect-wrap {
  width: 100%;
  max-width: 540px;
  padding-bottom: 24px;
}
.chapter-subtitle { font-size: 0.78rem; color: var(--ink-soft); letter-spacing: 1.4px; text-transform: uppercase; font-weight: 600; }
.chapter-progress { font-size: 0.78rem; color: var(--ink-mute); margin: 4px 0 16px; }
.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}
.level-tile {
  appearance: none;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 4px 8px;
  cursor: pointer;
  color: var(--ink);
  font-family: inherit;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  aspect-ratio: 1/1;
  position: relative;
  box-shadow: var(--shadow-sm);
  transition: transform 0.1s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.level-tile:active { transform: scale(0.93); }
.level-tile.locked { opacity: 0.45; cursor: default; }
.level-tile.done {
  background: linear-gradient(180deg, var(--paper), var(--paper-2));
  border-color: var(--line-strong);
}
@media (hover:hover) { .level-tile:hover { box-shadow: var(--shadow); transform: translateY(-2px); } }
.level-tile-top { position: relative; width: 100%; display: flex; justify-content: center; align-items: center; min-height: 22px; }
.level-tile-num { font-family: var(--font-serif); font-weight: 600; font-size: 1.05rem; }
.level-tile-lock { position: absolute; top: -2px; right: 4px; width: 14px; height: 14px; color: var(--ink-mute); }
.level-tile-check { position: absolute; top: -2px; right: 4px; width: 14px; height: 14px; color: var(--success); }
.level-tile-lock svg, .level-tile-check svg { width: 100%; height: 100%; }
.level-tile-name { font-size: 0.54rem; color: var(--ink-soft); text-align: center; line-height: 1.1; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 2px; }
.level-tile-stars { display: flex; gap: 1px; font-size: 0.55rem; color: var(--ink-mute); margin-top: auto; }
.level-tile-stars .s { display: inline-flex; width: 11px; height: 11px; opacity: 0.25; }
.level-tile-stars .s.on { opacity: 1; color: var(--accent); }
.level-tile-stars .s svg { width: 100%; height: 100%; }

/* ---------- Game ---------- */
#screen-game {
  display: none;
  gap: 8px;
  padding-top: calc(env(safe-area-inset-top,0px) + 6px);
}
#screen-game.active { display: flex; }
.hud {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px;
  flex-shrink: 0;
  gap: 8px;
}
.hud-left, .hud-right { display: flex; align-items: center; gap: 8px; }
.hud-center { display: flex; align-items: center; gap: 6px; flex: 1; justify-content: center; }
.chip-level {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 0.92rem;
  padding: 7px 12px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
  color: var(--ink);
  min-width: 54px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.hud-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 5px 10px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  min-width: 58px;
  box-shadow: var(--shadow-sm);
}
.hud-lbl { font-size: 0.58rem; letter-spacing: 1px; text-transform: uppercase; color: var(--ink-mute); font-weight: 600; }
.hud-val { font-weight: 700; font-size: 0.92rem; font-variant-numeric: tabular-nums; color: var(--ink); }
.hud-val.pulse { animation: chipPulse 0.4s cubic-bezier(.3,1.6,.4,1); }
@keyframes chipPulse {
  0%   { transform: scale(1); color: var(--ink); }
  40%  { transform: scale(1.25); color: var(--accent); }
  100% { transform: scale(1); color: var(--ink); }
}
.hud-stars { display: flex; gap: 2px; padding: 7px 10px; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-sm); }
.hud-stars .s { width: 14px; height: 14px; color: var(--ink-mute); opacity: 0.30; display: inline-flex; }
.hud-stars .s.on { opacity: 1; color: var(--accent); }
.hud-stars .s svg { width: 100%; height: 100%; }

.board-wrap {
  position: relative;
  width: 100%;
  flex: 1;
  min-height: 0;
  aspect-ratio: 1/1;
  max-width: 640px;
  margin: 0 auto;
}
canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: var(--radius-lg);
  touch-action: none;
  cursor: grab;
  box-shadow: var(--shadow-lg);
}
canvas:active { cursor: grabbing; }

.goal-toast {
  position: absolute;
  top: 14px; left: 50%;
  transform: translate(-50%, -10px);
  background: var(--ink);
  color: var(--paper);
  padding: 8px 18px;
  border-radius: 20px;
  font-size: 0.72rem;
  letter-spacing: 2.2px;
  font-weight: 700;
  text-transform: uppercase;
  opacity: 0;
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
  z-index: 4;
  box-shadow: var(--shadow);
}
.goal-toast.show { opacity: 0.95; transform: translate(-50%, 0); }

.toolbar {
  display: flex;
  gap: 8px;
  justify-content: center;
  padding: 6px 0 4px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, var(--ink) 60%, transparent);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  border-radius: var(--radius-lg);
  z-index: 10;
  padding: 20px;
  text-align: center;
  animation: overlayFade 0.28s ease;
}
.overlay.show { display: flex; }
@keyframes overlayFade { from { opacity: 0; } to { opacity: 1; } }

.win-card, .pause-card, .intro-card, .confirm-card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 26px 24px 22px;
  max-width: 360px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: overlayIn 0.42s cubic-bezier(.2,1.1,.3,1.35);
  position: relative;
  overflow: hidden;
}
@keyframes overlayIn {
  0%   { opacity: 0; transform: scale(0.85) translateY(14px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}

.win-badge {
  font-size: 0.68rem;
  letter-spacing: 3px;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
}
.win-stars { display: flex; gap: 8px; justify-content: center; margin: 6px 0 2px; }
.win-stars .s { width: 38px; height: 38px; color: var(--accent); animation: starPop 0.55s cubic-bezier(.3,1.7,.4,1.1) both; display: inline-flex; filter: drop-shadow(0 4px 10px rgba(217,162,78,0.4)); }
.win-stars .s svg { width: 100%; height: 100%; }
.win-stars .s:nth-child(1) { animation-delay: 0.15s; }
.win-stars .s:nth-child(2) { animation-delay: 0.35s; }
.win-stars .s:nth-child(3) { animation-delay: 0.55s; }
@keyframes starPop {
  0%   { transform: scale(0) rotate(-30deg); opacity: 0; }
  60%  { transform: scale(1.25) rotate(8deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); }
}
.win-title { font-family: var(--font-serif); font-weight: 700; font-size: 2rem; letter-spacing: 0.2px; color: var(--ink); }
.win-line { font-size: 0.9rem; color: var(--ink-soft); }
.win-stats {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  margin: 6px 0 8px;
  padding: 12px 14px;
  background: var(--paper-2);
  border-radius: var(--radius);
  border: 1px solid var(--line);
}
.ws { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.ws-lbl { font-size: 0.62rem; letter-spacing: 1.4px; text-transform: uppercase; color: var(--ink-mute); font-weight: 600; }
.ws-val { font-family: var(--font-serif); font-weight: 700; font-size: 1.25rem; color: var(--ink); font-variant-numeric: tabular-nums; }
.ws-sep { width: 1px; height: 28px; background: var(--line); }
.win-eff { color: var(--accent); font-weight: 700; }
.win-btns { display: flex; gap: 8px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }
.win-btns .btn { flex: 1; min-width: 90px; padding: 11px 14px; font-size: 0.9rem; }

.pause-card h2 { font-family: var(--font-serif); font-size: 1.6rem; margin-bottom: 4px; }
.pause-card .btn { width: 100%; }

.intro-kicker { font-size: 0.72rem; letter-spacing: 3px; text-transform: uppercase; color: var(--ink-mute); font-weight: 700; }
.chapter-intro-sub { font-size: 0.9rem; font-weight: 600; color: var(--accent); margin-bottom: 4px; }
.intro-card h2 { font-family: var(--font-serif); font-size: 1.8rem; }
.intro-card p { font-size: 0.9rem; color: var(--ink-soft); line-height: 1.6; }
.intro-card .btn { margin-top: 8px; }

.confirm-card h3 { font-family: var(--font-serif); font-size: 1.25rem; }
.confirm-card p { font-size: 0.88rem; color: var(--ink-soft); line-height: 1.55; }
.confirm-btns { display: flex; gap: 8px; margin-top: 8px; }
.confirm-btns .btn { flex: 1; }

.complete-card .win-title { font-size: 1.8rem; }

/* ---------- Responsive ---------- */
@media (max-width: 380px) {
  .hud-stat { min-width: 48px; padding: 4px 8px; }
  .hud-val { font-size: 0.84rem; }
  .btn-icon { width: 40px; height: 40px; }
  .btn-icon.btn-sm { width: 36px; height: 36px; }
  .chip-level { font-size: 0.82rem; padding: 5px 10px; min-width: 46px; }
  .level-grid { grid-template-columns: repeat(4, 1fr); }
  .hud-stars .s { width: 12px; height: 12px; }
  .title { font-size: 2.1rem; }
  .tut-step { padding: 12px 14px; gap: 10px; }
  .tut-n { width: 30px; height: 30px; flex-basis: 30px; font-size: 0.85rem; }
  .menu-hero { width: 160px; height: 140px; }
}
@media (max-height: 640px) {
  .title { font-size: 1.9rem; }
  .menu-hero { width: 130px; height: 110px; }
  .menu-wrap { gap: 6px; }
  .menu-buttons { gap: 8px; margin-top: 10px; }
  .btn { padding: 11px 18px; min-height: 44px; }
}
@media (min-width: 600px) {
  #app { padding: 18px; }
  .chapters-grid { grid-template-columns: 1fr 1fr; }
}
@media (min-width: 900px) {
  #app { max-width: 780px; }
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ---------- Utilities ---------- */
.hidden { display: none !important; }

/* ---------- Small star icon (used in menu meta etc.) ---------- */
.star-mini {
  display: inline-block;
  width: 12px; height: 12px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M12 2l2.9 6.1 6.6.9-4.8 4.7 1.1 6.7L12 17.1 6.2 20.4l1.1-6.7L2.5 9l6.6-.9L12 2z'/></svg>") no-repeat center / contain;
  mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M12 2l2.9 6.1 6.6.9-4.8 4.7 1.1 6.7L12 17.1 6.2 20.4l1.1-6.7L2.5 9l6.6-.9L12 2z'/></svg>") no-repeat center / contain;
  vertical-align: -2px;
  margin-right: 3px;
}
