/* ============================================================
   CELLBOUND — pixel-perfect retro CRT aesthetic
   Fully responsive, iOS-friendly with touch controls
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=VT323&family=Press+Start+2P&display=swap');

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;       /* dynamic viewport height (iOS-aware) */
  height: -webkit-fill-available;
  background: #06060c;
  color: #f1faee;
  font-family: 'VT323', 'Courier New', monospace;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;   /* disable browser gestures */
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at center, #0d0d18 0%, #050509 70%, #000 100%);
  /* iOS safe-area padding */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

#game-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  gap: 8px;
  padding: 8px;
}

/* Game container — fits viewport while maintaining 16:9 */
#game-container {
  position: relative;
  /* fluid 16:9 fit: take whichever dimension is binding */
  width: min(100%, calc(100vh * 16 / 9));
  width: min(100%, calc(100dvh * 16 / 9));
  aspect-ratio: 16 / 9;
  max-height: 100%;
  border: 2px solid #2a2e3e;
  box-shadow:
    0 0 0 1px #000,
    0 0 40px rgba(95, 196, 212, 0.08),
    inset 0 0 80px rgba(0, 0, 0, 0.6);
  background: #000;
  overflow: hidden;
  image-rendering: pixelated;
}

/* CRT scanline overlay */
#game-container::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(
      0deg,
      rgba(0, 0, 0, 0.18) 0px,
      rgba(0, 0, 0, 0.18) 1px,
      transparent 1px,
      transparent 3px
    );
  z-index: 10;
  mix-blend-mode: multiply;
}

/* faint vignette */
#game-container::before {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse at center, transparent 50%, rgba(0,0,0,0.55) 100%);
  z-index: 9;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  image-rendering: -moz-crisp-edges;
  touch-action: none;
}

/* ============ START / TITLE OVERLAY ============ */

#overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 50;
  background: rgba(6, 6, 12, 0.9);
  text-align: center;
  transition: opacity 0.3s;
  cursor: pointer;
  padding: 16px;
  overflow: auto;
}

#overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: 100%;
}

#title {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(24px, 6vw, 56px);
  letter-spacing: clamp(2px, 0.6vw, 6px);
  color: #f1faee;
  text-shadow:
    0 0 20px rgba(95, 196, 212, 0.5),
    3px 3px 0 #1a1d29,
    6px 6px 0 #5fc4d4;
  margin-bottom: 4px;
  animation: glow 2.6s ease-in-out infinite;
  line-height: 1.2;
}

@keyframes glow {
  0%, 100% { text-shadow: 0 0 20px rgba(95,196,212,0.5), 3px 3px 0 #1a1d29, 6px 6px 0 #5fc4d4; }
  50%      { text-shadow: 0 0 36px rgba(95,196,212,0.8), 3px 3px 0 #1a1d29, 6px 6px 0 #e76f51; }
}

#subtitle {
  font-size: clamp(14px, 2.5vw, 28px);
  color: #5fc4d4;
  letter-spacing: clamp(4px, 1vw, 12px);
  margin-bottom: clamp(12px, 4vh, 36px);
  opacity: 0.85;
}

#hint {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(8px, 1.4vw, 12px);
  letter-spacing: 2px;
  color: #f4a261;
  margin-bottom: clamp(12px, 3vh, 28px);
  animation: blink 1.2s infinite;
}

@keyframes blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0.25; }
}

#controls-desktop {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  font-size: clamp(14px, 1.8vw, 22px);
  color: #f1faee;
  background: rgba(20, 22, 34, 0.6);
  border: 1px solid #2a2e3e;
  padding: clamp(10px, 1.5vw, 18px) clamp(14px, 2.5vw, 28px);
  min-width: clamp(220px, 40vw, 320px);
}

#controls-mobile { display: none; }

.ctrl-row {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-start;
  white-space: nowrap;
}

.ctrl-row > span:first-child {
  flex: 0 0 70px;
  text-align: left;
  color: #8a8aa0;
  letter-spacing: 1px;
}

.key {
  display: inline-block;
  min-width: 24px;
  padding: 2px 6px;
  border: 1px solid #5fc4d4;
  border-bottom: 3px solid #5fc4d4;
  background: rgba(95, 196, 212, 0.08);
  color: #f1faee;
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(8px, 1vw, 11px);
  text-align: center;
  letter-spacing: 0;
}

.dim {
  color: #5a5a70;
  font-size: clamp(11px, 1.4vw, 18px);
}

#footer {
  font-size: clamp(10px, 1.2vw, 16px);
  color: #5a5a70;
  letter-spacing: 2px;
  flex-shrink: 0;
}

/* ============ TOUCH CONTROLS ============ */

#touch-controls {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 30;
}

.touch-cluster {
  position: absolute;
  bottom: 8px;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.touch-left  { left: 8px; }
.touch-right { right: 8px; }

/* D-pad */
.dpad {
  position: relative;
  width: 132px;
  height: 132px;
}

.dpad-center {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 32%;
  height: 32%;
  transform: translate(-50%, -50%);
  background: rgba(95, 196, 212, 0.12);
  border: 1px solid rgba(95, 196, 212, 0.3);
  pointer-events: none;
}

.tbtn {
  background: rgba(20, 22, 34, 0.55);
  border: 2px solid rgba(95, 196, 212, 0.55);
  color: #f1faee;
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  letter-spacing: 1px;
  border-radius: 0;
  cursor: pointer;
  transition: background 0.05s, border-color 0.05s, transform 0.05s;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  text-shadow: 0 1px 0 #000;
  padding: 0;
  position: relative;
}

.tbtn::after {
  content: '';
  position: absolute;
  inset: 2px;
  border: 1px solid rgba(95, 196, 212, 0.15);
  pointer-events: none;
}

.tbtn.active,
.tbtn:active {
  background: rgba(95, 196, 212, 0.45);
  border-color: #5fc4d4;
  transform: scale(0.96);
  box-shadow: 0 0 12px rgba(95, 196, 212, 0.6);
}

/* D-pad button positioning */
.dpad-up,
.dpad-down,
.dpad-left,
.dpad-right {
  position: absolute;
  width: 36%;
  height: 36%;
}

.dpad-up    { top: 0;       left: 32%;  display: none; /* not needed; jump is separate */ }
.dpad-down  { bottom: 0;    left: 32%; }
.dpad-left  { left: 0;      top: 32%; }
.dpad-right { right: 0;     top: 32%; }

.dpad-left::before  { content: '◀'; font-size: 18px; }
.dpad-right::before { content: '▶'; font-size: 18px; }
.dpad-down::before  { content: '▼'; font-size: 14px; opacity: 0.7; }

/* Action pad — diamond layout */
.action-pad {
  position: relative;
  width: 156px;
  height: 156px;
}

.act {
  position: absolute;
  width: 64px;
  height: 64px;
  border-radius: 50%;
  font-size: 11px;
}

.act > span { display: block; }

.act-jump  { top: 0;            left: 50%; transform: translateX(-50%); border-color: rgba(244, 162, 97, 0.7); }
.act-jump.active,
.act-jump:active { background: rgba(244, 162, 97, 0.4); border-color: #f4a261; transform: translateX(-50%) scale(0.96); }

.act-atk   { top: 50%;          right: 0;  transform: translateY(-50%); border-color: rgba(230, 57, 70, 0.7); }
.act-atk.active,
.act-atk:active { background: rgba(230, 57, 70, 0.4); border-color: #e63946; transform: translateY(-50%) scale(0.96); }

.act-heavy { bottom: 0;         left: 50%; transform: translateX(-50%); border-color: rgba(244, 208, 63, 0.7); }
.act-heavy.active,
.act-heavy:active { background: rgba(244, 208, 63, 0.35); border-color: #f4d03f; transform: translateX(-50%) scale(0.96); }

.act-roll  { top: 50%;          left: 0;   transform: translateY(-50%); border-color: rgba(95, 196, 212, 0.7); }
.act-roll.active,
.act-roll:active { background: rgba(95, 196, 212, 0.4); border-color: #5fc4d4; transform: translateY(-50%) scale(0.96); }

.act-use {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  font-size: 10px;
  border-color: rgba(82, 183, 136, 0.7);
}
.act-use.active,
.act-use:active { background: rgba(82, 183, 136, 0.4); border-color: #52b788; transform: scale(0.96); }

.pause-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 4px;
  font-size: 18px;
  pointer-events: auto;
  z-index: 35;
}

/* ============ TOUCH-DEVICE-ONLY DISPLAY ============ */

@media (pointer: coarse), (max-width: 900px), (hover: none) {
  #touch-controls { display: block; }
  #controls-desktop { display: none; }
  #controls-mobile {
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
    margin-top: 8px;
    color: #8a8aa0;
    font-size: clamp(11px, 2vw, 16px);
    letter-spacing: 1px;
  }
  .mctrl { color: #f1faee; }
  .mctrl-dim { color: #5a5a70; font-size: clamp(10px, 1.6vw, 14px); }
  .rotate-hint { color: #f4a261; margin-top: 6px; }

  /* Fill viewport but maintain 16:9 aspect (letterbox if needed).
     Keep safe-area padding so the canvas isn't hidden behind the iPhone notch. */
  body {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  #game-wrap { padding: 0; gap: 0; height: 100%; height: 100dvh; }
  #game-container {
    border: none;
    box-shadow: none;
    width: min(100vw, calc(100dvh * 16 / 9));
    width: min(100vw, calc(100vh * 16 / 9));
    height: min(100dvh, calc(100vw * 9 / 16));
    height: min(100vh,  calc(100vw * 9 / 16));
    max-width: 100vw;
    max-height: 100dvh;
    aspect-ratio: 16 / 9;
  }
  #footer { display: none; }

  /* Respect iOS safe areas (notch, home indicator) on touch button positions */
  .touch-left   { left:   max(8px, env(safe-area-inset-left)); }
  .touch-right  { right:  max(8px, env(safe-area-inset-right)); }
  .touch-cluster{ bottom: max(8px, env(safe-area-inset-bottom)); }
  .pause-btn    { top:    max(8px, env(safe-area-inset-top)); right: max(8px, env(safe-area-inset-right)); }
}

/* In landscape on small screens, hide rotate hint */
@media (orientation: landscape) {
  .rotate-hint { display: none; }
}

/* On portrait phones, scale d-pad/buttons down a bit */
@media (orientation: portrait) and (max-width: 600px) {
  .dpad { width: 110px; height: 110px; }
  .action-pad { width: 130px; height: 130px; }
  .act { width: 54px; height: 54px; font-size: 10px; }
  .act-use { width: 42px; height: 42px; font-size: 9px; }
}

/* On very small landscape (iPhone SE landscape), trim padding */
@media (orientation: landscape) and (max-height: 420px) {
  .touch-cluster { bottom: 4px; }
  .touch-left { left: 4px; }
  .touch-right { right: 4px; }
  .dpad { width: 110px; height: 110px; }
  .action-pad { width: 130px; height: 130px; }
  .act { width: 52px; height: 52px; font-size: 10px; }
  .act-use { width: 38px; height: 38px; font-size: 8px; }
  .pause-btn { width: 28px; height: 28px; font-size: 14px; }
}

/* desktop / hover devices: hide touch controls even at narrow widths */
@media (hover: hover) and (pointer: fine) {
  #touch-controls { display: none !important; }
  #controls-desktop { display: grid; }
  #controls-mobile { display: none; }
}
