: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);
}

* { 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 {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: flex-end;
  gap: 30px;
  padding: 16px 18px;
}

.letter-picker {
  position: relative;
  display: grid;
  gap: 10px;
  width: 65px;
  justify-items: stretch;
}

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

.letter-picker-button {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  min-width: 100%;
  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);
}

.form-control {
  display: grid;
  gap: 10px;
}

.form-label {
  color: var(--muted);
  font-weight: 800;
}

.form-tabs {
  display: flex;
  gap: 10px;
  padding: 4px;
  border: 3px solid rgba(108, 99, 255, 0.28);
  border-radius: 999px;
  background: #fff;
}

.form-tab {
  min-height: 42px;
  border-radius: 999px;
  padding: 0 16px;
  color: rgba(74, 66, 218, 0.9);
  background: transparent;
  box-shadow: none;
}

.form-tab.active,
.form-tab[aria-selected="true"] {
  color: var(--primary-dark) !important;
  background-color: rgba(108, 99, 255, 0.18) !important;
  box-shadow: inset 0 0 0 1px rgba(108, 99, 255, 0.18) !important;
}

.form-tab:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.mode-control {
  display: grid;
  gap: 10px;
  color: var(--muted);
  font-weight: 800;
}

.mode-label { direction: rtl; }

.mode-toggle {
  display: flex;
  overflow: hidden;
  min-height: 52px;
  border: 3px solid rgba(108, 99, 255, 0.28);
  border-radius: 999px;
  padding: 4px;
  gap: 10px;
  background: #fff;
}

.mode-button {
  min-height: 40px;
  border-radius: 999px;
  padding: 0 22px;
  color: rgba(74, 66, 218, 0.9);
  background: transparent;
  box-shadow: none;
  direction: rtl;
}

.mode-button.active {
  color: var(--primary-dark);
  background: rgba(108, 99, 255, 0.18);
  box-shadow: inset 0 0 0 1px rgba(108, 99, 255, 0.18);
}

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); }

.icon-button {
  display: grid;
  place-items: center;
  align-self: end;
  width: 52px;
  min-width: 52px;
  padding: 0;
  font-size: 1.7rem;
  line-height: 1;
  color: var(--primary-dark);
  background: rgba(108, 99, 255, 0.18);
  box-shadow: inset 0 0 0 1px rgba(108, 99, 255, 0.18);
}

.icon-button:active {
  transform: translateY(1px);
  background: rgba(108, 99, 255, 0.28);
  box-shadow: inset 0 0 0 1px rgba(108, 99, 255, 0.28);
}

.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; }

.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: pointer;
}

#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: 1rem;
  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: 10px;
  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 {
  color: #fff;
  background: rgba(108, 99, 255, 0.85);
}

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

@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; }
  .form-tabs { width: 100%; justify-content: space-between; }
  .form-tab { flex: 1; }
  .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; }
}

.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;
}
