:root {
  --bg: #fff7df;
  --card: #ffffff;
  --ink: #22223b;
  --muted: #6c6a7c;
  --primary: #6c63ff;
  --primary-dark: #4a42da;
  --trace-purple: #8757f2;
  --marker-purple: #9b72f4;
  --accent: #00b894;
  --trace: #ff7a59;
  --danger: #ff3860;
  --guide: #25233a;
  --line: #000;
  --shadow: 0 14px 34px rgba(67, 56, 202, 0.1);
  --example-word-font: "Noto Naskh Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  background:
    radial-gradient(circle at top left, rgba(108, 99, 255, 0.22), transparent 32rem),
    radial-gradient(circle at bottom right, rgba(0, 184, 148, 0.18), transparent 28rem),
    var(--bg);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button,
select { font: inherit; }

.app {
  width: min(1120px, calc(100% - 32px));
  margin: 0 auto;
  padding: 18px 0 36px;
}

.controls,
.practice-card {
  border: 1px solid rgba(34, 34, 59, 0.06);
  border-radius: 28px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: var(--shadow);
}

.controls {
  position: relative;
  display: block;
  min-height: 88px;
  padding: 18px;
}

.controls.lesson-title-mode {
  min-height: 84px;
}

.lesson-top-title {
  position: absolute;
  inset-inline: 0;
  top: 18px;
  margin: 0;
  text-align: center;
  font-size: clamp(1.1rem, 2vw, 1.35rem);
  font-weight: 900;
  color: var(--guide);
  pointer-events: none;
}

.letter-picker {
  position: absolute;
  inset-inline-start: 18px;
  top: 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  width: auto;
}

.letter-picker-heading {
  color: var(--muted);
  font-weight: 800;
  text-align: right;
  white-space: nowrap;
  line-height: 1;
}

.letter-picker-button {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 65px;
  min-width: 65px;
  min-height: 52px;
  padding: 0 10px 0 24px;
  border-radius: 12px;
  font-size: 1rem;
  color: var(--primary-dark);
  background: rgba(108, 99, 255, 0.12);
  box-shadow: none;
  border: 2px solid rgba(108, 99, 255, 0.3);
}

.letter-picker-button[aria-expanded="true"] {
  background: rgba(108, 99, 255, 0.18);
  border-color: rgba(108, 99, 255, 0.38);
}

.letter-picker-text {
  display: block;
  width: 100%;
  text-align: right;
}

.letter-picker-value {
  display: block;
  min-width: 0;
  color: var(--primary-dark);
  font-size: 1.15rem;
  line-height: 1;
}

.letter-picker-caret {
  position: absolute;
  left: 8px;
  top: 50%;
  color: rgba(74, 66, 218, 0.8);
  font-size: 0.8rem;
  transform: translateY(-50%);
  transition: transform 160ms ease;
}

.letter-picker-button[aria-expanded="true"] .letter-picker-caret {
  transform: translateY(-50%) rotate(180deg);
}

button {
  min-height: 52px;
  cursor: pointer;
  border: 0;
  border-radius: 999px;
  padding: 0 22px;
  color: #fff;
  font-weight: 900;
  background: var(--primary);
  box-shadow: 0 7px 16px rgba(108, 99, 255, 0.2);
  transition: transform 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

button:hover { transform: translateY(-2px); }

.practice-card { margin-top: 18px; padding: 20px; }

.practice-card {
  margin-top: 24px;
  padding: 24px;
  border-color: rgba(34, 34, 59, 0.12);
  background: #ffffff;
  box-shadow: 0 18px 44px rgba(34, 34, 59, 0.12);
}

.stage-wrap {
  position: relative;
  overflow: hidden;
  min-height: 360px;
  aspect-ratio: 720 / 520;
  width: 100%;
  border-radius: 24px;
  background: #fff;
  box-shadow: inset 0 0 0 1.5px rgba(108, 99, 255, 0.14);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.stage-wrap::after {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  pointer-events: none;
  border: 0 solid rgba(255, 56, 96, 0);
  border-radius: 24px;
  content: "";
  transition: border-width 160ms ease, border-color 160ms ease;
}

.stage-wrap.off-path::after { border-width: 0; border-color: transparent; }

.letter-lesson {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: block;
  padding: 24px;
  background:
    radial-gradient(circle at top right, rgba(255, 196, 96, 0.28), transparent 28rem),
    linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(255, 252, 244, 0.98));
}

.letter-lesson-panels {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
  align-items: stretch;
}

.letter-lesson[hidden] {
  display: none !important;
}

.lesson-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  padding: 16px;
  border: 2px solid rgba(108, 99, 255, 0.12);
  border-radius: 24px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.98), rgba(249, 246, 255, 0.96));
  box-shadow: 0 16px 34px rgba(34, 34, 59, 0.08);
  opacity: 0.54;
  transform: scale(0.97);
  transition: opacity 220ms ease, transform 220ms ease, box-shadow 220ms ease, border-color 220ms ease;
}

.lesson-panel.active {
  opacity: 1;
  transform: scale(1);
  border-color: rgba(135, 87, 242, 0.4);
  box-shadow: 0 24px 40px rgba(135, 87, 242, 0.14);
}

.lesson-panel.active.idle {
  animation: lessonCardPulse 1500ms ease-in-out infinite;
}

.lesson-panel.seen {
  opacity: 0.92;
}

.lesson-panel-svg-wrap {
  position: relative;
  overflow: hidden;
  min-height: 220px;
  border-radius: 18px;
  background: #fff;
  box-shadow: inset 0 0 0 1px rgba(108, 99, 255, 0.08);
}

.lesson-panel-svg {
  display: block;
  width: 100%;
  height: 100%;
}

.lesson-panel .writing-line,
.lesson-panel .lower-guide-line {
  stroke: rgba(34, 34, 59, 0.18);
  stroke-width: 3;
}

.lesson-panel .lower-guide-line {
  stroke-dasharray: 10 10;
}

.lesson-outline-path {
  fill: none;
  stroke: rgba(34, 34, 59, 0.16);
  stroke-width: 24;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.lesson-demo-path {
  fill: none;
  stroke: var(--trace-purple);
  stroke-width: 20;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: stroke 180ms ease;
}

.lesson-dot-fill {
  fill: rgba(34, 34, 59, 0.16);
  stroke: rgba(34, 34, 59, 0.16);
  stroke-width: 3;
  fill-opacity: 1;
  stroke-opacity: 1;
}

.lesson-dot-fill.complete {
  fill: var(--trace-purple);
  stroke: var(--trace-purple);
  fill-opacity: 1;
  stroke-opacity: 1;
}

.lesson-replay {
  justify-self: center;
  display: grid;
  place-items: center;
  width: 42px;
  min-width: 42px;
  min-height: 42px;
  padding: 0;
  border-radius: 999px;
  font-size: 1.2rem;
  font-weight: 1000;
  line-height: 1;
  background: rgba(108, 99, 255, 0.12);
  color: var(--primary-dark);
  box-shadow: none;
}

.lesson-replay[hidden] {
  display: none !important;
}

.lesson-word {
  margin: 0;
  text-align: center;
  font-family: var(--example-word-font);
  font-size: clamp(1.3rem, 2.8vw, 1.8rem);
  font-weight: 1000;
  color: #b7bac8;
}

.baa-word {
  font-family: var(--example-word-font);
}

.lesson-word-letter {
  color: var(--trace-purple);
}

.lesson-hand {
  pointer-events: none;
  color: var(--trace-purple);
}

.lesson-hand-icon {
  opacity: 0;
  transform-origin: 0px 0px;
  will-change: transform, opacity;
}

.lesson-hand-art {
  fill: currentColor;
  filter: drop-shadow(0 10px 12px rgba(135, 87, 242, 0.2));
}

.lesson-hand.looping .lesson-hand-icon {
  animation: handGreetingLoop var(--hand-wave-duration, 4600ms) ease-in-out 1 forwards;
}

.lesson-panel.idle .lesson-demo-path {
  stroke: rgba(135, 87, 242, 0.55);
}

.lesson-panel.idle .lesson-dot-fill {
  fill: rgba(34, 34, 59, 0.16);
  stroke: rgba(34, 34, 59, 0.16);
  fill-opacity: 1;
  stroke-opacity: 1;
}

.stage-wrap.lesson-active .guide-svg,
.stage-wrap.lesson-active #traceCanvas,
.stage-wrap.lesson-active #ghostCanvas,
.stage-wrap.lesson-active .sparkle-layer,
.stage-wrap.lesson-active .coming-soon,
.stage-wrap.lesson-active .nudge {
  display: none !important;
}

.guide-svg,
#traceCanvas,
#ghostCanvas { display: block; width: 100%; height: auto; }

.guide-svg {
  width: 100%;
  height: 100%;
  min-height: 360px;
}

#traceCanvas,
#ghostCanvas {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  height: 100%;
}

#traceCanvas { z-index: 4; cursor: crosshair; }

#ghostCanvas { z-index: 3; pointer-events: none; transition: opacity 600ms ease; }
#ghostCanvas.fade { opacity: 0; }

#traceCanvas,
#ghostCanvas {
  touch-action: none;
}

.writing-line,
.lower-guide-line { stroke: var(--line); stroke-linecap: round; }
.writing-line { stroke-width: 1.5; opacity: 1; }
.top-line,
.bottom-line,
.lower-guide-line { stroke: rgba(0, 0, 0, 0.32); }
.lower-guide-line { stroke-width: 1.25; stroke-dasharray: 8 12; opacity: 1; }

.corridor-path { fill: none; stroke: transparent; stroke-linecap: round; stroke-linejoin: round; }
.path-outline,
.path-interior,
.guide-path,
.progress-path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.path-outline { stroke: var(--trace-purple); stroke-width: 22; filter: drop-shadow(0 8px 8px rgba(135, 87, 242, 0.12)); }
.path-interior { stroke: #fff; stroke-width: 17; }
.guide-path { stroke: rgba(156, 156, 156, 0.42); stroke-width: 4; stroke-dasharray: 10 10; opacity: 1; }
.progress-path { stroke: var(--trace-purple); stroke-width: 18; pointer-events: none; opacity: 1; filter: none; transition: none; }

.start-dot { fill: var(--marker-purple); filter: drop-shadow(0 8px 10px rgba(135, 87, 242, 0.28)); }
.start-bubble { transition: opacity 160ms ease; }
.start-bubble.hide { opacity: 0; }
.start-bubble.off-path-marker { opacity: 1; filter: none; }
.start-arrow { fill: none; stroke: #fff; stroke-width: 3.5; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }

.sparkle-layer {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 5;
  overflow: hidden;
  pointer-events: none;
}
.sparkle {
  position: absolute;
  color: var(--marker-purple);
  font-size: 16px;
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 0 3px #fff, 0 0 8px rgba(155, 114, 244, 0.45);
  transform: translate(-50%, -50%) scale(0.65);
  animation: sparkleFloat 420ms ease-out forwards;
}

.dot-target { display: none; fill: #fff; stroke: var(--trace-purple); stroke-width: 4; opacity: 1; }
.dot-target.ready { animation: pulseDot 900ms ease-in-out infinite; }
.dot-target.complete { fill: var(--trace-purple); stroke: var(--trace-purple); animation: none; }

.nudge,
.coach-bubble { position: absolute; z-index: 6; left: 50%; }
.nudge {
  display: none;
  top: 76px;
  pointer-events: none;
  opacity: 0;
  transform: translateX(-50%) translateY(8px);
  transition: opacity 180ms ease, transform 180ms ease;
  border-radius: 999px;
  padding: 10px 18px;
  color: #fff;
  font-weight: 900;
  background: var(--danger);
  box-shadow: 0 12px 24px rgba(255, 56, 96, 0.24);
}

.nudge.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.confetti-layer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 20;
  overflow: hidden;
  pointer-events: none;
}
.confetti-piece {
  position: absolute;
  top: -24px;
  width: 10px;
  height: 16px;
  border-radius: 3px;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.16);
  animation: confettiFall 3000ms cubic-bezier(0.12, 0.78, 0.33, 1) forwards;
}

.feedback-toast {
  position: fixed;
  z-index: 21;
  top: 24px;
  left: 50%;
  max-width: min(520px, calc(100% - 32px));
  border-radius: 999px;
  padding: 16px 24px;
  color: #fff;
  font-size: clamp(1.2rem, 4vw, 2rem);
  font-weight: 1000;
  text-align: center;
  background: linear-gradient(135deg, var(--trace-purple), var(--marker-purple));
  box-shadow: 0 18px 42px rgba(108, 99, 255, 0.24);
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(-18px);
  transition: opacity 200ms ease, transform 200ms ease;
}

.feedback-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.coming-soon {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 7;
  display: grid;
  place-content: center;
  gap: 0;
  border-radius: 24px;
  text-align: center;
  color: var(--primary-dark);
  background: rgba(255, 255, 255, 0.78);
}

.coming-soon::before {
  position: absolute;
  top: 22px;
  right: 22px;
  bottom: 22px;
  left: 22px;
  border: 2px dashed rgba(108, 99, 255, 0.35);
  border-radius: 20px;
  content: "";
}

.coming-soon span {
  position: relative;
  z-index: 1;
  display: inline-block;
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 2rem;
  font-weight: 900;
  color: var(--primary-dark);
  background: rgba(255, 255, 255, 0.9);
}

.coming-soon[hidden] {
  display: none !important;
}

.unsupported-device {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 99999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  background: rgba(255, 255, 255, 0.96);
}

.unsupported-device[hidden] {
  display: none !important;
}

body.unsupported-device-active {
  overflow: hidden;
}

.unsupported-device-panel {
  width: min(100%, 560px);
  border: 2px solid rgba(108, 99, 255, 0.22);
  border-radius: 24px;
  padding: 24px 22px;
  color: var(--primary-dark);
  background: #fff;
  box-shadow: 0 20px 42px rgba(34, 34, 59, 0.12);
}

.unsupported-device-panel h2 {
  margin: 0 0 12px;
  font-size: clamp(1.6rem, 3.8vw, 2.2rem);
  line-height: 1.2;
}

.unsupported-device-panel p {
  margin: 0;
  font-size: clamp(1rem, 2.5vw, 1.2rem);
  line-height: 1.6;
}

.unsupported-device-panel p + p {
  margin-top: 10px;
}

.unsupported-device-debug {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid rgba(34, 34, 59, 0.12);
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.45;
  text-align: left;
  word-break: break-word;
}

.stage-wrap.coming-soon-state .guide-svg,
.stage-wrap.coming-soon-state #traceCanvas,
.stage-wrap.coming-soon-state #ghostCanvas,
.stage-wrap.coming-soon-state .sparkle-layer,
.stage-wrap.coming-soon-state .nudge {
  display: none !important;
}

.letter-modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: rgba(15, 12, 40, 0.92);
}

.letter-modal[hidden] { display: none; }

.letter-modal-header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 52px;
  padding-inline: 60px;
  color: #fff;
}

.letter-modal-header h2 {
  margin: 0;
  font-size: clamp(1.5rem, 3.4vw, 2rem);
  text-align: center;
}

.letter-modal-close {
  position: absolute;
  top: 0;
  right: 0;
  width: 52px;
  min-width: 52px;
  min-height: 52px;
  border-radius: 14px;
  padding: 0;
  font-size: 2rem;
  line-height: 1;
}

.letter-grid {
  flex: 1;
  overflow: auto;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(72px, 1fr));
  gap: 10px;
  align-content: start;
}

.letter-tile {
  min-height: 72px;
  border-radius: 14px;
  font-size: 2rem;
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
  box-shadow: none;
}

.letter-tile.active,
.letter-tile:hover { background: rgba(108, 99, 255, 0.85); }

@keyframes pulseDot {
  50% { fill: rgba(135, 87, 242, 0.16); stroke-width: 5; }
}

@keyframes lessonCardPulse {
  0%, 100% {
    transform: scale(1);
    border-color: rgba(135, 87, 242, 0.4);
    box-shadow: 0 24px 40px rgba(135, 87, 242, 0.14);
  }
  50% {
    transform: scale(1.02);
    border-color: rgba(135, 87, 242, 0.62);
    box-shadow: 0 28px 52px rgba(135, 87, 242, 0.24);
  }
}

@keyframes handGreetingLoop {
  0%,
  8% {
    opacity: 0;
    transform: translateX(var(--hand-enter-x, 0px)) scale(0.92) rotate(0deg);
  }
  14%,
  18% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(0deg);
  }
  22% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(var(--hand-wave-rotate, 0deg));
  }
  26% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * -0.95));
  }
  30% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * 0.9));
  }
  34% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * -0.75));
  }
  38% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * 0.55));
  }
  44% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(0deg);
  }
  50%,
  56% {
    opacity: 0;
    transform: translateX(var(--hand-enter-x, 0px)) scale(0.94) rotate(0deg);
  }
  62%,
  66% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(0deg);
  }
  70% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(var(--hand-wave-rotate, 0deg));
  }
  74% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * -0.95));
  }
  78% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * 0.9));
  }
  82% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * -0.75));
  }
  86% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(calc(var(--hand-wave-rotate, 0deg) * 0.55));
  }
  92% {
    opacity: 1;
    transform: translateX(0px) scale(1) rotate(0deg);
  }
  100% {
    opacity: 0;
    transform: translateX(var(--hand-enter-x, 0px)) scale(0.94) rotate(0deg);
  }
}

@keyframes sparkleFloat {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.45) rotate(0deg); }
  30% { opacity: 1; }
  100% { opacity: 0; transform: translate(calc(-50% + var(--sparkle-x)), calc(-50% + var(--sparkle-y))) scale(1.05) rotate(32deg); }
}

@keyframes confettiFall {
  0% { opacity: 1; transform: translate3d(0, 0, 0) rotate(0deg); }
  100% { opacity: 0; transform: translate3d(var(--confetti-x), 110vh, 0) rotate(var(--confetti-rotate)); }
}

@media (max-width: 540px) {
  .app { width: min(100% - 20px, 1120px); padding: 20px 0; }
  .controls,
  .practice-card { border-radius: 20px; padding: 12px; }
  .letter-picker {
    inset-inline-start: 12px;
    top: 12px;
  }
  .nudge { width: calc(100% - 36px); text-align: center; }
  .letter-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .unsupported-device { padding: 14px; }
  .unsupported-device-panel { padding: 20px 16px; }
  .letter-lesson { padding: 16px; gap: 14px; }
  .letter-lesson-panels { grid-template-columns: 1fr; }
  .lesson-panel-svg-wrap { min-height: 170px; }
  .lesson-word { font-size: 1.5rem; }
}

.controls.session-student-locked {
  opacity: 0.92;
}

.session-setup-locked button,
.session-setup-locked [role="tab"] {
  cursor: not-allowed;
}

.remote-trace-canvas {
  position: absolute;
  inset: 0;
  z-index: 5;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.remote-cursor {
  position: absolute;
  z-index: 6;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  border: 3px solid #0f8f77;
  background: rgba(15, 143, 119, 0.18);
  transform: translate(-50%, -50%);
  pointer-events: none;
  box-shadow: 0 0 0 6px rgba(15, 143, 119, 0.12);
  transition: left 60ms linear, top 60ms linear, opacity 120ms ease;
}

.remote-cursor.hidden {
  opacity: 0;
}

.remote-status-chip {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 7;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(15, 143, 119, 0.14);
  color: #0d6153;
  font-size: 0.92rem;
  font-weight: 800;
  pointer-events: none;
}

.remote-status-chip[data-state="off_path"],
.remote-status-chip[data-state="snap_back"],
.remote-status-chip[data-state="restart"] {
  background: rgba(255, 122, 89, 0.18);
  color: #8d3f2a;
}
