/* =====================================================================
   Loop and Link — "smart notebook" aesthetic
   Warm paper, ruled-line motif, ink blue + coral + gold.
   ===================================================================== */
:root {
  --paper: #FBF6EA;
  --paper-deep: #F3EAD5;
  --card: #FFFDF7;
  --ink: #1C3354;
  --ink-soft: #5A7090;
  --line: #A9C7E4;
  --accent: #FF6B4A;
  --accent-deep: #E5532F;
  --green: #2FAE74;
  --gold: #FFB627;
  --blue: #2563C4;
  --c-unit1: #2563C4;
  --c-unit2: #E459A6;
  --c-unit3: #E5532F;
  --c-unit4: #2FAE74;
  --c-unit5: #8B5CD6;
  --c-unit6: #D98E04;
  --c-unit7: #0FA3A8;
  --radius: 22px;
  --shadow: 0 5px 0 rgba(28, 51, 84, 0.85);
  --font-display: 'Fredoka', sans-serif;
  --font-body: 'Atkinson Hyperlegible', sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
  font-family: var(--font-body);
  color: var(--ink);
  background:
    repeating-linear-gradient(transparent 0 38px, rgba(169, 199, 228, 0.22) 38px 39.5px),
    radial-gradient(1200px 600px at 80% -10%, #fff8e8 0%, transparent 60%),
    var(--paper);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

.screen { display: none; min-height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

/* =================== buttons =================== */
.btn {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 600;
  padding: 0.7em 1.4em;
  border-radius: 999px;
  border: 3px solid var(--ink);
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease, opacity 0.2s;
  box-shadow: var(--shadow);
  background: var(--card);
  color: var(--ink);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: var(--card); }
.btn.danger { color: #C0392B; border-color: #C0392B; box-shadow: 0 5px 0 rgba(192, 57, 43, 0.7); }
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 1px 0 rgba(28, 51, 84, 0.85); }
.btn:disabled { opacity: 0.35; cursor: default; }

.icon-btn {
  font-size: 1.5rem;
  width: 52px; height: 52px;
  border-radius: 16px;
  border: 3px solid var(--ink);
  background: var(--card);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: grid; place-items: center;
  transition: transform 0.08s;
  color: var(--ink);
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(28,51,84,.85); }

/* =================== HOME =================== */
.home-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: clamp(16px, 3vw, 36px) clamp(16px, 4vw, 48px) 0;
  gap: 16px;
}
.logo-wrap { flex: 1; max-width: 640px; }
#logo-canvas {
  width: 100%;
  height: clamp(110px, 16vw, 170px);
  display: block;
  cursor: pointer;
}
.tagline {
  font-family: var(--font-display);
  color: var(--ink-soft);
  font-size: clamp(1rem, 2vw, 1.25rem);
  margin: 2px 0 0 8px;
}
.home-actions { display: flex; gap: 12px; align-items: center; padding-top: 8px; }
.star-bank {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--font-display);
  font-weight: 700; font-size: 1.3rem;
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: 999px;
  box-shadow: var(--shadow);
  padding: 8px 18px;
}
.star-bank-icon { color: var(--gold); font-size: 1.5rem; text-shadow: 0 1px 0 #c98a00; }

#unit-map {
  display: flex; flex-direction: column;
  gap: clamp(14px, 2vw, 22px);
  padding: clamp(16px, 3vw, 32px) clamp(16px, 4vw, 48px) 24px;
  max-width: 1080px;
  width: 100%;
  margin: 0 auto;
}

.unit-card {
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(16px, 2.5vw, 26px);
  border-left: 14px solid var(--unit-color, var(--blue));
  animation: rise 0.45s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(24px); }
  to { opacity: 1; transform: none; }
}
.unit-head { display: flex; gap: 16px; align-items: center; margin-bottom: 14px; }
.unit-icon {
  font-size: 2.2rem;
  width: 64px; height: 64px;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--unit-color) 14%, white);
  border: 3px solid var(--unit-color);
  border-radius: 18px;
  flex-shrink: 0;
}
.unit-head h2 { font-family: var(--font-display); font-size: clamp(1.15rem, 2.4vw, 1.5rem); }
.unit-head p { color: var(--ink-soft); font-size: 0.95rem; }

.lesson-row { display: flex; flex-wrap: wrap; gap: 12px; }
.lesson-node {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: none; border: none; cursor: pointer;
  font-family: var(--font-display);
  width: 104px;
  padding: 8px 4px;
  border-radius: 16px;
  transition: background 0.15s;
}
.lesson-node:not(.locked):hover { background: var(--paper-deep); }
.node-bubble {
  width: 58px; height: 58px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: var(--card);
  font-size: 1.3rem; font-weight: 700;
  box-shadow: 0 4px 0 rgba(28, 51, 84, 0.85);
  transition: transform 0.1s;
  color: var(--ink);
}
.lesson-node:not(.locked):active .node-bubble { transform: translateY(3px); box-shadow: 0 1px 0 rgba(28,51,84,.85); }
.lesson-node.done .node-bubble { background: var(--unit-color, var(--green)); color: #fff; }
.lesson-node.locked { cursor: default; opacity: 0.45; }
.lesson-node.locked .node-bubble { box-shadow: none; border-style: dashed; }
.node-title { font-size: 0.85rem; font-weight: 600; color: var(--ink); text-align: center; line-height: 1.15; }
.node-stars { font-size: 0.95rem; color: var(--gold); letter-spacing: 1px; min-height: 1.2em; }
.node-stars .dim { color: #d8d2c2; }

.home-footer {
  text-align: center;
  color: var(--ink-soft);
  font-size: 0.85rem;
  padding: 8px 0 calc(18px + env(safe-area-inset-bottom));
  margin-top: auto;
}

/* =================== LESSON =================== */
#screen-lesson {
  padding: clamp(10px, 2vw, 20px) clamp(12px, 3vw, 32px) calc(12px + env(safe-area-inset-bottom));
  max-width: 1100px; margin: 0 auto; width: 100%;
  height: 100dvh;
}
.lesson-header { display: flex; align-items: center; gap: 14px; }
.lesson-header-mid { flex: 1; text-align: center; }
#lesson-title { font-family: var(--font-display); font-size: clamp(1.2rem, 2.6vw, 1.6rem); }
#step-dots { display: flex; gap: 6px; justify-content: center; margin-top: 6px; flex-wrap: wrap; }
.dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: #ddd5c2; transition: all 0.2s;
}
.dot.past { background: var(--green); }
.dot.now { background: var(--accent); transform: scale(1.45); }

.hint-bar {
  display: flex; align-items: center; gap: 14px;
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 10px 16px;
  margin: 12px 0;
  min-height: 64px;
}
.step-badge {
  font-family: var(--font-display);
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 999px;
  color: #fff;
  background: var(--blue);
  white-space: nowrap;
  flex-shrink: 0;
}
.step-badge[data-mode='trace'], .step-badge[data-mode='ghost'] { background: var(--accent); }
.step-badge[data-mode='solo'] { background: var(--green); }
#step-text { font-size: clamp(0.95rem, 2vw, 1.1rem); flex: 1; }
.ref-wrap {
  width: 130px; height: 60px;
  flex-shrink: 0;
  background: var(--paper);
  border: 2px dashed var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.ref-wrap canvas { width: 100%; height: 100%; display: block; }

.canvas-card {
  position: relative;
  flex: 1;
  min-height: 280px;
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
#practice-canvas { width: 100%; height: 100%; display: block; touch-action: none; }

.controls { display: flex; gap: 12px; margin-top: 14px; align-items: center; }

/* ---------- feedback overlay ---------- */
.feedback {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: rgba(251, 246, 234, 0.82);
  backdrop-filter: blur(3px);
  opacity: 0; pointer-events: none;
  transition: opacity 0.22s;
}
.feedback.open { opacity: 1; pointer-events: auto; }
.feedback-card {
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 36px;
  text-align: center;
  max-width: 92%;
  transform: scale(0.9);
  transition: transform 0.25s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.feedback.open .feedback-card { transform: scale(1); }
.fb-stars { font-size: 3rem; letter-spacing: 8px; margin-bottom: 8px; }
.fb-star { color: #e3dcc8; display: inline-block; }
.fb-star.lit {
  color: var(--gold);
  text-shadow: 0 2px 0 #c98a00;
  animation: pop 0.45s cubic-bezier(0.2, 1.8, 0.4, 1) both;
}
.fb-star.big { font-size: 4.2rem; }
@keyframes pop {
  from { transform: scale(0) rotate(-40deg); }
  to { transform: scale(1) rotate(0); }
}
#fb-msg, #done-msg { font-size: 1.15rem; margin-bottom: 18px; }
.fb-buttons { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

/* =================== DONE =================== */
#screen-done { align-items: center; justify-content: center; position: relative; overflow: hidden; }
.done-card {
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 44px 56px;
  text-align: center;
  animation: rise 0.4s cubic-bezier(0.2, 0.9, 0.3, 1.3) both;
  margin: 20px;
}
.done-card h1 { font-family: var(--font-display); font-size: 2rem; margin-bottom: 10px; }

/* =================== confetti =================== */
.confetti {
  position: absolute;
  top: 55%;
  width: 12px; height: 12px;
  border-radius: 3px;
  pointer-events: none;
  z-index: 50;
  animation: blast 1.5s cubic-bezier(0.15, 0.6, 0.4, 1) both;
}
@keyframes blast {
  0% { transform: translate(0, 0) rotate(0); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) rotate(var(--rot)); opacity: 0; }
}

/* =================== settings modal =================== */
.modal {
  position: fixed; inset: 0;
  background: rgba(28, 51, 84, 0.4);
  display: grid; place-items: center;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s;
  z-index: 100;
}
.modal.open { opacity: 1; pointer-events: auto; }
.modal-card {
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
  width: min(420px, 92vw);
  display: flex; flex-direction: column; gap: 18px;
}
.modal-card h2 { font-family: var(--font-display); }
.opt-row {
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
  font-weight: 700;
}
.opt-row small { font-weight: 400; color: var(--ink-soft); }
.opt-row input[type='checkbox'] {
  width: 30px; height: 30px;
  accent-color: var(--green);
  flex-shrink: 0;
}

@media (max-width: 640px) {
  .home-header { flex-direction: column; }
  .home-actions { align-self: flex-end; }
  .ref-wrap { display: none !important; }
}
