:root {
    --board-bg: #f0f0f0;
    --cell-bg: #ffffff;
    --border-color: #cccccc;
    --border-thick: #999999;
    --player-x-color: #dc3545; /* Red */
    --player-o-color: #0d6efd; /* Blue */
    --highlight-color: rgba(40, 167, 69, 0.3); /* Greenish highlight */
    --win-bg-x: rgba(220, 53, 69, 0.3);
    --win-bg-o: rgba(13, 110, 253, 0.3);
    --draw-bg: rgba(108, 117, 125, 0.3); /* Gray */
    --win-line-color: #333;
}

body {
    font-family: sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #e9e9e9;
    margin: 0;
    padding: 10px;
    box-sizing: border-box;
}

#welcome-screen, #game-area {
    background-color: var(--board-bg);
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    max-width: 95vw; /* Limit width */
    width: 600px; /* Default width */
}

#welcome-screen h1, #welcome-screen h2 {
    text-align: center;
    margin-bottom: 15px;
}

.rules {
    background-color: #fff;
    padding: 5px 15px;
    border-radius: 5px;
    margin-bottom: 20px;
    font-size: 0.9em;
    max-height: 30vh;
    overflow-y: auto;
}
.rules li {
    margin-bottom: 5px;
}

.options {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}
.option-group {
    display: flex;
    align-items: center;
    gap: 10px;
}
select, button {
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 1em;
}
#start-button, #restart-button {
    background-color: #28a745; /* Green */
    color: white;
    cursor: pointer;
    transition: background-color 0.2s;
}
#start-button:hover, #restart-button:hover {
    background-color: #218838;
}
#restart-button {
    background-color: #ffc107; /* Yellow */
    color: #333;
}
#restart-button:hover {
     background-color: #e0a800;
}


#game-area {
    display: flex; /* Use flex for game area layout */
    flex-direction: column;
    align-items: center;
    gap: 20px; /* Space between controls and board */
}

#controls {
    width: 100%;
    text-align: center;
    background-color: #fff;
    padding: 10px;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
}
#status-message {
    font-size: 1.2em;
    font-weight: bold;
    margin-bottom: 10px;
}
.mode-display {
    font-size: 0.9em;
    color: #555;
}


#main-board-container {
    position: relative; /* Needed for absolute positioning of win line */
    width: 100%;
    max-width: 500px; /* Max size of the board */
    aspect-ratio: 1 / 1; /* Keep it square */
    background-color: var(--board-bg);
    padding: 5px; /* Small padding around the main board */
    box-sizing: border-box;
    border: 2px solid var(--border-thick);
    border-radius: 5px;
}

#main-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 5px; /* Gap between small boards */
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}

.small-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 2px; /* Gap between cells */
    background-color: var(--cell-bg);
    border: 1px solid var(--border-color);
    position: relative; /* For overlay */
    transition: background-color 0.3s ease; /* Smooth highlight */
}

.cell {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(12px, 5vmin, 30px); /* Responsive font size */
    font-weight: bold;
    background-color: var(--cell-bg);
    cursor: pointer;
    border: 1px solid var(--border-color);
    aspect-ratio: 1 / 1; /* Keep cells square */
    transition: background-color 0.2s ease;
    position: relative; /* For last move indicator */
}

.cell:hover {
    background-color: #e8e8e8;
}

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

/* Last move indicator */
.cell.last-move::after {
    content: '';
    position: absolute;
    bottom: 2px;
    left: calc(50% - 3px);
    width: 6px;
    height: 6px;
    background-color: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
}


/* Small board states */
.small-board.playable-board {
    background-color: var(--highlight-color) !important; /* Highlight playable boards */
    border: 2px solid green;
}

.small-board.won-x, .small-board.won-o, .small-board.draw {
    cursor: not-allowed;
}
.small-board.won-x .cell, .small-board.won-o .cell, .small-board.draw .cell {
    cursor: not-allowed;
    pointer-events: none; /* Disable clicks on cells of finished boards */
}

/* Overlay for won/drawn small boards */
.small-board::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(40px, 20vmin, 100px); /* Large X/O */
    font-weight: bold;
    pointer-events: none; /* Allow clicks through overlay */
    z-index: 1; /* Above cells */
}

.small-board.won-x::before {
    content: 'X';
    color: var(--player-x-color);
    background-color: var(--win-bg-x);
}
.small-board.won-o::before {
    content: 'O';
    color: var(--player-o-color);
    background-color: var(--win-bg-o);
}
.small-board.draw::before {
    content: '–'; /* Draw symbol */
    color: #6c757d;
    background-color: var(--draw-bg);
    font-size: clamp(60px, 25vmin, 120px); /* Larger dash */
}

/* Win line styling (initially hidden) */
.win-line {
    position: absolute;
    background-color: var(--win-line-color);
    opacity: 0.8;
    display: none; /* Hide initially */
    z-index: 2; /* Above small board overlays */
    pointer-events: none;
}

/* Small board win line adjustments */
.small-board .win-line {
    transform-origin: top left; /* Important for rotation */
}

/* Main board win line */
.main-win-line {
     transform-origin: top left; /* Important for rotation */
}


/* Responsive adjustments */
@media (max-width: 650px) {
    body {
        padding: 5px;
    }
    #welcome-screen, #game-area {
       padding: 15px;
       width: auto; /* Allow shrinking */
       max-width: 95vw;
    }
    #main-board-container {
        padding: 3px;
    }
     #main-board {
        gap: 3px;
    }
    .small-board {
        gap: 1px;
    }
    .cell {
        border-width: 1px;
    }
    #status-message {
        font-size: 1.1em;
    }
     button, select {
         font-size: 0.9em;
     }
}

@media (max-width: 400px) {
     #status-message {
        font-size: 1em;
    }
     .rules {
         font-size: 0.8em;
     }
}

/* Hide difficulty selector by default */
#difficulty-selector { display: none; }