:root {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-card: rgba(30, 41, 59, 0.7);
  --border-color: rgba(255, 255, 255, 0.1);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  
  --color-x: #ef4444;
  --color-x-glow: rgba(239, 68, 68, 0.35);
  --color-o: #3b82f6;
  --color-o-glow: rgba(59, 130, 246, 0.35);

  --board-bg: #e2c08d;
  --board-grid: #8a6231;
  --board-cell-hover: rgba(138, 98, 49, 0.2);
  --cell-size: 38px;
  --radius: 12px;
}

[data-theme="dark"] {
  --board-bg: #1e293b;
  --board-grid: #334155;
  --board-cell-hover: rgba(51, 65, 85, 0.5);
}

[data-theme="neon"] {
  --bg-primary: #0a0a14;
  --bg-secondary: #121226;
  --board-bg: #14142b;
  --board-grid: #00f2fe;
  --board-cell-hover: rgba(0, 242, 254, 0.2);
  --color-x: #ff007f;
  --color-o: #00f2fe;
}

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

body {
  font-family: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* App Header */
.app-header {
  background: var(--bg-card);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-color);
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 50;
}

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

.brand-icon {
  font-size: 24px;
}

.brand h1 {
  font-size: 1.25rem;
  font-weight: 800;
  background: linear-gradient(135deg, #ef4444, #3b82f6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: var(--bg-secondary);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: all 0.2s ease;
  position: relative;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-1px);
}

.icon-btn.muted {
  color: var(--color-x);
}

.badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--color-x);
  color: white;
  font-size: 11px;
  font-weight: bold;
  padding: 2px 6px;
  border-radius: 10px;
}

/* Mode Bar */
.mode-bar {
  display: flex;
  justify-content: center;
  padding: 12px 16px 0;
  max-width: 600px;
  margin: 0 auto;
  width: 100%;
}

.mode-selector {
  display: flex;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 4px;
  border-radius: 30px;
  width: 100%;
}

.mode-tab {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s ease;
}

.mode-tab:hover {
  color: var(--text-main);
}

.mode-tab.active {
  background: #3b82f6;
  color: #fff;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.35);
}

/* Online Banner */
.online-banner {
  background: rgba(59, 130, 246, 0.15);
  border: 1px solid rgba(59, 130, 246, 0.3);
  padding: 8px 16px;
  margin: 10px 16px 0;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  max-width: 700px;
  align-self: center;
  width: calc(100% - 32px);
}

.badge-x {
  color: var(--color-x);
  font-weight: 700;
}

.badge-o {
  color: var(--color-o);
  font-weight: 700;
}

/* Game Info / Scoreboard */
.game-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 600px;
  margin: 14px auto 8px;
  width: 100%;
  padding: 0 16px;
  gap: 12px;
}

.player-card {
  flex: 1;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: all 0.25s ease;
  position: relative;
  overflow: hidden;
}

.player-card.active {
  border-color: #3b82f6;
  box-shadow: 0 0 16px rgba(59, 130, 246, 0.25);
}

.card-x.active {
  border-color: var(--color-x);
  box-shadow: 0 0 16px var(--color-x-glow);
}

.player-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 18px;
  flex-shrink: 0;
}

.card-x .player-avatar {
  background: rgba(239, 68, 68, 0.15);
  color: var(--color-x);
  border: 2px solid var(--color-x);
}

.card-o .player-avatar {
  background: rgba(59, 130, 246, 0.15);
  color: var(--color-o);
  border: 2px solid var(--color-o);
}

.player-details {
  flex: 1;
  min-width: 0;
}

.player-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-score {
  font-size: 18px;
  font-weight: 800;
}

.player-timer {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}

.player-card.active .player-timer {
  color: #fbbf24;
}

/* VS Badge & Turn Center */
.vs-divider {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
}

/* Status Banner */
.status-banner {
  text-align: center;
  margin: 4px 16px 12px;
  font-size: 14px;
  font-weight: 600;
  min-height: 24px;
}

.win-banner {
  color: #10b981;
  font-weight: 800;
  font-size: 16px;
  animation: pulse 1s infinite alternate;
}

.draw-banner {
  color: #fbbf24;
  font-weight: 800;
}

/* Board Area */
.board-container {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 8px 12px;
  overflow: auto;
}

.board-wrapper {
  background: var(--board-bg);
  padding: 10px;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4), inset 0 0 10px rgba(0, 0, 0, 0.2);
  border: 2px solid var(--board-grid);
  position: relative;
  max-width: 95vw;
  max-height: 65vh;
  overflow: auto;
  user-select: none;
}

.board {
  display: grid;
  gap: 1px;
  background-color: var(--board-grid);
  border: 1px solid var(--board-grid);
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background-color: var(--board-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  transition: background-color 0.15s ease;
}

.cell:hover {
  background-color: var(--board-cell-hover);
}

.cell.last-move::after {
  content: '';
  position: absolute;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #fbbf24;
  top: 4px;
  right: 4px;
  box-shadow: 0 0 6px #fbbf24;
}

.cell.win-cell {
  background-color: rgba(16, 185, 129, 0.35) !important;
  animation: winGlow 1s infinite alternate;
}

@keyframes winGlow {
  0% { box-shadow: inset 0 0 8px #10b981; }
  100% { box-shadow: inset 0 0 18px #10b981; }
}

.piece-svg {
  width: 78%;
  height: 78%;
  animation: popIn 0.18s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.piece-x {
  color: var(--color-x);
}

.piece-o {
  color: var(--color-o);
}

@keyframes popIn {
  0% { transform: scale(0.3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* Bottom Controls */
.bottom-controls {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 12px 16px 20px;
  max-width: 500px;
  margin: 0 auto;
  width: 100%;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: all 0.2s ease;
}

.btn-primary {
  background: #3b82f6;
  color: white;
}

.btn-primary:hover {
  background: #2563eb;
}

.btn-secondary {
  background: var(--bg-secondary);
  color: var(--text-main);
  border-color: var(--border-color);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.1);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

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

/* Modals */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal.show {
  display: flex;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
}

.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  width: 100%;
  max-width: 460px;
  position: relative;
  z-index: 10;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  animation: slideUp 0.25s ease-out;
}

@keyframes slideUp {
  0% { transform: translateY(20px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h2 {
  font-size: 1.15rem;
  font-weight: 700;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.form-control {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}

.form-control:focus {
  border-color: #3b82f6;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 14px;
}

.checkbox-label input {
  width: 18px;
  height: 18px;
  accent-color: #3b82f6;
}

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* Online Modal specific */
.online-divider {
  text-align: center;
  position: relative;
  font-size: 12px;
  color: var(--text-muted);
  margin: 6px 0;
}

.online-divider::before, .online-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 42%;
  height: 1px;
  background: var(--border-color);
}
.online-divider::before { left: 0; }
.online-divider::after { right: 0; }

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

.join-row input {
  flex: 1;
}

.room-created-box {
  background: var(--bg-primary);
  padding: 14px;
  border-radius: 10px;
  border: 1px solid rgba(59, 130, 246, 0.4);
}

.copy-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

/* Chat Drawer */
.chat-drawer {
  position: fixed;
  bottom: 80px;
  right: 20px;
  width: 320px;
  height: 420px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.4);
  display: none;
  flex-direction: column;
  z-index: 80;
  overflow: hidden;
}

.chat-drawer.open {
  display: flex;
}

.chat-header {
  padding: 10px 14px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  font-weight: 700;
}

.chat-messages {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-msg {
  padding: 8px 12px;
  border-radius: 12px;
  max-width: 85%;
  font-size: 13px;
}

.msg-self {
  align-self: flex-end;
  background: #3b82f6;
  color: white;
  border-bottom-right-radius: 2px;
}

.msg-other {
  align-self: flex-start;
  background: var(--bg-primary);
  color: var(--text-main);
  border-bottom-left-radius: 2px;
}

.msg-sender {
  font-size: 10px;
  opacity: 0.7;
  margin-bottom: 2px;
}

.chat-chips {
  display: flex;
  gap: 6px;
  padding: 6px 12px;
  overflow-x: auto;
  background: var(--bg-primary);
  border-top: 1px solid var(--border-color);
}

.chat-chip {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 11px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--text-muted);
}

.chat-chip:hover {
  color: var(--text-main);
  border-color: #3b82f6;
}

.chat-input-row {
  display: flex;
  padding: 8px;
  gap: 6px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
}

.chat-input-row input {
  flex: 1;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-primary);
  color: var(--text-main);
  font-size: 13px;
  outline: none;
}

/* Toast notifications */
.toast-container {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid var(--border-color);
  backdrop-filter: blur(8px);
  color: #fff;
  padding: 10px 18px;
  border-radius: 30px;
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: toastIn 0.25s ease-out;
  pointer-events: auto;
}

.toast-success i { color: #10b981; }
.toast-error i { color: #ef4444; }
.toast-warning i { color: #fbbf24; }
.toast-info i { color: #3b82f6; }

.toast.fade-out {
  opacity: 0;
  transform: translateY(10px);
  transition: all 0.3s ease;
}

@keyframes toastIn {
  0% { transform: translateY(15px); opacity: 0; }
  100% { transform: translateY(0); opacity: 1; }
}

@media (max-width: 600px) {
  :root {
    --cell-size: 32px;
  }
  .player-name {
    font-size: 11px;
  }
  .player-avatar {
    width: 30px;
    height: 30px;
    font-size: 15px;
  }
  .player-score {
    font-size: 15px;
  }
  .chat-drawer {
    right: 10px;
    left: 10px;
    width: auto;
    bottom: 75px;
  }
}
