:root {
  --bg: #07101c;
  --panel: #0f1f35;
  --panel-2: #152a45;
  --gold: #f5c542;
  --gold-deep: #c99212;
  --blue: #3aa0ff;
  --blue-deep: #1a5fb4;
  --red: #ff4d5a;
  --red-deep: #b51d2a;
  --elixir: #c35bff;
  --elixir-deep: #7b2cbf;
  --grass: #2d8a4e;
  --text: #f4f7fb;
  --muted: #8fa3bd;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --font-display: "Exo 2", system-ui, sans-serif;
  --font-ui: "Nunito", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

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

#app {
  height: 100%;
  width: 100%;
  position: relative;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ——— Title ——— */
#screen-title {
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-t)) 20px calc(24px + var(--safe-b));
  overflow: hidden;
}

.title-bg {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%, #1a3a6a 0%, transparent 60%),
    radial-gradient(ellipse 60% 40% at 20% 80%, #3a1a5a 0%, transparent 50%),
    linear-gradient(180deg, #0a1628 0%, #07101c 100%);
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
  opacity: 0.45;
  animation: float 8s ease-in-out infinite;
}

.orb-a { width: 220px; height: 220px; background: #1e6fd9; top: 12%; left: 10%; }
.orb-b { width: 180px; height: 180px; background: #9b3de8; bottom: 18%; right: 8%; animation-delay: -2s; }
.orb-c { width: 140px; height: 140px; background: #f5c542; top: 40%; left: 55%; opacity: 0.2; animation-delay: -4s; }

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-18px); }
}

.title-content {
  position: relative;
  z-index: 1;
  text-align: center;
  max-width: 420px;
  width: 100%;
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  letter-spacing: 0.16em;
  font-weight: 800;
  color: var(--gold);
  background: rgba(245, 197, 66, 0.12);
  border: 1px solid rgba(245, 197, 66, 0.35);
  padding: 6px 12px;
  border-radius: 999px;
  margin-bottom: 18px;
}

.logo {
  font-family: var(--font-display);
  line-height: 0.95;
  margin-bottom: 12px;
}

.logo-crown {
  display: block;
  font-size: clamp(2.8rem, 12vw, 4.2rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  background: linear-gradient(180deg, #ffe38a 0%, #f5c542 45%, #c99212 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 0 #6b4a00);
}

.logo-arena {
  display: block;
  font-size: clamp(2.2rem, 10vw, 3.4rem);
  font-weight: 900;
  letter-spacing: 0.18em;
  color: #e8f1ff;
  text-shadow: 0 3px 0 #1a3a6a, 0 8px 24px rgba(0,0,0,0.45);
}

.tagline {
  color: var(--muted);
  font-size: 0.95rem;
  margin-bottom: 28px;
}

.btn {
  font-family: var(--font-display);
  font-weight: 800;
  letter-spacing: 0.08em;
  border: none;
  cursor: pointer;
  border-radius: 14px;
  padding: 14px 28px;
  font-size: 1.05rem;
  transition: transform 0.12s ease, filter 0.12s ease, background 0.15s ease;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: linear-gradient(180deg, #5eb3ff 0%, #2a7de0 55%, #1a5fb4 100%);
  color: #fff;
  box-shadow:
    0 0 0 2px rgba(255,255,255,0.15) inset,
    0 6px 0 #0f3d7a,
    0 12px 28px rgba(42, 125, 224, 0.45);
  min-width: 200px;
}

.btn-primary:hover {
  filter: brightness(1.06);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  box-shadow: none;
  margin-top: 8px;
}

.btn-ghost:hover {
  color: var(--text);
}

.btn-pulse {
  animation: pulse 1.8s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 2px rgba(255,255,255,0.15) inset, 0 6px 0 #0f3d7a, 0 12px 28px rgba(42, 125, 224, 0.45); }
  50% { box-shadow: 0 0 0 2px rgba(255,255,255,0.2) inset, 0 6px 0 #0f3d7a, 0 12px 36px rgba(94, 179, 255, 0.7); }
}

.title-meta {
  display: flex;
  justify-content: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 22px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.title-meta span {
  background: rgba(255,255,255,0.06);
  padding: 6px 10px;
  border-radius: 8px;
}

.disclaimer {
  margin-top: 28px;
  font-size: 0.68rem;
  line-height: 1.4;
  color: #5d7190;
  max-width: 320px;
  margin-left: auto;
  margin-right: auto;
}

/* ——— Battle ——— */
#screen-battle {
  background: #050c16;
}

#battle-frame {
  flex: 1;
  display: flex;
  flex-direction: column;
  max-width: 480px;
  width: 100%;
  margin: 0 auto;
  min-height: 0;
  padding-top: var(--safe-t);
  padding-bottom: var(--safe-b);
}

#hud-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px 6px;
  background: linear-gradient(180deg, #12233a 0%, #0c1828 100%);
  border-bottom: 2px solid rgba(255,255,255,0.06);
  flex-shrink: 0;
}

.hud-player {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.hud-player.you {
  justify-content: flex-end;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 0.62rem;
  font-weight: 900;
  font-family: var(--font-display);
  flex-shrink: 0;
}

.enemy-av {
  background: linear-gradient(160deg, #ff7a84, #c62828);
  box-shadow: 0 0 0 2px rgba(255,100,100,0.4);
}

.you-av {
  background: linear-gradient(160deg, #5eb3ff, #1565c0);
  box-shadow: 0 0 0 2px rgba(80,160,255,0.4);
}

.hud-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}

.hud-info.right {
  align-items: flex-end;
}

.hud-info .name {
  font-size: 0.78rem;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.crowns {
  display: flex;
  gap: 3px;
}

.crowns i {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  background: #2a3a50;
  display: block;
  position: relative;
}

.crowns i.on {
  background: linear-gradient(180deg, #ffe38a, #f5c542);
  box-shadow: 0 0 8px rgba(245, 197, 66, 0.6);
}

.timer-wrap {
  text-align: center;
  flex-shrink: 0;
  min-width: 72px;
}

#match-timer {
  display: block;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 1.25rem;
  letter-spacing: 0.04em;
  color: #fff;
  font-variant-numeric: tabular-nums;
}

#timer-label {
  font-size: 0.58rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: var(--muted);
}

#timer-label.overtime {
  color: var(--red);
}

#arena-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #061018;
  overflow: hidden;
}

#arena {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 100%;
  object-fit: contain;
  touch-action: none;
  cursor: crosshair;
}

#deploy-hint {
  position: absolute;
  bottom: 12%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.72);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 8px 14px;
  border-radius: 999px;
  pointer-events: none;
  border: 1px solid rgba(255,255,255,0.12);
  white-space: nowrap;
}

#deploy-hint.hidden {
  display: none;
}

#hud-bottom {
  flex-shrink: 0;
  background: linear-gradient(180deg, #0e1c30 0%, #0a1422 100%);
  border-top: 2px solid rgba(255,255,255,0.06);
  padding: 6px 8px 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 6px 8px;
  align-items: end;
}

#elixir-bar {
  grid-column: 1 / -1;
  height: 18px;
  border-radius: 999px;
  background: #1a0f28;
  border: 2px solid #4a2a6a;
  position: relative;
  overflow: hidden;
  box-shadow: inset 0 2px 6px rgba(0,0,0,0.5);
}

#elixir-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 50%;
  background: linear-gradient(180deg, #e0a0ff 0%, #c35bff 40%, #7b2cbf 100%);
  box-shadow: 0 0 12px rgba(195, 91, 255, 0.55);
  transition: width 0.08s linear;
}

#elixir-ticks {
  position: absolute;
  inset: 0;
  display: flex;
  pointer-events: none;
}

#elixir-ticks span {
  flex: 1;
  border-right: 1px solid rgba(0,0,0,0.28);
}

#elixir-ticks span:last-child {
  border-right: none;
}

#elixir-count {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 0.85rem;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0,0,0,0.8);
  z-index: 2;
}

#hand {
  display: flex;
  gap: 6px;
  justify-content: center;
  min-width: 0;
  flex: 1;
}

.card {
  flex: 1;
  max-width: 78px;
  aspect-ratio: 0.72;
  border-radius: 10px;
  position: relative;
  cursor: pointer;
  border: 2px solid #3a4a60;
  background: #1a2838;
  overflow: hidden;
  transition: transform 0.12s ease, border-color 0.12s ease, filter 0.12s ease;
  box-shadow: 0 4px 0 rgba(0,0,0,0.35);
}

.card:active {
  transform: scale(0.96);
}

.card.selected {
  border-color: var(--gold);
  transform: translateY(-8px) scale(1.04);
  box-shadow: 0 10px 20px rgba(245, 197, 66, 0.35), 0 4px 0 rgba(0,0,0,0.35);
  z-index: 2;
}

.card.disabled {
  filter: grayscale(0.55) brightness(0.72);
  cursor: not-allowed;
}

.card-art {
  position: absolute;
  inset: 0 0 22% 0;
  display: grid;
  place-items: center;
}

.card-art canvas {
  width: 78%;
  height: 78%;
}

.card-name {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 18px;
  text-align: center;
  font-size: 0.52rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: #e8f0ff;
  text-shadow: 0 1px 2px #000;
  padding: 0 2px;
  line-height: 1.1;
}

.card-cost {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #e0a0ff, #9b3de8 55%, #5a1a8a);
  border: 1.5px solid #f0c8ff;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 0.72rem;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.45);
  z-index: 2;
}

.card-rarity {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 5px;
}

.rarity-common { background: #8fa3bd; }
.rarity-rare { background: #3aa0ff; }
.rarity-epic { background: #c35bff; }
.rarity-legendary { background: linear-gradient(90deg, #f5c542, #ff8a3d); }

#next-card {
  width: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

#next-card-inner {
  width: 44px;
  aspect-ratio: 0.72;
  border-radius: 8px;
  border: 2px dashed #3a4a60;
  background: #121c2a;
  overflow: hidden;
  position: relative;
  opacity: 0.75;
}

#next-card-inner canvas {
  width: 100%;
  height: 100%;
}

#next-card > span {
  font-size: 0.55rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--muted);
}

/* Overlay */
#overlay {
  position: absolute;
  inset: 0;
  background: rgba(4, 10, 18, 0.78);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 20px;
}

#overlay.hidden {
  display: none;
}

.overlay-card {
  background: linear-gradient(180deg, #18304f 0%, #0f1f35 100%);
  border: 2px solid rgba(255,255,255,0.1);
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  width: min(340px, 100%);
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
}

#result-label {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 2rem;
  letter-spacing: 0.08em;
  margin-bottom: 12px;
}

#result-label.win {
  background: linear-gradient(180deg, #ffe38a, #f5c542);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

#result-label.lose {
  color: #ff7a84;
}

#result-label.draw {
  color: #8fa3bd;
}

.result-crowns {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 12px;
  min-height: 28px;
}

.result-crowns span {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: linear-gradient(180deg, #ffe38a, #c99212);
  box-shadow: 0 0 12px rgba(245, 197, 66, 0.5);
}

#result-sub {
  color: var(--muted);
  font-size: 0.9rem;
  margin-bottom: 20px;
}

@media (min-width: 600px) {
  #battle-frame {
    border-left: 1px solid rgba(255,255,255,0.04);
    border-right: 1px solid rgba(255,255,255,0.04);
    box-shadow: 0 0 60px rgba(0,0,0,0.4);
  }

  .card {
    max-width: 86px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn-pulse, .orb {
    animation: none;
  }
}
