/* ============================================================
   KLUSTER MAGNATE — Design System & Styles
   ============================================================ */

:root {
  --bg-deep:     #08080f;
  --bg-surface:  #0f0f1e;
  --bg-card:     rgba(255,255,255,0.04);
  --bg-card-hov: rgba(255,255,255,0.07);
  --border:      rgba(255,255,255,0.08);
  --border-glow: rgba(168,85,247,0.4);

  --p1-color:    #e879f9;   /* fuchsia */
  --p2-color:    #22d3ee;   /* cyan    */
  --accent:      #a78bfa;   /* violet  */
  --gold:        #fbbf24;

  --text-primary:   #f1f0ff;
  --text-secondary: rgba(241,240,255,0.55);
  --text-muted:     rgba(241,240,255,0.3);

  --radius-sm:  8px;
  --radius-md:  16px;
  --radius-lg:  28px;
  --radius-xl:  40px;

  --shadow-card: 0 8px 40px rgba(0,0,0,0.6), 0 0 0 1px rgba(255,255,255,0.05);
  --shadow-glow-p1: 0 0 30px rgba(232,121,249,0.35);
  --shadow-glow-p2: 0 0 30px rgba(34,211,238,0.35);

  --font-main: 'Outfit', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  --arena-size: min(92vw, 92vh, 640px);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text-primary);
  font-family: var(--font-main);
  -webkit-font-smoothing: antialiased;
}

/* ── Screens ─────────────────────────────────────────────── */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: opacity .35s ease;
  opacity: 0;
}
.screen.active { display: flex; opacity: 1; }

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 28px;
  border-radius: var(--radius-md);
  font-family: var(--font-main);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease;
  letter-spacing: .04em;
  text-transform: uppercase;
  outline: none;
  white-space: nowrap;
}
.btn:active { transform: scale(0.96); }

.btn-primary {
  background: linear-gradient(135deg, #9333ea, #e879f9);
  color: #fff;
  box-shadow: 0 4px 24px rgba(168,85,247,0.4);
}
.btn-primary:hover {
  box-shadow: 0 6px 36px rgba(232,121,249,0.6);
  transform: translateY(-2px);
}

.btn-secondary {
  background: rgba(255,255,255,0.06);
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.btn-secondary:hover {
  background: rgba(255,255,255,0.1);
  border-color: rgba(168,85,247,0.4);
  transform: translateY(-2px);
}

.btn-icon {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 8px 12px;
  transition: background .18s;
}
.btn-icon:hover { background: rgba(255,255,255,0.12); }

/* ── Inputs ──────────────────────────────────────────────── */
input[type="text"] {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 16px;
  color: var(--text-primary);
  font-family: var(--font-main);
  font-size: .95rem;
  outline: none;
  width: 100%;
  transition: border-color .2s;
}
input[type="text"]::placeholder { color: var(--text-muted); }
input[type="text"]:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(167,139,250,0.15); }

select {
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  color: var(--text-primary);
  font-family: var(--font-main);
  font-size: .9rem;
  outline: none;
  cursor: pointer;
  margin-left: 8px;
  transition: border-color .2s;
}
select:focus { border-color: var(--accent); }
select option { background: #1a1a2e; }

/* ============================================================
   LOBBY SCREEN
   ============================================================ */
#screen-lobby { overflow: auto; padding: 24px 16px; }

.lobby-bg-glow {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 60% 50% at 20% 30%, rgba(168,85,247,0.15) 0%, transparent 70%),
    radial-gradient(ellipse 50% 40% at 80% 70%, rgba(6,182,212,0.12) 0%, transparent 70%);
  pointer-events: none;
}

.lobby-container {
  position: relative;
  z-index: 1;
  max-width: 1100px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 40px;
}

/* Logo */
.logo-wrap { text-align: center; }
.logo-icon {
  width: 80px; height: 80px;
  margin: 0 auto 16px;
  animation: float 4s ease-in-out infinite;
}
.logo-icon svg { width: 100%; height: 100%; }

.logo-title {
  font-size: clamp(2.2rem, 6vw, 3.8rem);
  font-weight: 900;
  letter-spacing: .12em;
  color: #fff;
  text-shadow: 0 0 40px rgba(168,85,247,0.5);
}
.logo-title span {
  background: linear-gradient(90deg, #e879f9, #22d3ee);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-left: 8px;
}
.logo-sub { color: var(--text-secondary); letter-spacing: .15em; font-size: .85rem; margin-top: 6px; }

/* Cards grid */
.lobby-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
  width: 100%;
}

.lobby-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 28px 32px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: relative;
  transition: border-color .25s, box-shadow .25s, transform .25s;
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow-card);
}
.lobby-card:hover {
  border-color: rgba(168,85,247,0.35);
  box-shadow: var(--shadow-card), 0 0 40px rgba(168,85,247,0.1);
  transform: translateY(-4px);
}
.lobby-card.featured {
  border-color: rgba(168,85,247,0.4);
  background: linear-gradient(145deg, rgba(147,51,234,0.12), rgba(6,182,212,0.06));
}

.card-badge {
  position: absolute;
  top: -12px; left: 50%; transform: translateX(-50%);
  background: linear-gradient(90deg, #9333ea, #e879f9);
  color: #fff;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .12em;
  padding: 4px 14px;
  border-radius: 20px;
  white-space: nowrap;
}
.card-icon { font-size: 2rem; }
.lobby-card h2 { font-size: 1.4rem; font-weight: 700; }
.lobby-card p { color: var(--text-secondary); line-height: 1.6; font-size: .92rem; }

.card-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 4px 0;
}
.card-options label {
  display: flex;
  align-items: center;
  font-size: .88rem;
  color: var(--text-secondary);
  gap: 8px;
}

.online-buttons { display: flex; flex-direction: column; gap: 10px; }
.divider-or {
  text-align: center;
  color: var(--text-muted);
  font-size: .8rem;
  letter-spacing: .1em;
  position: relative;
}
.divider-or::before, .divider-or::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: var(--border);
}
.divider-or::before { left: 0; }
.divider-or::after { right: 0; }

.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; }

.conn-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: .85rem;
  color: var(--text-secondary);
}
.conn-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--gold);
  animation: pulse-dot 1.2s ease-in-out infinite;
}
.conn-status.connected .conn-dot { background: #4ade80; animation: none; }
.conn-status.hidden { display: none; }

/* ============================================================
   WAITING ROOM
   ============================================================ */
#screen-waiting { gap: 24px; }
#screen-waiting,
#screen-result {
  padding: 24px 16px;
  overflow: auto;
}
#screen-waiting h2 { font-size: 1.2rem; color: var(--text-secondary); letter-spacing: .1em; }

.waiting-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 40px 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  max-width: 480px;
  width: 90%;
  box-shadow: var(--shadow-card);
  backdrop-filter: blur(16px);
}

.room-code-display {
  font-family: var(--font-mono);
  font-size: 3rem;
  font-weight: 700;
  letter-spacing: .3em;
  background: linear-gradient(90deg, #e879f9, #22d3ee);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.copy-btn { width: 100%; }

.waiting-players {
  display: flex;
  align-items: center;
  gap: 24px;
  width: 100%;
  justify-content: center;
  padding-top: 8px;
}
.player-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.player-avatar {
  width: 56px; height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
  border: 2px solid;
}
.p1-color { color: var(--p1-color); border-color: var(--p1-color); background: rgba(232,121,249,0.12); }
.p2-color { color: var(--p2-color); border-color: var(--p2-color); background: rgba(34,211,238,0.12); }
.vs-label { font-size: 1.2rem; font-weight: 900; color: var(--text-muted); }
.ready-badge {
  font-size: .65rem;
  font-weight: 700;
  letter-spacing: .1em;
  background: rgba(74,222,128,0.15);
  color: #4ade80;
  padding: 3px 10px;
  border-radius: 20px;
}
.waiting-dots { color: var(--text-muted); letter-spacing: .2em; animation: blink 1.2s ease-in-out infinite; }

.back-btn {
  position: absolute;
  top: 20px; left: 20px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: .9rem;
  cursor: pointer;
  font-family: var(--font-main);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  transition: color .2s, background .2s;
}
.back-btn:hover { color: #fff; background: rgba(255,255,255,0.07); }

/* ============================================================
   GAME SCREEN
   ============================================================ */
#screen-game {
  padding: 0;
  justify-content: flex-start;
  background: var(--bg-deep);
  gap: 0;
  height: 100dvh;
  overflow: hidden;
}

/* HUD */
.hud {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(8,8,15,0.8);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  z-index: 10;
  flex-shrink: 0;
}

.hud-player {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 140px;
}
.hud-player.p2-hud { text-align: right; align-items: flex-end; }

.hud-name {
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--text-secondary);
}
.hud-p1 .hud-name { color: var(--p1-color); }
.hud-p2 .hud-name { color: var(--p2-color); }

.hud-magnets {
  display: flex;
  align-items: center;
  gap: 6px;
}
.hud-count {
  font-size: 1.8rem;
  font-weight: 900;
  line-height: 1;
  font-family: var(--font-mono);
}
.hud-p1 .hud-count { color: var(--p1-color); }
.hud-p2 .hud-count { color: var(--p2-color); }
.hud-label { font-size: .72rem; color: var(--text-muted); align-self: flex-end; padding-bottom: 4px; }

.magnet-dot-container {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  max-width: 80px;
}

.hud-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.turn-indicator {
  position: relative;
  padding: 8px 20px;
  border-radius: var(--radius-md);
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  font-weight: 700;
  font-size: .9rem;
  letter-spacing: .06em;
  overflow: hidden;
}
.turn-glow {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0;
  transition: opacity .4s;
  pointer-events: none;
}
.turn-indicator.p1-turn {
  border-color: rgba(232,121,249,0.5);
  color: var(--p1-color);
}
.turn-indicator.p1-turn .turn-glow {
  background: radial-gradient(ellipse at 50% 50%, rgba(232,121,249,0.2), transparent);
  opacity: 1;
}
.turn-indicator.p2-turn {
  border-color: rgba(34,211,238,0.5);
  color: var(--p2-color);
}
.turn-indicator.p2-turn .turn-glow {
  background: radial-gradient(ellipse at 50% 50%, rgba(34,211,238,0.2), transparent);
  opacity: 1;
}

/* Game arena wrap */
.game-wrap {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  padding: 16px;
}

#game-canvas {
  display: block;
  cursor: crosshair;
  touch-action: none;
  max-width: 100%;
  max-height: 100%;
}

.placement-hint {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(8,8,15,0.75);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 20px;
  font-size: .82rem;
  color: var(--text-secondary);
  backdrop-filter: blur(8px);
  pointer-events: none;
  transition: opacity .3s;
  width: min(90%, 420px);
  text-align: center;
}

/* Replay bar */
.replay-bar {
  width: 100%;
  padding: 12px 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  background: rgba(8,8,15,0.85);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(12px);
  flex-shrink: 0;
}
.replay-bar.hidden { display: none; }

/* ============================================================
   RESULT SCREEN
   ============================================================ */
.result-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 48px 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  max-width: 520px;
  width: 90%;
  backdrop-filter: blur(20px);
  box-shadow: var(--shadow-card), 0 0 60px rgba(251,191,36,0.08);
}
.result-trophy { font-size: 4rem; animation: bounce 1s ease-in-out; }
#result-title { font-size: 2rem; font-weight: 900; text-align: center; }
#result-subtitle { color: var(--text-secondary); text-align: center; }

.result-stats {
  display: flex;
  gap: 16px;
  width: 100%;
  justify-content: center;
}
.stat-box {
  flex: 1;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.stat-label { font-size: .72rem; color: var(--text-muted); letter-spacing: .06em; text-transform: uppercase; }
.stat-val { font-size: 1.8rem; font-weight: 900; font-family: var(--font-mono); color: var(--accent); }

.result-actions { display: flex; gap: 12px; width: 100%; }
.result-actions .btn { flex: 1; }

/* ============================================================
   OVERLAYS
   ============================================================ */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(6px);
}
.overlay.hidden { display: none; }

.overlay-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 40px 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  min-width: 280px;
  box-shadow: var(--shadow-card);
}
.overlay-card h3 { font-size: 1.6rem; font-weight: 700; }
.overlay-card .btn { width: 100%; }

/* Snap overlay */
.snap-overlay {
  background: rgba(0,0,0,0.6);
  flex-direction: column;
  gap: 20px;
  pointer-events: none;
}
.snap-burst { position: relative; display: flex; align-items: center; justify-content: center; }
.snap-ring {
  position: absolute;
  border-radius: 50%;
  border: 2px solid rgba(232,121,249,0.6);
  animation: snap-ring 0.6s ease-out forwards;
}
.r1 { width: 80px; height: 80px; animation-delay: 0s; }
.r2 { width: 130px; height: 130px; animation-delay: .08s; border-color: rgba(167,139,250,0.4); }
.r3 { width: 180px; height: 180px; animation-delay: .16s; border-color: rgba(34,211,238,0.3); }
.snap-text {
  font-size: 3.5rem;
  font-weight: 900;
  font-family: var(--font-mono);
  background: linear-gradient(90deg, #e879f9, #22d3ee);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: none;
  animation: snap-text 0.5s cubic-bezier(0.34,1.56,0.64,1) forwards;
  transform: scale(0);
}
.snap-info {
  color: var(--text-primary);
  font-size: 1.1rem;
  text-align: center;
  background: rgba(255,255,255,0.08);
  padding: 10px 24px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}

/* ============================================================
   ANIMATIONS
   ============================================================ */
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(-8px); }
}
@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.4; transform: scale(0.75); }
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.3; }
}
@keyframes snap-ring {
  0%   { transform: scale(0.4); opacity: 1; }
  100% { transform: scale(1.8); opacity: 0; }
}
@keyframes snap-text {
  to { transform: scale(1); }
}
@keyframes bounce {
  0%   { transform: scale(0) rotate(-15deg); }
  60%  { transform: scale(1.2) rotate(5deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* Magnet dots in HUD */
.m-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--p1-color);
  box-shadow: 0 0 4px currentColor;
  transition: opacity .3s;
}
.m-dot.p2 { background: var(--p2-color); }
.m-dot.used { opacity: 0.18; }

/* ── Ripple on button click ──────────────────────────────── */
.btn { overflow: hidden; position: relative; }
.ripple {
  position: absolute;
  border-radius: 50%;
  background: rgba(255,255,255,0.25);
  transform: scale(0);
  animation: ripple-anim .5s linear;
  pointer-events: none;
}
@keyframes ripple-anim { to { transform: scale(4); opacity: 0; } }

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 600px) {
  .lobby-cards { grid-template-columns: 1fr; }
  .result-stats { flex-direction: column; gap: 10px; }
  .result-container { padding: 32px 24px; }
  .hud-player { min-width: 0; }
  .hud-count { font-size: 1.4rem; }
  .result-actions { flex-direction: column; }
  .waiting-container { padding: 32px 24px; }
}

@media (max-width: 1024px) {
  #screen-lobby {
    padding: 32px 16px;
  }

  .lobby-container {
    gap: 28px;
  }

  .lobby-cards {
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  }

  .lobby-card {
    padding: 24px 22px 28px;
  }

  .hud {
    padding: 12px;
    gap: 12px;
  }

  .hud-player {
    min-width: 110px;
  }

  .hud-count {
    font-size: 1.5rem;
  }

  .turn-indicator {
    padding: 8px 14px;
    font-size: .82rem;
  }

  .game-wrap {
    padding: 12px;
  }

  .result-container,
  .waiting-container,
  .overlay-card {
    width: min(100%, 560px);
  }
}

@media (max-width: 768px) {
  .btn {
    width: 100%;
    white-space: normal;
  }

  .card-options label {
    flex-direction: column;
    align-items: flex-start;
  }

  select {
    margin-left: 0;
    width: 100%;
  }

  .join-row {
    flex-direction: column;
  }

  #screen-waiting,
  #screen-result {
    justify-content: flex-start;
  }

  .waiting-container {
    padding: 24px 20px;
    gap: 18px;
  }

  .back-btn {
    position: static;
    align-self: flex-start;
    padding-left: 0;
  }

  .room-code-display {
    font-size: clamp(2rem, 9vw, 3rem);
    letter-spacing: .18em;
    text-align: center;
  }

  .waiting-players {
    flex-direction: column;
    gap: 14px;
  }

  .hud {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: start;
  }

  .hud-player {
    min-width: 0;
  }

  .hud-player.p2-hud {
    justify-self: end;
  }

  .hud-magnets {
    flex-wrap: wrap;
  }

  .magnet-dot-container {
    max-width: 64px;
  }

  .placement-hint {
    bottom: 12px;
    padding: 9px 14px;
    font-size: .78rem;
  }

  .result-container {
    padding: 28px 20px;
  }

  .result-stats,
  .result-actions {
    flex-direction: column;
  }

  .overlay-card {
    min-width: 0;
    width: min(100%, 420px);
    padding: 28px 20px;
  }

  .snap-text {
    font-size: clamp(2rem, 11vw, 3.5rem);
  }

  .snap-info {
    width: min(90%, 420px);
    font-size: .95rem;
    padding: 10px 16px;
  }
}

@media (max-width: 480px) {
  #screen-lobby {
    padding: 24px 12px;
  }

  .logo-title span {
    display: block;
    margin-left: 0;
  }

  .logo-sub {
    letter-spacing: .1em;
  }

  .lobby-card {
    border-radius: 22px;
    padding: 22px 18px 24px;
  }

  .hud {
    grid-template-columns: 1fr;
    justify-items: stretch;
  }

  .hud-center {
    order: -1;
    margin-bottom: 2px;
  }

  .hud-player.p2-hud,
  .hud-player.p1-hud {
    align-items: flex-start;
    text-align: left;
    justify-self: stretch;
  }

  .hud-player.p2-hud .hud-magnets {
    justify-content: flex-start;
  }

  .turn-indicator {
    width: 100%;
    text-align: center;
  }

  .replay-bar {
    padding: 12px;
  }

  .replay-bar .btn {
    width: 100%;
  }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }
