:root {
  --bg-a: #0f172a;
  --bg-b: #12233f;
  --bg-c: #1f2f57;
  --card: rgba(255, 255, 255, 0.94);
  --text: #1c2432;
  --muted: #64748b;
  --accent: #ef476f;
  --accent-2: #22c55e;
  --accent-3: #ffd166;
  --player-1: #ff4d6d;
  --player-2: #22c55e;
  --board: #16324f;
  --slot: #e2e8f0;
  --shadow: 0 20px 50px rgba(13, 25, 45, 0.25);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Nunito", sans-serif;
  color: var(--text);
  background: radial-gradient(1200px 600px at 10% 0%, #273e70 0%, var(--bg-a) 60%),
    linear-gradient(135deg, var(--bg-a) 0%, var(--bg-b) 50%, var(--bg-c) 100%);
  overflow-x: hidden;
}

.bg-glow {
  position: fixed;
  border-radius: 999px;
  filter: blur(60px);
  opacity: 0.35;
  pointer-events: none;
}

.bg-glow-a {
  width: 280px;
  height: 280px;
  background: #f43f5e;
  top: -80px;
  right: -80px;
}

.bg-glow-b {
  width: 320px;
  height: 320px;
  background: #22d3ee;
  bottom: -120px;
  left: -100px;
}

.page {
  max-width: 1080px;
  margin: 0 auto;
  padding-top: max(24px, calc(env(safe-area-inset-top) + 10px));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-bottom: 32px;
  padding-left: max(16px, env(safe-area-inset-left));
}

.hero h1 {
  font-family: "Bungee", cursive;
  font-size: clamp(2rem, 5vw, 3rem);
  margin: 8px 0 0;
  color: #f8fafc;
  letter-spacing: 1px;
}

.hero p {
  color: #d7e0ef;
  margin-top: 8px;
}

.card {
  background: var(--card);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 20px;
  margin-top: 16px;
  backdrop-filter: blur(2px);
}

.hidden {
  display: none !important;
}

.field label {
  font-weight: 700;
  display: block;
  margin-bottom: 6px;
}

.field input {
  width: 100%;
  border-radius: 12px;
  border: 2px solid #dbe4ef;
  padding: 12px;
  font-size: 1rem;
  outline: none;
}

.field input:focus {
  border-color: #4f46e5;
}

.mode-picker {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.btn-mode {
  color: #334155;
  background: linear-gradient(135deg, #eef2ff 0%, #dbeafe 100%);
  border: 2px solid #d4dbec;
}

.btn-mode.active {
  color: #fff;
  border-color: transparent;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
}

.lobby-step {
  margin-top: 6px;
}

.step-chip {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 4px 10px;
  margin: 0 0 8px;
  font-size: 0.8rem;
  font-weight: 900;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  color: #9a3412;
  background: linear-gradient(135deg, #fde68a 0%, #fed7aa 100%);
}

.lobby-options {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.option-card {
  border-radius: 14px;
  border: 2px solid #dbe4ef;
  background: rgba(248, 250, 252, 0.75);
  padding: 12px;
}

.option-card h3 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.option-copy {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.95rem;
}

.option-card .btn {
  width: 100%;
}

.join-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
}

.buttons-row {
  margin-top: 14px;
}

.btn {
  border: none;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
  padding: 11px 16px;
  transition: transform 0.15s ease, filter 0.2s ease;
}

.btn:hover {
  transform: translateY(-1px);
  filter: brightness(0.98);
}

.btn:disabled {
  cursor: not-allowed;
  filter: grayscale(0.2);
  opacity: 0.6;
}

.btn-primary {
  color: #fff;
  background: linear-gradient(135deg, #ef476f 0%, #f77f00 100%);
}

.btn-secondary {
  color: #fff;
  background: linear-gradient(135deg, #0ea5e9 0%, #2563eb 100%);
}

.btn-tertiary {
  color: #1e293b;
  background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
}

.divider {
  display: flex;
  align-items: center;
  margin: 16px 0;
  color: var(--muted);
  font-weight: 700;
}

.divider::before,
.divider::after {
  content: "";
  height: 2px;
  background: #d5deea;
  flex: 1;
}

.divider span {
  padding: 0 10px;
}

.error {
  color: #b42318;
  margin-top: 10px;
  min-height: 20px;
}

.top-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.top-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.status {
  margin: 8px 0 4px;
  font-weight: 800;
}

.turn {
  margin: 0 0 12px;
  font-weight: 700;
  color: #334155;
}

.turn.your-turn {
  animation: your-turn-bounce 0.8s ease-in-out infinite;
}

@keyframes your-turn-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.player-name {
  font-weight: 800;
}

.player-name.p1 {
  color: var(--player-1);
}

.player-name.p2 {
  color: var(--player-2);
}

.turn-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
  animation: turn-dot-pulse 1s ease-in-out infinite;
}

.turn-dot.p1 {
  background: var(--player-1);
  box-shadow: 0 0 8px rgba(255, 77, 109, 0.6);
}

.turn-dot.p2 {
  background: var(--player-2);
  box-shadow: 0 0 8px rgba(34, 197, 94, 0.6);
}

@keyframes turn-dot-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.3); opacity: 0.7; }
}

.small-muted {
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.95rem;
}

.room-timer {
  margin: 4px 0 0;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--muted);
}

.room-timer.warn {
  color: #d97706;
}

.room-timer.urgent {
  color: #dc2626;
  animation: pulse-text 1s ease-in-out infinite;
}

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

.chip-count {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 0 2px;
}

.chip-count-bar {
  flex: 1;
  height: 22px;
  border-radius: 11px;
  overflow: hidden;
  background: #e2e8f0;
  display: flex;
  position: relative;
}

.chip-count-fill {
  height: 100%;
  transition: width 0.4s ease;
}

.chip-count-fill.p1 {
  background: linear-gradient(90deg, #ff4d6d 0%, #ff6b81 100%);
}

.chip-count-fill.p2 {
  background: linear-gradient(90deg, #1db954 0%, #22c55e 100%);
}

.chip-count-label {
  font-size: 0.85rem;
  font-weight: 800;
  min-width: 28px;
  text-align: center;
}

.chip-count-label.p1 {
  color: var(--player-1);
}

.chip-count-label.p2 {
  color: var(--player-2);
}

.cell.flip-blink::after {
  animation: flip-blink 0.6s ease-in-out;
}

@keyframes flip-blink {
  0% { opacity: 1; transform: scale(1); }
  25% { opacity: 0.3; transform: scale(0.7); }
  50% { opacity: 0; transform: scale(0.5); }
  75% { opacity: 0.5; transform: scale(0.8); }
  100% { opacity: 1; transform: scale(1); }
}

.cell.flip-placed::after {
  animation: flip-placed 0.5s ease-out;
}

@keyframes flip-placed {
  0% { opacity: 0; transform: scale(0); }
  50% { opacity: 1; transform: scale(1.2); }
  100% { opacity: 1; transform: scale(1); }
}

.board-shell {
  --cell-size: clamp(24px, 8.2vw, 52px);
  --fit-cols: 9;
  position: relative;
  width: 100%;
  margin-top: 12px;
  padding: 2px 4px 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  user-select: none;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-size));
  grid-template-rows: repeat(var(--rows), var(--cell-size));
  gap: 6px;
  background: linear-gradient(160deg, #194165 0%, #0e2640 100%);
  padding: 10px;
  border-radius: 16px;
  box-shadow: inset 0 4px 0 rgba(255, 255, 255, 0.08), 0 18px 30px rgba(8, 16, 30, 0.35);
  width: max-content;
  margin: 0 auto;
  position: relative;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #f8fafc 0%, #e2e8f0 45%, #c7d2e0 100%);
  position: relative;
  overflow: hidden;
  border: none;
  padding: 0;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
}

.cell.p1::after,
.cell.p2::after {
  content: "";
  position: absolute;
  top: 3px;
  right: 3px;
  bottom: 3px;
  left: 3px;
  border-radius: 50%;
  transform: scale(0.5);
  animation: pop-in 0.25s ease forwards;
  z-index: 2;
}

.cell.p1::after {
  background: radial-gradient(circle at 32% 30%, #ff9ab0 0%, #ff4d6d 48%, #cb2f52 100%);
}

.cell.p2::after {
  background: radial-gradient(circle at 32% 30%, #9ae6b4 0%, #22c55e 48%, #15803d 100%);
}

.cell.clickable {
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.cell.clickable:hover {
  transform: scale(1.06);
  filter: brightness(1.04);
}

.cell:disabled {
  cursor: default;
}

.cell.valid-cell {
  box-shadow: inset 0 0 0 3px rgba(34, 197, 94, 0.5);
}

.cell.win-cell::before {
  content: "";
  position: absolute;
  top: 1px;
  right: 1px;
  bottom: 1px;
  left: 1px;
  border-radius: 50%;
  border: 3px solid #ffe066;
  box-shadow: 0 0 14px rgba(255, 224, 102, 0.85);
  animation: win-ring 0.8s ease-in-out infinite;
  z-index: 3;
}

.win-line {
  position: absolute;
  height: 8px;
  border-radius: 999px;
  background: linear-gradient(90deg, #fff6bf 0%, #ffe066 35%, #ffb703 100%);
  box-shadow: 0 0 16px rgba(255, 217, 102, 0.95);
  transform-origin: left center;
  animation: draw-line 0.45s ease-out forwards;
  pointer-events: none;
  z-index: 4;
}

.pulse {
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(239, 71, 111, 0.4);
  }
  50% {
    box-shadow: 0 0 0 10px rgba(239, 71, 111, 0);
  }
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: scale(0.3);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes draw-line {
  from {
    opacity: 0;
    width: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes win-ring {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
}

@media (max-width: 780px) {
  .page {
    padding-top: max(18px, calc(env(safe-area-inset-top) + 8px));
    padding-right: max(14px, env(safe-area-inset-right));
    padding-bottom: 24px;
    padding-left: max(14px, env(safe-area-inset-left));
  }

  .card {
    padding: 16px;
  }

  .board-shell {
    --cell-size: max(20px, min(34px, calc((100vw - 118px) / var(--fit-cols))));
    padding: 2px 3px 8px;
  }

  .board-grid {
    gap: 4px;
    padding: 9px;
  }

  .join-grid {
    grid-template-columns: 1fr;
  }

  .join-grid .btn {
    width: 100%;
  }

  .lobby-options {
    grid-template-columns: 1fr;
  }

  .mode-picker {
    grid-template-columns: 1fr;
  }

  .top-row {
    flex-direction: column;
    align-items: stretch;
  }

  .top-row .btn {
    width: 100%;
  }

  .top-actions {
    flex-direction: column;
    width: 100%;
  }
}

@media (max-width: 420px) {
  .page {
    padding-top: max(16px, calc(env(safe-area-inset-top) + 6px));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-left: max(12px, env(safe-area-inset-left));
  }

  .board-shell {
    --cell-size: max(18px, min(30px, calc((100vw - 104px) / var(--fit-cols))));
    padding: 2px 2px 8px;
  }

  .board-grid {
    gap: 3px;
    padding: 8px;
  }
}
