:root {
  --bg: #0f172a;
  --card: #1e293b;
  --card-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --x: #38bdf8;
  --o: #f472b6;
  --accent: #22c55e;
  --danger: #ef4444;
  --warn: #f59e0b;
  --radius: 14px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(circle at top, #1e293b 0%, var(--bg) 60%);
  color: var(--text);
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid #ffffff14;
}

h1 { margin: 0; font-size: 1.6rem; letter-spacing: 0.5px; }
h2 { margin: 0 0 8px; font-size: 1.15rem; display: flex; align-items: center; gap: 8px; }
.x { color: var(--x); }
.o { color: var(--o); }

main { max-width: 1000px; margin: 0 auto; padding: 24px 16px; }

.me {
  background: var(--card);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.9rem;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: 0 10px 30px #0006;
}
.narrow { max-width: 420px; margin: 40px auto; }

.grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 760px) { .grid { grid-template-columns: 1fr; } }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.center { text-align: center; }
.error { color: var(--danger); min-height: 1.2em; margin: 10px 0 0; }

.badge {
  background: var(--accent);
  color: #052e16;
  font-size: 0.8rem;
  padding: 2px 9px;
  border-radius: 999px;
}

form { display: flex; gap: 8px; margin-top: 14px; }

input {
  flex: 1;
  min-width: 0;
  padding: 11px 14px;
  border-radius: 10px;
  border: 1px solid #ffffff22;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}
input:focus { outline: 2px solid var(--x); border-color: transparent; }

button {
  padding: 10px 18px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #052e16;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  transition: transform 0.08s, filter 0.15s;
}
button:hover:not(:disabled) { filter: brightness(1.1); }
button:active:not(:disabled) { transform: scale(0.97); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.secondary { background: var(--card-2); color: var(--text); }
button.danger { background: var(--danger); color: #fff; }

/* Player list */
.player-list, .leaderboard, .chat-log { list-style: none; padding: 0; margin: 14px 0 0; }

.player-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  background: var(--card-2);
  border-radius: 10px;
}
.avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700;
  color: #0f172a;
  flex-shrink: 0;
}
.player-info { flex: 1; min-width: 0; }
.player-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-meta { font-size: 0.8rem; color: var(--muted); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.dot.idle { background: var(--accent); }
.dot.invited { background: var(--warn); }
.dot.playing { background: var(--danger); }

.leaderboard li {
  display: flex;
  justify-content: space-between;
  padding: 9px 12px;
  border-radius: 8px;
  margin-bottom: 6px;
  background: var(--card-2);
  font-size: 0.92rem;
}
.leaderboard li .rank { color: var(--muted); width: 26px; display: inline-block; }

/* Game */
.versus { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.pl {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px;
  border-radius: 12px;
  border: 2px solid transparent;
  transition: border-color 0.2s, background 0.2s;
}
.pl.active { border-color: currentColor; background: #ffffff08; }
#px { color: var(--x); }
#po { color: var(--o); }
.pl .nm { color: var(--text); font-weight: 600; text-align: center; word-break: break-word; }
.pl .sc { color: var(--muted); font-size: 0.85rem; }
.mark { font-size: 1.8rem; font-weight: 800; }
.vs { text-align: center; color: var(--muted); font-weight: 700; }

.status { text-align: center; font-size: 1.1rem; font-weight: 600; min-height: 1.5em; margin: 16px 0; }

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(100%, 360px);
  margin: 0 auto;
}
.cell {
  aspect-ratio: 1;
  border-radius: 12px;
  background: var(--card-2);
  font-size: clamp(2.5rem, 12vw, 4rem);
  font-weight: 800;
  display: grid; place-items: center;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s, transform 0.1s;
}
.cell.playable:hover { background: #334155; }
.cell.taken, .cell.locked { cursor: default; }
.cell.X { color: var(--x); }
.cell.O { color: var(--o); }
.cell.pop { animation: pop 0.25s ease-out; }
.cell.win { background: #22c55e33; animation: pulse 0.9s ease-in-out infinite alternate; }
@keyframes pop { from { transform: scale(0.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes pulse { to { background: #22c55e66; } }

.actions { display: flex; justify-content: center; gap: 10px; margin-top: 20px; flex-wrap: wrap; }

/* Chat */
.chat { display: flex; flex-direction: column; height: 100%; min-height: 360px; }
.chat-log {
  flex: 1;
  overflow-y: auto;
  max-height: 380px;
  min-height: 200px;
  background: var(--bg);
  border-radius: 10px;
  padding: 10px;
}
.chat-log li { margin-bottom: 8px; font-size: 0.92rem; line-height: 1.35; word-wrap: break-word; }
.chat-log li .who { font-weight: 700; margin-right: 6px; }
.chat-log li.mine .who { color: var(--accent); }
.chat-log li.sys { color: var(--muted); font-style: italic; font-size: 0.85rem; }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: #000a;
  display: grid; place-items: center;
  z-index: 50;
  padding: 16px;
}
.modal-box {
  background: var(--card);
  border-radius: var(--radius);
  padding: 26px;
  width: min(100%, 380px);
  text-align: center;
  box-shadow: 0 20px 50px #000a;
  animation: pop 0.2s ease-out;
}
.modal-box p { font-size: 1.1rem; margin: 0 0 16px; }
.timer { height: 6px; background: var(--bg); border-radius: 99px; overflow: hidden; }
#modal-timer-bar { height: 100%; width: 100%; background: var(--warn); }

/* Toast + connection */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%);
  background: var(--card-2);
  border: 1px solid #ffffff22;
  padding: 12px 20px;
  border-radius: 10px;
  box-shadow: 0 10px 30px #0008;
  z-index: 60;
  max-width: calc(100% - 32px);
}
.conn {
  position: fixed; top: 0; left: 0; right: 0;
  background: var(--warn);
  color: #1c1917;
  text-align: center;
  padding: 6px;
  font-weight: 600;
  z-index: 70;
}
