:root {
    --bg-color: #121212;
    --card-bg: #1e1e1e;
    --text-color: #ffffff;
    --accent: #007bff;
    --red: #d32f2f;
    --yellow: #fbc02d;
    --green: #388e3c;
    --brown: #A0522D;
    /* Brighter Brown (Sienna) */
    --blue: #1976d2;
    --pink: #FF4081;
    /* Brighter Pink */
    --black: #000000;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    touch-action: manipulation;
    /* Disable double-tap zoom */
}

#scoreboard {
    background-color: var(--card-bg);
    padding: 10px;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
    flex-shrink: 0;
}

.player-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
    font-size: 1.2rem;
}

.player-name {
    flex: 2;
    font-weight: 500;
}

.active-player {
    color: var(--yellow);
}

.player-score {
    flex: 1;
    text-align: right;
    font-weight: bold;
    font-size: 1.5rem;
}

.player-frames {
    flex: 1;
    text-align: right;
    font-size: 1rem;
    color: #aaa;
}

.info-bar {
    display: flex;
    justify-content: space-between;
    font-size: 0.9rem;
    color: #888;
    margin-top: 5px;
}

#controls {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 10px;
    gap: 10px;
    overflow-y: auto;
    /* Prevent scrolling if possible to force fit */
}

/* Main 3x3 Grid - Takes most space */
.button-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    flex: 3;
    /* Allocate 3 parts of space */
    min-height: 200px;
    /* Ensure minimum usability */
}

.button-grid button {
    height: 100%;
    /* Fill the grid cell */
    width: 100%;
}

/* Secondary Controls (Undo, Next Frame, End Frame) - Takes less space */
.secondary-controls {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    flex: 1;

    /* Allocate 1 part of space */
    min-height: 60px;
}

button {
    border: none;
    border-radius: 8px;
    font-size: 1.2rem;
    font-weight: bold;
    color: white;
    cursor: pointer;
    transition: transform 0.1s, opacity 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    text-transform: uppercase;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    /* min-height removed to allow flex shrinking if really needed, but kept in grid defs */
}

button:active {
    transform: scale(0.95);
}

button:disabled {
    opacity: 0.3;
    pointer-events: none;
}

.btn-red {
    background-color: var(--red);
}

.btn-yellow {
    background-color: var(--yellow);
    color: black;
}

.btn-green {
    background-color: var(--green);
}

.btn-brown {
    background-color: var(--brown);
}

.btn-blue {
    background-color: var(--blue);
}

.btn-pink {
    background-color: var(--pink);
}

.btn-black {
    background-color: var(--black);
    border: 1px solid #333;
}

.btn-action {
    background-color: #444;
    font-size: 0.9rem;
}

.secondary-controls button {
    height: 100%;
    width: 100%;
}

/* Modal */
.modal {
    display: None;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-content {
    background: var(--card-bg);
    padding: 20px;
    border-radius: 10px;
    width: 90%;
    max-width: 400px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal h2 {
    margin-top: 0;
}

input[type="text"],
input[type="number"] {
    width: 100%;
    padding: 10px;
    margin: 10px 0;
    background: #333;
    border: 1px solid #555;
    color: white;
    font-size: 1.1rem;
    border-radius: 5px;
}

.modal-buttons {
    display: flex;
    gap: 10px;
    margin-top: 20px;
}

.modal-btn {
    flex: 1;
    padding: 15px;
    background: var(--accent);
    color: white;
    text-align: center;
    border-radius: 5px;
}

.player-select-item {
    padding: 10px;
    background: #333;
    margin-bottom: 5px;
    border-radius: 5px;
    cursor: pointer;
}

.player-select-item.selected {
    background: var(--accent);
}

/* ... existing styles ... */

.ball-indicators-bar {
    background-color: #222;
    padding: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.ball-indicator {
    flex: 1 1 24px;
    max-width: 24px;
    min-width: 0;
    aspect-ratio: 1 / 1;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: clamp(0.6rem, 2vw, 0.8rem);
    color: black;
    font-weight: bold;
    box-shadow: inset 0 -2px 5px rgba(0, 0, 0, 0.3);
}

.bottom-controls-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
    margin-top: 10px;
    padding-bottom: 100px;
    /* Large padding to ensure buttons clear mobile browser bars */
}

/* Removed flex button styling */

/* Enhanced Selection Sizes */
#order-list-container {
    display: grid;
    gap: 15px;
    margin: 20px 0;
}

.order-item {
    background: rgba(255, 255, 255, 0.1);
    padding: 25px;
    border-radius: 12px;
    text-align: center;
    font-size: 1.5rem;
    font-weight: bold;
    cursor: pointer;
    border: 2px solid transparent;
    transition: background 0.2s;
}

.order-item:active {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
}

.player-team-row {
    padding: 20px !important;
    /* Force larger padding */
}

.team-toggle-btn {
    font-size: 1.3rem !important;
    padding: 15px 25px !important;
}

/* --- Mobile Responsiveness --- */
@media (max-width: 600px) {
    html {
        font-size: 14px;
        /* Scale down base font size */
    }

    body {
        padding: 10px;
    }

    /* Scoreboard */
    #scoreboard {
        padding: 10px 5px;
        /* Reduce horizontal padding */
    }

    .player-name {
        font-size: 1.2rem;
    }

    .player-score {
        font-size: 1.5rem;
    }

    /* Controls Grid */
    #controls {
        padding: 5px;
        gap: 5px;
    }

    .button-grid {
        gap: 5px;
    }

    button {
        min-height: 50px;
        /* Reduce button height */
        font-size: 1rem;
        border-radius: 6px;
    }

    .btn-red {
        background-color: var(--red);
    }

    /* Bottom Controls */
    .bottom-controls-grid {
        padding-bottom: 80px;
        /* Reduce bottom padding */
        gap: 5px;
    }

    /* Modals */
    .modal-content {
        width: 90%;
        max-width: 95%;
        padding: 15px;
        max-height: 90vh;
        overflow-y: auto;
    }

    .modal h2 {
        font-size: 1.3rem;
    }

    /* Dashboard Specific */
    .table-card {
        padding: 10px;
    }

    .table-title {
        font-size: 1rem;
    }

    #dashboard-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }
}

/* Very Small Screens (e.g. Foldable outer screens, iPhone SE) */
@media (max-width: 350px) {
    html {
        font-size: 12px;
    }

    .button-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    button {
        padding: 5px;
        min-height: 45px;
    }
}

/* Landscape Mode Optimization (Short Screens) */
@media (max-height: 500px) {
    .modal-content {
        padding: 10px;
        max-height: 95vh;
        overflow-y: auto;
    }

    .modal h2 {
        margin: 5px 0;
        font-size: 1.1rem;
    }

    input[type="text"],
    input[type="number"],
    select {
        padding: 5px;
        margin: 5px 0;
        font-size: 0.9rem;
    }

    .modal-btn {
        padding: 8px;
    }

    /* Ensure header/footer don't eat too much space */
    header {
        margin-bottom: 10px;
        padding-bottom: 5px;
    }
}