/* ============================================
   Nexus Tic-Tac-Toe — Professional Stylesheet
   ============================================ */

:root {
    --font: 'Cairo', sans-serif;

    --bg-primary: #0a0a1a;
    --bg-secondary: #12122a;
    --surface: rgba(255, 255, 255, 0.04);
    --surface-hover: rgba(255, 255, 255, 0.08);
    --border: rgba(255, 255, 255, 0.1);
    --text: #f0f0f5;
    --text-muted: rgba(240, 240, 245, 0.55);

    --accent-x: #ff4757;
    --accent-x-glow: rgba(255, 71, 87, 0.45);
    --accent-o: #2ed573;
    --accent-o-glow: rgba(46, 213, 115, 0.45);
    --accent-primary: #6c5ce7;
    --accent-secondary: #a29bfe;
    --accent-gold: #ffd93d;

    --radius-sm: 10px;
    --radius-md: 16px;
    --radius-lg: 24px;
    --radius-xl: 32px;

    --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
    --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    --board-size: 110px;
    --board-gap: 12px;
}

[data-theme="light"] {
    --bg-primary: #eef1f8;
    --bg-secondary: #ffffff;
    --surface: rgba(0, 0, 0, 0.03);
    --surface-hover: rgba(0, 0, 0, 0.06);
    --border: rgba(0, 0, 0, 0.1);
    --text: #1a1a2e;
    --text-muted: rgba(26, 26, 46, 0.55);
    --shadow: 0 20px 60px rgba(0, 0, 0, 0.12);
}

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text);
    min-height: 100vh;
    overflow-x: hidden;
    line-height: 1.6;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.hidden {
    display: none !important;
}

/* === Ambient Background === */
.ambient-bg {
    position: fixed;
    inset: 0;
    z-index: -2;
    overflow: hidden;
    background: linear-gradient(145deg, var(--bg-primary) 0%, var(--bg-secondary) 100%);
}

.orb {
    position: absolute;
    border-radius: 50%;
    filter: blur(90px);
    opacity: 0.35;
    animation: orbDrift 22s ease-in-out infinite;
}

.orb-1 {
    width: 450px;
    height: 450px;
    background: linear-gradient(135deg, var(--accent-x), var(--accent-gold));
    top: -180px;
    left: -120px;
}

.orb-2 {
    width: 380px;
    height: 380px;
    background: linear-gradient(135deg, var(--accent-o), #1dd1a1);
    bottom: -100px;
    right: -80px;
    animation-delay: -8s;
}

.orb-3 {
    width: 320px;
    height: 320px;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    top: 40%;
    left: 50%;
    transform: translateX(-50%);
    animation-delay: -15s;
}

@keyframes orbDrift {
    0%, 100% { transform: translate(0, 0) scale(1); }
    33% { transform: translate(60px, -80px) scale(1.08); }
    66% { transform: translate(-40px, 60px) scale(0.95); }
}

#particles, #confetti {
    position: fixed;
    inset: 0;
    pointer-events: none !important;
    z-index: 0;
}

#confetti {
    z-index: 200;
}

/* === Header === */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 2rem;
    position: relative;
    z-index: 10;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-weight: 800;
    font-size: 1.25rem;
    letter-spacing: -0.02em;
}

.logo-icon {
    color: var(--accent-primary);
    font-size: 1.5rem;
    animation: logoSpin 8s linear infinite;
}

@keyframes logoSpin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

.icon-btn {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.15rem;
    transition: var(--transition);
    backdrop-filter: blur(12px);
}

.icon-btn:hover {
    background: var(--surface-hover);
    transform: translateY(-2px);
}

[data-theme="dark"] .theme-icon--light,
[data-theme="light"] .theme-icon--dark,
.sound-muted .sound-icon--on {
    display: none;
}

/* === Main Layout === */
.app-main {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 0 1.5rem 3rem;
    min-height: calc(100vh - 80px);
    position: relative;
    z-index: 5;
}

.panel {
    position: relative;
    width: 100%;
    max-width: 560px;
    background: var(--surface);
    backdrop-filter: blur(24px) saturate(160%);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 2.5rem 2rem;
    box-shadow: var(--shadow);
    animation: panelEnter 0.6s ease-out;
}

.panel-glow {
    position: absolute;
    top: -1px;
    left: 50%;
    transform: translateX(-50%);
    width: 60%;
    height: 2px;
    background: linear-gradient(90deg, transparent, var(--accent-primary), transparent);
    border-radius: 2px;
}

@keyframes panelEnter {
    from {
        opacity: 0;
        transform: translateY(24px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.panel-title {
    font-size: 2rem;
    font-weight: 800;
    text-align: center;
    margin-bottom: 0.35rem;
    background: linear-gradient(135deg, var(--text), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.panel-subtitle {
    text-align: center;
    color: var(--text-muted);
    margin-bottom: 2rem;
    font-size: 0.95rem;
}

/* === Mode Selector === */
.mode-selector {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-bottom: 1.75rem;
}

.mode-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.35rem;
    padding: 1.1rem 0.75rem;
    border-radius: var(--radius-md);
    border: 2px solid var(--border);
    background: transparent;
    color: var(--text);
    cursor: pointer;
    transition: var(--transition);
    font-family: var(--font);
    text-align: center;
}

.mode-card:hover {
    border-color: var(--accent-primary);
    background: var(--surface-hover);
}

.mode-card.active {
    border-color: var(--accent-primary);
    background: rgba(108, 92, 231, 0.12);
    box-shadow: 0 0 24px rgba(108, 92, 231, 0.2);
}

.mode-icon {
    font-size: 1.75rem;
}

.mode-label {
    font-weight: 700;
    font-size: 0.95rem;
}

.mode-desc {
    font-size: 0.72rem;
    color: var(--text-muted);
    line-height: 1.4;
}

/* === Form === */
.form-grid {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 1.25rem;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.input-group label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
}

.input-group input {
    padding: 0.85rem 1rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-family: var(--font);
    font-size: 1rem;
    transition: var(--transition);
    outline: none;
}

.input-group input:focus {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.2);
}

.input-group input::placeholder {
    color: var(--text-muted);
}

.ai-settings {
    margin-bottom: 1.25rem;
}

.ai-settings label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.difficulty-selector {
    display: flex;
    gap: 0.5rem;
}

.diff-btn {
    flex: 1;
    padding: 0.6rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: transparent;
    color: var(--text);
    font-family: var(--font);
    font-weight: 600;
    font-size: 0.85rem;
    cursor: pointer;
    transition: var(--transition);
}

.diff-btn:hover {
    background: var(--surface-hover);
}

.diff-btn.active {
    background: var(--accent-primary);
    border-color: var(--accent-primary);
    color: #fff;
}

.form-row {
    margin-bottom: 1.75rem;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    cursor: pointer;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.checkbox-label input {
    width: 18px;
    height: 18px;
    accent-color: var(--accent-primary);
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius-sm);
    border: none;
    font-family: var(--font);
    font-weight: 700;
    font-size: 0.9rem;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none !important;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: #fff;
    box-shadow: 0 4px 20px rgba(108, 92, 231, 0.35);
}

.btn-primary:not(:disabled):hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 28px rgba(108, 92, 231, 0.45);
}

.btn-lg {
    width: 100%;
    padding: 1rem 1.5rem;
    font-size: 1.05rem;
    border-radius: var(--radius-md);
}

.btn-arrow {
    transition: transform 0.3s ease;
}

.btn-primary:not(:disabled):hover .btn-arrow {
    transform: translateX(-4px);
}

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-secondary:not(:disabled):hover {
    background: var(--surface-hover);
    transform: translateY(-1px);
}

.btn-ghost {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid transparent;
}

.btn-ghost:hover {
    color: var(--text);
    background: var(--surface);
}

.btn-accent {
    background: linear-gradient(135deg, var(--accent-x), var(--accent-gold));
    color: #fff;
}

.btn-accent:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(255, 71, 87, 0.35);
}

/* === Game Panel === */
.game-top-bar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.75rem;
}

.status-card {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.status-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
}

.status-value {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1rem;
}

.player-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    font-weight: 800;
    font-size: 0.85rem;
}

.player-badge.badge-x {
    background: var(--accent-x);
    color: #fff;
    box-shadow: 0 0 16px var(--accent-x-glow);
}

.player-badge.badge-o {
    background: var(--accent-o);
    color: #fff;
    box-shadow: 0 0 16px var(--accent-o-glow);
}

/* Timer */
.timer-ring {
    position: relative;
    width: 52px;
    height: 52px;
}

.timer-ring svg {
    width: 100%;
    height: 100%;
    transform: rotate(-90deg);
}

.timer-bg {
    fill: none;
    stroke: var(--border);
    stroke-width: 2.5;
}

.timer-progress {
    fill: none;
    stroke: var(--accent-primary);
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-dasharray: 97.4;
    stroke-dashoffset: 0;
    transition: stroke-dashoffset 1s linear, stroke 0.3s ease;
}

.timer-ring.warning .timer-progress {
    stroke: var(--accent-gold);
}

.timer-ring.danger .timer-progress {
    stroke: var(--accent-x);
}

.timer-text {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    font-weight: 800;
}

/* Scoreboard */
.scoreboard {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
}

.score-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.4rem 0.75rem;
    border-radius: var(--radius-sm);
    background: var(--surface);
    border: 1px solid var(--border);
    min-width: 56px;
}

.score-name {
    font-size: 0.65rem;
    color: var(--text-muted);
    font-weight: 600;
    max-width: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.score-num {
    font-size: 1.15rem;
    font-weight: 800;
}

.score-x .score-num { color: var(--accent-x); }
.score-o .score-num { color: var(--accent-o); }

/* === 3D Board === */
.board-wrapper {
    perspective: 900px;
    display: flex;
    justify-content: center;
    margin-bottom: 1.5rem;
}

.board-3d {
    transform-style: preserve-3d;
    transition: transform 0.15s ease-out;
    transform: rotateX(8deg);
}

.game-board {
    display: grid;
    grid-template-columns: repeat(3, var(--board-size));
    grid-template-rows: repeat(3, var(--board-size));
    gap: var(--board-gap);
    padding: 1.25rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow:
        0 25px 50px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.06);
    position: relative;
    z-index: 1;
}

.cell {
    position: relative;
    width: var(--board-size);
    height: var(--board-size);
    border: none;
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
    cursor: pointer;
    font-family: var(--font);
    font-size: 2.75rem;
    font-weight: 800;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.25s ease, box-shadow 0.25s ease, background 0.25s ease;
    outline: none;
    overflow: hidden;
    pointer-events: auto;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.cell::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255,255,255,0.08), transparent);
    opacity: 0;
    transition: opacity 0.25s ease;
    pointer-events: none;
}

.cell:not(:disabled):hover {
    transform: translateZ(12px) scale(1.04);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.cell:not(:disabled):hover::before {
    opacity: 1;
}

.cell:not(:disabled):focus-visible {
    outline: 2px solid var(--accent-primary);
    outline-offset: 2px;
}

.cell:disabled {
    cursor: default;
}

.cell.x {
    color: var(--accent-x);
    text-shadow: 0 0 20px var(--accent-x-glow);
    animation: markPop 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.cell.o {
    color: var(--accent-o);
    text-shadow: 0 0 20px var(--accent-o-glow);
    animation: markPop 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.cell.winning {
    animation: winGlow 1s ease-in-out infinite;
    z-index: 2;
}

@keyframes markPop {
    0% { transform: scale(0) rotateY(90deg); opacity: 0; }
    100% { transform: scale(1) rotateY(0); opacity: 1; }
}

@keyframes winGlow {
    0%, 100% {
        box-shadow: 0 0 20px var(--accent-gold);
        transform: translateZ(16px) scale(1);
    }
    50% {
        box-shadow: 0 0 40px var(--accent-gold);
        transform: translateZ(24px) scale(1.06);
    }
}

.cell.ai-thinking {
    animation: cellPulse 0.6s ease-in-out infinite;
}

@keyframes cellPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Move History */
.move-history {
    margin-bottom: 1.5rem;
}

.history-label {
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.history-track {
    display: flex;
    gap: 0.4rem;
    overflow-x: auto;
    padding-bottom: 0.25rem;
    scrollbar-width: thin;
}

.history-chip {
    flex-shrink: 0;
    padding: 0.25rem 0.6rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 700;
    background: var(--surface);
    border: 1px solid var(--border);
}

.history-chip.chip-x { color: var(--accent-x); }
.history-chip.chip-o { color: var(--accent-o); }

/* Controls */
.controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.6rem;
    justify-content: center;
}

/* === Modal === */
.modal {
    border: none;
    padding: 0;
    background: transparent;
    max-width: 90vw;
}

.modal::backdrop {
    background: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(6px);
}

.modal-content {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    padding: 2.5rem 2rem;
    text-align: center;
    min-width: 300px;
    max-width: 400px;
    animation: modalPop 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

@keyframes modalPop {
    from { transform: scale(0.7); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-icon {
    font-size: 3.5rem;
    margin-bottom: 0.75rem;
    animation: iconBounce 0.6s ease;
}

@keyframes iconBounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

.modal-title {
    font-size: 1.5rem;
    font-weight: 800;
    margin-bottom: 0.5rem;
}

.modal-message {
    color: var(--text-muted);
    margin-bottom: 1rem;
    font-size: 1rem;
}

.modal-stats {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.modal-stats strong {
    display: block;
    font-size: 1.25rem;
    color: var(--text);
}

.modal-actions {
    display: flex;
    gap: 0.75rem;
    justify-content: center;
}

/* === Responsive === */
@media (max-width: 640px) {
    :root {
        --board-size: 88px;
        --board-gap: 8px;
    }

    .app-header {
        padding: 1rem 1.25rem;
    }

    .panel {
        padding: 1.75rem 1.25rem;
        border-radius: var(--radius-lg);
    }

    .panel-title {
        font-size: 1.6rem;
    }

    .mode-selector {
        grid-template-columns: 1fr;
    }

    .game-top-bar {
        grid-template-columns: 1fr;
        text-align: center;
    }

    .status-card {
        align-items: center;
    }

    .scoreboard {
        justify-content: center;
    }

    .timer-ring {
        margin: 0 auto;
    }

    .cell {
        font-size: 2.2rem;
    }

    .controls .btn {
        flex: 1 1 calc(50% - 0.3rem);
        min-width: 120px;
    }
}

@media (max-width: 380px) {
    :root {
        --board-size: 76px;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .board-3d {
        transform: none !important;
    }
}
