/* ==================== ROOT VARIABLES ==================== */
:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #818cf8;
    --secondary: #ec4899;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    
    --bg-primary: #ffffff;
    --bg-secondary: #f8fafc;
    --bg-tertiary: #f1f5f9;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-tertiary: #94a3b8;
    
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1);
    
    --radius-sm: 0.5rem;
    --radius-md: 0.75rem;
    --radius-lg: 1rem;
    --radius-xl: 1.5rem;
    
    --transition-fast: 150ms ease;
    --transition-base: 250ms ease;
    --transition-slow: 350ms ease;
}

/* Dark Theme */
[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-tertiary: #94a3b8;
    --border-color: #334155;
}

/* ==================== RESET & BASE ==================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Cairo', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, var(--bg-secondary) 0%, var(--bg-tertiary) 100%);
    color: var(--text-primary);
    min-height: 100vh;
    padding: 1rem;
    transition: background var(--transition-base), color var(--transition-base);
    overflow-x: hidden;
}

/* ==================== CONTAINER ==================== */
.container {
    max-width: 600px;
    margin: 0 auto;
    animation: fadeIn 0.5s ease;
}

/* ==================== HEADER ==================== */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: var(--bg-primary);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
}

.logo {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.logo-icon {
    width: 2.5rem;
    height: 2.5rem;
    stroke-width: 2.5;
    color: var(--primary);
    animation: rotate 3s linear infinite;
}

.logo h1 {
    font-size: 1.75rem;
    font-weight: 900;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.theme-toggle {
    width: 3rem;
    height: 3rem;
    border: none;
    background: var(--bg-secondary);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-base);
    box-shadow: var(--shadow-md);
}

.theme-toggle:hover {
    transform: scale(1.1) rotate(15deg);
    box-shadow: var(--shadow-lg);
}

.theme-toggle svg {
    width: 1.5rem;
    height: 1.5rem;
    stroke-width: 2;
    color: var(--primary);
}

/* ==================== MODE SELECTION ==================== */
.mode-selection {
    background: var(--bg-primary);
    padding: 2rem;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    animation: slideUp 0.5s ease;
}

.mode-selection h2 {
    text-align: center;
    margin-bottom: 2rem;
    font-size: 1.75rem;
    font-weight: 700;
}

.mode-cards {
    display: grid;
    gap: 1.5rem;
}

.mode-card {
    padding: 2rem;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 2px solid transparent;
    cursor: pointer;
    transition: all var(--transition-base);
    text-align: center;
}

.mode-card:hover {
    transform: translateY(-5px);
    border-color: var(--primary);
    box-shadow: var(--shadow-lg);
}

.mode-icon {
    width: 4rem;
    height: 4rem;
    margin: 0 auto 1rem;
    color: var(--primary);
    stroke-width: 2;
}

.mode-card h3 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
    font-weight: 700;
}

.mode-card p {
    color: var(--text-secondary);
    margin-bottom: 1rem;
}

.difficulty-select {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    margin-top: 1rem;
}

/* ==================== GAME CONTAINER ==================== */
.game-container {
    animation: slideUp 0.5s ease;
}

.game-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-bottom: 2rem;
    padding: 1.5rem;
    background: var(--bg-primary);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
}

.player-info {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex: 1;
}

.player-avatar {
    width: 3.5rem;
    height: 3.5rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.75rem;
    font-weight: 900;
    box-shadow: var(--shadow-md);
    transition: all var(--transition-base);
}

.x-avatar {
    background: linear-gradient(135deg, var(--primary), var(--primary-light));
    color: white;
}

.o-avatar {
    background: linear-gradient(135deg, var(--secondary), #f472b6);
    color: white;
}

.player-info.active .player-avatar {
    transform: scale(1.15);
    box-shadow: var(--shadow-xl);
    animation: pulse 1s ease infinite;
}

.player-details {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.player-name {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.player-score {
    font-size: 1.5rem;
    font-weight: 900;
    color: var(--text-primary);
}

.turn-indicator {
    text-align: center;
    padding: 1rem;
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
}

.turn-text {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.current-player {
    font-size: 2rem;
    font-weight: 900;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* ==================== GAME BOARD ==================== */
.board-container {
    position: relative;
    margin-bottom: 2rem;
}

.game-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    background: var(--bg-primary);
    padding: 1.5rem;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
}

.cell {
    aspect-ratio: 1;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    font-weight: 900;
    transition: all var(--transition-fast);
    position: relative;
    overflow: hidden;
}

.cell::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, var(--primary-light), var(--secondary));
    opacity: 0;
    transition: opacity var(--transition-base);
}

.cell:hover::before {
    opacity: 0.1;
}

.cell:hover {
    transform: scale(0.95);
}

.cell.disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

.cell.x {
    color: var(--primary);
    animation: popIn 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.cell.o {
    color: var(--secondary);
    animation: popIn 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.cell.winner {
    background: linear-gradient(135deg, var(--success), #34d399);
    color: white;
    animation: winner 0.5s ease forwards;
}

.win-line {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

/* ==================== GAME CONTROLS ==================== */
.game-controls {
    display: flex;
    gap: 1rem;
    margin-bottom: 2rem;
}

.btn {
    flex: 1;
    padding: 1rem 1.5rem;
    border: none;
    border-radius: var(--radius-lg);
    font-size: 1rem;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all var(--transition-base);
    box-shadow: var(--shadow-md);
}

.btn svg {
    width: 1.25rem;
    height: 1.25rem;
    stroke-width: 2.5;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

.btn-secondary {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 2px solid var(--border-color);
}

.btn-secondary:hover {
    background: var(--bg-tertiary);
    transform: translateY(-2px);
}

.btn-sm {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
}

.btn:active {
    transform: scale(0.95);
}

/* ==================== STATS ==================== */
.stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    padding: 1.5rem;
    background: var(--bg-primary);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-lg);
}

.stat-item {
    text-align: center;
    padding: 1rem;
    background: var(--bg-secondary);
    border-radius: var(--radius-md);
}

.stat-label {
    display: block;
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.stat-value {
    display: block;
    font-size: 1.5rem;
    font-weight: 900;
    color: var(--primary);
}

/* ==================== MODAL ==================== */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
    animation: fadeIn 0.3s ease;
}

.modal-content {
    background: var(--bg-primary);
    padding: 2.5rem;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    max-width: 400px;
    width: 100%;
    animation: scaleIn 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    position: relative;
    overflow: hidden;
}

.modal-header {
    text-align: center;
    margin-bottom: 2rem;
}

.confetti {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100%;
    pointer-events: none;
    overflow: hidden;
}

.result-icon {
    width: 5rem;
    height: 5rem;
    margin: 0 auto 1rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    animation: bounceIn 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.result-icon.win {
    background: linear-gradient(135deg, var(--success), #34d399);
}

.result-icon.draw {
    background: linear-gradient(135deg, var(--warning), #fbbf24);
}

#resultTitle {
    font-size: 2rem;
    font-weight: 900;
    margin-bottom: 0.5rem;
}

#resultMessage {
    color: var(--text-secondary);
    font-size: 1.125rem;
}

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* ==================== SETTINGS ==================== */
.settings {
    position: fixed;
    bottom: 2rem;
    left: 2rem;
    z-index: 100;
}

.settings-btn {
    width: 3.5rem;
    height: 3.5rem;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-xl);
    transition: all var(--transition-base);
}

.settings-btn:hover {
    transform: scale(1.1) rotate(90deg);
}

.settings-btn svg {
    width: 1.75rem;
    height: 1.75rem;
    stroke-width: 2;
}

.settings-panel {
    position: fixed;
    bottom: 6rem;
    left: 2rem;
    background: var(--bg-primary);
    padding: 1.5rem;
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    min-width: 250px;
    animation: slideUp 0.3s ease;
}

.settings-content h3 {
    margin-bottom: 1rem;
    font-weight: 700;
}

.setting-item {
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border-color);
}

.setting-item:last-of-type {
    border-bottom: none;
}

.setting-item label {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
}

.setting-item input[type="checkbox"] {
    width: 2.5rem;
    height: 1.5rem;
    cursor: pointer;
    appearance: none;
    background: var(--bg-tertiary);
    border-radius: 1rem;
    position: relative;
    transition: all var(--transition-base);
}

.setting-item input[type="checkbox"]::before {
    content: '';
    position: absolute;
    width: 1.25rem;
    height: 1.25rem;
    border-radius: 50%;
    background: white;
    top: 50%;
    right: 0.125rem;
    transform: translateY(-50%);
    transition: all var(--transition-base);
    box-shadow: var(--shadow-md);
}

.setting-item input[type="checkbox"]:checked {
    background: var(--primary);
}

.setting-item input[type="checkbox"]:checked::before {
    right: calc(100% - 1.375rem);
}

#resetStats {
    width: 100%;
    margin-top: 1rem;
}

/* ==================== INSTALL PROMPT ==================== */
.install-prompt {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-primary);
    padding: 1.5rem;
    box-shadow: 0 -4px 6px -1px rgb(0 0 0 / 0.1);
    z-index: 999;
    animation: slideUpPrompt 0.3s ease;
}

.install-content {
    max-width: 600px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 1rem;
}

.install-icon {
    width: 2.5rem;
    height: 2.5rem;
    color: var(--primary);
    stroke-width: 2;
    flex-shrink: 0;
}

.install-content h3 {
    font-size: 1.125rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
}

.install-content p {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.install-actions {
    display: flex;
    gap: 0.5rem;
    margin-right: auto;
}

/* ==================== AD CONTAINER ==================== */
.ad-container {
    text-align: center;
    margin: 1.5rem 0;
    padding: 1rem;
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    border: 2px dashed var(--border-color);
    min-height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ad-container ins {
    display: block !important;
}

/* ==================== UTILITY CLASSES ==================== */
.hidden {
    display: none !important;
}

/* ==================== ANIMATIONS ==================== */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideUpPrompt {
    from {
        transform: translateY(100%);
    }
    to {
        transform: translateY(0);
    }
}

@keyframes scaleIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes popIn {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes winner {
    0%, 100% {
        transform: scale(1);
    }
    25% {
        transform: scale(1.1) rotate(-5deg);
    }
    75% {
        transform: scale(1.1) rotate(5deg);
    }
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.7;
    }
}

@keyframes rotate {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

@keyframes bounceIn {
    0% {
        transform: scale(0) rotate(-180deg);
        opacity: 0;
    }
    60% {
        transform: scale(1.2) rotate(20deg);
    }
    100% {
        transform: scale(1) rotate(0);
        opacity: 1;
    }
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 640px) {
    body {
        padding: 0.5rem;
    }
    
    .header {
        padding: 1rem;
        margin-bottom: 1rem;
    }
    
    .logo h1 {
        font-size: 1.5rem;
    }
    
    .logo-icon {
        width: 2rem;
        height: 2rem;
    }
    
    .mode-selection {
        padding: 1.5rem;
    }
    
    .mode-selection h2 {
        font-size: 1.5rem;
    }
    
    .mode-icon {
        width: 3rem;
        height: 3rem;
    }
    
    .game-info {
        flex-wrap: wrap;
        padding: 1rem;
    }
    
    .player-avatar {
        width: 3rem;
        height: 3rem;
        font-size: 1.5rem;
    }
    
    .game-board {
        padding: 1rem;
        gap: 0.75rem;
    }
    
    .cell {
        font-size: 2.5rem;
    }
    
    .stats {
        grid-template-columns: 1fr;
        padding: 1rem;
    }
    
    .modal-content {
        padding: 2rem;
    }
    
    .settings {
        bottom: 1rem;
        left: 1rem;
    }
    
    .settings-panel {
        bottom: 4.5rem;
        left: 1rem;
        right: 1rem;
        max-width: calc(100% - 2rem);
    }
    
    .install-content {
        flex-wrap: wrap;
    }
    
    .install-actions {
        width: 100%;
        margin-top: 0.5rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}