/* --- Global Styles --- */
:root {
    --color-bg: #f4f7f6;
    --color-dark: #333;
    --color-light: #fff;
    --color-gray: #ccc;
    --color-accent: #007bff;
    
    /* NEW: Updated Player Colors */
    --player-0-color: #FF6B6B; /* Red */
    --player-1-color: #FED766; /* Yellow */
    --player-2-color: #4CAF50; /* Green */
    --player-3-color: #45B7D1; /* Blue */

    --board-size: 550px;
    --cell-size: calc(var(--board-size) / 9);
    --label-size: 30px; 
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background-color: var(--color-bg);
    color: var(--color-dark);
    margin: 0;
    padding: 20px;
    min-height: 100vh;
}

header {
    text-align: center;
    margin-bottom: 20px;
    color: var(--color-dark);
}

main.screen {
    display: none;
    width: 100%;
}
main.screen.active {
    display: block;
}

button {
    font-size: 1rem;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    background-color: var(--color-accent);
    color: var(--color-light);
    cursor: pointer;
    transition: background-color 0.2s;
}
button:hover {
    background-color: #0056b3;
}
button:disabled {
    background-color: var(--color-gray);
    cursor: not-allowed;
}

/* --- Setup Screen --- */
#setup-screen {
    background: var(--color-light);
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    text-align: center;
    max-width: 900px;
    margin: 0 auto; /* Center setup screen */
}
.setup-form {
    margin: 20px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
}
.setup-form label,
.setup-form select {
    font-size: 1.1rem;
}
.setup-form select {
    padding: 8px;
    border-radius: 4px;
    border: 1px solid var(--color-gray);
}
#player-types-container {
    flex-direction: column;
    gap: 10px;
}

/* --- 3-Column Game Screen Layout --- */
#game-wrapper {
    display: grid;
    /* Flexible 3-column layout */
    grid-template-columns: minmax(250px, 1fr) 2fr minmax(250px, 1fr);
    gap: 25px;
    max-width: 1600px;
    margin: 0 auto;
}

#left-panel, #right-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-top: 100px; /* Align with bottom of header/info */
}

#main-game-area {
    display: flex;
    flex-direction: column;
    align-items: center; /* Center the board */
    min-width: 600px; /* Ensure board has space */
}

/* Info Box Panel Style */
.info-box {
    background: var(--color-light);
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.05);
}
.info-box h3 {
    margin-top: 0;
    border-bottom: 2px solid var(--color-bg);
    padding-bottom: 10px;
    color: var(--color-accent);
}

/* List styles for panels */
.info-box ul {
    padding-left: 20px;
    margin: 0;
}
.info-box li {
    margin-bottom: 10px;
    line-height: 1.4;
}
.info-box ol {
    padding-left: 20px;
    margin-top: 5px;
}

/* --- Info Panel (Turn/Timer) --- */
#info-panel {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--color-light);
    padding: 15px 25px;
    border-radius: 8px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.05);
    font-size: 1.25rem;
    font-weight: 500;
    width: 100%; /* Fill the main column */
    max-width: calc(var(--board-size) + var(--label-size) + 2px);
    box-sizing: border-box; /* Include padding in width */
}
#info-turn span {
    font-weight: bold;
    padding: 5px 10px;
    border-radius: 4px;
}
#info-panel button#new-restart-btn {
    font-size: 1rem;
    padding: 8px 15px;
}

/* --- Board --- */
#board-container {
    display: grid;
    grid-template-columns: var(--label-size) var(--board-size);
    grid-template-rows: var(--label-size) var(--board-size);
    gap: 2px;
    margin-top: 20px; /* Space below info panel */
}

#board-corner {
    grid-column: 1 / 2;
    grid-row: 1 / 2;
    background: #dcdcdc;
}

#top-labels {
    grid-column: 2 / 3;
    grid-row: 1 / 2;
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    gap: 2px;
    background: #dcdcdc;
}

#left-labels {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
    display: grid;
    grid-template-rows: repeat(9, 1fr);
    gap: 2px;
    background: #dcdcdc;
}

.label-cell {
    background-color: #e9e9e9;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    font-weight: bold;
    color: #555;
}

#board {
    grid-column: 2 / 3;
    grid-row: 2 / 3;
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    width: 100%; 
    height: 100%; 
    background: #dcdcdc;
    border: 2px solid #555;
    border-radius: 4px;
    gap: 2px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.cell {
    width: 100%;
    height: 100%;
    background: var(--color-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--color-light);
    cursor: not-allowed;
    transition: all 0.2s ease;
    box-sizing: border-box;
}

/* Cell States */
.cell.legal-move { background: #e0ffe0; cursor: pointer; }
.cell.legal-move:hover { background: #c0ffc0; border: 2px solid var(--color-accent); }
.cell.selected { border: 3px solid var(--color-accent) !important; box-shadow: 0 0 10px var(--color-accent); transform: scale(1.05); }

.cell.placed { animation: place-anim 0.3s ease-out; }
@keyframes place-anim { 0% { transform: scale(0.5); opacity: 0.5; } 100% { transform: scale(1); opacity: 1; } }

/* NEW: Updated Player Colors & Text Contrast */
.cell[data-owner="0"], [data-player-color="0"] { background-color: var(--player-0-color); color: var(--color-light); } /* Red */
.cell[data-owner="1"], [data-player-color="1"] { background-color: var(--player-1-color); color: var(--color-dark); }  /* Yellow */
.cell[data-owner="2"], [data-player-color="2"] { background-color: var(--player-2-color); color: var(--color-light); } /* Green */
.cell[data-owner="3"], [data-player-color="3"] { background-color: var(--player-3-color); color: var(--color-light); } /* Blue */


/* --- Hand & Right Panel --- */
#hand-container {
    /* Now an info-box, so it has padding */
    display: flex;
    flex-direction: column;
    align-items: center;
}

#hand {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 100%;
    max-width: 250px; /* Give cards a bit more space */
}

.card {
    font-size: 1.5rem;
    font-weight: bold;
    padding: 20px;
    width: 100%;
    border-radius: 8px;
    border: 2px solid transparent;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    transition: all 0.2s ease;
    box-sizing: border-box; /* Include padding in width */
}
.card:hover { transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.15); }
.card.selected { border-color: var(--color-accent); box-shadow: 0 0 10px var(--color-accent); transform: scale(1.05); }

/* Player Info List */
#player-info-list {
    list-style: none;
    padding: 0;
}
#player-info-list li {
    display: flex;
    align-items: center;
    font-size: 1.1rem;
    margin-bottom: 12px;
}
.color-swatch {
    width: 20px;
    height: 20px;
    border-radius: 4px;
    margin-right: 10px;
    border: 1px solid #0002;
}

/* Move Log styles */
#move-list {
    height: 250px; /* Set a fixed height */
    overflow-y: auto; /* Make it scrollable */
    border: 1px solid var(--color-bg);
    padding: 10px;
    border-radius: 4px;
    background: #fafafa;
    display: flex; /* Use flex-column-reverse to show new items at top */
    flex-direction: column-reverse; /* Newest items will be at the top */
}
#move-list p {
    margin: 0 0 8px 0;
    font-size: 0.9rem;
    line-height: 1.3;
    padding-bottom: 8px;
    border-bottom: 1px dashed var(--color-gray);
}
#move-list p:first-child { /* This now styles the *newest* item */
    border-bottom: none;
}
#move-list p:last-child { /* This styles the *oldest* item */
    border-bottom: none;
}


/* --- Game Over Modal --- */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}
.modal-backdrop.hidden {
    display: none;
}
.modal-content {
    background: var(--color-light);
    padding: 30px 40px;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 5px 20px rgba(0,0,0,0.2);
    animation: modal-fade-in 0.3s;
}
@keyframes modal-fade-in { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }
#game-over-message { font-size: 1.2rem; margin: 20px 0; }


/*
==================================================
Responsive Styles for Mobile
==================================================
*/

@media (max-width: 900px) {
    /* --- Responsive Layout --- */
    #game-wrapper {
        grid-template-columns: 1fr; /* Stack to a single column */
    }

    #main-game-area {
        order: 1; /* Show game first */
        min-width: 100%; /* Use full width */
    }

    #right-panel {
        order: 2; /* Show hand/log second */
        margin-top: 20px; /* Reset margin */
    }

    #left-panel {
        order: 3; /* Show "How to Play" last */
        margin-top: 20px; /* Reset margin */
    }

    /* --- Responsive Board Size --- */
    :root {
        /* Adjust board size to fit screen, leave ~40px total padding */
        --board-size: calc(100vw - 44px); 
        /* Adjust label size to be smaller */
        --label-size: 25px; 
    }

    /* Make board container flexible */
    #board-container {
        grid-template-columns: var(--label-size) 1fr; /* Use 1fr for board */
        grid-template-rows: var(--label-size) 1fr;
        
        /* Recalculate container width */
        width: calc(var(--board-size) + var(--label-size) + 2px); 
        max-width: 100%;
    }

    /* Override fixed-width board from main CSS */
    #board {
        width: 100%;
        height: 100%;
    }
    
    #top-labels, #left-labels {
        width: 100%;
        height: 100%;
    }

    /* --- Responsive UI Elements --- */
    
    /* Stack info panel on mobile */
    #info-panel {
        flex-direction: column;
        gap: 10px;
        align-items: stretch; /* Make items full-width */
    }

    #info-panel button#new-restart-btn {
        align-self: center; /* Center the play again button */
        width: 100%;
        margin-top: 10px;
    }
    
    /* Adjust fonts */
    header h1 {
        font-size: 1.5rem;
    }

    .info-box h3 {
        font-size: 1.1rem;
    }
    
    .cell {
        font-size: 1.2rem; /* Smaller font on cards */
    }
    
    .label-cell {
        font-size: 0.8rem; /* Smaller font on labels */
    }

    #hand {
        max-width: 100%; /* Allow hand to be full width */
    }
    
    .card {
        padding: 15px; /* Smaller cards */
        font-size: 1.2rem;
    }
}

/* Add a breakpoint for very small phones */
@media (max-width: 400px) {
    :root {
        --board-size: calc(100vw - 44px);
        --label-size: 20px;
    }
    
    .cell {
        font-size: 1rem; /* Even smaller */
    }
    
    .label-cell {
        font-size: 0.7rem;
    }
    
    .card {
        padding: 10px;
        font-size: 1rem;
    }

    body {
        padding: 10px; /* Reduce body padding */
    }
}