/* Base Styles */
body {
    font-family: 'Roboto', 'Arial', sans-serif;
    background-color: #111;
    background-image: 
        linear-gradient(to right top, rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8)),
        url('https://www.dragonsports.eu/512925-verylarge_default/panel-for-mma-cage-with-door-ninestars.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center top;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height for modern browsers */
    color: white;
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    overflow-y: auto;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Main Container */
#defaultScreen {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh; /* Dynamic viewport height */
    padding: 8px;
    box-sizing: border-box;
    gap: 8px;
}

/* Top Section */
#topSection {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 15px;
}

.score-name-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.timer-round-row {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin: 10px 0;
}

/* Fighter Names and Scores */
.fighter-name {
    flex: 1;
    padding: 12px 15px;
    background-color: rgba(26, 26, 26, 0.9);
    border-radius: 8px;
    font-size: 1.4em;
    font-weight: bold;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
    min-height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.fighter-name:hover {
    background-color: rgba(40, 40, 40, 0.9);
}

.fighter-name.blue {
    border: 3px solid #2196F3;
    color: #2196F3;
}

.fighter-name.red {
    border: 3px solid #f44336;
    color: #f44336;
}

.score {
    width: 80px;
    height: 60px;
    background-color: rgba(26, 26, 26, 0.9);
    border-radius: 10px;
    font-size: 2.5em;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.3s ease;
}

.score.blue {
    border: 4px solid #2196F3;
    color: #2196F3;
}

.score.red {
    border: 4px solid #f44336;
    color: #f44336;
}

/* Timer and Round Selector */
#timer {
    font-size: 2.2em;
    font-weight: bold;
    padding: 10px 20px;
    border-radius: 8px;
    min-width: 120px;
    text-align: center;
    background-color: #e0e0e0;
    color: #333;
    border: 3px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    transition: all 0.3s ease;
}

#timer:hover {
    background-color: #f0f0f0;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#timer.running {
    background-color: #4CAF50;
    color: white;
}

#timer.paused {
    background-color: #FFD700;
    color: black;
}

#timer.ended {
    background-color: #FF0000;
    color: white;
}

#roundSelector {
    font-size: 1.2em;
    padding: 8px 12px;
    border-radius: 6px;
    background-color: #333;
    color: white;
    border: 2px solid #666;
}

#swapButton {
    background-color: #333;
    color: white;
    border: 2px solid #666;
    padding: 5px 10px;
    font-size: 1.2em;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: all 0.3s ease;
}

#swapButton:hover, #swapButton:active {
    background-color: #444;
    border-color: #888;
}

.swap-blue-arrow {
    color: #2196F3;
    font-weight: bold;
    font-size: 1.5em;
}

.swap-red-arrow {
    color: #f44336;
    font-weight: bold;
    font-size: 1.5em;
}

/* Graph Container */
#graphContainer {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 5px 0;
    min-height: 200px;
    max-height: 50vh; /* Limit graph height to preserve space for controls */
}

.graph-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
    height: 100%;
}

.graph {
    flex: 1;
    background-color: rgba(51, 51, 51, 0.9);
    border: 1px solid #666;
    border-radius: 8px;
    min-height: 100px;
    max-height: 25vh; /* Each graph gets max 25% of viewport height */
}

/* Button Container */
#buttonContainer {
    flex: 0 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 15px;
    margin: 15px 0;
    padding: 0 10px;
}

.fighter-button {
    flex: 1;
    height: 80px;
    font-size: 1.8em;
    font-weight: bold;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    max-width: 120px;
}

.fighter-button:active {
    transform: scale(0.95);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.blue-button {
    background: linear-gradient(135deg, #2196F3, #1976D2);
    color: white;
    border: 3px solid #1976D2;
}

.red-button {
    background: linear-gradient(135deg, #f44336, #d32f2f);
    color: white;
    border: 3px solid #d32f2f;
}

#controlButtonContainer {
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex: 0 0 auto;
}

#controlButtonContainer button {
    padding: 12px 20px;
    font-size: 1.1em;
    font-weight: bold;
    border: none;
    border-radius: 8px;
    background-color: #333;
    color: white;
    border: 2px solid #666;
    cursor: pointer;
    transition: all 0.3s ease;
    min-width: 120px;
}

#controlButtonContainer button:hover,
#controlButtonContainer button:active {
    background-color: #444;
    border-color: #888;
}

/* Bottom Buttons */
#bottomButtons {
    flex: 0 0 auto;
    display: flex;
    justify-content: center;
    gap: 15px;
    padding: 10px 15px 15px 15px; /* Extra bottom padding for mobile browsers */
    padding-bottom: max(15px, env(safe-area-inset-bottom, 15px)); /* Respect safe areas */
}

#exportCSVButton {
    background-color: #0099ff;
    color: white;
    padding: 12px 25px;
    font-size: 1.2em;
    font-weight: bold;
    border-radius: 8px;
    border: 2px solid #0077cc;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}

#exportCSVButton:hover,
#exportCSVButton:active {
    background-color: #0077cc;
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.3);
}

#helpButton {
    background-color: #4CAF50;
    color: white;
    padding: 12px 25px;
    font-size: 1.2em;
    font-weight: bold;
    border-radius: 8px;
    border: 2px solid #388E3C;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
}

#helpButton:hover,
#helpButton:active {
    background-color: #388E3C;
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.3);
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    overflow: auto;
}

.modal-content {
    background-color: #222;
    margin: 20% auto;
    padding: 25px;
    border: 1px solid #444;
    max-width: 90%;
    width: 400px;
    border-radius: 12px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
    position: relative;
}

.close {
    color: #aaa;
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close:hover {
    color: white;
}

.modal-content input {
    width: 100%;
    padding: 12px;
    font-size: 1.1em;
    border: 2px solid #666;
    border-radius: 6px;
    background-color: #333;
    color: white;
    margin-bottom: 15px;
    box-sizing: border-box;
}

.modal-content input:focus {
    outline: none;
    border-color: #2196F3;
}

.dark-grey-button {
    background-color: #444;
    color: white;
    padding: 10px 20px;
    font-size: 1em;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    width: 100%;
}

.dark-grey-button:hover,
.dark-grey-button:active {
    background-color: #555;
}

/* Help Modal Specific */
.help-content {
    max-width: 95%;
    max-height: 85vh;
    overflow-y: auto;
}

.help-content h2 {
    margin-top: 0;
    color: #4CAF50;
    text-align: center;
}

.help-sections {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin: 20px 0;
}

.help-section {
    background-color: #333;
    padding: 15px;
    border-radius: 8px;
}

.help-section h3 {
    margin-top: 0;
    padding-bottom: 8px;
    border-bottom: 1px solid #555;
    color: #4CAF50;
}

.help-section ul,
.help-section ol {
    padding-left: 20px;
    margin-bottom: 10px;
}

.help-section li {
    margin-bottom: 6px;
    line-height: 1.4;
}

/* Username Modal */
.username-modal {
    display: none;
    position: fixed;
    z-index: 200;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    overflow: auto;
}

.username-content {
    background-color: #222;
    margin: 25% auto;
    padding: 25px;
    border: 1px solid #444;
    max-width: 90%;
    width: 400px;
    border-radius: 15px;
    box-shadow: 0 0 25px rgba(0, 0, 0, 0.7);
    text-align: center;
}

.username-content h2 {
    color: #4CAF50;
    margin-top: 0;
    margin-bottom: 15px;
    font-size: 1.8em;
}

.username-content p {
    margin-bottom: 20px;
    font-size: 1.1em;
    color: #ccc;
}

#usernameInput {
    width: 100%;
    max-width: 280px;
    padding: 12px;
    font-size: 1.2em;
    border: 2px solid #666;
    border-radius: 8px;
    background-color: #333;
    color: white;
    margin-bottom: 20px;
    box-sizing: border-box;
    text-align: center;
}

#usernameInput:focus {
    outline: none;
    border-color: #4CAF50;
    box-shadow: 0 0 8px rgba(76, 175, 80, 0.3);
}

#usernameInput::placeholder {
    color: #999;
}

.save-username {
    background-color: #4CAF50;
    color: white;
    padding: 12px 25px;
    font-size: 1.2em;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.3s ease;
    min-width: 140px;
}

.save-username:hover,
.save-username:active {
    background-color: #388E3C;
}

.save-username:disabled {
    background-color: #666;
    cursor: not-allowed;
}

/* Notification Container */
#notificationContainer {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 1000;
}

/* Mobile Browser Viewport Fix */
@media screen and (max-height: 650px) {
    #defaultScreen {
        padding: 5px;
        gap: 5px;
    }
    
    #topSection {
        margin-bottom: 8px;
        gap: 8px;
    }
    
    .timer-round-row {
        margin: 5px 0;
    }
    
    #graphContainer {
        margin: 5px 0;
        min-height: 150px;
        max-height: 40vh;
    }
    
    .graph {
        min-height: 75px;
        max-height: 20vh;
    }
    
    #buttonContainer {
        margin: 8px 0;
    }
    
    #bottomButtons {
        padding: 8px 15px 12px 15px;
    }
    
    .fighter-button {
        height: 65px;
        font-size: 1.5em;
    }
    
    #controlButtonContainer button {
        padding: 8px 15px;
        font-size: 0.95em;
    }
}

/* Very short mobile screens (like when keyboard is open) */
@media screen and (max-height: 500px) {
    #graphContainer {
        min-height: 120px;
        max-height: 30vh;
    }
    
    .graph {
        min-height: 60px;
        max-height: 15vh;
    }
    
    .fighter-button {
        height: 55px;
        font-size: 1.3em;
    }
    
    #timer {
        font-size: 1.8em;
        padding: 6px 12px;
    }
}
@media (max-width: 480px) {
    #defaultScreen {
        padding: 8px;
    }

    .fighter-name {
        font-size: 1.2em;
        padding: 10px 12px;
        min-height: 45px;
    }

    .score {
        width: 70px;
        height: 50px;
        font-size: 2em;
    }

    #timer {
        font-size: 1.8em;
        padding: 8px 15px;
        min-width: 100px;
    }

    #roundSelector {
        font-size: 1em;
        padding: 6px 8px;
    }

    .fighter-button {
        height: 70px;
        font-size: 1.6em;
        max-width: 100px;
    }

    #controlButtonContainer button {
        padding: 10px 15px;
        font-size: 1em;
        min-width: 100px;
    }

    .graph {
        min-height: 120px;
        max-height: 160px;
    }

    .username-content {
        margin: 20% auto;
        padding: 20px;
        width: 350px;
    }

    .username-content h2 {
        font-size: 1.6em;
    }

    #usernameInput {
        font-size: 1.1em;
        padding: 10px;
    }

    .save-username {
        font-size: 1.1em;
        padding: 10px 20px;
    }
}

@media (max-width: 350px) {
    .fighter-name {
        font-size: 1.1em;
        padding: 8px 10px;
    }

    .score {
        width: 60px;
        height: 45px;
        font-size: 1.8em;
    }

    #timer {
        font-size: 1.6em;
        padding: 6px 12px;
        min-width: 90px;
    }

    #roundSelector {
        font-size: 0.9em;
        padding: 5px 6px;
    }

    .fighter-button {
        height: 60px;
        font-size: 1.4em;
        max-width: 80px;
    }

    .username-content {
        width: 300px;
        padding: 15px;
    }
}

/* Landscape Orientation */
@media (orientation: landscape) and (max-height: 600px) {
    #defaultScreen {
        padding: 5px;
    }

    #topSection {
        margin-bottom: 8px;
        gap: 5px;
    }

    .timer-round-row {
        margin: 5px 0;
    }

    #buttonContainer {
        margin: 8px 0;
    }

    #bottomButtons {
        padding: 8px;
    }

    .graph {
        min-height: 100px;
        max-height: 120px;
    }

    .username-content {
        margin: 10% auto;
    }
}

/* Animation for score updates */
@keyframes scoreUpdate {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

.score.updating {
    animation: scoreUpdate 0.3s ease-in-out;
}

/* Shake animation for invalid input */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}