:root {
  --bg1: #0b1020;
  --bg2: #070a12;
  --panel: rgba(255, 255, 255, .08);
  --panel2: rgba(255, 255, 255, .12);
  --stroke: rgba(255, 255, 255, .18);
  --text: rgba(255, 255, 255, .92);
  --muted: rgba(255, 255, 255, .68);
  --shadow: 0 18px 60px rgba(0, 0, 0, .45);
  --r: 18px;
  --frameW: min(92vw, 520px);
}

* {
  box-sizing: border-box
}

html,
body {
  height: 100%
}

body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
  color: var(--text);
  background:
    radial-gradient(1200px 800px at 18% 10%, rgba(130, 160, 255, .25), transparent 55%),
    radial-gradient(900px 700px at 85% 25%, rgba(255, 120, 210, .18), transparent 55%),
    radial-gradient(900px 900px at 55% 85%, rgba(120, 255, 200, .12), transparent 55%),
    linear-gradient(180deg, var(--bg1), var(--bg2));
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.bg-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
}

.grain {
  opacity: .06;
  background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><filter id="n"><feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="3" stitchTiles="stitch"/></filter><rect width="100%25" height="100%25" filter="url(%23n)" opacity="0.45"/></svg>');
  mix-blend-mode: overlay;
  animation: grain 10s steps(10) infinite;
}

@keyframes grain {
  0% {
    transform: translate(0, 0)
  }

  10% {
    transform: translate(-2%, 1%)
  }

  20% {
    transform: translate(-4%, -1%)
  }

  30% {
    transform: translate(2%, -2%)
  }

  40% {
    transform: translate(-1%, 3%)
  }

  50% {
    transform: translate(-3%, 1%)
  }

  60% {
    transform: translate(2%, 2%)
  }

  70% {
    transform: translate(3%, -1%)
  }

  80% {
    transform: translate(-2%, -2%)
  }

  90% {
    transform: translate(1%, 3%)
  }

  100% {
    transform: translate(0, 0)
  }
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
  gap: 8px;
  margin: 0 auto;
}

/* Top Bar: Compact for mobile */
.topbar {
  width: 100%;
  max-width: 520px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  height: 48px;
  /* Fixed compact height */
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0;
  background: transparent;
  border: none;
  backdrop-filter: none;
}

.brand h1 {
  font-size: 14px;
  margin: 0;
  letter-spacing: -0.3px;
  font-weight: 800;
  line-height: 1.1;
  max-width: 100px;
  /* prevent stealing space */
}

.brand small {
  display: none;
}

/* Hidden on mobile by default to save space */

.controls {
  display: flex;
  gap: 6px;
  align-items: center;
}

.btn {
  appearance: none;
  border: none;
  padding: 6px 10px;
  /* smaller buttons */
  border-radius: 6px;
  background: rgba(255, 255, 255, .1);
  color: var(--text);
  border: 1px solid var(--stroke);
  backdrop-filter: blur(10px);
  font-weight: 700;
  font-size: 11px;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}

.btn:active {
  transform: scale(.96)
}

.btn.primary {
  background: linear-gradient(180deg, rgba(120, 255, 190, .25), rgba(120, 255, 190, .1));
  border-color: rgba(120, 255, 190, .4);
  color: #aaffd0;
}

.btn.danger {
  background: linear-gradient(180deg, rgba(255, 120, 160, .25), rgba(255, 120, 160, .1));
  border-color: rgba(255, 120, 160, .4);
  color: #ffdae5;
}

/* HUD: Grid for 2 players (Checkers style) */
.hud {
  width: 100%;
  max-width: 520px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding-bottom: 2px;
}

.hudCard {
  flex: 0 0 auto;
  min-width: 0;
  /* allows shrinking */
  background: linear-gradient(180deg, rgba(255, 255, 255, .12), rgba(255, 255, 255, .04));
  border: 1px solid var(--stroke);
  border-radius: 12px;
  padding: 6px 8px;
  /* reduced padding */
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  position: relative;
}

.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, .2);
  background: rgba(0, 0, 0, .3);
  flex: 0 0 auto;
  overflow: hidden;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block
}

.meta {
  min-width: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center
}

.name {
  font-weight: 800;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
  opacity: 0.9;
}

.hpLine {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%
}

.hearts {
  display: flex;
  gap: 2px;
}

.heart {
  font-size: 12px;
  filter: drop-shadow(0 0 5px rgba(255, 20, 80, .5))
}

.heart.empty {
  opacity: .15;
  filter: none
}

.dead {
  opacity: .5;
  filter: grayscale(1);
}

/* Main area */
.stage {
  width: 100%;
  max-width: 520px;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  /* Aligns content to top, removing top gap */
  align-items: center;
  min-height: 0;
  padding: 10px 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.arenaWrap {
  width: 100%;
  aspect-ratio: 1/1;
  border-radius: 20px;
  padding: 6px;
  /* Outer rim */
  background: linear-gradient(180deg, rgba(255, 255, 255, .1), rgba(255, 255, 255, .02));
  border: 1px solid rgba(255, 255, 255, .15);
  box-shadow: 0 20px 80px rgba(0, 0, 0, .6);
  backdrop-filter: blur(12px);
  position: relative;
  flex: 0 0 auto;
  /* Prevent squishing */
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 14px;
  background: linear-gradient(180deg, rgba(10, 12, 20, .9), rgba(6, 7, 12, .95));
  border: 1px solid rgba(255, 255, 255, .08);
}

.gameContent {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  /* Enforces the distance between arena and button */
}

.leaveBtnLayer {
  position: static;
  display: none;
  justify-content: center;
  width: 100%;
  z-index: 10;
  /* removed margins, handled by gap */
}

.leaveBtnLayer.show {
  display: flex;
}

#btnLeave {
  background: rgba(255, 40, 60, 0.95);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.4);
  padding: 12px 32px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 800;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  pointer-events: auto;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  transition: transform .1s;
}

#btnLeave:active {
  transform: scale(0.96)
}

/* Responsive Improvements */
@media (min-width: 480px) {
  .topbar {
    height: auto
  }

  /* Desktop override removed as static is now default */

  .brand {
    padding: 6px 12px;
    background: rgba(255, 255, 255, .05);
    border: 1px solid var(--stroke);
    border-radius: 12px;
  }

  .brand h1 {
    font-size: 15px;
    max-width: none
  }

  .brand small {
    display: block;
    color: var(--muted);
    font-size: 10px;
    margin-top: 2px
  }

  .btn {
    font-size: 12px;
    padding: 10px 14px
  }

  .avatar {
    width: 38px;
    height: 38px
  }

  .hudCard {
    padding: 10px
  }

  .heart {
    font-size: 14px;
    gap: 3px
  }
}

/* Modal menu */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, .65);
  backdrop-filter: blur(8px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
  z-index: 50;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto
}

.modal {
  width: 100%;
  max-width: 360px;
  /* Slimmer for a menu look */
  border-radius: 24px;
  background: #141824;
  border: 1px solid rgba(255, 255, 255, .15);
  box-shadow: 0 40px 120px rgba(0, 0, 0, .8);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modalHead {
  padding: 24px 20px 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* No border, cleaner look */
}

.modalHead h2 {
  margin: 0;
  font-size: 18px;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 2px;
  opacity: 0.9;
}

.modalBody {
  padding: 20px 24px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.menuBtn {
  appearance: none;
  border: none;
  width: 100%;
  padding: 16px;
  border-radius: 16px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: transform 0.1s, filter 0.1s;
  color: #fff;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.menuBtn:active {
  transform: scale(0.98);
}

.menuBtn:hover {
  filter: brightness(1.1);
}

/* Play Button */
.menuBtn.play {
  background: linear-gradient(135deg, #6e8efb, #a777e3);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

/* Points Button */
.menuBtn.points {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text);
}

/* Ticket Button */
.menuBtn.ticket {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.05);
  color: var(--muted);
  cursor: pointer; /* Changed from default to pointer */
}

/* Play State Logic check for disabling */
.menuBtn:disabled, .menuBtn[disabled] {
    opacity: 0.5;
    cursor: not-allowed;
    filter: grayscale(1);
    transform: none;
}

#matchSearchUI {
  margin-top: 20px;
  text-align: center;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(255, 255, 255, .2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg)
  }
}
