*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --border: #2a2f3d;
  --text: #e8eaed;
  --muted: #8b919e;
  --accent: #4ade80;
  --warn: #fbbf24;
  --danger: #f87171;
  --sprite-size: calc(min(74vw, 38dvh) * 0.9);
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  overflow: hidden;
}

body {
  height: 100dvh;
}

.container {
  max-width: 520px;
  margin: 0 auto;
  padding: 4px 4px max(4px, env(safe-area-inset-bottom));
  height: 100dvh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 3px;
  overflow: hidden;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  flex-shrink: 0;
  padding: 0 2px;
  min-height: 26px;
}

h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.room-tag {
  font-size: 0.7rem;
  color: var(--muted);
  font-family: ui-monospace, monospace;
}

.badge {
  font-size: 0.75rem;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge.ok {
  color: var(--accent);
  border-color: #166534;
}

.badge.warn {
  color: var(--warn);
  border-color: #854d0e;
}

.badge.error {
  color: var(--danger);
  border-color: #991b1b;
}

.counter-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  text-align: center;
}

.counter-value {
  font-size: 4rem;
  font-weight: 800;
  line-height: 1;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.counter-label {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.875rem;
}

.video-wrap {
  position: relative;
  width: 100%;
  flex: 1 1 50%;
  min-height: 0;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

video,
canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

video {
  transform: scaleX(-1);
}

.status-bar {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 600;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
}

.phase-up {
  color: var(--accent);
}

.phase-down {
  color: var(--warn);
}

.phase-unknown {
  color: var(--muted);
}

.controls {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

button {
  flex: 1;
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #052e16;
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
}

.hint strong {
  color: var(--text);
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.72);
  padding: 16px;
  text-align: center;
}

.overlay.hidden {
  display: none;
}

.overlay p {
  margin: 0 0 12px;
  color: var(--muted);
}

/* Host page */
.host-page {
  text-align: center;
}

.host-page .container {
  justify-content: center;
  align-items: center;
}

.qr-box {
  background: #fff;
  padding: 16px;
  border-radius: 12px;
  display: inline-block;
}

.link-box {
  word-break: break-all;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 0.85rem;
  color: var(--accent);
  margin-top: 12px;
}

.loading-text {
  color: var(--muted);
  font-size: 0.9rem;
}

/* ── Battle arena ── */

.battle-arena {
  position: relative;
  background: linear-gradient(180deg, #1a2035 0%, #12151f 60%, var(--surface) 100%);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 2px 4px;
  overflow: hidden;
  flex: 1 1 50%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.monster-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  padding: 0 6px;
  gap: 8px;
}

.monster-progress {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}

.monster-name {
  font-size: 1.25rem;
  font-weight: 800;
  flex: 1;
  text-align: left;
}

.monster-carousel {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: stretch;
  gap: 0;
  position: relative;
}

.nav-arrow {
  flex-shrink: 0;
  width: 52px;
  align-self: center;
  height: 72px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.65);
  color: var(--text);
  font-size: 2.8rem;
  font-weight: 300;
  line-height: 1;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 52px;
  position: relative;
  z-index: 20;
  backdrop-filter: blur(4px);
}

.nav-arrow:active:not(:disabled) {
  background: rgba(74, 222, 128, 0.25);
  border-color: var(--accent);
  color: var(--accent);
}

.nav-arrow:disabled,
.nav-arrow.disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.monster-stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  overflow: hidden;
  padding: 0 2px;
  z-index: 1;
}

.damage-total {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 1.35rem;
  font-weight: 800;
  color: #fca5a5;
  z-index: 6;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.8);
  pointer-events: none;
}

.monster-shadow {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 65%;
  height: calc(var(--sprite-size) * 0.1);
  min-height: 12px;
  background: rgba(0, 0, 0, 0.45);
  border-radius: 50%;
  filter: blur(4px);
}

.monster-sprite {
  position: relative;
  z-index: 1;
}

.monster-face {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.eye {
  position: absolute;
  width: calc(var(--sprite-size) * 0.09);
  height: calc(var(--sprite-size) * 0.11);
  background: #111;
  border-radius: 50%;
  top: 32%;
}

.eye-l { left: 26%; }
.eye-r { right: 26%; }

.mouth {
  position: absolute;
  bottom: 26%;
  left: 50%;
  transform: translateX(-50%);
  width: calc(var(--sprite-size) * 0.16);
  height: calc(var(--sprite-size) * 0.06);
  border-bottom: calc(var(--sprite-size) * 0.025) solid #111;
  border-radius: 0 0 50% 50%;
}

.damage-floats {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.float-text {
  position: absolute;
  font-size: 3rem;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 3px 12px rgba(0, 0, 0, 0.85);
  animation: float-up 0.9s ease-out forwards;
}

.float-damage {
  color: #fca5a5;
}

.hp-panel {
  width: 100%;
  flex-shrink: 0;
  padding: 2px 4px 0;
  z-index: 6;
  position: relative;
}

.hp-bar {
  height: 16px;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 999px;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.12);
}

.hp-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #dc2626, #f87171);
  border-radius: 999px;
  transition: width 0.25s ease;
}

.hp-text {
  text-align: center;
  font-size: 2rem;
  font-weight: 900;
  color: #fff;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.7);
  letter-spacing: 0.04em;
}

.battle-message {
  text-align: center;
  font-size: 0.65rem;
  color: var(--muted);
  margin: 2px 0 0;
  flex-shrink: 0;
}

.video-wrap--compact {
  flex: 1;
  min-height: 0;
}

.victory-screen {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  backdrop-filter: blur(6px);
}

.victory-screen.hidden {
  display: none;
}

.victory-modal {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.victory-modal-title {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--accent);
  text-align: center;
}

/* ── Trophy card ── */

.trophy-card {
  position: relative;
  width: 100%;
  background: linear-gradient(160deg, #252a3a 0%, #1a1d27 100%);
  border: 3px solid #c9a227;
  border-radius: 16px;
  padding: 16px 16px 20px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.08);
  overflow: hidden;
}

.trophy-card-brand {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #c9a227;
  margin-bottom: 8px;
}

.trophy-monster-wrap {
  height: 120px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  margin: 4px 0 8px;
  position: relative;
  z-index: 1;
}

.trophy-monster-wrap .trophy-monster {
  --sprite-size: 100px;
  pointer-events: none;
  z-index: 1;
}

.trophy-name {
  font-size: 1.5rem;
  font-weight: 900;
  color: #fff;
  margin-bottom: 4px;
}

.trophy-hp {
  font-size: 1rem;
  font-weight: 600;
  color: #fca5a5;
  margin-bottom: 8px;
}

.trophy-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.72rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
  padding-top: 8px;
  gap: 8px;
}

.trophy-meta strong {
  color: var(--accent);
}

.trophy-stamp {
  position: absolute;
  top: 38%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-16deg);
  font-size: 2.2rem;
  font-weight: 900;
  letter-spacing: 0.1em;
  color: rgba(220, 38, 38, 0.92);
  border: 5px solid rgba(220, 38, 38, 0.92);
  border-radius: 10px;
  padding: 6px 16px;
  pointer-events: none;
  background: rgba(220, 38, 38, 0.06);
  box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.25) inset;
  z-index: 10;
}

.victory-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
}

.victory-actions button {
  width: 100%;
}

/* ── Monster base ── */

.monster {
  position: relative;
  transform-origin: center bottom;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  width: 100%;
  margin-bottom: 2px;
  z-index: 1;
  pointer-events: none;
}

.monster .monster-sprite {
  width: var(--sprite-size, 140px);
  height: var(--sprite-size, 140px);
  border-radius: 50%;
  background: var(--monster-color, #4ade80);
  border: 4px solid rgba(255, 255, 255, 0.15);
  box-shadow: inset 0 -16px 0 rgba(0, 0, 0, 0.15);
}

.monster-defeated .monster-sprite {
  opacity: 0.35;
  filter: grayscale(0.8);
}

/* Idle bobbing */
.monster-idle .monster-sprite {
  animation: monster-bob 2s ease-in-out infinite;
}

.monster-idle .monster-shadow {
  animation: shadow-pulse 2s ease-in-out infinite;
}

/* Attack lunge */
.monster-attack .monster-sprite {
  animation: monster-lunge 0.45s ease-out;
}

.monster-attack .mouth {
  height: 12px;
  border-bottom-width: 4px;
}

/* Hit reaction */
.monster-hit .monster-sprite {
  animation: monster-shake 0.35s ease;
  filter: brightness(1.4) saturate(1.2);
}

/* Death */
.monster-dead .monster-sprite {
  animation: monster-die 0.6s ease forwards;
}

.monster-dead .monster-shadow {
  animation: shadow-shrink 0.6s ease forwards;
}

/* Spawn */
.monster-spawn .monster-sprite {
  animation: monster-spawn 0.5s ease;
}

/* ── Monster types ── */

.monster--slime {
  --monster-color: #4ade80;
}

.monster--slime .monster-sprite {
  border-radius: 45% 45% 40% 40%;
  height: calc(var(--sprite-size, 140px) * 0.85);
  margin-top: 12px;
}

.monster--goblin {
  --monster-color: #a3e635;
}

.monster--goblin .monster-sprite {
  width: calc(var(--sprite-size, 140px) * 0.9);
  height: calc(var(--sprite-size, 140px) * 0.95);
  border-radius: 40% 40% 35% 35%;
}

.monster--goblin .monster-extra::before,
.monster--goblin .monster-extra::after {
  content: "";
  position: absolute;
  top: -10px;
  width: 26px;
  height: 32px;
  background: #84cc16;
  border-radius: 50% 50% 0 0;
  border: 2px solid rgba(0, 0, 0, 0.15);
}

.monster--goblin .monster-extra::before { left: 12px; transform: rotate(-15deg); }
.monster--goblin .monster-extra::after { right: 12px; transform: rotate(15deg); }

.monster--orc {
  --monster-color: #65a30d;
}

.monster--orc {
  --monster-color: #65a30d;
}

.monster--orc .monster-sprite {
  width: calc(var(--sprite-size, 140px) * 1.05);
  height: calc(var(--sprite-size, 140px) * 1);
  border-radius: 35%;
}

.monster--orc .monster-extra::before {
  content: "";
  position: absolute;
  top: 20%;
  left: calc(var(--sprite-size) * -0.2);
  width: calc(var(--sprite-size) * 0.22);
  height: calc(var(--sprite-size) * 0.48);
  background: #4d7c0f;
  border-radius: 10px;
  transform: rotate(15deg);
  animation: orc-arm 2s ease-in-out infinite;
}

.monster--orc .monster-extra::after {
  content: "";
  position: absolute;
  top: 20%;
  right: calc(var(--sprite-size) * -0.2);
  width: calc(var(--sprite-size) * 0.22);
  height: calc(var(--sprite-size) * 0.48);
  background: #4d7c0f;
  border-radius: 10px;
  transform: rotate(-15deg);
  animation: orc-arm 2s ease-in-out infinite 0.3s;
}

.monster--troll {
  --monster-color: #d97706;
}

.monster--troll {
  --monster-color: #d97706;
}

.monster--troll .monster-sprite {
  width: calc(var(--sprite-size, 140px) * 1.1);
  height: calc(var(--sprite-size, 140px) * 1.05);
  border-radius: 30%;
}

.monster--troll .eye {
  width: 16px;
  height: 10px;
  background: #fef3c7;
  border: 2px solid #111;
}

.monster--troll .monster-extra::before {
  content: "";
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-bottom: 28px solid #b45309;
}

.monster--dragon {
  --monster-color: #dc2626;
}

.monster--dragon .monster-sprite {
  width: calc(var(--sprite-size) * 1.15);
  height: calc(var(--sprite-size) * 1);
  border-radius: 40% 40% 30% 30%;
}

.monster--dragon .monster-extra::before,
.monster--dragon .monster-extra::after {
  content: "";
  position: absolute;
  top: 30%;
  width: calc(var(--sprite-size) * 0.38);
  height: calc(var(--sprite-size) * 0.26);
  background: #b91c1c;
  border-radius: 60% 20% 60% 20%;
  animation: wing-flap 1.2s ease-in-out infinite;
}

.monster--dragon .monster-extra::before {
  left: calc(var(--sprite-size) * -0.34);
  transform-origin: right center;
}

.monster--dragon .monster-extra::after {
  right: calc(var(--sprite-size) * -0.34);
  transform-origin: left center;
  animation-delay: 0.15s;
}

.monster--dragon .mouth {
  width: calc(var(--sprite-size) * 0.22);
  border-bottom-color: #fef3c7;
}

/* ── Keyframes ── */

@keyframes monster-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

@keyframes shadow-pulse {
  0%, 100% { transform: translateX(-50%) scale(1); opacity: 0.35; }
  50% { transform: translateX(-50%) scale(0.85); opacity: 0.25; }
}

@keyframes monster-lunge {
  0% { transform: translateY(0) scale(1); }
  30% { transform: translateY(12px) scale(1.08, 0.92); }
  60% { transform: translateY(-20px) scale(0.95, 1.05); }
  100% { transform: translateY(0) scale(1); }
}

@keyframes monster-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-8px) rotate(-3deg); }
  40% { transform: translateX(8px) rotate(3deg); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

@keyframes monster-die {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.1) rotate(5deg); opacity: 0.7; }
  100% { transform: scale(0) rotate(20deg); opacity: 0; }
}

@keyframes shadow-shrink {
  to { transform: translateX(-50%) scale(0); opacity: 0; }
}

@keyframes monster-spawn {
  0% { transform: scale(0) translateY(20px); opacity: 0; }
  70% { transform: scale(1.1) translateY(-5px); opacity: 1; }
  100% { transform: scale(1) translateY(0); }
}

@keyframes float-up {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-50px) scale(1.2); }
}

@keyframes orc-arm {
  0%, 100% { transform: rotate(15deg); }
  50% { transform: rotate(35deg); }
}

@keyframes wing-flap {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(-25deg); }
}

.monster--orc .monster-extra::after {
  animation-name: orc-arm-right;
}

@keyframes orc-arm-right {
  0%, 100% { transform: rotate(-15deg); }
  50% { transform: rotate(-35deg); }
}

