/* ─── Reset & Base ──────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0f1117;
  --bg2:      #191c27;
  --bg3:      #1f2333;
  --bg4:      #252a3a;
  --border:   #2e3349;
  --text:     #e8eaf0;
  --text2:    #9aa0b8;
  --text3:    #5f6580;
  --primary:  #4f8ef7;
  --primary2: #3a74e0;
  --success:  #34c47c;
  --danger:   #e85f5f;
  --warning:  #f0a733;
  --gold:     #f5c542;
  --silver:   #c0c5d0;
  --bronze:   #d4853a;
  --radius:   10px;
  --radius-lg:16px;
  --shadow:   0 4px 24px rgba(0,0,0,.45);
}

html, body { height: 100%; font-family: 'Inter', sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }

/* ─── Screens ───────────────────────────────────────────────────────────────── */
.screen { display: none; min-height: 100vh; }
.screen.active { display: flex; flex-direction: column; }

/* ─── Auth ──────────────────────────────────────────────────────────────────── */
#auth-screen { align-items: center; justify-content: center; background: radial-gradient(ellipse at 50% 0%, #1a2040 0%, #0f1117 70%); }

.auth-container {
  width: 380px; background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 2.5rem 2rem; box-shadow: var(--shadow);
}

.auth-logo { text-align: center; margin-bottom: 2rem; }
.auth-logo .logo-icon { font-size: 3rem; }
.auth-logo h1 { font-size: 1.8rem; font-weight: 700; color: var(--primary); margin: .3rem 0 .2rem; }
.auth-logo p { color: var(--text2); font-size: .9rem; }

.auth-tabs { display: flex; background: var(--bg3); border-radius: var(--radius); padding: 3px; margin-bottom: 1.5rem; }
.auth-tab {
  flex: 1; border: none; background: transparent; color: var(--text2);
  padding: .5rem; border-radius: 7px; cursor: pointer; font-size: .9rem;
  font-family: inherit; transition: all .2s;
}
.auth-tab.active { background: var(--bg4); color: var(--text); font-weight: 600; }

.auth-form { display: none; flex-direction: column; gap: 1rem; }
.auth-form.active { display: flex; }

.field { display: flex; flex-direction: column; gap: .4rem; }
.field label { font-size: .85rem; color: var(--text2); font-weight: 500; }
.field .hint { font-weight: 400; color: var(--text3); font-size: .78rem; }

input[type="text"], input[type="password"], input[type="email"] {
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: .7rem 1rem; font-size: .95rem; font-family: inherit;
  outline: none; transition: border-color .2s, box-shadow .2s; width: 100%;
}
input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,142,247,.2); }

.form-error { color: var(--danger); font-size: .85rem; min-height: 1.2em; }

/* ─── Buttons ───────────────────────────────────────────────────────────────── */
.btn {
  border: none; border-radius: var(--radius); padding: .65rem 1.2rem;
  font-family: inherit; font-size: .9rem; font-weight: 600; cursor: pointer;
  transition: all .18s; display: inline-flex; align-items: center; gap: .4rem;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary2); transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }
.btn-ghost { background: var(--bg3); color: var(--text2); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg4); color: var(--text); }
.btn-danger { background: rgba(232,95,95,.12); color: var(--danger); border: 1px solid rgba(232,95,95,.3); }
.btn-danger:hover { background: rgba(232,95,95,.22); }
.btn-block { width: 100%; justify-content: center; }
.btn-large { padding: .9rem 2rem; font-size: 1rem; }
.btn-sm { padding: .4rem .8rem; font-size: .82rem; }

/* ─── Header ────────────────────────────────────────────────────────────────── */
.header {
  background: var(--bg2); border-bottom: 1px solid var(--border);
  padding: .8rem 1.5rem; display: flex; align-items: center; justify-content: space-between;
}
.header-logo { font-size: 1.2rem; font-weight: 700; color: var(--primary); }
.header-info { display: flex; align-items: center; gap: 1rem; }
.online-badge { background: rgba(52,196,124,.12); color: var(--success); border: 1px solid rgba(52,196,124,.25); border-radius: 20px; padding: .2rem .8rem; font-size: .82rem; }
.user-info { display: flex; align-items: center; gap: .6rem; }
.elo-badge { background: var(--bg3); border: 1px solid var(--border); border-radius: 20px; padding: .2rem .8rem; font-size: .82rem; color: var(--gold); }

/* ─── Nav tabs ──────────────────────────────────────────────────────────────── */
.lobby-nav {
  display: flex; background: var(--bg2); border-bottom: 1px solid var(--border);
  padding: 0 1rem;
}
.nav-tab {
  border: none; background: transparent; color: var(--text2);
  padding: 1rem 1.3rem; cursor: pointer; font-family: inherit; font-size: .9rem;
  border-bottom: 2px solid transparent; transition: all .2s;
}
.nav-tab:hover { color: var(--text); }
.nav-tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-icon { font-style: normal; }

/* ─── Lobby content ─────────────────────────────────────────────────────────── */
.lobby-content { flex: 1; padding: 2rem; display: flex; justify-content: center; align-items: flex-start; }
.tab-panel { display: none; width: 100%; max-width: 560px; }
.tab-panel.active { display: block; }

.panel-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 2rem; display: flex; flex-direction: column; gap: 1.5rem;
}
.panel-card h2 { font-size: 1.3rem; }
.panel-desc { color: var(--text2); font-size: .9rem; }

/* Room code display */
.room-code-display { text-align: center; display: flex; flex-direction: column; gap: 1rem; align-items: center; }
.big-code {
  font-size: 2.5rem; font-weight: 700; letter-spacing: 8px; color: var(--primary);
  background: var(--bg3); border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 1rem 2rem;
}
.waiting-text { color: var(--text2); font-size: .9rem; }

/* Join form */
.join-form { display: flex; flex-direction: column; gap: 1rem; }

/* Matchmaking */
.searching-animation { display: flex; align-items: center; justify-content: center; height: 120px; position: relative; }
.pulse-ring {
  position: absolute; width: 90px; height: 90px; border-radius: 50%;
  border: 2px solid var(--primary); animation: pulse 1.5s ease-out infinite;
}
@keyframes pulse {
  0% { transform: scale(1); opacity: 1; }
  100% { transform: scale(2.2); opacity: 0; }
}
.pulse-dot { font-size: 2rem; z-index: 1; }
#matchmaking-text { text-align: center; color: var(--text2); }

/* ─── Leaderboard ───────────────────────────────────────────────────────────── */
.rank-panel { max-width: 800px !important; }
.rank-header { display: flex; justify-content: space-between; align-items: center; }
.rank-table-wrap { overflow-x: auto; }
.rank-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.rank-table th {
  text-align: left; padding: .7rem 1rem; background: var(--bg3);
  color: var(--text2); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .05em;
}
.rank-table td { padding: .7rem 1rem; border-bottom: 1px solid var(--border); }
.rank-table tr:last-child td { border-bottom: none; }
.rank-table tr:hover td { background: var(--bg3); }
.rank-num { font-weight: 700; }
.rank-1 { color: var(--gold); }
.rank-2 { color: var(--silver); }
.rank-3 { color: var(--bronze); }
.rank-me td { background: rgba(79,142,247,.08) !important; }
.elo-val { font-weight: 700; color: var(--primary); }
.win-val { color: var(--success); }
.loss-val { color: var(--danger); }
.my-rank-banner {
  background: rgba(79,142,247,.1); border: 1px solid rgba(79,142,247,.3);
  border-radius: var(--radius); padding: .8rem 1.2rem; font-size: .9rem;
  display: none;
}
.my-rank-banner.visible { display: block; }

/* ─── Game screen ───────────────────────────────────────────────────────────── */
#game-screen { display: none; flex-direction: row; }
#game-screen.active { display: flex; }

.game-layout {
  display: flex; width: 100%; min-height: 100vh;
  align-items: center; justify-content: center; gap: 2rem; padding: 1.5rem;
  background: var(--bg);
}

.game-sidebar { width: 200px; display: flex; flex-direction: column; gap: 1rem; }

/* Player cards */
.player-card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; position: relative; transition: border-color .3s;
}
.player-card.active-turn { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.player-piece { font-size: 1.8rem; line-height: 1; margin-bottom: .4rem; }
.piece-X { color: #1a1a1a; text-shadow: 0 0 2px #000; }
.piece-O { color: #fff; text-shadow: 0 0 2px #aaa; }
.player-name { font-weight: 600; font-size: .95rem; }
.player-elo { font-size: .8rem; color: var(--text2); margin-top: .2rem; }
.turn-indicator {
  position: absolute; top: .6rem; right: .6rem;
  font-size: .7rem; padding: .2rem .5rem; border-radius: 4px;
  background: rgba(79,142,247,.15); color: var(--primary); display: none;
}
.player-card.active-turn .turn-indicator { display: block; }

/* Timer */
.timer-box {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; text-align: center;
}
.timer-label { font-size: .75rem; color: var(--text2); margin-bottom: .3rem; }
.timer-value { font-size: 2.4rem; font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }
.timer-value.warning { color: var(--warning); }
.timer-value.danger  { color: var(--danger); animation: blink .5s ease-in-out infinite alternate; }
@keyframes blink { from { opacity: 1; } to { opacity: .5; } }

.game-meta { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem; font-size: .8rem; display: flex; flex-direction: column; gap: .4rem; }
.game-meta strong { color: var(--primary); }
.resign-btn { align-self: stretch; }

/* Rules card */
.rules-card { background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; font-size: .83rem; }
.rules-card h3 { margin-bottom: .8rem; font-size: .9rem; color: var(--text2); }
.rules-card ul { list-style: none; display: flex; flex-direction: column; gap: .5rem; }
.rules-card li { color: var(--text2); }
.rules-card li::before { content: '▸ '; color: var(--primary); }

/* ─── Board ─────────────────────────────────────────────────────────────────── */
.board-container {
  display: flex; align-items: center; justify-content: center;
}
.board {
  display: grid;
  grid-template-columns: repeat(15, 38px);
  grid-template-rows: repeat(15, 38px);
  background: #c8a45a;
  border: 2px solid #8b6914;
  border-radius: 4px;
  box-shadow: 0 8px 40px rgba(0,0,0,.6);
  position: relative;
}
.cell {
  width: 38px; height: 38px;
  border: 1px solid #8b6914;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; position: relative; transition: background .15s;
}
.cell:hover { background: rgba(255,255,255,.15); }
.cell.no-click { cursor: default; }
.cell.no-click:hover { background: transparent; }

/* Pieces */
.piece {
  width: 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  animation: pop .18s ease-out;
  pointer-events: none; position: relative;
}
@keyframes pop {
  0%   { transform: scale(0); }
  70%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}
.piece-black {
  background: radial-gradient(circle at 35% 35%, #4a4a4a, #000);
  box-shadow: inset -2px -3px 6px rgba(0,0,0,.5), inset 2px 2px 4px rgba(255,255,255,.15), 0 2px 5px rgba(0,0,0,.5);
}
.piece-white {
  background: radial-gradient(circle at 35% 35%, #fff, #d0d0d0);
  box-shadow: inset -2px -3px 6px rgba(0,0,0,.2), inset 2px 2px 4px rgba(255,255,255,.8), 0 2px 5px rgba(0,0,0,.4);
}
.piece.winning { animation: none; }
.piece.winning::after {
  content: ''; position: absolute; inset: -4px;
  border-radius: 50%; border: 3px solid var(--gold);
  animation: win-glow 1s ease-in-out infinite alternate;
}
@keyframes win-glow { from { box-shadow: 0 0 8px var(--gold); } to { box-shadow: 0 0 20px var(--gold); } }

/* ─── Result Modal ──────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.75);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  animation: fadeIn .25s ease-out;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal-box {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 2.5rem; text-align: center; max-width: 420px; width: 90%;
  box-shadow: var(--shadow); animation: slideUp .25s ease-out;
  display: flex; flex-direction: column; gap: 1rem; align-items: center;
}
@keyframes slideUp { from { transform: translateY(30px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-icon { font-size: 3rem; }
.modal-box h2 { font-size: 1.5rem; }
.modal-box p { color: var(--text2); }
.elo-change-row { display: flex; gap: 1.5rem; flex-wrap: wrap; justify-content: center; }
.elo-chip { background: var(--bg3); border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem 1.2rem; font-size: .9rem; }
.elo-chip .delta { font-weight: 700; margin-left: .4rem; }
.delta-pos { color: var(--success); }
.delta-neg { color: var(--danger); }
.modal-actions { display: flex; gap: 1rem; justify-content: center; flex-wrap: wrap; }

/* ─── Toast ─────────────────────────────────────────────────────────────────── */
#toast-container { position: fixed; bottom: 1.5rem; right: 1.5rem; display: flex; flex-direction: column; gap: .5rem; z-index: 200; }
.toast {
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .7rem 1.2rem; font-size: .88rem; max-width: 320px;
  animation: slideInRight .25s ease-out;
}
@keyframes slideInRight { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.toast-success { border-left: 3px solid var(--success); }
.toast-error   { border-left: 3px solid var(--danger); }
.toast-info    { border-left: 3px solid var(--primary); }

/* ─── Responsive ────────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .game-layout { flex-direction: column; padding: 1rem; gap: 1rem; }
  .game-sidebar { width: 100%; flex-direction: row; flex-wrap: wrap; }
  .player-card { flex: 1; min-width: 150px; }
  .timer-box { min-width: 120px; }
  .right-sidebar { display: none; }
  .board { grid-template-columns: repeat(15, 28px); grid-template-rows: repeat(15, 28px); }
  .cell { width: 28px; height: 28px; }
  .piece { width: 22px; height: 22px; }
  .lobby-content { padding: 1rem; }
}
@media (max-width: 600px) {
  .auth-container { width: 95%; }
  .header { padding: .6rem 1rem; }
  .header-info { gap: .5rem; }
  .lobby-nav { overflow-x: auto; }
  .nav-tab { padding: .8rem .8rem; font-size: .82rem; }
}
