/**
 * P2P Battle Preparation Modal - Vanilla HTML/CSS/JS
 * Replaces the Phaser-based modal with a standard DOM modal
 */

/* ── Modal overlay ── */
#p2p-prep-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.92);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s ease;
  padding: 16px;
}

#p2p-prep-container.active {
  opacity: 1;
  pointer-events: auto;
}

#p2p-prep-container.active .p2p-prep-modal {
  animation: p2pModalIn 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes p2pModalIn {
  from {
    opacity: 0;
    transform: scale(0.92) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes borderShimmer {
  0% { border-color: rgba(139, 90, 43, 0.5); }
  50% { border-color: rgba(255, 215, 0, 0.35); }
  100% { border-color: rgba(139, 90, 43, 0.5); }
}

/* ── Modal box ── */
.p2p-prep-modal {
  width: 100%;
  max-width: 520px;
  height: 85vh;
  max-height: 640px;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.8);
  border: 2px solid rgba(139, 90, 43, 0.6);
  background: #1a0f2e;
  position: relative;
  display: flex;
  flex-direction: column;
  /* Shimmer is added via JS class on active */
}

.p2p-prep-modal.shimmer-border {
  animation: borderShimmer 3s ease-in-out infinite;
}

/* ── Close button ── */
.p2p-prep-close {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 10;
  width: 40px;
  height: 40px;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, background 0.2s ease;
  padding: 0;
}

.p2p-prep-close:hover {
  transform: scale(1.1);
  background: rgba(139, 0, 0, 0.7);
}

.p2p-prep-close:active {
  transform: scale(0.95);
}

.p2p-prep-close img {
  width: 22px;
  height: 22px;
  object-fit: contain;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.8));
}

/* ── Scrollable content area ── */
.p2p-prep-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(139, 69, 19, 0.6) rgba(0, 0, 0, 0.2);
  -ms-overflow-style: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.p2p-prep-content::-webkit-scrollbar {
  width: 5px;
}
.p2p-prep-content::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 3px;
}
.p2p-prep-content::-webkit-scrollbar-thumb {
  background: rgba(139, 69, 19, 0.6);
  border-radius: 3px;
}

/* ── Decorative border inside ── */
.p2p-prep-inner-border {
  position: absolute;
  inset: 6px;
  border: 1px solid rgba(255, 215, 0, 0.2);
  border-radius: 10px;
  pointer-events: none;
  z-index: 0;
}

/* ── Title ── */
.p2p-prep-title {
  text-align: center;
  padding: 6px 0 2px;
  position: relative;
}

.p2p-prep-title h2 {
  font-family: 'Cinzel', serif;
  font-size: 26px;
  color: #FFD700;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.8);
  margin: 0;
}

.p2p-prep-title::after {
  content: '';
  display: block;
  width: 100px;
  height: 2px;
  margin: 4px auto 0;
  background: linear-gradient(90deg, transparent, rgba(255, 215, 0, 0.6), transparent);
}

/* ── Section container ── */
.p2p-section {
  background: rgba(255, 255, 255, 0.04);
  border-radius: 10px;
  border: 1px solid;
  padding: 8px 10px;
}

.p2p-section-heroes {
  border-color: rgba(139, 90, 43, 0.4);
}

.p2p-section-boosts {
  border-color: rgba(65, 105, 225, 0.4);
}

.p2p-section-label {
  font-family: 'Cinzel', serif;
  font-size: 14px;
  color: #FFD700;
  margin: 0 0 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.p2p-section-boosts .p2p-section-label {
  color: #87CEFA;
}

.p2p-section-count {
  font-size: 11px;
  font-family: 'Cinzel', serif;
  color: #aaa;
  margin-left: auto;
}

/* ── Cards grid ── */
.p2p-cards-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

@media (max-width: 480px) {
  .p2p-cards-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 360px) {
  .p2p-cards-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ── Card ── */
.p2p-card {
  background: rgba(26, 26, 46, 0.95);
  border: 1.5px solid rgba(139, 90, 43, 0.6);
  border-radius: 8px;
  position: relative;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px;
  gap: 4px;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.p2p-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  border-radius: 8px 8px 0 0;
  opacity: 0.9;
}

/* Rarity top bar colors */
.p2p-card[data-rarity="Common"]::before    { background: #808080; }
.p2p-card[data-rarity="Rare"]::before      { background: #4169E1; }
.p2p-card[data-rarity="Epic"]::before      { background: #9370DB; }
.p2p-card[data-rarity="Legendary"]::before { background: #FFD700; }

.p2p-card[data-rarity="common"]::before    { background: #808080; }
.p2p-card[data-rarity="rare"]::before      { background: #4169E1; }
.p2p-card[data-rarity="epic"]::before      { background: #9370DB; }
.p2p-card[data-rarity="legendary"]::before { background: #FFD700; }

/* Selection state */
.p2p-card.selected {
  border-color: currentColor;
}

.p2p-card.selected[data-rarity="Common"],
.p2p-card.selected[data-rarity="common"]    { border-color: #b0b0b0; box-shadow: 0 0 0 1.5px rgba(176,176,176,0.7), inset 0 0 10px rgba(176,176,176,0.15); }
.p2p-card.selected[data-rarity="Rare"],
.p2p-card.selected[data-rarity="rare"]        { border-color: #4facfe; box-shadow: 0 0 0 1.5px rgba(79,172,254,0.7), inset 0 0 10px rgba(79,172,254,0.15); }
.p2p-card.selected[data-rarity="Epic"],
.p2p-card.selected[data-rarity="epic"]        { border-color: #bc6bff; box-shadow: 0 0 0 1.5px rgba(188,107,255,0.7), inset 0 0 10px rgba(188,107,255,0.15); }
.p2p-card.selected[data-rarity="Legendary"],
.p2p-card.selected[data-rarity="legendary"]  { border-color: #ffeb3b; box-shadow: 0 0 0 1.5px rgba(255,235,59,0.7), inset 0 0 10px rgba(255,235,59,0.15); }

.p2p-card:hover {
  transform: scale(1.04);
}

.p2p-card:active {
  transform: scale(0.97);
}

/* Image area */
.p2p-card-image {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 4px;
  overflow: hidden;
  background: rgba(10, 10, 24, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.p2p-card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Flavor image for boost icon */
.p2p-card-image.icon-img img {
  width: 70%;
  height: 70%;
  object-fit: contain;
}

/* Card name */
.p2p-card-name {
  font-family: 'Cinzel', serif;
  font-size: 10px;
  color: #ffffff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.8);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  margin-top: 2px;
  line-height: 1.2;
}

/* Rarity badge */
.p2p-card-rarity {
  font-family: 'Cinzel', serif;
  font-size: 8px;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(0,0,0,0.3);
  border: 1px solid currentColor;
  line-height: 1;
}

/* Power bar line (hero cards) */
.p2p-card-power {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px;
  background: rgba(255, 215, 0, 0.08);
  border-radius: 3px;
  border: 1px solid rgba(255, 215, 0, 0.25);
  margin-top: 2px;
}

.p2p-card-power span:first-child {
  font-size: 10px;
}

.p2p-card-power span:last-child {
  font-family: 'Cinzel', serif;
  font-size: 10px;
  color: #FFD700;
  font-weight: 700;
}

/* Quantity badge (boosts) */
.p2p-card-qty {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background: rgba(0,0,0,0.7);
  color: #ddd;
  font-family: 'Cinzel', serif;
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 6px;
}

/* Selection number badge */
.p2p-card-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Cinzel', serif;
  font-size: 11px;
  font-weight: 700;
  color: #000;
  z-index: 2;
}

.p2p-card-badge::before {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  opacity: 0.5;
  z-index: -1;
}

/* Selection badge colors by rarity */
.p2p-card[data-rarity="Common"]    .p2p-card-badge,
.p2p-card[data-rarity="common"]    .p2p-card-badge { background: rgba(176,176,176,0.95); }
.p2p-card[data-rarity="Common"]    .p2p-card-badge::before,
.p2p-card[data-rarity="common"]    .p2p-card-badge::before { background: #b0b0b0; }

.p2p-card[data-rarity="Rare"]      .p2p-card-badge,
.p2p-card[data-rarity="rare"]      .p2p-card-badge { background: rgba(79,172,254,0.95); }
.p2p-card[data-rarity="Rare"]      .p2p-card-badge::before,
.p2p-card[data-rarity="rare"]      .p2p-card-badge::before { background: #4facfe; }

.p2p-card[data-rarity="Epic"]      .p2p-card-badge,
.p2p-card[data-rarity="epic"]      .p2p-card-badge { background: rgba(188,107,255,0.95); }
.p2p-card[data-rarity="Epic"]      .p2p-card-badge::before,
.p2p-card[data-rarity="epic"]      .p2p-card-badge::before { background: #bc6bff; }

.p2p-card[data-rarity="Legendary"]  .p2p-card-badge,
.p2p-card[data-rarity="legendary"]  .p2p-card-badge { background: rgba(255,235,59,0.95); }
.p2p-card[data-rarity="Legendary"]  .p2p-card-badge::before,
.p2p-card[data-rarity="legendary"]  .p2p-card-badge::before { background: #ffeb3b; }

/* Rarity text colors */
.p2p-card[data-rarity="Common"]    .p2p-card-rarity,
.p2p-card[data-rarity="common"]    .p2p-card-rarity    { color: #b0b0b0; border-color: rgba(176,176,176,0.5); }
.p2p-card[data-rarity="Rare"]      .p2p-card-rarity,
.p2p-card[data-rarity="rare"]      .p2p-card-rarity      { color: #4facfe; border-color: rgba(79,172,254,0.5); }
.p2p-card[data-rarity="Epic"]      .p2p-card-rarity,
.p2p-card[data-rarity="epic"]      .p2p-card-rarity      { color: #bc6bff; border-color: rgba(188,107,255,0.5); }
.p2p-card[data-rarity="Legendary"] .p2p-card-rarity,
.p2p-card[data-rarity="legendary"] .p2p-card-rarity      { color: #ffeb3b; border-color: rgba(255,235,59,0.5); }

/* ── Empty state ── */
.p2p-empty {
  text-align: center;
  color: #888;
  font-family: 'Cinzel', serif;
  font-size: 13px;
  padding: 16px;
  grid-column: 1 / -1;
}

/* ── Selected summary panel ── */
.p2p-summary {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 215, 0, 0.4);
  border-radius: 10px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.p2p-summary-label {
  font-family: 'Cinzel', serif;
  font-size: 13px;
  color: #ffffff;
}

.p2p-summary-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.p2p-summary-heroes {
  font-family: 'Cinzel', serif;
  font-size: 12px;
  color: #FFD700;
}

.p2p-summary-heroes.complete { color: #4CAF50; }

.p2p-summary-boosts {
  font-family: 'Cinzel', serif;
  font-size: 12px;
  color: #87CEFA;
}

.p2p-summary-boosts.complete { color: #4CAF50; }

/* ── Toast ── */
.p2p-toast {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(139, 0, 0, 0.92);
  border-radius: 8px;
  padding: 10px 20px;
  font-family: 'Cinzel', serif;
  font-size: 13px;
  color: #ffffff;
  z-index: 20;
  pointer-events: none;
  white-space: nowrap;
  animation: toastIn 0.25s ease;
}

.p2p-toast.hiding {
  animation: toastOut 0.4s ease forwards;
}

@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%,-40%); }
  to   { opacity: 1; transform: translate(-50%,-50%); }
}
@keyframes toastOut {
  from { opacity: 1; transform: translate(-50%,-50%); }
  to   { opacity: 0; transform: translate(-50%,-60%); }
}

/* ── Action buttons ── */
.p2p-actions {
  display: flex;
  gap: 12px;
  padding: 8px 12px 12px;
  justify-content: center;
  margin-top: auto;
  flex-shrink: 0;
}

.p2p-btn {
  flex: 1;
  max-width: 170px;
  padding: 10px 0;
  border-radius: 8px;
  border: 2px solid rgba(255,255,255,0.2);
  font-family: 'Cinzel', serif;
  font-size: 13px;
  font-weight: 700;
  color: #ffffff;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  position: relative;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.p2p-btn:hover {
  transform: scale(1.03);
  border-color: rgba(255,215,0,0.6);
  box-shadow: 0 0 12px rgba(255,215,0,0.15);
}

.p2p-btn:active {
  transform: scale(0.97);
}

.p2p-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  border-color: rgba(255,255,255,0.1);
}

.p2p-btn-create {
  background: rgba(34, 139, 34, 0.85);
}
.p2p-btn-create:hover:not(:disabled) {
  background: rgba(34, 139, 34, 1);
}

.p2p-btn-find {
  background: rgba(65, 105, 225, 0.85);
}
.p2p-btn-find:hover:not(:disabled) {
  background: rgba(65, 105, 225, 1);
}

/* ── Loading overlay ── */
.p2p-loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(26, 15, 46, 0.8);
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
}

.p2p-loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid rgba(255,255,255,0.15);
  border-top-color: #FFD700;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.p2p-loading-text {
  font-family: 'Cinzel', serif;
  font-size: 16px;
  color: #FFD700;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Shake animation for over-selection */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

.p2p-card.shake {
  animation: shake 0.35s ease;
}

/* Selection bounce */
@keyframes selectBounce {
  0% { transform: scale(1); }
  40% { transform: scale(1.08); }
  100% { transform: scale(1.04); }
}
.p2p-card.bounce {
  animation: selectBounce 0.2s ease;
}

/* Glow on hero cards */
.p2p-card.hero-card {
  border-color: rgba(139, 90, 43, 0.4);
}

/* Glow on boost cards */
.p2p-card.boost-card {
  border-color: rgba(65, 105, 225, 0.4);
}

/* ── Responsive ── */
@media (max-width: 640px) {
  .p2p-prep-modal {
    height: 88vh;
    max-height: none;
    border-radius: 10px;
  }
  #p2p-prep-container {
    padding: 8px;
  }
  .p2p-prep-title h2 {
    font-size: 20px;
  }
  .p2p-cards-grid {
    gap: 6px;
  }
  .p2p-card {
    padding: 4px;
  }
  .p2p-card-name {
    font-size: 9px;
  }
}
