:root {
  --bg: #FFF4E6;
  --ink: #1E2142;
  --muted: #6E7191;
  --line: #EBD9C3;
  --card: #FFFFFF;
  --coral: #FF5A4E;
  --coral-d: #D9402F;
  --navy: #2B3A78;
  --sun: #FFC83D;
  --leaf: #1FB27A;
  --leaf-l: #6FE0AF;
  --amber: #FF9F1C;
  --red: #EF3E36;
  --spent: #DCD5CB;
  --skin: #F4B88E;
  --skin-d: #E09C73;
  --hair: #3A2A22;
  --display: 'Fredoka', 'Nunito', system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background:
    radial-gradient(1100px 620px at 12% -12%, #FFE1BF 0, transparent 62%),
    radial-gradient(900px 520px at 108% 8%, #FFDDE3 0, transparent 58%),
    var(--bg);
  color: var(--ink);
  font-family: 'Nunito', system-ui, sans-serif;
  font-weight: 700;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }

.app {
  height: 100dvh;
  max-width: 1640px;
  margin: 0 auto;
  padding: 2vh 2.6vw 2.6vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-columns: minmax(0, 1fr);
  gap: 1.8vh;
}

/* ---------- barra ---------- */
.top { display: flex; align-items: center; gap: 16px; min-height: 44px; }
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--display); font-weight: 600; font-size: clamp(16px, 2.3vh, 24px); flex: 1; min-width: 0; }
.brand-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-mark { width: 1.7em; height: 1.7em; border-radius: 30%; background: var(--sun); display: grid; place-items: center; box-shadow: 0 3px 0 #E5A91D; flex: none; }
.brand-mark svg { width: 62%; fill: var(--ink); }
.round { font-family: var(--display); font-weight: 600; font-size: clamp(16px, 2.4vh, 26px); color: var(--muted); font-variant-numeric: tabular-nums; }
.round b { color: var(--ink); }
.tools { display: flex; gap: 4px; }
.icon-btn { width: 44px; height: 44px; border: 0; background: transparent; border-radius: 12px; display: grid; place-items: center; color: var(--muted); transition: background .15s, color .15s; }
.icon-btn:hover { background: #FFE6CC; color: var(--ink); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn .snd-off, .muted .icon-btn .snd-on { display: none; }
.muted .icon-btn .snd-off { display: block; }
.app:not([data-phase="play"]) .play-only { visibility: hidden; }
.app[data-phase="start"] .round { visibility: hidden; }

/* ---------- escenario ---------- */
.stage { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(300px, 1fr); gap: 2.4vw; min-height: 0; }
.scene-wrap {
  position: relative; min-height: 0; border-radius: 28px; overflow: hidden;
  background: #FFF8EF;
  box-shadow: 0 0 0 3px #F3E2CC, 0 18px 40px -22px rgba(122, 74, 28, .45);
}
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.scene-wrap.shake { animation: shake .38s ease; }
@keyframes shake { 20% { transform: translateX(-9px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(3px); } }

.console-txt { font: 700 13px ui-monospace, 'SFMono-Regular', Menlo, monospace; fill: #5CF2A6; letter-spacing: .5px; }
.low .console-txt { fill: #FFB35C; }
.out .console-txt { fill: #FF6B61; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .25; } }

.limb { fill: none; stroke-width: 11; stroke-linecap: round; stroke-linejoin: round; }
.skin { stroke: var(--skin); }
.skin-d { stroke: var(--skin-d); }
.torso { stroke: var(--coral); stroke-width: 26; stroke-linecap: round; }
.sleeve { stroke: var(--coral); stroke-width: 15; stroke-linecap: round; }
.sleeve.d { stroke: var(--coral-d); }
.shorts { stroke: var(--navy); stroke-width: 17; stroke-linecap: round; }
.shorts.d { stroke: #212D63; }
.shoe { stroke: var(--ink); stroke-width: 9; stroke-linecap: round; }
.shoe.d { stroke: #43476A; }
.hair, .ponytail, .hair-fill { fill: var(--hair); }
.face { fill: var(--skin); }
.ear { fill: var(--skin-d); }
.band { fill: none; stroke: var(--sun); stroke-width: 5; stroke-linecap: round; }
.eye { fill: var(--ink); }
.eye-closed { fill: none; stroke: var(--ink); stroke-width: 2.2; stroke-linecap: round; }
.cheek { fill: #FF8A8A; opacity: .45; transition: opacity .4s; }
.tired .cheek, .cheek.hot { opacity: .85; }
.mouth-ok { fill: none; stroke: #9C3B33; stroke-width: 2.2; stroke-linecap: round; }
.mouth-tired, .mouth-open { fill: #9C3B33; }
.mouth-tired { display: none; }
.tired .mouth-ok { display: none; }
.tired .mouth-tired { display: inline; }
.drop { fill: #7FCBFF; }

/* ciclo de carrera: las piernas van desfasadas medio ciclo y cada brazo acompaña a la pierna contraria */
.runner .leg, .runner .shin, .runner .arm, .runner .ponytail, .clock-hand { transform-box: view-box; }
.leg { transform-origin: 190px 158px; animation: thigh .6s ease-in-out infinite; }
.shin { transform-origin: 190px 198px; animation: shin .6s ease-in-out infinite; }
.arm { transform-origin: 201px 112px; animation: arm .6s ease-in-out infinite; }
.leg-b, .leg-b .shin, .arm-b { animation-delay: -.3s; }
.bob { animation: bob .3s ease-in-out infinite; }
.ponytail { transform-origin: 193px 80px; animation: pony .3s ease-in-out infinite; }
.belt { animation: belt .3s linear infinite; }
.clock-hand { transform-origin: 402px 84px; animation: spin 60s linear infinite; }

@keyframes thigh { 0%, 100% { transform: rotate(-38deg); } 25% { transform: rotate(-8deg); } 50% { transform: rotate(32deg); } 75% { transform: rotate(-6deg); } }
@keyframes shin { 0%, 100% { transform: rotate(8deg); } 25% { transform: rotate(16deg); } 50% { transform: rotate(38deg); } 70% { transform: rotate(108deg); } 85% { transform: rotate(68deg); } }
@keyframes arm { 0%, 100% { transform: rotate(34deg); } 50% { transform: rotate(-48deg); } }
@keyframes bob { 0%, 100% { transform: translateY(-4px); } 50% { transform: translateY(2px); } }
@keyframes pony { 0%, 100% { transform: rotate(-9deg); } 50% { transform: rotate(10deg); } }
@keyframes belt { to { stroke-dashoffset: 24; } }
@keyframes spin { to { transform: rotate(360deg); } }

.sweat { opacity: 0; transition: opacity .5s; }
.tired .sweat, .sweat.on { opacity: 1; }
.sweat .drop { animation: drip 1.1s ease-in infinite; }
.sweat .drop:nth-child(2) { animation-delay: -.45s; }
.sweat .drop:nth-child(3) { animation-delay: -.8s; }
@keyframes drip { 0% { transform: translateY(0); opacity: 0; } 15% { opacity: 1; } 100% { transform: translateY(18px); opacity: 0; } }

.spent { display: none; }
.out .runner { display: none; }
.out .spent { display: inline; }
.out .belt { animation-play-state: paused; }
.breath { animation: breath 1.3s ease-in-out infinite; }
@keyframes breath { 50% { transform: translateY(2.5px); } }

.floats { position: absolute; inset: 0; pointer-events: none; }
.float-loss {
  position: absolute; left: 46%; top: 16%;
  font-family: var(--display); font-weight: 700; font-size: clamp(28px, 6vh, 64px); color: var(--red);
  text-shadow: 0 3px 0 #fff, 0 0 18px #fff;
  animation: floatUp 1.25s cubic-bezier(.2, .7, .3, 1) forwards;
}
@keyframes floatUp { 0% { transform: translateY(12px) scale(.6); opacity: 0; } 18% { transform: translateY(0) scale(1.08); opacity: 1; } 70% { opacity: 1; } 100% { transform: translateY(-46px) scale(1); opacity: 0; } }

/* ---------- ATP ---------- */
.side { display: grid; grid-template-rows: auto minmax(0, 1fr); gap: 2vh; min-height: 0; }
.meter {
  background: var(--card); border-radius: 24px; padding: 2.2vh 1.6vw;
  display: flex; align-items: center; gap: 1.4vw;
  box-shadow: 0 0 0 3px #F3E2CC, 0 14px 30px -22px rgba(122, 74, 28, .5);
}
.battery { flex: 1; display: flex; align-items: center; min-width: 0; }
.cells {
  flex: 1; display: flex; gap: 5px; padding: 6px; border: 5px solid var(--ink); border-radius: 16px; min-width: 0;
  height: clamp(54px, 9vh, 96px);
}
.nub { width: 9px; height: 40%; background: var(--ink); border-radius: 0 6px 6px 0; flex: none; }
.cell {
  position: relative; flex: 1; border-radius: 9px; min-width: 0;
  display: grid; place-items: center;
  background: linear-gradient(180deg, var(--leaf-l), var(--leaf));
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, .12), inset 0 3px 0 rgba(255, 255, 255, .45);
  font-family: var(--display); font-weight: 700; color: #fff; font-size: clamp(11px, 1.9vh, 20px); letter-spacing: .3px;
  transition: background .3s, color .3s;
}
.lvl-mid .cell:not(.spent) { background: linear-gradient(180deg, #FFD36B, var(--amber)); }
.lvl-low .cell:not(.spent) { background: linear-gradient(180deg, #FF8C7A, var(--red)); animation: pulse .9s ease-in-out infinite; }
@keyframes pulse { 50% { filter: brightness(1.18); transform: scale(1.04); } }
.cell.spent { background: var(--spent); color: #A39A8E; box-shadow: inset 0 3px 6px rgba(0, 0, 0, .08); }
.cell.losing { animation: zap .5s ease; }
@keyframes zap { 0% { transform: scale(1); } 30% { transform: scale(1.18) rotate(-4deg); background: #fff; color: var(--red); } 100% { transform: scale(1); } }
.pi {
  position: absolute; top: -6px; left: 50%; white-space: nowrap;
  font-family: var(--display); font-size: clamp(14px, 2.2vh, 22px); color: var(--navy);
  animation: piOff 1.1s ease-out forwards;
}
@keyframes piOff { 0% { transform: translate(-50%, 0); opacity: 0; } 20% { opacity: 1; } 100% { transform: translate(-10%, -48px) rotate(14deg); opacity: 0; } }

.atp-count { display: flex; flex-direction: column; align-items: center; line-height: .9; font-family: var(--display); min-width: 3.4em; }
.atp-count b { font-size: clamp(40px, 8.5vh, 92px); font-weight: 700; font-variant-numeric: tabular-nums; }
.atp-count span { font-size: clamp(13px, 2vh, 20px); color: var(--muted); font-weight: 600; letter-spacing: 1px; }
.atp-count b.bump { animation: bump .45s ease; }
@keyframes bump { 35% { transform: scale(1.3); color: var(--red); } }

.clue {
  position: relative; background: var(--card); border-radius: 24px; padding: 2.6vh 1.8vw 2.6vh 1.6vw;
  display: flex; gap: 1vw; align-items: flex-start; min-height: 0; overflow: auto;
  box-shadow: 0 0 0 3px #F3E2CC, 0 14px 30px -22px rgba(122, 74, 28, .5);
}
.clue::before { content: ''; position: absolute; left: 0; top: 22px; bottom: 22px; width: 6px; border-radius: 0 6px 6px 0; background: var(--sun); }
.bulb { flex: none; width: clamp(28px, 4.4vh, 46px); height: auto; fill: #FFF1C2; stroke: #E5A91D; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; margin-top: .2em; }
.clue p { margin: 0; font-size: clamp(18px, 3.3vh, 38px); line-height: 1.28; font-weight: 800; text-wrap: pretty; }

/* ---------- palabra ---------- */
.word { --s: 64px; display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-end; column-gap: calc(var(--s) * .55); row-gap: 1.2vh; min-height: calc(var(--s) * 1.25); }
.w { display: flex; gap: calc(var(--s) * .12); }
.slot {
  width: var(--s); height: calc(var(--s) * 1.2);
  display: grid; place-items: end center; padding-bottom: calc(var(--s) * .1);
  border-bottom: max(4px, calc(var(--s) * .08)) solid var(--ink); border-radius: 3px;
  font-family: var(--display); font-weight: 700; font-size: calc(var(--s) * .82); line-height: 1;
}
.slot b { font-weight: inherit; opacity: 0; transform: translateY(30%) scale(.5); }
.slot.on b { opacity: 1; transform: none; transition: transform .35s cubic-bezier(.3, 1.6, .5, 1), opacity .2s; }
.slot.on { border-color: var(--leaf); }
.slot.missed b { color: var(--coral); }
.slot.missed { border-color: var(--coral); }
.slot.punct { border-color: transparent; }
.slot.punct b { opacity: 1; transform: none; }
.won .slot.on { animation: wave .6s ease calc(var(--i) * 55ms); }
@keyframes wave { 40% { transform: translateY(-22%); } }

/* ---------- teclado ---------- */
.kb { display: flex; flex-direction: column; align-items: center; gap: 1vh; }
.kb-row { display: flex; gap: clamp(4px, .55vw, 10px); }
.key {
  width: clamp(34px, 5.4vw, 80px); height: clamp(40px, 6.6vh, 72px);
  border: 0; border-radius: 14px; background: var(--card);
  box-shadow: 0 0 0 2px var(--line), 0 5px 0 var(--line);
  font-family: var(--display); font-weight: 600; font-size: clamp(18px, 3.2vh, 34px);
  transition: transform .08s, background .2s, color .2s, box-shadow .2s;
}
.key:hover:not(:disabled) { background: #FFF2E2; }
.key:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 0 0 2px var(--line), 0 1px 0 var(--line); }
.key:disabled { cursor: default; }
.key.hit { background: var(--leaf); color: #fff; box-shadow: 0 0 0 2px var(--leaf), 0 5px 0 #138A5D; }
.key.miss { background: #EFE7DC; color: #B3A89A; box-shadow: 0 0 0 2px #E6DCCD, 0 2px 0 #E6DCCD; transform: translateY(3px); text-decoration: line-through; text-decoration-thickness: 3px; text-decoration-color: var(--coral); }
.app:not([data-phase="play"]) .key:not(.hit):not(.miss) { opacity: .55; }

/* ---------- tarjetas ---------- */
.overlay { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; z-index: 10; background: rgba(255, 244, 230, .55); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); animation: fade .25s ease; }
.overlay[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }
.card {
  width: min(760px, 100%); max-height: calc(100dvh - 32px); overflow: auto;
  background: var(--card); border-radius: 32px; padding: clamp(24px, 5vh, 56px) clamp(20px, 4vw, 56px);
  text-align: center;
  box-shadow: 0 0 0 4px #F3E2CC, 0 40px 80px -30px rgba(122, 74, 28, .55);
  animation: pop .4s cubic-bezier(.3, 1.5, .5, 1);
}
@keyframes pop { from { transform: scale(.85) translateY(20px); opacity: 0; } }
.card h1 { font-family: var(--display); font-weight: 700; font-size: clamp(34px, 7.4vh, 78px); line-height: 1.02; margin: 0 0 3.2vh; letter-spacing: -.5px; }
.card h1 .hl { color: var(--coral); }
.card h2 { font-family: var(--display); font-weight: 700; font-size: clamp(28px, 5.6vh, 60px); line-height: 1.05; margin: 0 0 2.4vh; }
.card.win h2 { color: var(--leaf); }
.card.lose h2 { color: var(--coral); }
.rules { list-style: none; margin: 0 auto 4vh; padding: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.rules li { display: flex; align-items: center; gap: 10px; background: #FFF4E6; border-radius: 999px; padding: 10px 20px 10px 12px; font-size: clamp(15px, 2.3vh, 22px); }
.rules .chip { font-family: var(--display); font-weight: 700; border-radius: 999px; padding: 3px 12px; color: #fff; background: var(--leaf); }
.rules .chip.minus { background: var(--red); }
.rules .chip.zero { background: var(--ink); }
.result-word { font-family: var(--display); font-weight: 700; font-size: clamp(32px, 7vh, 72px); letter-spacing: 2px; line-height: 1.05; margin: 0 0 2.4vh; word-break: break-word; }
.result-word .m { color: var(--coral); }
.result-meta { display: flex; justify-content: center; align-items: center; gap: 14px; margin: 0 0 4vh; color: var(--muted); font-size: clamp(16px, 2.4vh, 24px); }
.mini { display: inline-flex; gap: 4px; padding: 4px; border: 3px solid var(--ink); border-radius: 9px; position: relative; }
.mini::after { content: ''; position: absolute; right: -8px; top: 30%; bottom: 30%; width: 5px; background: var(--ink); border-radius: 0 3px 3px 0; }
.mini i { width: clamp(14px, 2.2vh, 22px); height: clamp(20px, 3.2vh, 32px); border-radius: 4px; background: var(--leaf); }
.mini i.x { background: var(--spent); }
.primary {
  border: 0; border-radius: 999px; background: var(--coral); color: #fff;
  font-family: var(--display); font-weight: 600; font-size: clamp(20px, 3.4vh, 34px);
  padding: 1.5vh clamp(28px, 3.4vw, 54px); min-height: 52px;
  box-shadow: 0 6px 0 var(--coral-d); transition: transform .08s, box-shadow .08s, filter .15s;
  display: inline-flex; align-items: center; gap: .5em;
}
.primary:hover { filter: brightness(1.06); }
.primary:active { transform: translateY(5px); box-shadow: 0 1px 0 var(--coral-d); }
.primary svg { width: 1em; height: 1em; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.primary.small { font-size: 18px; padding: 10px 26px; min-height: 0; box-shadow: 0 4px 0 var(--coral-d); }
.ghost { border: 0; background: #FFF0DF; border-radius: 999px; padding: 10px 18px; font-weight: 800; font-size: 15px; color: var(--ink); }
.ghost:hover { background: #FFE4C6; }
.kbd-hint { margin-top: 2.2vh; color: #B3A89A; font-size: 14px; }
.kbd-hint kbd { font: 700 12px ui-monospace, Menlo, monospace; border: 1.5px solid #D9CCBA; border-bottom-width: 3px; border-radius: 6px; padding: 1px 6px; color: var(--muted); }

.score { font-family: var(--display); font-weight: 700; font-size: clamp(54px, 12vh, 130px); line-height: 1; margin: 0 0 .6vh; color: var(--leaf); }
.score.low { color: var(--coral); }
.score small { font-size: .38em; color: var(--muted); font-weight: 600; }
.score-label { color: var(--muted); font-size: clamp(16px, 2.4vh, 24px); margin: 0 0 3vh; }
.recap { list-style: none; padding: 0; margin: 0 auto 4vh; max-width: 520px; text-align: left; }
.recap li { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 1vh 4px; border-bottom: 2px dashed #F1E3D1; font-family: var(--display); font-weight: 600; font-size: clamp(17px, 2.6vh, 26px); }
.recap li:last-child { border-bottom: 0; }
.recap li.l { color: var(--coral); }
.recap .mini { padding: 3px; border-width: 2px; gap: 3px; }
.recap .mini i { width: 10px; height: 16px; border-radius: 3px; }
.recap .mini::after { right: -6px; width: 4px; }
.final-actions { display: flex; justify-content: center; gap: 12px; flex-wrap: wrap; align-items: center; }

/* ---------- editor ---------- */
.editor { border: 0; border-radius: 26px; padding: 0; width: min(760px, calc(100vw - 32px)); box-shadow: 0 40px 80px -30px rgba(30, 33, 66, .5); color: var(--ink); }
.editor::backdrop { background: rgba(30, 33, 66, .35); backdrop-filter: blur(3px); }
.editor form { padding: 26px; display: flex; flex-direction: column; gap: 14px; }
.editor h2 { margin: 0; font-family: var(--display); font-weight: 600; font-size: 26px; }
.editor textarea {
  width: 100%; min-height: 300px; max-height: 55vh; resize: vertical;
  border: 2px solid var(--line); border-radius: 16px; padding: 14px 16px;
  font: 700 16px/1.6 'Nunito', system-ui, sans-serif; color: var(--ink); background: #FFFCF8;
}
.editor textarea:focus, .editor input:focus { outline: none; border-color: var(--coral); }
.ed-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.ed-atp { display: flex; align-items: center; gap: 10px; font-weight: 800; }
.ed-atp input { width: 72px; border: 2px solid var(--line); border-radius: 12px; padding: 7px 10px; font: 800 17px 'Nunito', sans-serif; color: var(--ink); }
.ed-count { color: var(--muted); }
.ed-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }

/* ---------- pantallas chicas ---------- */
@media (max-width: 820px), (max-aspect-ratio: 4/5) {
  body { overflow: auto; }
  .app { height: auto; min-height: 100dvh; padding: 12px 16px 20px; gap: 14px; grid-template-rows: auto auto auto auto; }
  .stage { grid-template-columns: 1fr; gap: 12px; }
  .scene-wrap { height: 34vh; min-height: 200px; }
  .side { grid-template-rows: auto auto; gap: 12px; }
  .meter { padding: 12px 16px; gap: 14px; }
  .cells { height: 56px; }
  .clue { padding: 16px 18px; }
  .clue p { font-size: 19px; }
  .brand-text { display: none; }
  .key { width: calc((100vw - 32px - 9 * 5px) / 10); height: 48px; border-radius: 10px; }
  .kb-row { gap: 5px; }
}

@media (prefers-reduced-motion: reduce) {
  .scene-wrap.shake, .lvl-low .cell, .won .slot.on { animation: none; }
}
