/* ==========================================================================
   PeerPlay - Design System & Modern UI Stylesheet
   ========================================================================== */

/* --- 1. Design Tokens & CSS Variables --- */
:root {
    /* Color Palette */
    --bg-base: #090d16;
    --bg-mesh-1: rgba(99, 102, 241, 0.08);
    --bg-mesh-2: rgba(244, 63, 94, 0.06);
    --bg-mesh-3: rgba(6, 182, 212, 0.06);

    --surface-card: #111827;
    --surface-elevated: #1a2336;
    --surface-higher: #222d42;
    --surface-subtle: rgba(255, 255, 255, 0.04);

    --border-subtle: rgba(255, 255, 255, 0.08);
    --border-medium: rgba(255, 255, 255, 0.15);
    --border-highlight: rgba(255, 255, 255, 0.25);

    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --text-on-accent: #ffffff;

    /* Brand & Player Accent Colors */
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --primary-active: #4338ca;
    --primary-light: #818cf8;
    --primary-glow: rgba(99, 102, 241, 0.35);

    --color-x: #f43f5e;
    --color-x-hover: #e11d48;
    --color-x-glow: rgba(244, 63, 94, 0.35);
    --color-x-bg: rgba(244, 63, 94, 0.12);

    --color-o: #06b6d4;
    --color-o-hover: #0891b2;
    --color-o-glow: rgba(6, 182, 212, 0.35);
    --color-o-bg: rgba(6, 182, 212, 0.12);

    /* Semantic Feedback */
    --color-success: #10b981;
    --color-success-bg: rgba(16, 185, 129, 0.15);
    --color-warning: #f59e0b;
    --color-warning-bg: rgba(245, 158, 11, 0.15);
    --color-error: #ef4444;
    --color-error-bg: rgba(239, 68, 68, 0.15);

    /* Typography Scale */
    --font-family: 'Outfit', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    /* Spacing Scale */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;
    --space-12: 48px;

    /* Border Radius Scale */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --radius-xl: 24px;
    --radius-pill: 9999px;

    /* Shadows & Elevation */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px -1px rgba(0, 0, 0, 0.4), 0 2px 6px -2px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 10px 28px -3px rgba(0, 0, 0, 0.5), 0 4px 12px -4px rgba(0, 0, 0, 0.4);
    --shadow-card: 0 16px 36px -8px rgba(0, 0, 0, 0.55), 0 0 0 1px var(--border-subtle);

    /* Transitions & Easings */
    --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 220ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-bounce: 300ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

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

html {
    font-size: 16px;
    scroll-behavior: smooth;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-base);
    background-image: 
        radial-gradient(circle at 15% 15%, var(--bg-mesh-1) 0%, transparent 40%),
        radial-gradient(circle at 85% 20%, var(--bg-mesh-2) 0%, transparent 40%),
        radial-gradient(circle at 50% 85%, var(--bg-mesh-3) 0%, transparent 45%);
    background-attachment: fixed;
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: var(--space-4) var(--space-4) var(--space-8);
    overflow-x: hidden;
}

/* Accessible focus styling */
:focus-visible {
    outline: 2px solid var(--primary-light);
    outline-offset: 3px;
}

/* Screen reader utility */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* --- 3. App Header & Navbar --- */
.app-header {
    width: 100%;
    max-width: 960px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-4);
    margin-bottom: var(--space-6);
    background: rgba(17, 24, 39, 0.7);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    position: sticky;
    top: var(--space-3);
    z-index: 50;
}

.brand-container {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    text-decoration: none;
    color: inherit;
}

.brand-logo-badge {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, var(--color-x) 0%, var(--primary) 50%, var(--color-o) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 14px var(--primary-glow);
    flex-shrink: 0;
}

.brand-logo-badge svg {
    width: 22px;
    height: 22px;
    color: white;
}

.brand-info {
    display: flex;
    flex-direction: column;
}

.brand-title {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, #ffffff 40%, var(--primary-light) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.brand-subtitle {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

/* Header Controls & Status Pill */
.header-actions {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

.status-pill {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: 6px 14px;
    border-radius: var(--radius-pill);
    font-size: 0.8rem;
    font-weight: 600;
    background: var(--surface-subtle);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    transition: var(--transition-normal);
}

.status-pill .pulse-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    transition: var(--transition-normal);
}

.status-pill.status-ready .pulse-dot {
    background: var(--color-success);
    box-shadow: 0 0 10px var(--color-success);
}
.status-pill.status-connecting .pulse-dot {
    background: var(--color-warning);
    box-shadow: 0 0 10px var(--color-warning);
    animation: pulseGlow 1.2s infinite ease-in-out;
}
.status-pill.status-connected .pulse-dot {
    background: var(--primary-light);
    box-shadow: 0 0 10px var(--primary);
    animation: pulseGlow 1.6s infinite ease-in-out;
}

@keyframes pulseGlow {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.85); }
}

.icon-btn {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-md);
    background: var(--surface-subtle);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition-fast);
}

.icon-btn:hover {
    background: var(--surface-elevated);
    border-color: var(--border-medium);
    color: var(--text-primary);
    transform: translateY(-1px);
}

.icon-btn:active {
    transform: translateY(0);
}

.icon-btn svg {
    width: 18px;
    height: 18px;
}

/* --- 4. App Main Container --- */
.app-container {
    width: 100%;
    max-width: 960px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* --- 5. Lobby Section --- */
#lobby {
    width: 100%;
    max-width: 760px;
    display: flex;
    flex-direction: column;
    gap: var(--space-6);
    animation: fadeInSlide 0.35s ease-out;
}

.lobby-hero {
    text-align: center;
    padding: var(--space-4) 0;
}

.hero-badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: 4px 12px;
    background: rgba(99, 102, 241, 0.12);
    border: 1px solid rgba(99, 102, 241, 0.3);
    border-radius: var(--radius-pill);
    color: var(--primary-light);
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: var(--space-3);
}

.hero-title {
    font-size: 2.2rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    line-height: 1.2;
    margin-bottom: var(--space-2);
}

.hero-subtitle {
    color: var(--text-secondary);
    font-size: 1rem;
    max-width: 520px;
    margin: 0 auto;
    line-height: 1.5;
}

/* Lobby Action Cards Grid */
.lobby-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-5);
    width: 100%;
}

.lobby-card {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    padding: var(--space-6);
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-card);
    transition: var(--transition-normal);
    position: relative;
    overflow: hidden;
}

.lobby-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: transparent;
    transition: var(--transition-normal);
}

.lobby-card:hover {
    border-color: var(--border-medium);
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.lobby-card.card-host:hover::before {
    background: linear-gradient(90deg, var(--color-x), var(--primary));
}

.lobby-card.card-join:hover::before {
    background: linear-gradient(90deg, var(--primary), var(--color-o));
}

.card-header-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    margin-bottom: var(--space-4);
}

.card-icon-bubble {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.card-icon-bubble.host-icon {
    background: var(--color-x-bg);
    color: var(--color-x);
    border: 1px solid rgba(244, 63, 94, 0.25);
}

.card-icon-bubble.join-icon {
    background: var(--color-o-bg);
    color: var(--color-o);
    border: 1px solid rgba(6, 182, 212, 0.25);
}

.card-icon-bubble svg {
    width: 22px;
    height: 22px;
}

.card-title-wrap {
    display: flex;
    flex-direction: column;
}

.card-title {
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.card-desc {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

/* Host Box & Room Code Display */
.host-action-area {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

.room-display-box {
    background: var(--surface-elevated);
    border: 1px dashed var(--border-medium);
    border-radius: var(--radius-md);
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    transition: var(--transition-normal);
}

.room-display-box.has-code {
    border-style: solid;
    border-color: rgba(99, 102, 241, 0.4);
    background: rgba(99, 102, 241, 0.08);
}

.room-code-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.room-code-value-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
}

.room-code-text {
    font-family: var(--font-mono);
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-primary);
    letter-spacing: 0.03em;
    word-break: break-all;
}

.copy-btn {
    padding: 6px 12px;
    font-size: 0.8rem;
    font-weight: 600;
    border-radius: var(--radius-sm);
    background: var(--surface-higher);
    color: var(--text-primary);
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
    transition: var(--transition-fast);
}

.copy-btn:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: white;
}

.copy-btn.copied {
    background: var(--color-success);
    border-color: var(--color-success);
    color: white;
}

/* Join Box & Inputs */
.join-action-area {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.input-field-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.text-input {
    width: 100%;
    padding: 12px 42px 12px 14px;
    background: var(--surface-elevated);
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-md);
    font-family: var(--font-mono);
    font-size: 0.95rem;
    color: var(--text-primary);
    transition: var(--transition-fast);
}

.text-input::placeholder {
    font-family: var(--font-family);
    color: var(--text-muted);
    font-size: 0.9rem;
}

.text-input:hover {
    border-color: var(--border-highlight);
}

.text-input:focus {
    border-color: var(--primary-light);
    box-shadow: 0 0 0 3px var(--primary-glow);
    background: var(--surface-higher);
}

.input-paste-btn {
    position: absolute;
    right: 8px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 4px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-fast);
}

.input-paste-btn:hover {
    color: var(--text-primary);
    background: var(--surface-subtle);
}

/* Buttons System */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: 12px 20px;
    border-radius: var(--radius-md);
    font-family: var(--font-family);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: var(--transition-fast);
    text-decoration: none;
    position: relative;
}

.btn:active {
    transform: scale(0.98);
}

.btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
    transform: none !important;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
    color: white;
    box-shadow: 0 4px 14px var(--primary-glow);
}

.btn-primary:hover:not(:disabled) {
    background: linear-gradient(135deg, var(--primary-light) 0%, var(--primary) 100%);
    box-shadow: 0 6px 20px var(--primary-glow);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--surface-elevated);
    color: var(--text-primary);
    border-color: var(--border-medium);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--surface-higher);
    border-color: var(--border-highlight);
    transform: translateY(-1px);
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--surface-subtle);
    color: var(--text-primary);
}

.btn-danger {
    background: var(--color-error-bg);
    color: var(--color-error);
    border-color: rgba(239, 68, 68, 0.3);
}

.btn-danger:hover:not(:disabled) {
    background: var(--color-error);
    color: white;
    box-shadow: 0 4px 14px rgba(239, 68, 68, 0.3);
}

.btn-sm {
    padding: 8px 14px;
    font-size: 0.85rem;
    border-radius: var(--radius-sm);
}

/* Status Banner & Spinner */
.status-msg-banner {
    display: none;
    align-items: center;
    justify-content: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    background: var(--surface-elevated);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    color: var(--text-secondary);
    text-align: center;
    animation: fadeInSlide 0.25s ease-out;
}

.status-msg-banner.active {
    display: flex;
}

.status-msg-banner.is-error {
    background: var(--color-error-bg);
    border-color: rgba(239, 68, 68, 0.3);
    color: var(--color-error);
}

.status-msg-banner.is-success {
    background: var(--color-success-bg);
    border-color: rgba(16, 185, 129, 0.3);
    color: var(--color-success);
}

.loading-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-top-color: var(--primary-light);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    flex-shrink: 0;
}

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

/* Lobby Features Footer */
.lobby-features-strip {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-6);
    color: var(--text-muted);
    font-size: 0.82rem;
    padding: var(--space-2) 0;
    flex-wrap: wrap;
}

.feature-tag {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.feature-tag svg {
    width: 15px;
    height: 15px;
    color: var(--text-secondary);
}

/* --- 6. In-Game Arena Section --- */
#game-ui {
    width: 100%;
    max-width: 920px;
    display: flex;
    flex-direction: column;
    gap: var(--space-5);
    animation: fadeInSlide 0.35s ease-out;
}

/* Match Header & Scoreboard Bar */
.match-scoreboard-card {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    padding: var(--space-4) var(--space-6);
    box-shadow: var(--shadow-card);
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: var(--space-4);
}

.player-score-card {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-lg);
    background: var(--surface-subtle);
    border: 1px solid transparent;
    transition: var(--transition-normal);
}

.player-score-card.player-x {
    justify-content: flex-start;
}

.player-score-card.player-o {
    justify-content: flex-end;
    flex-direction: row-reverse;
}

.player-score-card.is-active-turn {
    background: var(--surface-elevated);
    box-shadow: 0 0 16px -2px rgba(0,0,0,0.5);
}

.player-score-card.player-x.is-active-turn {
    border-color: var(--color-x);
    box-shadow: 0 0 20px -3px var(--color-x-glow);
}

.player-score-card.player-o.is-active-turn {
    border-color: var(--color-o);
    box-shadow: 0 0 20px -3px var(--color-o-glow);
}

.player-avatar-badge {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    font-weight: 800;
    flex-shrink: 0;
    position: relative;
}

.player-avatar-badge.badge-x {
    background: var(--color-x-bg);
    color: var(--color-x);
    border: 2px solid var(--color-x);
}

.player-avatar-badge.badge-o {
    background: var(--color-o-bg);
    color: var(--color-o);
    border: 2px solid var(--color-o);
}

.player-details {
    display: flex;
    flex-direction: column;
}

.player-score-card.player-o .player-details {
    text-align: right;
}

.player-name-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.player-score-card.player-o .player-name-row {
    justify-content: flex-end;
}

.player-label {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--text-primary);
}

.role-tag {
    font-size: 0.7rem;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: var(--radius-pill);
    background: var(--surface-higher);
    color: var(--text-secondary);
}

.player-score-tally {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 500;
}

.player-score-tally strong {
    color: var(--text-primary);
    font-size: 1rem;
}

/* Scoreboard Center / VS / Draws */
.scoreboard-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1);
}

.vs-pill {
    font-size: 0.75rem;
    font-weight: 800;
    letter-spacing: 0.1em;
    padding: 4px 10px;
    border-radius: var(--radius-pill);
    background: var(--surface-elevated);
    border: 1px solid var(--border-subtle);
    color: var(--text-muted);
}

.draws-counter {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Turn Indicator Banner */
.turn-banner {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-5);
    border-radius: var(--radius-lg);
    background: var(--surface-elevated);
    border: 1px solid var(--border-subtle);
    font-size: 1.05rem;
    font-weight: 700;
    box-shadow: var(--shadow-sm);
    transition: var(--transition-normal);
}

.turn-banner.turn-my {
    background: rgba(16, 185, 129, 0.1);
    border-color: rgba(16, 185, 129, 0.4);
    color: var(--color-success);
}

.turn-banner.turn-opponent {
    background: rgba(244, 63, 94, 0.08);
    border-color: rgba(244, 63, 94, 0.3);
    color: var(--text-secondary);
}

.turn-banner.turn-gameover {
    background: rgba(99, 102, 241, 0.12);
    border-color: rgba(99, 102, 241, 0.4);
    color: var(--primary-light);
}

/* Two-Column Arena Layout */
.arena-grid {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: var(--space-5);
    align-items: start;
}

/* Board Arena Container */
.board-arena-card {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    padding: var(--space-6);
    box-shadow: var(--shadow-card);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-5);
}

/* 3x3 Game Board Grid */
#board {
    display: grid;
    grid-template-columns: repeat(3, 105px);
    grid-template-rows: repeat(3, 105px);
    gap: 12px;
    justify-content: center;
    padding: 10px;
    background: var(--surface-elevated);
    border-radius: var(--radius-xl);
    border: 1px solid var(--border-subtle);
    box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.4);
}

.cell {
    width: 105px;
    height: 105px;
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: var(--transition-fast);
    position: relative;
    user-select: none;
    box-shadow: var(--shadow-sm);
}

.cell:hover:not(.x):not(.o) {
    background: var(--surface-higher);
    border-color: var(--border-medium);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.cell:active:not(.x):not(.o) {
    transform: scale(0.96);
}

.cell.disabled, .cell.x, .cell.o {
    cursor: default;
}

/* Cell Marks SVG Styling */
.cell svg {
    width: 62px;
    height: 62px;
    animation: markPop 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.cell.x svg {
    color: var(--color-x);
    filter: drop-shadow(0 0 10px var(--color-x-glow));
}

.cell.o svg {
    color: var(--color-o);
    filter: drop-shadow(0 0 10px var(--color-o-glow));
}

@keyframes markPop {
    0% {
        opacity: 0;
        transform: scale(0.3) rotate(-15deg);
    }
    70% {
        transform: scale(1.15) rotate(4deg);
    }
    100% {
        opacity: 1;
        transform: scale(1) rotate(0deg);
    }
}

/* Winning Cell Glow Animation */
.cell.win-cell {
    border-color: var(--primary-light);
    background: rgba(99, 102, 241, 0.2);
    box-shadow: 0 0 24px var(--primary-glow);
    animation: winPulse 1.4s infinite ease-in-out;
}

.cell.win-cell.x {
    border-color: var(--color-x);
    background: var(--color-x-bg);
    box-shadow: 0 0 28px var(--color-x-glow);
}

.cell.win-cell.o {
    border-color: var(--color-o);
    background: var(--color-o-bg);
    box-shadow: 0 0 28px var(--color-o-glow);
}

@keyframes winPulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
}

/* Match Action Buttons Row */
.board-actions-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    justify-content: center;
}

/* Live Chat Card */
.chat-card {
    background: var(--surface-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-xl);
    padding: var(--space-4);
    box-shadow: var(--shadow-card);
    display: flex;
    flex-direction: column;
    height: 480px;
}

.chat-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: var(--space-3);
    border-bottom: 1px solid var(--border-subtle);
    margin-bottom: var(--space-3);
}

.chat-title-row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: 0.95rem;
    font-weight: 700;
}

.chat-title-row svg {
    width: 18px;
    height: 18px;
    color: var(--primary-light);
}

/* Scrollable Messages Stream */
#chat-box {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    padding-right: var(--space-1);
    margin-bottom: var(--space-3);
}

/* Custom Scrollbar */
#chat-box::-webkit-scrollbar {
    width: 5px;
}
#chat-box::-webkit-scrollbar-track {
    background: transparent;
}
#chat-box::-webkit-scrollbar-thumb {
    background: var(--surface-higher);
    border-radius: var(--radius-pill);
}

/* Chat Message Bubbles */
.msg {
    display: flex;
    flex-direction: column;
    max-width: 82%;
    padding: 8px 12px;
    border-radius: var(--radius-md);
    font-size: 0.88rem;
    line-height: 1.4;
    word-break: break-word;
    animation: fadeInSlide 0.2s ease-out;
}

.msg-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    margin-bottom: 2px;
    font-size: 0.72rem;
    font-weight: 600;
    opacity: 0.8;
}

.msg.me {
    align-self: flex-end;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
    color: white;
    border-bottom-right-radius: 2px;
}

.msg.them {
    align-self: flex-start;
    background: var(--surface-elevated);
    border: 1px solid var(--border-subtle);
    color: var(--text-primary);
    border-bottom-left-radius: 2px;
}

.msg.system {
    align-self: center;
    background: var(--surface-subtle);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    font-size: 0.78rem;
    padding: 4px 12px;
    border-radius: var(--radius-pill);
    text-align: center;
    max-width: 95%;
}

.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    font-size: 0.85rem;
    text-align: center;
    padding: var(--space-4);
    gap: var(--space-2);
}

.chat-empty-state svg {
    width: 32px;
    height: 32px;
    color: var(--text-muted);
    opacity: 0.5;
}

/* Quick Reactions Row */
.quick-reactions-row {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: var(--space-2);
    margin-bottom: var(--space-2);
}

.quick-reactions-row::-webkit-scrollbar {
    display: none;
}

.reaction-btn {
    padding: 4px 8px;
    border-radius: var(--radius-pill);
    background: var(--surface-elevated);
    border: 1px solid var(--border-subtle);
    font-size: 0.8rem;
    color: var(--text-secondary);
    cursor: pointer;
    white-space: nowrap;
    transition: var(--transition-fast);
}

.reaction-btn:hover {
    background: var(--surface-higher);
    border-color: var(--border-medium);
    color: var(--text-primary);
    transform: translateY(-1px);
}

/* Chat Input Bar */
.chat-input-bar {
    display: flex;
    gap: var(--space-2);
    align-items: center;
}

.chat-input-bar .text-input {
    padding: 10px 14px;
    font-family: var(--font-family);
    font-size: 0.9rem;
}

.chat-input-bar .btn {
    padding: 10px 14px;
}

/* --- 7. Modals & Overlays --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(4, 7, 13, 0.75);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-normal);
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-card {
    background: var(--surface-card);
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-xl);
    padding: var(--space-6);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7), 0 0 0 1px var(--border-subtle);
    max-width: 440px;
    width: 100%;
    transform: scale(0.94) translateY(10px);
    transition: transform var(--transition-bounce), opacity var(--transition-normal);
    position: relative;
}

.modal-overlay.active .modal-card {
    transform: scale(1) translateY(0);
}

.modal-close-btn {
    position: absolute;
    top: var(--space-4);
    right: var(--space-4);
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition-fast);
}

.modal-close-btn:hover {
    color: var(--text-primary);
    background: var(--surface-subtle);
}

.modal-icon-badge {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto var(--space-4);
}

.modal-icon-badge.badge-win {
    background: rgba(16, 185, 129, 0.15);
    color: var(--color-success);
    box-shadow: 0 0 24px rgba(16, 185, 129, 0.3);
}

.modal-icon-badge.badge-draw {
    background: rgba(245, 158, 11, 0.15);
    color: var(--color-warning);
    box-shadow: 0 0 24px rgba(245, 158, 11, 0.3);
}

.modal-icon-badge.badge-loss {
    background: rgba(244, 63, 94, 0.15);
    color: var(--color-x);
    box-shadow: 0 0 24px rgba(244, 63, 94, 0.3);
}

.modal-title {
    font-size: 1.5rem;
    font-weight: 800;
    text-align: center;
    margin-bottom: var(--space-2);
}

.modal-subtitle {
    font-size: 0.95rem;
    color: var(--text-secondary);
    text-align: center;
    margin-bottom: var(--space-5);
    line-height: 1.5;
}

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

/* Modal Instructions for How to Play */
.guide-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    margin-bottom: var(--space-5);
}

.guide-item {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.4;
}

.guide-step {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: var(--surface-higher);
    color: var(--primary-light);
    font-size: 0.75rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* --- 8. Toast Notifications --- */
#toast-container {
    position: fixed;
    bottom: var(--space-6);
    right: var(--space-6);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    z-index: 150;
    pointer-events: none;
}

.toast {
    background: var(--surface-elevated);
    border: 1px solid var(--border-medium);
    border-radius: var(--radius-md);
    padding: 10px 16px;
    color: var(--text-primary);
    font-size: 0.88rem;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    gap: var(--space-2);
    pointer-events: auto;
    animation: toastEnter 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.toast-success {
    border-color: rgba(16, 185, 129, 0.4);
    background: #0d231e;
}

.toast.toast-error {
    border-color: rgba(239, 68, 68, 0.4);
    background: #281014;
}

@keyframes toastEnter {
    from {
        opacity: 0;
        transform: translateY(16px) scale(0.92);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* --- 9. Animations & Keyframes --- */
@keyframes fadeInSlide {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* --- 10. Responsive Design & Media Queries --- */
@media (max-width: 860px) {
    .arena-grid {
        grid-template-columns: 1fr;
    }
    
    .chat-card {
        height: 380px;
    }

    .lobby-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 520px) {
    body {
        padding: var(--space-2) var(--space-2) var(--space-6);
    }

    .app-header {
        padding: var(--space-2) var(--space-3);
        margin-bottom: var(--space-4);
    }

    .brand-title {
        font-size: 1.1rem;
    }

    .hero-title {
        font-size: 1.65rem;
    }

    .match-scoreboard-card {
        padding: var(--space-3);
        gap: var(--space-2);
    }

    .player-avatar-badge {
        width: 36px;
        height: 36px;
        font-size: 1.1rem;
    }

    .player-label {
        font-size: 0.85rem;
    }

    #board {
        grid-template-columns: repeat(3, 85px);
        grid-template-rows: repeat(3, 85px);
        gap: 8px;
        padding: 8px;
    }

    .cell {
        width: 85px;
        height: 85px;
    }

    .cell svg {
        width: 50px;
        height: 50px;
    }

    #toast-container {
        left: var(--space-4);
        right: var(--space-4);
        bottom: var(--space-4);
    }
}

/* Respect user's motion preferences */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}