:root {
    --bg-color: #FFFBF0;
    /* Cream */
    --text-color: #5D5D5D;
    --card-bg: #FFFFFF;

    --p1-color: #87CEEB;
    /* Sky Blue */
    --p1-dark: #00BFFF;
    --p2-color: #E0BBE4;
    /* Pastel Purple */
    --p2-dark: #957DAD;

    --tile-color: #FFFFFF;
    --tile-selected: #FFE4B5;
    /* Moccasin */
    --tile-border: #EEE;

    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
    --radius: 16px;
}

body {
    margin: 0;
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100vh;
    min-height: 100vh;
    /* Allow growth */
    display: flex;
    justify-content: center;
    align-items: flex-start;
    /* Start from top so scrolling works naturally */
    overflow-y: auto;
    /* Enable vertical scrolling */
    -webkit-overflow-scrolling: touch;
}

.main-container {
    width: 100%;
    /* max-width: 500px;  Removed to fill screen */
    height: 100%;
    /* max-height: 800px; Removed to fill screen */
    display: flex;
    flex-direction: column;
    padding: 20px;
    box-sizing: border-box;
    align-items: center;
    align-items: center;
    justify-content: center;
    /* Center content vertically */
    min-height: 100%;
    /* Ensure it fills at least the viewport */
}

/* Header & Scores */
header {
    width: 100%;
    max-width: 600px;
    /* Keep header reasonable */
    display: flex;
    justify-content: center;
    /* Changed from space-between to center for better wrapping/shrinking control */
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
    flex-shrink: 0;
}

.player-score {
    background: var(--card-bg);
    padding: 10px 20px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-align: center;
    opacity: 0.6;
    transition: all 0.3s ease;
    border: 3px solid transparent;
}

.player-score.active {
    opacity: 1;
    transform: scale(1.05);
    background: #FFF;
}

#p1-score-box.active {
    border-color: var(--p1-color);
}

#p2-score-box.active {
    border-color: var(--p2-color);
}

.player-label {
    font-size: 0.8rem;
    font-weight: bold;
    text-transform: uppercase;
    color: #888;
}

.player-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.btn-reload {
    width: 40px;
    height: 40px;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #FFB7B2;
    /* Rose */
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 1.2rem;
    cursor: pointer;
    font-weight: bold;
    transition: all 0.3s;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.btn-reload:hover {
    transform: translateY(-2px) rotate(180deg);
    /* Spin on hover */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.btn-reload:active {
    transform: translateY(0);
}

.btn-reload:disabled {
    background: #CCC;
    opacity: 0.3;
    pointer-events: none;
    box-shadow: none;
}

.score {
    font-size: 1.5rem;
    font-weight: bold;
}

.game-info {
    text-align: center;
}

h1 {
    margin: 0;
    font-size: 3rem;
    /* Increased from 1.5rem */
    color: #FFDAC1;
    /* Pastel Peach */
    text-shadow: 2px 2px 0 #FFB7B2;
}

.subtitle {
    font-size: 1.3rem;
    /* Increased from 0.9rem */
    color: #B5EAD7;
    /* Mint */
    font-weight: bold;
    margin-top: -5px;
}

.timer {
    font-size: 1.2rem;
    font-weight: bold;
    color: #555;
    margin-top: 5px;
    transition: color 0.3s;
}

.timer.warning {
    color: #FF6B6B;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }

    100% {
        opacity: 1;
    }
}

/* Round Indicators */
.rounds {
    display: flex;
    gap: 5px;
    justify-content: center;
    margin-top: 5px;
}

.round-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #DDD;
    /* Empty */
    border: 1px solid #CCC;
}

.round-dot.filled.p1 {
    background-color: var(--p1-dark);
    border-color: var(--p1-dark);
}

.round-dot.filled.p2 {
    background-color: var(--p2-dark);
    border-color: var(--p2-dark);
}

/* Game Board */
.game-board-container {
    background: var(--card-bg);
    padding: 20px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: 600px;
    /* Max width for gameplay area */
    flex-grow: 1;
    /* Allow it to take up space */
    max-height: 600px;
    /* Square-ish limit */
    aspect-ratio: 1 / 1;
}

.game-board {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 15px;
    /* Bigger gap */
    width: 100%;
    height: 100%;
}

.tile {
    width: auto;
    /* Fluid width */
    height: auto;
    /* Fluid height */
    /* width: 60px; REMOVED */
    /* height: 60px; REMOVED */
    background: var(--tile-color);
    border: 3px solid var(--tile-border);
    /* Thicker border */
    border-radius: 16px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.5rem, 5vw, 3.5rem);
    /* Responsive font size */
    font-weight: bold;
    color: var(--text-color);
    cursor: pointer;
    user-select: none;
    transition: transform 0.1s, background-color 0.2s;
    position: relative;
    /* Restored for absolute child positioning */
    box-shadow: 0 4px 0 #EEE;
    /* 3D effect */
}

.tile.pop-in {
    animation: tilePop 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) backwards;
}

@keyframes tilePop {
    from {
        transform: scale(0);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.tile:active {
    transform: translateY(4px);
    box-shadow: 0 0 0 #EEE;
}

.tile.selected {
    transform: translateY(2px);
    color: #555;
    /* Keep dark text for contrast on pastel */
}

.tile.selected.p1 {
    background-color: var(--p1-color);
    border-color: var(--p1-dark);
    box-shadow: 0 2px 0 var(--p1-dark);
}

.tile.selected.p2 {
    background-color: var(--p2-color);
    border-color: var(--p2-dark);
    box-shadow: 0 2px 0 var(--p2-dark);
}

.tile-points {
    position: absolute;
    top: 6px;
    /* Adjusted */
    right: 8px;
    font-size: clamp(0.7rem, 2vw, 1.2rem);
    font-weight: bold;
    /* Bold for visibility */
    color: #888;
    /* Darker than #AAA */
    z-index: 2;
    /* Ensure on top */
}

/* Controls */
.controls {
    width: 100%;
    max-width: 600px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    flex-shrink: 0;
}

/* Current Word Container (Top) */
.current-word-container {
    width: 100%;
    max-width: 600px;
    height: 80px;
    /* Fixed height to prevent jump */
    background: transparent;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    /* Space between small tiles */
    margin-bottom: 20px;
    border-bottom: 2px solid #EEE;
}

.placeholder-text {
    font-size: 1.2rem;
    color: #CCC;
    font-weight: bold;
    letter-spacing: 1px;
}

/* Small Tiles for Word Display */
.tile-small {
    width: 45px;
    height: 45px;
    background: var(--tile-color);
    border: 2px solid var(--tile-border);
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.5rem;
    font-weight: bold;
    color: var(--text-color);
    position: relative;
    box-shadow: 0 2px 0 #EEE;
}

.tile-small.pop-in {
    animation: popIn 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    animation-fill-mode: backwards;
    /* Ensures it stays hidden during delay */
}

.tile-points-small {
    position: absolute;
    top: 2px;
    right: 3px;
    font-size: 0.6rem;
    color: #AAA;
    font-weight: normal;
}

@keyframes popIn {
    from {
        transform: scale(0.5);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.button-row {
    display: flex;
    gap: 20px;
    width: 100%;
}

button {
    flex: 1;
    padding: 15px;
    border: none;
    border-radius: 50px;
    font-size: 1.1rem;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.2s;
}

button:active {
    transform: scale(0.98);
}

.btn-primary {
    background-color: #B5EAD7;
    /* Mint */
    color: #444;
}

.btn-secondary {
    background-color: #EEE;
    color: #333;
}

.message-area {
    height: 20px;
    color: #FF6B6B;
    font-size: 0.9rem;
    font-weight: bold;
}

@media (max-width: 600px) {
    .main-container {
        padding: 5px 10px;
        /* Reduced top/bottom padding */
        justify-content: flex-start;
        /* Start from top instead of center */
    }

    h1 {
        font-size: 1.5rem;
    }

    .subtitle {
        font-size: 0.8rem;
    }

    .player-score {
        padding: 5px 10px;
        min-width: 40px;
    }

    .score {
        font-size: 1.2rem;
    }

    .player-label {
        font-size: 0.6rem;
    }

    header {
        gap: 5px;
        /* Tighter gap */
    }

    header {
        margin-bottom: 10px;
    }

    .game-board-container {
        padding: 10px;
        margin-bottom: 10px;
        max-height: 50vh;
        /* Constraint height to viewport */
        width: auto;
        /* Allow width to shrink with height */
        aspect-ratio: 1/1;
        /* Maintain square */
    }

    .player-wrapper {
        gap: 5px;
    }

    .game-board {
        gap: 8px;
    }

    .tile {
        border-width: 2px;
        border-radius: 10px;
    }

    .tile-points {
        font-size: 0.8rem;
        top: 2px;
        right: 4px;
    }

    .current-word-container {
        height: 60px;
        margin-bottom: 10px;
    }

    .tile-small {
        width: 35px;
        height: 35px;
        font-size: 1.2rem;
    }

    .controls {
        gap: 10px;
    }

    button {
        padding: 12px;
        font-size: 1rem;
    }
}

@media (max-width: 380px) {
    .tile {
        border-radius: 8px;
    }

    h1 {
        font-size: 1.2rem;
    }

    .player-score {
        padding: 4px 6px;
    }
}

/* Score Popup */
.score-popup {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.95);
    padding: 30px 50px;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    text-align: center;
    z-index: 100;
    pointer-events: none;
    /* Let clicks pass through if it lingers */
    border: 3px solid #4CAF50;
    /* Green Border */
    box-shadow: 0 0 15px #81C784, 0 10px 30px rgba(0, 0, 0, 0.15);
    /* Green Glow */
}

.score-popup.hidden {
    display: none;
}

.score-popup.animate {
    animation: popFade 2s forwards;
}

.popup-word {
    font-size: 2rem;
    font-weight: bold;
    color: #555;
    margin-bottom: 5px;
    letter-spacing: 2px;
}

.popup-points {
    font-size: 3rem;
    font-weight: bold;
    color: #4CAF50;
    /* Green */
    text-shadow: 2px 2px 0px rgba(0, 0, 0, 0.05);
}

@keyframes popFade {
    0% {
        opacity: 0;
        transform: translate(-50%, -40%) scale(0.8);
    }

    10% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1.1);
    }

    20% {
        transform: translate(-50%, -50%) scale(1);
    }

    70% {
        opacity: 1;
        transform: translate(-50%, -55%) scale(1);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -70%) scale(0.95);
    }
}

/* Round Indicators */
.rounds {
    display: flex;
    justify-content: center;
    gap: 5px;
    margin-top: 5px;
}

.round-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #DDD;
    border: 2px solid #BBB;
}

.round-dot.won {
    background: #FFB347;
    /* Gold/Orange for win */
    border-color: #E59400;
    box-shadow: 0 0 5px #FFB347;
}

/* Game Overlay */
.game-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 200;
    backdrop-filter: blur(5px);
}

.game-overlay.hidden {
    display: none;
}

.overlay-content {
    background: white;
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    max-width: 90%;
    width: 400px;
}

#overlay-title {
    font-size: 2.5rem;
    color: #4CAF50;
    margin: 0 0 20px 0;
}

#overlay-message {
    font-size: 1.2rem;
    color: #555;
    margin-bottom: 30px;
}

#overlay-btn {
    width: 100%;
    font-size: 1.2rem;
    padding: 15px;
}

#confetti-canvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    /* Let clicks pass through */
    z-index: 99;
    /* Above board, below overlay/popups (100) */
}

/* Lobby Styles */
input[type="text"] {
    padding: 15px;
    font-size: 1.2rem;
    border: 3px solid #EEE;
    border-radius: 12px;
    outline: none;
    width: 100%;
    box-sizing: border-box;
    text-align: center;
    color: #555;
    font-weight: bold;
    margin-bottom: 20px;
    transition: border-color 0.3s;
}

input[type="text"]:focus {
    border-color: #B5EAD7;
}

.lobby-controls {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
}

.join-section {
    display: flex;
    gap: 10px;
}

.join-section input {
    margin-bottom: 0;
    flex: 1;
    text-transform: uppercase;
}

.join-section button {
    flex: 1;
}