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

:root {
  --bg: #0f0f13;
  --surface: #1a1a24;
  --border: #2a2a3a;
  --text: #f0f0f5;
  --muted: #707088;
  --x-color: #ff6b6b;
  --o-color: #4ecdc4;
  --accent: #7c6af7;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.screen { width: 100%; }
.hidden { display: none !important; }

/* ---- Lobby ---- */
.lobby-container {
  max-width: 360px;
  margin: 0 auto;
  padding: 2.5rem 1.75rem;
}

#lobby-default {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.1rem;
}

#matchmaking-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  padding: 2rem 0 1rem;
}

h1 {
  font-size: 2.6rem;
  font-weight: 900;
  letter-spacing: -1.5px;
  background: linear-gradient(135deg, var(--x-color) 0%, var(--o-color) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: #606075;
  font-size: 0.95rem;
  margin-top: -0.5rem;
  margin-bottom: 0.4rem;
}

/* ---- Buttons ---- */
.btn {
  padding: 0.75rem 1.5rem;
  border-radius: 12px;
  border: none;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
  width: 100%;
}
.btn:active { transform: scale(0.96); }

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { opacity: 0.88; }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: #444455; }

.btn-ghost {
  background: transparent;
  color: #606075;
  border: 1px solid var(--border);
}
.btn-ghost:hover { color: var(--text); }

.btn-quickplay {
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
  color: #fff;
  font-weight: 700;
}
.btn-quickplay:hover { opacity: 0.88; }

.btn-small {
  width: auto;
  padding: 0.45rem 1.1rem;
  font-size: 0.85rem;
  margin-top: 0.5rem;
}

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

/* ---- Divider ---- */
.divider {
  width: 100%;
  text-align: center;
  position: relative;
  color: #404055;
  font-size: 0.8rem;
}
.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0; right: 0;
  height: 1px;
  background: var(--border);
}
.divider span {
  background: var(--bg);
  position: relative;
  padding: 0 0.75rem;
}

/* ---- Join form ---- */
.join-form {
  display: flex;
  gap: 0.6rem;
  width: 100%;
}

.join-form input {
  flex: 1;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 1rem;
  font-weight: 800;
  letter-spacing: 4px;
  text-transform: uppercase;
  outline: none;
  transition: border-color 0.2s;
}
.join-form input:focus { border-color: var(--accent); }
.join-form input::placeholder {
  letter-spacing: 2px;
  font-weight: 400;
  color: #404055;
  font-size: 0.75rem;
  text-transform: uppercase;
}

.join-form .btn {
  width: auto;
  flex-shrink: 0;
  padding: 0.75rem 1.25rem;
}

.error-msg {
  color: var(--x-color);
  font-size: 0.875rem;
  text-align: center;
}

.quickplay-hint {
  color: #404055;
  font-size: 0.8rem;
  text-align: center;
  margin-top: -0.5rem;
}

/* ---- Loading screen ---- */
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
}

.loading-label {
  font-size: 1rem;
  font-weight: 600;
  color: var(--muted);
}

/* ---- Matchmaking ---- */
.spinner {
  width: 52px;
  height: 52px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.matchmaking-label {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
}

.matchmaking-hint {
  color: #404055;
  font-size: 0.85rem;
  margin-top: -0.75rem;
}

/* ---- Game Screen ---- */
.game-container {
  max-width: 400px;
  margin: 0 auto;
  padding: 1.5rem 1.5rem 2.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
}

.game-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.room-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.room-label {
  color: #404055;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.room-code {
  font-weight: 900;
  font-size: 1.05rem;
  letter-spacing: 3px;
  color: var(--text);
}

.copy-btn {
  background: none;
  border: none;
  color: #404055;
  cursor: pointer;
  padding: 5px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  transition: color 0.15s, background 0.15s;
}
.copy-btn:hover {
  color: var(--text);
  background: var(--surface);
}
.copy-btn.copied { color: var(--o-color); }

.player-badge {
  padding: 0.3rem 0.85rem;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.player-badge.x { background: rgba(255,107,107,0.12); color: var(--x-color); }
.player-badge.o { background: rgba(78,205,196,0.12); color: var(--o-color); }

/* ---- Status bar ---- */
.status-bar {
  font-size: 1rem;
  font-weight: 600;
  color: #404055;
  text-align: center;
  min-height: 1.5em;
  transition: color 0.3s;
}
.status-bar.status-in {
  animation: status-slide-in 0.22s ease forwards;
}
@keyframes status-slide-in {
  from { opacity: 0; transform: translateY(7px); }
  to   { opacity: 1; transform: translateY(0); }
}
.status-bar.active { color: var(--text); }
.status-bar.win { color: #ffd700; }
.status-bar.draw { color: #888899; }
.status-bar.lose { color: #404055; }

/* ---- Turn timer ---- */
.turn-timer {
  font-size: 3.5rem;
  font-weight: 900;
  color: var(--text);
  line-height: 1;
  min-height: 4rem;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.5rem;
  transition: color 0.15s;
}
.turn-timer.urgent { color: var(--x-color); }
.timer-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.3px;
}

/* ---- Board ---- */
.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(330px, 88vw);
  position: relative;
}

.win-line-svg {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
}

.win-line-el {
  stroke-width: 3.5;
  stroke-linecap: round;
  stroke-dasharray: 130;
  stroke-dashoffset: 130;
  transition: stroke-dashoffset 0.45s ease 0.25s;
}

.cell {
  aspect-ratio: 1;
  background: var(--surface);
  border-radius: 16px;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.8rem, 8vw, 2.6rem);
  font-weight: 900;
  cursor: default;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  user-select: none;
  color: transparent;
}

.cell.clickable {
  cursor: pointer;
}
.cell.clickable:hover {
  background: #1e1e2c;
  border-color: #3a3a50;
}
.cell.clickable:active {
  transform: scale(0.93);
}

.cell.x { color: var(--x-color); }
.cell.o { color: var(--o-color); }

/* ---- X/O SVG draw animation ---- */
.cell-svg {
  width: 62%;
  height: 62%;
  overflow: visible;
}
.x-line {
  fill: none;
  stroke: var(--x-color);
  stroke-width: 9;
  stroke-linecap: round;
  stroke-dasharray: 91;
  stroke-dashoffset: 91;
}
.x-line-2 {
  stroke-dasharray: 91;
  stroke-dashoffset: 91;
}
.cell-svg.draw .x-line   { animation: draw-stroke 0.26s ease forwards; }
.cell-svg.draw .x-line-2 { animation: draw-stroke 0.26s ease 0.12s forwards; }
.o-circle {
  fill: none;
  stroke: var(--o-color);
  stroke-width: 9;
  stroke-linecap: round;
  stroke-dasharray: 202;
  stroke-dashoffset: 202;
}
.cell-svg.draw .o-circle { animation: draw-stroke 0.38s ease forwards; }
@keyframes draw-stroke { to { stroke-dashoffset: 0; } }

.cell.winner {
  border-color: #ffd700;
  background: rgba(255, 215, 0, 0.07);
  animation: pulse-win 0.5s ease forwards;
}

@keyframes pulse-win {
  0%   { transform: scale(1); }
  35%  { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.cell.pop {
  animation: pop-in 0.22s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

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

/* ---- Rematch toast ---- */
.rematch-toast {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.8rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.rematch-toast-btns {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.rematch-toast .btn-small {
  margin-top: 0;
  width: auto;
}

/* ---- Game actions ---- */
.game-actions {
  display: flex;
  gap: 0.75rem;
  width: 100%;
}
.game-actions .btn {
  flex: 1;
  width: auto;
}

/* ---- Auth bar ---- */
.auth-bar {
  position: fixed; top: 1rem; right: 1rem; z-index: 100;
  display: flex; align-items: center; gap: 0.75rem;
}
.btn-google {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.5rem 1rem; background: #fff; color: #3c4043;
  border: 1px solid #dadce0; border-radius: 10px;
  font-size: 0.875rem; font-weight: 600; cursor: pointer; width: auto;
  transition: box-shadow 0.15s;
}
.btn-google:hover { box-shadow: 0 2px 8px rgba(0,0,0,0.25); }
.user-info {
  display: flex; align-items: center; gap: 0.5rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 0.35rem 0.75rem;
}
.user-avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
.user-name {
  font-size: 0.82rem; font-weight: 600; color: var(--text);
  max-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.btn-signout { background: none; border: none; color: var(--muted); font-size: 0.75rem; cursor: pointer; }
.btn-signout:hover { color: var(--text); }

/* ---- Auth bar stats ---- */
.auth-stats { display: flex; gap: 0.5rem; font-size: 0.75rem; font-weight: 700; }
.auth-stats #stat-wins   { color: #ffd700; }
.auth-stats #stat-losses { color: var(--x-color); }
.auth-stats #stat-draws  { color: var(--muted); }

/* ---- Mode picker ---- */
.mode-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  width: 100%;
}

.mode-btn {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 0.55rem 0.65rem;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  line-height: 1.2;
  width: 100%;
}
.mode-btn:hover { border-color: #3a3a50; color: var(--text); }
.mode-btn.active {
  border-color: var(--accent);
  color: var(--text);
  background: rgba(124, 106, 247, 0.1);
}
.mode-tag {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--muted);
  opacity: 0.85;
}
.mode-btn.active .mode-tag { color: var(--accent); opacity: 1; }
.mode-btn.wide { grid-column: 1 / -1; }

/* ---- Game header right side ---- */
.header-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* ---- Mode badge in game ---- */
.mode-badge {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.22rem 0.6rem;
  background: rgba(124, 106, 247, 0.12);
  color: var(--accent);
  border-radius: 20px;
  letter-spacing: 0.3px;
}

/* ---- Modal overlay ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  backdrop-filter: blur(3px);
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 2rem 1.75rem;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.modal-wide { max-width: 420px; }

.modal-title {
  font-size: 1.35rem;
  font-weight: 900;
  letter-spacing: -0.5px;
  color: var(--text);
}

.modal-hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin-top: -0.5rem;
}

.modal-input {
  padding: 0.75rem 1rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 1px;
  outline: none;
  transition: border-color 0.2s;
  width: 100%;
}
.modal-input:focus { border-color: var(--accent); }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
}
.modal-close:hover { color: var(--text); background: var(--bg); }

/* ---- Mode tabs (in leaderboard modal) ---- */
.mode-tabs {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.tab-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.32rem 0.75rem;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---- Leaderboard ---- */
.leaderboard-content {
  min-height: 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.leaderboard-table th {
  color: var(--muted);
  font-weight: 600;
  text-align: left;
  padding: 0.35rem 0.5rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.leaderboard-table td {
  padding: 0.55rem 0.5rem;
  border-bottom: 1px solid rgba(42, 42, 58, 0.5);
  color: var(--text);
}
.leaderboard-table tr:last-child td { border-bottom: none; }
.lb-rank { color: var(--muted); font-weight: 700; width: 2rem; }
.lb-rank.gold   { color: #ffd700; }
.lb-rank.silver { color: #c0c0c0; }
.lb-rank.bronze { color: #cd7f32; }
.lb-name { font-weight: 700; }
.lb-wins   { color: #ffd700; font-weight: 700; text-align: right; }
.lb-losses { color: var(--x-color); text-align: right; }
.lb-draws  { color: var(--muted); text-align: right; }
.lb-empty {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 2rem 0;
  width: 100%;
}
.lb-me { background: rgba(124, 106, 247, 0.08); border-radius: 6px; }

/* ---- 6×6 board adjustments ---- */
.board.big {
  width: min(480px, 94vw) !important;
  gap: 7px;
}
.board.big .cell {
  border-radius: 10px;
}
.board.big .cell-svg {
  width: 55%;
  height: 55%;
}

/* ---- Avatar upload ---- */
.avatar-wrap {
  position: relative; width: 28px; height: 28px; cursor: pointer;
  display: block; flex-shrink: 0;
}
.avatar-wrap .user-avatar {
  width: 28px; height: 28px;
  transition: opacity 0.15s;
}
.avatar-wrap:hover .user-avatar { opacity: 0.7; }
.avatar-edit {
  position: absolute; bottom: -2px; right: -3px;
  width: 13px; height: 13px;
  background: var(--accent); border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 7px; line-height: 1; pointer-events: none;
  border: 1.5px solid var(--bg);
  transition: transform 0.15s;
}
.avatar-wrap:hover .avatar-edit { transform: scale(1.15); }

/* ---- Custom symbol image in cells ---- */
.cell-img { width: 68%; height: 68%; object-fit: cover; border-radius: 50%; display: block; }

/* ---- Skip button (username modal) ---- */
.btn-skip {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0.25rem;
  text-align: center;
  transition: color 0.15s;
}
.btn-skip:hover { color: var(--text); }

/* ---- CPU buttons ---- */
.cpu-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  width: 100%;
}

.btn-cpu {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  font-size: 0.9rem;
}
.btn-cpu:hover {
  color: var(--text);
  border-color: #444455;
}

/* ---- Leaderboard btn spacing ---- */
#leaderboard-btn { margin-top: 0.25rem; }

/* ---- Responsive ---- */
@media (max-width: 400px) {
  .lobby-container, .game-container {
    padding: 1.5rem 1.25rem;
  }
  h1 { font-size: 2.1rem; }
  .mode-btn { font-size: 0.76rem; }
}
