/**
 * P2P Battle Screen — Vanilla HTML/CSS/JS
 * Full-screen match-3 battle: top opponent panel, 7x7 board, bottom player panel.
 */

#p2p-battle-screen {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: none;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  font-family: 'Cinzel', Georgia, serif;
  color: #f4e9c1;
}

#p2p-battle-screen.active {
  display: flex;
}

/* ── Background ── */
.battle-bg {
  position: absolute;
  inset: 0;
  background: url('../assets/battle/background.jpg') center center / cover no-repeat #1a1208;
  z-index: 0;
}

.battle-bg::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, rgba(0,0,0,0) 45%, rgba(0,0,0,0.55) 100%);
}

/* ── Top / Bottom panels ── */
.battle-panel {
  position: relative;
  z-index: 2;
  flex: 0 0 auto;
  height: 17vh;
  min-height: 96px;
  display: flex;
  align-items: center;
  padding: 0 2.5vw;
  box-sizing: border-box;
  background: rgba(8, 6, 3, 0.78);
  box-shadow: inset 0 -1px 0 rgba(0,0,0,0.5);
}

.battle-top-panel {
  box-shadow: inset 0 1px 0 rgba(0,0,0,0.5);
}

.battle-bottom-panel {
  box-shadow: inset 0 -1px 0 rgba(0,0,0,0.5);
}

.panel-content {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 92%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

/* Верхняя панель: герои сверху, шкалы под ними */
.battle-top-panel .panel-content {
  flex-direction: column;
}

/* Нижняя панель: шкалы сверху (над героями) */
.battle-bottom-panel .panel-content {
  flex-direction: column-reverse;
}

/* ── Hero icons row ── */
.panel-heroes {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex: 0 0 auto;
}

.panel-hero {
  width: 44px;
  height: 34px;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid rgba(255, 215, 0, 0.55);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.6), inset 0 0 0 1px rgba(0, 0, 0, 0.45);
  background: rgba(20, 14, 6, 0.7);
  flex-shrink: 0;
}

.panel-hero img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.panel-hero.dead {
  filter: grayscale(1) brightness(0.45);
  border-color: rgba(180, 40, 40, 0.7);
}

/* ── Vitals (HP / Mana bars) ── */
.panel-vitals {
  width: 100%;
  max-width: 340px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.vital-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.vital-label {
  font-size: 11px;
  font-weight: 700;
  width: 22px;
  flex: 0 0 auto;
  text-shadow: 0 1px 2px #000;
  letter-spacing: 0.5px;
}

.vital-label.hp { color: #ff6b6b; }
.vital-label.mp { color: #5db4ff; }

.vital-bar {
  flex: 1 1 auto;
  height: 12px;
  border-radius: 7px;
  background: rgba(0, 0, 0, 0.6);
  border: 1px solid rgba(0, 0, 0, 0.7);
  overflow: hidden;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.7);
  position: relative;
}

.vital-fill {
  height: 100%;
  width: 100%;
  border-radius: 6px;
  /* scaleX вместо width — compositor-only анимация, без layout/paint */
  transform: scaleX(1);
  transform-origin: left center;
  transition: transform 0.45s cubic-bezier(0.22, 1, 0.36, 1);
  position: relative;
  will-change: transform;
}

.vital-fill.hp {
  background: linear-gradient(180deg, #ff8a8a 0%, #d63031 60%, #a51b1b 100%);
}

.vital-fill.mp {
  background: linear-gradient(180deg, #7ec6ff 0%, #2980d6 60%, #1c5fa0 100%);
}

.vital-fill::after {
  content: '';
  position: absolute;
  inset: 0 0 50% 0;
  background: linear-gradient(180deg, rgba(255,255,255,0.45), rgba(255,255,255,0));
  border-radius: 6px 6px 0 0;
}

.vital-text {
  font-size: 10px;
  font-weight: 700;
  min-width: 56px;
  text-align: right;
  flex: 0 0 auto;
  text-shadow: 0 1px 2px #000;
}

.panel-status {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
  min-height: 16px;
}

.status-chip {
  font-size: 9px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 8px;
  text-shadow: 0 1px 1px #000;
  border: 1px solid rgba(0,0,0,0.5);
}

.status-chip.shield { background: rgba(70, 130, 220, 0.35); color: #bfe0ff; }
.status-chip.poison { background: rgba(120, 200, 80, 0.3); color: #c8f0a0; }
.status-chip.skip { background: rgba(180, 120, 200, 0.3); color: #e8c8f0; }
.status-chip.boost { background: rgba(255, 180, 60, 0.3); color: #ffe2a0; }

/* ── Board area ── */
.battle-board-wrap {
  position: relative;
  z-index: 1;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  box-sizing: border-box;
  min-height: 0;
}

.battle-board-frame {
  position: relative;
  aspect-ratio: 1 / 1;
  width: 98vw;
  max-width: 98vw;
  max-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.battle-board-frame::before {
  content: '';
  position: absolute;
  inset: -14%;
  background: url('../assets/battle/playing_field.webp') center center / 100% 100% no-repeat;
  z-index: 0;
  border-radius: 12px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.6);
}

.battle-board {
  position: relative;
  z-index: 1;
  width: 86%;
  height: 86%;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(7, 1fr);
  gap: 1.5%;
  padding: 1%;
  box-sizing: border-box;
}

.battle-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 14%;
  contain: layout style paint;
}

.battle-cell img {
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
  /* transition только для hover/selected — не висит на каждом img
     во время анимаций падения/очистки (там работают keyframes). */
}

.battle-cell:hover img {
  transform: scale(1.06);
  transition: transform 0.2s ease;
}

.battle-cell.selected img {
  transform: scale(1.14);
  transition: transform 0.12s ease;
}

/* На тач-устройствах :hover "залипает" и держит лишние paint-ы */
@media (hover: none) {
  .battle-cell:hover img { transform: none; }
}

.battle-cell.selected::after {
  content: '';
  position: absolute;
  inset: 6%;
  border: 2px solid rgba(255, 235, 120, 0.95);
  border-radius: 18%;
  box-shadow: inset 0 0 0 1px rgba(255, 235, 120, 0.5);
  pointer-events: none;
}

.battle-cell.clearing img {
  animation: tileClear 0.3s ease forwards;

}

.battle-cell.dropping img {
  animation: tileDrop 0.38s cubic-bezier(0.34, 1.4, 0.5, 1) forwards;

}

.battle-cell.hint img {
  animation: tileHint 0.9s ease infinite;
}

@keyframes tileClear {
  0% { transform: scale(1); opacity: 1; }
  35% { transform: scale(1.25); opacity: 1; }
  100% { transform: scale(0.1); opacity: 0; }
}

@keyframes tileDrop {
  0% { transform: translateY(-50%) scale(0.85); opacity: 0; }
  60% { opacity: 1; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes tileHint {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

/* ── Turn indicator ── */
.battle-turn-indicator {
  position: absolute;
  top: 17vh;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(15, 10, 4, 0.82);
  border: 1px solid rgba(255, 215, 0, 0.45);
  border-radius: 20px;
  padding: 4px 14px;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 3px 12px rgba(0,0,0,0.6);
  white-space: nowrap;
}

.battle-turn-indicator .turn-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #888;
  box-shadow: 0 0 6px currentColor;
}

.battle-turn-indicator.mine .turn-dot { background: #5dd55d; }
.battle-turn-indicator.enemy .turn-dot { background: #ff5b5b; }

.battle-turn-indicator.mine { border-color: rgba(93, 213, 93, 0.6); }
.battle-turn-indicator.enemy { border-color: rgba(255, 91, 91, 0.6); }

.turn-timer {
  font-variant-numeric: tabular-nums;
  color: #ffd86b;
}

.turn-timer.urgent {
  color: #ff5b5b;
  animation: pulse 0.7s ease infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ── Boost bar ── */
.battle-boost-bar {
  position: absolute;
  z-index: 4;
  left: 50%;
  bottom: calc(17vh + 6px);
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  padding: 5px 10px;
  background: rgba(15, 10, 4, 0.7);
  border: 1px solid rgba(255, 215, 0, 0.35);
  border-radius: 14px;
}

.battle-boost {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: rgba(30, 20, 8, 0.8);
  border: 1px solid rgba(255, 215, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  padding: 0;
}

.battle-boost img {
  width: 82%;
  height: 82%;
  object-fit: contain;
}

.battle-boost:disabled,
.battle-boost.used {
  filter: grayscale(0.8) brightness(0.5);
  cursor: not-allowed;
}

/* Недостаточно маны: приглушён, синеватый оверлей */
.battle-boost.no-mana {
  filter: grayscale(0.5) brightness(0.55);
  cursor: not-allowed;
  border-color: rgba(93, 180, 255, 0.5);
}

.battle-boost:not(:disabled):hover {
  border-color: rgba(255, 235, 120, 0.9);
  box-shadow: 0 0 8px rgba(255, 215, 0, 0.4);
}

.battle-boost .boost-qty {
  position: absolute;
  bottom: 1px;
  right: 3px;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 1px 2px #000;
}

/* Стоимость маны буста в бою */
.battle-boost .boost-mana-cost {
  position: absolute;
  top: 1px;
  left: 2px;
  font-size: 9px;
  font-weight: 700;
  color: #5db4ff;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 4px;
  padding: 0 3px;
  line-height: 1.3;
  text-shadow: 0 1px 1px #000;
}

.battle-boost .boost-mana-cost.insufficient {
  color: #ff6b6b;
}

/* ── Floating combat text ── */
.float-text {
  position: absolute;
  z-index: 6;
  font-weight: 800;
  font-size: 20px;
  text-shadow: 0 2px 4px #000, 0 0 6px rgba(0,0,0,0.8);
  pointer-events: none;
  animation: floatUp 1.1s ease-out forwards;
  white-space: nowrap;
}

.float-text.dmg { color: #ff5b5b; }
.float-text.crit { color: #ffd000; font-size: 26px; }
.float-text.heal { color: #5dd55d; }
.float-text.mana { color: #5db4ff; }
.float-text.poison { color: #b6f06b; }
.float-text.miss { color: #cccccc; font-size: 16px; }

@keyframes floatUp {
  0% { transform: translate(-50%, 0) scale(0.7); opacity: 0; }
  20% { transform: translate(-50%, -10px) scale(1.15); opacity: 1; }
  100% { transform: translate(-50%, -60px) scale(1); opacity: 0; }
}

/* ── Result overlay ── */
.battle-result-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.85);
  text-align: center;
  padding: 24px;
}

.battle-result-overlay.active {
  display: flex;
  animation: fadeIn 0.4s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.result-title {
  font-size: 42px;
  font-weight: 800;
  margin-bottom: 12px;
  text-shadow: 0 3px 10px #000;
  letter-spacing: 2px;
}

.result-title.win { color: #ffd000; }
.result-title.lose { color: #ff5b5b; }
.result-title.draw { color: #bfbfbf; }

.result-sub {
  font-size: 15px;
  color: #d8cba0;
  margin-bottom: 14px;
  max-width: 320px;
}

.result-gems {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin-bottom: 22px;
  max-width: 340px;
}

.result-gems .rg-win {
  font-family: 'Cinzel', Georgia, serif;
  font-size: 26px;
  font-weight: 800;
  color: #ffd000;
  text-shadow: 0 2px 8px rgba(255, 208, 0, 0.5);
}

.result-gems .rg-draw {
  font-family: 'Cinzel', Georgia, serif;
  font-size: 16px;
  color: #bfbfbf;
}

.result-gems .rg-lose {
  font-family: 'Cinzel', Georgia, serif;
  font-size: 22px;
  font-weight: 700;
  color: #ff7a7a;
}

.result-gems .rg-note {
  font-size: 12px;
  color: #9c8f6a;
  text-align: center;
  line-height: 1.35;
}

.result-btn {
  font-family: 'Cinzel', Georgia, serif;
  font-size: 15px;
  font-weight: 700;
  color: #1a1208;
  background: linear-gradient(180deg, #ffe08a, #d9a73a);
  border: 1px solid #8a6a25;
  border-radius: 10px;
  padding: 11px 30px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  letter-spacing: 0.5px;
}

.result-btn:active {
  transform: translateY(1px);
}

/* ── Leave button ── */
.battle-leave-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 5;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: rgba(15, 10, 4, 0.7);
  border: 1px solid rgba(255, 215, 0, 0.4);
  color: #ffd86b;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}

.battle-leave-btn:active { transform: scale(0.92); }

/* ── Reconnecting banner ── */
.battle-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 9;
  background: rgba(15, 10, 4, 0.9);
  border: 1px solid rgba(255, 215, 0, 0.4);
  border-radius: 12px;
  padding: 14px 22px;
  font-size: 15px;
  font-weight: 700;
  text-align: center;
  display: none;
}

.battle-banner.active { display: block; }

/* ── Larger screens ── */
@media (min-width: 720px) {
  .battle-panel { height: 15vh; min-height: 110px; }
  .battle-turn-indicator { top: 15vh; }
  .battle-boost-bar { bottom: calc(15vh + 6px); }
  .panel-hero { width: 46px; height: 46px; }
}

/* Dim the board when it's the opponent's turn.
   Overlay-слой вместо filter:brightness — один композитный слой
   вместо перерастеризации всех 49 тайлов каждый кадр.
   Размер/позиция совпадают с .battle-board (86% от frame, по центру). */
.battle-board-wrap .battle-board-dim {
  position: absolute;
  top: 7%;
  left: 7%;
  width: 86%;
  height: 86%;
  z-index: 2;
  background: rgba(0, 0, 0, 0.18);
  border-radius: 12px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
}
.battle-board-wrap.locked .battle-board-dim { opacity: 1; }
.battle-board-wrap.locked .battle-cell { cursor: default; }
