/* ============================================================
   RESET & BASE
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --pixel: 'Press Start 2P', monospace;
  --bg: #0a0010;
  --bg2: #110020;
  --gold: #f0c040;
  --red: #e03030;
  --green: #40c060;
  --purple: #9040c0;
  --gray: #888;
  --white: #f0f0f0;
  --border: #3a2060;
  --overlay-bg: rgba(5, 0, 18, 0.92);
  --tier0: #a0d8a0;
  --tier1: #60c8e0;
  --tier2: #e0c030;
  --tier3: #e08020;
  --tier4: #d060e0;
  --tier5: #ffe040;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--white);
  font-family: var(--pixel);
  font-size: 10px;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

.hidden { display: none !important; }

/* ============================================================
   SCHERMATA DI CARICAMENTO
   ============================================================ */
#loading-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  z-index: 100;
}
#loading-content { text-align: center; padding: 20px; }
#loading-logo {
  max-width: 280px;
  width: 80vw;
  image-rendering: pixelated;
  margin-bottom: 16px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}
#loading-sub { font-size: 8px; color: var(--gray); margin-bottom: 28px; }
#progress-bar-wrap {
  width: 260px; height: 14px;
  background: #1a0830;
  border: 2px solid var(--border);
  margin: 0 auto 12px;
  image-rendering: pixelated;
}
#progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--purple), var(--gold));
  width: 0%;
  transition: width 0.3s;
}
#loading-msg { font-size: 7px; color: var(--gray); }

/* ============================================================
   SCHERMATA TITOLO
   ============================================================ */
#title-screen {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  background-image: url('gfx/battlefield.png');
  background-size: cover;
  background-position: center bottom;
  z-index: 90;
}
#title-screen::before {
  content: '';
  position: absolute; inset: 0;
  background: rgba(5, 0, 18, 0.78);
}
#title-content {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px;
  text-align: center;
  width: min(360px, 92vw);
  max-height: 100vh;
  overflow-y: auto;
}
#title-logo {
  width: 100%;
  max-width: 300px;
  max-height: 30vh;
  object-fit: contain;
  image-rendering: pixelated;
  display: block;
  margin-bottom: 20px;
  filter: drop-shadow(0 0 18px rgba(144, 64, 192, 0.7));
}
#title-sub {
  font-size: 8px;
  color: var(--gray);
  margin-bottom: 32px;
  letter-spacing: 1px;
}
.title-btn {
  width: 100%;
  margin-top: 12px !important;
  font-size: 9px !important;
  letter-spacing: 1px;
}

/* ============================================================
   LAYOUT PRINCIPALE
   ============================================================ */
#game-screen {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
}

/* ============================================================
   BARRA SUPERIORE
   ============================================================ */
#top-bar {
  flex-shrink: 0;
  background: var(--bg2);
  border-bottom: 2px solid var(--border);
  padding: 4px 8px 2px;
}
#top-title {
  font-size: 11px;
  color: var(--gold);
  text-shadow: 1px 1px 0 #000;
  text-align: center;
  letter-spacing: 1px;
  margin-bottom: 4px;
}
#top-stats {
  display: flex;
  gap: 6px;
  justify-content: space-between;
  flex-wrap: wrap;
}
.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 60px;
}
.stat-label { font-size: 6px; color: var(--gray); }
.stat-val { font-size: 9px; color: var(--gold); }
#top-hp-bar-wrap {
  height: 6px;
  background: #1a0830;
  border: 1px solid var(--border);
  margin: 4px 0 2px;
}
#top-hp-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--red), #f06040);
  width: 100%;
  transition: width 0.3s;
}
#top-buttons {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  margin-top: 2px;
}
.icon-btn {
  background: var(--bg);
  border: 2px solid var(--border);
  color: var(--white);
  font-size: 14px;
  width: 36px; height: 36px;
  cursor: pointer;
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  touch-action: manipulation;
}
.icon-btn:active { background: var(--border); }
.icon-btn.danger { border-color: var(--red); color: var(--red); }

/* ============================================================
   CAMPO DI BATTAGLIA
   ============================================================ */
#battlefield {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: url('gfx/battlefield.png') center bottom / cover no-repeat;
  touch-action: none;
}

#castle-img {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  height: clamp(140px, 32vh, 280px);
  image-rendering: pixelated;
  pointer-events: none;
}

/* ============================================================
   ELITE ENEMY GLOW
   ============================================================ */
.enemy.elite .unit-sprite {
  filter: drop-shadow(0 0 5px #ffd700) drop-shadow(0 0 10px #ffa500);
  animation: elite-pulse 1.6s ease-in-out infinite;
}
@keyframes elite-pulse {
  0%, 100% { filter: drop-shadow(0 0 4px #ffd700) drop-shadow(0 0 8px #ffa500); }
  50%       { filter: drop-shadow(0 0 10px #ffe84d) drop-shadow(0 0 20px #ff8800); }
}
.enemy.elite .unit-name-label {
  color: #ffd700;
  text-shadow: 0 0 4px #ff8800;
}

/* ============================================================
   SPRITE NEMICI (CSS pixel art)
   ============================================================ */
.enemy {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  will-change: transform;
  touch-action: manipulation;
}
.unit-name-label {
  font-size: 8px;
  color: #fff;
  text-shadow: 1px 1px 0 #000, -1px -1px 0 #000;
  white-space: nowrap;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
  pointer-events: none;
}
.hp-bar-wrap {
  width: clamp(36px, 8vmin, 56px); height: 5px;
  background: #300;
  border: 1px solid #000;
  margin-top: 2px;
  pointer-events: none;
  display: none;
}
.hp-bar-wrap.damaged { display: block; }
.hp-bar-fill {
  height: 100%;
  background: var(--green);
  transition: width 0.1s, background 0.3s;
}
.hp-bar-fill.low { background: var(--red); }
.hp-bar-fill.mid { background: #e0c000; }


/* Corpo nemico generico */
.enemy-body {
  width: 28px;
  height: 36px;
  position: relative;
  image-rendering: pixelated;
  pointer-events: none;
}
/* Testa */
.enemy-body::before {
  content: '';
  position: absolute;
  top: 0; left: 4px;
  width: 20px; height: 18px;
  border-radius: 2px;
}
/* Corpo */
.enemy-body::after {
  content: '';
  position: absolute;
  bottom: 0; left: 2px;
  width: 24px; height: 20px;
  border-radius: 1px;
}

/* Orchi – verde scuro, tozzo */
.enemy-orchi .enemy-body::before { background: #3d7a4a; box-shadow: -3px 4px 0 #2a5c34, 3px 4px 0 #2a5c34; }
.enemy-orchi .enemy-body::after  { background: #4a6e3a; }
.enemy-orchi .enemy-body { filter: drop-shadow(0 2px 2px #000a); }

/* Mannari – bruno, più alto */
.enemy-mannari .enemy-body { width: 26px; height: 40px; }
.enemy-mannari .enemy-body::before { background: #8b6020; top: 0; left: 3px; width: 20px; height: 16px; border-radius: 50% 50% 2px 2px; box-shadow: -5px 6px 0 #6b4010, 5px 6px 0 #6b4010; }
.enemy-mannari .enemy-body::after  { background: #704e18; bottom: 0; left: 1px; width: 24px; height: 26px; }

/* Vampiri – viola, slanciato con mantello */
.enemy-vampiri .enemy-body { width: 24px; height: 44px; }
.enemy-vampiri .enemy-body::before { background: #c0a0c0; top: 0; left: 2px; width: 20px; height: 16px; border-radius: 4px; box-shadow: -4px 8px 0 #300030, 4px 8px 0 #300030; }
.enemy-vampiri .enemy-body::after  { background: #4a0860; bottom: 0; left: -2px; width: 28px; height: 30px; border-radius: 0 0 4px 4px; }

/* Drow – grigio scuro, slanciato */
.enemy-drow .enemy-body { width: 22px; height: 40px; }
.enemy-drow .enemy-body::before { background: #888; top: 0; left: 3px; width: 16px; height: 14px; border-radius: 3px; box-shadow: -4px 5px 0 #444, 4px 5px 0 #444; }
.enemy-drow .enemy-body::after  { background: #555; bottom: 0; left: 1px; width: 20px; height: 28px; }

/* Demoni – rosso, grande, corni */
.enemy-demoni .enemy-body { width: 34px; height: 48px; }
.enemy-demoni .enemy-body::before { background: #c03020; top: 0; left: 5px; width: 24px; height: 20px; border-radius: 2px; box-shadow: -6px -4px 0 #901010, 6px -4px 0 #901010, -3px 8px 0 #000, 3px 8px 0 #000; }
.enemy-demoni .enemy-body::after  { background: #a02010; bottom: 0; left: 1px; width: 32px; height: 30px; }

/* ============================================================
   SPRITE UNITÀ GIOCATORE
   ============================================================ */
.unit {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  will-change: transform;
  pointer-events: none;
}
.unit-sprite {
  height: clamp(80px, 16vmin, 140px);
  image-rendering: pixelated;
  pointer-events: none;
}

/* ============================================================
   PARTICELLE E PROIETTILI
   ============================================================ */
.particle {
  position: absolute;
  pointer-events: none;
  font-size: 8px;
  font-family: var(--pixel);
  text-shadow: 1px 1px 0 #000;
  will-change: transform, opacity;
  white-space: nowrap;
}
.particle-damage { color: #ff6040; }
.particle-gold   { color: var(--gold); }
.particle-miss   { color: var(--gray); }

.fight-cloud {
  position: absolute;
  width: 120px; height: 120px;
  pointer-events: none;
  background: url('gfx/slash.gif') center / contain no-repeat;
  will-change: transform, opacity;
}

.projectile {
  position: absolute;
  width: 8px; height: 8px;
  border-radius: 50%;
  pointer-events: none;
  will-change: transform;
}
.proj-lord    { background: #d060e0; box-shadow: 0 0 6px #d060e0; }
.proj-signore { background: var(--gold); box-shadow: 0 0 8px var(--gold); }
.proj-angel   { background: #60d0ff; box-shadow: 0 0 8px #60d0ff; }
.unit.angel .unit-sprite { filter: drop-shadow(0 0 5px #60d0ff) drop-shadow(0 0 10px #0090cc); }

/* ============================================================
   AREA INFERIORE
   ============================================================ */
#bottom-area {
  flex-shrink: 0;
  flex-grow: 0;
  background: var(--bg2);
  border-top: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 30vh;
  min-height: 160px;
  max-height: 220px;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

/* LEADERBOARD */
#leaderboard {
  padding: 6px 8px 4px;
  border-bottom: 1px solid var(--border);
}
.lb-title {
  font-size: 7px;
  color: var(--gray);
  margin-bottom: 4px;
  text-align: center;
}
.lb-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 7px;
  margin-bottom: 3px;
}
.lb-rank { color: var(--gold); width: 12px; }
.lb-name { color: var(--white); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-tier { font-size: 6px; }
.lb-dmg  { color: var(--red); white-space: nowrap; }

/* BOTTONE RIPARAZIONE */
#repair-area { padding: 4px 8px 2px; }
.repair-btn {
  width: 100%;
  background: #0a1f10;
  border: 2px solid var(--green);
  color: var(--green);
  font-family: var(--pixel);
  font-size: 7px;
  padding: 6px 8px;
  cursor: pointer;
  border-radius: 4px;
  text-align: center;
  touch-action: manipulation;
  transition: background 0.1s, border-color 0.1s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.repair-btn:active:not(.disabled) { background: #142d1a; }
.repair-btn.disabled { opacity: 0.4; border-color: #1a3020; cursor: not-allowed; }

/* BOTTONI RECLUTA */
#recruit-area { padding: 4px 8px; }
#recruit-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.recruit-btn {
  background: #1a0830;
  border: 2px solid var(--border);
  color: var(--white);
  font-family: var(--pixel);
  font-size: 6px;
  padding: 6px 4px;
  cursor: pointer;
  border-radius: 4px;
  text-align: left;
  line-height: 1.6;
  touch-action: manipulation;
  transition: border-color 0.1s, background 0.1s;
  min-height: 54px;
}
.recruit-btn .btn-name { font-size: 8px; display: block; margin-bottom: 2px; }
.recruit-btn .btn-cost { color: var(--gold); }
.recruit-btn .btn-count { color: var(--green); }
.recruit-btn .btn-power { color: #a080d0; }
.recruit-btn:active:not(.disabled) { background: #2a1050; }
.recruit-btn.disabled { opacity: 0.4; border-color: #2a1840; cursor: not-allowed; }
.recruit-btn.flash { border-color: var(--gold); background: #2a1840; }

.gold-msg {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.85);
  border: 2px solid var(--red);
  color: var(--red);
  font-family: var(--pixel);
  font-size: 8px;
  padding: 8px 16px;
  border-radius: 4px;
  z-index: 50;
  pointer-events: none;
  animation: fadeout 1.2s forwards;
}
@keyframes fadeout {
  0%   { opacity: 1; }
  60%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ============================================================
   OVERLAYS (pausa, game over, conferma)
   ============================================================ */
.overlay {
  position: fixed; inset: 0;
  background: var(--overlay-bg);
  display: flex; align-items: center; justify-content: center;
  z-index: 80;
}
.overlay-box {
  background: var(--bg2);
  border: 3px solid var(--border);
  padding: 24px 20px;
  text-align: center;
  max-width: 320px;
  width: 90%;
  border-radius: 4px;
}
.overlay-title {
  font-size: 14px;
  color: var(--gold);
  margin-bottom: 16px;
  text-shadow: 2px 2px 0 #000;
}
.overlay-title.red-text { color: var(--red); }

/* Cycle complete overlay */
.cycle-sub {
  font-size: 7px;
  color: var(--gray);
  margin-bottom: 8px;
  text-align: center;
}
.cycle-no-fallen {
  font-size: 7px;
  color: var(--gray);
  text-align: center;
  padding: 8px 0;
}
.cycle-hero-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
  font-size: 7px;
}
.cycle-hero-rank  { color: var(--gray); width: 12px; flex-shrink: 0; }
.cycle-hero-name  { color: var(--white); flex: 1; }
.cycle-hero-tier  { color: var(--purple); }
.cycle-hero-dmg   { color: var(--red); }
#cycle-fallen-list { max-height: 160px; overflow-y: auto; }
.go-fallen-title { font-size: 7px; color: var(--gold); margin: 10px 0 4px; }
.go-fallen-list  { max-height: 240px; overflow-y: auto; margin-top: 4px; }
.big-btn {
  display: block;
  width: 100%;
  background: #1a0830;
  border: 2px solid var(--purple);
  color: var(--white);
  font-family: var(--pixel);
  font-size: 9px;
  padding: 12px 8px;
  margin-top: 12px;
  cursor: pointer;
  border-radius: 4px;
  touch-action: manipulation;
}
.big-btn:active { background: #2a1050; }
.big-btn.danger { border-color: var(--red); color: var(--red); }
.small-text { font-size: 7px; color: var(--gray); margin-bottom: 8px; min-height: 16px; }
.stats-block { font-size: 8px; line-height: 2; color: var(--white); margin-bottom: 8px; }
.confirm-text { font-size: 8px; line-height: 2; margin-bottom: 8px; }
.confirm-btns { display: flex; flex-direction: column; gap: 6px; }

/* INFO OVERLAY */
.info-box {
  max-width: 360px;
  max-height: 80vh;
  overflow-y: auto;
  text-align: left;
}
.info-body {
  font-size: 7px;
  line-height: 1.9;
  color: var(--white);
  margin-bottom: 12px;
}
.info-body p { margin-bottom: 6px; }
.info-section {
  color: var(--gold);
  font-size: 7px;
  margin-top: 10px !important;
  margin-bottom: 2px !important;
}
.info-tier { font-size: 7px; }
.info-tier.t0 { color: var(--tier0); }
.info-tier.t1 { color: var(--tier1); }
.info-tier.t2 { color: var(--tier2); }
.info-tier.t3 { color: var(--tier3); }
.info-tier.t4 { color: var(--tier4); }
.info-tier.t5 { color: var(--tier5); }

/* ============================================================
   ANIMAZIONI
   ============================================================ */
@keyframes hit-flash {
  0%   { filter: brightness(3); }
  100% { filter: brightness(1); }
}
.hit-flash { animation: hit-flash 0.15s ease-out; }

@keyframes spawn-pop {
  0%   { transform: scale(0); opacity: 0; }
  70%  { transform: scale(1.2); opacity: 1; }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%,100% { transform: translateX(0); }
  20%     { transform: translateX(-5px); }
  40%     { transform: translateX(5px); }
  60%     { transform: translateX(-3px); }
  80%     { transform: translateX(3px); }
}
.spawn-pop { animation: spawn-pop 0.3s ease-out; }
