:root {
    --primary: #8b5cf6;
    --primary-hover: #a78bfa;
    --secondary: #3b82f6;
    --accent: #f59e0b;
    --success: #10b981;
    --danger: #ef4444;
    --background: #0f172a;
    --surface: rgba(30, 41, 59, 0.7);
    --surface-border: rgba(255, 255, 255, 0.1);
    --glass: rgba(15, 23, 42, 0.8);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
}

body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--background);
    font-family: 'Outfit', sans-serif;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: 100%;
    transition: filter 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

#gameCanvas.blur {
    filter: blur(5px);
}

/* UI Container */
#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 10px;
    box-sizing: border-box;
    z-index: 100;
}

/* Modern Glassmorphism Toolbar */
#toolbar-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: auto;
}

#toolbar {
    background: var(--surface);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    padding: 10px 16px;
    display: flex;
    gap: 12px;
    justify-content: center;
    align-items: center;
    border: 1px solid var(--surface-border);
    border-radius: 20px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
    flex-wrap: wrap;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    max-height: 500px;
    overflow: hidden;
}

#toolbar.collapsed {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin: 0;
    border-color: transparent;
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
    /* Prevent interaction while collapsed */
}

#btn-collapse-toolbar {
    background: rgba(139, 92, 246, 0.8);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    padding: 0;
    justify-content: center;
    font-size: 20px;
    border: 2px solid rgba(255, 255, 255, 0.2);
}

.mobile-only {
    display: none;
}

/* Collapsible Building Menu */
#building-menu {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    background: var(--glass);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 16px;
    border-radius: 24px;
    border: 1px solid var(--surface-border);
    box-shadow: 0 16px 50px rgba(0, 0, 0, 0.6);
    transition: all 0.5s cubic-bezier(0.19, 1, 0.22, 1);
    max-height: 600px;
    opacity: 1;
    transform: translateY(0);
    overflow-y: auto;
    width: calc(100% - 40px);
    max-width: 800px;
    pointer-events: auto;
}

#building-menu.collapsed {
    max-height: 0;
    opacity: 0;
    padding-top: 0;
    padding-bottom: 0;
    border-color: transparent;
    transform: translateY(10px);
    pointer-events: none;
    visibility: hidden;
    /* Prevent interaction while collapsed */
}

#building-menu h3 {
    margin: 4px 0;
    font-size: 15px;
    width: 100%;
    text-align: center;
    color: #94a3b8;
}

/* Divider for logical grouping */
.divider {
    width: 2px;
    height: 30px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
}

.b-info {
    display: none;
    font-size: 11px;
    font-weight: normal;
    background: rgba(0, 0, 0, 0.4);
    padding: 3px 6px;
    border-radius: 4px;
    margin-top: 4px;
    color: #cbd5e1;
    pointer-events: none;
    text-align: left;
    width: 100%;
}

#building-menu.show-info .b-info {
    display: block;
}

#building-menu.show-info button {
    flex-direction: column;
    align-items: flex-start;
    padding: 12px;
}

button {
    background: rgba(45, 55, 72, 0.8);
    color: var(--text-main);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 12px;
    padding: 10px 20px;
    font-size: 15px;
    font-family: 'Outfit', sans-serif;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    pointer-events: auto;
}

button:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

button:active {
    transform: translateY(1px) scale(0.97);
}

/* Active State Tuning */
button.active {
    background: linear-gradient(135deg, var(--success) 0%, #059669 100%);
    border-color: #34d399;
    box-shadow: 0 4px 20px rgba(16, 185, 129, 0.5);
    color: white;
    transform: scale(1.05);
}

/* Modern Info Panel */
#info-panel {
    pointer-events: auto;
    background: var(--surface);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    color: var(--text-main);
    padding: 12px 24px;
    font-size: 19px;
    font-weight: 800;
    border-radius: 16px;
    border: 1px solid var(--surface-border);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    gap: 12px;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 1000;
}

#info-panel.pulse {
    transform: scale(1.1);
    border-color: #34d399;
}

#money {
    color: #4ade80;
    text-shadow: 0 0 10px rgba(74, 222, 128, 0.4);
    font-size: 20px;
    font-weight: 900;
}

#lp-points {
    color: #f0abfc;
    text-shadow: 0 0 10px rgba(240, 171, 252, 0.4);
    font-size: 16px;
}

#toast-container {
    position: fixed;
    top: 24px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    pointer-events: none;
}

.toast {
    background: rgba(15, 23, 42, 0.9);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.15);
    color: white;
    padding: 12px 24px;
    border-radius: 14px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.5), 0 8px 10px -6px rgba(0, 0, 0, 0.5);
    font-weight: 600;
    font-size: 14px;
    animation: toastScaleIn 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards,
               toastFadeOut 0.5s ease 2.5s forwards;
    display: flex;
    align-items: center;
    gap: 10px;
    letter-spacing: 0.3px;
    min-width: 200px;
}

@keyframes toastScaleIn {
    from { transform: translateY(-20px) scale(0.8); opacity: 0; }
    to { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes toastFadeOut {
    to { transform: translateY(-10px) scale(0.95); opacity: 0; }
}

/* State of the Art Panels */
.side-panel {
    background: rgba(15, 23, 42, 0.85) !important;
    backdrop-filter: blur(20px) !important;
    border: 1px solid rgba(255, 255, 255, 0.1) !important;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6) !important;
    border-radius: 20px !important;
    color: white !important;
}

#stats-panel {
    background: linear-gradient(180deg, rgba(30, 41, 59, 0.9), rgba(15, 23, 42, 0.95));
}

.money-change {
    position: absolute;
    font-weight: bold;
    color: #ef4444;
    pointer-events: none;
    animation: floatUpFade 1s forwards;
    z-index: 1001;
}

.money-change.plus {
    color: #4ade80;
}

@keyframes floatUpFade {
    0% {
        transform: translateY(0);
        opacity: 1;
    }

    100% {
        transform: translateY(-40px);
        opacity: 0;
    }
}

/* Floating animation for UI elements */
@keyframes float {
    0% {
        transform: translateY(0px);
    }

    50% {
        transform: translateY(-5px);
    }

    100% {
        transform: translateY(0px);
    }
}

/* Main Menu Overlay */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.95);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
}

.overlay.hidden {
    display: none !important;
}

.menu-box {
    background: #1e293b;
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5);
    text-align: center;
    max-width: 800px;
    width: 90%;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
}

.menu-box h1 {
    margin-top: 0;
    color: #38bdf8;
    font-size: 32px;
}

.menu-box h3 {
    margin-bottom: 20px;
    color: #94a3b8;
}

.tabs {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 20px;
}

.profile-tab {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    font-size: 18px;
    padding: 10px 20px;
}

.profile-tab.active {
    background: #3b82f6;
    border-color: #60a5fa;
    color: white;
}

.map-grid {
    display: flex;
    gap: 20px;
    justify-content: center;
    flex-wrap: wrap;
}

.map-card {
    background: #334155;
    padding: 20px;
    border-radius: 12px;
    width: 200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 2px solid transparent;
    transition: all 0.2s;
}

.map-card:hover {
    transform: translateY(-5px);
    border-color: #38bdf8;
}

.map-card h4 {
    margin: 0 0 10px 0;
    font-size: 22px;
    color: #f8fafc;
}

.map-card p {
    margin: 0 0 15px 0;
    font-size: 14px;
    color: #cbd5e1;
}

.map-card .cost {
    font-weight: bold;
    color: #f59e0b;
}

.buy-btn {
    background: #f59e0b;
    color: #1e293b;
    border-color: #f59e0b;
    width: 100%;
    justify-content: center;
}

.buy-btn:hover {
    background: #d97706;
}

.play-btn {
    background: #10b981;
    border-color: #10b981;
    width: 100%;
    justify-content: center;
}

.play-btn:hover {
    background: #059669;
}

.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
    z-index: 2000;
    max-width: 400px;
    width: 90%;
}

.auth-modal-content {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    color: #1e293b;
}

.auth-modal-content input {
    padding: 0.8rem;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    font-size: 1rem;
}

.auth-actions {
    display: flex;
    gap: 1rem;
}

.auth-note {
    font-size: 0.85rem;
    color: #64748b;
    text-align: center;
}

.danger-btn {
    background: #ef4444;
    color: white;
    border: none;
    padding: 0.8rem;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}

.danger-btn:hover {
    background: #dc2626;
}

.auth-status {
    margin-top: 1.5rem;
    font-size: 0.9rem;
    color: #94a3b8;
}

#auth-status button {
    background: none;
    border: none;
    color: #3b82f6;
    text-decoration: underline;
    cursor: pointer;
    padding: 0;
    margin-left: 5px;
}

/* Hide specific buttons */
.hidden {
    display: none !important;
}

/* Spinner for loading screen */
.spinner {
    width: 50px;
    height: 50px;
    border: 5px solid rgba(255, 255, 255, 0.1);
    border-top: 5px solid #3b82f6;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

@media (max-width: 600px) {
    .mobile-only {
        display: flex;
    }

    #toolbar {
        flex-direction: column;
        width: 100%;
        padding: 12px;
        gap: 6px;
    }

    #secondary-toolbar {
        width: 100%;
        justify-content: space-between;
        gap: 6px;
    }

    #secondary-toolbar button {
        flex: 1;
        padding: 12px 6px;
        font-size: 11px;
    }

    #building-menu {
        max-height: 40vh !important;
        padding: 8px;
    }

    #building-menu button {
        padding: 10px 8px;
        font-size: 15px;
        /* Increased from 11px for readability */
    }

    #info-panel {
        font-size: 14px;
        padding: 4px 8px;
        top: 10px;
        right: 10px;
        gap: 4px;
        max-width: 65%;
        position: relative;
        /* Change from absolute to relative to stay in flow */
    }

    #top-bar {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
        padding: 5px 10px;
        pointer-events: none;
    }

    #top-right-group {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 5px;
    }

    #building-menu {
        max-height: 50vh !important;
        padding: 10px;
        position: absolute;
        top: 70px;
        /* Move below top bar */
        left: 5%;
        width: 90%;
        z-index: 2000;
    }

    #toolbar-container {
        margin-bottom: 15px;
        /* AppsGeyser ad is usually at bottom, let's keep it centered but higher */
        position: fixed;
        bottom: 80px;
        left: 0;
        width: 100%;
        display: flex;
        justify-content: center;
        pointer-events: none;
    }

    #toolbar {
        pointer-events: auto;
        width: 90%;
        max-width: 350px;
        flex-direction: row;
        /* Horizontal is better even on mobile if it wraps */
        justify-content: center;
        padding: 10px;
    }

    #camera-controls {
        bottom: 160px;
        /* Above toolbar */
        right: 15px;
        flex-direction: column;
        /* Vertical stack is better for camera on mobile edges */
    }

    .map-grid {
        flex-direction: column;
        align-items: center;
    }

    .map-card {
        width: 100%;
        max-width: 280px;
        box-sizing: border-box;
    }

    .menu-box {
        padding: 20px;
        width: 95%;
    }

    .profile-tab {
        padding: 8px 12px;
        font-size: 14px;
    }
}

/* Tutorial styles */
#tutorial-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    z-index: 10000;
    pointer-events: none;
}

#tutorial-tooltip {
    position: absolute;
    background: #1e293b;
    border: 2px solid #a855f7;
    border-radius: 12px;
    padding: 15px;
    color: white;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.5);
    max-width: 250px;
    pointer-events: auto;
    transition: all 0.3s ease;
}

#tutorial-tooltip p {
    margin: 0 0 10px 0;
    font-size: 15px;
}

#btn-next-tutorial {
    background: #a855f7;
    width: 100%;
    justify-content: center;
}

/* Floating Camera Controls */
#camera-controls {
    position: absolute;
    bottom: 20px;
    right: 20px;
    display: flex;
    gap: 8px;
    pointer-events: auto;
    background: rgba(30, 41, 59, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 8px 12px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    z-index: 50;
}

#custom-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 6000;
}

#custom-modal {
    background: #1e293b;
    color: white;
    border: 2px solid #3b82f6;
    border-radius: 16px;
    padding: 25px;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    gap: 20px;
    animation: modalPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes modalPop {
    from {
        transform: scale(0.8);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

#custom-modal h2 {
    margin: 0;
    color: #60a5fa;
    font-size: 24px;
}

#custom-modal p {
    margin: 0;
    font-size: 16px;
    line-height: 1.5;
    opacity: 0.9;
}

.modal-buttons {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.modal-btn {
    padding: 10px 20px;
    border-radius: 8px;
    border: none;
    font-weight: bold;
    cursor: pointer;
    transition: 0.2s;
}

.modal-btn-confirm {
    background: #3b82f6;
    color: white;
}

.modal-btn-confirm:hover {
    background: #2563eb;
}

.modal-btn-cancel {
    background: #475569;
    color: #cbd5e1;
}

.modal-btn-cancel:hover {
    background: #334155;
}

/* Building Info Panel */
#building-info-panel {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(15, 23, 42, 0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 2px solid #3b82f6;
    border-radius: 16px;
    padding: 24px;
    color: white;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    display: none;
    flex-direction: column;
    gap: 12px;
    width: 90%;
    max-width: 320px;
    max-height: 80vh;
    overflow-y: auto;
    z-index: 1000;
    animation: modalPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    pointer-events: auto;
}

#bip-title {
    margin: 0;
    font-size: 24px;
    color: #60a5fa;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 8px;
}

#bip-level-container {
    font-size: 14px;
    color: #94a3b8;
    margin-bottom: 4px;
}

#bip-desc {
    margin: 0;
    font-size: 15px;
    line-height: 1.4;
    opacity: 0.9;
}

#bip-upgrade-btn {
    background-color: #a855f7;
    border: none;
    padding: 12px 15px;
    border-radius: 10px;
    color: white;
    font-weight: bold;
    cursor: pointer;
    transition: 0.3s;
    font-size: 16px;
    box-shadow: 0 4px 12px rgba(168, 85, 247, 0.3);
}

#bip-upgrade-btn:hover {
    background-color: #9333ea;
    transform: translateY(-2px);
}

#bip-upgrade-btn:disabled {
    background-color: #475569;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

#bip-upgrade-btn.on-fire {
    background-color: #ef4444 !important;
}

#bip-upgrade-btn.max-level {
    background-color: #64748b !important;
}

/* New Top Bar Layout */
#top-bar {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 10px;
    box-sizing: border-box;
    pointer-events: none;
    z-index: 100;
}

#top-right-group {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    pointer-events: none;
}

#top-right-group>* {
    pointer-events: auto;
}

.round-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(8px);
    color: white;
    font-size: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: 0.3s;
    pointer-events: auto;
}

.round-btn:hover {
    transform: scale(1.1);
    background: #3b82f6;
}

.glow-btn {
    box-shadow: 0 0 15px rgba(139, 92, 246, 0.5);
    border: 1px solid rgba(139, 92, 246, 0.3);
}

.glass-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

#stats-panel,
#quests-panel,
#building-info-panel,
#toolbar-container,
#building-menu,
#info-panel,
#camera-controls,
.round-btn,
#top-bar button {
    pointer-events: auto;
}

.bip-close {
    position: absolute;
    top: 15px;
    right: 15px;
    cursor: pointer;
    font-size: 24px;
    color: #94a3b8;
    z-index: 100;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    transition: all 0.2s;
    pointer-events: auto;
}

.bip-close:hover {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    transform: scale(1.1);
}

/* Visual Feedback for Inactive Buildings */
.inactive-building {
    filter: opacity(0.6) grayscale(0.5) contrast(0.8);
    pointer-events: none;
}

/* Tooltip/Label warning */
.no-workers-warning {
    color: #ef4444;
    font-weight: bold;
    font-size: 11px;
    background: rgba(0, 0, 0, 0.5);
    padding: 2px 4px;
    border-radius: 4px;
}

/* MOBILE PORTRAIT OPTIMIZATIONS */
@media screen and (orientation: portrait) and (max-width: 600px) {
    #top-bar {
        flex-direction: column !important;
        align-items: center !important;
        gap: 10px !important;
        padding: 10px 5px !important;
    }

    #top-right-group {
        width: 100% !important;
        justify-content: center !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    #info-panel {
        font-size: 13px !important;
        padding: 8px 12px !important;
        width: 90% !important;
        justify-content: center !important;
        box-sizing: border-box !important;
    }

    #building-menu {
        top: 130px !important; /* Move down further to avoid overlapping with stacked top bar */
        max-height: 45vh !important;
        width: 95% !important;
        left: 2.5% !important;
    }

    #camera-controls {
        bottom: 120px !important;
        right: 10px !important;
    }
    
    /* Ensure the main toolbar doesn't hide behind the bottom of the screen */
    #toolbar-container {
        bottom: 20px !important;
    }
    
    #toolbar button {
        width: 50px !important;
        height: 50px !important;
        font-size: 20px !important;
    }
}