*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-cell: #252542;
  --bg-said: #2ecc71;
  --bg-said-glow: #27ae60;
  --text: #e0e0e0;
  --text-said: #fff;
  --text-muted: #888;
  --accent: #e94560;
  --accent-hover: #c0392b;
  --border: #333355;
  --radius: 8px;
  --shadow: 0 4px 20px rgba(0,0,0,0.4);
}

:root.light {
  --bg: #f0f2f5;
  --bg-card: #ffffff;
  --bg-cell: #e8eaed;
  --bg-said: #27ae60;
  --bg-said-glow: #2ecc71;
  --text: #1a1a2e;
  --text-said: #fff;
  --text-muted: #666;
  --accent: #e94560;
  --accent-hover: #c0392b;
  --border: #d0d0d0;
  --shadow: 0 4px 20px rgba(0,0,0,0.1);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
}

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  backdrop-filter: blur(4px);
}

.modal-overlay.hidden { display: none; }

.modal-confirm { z-index: 150; }

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  max-width: 400px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}

.modal-wide {
  max-width: 500px;
}

.modal h2 {
  font-size: 1.6rem;
  margin-bottom: 8px;
  color: var(--accent);
}

.modal h3 {
  font-size: 1.2rem;
  margin-bottom: 8px;
}

.modal p {
  color: var(--text-muted);
  margin-bottom: 16px;
  font-size: 0.95rem;
}

.modal input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 12px;
  outline: none;
  transition: border-color 0.2s;
}

.modal input[type="text"]:focus {
  border-color: var(--accent);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.error-text {
  color: var(--accent);
  font-size: 0.85rem;
  min-height: 1.2em;
  margin-top: 4px;
}

/* ---- Buttons ---- */
.btn {
  padding: 10px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  font-weight: 600;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover { background: var(--bg-cell); }

.btn-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}

.btn-close:hover { color: var(--text); }

/* ---- Theme Toggle ---- */
.theme-toggle {
  position: fixed;
  top: 12px;
  right: 12px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  transition: background 0.2s;
  line-height: 1;
}

.theme-toggle:hover { background: var(--bg-cell); }
.theme-toggle:active { transform: scale(0.9); }

/* ---- Header ---- */
header {
  text-align: center;
  padding: 16px 16px 8px;
}

header h1 {
  font-size: 1.4rem;
  color: var(--accent);
}

.player-name {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 2px;
}

/* ---- Board ---- */
.board {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  width: 100%;
  max-width: 500px;
  padding: 8px 12px;
  aspect-ratio: 1;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: var(--bg-cell);
  border-radius: 6px;
  padding: 4px;
  font-size: clamp(0.55rem, 2.5vw, 0.85rem);
  line-height: 1.2;
  word-break: break-word;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background 0.3s, transform 0.15s, box-shadow 0.3s;
  position: relative;
  overflow: hidden;
}

.cell:active {
  transform: scale(0.95);
}

.cell.said {
  background: var(--bg-said);
  color: var(--text-said);
  font-weight: 600;
  box-shadow: 0 0 8px rgba(46, 204, 113, 0.3);
}

.cell.bingo-line {
  animation: pulseGlow 1s ease-in-out infinite;
}

@keyframes pulseGlow {
  0%, 100% { box-shadow: 0 0 8px rgba(46, 204, 113, 0.3); }
  50% { box-shadow: 0 0 20px rgba(46, 204, 113, 0.7); }
}

/* Peek board (read-only, smaller) */
.board-peek {
  max-width: 350px;
  margin: 0 auto;
  pointer-events: none;
}

.board-peek .cell {
  font-size: clamp(0.45rem, 2vw, 0.7rem);
  cursor: default;
}

/* ---- FAB (Players button) ---- */
.fab {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  box-shadow: 0 4px 12px rgba(233, 69, 96, 0.4);
  z-index: 50;
  transition: transform 0.2s, bottom 0.3s ease;
}

.fab:active { transform: scale(0.9); }

.fab-left {
  left: 20px;
  right: auto;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

.fab-left-top {
  left: 20px;
  right: auto;
  bottom: 84px;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

.fab-left-top.mic-active {
  background: var(--accent);
  border-color: var(--accent);
}

/* Shift FABs up when mic bar is visible */
.mic-shifted {
  --fab-offset: 48px;
}

.mic-shifted .fab {
  bottom: calc(20px + var(--fab-offset));
}

.mic-shifted .fab-left-top {
  bottom: calc(84px + var(--fab-offset));
}

.mic-shifted .fab-top-right {
  bottom: calc(84px + var(--fab-offset));
}

.fab-icon { line-height: 1; }

.fab-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 0.7rem;
  font-weight: 700;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--accent);
}

/* ---- Words List Modal ---- */
.modal-tall {
  max-width: 420px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  text-align: left;
}

.all-words-list {
  overflow-y: auto;
  flex: 1;
  padding: 4px 0;
}

.word-list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.word-list-item:last-child { border-bottom: none; }

.word-list-item .word-name {
  flex: 1;
  font-size: 0.9rem;
  word-break: break-word;
}

.word-list-item .word-said-tag {
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--bg-said);
  color: #fff;
}

.word-list-item .btn-mark {
  padding: 4px 12px;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: none;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

.word-list-item .btn-mark:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.word-list-item .btn-unmark {
  padding: 4px 10px;
  font-size: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

.word-list-item .btn-unmark:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---- Mic Mode ---- */
.mic-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 55;
  font-size: 0.85rem;
  animation: fadeInUp 0.25s ease-out;
}

.mic-bar.hidden { display: none; }

.mic-pulse {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  animation: micDot 1.2s ease-in-out infinite;
}

@keyframes micDot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.7); }
}

@keyframes fadeInUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.mic-transcript {
  color: var(--text-muted);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  flex: 1;
}

.mic-toast {
  position: fixed;
  bottom: 52px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 160;
  box-shadow: 0 4px 20px rgba(233, 69, 96, 0.3);
  animation: toastIn 0.3s ease-out;
  max-width: 90vw;
}

.mic-toast.hidden { display: none; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

.mic-toast-text {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.mic-toast-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.btn-sm {
  padding: 5px 12px;
  font-size: 0.8rem;
}

/* ---- Log Panel ---- */
.fab-top-right {
  bottom: 84px;
  background: var(--bg-cell);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

.log-panel {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 320px;
  max-height: 75vh;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  z-index: 60;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.log-panel.hidden { display: none; }

.log-entries {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  min-height: 120px;
  max-height: 50vh;
}

.log-entry {
  padding: 6px 14px;
  font-size: 0.82rem;
  line-height: 1.4;
  border-bottom: 1px solid rgba(51, 51, 85, 0.4);
  animation: logSlideIn 0.25s ease-out;
}

.log-entry:last-child { border-bottom: none; }

.log-entry .log-name {
  font-weight: 700;
  color: var(--accent);
}

.log-entry.log-bingo {
  background: rgba(46, 204, 113, 0.1);
  border-left: 3px solid var(--bg-said);
  padding-left: 11px;
}

.log-entry.log-super-bingo {
  background: linear-gradient(90deg, rgba(233,69,96,0.1), rgba(155,89,182,0.1), rgba(52,152,219,0.1));
  border-left: 3px solid var(--accent);
  padding-left: 11px;
}

.log-entry.log-bingo .log-name,
.log-entry.log-super-bingo .log-name {
  color: var(--bg-said);
}

.log-entry.log-reaction {
  font-size: 0.9rem;
}

.log-entry .log-time {
  color: var(--text-muted);
  font-size: 0.7rem;
  margin-left: 6px;
}

@keyframes logSlideIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

.reactions-bar {
  display: flex;
  gap: 4px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  justify-content: center;
  flex-wrap: wrap;
}

.reaction-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 10px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  font-family: inherit;
}

.reaction-btn:hover {
  background: var(--bg-cell);
}

.reaction-btn:active {
  transform: scale(0.9);
}

/* Floating reaction animation */
.floating-reaction {
  position: fixed;
  font-size: 1.4rem;
  pointer-events: none;
  z-index: 150;
  animation: floatUp 2s ease-out forwards;
  white-space: nowrap;
}

@keyframes floatUp {
  0% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-120px) scale(1.3); }
}

/* ---- Players Panel ---- */
.panel {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  max-height: 70vh;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  z-index: 60;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel.hidden { display: none; }

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.panel-header h3 { font-size: 1rem; }

.players-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

.players-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s;
}

.players-list li:hover {
  background: var(--bg-cell);
}

.player-info {
  display: flex;
  flex-direction: column;
}

.player-list-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.player-score {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.player-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--bg-said);
  flex-shrink: 0;
}

.player-status.offline {
  background: var(--text-muted);
}

/* ---- Bingo Overlay ---- */
.bingo-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.bingo-overlay.hidden { display: none; }

/* Wrapper keeps centering stable while inner text animates */
.bingo-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
}

.bingo-text {
  font-size: clamp(3rem, 15vw, 8rem);
  font-weight: 900;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.1;
  padding: 0 16px;
}

.bingo-text.bingo {
  color: var(--bg-said);
  text-shadow: 0 0 40px rgba(46, 204, 113, 0.6), 0 0 80px rgba(46, 204, 113, 0.3);
  animation: bingoAnim 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
}

.bingo-text.super-bingo {
  color: #fff;
  animation: superBingoEntry 1.2s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
  text-shadow:
    0 0 20px #e74c3c,
    0 0 40px #f39c12,
    0 0 60px #9b59b6,
    0 0 80px #3498db;
}

/* After entry finishes, pulse and glow kick in via JS class */
.bingo-text.super-bingo.animating {
  animation: superBingoPulse 1.5s ease-in-out infinite;
}

.bingo-text.super-bingo.animating {
  animation: superBingoPulse 1.5s ease-in-out infinite,
             superBingoGlow 2s ease-in-out infinite;
}

/* Confetti particles */
.confetti {
  position: fixed;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  z-index: 201;
  pointer-events: none;
  animation: confettiFall linear forwards;
}

@keyframes bingoAnim {
  0% { transform: scale(0.1) rotate(-15deg); opacity: 0; }
  60% { transform: scale(1.15) rotate(3deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

@keyframes superBingoEntry {
  0% { transform: scale(0) rotate(-20deg); opacity: 0; }
  40% { transform: scale(1.3) rotate(5deg); opacity: 1; }
  60% { transform: scale(0.9) rotate(-2deg); opacity: 1; }
  80% { transform: scale(1.05) rotate(1deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

@keyframes superBingoPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.06); }
}

@keyframes superBingoGlow {
  0% { text-shadow: 0 0 20px #e74c3c, 0 0 40px #f39c12, 0 0 60px #9b59b6, 0 0 80px #3498db; }
  25% { text-shadow: 0 0 30px #f39c12, 0 0 50px #2ecc71, 0 0 70px #e74c3c, 0 0 90px #9b59b6; }
  50% { text-shadow: 0 0 30px #9b59b6, 0 0 50px #3498db, 0 0 70px #2ecc71, 0 0 90px #f39c12; }
  75% { text-shadow: 0 0 30px #3498db, 0 0 50px #e74c3c, 0 0 70px #f39c12, 0 0 90px #2ecc71; }
  100% { text-shadow: 0 0 20px #e74c3c, 0 0 40px #f39c12, 0 0 60px #9b59b6, 0 0 80px #3498db; }
}

@keyframes confettiFall {
  0% { transform: translateY(0) rotate(0deg); opacity: 1; }
  100% { transform: translateY(100vh) rotate(720deg); opacity: 0; }
}

/* ---- Connection Status ---- */
.connection-status {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--accent);
  color: #fff;
  text-align: center;
  padding: 6px;
  font-size: 0.85rem;
  z-index: 300;
  animation: fadeInDown 0.3s;
}

.connection-status.hidden { display: none; }

@keyframes fadeInDown {
  from { transform: translateY(-100%); }
  to { transform: translateY(0); }
}

/* ---- Responsive ---- */
@media (max-width: 400px) {
  .board { gap: 3px; padding: 4px 8px; }
  .cell { border-radius: 4px; }
  header h1 { font-size: 1.2rem; }
  .panel { width: 100%; }
  .log-panel { width: 100%; }
}

@media (min-width: 600px) {
  header { padding: 24px 16px 12px; }
  header h1 { font-size: 1.8rem; }
  .board { padding: 12px 0; }
}

/* ---- Utility ---- */
.hidden { display: none !important; }
