/* Basic Reset & Defaults */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    padding: 20px;
    background-color: #2a2a2a;
    color: #e0e0e0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

h1 {
    width: 100%;
    text-align: center;
    margin-bottom: 20px;
    color: #ffffff;
}

/* General Panel Styling */
.panel {
    border: 1px solid #555;
    background-color: #3d3d3d;
    padding: 15px 20px;
    border-radius: 6px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.4);
    /* REMOVED default width: 100%; let container decide */
    margin-bottom: 20px; /* Default margin for stacking */
}
/* Remove bottom margin from last panel inside main-game */
#main-game > .panel:last-child {
    margin-bottom: 0;
}


.panel h2 {
    margin-bottom: 15px;
    color: #ffffff;
    border-bottom: 1px solid #555;
    padding-bottom: 8px;
    font-size: 1.1em;
    font-weight: 600;
}

/* Game Setup Area Styling */
#game-setup {
    max-width: 500px;
    margin: 30px auto;
    text-align: center;
    border: 1px solid #555;
    background-color: #3d3d3d;
    padding: 15px 20px;
    border-radius: 6px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.4);
    width: auto;
    display: block; /* Initially visible */
    margin-bottom: 20px;
}

#game-setup div {
    margin: 20px 0;
}

#game-setup label {
    margin-right: 10px;
    font-weight: 500;
}

#days-select {
    padding: 8px 10px;
    border: 1px solid #666;
    border-radius: 4px;
    background-color: #555;
    color: #eee;
    font-size: 1em;
    min-width: 120px;
    cursor: pointer;
}

#days-select:disabled {
     opacity: 0.6;
     cursor: not-allowed;
}

#start-game-button {
    padding: 12px 30px;
    font-size: 1.1em;
    background-color: #4CAF50;
    border-color: #3a8e40;
    color: white;
    font-weight: bold;
    margin-top: 10px;
}

#start-game-button:hover {
     background-color: #5cbf60;
}

#start-game-button:disabled {
    background-color: #5a5a5a;
    border-color: #777;
     opacity: 0.6;
     cursor: not-allowed;
}

/* Main Game Area - Hidden Initially */
#main-game {
    display: none; /* Hide main game area by default */
    width: 100%; /* Occupy full width */
    /* It's a container for its children, default flow (block) is fine */
    /* Children will stack unless put in a flex/grid wrapper */
}

/* Styling for the wrapper containing the top 5 panels */
#top-panels-wrapper {
    display: flex; /* Use Flexbox to arrange top panels in a row */
    flex-wrap: nowrap; /* Prevent top panels from wrapping */
    gap: 20px; /* Space between the top panels */
    width: 100%; /* Make the wrapper take full width */
    margin-bottom: 20px; /* Space below the top row */
}

/* Replace the existing rule for panels inside the wrapper */
#top-panels-wrapper > .panel {
    flex: 1 1 0; /* Grow equally, shrink equally, basis 0 - THIS IS THE KEY LINE */
    min-width: 150px; /* Prevent excessive shrinking */
    width: auto; /* Let flexbox determine width */
    margin-bottom: 0; /* Remove default panel margin for items in this row */
}
/* Travel Panel - Stacks below wrapper */
#travel-panel {
     width: 100%; /* Ensure it takes full width */
     margin-bottom: 20px; /* Keep space below it */
}

#travel-panel ul#travel-locations-list {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    padding-top: 10px;
}
#travel-panel li {
    border-bottom: none;
    padding: 0;
    flex: 1 1 120px;
}
#travel-panel button {
    width: 100%;
    margin: 0;
}


/* Messages Panel - Stacks below wrapper */
#messages {
    width: 100%; /* Ensure it takes full width */
    margin-bottom: 0; /* Last item, remove bottom margin */
}

#message-list {
    max-height: 250px;
    overflow-y: auto;
    border: 1px solid #555;
    padding: 15px;
    margin-top: 10px;
    background-color: #303030;
    font-size: 0.9em;
    color: #ccc;
}

#message-list li {
    border-bottom: 1px dotted #444;
    padding: 6px 2px;
    display: block; /* Override internal flex for message list items */
}


/* List Styling (inside panels) */
ul {
    list-style: none;
}

/* Default li inside panels (excluding travel list and message list) */
.panel > ul:not(#travel-locations-list):not(#message-list) > li {
    padding: 5px 0;
    border-bottom: 1px dashed #555;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 5px;
}
.panel > ul > li:last-child {
    border-bottom: none;
}

/* Input Styling */
input[type="number"] {
    width: 65px;
    padding: 6px;
    margin-left: 5px;
    border: 1px solid #666;
    border-radius: 4px;
    text-align: left; /* Corrected alignment */
    background-color: #555;
    color: #eee;
}

/* Button Styling (General) */
button {
    padding: 9px 16px;
    margin: 5px;
    cursor: pointer;
    border: 1px solid #777;
    border-radius: 4px;
    background-color: #5a5a5a;
    color: #eee;
    transition: background-color 0.2s ease, box-shadow 0.2s ease;
    font-weight: 500;
}

button:hover {
    background-color: #6a6a6a;
    border-color: #888;
    box-shadow: 0 1px 2px rgba(0,0,0,0.3);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.5;
    box-shadow: none;
    background-color: #444;
    border-color: #555;
}

/* Specific Panel Content Adjustments */
#market ul li span.price,
#inventory ul li span.inv-qty {
    font-weight: bold;
    min-width: 60px;
    text-align: left;
}

#market ul li span.price {
    color: #8fbc8f;
    margin-right: auto;
    padding-right: 10px;
}

#market button {
    display: inline-block;
}

#actions button {
    display: block;
    width: calc(100% - 10px);
    margin: 10px 5px;
}

/* Hidden Panels Default State */
#game-over-panel,
#modal-overlay {
    display: none;
}

/* Game Over Panel */
#game-over-panel {
    width: 100%;
    max-width: 600px;
    margin: 30px auto;
    border: 3px solid #ff6347;
    background-color: #4b0000;
    color: #eee;
    text-align: center;
    padding: 30px;
    border-radius: 6px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.4);
    margin-bottom: 0; /* Reset default panel margin */
}

#game-over-panel h2 {
    color: #ff6347;
    font-size: 1.5em;
     border-bottom: 1px solid #777;
     margin-bottom: 15px;
     padding-bottom: 8px;
}
 #game-over-panel h3 {
    margin-top: 20px;
    font-size: 1.2em;
    color: #fff;
 }
 #game-over-panel p {
    margin: 10px 0;
    font-size: 1.1em;
 }
 #game-over-panel hr {
    border: none;
    border-top: 1px solid #777;
    margin: 15px 0;
 }

 /* Leaderboard Display Styling */
.leaderboard-display {
    margin-top: 25px;
    padding-top: 15px;
    border-top: 1px solid #555; /* Separator */
}

.leaderboard-display h3 {
    margin-bottom: 10px;
    color: #eee;
}

.leaderboard-display ol {
    list-style-type: decimal; /* Use numbers for rank */
    margin-left: 25px; /* Indent list */
    padding: 0;
}

.leaderboard-display li {
    border-bottom: 1px dotted #555; /* Separator */
    padding: 6px 0;
    font-size: 0.95em;
    color: #ccc;
    display: list-item; /* Force list item display */
    justify-content: flex-start; /* Reset from panel li */
    text-align: left; /* Reset from panel li */
    flex-wrap: nowrap; /* Prevent wrapping inside leaderboard li */
}

.leaderboard-display li:last-child {
    border-bottom: none;
}

.leaderboard-display li span {
    display: inline-block; /* Allow spacing */
    margin: 0 5px;
    vertical-align: middle; /* Align spans nicely */
}

.leaderboard-name {
    font-weight: bold;
    min-width: 100px;
    display: inline-block;
    margin-right: auto; /* Push cash/score right */
}
.leaderboard-cash {
     color: #8fbc8f; /* Match price color */
     min-width: 100px; /* Adjust as needed */
     text-align: right;
     display: inline-block;
}
.leaderboard-score {
     min-width: 60px; /* Adjust as needed */
     text-align: right;
     display: inline-block;
}


 /* Modal Styles */
#modal-overlay {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 1000;
    display: none; /* Correct default */
    justify-content: center;
    align-items: center;
    padding: 20px;
}

#modal-box {
    background-color: #3d3d3d;
    color: #e0e0e0;
    padding: 30px;
    border-radius: 8px;
    border: 1px solid #555;
    box-shadow: 0 5px 15px rgba(0,0,0,0.5);
    text-align: center;
    max-width: 500px;
    width: 90%;
}

#modal-message {
    margin-bottom: 25px;
    font-size: 1.1em;
    line-height: 1.7;
    white-space: pre-wrap; /* Allow \n for line breaks */
}

#modal-buttons button {
    padding: 10px 20px;
    margin: 5px 10px;
    min-width: 80px;
    font-size: 1em;
}

#modal-buttons button.confirm {
    background-color: #4CAF50;
    border-color: #3a8e40;
    color: white;
}
#modal-buttons button.confirm:hover {
    background-color: #5cbf60;
}

#modal-buttons button.cancel {
    background-color: #f44336;
    border-color: #d32f2f;
    color: white;
}
#modal-buttons button.cancel:hover {
    background-color: #f66a6a;
}

@media (max-width: 768px) {

    body {
        padding: 10px; /* Reduce padding on smaller screens */
        gap: 15px; /* Slightly reduce gap */
    }

    h1 {
        font-size: 1.5em; /* Slightly smaller title */
        margin-bottom: 15px;
    }

    /* Adjust general panel padding */
    .panel {
        padding: 10px 15px;
        margin-bottom: 15px; /* Adjust default margin */
    }

     /* Make setup panel full width on mobile */
     #game-setup {
        max-width: none;
        width: 100%;
        margin: 15px 0; /* Adjust margin */
     }

    /* Main Game container gap */
     #main-game {
        gap: 15px;
     }

    /* Make the top panels stack vertically */
    #top-panels-wrapper {
        flex-direction: column; /* Stack panels vertically */
        gap: 15px; /* Use gap for spacing when stacked */
        margin-bottom: 15px; /* Adjust space below */
    }

    /* Reset flex properties for panels inside the wrapper */
    #top-panels-wrapper > .panel {
        flex: none; /* Remove flex sizing */
        width: 100%; /* Make each panel full width */
        min-width: initial; /* Remove min-width */
        margin-bottom: 0; /* Rely on parent gap */
    }

    /* Adjust Travel panel button layout for smaller screens */
    #travel-panel li {
        /* Allow maybe 2-3 buttons per row */
        flex-basis: calc(50% - 10px); /* Two buttons per row, accounting for gap */
        min-width: 100px; /* Smaller min-width */
    }

     /* Adjust message list height slightly */
     #message-list {
        max-height: 200px;
     }

    /* Adjust padding for Game Over panel */
     #game-over-panel {
        padding: 20px;
     }

     /* Adjust padding for Modal */
     #modal-box {
        padding: 20px;
     }
     #modal-message {
        margin-bottom: 20px;
     }
      #modal-buttons button {
        padding: 8px 16px;
        font-size: 0.9em;
     }

} /* End of @media block */