:root {
    --color-bg-scoreboard: #ffffff;
    --color-text-scoreboard: #333333;
    --color-score-bg: #000000;
    --color-score-text: #ffff00;
    --color-legs-text: #d9534f; /* Danger color */
    --font-score: 'Courier New', Courier, monospace;
}

html, body.scoreboard-body {
    background-color: var(--color-bg-scoreboard);
    color: var(--color-text-scoreboard);
    display: flex;
    flex-direction: column;
    height: 100dvh;
    width: 100vw;
    padding: 0;
    margin: 0;
    overflow: hidden; /* Prevent scrolling */
    touch-action: manipulation; /* Disable double-tap zoom */
}

/* Button Overrides for Scoreboard */
body.scoreboard-body .btn-warning {
    background-color: var(--color-primary);
    color: var(--color-primary-text);
}

body.scoreboard-body .btn-primary {
    background-color: var(--color-secondary);
    color: var(--color-secondary-text);
}

/* Header */
.sb-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background-color: #f8f9fa;
    border-bottom: 1px solid #ddd;
    flex-shrink: 0;
    /* Allow header to be sized by content but restricted */
    height: auto;
    max-height: 15vh;
}

.sb-logo {
    /* Maximize height within header, respecting constraints */
    height: auto;
    max-height: 12vh; /* Use VH to be responsive */
    width: auto;
    object-fit: contain;
}

.sb-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    flex: 1;
    height: 85%;
}

.sb-game-modes {
    display: flex;
    gap: 5px;
    margin-top: auto;
}

/* Leg Scores - Responsive adjustments */
.sb-leg-scores {
    display: flex;
    gap: 20px;
    align-items: center;
    /* Desktop: 20% height logic handled by media query or specific rule */
    height: auto;
    flex: 1;
    justify-content: center;
}

.sb-leg-box {
    font-size: 2rem;
    font-weight: bold;
    color: var(--color-legs-text);
    background: #fff;
    border: 2px solid var(--color-legs-text);
    padding: 0 10px;
    border-radius: 5px;
    min-width: 50px;
    text-align: center;
    height: 75%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Main Game Area */
.sb-main {
    display: flex;
    flex: 1;
    overflow: hidden;
    /* height: 100%; Removed to allow flexbox to handle available space properly */
    min-height: 0; /* Important for flex child scrolling/containment */
}

.sb-player-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-right: 1px solid #eee;
    background-color: #fff;
    transition: background-color 0.3s;
    overflow: hidden; /* Keep content inside */
}

.sb-player-col.active {
    background-color: color-mix(in srgb, var(--color-primary) 25%, white);
}

.sb-player-col.active .sb-player-select {
    border-block-color: var(--color-primary);
    border-block-width: 0.2rem;
}

.sb-player-col:last-child {
    border-right: none;
}

.sb-player-select {
    width: 100%;
    font-size: 1.2rem;
    margin-bottom: 10px;
    padding: 5px;
}

.sb-current-score {
    font-size: 6rem; /* Big score */
    font-weight: bold;
    text-align: center;
    color: var(--color-text-scoreboard);
    flex: 1 1 auto; /* Allow shrinking and growing */
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0; /* Allow shrinking */
    cursor: pointer;
}

.sb-score-history {
    /* Desktop Default: Grow upwards from bottom */
    height: auto;
    max-height: 50vh; /* Don't take more than half screen */
    overflow-y: auto;
    border-top: 1px solid #eee;
    padding-top: 5px;
    font-size: 1.6rem;
    color: #666;
    text-align: center;
    white-space: normal; /* Wrap text */
    overflow-wrap: break-word;
    flex-shrink: 0; /* Don't shrink below content needs if possible, push score up */
}

.sb-score-history ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

/* Keypad Column (Center on Desktop) */
.sb-keypad-col {
    flex: 0 0 300px;
    display: flex;
    flex-direction: column;
    padding: 10px;
    background-color: #f1f1f1;
    overflow: hidden;
}

.sb-input-display {
    background: #fff;
    border: 1px solid #ccc;
    height: 50px;
    font-size: 2rem;
    text-align: center;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.sb-keypad-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 5px;
    flex: 1;
}

.sb-key {
    font-size: 1.5rem;
    font-weight: bold;
    border: 1px solid #ccc;
    background-color: #fff;
    cursor: pointer;
    border-radius: 5px;
    transition: background-color 0.1s;
    height: 100%;
    width: 100%;
}

.sb-key:active {
    background-color: #ddd;
}

.sb-key-enter {
    background-color: #28a745;
    color: white;
}

.sb-key-undo {
    background-color: #d9534f;
    color: white;
}

.sb-outshot-display {
    text-align: center;
    font-weight: bold;
    color: #ff0000;
    min-height: 1.5em;
    margin-top: 5px;
    font-size: 2rem;
    flex-shrink: 0;
}

/* Winner Overlay */
#winner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    z-index: 9999;
    display: none; /* Hidden by default */
    align-items: center;
    justify-content: center;
    flex-direction: column; /* Ensure column layout for controls */
    cursor: pointer;
}

.winner-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
    z-index: 10000;
}

.winner-text {
    color: #fff;
    font-size: 5rem;
    font-weight: bold;
    text-align: center;
    animation: pulse 1.5s infinite;
    padding: 20px;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* Desktop Large Screen Adjustments */
@media (min-width: 961px) {
    .sb-leg-scores {
        height: 20vh; /* Request: 20% height on large screen */
    }
    .sb-leg-box {
        font-size: 3rem; /* Grow text */
    }
}

/* Landscape Tablet / Short Screens Optimization */
@media (max-height: 850px) and (min-width: 961px) {
    .sb-header {
        padding: 5px 10px;
    }

    .sb-logo {
        height: auto;
        max-height: 100%;
    }

    .sb-leg-scores {
        height: 15vh; /* Reduce slightly on short screens */
    }

    .sb-leg-box {
        font-size: 2rem;
        min-width: 40px;
        width: 7rem;
    }

    .sb-current-score {
        font-size: 10vh; /* Scale with height */
    }

    .sb-score-history {
        max-height: 50%;
        font-size: 1.2rem;
    }
}

/* Mobile & Tablet Responsiveness */
@media (max-width: 960px) {
    /* Hide logo on small screens */
    .sb-header .sb-logo {
        display: none;
    }

    .sb-header {
        flex-wrap: wrap;
        height: auto;
        padding: 5px;
    }

    .sb-leg-scores {
        width: 100%; /* Full width */
        height: auto;
        justify-content: space-between;
        margin-bottom: 5px;
    }

    .sb-leg-box {
        font-size: 1.5rem;
        padding: 5px;
    }

    .sb-main {
        display: grid;
        grid-template-columns: 1fr 1fr;
        /* V2.1.1: Optimize rows. Auto for player top section, 1fr for whatever space is left, but actually we want top to shrink and keypad to fit */
        /* Let's use flex-like behavior in grid rows: minmax(0, 1.5fr) for players, minmax(0, 1fr) for keypad? */
        /* Or explicit percentages: 45% players, 55% keypad */
        grid-template-rows: 45% 55%;
        overflow: hidden;
        flex: 1; /* Ensure it takes remaining space */
    }

    /* Target Player 1 */
    .sb-player-col:nth-child(1) {
        grid-row: 1;
        grid-column: 1;
        width: auto;
        height: 100%;
        border-bottom: 1px solid #eee;
    }

    /* Target Player 2 */
    .sb-player-col:nth-child(3) {
        grid-row: 1;
        grid-column: 2;
        width: auto;
        height: 100%;
        border-left: 1px solid #eee;
        border-bottom: 1px solid #eee;
        border-right: none;
    }

    /* Target Keypad */
    .sb-keypad-col {
        grid-row: 2;
        grid-column: 1 / span 2;
        width: 100%;
        height: 100%;
        flex: 1;
        padding: 5px;
        /* Ensure keypad grid stays contained */
        min-height: 0;
        /* Add spacer at bottom for mobile/tablet URL bars and safe areas */
        padding-bottom: calc(50px + env(safe-area-inset-bottom));
    }

    .sb-current-score {
        /* V2.1.1: More aggressive scaling for small screens */
        font-size: clamp(1.5rem, 10vmin, 4rem);
        flex-shrink: 1;
        /* Ensure it doesn't overflow container */
        overflow: hidden;
        min-height: 0;
    }

    .sb-score-history {
        height: auto;
        max-height: 50%;
        flex-grow: 1;
        font-size: 0.8rem;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        align-content: flex-start;
        gap: 5px;
        overflow: hidden;
    }

    .sb-score-history ul {
        display: contents; /* Let LIs be flex items of history container? Or make UL flex? */
    }

    /* Make UL flex container if display contents not supported well, but let's try just styling UL */
    .sb-score-history ul {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 5px;
        width: 100%;
    }

    .sb-score-history li {
        display: block;
    }

    .sb-outshot-display {
        font-size: 1.1rem;
        margin-bottom: 10px;
        max-height: 35%;
        margin-top: 0px;
        overflow-y: auto;
        white-space: normal;
        flex-shrink: 0;
    }

    .sb-input-display {
        height: 40px;
        font-size: 1.5rem;
    }

    .winner-text {
        font-size: 3rem; /* Smaller text on mobile */
    }
}

/* Mobile Specific (Small) adjustments */
@media (max-width: 600px) {
    .sb-game-modes button {
        padding: 5px 10px;
        font-size: 0.8rem;
    }

    .sb-player-select {
        font-size: 1rem;
    }
}

/* Short Screen Mobile Layout Override */
@media (max-width: 960px) and (max-height: 750px) {
    .sb-header {
        padding: 2px 5px; /* Minimal padding */
    }

    .sb-leg-scores {
        margin-bottom: 2px;
        height: auto;
        gap: 5px; /* Reduce gap between scores and controls */
    }

    .sb-leg-box {
        font-size: 1.2rem;
        height: auto;
        min-height: 30px; /* Reduced fixed height or auto */
        min-width: 30px;
        padding: 0 5px;
    }

    .sb-controls {
        gap: 2px;
    }

    .sb-game-modes button, .btn-sm {
        padding: 2px 5px;
        font-size: 0.75rem; /* Tiny buttons */
        margin-bottom: 0; /* ensure no margin */
    }

    /* Shift grid to give more space to keypad */
    .sb-main {
        grid-template-rows: 35% 65%; /* 35% players, 65% keypad */
    }

    /* Player column adjustments */
    .sb-player-select {
        font-size: 0.9rem;
        padding: 2px;
        margin-bottom: 2px;
        height: auto;
        min-height: 25px; /* Force height */
    }

    .sb-current-score {
        /* Clamp aggressively */
        font-size: clamp(1.5rem, 8vh, 2.5rem);
        min-height: 0; /* Allow shrinking */
    }

    .sb-score-history {
        /* Hide history on super short screens if needed, or make very small */
        font-size: 0.7rem;
        max-height: 25%; /* Limit height */
        margin-top: 0;
        padding-top: 2px;
    }

    .sb-score-history ul {
        gap: 2px;
    }

    /* Keypad Adjustments */
    .sb-keypad-col {
        /* Remove the large padding bottom designed for URL bars/safe areas, as it causes cutoff */
        padding-bottom: 5px;
        padding-top: 2px;
        padding-left: 2px;
        padding-right: 2px;
    }

    .sb-input-display {
        height: 30px;
        font-size: 1.2rem;
        margin-bottom: 2px;
    }

    .sb-outshot-display {
        font-size: 0.9rem;
        margin-top: 0;
        margin-bottom: 2px;
        min-height: 1.2em;
    }

    .sb-key {
        font-size: 1.2rem; /* Ensure keys are readable but not huge */
        border-radius: 3px;
    }

    /* Ensure match selection container (if visible) is small */
    #match-selection-container {
        margin: 2px 0 !important;
    }
    #match-select {
        padding: 2px !important;
        font-size: 0.9rem !important;
        height: auto;
    }
}
