:root {
  --ink: #14131C;
  --ink-2: #221F2E;
  --ink-3: #2F2B40;
  --paper: #F3E9D6;
  --paper-dim: #C9BFAE;
  --fur: #E08A3C;
  --fur-deep: #B85E22;
  --sakura: #F0A7BC;
  --teal: #35B5AD;
  --leaf: #5C9C46;
  --mute: #8A8494;

  --font-pixel: 'Silkscreen', 'Courier New', monospace;
  --font-display: 'Instrument Serif', Georgia, 'Times New Roman', serif;
  --font-body: 'Instrument Sans', 'Helvetica Neue', Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  touch-action: manipulation;   /* sin zoom por doble toque en movil */
  overscroll-behavior: none;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: 0;
  background: none;
}

button:focus-visible, a:focus-visible {
  outline: 2px solid var(--fur);
  outline-offset: 3px;
}

/* ---------- Escenario ---------- */

#app {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  background:
    radial-gradient(ellipse at 50% 120%, rgba(224,138,60,0.10), transparent 60%),
    var(--ink);
}

#stage {
  position: relative;
  width: 384px;
  height: 216px;
  transform-origin: center center;
}

#game {
  display: block;
  touch-action: none;
  width: 384px;
  height: 216px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: var(--ink-2);
}

/* Todo lo que va encima del canvas se escala junto con el stage. */
.overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* Sonda de viewport estable: mide el tamano con las barras del navegador visibles (svh),
   que no cambia cuando Safari o Chrome esconden la barra al tocar o desplazar. */
#vp { position: fixed; top: 0; left: 0; width: 100vw; height: 100vh; width: 100svw; height: 100svh; visibility: hidden; pointer-events: none; }

/* #app recorta al viewport; #rot es lo que se rota en vertical, para que nada desborde la pagina. */
#rot { position: absolute; inset: 0; display: grid; place-items: center; }
body.rotated #rot {
  inset: auto; top: 0; left: 0;
  width: 100vh; height: 100vw;
  width: 100svh; height: 100svw;
  transform-origin: 0 0;
  transform: rotate(90deg) translate(0, -100%);
}
#debug { position: fixed; left: 4px; top: 4px; z-index: 99; margin: 0; padding: 4px 6px; font: 10px/1.3 monospace; color: #0f0; background: rgba(0,0,0,0.7); pointer-events: none; white-space: pre; }
#debug[hidden] { display: none; }
.btn-mute.btn-music { right: 72px; }

#title .audio-hint { color: var(--teal); animation: blink 1.4s steps(1) infinite; }
#title .audio-hint[hidden] { display: none; }
/* Pantalla completa: boton redondo como los del pad, arriba a la derecha, solo en tactil (Android) */
#touch-full {
  display: none; position: absolute; top: 12px; right: 18px; width: 46px; height: 46px; border-radius: 50%;
  background: rgba(243,233,214,0.10); border: 1px solid rgba(243,233,214,0.25); color: var(--paper);
  align-items: center; justify-content: center; touch-action: none; -webkit-tap-highlight-color: transparent;
}
.is-touch #touch-full:not([hidden]) { display: flex; }
#touch-full:active { background: rgba(224,138,60,0.45); }
#stage:has(#gate.on) ~ #touch-full, #stage:has(#intro.on) ~ #touch-full { display: none; }

/* ---------- HUD ---------- */

#hud {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 6px 8px;
  font-family: var(--font-pixel);
  font-size: 7px;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--paper);
  text-shadow: 0 1px 0 rgba(0,0,0,0.6);
  opacity: 0;
  transition: opacity 400ms ease;
}
#hud.on { opacity: 1; }

#hud-chapter { max-width: 55%; line-height: 1.5; }
#hud-chapter small { display: block; color: var(--paper-dim); font-size: 6px; }

#hud-items {
  display: flex;
  align-items: center;
  gap: 4px;
}
#hud-items canvas { image-rendering: pixelated; width: 14px; height: 10px; }
#hud-count { min-width: 24px; text-align: right; }
#hud-count.full { color: var(--fur); }

#hud-paws {
  position: absolute;
  left: 50%;
  top: 7px;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
}
#hud-paws span {
  width: 5px; height: 5px;
  background: var(--ink-3);
  box-shadow: 0 0 0 1px rgba(0,0,0,0.35);
}
#hud-paws span.done { background: var(--fur); }
#hud-paws span.now { background: var(--paper); }

#hud-time {
  position: absolute; left: 50%; top: 15px; transform: translateX(-50%);
  font-family: var(--font-pixel); font-size: 7px; color: var(--paper); text-shadow: 0 1px 0 rgba(0,0,0,0.6);
}
#hud-time.pen { color: var(--fur); }
#title .best { font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase; color: var(--fur); margin: 6px 0 0; }
#title .best:empty { display: none; }

/* Pantalla final: sin ventana de imagen en fase de lectura, la caja ocupa todo para los puntajes */
.cine.read.final .cine-art { display: none; }
.cine.read.final .cine-box { min-height: calc(100% - 12px); margin-top: 6px; }
.cine.final .cine-box .body { min-height: 0; }

/* Mejores tiempos (pantalla final) */
#scores { margin-top: 4px; max-width: 78%; }
#score-you { margin: 0 0 3px; font-size: 7.5px; color: var(--paper); }
#score-form { display: flex; align-items: center; gap: 5px; margin: 0 0 4px; }
#score-form[hidden] { display: none; }
#score-label { font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase; color: var(--paper-dim); }
#score-name {
  width: 30px; padding: 2px 3px 1px; font-family: var(--font-pixel); font-size: 8px; text-transform: uppercase;
  color: var(--paper); background: var(--ink-3); border: 1px solid var(--paper-dim); text-align: center; letter-spacing: 0.1em;
}
#score-name:focus { outline: 1px solid var(--fur); }
.btn.btn-inline { position: static; margin: 0; padding: 3px 7px 2px; font-size: 6px; }
#score-title { margin: 2px 0 0; font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase; color: var(--paper); }
#score-title:empty { display: none; }
#score-list { margin: 0; padding: 0 0 0 12px; font-family: var(--font-pixel); font-size: 6px; color: var(--paper-dim); line-height: 1.7; }
#score-list li.me { color: var(--fur); }

#hint {
  position: absolute;
  top: auto;
  left: 0; right: 0; bottom: 6px;
  text-align: center;
  font-family: var(--font-pixel);
  font-size: 6px;
  text-transform: uppercase;
  color: var(--paper);
  text-shadow: 0 1px 0 rgba(0,0,0,0.7);
  opacity: 0;
  transition: opacity 500ms ease;
}
#hint.on { opacity: 0.9; }

/* ---------- Pantallas (titulo, capitulo, recuerdo) ---------- */

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 14px 28px;
  pointer-events: auto;
  background: rgba(20,19,28,0.86);
  opacity: 0;
  visibility: hidden;
  transition: opacity 380ms ease, visibility 0s linear 380ms;
}
.screen.on {
  opacity: 1;
  visibility: visible;
  transition: opacity 380ms ease;
}

.kicker {
  font-family: var(--font-pixel);
  font-size: 6px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fur);
  margin: 0 0 4px;
}

.display {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 400;
  font-size: 30px;
  line-height: 1;
  margin: 0;
  color: var(--paper);
  letter-spacing: -0.01em;
}

.display.big { font-size: 42px; }

.body {
  font-size: 8px;
  line-height: 1.45;
  color: var(--paper-dim);
  max-width: 250px;
  margin: 6px auto 0;
}
.body strong { color: var(--paper); font-weight: 600; }

.btn {
  margin-top: 10px;
  padding: 5px 12px 4px;
  font-family: var(--font-pixel);
  font-size: 7px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink);
  background: var(--paper);
  box-shadow: 0 2px 0 var(--fur-deep);
  transition: transform 80ms ease, box-shadow 80ms ease;
}
.btn:hover { background: #fff; }
.btn:active { transform: translateY(2px); box-shadow: 0 0 0 var(--fur-deep); }

.btn-mute {
  position: absolute;
  width: max-content; height: auto;
  right: 6px;
  bottom: 5px;
  font-family: var(--font-pixel);
  font-size: 6px;
  text-transform: uppercase;
  color: var(--paper-dim);
  padding: 2px 4px;
  pointer-events: auto;
}
.btn-mute:hover { color: var(--paper); }

/* Puerta de entrada: pantalla negra que espera el primer toque (desbloquea el audio) */
#gate { background: var(--ink); }
#gate .kicker { font-size: 9px; color: var(--fur); margin-bottom: 8px; }
#gate .gate-text { font-family: var(--font-pixel); font-size: 7px; text-transform: uppercase; color: var(--paper); animation: blink 1.2s steps(1) infinite; margin: 0; }

/* Intro cinematica: planos con barras de cine, fundidos y leyenda */
#intro { background: #000; padding: 0; overflow: hidden; }
#intro .cine-art { animation-duration: 7s; }
.intro-fade { position: absolute; inset: 0; background: #000; opacity: 1; transition: opacity 700ms ease; pointer-events: none; }
.intro-bar { position: absolute; left: 0; right: 0; height: 20px; background: #000; z-index: 2; }
.intro-bar.top { top: 0; } .intro-bar.bottom { bottom: 0; height: 36px; }
.intro-caption {
  position: absolute; left: 20px; right: 20px; bottom: 9px; margin: 0; z-index: 3; text-align: center;
  font-family: var(--font-display); font-style: italic; font-size: 12px; line-height: 1.2; color: var(--paper);
  text-shadow: 0 1px 0 rgba(0,0,0,0.9), 0 0 6px rgba(0,0,0,0.8);
}
.intro-skip { position: absolute; right: 8px; bottom: 2px; margin: 0; z-index: 3; font-family: var(--font-pixel); font-size: 5px; text-transform: uppercase; color: var(--paper-dim); }
#stage:has(#intro.on) .btn-mute, #stage:has(#gate.on) .btn-mute, #stage:has(#intro.on) .btn-full, #stage:has(#gate.on) .btn-full { display: none; }

/* Revelacion del titulo tras la intro */
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes titleIn { from { opacity: 0; transform: scale(1.3); letter-spacing: 0.08em; } to { opacity: 1; transform: none; letter-spacing: -0.01em; } }
#title.reveal .kicker { animation: fadeUp 700ms 500ms both; }
#title.reveal .display.big { animation: titleIn 1100ms 200ms cubic-bezier(0.2, 0.8, 0.2, 1) both; }
#title.reveal .body { animation: fadeUp 700ms 1100ms both; }
#title.reveal .btn, #title.reveal .controls, #title.reveal .best { animation: fadeUp 600ms 1500ms both; }
@media (prefers-reduced-motion: reduce) { #title.reveal * { animation: none !important; } .intro-fade { transition: none; } }

/* Creditos finales: rollo que sube, con Kazuki caminando abajo */
#credits { background: var(--ink); padding: 0; justify-content: flex-start; }
.credits-viewport { position: absolute; inset: 0 0 26px 0; overflow: hidden; -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 14%, #000 86%, transparent 100%); mask-image: linear-gradient(180deg, transparent 0, #000 14%, #000 86%, transparent 100%); }
.credits-roll { position: absolute; left: 0; right: 0; top: 100%; text-align: center; animation: roll 52s linear forwards; }
@keyframes roll { to { transform: translateY(calc(-100% - 216px)); } }
.credits-roll.done { animation: none; top: auto; bottom: 30px; transform: none; }
.credits-roll h2 { font-family: var(--font-display); font-style: italic; font-weight: 400; font-size: 26px; margin: 18px 0 6px; color: var(--paper); }
.credits-roll h3 { font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--fur); margin: 14px 0 4px; }
.credits-roll .row { display: flex; justify-content: center; gap: 6px; font-size: 7.5px; line-height: 1.5; color: var(--paper-dim); }
.credits-roll .row b { color: var(--paper); font-weight: 600; }
.credits-roll .row i { font-family: var(--font-display); font-style: italic; }
.credits-roll p { font-family: var(--font-display); font-style: italic; font-size: 10px; color: var(--paper); margin: 8px 24px; }
#credits-hero { position: absolute; left: 8px; bottom: 4px; width: 72px; height: 60px; image-rendering: pixelated; }
#credits .btn { position: absolute; left: 50%; transform: translateX(-50%); bottom: 12px; }
#credits .btn[hidden] { display: none; }
#stage:has(#credits.on) .btn-full { display: none; }
@media (prefers-reduced-motion: reduce) { .credits-roll { animation: none; top: auto; bottom: 30px; } }

/* Titulo */
#title { background: linear-gradient(180deg, rgba(20,19,28,0.15) 0%, rgba(20,19,28,0.55) 55%, rgba(20,19,28,0.85) 100%); }
#title-hero { width: 108px; height: 90px; image-rendering: pixelated; margin-bottom: 2px; }
#title .display.big { margin-top: 0; }
#title .body { max-width: 230px; }
#title .controls {
  font-family: var(--font-pixel);
  font-size: 6px;
  text-transform: uppercase;
  color: var(--mute);
  margin-top: 8px;
}

/* Cutscenes estilo NES en dos fases: "look" (panel solo, paneo amplio) y "read" (panel arriba, texto abajo) */
.screen.cine { justify-content: flex-end; padding: 0; background: var(--ink); overflow: hidden; }
.cine-art {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; image-rendering: pixelated;
  transform-origin: center; animation: kenburns 6s cubic-bezier(0.2, 0.7, 0.3, 1) both;
}
.cine-art.alt { animation-name: kenburns-alt; }
@keyframes kenburns { from { transform: scale(1.45) translateX(9%); } to { transform: scale(1.02) translateX(0); } }
@keyframes kenburns-alt { from { transform: scale(1.45) translateX(-9%); } to { transform: scale(1.02) translateX(0); } }
.cine.look .cine-box { visibility: hidden; }
.cine.look .cine-skip {
  position: absolute; left: 0; right: 0; bottom: 6px; text-align: center;
  font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase; color: var(--paper);
  text-shadow: 0 1px 0 rgba(0,0,0,0.7); animation: blink 1.2s steps(1) infinite;
}
.cine-skip { display: none; }
.cine.look .cine-skip { display: block; }
.cine.read .cine-art {
  top: 6px; left: 6px; width: calc(100% - 12px); height: calc(100% - 96px);
  object-fit: contain; animation: none; transform: none;
  border: 2px solid var(--paper); box-sizing: border-box; background: var(--ink);
}
.cine-progress {
  position: absolute; top: 6px; right: 8px; margin: 0;
  font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase;
  color: var(--paper); text-shadow: 0 1px 0 rgba(0,0,0,0.7);
}
.cine-box {
  position: relative; width: calc(100% - 12px); margin: 0 6px 6px; min-height: 80px;
  padding: 6px 9px 6px; text-align: left;
  background: rgba(20,19,28,0.94);
  border: 2px solid var(--paper);
  box-shadow: 0 0 0 2px var(--ink), 0 0 0 3px var(--paper-dim);
}
.cine-box .kicker { margin: 0 0 1px; }
.cine-box .display { font-size: 16px; line-height: 1.05; margin: 0 0 3px; }
.cine-box .body { margin: 0; max-width: none; font-size: 7.5px; min-height: 22px; color: var(--paper); }
.cine-text.typing::after { content: '_'; color: var(--fur); animation: blink 0.7s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.cine-box .btn { position: absolute; right: 8px; bottom: 6px; margin: 0; padding: 3px 8px 2px; font-size: 6px; }
.cine-box .btn[hidden] { display: none; }
#chapter-goal {
  margin: 4px 0 0;
  font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase;
  color: var(--teal); letter-spacing: 0.04em; max-width: 78%;
}
#memory-badge {
  display: inline-block; margin-top: 4px;
  font-family: var(--font-pixel); font-size: 6px; text-transform: uppercase;
  color: var(--fur); letter-spacing: 0.06em;
}
#memory-badge:empty { display: none; }
#memory .credits {
  margin: 4px 0 0; max-width: 78%;
  font-family: var(--font-display); font-style: italic; font-size: 9.5px; color: var(--paper-dim);
}

/* Dialogo en juego (encuentro) */
#dialog {
  position: absolute;
  top: auto;
  left: 20px; right: 20px; bottom: 14px;
  padding: 7px 10px 6px;
  background: rgba(20,19,28,0.92);
  border: 1px solid rgba(243,233,214,0.18);
  box-shadow: 0 2px 0 rgba(0,0,0,0.4);
  font-size: 7.5px;
  line-height: 1.45;
  color: var(--paper);
  pointer-events: auto;
  opacity: 0;
  visibility: hidden;
  transition: opacity 250ms ease, visibility 0s linear 250ms;
}
#dialog.on { opacity: 1; visibility: visible; transition: opacity 250ms ease; }
#dialog .btn { margin-top: 5px; padding: 3px 8px 2px; font-size: 6px; }

/* ---------- Controles tactiles ---------- */

#touch {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: none;
  justify-content: space-between;
  padding: 0 18px calc(14px + env(safe-area-inset-bottom));
  pointer-events: none;
}
.is-touch #touch { display: flex; }
#touch .pad { display: flex; gap: 12px; }
#touch button {
  width: 58px; height: 58px;
  border-radius: 50%;
  background: rgba(243,233,214,0.10);
  border: 1px solid rgba(243,233,214,0.25);
  color: var(--paper);
  font-family: var(--font-pixel);
  font-size: 11px;
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#touch button:active, #touch button.held { background: rgba(224,138,60,0.45); }
#touch .jump { width: 68px; height: 68px; }

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .screen, #hud, #hint, #dialog { transition: none; }
  .cine-art { animation: none; transform: none; }
}

/* En cutscenes el boton de sonido sube a la esquina superior izquierda para no tapar la caja */
#stage:has(.screen.cine.on) .btn-mute, #stage:has(#credits.on) .btn-mute { top: 5px; bottom: auto; left: 6px; right: auto; }
#stage:has(.screen.cine.on) .btn-mute.btn-music, #stage:has(#credits.on) .btn-mute.btn-music { left: 64px; }
