/* ══════════════════════════════════════════════ TOKENS ═══ */
:root {
  --bg: #F4EFE6;
  --surface: #FFFFFF;
  --surface-2: #EDE8DF;
  --surface-3: #E3DDD3;
  --border: #D6CFC3;
  --text: #1C1C1E;
  --text-muted: #8A8A8E;
  --x: #D63031;
  --x-light: rgba(214, 48, 49, 0.12);
  --o: #0984E3;
  --o-light: rgba(9, 132, 227, 0.12);
  --active: #F9A825;
  --active-light: rgba(249, 168, 37, 0.18);
  --green: #00B894;
  --shadow-sm: 0 1px 4px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  --shadow-md: 0 4px 16px rgba(0,0,0,.09), 0 1px 4px rgba(0,0,0,.05);
  --shadow-lg: 0 8px 32px rgba(0,0,0,.12), 0 2px 8px rgba(0,0,0,.06);
  --r: 14px;
  --r-sm: 8px;
  --r-xs: 5px;
}

/* ══════════════════════════════════════════════ RESET ════ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: 'Nunito', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ══════════════════════════════════════════════ SCREENS ══ */
.screen { display: none; min-height: 100vh; }
.screen.active { display: flex; }

/* ══════════════════════════════════════════════ BUTTONS ══ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-family: inherit; font-weight: 700; border: none; cursor: pointer;
  border-radius: var(--r-sm); padding: 10px 20px; font-size: .95rem;
  transition: transform .12s, box-shadow .12s, opacity .12s;
  outline: none; user-select: none;
}
.btn:active { transform: translateY(1px) scale(.98); }
.btn-primary {
  background: var(--text); color: #fff;
  box-shadow: 0 3px 10px rgba(0,0,0,.2);
}
.btn-primary:hover { box-shadow: 0 5px 16px rgba(0,0,0,.28); transform: translateY(-1px); }
.btn-ghost {
  background: var(--surface-2); color: var(--text);
  box-shadow: var(--shadow-sm);
}
.btn-ghost:hover { background: var(--surface-3); }
.btn-large { padding: 14px 32px; font-size: 1.05rem; border-radius: var(--r); }
.btn-sm { padding: 7px 14px; font-size: .85rem; }
.btn-icon {
  background: none; border: none; cursor: pointer; font-size: 1.1rem;
  color: var(--text-muted); padding: 4px 8px; border-radius: 6px;
}
.btn-icon:hover { background: var(--surface-2); }
.btn:disabled { opacity: .4; cursor: not-allowed; transform: none !important; }

/* ══════════════════════════════════════════════ REGISTER ═ */
#screen-register {
  align-items: center; justify-content: center; padding: 24px;
  background: radial-gradient(ellipse at 60% 20%, rgba(214,48,49,.06) 0%, transparent 60%),
              radial-gradient(ellipse at 30% 80%, rgba(9,132,227,.06) 0%, transparent 60%),
              var(--bg);
}
.register-card {
  background: var(--surface); border-radius: 24px; padding: 40px 36px;
  box-shadow: var(--shadow-lg); width: 100%; max-width: 440px;
  display: flex; flex-direction: column; gap: 22px;
}
.register-logo {
  display: flex; gap: 6px; font-size: 2.4rem; font-weight: 900; line-height: 1;
  margin-bottom: -4px;
}
.logo-x { color: var(--x); }
.logo-o { color: var(--o); }
.register-card h1 { font-size: 1.6rem; font-weight: 900; }
.subtitle { color: var(--text-muted); font-size: .95rem; margin-top: -16px; }

.form-group { display: flex; flex-direction: column; gap: 8px; }
.form-group label { font-weight: 700; font-size: .88rem; color: var(--text-muted); letter-spacing: .04em; text-transform: uppercase; }
.form-group input {
  padding: 11px 14px; border: 2px solid var(--border); border-radius: var(--r-sm);
  font-family: inherit; font-size: 1rem; background: var(--surface-2);
  transition: border-color .2s, background .2s; outline: none; color: var(--text);
}
.form-group input:focus { border-color: var(--text); background: var(--surface); }

/* Avatar grid */
.avatar-grid {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px;
}
.avatar-option {
  aspect-ratio: 1; border-radius: var(--r-sm); font-size: 1.5rem;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface-2); cursor: pointer; border: 2px solid transparent;
  transition: transform .12s, border-color .15s, background .15s;
}
.avatar-option:hover { transform: scale(1.08); background: var(--surface-3); }
.avatar-option.selected { border-color: var(--text); background: var(--surface-3); transform: scale(1.05); }

/* Color grid */
.color-grid { display: flex; gap: 8px; flex-wrap: wrap; }
.color-swatch {
  width: 34px; height: 34px; border-radius: 50%; cursor: pointer;
  border: 3px solid transparent; transition: transform .12s, box-shadow .15s;
  box-shadow: 0 2px 6px rgba(0,0,0,.15);
}
.color-swatch:hover { transform: scale(1.12); }
.color-swatch.selected { border-color: var(--text); transform: scale(1.15); box-shadow: 0 3px 10px rgba(0,0,0,.25); }

.error-msg { color: var(--x); font-size: .88rem; font-weight: 600; }
.hidden { display: none !important; }

/* ══════════════════════════════════════════════ LOBBY ════ */
#screen-lobby { flex-direction: column; }

.lobby-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; background: var(--surface);
  box-shadow: 0 1px 0 var(--border), var(--shadow-sm);
  position: sticky; top: 0; z-index: 50;
}
.header-brand { display: flex; align-items: center; gap: 4px; font-size: 1rem; font-weight: 800; }
.brand-x { color: var(--x); font-size: 1.1rem; }
.brand-o { color: var(--o); font-size: 1.1rem; }
.brand-name { margin-left: 6px; }

.profile-pill {
  display: flex; align-items: center; gap: 8px; padding: 7px 14px;
  background: var(--surface-2); border: none; border-radius: 50px; cursor: pointer;
  font-family: inherit; font-size: .9rem; font-weight: 700;
  transition: background .15s, box-shadow .15s;
  box-shadow: var(--shadow-sm);
}
.profile-pill:hover { background: var(--surface-3); box-shadow: var(--shadow-md); }
.edit-icon { color: var(--text-muted); font-size: .85rem; }
#header-avatar { font-size: 1.2rem; }

/* Notification */
.game-notif {
  background: var(--surface); border-bottom: 2px solid var(--active);
  padding: 12px 24px; animation: slideDown .3s ease;
}
@keyframes slideDown { from { transform: translateY(-100%); opacity:0; } to { transform: none; opacity:1; } }
.notif-inner { display: flex; align-items: center; gap: 12px; max-width: 800px; margin: 0 auto; }
.notif-inner > span { font-size: 2rem; }
.notif-text { flex: 1; }
.notif-text strong { display: block; font-size: 1rem; }
.notif-text span { font-size: .88rem; color: var(--text-muted); }
.notif-actions { display: flex; gap: 8px; }

.lobby-main {
  display: grid; grid-template-columns: 1fr 320px; gap: 24px;
  padding: 24px; max-width: 1000px; margin: 0 auto; width: 100%; flex: 1;
}
@media (max-width: 700px) {
  .lobby-main { grid-template-columns: 1fr; }
}

.lobby-players h2 {
  font-size: 1.1rem; font-weight: 800; margin-bottom: 16px;
  display: flex; align-items: center; gap: 8px;
}
.badge {
  background: var(--text); color: #fff; font-size: .75rem;
  padding: 2px 8px; border-radius: 50px; font-weight: 700;
}
.players-grid { display: flex; flex-direction: column; gap: 8px; }
.player-item {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border-radius: var(--r); padding: 12px 16px;
  box-shadow: var(--shadow-sm); transition: box-shadow .15s;
}
.player-item:hover { box-shadow: var(--shadow-md); }
.pi-avatar { font-size: 1.8rem; }
.pi-name { font-weight: 700; flex: 1; }
.pi-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); box-shadow: 0 0 6px rgba(0,184,148,.5); }
.pi-you { font-size: .75rem; font-weight: 700; color: var(--text-muted); background: var(--surface-2); padding: 2px 8px; border-radius: 50px; }

.lobby-actions { display: flex; flex-direction: column; gap: 16px; }
.action-card {
  background: var(--surface); border-radius: 20px; padding: 28px 24px;
  box-shadow: var(--shadow-md); text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.action-icon { font-size: 2.8rem; }
.action-card h3 { font-size: 1.1rem; font-weight: 800; }
.action-card p { font-size: .88rem; color: var(--text-muted); line-height: 1.5; }
.waiting {
  display: flex; align-items: center; gap: 10px;
  font-size: .88rem; color: var(--text-muted);
}
.spinner {
  width: 18px; height: 18px; border: 2.5px solid var(--border);
  border-top-color: var(--text); border-radius: 50%;
  animation: spin .7s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Leaderboard */
.leaderboard-card {
  background: var(--surface); border-radius: 20px; padding: 22px 20px;
  box-shadow: var(--shadow-md);
}
.leaderboard-card h3 { font-size: 1rem; font-weight: 800; margin-bottom: 14px; }
.leaderboard-list { display: flex; flex-direction: column; gap: 8px; }
.lb-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: var(--r-sm);
  background: var(--surface-2); transition: background .15s;
}
.lb-item:hover { background: var(--surface-3); }
.lb-rank { font-size: 1.3rem; width: 28px; text-align: center; flex-shrink: 0; }
.lb-avatar { font-size: 1.4rem; flex-shrink: 0; }
.lb-name { font-weight: 700; flex: 1; font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-points {
  font-weight: 800; font-size: .95rem;
  color: var(--text-muted); flex-shrink: 0;
}
.lb-item.top-1 { background: linear-gradient(135deg, #FFF8E1, #FFF3CD); }
.lb-item.top-2 { background: linear-gradient(135deg, #F5F5F5, #EEEEEE); }
.lb-item.top-3 { background: linear-gradient(135deg, #FFF3E0, #FFE0B2); }
.lb-empty { text-align: center; color: var(--text-muted); font-size: .88rem; padding: 16px 0; }
.points-legend {
  display: flex; flex-direction: column; gap: 3px;
  margin-top: 12px; padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: .78rem; color: var(--text-muted);
}

/* ══════════════════════════════════════════════ MODALS ═══ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 200; padding: 20px;
}
.modal-card {
  background: var(--surface); border-radius: 20px; padding: 28px 28px;
  box-shadow: var(--shadow-lg); width: 100%; max-width: 420px;
  display: flex; flex-direction: column; gap: 16px;
  animation: modalIn .25s ease;
}
@keyframes modalIn { from { transform: scale(.92); opacity:0; } to { transform: none; opacity:1; } }
.modal-header { display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { font-size: 1.1rem; font-weight: 800; }

/* ══════════════════════════════════════════════ GAME ═════ */
#screen-game { flex-direction: column; min-height: 100vh; padding-bottom: 20px; }

.game-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--surface);
  box-shadow: 0 1px 0 var(--border);
  gap: 12px;
}
.player-card {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface-2); border-radius: var(--r); padding: 8px 14px;
  box-shadow: var(--shadow-sm); min-width: 0;
  transition: box-shadow .2s, background .2s;
}
.player-card.active { background: var(--active-light); box-shadow: 0 0 0 2px var(--active), var(--shadow-sm); }
.player-card.right { flex-direction: row-reverse; }
.pc-avatar { font-size: 1.8rem; flex-shrink: 0; }
.pc-info { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.pc-name { font-weight: 800; font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pc-symbol { font-size: .85rem; font-weight: 700; }
.pc-symbol.x { color: var(--x); }
.pc-symbol.o { color: var(--o); }
.pc-score { font-size: 1.5rem; font-weight: 900; color: var(--text-muted); }
.pc-score.x { color: var(--x); }
.pc-score.o { color: var(--o); }

.game-status-center { text-align: center; flex: 1; }
.status-text { font-size: .9rem; font-weight: 700; color: var(--text-muted); }
.turn-dot {
  width: 10px; height: 10px; border-radius: 50%; background: var(--active);
  margin: 4px auto 0; animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.4); opacity: .6; }
}

/* ── DICE PHASE ──────────────────────────── */
.dice-phase {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 32px; padding: 32px 20px;
}
.dice-phase h2 { font-size: 1.2rem; font-weight: 800; text-align: center; }
.dice-arena {
  display: flex; align-items: center; gap: 32px;
  flex-wrap: wrap; justify-content: center;
}
.dice-player {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
}
.dice-player-name { font-size: 1rem; font-weight: 700; }
.dice-wrap {
  perspective: 400px;
  width: 90px; height: 90px;
}
.dice {
  width: 90px; height: 90px;
  position: relative; transform-style: preserve-3d;
  transition: transform .6s cubic-bezier(.25,.46,.45,.94);
}
.dice .face {
  position: absolute; width: 90px; height: 90px;
  font-size: 3.2rem; display: flex; align-items: center; justify-content: center;
  background: var(--surface); border: 2px solid var(--border);
  border-radius: 16px; backface-visibility: hidden;
  box-shadow: inset 0 2px 4px rgba(255,255,255,.6), inset 0 -2px 4px rgba(0,0,0,.06);
}
.dice .front  { transform: translateZ(45px); }
.dice .back   { transform: rotateY(180deg) translateZ(45px); }
.dice .right  { transform: rotateY(90deg) translateZ(45px); }
.dice .left   { transform: rotateY(-90deg) translateZ(45px); }
.dice .top    { transform: rotateX(90deg) translateZ(45px); }
.dice .bottom { transform: rotateX(-90deg) translateZ(45px); }

@keyframes diceRoll {
  0%   { transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg); }
  20%  { transform: rotateX(200deg) rotateY(100deg) rotateZ(80deg); }
  40%  { transform: rotateX(380deg) rotateY(240deg) rotateZ(160deg); }
  60%  { transform: rotateX(520deg) rotateY(360deg) rotateZ(220deg); }
  80%  { transform: rotateX(680deg) rotateY(440deg) rotateZ(300deg); }
  100% { transform: rotateX(720deg) rotateY(480deg) rotateZ(360deg); }
}
.dice.rolling { animation: diceRoll .9s cubic-bezier(.25,.46,.45,.94) forwards; }

.dice-result {
  font-size: 2rem; font-weight: 900; min-height: 2.5rem;
  transition: transform .2s, color .2s;
}
.dice-result.winner { color: var(--green); transform: scale(1.2); }
.dice-result.loser { color: var(--text-muted); }
.dice-vs { font-size: 1.5rem; font-weight: 900; color: var(--text-muted); }

/* ── SYMBOL CHOICE ───────────────────────── */
.symbol-phase {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 32px; padding: 32px 20px;
}
.symbol-phase h2 { font-size: 1.3rem; font-weight: 800; }
.symbol-choice { display: flex; gap: 24px; }
.symbol-btn {
  width: 140px; height: 140px; border-radius: 20px; border: 3px solid var(--border);
  background: var(--surface); cursor: pointer; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px;
  font-family: inherit; font-size: .95rem; font-weight: 700;
  box-shadow: var(--shadow-md);
  transition: transform .15s, border-color .15s, box-shadow .15s;
}
.symbol-btn:hover { transform: translateY(-4px); box-shadow: var(--shadow-lg); }
.symbol-btn:hover[data-symbol="X"] { border-color: var(--x); }
.symbol-btn:hover[data-symbol="O"] { border-color: var(--o); }
.sym-x { font-size: 3rem; color: var(--x); font-weight: 900; }
.sym-o { font-size: 3rem; color: var(--o); font-weight: 900; }

/* ── BOARD ───────────────────────────────── */
.board-phase {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 16px; position: relative;
}
.mega-board {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 10px; width: min(96vw, 560px); aspect-ratio: 1;
}
.mini-board {
  position: relative; border-radius: 12px;
  background: var(--surface); box-shadow: var(--shadow-md);
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 3px; padding: 5px;
  transition: box-shadow .2s, background .2s, opacity .2s;
  border: 2px solid transparent;
  overflow: hidden;
}
.mini-board.active {
  border-color: var(--active);
  box-shadow: 0 0 0 3px var(--active-light), var(--shadow-md);
  background: rgba(249,168,37,.05);
}
.mini-board.dimmed { opacity: .45; pointer-events: none; }
.mini-board.won { pointer-events: none; }

.cell {
  aspect-ratio: 1; border-radius: var(--r-xs);
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem; font-weight: 900; cursor: pointer;
  transition: background .12s, transform .1s, box-shadow .12s;
  border: 1.5px solid var(--border);
  position: relative; overflow: hidden;
}
.cell.playable:hover {
  background: var(--active-light); border-color: var(--active);
  transform: scale(1.06); box-shadow: 0 2px 8px rgba(0,0,0,.1);
  z-index: 2;
}
.cell.x { color: var(--x); background: var(--x-light); border-color: rgba(214,48,49,.2); cursor: default; }
.cell.o { color: var(--o); background: var(--o-light); border-color: rgba(9,132,227,.2); cursor: default; }
.cell.x::after, .cell.o::after { content: attr(data-sym); font-size: 1.3em; }

/* Mini board won overlay — replaces the entire mini-board */
.mini-winner-overlay {
  position: absolute; inset: 0; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 900; z-index: 10;
  animation: popIn .4s cubic-bezier(.34,1.56,.64,1);
  font-size: clamp(3rem, 10vw, 5rem);
  line-height: 1;
}
.mini-winner-overlay.x {
  background: var(--x);
  color: #fff;
  text-shadow: 0 2px 12px rgba(0,0,0,.25);
  box-shadow: inset 0 -4px 12px rgba(0,0,0,.15);
}
.mini-winner-overlay.o {
  background: var(--o);
  color: #fff;
  text-shadow: 0 2px 12px rgba(0,0,0,.25);
  box-shadow: inset 0 -4px 12px rgba(0,0,0,.15);
}
.mini-winner-overlay.draw {
  background: var(--surface-3);
  color: var(--text-muted);
  font-size: clamp(1.2rem, 4vw, 1.8rem);
  letter-spacing: .05em;
}

@keyframes popIn {
  from { transform: scale(.4); opacity:0; }
  to   { transform: scale(1);   opacity:1; }
}

/* Win line SVG */
.win-line-svg {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; overflow: visible;
}
#win-line {
  stroke-width: 6px; stroke-linecap: round;
  stroke-dasharray: 400; stroke-dashoffset: 400;
}
#win-line.x-line { stroke: var(--x); }
#win-line.o-line { stroke: var(--o); }
#win-line.animate { animation: drawLine .5s ease forwards .1s; }
@keyframes drawLine {
  to { stroke-dashoffset: 0; }
}

/* ── GAME OVER ───────────────────────────── */
.gameover-card { text-align: center; align-items: center; }
.go-animation { font-size: 4rem; animation: bounceIn .5s cubic-bezier(.34,1.56,.64,1); }
@keyframes bounceIn { from { transform: scale(.3); opacity:0; } to { transform: scale(1); opacity:1; } }
.gameover-card h2 { font-size: 1.6rem; font-weight: 900; }
.gameover-card p { color: var(--text-muted); }
.go-counts { display: flex; gap: 24px; font-size: 1.2rem; font-weight: 800; }
.count-x { color: var(--x); display: flex; align-items: center; gap: 8px; }
.count-o { color: var(--o); display: flex; align-items: center; gap: 8px; }
.go-actions { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

/* ══════════════════════════════════════════════ TOAST ════ */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 12px 24px;
  border-radius: 50px; font-size: .9rem; font-weight: 700;
  box-shadow: var(--shadow-lg); z-index: 999;
  animation: toastIn .25s ease;
  max-width: 90vw; text-align: center;
}
@keyframes toastIn {
  from { transform: translateX(-50%) translateY(20px); opacity:0; }
  to   { transform: translateX(-50%) translateY(0);    opacity:1; }
}

/* ══════════════════════════════════════════ RESPONSIVE ═══ */
@media (max-width: 480px) {
  .register-card { padding: 28px 20px; }
  .avatar-grid { grid-template-columns: repeat(6, 1fr); }
  .dice-arena { gap: 16px; }
  .symbol-btn { width: 110px; height: 110px; }
  .mega-board { gap: 6px; }
  .mini-board { gap: 2px; padding: 3px; }
}
