:root {
    --bg-color-light: #e0e5ec;
    --primary-color-light: #3a3a3a;
    --shadow-light-1: #ffffff;
    --shadow-light-2: #a3b1c6;
    
    --bg-color-dark: #2c3e50;
    --primary-color-dark: #ecf0f1;
    --shadow-dark-1: #34495e;
    --shadow-dark-2: #243342;

    --player-x-color: #3498db;
    --player-o-color: #e74c3c;
    --active-color: #2ecc71;

    --transition-speed: 0.3s;
    --font-family: 'Poppins', sans-serif;
}

body {
    font-family: var(--font-family);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    background-color: var(--bg-color-light);
    color: var(--primary-color-light);
    transition: background-color var(--transition-speed), color var(--transition-speed);
    overflow: hidden; /* Prevents scrolling */
}

body.dark-mode {
    background-color: var(--bg-color-dark);
    color: var(--primary-color-dark);
}

.main-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    padding: 2rem;
    width: 100%;
    max-width: 400px;
}

.header {
    text-align: center;
    width: 100%;
}

h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 1rem;
}

.controls-container {
    display: flex;
    justify-content: space-around;
    align-items: center;
    width: 100%;
}

.icon-button {
    border: none;
    outline: none;
    cursor: pointer;
    font-size: 1.2rem;
    width: 60px; /* Fixed width */
    height: 60px; /* Fixed height */
    border-radius: 20px;
    background: var(--bg-color-light);
    box-shadow: -7px -7px 14px var(--shadow-light-1), 7px 7px 14px var(--shadow-light-2);
    transition: all var(--transition-speed) ease;
    color: var(--primary-color-light);
    display: flex; /* Added for centering */
    justify-content: center; /* Added for centering */
    align-items: center; /* Added for centering */
}

body.dark-mode .icon-button {
    background: var(--bg-color-dark);
    box-shadow: -7px -7px 14px var(--shadow-dark-1), 7px 7px 14px var(--shadow-dark-2);
    color: var(--primary-color-dark);
}

.icon-button:hover {
    transform: translateY(-3px);
}

.icon-button:active {
    transform: translateY(0);
    box-shadow: inset -5px -5px 10px var(--shadow-light-1), inset 5px 5px 10px var(--shadow-light-2);
}
body.dark-mode .icon-button:active {
    box-shadow: inset -5px -5px 10px var(--shadow-dark-1), inset 5px 5px 10px var(--shadow-dark-2);
}

#player-mode-toggle .two-player-icon { display: none; }
body.two-player-mode #player-mode-toggle .one-player-icon { display: none; }
body.two-player-mode #player-mode-toggle .two-player-icon { display: inline-block; }


.game-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 15px;
    /* Increased size */
    width: clamp(330px, 90vw, 400px);
    height: clamp(330px, 90vw, 400px);
}

.cell {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 3.5rem; /* Increased font size for larger cells */
    font-weight: bold;
    cursor: pointer;
    border-radius: 15px;
    background: var(--bg-color-light);
    box-shadow: -7px -7px 14px var(--shadow-light-1), 7px 7px 14px var(--shadow-light-2);
    transition: all var(--transition-speed) ease;
}

body.dark-mode .cell {
    background: var(--bg-color-dark);
    box-shadow: -7px -7px 14px var(--shadow-dark-1), 7px 7px 14px var(--shadow-dark-2);
}

.cell:not(.X):not(.O):hover { transform: scale(1.03); }

.cell.X, .cell.O {
    cursor: not-allowed;
    box-shadow: inset -5px -5px 10px var(--shadow-light-1), inset 5px 5px 10px var(--shadow-light-2);
    animation: place-mark 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
body.dark-mode .cell.X, body.dark-mode .cell.O {
    box-shadow: inset -5px -5px 10px var(--shadow-dark-1), inset 5px 5px 10px var(--shadow-dark-2);
}

@keyframes place-mark {
    from { transform: scale(0.7); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.cell.X { color: var(--player-x-color); }
.cell.O { color: var(--player-o-color); }

.cell.fade-out { animation: fade-out 0.5s ease-out forwards; }
@keyframes fade-out {
    from { opacity: 1; transform: scale(1); }
    to { opacity: 0; transform: scale(0.8); }
}

.status-display {
    font-size: 1.5rem;
    font-weight: 500;
    min-height: 27px; /* Reserve space */
}

/* Timer styles */
.timer-display {
    font-size: 1rem;
    color: var(--player-o-color);
    min-height: 20px; /* Reserve space */
    visibility: hidden; /* Use visibility to prevent layout shift */
}
.timer-display.visible {
    visibility: visible; /* Show timer */
}


.bottom-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    gap: 1rem;
}

.restart-button {
    font-family: var(--font-family);
    font-size: 1.1rem;
    padding: 1rem;
    border-radius: 20px;
    background: var(--bg-color-light);
    box-shadow: -7px -7px 14px var(--shadow-light-1), 7px 7px 14px var(--shadow-light-2);
    transition: all var(--transition-speed) ease;
    color: var(--primary-color-light);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    border: none;
    cursor: pointer;
}
body.dark-mode .restart-button {
    background: var(--bg-color-dark);
    box-shadow: -7px -7px 14px var(--shadow-dark-1), 7px 7px 14px var(--shadow-dark-2);
    color: var(--primary-color-dark);
}
.restart-button:hover { transform: translateY(-3px); }
.restart-button:active {
    transform: translateY(0);
    box-shadow: inset -5px -5px 10px var(--shadow-light-1), inset 5px 5px 10px var(--shadow-light-2);
}
body.dark-mode .restart-button:active {
    box-shadow: inset -5px -5px 10px var(--shadow-dark-1), inset 5px 5px 10px var(--shadow-dark-2);
}

.difficulty-controls {
    display: flex;
    gap: 0.5rem;
    background: var(--bg-color-light);
    padding: 0.5rem;
    border-radius: 20px;
    box-shadow: inset -5px -5px 10px var(--shadow-light-1), inset 5px 5px 10px var(--shadow-light-2);
    transition: all var(--transition-speed) ease;
}
body.dark-mode .difficulty-controls {
    background: var(--bg-color-dark);
    box-shadow: inset -5px -5px 10px var(--shadow-dark-1), inset 5px 5px 10px var(--shadow-dark-2);
}

.difficulty-btn {
    border: none;
    font-family: var(--font-family);
    font-size: 0.9rem;
    padding: 0.75rem 1rem;
    border-radius: 15px;
    cursor: pointer;
    background: transparent;
    color: var(--primary-color-light);
    transition: all var(--transition-speed) ease;
}
body.dark-mode .difficulty-btn {
    color: var(--primary-color-dark);
}

.difficulty-btn.active {
    background: var(--bg-color-light);
    color: var(--active-color);
    font-weight: bold;
    box-shadow: -5px -5px 10px var(--shadow-light-1), 5px 5px 10px var(--shadow-light-2);
}
body.dark-mode .difficulty-btn.active {
    background: var(--bg-color-dark);
    box-shadow: -5px -5px 10px var(--shadow-dark-1), 5px 5px 10px var(--shadow-dark-2);
}

/* Hide difficulty controls by default */
#difficulty-controls { display: none; }
/* Show them in single player mode */
body.single-player-mode #difficulty-controls { display: flex; }

.screen-too-narrow {
    display: none;
    text-align: center;
    padding: 20px;
    font-size: 1.2rem;
}
@media (max-width: 380px) {
    .main-container { display: none; }
    .screen-too-narrow { display: block; }
}