:root {
  --bg: #fff7e8;
  --ink: #1f1b16;
  --muted: #6d655b;
  --tile: #efe9dc;
  --tile-sel: #4a4458;
  --line: #e2d8c6;
  --green: #7ccc5c;
  --blue: #7fb3f2;
  --yellow: #f8d54a;
  --red: #f4726c;
  --accent: #ff8a3d;
  color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Nunito", system-ui, sans-serif;
  font-weight: 600;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }

/* Header */
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 12px 10px;
  border-bottom: 2px solid var(--line);
  background: #fff;
}
.top h1 {
  font-family: "Fredoka", sans-serif; font-weight: 700;
  font-size: clamp(18px, 5vw, 26px); margin: 0; line-height: 1.05; text-align: center;
}
.top h1 span { color: var(--accent); }
.top-right { display: flex; gap: 2px; }
.icon-btn {
  background: none; border: 0; font-size: 22px; width: 40px; height: 40px;
  border-radius: 12px; display: grid; place-items: center;
}
.icon-btn:active { background: var(--tile); }

main { max-width: 560px; margin: 0 auto; padding: 10px 12px 40px; }
.subhead { text-align: center; color: var(--muted); font-size: 14px; margin: 2px 0 6px; }
.subhead b { color: var(--accent); }

/* Avatar */
.avatar-row { display: flex; align-items: center; gap: 12px; margin: 6px 0 12px; min-height: 96px; }
.avatar {
  position: relative; z-index: 5; flex: 0 0 88px; width: 88px; height: 88px;
  transform-origin: 50% 100%;
}
.avatar img {
  width: 100%; height: 100%; object-fit: cover; border-radius: 50%;
  border: 4px solid #fff; box-shadow: 0 4px 0 var(--line), 0 6px 18px rgba(0,0,0,.12);
  transition: filter .3s;
}
.avatar[data-mood="idle"] { animation: bob 2.6s ease-in-out infinite; }
.avatar[data-mood="celebrate"] { animation: jump .9s cubic-bezier(.3,1.6,.5,1) 2; }
.avatar[data-mood="celebrate"] img { border-color: var(--yellow); }
.avatar[data-mood="puzzled"] { animation: tilt 1.1s ease-in-out; }
.avatar[data-mood="cry"] { animation: sob .5s ease-in-out infinite; }
.avatar[data-mood="cry"] img, .avatar[data-mood="fallen"] img { filter: saturate(.5) brightness(.95) hue-rotate(-10deg); }
.avatar[data-mood="fallen"] { animation: fall 1.1s cubic-bezier(.5,0,.7,1.3) forwards; }

.tears, .question-marks { position: absolute; inset: 0; pointer-events: none; opacity: 0; }
.avatar[data-mood="cry"] .tears, .avatar[data-mood="fallen"] .tears { opacity: 1; }
.tears i {
  position: absolute; top: 44%; width: 10px; height: 14px; background: #5aa9ff;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  animation: tear 0.9s linear infinite;
}
.tears i:first-child { left: 26%; }
.tears i:last-child { right: 26%; animation-delay: .45s; }
.avatar[data-mood="puzzled"] .question-marks { opacity: 1; }
.question-marks b {
  position: absolute; font-family: "Fredoka"; font-size: 26px; color: var(--accent);
  animation: qpop 1.1s ease-out;
}
.question-marks b:first-child { top: -14px; right: -6px; }
.question-marks b:last-child { top: -2px; right: -22px; font-size: 18px; animation-delay: .15s; }

@keyframes bob { 50% { transform: translateY(-4px) rotate(-2deg); } }
@keyframes jump {
  0% { transform: none; }
  35% { transform: translateY(-26px) rotate(-12deg) scale(1.08); }
  60% { transform: translateY(-30px) rotate(12deg) scale(1.1); }
  100% { transform: none; }
}
@keyframes tilt {
  0%,100% { transform: none; }
  20% { transform: rotate(-14deg); }
  40% { transform: rotate(10deg); }
  60% { transform: rotate(-8deg); }
  80% { transform: rotate(4deg); }
}
@keyframes sob { 50% { transform: translateY(2px) scale(.98); } }
@keyframes fall {
  0% { transform: none; }
  30% { transform: rotate(-10deg); }
  100% { transform: translate(14px, 18px) rotate(96deg); }
}
@keyframes tear { 0% { transform: translateY(0); opacity: 1; } 100% { transform: translateY(46px); opacity: 0; } }
@keyframes qpop { 0% { transform: scale(0) translateY(10px); } 60% { transform: scale(1.3); } 100% { transform: scale(1); } }

.bubble {
  position: relative; background: #fff; border: 2px solid var(--line);
  border-radius: 18px; padding: 10px 14px; font-family: "Fredoka"; font-weight: 600;
  font-size: 17px; line-height: 1.25; flex: 1; min-width: 0;
}
.bubble::before {
  content: ""; position: absolute; left: -10px; top: 50%; margin-top: -8px;
  border: 8px solid transparent; border-right-color: var(--line); border-left: 0;
}
.bubble::after {
  content: ""; position: absolute; left: -6px; top: 50%; margin-top: -6px;
  border: 6px solid transparent; border-right-color: #fff; border-left: 0;
}
.bubble.pop { animation: bubblepop .35s ease-out; }
@keyframes bubblepop { 0% { transform: scale(.9); } 60% { transform: scale(1.04); } }

/* Grid */
.grid, #solved { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
#solved { margin-bottom: 8px; grid-template-columns: 1fr; }
.tile {
  aspect-ratio: 1 / .82; border: 0; border-radius: 12px; background: var(--tile);
  font-family: "Fredoka"; font-weight: 700; text-transform: uppercase;
  font-size: clamp(13px, 4.2vw, 20px); padding: 4px; line-height: 1.05;
  display: grid; place-items: center; text-align: center; word-break: break-word;
  transition: transform .12s, background .15s, color .15s;
  user-select: none;
}
.tile.s { font-size: clamp(11px, 3.4vw, 17px); }
.tile.xs { font-size: clamp(9.5px, 2.8vw, 14px); }
.tile:active { transform: scale(.95); }
.tile.sel { background: var(--tile-sel); color: #fff; transform: scale(.97); }
.tile.hop { animation: hop .35s ease-in-out; }
.grid.shake .tile.sel { animation: shake .45s; }
@keyframes hop { 50% { transform: translateY(-10px); } }
@keyframes shake { 20%,60% { transform: translateX(-6px); } 40%,80% { transform: translateX(6px); } }

.group {
  border-radius: 12px; padding: 12px 8px; text-align: center; min-height: 76px;
  display: flex; flex-direction: column; justify-content: center;
  animation: reveal .45s cubic-bezier(.3,1.5,.5,1);
}
.group h3 { margin: 0; font-family: "Fredoka"; font-weight: 700; text-transform: uppercase; font-size: 18px; }
.group p { margin: 2px 0 0; text-transform: uppercase; font-size: 15px; }
@keyframes reveal { from { transform: scale(.85); opacity: 0; } }
.g-green { background: var(--green); }
.g-blue { background: var(--blue); }
.g-yellow { background: var(--yellow); }
.g-red { background: var(--red); }

.lives { display: flex; justify-content: center; align-items: center; gap: 8px; margin: 16px 0 12px; color: var(--muted); }
#lives { font-size: 22px; letter-spacing: 4px; min-height: 28px; }
#lives .gone { opacity: .15; filter: grayscale(1); }
#lives .lost { display: inline-block; animation: lose .6s ease-out; }
@keyframes lose { 0% { transform: scale(1.6) rotate(-20deg); } }

.controls { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.pill {
  border: 2px solid var(--ink); background: #fff; border-radius: 999px;
  padding: 11px 20px; font-family: "Fredoka"; font-weight: 600; font-size: 17px;
}
.pill.solid { background: var(--ink); color: #fff; }
.pill:disabled { opacity: .35; cursor: default; }
.pill:not(:disabled):active { transform: scale(.96); }

.toast {
  position: fixed; left: 50%; top: 84px; transform: translate(-50%, -10px);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px;
  font-family: "Fredoka"; font-size: 17px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 50; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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

/* Dialogs */
dialog.sheet {
  border: 0; border-radius: 22px; padding: 22px 18px 24px; width: min(460px, calc(100% - 24px));
  max-height: calc(100dvh - 24px); overflow-y: auto; background: #fff; color: var(--ink);
  text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(40, 30, 20, .45); }
dialog[open] { animation: sheetin .3s cubic-bezier(.3,1.3,.5,1); }
@keyframes sheetin { from { transform: translateY(30px) scale(.96); opacity: 0; } }
dialog h2 { font-family: "Fredoka"; font-weight: 700; font-size: 30px; margin: 6px 0 10px; }
dialog p { line-height: 1.45; margin: 8px 0; }
.close { position: absolute; top: 10px; right: 12px; background: none; border: 0; font-size: 22px; }
.muted { color: var(--muted); }
.legend { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 12px 0; }
.legend div { border-radius: 8px; padding: 8px 2px; font-family: "Fredoka"; font-size: 14px; }

.result-hero { position: relative; margin: 4px auto 0; width: 190px; }
.result-hero img {
  width: 190px; height: 190px; object-fit: cover; border-radius: 50%;
  border: 6px solid var(--yellow); display: block;
}
.result-hero.lost img { border-color: var(--blue); }
.result-hero img { animation: wag 1.4s ease-in-out infinite; }
@keyframes wag { 50% { transform: rotate(-4deg) scale(1.02); } }
.gaucho-says {
  position: absolute; right: -40px; top: -6px; background: var(--accent); color: #fff;
  font-family: "Fredoka"; font-weight: 700; padding: 6px 12px; border-radius: 16px 16px 16px 4px;
  font-size: 16px; transform: rotate(6deg); max-width: 150px; line-height: 1.15;
}
.emoji-grid { font-size: 28px; line-height: 1.1; margin: 4px 0 14px; letter-spacing: 2px; }

.stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin: 18px 0 6px; padding: 14px 0; border-top: 2px solid var(--line); border-bottom: 2px solid var(--line); }
.stats-row.two { grid-template-columns: repeat(2, 1fr); border-top: 0; margin-top: 0; padding-top: 4px; }
.stat b { display: block; font-family: "Fredoka"; font-size: 30px; font-weight: 600; }
.stat span { font-size: 13px; color: var(--muted); }
.stat.fire b { color: var(--accent); }

.section-title { text-align: left; font-family: "Fredoka"; font-weight: 700; font-size: 18px; margin: 18px 0 8px; letter-spacing: .5px; }
.dist { display: grid; grid-template-columns: 22px 1fr; gap: 6px; align-items: center; text-align: left; }
.dist .lbl { font-family: "Fredoka"; font-weight: 700; }
.dist .bar { background: #b9b2a6; color: #fff; border-radius: 4px; padding: 3px 8px; text-align: right; min-width: 28px; font-family: "Fredoka"; font-weight: 600; }
.dist .bar.today { background: var(--accent); }
.dist-note { font-size: 12px; color: var(--muted); text-align: left; margin-top: 6px; }

.badges { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px 6px; }
.badge { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 13px; line-height: 1.2; }
.badge .ico { font-size: 38px; width: 62px; height: 62px; display: grid; place-items: center; border-radius: 50%; background: #fff3d6; border: 3px solid var(--yellow); }
.badge.locked .ico { filter: grayscale(1); opacity: .35; background: var(--tile); border-color: var(--line); }
.badge .when { color: var(--muted); font-size: 11px; }
.badge .prog { width: 54px; height: 7px; border-radius: 4px; background: var(--tile); overflow: hidden; margin-top: 2px; }
.badge .prog i { display: block; height: 100%; background: var(--ink); }

.next { color: var(--muted); margin-top: 18px; }
.next b { font-family: "Fredoka"; color: var(--ink); font-size: 20px; }

.archive-list { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.archive-list button { border: 2px solid var(--line); background: #fff; border-radius: 12px; padding: 10px 2px; font-family: "Fredoka"; font-weight: 600; }
.archive-list button.won { background: var(--green); border-color: var(--green); }
.archive-list button.lost { background: var(--tile); }
.archive-list button.today { border-color: var(--accent); }
.archive-list small { display: block; font-size: 11px; color: var(--muted); font-family: "Nunito"; }

.badge-pop .badge-big { font-size: 84px; animation: spin 1s cubic-bezier(.3,1.5,.5,1); }
.badge-pop .prize { background: #fff3d6; border: 2px dashed var(--accent); border-radius: 12px; padding: 10px; font-weight: 800; }
.badge-pop .prize:empty { display: none; }
@keyframes spin { from { transform: scale(0) rotate(-200deg); } }

.practice-banner { background: var(--blue); border-radius: 10px; padding: 6px 10px; display: inline-block; color: var(--ink); }
.practice-banner a { color: inherit; font-weight: 800; }

.done-msg { text-align: center; font-family: "Fredoka"; font-size: 18px; margin: 4px 0 0; line-height: 1.35; }
.done-msg b { font-size: 22px; }
dialog .controls { margin: 4px 0 4px; }
.next:empty { display: none; }

.testbar {
  position: sticky; bottom: 0; background: #2d2a33; color: #fff; font-size: 13px;
  padding: 8px 12px max(8px, env(safe-area-inset-bottom)); display: flex; flex-wrap: wrap;
  gap: 6px 12px; align-items: center; justify-content: space-between;
}
.testbar-btns { display: flex; gap: 6px; flex-wrap: wrap; }
.testbar button { background: #4a4458; border: 0; color: #fff; border-radius: 8px; padding: 6px 10px; font-size: 13px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
