:root {
  --bg: #f4f7ff;
  --surface: #ffffff;
  --ink: #0f1b3d;
  --muted: #5b6785;
  --line: #dbe3f5;
  --brand: #1e3a8a;
  --brand-2: #3b82f6;
  --accent: #fbbf24;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --bad: #dc2626;
  --bad-bg: #fee2e2;
  --radius: 18px;
  --shadow: 0 6px 24px rgba(15, 27, 61, .08);
  --font-head: "Baloo 2", "Nunito", system-ui, sans-serif;
  --font-body: "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
main { flex: 1; padding-top: 24px; padding-bottom: 48px; outline: none; }
h1, h2, h3 { font-family: var(--font-head); line-height: 1.15; margin: 0 0 .4em; }
h1 { font-size: clamp(1.9rem, 4.5vw, 2.8rem); }
h2 { font-size: clamp(1.4rem, 3vw, 1.9rem); }
h3 { font-size: 1.25rem; }
a { color: var(--brand-2); }
img { max-width: 100%; }
button { font: inherit; cursor: pointer; }
.wrap { width: 100%; max-width: 1080px; margin: 0 auto; padding: 0 16px; }
.muted { color: var(--muted); }
.skip { position: absolute; left: -999px; top: 8px; background: #fff; padding: 8px 12px; border-radius: 8px; z-index: 100; }
.skip:focus { left: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Top bar */
.topbar { background: var(--brand); color: #fff; position: sticky; top: 0; z-index: 20; box-shadow: 0 2px 12px rgba(0,0,0,.15); }
.topbar-inner { display: flex; align-items: center; gap: 16px; min-height: 64px; flex-wrap: wrap; padding-top: 6px; padding-bottom: 6px; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; margin-right: auto; }
.brand-logo { font-size: 1.8rem; }
.brand-text { display: flex; flex-direction: column; line-height: 1.1; }
.brand-text strong { font-family: var(--font-head); font-size: 1.15rem; }
.brand-text small { color: #bfdbfe; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; font-size: .72rem; }
.topnav { display: flex; gap: 4px; flex-wrap: wrap; }
.topnav a { color: #dbeafe; text-decoration: none; font-weight: 700; padding: 6px 12px; border-radius: 999px; font-size: .95rem; }
.topnav a:hover, .topnav a.active { background: rgba(255,255,255,.15); color: #fff; }
.xp-pill { background: var(--accent); color: #3b2a00; font-weight: 800; padding: 6px 14px; border-radius: 999px; white-space: nowrap; }
@media (max-width: 720px) {
  .topnav { order: 3; width: 100%; justify-content: space-between; }
  .topnav a { padding: 6px 8px; font-size: .88rem; }
}

/* Hero */
.hero { background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 60%, #06b6d4 100%); color: #fff; border-radius: 28px; padding: clamp(24px, 5vw, 48px); display: grid; grid-template-columns: 1.4fr 1fr; gap: 24px; align-items: center; box-shadow: var(--shadow); }
.hero p { font-size: 1.1rem; color: #e0ecff; max-width: 52ch; }
.hero-art { font-size: clamp(3.5rem, 10vw, 6.5rem); text-align: center; line-height: 1.2; user-select: none; }
.hero-stats { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 16px; }
.stat { background: rgba(255,255,255,.15); border-radius: 14px; padding: 8px 14px; font-weight: 700; }
.stat b { font-size: 1.3rem; display: block; font-family: var(--font-head); }
@media (max-width: 720px) { .hero { grid-template-columns: 1fr; } .hero-art { order: -1; } }

/* Cards & grids */
.section-title { margin: 36px 0 14px; display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.grid { display: grid; gap: 18px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px; border: 1px solid var(--line); }
.track-card { text-decoration: none; color: inherit; display: flex; flex-direction: column; gap: 8px; border-top: 8px solid var(--c, var(--brand-2)); transition: transform .15s ease, box-shadow .15s ease; }
.track-card:hover { transform: translateY(-4px); box-shadow: 0 12px 32px rgba(15,27,61,.14); }
.track-emoji { font-size: 2.6rem; line-height: 1; }
.track-card p { margin: 0; color: var(--muted); flex: 1; }
.progress { height: 12px; background: #e8eefb; border-radius: 999px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--c, var(--brand-2)); border-radius: 999px; transition: width .4s ease; }
.progress-label { font-size: .9rem; font-weight: 700; color: var(--muted); }

/* Track page */
.crumbs { font-weight: 700; margin-bottom: 12px; font-size: .95rem; }
.crumbs a { text-decoration: none; }
.track-head { display: flex; gap: 18px; align-items: center; background: var(--surface); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow); border-left: 10px solid var(--c); flex-wrap: wrap; }
.track-head .track-emoji { font-size: 3.4rem; }
.track-head > div { flex: 1; min-width: 220px; }
.lesson-list { list-style: none; padding: 0; margin: 20px 0 0; display: grid; gap: 12px; }
.lesson-item a { display: flex; gap: 14px; align-items: center; background: var(--surface); padding: 16px 18px; border-radius: 14px; text-decoration: none; color: inherit; border: 2px solid var(--line); transition: border-color .15s; }
.lesson-item a:hover { border-color: var(--c); }
.lesson-num { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; background: #eef2ff; color: var(--brand); flex-shrink: 0; font-family: var(--font-head); font-size: 1.2rem; }
.lesson-item.done .lesson-num { background: var(--good); color: #fff; }
.lesson-item .meta { color: var(--muted); font-size: .9rem; }
.lesson-item .go { margin-left: auto; font-weight: 800; color: var(--c); white-space: nowrap; }

/* Lesson page */
.lesson { display: grid; grid-template-columns: minmax(0, 1fr); gap: 20px; max-width: 820px; margin: 0 auto; }
.lesson-content { font-size: 1.07rem; }
.lesson-content p { margin: 0 0 1em; }
.lesson-content ul, .lesson-content ol { padding-left: 1.3em; margin: 0 0 1em; }
.lesson-content li { margin-bottom: .35em; }
.callout { border-radius: 14px; padding: 14px 16px; margin: 0 0 1em; display: flex; gap: 12px; align-items: flex-start; }
.callout::before { font-size: 1.5rem; line-height: 1.2; }
.callout.tip { background: #fef9c3; } .callout.tip::before { content: "💡"; }
.callout.safe { background: #dcfce7; } .callout.safe::before { content: "🛡️"; }
.callout.fact { background: #e0f2fe; } .callout.fact::before { content: "🤓"; }
.callout.warn { background: #ffe4e6; } .callout.warn::before { content: "⚠️"; }
.callout > div > :last-child { margin-bottom: 0; }
.big-emoji { font-size: 2.6rem; text-align: center; margin: .2em 0 .5em; letter-spacing: .15em; }
code, .code { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; background: #eef2ff; border-radius: 6px; padding: 1px 6px; font-size: .92em; }
pre.code { padding: 14px 16px; overflow-x: auto; line-height: 1.5; background: #0f1b3d; color: #e0ecff; border-radius: 12px; }

.ex-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; flex-wrap: wrap; }
.dots { display: flex; gap: 6px; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: #dbe3f5; }
.dot.done { background: var(--good); }
.exercise { border-left: 8px solid var(--c, var(--brand-2)); }
.exercise.solved { border-left-color: var(--good); }
.ex-tag { display: inline-block; font-size: .78rem; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; color: var(--c, var(--brand)); background: #eef2ff; padding: 3px 10px; border-radius: 999px; margin-bottom: 8px; }
.ex-q { font-weight: 800; font-size: 1.12rem; margin: 0 0 14px; }
.options { display: grid; gap: 10px; }
.opt { text-align: left; padding: 12px 16px; border-radius: 12px; border: 2px solid var(--line); background: #fff; font-weight: 700; color: var(--ink); transition: border-color .12s, background .12s; }
.opt:hover:not(:disabled) { border-color: var(--brand-2); }
.opt.selected { border-color: var(--brand-2); background: #eff6ff; }
.opt.correct { border-color: var(--good); background: var(--good-bg); }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); }
.opt:disabled { cursor: default; }
.tf { grid-template-columns: 1fr 1fr; }

.btn { display: inline-flex; align-items: center; gap: 8px; padding: 11px 22px; border-radius: 999px; border: 0; font-weight: 800; background: var(--brand-2); color: #fff; text-decoration: none; transition: transform .1s, filter .15s; }
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.secondary { background: #e8eefb; color: var(--brand); }
.btn.good { background: var(--good); }
.btn.big { padding: 14px 28px; font-size: 1.1rem; }
.ex-actions { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; align-items: center; }
.feedback { margin-top: 14px; padding: 12px 16px; border-radius: 12px; font-weight: 700; display: none; }
.feedback.show { display: block; }
.feedback.good { background: var(--good-bg); color: #14532d; }
.feedback.bad { background: var(--bad-bg); color: #7f1d1d; }

/* Sort */
.sort-row { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 2px solid var(--line); border-radius: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.sort-row .label { flex: 1; min-width: 180px; font-weight: 700; }
.sort-row .choices { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 2px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 12px; font-weight: 800; font-size: .92rem; color: var(--ink); }
.chip.on { background: var(--brand-2); color: #fff; border-color: var(--brand-2); }
.sort-row.correct { border-color: var(--good); background: var(--good-bg); }
.sort-row.wrong { border-color: var(--bad); background: var(--bad-bg); }

/* Order */
.order-pool, .order-answer { display: flex; flex-direction: column; gap: 8px; min-height: 56px; }
.order-answer { background: #f1f5ff; border: 2px dashed #b7c6ea; border-radius: 14px; padding: 10px; margin-top: 12px; }
.order-answer:empty::before { content: "Tap the steps above in the right order…"; color: var(--muted); padding: 6px; }
.order-item { text-align: left; border: 2px solid var(--line); background: #fff; border-radius: 12px; padding: 10px 14px; font-weight: 700; color: var(--ink); }
.order-answer .order-item { border-color: #93c5fd; }
.order-answer .order-item::before { content: attr(data-n) ". "; color: var(--brand-2); font-weight: 800; }

/* Match */
.match-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.match-row:last-child { border-bottom: 0; }
.match-row .left { font-weight: 800; }
select { font: inherit; padding: 9px 10px; border-radius: 10px; border: 2px solid var(--line); background: #fff; color: var(--ink); width: 100%; }
select.correct { border-color: var(--good); background: var(--good-bg); }
select.wrong { border-color: var(--bad); background: var(--bad-bg); }
@media (max-width: 520px) { .match-row { grid-template-columns: 1fr; gap: 4px; } }

/* Password */
.pw-input { width: 100%; font: inherit; font-size: 1.1rem; padding: 12px 14px; border-radius: 12px; border: 2px solid var(--line); }
.meter { height: 14px; border-radius: 999px; background: #e8eefb; margin: 12px 0 6px; overflow: hidden; }
.meter > span { display: block; height: 100%; width: 0; transition: width .25s, background .25s; }
.checks { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 4px; }
.checks li::before { content: "⬜ "; }
.checks li.ok::before { content: "✅ "; }

/* Robot grid */
.robot-wrap { display: grid; grid-template-columns: auto 1fr; gap: 20px; align-items: start; }
@media (max-width: 680px) { .robot-wrap { grid-template-columns: 1fr; } }
.grid-board { display: grid; gap: 4px; background: #c7d4f0; padding: 6px; border-radius: 14px; width: max-content; max-width: 100%; }
.cell { width: clamp(38px, 11vw, 54px); aspect-ratio: 1; background: #f8fbff; border-radius: 8px; display: grid; place-items: center; font-size: clamp(1.3rem, 5vw, 1.8rem); position: relative; }
.cell.wall { background: #64748b; }
.cell.path { background: #dbeafe; }
.cell .bot { transition: transform .2s; display: inline-block; position: relative; }
.cell .dir { position: absolute; font-size: .8rem; color: var(--brand-2); font-weight: 900; }
.cell .dir.d0 { top: -2px; } .cell .dir.d1 { right: 1px; } .cell .dir.d2 { bottom: -2px; } .cell .dir.d3 { left: 1px; }
.cell.bonk { animation: shake .35s; background: var(--bad-bg); }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.palette, .program { display: flex; flex-wrap: wrap; gap: 8px; }
.program { min-height: 58px; background: #f1f5ff; border: 2px dashed #b7c6ea; border-radius: 14px; padding: 10px; align-content: flex-start; }
.program:empty::before { content: "Your program is empty. Tap blocks to add them."; color: var(--muted); }
.block { border: 0; border-radius: 10px; padding: 8px 12px; font-weight: 800; color: #fff; background: #3b82f6; box-shadow: 0 3px 0 rgba(0,0,0,.18); font-size: .95rem; }
.block.fwd { background: #2563eb; }
.block.left, .block.right { background: #7c3aed; }
.block.rep { background: #ea580c; }
.block.running { outline: 3px solid var(--accent); }
.program .block::after { content: " ✕"; opacity: .6; font-size: .8em; }
.robot-side h4 { margin: 0 0 6px; font-size: .95rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.robot-side > div { margin-bottom: 14px; }
.legend { font-size: .9rem; color: var(--muted); }

/* Lesson nav + completion */
.lesson-nav { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 8px; }
.complete-card { text-align: center; background: linear-gradient(135deg, #dcfce7, #e0f2fe); }
.complete-card .big-emoji { font-size: 3.5rem; }

/* Badges */
.badge { text-align: center; }
.badge .medal { font-size: 3.2rem; filter: grayscale(1); opacity: .35; }
.badge.earned .medal { filter: none; opacity: 1; }
.badge h3 { margin-top: 6px; }

/* Certificate */
.certificate { background: #fff; border: 10px double var(--brand); border-radius: 18px; padding: clamp(20px, 5vw, 48px); text-align: center; max-width: 760px; margin: 0 auto; }
.certificate .name { font-family: var(--font-head); font-size: clamp(1.8rem, 6vw, 3rem); border-bottom: 3px solid var(--accent); display: inline-block; min-width: 60%; padding: 0 12px; }
.cert-controls { max-width: 760px; margin: 0 auto 16px; display: flex; gap: 10px; flex-wrap: wrap; }
.cert-controls input { flex: 1; min-width: 180px; font: inherit; padding: 10px 14px; border-radius: 12px; border: 2px solid var(--line); }

/* Toast */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(120px); background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 999px; font-weight: 800; transition: transform .3s ease; z-index: 50; max-width: calc(100% - 32px); text-align: center; }
.toast.show { transform: translateX(-50%) translateY(0); }

.footer { background: #0f1b3d; color: #c7d2fe; padding: 22px 0; font-size: .92rem; }
.footer p { margin: 4px 0; }
.footer .muted { color: #8ea0d6; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; scroll-behavior: auto !important; } }
@media print {
  .topbar, .footer, .cert-controls, .crumbs, .skip, .toast { display: none !important; }
  body { background: #fff; }
  .certificate { border-color: #1e3a8a; }
}
