:root {
  --bg: #130d26;
  --surface: #1e1638;
  --surface-2: #2d2250;
  --text: #f8fafc;
  --muted: #a39bbb;
  --accent: #f43f5e;
  --gold: #fbbf24;
  --green: #10b981;
  --cyan: #38bdf8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  background: radial-gradient(circle at 50% 0%, #2e1065 0%, #130d26 72%);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 100;
}

/* Topbar */
.topbar {
  display: flex;
  flex-direction: column;
  background: rgba(19, 13, 38, 0.92);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(12px);
  z-index: 20;
  flex-shrink: 0;
}

.topbar-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
}

.room-quick-bar,
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-room-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(30, 22, 56, 0.95);
  border: 1px solid rgba(251, 191, 36, 0.45);
  color: var(--gold);
  padding: 5px 11px;
  border-radius: 99px;
  font-size: 0.78rem;
  font-weight: 900;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.14s ease;
}

.btn-room-chip:hover {
  transform: translateY(-1px);
  border-color: var(--gold);
}

.btn-drawer-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--surface-2);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 9px;
  font-size: 0.78rem;
  font-weight: 800;
  cursor: pointer;
}

.btn-drawer-toggle[aria-expanded='true'] {
  background: rgba(244, 63, 94, 0.22);
  border-color: rgba(244, 63, 94, 0.55);
  color: #fda4af;
}

.room-drawer {
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(15, 10, 30, 0.96);
  padding: 10px 14px;
  animation: pop-in 0.18s ease;
}

.room-drawer[hidden] {
  display: none !important;
}

.drawer-grid {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  max-width: 1150px;
  margin: 0 auto;
}

.player-name-box,
.room-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(30, 22, 56, 0.9);
  padding: 5px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-size: 0.76rem;
  color: var(--muted);
  font-weight: 700;
}

#player-name-input {
  width: 115px;
  background: rgba(19, 13, 38, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-weight: 700;
  font-size: 0.82rem;
  padding: 5px 8px;
  border-radius: 6px;
}

#room-code-input {
  width: 92px;
  background: rgba(19, 13, 38, 0.9);
  border: 1px solid rgba(244, 63, 94, 0.4);
  color: var(--gold);
  font-weight: 900;
  font-size: 0.82rem;
  text-transform: uppercase;
  padding: 5px 8px;
  border-radius: 6px;
}

.p2p-badge {
  background: rgba(16, 185, 129, 0.15);
  border: 1px solid rgba(16, 185, 129, 0.35);
  color: #6ee7b7;
  padding: 5px 10px;
  border-radius: 99px;
  font-size: 0.74rem;
  font-weight: 800;
  white-space: nowrap;
}

.host-settings {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.host-settings select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  padding: 6px 9px;
  font-size: 0.78rem;
  font-weight: 700;
}

.btn-subtle,
.btn-primary-sm,
.btn-icon {
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 800;
  font-size: 0.76rem;
  transition: transform 0.14s ease;
}

.btn-subtle {
  background: var(--surface-2);
  color: var(--text);
  padding: 6px 10px;
}

.btn-primary-sm {
  background: linear-gradient(135deg, #f43f5e, #e11d48);
  color: #fff;
  padding: 6px 11px;
}

.btn-icon {
  background: var(--surface);
  color: var(--text);
  width: 34px;
  height: 34px;
  border: 1px solid rgba(255, 255, 255, 0.16);
}

/* Toasts */
.toast-container {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 95;
  pointer-events: none;
}

.toast {
  background: #fff;
  color: #130d26;
  padding: 8px 16px;
  border-radius: 99px;
  font-weight: 900;
  font-size: 0.85rem;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  animation: pop-in 0.22s ease;
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: translateY(-10px) scale(0.9);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Main Arena */
.game-arena {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 290px 1fr;
  gap: 14px;
  padding: 12px 16px;
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
  overflow-y: auto;
}

@media (max-width: 820px) {
  .game-arena {
    grid-template-columns: 1fr;
  }
}

/* Players Ring */
.players-section {
  background: rgba(30, 22, 56, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.section-header {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.section-header h2 {
  font-size: 0.88rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.level-badge {
  align-self: flex-start;
  padding: 4px 10px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.05em;
}

.level-badge.level-1 {
  background: rgba(16, 185, 129, 0.2);
  color: #6ee7b7;
  border: 1px solid rgba(16, 185, 129, 0.4);
}

.level-badge.level-2 {
  background: rgba(245, 158, 11, 0.22);
  color: #fde047;
  border: 1px solid rgba(245, 158, 11, 0.5);
  animation: pulse-badge 1.2s infinite alternate;
}

.level-badge.level-3 {
  background: rgba(244, 63, 94, 0.25);
  color: #fda4af;
  border: 1px solid rgba(244, 63, 94, 0.55);
  animation: pulse-badge 0.8s infinite alternate;
}

@keyframes pulse-badge {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(1.04);
  }
}

.players-ring {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: rgba(19, 13, 38, 0.75);
  border: 2px solid rgba(255, 255, 255, 0.1);
  padding: 10px 12px;
  border-radius: 12px;
  transition: all 0.22s ease;
}

.player-card.active-turn {
  border-color: var(--gold);
  background: rgba(251, 191, 36, 0.12);
  box-shadow: 0 0 20px rgba(251, 191, 36, 0.22);
  transform: translateX(4px);
}

.player-card.eliminated {
  opacity: 0.42;
  filter: grayscale(0.8);
}

.player-card.exploded {
  animation: explode-shake 0.5s ease;
  border-color: var(--accent);
  background: rgba(244, 63, 94, 0.28);
}

@keyframes explode-shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20%,
  60% {
    transform: translateX(-8px) rotate(-2deg);
  }
  40%,
  80% {
    transform: translateX(8px) rotate(2deg);
  }
}

.player-left {
  display: flex;
  align-items: center;
  gap: 9px;
}

.avatar-dot {
  width: 34px;
  height: 34px;
  border-radius: 99px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 0.9rem;
  color: #fff;
  flex-shrink: 0;
}

.player-meta strong {
  display: block;
  font-size: 0.88rem;
}

.player-meta small {
  font-size: 0.74rem;
  color: var(--muted);
}

.player-right {
  text-align: right;
}

.player-hearts {
  font-size: 0.85rem;
  letter-spacing: 1px;
}

.player-pts {
  display: block;
  font-size: 0.74rem;
  color: var(--cyan);
  font-weight: 800;
}

/* Center Stage */
.center-stage {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.turn-banner {
  background: rgba(30, 22, 56, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 14px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.timer-bar-track {
  height: 10px;
  background: rgba(19, 13, 38, 0.9);
  border-radius: 99px;
  overflow: hidden;
}

.timer-bar-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #10b981, #fbbf24, #f43f5e);
  transition: width 0.1s linear;
}

.turn-info-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.92rem;
  font-weight: 700;
}

.timer-seconds {
  font-size: 1.15rem;
  color: var(--gold);
  font-variant-numeric: tabular-nums;
}

/* 4 Random Starting Letters Picker */
.starter-letters-box {
  background: rgba(30, 22, 56, 0.85);
  border: 2px dashed rgba(251, 191, 36, 0.5);
  border-radius: 16px;
  padding: 14px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: pop-in 0.25s ease;
}

.starter-letters-box[hidden],
.prefix-challenge-box[hidden],
.live-typing-preview[hidden],
.locked-prefix-tag[hidden] {
  display: none;
}

.starter-prompt {
  font-size: 0.88rem;
  color: #fde68a;
}

.starter-letters-grid {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.starter-letter-btn {
  width: 62px;
  height: 62px;
  border-radius: 14px;
  border: 2px solid rgba(251, 191, 36, 0.5);
  background: rgba(19, 13, 38, 0.9);
  color: #fff;
  font-size: 1.75rem;
  font-weight: 900;
  cursor: pointer;
  transition: all 0.16s ease;
}

.starter-letter-btn:hover,
.starter-letter-btn.selected {
  background: var(--gold);
  color: #130d26;
  border-color: #fff;
  transform: translateY(-3px) scale(1.06);
  box-shadow: 0 8px 22px rgba(251, 191, 36, 0.4);
}

/* Prefix Challenge Box */
.prefix-challenge-box {
  background: rgba(30, 22, 56, 0.85);
  border: 1px solid rgba(244, 63, 94, 0.35);
  border-radius: 16px;
  padding: 14px 18px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  animation: pop-in 0.22s ease;
}

.prev-word-display span,
.prefix-requirement span {
  display: block;
  font-size: 0.76rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.prev-word-display strong {
  font-size: 1.65rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.prev-word-display strong em {
  font-style: normal;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 4px;
}

.prefix-requirement {
  display: flex;
  align-items: center;
  gap: 10px;
}

.required-prefix-pill {
  background: linear-gradient(135deg, #f43f5e, #e11d48);
  color: #fff;
  padding: 6px 16px;
  border-radius: 12px;
  font-size: 1.5rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  box-shadow: 0 6px 18px rgba(244, 63, 94, 0.4);
}

.possible-count {
  font-size: 0.76rem;
  color: #6ee7b7;
  font-weight: 700;
}

/* Live Word Input */
.live-word-card {
  background: rgba(30, 22, 56, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 16px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.live-typing-preview {
  background: rgba(19, 13, 38, 0.8);
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 0.88rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

.live-typing-preview strong {
  color: var(--gold);
  font-size: 1.1rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.word-form {
  display: flex;
  gap: 8px;
}

.prefix-input-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  background: rgba(19, 13, 38, 0.95);
  border: 2px solid rgba(56, 189, 248, 0.45);
  border-radius: 12px;
  padding: 4px 10px;
  transition: border-color 0.2s ease;
}

.prefix-input-wrapper.shake {
  animation: explode-shake 0.38s ease;
  border-color: var(--accent);
}

.locked-prefix-tag {
  background: var(--accent);
  color: #fff;
  font-weight: 900;
  font-size: 1.25rem;
  padding: 4px 10px;
  border-radius: 8px;
  margin-right: 6px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

#word-input {
  flex: 1;
  background: transparent;
  border: none;
  color: #fff;
  font-size: 1.3rem;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 8px 4px;
  outline: none;
}

.btn-submit {
  background: linear-gradient(135deg, #10b981, #059669);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 0 20px;
  font-weight: 900;
  font-size: 0.96rem;
  cursor: pointer;
}

.btn-submit:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.match-actions {
  display: flex;
  justify-content: center;
}

.btn-start-match {
  background: linear-gradient(135deg, #fbbf24, #f59e0b);
  color: #130d26;
  border: none;
  padding: 11px 24px;
  border-radius: 12px;
  font-weight: 900;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(245, 158, 11, 0.35);
}

/* Chain History Trail */
.chain-history-box {
  background: rgba(30, 22, 56, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chain-header {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--muted);
}

.escalation-hint {
  color: #fde047;
  font-weight: 700;
}

.chain-trail {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  max-height: 135px;
  overflow-y: auto;
  padding: 4px 0;
}

.chain-pill {
  background: rgba(19, 13, 38, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.18);
  padding: 6px 12px;
  border-radius: 99px;
  font-weight: 800;
  font-size: 0.85rem;
  text-transform: uppercase;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  animation: pop-in 0.24s ease;
}

.chain-pill .link-highlight {
  color: var(--accent);
  font-weight: 900;
}

.chain-pill small {
  color: var(--muted);
  font-size: 0.7rem;
  text-transform: none;
}

.chain-empty {
  font-size: 0.82rem;
  color: var(--muted);
  font-style: italic;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(9, 5, 20, 0.82);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 90;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-card {
  background: #1e1638;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 18px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-badge {
  align-self: center;
  background: rgba(244, 63, 94, 0.2);
  color: #fda4af;
  padding: 4px 12px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 900;
}

.winner-stats {
  background: rgba(19, 13, 38, 0.75);
  padding: 14px;
  border-radius: 12px;
  font-weight: 800;
  color: #fde047;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn-close-text {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 6px;
  cursor: pointer;
}

/* ==================== MOBILE OPTIMIZATIONS ==================== */
@media (max-width: 720px) {
  .topbar-main {
    padding: 6px 10px;
  }

  .p2p-badge {
    font-size: 0.68rem;
    padding: 4px 8px;
    max-width: 145px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .btn-room-chip {
    font-size: 0.72rem;
    padding: 4px 9px;
  }

  .btn-drawer-toggle {
    font-size: 0.72rem;
    padding: 5px 9px;
  }

  .player-name-box,
  .room-controls,
  .host-settings {
    width: 100%;
    justify-content: space-between;
  }

  #player-name-input,
  #room-code-input {
    flex: 1;
    font-size: 16px; /* Prevents iOS zoom on focus */
  }

  .host-settings select {
    flex: 1;
    min-width: 0;
    font-size: 0.75rem;
  }

  .game-arena {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px 10px 14px;
  }

  .players-section {
    padding: 8px 10px;
    border-radius: 12px;
    gap: 6px;
    flex-shrink: 0;
  }

  .section-header {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .section-header h2 {
    font-size: 0.74rem;
  }

  .level-badge {
    font-size: 0.65rem;
    padding: 3px 8px;
  }

  .players-ring {
    flex-direction: row;
    overflow-x: auto;
    gap: 6px;
    padding-bottom: 2px;
    scrollbar-width: none;
  }

  .players-ring::-webkit-scrollbar {
    display: none;
  }

  .player-card {
    flex: 0 0 auto;
    min-width: 138px;
    padding: 6px 9px;
    border-radius: 10px;
    gap: 6px;
  }

  .player-card.active-turn {
    transform: none;
  }

  .avatar-dot {
    width: 26px;
    height: 26px;
    font-size: 0.75rem;
  }

  .player-meta strong {
    font-size: 0.76rem;
    max-width: 82px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .player-meta small {
    font-size: 0.65rem;
  }

  .player-hearts {
    font-size: 0.7rem;
  }

  .player-pts {
    font-size: 0.68rem;
  }

  .center-stage {
    gap: 8px;
  }

  .turn-banner {
    padding: 8px 12px;
    border-radius: 12px;
    gap: 5px;
  }

  .turn-info-row {
    font-size: 0.8rem;
  }

  .timer-seconds {
    font-size: 1rem;
  }

  .starter-letters-box {
    padding: 10px;
    gap: 8px;
  }

  .starter-prompt {
    font-size: 0.78rem;
  }

  .starter-letter-btn {
    width: 50px;
    height: 50px;
    font-size: 1.4rem;
    border-radius: 11px;
  }

  .prefix-challenge-box {
    padding: 9px 12px;
    border-radius: 12px;
    gap: 8px;
  }

  .prev-word-display strong {
    font-size: 1.25rem;
  }

  .required-prefix-pill {
    font-size: 1.2rem;
    padding: 4px 12px;
    border-radius: 9px;
  }

  .live-word-card {
    padding: 10px;
    border-radius: 12px;
    gap: 8px;
  }

  #word-input {
    font-size: 1.05rem;
    padding: 6px 4px;
    min-width: 0;
  }

  .locked-prefix-tag {
    font-size: 1rem;
    padding: 3px 8px;
  }

  .btn-submit {
    padding: 0 14px;
    font-size: 0.85rem;
  }

  .chain-history-box {
    padding: 9px 11px;
    border-radius: 12px;
  }

  .chain-trail {
    max-height: 85px;
    gap: 5px;
  }

  .chain-pill {
    padding: 4px 9px;
    font-size: 0.75rem;
  }
}
