:root {
  --bg: #f6f3ff;
  --bg2: #fff8ec;
  --card: #ffffff;
  --ink: #1f1d36;
  --muted: #615d80;
  --line: #e3def5;
  --brand: #5b4bd6;
  --brand-ink: #ffffff;
  --ok: #1f9d55;
  --ok-bg: #e5f7ec;
  --ko: #d64545;
  --ko-bg: #fde9e9;
  --warn: #c2410c;
  --gold: #f5a524;
  --shadow: 0 6px 0 rgba(31, 29, 54, 0.08), 0 12px 28px rgba(31, 29, 54, 0.08);
  color-scheme: light;
}
/* Colori del mondo: ricalcolati su ogni elemento che imposta --h. */
:root, [style*='--h'] {
  --fill: hsl(var(--h, 250) 85% 95%);
  --accent: hsl(var(--h, 250) 70% 42%);
  --accent-soft: hsl(var(--h, 250) 80% 88%);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #15132a;
    --bg2: #1b1830;
    --card: #221f3b;
    --ink: #f1efff;
    --muted: #aaa5cc;
    --line: #37325a;
    --brand: #8c7dff;
    --brand-ink: #15132a;
    --ok: #4ade80;
    --ok-bg: #12351f;
    --ko: #f87171;
    --ko-bg: #3d1717;
    --warn: #fb923c;
    --shadow: 0 6px 0 rgba(0, 0, 0, 0.3), 0 12px 28px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
  :root:not([data-theme='light']), :root:not([data-theme='light']) [style*='--h'] {
    --fill: hsl(var(--h, 250) 35% 22%);
    --accent: hsl(var(--h, 250) 85% 72%);
    --accent-soft: hsl(var(--h, 250) 40% 32%);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  font: 17px/1.5 Nunito, system-ui, sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at 10% 0%, var(--bg2), transparent 40%), var(--bg);
}
h1, h2, .brand, .btn, .big-expr, .display, .nota, .fb-title { font-family: 'Baloo 2', Nunito, system-ui, sans-serif; }
h1 { font-size: 1.8rem; line-height: 1.15; margin: 0.3em 0; }
h2 { font-size: 1.25rem; margin: 0 0 0.4em; }
main { max-width: 1040px; margin: 0 auto; padding: 16px 16px 48px; }
sup { line-height: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 3px solid var(--brand); outline-offset: 2px; }

.card { background: var(--card); border-radius: 22px; box-shadow: var(--shadow); padding: 20px; }
.btn {
  border: 2px solid var(--line); background: var(--card); border-radius: 14px; padding: 10px 18px;
  font-weight: 700; font-size: 1.05rem; box-shadow: 0 3px 0 var(--line); transition: transform 0.08s;
}
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--line); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; box-shadow: 0 4px 0 color-mix(in srgb, var(--brand) 60%, #000); }
.btn:disabled { opacity: 0.45; }
.btn.wide { width: 100%; margin-top: 12px; }
.btn.big { font-size: 1.3rem; padding: 14px 28px; }
.link { background: none; border: 0; color: var(--brand); text-decoration: underline; padding: 4px; font-weight: 600; }
.icon { background: var(--card); border: 2px solid var(--line); border-radius: 12px; width: 42px; height: 42px; font-size: 1.15rem; flex: none; }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.center { justify-content: center; text-align: center; }
.small { font-size: 0.9rem; color: var(--muted); }

/* barra in alto */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { background: none; border: 0; font-size: 1.4rem; font-weight: 800; display: flex; gap: 6px; align-items: center; padding: 0; }
.brand span { font-size: 1.15rem; }
.lvl { flex: 1; min-width: 0; }
.lvl-name { font-size: 0.85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.xpbar { height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; max-width: 320px; }
.xpbar div { height: 100%; background: linear-gradient(90deg, var(--gold), #ff7a59); border-radius: 99px; transition: width 0.4s; }
.xp { font-weight: 800; color: var(--gold); white-space: nowrap; }
@media (max-width: 560px) { .brand span { display: none; } }

/* benvenuto */
.welcome { max-width: 520px; text-align: center; padding-top: 8vh; }
.welcome form { display: grid; gap: 12px; text-align: left; margin-top: 20px; }
.welcome label { font-weight: 800; }
.welcome input { font: inherit; font-size: 1.2rem; padding: 12px 14px; border-radius: 12px; border: 2px solid var(--line); background: var(--bg); color: var(--ink); }
.owl { font-size: 3rem; line-height: 1; }
.owl.big { font-size: 5rem; animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-8px) rotate(-4deg); } }

/* mappa */
.hero { display: flex; gap: 12px; align-items: flex-end; margin: 8px 0 20px; }
.bubble { position: relative; background: var(--card); border-radius: 18px; padding: 14px 16px; box-shadow: var(--shadow); flex: 1; }
.bubble::before { content: ''; position: absolute; left: -10px; bottom: 14px; border: 10px solid transparent; border-right-color: var(--card); border-left: 0; }
.worlds { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.world { display: flex; flex-direction: column; border-top: 8px solid var(--accent); }
.w-head { display: flex; gap: 12px; align-items: center; }
.w-emoji { font-size: 2.4rem; width: 60px; height: 60px; display: grid; place-items: center; background: var(--fill); border-radius: 18px; flex: none; }
.w-n { font-size: 0.8rem; font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em; color: var(--accent); }
.world h2 { margin: 0; }
.w-topic { color: var(--muted); flex: 1; margin: 10px 0; }
.w-stats { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: 0.9rem; min-height: 28px; }
.w-btns { display: flex; gap: 8px; margin-top: 12px; }
.w-btns .btn { flex: 1; }
.stars { color: var(--gold); font-size: 1.4rem; letter-spacing: 2px; }
.stars .off { color: var(--line); }
footer { text-align: center; margin-top: 32px; }

/* ripasso */
.lesson .block { margin: 14px 0; border-left: 6px solid var(--accent); }
.lesson .block.trap { border-left-color: var(--warn); background: color-mix(in srgb, var(--warn) 9%, var(--card)); }
.lesson .block.tip { border-left-color: var(--gold); }
.lesson .fig { max-width: 220px; display: block; margin: 0 auto 8px; }
.lesson .fig-area { max-width: 260px; }
.lesson .numline { margin: 4px 0 8px; }
.names { border-collapse: collapse; margin-bottom: 8px; }
.names td { padding: 4px 10px; border-bottom: 1px solid var(--line); }
.names td:nth-child(odd) { font-weight: 800; color: var(--accent); text-align: right; }
.steps { background: var(--fill); border-radius: 12px; padding: 10px 14px; margin: 8px 0; font-weight: 600; }
.back { margin-bottom: 4px; }

/* gioco */
.play { max-width: 720px; }
.play-top { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.progress { flex: 1; height: 16px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress div { height: 100%; background: var(--accent); border-radius: 99px; transition: width 0.3s; }
.combo { font-weight: 800; min-width: 52px; text-align: right; opacity: 0.6; }
.combo.hot { opacity: 1; color: #f97316; animation: pop 0.4s; }
@keyframes pop { 50% { transform: scale(1.3); } }
.qcard { border-top: 8px solid var(--accent); }
.qmeta { font-size: 0.85rem; color: var(--accent); font-weight: 800; margin-bottom: 8px; }
.prompt { font-size: 1.15rem; font-weight: 600; }
.prompt .sub { display: block; margin-top: 8px; font-weight: 800; }
.claim { display: block; margin-top: 8px; padding: 10px 14px; border-radius: 12px; background: var(--fill); font-weight: 700; }
.kw { color: var(--warn); letter-spacing: 0.04em; }
.visual { display: flex; justify-content: center; margin: 14px 0 4px; }
.visual .fig { width: 100%; max-width: 250px; }
.visual .fig-area { max-width: 300px; }
.big-expr { font-size: 1.6rem; font-weight: 700; text-align: center; padding: 10px; background: var(--fill); border-radius: 14px; width: 100%; overflow-wrap: anywhere; }
.big-expr.small { font-size: 1.2rem; margin: 8px 0; }
.slot { display: inline-block; min-width: 1.6em; border-bottom: 3px dashed var(--accent); color: var(--accent); }
.answer { margin-top: 16px; }

.opts { display: grid; gap: 10px; }
.opts.inline { grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); }
.opts.inline .opt { font-size: 1.5rem; font-family: 'Baloo 2', sans-serif; text-align: center; }
.opt {
  text-align: left; padding: 14px 16px; border-radius: 14px; border: 2px solid var(--line); background: var(--card);
  font-weight: 700; box-shadow: 0 3px 0 var(--line);
}
.opt:hover:not(:disabled) { border-color: var(--accent); }
.opts.lines .opt { font-family: 'Baloo 2', sans-serif; font-size: 1.2rem; }
.opt.right, .display.right, .slots.right { border-color: var(--ok); background: var(--ok-bg); box-shadow: 0 3px 0 var(--ok); }
.opt.wrong, .display.wrong, .slots.wrong { border-color: var(--ko); background: var(--ko-bg); box-shadow: 0 3px 0 var(--ko); animation: shake 0.35s; }
.opt:disabled:not(.right):not(.wrong) { opacity: 0.55; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.numpad { max-width: 340px; margin: 0 auto; }
.display {
  font-size: 2.2rem; font-weight: 800; text-align: center; padding: 6px; border-radius: 14px;
  border: 2px solid var(--line); background: var(--bg); margin-bottom: 10px; min-height: 66px;
}
.display .unit { font-size: 1.3rem; color: var(--muted); margin-left: 6px; }
.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.key { padding: 12px 0; font-size: 1.5rem; font-weight: 800; border-radius: 14px; border: 2px solid var(--line); background: var(--card); box-shadow: 0 3px 0 var(--line); }
.key.fn { background: var(--fill); font-size: 1.2rem; }
.key:active:not(:disabled) { transform: translateY(2px); box-shadow: none; }

.slots { min-height: 60px; display: flex; flex-wrap: wrap; gap: 8px; padding: 10px; border: 2px dashed var(--accent-soft); border-radius: 14px; align-items: center; }
.slots.list, .pool.list { flex-direction: column; align-items: stretch; }
.pool { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.chip { padding: 10px 14px; border-radius: 12px; border: 2px solid var(--line); background: var(--card); font-weight: 800; font-size: 1.15rem; box-shadow: 0 3px 0 var(--line); text-align: left; }
.list .chip { font-size: 1rem; font-weight: 700; }
.chip:disabled { opacity: 0.25; }
.chip.placed { background: var(--fill); border-color: var(--accent); }
.chip .pos { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 99px; background: var(--accent); color: var(--card); font-size: 0.8rem; margin-right: 6px; }
.placeholder { color: var(--muted); font-size: 0.95rem; }

.line-wrap { cursor: pointer; touch-action: manipulation; border-radius: 14px; background: var(--fill); padding: 6px 2px; }
.line-val { text-align: center; margin: 8px 0 0; }

.hint-btn { margin-top: 12px; }
.hint { margin-top: 10px; padding: 10px 14px; background: color-mix(in srgb, var(--gold) 16%, var(--card)); border-radius: 12px; font-weight: 600; }

.feedback { margin-top: 16px; border-left: 8px solid var(--ok); animation: slide 0.25s ease-out; }
.feedback.ko { border-left-color: var(--ko); }
@keyframes slide { from { opacity: 0; transform: translateY(10px); } }
.fb-title { font-size: 1.4rem; font-weight: 800; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.feedback.ok .fb-title { color: var(--ok); }
.feedback.ko .fb-title { color: var(--ko); }
.gain { font-size: 1rem; color: var(--gold); background: color-mix(in srgb, var(--gold) 15%, var(--card)); padding: 2px 10px; border-radius: 99px; }
.explain { background: var(--fill); padding: 12px 14px; border-radius: 12px; margin-top: 8px; overflow-wrap: anywhere; }
details summary { cursor: pointer; font-weight: 700; color: var(--brand); }

/* risultati */
.result { text-align: center; max-width: 620px; margin: 20px auto; }
.res-emoji { font-size: 4rem; }
.stars-big .stars { font-size: 3rem; }
.score { font-size: 1.1rem; }
.badge { display: inline-block; background: color-mix(in srgb, var(--gold) 20%, var(--card)); padding: 4px 14px; border-radius: 99px; font-weight: 800; }
.nota { font-size: 2.6rem; font-weight: 800; }
.nota.pass { color: var(--ok); }
.nota.fail { color: var(--ko); }
.breakdown { margin: 12px auto; border-collapse: collapse; }
.breakdown td { padding: 6px 12px; border-bottom: 1px solid var(--line); text-align: left; }
.breakdown td:last-child { font-weight: 800; text-align: right; }

/* figure SVG */
.fig, .numline { display: block; width: 100%; height: auto; }
.fig text, .numline text { font-family: 'Baloo 2', sans-serif; text-anchor: middle; dominant-baseline: central; fill: var(--ink); }
.poly { fill: var(--fill); stroke: var(--ink); stroke-width: 2.5; stroke-linejoin: round; }
.tri { stroke: var(--ink); stroke-width: 1.2; stroke-dasharray: 4 3; }
.tri0 { fill: hsl(var(--h, 250) 70% 70% / 0.45); }
.tri1 { fill: hsl(calc(var(--h, 250) + 60) 70% 70% / 0.45); }
.tri2 { fill: hsl(calc(var(--h, 250) + 140) 70% 70% / 0.45); }
.spoke { stroke: var(--muted); stroke-width: 1.2; stroke-dasharray: 3 3; }
.gen { fill: var(--accent-soft); stroke: var(--ink); stroke-width: 2; stroke-linejoin: round; }
.ang { fill: var(--gold); fill-opacity: 0.85; stroke: var(--ink); stroke-width: 1; }
.ang-a { fill: #ff7a59; }
.ang-b { fill: #4cc9f0; }
.lbl { font-size: 15px; font-weight: 800; }
.lbl.small { font-size: 13px; }
.dot { fill: var(--ink); }
.cut { fill: none; stroke: var(--ko); stroke-width: 2; stroke-dasharray: 6 4; }
.axis { stroke: var(--ink); stroke-width: 3; fill: var(--ink); }
.tick { stroke: var(--ink); stroke-width: 2; }
.nl-lbl { font-size: 19px; font-weight: 700; }
.nl-lbl.zero { font-weight: 800; fill: var(--brand); }
.mark { stroke: var(--card); stroke-width: 3; }
.m-sel, .m-a { fill: var(--brand); }
.m-b { fill: var(--warn); }
.m-ok { fill: var(--ok); }
.m-ko { fill: var(--ko); }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 50; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
