// Pretence Games — DESKTOP-FIRST hi-fi prototype
// 3 theme presets (Pretence / Telenet / SCK CEN), inside browser window (SuccessFactors LMS context)

const { useState, useEffect, useMemo } = React;

/* ──────────── THEME PRESETS ────────────
   4-token white-label system: --dark · --bright · --soft · --cream
   + supporting --bright-2 (hover) and --pager (faded bright). */
const THEMES = {
  pretence: {
    label: "Pretence Games",
    "--dark":     "#2A0066",
    "--bright":   "#B934E6",
    "--bright-2": "#9C25C4",
    "--soft":     "#F0E5FA",
    "--cream":    "#F6F1EC",
    "--pager":    "#E3C8F1",
  },
  telenet: {
    label: "Telenet",
    "--dark":     "#3A1F1F",
    "--bright":   "#F5C42B",
    "--bright-2": "#D6A823",
    "--soft":     "#FEF6D8",
    "--cream":    "#FFF9EE",
    "--pager":    "#E0A88D",
  },
  sckcen: {
    label: "SCK CEN",
    "--dark":     "#3F2073",   /* aubergine — from sckcen.be footer */
    "--bright":   "#C745CC",   /* violet-magenta — search/lang dots */
    "--bright-2": "#A734AE",
    "--soft":     "#EBE0F4",
    "--cream":    "#F4EEF8",
    "--pager":    "#C7B0DA",
  },
};

/* ──────────── CONTENT PER SKIN ──────────── */
const CONTENT = {
  pretence: {
    appName: "Hupsakee Game",
    moduleName: "Middelkerke · stadsspel",
    url: "pretencify.be/hupsakee/middelkerke",
    welcomeTitle: "Welkom bij de Hupsakee Game!",
    welcomeBody: <>Dit spel leidt je rond in de straffe gemeente Middelkerke <b>langs 5 belangrijke locaties</b>. Voltooi alle opdrachten en kom meer te weten over de wondere geschiedenis van Middelkerke.</>,
    eyebrow: "Stadsspel · 5 locaties",
    overviewTitle: "Vijf locaties, één spel.",
    modules: [
      { title: "Zoek de code in het stadhuis", meta: "Locatie 1 · puzzel · ± 5 min" },
      { title: "Tijdlijn aan het strand", meta: "Locatie 2 · video + vraag · ± 4 min" },
      { title: "De legende van het Casino", meta: "Locatie 3 · verhaal · ± 3 min" },
      { title: "Naam de bekende inwoners", meta: "Locatie 4 · match-spel · ± 5 min" },
      { title: "Eindopdracht aan de pier", meta: "Locatie 5 · finale · ± 6 min" },
    ],
    videoTitle: "Introfilm",
    videoBody: "Bekijk eerst het instructiefilmpje en ontdek wie je gaat gidsen.",
    quizQ: "Wat was Middelkerke ooit in oorsprong?",
    quizOptions: ["Een vissersdorp", "Een kuuroord", "Een militaire basis", "Een havenstad"],
    quizCorrect: 0,
    quizExplain: "Middelkerke begon als kleine vissersgemeenschap voordat het in de 19de eeuw uitgroeide tot kuuroord.",
    rewardTitle: "Geweldig!",
    rewardSub: "Locatie 1 voltooid. Je gids wijst je naar de volgende plek op de kaart.",
    rewardScore: 36,
    rewardDelta: 9,
    modalH: "Hoe werkt deze game?",
    modalBody: "Vijf locaties, vijf opdrachten. Elke locatie heeft een korte uitleg, een opdracht en een belonging. Tussen de locaties krijg je tips van je gids.",
  },
  telenet: {
    appName: "Telenet Onboarding",
    moduleName: "Welcome to the team",
    url: "performance.successfactors.eu/learning/telenet/onboarding",
    welcomeTitle: "Welkom bij de Telenet Game!",
    welcomeBody: <>Dit spel leidt je doorheen je eerste weken bij Telenet <b>langs 5 belangrijke ankerpunten</b>. Voltooi alle opdrachten en ontdek meteen wie wat doet binnen het bedrijf.</>,
    eyebrow: "Onboarding · 5 modules",
    overviewTitle: "Vijf modules, één team.",
    modules: [
      { title: "Wie is Telenet?", meta: "Module 1 · video + quiz · ± 4 min" },
      { title: "Match de tools",  meta: "Module 2 · match-spel · ± 5 min" },
      { title: "Klantcase oplossen", meta: "Module 3 · scenario · ± 6 min" },
      { title: "Teamspirit-poll", meta: "Module 4 · anoniem · ± 3 min" },
      { title: "Finale: ken jij Telenet?", meta: "Module 5 · finale · ± 5 min" },
    ],
    videoTitle: "Introfilm",
    videoBody: "Bekijk eerst het instructiefilmpje en maak kennis met je buddy.",
    quizQ: "In welk jaar werd Telenet opgericht?",
    quizOptions: ["1985", "1996", "2002", "2008"],
    quizCorrect: 1,
    quizExplain: "Telenet werd opgericht in 1996 als spin-off van de Vlaamse kabeldistributie.",
    rewardTitle: "Geweldig!",
    rewardSub: "Module 1 voltooid. Je buddy stuurt je nu de volgende opdracht door.",
    rewardScore: 36,
    rewardDelta: 9,
    modalH: "Hoe werkt deze onboarding?",
    modalBody: "Vijf korte modules. Elke module bevat een filmpje, één vraag en een korte oefening. Je voortgang stroomt door naar HR.",
  },
  sckcen: {
    appName: "SCK CEN Academy",
    moduleName: "AI-geletterdheid · Module 1",
    url: "performance.successfactors.eu/learning/sckcen/ai-geletterdheid",
    welcomeTitle: "Welkom bij de AI-module van SCK CEN.",
    welcomeBody: <>Vijf korte modules over <b>AI op de werkvloer</b>. Wat het is, wat mag, welke tool waarvoor, en wie je verder helpt. Onder een kwartier ben je rond.</>,
    eyebrow: "SCK CEN Academy · AI Geletterdheid",
    overviewTitle: "Vijf modules, één pad.",
    modules: [
      { title: "Wat is AI?", meta: "Module 1 · 1:50 · video + vraag" },
      { title: "Hoe sta jij erin?", meta: "Module 2 · 1:30 · anonieme rondvraag" },
      { title: "Welke info mag erin?", meta: "Module 3 · 2:00 · video + vraag" },
      { title: "Welke tool wanneer?", meta: "Module 4 · 1:50 · video + vraag" },
      { title: "Wie helpt jou verder?", meta: "Module 5 · 1:40 · video + vraag" },
    ],
    videoTitle: "Wat is AI?",
    videoBody: "Bekijk de korte uitleg over hoe taalmodellen werken en waarom ze soms hallucineren.",
    quizQ: "Een collega vraagt aan ChatGPT om een interne SCK CEN-procedure samen te vatten. Wat doe je?",
    quizOptions: [
      "Plak gewoon de procedure — het is enkel een samenvatting.",
      "Gebruik CoPilot — die blijft binnen de SCK-omgeving.",
      "Vraag eerst toestemming aan je manager.",
      "Anonimiseer eerst alle namen en projecten.",
    ],
    quizCorrect: 1,
    quizExplain: "Zodra er SCK-info in je vraag zit, gebruik je CoPilot. Die blijft binnen de SCK-omgeving en valt onder onze ISO-afspraken.",
    rewardTitle: "Goed gedaan.",
    rewardSub: "Eerste Spark verdiend. Module 1 voltooid — door naar Hoe sta jij erin?",
    rewardScore: 1,
    rewardDelta: 1,
    modalH: "Hoe werkt deze module?",
    modalBody: "Je doorloopt vijf korte modules. Elke module heeft één filmpje en één vraag. Je antwoorden bepalen mee het AI-beleid van SCK CEN, en je Sparks stromen door naar SuccessFactors.",
  },
};

const TIP_ITEMS = {
  pretence: [
    { icon: "map",  title: "Bekijk de kaart",  desc: "Zie waar je heen moet en welke locaties je al deed." },
    { icon: "rank", title: "Vergelijk je score met andere deelnemers", desc: "Ranking-tab toont hoe jouw team scoort tegen de rest." },
    { icon: "tip",  title: "Vraag om een tip",  desc: "Vast aan een opdracht? Een tip kost punten maar zet je weer op weg." },
    { icon: "book", title: "Leer de geschiedenis", desc: "Naast elke locatie staat een korte achtergrond — handig voor de quiz." },
  ],
  telenet: [
    { icon: "map",  title: "Bekijk het overzicht", desc: "Zie welke modules je teamgenoten al hebben gedaan." },
    { icon: "rank", title: "Vergelijk je score met andere teams", desc: "Live leaderboard tijdens het event." },
    { icon: "tip",  title: "Vraag om een hint",  desc: "Vast aan een vraag? Een hint kost punten maar helpt je verder." },
    { icon: "book", title: "Leer Telenet kennen", desc: "Naast elke module staat een korte intro — handig voor de finale." },
  ],
  sckcen: [
    { icon: "map",  title: "Bekijk je leerpad", desc: "Vijf korte modules. Je voortgang bewaart automatisch in SuccessFactors." },
    { icon: "rank", title: "Verzamel Sparks", desc: "Elk juist antwoord levert een Spark op. Je manager ziet ze terug in je rapport." },
    { icon: "tip",  title: "Vraag om een toelichting", desc: "Een korte uitleg na elke vraag — geen goed of fout in de rondvraag." },
  ],
};

/* ──────────── PIXEL MASCOT ──────────── */
const MASCOT_CELLS = [
  [3,0],
  [2,1],[3,1],[4,1],[0,1],[7,1],
  [1,2],[2,2],[4,2],[5,2],
  [0,3],[1,3],[6,3],[7,3],
  [0,4],[1,4],[6,4],[7,4],
  [1,5],[2,5],[4,5],[5,5],
  [2,6],[3,6],[4,6],[0,6],[7,6],
  [3,7],
];
function Mascot({ size = 120 }) {
  const grid = 8;
  const cell = size / grid;
  return (
    <svg className="mascot" width={size} height={size} viewBox={`0 0 ${size} ${size}`} aria-hidden="true">
      {MASCOT_CELLS.map(([c, r], i) => (
        <rect key={i} x={c * cell + 1} y={r * cell + 1} width={cell - 2} height={cell - 2} rx={1} />
      ))}
      {/* center hole */}
      <rect x={cell * 3 + 1} y={cell * 3 + 1} width={cell * 2 - 2} height={cell * 2 - 2} rx={3} fill="white"/>
    </svg>
  );
}

/* ──────────── ICONS ──────────── */
const I = {
  flag: <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M4 22V4h12l-2 4 2 4H6"/><line x1="4" y1="4" x2="4" y2="22"/></svg>,
  arrowUp: <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"><line x1="12" y1="19" x2="12" y2="5"/><polyline points="5 12 12 5 19 12"/></svg>,
  arrowLeft: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><line x1="20" y1="12" x2="4" y2="12"/><polyline points="10 6 4 12 10 18"/></svg>,
  arrowRight: <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"><line x1="4" y1="12" x2="20" y2="12"/><polyline points="14 6 20 12 14 18"/></svg>,
  check: <svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>,
  play: <svg width="36" height="36" viewBox="0 0 24 24" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>,
  info: <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M3 4h14a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H9l-5 4z"/><line x1="11" y1="9.5" x2="11" y2="9.5"/><line x1="11" y1="12" x2="11" y2="14.5"/></svg>,
  infoBig: <svg width="54" height="54" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M3 4h14a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H9l-5 4z"/><line x1="10.5" y1="8.5" x2="10.5" y2="8.5"/><line x1="10.5" y1="11" x2="10.5" y2="14"/></svg>,
  x: <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>,
  chev: <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="9 18 15 12 9 6"/></svg>,
  // tip icons
  iMap:  <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><polygon points="2 6 9 3 15 6 22 3 22 19 15 22 9 19 2 22 2 6"/><line x1="9" y1="3" x2="9" y2="19"/><line x1="15" y1="6" x2="15" y2="22"/><circle cx="9" cy="11" r="2" fill="currentColor"/></svg>,
  iRank: <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="9" y="9" width="6" height="12"/><rect x="3" y="14" width="6" height="7"/><rect x="15" y="6" width="6" height="15"/><circle cx="12" cy="4" r="2"/></svg>,
  iTip:  <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M3 5h14a2 2 0 0 1 2 2v9a2 2 0 0 1-2 2H9l-5 4z"/><polygon points="11 8 12 11 15 11 12.5 13 13.5 16 11 14 8.5 16 9.5 13 7 11 10 11"/></svg>,
  iBook: <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M4 4h7v16H4z"/><path d="M11 4h2v16h-2z"/><path d="M13 4h7v16h-7z"/></svg>,
};
const TIP_ICONS = { map: I.iMap, rank: I.iRank, tip: I.iTip, book: I.iBook };

/* ──────────── TOP BAR ──────────── */
function TopBar({ content, progress = 0, score = null, scoreDelta = 0, flash = false }) {
  return (
    <div className="top-bar">
      <div className="brand-cell">
        <div className="brand-mark"><Mascot size={36}/></div>
        <div className="brand-text">
          <div className="brand-app">{content.appName}</div>
          <div className="brand-sub">{content.moduleName}</div>
        </div>
      </div>
      <div className="flag-bar" role="progressbar" aria-valuenow={progress}>
        <div className="fill" style={{ width: `calc(${progress}% - 6px)` }}></div>
        {flash && progress > 0 && (
          <div className="pulse" style={{ left: `calc(${progress}% - 4px)`, top: "50%" }}></div>
        )}
        <div className="flag-end">{I.flag}</div>
      </div>
      <div className="right-cell">
        {score !== null && (
          <div className="score-pill">
            {flash && scoreDelta > 0 && <span className="up">{I.arrowUp}</span>}
            <span>{score}</span>
          </div>
        )}
        <div className="user-chip">
          <span className="user-avatar">JD</span>
          <span>J. Dubois</span>
        </div>
      </div>
    </div>
  );
}

/* ──────────── SCREENS ──────────── */
function ScreenWelkom({ content, onNext }) {
  return (
    <>
      <div className="canvas">
        <div className="welkom">
          <div className="logo-wrap"><Mascot size={160}/></div>
          <h1>{content.welcomeTitle}</h1>
          <p>{content.welcomeBody}</p>
          <div className="pager">1 / 3</div>
        </div>
      </div>
      <div className="foot">
        <span className="foot-left">SuccessFactors-module · ~ 15 min</span>
        <button className="btn-pill" onClick={onNext}>Ga verder {I.arrowRight}</button>
      </div>
    </>
  );
}

function ScreenTips({ content, tips, onNext, onBack }) {
  return (
    <>
      <div className="canvas">
        <div className="tips-screen">
          <div className="speech-row">
            <div className="speech-mascot"><Mascot size={84}/></div>
            <div className="speech-bubble">Ontdek hier alle knoppen in het spel!</div>
          </div>
          <div className="tip-grid">
            {tips.map((t, i) => (
              <div className="tip-card" key={i}>
                <div className="tip-icon">{TIP_ICONS[t.icon]}</div>
                <div className="tip-body">
                  <div className="tip-title">{t.title}</div>
                  <div className="tip-desc">{t.desc}</div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </div>
      <div className="foot">
        <button className="btn-ghost" onClick={onBack}>{I.arrowLeft} Vorige</button>
        <button className="btn-pill" onClick={onNext}>Ga verder {I.arrowRight}</button>
      </div>
    </>
  );
}

function ScreenOverview({ content, modules, doneCount, onPick, onNext }) {
  return (
    <>
      <div className="canvas">
        <div className="overview">
          <div className="overview-head">
            <div className="eyebrow">{content.eyebrow}</div>
            <h2>{content.overviewTitle}</h2>
            <div className="overview-progress">
              <span className="pn">{doneCount}/{modules.length}</span>
              <div className="bar"><div style={{ width: `${(doneCount / modules.length) * 100}%` }}/></div>
            </div>
          </div>
          <div className="mod-list">
            {modules.map((m, i) => {
              const status = i < doneCount ? "done" : i === doneCount ? "active" : "todo";
              return (
                <div className={"mod-row " + status} key={i} onClick={() => onPick(i)}>
                  <div className="mod-num">{i + 1}</div>
                  <div>
                    <div className="mod-title">{m.title}</div>
                    <div className="mod-meta">{m.meta}</div>
                  </div>
                  <span style={{ color: "var(--bright)", opacity: 0.5 }}>{I.chev}</span>
                </div>
              );
            })}
          </div>
        </div>
      </div>
      <div className="foot">
        <span className="foot-pager">{doneCount} van {modules.length} voltooid</span>
        <button className="btn-pill" onClick={onNext}>
          {doneCount === 0 ? "Module 1 starten" : "Doorgaan"} {I.arrowRight}
        </button>
      </div>
    </>
  );
}

function ScreenVideo({ content, onNext, onBack }) {
  const [played, setPlayed] = useState(false);
  return (
    <>
      <div className="canvas">
        <div className="video-screen">
          <div className="video-frame">
            <div className="video-play-btn" onClick={() => setPlayed(true)}>
              <div className="circle">{I.play}</div>
            </div>
          </div>
          <div className="video-info">
            <h2>
              {content.videoTitle}
              <span className="info-icon">{I.info}</span>
            </h2>
            <p>{content.videoBody}</p>
          </div>
        </div>
      </div>
      <div className="foot">
        <button className="btn-ghost" onClick={onBack}>{I.arrowLeft} Vorige</button>
        <button className={"btn-pill" + (played ? "" : " disabled")} disabled={!played} onClick={onNext}>
          Vraag beantwoorden {I.arrowRight}
        </button>
      </div>
    </>
  );
}

function ScreenQuiz({ content, picked, onPick, onNext, onBack }) {
  const answered = picked != null;
  const isCorrect = picked === content.quizCorrect;
  return (
    <>
      <div className="canvas">
        <div className="quiz-screen">
          <div className="quiz-q">
            {content.quizQ}
            <span className="info-icon">{I.info}</span>
          </div>
          <div className="quiz-options">
            {content.quizOptions.map((opt, i) => {
              let state = "";
              if (answered) {
                if (i === content.quizCorrect) state = "is-correct";
                else if (i === picked) state = "is-wrong";
                else state = "is-dim";
              }
              return (
                <button key={i} className={"quiz-opt " + state} disabled={answered} onClick={() => onPick(i)}>
                  <span className="opt-letter">{String.fromCharCode(65 + i)}</span>
                  <span>{opt}</span>
                </button>
              );
            })}
          </div>
          {answered && (
            <div className={"quiz-feedback " + (isCorrect ? "correct" : "wrong")}>
              <b>{isCorrect ? "Juist." : "Niet helemaal."}</b>
              {content.quizExplain}
            </div>
          )}
        </div>
      </div>
      <div className="foot">
        <button className="btn-ghost" onClick={onBack}>{I.arrowLeft} Vorige</button>
        <button className={"btn-pill" + (answered ? "" : " disabled")} disabled={!answered} onClick={onNext}>
          {isCorrect ? "Spark verzilveren" : "Volgende"} {I.arrowRight}
        </button>
      </div>
    </>
  );
}

function ScreenReward({ content, onNext, onBack }) {
  const sparks = useMemo(() => Array.from({ length: 10 }, (_, i) => ({
    dx: (Math.random() - 0.5) * 320,
    dy: -80 - Math.random() * 120,
    delay: i * 0.1,
  })), []);
  return (
    <>
      <div className="canvas">
        <div className="reward-screen">
          <div className="reward-check">{I.check}</div>
          <h2 className="reward-h">{content.rewardTitle}</h2>
          <p className="reward-sub">{content.rewardSub}</p>
          <div className="reward-spark-pill">
            +{content.rewardDelta} {content.rewardDelta === 1 ? "Spark verdiend" : "punten"} · ⚡
          </div>
          {sparks.map((s, i) => (
            <div className="spark-burst" key={i}
              style={{ left: "50%", top: "44%", "--dx": `${s.dx}px`, "--dy": `${s.dy}px`, animationDelay: `${s.delay}s` }}></div>
          ))}
        </div>
      </div>
      <div className="foot">
        <button className="btn-ghost" onClick={onBack}>{I.arrowLeft} Vorige</button>
        <button className="btn-pill" onClick={onNext}>Volgende module {I.arrowRight}</button>
      </div>
    </>
  );
}

function ScreenModal({ content, onClose }) {
  return (
    <>
      <div className="canvas">
        <div className="modal-screen">
          <div className="modal-icon">{I.infoBig}</div>
          <div className="modal-h">{content.modalH}</div>
          <p>{content.modalBody}</p>
          <button className="btn-pill dark has-close" onClick={onClose}>
            <span className="close-x">{I.x}</span>
            Speel verder
          </button>
        </div>
      </div>
    </>
  );
}

/* ──────────── APP ──────────── */
const SCREENS = ["welkom", "tips", "overview", "video", "quiz", "reward", "modal"];
const SCREEN_LABELS = {
  welkom: "Welkom", tips: "Onboarding tips", overview: "Module-overzicht",
  video: "Introfilm", quiz: "Quiz", reward: "Reward", modal: "Info-modal",
};

function App() {
  const [theme, setTheme] = useState("sckcen");
  const [screen, setScreen] = useState("welkom");
  const [picked, setPicked] = useState(null);

  // apply theme tokens
  useEffect(() => {
    const root = document.documentElement;
    const tokens = THEMES[theme];
    Object.entries(tokens).forEach(([k, v]) => {
      if (k.startsWith("--")) root.style.setProperty(k, v);
    });
    root.dataset.theme = theme;
  }, [theme]);

  const content = CONTENT[theme];
  const tips = TIP_ITEMS[theme];

  const progress = { welkom: 0, tips: 0, overview: 6, video: 18, quiz: 28, reward: 42, modal: 6 }[screen];
  const showScore = ["overview", "video", "quiz", "reward"].includes(screen);
  const score = showScore ? (screen === "reward" ? content.rewardScore : Math.max(0, content.rewardScore - content.rewardDelta)) : null;

  const goNext = () => {
    const order = ["welkom","tips","overview","video","quiz","reward"];
    const idx = order.indexOf(screen);
    if (idx >= 0 && idx < order.length - 1) setScreen(order[idx + 1]);
    else if (screen === "modal") setScreen("welkom");
  };
  const goBack = () => {
    const order = ["welkom","tips","overview","video","quiz","reward"];
    const idx = order.indexOf(screen);
    if (idx > 0) setScreen(order[idx - 1]);
  };

  const renderScreen = () => {
    switch (screen) {
      case "welkom":   return <ScreenWelkom content={content} onNext={goNext}/>;
      case "tips":     return <ScreenTips content={content} tips={tips} onNext={goNext} onBack={goBack}/>;
      case "overview": return <ScreenOverview content={content} modules={content.modules} doneCount={1} onPick={() => setScreen("video")} onNext={() => setScreen("video")}/>;
      case "video":    return <ScreenVideo content={content} onNext={() => { setPicked(null); setScreen("quiz"); }} onBack={goBack}/>;
      case "quiz":     return <ScreenQuiz content={content} picked={picked} onPick={setPicked} onNext={() => setScreen("reward")} onBack={goBack}/>;
      case "reward":   return <ScreenReward content={content} onNext={() => setScreen("overview")} onBack={goBack}/>;
      case "modal":    return <ScreenModal content={content} onClose={() => setScreen("welkom")}/>;
      default:         return null;
    }
  };

  const appClass = "desktop-app"
    + (screen === "reward" ? " reward-bg" : "")
    + (screen === "modal"  ? " modal-bg"  : "");

  return (
    <div className="stage" data-screen-label={`${theme} · ${SCREEN_LABELS[screen]}`}>
      <aside className="stage-side">
        <div className="eyebrow">Pretence Games · Witlabel demo</div>
        <h1>Eén leermodule, drie huisstijlen.</h1>
        <p>Hetzelfde product, gewisseld tussen drie 4-token paletten. Telenet en Pretence staan als referentie; SCK CEN is de actuele pitch (kleuren uit hun website-huisstijl).</p>
        <div className="theme-row">
          {Object.entries(THEMES).map(([key, t]) => (
            <button key={key} className={"theme-chip " + (theme === key ? "active" : "")} onClick={() => setTheme(key)}>
              <span className="swatches">
                <span style={{ background: t["--dark"] }}/>
                <span style={{ background: t["--bright"] }}/>
                <span style={{ background: t["--soft"] }}/>
              </span>
              {t.label}
            </button>
          ))}
        </div>

        <div className="eyebrow" style={{ marginTop: 12 }}>Scherm</div>
        <div className="theme-row">
          {SCREENS.map(s => (
            <button key={s} className={"theme-chip " + (screen === s ? "active" : "")} onClick={() => setScreen(s)} style={{ padding: "7px 11px", fontSize: 12 }}>
              {SCREEN_LABELS[s]}
            </button>
          ))}
        </div>

        <div className="micro">
          Token-set per skin: <br/>
          <code>--dark · --bright · --soft · --cream</code><br/><br/>
          Reward-goud + status-kleuren zijn gedeeld. Geladen via SuccessFactors als SCORM/iframe-embed.
        </div>
      </aside>

      <ChromeWindow
        width={1200}
        height={760}
        url={"successfactors.eu/learning/" + theme + "/module-1"}
        tabs={[{ title: content.appName + " · " + SCREEN_LABELS[screen], active: true }]}
      >
        <div className={appClass}>
          <TopBar
            content={content}
            progress={progress}
            score={score}
            scoreDelta={content.rewardDelta}
            flash={screen === "reward"}
          />
          <div className="content">
            {renderScreen()}
          </div>
        </div>
      </ChromeWindow>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App/>);
