/* Clenchito's Adventure 4 — estilos de página y controles táctiles */

body {
  margin: 0;
  padding: 0;
  background: #1a1a2e;
  color: #ccc;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  font-family: 'Press Start 2P', monospace, Arial;
}

.game-header {
  width: 100%;
  max-width: 800px;
  box-sizing: border-box;
  padding: 16px 20px 12px;
  text-align: center;
  color: #fff;
  background: transparent;
  display: block;
  box-shadow: none;
}

.game-header h1 {
  color: #ffd700;
  text-shadow: 2px 2px 0 #ff6b6b;
  margin: 0 0 10px;
  font-size: clamp(16px, 4vw, 28px);
  line-height: 1.4;
}

.back-button {
  color: #87ceeb;
  background: transparent;
  text-decoration: none;
  font-size: 12px;
  padding: 0;
  font-weight: normal;
}

.back-button:hover {
  color: #ffd700;
  background: transparent;
}

.game-main {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#game-wrap {
  position: relative;
  box-sizing: content-box;
  width: min(960px, calc(100vw - 16px), max(480px, calc((100vh - 130px) * 1.6)));
  border: 4px solid #ffd700;
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(255, 215, 0, 0.3);
  background: #000;
  line-height: 0;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 10;
  border-radius: 4px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  outline: none;
  cursor: crosshair;
}

#game-wrap:fullscreen {
  width: 100vw;
  height: 100vh;
  border: 0;
  border-radius: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-wrap:fullscreen #game-canvas {
  width: min(100vw, 160vh);
  border-radius: 0;
}

.game-toolbar {
  margin-top: 10px;
}

.game-toolbar button {
  font-family: inherit;
  font-size: 10px;
  color: #ffd700;
  background: rgba(0, 0, 0, 0.6);
  border: 2px solid #ffd700;
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
}

.game-toolbar button:hover {
  background: rgba(255, 215, 0, 0.2);
}

.game-info {
  max-width: 820px;
  box-sizing: border-box;
  padding: 16px 20px;
  color: #ccc;
  text-align: center;
  font-family: Arial, sans-serif;
}

.game-info h2 {
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  color: #ffd700;
  margin: 18px 0 8px;
}

.controls {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 10px;
}

.control-item {
  background: rgba(255, 255, 255, 0.1);
  padding: 8px 12px;
  border-radius: 5px;
  font-size: 14px;
}

.control-item kbd {
  background: #ffd700;
  color: #1a1a2e;
  padding: 2px 8px;
  border-radius: 3px;
  font-weight: bold;
  font-family: Arial, sans-serif;
}

.tip {
  margin-top: 16px;
  font-size: 13px;
}

.game-footer {
  width: 100%;
  box-sizing: border-box;
  margin-top: 20px;
  background: #11111f;
  color: #aaa;
  font-family: Arial, sans-serif;
}

/* ───────────── Controles táctiles ───────────── */

#touch-ui {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
  line-height: normal;
  -webkit-user-select: none;
  user-select: none;
}

body.touch.st-playing #touch-ui {
  display: block;
}

body.touch.st-playing,
body.touch.st-paused {
  overflow: hidden;
  overscroll-behavior: none;
}

#look-zone,
#stick-zone {
  position: absolute;
  bottom: 0;
  pointer-events: auto;
  touch-action: none;
}

#look-zone {
  right: 0;
  top: 0;
  width: 50%;
}

#stick-zone {
  left: 0;
  width: 50%;
  height: 62%;
}

.zone-hint {
  position: absolute;
  left: calc(22px + env(safe-area-inset-left, 0px));
  bottom: calc(150px + env(safe-area-inset-bottom, 0px));
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: rgba(255, 215, 0, 0.55);
  pointer-events: none;
}

#stick-base {
  position: absolute;
  left: calc(84px + env(safe-area-inset-left, 0px));
  top: auto;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  width: 120px;
  height: 120px;
  margin-left: -60px;
  border-radius: 50%;
  border: 3px solid rgba(255, 215, 0, 0.8);
  background: rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

#stick-base.active {
  bottom: auto;
  margin-top: -60px;
  background: rgba(0, 0, 0, 0.5);
}

#stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: rgba(255, 215, 0, 0.85);
  border: 3px solid #1a1a2e;
  transform: translate(-50%, -50%);
}

.touch-btn {
  position: absolute;
  pointer-events: auto;
  touch-action: none;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  border: 3px solid #ffd700;
  background: rgba(0, 0, 0, 0.7);
  color: #ffd700;
  font-family: 'Press Start 2P', monospace, Arial;
  font-size: 9px;
  font-weight: bold;
  padding: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  z-index: 2;
}

.touch-btn.pressed {
  background: rgba(255, 215, 0, 0.45);
  color: #1a1a2e;
  transform: scale(0.94);
}

.touch-btn.fire {
  width: 104px;
  height: 104px;
  font-size: 9px;
  right: calc(18px + env(safe-area-inset-right, 0px));
  bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  background: rgba(255, 107, 107, 0.45);
}

#btn-use {
  right: calc(134px + env(safe-area-inset-right, 0px));
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
}

#btn-weapon {
  right: calc(34px + env(safe-area-inset-right, 0px));
  bottom: calc(140px + env(safe-area-inset-bottom, 0px));
}

#btn-pause {
  width: 46px;
  height: 46px;
  font-size: 12px;
  right: calc(12px + env(safe-area-inset-right, 0px));
  top: calc(12px + env(safe-area-inset-top, 0px));
}

/* Móvil horizontal: el juego ocupa toda la altura disponible */
@media (orientation: landscape) and (max-height: 540px) {
  body.touch .game-header,
  body.touch .game-toolbar,
  body.touch .game-info,
  body.touch .game-footer {
    display: none;
  }

  body.touch #game-wrap {
    width: min(calc(100vw - 8px), calc((100vh - 8px) * 1.6));
    border-width: 2px;
    margin-top: 2px;
  }

  /* Botones apilados en el margen derecho para no tapar el HUD */
  body.touch .touch-btn.fire {
    right: calc(10px + env(safe-area-inset-right, 0px));
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  }

  body.touch #btn-use {
    right: calc(24px + env(safe-area-inset-right, 0px));
    bottom: calc(132px + env(safe-area-inset-bottom, 0px));
  }

  body.touch #btn-weapon {
    right: calc(24px + env(safe-area-inset-right, 0px));
    bottom: calc(214px + env(safe-area-inset-bottom, 0px));
  }
}

/* Móvil vertical: canvas arriba a lo ancho, controles debajo */
@media (orientation: portrait) {
  body.touch #game-wrap {
    width: calc(100vw - 8px);
    border-width: 2px;
  }

  body.touch.st-playing .game-header,
  body.touch.st-paused .game-header,
  body.touch.st-playing .game-toolbar,
  body.touch.st-paused .game-toolbar,
  body.touch.st-playing .game-info,
  body.touch.st-paused .game-info,
  body.touch.st-playing .game-footer,
  body.touch.st-paused .game-footer {
    display: none;
  }
}

/* ───────────── Cross-device (dvh, safe areas, escalado nítido, avisos) ───────────── */

/* dvh con respaldo en vh: en móviles la barra del navegador cambia la altura visible */
body {
  min-height: 100dvh;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  box-sizing: border-box;
}

#game-wrap {
  width: min(960px, calc(100vw - 16px), max(480px, calc((100dvh - 130px) * 1.6)));
}

/* main.js fija el tamaño exacto del canvas en píxeles de dispositivo; se centra en el marco */
#game-canvas {
  margin: 0 auto;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

#game-wrap:fullscreen {
  height: 100dvh;
}

body.touch {
  -webkit-touch-callout: none;
}

@media (orientation: landscape) and (max-height: 540px) {
  body.touch #game-wrap {
    width: min(
      calc(100vw - 8px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
      calc((100vh - 8px) * 1.6)
    );
    width: min(
      calc(100vw - 8px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
      calc((100dvh - 8px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) * 1.6)
    );
    margin-top: calc(2px + env(safe-area-inset-top, 0px));
  }
}

#btn-fs-touch {
  width: 46px;
  height: 46px;
  font-size: 16px;
  right: calc(68px + env(safe-area-inset-right, 0px));
  top: calc(12px + env(safe-area-inset-top, 0px));
}

#btn-fs-touch[hidden] {
  display: none;
}

.ios-home-hint {
  font-family: Arial, sans-serif;
  font-size: 13px;
  color: #87ceeb;
  line-height: 1.5;
  margin: 10px auto 0;
  max-width: 520px;
  padding: 0 12px;
}

.ios-home-hint[hidden] {
  display: none;
}

#rotate-hint {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(16px + env(safe-area-inset-top, 0px)) calc(16px + env(safe-area-inset-right, 0px))
           calc(16px + env(safe-area-inset-bottom, 0px)) calc(16px + env(safe-area-inset-left, 0px));
  box-sizing: border-box;
  background: rgba(26, 26, 46, 0.94);
  line-height: normal;
}

#rotate-hint[hidden] {
  display: none;
}

.rotate-box {
  max-width: 340px;
  text-align: center;
  color: #fff;
  font-family: Arial, sans-serif;
  font-size: 15px;
  border: 3px solid #ffd700;
  border-radius: 12px;
  padding: 22px 18px;
  background: #11111f;
}

.rotate-icon {
  font-size: 56px;
  display: inline-block;
  animation: c4-rotate 1.8s ease-in-out infinite;
}

@keyframes c4-rotate {
  0%, 25% { transform: rotate(0deg); }
  55%, 100% { transform: rotate(-90deg); }
}

@media (prefers-reduced-motion: reduce) {
  .rotate-icon { animation: none; transform: rotate(-90deg); }
}

.rotate-title {
  font-family: 'Press Start 2P', monospace;
  color: #ffd700;
  font-size: 14px;
  line-height: 1.5;
  text-shadow: 2px 2px 0 #ff6b6b;
}

#btn-rotate-dismiss {
  margin-top: 12px;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: #1a1a2e;
  background: #ffd700;
  border: 0;
  border-radius: 6px;
  padding: 12px 14px;
  cursor: pointer;
  touch-action: manipulation;
}

/* ───────────── Controles táctiles fuera del HUD ───────────── */

/* Horizontal jugando (cualquier alto): el canvas deja canaletas laterales de 152 px para el stick
   (izquierda) y los botones (derecha), así ninguno se monta sobre el HUD */
@media (orientation: landscape) {
  body.touch.st-playing .game-header,
  body.touch.st-paused .game-header,
  body.touch.st-playing .game-toolbar,
  body.touch.st-paused .game-toolbar,
  body.touch.st-playing .game-info,
  body.touch.st-paused .game-info,
  body.touch.st-playing .game-footer,
  body.touch.st-paused .game-footer {
    display: none;
  }

  body.touch.st-playing #game-wrap,
  body.touch.st-paused #game-wrap {
    width: min(
      calc(100vw - 304px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
      calc((100vh - 8px) * 1.6)
    );
    width: min(
      calc(100vw - 304px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
      calc((100dvh - 8px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) * 1.6)
    );
    border-width: 2px;
    margin-top: calc(2px + env(safe-area-inset-top, 0px));
  }

  /* Botones apilados en la canaleta derecha también en tablets */
  body.touch .touch-btn.fire {
    right: calc(10px + env(safe-area-inset-right, 0px));
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  }

  body.touch #btn-use {
    right: calc(24px + env(safe-area-inset-right, 0px));
    bottom: calc(132px + env(safe-area-inset-bottom, 0px));
  }

  body.touch #btn-weapon {
    right: calc(24px + env(safe-area-inset-right, 0px));
    bottom: calc(214px + env(safe-area-inset-bottom, 0px));
  }
}

/* Vertical jugando: controles justo debajo del canvas (main.js calcula --c4-ctl-top) */
body.touch.portrait-play #stick-zone {
  top: var(--c4-ctl-top, 60%);
  height: auto;
}

body.touch.portrait-play #stick-base:not(.active) {
  top: 64px;
  bottom: auto;
}

body.touch.portrait-play .zone-hint {
  top: 34px;
  bottom: auto;
}

body.touch.portrait-play .touch-btn.fire {
  top: calc(var(--c4-ctl-top, 60%) + 100px);
  bottom: auto;
}

body.touch.portrait-play #btn-weapon {
  top: calc(var(--c4-ctl-top, 60%) + 8px);
  bottom: auto;
}

body.touch.portrait-play #btn-use {
  top: calc(var(--c4-ctl-top, 60%) + 150px);
  bottom: auto;
}
