@font-face {
  font-family: 'PixelFallback';
  src: local('Courier New');
}

:root {
  --bg: #14161c;
  --panel: #1f2330;
  --panel-border: #3b3f52;
  --text: #e8e6df;
  --muted: #9a9db0;
  --gold: #f2c14e;
  --danger: #e0555f;
  --success: #6fbf73;
  --accent: #3b6ea5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', Courier, monospace;
  image-rendering: pixelated;
  letter-spacing: 0.5px;
}

#app {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px;
}

.topbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  border: 4px solid var(--panel-border);
  background: var(--panel);
  padding: 12px 16px;
  margin-bottom: 16px;
  image-rendering: pixelated;
}

.topbar h1 {
  margin: 0;
  font-size: 22px;
  text-shadow: 2px 2px 0 #000;
  color: var(--gold);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.account-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.account-email {
  font-size: 11px;
  color: var(--muted);
}

.logout-btn {
  background: #2a2e3d;
  border-color: var(--panel-border);
  font-size: 11px;
  padding: 6px 10px;
}

.leaderboard-btn {
  background: #2a2e3d;
  border-color: var(--panel-border);
  font-size: 11px;
  padding: 6px 10px;
}

.restart-btn {
  background: var(--danger);
  border-color: #5a1a1a;
  font-size: 11px;
  padding: 6px 10px;
}

.restart-btn.armed {
  background: #ff7a33;
  border-color: #7a3a10;
  animation: restart-pulse 0.6s ease-in-out infinite alternate;
}

@keyframes restart-pulse {
  from { filter: brightness(1); }
  to { filter: brightness(1.35); }
}

.stats {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  font-size: 14px;
}

.stat b { color: var(--gold); }

main {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel {
  border: 4px solid var(--panel-border);
  background: var(--panel);
  padding: 12px 16px;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.panel-header h2 {
  margin: 0;
  font-size: 16px;
  color: var(--text);
}

button {
  font-family: inherit;
  background: var(--accent);
  color: #fff;
  border: 3px solid #223b52;
  padding: 8px 10px;
  cursor: pointer;
  font-size: 12px;
  box-shadow: 3px 3px 0 #000;
  transition: transform 0.05s ease;
}

button:hover:not(:disabled) { filter: brightness(1.15); }
button:active:not(:disabled) { transform: translate(2px, 2px); box-shadow: 1px 1px 0 #000; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.roster {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 12px;
}

.unit-card {
  border: 3px solid var(--panel-border);
  background: #171a24;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  text-align: center;
}

.unit-card.empty { opacity: 0.6; }

.unit-sprite {
  width: 56px;
  height: 56px;
  image-rendering: pixelated;
}

.unit-name { font-size: 13px; color: var(--gold); }
.unit-count { font-size: 11px; color: var(--muted); }

.unit-stat-row {
  width: 100%;
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
}

.hp-bar-track {
  width: 100%;
  height: 8px;
  background: #000;
  border: 1px solid var(--panel-border);
}

.hp-bar-fill {
  height: 100%;
  background: var(--success);
}

.unit-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  width: 100%;
}

.unit-actions button { font-size: 10px; padding: 6px 4px; }
.unit-actions button[data-action="sell"] { background: var(--danger); border-color: #5a1a1a; }

.battle-panel #battleBtn { font-size: 14px; padding: 10px 14px; background: var(--danger); border-color: #5a1a1a; }

.battle-result {
  padding: 8px 10px;
  margin-bottom: 10px;
  border: 2px solid var(--panel-border);
  font-size: 13px;
}

.battle-result.victory { border-color: var(--success); color: var(--success); }
.battle-result.defeat { border-color: var(--danger); color: var(--danger); }
.hidden { display: none; }

/* Sized fluidly (percentages, not fixed px) so the field and every token it holds always stay
   fully inside the panel's frame, on any screen width — nothing scrolls or clips off-screen.
   Units move through real continuous 2D space here, not discrete grid cells, so there's no
   cell-line pattern to draw — just open ground. */
.battle-grid {
  position: relative;
  width: 100%;
  max-width: 480px;
  aspect-ratio: 9 / 7;
  margin: 0 auto 12px;
  background: #10121a;
  border: 2px solid var(--panel-border);
  overflow: hidden;
}

.battle-token {
  position: absolute;
  /* (left, top) is the unit's real world position; anchor its feet there and scale the whole
     token around that same point — --token-scale shrinks the roster once it gets crowded. */
  transform: translate(-50%, -100%) scale(var(--token-scale, 1));
  transform-origin: center bottom;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: left 0.35s ease, top 0.35s ease, opacity 0.3s ease;
}

.token-sprite {
  transform-origin: bottom center;
}

.battle-token img {
  width: 32px;
  height: auto;
  image-rendering: pixelated;
  display: block;
}

.battle-token.team-enemy img { transform: scaleX(-1); }

.battle-token.dead {
  opacity: 0.18;
  filter: grayscale(1);
}

/* A little side-to-side waddle, one pulse per step (not an infinite loop that gets cut off
   mid-swing). Each pulse always plays to completion and ends back at rotate(0) on its own — a
   unit that stops moving just stops getting new pulses queued, instead of being yanked out of
   one, so it never looks like the walk animation is still acting on a unit that's stopped. */
.token-sprite.waddle-step {
  animation: token-waddle 0.28s ease-in-out 1;
}

@keyframes token-waddle {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(-9deg); }
  75% { transform: rotate(9deg); }
}

.token-hp-track {
  width: 24px;
  height: 4px;
  background: #000;
  margin-top: 2px;
}

.token-hp-fill {
  height: 100%;
  background: var(--success);
}

.team-enemy .token-hp-fill { background: var(--danger); }

/* Attack effects: brief pixel-style VFX spawned over the grid, positioned as % of the field
   so they land correctly regardless of the field's current fluid size. */
.fx {
  position: absolute;
  pointer-events: none;
  z-index: 25;
}

.fx-slash {
  width: 22px;
  height: 22px;
  margin-left: -11px;
  margin-top: -11px;
  animation: fx-slash-pop 0.28s ease-out forwards;
}

.fx-slash::before,
.fx-slash::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 4px;
  margin-top: -2px;
  margin-left: -11px;
  background: #f4f1e6;
  box-shadow: 0 0 0 1px #000;
}

.fx-slash::before { transform: rotate(45deg); }
.fx-slash::after { transform: rotate(-45deg); }

@keyframes fx-slash-pop {
  0% { opacity: 0; transform: scale(0.4); }
  35% { opacity: 1; transform: scale(1.15); }
  100% { opacity: 0; transform: scale(1.35); }
}

.fx-claw {
  width: 24px;
  height: 24px;
  margin-left: -12px;
  margin-top: -12px;
  animation: fx-slash-pop 0.3s ease-out forwards;
}

.fx-claw span {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 3px;
  margin-top: -1.5px;
  margin-left: -11px;
  background: var(--danger);
  box-shadow: 0 0 0 1px #2a0808;
}

.fx-claw span:nth-child(1) { transform: rotate(-40deg) translateY(-6px); }
.fx-claw span:nth-child(2) { transform: rotate(-40deg); }
.fx-claw span:nth-child(3) { transform: rotate(-40deg) translateY(6px); }

.fx-bolt {
  width: 14px;
  height: 34px;
  margin-left: -7px;
  margin-top: -30px;
}

.fx-bolt .bolt-shape {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, #fff6b0, #8ec9ff);
  clip-path: polygon(45% 0%, 100% 0%, 55% 40%, 90% 40%, 20% 100%, 40% 55%, 0% 55%);
  box-shadow: 0 0 6px 2px rgba(255, 255, 150, 0.6);
  animation: fx-bolt-flash 0.22s steps(2) forwards;
}

.fx-bolt .bolt-burst {
  position: absolute;
  left: 50%;
  bottom: -4px;
  width: 18px;
  height: 8px;
  margin-left: -9px;
  background: radial-gradient(circle, rgba(255, 255, 180, 0.9), rgba(255, 255, 180, 0) 70%);
  animation: fx-burst-flash 0.3s ease-out forwards;
}

@keyframes fx-bolt-flash {
  0% { opacity: 0; }
  20% { opacity: 1; }
  70% { opacity: 1; }
  100% { opacity: 0; }
}

@keyframes fx-burst-flash {
  0% { opacity: 0; transform: scale(0.3); }
  30% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.4); }
}

.fx-arrow {
  width: 16px;
  height: 5px;
  margin-left: -8px;
  margin-top: -2.5px;
  animation: fx-arrow-fly 0.3s linear forwards;
}

.fx-arrow .shaft {
  position: absolute;
  left: 0;
  top: 1px;
  width: 12px;
  height: 3px;
  background: #d8b978;
  box-shadow: 0 0 0 1px #4a3418;
}

.fx-arrow .head {
  position: absolute;
  left: 11px;
  top: 0;
  width: 0;
  height: 0;
  border-top: 2.5px solid transparent;
  border-bottom: 2.5px solid transparent;
  border-left: 5px solid #d8b978;
}

@keyframes fx-arrow-fly {
  from { left: var(--sx); top: var(--sy); opacity: 1; }
  to { left: var(--tx); top: var(--ty); opacity: 1; }
}

.modal-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0);
  transition: background 0.22s ease;
  z-index: 100;
}

.modal-overlay.hidden { display: none; }

.modal-overlay.open {
  background: rgba(0, 0, 0, 0.65);
}

.leaderboard-modal {
  position: relative;
  width: 360px;
  max-width: 90vw;
  height: 440px;
  max-height: 85vh;
  padding: 0;
  overflow: hidden;
}

.leaderboard-modal .panel-header {
  margin-bottom: 0;
  padding: 14px 16px 12px;
  border-bottom: 2px solid var(--panel-border);
}

.leaderboard-list {
  list-style: none;
  margin: 0;
  padding: 6px 16px 16px;
  height: calc(100% - 47px);
  overflow-y: auto;
}

.leaderboard-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 2px;
  font-size: 13px;
  border-bottom: 1px solid #2a2e3d;
}

.leaderboard-list li .rank {
  color: var(--gold);
  width: 26px;
  flex-shrink: 0;
}

.leaderboard-list li .name {
  flex: 1;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-list li .lvl { color: var(--muted); flex-shrink: 0; }

.leaderboard-empty {
  list-style: none;
  color: var(--muted);
  font-size: 12px;
  padding: 12px 2px;
}

.recovery-modal {
  width: 340px;
  max-width: 90vw;
}

.recovery-modal .panel-header { margin-bottom: 10px; }

.recovery-modal-desc {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  margin: 0 0 14px;
}

.recovery-code {
  background: #10121a;
  border: 2px solid var(--gold);
  color: var(--gold);
  font-size: 18px;
  letter-spacing: 2px;
  text-align: center;
  padding: 12px 8px;
  margin-bottom: 14px;
  word-break: break-all;
}

.pixel-dissolve {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-template-rows: repeat(16, 1fr);
  pointer-events: none;
  z-index: 5;
}

.pixel-dissolve div {
  background: var(--panel);
  opacity: 1;
  transition: opacity 0.2s linear;
  transition-delay: var(--d, 0ms);
}

.pixel-dissolve.no-transition div { transition: none !important; }

.toast {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  padding: 10px 16px;
  border: 3px solid #5a1a1a;
  font-size: 12px;
  box-shadow: 3px 3px 0 #000;
}

/* Phone layout: compact top bar, battle field right under it, roster docked
   as a fixed bottom panel so hiring/upgrading is always reachable. */
@media (max-width: 720px) {
  #app { padding: 10px; }

  .topbar { padding: 10px 12px; }
  .topbar h1 { font-size: 18px; }

  .topbar-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .account-row { flex-wrap: nowrap; gap: 6px; }

  .account-email {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 10px;
  }

  .account-row button {
    flex: 0 0 auto;
    font-size: 10px;
    padding: 6px 8px;
    white-space: nowrap;
  }

  .stats { font-size: 11px; gap: 10px; }

  main { padding-bottom: 254px; }

  .roster-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    z-index: 40;
    border-width: 3px 0 0 0;
    max-height: 48vh;
    overflow-y: auto;
    box-shadow: 0 -4px 0 #000;
  }

  .roster-panel .panel-header { margin-bottom: 8px; }

  .roster-panel .roster {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 6px;
  }

  .roster-panel .unit-card {
    flex: 0 0 148px;
    padding: 8px;
  }

  .roster-panel .unit-sprite { width: 44px; height: 44px; }
  .roster-panel .unit-actions button { font-size: 9px; padding: 6px 3px; }

  .toast { bottom: 262px; z-index: 55; }
}
