:root {
  --bg-outer: #0b0f19;
  --bg-phone: #141a29;
  --panel: #1c2538;
  --panel-border: #2d3a56;
  --cell-bg: #121826;
  --cell-border: #2a354d;
  --gold: #fbbf24;
  --hp: #22c55e;
  --enemy-hp: #ef4444;
  --accent: #38bdf8;
  --text: #f1f5f9;
  --muted: #94a3b8;
}

* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  background: radial-gradient(circle at top, #1b243b 0%, var(--bg-outer) 75%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#phone-frame {
  width: min(100vw, 430px);
  height: min(100vh, 880px);
  height: min(100dvh, 880px);
  background: var(--bg-phone);
  border: 1px solid #2b3650;
  border-radius: 22px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

@media (max-width: 460px) {
  #phone-frame {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
    border: none;
  }
}

/* Top HUD */
.top-hud {
  flex-shrink: 0;
  position: relative;
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 7px;
  background: linear-gradient(180deg, #1d2740 0%, #151c2e 100%);
  border-bottom: 1px solid var(--panel-border);
  gap: 8px;
}

.stage-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stage-title {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.5px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.wave-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: #263352;
  color: #cbd5e1;
}

.wave-badge.boss {
  background: linear-gradient(90deg, #dc2626, #b91c1c);
  color: #fff;
  animation: pulseBoss 0.9s infinite alternate;
}

@keyframes pulseBoss {
  from { transform: scale(1); box-shadow: 0 0 0 rgba(239, 68, 68, 0.4); }
  to   { transform: scale(1.06); box-shadow: 0 0 12px rgba(239, 68, 68, 0.85); }
}

.sub-meta {
  font-size: 11px;
  color: var(--muted);
}

.gold-pill {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(251, 191, 36, 0.14);
  border: 1px solid rgba(251, 191, 36, 0.45);
  color: var(--gold);
  font-weight: 800;
  font-size: 15px;
  padding: 5px 11px;
  border-radius: 999px;
  min-width: 92px;
  justify-content: center;
}

.hud-actions {
  display: flex;
  gap: 5px;
}

.icon-btn {
  background: #232f4a;
  border: 1px solid #334469;
  color: #e2e8f0;
  border-radius: 8px;
  padding: 5px 7px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.icon-btn:hover { background: #2d3c5e; }
.icon-btn.active { background: #0284c7; border-color: #38bdf8; color: #fff; }

/* Battle Arena */
.arena-wrap {
  position: relative;
  z-index: 2;
  height: clamp(185px, 26dvh, 235px);
  flex-shrink: 0;
  background: #0f172a;
  border-bottom: 1px solid var(--panel-border);
  overflow: hidden;
}

#battle-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

#banner-toast {
  position: absolute;
  top: 34px;
  left: 50%;
  transform: translateX(-50%) translateY(-10px);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid #475569;
  color: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
  z-index: 20;
}

#banner-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#banner-toast.warn { border-color: #f97316; color: #fed7aa; background: rgba(124, 45, 18, 0.92); }
#banner-toast.good { border-color: #22c55e; color: #bbf7d0; background: rgba(20, 83, 45, 0.92); }
#banner-toast.boss { border-color: #ef4444; color: #fecaca; background: rgba(127, 29, 29, 0.92); }

/* Equipped Hero Panel */
.hero-panel {
  flex-shrink: 0;
  position: relative;
  z-index: 3;
  padding: 7px 10px;
  background: #172034;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.equip-strip {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.eq-badge {
  background: #111827;
  border: 1px solid #2b3854;
  border-radius: 9px;
  padding: 5px 4px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.eq-badge .eq-ico { font-size: 17px; line-height: 1.1; }
.eq-badge .eq-name { font-size: 10px; color: var(--muted); }
.eq-badge .eq-lv { font-size: 11px; font-weight: 800; color: #e2e8f0; }

.stats-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
}

.stat-chip {
  background: #101624;
  border: 1px solid #253048;
  border-radius: 7px;
  padding: 4px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-chip .k { font-size: 10px; color: var(--muted); }
.stat-chip .v { font-size: 12px; font-weight: 800; color: #f8fafc; }
.stat-chip.hp .v  { color: #4ade80; }
.stat-chip.atk .v { color: #f87171; }
.stat-chip.def .v { color: #60a5fa; }
.stat-chip.spd .v { color: #facc15; }

/* Merge Board (strictly isolated scrollable region) */
.board-section {
  flex: 1 1 0;
  min-height: 0;
  padding: 6px 10px 6px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 6px;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.board-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
  gap: 6px;
}

.board-tools {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

.mini-btn {
  background: #23304d;
  border: 1px solid #3a4f7a;
  color: #e2e8f0;
  border-radius: 7px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}
.mini-btn:hover { background: #2d3e63; }
.mini-btn.chest {
  background: linear-gradient(180deg, #b45309, #92400e);
  border-color: #f59e0b;
  color: #fef3c7;
  animation: pulseGold 1s infinite alternate;
}
.mini-btn.hidden { display: none; }

@keyframes pulseGold {
  from { box-shadow: 0 0 0 rgba(245, 158, 11, 0.3); }
  to   { box-shadow: 0 0 12px rgba(245, 158, 11, 0.85); }
}

.merge-grid-scroll {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 2px 2px 4px;
  border-radius: 10px;
  background: rgba(10, 14, 24, 0.42);
  border: 1px solid #222d45;
}

.merge-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: minmax(60px, 1fr);
  gap: 6px;
  min-height: 100%;
}

.grid-cell {
  background: var(--cell-bg);
  border: 2px solid var(--cell-border);
  border-radius: 11px;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-height: 58px;
  transition: transform 0.1s, border-color 0.15s, box-shadow 0.15s;
  touch-action: pan-y;
}

.grid-cell.has-item {
  background: radial-gradient(circle at 50% 30%, #23304f 0%, #141c2e 100%);
  touch-action: none;
}

.grid-cell.selected {
  border-color: #38bdf8 !important;
  box-shadow: 0 0 12px rgba(56, 189, 248, 0.7);
}

.grid-cell.merge-hint {
  animation: shimmerHint 1.4s infinite ease-in-out;
}

@keyframes shimmerHint {
  0%, 100% { box-shadow: 0 0 0 rgba(251, 191, 36, 0); }
  50%      { box-shadow: 0 0 10px rgba(251, 191, 36, 0.45); }
}

.grid-cell.merge-target {
  border-color: #fbbf24 !important;
  box-shadow: 0 0 16px rgba(251, 191, 36, 0.9) !important;
  transform: scale(1.04);
}

.grid-cell.dragging-src {
  opacity: 0.35;
}

.cell-ico {
  font-size: 26px;
  line-height: 1;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.5));
}

.cell-lv {
  margin-top: 2px;
  font-size: 11px;
  font-weight: 800;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.85);
}

.cell-top-star {
  position: absolute;
  top: 3px;
  right: 5px;
  font-size: 10px;
  color: #fbbf24;
}

/* Rarity Borders */
.tier-1 { border-color: #94a3b8; }
.tier-2 { border-color: #4ade80; }
.tier-3 { border-color: #38bdf8; }
.tier-4 { border-color: #c084fc; }
.tier-5 { border-color: #fb923c; }
.tier-6 { border-color: #f43f5e; box-shadow: inset 0 0 10px rgba(244, 63, 94, 0.35); }

/* Bottom Shop & Sell Zone */
.shop-bar {
  flex-shrink: 0;
  position: relative;
  z-index: 3;
  padding: 7px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(180deg, #182136 0%, #121929 100%);
  border-top: 1px solid var(--panel-border);
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.shop-buttons {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.buy-btn {
  background: linear-gradient(180deg, #243252 0%, #19243d 100%);
  border: 1px solid #354973;
  border-radius: 10px;
  padding: 6px 3px;
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  transition: transform 0.08s, border-color 0.15s, opacity 0.15s;
}

.buy-btn:active { transform: scale(0.95); }
.buy-btn.affordable {
  border-color: #fbbf24;
  background: linear-gradient(180deg, #2d3e66 0%, #1e2c4a 100%);
  box-shadow: 0 2px 10px rgba(251, 191, 36, 0.2);
}
.buy-btn.disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.buy-btn .b-ico { font-size: 18px; line-height: 1.1; }
.buy-btn .b-name { font-size: 10px; color: #cbd5e1; font-weight: 700; }
.buy-btn .b-lv { font-size: 10px; color: #7dd3fc; }
.buy-btn .b-price { font-size: 11px; font-weight: 800; color: var(--gold); }

#sell-zone {
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  color: #94a3b8;
  border: 1px dashed #3b4b6b;
  border-radius: 8px;
  padding: 5px;
  background: rgba(15, 23, 42, 0.45);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

#sell-zone.drag-over {
  background: rgba(239, 68, 68, 0.25);
  border-color: #ef4444;
  color: #fecaca;
}

/* Floating Drag Ghost */
#drag-ghost {
  position: fixed;
  width: 72px;
  height: 72px;
  border-radius: 14px;
  background: rgba(30, 41, 69, 0.95);
  border: 2px solid #fbbf24;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.6);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 999;
  transform: translate(-50%, -50%) scale(1.08);
}
