/* CSS Variables */
:root {
    --bg-dark: #0a0a0f;
    --bg-card: #12121a;
    --bg-elevated: #1a1a25;
    --cyan: #00d4ff;
    --coral: #ff6b6b;
    --green: #2ecc71;
    --blue: #3498db;
    --red: #e74c3c;
    --white: #ffffff;
    --gray: #8a8a9a;
    --border: #2a2a3a;
    --grid-line: #2a2a3a;
    --cell-bg: #15151f;
    --cell-hover: #1e1e2a;
}

/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Space Grotesk', sans-serif;
    background: var(--bg-dark);
    color: var(--white);
    min-height: 100vh;
    overflow-x: hidden;
}

/* Animated Background */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: 
        radial-gradient(ellipse at 20% 20%, rgba(0, 212, 255, 0.08) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 80%, rgba(255, 107, 107, 0.08) 0%, transparent 50%),
        radial-gradient(ellipse at 50% 50%, rgba(46, 204, 113, 0.05) 0%, transparent 60%);
    pointer-events: none;
    z-index: -1;
}

/* Pages */
.page {
    display: none;
    min-height: 100vh;
    flex-direction: column;
}

.page.active {
    display: flex;
}

/* Landing Page */
.landing-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    animation: fadeInUp 0.8s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Game Title - Size Reduced */
.game-title {
    font-family: 'Orbitron', sans-serif;
    font-size: clamp(1.8rem, 5.5vw, 3rem); 
    font-weight: 800;
    letter-spacing: 0.1em;
    margin-bottom: 0.5rem;
    text-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
}

.cyan { color: var(--cyan); }
.coral { color: var(--coral); }
.white { color: var(--white); }

.cyan-bg { background-color: var(--cyan); }
.coral-bg { background-color: var(--coral); }

.tagline {
    font-size: 0.8rem; 
    color: var(--gray);
    margin-bottom: 2rem; 
    letter-spacing: 0.15em;
    text-transform: uppercase;
}

/* Setup Section */
.setup-section {
    width: 100%;
    max-width: 550px; 
    display: flex;
    flex-direction: column;
    gap: 1.5rem; 
}

/* Player Inputs */
.player-inputs {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.player-input {
    width: 100%;
    max-width: 350px;   /* controls size */
    min-width: unset;   /* remove stretching */

    display: flex;
    align-items: center;
    gap: 0.6rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.5rem 0.8rem;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.player-input:focus-within {
    border-color: var(--cyan);
    box-shadow: 0 0 20px rgba(0, 212, 255, 0.15);
}

.player-input:nth-child(2):focus-within {
    border-color: var(--coral);
    box-shadow: 0 0 20px rgba(255, 107, 107, 0.15);
}

.player-indicator {
    width: 16px; 
    height: 16px;
    border-radius: 50%;
    flex-shrink: 0;
}

.player-input input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--white);
    font-family: inherit;
    font-size: 0.9rem; 
}

.player-input input::placeholder {
    color: var(--gray);
}

/* Grid Selection */
.grid-selection h3 {
    font-size: 0.85rem; 
    color: var(--gray);
    margin-bottom: 0.8rem; 
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.grid-options {
    display: flex;
    gap: 0.8rem;
    justify-content: center;
    flex-wrap: wrap;
}

.grid-option {
    background: var(--bg-card);
    border: 2px solid var(--border);
    border-radius: 12px; 
    padding: 0.9rem; 
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.6rem; 
    min-width: 85px; 
}

.grid-option:hover {
    border-color: var(--green);
    background: var(--bg-elevated);
    transform: translateY(-2px); 
}

.grid-option.selected {
    border-color: var(--green);
    background: rgba(46, 204, 113, 0.1);
    box-shadow: 0 0 20px rgba(46, 204, 113, 0.2);
}

.grid-preview {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2px;
    width: 36px; 
    height: 36px;
}

.grid-preview.grid-5 {
    grid-template-columns: repeat(5, 1fr);
    width: 44px; 
    height: 44px;
}

.grid-preview.grid-7 {
    grid-template-columns: repeat(7, 1fr);
    width: 52px; 
    height: 52px;
}

.grid-preview span {
    background: var(--border);
    border-radius: 2px;
}

.grid-option.selected .grid-preview span {
    background: var(--green);
}

.grid-label {
    font-size: 0.8rem; /* Reduced from 0.9rem */
    font-weight: 600;
    color: var(--white);
}

/* Buttons - Sizes Reduced */
.btn {
    font-family: 'Space Grotesk', sans-serif;
    font-size: 0.85rem; 
    font-weight: 600;
    padding: 0.7rem 1.4rem; 
    border: none;
    border-radius: 10px; 
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.btn-green {
    background: var(--green);
    color: var(--white);
}

.btn-green:hover {
    background: #27ae60;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(46, 204, 113, 0.3);
}

.btn-blue {
    background: var(--blue);
    color: var(--white);
}

.btn-blue:hover {
    background: #2980b9;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(52, 152, 219, 0.3);
}

.btn-red {
    background: var(--red);
    color: var(--white);
}

.btn-red:hover {
    background: #c0392b;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(231, 76, 60, 0.3);
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.8rem; 
    align-items: center;
}

.action-buttons .btn {
    width: 100%;
    max-width: 200px; 
}

/* Footer */
footer {
    padding: 1.2rem;
    display: flex;
    justify-content: center; 
    align-items: center;     
    width: 100%;
    color: var(--gray);
    font-size: 0.85rem;
    margin: 0; 
}

footer p {
    margin: 0;
}

.footer-link {
    color: var(--green);
    text-decoration: none;
    font-weight: 600;
    transition: color 0.3s ease;
    margin-left: 0.3rem; 
}

.footer-link:hover {
    color: #27ae60;
    text-decoration: underline;
}

.footer-link:hover {
    color: #27ae60;
    text-decoration: underline;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(5px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    animation: fadeIn 0.3s ease;
}

.modal.active {
    display: flex;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 16px; 
    padding: 1.5rem; 
    max-width: 400px; 
    width: 100%;
    text-align: center;
    animation: slideUp 0.4s ease;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-content h2 {
    font-family: 'Orbitron', sans-serif;
    font-size: 1.2rem; 
    margin-bottom: 0.8rem;
}

.modal-content p {
    color: var(--gray);
    margin-bottom: 1.2rem;
    font-size: 0.9rem;
}

.modal-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.modal-buttons .btn {
    width: 100%;
}

/* Rules Modal */
.rules-list {
    text-align: left;
    margin-bottom: 1.2rem;
}

.rule-item {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    padding: 0.8rem; 
    background: var(--bg-elevated);
    border-radius: 10px;
    margin-bottom: 0.6rem;
}

.rule-grid {
    font-weight: 700;
    color: var(--green);
    font-size: 0.9rem;
}

.rule-desc {
    color: var(--gray);
    font-size: 0.85rem; 
}

.rule-desc strong {
    color: var(--white);
}

.rules-note {
    font-size: 0.8rem; 
    padding: 0.6rem;
    background: var(--bg-elevated);
    border-radius: 8px;
}

/* Game Page */
#game-page {
    background: var(--bg-dark);
}

.game-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.8rem 1.2rem;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
    gap: 0.8rem;
}

.game-name {
    font-family: 'Orbitron', sans-serif;
    font-size: clamp(0.9rem, 2.5vw, 1.2rem); 
    font-weight: 700;
    letter-spacing: 0.05em;
}

.turn-indicator {
    flex: 1;
    display: flex;
    justify-content: center;
}

.turn-content {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 1rem;
    background: var(--bg-elevated);
    border-radius: 50px;
    border: 1px solid var(--border);
}

#turn-text {
    font-weight: 500;
    font-size: 0.85rem;
}

.game-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1.2rem;
    gap: 1.2rem;
}

/* Player Symbol (X and O) Styles */
.player-symbol {
    font-family: 'Orbitron', sans-serif;
    font-weight: 900;
    font-size: 1.1rem;
    min-width: 20px;
    text-align: center;
}

.player-symbol.cyan {
    color: var(--cyan);
}

.player-symbol.coral {
    color: var(--coral);
}

/* Score Board */
.score-board {
    display: flex;
    gap: 1.5rem;
    flex-wrap: wrap;
    justify-content: center;
}

.score-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 1rem;
    background: var(--bg-card);
    border-radius: 10px;
    border: 1px solid var(--border);
    font-size: 0.9rem;
}

.score {
    font-family: 'Orbitron', sans-serif;
    font-size: 1.1rem; 
    font-weight: 700;
    margin-left: 0.3rem;
}

.player1-score .score {
    color: var(--cyan);
}

.player2-score .score {
    color: var(--coral);
}

/* Game Grid */
.game-grid {
    display: grid;
    gap: 5px;
    background: var(--grid-line);
    padding: 5px;
    border-radius: 14px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
}

.game-grid.size-3 {
    grid-template-columns: repeat(3, 1fr);
    width: min(320px, 88vw);
}

.game-grid.size-5 {
    grid-template-columns: repeat(5, 1fr);
    width: min(360px, 90vw);
}

.game-grid.size-7 {
    grid-template-columns: repeat(7, 1fr);
    width: min(400px, 92vw);
}

.cell {
    aspect-ratio: 1;
    background: var(--cell-bg);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Orbitron', sans-serif;
    font-size: clamp(1.3rem, 4.5vw, 2rem); 
    font-weight: 900;
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
}

.cell:hover:not(.taken) {
    background: var(--cell-hover);
    transform: scale(1.02);
}

.cell.taken {
    cursor: not-allowed;
}

.cell.x {
    color: var(--cyan);
    text-shadow: 0 0 15px rgba(0, 212, 255, 0.5);
}

.cell.o {
    color: var(--coral);
    text-shadow: 0 0 15px rgba(255, 107, 107, 0.5);
}

.cell.winning {
    animation: winPulse 0.6s ease infinite alternate;
}

@keyframes winPulse {
    0% {
        transform: scale(1);
        box-shadow: 0 0 8px currentColor;
    }
    100% {
        transform: scale(1.05);
        box-shadow: 0 0 20px currentColor;
    }
}

.cell.x.winning {
    background: rgba(0, 212, 255, 0.15);
    border: 2px solid var(--cyan);
}

.cell.o.winning {
    background: rgba(255, 107, 107, 0.15);
    border: 2px solid var(--coral);
}

/* Cell Animation */
.cell.animate-in {
    animation: cellPop 0.3s ease;
}

@keyframes cellPop {
    0% {
        transform: scale(0.8);
        opacity: 0;
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Responsive */
@media (max-width: 600px) {
    .landing-container {
        padding: 1.2rem;
        justify-content: flex-start;
        padding-top: 2.5rem;
    }
    
    .game-title {
        margin-bottom: 0.2rem;
    }
    
    .tagline {
        margin-bottom: 1.5rem;
        font-size: 0.8rem;
    }
    
    .player-inputs {
        flex-direction: column;
    }
    
    .player-input {
        width: 100%;
    }
    
    .grid-options {
        gap: 0.6rem;
    }
    
    .grid-option {
        padding: 0.7rem;
        min-width: 75px;
    }
    
    .grid-preview {
        width: 30px;
        height: 30px;
    }
    
    .grid-preview.grid-5 {
        width: 38px;
        height: 38px;
    }
    
    .grid-preview.grid-7 {
        width: 46px;
        height: 46px;
    }
    
    .grid-label {
        font-size: 0.75rem;
    }
    
    /* Mobile Button Sizing Fixes */
    .btn {
        padding: 0.5rem 1rem; /* Reduced from 0.65rem 1.2rem */
        font-size: 0.75rem; /* Reduced from 0.8rem */
        white-space: nowrap; /* Prevents text wrapping */
    }
    
    /* Specific sizing for header buttons (e.g., Back button) */
    .game-header .btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.7rem;
    }
    
    .game-header {
        padding: 0.6rem 0.8rem;
    }
    
    .game-main {
        padding: 0.8rem;
        gap: 0.8rem;
    }
    
    .score-board {
        gap: 0.8rem;
    }
    
    .score-item {
        padding: 0.4rem 0.8rem;
        font-size: 0.8rem;
    }
    
    .game-grid {
        gap: 3px;
        padding: 3px;
    }
    
    .cell {
        font-size: clamp(1rem, 3.5vw, 1.8rem);
    }
    
    .modal-content {
        padding: 1.2rem;
    }
    
    .modal-content h2 {
        font-size: 1.1rem;
    }
    
    #finish-btn {
        font-size: 0.7rem;
        padding: 0.4rem 0.8rem;
    }
}

@media (max-width: 380px) {
    .game-header {
        justify-content: center;
    }
    
    .game-name {
        order: 1;
        width: 100%;
        text-align: center;
    }
    
    .turn-indicator {
        order: 2;
    }
    
    #finish-btn {
        order: 3;
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}