/* ==========================================================
   CHESS+ ROGUELIKE: FIXED 512x288 PIXEL RETRO STYLE
   ========================================================== */

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

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  -webkit-font-smoothing: none;
  -moz-osx-font-smoothing: grayscale;
}

:root {
  --font-pixel: 'Silkscreen', 'Press Start 2P', monospace;
  --font-title: 'Press Start 2P', monospace;

  /* Palette: 16-bit retro arcade */
  --bg-viewport: #07090f;
  --bg-app: #0e111a;
  --bg-panel: #141824;
  --bg-card: #1b2030;
  --border-dark: #22283a;
  --border-light: #3a4460;
  --border-gold: #c89b3c;
  --gold: #f0c242;
  --gold-dim: #9a7620;
  --text-main: #e8ecf4;
  --text-muted: #828c9e;
  --accent-red: #e04848;
  --accent-green: #3ec86a;
  --accent-blue: #3e88e8;

  /* Board geometry */
  --tile-size: 22px;
}

html, body {
  width: 100%;
  height: 100%;
  background: var(--bg-viewport);
  color: var(--text-main);
  font-family: var(--font-pixel);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.hidden {
  display: none !important;
}

/* ===== VIEWPORT CONTAINER (AUTO-CENTER & AUTO-SCALE) ===== */
#gameViewport {
  width: 100vw;
  width: 100dvw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--bg-viewport);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  touch-action: manipulation;
}

/* ===== FIXED 512x288 APP CANVAS ===== */
#app {
  width: 512px;
  height: 288px;
  min-width: 512px;
  min-height: 288px;
  max-width: 512px;
  max-height: 288px;
  position: relative;
  background: var(--bg-app);
  border: 2px solid var(--border-dark);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transform-origin: center center;
}

/* ===== HEADER (20px) ===== */
.game-header {
  height: 20px;
  min-height: 20px;
  max-height: 20px;
  padding: 2px 6px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border-dark);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 4px;
}
.logo-text {
  font-family: var(--font-title);
  font-size: 7px;
  color: var(--gold);
  letter-spacing: 0.5px;
}
.logo-plus {
  color: var(--accent-red);
}
.rogue-tag {
  font-family: var(--font-pixel);
  font-size: 6px;
  background: var(--gold-dim);
  color: #fff;
  padding: 1px 3px;
  border-radius: 2px;
}

.rogue-stats {
  display: flex;
  align-items: center;
  gap: 6px;
}
.stat-pill {
  display: flex;
  align-items: center;
  gap: 3px;
  background: var(--bg-card);
  border: 1px solid var(--border-dark);
  padding: 1px 4px;
  font-size: 6px;
}
.stat-label {
  color: var(--text-muted);
}
.stat-val {
  color: var(--gold);
  font-weight: 700;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 3px;
}
.btn {
  font-family: var(--font-pixel);
  font-size: 6px;
  font-weight: 700;
  padding: 1px 5px;
  height: 16px;
  border: 1px solid var(--border-light);
  background: var(--bg-card);
  color: var(--text-main);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}
.btn:hover:not(:disabled) {
  border-color: var(--gold);
  background: #272f44;
}
.btn:active:not(:disabled) {
  transform: translateY(1px);
}
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.btn-gold {
  border-color: var(--border-gold);
  color: var(--gold);
}
.btn-cta-battle {
  background: #245830;
  border-color: var(--accent-green);
  color: #afffa4;
}
.btn-cta-battle:hover:not(:disabled) {
  background: #2d723d;
  color: #fff;
}

/* ===== MAIN GAME LAYOUT (220px) ===== */
.game-layout {
  height: 220px;
  min-height: 220px;
  max-height: 220px;
  display: flex;
  gap: 4px;
  padding: 2px 4px 0 4px;
  overflow: hidden;
}

/* Panel Box Common */
.card-box {
  background: var(--bg-panel);
  border: 1px solid var(--border-dark);
  padding: 4px;
}
.panel-title {
  font-family: var(--font-title);
  font-size: 6px;
  color: var(--gold);
  margin-bottom: 3px;
  padding-bottom: 2px;
  border-bottom: 1px solid var(--border-dark);
  letter-spacing: 0.5px;
}

/* ===== LEFT PANEL: TALISMANS (106px) ===== */
.left-panel {
  width: 106px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}
.talisman-panel {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.talisman-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: rgba(0, 0, 0, 0.25);
  border: 1px dashed var(--border-dark);
  padding: 4px;
  gap: 4px;
}
.talisman-title {
  font-size: 6px;
  font-weight: 700;
  color: var(--gold-light);
}
.talisman-desc {
  font-size: 5px;
  color: var(--text-muted);
  line-height: 1.3;
}
.talisman-badge {
  font-size: 5px;
  background: #262a3a;
  border: 1px solid var(--border-light);
  color: var(--text-muted);
  padding: 1px 4px;
}

/* ===== CENTER ARENA (262px) ===== */
.arena-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
}
.board-view {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Top Battle Controls Bar */
.board-top-controls {
  width: 100%;
  height: 18px;
  min-height: 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4px;
  margin-bottom: 2px;
  background: var(--bg-panel);
  border: 1px solid var(--border-dark);
}
.enemy-status-badge {
  display: flex;
  align-items: center;
}
.enemy-text {
  font-family: var(--font-title);
  font-size: 7px;
  color: var(--gold);
  letter-spacing: 0.5px;
}
.phase-buttons {
  display: flex;
  gap: 3px;
}

/* ===== CHESSBOARD CONTAINER ===== */
.board-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-frame {
  background: #181c28;
  border: 1px solid var(--border-gold);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1px;
}

.board-labels-top,
.board-labels-bottom {
  display: grid;
  grid-template-columns: 8px repeat(8, var(--tile-size)) 8px;
  font-size: 5px;
  color: var(--text-muted);
  text-align: center;
  line-height: 8px;
  height: 8px;
}

.board-with-ranks {
  display: flex;
  align-items: center;
}

.board-ranks {
  display: flex;
  flex-direction: column;
  width: 8px;
  height: calc(var(--tile-size) * 8);
  font-size: 5px;
  color: var(--text-muted);
  text-align: center;
}
.board-ranks span {
  height: var(--tile-size);
  line-height: var(--tile-size);
}

.board-container {
  width: calc(var(--tile-size) * 8);
  height: calc(var(--tile-size) * 8);
  position: relative;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, var(--tile-size));
  grid-template-rows: repeat(8, var(--tile-size));
  width: calc(var(--tile-size) * 8);
  height: calc(var(--tile-size) * 8);
  gap: 0;
  position: relative;
  z-index: 1;
}

.square {
  width: var(--tile-size);
  height: var(--tile-size);
  position: relative;
  cursor: default;
  overflow: visible;
  padding: 0;
  margin: 0;
}

/* 0. Floor tile */
.square .tile-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  pointer-events: none;
  z-index: 0;
  display: block;
}

/* 1. Yellow deploy zone starting outline: ON THE FLOOR */
.square.deploy-zone::before {
  content: '';
  position: absolute;
  inset: 1px;
  border: 1px dashed var(--gold);
  pointer-events: none;
  z-index: 1;
}

/* 2. Piece shadow on floor */
.square .piece-shadow {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  pointer-events: none;
  z-index: 2;
}

/* 3. Ray overlays: ON THE FLOOR under pieces */
.square .ray-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  opacity: 0;
  pointer-events: none;
  z-index: 3;
}
.square.show-ray .ray-overlay {
  opacity: 1;
}

/* 10. CHESS PIECES: TOP LAYER */
.square .piece {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: calc(var(--tile-size) * 2);
  image-rendering: pixelated;
  object-fit: fill;
  pointer-events: none;
  z-index: 10;
  transition: transform 0.1s ease;
}

.square .preview-piece {
  z-index: 12;
}

.square.piece-hover .piece {
  transform: translateY(-2px);
  z-index: 20;
}
.square.selected .piece {
  transform: translateY(-3px);
  z-index: 20;
}

/* Piece HUD (HP fraction 3/3, 2/3 - TOP LAYER ABOVE PIECE) */
.piece-hud {
  position: absolute;
  bottom: 1px;
  left: 0;
  right: 0;
  z-index: 30; /* Higher than piece (z-index 10 / 20) */
  display: flex;
  justify-content: center;
  pointer-events: none;
}
.hud-hp-num {
  font-family: var(--font-pixel);
  font-size: 6px;
  font-weight: 700;
  color: #fff;
  text-shadow: 1px 1px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
  white-space: nowrap;
}

/* Floating damage numbers (-1, -2 without heart) */
.dmg-popup {
  position: absolute;
  top: -4px;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--font-pixel);
  font-size: 8px;
  font-weight: 700;
  color: #ff3e3e;
  text-shadow: 1px 1px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
  pointer-events: none;
  z-index: 50;
  animation: dmgFloat 0.75s ease-out forwards;
}
@keyframes dmgFloat {
  0% { opacity: 1; transform: translate(-50%, 0) scale(1.2); }
  60% { opacity: 1; transform: translate(-50%, -10px) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -16px) scale(0.9); }
}

/* ==========================================================
   COMBAT & MOVEMENT ANIMATIONS (Retro Pixel Arcade)
   ========================================================== */

/* 1. Smooth slide move to empty square */
.square .piece.piece-advance-move {
  z-index: 45 !important;
  transition: none !important;
  animation: pieceAdvanceMove 0.22s cubic-bezier(0.25, 0.9, 0.35, 1) forwards;
}
@keyframes pieceAdvanceMove {
  0% {
    transform: translate(0, 0);
  }
  40% {
    transform: translate(calc(var(--move-dx, 0px) * 0.45), calc(var(--move-dy, 0px) * 0.45 - 3px));
  }
  100% {
    transform: translate(var(--move-dx, 0px), var(--move-dy, 0px));
  }
}

/* 2. Attack with recoil (Enemy survives: strike forward, bounce back home) */
.square .piece.piece-attacking,
.square .piece.piece-flying-attack-recoil {
  z-index: 45 !important;
  transition: none !important;
  animation: pieceAttackRecoil 0.24s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}
@keyframes pieceAttackRecoil {
  0% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(calc(var(--atk-dx, 0px) * 0.3), calc(var(--atk-dy, 0px) * 0.3 - 4px)) scale(1.1);
  }
  50% {
    /* Impact point: strikes forward */
    transform: translate(calc(var(--atk-dx, 0px) * 0.8), calc(var(--atk-dy, 0px) * 0.8 - 2px)) scale(1.15);
  }
  75% {
    transform: translate(calc(var(--atk-dx, 0px) * 0.3), calc(var(--atk-dy, 0px) * 0.3 - 1px)) scale(1.05);
  }
  100% {
    transform: translate(0, 0) scale(1);
  }
}

/* 3. Attack and capture (Enemy dies: charge forward, crush, occupy tile) */
.square .piece.piece-capturing,
.square .piece.piece-flying-attack-capture {
  z-index: 45 !important;
  transition: none !important;
  animation: pieceCaptureMove 0.24s cubic-bezier(0.25, 0.9, 0.35, 1) forwards;
}
@keyframes pieceCaptureMove {
  0% {
    transform: translate(0, 0) scale(1);
  }
  40% {
    transform: translate(calc(var(--atk-dx, 0px) * 0.45), calc(var(--atk-dy, 0px) * 0.45 - 5px)) scale(1.2);
  }
  85% {
    transform: translate(var(--atk-dx, 0px), calc(var(--atk-dy, 0px) - 2px)) scale(1.1);
  }
  100% {
    transform: translate(var(--atk-dx, 0px), var(--atk-dy, 0px)) scale(1);
  }
}

/* 4. Defender reaction animations */
.square .piece.piece-hit-flash {
  animation: pieceHitFlash 0.22s ease-out forwards;
}
@keyframes pieceHitFlash {
  0% { filter: brightness(3.5) contrast(1.4); transform: translate(1px, -1px); }
  25% { filter: brightness(2.5) contrast(1.2); transform: translate(-2px, 1px); }
  50% { filter: brightness(1.8); transform: translate(2px, 0); }
  75% { filter: brightness(1.3); transform: translate(-1px, 0); }
  100% { filter: none; transform: translate(0, 0); }
}

.square .piece.piece-defeat-fade {
  animation: pieceDefeatFade 0.22s ease-out forwards;
}
@keyframes pieceDefeatFade {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(0.7) translateY(-2px); }
  100% { opacity: 0; transform: scale(0.1) translateY(2px); }
}

/* Hide piece HUD during flight */
.piece-advance-move ~ .piece-hud,
.piece-capturing ~ .piece-hud {
  opacity: 0;
}

/* ===== RIGHT PANEL: RECRUIT & LOG (136px) ===== */
/* ===== RIGHT PANEL: RECRUIT (136px) ===== */
.right-panel {
  width: 136px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}

.recruit-panel {
  flex: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.recruit-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 3px;
}
.btn-reroll {
  font-size: 5px;
  height: 14px;
  padding: 1px 4px;
}
.recruit-subtitle {
  font-size: 5px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.recruit-cards-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

/* Horizontal recruit card */
.reward-card {
  height: 52px;
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--bg-card);
  border: 1px solid var(--border-dark);
  padding: 4px;
  cursor: pointer;
}
.reward-card:hover {
  border-color: var(--gold);
  background: #232a3d;
}

.reward-card-img-wrap {
  width: 18px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.reward-card-img {
  width: 18px;
  height: 36px;
  image-rendering: pixelated;
  object-fit: contain;
}

.reward-card-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.card-name {
  font-family: var(--font-title);
  font-size: 6px;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-rarity {
  font-size: 5px;
  color: var(--gold-light);
}
.card-stats {
  font-size: 5px;
  color: var(--text-muted);
  display: flex;
  gap: 4px;
}
.stat-badge {
  color: var(--text-main);
}

.btn-pick-card {
  font-family: var(--font-pixel);
  font-size: 6px;
  font-weight: 700;
  height: 20px;
  padding: 1px 6px;
  background: var(--gold-dim);
  border: 1px solid var(--border-gold);
  color: #fff;
  cursor: pointer;
}
.btn-pick-card:hover {
  background: var(--gold);
  color: #000;
}

.recruit-locked-msg,
.recruit-picked-msg {
  font-size: 5px;
  color: var(--text-muted);
  text-align: center;
  padding: 12px 4px;
}

/* ===== FOOTER: BENCH DOCK (46px) ===== */
.inventory-dock {
  height: 46px;
  min-height: 46px;
  max-height: 46px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border-dark);
  padding: 2px 6px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.inventory-header {
  height: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 5px;
}
.inv-title {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--gold);
}
.inv-hint {
  color: var(--text-muted);
}
.inv-count-badge {
  color: var(--text-muted);
}

.inventory-slots {
  display: flex;
  gap: 4px;
  align-items: center;
  justify-content: center;
  height: 30px;
}
.inv-slot {
  width: 26px;
  height: 28px;
  background: var(--bg-card);
  border: 1px solid var(--border-dark);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
}
.inv-slot:hover {
  border-color: var(--gold);
}
.inv-slot.selected {
  border-color: var(--accent-green);
  background: #1b2e22;
}
.inv-slot.deployed {
  opacity: 0.4;
}
.inv-slot-img-wrap {
  width: 14px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.inv-slot-piece {
  width: 12px;
  height: 24px;
  image-rendering: pixelated;
  object-fit: contain;
}
.inv-slot-badge {
  display: none;
}
.inv-slot-stats {
  font-size: 4px;
  color: var(--gold);
  line-height: 1;
}
.inv-slot-status {
  display: none;
}

/* ===== MODALS (PIXEL FRAMED) ===== */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-box {
  background: var(--bg-panel);
  border: 2px solid var(--border-gold);
  padding: 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 240px;
}
.modal-title {
  font-family: var(--font-title);
  font-size: 8px;
  color: var(--gold);
}
.modal-subtitle {
  font-size: 6px;
  color: var(--text-main);
  line-height: 1.3;
}
.reward-pill, .defeat-stats {
  font-size: 6px;
  color: var(--accent-green);
}
.defeat-stats {
  color: var(--accent-red);
}
.btn-large {
  font-size: 7px;
  height: 20px;
  padding: 2px 8px;
  background: var(--gold-dim);
  border: 1px solid var(--border-gold);
  color: #fff;
}
.btn-large:hover {
  background: var(--gold);
  color: #000;
}

/* Promotion Modal */
.promotion-choices {
  display: flex;
  gap: 4px;
}
.promo-btn {
  width: 24px;
  height: 36px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.promo-btn:hover {
  border-color: var(--gold);
}
.promo-btn img {
  width: 16px;
  height: 32px;
  image-rendering: pixelated;
  object-fit: contain;
}

/* ===== MOBILE PORTRAIT ROTATE OVERLAY ===== */
.rotate-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: rgba(7, 9, 15, 0.98);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 16px;
  cursor: pointer;
  touch-action: manipulation;
}

@media (orientation: portrait) and (max-width: 950px) {
  .rotate-overlay {
    display: flex;
  }
}

.rotate-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 320px;
}

.rotate-icon-wrap {
  width: 48px;
  height: 48px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.rotate-phone-icon {
  width: 22px;
  height: 36px;
  border: 3px solid var(--gold);
  border-radius: 4px;
  position: relative;
  animation: phoneRotate 2s ease-in-out infinite;
}
.rotate-phone-icon::after {
  content: '';
  position: absolute;
  bottom: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 4px;
  height: 4px;
  background: var(--gold);
  border-radius: 50%;
}

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

.rotate-title {
  font-family: var(--font-title);
  font-size: 10px;
  color: var(--gold);
  margin-bottom: 8px;
  line-height: 1.4;
  text-shadow: 1px 1px 0 #000;
}

.rotate-subtitle {
  font-family: var(--font-pixel);
  font-size: 7px;
  color: var(--text-muted);
  margin-bottom: 14px;
  line-height: 1.4;
}

.rotate-btn {
  font-size: 7px;
  padding: 6px 12px;
}
