:root {
  --cream: #fff6e5;
  --paper: #fffdf8;
  --ink: #4a3347;
  --ink-soft: #7a6177;
  --pink: #ff8fb0;
  --pink-d: #d9567f;
  --mint: #8fdcc0;
  --mint-d: #3fa886;
  --butter: #ffe08a;
  --butter-d: #e8b84a;
  --line: #ecd6bf;
  --bad: #b8433f;
  --good: #2f8a68;
  --r: 16px;
  --font: ui-rounded, "Nunito", "Quicksand", "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--cream);
  color: var(--ink);
  font: 16px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body.sheet-open { overflow: hidden; }

#app {
  height: 100dvh;
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

h2, h3 { margin: 0; line-height: 1.2; }
h3 { font-size: 1.05rem; margin: 18px 0 8px; }
p { margin: 0 0 6px; }
.muted { color: var(--ink-soft); }
.small { font-size: .875rem; }
.ok { color: var(--good); font-weight: 700; }
.warn { color: var(--bad); }
.grow { flex: 1; min-width: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ico { display: inline-block; flex: none; image-rendering: pixelated; background-repeat: no-repeat; }
.locked .ico { filter: grayscale(1) brightness(.85) opacity(.65); }

:focus-visible { outline: 3px solid var(--pink-d); outline-offset: 2px; }

/* ---------- barra superior ---------- */
#hud {
  display: flex;
  gap: 8px;
  padding: 10px 10px 6px;
  align-items: stretch;
}
.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 4px 12px;
  background: var(--paper);
  border: 2px solid var(--line);
  border-bottom-width: 4px;
  border-radius: var(--r);
  font-weight: 700;
  color: var(--ink);
}
.chip-btn { flex: 1; min-width: 0; text-align: left; font: inherit; font-weight: 700; cursor: pointer; }
.chip-btn.done { background: var(--butter); border-color: var(--butter-d); }
.chip-btn.done .muted { color: var(--ink); }
.quest-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.quest-text > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.quest-text .muted { font-size: .8rem; font-weight: 600; }

/* ---------- escenario ---------- */
#stage {
  position: relative;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 6px 10px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  --cw: 320px;
}
#game {
  display: block;
  flex: none;
  image-rendering: pixelated;
  border: 3px solid var(--line);
  border-radius: 6px;
  background: #bfe8a0;
  touch-action: manipulation;
}
#side { display: grid; gap: 10px; align-content: start; width: var(--cw); max-width: 100%; }
#stage.wide { flex-direction: row; align-items: flex-start; justify-content: center; gap: 16px; padding-top: 10px; }
#stage.wide #side { width: 300px; }
#selected {
  background: var(--paper);
  border: 2px solid var(--line);
  border-bottom-width: 4px;
  border-radius: var(--r);
  padding: 12px;
  display: grid;
  gap: 10px;
}
.sel-head { display: flex; gap: 12px; align-items: flex-start; }
.sel-title { flex: 1; min-width: 0; }
.sel-title h2 { font-size: 1.2rem; }
.meters { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.meters.tight { gap: 6px; margin-top: 6px; }
.meter { display: grid; gap: 3px; }
.meter-label { font-size: .78rem; font-weight: 700; color: var(--ink-soft); letter-spacing: .02em; }
.bar { height: 12px; background: #f3e6d6; border-radius: 8px; overflow: hidden; border: 2px solid var(--line); }
.bar > i { display: block; height: 100%; border-radius: 6px; transition: width .4s ease; }
.bar.fed > i { background: var(--mint-d); }
.bar.happy > i { background: var(--pink); }
.bar.quest > i { background: var(--butter-d); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.actions.col { flex-direction: column; margin-top: 12px; }
.actions.col .btn { width: 100%; }
.foods { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.foods .meter-label { width: 100%; }

/* ---------- botones ---------- */
.btn, .link {
  font: inherit;
  font-weight: 700;
  color: var(--ink);
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 6px 14px;
  background: var(--paper);
  border: 2px solid var(--line);
  border-bottom-width: 4px;
  border-radius: 14px;
  cursor: pointer;
  position: relative;
}
.btn:active:not(:disabled) { transform: translateY(2px); border-bottom-width: 2px; }
.btn.primary { background: var(--pink); border-color: var(--pink-d); }
.btn.mint { background: var(--mint); border-color: var(--mint-d); }
.btn.highlight { background: var(--butter); border-color: var(--butter-d); }
.btn.small { min-height: 40px; min-width: 40px; padding: 4px 10px; }
.btn.food { padding: 4px 10px; }
.btn.food.fav { border-color: var(--butter-d); background: #fff6d6; }
.btn .star { color: var(--butter-d); font-size: .8rem; position: absolute; top: -4px; right: 2px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.link { background: none; border: 0; padding: 0; text-decoration: underline; cursor: pointer; color: var(--pink-d); }

/* ---------- dock ---------- */
#dock {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  background: var(--paper);
  border-top: 2px solid var(--line);
}
#dock button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 60px;
  padding: 6px 0;
  font: inherit;
  font-size: .74rem;
  font-weight: 700;
  color: var(--ink);
  background: transparent;
  border: 2px solid transparent;
  border-radius: 14px;
  cursor: pointer;
}
#dock button:hover { background: #fff1d8; }
#dock button:active { background: var(--butter); }

/* ---------- avisos ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(92px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: min(92vw, 460px);
  padding: 10px 16px;
  background: var(--ink);
  color: #fff;
  border-radius: 14px;
  font-weight: 700;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.tap { pointer-events: auto; cursor: pointer; }
/* con un panel abierto el aviso sube, para no tapar su contenido */
body.sheet-open .toast { bottom: auto; top: calc(12px + env(safe-area-inset-top)); transform: translate(-50%, -12px); }
body.sheet-open .toast.show { transform: translate(-50%, 0); }
.toast.warn { background: var(--bad); }

/* ---------- hojas (paneles) ---------- */
.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(74, 51, 71, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}
.sheet {
  width: 100%;
  max-width: 600px;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  border-radius: 22px 22px 0 0;
  border: 2px solid var(--line);
  border-bottom: 0;
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(24px); opacity: 0; } }
.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px 8px;
}
.sheet-head h2 { font-size: 1.3rem; }
.sheet-body {
  overflow-y: auto;
  padding: 4px 16px calc(18px + env(safe-area-inset-bottom));
}
@media (min-width: 700px) {
  .backdrop { align-items: center; }
  .sheet { border-radius: 22px; border-bottom: 2px solid var(--line); }
}

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  margin-bottom: 8px;
  background: #fff;
  border: 2px solid var(--line);
  border-radius: 14px;
  flex-wrap: wrap;
}
.row .grow { flex: 1 1 150px; }
.row .price { margin-left: auto; }
.row > .btn:last-child { margin-left: auto; }
.price { display: inline-flex; align-items: center; gap: 4px; font-weight: 800; }
.tabs { display: flex; gap: 6px; margin: 4px 0 12px; }
.tab {
  flex: 1;
  min-height: 44px;
  font: inherit;
  font-weight: 700;
  color: var(--ink);
  background: #fff;
  border: 2px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}
.tab[aria-selected="true"] { background: var(--butter); border-color: var(--butter-d); }
.quest-card {
  padding: 14px;
  background: #fff;
  border: 2px solid var(--line);
  border-radius: 14px;
  display: grid;
  gap: 6px;
}
.quest-card h3 { margin: 0; }
.chapter { color: var(--pink-d); font-weight: 800; font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; }
.done-list { margin: 0; padding-left: 4px; list-style: none; }
.done-list li { padding: 4px 0; }
input.text {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  margin: 4px 0 12px;
  font: inherit;
  color: var(--ink);
  background: #fff;
  border: 2px solid var(--line);
  border-radius: 12px;
}
/* etiquetas de rareza: el color nunca va solo, siempre con su texto */
.rarity {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  border: 2px solid var(--line);
  font-size: .75rem;
  font-weight: 800;
  line-height: 1.3;
  vertical-align: middle;
  color: var(--ink);
}
.rarity.poco { background: #d8f5ea; border-color: var(--mint-d); }
.rarity.raro { background: #ecdfff; border-color: #a58ae0; }
.rarity.brillante { background: var(--butter); border-color: var(--butter-d); }
.rarity.brillante::before { content: "✦ "; }
#soundTitle { margin-bottom: 8px; }
.volume-row { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; margin: 0 0 12px; }
.volume-row .meter-label { margin: 0; }
.range { width: 100%; min-height: 44px; margin: 0; accent-color: var(--pink-d); }
.name-dialog { display: grid; gap: 4px; }
.name-head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.name-head p { margin: 0; }
.confirm { display: grid; gap: 8px; }

@media (max-width: 480px) {
  /* el chip de espacio ahora lleva el nombre del corral: la misión pasa a su propia fila */
  #hud { flex-wrap: wrap; }
  #hud .small-chip { flex: 1; min-width: 0; }
  #hud .chip-btn { order: 3; flex: 1 1 100%; min-height: 44px; }
  #hud .chip-btn.gift { order: 1; flex: none; min-height: 48px; } /* la cesta se queda en la primera fila */
}
@media (max-width: 380px) {
  .meters { grid-template-columns: 1fr; }
  #dock { gap: 2px; padding-left: 4px; padding-right: 4px; }
  #dock button { font-size: .64rem; letter-spacing: -.01em; }
}
@media (prefers-reduced-motion: reduce) {
  .sheet, .toast, .bar > i { animation: none; transition: none; }
}

/* ---------- animales agrupados por corral ---------- */
.group + .group { margin-top: 12px; }
.group h3 { margin: 0; }

/* ---------- fase 2B: corazones, enfermedad y veterinaria ---------- */
.hearts { display: inline-block; color: var(--pink-d); font-size: .95rem; letter-spacing: .08em; line-height: 1.2; vertical-align: middle; }
.sel-title .hearts { margin: 2px 0 4px; }
.sick-note {
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 4px 8px;
  padding: 8px 10px; margin-top: 4px;
  background: #fff1f4; border: 2px dashed var(--pink); border-radius: 12px;
}
.sick-note strong { color: var(--pink-d); }
.sick-note .btn { grid-column: 1 / -1; justify-self: start; }
.meters.tight + .sick-note, .group .sick-note { padding: 4px 8px; }
.sick-line { display: flex; align-items: center; gap: 6px; font-weight: 700; color: var(--pink-d); }
.empty { display: grid; justify-items: center; gap: 8px; padding: 18px 8px; text-align: center; color: var(--ink-soft); }

/* ---------- quién habla en las misiones ---------- */
.speaker { display: flex; align-items: center; gap: 8px; margin: 2px 0; }
.avatar {
  display: inline-grid; place-items: center; flex: none;
  width: 28px; height: 28px; border-radius: 50%;
  border: 2px solid var(--line); font-weight: 800; font-size: .85rem; color: var(--ink);
}
.face { border-radius: 12px; border: 2px solid var(--line); flex: none; box-sizing: content-box; }
.quest-text-body { line-height: 1.5; }

/* ---------- secundarias e historial ---------- */
.side-list { display: grid; gap: 10px; }
.quest-card.side { gap: 4px; padding: 12px; }
.quest-card.side h4 { margin: 0; font-size: 1rem; }
.quest-card.side.ready { border-color: var(--butter-d); background: #fffaf0; }
.side-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.side-foot p { margin: 0; }
.history { margin-top: 16px; border-top: 2px dashed var(--line); padding-top: 8px; }
.history summary {
  cursor: pointer; font-weight: 800; min-height: 44px; display: flex; align-items: center;
  list-style-position: inside;
}
.history summary::marker { color: var(--pink-d); }

/* ---------- secretos ---------- */
.secrets h3 { margin: 18px 0 6px; }
.secret-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 4px; }
.secret-list li { padding: 6px 10px; border-radius: 10px; background: #fff; border: 2px solid var(--line); }
.secret-list li.found { border-color: var(--butter-d); background: #fffaf0; }
.secret-list li.found strong { color: var(--pink-d); }

/* ---------- pulido visual (etapa e): feedback sutil, sin cambiar la identidad ---------- */
:root { --ease: cubic-bezier(.2, .7, .3, 1); }
#coins, #readyCount, #spaceInfo, #questProg, .price, .bar + .small { font-variant-numeric: tabular-nums; }
.btn { transition: background-color .15s var(--ease), border-color .15s var(--ease), transform .08s var(--ease); }
@media (hover: hover) {
  .btn:hover:not(:disabled) { background-color: #fff4e2; }
  .btn.primary:hover:not(:disabled) { background-color: #ff9fbb; }
  .btn.mint:hover:not(:disabled) { background-color: #a2e4cc; }
  .chip-btn:hover { border-color: var(--butter-d); }
  .row:hover { border-color: #e2c6a8; }
}
.row { transition: border-color .15s var(--ease); }
/* misión lista para reclamar: un latido suave que invita a tocar */
.chip-btn.done, #collectAll:not([hidden]) { animation: nudge 2.4s var(--ease) infinite; }
@keyframes nudge { 0%, 80%, 100% { transform: none; } 88% { transform: translateY(-2px); } }
/* el botón del lugar donde estás queda marcado en la barra */
#dock button.on { background: #fff1d8; border-color: var(--line); }
.quest-card { box-shadow: 0 2px 0 var(--line); }
.quest-card h3 { font-size: 1.1rem; }
.sheet-body > div > h3:first-child { margin-top: 4px; }
.toast.show { animation: toast-in .22s var(--ease); }
@keyframes toast-in { from { transform: translate(-50%, 8px) scale(.97); } }
body.sheet-open .toast.show { animation: none; }
#selected { animation: card-in .18s var(--ease); }
@keyframes card-in { from { opacity: 0; transform: translateY(6px); } }
@media (prefers-reduced-motion: reduce) {
  .btn, .row { transition: none; }
  .chip-btn.done, #collectAll:not([hidden]), .toast.show, #selected { animation: none; }
}
#collectAll { gap: 0; } /* el número va pegado a los paréntesis */

/* ---------- escenas con diálogo ---------- */
.story { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: center; padding: 12px; background: rgba(74, 51, 71, .35); }
.story-card { width: 100%; max-width: 560px; display: grid; gap: 10px; padding: 14px 16px calc(14px + env(safe-area-inset-bottom)); background: var(--paper); border: 2px solid var(--line); border-radius: 20px; animation: up .18s ease-out; }
.story-head { display: flex; align-items: flex-end; gap: 10px; }
.story-name { font-size: 1.05rem; padding-bottom: 6px; }
.story-text { margin: 0; min-height: 4.5em; line-height: 1.5; }
.story-actions { display: flex; justify-content: space-between; gap: 10px; }
@media (min-width: 700px) { .story { align-items: center; } }
@media (prefers-reduced-motion: reduce) { .story-card { animation: none; } }
.diary li { display: flex; align-items: center; gap: 8px; }
.chip-btn.gift { flex: none; background: var(--butter); border-color: var(--butter-d); animation: glow 1.6s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 0 0 4px rgba(232, 184, 74, .45); } }
@media (prefers-reduced-motion: reduce) { .chip-btn.gift { animation: none; } }
.tabs .tab[aria-pressed="true"] { background: var(--butter); border-color: var(--butter-d); }
