body {
    font-family: 'Fira Code', monospace;
    background-color: #1a1a1a;
    color: #d4d4d4;
    overflow: hidden;
    touch-action: manipulation;
}
#top-inventory-bar {
    position: fixed; top: 0; left: 0; width: 100%;
    background-color: #252526;
    border-bottom: 1px solid #444;
    padding: 0.5rem 1rem;
    display: flex; flex-wrap: wrap; gap: 1.5rem;
    z-index: 10; 
    min-height: 50px;
    height: auto;
    align-items: center;
    box-sizing: border-box;
}
.top-inv-item { display: flex; align-items: center; gap: 0.5rem; }
.top-inv-item span:first-child { color: #f0a048; }

#game-container {
    display: grid;
    grid-template-columns: 3fr 1fr;
    gap: 1rem;
    height: calc(100vh - 50px);
    padding: 1rem;
    margin-top: 50px;
    box-sizing: border-box;
}
#game-map-container {
    background-color: #000;
    border: 1px solid #444;
    border-radius: 8px;
    overflow: hidden;
}
#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
}
#sidebar {
    background-color: #252526;
    border: 1px solid #444;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    border-radius: 8px;
    overflow-y: auto;
}
.panel {
    background-color: #333;
    border: 1px solid #555;
    padding: 0.75rem;
    border-radius: 6px;
    margin-bottom: 1rem;
}
.panel h3 {
    font-weight: bold; color: #f0a048;
    border-bottom: 1px solid #555;
    padding-bottom: 0.5rem; margin-bottom: 0.5rem;
}
#base-actions-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
    gap: 0.5rem;
    margin: 0.75rem 0;
}
.item-list li {
    display: flex; justify-content: space-between;
    padding: 0.5rem;
    cursor: pointer; border-radius: 4px;
}
.item-list li:hover { background-color: #444; }

#sidebar-tabs { display: flex; }
.tab-button {
    flex-grow: 1; padding: 0.75rem 0.5rem;
    background-color: #333;
    border: 1px solid #555; border-bottom: none; cursor: pointer;
    color: #d4d4d4; border-radius: 4px 4px 0 0; font-size: 0.9rem;
    transition: background-color 0.2s, color 0.2s;
    text-align: center;
}
.tab-button.active { background-color: #252526; color: #f0a048; border-bottom: 1px solid #252526; }
.tab-panel {
    display: none; background-color: #252526; padding: 0.75rem;
    border: 1px solid #555; border-top: none; margin-bottom: 1rem;
    border-radius: 0 0 6px 6px;
    max-height: 250px; overflow-y: auto;
}
.tab-panel.active { display: block; }
#tab-inventory .item-list li { font-size: 0.8rem; }
#tab-electricity p { font-size: 0.8rem; padding-bottom: 0.25rem; }
#tab-electricity .network-divider { border-top: 1px solid #555; margin: 0.5rem 0; }

.tech-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
.tech-button {
    border: 1px solid #555; padding: 0.75rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    text-align: center; cursor: pointer;
}
.tech-button.available { background-color: #4a6b4c; }
.tech-button.available:hover { background-color: #5d8760; }
.tech-button.researching { background-color: #a08c4a; color: #1e1e1e; font-weight: bold; }
.tech-button.completed { background-color: #333; color: #888; cursor: not-allowed; }
.tech-button.locked { background-color: #444; color: #777; cursor: not-allowed; border-style: dashed; }
.progress-bar-container { width: 100%; background-color: #444; border-radius: 4px; overflow: hidden; border: 1px solid #555; }
.progress-bar { height: 1rem; background-color: #f0a048; transition: width 0.5s ease-in-out; }

#tab-recipes h4 {
    color: #f0a048;
    font-weight: bold;
    margin-top: 0.75rem;
    margin-bottom: 0.25rem;
    border-bottom: 1px solid #555;
    padding-bottom: 0.25rem;
}

#tab-recipes .recipe-item {
    background-color: #333;
    border: 1px solid #444;
    border-radius: 4px;
    padding: 0.5rem;
    margin-bottom: 0.5rem;
    font-size: 0.8rem;
}

#tab-recipes .recipe-item strong {
    color: #d4d4d4;
    display: block;
    margin-bottom: 0.25rem;
}

#tab-recipes .recipe-details {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.1rem 0.5rem;
    font-size: 0.75rem;
    color: #aaa;
}
#tab-recipes .recipe-details span:first-child {
    color: #888;
}

.action-button {
    background-color: #f0a048; color: #1e1e1e;
    border: none; padding: 0.75rem;
    border-radius: 4px;
    font-weight: bold; cursor: pointer; width: 100%;
    text-align: center; font-size: 0.9rem;
    transition: background-color 0.2s;
}
.action-button:hover { background-color: #ffd37e; }
.action-button:disabled { background-color: #444; color: #888; cursor: not-allowed; }
.action-button.secondary { background-color: #5a5a5a; color: #d4d4d4; }
.action-button.secondary:hover:not(:disabled) { background-color: #6a6a6a; }
.action-button.secondary.active { background-color: #f0a048; color: #1e1e1e; }

#actions-panel { margin-top: auto; padding: 0; background-color: transparent; border: none; }
#actions-panel .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }
.subtab-button {
    background-color: #4a4a4a;
    color: #ccc;
    border: 1px solid #666;
    padding: 0.5rem;
    font-size: 0.8rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}
.subtab-button:hover { background-color: #5a5a5a; }
.subtab-button.active { background-color: #6a8a6c; color: #fff; border-color: #82a884; }

#machine-modal {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(0,0,0,0.8); z-index: 20; display: flex;
    justify-content: center; align-items: center;
}
#modal-content {
    background-color: #252526; border: 1px solid #444; border-radius: 8px;
    width: 80%; max-width: 900px; display: grid;
    grid-template-areas: "header header" "player machine";
    grid-template-columns: 1fr 1fr; gap: 1rem; padding: 1.5rem; position: relative;
    box-sizing: border-box;
}
#modal-close-btn { grid-area: header; width: auto; justify-self: end; background-color: #8b4513; color: white; }
.inventory-panel { background-color: #333; padding: 1rem; border-radius: 6px; }
.inventory-panel ul { max-height: 400px; overflow-y: auto; }
#modal-player-inv { grid-area: player; }
#modal-machine-inv { grid-area: machine; }
.transfer-button {
    background-color: #5a5a5a;
    color: #d4d4d4;
    border: 1px solid #666;
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
    margin-left: 0.25rem;
}

.transfer-button:hover {
    background-color: #6a6a6a;
}
.hidden { display: none !important; }

#recipe-tooltip {
    display: none;
    position: absolute;
    background-color: #1e1e1e;
    border: 1px solid #555;
    border-radius: 6px;
    padding: 0.75rem;
    z-index: 30;
    font-size: 0.8rem;
    width: 200px;
    pointer-events: none;
}

#mobile-controls {
    display: none;
    position: fixed;
    bottom: 30px;
    right: 10px; 
    z-index: 100;
    width: 150px;
    height: 150px; 
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-template-areas:
        ". up ."
        "left . right"
        ". down .";
    opacity: 0.7;
}
.d-pad-btn {
    background-color: rgba(51, 51, 51, 0.8);
    color: #fff;
    border: 1px solid #555;
    font-size: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-user-select: none;
}
.d-pad-btn:active {
    background-color: #f0a048;
}
#d-pad-up { grid-area: up; border-radius: 8px 8px 0 0; }
#d-pad-left { grid-area: left; border-radius: 8px 0 0 8px; }
#d-pad-right { grid-area: right; border-radius: 0 8px 8px 0; }
#d-pad-down { grid-area: down; border-radius: 0 0 8px 8px; }

@media (max-width: 800px) {
    /* --- DEFINITIVE MOBILE LAYOUT FIX --- */
    
    html, body {
        height: 100%;
        overflow: hidden;
        position: fixed;
        width: 100%;
    }

    #top-inventory-bar {
        overflow-x: auto;
        white-space: nowrap;
        justify-content: flex-start;
        -ms-overflow-style: none;
        scrollbar-width: none;
    }
    #top-inventory-bar::-webkit-scrollbar {
        display: none;
    }

    #mobile-controls {
        display: grid;
    }
    
    #in-game-menu-button {
        display: none;
    }

    #game-container {
        display: flex;
        flex-direction: column;
        grid-template-columns: none;
        height: calc(100vh - 50px);
        padding: 0.5rem;
        gap: 0.5rem;
    }

    #game-map-container {
        height: 50%;
        width: 100%;
    }

    /* This is the key fix for the sidebar scrolling */
    #sidebar {
        height: 50%;
        width: 100%;
        margin-top: 0;
        overflow-y: auto; /* Makes the entire sidebar scrollable if content overflows */
        padding: 0.75rem; /* Use padding for consistent spacing */
        border: 1px solid #444; /* Re-add border for consistency */
        background-color: #252526; /* Re-add background for consistency */
    }

    /* This prevents the last item's margin from breaking the scroll container */
    #sidebar .panel:last-child {
        margin-bottom: 0;
    }
    
    /* Revised Modal Styles */
    #modal-content {
        width: 95vw;
        height: 90vh;
        display: flex;
        flex-direction: column;
        grid-template-areas: none;
        grid-template-columns: none;
        padding: 0.75rem;
        gap: 0.75rem;
        overflow: hidden;
    }

    #modal-close-btn {
        flex-shrink: 0;
        width: auto;
        align-self: flex-end;
    }

    .inventory-panel {
        background-color: #333;
        padding: 0.75rem;
        border-radius: 6px;
        flex-grow: 1;
        overflow-y: auto;
        min-height: 0;
    }

    .inventory-panel ul {
        max-height: none;
    }
}

.menu-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.85);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 200;
}

.menu-panel {
    background-color: #1a202c;
    border: 1px solid #4a5568;
    border-radius: 8px;
    padding: 2rem;
    width: 90%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.menu-title {
    font-size: 2rem;
    font-weight: bold;
    color: #9f7aea;
    text-align: center;
    margin-bottom: 1rem;
}

.menu-button {
    background-color: #805ad5;
    color: #edf2f7;
    border: none;
    padding: 0.75rem;
    border-radius: 4px;
    font-weight: bold;
    cursor: pointer;
    text-align: center;
    font-size: 1rem;
    transition: background-color 0.2s;
}

.menu-button:hover {
    background-color: #9f7aea;
}

.menu-button.secondary {
    background-color: #4a5568;
}

.menu-button.secondary:hover {
    background-color: #718096;
}

.setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    color: #e2e8f0;
}

.settings-toggle {
    width: 20px;
    height: 20px;
}

.save-list-container {
    max-height: 50vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.save-entry {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.save-entry .menu-button.load {
    flex-grow: 1;
    text-align: left;
    padding-left: 1rem;
}

.save-entry .menu-button.delete {
    flex-grow: 0;
    flex-shrink: 0;
    width: 44px;
    background-color: #c53030;
}

.save-entry .menu-button.delete:hover {
    background-color: #e53e3e;
}

#profile-list .menu-button {
    width: 100%;
    text-align: left;
    padding-left: 1rem;
    background-color: #2d3748;
}

#profile-list .menu-button:hover {
    background-color: #4a5568;
}


#intro-text {
    font-size: 1.1rem;
    line-height: 1.6;
    min-height: 150px;
    white-space: pre-wrap;
}

#intro-text.typing::after {
    content: '▋';
    animation: blink 1s step-end infinite;
    color: #9f7aea;
    margin-left: 0.25rem;
}

@keyframes blink {
    50% {
        opacity: 0;
    }
}

#in-game-menu-button {
    position: fixed;
    top: 60px;
    left: 10px;
    z-index: 150;
    background-color: rgba(45, 55, 72, 0.8);
    border: 1px solid #4a5568;
    color: #e2e8f0;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s;
}

#in-game-menu-button:hover {
    background-color: #4a5568;
}