:root {
    --bg: #f3f6fb;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --text: #0f172a;
    --text-muted: #475569;
    --primary: #2563eb;
    --primary-strong: #1d4ed8;
    --x-color: #0c4a6e;
    --o-color: #b45309;
    --x-soft: #e0f2fe;
    --o-soft: #ffedd5;
    --success: #15803d;
    --danger: #b91c1c;
    --warning: #b45309;
    --border: #dbe5f1;
    --focus: #0ea5e9;
    --radius: 16px;
    --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: radial-gradient(circle at top, #dbeafe 0, var(--bg) 45%);
    color: var(--text);
    min-height: 100vh;
}

.app {
    max-width: 760px;
    margin: 0 auto;
    min-height: 100vh;
    padding: 12px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.header {
    padding: 14px;
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
}

.logo { display: flex; gap: 10px; align-items: center; }
.logo-mark {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: linear-gradient(135deg, var(--primary), #7c3aed);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.logo h1 { margin: 0; font-size: 1.05rem; }
.subtitle { margin: 2px 0 0; color: var(--text-muted); font-size: .82rem; }

.header-controls {
    display: grid;
    grid-template-columns: auto auto 1fr;
    gap: 10px;
    align-items: center;
    width: 100%;
}
.user-info { display: flex; align-items: center; gap: 10px; justify-self: end; min-width: 0; }
.avatar { width: 40px; height: 40px; border-radius: 50%; border: 2px solid #dbeafe; }
.name { font-weight: 600; font-size: .92rem; }
.stats { font-size: .78rem; color: var(--text-muted); }

.theme-toggle,
.lang-toggle {
    border: 1px solid var(--border);
    background: var(--surface-soft);
    color: var(--text);
    border-radius: 10px;
    min-height: 36px;
    padding: 6px 10px;
    font-size: .82rem;
    font-weight: 600;
    white-space: nowrap;
}

.lang-toggle { min-width: 52px; }

.nav {
    margin: 12px 0;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}
.nav-btn {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 12px;
    padding: 12px;
    font-weight: 600;
    color: var(--text-muted);
}
.nav-btn.active {
    color: var(--primary-strong);
    border-color: #bfdbfe;
    background: #eff6ff;
}

.page { display: none; }
.page.active { display: block; }

.game-status { padding: 16px; margin-bottom: 12px; text-align: center; }
.status-text { font-size: clamp(.95rem, 2.4vw, 1.05rem); font-weight: 700; word-break: break-word; }
.status-text.waiting { color: var(--warning); }
.status-text.playing { color: var(--primary-strong); }
.status-text.finished { color: var(--success); }
.status-text.error { color: var(--danger); }
.status-caption { margin-top: 6px; color: var(--text-muted); font-size: .9rem; }

.players {
    margin-top: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}
.player-chip {
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 8px 12px;
    display: inline-flex;
    gap: 8px;
    align-items: center;
}
.chip-mark { font-weight: 800; min-width: 1ch; text-align: center; }
.chip-x { background: var(--x-soft); border-color: #7dd3fc; color: var(--x-color); }
.chip-o { background: var(--o-soft); border-color: #fdba74; color: var(--o-color); }
.vs { color: var(--text-muted); font-weight: 700; }

.game-board { padding: 14px; margin-bottom: 12px; }
.board {
    width: min(92vw, 340px);
    margin: 0 auto;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}
.cell {
    border: 1px solid var(--border);
    background: var(--surface-soft);
    border-radius: 12px;
    aspect-ratio: 1;
    font-size: clamp(2.2rem, 6vw, 2.9rem);
    font-weight: 800;
    color: var(--text);
}
.cell.x {
    color: var(--x-color);
    background: var(--x-soft);
    border-color: #7dd3fc;
}
.cell.o {
    color: var(--o-color);
    background: var(--o-soft);
    border-color: #fdba74;
}
.cell:not(:disabled):hover { background: #eef2ff; }
.cell:disabled { opacity: .95; }

.game-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
}
.btn {
    min-height: 48px;
    border: 1px solid transparent;
    border-radius: 12px;
    font-weight: 600;
    padding: 10px 12px;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-strong); }
.btn-secondary { background: #e2e8f0; color: #0f172a; }
.btn-ghost { background: #fff; border-color: #bfdbfe; color: var(--primary-strong); }

.active-games { padding: 14px; }
.section-title-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.section-title-row h3, h2 { margin: 0; }
.section-subtitle { margin: 8px 0 12px; color: var(--text-muted); font-size: .9rem; }
.link-btn { border: none; background: transparent; color: var(--primary-strong); text-decoration: underline; }

.games-list, .leaderboard-list { display: grid; gap: 8px; }
.game-item, .leaderboard-item {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px;
    background: var(--surface-soft);
}
.game-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}
.game-meta { color: var(--text-muted); font-size: .82rem; }
.join-btn { min-height: 40px; }

.leaderboard-item { display: grid; grid-template-columns: 28px 1fr auto; gap: 10px; align-items: center; }
.rank { font-weight: 700; color: var(--primary-strong); }
.rating { font-weight: 700; color: var(--success); }

.profile-card { text-align: center; margin-bottom: 12px; }
.profile-avatar { width: 78px; height: 78px; border-radius: 50%; border: 3px solid #bfdbfe; }
.profile-name { margin-top: 8px; font-weight: 700; }
.profile-username { color: var(--text-muted); margin-top: 4px; }
.profile-stats {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}
.stat-item {
    border: 1px solid var(--border);
    background: var(--surface-soft);
    border-radius: 12px;
    padding: 12px;
    text-align: center;
}
.stat-value { font-size: 1.2rem; font-weight: 700; display: block; }
.stat-label { color: var(--text-muted); font-size: .82rem; }

.state {
    border: 1px dashed var(--border);
    border-radius: 12px;
    padding: 14px;
    text-align: center;
    color: var(--text-muted);
}
.state.error { border-color: #fecaca; color: var(--danger); background: #fef2f2; }
.state.success { border-color: #bbf7d0; color: var(--success); background: #f0fdf4; }

.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(2, 6, 23, 0.55);
}
.modal-content {
    max-width: 360px;
    width: 100%;
    background: var(--surface);
    border-radius: 16px;
    padding: 16px;
    text-align: center;
}
.result-icon { font-size: 2.4rem; margin: 4px 0; }
.modal-footer { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255,255,255,.8);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 8px;
}
.spinner {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 3px solid #bfdbfe;
    border-top-color: var(--primary);
    animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.notifications {
    position: fixed;
    right: 12px;
    bottom: 12px;
    display: grid;
    gap: 8px;
    z-index: 1000;
}
.notification {
    background: #0f172a;
    color: #fff;
    border-radius: 10px;
    padding: 10px 12px;
    font-size: .92rem;
}
.notification.success { background: var(--success); }
.notification.error { background: var(--danger); }
.notification.warning { background: var(--warning); }

button { cursor: pointer; }
button:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

[hidden] { display: none !important; }

@media (max-width: 640px) {
    .app { padding: 10px; }
    .game-actions { grid-template-columns: 1fr; }
    .header { align-items: flex-start; flex-direction: column; }
    .logo { width: 100%; }
    .header-controls { width: 100%; grid-template-columns: auto auto; }
    .user-info { grid-column: 1 / -1; justify-self: start; }
    .name, .stats { max-width: 60vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    .modal-footer { grid-template-columns: 1fr; }
}

:root[data-theme="dark"] {
    --bg: #020617;
    --surface: #0f172a;
    --surface-soft: #111c34;
    --text: #e2e8f0;
    --text-muted: #94a3b8;
    --border: #23314d;
    --shadow: none;
}

:root[data-theme="dark"] body { background: #020617; }
:root[data-theme="dark"] .btn-secondary { background: #1e293b; color: var(--text); }
:root[data-theme="dark"] .loading-overlay { background: rgba(2,6,23,.84); }