/* =========================================================
   RAYUELA 3D · El Eco Perdido
   Paleta Laialá: terracota #cd7753 · teal #50979a · teal claro #77beba
   amarillo #e0a51a · celeste #bed5d3 · negro #1f1816 · crema #eaca6e · marrón #663b2c
   ========================================================= */
:root {
  --terracota: #cd7753;
  --terracota-osc: #a86244;
  --teal: #50979a;
  --teal-osc: #427c7e;
  --teal-claro: #77beba;
  --amarillo: #e0a51a;
  --celeste: #bed5d3;
  --negro: #1f1816;
  --crema: #eaca6e;
  --marron: #663b2c;
  --papel: #fbf5e6;
  --f-titulo: "Playfair Display", Georgia, serif;
  --f-cuerpo: "Poppins", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; background: #1f1816;
  font-family: var(--f-cuerpo); color: var(--negro);
  user-select: none; -webkit-user-select: none;
}

#renderCanvas { width: 100%; height: 100%; display: block; touch-action: none; outline: none; }

.hidden { display: none !important; }

/* ---------- Botones ---------- */
.btn {
  font-family: var(--f-cuerpo); font-weight: 800; font-size: 17px;
  border: 0; border-radius: 16px; padding: 12px 22px; cursor: pointer;
  background: var(--papel); color: var(--marron);
  box-shadow: 0 4px 0 #d8c9a3, 0 10px 22px rgba(0,0,0,.18);
  transition: transform .12s ease, box-shadow .12s ease, opacity .2s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #d8c9a3, 0 6px 14px rgba(0,0,0,.18); }
.btnMain { background: var(--terracota-osc); color: #fff; box-shadow: 0 4px 0 #7e4630, 0 12px 26px rgba(0,0,0,.25); }
.btnMain:active { box-shadow: 0 1px 0 #7e4630, 0 6px 14px rgba(0,0,0,.25); }
.btn:disabled { opacity: .45; cursor: default; }
.btnSmall { font-size: 15px; padding: 9px 16px; }

/* ---------- Pantalla de inicio ---------- */
#titleScreen {
  position: fixed; inset: 0; z-index: 50;
  display: flex; padding: 20px;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(119,190,186,.55), transparent 60%),
    radial-gradient(ellipse at 50% 120%, rgba(205,119,83,.45), transparent 60%),
    #2a3b3a;
  transition: opacity .8s ease;
  overflow-y: auto;
}
#titleScreen.out { opacity: 0; pointer-events: none; }
.titleInner { width: min(560px, 100%); margin: auto; text-align: center; color: var(--papel); } /* margin:auto centra sin cortar arriba si hay que scrollear */
.titleLogo { width: min(300px, 70vw); filter: drop-shadow(0 14px 24px rgba(0,0,0,.45)); animation: bob 3.5s ease-in-out infinite; }
#titleScreen h1 {
  font-family: var(--f-titulo); font-weight: 900; font-size: clamp(34px, 7vw, 54px);
  margin: 6px 0 6px; color: var(--crema); letter-spacing: .01em;
  text-shadow: 0 3px 0 var(--marron), 0 10px 24px rgba(0,0,0,.4);
}
.titleStory { font-size: clamp(15px, 2.4vw, 18px); line-height: 1.5; margin: 0 auto 18px; max-width: 460px; opacity: .95; }
.titleStory b { color: var(--crema); }
#loadBar { width: min(320px, 80%); height: 10px; margin: 0 auto; border-radius: 10px; background: rgba(255,255,255,.15); overflow: hidden; }
#loadFill { width: 0%; height: 100%; background: linear-gradient(90deg, var(--amarillo), var(--terracota)); transition: width .25s; }
#loadText { font-size: 13px; margin: 8px 0 16px; opacity: .8; }
/* Selector de misión */
#missionPick { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin: 0 auto 12px; }
.missionChip {
  font-family: var(--f-cuerpo); font-weight: 700; font-size: 13.5px; cursor: pointer;
  border: 2px solid rgba(251,245,230,.5); border-radius: 999px; padding: 8px 14px;
  background: rgba(251,245,230,.12); color: var(--papel);
}
.missionChip.active { background: var(--crema); color: var(--marron); border-color: var(--crema); }
.missionChip:disabled { opacity: .5; cursor: default; }

/* Selector de nivel */
#levelPick { display: flex; flex-direction: column; gap: 10px; width: min(420px, 100%); margin: 0 auto; }
.levelBtn {
  display: flex; align-items: center; gap: 12px; text-align: left; cursor: pointer;
  border: 0; border-radius: 18px; padding: 12px 16px; font-family: var(--f-cuerpo);
  background: var(--papel); color: var(--negro);
  box-shadow: 0 4px 0 #d8c9a3, 0 12px 26px rgba(0,0,0,.25); transition: transform .12s, opacity .2s;
}
.levelBtn[data-level="1"] { border-left: 8px solid var(--teal); }
.levelBtn[data-level="2"] { border-left: 8px solid var(--terracota); }
.levelBtn:active { transform: translateY(3px); }
.levelBtn:disabled { opacity: .45; cursor: default; }
.lvIcon { font-size: 30px; }
.lvText { display: flex; flex-direction: column; flex: 1; }
.lvText b { font-size: 17px; font-weight: 800; color: var(--marron); }
.lvText small { font-size: 12.5px; opacity: .75; }
.lvBest { display: flex; flex-direction: column; align-items: flex-end; font-size: 14px; }
.lvBest small { font-size: 11.5px; opacity: .7; }
#progressLine { margin-top: 10px; font-size: 13.5px; font-weight: 600; color: var(--crema); min-height: 1em; }
#directWrap { display: inline-flex; gap: 6px; align-items: center; margin-top: 8px; font-size: 13.5px; cursor: pointer; }
#directWrap input { width: 18px; height: 18px; accent-color: var(--terracota); }
.titleControls { display: flex; flex-direction: column; gap: 4px; margin-top: 18px; font-size: 12.5px; opacity: .75; }

@keyframes bob { 0%,100% { transform: translateY(0) rotate(-1deg); } 50% { transform: translateY(-8px) rotate(1deg); } }

/* ---------- HUD ---------- */
#hud {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) 14px 0; pointer-events: none;
}
#objective {
  background: rgba(251,245,230,.94); border-radius: 18px; padding: 10px 16px 11px;
  box-shadow: 0 8px 22px rgba(0,0,0,.2); border-left: 6px solid var(--teal);
  max-width: min(60vw, 380px);
}
#zoneName { font-family: var(--f-titulo); font-weight: 900; font-size: 13px; letter-spacing: .06em; text-transform: uppercase; color: var(--teal-osc); }
#objectiveText { font-size: 15.5px; font-weight: 600; line-height: 1.3; margin-top: 2px; }
#stats { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; pointer-events: auto; }
.pill {
  background: rgba(251,245,230,.94); border-radius: 999px; padding: 8px 14px;
  font-weight: 800; font-size: 15px; box-shadow: 0 6px 16px rgba(0,0,0,.18);
  border: 0; font-family: var(--f-cuerpo); color: var(--negro);
}
.pillBtn { cursor: pointer; padding: 8px 12px; }
.pill.pop { animation: pop .5s ease; }
#timerPill { min-width: 62px; text-align: center; }
#timerPill.urgent { background: var(--amarillo); animation: pop .9s ease infinite; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.25); background: var(--crema); } 100% { transform: scale(1); } }

/* ---------- Diálogo ---------- */
#dialog {
  position: fixed; z-index: 25; left: 50%; bottom: max(22px, env(safe-area-inset-bottom));
  transform: translateX(-50%); width: min(640px, calc(100% - 28px));
  background: var(--papel); border-radius: 22px; padding: 16px 20px 14px;
  box-shadow: 0 18px 48px rgba(0,0,0,.35); border: 3px solid var(--marron);
  animation: dialogIn .3s ease;
}
@keyframes dialogIn { from { transform: translate(-50%, 20px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }
.dialogName {
  position: absolute; top: -15px; left: 18px; background: var(--teal-osc); color: #fff;
  font-weight: 800; font-size: 13px; letter-spacing: .1em; padding: 4px 14px; border-radius: 999px;
}
#dialogText { font-size: clamp(16px, 2.3vw, 19px); line-height: 1.45; min-height: 2.9em; margin-top: 4px; }
#dialogText b { color: var(--terracota-osc); }
#dialogNext { display: block; margin: 10px 0 0 auto; }

/* ---------- Avisos ---------- */
#toast {
  position: fixed; z-index: 30; left: 50%; bottom: max(150px, calc(env(safe-area-inset-bottom) + 140px)); transform: translateX(-50%);
  width: max-content; max-width: min(620px, calc(100% - 32px));
  background: rgba(31,24,22,.92); color: #fff; font-weight: 700; font-size: clamp(16px, 2.4vw, 20px);
  padding: 12px 20px; border-radius: 16px; text-align: center; line-height: 1.35;
  box-shadow: 0 12px 30px rgba(0,0,0,.3); animation: toastIn .25s ease;
}
#toast.good { background: rgba(66,124,126,.96); }
#toast.bad { background: rgba(168,98,68,.96); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -8px); } to { opacity: 1; transform: translate(-50%, 0); } }

#xpFloat { position: fixed; inset: 0; pointer-events: none; z-index: 31; }
.xpBubble {
  position: absolute; right: 24px; top: 70px; font-weight: 800; font-size: 22px; color: var(--amarillo);
  text-shadow: 0 2px 0 var(--marron), 0 4px 12px rgba(0,0,0,.4); animation: xpUp 1.4s ease forwards;
}
@keyframes xpUp { from { opacity: 0; transform: translateY(20px) scale(.8); } 20% { opacity: 1; transform: translateY(0) scale(1.1); } to { opacity: 0; transform: translateY(-30px) scale(1); } }

#listenBtn {
  position: fixed; z-index: 12; right: max(16px, env(safe-area-inset-right)); bottom: max(26px, env(safe-area-inset-bottom));
  background: var(--teal-osc); color: #fff; box-shadow: 0 4px 0 #2f5a5c, 0 10px 24px rgba(0,0,0,.25);
}
#listenBtn:active { transform: translateY(3px); }

/* ---------- Joystick ---------- */
#joystick {
  position: fixed; z-index: 15; left: max(22px, env(safe-area-inset-left)); bottom: max(26px, env(safe-area-inset-bottom));
  width: 128px; height: 128px; border-radius: 50%;
  background: radial-gradient(circle, rgba(251,245,230,.28), rgba(251,245,230,.12));
  border: 3px solid rgba(251,245,230,.6); display: none; touch-action: none;
}
#joyKnob {
  position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px;
  border-radius: 50%; background: var(--papel); box-shadow: 0 6px 16px rgba(0,0,0,.3);
  border: 3px solid var(--terracota);
}
body.touch #joystick { display: block; }
body.dialogOpen #joystick, body.dialogOpen #listenBtn { opacity: .35; }

/* ---------- Fundido ---------- */
#fade { position: fixed; inset: 0; z-index: 40; background: #1f1816; opacity: 0; pointer-events: none; transition: opacity .6s ease; }
#fade.on { opacity: 1; }

/* ---------- Premio ---------- */
#rewardScreen {
  position: fixed; inset: 0; z-index: 45; display: grid; place-items: center; padding: 18px;
  background: rgba(31,24,22,.6); overflow-y: auto; animation: toastIn .4s ease;
}
.rewardCard {
  width: min(560px, 100%); background: var(--papel); border-radius: 28px; padding: 26px 22px 22px;
  text-align: center; box-shadow: 0 30px 80px rgba(0,0,0,.45); border: 4px solid var(--amarillo);
}
.rewardKicker { font-weight: 800; letter-spacing: .12em; font-size: 13px; color: var(--teal-osc); }
.rewardKey { font-size: 64px; animation: bob 2.5s ease-in-out infinite; }
.rewardCard h2 { font-family: var(--f-titulo); font-weight: 900; font-size: clamp(24px, 4.6vw, 34px); margin: 4px 0 8px; color: var(--marron); line-height: 1.15; }
.rewardCard p { margin: 6px 0; line-height: 1.45; }
.rewardSummary { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin: 14px 0; }
.rewardSummary div { background: #fff; border-radius: 14px; padding: 8px 12px; border: 2px solid var(--celeste); display: flex; flex-direction: column; }
.rewardSummary span { font-weight: 600; font-size: 18px; letter-spacing: .04em; }
.rewardSummary b { color: var(--terracota-osc); font-weight: 800; }
.rewardSummary small { font-size: 12px; color: var(--teal-osc); font-weight: 600; }
.rewardXp { font-weight: 800; font-size: 26px; color: var(--marron); margin: 8px 0; }
.rewardXp small { font-size: 14px; font-weight: 600; opacity: .75; }
.rewardStars { font-size: 34px; letter-spacing: 4px; color: var(--amarillo); }
.rewardStats { font-size: 14.5px; line-height: 1.5; color: var(--negro); }
.rewardStats b { color: var(--terracota-osc); }
.rewardNext { font-size: 14px; opacity: .85; }
.rewardBtns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }

@media (max-width: 600px) {
  #objective { padding: 8px 12px 9px; }
  #objectiveText { font-size: 13.5px; }
  .pill { font-size: 13px; padding: 7px 11px; }
  #listenBtn { font-size: 15px; padding: 12px 16px; }
}

/* Barra "sin porcentaje": cuando el servidor no informa el tamaño del archivo */
#loadBar.indeterminate #loadFill { width: 35% !important; animation: loadSlide 1.2s ease-in-out infinite; }
@keyframes loadSlide { from { transform: translateX(-100%); } to { transform: translateX(290%); } }

/* Pantallas bajitas (celular acostado, notebooks chicas) */
@media (max-height: 640px) {
  .titleLogo { width: min(170px, 40vw); }
  #titleScreen h1 { font-size: 30px; margin: 0 0 4px; }
  .titleStory { font-size: 14px; margin-bottom: 10px; }
  .titleControls { display: none; }
}

/* Panel de la Misión 2 */
#wordPanel {
  position: fixed; z-index: 11; left: 50%; transform: translateX(-50%);
  top: max(92px, calc(env(safe-area-inset-top) + 82px));
  width: min(520px, calc(100% - 28px)); text-align: center;
  background: rgba(31,43,44,.92); color: #fff; border: 3px solid var(--crema);
  border-radius: 20px; padding: 8px 14px 12px; box-shadow: 0 12px 30px rgba(0,0,0,.3);
}
#wpCount { font-size: 12px; font-weight: 600; color: var(--crema); letter-spacing: .05em; }
.wpWord { display: block; font-weight: 800; font-size: clamp(26px, 6vw, 38px); letter-spacing: .06em; }
.wpWord.done { color: var(--amarillo); }
.wpRiddle { display: block; font-weight: 600; font-size: clamp(15px, 3.6vw, 18px); line-height: 1.35; margin: 4px 0 2px; }
#wpSlots { display: flex; gap: 6px; justify-content: center; margin-top: 8px; flex-wrap: wrap; }
#wpSlots span {
  min-width: 52px; padding: 6px 8px; border-radius: 12px; font-weight: 800; font-size: 18px;
  background: rgba(255,255,255,.12); border: 2px solid rgba(255,255,255,.3); color: rgba(255,255,255,.55);
}
#wpSlots span.next { border-color: var(--teal-claro); color: #fff; }
#wpSlots span.filled { background: var(--amarillo); border-color: var(--amarillo); color: var(--marron); }
@media (max-width: 600px) { #wordPanel { top: max(124px, calc(env(safe-area-inset-top) + 112px)); } #wpSlots span { min-width: 44px; font-size: 16px; } }

/* Portada: próxima misión y llaves por nivel */
.lvText em { font-style: normal; font-size: 12px; font-weight: 600; color: var(--teal-osc); margin-top: 2px; }
.lvBest { flex-direction: row !important; gap: 2px; font-size: 18px; }
.lvBest .lockedKey { opacity: .35; font-size: 14px; }
.linkBtn { background: none; border: 0; color: var(--crema); font-family: var(--f-cuerpo); font-weight: 600; font-size: 13.5px; text-decoration: underline; cursor: pointer; margin-top: 8px; }
#missionPick { display: flex; flex-direction: column; gap: 6px; align-items: center; margin: 8px auto 0; }
#missionPick .missionChip { width: min(360px, 100%); }

/* Panel del caso (Biblioteca) */
#casePanel {
  position: fixed; z-index: 11; left: 50%; transform: translateX(-50%);
  top: max(92px, calc(env(safe-area-inset-top) + 82px));
  width: min(560px, calc(100% - 28px)); max-height: 46vh; overflow-y: auto;
  background: #f6ead0; color: var(--negro); border: 3px solid var(--marron);
  border-radius: 18px; padding: 10px 16px 12px; box-shadow: 0 14px 34px rgba(0,0,0,.35);
  background-image: linear-gradient(rgba(102,59,44,.06) 1px, transparent 1px); background-size: 100% 26px;
}
.caseHead { display: flex; justify-content: space-between; align-items: center; font-weight: 800; color: var(--marron); font-size: 14px; }
#caseToggle { border: 0; background: var(--marron); color: #fff; border-radius: 8px; width: 30px; height: 26px; cursor: pointer; font-size: 14px; }
#caseText { font-family: var(--f-titulo); font-size: clamp(16px, 2.6vw, 19px); line-height: 1.45; margin: 6px 0; }
#caseQ { font-weight: 800; font-size: 16px; margin: 6px 0; color: var(--terracota-osc); }
#caseAnswers { display: flex; flex-direction: column; gap: 4px; font-size: 15px; }
#caseAnswers b { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--marron); color: #fff; margin-right: 6px; font-size: 13px; }
#casePanel.collapsed #caseText { display: none; }
#casePanel.collapsed #caseToggle { transform: rotate(-90deg); }
@media (max-width: 600px) { #casePanel { top: max(124px, calc(env(safe-area-inset-top) + 112px)); max-height: 40vh; } }
