:root {
  --bg-deep: #1a1b3a;
  --bg-deep-2: #262a5e;
  --purple: #7c5cff;
  --purple-dark: #5a3fd6;
  --pink: #ff5ca8;
  --cyan: #3ee6d0;
  --yellow: #ffd23f;
  --green: #3ddc84;
  --red: #ff5c5c;
  --card-bg: #ffffff;
  --card-bg-soft: #f5f4ff;
  --text-dark: #1f1b3d;
  --text-muted: #6b6890;
  --border-soft: #e4e1ff;
  --shadow-lg: 0 12px 32px rgba(30, 20, 80, 0.25);
  --shadow-sm: 0 4px 12px rgba(30, 20, 80, 0.12);
  --radius: 16px;
  --radius-sm: 10px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, var(--bg-deep), var(--bg-deep-2));
  color: var(--text-dark);
}

button {
  font-family: inherit;
  cursor: pointer;
}

.hidden { display: none !important; }

/* ---------- Splash ---------- */
.splash {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 30%, var(--bg-deep-2), var(--bg-deep));
  z-index: 1000;
  transition: opacity 0.4s ease;
}
.splash-inner { text-align: center; color: white; }
.splash-robot {
  font-size: 4rem;
  animation: bob 1.4s ease-in-out infinite;
}
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-3deg); }
  50% { transform: translateY(-14px) rotate(3deg); }
}
.splash h1 { margin: 0.5rem 0 0.25rem; font-size: 2rem; }
.splash p { color: #c9c6f5; margin: 0 0 1rem; }
.splash-bar {
  width: 240px;
  height: 8px;
  background: rgba(255,255,255,0.15);
  border-radius: 99px;
  overflow: hidden;
  margin: 0 auto;
}
.splash-bar-fill {
  height: 100%;
  width: 10%;
  background: linear-gradient(90deg, var(--cyan), var(--purple));
  border-radius: 99px;
  transition: width 0.3s ease;
}

/* ---------- App shell ---------- */
/* height (not min-height) + overflow:hidden here is what makes each screen
   responsible for its own scrolling below, instead of long content (like an
   expanded details box) growing the whole page and pushing sibling panels
   off screen. */
.app { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1.5rem;
  background: rgba(255,255,255,0.06);
  backdrop-filter: blur(6px);
  color: white;
  position: sticky;
  top: 0;
  z-index: 50;
}
.brand { font-weight: 700; font-size: 1.15rem; display: flex; align-items: center; gap: 0.4rem; }
.stats { display: flex; align-items: center; gap: 1rem; }
.stat {
  display: flex; align-items: center; gap: 0.35rem;
  background: rgba(255,255,255,0.08);
  padding: 0.35rem 0.7rem;
  border-radius: 99px;
  font-weight: 600;
  font-size: 0.95rem;
}
#rank-name { color: var(--yellow); }

.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.55rem 1.1rem;
  font-weight: 700;
  font-size: 0.95rem;
  transition: transform 0.12s ease, box-shadow 0.12s ease, opacity .15s ease;
  box-shadow: var(--shadow-sm);
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn-primary { background: linear-gradient(135deg, var(--purple), var(--pink)); color: white; }
.btn-secondary { background: white; color: var(--text-dark); }
.btn-ghost { background: rgba(255,255,255,0.1); color: white; }
.btn-small { padding: 0.35rem 0.6rem; font-size: 0.85rem; border-radius: 8px; }

.screen-root { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* ---------- Map screen ---------- */
#map-screen { padding: 2rem 1rem 4rem; overflow-y: auto; }
.map-intro { text-align: center; color: white; margin-bottom: 2rem; }
.map-intro h2 { font-size: 1.6rem; margin-bottom: 0.25rem; }
.map-intro p { color: #c9c6f5; }

.world-block { max-width: 100%; margin: 0 auto 2.5rem; }
.world-title {
  color: white;
  font-size: 1.1rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 1.25rem;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  opacity: 0.85;
}

/* Horizontal "train journey" track: stations laid out left-to-right, connected
   by a rail line, scrolling horizontally when a world has more stations than
   fit the viewport. Labels sit in normal flow below each station, so they can
   never overlap a neighboring node. */
.world-track-wrap {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.5rem 0 0.75rem;
  scroll-snap-type: x proximity;
  -webkit-overflow-scrolling: touch;
}
.world-track {
  display: flex;
  align-items: flex-start;
  gap: 2rem;
  width: max-content;
  min-width: 100%;
  justify-content: center;
  padding: 0 2rem;
  position: relative;
}
.world-track::before {
  content: '';
  position: absolute;
  top: 42px;
  left: 2rem;
  right: 2rem;
  height: 4px;
  background: linear-gradient(90deg, rgba(255,255,255,0.1), rgba(255,255,255,0.4), rgba(255,255,255,0.1));
  border-radius: 4px;
  z-index: 0;
}
.world-track.world-track--single::before { display: none; }

.station {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  flex: 0 0 auto;
  width: 100px;
  scroll-snap-align: center;
  position: relative;
  z-index: 1;
}

.level-node {
  width: 84px; height: 84px;
  border-radius: 50%;
  border: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 1.8rem;
  position: relative;
  box-shadow: var(--shadow-lg);
  transition: transform 0.15s ease;
  flex-shrink: 0;
}
.level-node:hover:not(.locked) { transform: scale(1.08); }
.level-node.available {
  background: linear-gradient(135deg, var(--purple), var(--pink));
  animation: pulse 2s ease-in-out infinite;
}
.level-node.completed { background: linear-gradient(135deg, var(--green), var(--cyan)); }
.level-node.locked { background: #4a4772; cursor: not-allowed; opacity: 0.6; }
.level-node.boss { width: 100px; height: 100px; font-size: 2.2rem; background: linear-gradient(135deg, var(--red), var(--purple)); }
.node-check { position: absolute; top: -4px; right: -4px; background: white; border-radius: 50%; font-size: 1rem; padding: 2px; }
.node-label {
  font-size: 0.75rem; color: white; text-align: center; width: 100%;
  font-weight: 600; line-height: 1.25;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(124,92,255,0.5), var(--shadow-lg); }
  50% { box-shadow: 0 0 0 10px rgba(124,92,255,0), var(--shadow-lg); }
}

/* ---------- Challenge screen ---------- */
#challenge-screen { padding: 1.25rem; flex: 1; display: flex; flex-direction: column; gap: 1rem; min-height: 0; }
.challenge-topbar { display: flex; align-items: center; justify-content: space-between; color: white; }
.challenge-topbar h2 { margin: 0; font-size: 1.3rem; }
.challenge-tabs { display: none; gap: 0.4rem; margin-bottom: 0.5rem; }
.challenge-tabs button {
  flex: 1; background: rgba(255,255,255,0.1); color: white; border: none;
  padding: 0.55rem; border-radius: var(--radius-sm); font-weight: 600;
}
.challenge-tabs button.active { background: linear-gradient(135deg, var(--purple), var(--pink)); }

.challenge-grid {
  display: grid;
  grid-template-columns: 1.1fr 1.4fr 1.2fr;
  gap: 1rem;
  flex: 1;
  min-height: 0;
}
.panel {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 1rem;
  overflow-y: auto;
  min-height: 0;
}
.panel h3 { margin-top: 0; font-size: 1rem; color: var(--purple-dark); }

.story-text { line-height: 1.5; font-size: 0.95rem; }
.teach-box { background: var(--card-bg-soft); border-radius: var(--radius-sm); padding: 0.75rem; margin: 0.75rem 0; font-size: 0.88rem; }
.teach-box code, .story-text code { background: #ece9ff; padding: 0.1rem 0.35rem; border-radius: 4px; }
.teach-example {
  background: #1e1e2e; color: #d6d0ff; padding: 0.6rem 0.75rem; border-radius: 8px;
  font-family: 'Cascadia Code', Consolas, monospace; font-size: 0.82rem; white-space: pre-wrap; margin-top: 0.4rem;
}
.commands-box {
  background: #eef8ff; border: 1px solid #cfe9ff; border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem; margin: 0.75rem 0; font-size: 0.82rem;
}
.commands-box summary { cursor: pointer; font-weight: 700; color: var(--purple-dark); padding: 0.15rem 0; }
.commands-box summary::marker { color: var(--purple); }
.commands-box .command-row { margin-top: 0.5rem; }
.command-row { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.4rem 0.5rem; margin-bottom: 0.35rem; }
.command-row:last-child { margin-bottom: 0; }
.command-row code {
  background: #dcecff; color: #14488a; padding: 0.1rem 0.4rem; border-radius: 4px;
  font-family: 'Cascadia Code', Consolas, monospace; font-size: 0.78rem; word-break: break-word;
}
.command-row span { color: var(--text-muted); }

.hints-list { margin-top: 0.75rem; }
.hint-item {
  background: #fff8e1; border-left: 3px solid var(--yellow);
  padding: 0.5rem 0.6rem; border-radius: 6px; font-size: 0.85rem; margin-bottom: 0.4rem;
}
.hint-reveal-btn { margin-top: 0.5rem; }

.editor-panel { display: flex; flex-direction: column; min-height: 0; }
.editor-toolbar { display: flex; gap: 0.5rem; margin-bottom: 0.6rem; }
.cm-host { flex: 1; border: 1px solid var(--border-soft); border-radius: var(--radius-sm); overflow: hidden; min-height: 220px; }
.cm-editor { height: 100%; font-size: 0.92rem; }
.cm-scroller { font-family: 'Cascadia Code', Consolas, monospace !important; }

.console-output {
  margin-top: 0.6rem; background: #14142b; color: #b9f5d0;
  font-family: 'Cascadia Code', Consolas, monospace; font-size: 0.82rem;
  padding: 0.6rem; border-radius: 8px; min-height: 60px; max-height: 140px; overflow-y: auto;
  white-space: pre-wrap;
}
.console-output .err-line { color: #ff9d9d; }

.feedback-banner {
  margin-top: 0.6rem; padding: 0.6rem 0.75rem; border-radius: 8px; font-size: 0.88rem; font-weight: 600;
  display: none;
}
.feedback-banner.show { display: block; }
.feedback-banner.success { background: #e3fbea; color: #1a7a44; }
.feedback-banner.error { background: #ffecec; color: #a8282c; }

.visual-panel { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.visual-stage { width: 100%; flex: 1; display: flex; align-items: center; justify-content: center; }

/* Grid renderer */
.grid-stage {
  display: grid;
  gap: 3px;
  background: var(--border-soft);
  border-radius: 10px;
  padding: 6px;
}
.grid-cell {
  width: 38px; height: 38px;
  background: #f8f7ff;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
  position: relative;
}
.grid-cell.wall { background: #3d3a5c; }
.grid-cell.goal::after { content: '🚩'; }
.grid-cell.coin::after { content: '🪙'; font-size: 1rem; }
.grid-robot {
  position: absolute;
  font-size: 1.5rem;
  transition: left 0.28s ease, top 0.28s ease, transform 0.28s ease;
  pointer-events: none;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.3));
}
.grid-robot.bump { animation: bump 0.25s ease; }
@keyframes bump { 0%,100% { transform: scale(1); } 50% { transform: scale(0.85) rotate(8deg); } }
.speech-bubble {
  position: absolute; background: white; border: 2px solid var(--purple);
  border-radius: 10px; padding: 2px 8px; font-size: 0.7rem; font-weight: 700;
  white-space: nowrap; transform: translateY(-140%);
  animation: pop-fade 1.6s ease forwards;
}
@keyframes pop-fade { 0% { opacity:0; transform: translateY(-120%) scale(0.6);} 15% { opacity:1; transform: translateY(-140%) scale(1);} 80% { opacity:1; } 100% { opacity:0; } }

/* Canvas renderer */
.turtle-canvas-wrap { background: white; border-radius: 10px; border: 1px solid var(--border-soft); }

/* Console renderer stage */
.console-stage-box {
  background: #14142b; color: #d6d0ff; border-radius: 10px; padding: 1rem;
  font-family: 'Cascadia Code', Consolas, monospace; font-size: 0.95rem;
  width: 100%; min-height: 160px; white-space: pre-wrap;
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20,15,50,0.55);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.modal-card {
  background: white; border-radius: var(--radius); padding: 2rem;
  max-width: 380px; text-align: center; box-shadow: var(--shadow-lg);
  animation: pop-in 0.35s cubic-bezier(.2,1.4,.4,1);
}
@keyframes pop-in { 0% { transform: scale(0.6); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
.modal-card .big-icon { font-size: 3rem; margin-bottom: 0.5rem; }
.big-icon.trophy-spin {
  font-size: 4rem;
  animation: trophy-spin-in 0.9s cubic-bezier(.2,1.6,.4,1),
             trophy-float 1.8s ease-in-out 0.9s infinite;
}
@keyframes trophy-spin-in {
  0% { transform: scale(0) rotate(-180deg); opacity: 0; }
  60% { transform: scale(1.25) rotate(15deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); }
}
@keyframes trophy-float {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-8px) scale(1.06); }
}
.modal-card h2 { margin: 0 0 0.4rem; }
.modal-card p { color: var(--text-muted); margin-bottom: 1.25rem; }
.xp-tick { font-size: 1.4rem; font-weight: 800; color: var(--purple); }
.badge-earned { display: flex; align-items: center; gap: 0.5rem; justify-content: center; margin: 0.75rem 0; font-weight: 700; }
.modal-actions { display: flex; gap: 0.6rem; justify-content: center; }

/* Robot traveling from one map station to the next after completing a level */
.map-traveler {
  position: fixed;
  font-size: 2.2rem;
  z-index: 400;
  pointer-events: none;
  transform: translate(-50%, -50%);
  transition: left 1.3s cubic-bezier(.3,.6,.4,1), top 1.3s cubic-bezier(.3,.6,.4,1), transform 1.3s ease;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.35));
}

/* ---------- Global syntax reference side panel ---------- */
.syntax-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(360px, 92vw);
  height: 100vh;
  background: var(--card-bg);
  box-shadow: -8px 0 32px rgba(20,15,50,0.35);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 250;
  display: flex;
  flex-direction: column;
}
.syntax-panel.open { transform: translateX(0); }
.syntax-panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.25rem; border-bottom: 1px solid var(--border-soft);
}
.syntax-panel-head h3 { margin: 0; font-size: 1.05rem; color: var(--purple-dark); }
.syntax-panel-head .btn {
  background: var(--card-bg-soft); color: var(--text-dark); box-shadow: none;
}
.syntax-panel-head .btn:hover { background: var(--border-soft); }
.syntax-panel-body { padding: 1rem 1.25rem; overflow-y: auto; flex: 1; min-height: 0; }
.syntax-item {
  background: var(--card-bg-soft); border-radius: var(--radius-sm);
  padding: 0.6rem 0.75rem; margin-bottom: 0.85rem; font-size: 0.85rem;
}
.syntax-item summary { cursor: pointer; font-weight: 700; color: var(--text-dark); }
.syntax-item summary::marker { color: var(--purple); }
.syntax-item-desc { margin-top: 0.4rem; color: var(--text-muted); }
.syntax-item .teach-example { margin-top: 0.4rem; }

/* Confetti */
.confetti-piece {
  position: fixed; top: -10px; width: 8px; height: 14px; z-index: 300; pointer-events: none;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to { transform: translateY(110vh) rotate(720deg); opacity: 0.3; }
}

/* ---------- Responsive ---------- */
@media (max-width: 880px) {
  .challenge-grid { grid-template-columns: 1fr; }
  .challenge-tabs { display: flex; }
  .panel { display: none; }
  .panel.tab-active { display: block; }
  .editor-panel.tab-active { display: flex; }
}
@media (max-width: 560px) {
  .app-header { flex-wrap: wrap; gap: 0.5rem; }
  .stats { gap: 0.5rem; }
  .world-track { justify-content: flex-start; }
}
