:root {
  color-scheme: dark;
  font-family: Inter, system-ui, sans-serif;
  --gold: #ffd55c;
  --wood: #6d3517;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #07120d;
  touch-action: none;
}

#game-shell {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 30%, #244c32, #07120d 70%);
}

canvas {
  display: block;
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
  background: #87ceeb;
  box-shadow: 0 0 48px #0009;
}

.card {
  position: absolute;
  left: 50%;
  top: 50%;
  width: min(88vw, 560px);
  transform: translate(-50%, -50%);
  padding: 28px 30px;
  border: 4px solid #d18a3c;
  border-radius: 22px;
  color: white;
  text-align: center;
  background: linear-gradient(#5d2d16ee, #2b140bee);
  box-shadow: 0 16px 50px #000a, inset 0 0 0 3px #2a1208;
  z-index: 10;
}

.card h1, .card h2 { margin: 4px 0 12px; color: var(--gold); }
.card h1 { font-size: clamp(34px, 8vw, 66px); letter-spacing: .04em; }
.card p { font-size: clamp(14px, 2.5vw, 19px); }
#message-text { margin: 6px 0; white-space: pre-line; line-height: 1.24; }
#message-card {
  width: min(94vw, 760px);
  max-height: 96vh;
  padding: 14px 24px 16px;
  overflow: auto;
  touch-action: pan-y; overscroll-behavior: contain;
}
#message-card h2 { margin: 0 0 4px; }
#message-card .record-text { margin: 5px 0 9px; }
#message-card button { padding-block: 10px; }
.records-summary { min-height: 36px; margin: 8px 0 14px; color: #fff0a5; font-weight: 800; }
.campaign-card { width: min(97vw, 1180px); max-height: 98vh; padding: 14px 18px 12px; overflow: hidden; }
.campaign-card h1 { margin-bottom: 2px; }
.campaign-intro { margin: 2px 0 5px; }
.campaign-progress { margin: 4px 0 7px; color: #dfff72; font-weight: 900; }
.map-character-picker {
  display: flex; justify-content: center; align-items: center; gap: 7px; margin: 2px 0 7px;
  color: #fff3b4; font: 900 11px/1 system-ui, sans-serif; letter-spacing: .08em;
}
.card .map-character-choice {
  min-width: 78px; padding: 6px 10px; border-width: 2px; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
  color: #fff; background: linear-gradient(#52604a, #283323); box-shadow: 0 3px 0 #111b0f;
  font-size: 12px; opacity: .62;
}
.card .map-character-choice::before {
  content: ''; width: 22px; height: 30px; margin-block: -5px;
  background-position: center; background-repeat: no-repeat; background-size: contain;
}
.card .map-character-choice[data-map-character="buendi"]::before { background-image: url('assets/personaje_buendi_aprobado_v1.png'); }
.card .map-character-choice[data-map-character="topo"]::before { background-image: url('assets/personaje_topo_aprobado_v1.png'); }
.card .map-character-choice[data-map-character="buendi"] { border-color: #ff8c71; }
.card .map-character-choice[data-map-character="topo"] { border-color: #a5e978; }
.card .map-character-choice.active { opacity: 1; transform: translateY(-1px); box-shadow: 0 4px 0 #111b0f, 0 0 12px #e6ff6d75; }
.map-viewport {
  width: 100%; height: min(61vh, 610px); margin: 6px 0 9px; overflow: auto;
  border: 4px solid #d18a3c; border-radius: 18px; background: #173421;
  box-shadow: inset 0 0 24px #0009; scroll-behavior: smooth; scrollbar-width: thin;
  touch-action: pan-x pan-y; overscroll-behavior: contain;
}
.level-map {
  position: relative; width: 1450px; height: auto; aspect-ratio: 16 / 9;
  background: #173421 url('assets/mapa_requena_aventura_v4_arrabal.png') center/100% 100% no-repeat;
}
.map-art { position: absolute; inset: 0; width: 100%; height: 100%; display: block; object-fit: contain; z-index: 1; pointer-events: none; }
.card .level-node {
  position: absolute; width: 62px; min-width: 62px; height: 62px; margin: 0; padding: 0;
  display: grid; place-items: center; border-radius: 50%;
  color: #3b210b; background: radial-gradient(circle at 35% 25%, #fff6a3, #f0b326 70%);
  border: 5px solid #fff6c3; box-shadow: 0 5px 0 #704315, 0 0 18px #ffdf5fa8;
  transform: translate(-50%, -50%); z-index: 3;
}
.level-node strong { font-size: 23px; }
.level-node .map-sign {
  position: absolute; left: 50%; bottom: 70px; width: 148px; padding: 5px 7px;
  transform: translateX(-50%) rotate(-1deg); border: 3px solid #f0c56d; border-radius: 8px;
  color: #fff8dc; background: linear-gradient(#70411f, #3b200e); font-size: 14px; line-height: 1.1;
  font-weight: 1000; letter-spacing: .07em; text-shadow: 0 2px 0 #0007; pointer-events: none;
  box-shadow: 0 4px 0 #2a1409, 0 6px 12px #0007;
}
.level-node .map-sign::before, .level-node .map-sign::after {
  content: ''; position: absolute; top: 50%; width: 9px; height: 9px; border-radius: 50%;
  background: #ffe798; box-shadow: inset 0 0 0 2px #624016; transform: translateY(-50%);
}
.level-node .map-sign::before { left: 6px; }
.level-node .map-sign::after { right: 6px; }
.level-node[data-level="avenida"] .map-sign {
  color: #efffd0; border-color: #b9e879; background: linear-gradient(#548f38, #285b2c); box-shadow: 0 4px 0 #173c20, 0 6px 14px #0007;
}
.level-node[data-level="villa"] .map-sign {
  color: #fff0d3; border-color: #ffab72; background: linear-gradient(#bd493e, #782626); box-shadow: 0 4px 0 #48161a, 0 6px 14px #0007;
}
.level-node[data-level="penas"] .map-sign {
  color: #513304; border-color: #fff1a0; background: linear-gradient(#ffe36d, #dca52c); text-shadow: 0 1px 0 #fff5a8;
  box-shadow: 0 4px 0 #8c6418, 0 6px 14px #0007;
}
.level-node .node-status {
  position: absolute; left: 50%; top: 69px; width: 104px; padding: 5px 7px; transform: translateX(-50%);
  border: 2px solid #f0c56d; border-radius: 9px; color: #dfff72; background: #2c180ddd;
  font-size: 11px; line-height: 1.15; pointer-events: none;
}
.card .level-node:disabled { opacity: 1; filter: none; }
.card .level-node.locked { color: #b9b1a7; border-color: #9a9188; background: radial-gradient(circle at 35% 25%, #777, #343434 70%); box-shadow: 0 5px 0 #222, 0 0 12px #0008; }
.card .level-node.next { color: #44300b; animation: nodePulse 1.25s ease-in-out infinite alternate; }
.card .level-node.completed { border-color: #dfff72; box-shadow: 0 5px 0 #315617, 0 0 24px #baf05bcc; }
.card .level-node.selected { animation: selectedNode .23s ease-in-out 2 alternate; box-shadow: 0 5px 0 #704315, 0 0 30px #ffffff; }
.map-avatar {
  position: absolute; left: 21.5%; top: 75%; width: 58px; height: 88px; z-index: 5;
  transform: translate(-50%, -103%); transition: left 2.2s ease-in-out, top 2.2s ease-in-out;
  background-position: center bottom;
  background-repeat: no-repeat;
  background-size: contain;
  filter: drop-shadow(0 5px 4px #0007);
}
.map-avatar.buendi { background-image: url('assets/personaje_buendi_aprobado_v1.png'); }
.map-avatar.topo { background-image: url('assets/personaje_topo_aprobado_v1.png'); }
.map-avatar i { display: none; }
.avatar-name {
  position: absolute; left: 50%; bottom: 76px; padding: 3px 7px; transform: translateX(-50%);
  border: 2px solid #fff0a0; border-radius: 8px; color: white; background: #29170de6;
  font: 1000 9px/1 system-ui, sans-serif; letter-spacing: .07em; white-space: nowrap;
}
.avatar-head { left: 8px; top: 5px; width: 34px; height: 29px; border-radius: 48% 48% 44% 44%; background: #f1bd8b; border: 3px solid #542d1b; }
.avatar-head::before { content: ''; position: absolute; left: -4px; top: -8px; width: 38px; height: 15px; border-radius: 14px 14px 5px 5px; background: #e33d31; border: 3px solid #542d1b; }
.avatar-head::after { content: ''; position: absolute; right: 3px; top: 10px; width: 5px; height: 5px; border-radius: 50%; background: #2a1a12; box-shadow: -13px 0 #2a1a12; }
.avatar-body { left: 7px; top: 31px; width: 36px; height: 30px; border: 3px solid #542d1b; border-radius: 9px 9px 6px 6px; background: linear-gradient(90deg, #e43c31 0 25%, #2670b8 25% 75%, #e43c31 75%); }
.avatar-feet { left: 5px; top: 59px; width: 17px; height: 9px; border-radius: 7px 2px 3px 3px; background: #55301f; box-shadow: 23px 0 #55301f; }
.map-avatar.topo .avatar-head::before { background: #4d9d3b; }
.map-avatar.topo .avatar-body { background: linear-gradient(90deg, #4d9d3b 0 25%, #7b315f 25% 75%, #4d9d3b 75%); }
.map-avatar.topo .avatar-name { border-color: #c9f38f; }
.map-avatar.choosing { animation: chooseHop .26s ease-in-out 2 alternate; }
.map-avatar.to-villa { animation: walkToVilla 3.1s ease-in-out forwards; }
.map-avatar.at-penas { left: 50%; top: 28%; }
.map-avatar.at-agricultura { left: 70%; top: 54%; }
.map-avatar.at-cuevas { left: 85%; top: 31%; }
@keyframes nodePulse { from { scale: .96; } to { scale: 1.08; } }
@keyframes selectedNode { to { scale: 1.16; } }
@keyframes chooseHop { to { margin-top: -12px; } }
@keyframes walkToVilla {
  0% { left: 21.5%; top: 75%; transform: translate(-50%, -100%) rotate(0); }
  30% { left: 25%; top: 64%; transform: translate(-50%, -100%) rotate(4deg); }
  65% { left: 29%; top: 53%; transform: translate(-50%, -100%) rotate(-3deg); }
  100% { left: 32%; top: 44%; transform: translate(-50%, -100%) rotate(0); }
}
.player-name-label { display: block; margin: 8px 0 5px; color: #dfff9a; font: 900 12px/1 system-ui, sans-serif; letter-spacing: .12em; }
.player-name {
  width: min(100%, 280px); padding: 9px 14px; border: 3px solid #d18a3c; border-radius: 12px;
  color: #fff8cf; background: #1b120dd9; text-align: center; text-transform: uppercase;
  font: 900 17px/1 system-ui, sans-serif; outline: none;
}
.player-name:focus { border-color: #dfff72; box-shadow: 0 0 0 3px #dfff7233; }
.player-pin { width: 150px; letter-spacing: .45em; }
.profile-status { min-height: 18px; margin: 5px 0 8px; color: #ffd96a; font: 800 12px/1.2 system-ui, sans-serif; }
.ranking-list { margin: 12px 0 16px; display: grid; gap: 5px; }
.ranking-row {
  display: grid; grid-template-columns: 40px 1fr 90px 70px; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: 9px; background: #1d160fcc; text-align: left; font-weight: 800;
}
.ranking-row:nth-child(1) { color: #ffe36f; background: #5b3b18dd; }
.ranking-row:nth-child(2) { color: #e7eef4; }
.ranking-row:nth-child(3) { color: #efb476; }
.ranking-empty { padding: 24px 8px; color: #fff1bd; }
#ranking-card { width: min(94vw, 680px); max-height: 94vh; overflow: auto; touch-action: pan-y; overscroll-behavior: contain; }
.medal-badge {
  width: 68px; height: 68px; margin: 3px auto 5px;
  display: grid; place-items: center; border: 5px solid #fff4c1;
  border-radius: 50%; color: #3b210b; font: 1000 16px/1 system-ui, sans-serif;
  box-shadow: 0 0 25px currentColor, inset 0 0 0 5px #0002;
}
.medal-badge.gold { background: linear-gradient(135deg, #fff4a0, #e5a91f); color: #805100; }
.medal-badge.silver { background: linear-gradient(135deg, #ffffff, #9eabb5); color: #4b5962; }
.medal-badge.bronze { background: linear-gradient(135deg, #efbb7a, #984d20); color: #633014; }
.record-text { color: #dfff72; font-weight: 900; }
.eyebrow { margin: 0; color: #d9ff9c; font-weight: 900; letter-spacing: .14em; }
.help { opacity: .8; }
.hidden { display: none; }

#pause-button {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 8;
  padding: 9px 18px;
  border: 3px solid #f6d476;
  border-radius: 12px;
  color: #fff8dc;
  background: #3a241bd9;
  font: 900 15px/1 system-ui, sans-serif;
  box-shadow: 0 4px 0 #160b07aa;
}

.card button {
  min-width: 180px;
  padding: 13px 24px;
  border: 3px solid #fff3b4;
  border-radius: 14px;
  color: #382000;
  background: linear-gradient(#ffe980, #ffb42f);
  font: 900 20px/1 system-ui, sans-serif;
  cursor: pointer;
  box-shadow: 0 5px 0 #9a4d13;
}
.card button + button { margin-left: 10px; }
.card button.secondary {
  color: #fff8dc;
  background: linear-gradient(#6c8442, #365428);
  box-shadow: 0 5px 0 #183217;
}
.card button:disabled { opacity: .45; cursor: not-allowed; filter: grayscale(.7); }
.character-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 18px 0;
}
.card .character-choice {
  min-width: 0;
  margin: 0;
  padding: 8px 8px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #f4ffe7;
  background: linear-gradient(#41553a, #20301e);
  box-shadow: 0 4px 0 #101b0f;
  opacity: .72;
}
.card .character-choice::before {
  content: '';
  width: 72px;
  height: 86px;
  margin-bottom: 3px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  filter: drop-shadow(0 4px 3px #0008);
}
.card .character-choice[data-character="buendi"]::before { background-image: url('assets/personaje_buendi_aprobado_v1.png'); }
.card .character-choice[data-character="topo"]::before { background-image: url('assets/personaje_topo_aprobado_v1.png'); }
.card .character-choice[data-character="buendi"] { border-color: #ff9b78; }
.card .character-choice[data-character="topo"] { border-color: #a8e67d; }
.card .character-choice.active {
  opacity: 1;
  transform: translateY(-2px);
  box-shadow: 0 6px 0 #101b0f, 0 0 20px #dfff6170;
}
.character-choice small {
  display: block;
  margin-top: 7px;
  font: 700 12px/1.15 system-ui, sans-serif;
}

#touch-controls {
  position: absolute;
  inset: auto 0 max(14px, env(safe-area-inset-bottom)) 0;
  display: flex;
  justify-content: space-between;
  align-items: end;
  padding: 0 max(18px, env(safe-area-inset-right)) 0 max(18px, env(safe-area-inset-left));
  pointer-events: none;
}

.touch-group { display: flex; gap: 14px; }
.touch {
  width: clamp(58px, 10vw, 84px);
  aspect-ratio: 1;
  border: 3px solid #ffffffaa;
  border-radius: 50%;
  color: white;
  background: #14251db8;
  font: 900 clamp(22px, 5vw, 38px)/1 system-ui, sans-serif;
  box-shadow: 0 6px 18px #0007;
  pointer-events: auto;
  user-select: none;
  -webkit-user-select: none;
}

.touch.pressed { transform: translateY(3px) scale(.96); background: #478f3fd9; }
.jump { width: clamp(70px, 12vw, 96px); background: #8b3d1fc9; }
.action-group { align-items: end; }
.fire { background: #487b24d9; color: #dfff61; }

@media (hover: hover) and (pointer: fine) {
  #touch-controls { opacity: .45; }
}

@media (max-height: 620px) and (orientation: landscape) {
  #message-card { width: min(96vw, 820px); padding: 8px 20px 10px; }
  #message-card h2 { font-size: 24px; }
  #message-card p { font-size: 13px; }
  #message-text { line-height: 1.14; }
  .medal-badge {
    position: absolute;
    top: 10px;
    right: 18px;
    width: 54px;
    height: 54px;
    margin: 0;
    border-width: 4px;
    font-size: 13px;
  }
  #message-card button { min-width: 150px; padding: 8px 18px; font-size: 17px; }
  .campaign-card { width: 98vw; padding: 6px 10px 8px; }
  .campaign-card h1 { font-size: 28px; margin: 0; }
  .campaign-card .eyebrow { font-size: 12px; }
  .campaign-intro, .campaign-progress { margin: 4px 0 6px; font-size: 13px; }
  .map-viewport { height: 62vh; margin: 4px 0 6px; }
  .level-map { width: 1180px; height: auto; aspect-ratio: 16 / 9; }
  .card .level-node { width: 50px; min-width: 50px; height: 50px; border-width: 4px; }
  .level-node strong { font-size: 18px; }
  .level-node small { bottom: 56px; width: 128px; font-size: 9px; }
  .level-node small + small { top: 55px; }
  #campaign-ranking-button { padding: 8px 16px; font-size: 16px; }
}

/* Botones de prueba: solo con «?pruebas=1». */
body:not(.modo-pruebas) #test-spiny-button, body:not(.modo-pruebas) #test-final-button, body:not(.modo-pruebas) #campaign-boss-test-button, body:not(.modo-pruebas) #campaign-boss-test-button { display: none !important; }

/* Móvil en horizontal: el mapa ocupa menos alto y toda la tarjeta se puede desplazar con el dedo */
@media (orientation: landscape) and (max-height: 500px) and (pointer: coarse) {
  .campaign-card {
    max-height: 100vh; padding: 6px 12px 8px;
    overflow-y: auto; overscroll-behavior: contain; touch-action: pan-y;
  }
  .campaign-card h1 { font-size: 24px; }
  .campaign-intro, .campaign-progress { margin: 2px 0 4px; font-size: 12px; }
  .map-viewport { height: 44vh; margin: 3px 0 5px; }

  /* Pantalla de inicio en horizontal: título y personaje a la izquierda, botones a la derecha */
  #start-card:not(.hidden) {
    width: min(94vw, 760px); max-height: 100vh; padding: 8px 16px 10px; overflow-y: auto;
    display: grid; grid-template-columns: 1fr 1fr; gap: 3px 16px; align-items: center;
    touch-action: pan-y; overscroll-behavior: contain;
  }
  #start-card > * { margin: 2px 0; }
  #start-card h1 { font-size: 30px; margin: 0 0 2px; }
  #start-card > :nth-child(1) { grid-area: 1 / 1; }
  #start-card > :nth-child(2) { grid-area: 2 / 1; }
  #start-card > :nth-child(3) { grid-area: 3 / 1; font-size: 13px; }
  #start-card > :nth-child(4) { grid-area: 4 / 1; }
  #start-card > :nth-child(5) { grid-area: 5 / 1; }
  #start-card > :nth-child(6) { grid-area: 6 / 1; }
  #start-card > :nth-child(7) { grid-area: 7 / 1; min-height: 0; margin: 0; }
  #start-card > :nth-child(8) { grid-area: 1 / 2; }
  #start-card > :nth-child(9) { grid-area: 2 / 2; }
  #start-card > :nth-child(10) { grid-area: 3 / 2; }
  #start-card > :nth-child(11) { grid-area: 4 / 2; }
  #start-card > :nth-child(12) { grid-area: 5 / 2; }
  #start-card > :nth-child(13) { grid-area: 6 / 2; font-size: 11px; }
  #start-card button { padding: 7px 10px; font-size: 14px; }
  #start-card .player-name { padding: 5px 8px; font-size: 15px; }
  #start-card .character-choice { padding: 6px 8px; }
  /* Sin desplazamiento: en horizontal se oculta el texto descriptivo y se compactan los bloques. */
  #start-card:not(.hidden) { overflow: hidden; }
  #start-card > p:not([class]) { display: none; }
  #start-card h1 { font-size: 26px; }
  #start-card .character-picker { gap: 8px; }
  #start-card .character-choice { padding: 4px 6px; font-size: 13px; }
  #start-card .character-choice small { font-size: 10px; }
  #start-card .records-summary { margin: 0; min-height: 0; font-size: 12px; }
  #start-card #start-button, #start-card #ranking-button, #start-card #back-to-map-button { padding: 6px 10px; }
  #start-card .help { font-size: 10px; margin: 0; }
  #ranking-card, #message-card { max-height: 100vh; padding: 8px 16px 10px; }
  #ranking-card h2, #message-card h2 { font-size: 24px; }
  #ranking-card { touch-action: pan-y; overscroll-behavior: contain; }
}

/* Aviso «gira el móvil» (orientacion.js): solo se muestra en móviles táctiles en vertical */
#rotate-overlay {
  position: fixed; inset: 0; z-index: 1000; display: none; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px; padding: 24px; text-align: center;
  color: #fff; background: radial-gradient(circle at 50% 30%, #244c32, #07120d 70%);
}
#rotate-overlay.visible { display: flex; }
#rotate-overlay h2 { margin: 14px 0 0; color: var(--gold); font-size: clamp(28px, 8vw, 40px); }
#rotate-overlay p { margin: 0; font-size: 17px; }
#rotate-overlay .rotate-small { font-size: 14px; opacity: .8; }
.rotate-phone {
  width: 62px; height: 108px; border: 5px solid #fff6c3; border-radius: 14px;
  box-shadow: 0 6px 18px #0009; animation: rotarMovil 2.6s ease-in-out infinite;
}
@keyframes rotarMovil {
  0%, 15% { transform: rotate(0deg); }
  45%, 75% { transform: rotate(90deg); }
  100% { transform: rotate(0deg); }
}

