/* General Settings */
:root {
    --footer-height: 70px; /* Base footer height */
    --header-height: 70px;
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --total-footer-height: calc(var(--footer-height) + var(--safe-area-bottom));
}

body {
    font-family: 'League Gothic', sans-serif;
    background-color: #000;
    color: #fff;
    text-align: center;
    margin: 0;
    padding: 0;
    -webkit-user-select: none; /* Disable text selection on mobile */
    user-select: none;
    -webkit-tap-highlight-color: transparent; /* Remove tap highlights */
    box-sizing: border-box;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Fixed Header */
.app-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: var(--header-height); /* Fixed header height */
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #000;
    padding: 10px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    box-sizing: border-box;
}

/* Main Content Areas */
.scoring-screen, .new-game-screen {
    margin-top: var(--header-height); /* Space for fixed header */
    margin-bottom: var(--total-footer-height); /* Space for fixed footer */
    padding: 20px;
    flex: 1; /* Take up remaining space */
    min-height: calc(100vh - 70px - var(--total-footer-height)); /* Ensure proper height */
}

.field-selector {
    position: absolute;
    right: 25px;
    background-color: #ff008c; /* Default to DVC color, will be updated by JS */
    color: #fff;
    padding: 8px 12px;
    border-radius: 20px;
    font-size: 28px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

.home-link {
    position: absolute;
    left: 15px;
    color: #fff;
    font-size: 20px;
    text-decoration: none;
    padding: 8px;
    border-radius: 50%;
    transition: background-color 0.3s ease;
}

.home-link:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

.logo {
    width: 50px;
    height: 50px;
    margin-right: 10px;
}

.app-title {
    font-family: 'League Gothic', sans-serif;
    font-size: 36px;
    margin: 0;
    font-weight: normal; /* Ensure font-weight is never bold */
    color: #fff;
}

/* Scoring Screen */
.scoring-screen {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: calc(var(--header-height) + 30px); /* Prevent header from overlapping */
}

.team-name {
    font-size: 48px;
    margin-bottom: .0px;
    margin-block-start: 0px;
    font-weight: normal;
    color: var(--team-color); /* Dynamic team color */
}

.score {
    font-size: 72px;
    margin-bottom: 0px;
    font-weight: normal;
}

.score-edit input {
    font-size: 24px;
    font-family: 'League Gothic', sans-serif;
    font-weight: normal;
}

/* Score Buttons */
.score-buttons {
    /* display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center; */
    /* margin: auto;
    /* margin-left: 20px; */ 
    width: 100%; 
}

.score-buttons .score-btn {
    font-family: 'League Gothic', sans-serif;
    font-weight: normal;
    font-size: 3em;
    padding: 10px;
    margin: auto;
    margin-bottom: 10px;
    border: none;
    width: 90%; /* Full width for the button */
    cursor: pointer;
    touch-action: manipulation; /* Optimize for touch */
    -webkit-tap-highlight-color: transparent;
}

.vs {
    font-size: 2em;
}

#btnMinus1 {
    background-color: #000; /* Black background */
    color: var(--team-color); /* Team color for text */
    border: solid 2px var(--team-color); /* Solid 2px border with team color */
}

/* Transparency for other buttons */
#btnPlus4 {
    background-color: var(--team-color);
}

#btnPlus3 {
    background-color: var(--team-color);
}

#btnPlus2 {
    background-color: var(--team-color);
}

#btnPlus1 {
    background-color: var(--team-color);
}

/* Footer with Icons */
.app-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: var(--footer-height);
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: #333;
    padding: 10px;
    z-index: 1000;
    box-sizing: border-box;
    /* Add safe area support for mobile devices */
    padding-bottom: calc(10px + var(--safe-area-bottom));
}

.footer-btn {
    font-size: 24px;
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    opacity: 0.7;
}

.footer-btn.active {
    opacity: 1; /* Fully visible when active */
}

.footer-btn i {
    font-size: 24px; /* Ensure icons are large enough to be visible */
    color: #fff; /* Set the icon color to white */
}

/* History Section */
.history-section {
    margin: 20px auto; /* Centered with margins on left and right */
    width: 90%; /* Not full width */
    font-family: 'Roboto', sans-serif;
    background-color: #fff;
    color: #333;
    border-radius: 8px;
    box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.history-section h2 {
    font-size: 24px;
    margin-bottom: 10px;
}

.history-section table {
    width: 100%;
    border-collapse: collapse;
}

.history-section th, .history-section td {
    padding: 10px;
    text-align: center; /* Center text in the table */
}

.history-section th {
    background-color: #f4f4f4;
}

.history-section tr:nth-child(even) {
    background-color: #f9f9f9; /* Slightly more contrast between rows */
}

.history-section tr:nth-child(odd) {
    background-color: #ffffff;
}

/* History Section Toggle */
.footer-btn.history {
    opacity: 0.7;
}

.footer-btn.history.active {
    opacity: 1; /* Full opacity when history is active */
}

/* Dropdown for New Game */
#newGameScreen select, #newGameScreen button {
    font-family: 'League Gothic', sans-serif;
    font-weight: normal;
    font-size: 2em; 
    padding: 10px;
    margin: 10px;
    width: 90%; /* Make sure dropdowns are full width for easier selection */
}

.field-display {
    font-size: 1.5em;
    margin-bottom: 20px;
    color: #4CAF50;
    font-weight: normal;
}

#errorMessage {
    color: red;
}

#saveScoreButton {
    font-family: 'League Gothic', sans-serif;
    font-weight: normal;
    padding: 10px;
    margin-top: 10px;
    font-size: 2em; /* Updated button text to 3em */
    cursor: pointer;
}

[hidden] {
    display: none !important;
}

.field-selector {
    cursor: default;
}

button.secondary {
    background-color: #333;
    color: #fff;
    border: 1px solid #666;
}

button.danger {
    background-color: #c62828;
    color: #fff;
    border: none;
}

/* Storage warning (private mode etc.) */
body.storage-warn {
    padding-top: 64px;
}

.storage-warning {
    position: fixed;
    top: var(--header-height);
    left: 0;
    right: 0;
    z-index: 999;
    background: #b71c1c;
    color: #fff;
    font-family: 'Roboto', sans-serif;
    font-size: 14px;
    padding: 8px 16px;
}

/* Scoreboard strip: both teams and the half-inning */
.scoreboard {
    display: flex;
    align-items: stretch;
    justify-content: space-between;
    gap: 8px;
    width: 90%;
    max-width: 520px;
    margin: 0 auto 10px;
}

.sb-team {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 4px 6px;
    border: 2px solid transparent;
    border-radius: 10px;
    opacity: 0.6;
    min-width: 0;
}

.sb-team.batting {
    border-color: currentColor;
    opacity: 1;
}

.sb-name {
    font-size: 20px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.sb-score {
    font-size: 30px;
    color: #fff;
}

.sb-half {
    align-self: center;
    font-size: 22px;
    color: #ccc;
    white-space: nowrap;
}

.half-net {
    font-family: 'Roboto', sans-serif;
    font-size: 15px;
    color: #bbb;
    margin: 2px 0 12px;
}

.score {
    cursor: pointer;
}

/* Score with its edit button beside it; the button sits outside the centred number */
.score-row {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}

.edit-score-btn {
    position: absolute;
    left: 100%;
    margin-left: 12px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid var(--team-color, #fff);
    background: transparent;
    color: var(--team-color, #fff);
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* No double-tap zoom or long-press text selection on any control */
button, select, .score {
    touch-action: manipulation;
}

.score-edit button {
    font-family: 'League Gothic', sans-serif;
    font-size: 2em;
    padding: 6px 16px;
    margin: 10px 4px 0;
}

#btnMinus1.is-blocked {
    opacity: 0.35;
}

/* Toast */
.toast {
    position: fixed;
    left: 16px;
    right: 16px;
    top: calc(var(--header-height) + 8px);
    z-index: 3000;
    margin: 0 auto;
    max-width: 480px;
    background: #fff;
    color: #111;
    font-family: 'Roboto', sans-serif;
    font-size: 16px;
    line-height: 1.35;
    padding: 12px 16px;
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.6);
    pointer-events: none; /* taps go through to the buttons underneath */
}

.toast.warn {
    background: #ffd54f;
}

/* Footer labels */
.footer-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.footer-btn span {
    font-family: 'Roboto', sans-serif;
    font-size: 12px;
    color: #fff;
}

.footer-btn:disabled {
    opacity: 0.25;
    cursor: default;
}

/* New game */
.select-label {
    display: block;
    font-family: 'Roboto', sans-serif;
    font-size: 14px;
    color: #bbb;
    margin-top: 6px;
}

/* Previous games */
.games-screen {
    box-sizing: border-box;
    width: 100%;
    margin-top: var(--header-height);
    margin-bottom: var(--total-footer-height);
    padding: 20px 16px;
    flex: 1;
}

.games-screen h1 {
    font-weight: normal;
    font-size: 36px;
    margin: 0 0 12px;
}

.games-list {
    list-style: none;
    margin: 0 auto;
    padding: 0;
    max-width: 560px;
}

.game-item {
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin-bottom: 10px;
    padding: 12px 10px;
    background: #1c1c1c;
    border: 1px solid #333;
    border-radius: 12px;
    color: #fff;
    font-family: 'League Gothic', sans-serif;
    cursor: pointer;
}

.gl-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
}

.gl-team {
    flex: 1;
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
}

.gl-team:first-child {
    justify-content: flex-end;
}

.gl-team:last-child {
    flex-direction: row-reverse;
    justify-content: flex-end;
}

.gl-name {
    font-size: 24px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.gl-score {
    font-size: 30px;
    opacity: 0.75;
}

.gl-team.lead .gl-score {
    opacity: 1;
}

.gl-dash {
    font-size: 24px;
    color: #777;
}

.gl-meta {
    font-family: 'Roboto', sans-serif;
    font-size: 13px;
    color: #aaa;
}

.gl-badge {
    display: inline-block;
    margin-right: 6px;
    font-family: 'Roboto', sans-serif;
    font-size: 11px;
    background: #4CAF50;
    color: #fff;
    padding: 2px 6px;
    border-radius: 8px;
}

.games-empty, .detail-meta, .detail-note {
    font-family: 'Roboto', sans-serif;
    color: #aaa;
    font-size: 14px;
}

.back-btn {
    display: block;
    background: none;
    border: none;
    color: #ccc;
    font-family: 'Roboto', sans-serif;
    font-size: 15px;
    padding: 6px 0;
    margin-bottom: 6px;
    cursor: pointer;
}

.line-score-wrap {
    overflow-x: auto;
    max-width: 100%;
    margin: 12px auto;
}

.line-score {
    border-collapse: collapse;
    margin: 0 auto;
    font-size: 22px;
}

.line-score th, .line-score td {
    padding: 6px 10px;
    border-bottom: 1px solid #333;
    font-weight: normal;
    text-align: center;
    min-width: 22px;
}

.line-score tbody th {
    text-align: left;
    white-space: nowrap;
}

.line-score td.now {
    background: #2a2a2a;
}

.line-score td.total {
    font-size: 26px;
    border-left: 1px solid #555;
}

.detail-actions, .modal-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: 400px;
    margin: 20px auto 0;
}

.detail-actions button, .modal-buttons button {
    font-family: 'League Gothic', sans-serif;
    font-size: 28px;
    padding: 10px;
    border-radius: 25px;
    cursor: pointer;
}

#resumeGameButton {
    background: #4CAF50;
    color: #fff;
    border: none;
}

/* Modal */
.modal {
    display: flex;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    padding: 16px;
    box-sizing: border-box;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
}

.modal-content {
    box-sizing: border-box;
    background-color: #302f2f;
    margin: auto;
    padding: 24px;
    border-radius: 15px;
    width: 85%;
    max-width: 400px;
    text-align: center;
    border: 2px solid #fff;
}

.modal-content h2 {
    color: #fff;
    margin: 0;
    font-family: 'Roboto', sans-serif;
    font-weight: normal;
    font-size: 18px;
    line-height: 1.4;
}

/* Mobile Optimizations */
@media screen and (max-width: 768px) {
    /* Update footer height for mobile */
    :root {
        --footer-height: 72px; /* Footer on mobile */
        --header-height: 64px;
    }
    
    /* Use large viewport height to account for mobile browser UI */
    html {
        height: 100%;
        height: 100lvh; /* Large viewport height */
    }
    
    .app-header {
        height: var(--header-height);
        padding: 12px;
    }
    
    .scoring-screen {
        justify-content: flex-start;
        padding-top: 16px;
    }

    .scoring-screen, .new-game-screen, .games-screen {
        margin-top: var(--header-height);
        margin-bottom: var(--total-footer-height);
    }
    
    body {
        /* min-height, not height: a fixed height let tall screens spill past the body, so the
           padding below never reached the content and the last buttons stayed under the footer */
        min-height: 100%;
        /* Use the calculated total footer height instead of hard-coded value */
        padding-bottom: var(--total-footer-height);
    }
    
    .app-header {
        padding: 8px;
    }
    
    .app-title {
        font-size: 28px;
    }
    
    .logo {
        width: 40px;
        height: 40px;
    }
    
    .home-link, .field-selector {
        font-size: 18px;
        padding: 6px 8px;
    }
    
    .field-selector {
        font-size: 18px;
        padding: 6px 10px;
        right: 20px; /* Slightly less padding on mobile but still away from edge */
    }
    
    .modal-content {
        width: 90%;
        padding: 20px;
    }
    
    .team-name {
        font-size: 36px;
    }
    
    .score {
        font-size: 60px;
    }
    
    .score-btn {
        font-size: 2.5em !important;
        padding: 8px !important;
        margin-bottom: 8px !important;
    }
    
    .field-display {
        font-size: 1.2em;
    }
    
    #newGameScreen select, #newGameScreen button {
        font-size: 1.5em;
        padding: 8px;
    }
    
    /* Mobile footer fixes */
    :root {
        --footer-height: 72px; /* Footer on mobile */
    }
    
    .app-footer {
        padding: 6px 10px;
        /* Ensure footer is always visible on mobile */
        bottom: 0;
        position: fixed;
        height: var(--footer-height);
        /* Add extra padding for mobile safe areas */
        padding-bottom: calc(6px + var(--safe-area-bottom));
        /* Prevent footer from being cut off */
        width: 100%;
        box-sizing: border-box;
    }
    
    .footer-btn {
        font-size: 28px;
        padding: 8px;
        min-height: 44px; /* Ensure touch target is large enough */
        min-width: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .footer-btn i {
        font-size: 28px;
    }
}

/* Short screens (phones held sideways): slimmer header and footer so the buttons get the room */
@media screen and (max-height: 500px) {
    :root {
        --footer-height: 58px;
        --header-height: 52px;
    }

    .app-header {
        padding: 6px;
    }

    .logo {
        width: 34px;
        height: 34px;
    }

    .app-title {
        font-size: 24px;
    }

    .app-footer {
        height: var(--footer-height);
        padding: 4px 10px;
        padding-bottom: calc(4px + var(--safe-area-bottom));
    }

    .footer-btn, .footer-btn i {
        font-size: 20px;
    }

    .footer-btn {
        min-height: 44px;
    }
}

/* Phones: the scoring screen fills exactly the space between header and footer, and the five
   score buttons share whatever height is left, so a scorer never has to scroll mid-game. */
@media screen and (max-width: 768px) {
    .scoring-screen {
        box-sizing: border-box;
        width: 100%;
        min-height: calc(100vh - var(--header-height) - var(--total-footer-height));
        min-height: calc(100dvh - var(--header-height) - var(--total-footer-height));
        margin-bottom: 0;
        padding: 8px 0 10px;
    }

    .scoreboard {
        margin-bottom: 4px;
    }

    .team-name {
        font-size: 32px;
        line-height: 1.1;
        margin: 0;
    }

    .score {
        font-size: 56px;
        line-height: 1.05;
    }

    .half-net {
        margin: 0 0 8px;
    }

    .score-buttons {
        flex: 1 0 auto;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
    }

    .score-buttons .score-btn {
        flex: 1 1 0;
        min-height: 44px;
        max-height: 84px;
        margin: 0 !important;
        padding: 0 !important;
        line-height: 1;
        font-size: 2.4em !important;
    }
}

/* Short phone screens: tighten the top so the buttons keep a good size */
@media screen and (max-width: 768px) and (max-height: 620px) {
    :root { --header-height: 56px; --footer-height: 64px; }
    .logo { width: 34px; height: 34px; }
    .app-title { font-size: 24px; }
    .sb-name { font-size: 16px; }
    .sb-score { font-size: 22px; }
    .team-name { font-size: 26px; }
    .score { font-size: 44px; }
    .half-net { font-size: 13px; margin-bottom: 6px; }
    .score-buttons { gap: 6px; }
    .score-buttons .score-btn { min-height: 40px; font-size: 2em !important; }
}

/* Very short portrait screens (iPhone SE 1st gen in Safari): the strip at the top already
   highlights the batting team, so drop the big name to keep all five buttons on screen. */
@media screen and (max-width: 768px) and (max-height: 480px) and (orientation: portrait) {
    .team-name { display: none; }
    .score-buttons { gap: 5px; }
    .score-buttons .score-btn { min-height: 36px; font-size: 1.8em !important; }
    .scoring-screen { padding: 2px 0 4px; }
}

/* Phone held sideways: one row of score buttons under the score, nothing to scroll. */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .scoring-screen {
        box-sizing: border-box;
        width: 100%;
        min-height: calc(100vh - var(--header-height) - var(--total-footer-height));
        min-height: calc(100dvh - var(--header-height) - var(--total-footer-height));
        margin-top: var(--header-height);
        margin-bottom: 0;
        padding: 4px 0 8px;
        justify-content: flex-start;
    }

    body {
        padding-bottom: var(--total-footer-height);
    }

    .team-name { display: none; }
    .scoreboard { margin-bottom: 0; }
    .sb-name { font-size: 16px; }
    .sb-score { font-size: 22px; }
    .score { font-size: 40px; line-height: 1.05; }
    .half-net { font-size: 13px; margin: 0 0 6px; }

    .score-buttons {
        flex: 1 0 auto;
        display: flex;
        flex-direction: row;
        align-items: stretch;
        justify-content: center;
        gap: 8px;
        width: 96%;
        max-width: 900px;
    }

    .score-buttons .score-btn {
        flex: 1 1 0;
        width: auto;
        min-height: 48px;
        max-height: 110px;
        margin: 0 !important;
        padding: 0 !important;
        font-size: 2.2em !important;
        line-height: 1;
    }
}

/* Short phones: keep the whole new-game form above the footer */
@media screen and (max-width: 768px) and (max-height: 620px) {
    .new-game-screen { padding-top: 8px; }
    .new-game-screen h1 { font-size: 28px; margin: 4px 0; }
    #newGameScreen select, #newGameScreen button { font-size: 1.3em; margin: 6px; padding: 6px; min-height: 44px; }
    .vs { font-size: 1.4em; }
}
