@import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;600;700&display=swap');

:root {
    --bg-color: #1a2a33;
    --board-bg: #1f3641;
    --text-color: #a8bec9;
    --accent-color-1: #31c3bd; /* Teal - X */
    --accent-color-2: #f2b137; /* Yellow/Orange - O */
    --danger-color: #db4455;
    --success-color: #32de84;
    --shadow-dark: #10212a;
    --font-family: 'Montserrat', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    height: 100dvh;
    padding: 0.5rem;
}

/* --- Background Animation --- */
#background-animation {
    position: fixed; 
    top: -10%; 
    left: -10%;
    width: 120%; 
    height: 120%;
    display: grid; 
    grid-template-columns: repeat(9, 1fr);
    gap: 4px; 
    z-index: 0; 
    opacity: 0.08;
    pointer-events: none;
}
.bg-cell {
    background-color: var(--board-bg);
    border-radius: 4px;
    display: flex; 
    align-items: center; 
    justify-content: center;
}
.bg-cell .x { font-weight: 700; font-size: 2rem; color: var(--accent-color-1); transition: opacity 2s ease-out; }
.bg-cell .o { font-weight: 700; font-size: 2rem; color: var(--accent-color-2); transition: opacity 2s ease-out; }

/* --- Game Setup / Home Screen --- */
.game-setup {
    position: relative;
    z-index: 1;
    text-align: center;
    width: 100%;
    max-width: 420px;
    padding: 1rem;
}

.setup-content {
    background: var(--board-bg);
    padding: 2.5rem 1.5rem;
    border-radius: 20px;
    box-shadow: 0 8px 0 var(--shadow-dark);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.setup-content h1 {
    font-size: clamp(1.8rem, 7vw, 2.4rem);
    color: var(--accent-color-1);
    font-weight: 700;
}

.setup-content .subtitle {
    font-size: clamp(0.9rem, 3.5vw, 1.1rem);
    color: var(--text-color);
    margin-bottom: 1rem;
}

.setup-content .buttons {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    width: 100%;
}

/* --- Main Game Container --- */
.game-container {
    width: 100%;
    max-width: 450px;
    height: 100%;
    max-height: 750px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    position: relative;
    z-index: 1;
    gap: 0.5rem;
}

.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 0.5rem;
    flex-shrink: 0;
}

.status-display {
    display: flex;
    gap: 0.5rem;
    background: var(--board-bg);
    padding: 0.4rem 0.75rem;
    border-radius: 10px;
    box-shadow: inset 0 -2px 0 var(--shadow-dark);
}

.player-info {
    font-weight: 700;
    transition: all 0.2s ease;
    opacity: 0.4;
    font-size: clamp(0.75rem, 3.2vw, 0.95rem);
    color: var(--text-color);
}

.player-info.active-player {
    opacity: 1;
    transform: scale(1.02);
}

#player-x-display.active-player { color: var(--accent-color-1); }
#player-o-display.active-player { color: var(--accent-color-2); }

.game-header .buttons {
    display: flex;
    gap: 0.4rem;
}

/* --- Responsive Game Board --- */
.main-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 6px;
    background-color: var(--shadow-dark);
    padding: 6px;
    border-radius: 16px;
    width: 100%;
    aspect-ratio: 1 / 1;
    max-height: 80vw;
}

@media (min-height: 700px) {
    .main-board {
        max-height: 420px;
    }
}

.small-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 3px;
    position: relative;
    border-radius: 10px;
    border: 2px solid transparent;
    transition: all 0.2s ease;
    background-color: var(--board-bg);
    padding: 3px;
}

.cell {
    background-color: var(--bg-color);
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background-color 0.2s ease;
    touch-action: manipulation;
    position: relative;
    font-size: clamp(1rem, 4.5vw, 1.8rem);
    font-weight: 700;
    width: 100%;
    height: 100%;
}

.cell:not(.x):not(.o):active {
    background-color: #2c4a58;
}

.cell.x { color: var(--accent-color-1); }
.cell.o { color: var(--accent-color-2); }

.cell .mark {
    animation: pop-in 0.25s ease;
}

/* --- Board Highlights & Overlay States --- */
.small-board.active-board {
    border-color: var(--accent-color-1);
    box-shadow: 0 0 8px rgba(49, 195, 189, 0.4);
}

.small-board.next-move-indicator {
    border-color: rgba(168, 190, 201, 0.5);
    border-style: dashed;
}

.cell.last-move::after {
    content: '';
    position: absolute;
    top: -2px; left: -2px; right: -2px; bottom: -2px;
    border-radius: 4px;
    animation: flash 0.8s ease-out;
    pointer-events: none;
}

@keyframes flash {
    0%, 100% { border: 2px solid transparent; }
    50% { border: 2px solid var(--accent-color-1); }
}

.small-board.won {
    opacity: 0.85;
}

.small-board.won::after {
    content: attr(data-winner);
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(31, 54, 65, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(2.5rem, 10vw, 3.5rem);
    font-weight: 700;
    border-radius: 8px;
    animation: pop-in 0.3s ease;
    z-index: 10;
}

.small-board.won.x-win::after { color: var(--accent-color-1); }
.small-board.won.o-win::after { color: var(--accent-color-2); }

@keyframes pop-in {
    from { transform: scale(0.5); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/* --- Footer --- */
.game-footer {
    height: 30px;
    flex-shrink: 0;
}

.status-text {
    font-size: clamp(0.85rem, 3.5vw, 1rem);
    font-weight: 700;
    text-align: center;
    color: var(--success-color);
}

/* --- Buttons --- */
button {
    font-family: var(--font-family);
    font-size: clamp(0.75rem, 2.5vw, 0.9rem);
    font-weight: 700;
    text-transform: uppercase;
    padding: 0.6rem 0.8rem;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.15s ease;
    background-color: var(--accent-color-2);
    color: var(--bg-color);
    border-bottom: 4px solid #cc8b13;
    touch-action: manipulation;
}

button:active {
    transform: translateY(2px);
    border-bottom-width: 2px;
}

#rules-btn, #restart-btn {
    background-color: var(--text-color);
    color: var(--bg-color);
    border-bottom: 4px solid #6B8997;
}

#vs-ai-btn {
    background-color: var(--accent-color-1);
    color: var(--bg-color);
    border-bottom: 4px solid #118c87;
}

#confirm-restart-btn {
    background-color: var(--danger-color);
    color: white;
    border-bottom-color: #a72836;
}

#cancel-restart-btn, #close-rules-btn {
    background-color: var(--text-color);
    color: var(--bg-color);
    border-bottom-color: #6B8997;
}

/* --- Modals & Overlay Fix --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.75);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 1rem;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

.modal-overlay.visible {
    opacity: 1;
    pointer-events: all;
}

.modal {
    background-color: var(--board-bg);
    padding: 1.25rem;
    border-radius: 16px;
    text-align: center;
    display: none; /* Managed explicitly via script.js */
    flex-direction: column;
    gap: 0.85rem;
    width: 100%;
    max-width: 360px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 10px 0 var(--shadow-dark);
}

.modal h2 {
    font-size: 1.3rem;
    color: white;
}

.modal p {
    font-size: 0.85rem;
    line-height: 1.4;
    color: var(--text-color);
    text-align: left;
}

.modal p strong {
    color: white;
}

.modal-buttons {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    margin-top: 0.5rem;
}

.modal-buttons button {
    flex: 1;
}

.hidden {
    display: none !important;
}