:root {
  --bg: #f4f0e8;
  --surface: #fffdf9;
  --ink: #22242d;
  --muted: #6d707c;
  --line: #e3dccf;
  --stage: #e9e3d6;
  --track: #e2dbcd;
  --accent: #5b54d6;
  --accent-soft: #ebe9fb;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 10px 30px rgba(40, 30, 10, .10);
  --font: "Fredoka", ui-rounded, system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161b;
    --surface: #1e2027;
    --ink: #eceaf2;
    --muted: #9b9dab;
    --line: #2e313a;
    --stage: #101115;
    --track: #33363f;
    --accent: #8f89ff;
    --accent-soft: #2a2947;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 10px 30px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ---------- Inicio ---------- */
.start { overflow-y: auto; align-items: center; padding: 32px 16px; }
.start-inner { width: 100%; max-width: 980px; margin: auto 0; display: flex; flex-direction: column; gap: 22px; }
.brand { text-align: center; }
.logo { width: 130px; height: 130px; border-radius: 50%; border: 4px solid #fff; box-shadow: var(--shadow); object-fit: cover; }
.brand h1 { font-size: 40px; line-height: 1.1; font-weight: 600; margin: 12px 0 4px; letter-spacing: -.5px; }
.brand p { margin: 0; color: var(--muted); font-size: 17px; line-height: 1.4; }

.drop {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 22px 20px; border: 2px dashed var(--line); border-radius: 22px;
  background: var(--surface); cursor: pointer; text-align: center;
  transition: border-color .2s, background .2s, transform .2s;
}
.drop svg { color: var(--accent); margin-bottom: 4px; }
.drop strong { font-size: 20px; font-weight: 500; }
.drop span { color: var(--muted); }
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop.over { transform: scale(1.01); }

.label { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 500; }
.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.levels button {
  display: flex; flex-direction: column; gap: 2px; align-items: flex-start; text-align: left;
  padding: 12px; border-radius: 14px; border: 2px solid var(--line); background: var(--surface); cursor: pointer;
  transition: border-color .15s, background .15s;
}
.levels strong { font-weight: 500; font-size: 16px; }
.levels span { font-size: 13px; color: var(--muted); line-height: 1.3; }
.levels button[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); }

.continue {
  display: flex; align-items: center; gap: 14px; padding: 12px;
  background: var(--surface); border-radius: 18px; box-shadow: var(--shadow);
}
.continue img { width: 64px; height: 64px; object-fit: cover; border-radius: 12px; background: #fff; }
.cont-text { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.cont-text strong { font-weight: 500; font-size: 17px; }
.cont-text span { color: var(--muted); font-size: 14px; }

.btn {
  border: 0; border-radius: 12px; padding: 11px 18px; font-weight: 500; font-size: 16px; cursor: pointer;
  background: var(--track); transition: filter .15s, transform .1s;
}
.btn:hover { filter: brightness(.96); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: #fff; }

.setup { display: grid; grid-template-columns: 1fr 1.3fr; gap: 16px; align-items: end; }
.continue { max-width: 560px; width: 100%; margin: 0 auto; }

.bank h2 { font-size: 24px; font-weight: 600; margin: 6px 0 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  flex: 0 0 auto; border: 2px solid var(--line); background: var(--surface); border-radius: 99px;
  padding: 8px 16px; font-weight: 500; cursor: pointer; transition: background .15s, border-color .15s;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.bank-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; margin-top: 14px; }
.scene {
  display: flex; flex-direction: column; text-align: left; padding: 0; border: 0; border-radius: 18px; overflow: hidden;
  background: var(--surface); box-shadow: var(--shadow); cursor: pointer; transition: transform .15s;
}
.scene:hover { transform: translateY(-2px); }
.scene:active { transform: scale(.98); }
.thumb { display: block; aspect-ratio: 4 / 3; background: var(--track); }
.thumb img { display: block; width: 100%; height: 100%; object-fit: cover; animation: fade .3s ease; }
@keyframes fade { from { opacity: 0; } }
.scene-name { padding: 10px 12px 0; font-weight: 500; font-size: 16px; }
.scene-cat { padding: 0 12px 10px; font-size: 13px; color: var(--muted); }
.credits { font-size: 12px; color: var(--muted); text-align: center; margin: 18px 0 0; }
.credits a { color: inherit; }

.works h2 { font-size: 24px; font-weight: 600; margin: 6px 0 12px; }
.works-row { display: flex; gap: 18px; overflow-x: auto; padding: 16px 14px 12px 2px; }
.work {
  flex: 0 0 auto; width: 190px; display: flex; flex-direction: column; text-align: left; padding: 0; border: 0;
  border-radius: 16px; overflow: visible; background: var(--surface); box-shadow: var(--shadow); cursor: pointer;
}
.work-img { position: relative; display: block; aspect-ratio: 4 / 3; }
.work-img img { width: 100%; height: 100%; object-fit: cover; border-radius: 16px 16px 0 0; display: block; }
.work-name { padding: 8px 12px 10px; font-weight: 500; }

/* Pegatina de "terminado" */
.sticker {
  position: absolute; top: -8px; right: -8px; z-index: 1;
  display: grid; place-items: center; width: 74px; height: 74px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff3a8, #ffc93c 45%, #f59e0b);
  color: #7a3b00; font-weight: 600; font-size: 12px; line-height: 1.05; text-align: center;
  box-shadow: 0 0 0 4px #fff, 0 4px 12px rgba(0, 0, 0, .25); transform: rotate(12deg);
}
.sticker::before { content: "✓"; display: block; font-size: 22px; line-height: 1; margin-bottom: -4px; color: #1b8a3a; }
.sticker { flex-direction: column; }
.sticker.big { width: 104px; height: 104px; font-size: 15px; top: -18px; right: -18px; }
.sticker.big::before { font-size: 32px; }
.thumb { position: relative; }
.thumb .sticker { display: none; top: 8px; right: 8px; width: 64px; height: 64px; font-size: 11px; }
.scene.is-done .thumb .sticker { display: grid; }

.pts-pill {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 12px; padding: 8px 16px; border-radius: 99px;
  background: linear-gradient(135deg, #ffe08a, #ffc93c); color: #5a3300; font-weight: 500; box-shadow: var(--shadow);
}
.pts-pill strong { font-weight: 600; font-size: 18px; }
.cont-actions { display: flex; gap: 8px; }
.btn.ghost { background: transparent; border: 2px solid var(--line); color: var(--muted); }
.sec-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.sec-head h2 { margin: 6px 0 8px; font-size: 24px; font-weight: 600; }
.muted-tag { color: var(--muted); font-size: 14px; font-weight: 500; }
.sec-sub { margin: 0 0 12px; color: var(--muted); }

.retos { background: var(--surface); border-radius: 22px; padding: 16px 18px 8px; box-shadow: var(--shadow); }
.world h3 { font-size: 15px; font-weight: 500; margin: 10px 0 8px; color: var(--muted); }
.world-row { display: grid; grid-template-columns: repeat(10, 1fr); gap: 8px; margin-bottom: 8px; }
.reto {
  aspect-ratio: 1; border: 0; border-radius: 16px; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: linear-gradient(160deg, #8f89ff, #5b54d6); color: #fff; box-shadow: 0 3px 0 #3f39a8; transition: transform .1s;
}
.reto:active { transform: translateY(2px); box-shadow: 0 1px 0 #3f39a8; }
.reto .num { font-weight: 600; font-size: 19px; line-height: 1; }
.reto .rstars { font-size: 10px; letter-spacing: 1px; color: #ffe08a; }
.reto.done { background: linear-gradient(160deg, #4cd08a, #20a060); box-shadow: 0 3px 0 #157a47; }
.reto.final { background: linear-gradient(160deg, #ff9f6b, #e8506b); box-shadow: 0 3px 0 #a8304a; }
.reto.final.done { background: linear-gradient(160deg, #ffd166, #f59e0b); box-shadow: 0 3px 0 #a86b00; }
.reto.locked { background: var(--track); color: var(--muted); box-shadow: none; cursor: default; }
.reto.locked .rstars { color: transparent; }
@media (max-width: 600px) { .world-row { grid-template-columns: repeat(5, 1fr); } }

.bank-tools { display: flex; gap: 8px; margin-bottom: 12px; }
.bank-tools input {
  flex: 1; min-width: 0; font: inherit; font-size: 16px; padding: 10px 14px; border-radius: 12px;
  border: 2px solid var(--line); background: var(--surface); color: var(--ink);
}
.bank-tools input:focus { outline: none; border-color: var(--accent); }
.chip small { opacity: .7; font-size: 12px; }
.btn.more { display: block; margin: 18px auto 0; }
.work { position: relative; }
.work-meta { padding: 0 12px 10px; margin-top: -6px; font-size: 13px; color: var(--muted); }

/* ---------- Juego ---------- */
.bar {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line); z-index: 2;
}
.icon {
  width: 42px; height: 42px; flex: 0 0 auto; border-radius: 12px; border: 0; background: transparent;
  display: grid; place-items: center; cursor: pointer; transition: background .15s;
}
.icon:hover { background: var(--bg); }
.icon.on { background: var(--accent); color: #fff; }
.game-icon { width: 46px; height: 46px; border-radius: 50%; border: 2px solid #ffc93c; object-fit: cover; flex: 0 0 auto; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.mode-tag { background: #e8506b; color: #fff; font-weight: 600; font-size: 13px; padding: 4px 10px; border-radius: 99px; flex: 0 0 auto; }
.clock {
  flex: 0 0 auto; font-weight: 600; font-variant-numeric: tabular-nums; padding: 6px 10px; border-radius: 10px;
  background: var(--bg); min-width: 4.2em; text-align: center;
}
.clock::before { content: "⏱ "; }
.clock.limit::before { content: "⏳ "; }
.clock.warn { background: #ffe08a; color: #5a3300; }
.clock.danger { background: #e63946; color: #fff; animation: pulse .8s ease infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
.icon { position: relative; }
.badge {
  position: absolute; top: 2px; right: 2px; min-width: 18px; height: 18px; padding: 0 4px; border-radius: 99px;
  background: #e8506b; color: #fff; font-size: 11px; font-weight: 600; display: grid; place-items: center;
}
.progress { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; padding: 0 8px; }
#pct { font-weight: 600; min-width: 3.4em; font-variant-numeric: tabular-nums; }
.track { flex: 1; height: 8px; border-radius: 99px; background: var(--track); overflow: hidden; }
.track > div { height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .3s ease; }

.stage { flex: 1; position: relative; background: var(--stage); min-height: 0; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; }
#board.grabbing { cursor: grabbing; }
.zoom {
  position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 2px;
  background: var(--surface); border-radius: 14px; padding: 4px; box-shadow: var(--shadow);
}
@media (pointer: coarse) { .zoom #zoomIn, .zoom #zoomOut { display: none; } }

.palette {
  display: flex; gap: 10px; overflow-x: auto; overflow-y: hidden;
  padding: 14px 16px; padding-bottom: max(14px, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line);
  scrollbar-width: thin; scroll-padding: 0 16px; z-index: 2;
}
.sw {
  flex: 0 0 auto; width: 56px; height: 56px; border-radius: 50%; border: 0; padding: 4px; cursor: pointer;
  background: conic-gradient(var(--accent) calc(var(--p, 0) * 360deg), var(--track) 0);
  transition: transform .18s ease, width .4s ease, padding .4s ease, margin .4s ease, opacity .3s ease;
}
.sw .dot {
  display: grid; place-items: center; width: 100%; height: 100%; border-radius: 50%;
  background: var(--c); color: var(--fg); font-weight: 600; font-size: 17px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12);
}
.sw.sel { transform: translateY(-3px) scale(1.1); }
.sw.sel .dot { box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12), 0 0 0 3px var(--surface), 0 0 0 5px var(--ink); }
.sw .dot.small { font-size: 14px; }
.palette.many { display: grid; grid-auto-flow: column; grid-template-rows: repeat(2, 50px); grid-auto-columns: 50px; gap: 8px 10px; padding-top: 10px; }
.palette.many .sw { width: 50px; height: 50px; padding: 3px; }
.palette.many .sw .dot { font-size: 15px; }
.palette.many .sw .dot.small { font-size: 13px; }
.palette.many .sw.done { width: 50px; }
.sw.done { width: 0; padding: 0; margin-right: -10px; opacity: 0; transform: scale(.2); }

/* ---------- Capas ---------- */
.overlay {
  position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; z-index: 10;
  background: rgba(20, 18, 30, .45); backdrop-filter: blur(3px);
}
.card {
  background: var(--surface); border-radius: 22px; padding: 26px 24px; width: min(360px, 100%);
  box-shadow: var(--shadow); text-align: center;
}
.card p { margin: 14px 0 12px; font-weight: 500; }
.spinner {
  width: 42px; height: 42px; margin: 0 auto; border-radius: 50%;
  border: 4px solid var(--track); border-top-color: var(--accent); animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.done-card { width: auto; max-width: 100%; }
.done-card h2 { margin: 0 0 16px; font-weight: 600; font-size: 30px; }
.done-avatar { width: 96px; height: 96px; border-radius: 50%; object-fit: cover; margin: -70px auto 8px; display: block; box-shadow: 0 0 0 5px var(--surface), var(--shadow); }
.done-img { position: relative; display: inline-block; margin: 0 auto 20px; }
#doneCanvas, #workImg { display: block; border-radius: 10px; box-shadow: var(--shadow); }
#workImg { max-width: min(560px, calc(100vw - 80px)); max-height: 55vh; }
.done-stars { display: flex; justify-content: center; gap: 8px; margin: -6px 0 6px; }
.done-stars .star { font-size: 44px; color: var(--track); animation: starpop .5s cubic-bezier(.2, 1.8, .4, 1) both; }
.done-stars .star.on { color: #ffc93c; text-shadow: 0 2px 0 #c98a00; }
@keyframes starpop { from { transform: scale(0) rotate(-90deg); } }
.score { background: var(--bg); border-radius: 16px; padding: 12px 16px; margin: 0 auto 14px; max-width: 420px; }
.score-top { display: flex; justify-content: space-between; color: var(--muted); font-weight: 500; margin-bottom: 6px; }
.score-top b { color: var(--ink); }
.lines .line { display: flex; justify-content: space-between; font-size: 15px; padding: 2px 0; animation: linein .35s ease both; }
.lines .line span { color: var(--muted); }
@keyframes linein { from { opacity: 0; transform: translateX(-12px); } }
.pts { font-size: 46px; font-weight: 600; color: var(--accent); line-height: 1.1; margin-top: 6px; font-variant-numeric: tabular-nums; }
.pts.counting { animation: shake .12s linear infinite; }
.pts.landed { animation: land .6s cubic-bezier(.2, 1.8, .4, 1); }
@keyframes shake { 50% { transform: translateY(-2px) scale(1.03); } }
@keyframes land { 40% { transform: scale(1.3); } }
.pts-label { color: var(--muted); font-weight: 500; margin-top: -4px; }
.record {
  display: flex; align-items: center; gap: 14px; text-align: left; margin: 0 auto 14px; max-width: 420px;
  padding: 12px 16px; border-radius: 16px; color: #5a3300; background: linear-gradient(135deg, #fff1b8, #ffc93c);
  box-shadow: 0 0 0 3px #fff, 0 8px 24px rgba(245, 158, 11, .45);
}
.record.show { animation: recordin .8s cubic-bezier(.2, 1.6, .4, 1) both; }
@keyframes recordin { from { transform: scale(.2) rotate(-8deg); opacity: 0; } }
.record-badge { font-size: 40px; animation: wobble 1.2s ease-in-out infinite; }
@keyframes wobble { 50% { transform: rotate(-12deg) scale(1.1); } }
.record strong { display: block; font-size: 20px; font-weight: 600; }
.record span { font-size: 14px; }
.intro-card { max-width: 420px; width: 100%; }
.intro-list { list-style: none; padding: 0; margin: 0 0 12px; text-align: left; }
.intro-list li { padding: 8px 12px; border-radius: 10px; background: var(--bg); margin-bottom: 6px; font-weight: 500; }
.intro-best { color: var(--muted); margin: 0 0 14px !important; font-weight: 400 !important; }
.done-card { max-height: calc(100dvh - 32px); overflow-y: auto; }
.muted { color: var(--muted); margin: -10px 0 16px !important; font-weight: 400 !important; }

/* Mensajes de ánimo */
.celebrate { position: fixed; inset: 0; z-index: 30; pointer-events: none; display: flex; justify-content: center; align-items: flex-start; padding-top: 90px; }
.cel-card {
  display: flex; align-items: center; gap: 14px; background: var(--surface); color: var(--ink);
  padding: 10px 22px 10px 10px; border-radius: 99px; box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
  border: 3px solid #ffc93c; max-width: calc(100vw - 32px);
}
.cel-card img { width: 58px; height: 58px; border-radius: 50%; object-fit: cover; flex: 0 0 auto; }
.cel-card p { margin: 0; font-weight: 600; font-size: 20px; }
.celebrate.show .cel-card { animation: pop .5s cubic-bezier(.2, 1.6, .4, 1) both, out .4s ease 1.9s forwards; }
.celebrate.big { align-items: center; padding-top: 0; background: rgba(20, 18, 30, .25); }
.celebrate.big .cel-card { flex-direction: column; text-align: center; border-radius: 28px; padding: 26px 30px; gap: 12px; }
.celebrate.big .cel-card img { width: 130px; height: 130px; }
.celebrate.big .cel-card p { font-size: 28px; }
.celebrate.big.show .cel-card { animation: pop .6s cubic-bezier(.2, 1.6, .4, 1) both, out .4s ease 2.8s forwards; }
@keyframes pop { from { transform: scale(.3) translateY(-20px); opacity: 0; } }
@keyframes out { to { transform: scale(.8); opacity: 0; } }
.confetti { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 40; pointer-events: none; }
.row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: 120px; transform: translate(-50%, 12px); z-index: 20;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 700px) { .setup { grid-template-columns: 1fr; } }
@media (max-width: 480px) {
  .brand h1 { font-size: 32px; }
  .levels button { align-items: center; text-align: center; }
  .bank-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
}
.done-card .done-avatar { margin: 0 auto 6px; width: 84px; height: 84px; }

.toast { display: flex; align-items: center; gap: 10px; }
.toast img { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; margin: -4px 0 -4px -8px; }
.record-face { width: 64px; height: 64px; border-radius: 50%; object-fit: cover; box-shadow: 0 0 0 3px #fff; flex: 0 0 auto; }
.record .record-badge { font-size: 30px; order: 3; margin-left: auto; }

.btn.danger { background: #fde2e4; color: #b3261e; }
.link-btn { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 13px; padding: 4px; }
