body {
    margin: 0;
    padding: 0;
    font-family: 'Space Mono', monospace;
    color: #333333;
    height: 100vh;
    display: flex;
    background-color: #f5f5f5;
    position: relative;
}

/* Light/Dark mode styling */
body.light-mode {
    background-color: #f5f5f5;
    color: #333333;
}

body.dark-mode {
    background-color: #333;
    color: #f5f5f5;
}

.main-container {
    width: 100%;
    height: 100%;
    display: flex;
    position: relative; /* Added to ensure proper positioning of children */
    overflow: hidden; /* Prevent scrolling caused by absolute positioning */
}

.map-container {
    width: 55%; /* Fixed width for map container */
    height: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: stretch;
    overflow: hidden;
    position: relative;
    padding: 0;
    background-color: #f8f8f8; /* Match input container background */
}

body.dark-mode .map-container {
    background-color: #444; /* Match dark mode input container */
}

.input-container {
    width: 100%;
    height: 100%;
    padding: 50px 15px 20px 15px; /* Increased bottom padding from 10px to 20px */
    overflow-y: auto;
    box-sizing: border-box;
    background-color: #f8f8f8;
    position: relative; /* Changed from absolute to relative */
}

body.dark-mode .input-container {
    background-color: #444;
}

.input-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(85px, 1fr)); /* Changed to auto-fit with min width */
    gap: 6px; /* Reduced from 10px to 6px */
}

/* Fix vertical alignment of country labels */
.input-item {
    position: relative;
    min-height: 70px; /* Reduced from 80px to 70px */
    margin-bottom: 6px; /* Reduced from 10px to 6px */
    padding: 0;
    width: 100%;
    box-sizing: border-box;
}

.input-item label {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 30px; /* Reduced from 35px to 30px */
    font-weight: normal; /* Changed from bold to normal */
    font-size: 0.82rem;
    text-align: center;
    padding: 2px; /* Reduced from 3px to 2px */
    /* Flex properties for vertical alignment */
    display: flex;
    align-items: flex-end; /* This aligns text to bottom */
    justify-content: center;
    line-height: 1.2;
    word-wrap: break-word;
    hyphens: auto;
}

/* Special handling for multi-line text */
.input-item label:has(br), 
.input-item label:not(:empty) {
    bottom: 30px; /* Consistent with single-line */
}

.input-item input {
    position: absolute;
    bottom: 2px; /* Reduced from 5px to 2px */
    left: 50%;
    transform: translateX(-50%);
    width: 50px;
    height: 25px;
    text-align: center;
    padding: 2px 0;
    font-family: 'Space Mono', monospace;
    border: 1px solid #ccc;
    border-radius: 3px;
    background-color: white;
    align-self: center; /* Ensure center alignment */
}

body.dark-mode .input-item input {
    background-color: #555;
    color: #fff;
    border-color: #666;
}

/* Remove default number input arrows */
.input-item input::-webkit-outer-spin-button,
.input-item input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
.input-item input[type=number] {
    -moz-appearance: textfield;
}

/* Add bottom margin to ensure input doesn't overlap labels */
.input-item:after {
    content: '';
    display: block;
    height: 35px; /* Height of input + margin */
    width: 100%;
}

.bonus-section {
    margin-top: 6px; /* Reduced from 10px to 6px */
    border-top: 1px solid #ddd;
    padding-top: 6px; /* Reduced from 8px to 6px */
}

body.dark-mode .bonus-section {
    border-top-color: #555;
}

.bonus-section h3 {
    margin-top: 0;
    margin-bottom: 6px; /* Reduced from 8px to 6px */
    font-size: 0.95rem; /* Slightly smaller */
}

.bonus-section .input-item input {
    width: 70px; /* Double the width */
    height: 25px; /* Double the height */
}

/* Bonus section styling adjustments */
.bonus-section {
    margin-top: 6px;
    border-top: 1px solid #ddd;
    padding-top: 6px;
}

.bonus-section h3 {
    margin-top: 0;
    margin-bottom: 6px;
    font-size: 0.95rem;
}

.bonus-section .input-item {
    /* Increase height for bonus items */
    min-height: 70px; /* Increased from 60px to 70px */
    margin-bottom: 8px; /* Increased from 6px to 8px */
    /* Use tight flex spacing to keep elements close */
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
}

.bonus-section .input-item label {
    bottom: 35px; /* Increased from 28px to 35px for more space */
    font-size: 0.8rem;
    text-align: center;
    width: 100%; /* Ensure text has full width */
    padding: 0 2px; /* Add horizontal padding */
    white-space: normal; /* Allow text to wrap */
}

.bonus-section .input-item input {
    width: 70px;
    height: 25px;
}

h2 {
    position: absolute;
    top: 20px; /* Align with buttons */
    left: 20px;
    margin: 0;
    text-align: left;
    font-size: 1.3rem;
    z-index: 100;
    /* Add flexbox properties to match button height */
    height: 40px;
    display: flex;
    align-items: center; /* Vertically center text */
}

svg {
    max-width: 100%;
    height: 100% !important;
    transition: background-color 0.5s ease; /* Add transition for background color */
}

body.light-mode svg {
    background-color: #f5f5f5; /* Light mode background */
}

body.dark-mode svg {
    background-color: #333; /* Dark mode background */
}

path:hover {
    fill-opacity: 0.8;
    cursor: pointer;
}

#svg-container {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: stretch;
    margin: 0;
    padding: 0;
    background-color: inherit; /* Inherit from parent */
}

#error-message {
    color: red;
    text-align: center;
    display: none;
}

/* Theme toggle button styles */
.theme-toggle {
    position: absolute;
    top: 20px;
    right: 20px; /* Keep at far right */
    background-color: transparent;
    color: #333;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    cursor: pointer;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.3s, transform 0.3s;
    z-index: 100;
}

.theme-toggle:hover {
    background-color: rgba(0, 0, 0, 0.1);
    transform: scale(1.1);
}

body.dark-mode .theme-toggle {
    color: #f5f5f5;
}

body.dark-mode .theme-toggle:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

.theme-toggle .fa-sun {
    display: none;
}

.theme-toggle .fa-moon {
    display: block;
}

body.dark-mode .theme-toggle .fa-sun {
    display: block;
}

body.dark-mode .theme-toggle .fa-moon {
    display: none;
}

/* Home button styles */
.home-link {
    position: absolute;
    top: 20px;
    right: 70px; /* Adjust as needed */
    color: #333;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    font-size: 20px;
    transition: background-color 0.3s, transform 0.3s;
    text-decoration: none;
    z-index: 100;
    outline: none;
}

.home-link:hover {
    background-color: rgba(0, 0, 0, 0.1);
    transform: scale(1.1);
    color: #333;
    text-decoration: none;
    outline: none;
}

body.dark-mode .home-link {
    color: #f5f5f5;
}

body.dark-mode .home-link:hover {
    background-color: rgba(255, 255, 255, 0.1);
    color: #f5f5f5;
}

/* Info button styles */
.info-button {
    position: absolute;
    top: 20px;
    right: 120px; /* Adjust as needed */
    background-color: transparent;
    color: #333;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    cursor: pointer;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.3s, transform 0.3s;
    z-index: 100;
}

.info-button:hover {
    background-color: rgba(0, 0, 0, 0.1);
    transform: scale(1.1);
}

body.dark-mode .info-button {
    color: #f5f5f5;
    }

body.dark-mode .info-button:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Info popup styles */
.info-popup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.popup-content {
    background-color: #f8f8f8;
    width: 80%;
    max-width: 500px;
    max-height: 80vh;
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
}

body.dark-mode .popup-content {
    background-color: #444;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
}

.popup-content h3 {
    margin-top: 0;
    text-align: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
}

body.dark-mode .popup-content h3 {
    border-bottom-color: #555;
}

.popup-text {
    overflow-y: auto;
    margin: 15px 0;
    padding-right: 10px;
}

.popup-text p, .popup-text ol, .popup-text ul {
    margin-bottom: 15px;
}

.close-popup-button {
    align-self: center;
    padding: 8px 20px;
    background-color: #4a6fa5;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-family: 'Space Mono', monospace;
    transition: background-color 0.3s;
    margin-top: 10px;
}

.close-popup-button:hover {
    background-color: #3a5a8a;
}

body.dark-mode .close-popup-button {
    background-color: #5a7fb5;
}

body.dark-mode .close-popup-button:hover {
    background-color: #4a6fa5;
}

/* Theme transitions */
body.theme-enabled {
    transition: background-color 0.5s ease, color 0.5s ease;
}

body.theme-enabled .theme-toggle,
body.theme-enabled .home-link,
body.theme-enabled .input-container,
body.theme-enabled .input-item input,
body.theme-enabled .info-button,
body.theme-enabled .popup-content,
body.theme-enabled .close-popup-button {
    transition: color 0.5s ease, background-color 0.3s ease, transform 0.3s ease, border-color 0.3s ease;
}

/* Customize scrollbar for popup text */
.popup-text::-webkit-scrollbar {
    width: 8px;
}

.popup-text::-webkit-scrollbar-track {
    background: #f0f0f0;
    border-radius: 4px;
}

.popup-text::-webkit-scrollbar-thumb {
    background: #aaa;
    border-radius: 4px;
}

.popup-text::-webkit-scrollbar-thumb:hover {
    background: #888;
}

body.dark-mode .popup-text::-webkit-scrollbar-track {
    background: #333;
}

body.dark-mode .popup-text::-webkit-scrollbar-thumb {
    background: #666;
}

body.dark-mode .popup-text::-webkit-scrollbar-thumb:hover {
    background: #888;
}

/* Mobile Responsive Design */
@media (max-width: 768px), (orientation: portrait) {
    .main-container {
        flex-direction: column;
    }
    
    .map-container {
        width: 100%;
        height: auto;
        min-height: 300px;
        max-height: 50%;
        overflow: hidden; /* Ensure overflow is hidden to maintain cropping */
        position: relative;
    }
    
    #svg-container {
        height: auto;
        display: flex;
        justify-content: center; /* Center the SVG horizontally */
        align-items: center; /* Center the SVG vertically */
        overflow: hidden; /* Hide overflow */
    }
    
    svg {
        width: 100% !important;
        height: auto !important;
        min-height: 300px;
        max-height: 50vh;
        object-fit: cover; /* Change to cover to maintain aspect ratio while filling container */
        object-position: center top; /* Position from center-top */
        overflow: hidden;
        display: block; /* Ensure proper display */
    }
    
    .input-container {
        width: 100%;
        height: 50%;
        padding: 60px 10px 10px 10px;
        overflow-y: auto;
    }
    
    .input-grid {
        grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); /* Adjusted for mobile */
    }
    
    .input-item {
        min-height: 80px; /* Reduced from 95px */
    }
    
    .input-item label {
        bottom: 38px; /* Reduced from 45px */
        font-size: 0.75rem;
        line-height: 1.1;
        padding: 3px 2px;
    }
    
    .input-item input {
        height: 28px;
        bottom: 5px;
    }

    /* Adjust buttons positioning for mobile */
    .theme-toggle,
    .home-link,
    .info-button {
        top: 10px;
    }
    
    h2 {
        top: 10px; /* Keep at 10px on mobile */
        left: 10px;
        font-size: 1.1rem;
        height: 35px; /* Match mobile button height */
    }
    
    /* Adjust popup for mobile */
    .popup-content {
        width: 90%;
        max-height: 70vh;
        padding: 15px;
    }
    
    .bonus-section .input-item input {
        width: 60px;
    }

    .input-item {
        height: 50px; /* Slightly smaller for mobile */
    }
    
    .input-item label {
        height: 25px;
        font-size: 0.75rem;
    }

    .controls-container {
        padding: 8px 0;
        position: relative;
        top: auto;
        width: calc(100% - 20px); /* Account for container padding */
        margin-bottom: 10px;
    }
    
    .control-button {
        padding: 6px 10px;
        font-size: 0.75rem;
    }
    
    .score-display {
        font-size: 0.85rem;
    }

    /* Center bonus heading on mobile */
    .bonus-section h3 {
        text-align: center;
        width: 100%;
        font-weight: bold;
        margin-bottom: 15px; /* Add more space below the heading */
    }
    
    /* Improve vertical spacing in bonus section on mobile */
    .bonus-section .input-item {
        min-height: 65px; /* Increased from 55px */
        margin-bottom: 15px;
        padding: 5px 0;
    }
    
    .bonus-section .input-item label {
        min-height: 20px; /* Increased from 15px */
        bottom: 30px; /* Set explicit bottom value for mobile */
        margin-bottom: 8px; /* Increased from 6px */
        font-size: 0.8rem;
        padding: 0 2px;
    }
    
    .bonus-section .input-item input {
        margin-top: 2px;
    }

    /* Add extra padding at the bottom for mobile to prevent Firefox search bar overlap */
    .bonus-section {
        margin-bottom: 80px; /* Add significant bottom margin on mobile */
    }
}

/* For very tall and narrow screens, adjust height proportions */
@media (max-height: 600px) and (orientation: portrait) {
    .map-container {
        height: auto;
        max-height: 45vh;
        overflow: hidden;
    }
    
    svg {
        max-height: 45vh;
        overflow: hidden;
    }
    
    .input-container {
        height: auto;
        min-height: 55vh;
    }
}

/* For wider but shorter screens in landscape */
@media (max-height: 500px) and (orientation: landscape) {
    .main-container {
        flex-direction: row;
    }
    
    .map-container {
        width: 60%;
        height: 100%;
        overflow: hidden;
    }
    
    svg {
        height: 100% !important;
        width: auto !important;
        overflow: hidden;
    }
    
    .input-container {
        height: 100%;
        padding-top: 50px;
    }
    
    svg {
        height: 100% !important;
        width: auto !important;
    }
    
    .input-item {
        min-height: 60px;
    }
    
    .input-item label {
        min-height: 38px;
        font-size: 0.72rem;
    }
    
    .input-grid {
        grid-template-columns: repeat(auto-fit, minmax(75px, 1fr)); /* More columns in landscape */
    }
}

/* Even smaller screens */
@media (max-width: 480px) {
    .input-grid {
        grid-template-columns: repeat(auto-fit, minmax(60px, 1fr)); /* Adjusted for very small screens */
        gap: 5px;
    }
    
    .input-item {
        min-height: 75px; /* Reduced from 80px */
    }
    
    .input-item label {
        bottom: 35px; /* Reduced from 40px */
        font-size: 0.7rem;
        /* Handle very long country names by allowing them to be truncated with ellipsis if absolutely necessary */
        white-space: normal;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3; /* Show max 3 lines */
        -webkit-box-orient: vertical;
    }
    
    .theme-toggle,
    .home-link,
    .info-button {
        width: 35px;
        height: 35px;
        font-size: 16px;
    }
    
    .theme-toggle {
        right: 10px;
    }
    
    .home-link {
        right: 50px;
    }
    
    .info-button {
        right: 90px;
    }
    
    h2 {
        font-size: 1rem;
    }

    .input-item {
        min-height: 65px;
        padding: 3px 0;
        justify-content: flex-start; /* Keep consistent with larger screens */
    }
    
    .input-item label {
        min-height: 28px;
        margin-bottom: 6px; /* Fixed spacing */
        font-size: 0.7rem;
        line-height: 1.05;
        /* Handle very long country names by allowing them to be truncated with ellipsis if absolutely necessary */
        white-space: normal;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3; /* Show max 3 lines */
        -webkit-box-orient: vertical;
    }

    .input-item input {
        margin-top: auto; /* Push input to bottom */
    }
    
    .controls-container {
        gap: 5px;
    }
    
    .control-button {
        padding: 5px 8px;
        font-size: 0.7rem;
    }

    /* Further adjustments for very small screens */
    .bonus-section h3 {
        font-size: 0.95rem;
        margin-bottom: 12px;
    }
    
    .bonus-section .input-item {
        min-height: 50px;
        padding: 4px 0;
        justify-content: space-between;
    }
    
    .bonus-section .input-item label {
        margin-bottom: 5px; /* Fixed spacing */
        min-height: 15px;
    }
}

/* For very tall and narrow screens, adjust height proportions */
@media (max-height: 600px) and (orientation: portrait) {
    .map-container {
        height: 40%;
    }
    
    .input-container {
        height: 60%;
    }
}

/* Very narrow screens fix */
@media (max-width: 320px) {
    .input-item {
        min-height: 70px; /* Reduced from 75px */
    }
    
    .input-grid {
        grid-template-columns: repeat(auto-fit, minmax(50px, 1fr));
        gap: 3px;
    }
    
    .input-item input {
        width: 40px;
        bottom: 3px;
    }

    .input-item label {
        bottom: 30px; /* Reduced from 35px */
        font-size: 0.65rem;
    }
}

/* Controls container styles */
.controls-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin-bottom: 10px; /* Reduced from 15px to 10px */
    padding: 3px 5px 8px; /* Reduced padding */
    border-bottom: 1px solid #ddd;
    position: relative; /* Changed from sticky to relative for better mobile behavior */
    background-color: inherit; /* Inherit background color */
    z-index: 10; /* Ensure controls appear above other content */
}

body.dark-mode .controls-container {
    border-bottom-color: #555;
}

/* Button styles */
.control-button {
    padding: 8px 15px;
    border: none;
    border-radius: 4px;
    font-family: 'Space Mono', monospace;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background-color 0.3s, transform 0.2s;
}

.control-button:hover {
    transform: translateY(-2px);
}

.check-button {
    background-color: #4a6fa5;
    color: white;
}

.check-button:hover {
    background-color: #3a5a8a;
}

.reset-button {
    background-color: #a56a4a;
    color: white;
}

.reset-button:hover {
    background-color: #8a5a3a;
}

.hardmode-toggle {
    background-color: #6a4aa5;
    color: white;
}

.hardmode-toggle:hover {
    background-color: #5a3a8a;
}

/* Score display */
.score-display {
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 1rem;
    padding: 8px 15px;
    background-color: #f0f0f0;
    border-radius: 4px;
    min-width: 80px;
}

body.dark-mode .score-display {
    background-color: #555;
}

/* Input styling for correct/incorrect answers */
.correct-answer {
    border-color: #008800 !important;
    background-color: rgba(0, 255, 0, 0.1) !important;
}

.incorrect-answer {
    border-color: #880000 !important;
    background-color: rgba(255, 0, 0, 0.1) !important;
}

/* Custom confirmation popup styles */
.confirm-popup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.confirm-popup .popup-content {
    width: 85%;
    max-width: 400px;
    padding: 20px;
    border-radius: 10px;
    text-align: center;
}

.confirm-popup h3 {
    margin-top: 0;
    margin-bottom: 15px;
    font-size: 1.2rem;
}

.confirm-message {
    margin: 15px 0 25px;
    font-size: 0.95rem;
    line-height: 1.4;
}

.confirm-buttons {
    display: flex;
    justify-content: center;
    gap: 15px;
}

.confirm-button {
    padding: 8px 20px;
    border: none;
    border-radius: 5px;
    font-family: 'Space Mono', monospace;
    cursor: pointer;
    transition: background-color 0.3s, transform 0.2s;
    min-width: 100px;
}

.confirm-button:hover {
    transform: translateY(-2px);
}

.cancel-button {
    background-color: #888;
    color: white;
}

.cancel-button:hover {
    background-color: #777;
}

.confirm-button-primary {
    background-color: #a56a4a;
    color: white;
}

.confirm-button-primary:hover {
    background-color: #8a5a3a;
}

/* Dark mode styling for confirmation popup */
body.dark-mode .confirm-popup .popup-content {
    background-color: #444;
    color: #f5f5f5;
}

body.dark-mode .confirm-popup h3 {
    border-bottom-color: #555;
}

/* Mobile-specific styling for confirmation popup */
@media (max-width: 768px) {
    .confirm-popup .popup-content {
        width: 90%;
        padding: 15px;
    }
    
    .confirm-message {
        margin: 12px 0 20px;
        font-size: 0.9rem;
    }
    
    .confirm-buttons {
        gap: 10px;
    }
    
    .confirm-button {
        padding: 7px 15px;
        min-width: 80px;
        font-size: 0.85rem;
    }
}
