:root {
    /* Theme Variables */
    --primary-color: #007bff;
    --primary-hover: #0056b3;
    --success-color: #28a745;
    --danger-color: #dc3545;
    --accent-info: #00aaff; /* Consolidated from marquee and spinner */
    --bg-light: #f8f9fa;
    --panel-bg: rgba(255, 255, 255, 0.98);
    --overlay-bg: rgba(255, 255, 255, 0.85);
    
    /* Grays & Borders */
    --border-color: #ccc;
    --border-light: #eee;   /* Repeated in fieldset, asset-btn, selector */
    --border-subtle: #ddd;  /* Repeated in project-manager h3, material img */
    --bg-hover: #f0f0f0;    /* Repeated in tool-btn, asset-btn, view-item, etc */
    --text-main: #333;
    --text-muted: #666;
    
    /* Unified Spacing & Typography */
    --panel-padding: 8px;
    --group-margin: 8px;
    --input-padding: 4px;
    --radius: 4px;
    --font-size: 13px;
    --font-size-sm: 12px;   /* Unified small text across labels/buttons */
    --screen-offset: 8px;

    /* Shadows */
    --shadow-sm: 0 2px 10px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.15);
    --shadow-lg: 0 10px 40px rgba(0,0,0,0.4);
}

body { margin: 0; overflow: hidden; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }

/* Generic Panel Styling - Unified Base */
#ui-panel, #project-manager {
    position: absolute; 
    top: var(--screen-offset);
    background: var(--panel-bg); 
    padding: var(--panel-padding);
    border-radius: var(--radius); 
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
    z-index: 100;
    user-select: none;
}

#ui-panel {
    left: var(--screen-offset);
    width: 240px; 
    max-height: 92vh; 
    overflow-y: auto;
}

#project-manager {
    right: var(--screen-offset);
    width: 200px;
    font-family: inherit;
}

fieldset { 
    border: 1px solid var(--border-light); 
    border-radius: var(--radius); 
    padding: var(--panel-padding); 
    margin-bottom: var(--group-margin); 
}

legend { font-weight: bold; color: var(--text-main); padding: 0 4px; font-size: var(--font-size-sm); text-transform: uppercase; letter-spacing: 0.5px; }

/* Unified Form Controls */
.form-group { 
    margin-top: 6px; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    font-size: var(--font-size);
}

input[type="number"], select { 
    height: 24px;
    padding: 2px 4px; 
    border: 1px solid var(--border-color); 
    border-radius: var(--radius); 
    background: white;
    font-size: var(--font-size-sm);
}

/* Buttons */
.tool-group { display: flex; gap: 4px; margin-bottom: var(--group-margin); flex-wrap: wrap; }

.tool-btn {
    flex: 1; padding: 6px 4px; font-size: var(--font-size-sm); cursor: pointer;
    background: var(--bg-hover); color: var(--text-main); border: 1px solid var(--border-color); 
    border-radius: var(--radius); font-weight: 500; transition: all 0.15s;
    display: flex; align-items: center; justify-content: center;
}

.tool-btn.active { background: var(--success-color); color: white; border-color: #1e7e34; }
.tool-btn:hover:not(.active) { background: #e2e2e2; }

/* Standard Icon Button */
.icon-btn {
    flex: 1; padding: 4px; background: white; border: 1px solid var(--border-color);
    border-radius: var(--radius); cursor: pointer; transition: all 0.1s;
    display: flex; align-items: center; justify-content: center;
}
.icon-btn svg { width: 16px; height: 16px; }
.icon-btn.active { background: #e9ecef; border-color: var(--primary-color); }

/* View Mode Specific Styling */
.view-mode-group {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-start;
}
.view-mode-group .icon-btn {
    flex: 0 0 auto;
    padding: 3px;
    width: 32px;
    height: 32px;
}
.view-mode-group .icon-btn svg {
    width: 22px;
    height: 22px;
}

.apply-btn {
    width: 100%; padding: 6px; margin-top: 8px;
    background: var(--primary-color); color: white; border: none;
    border-radius: var(--radius); cursor: pointer; font-weight: bold; font-size: var(--font-size-sm);
}
.apply-btn:hover { background: var(--primary-hover); }

/* Action Bar (Bottom) */
#action-bar {
    position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
    display: none; gap: 12px; z-index: 10; background: var(--panel-bg);
    padding: 8px 16px; border-radius: 25px; box-shadow: var(--shadow-md);
    border: 1px solid var(--border-color);
}
.action-btn {
    width: 36px; height: 36px; border-radius: 50%; border: none; cursor: pointer;
    display: flex; align-items: center; justify-content: center; transition: transform 0.1s;
}
#btn-accept { background: var(--success-color); }
#btn-cancel { background: var(--danger-color); }
.action-btn svg { width: 20px; height: 20px; fill: white; }

/* Notifications & Status */
#notification {
    position: absolute; top: var(--screen-offset); left: 50%; transform: translateX(-50%);
    background: var(--danger-color); color: white; padding: 8px 16px; border-radius: var(--radius);
    font-size: var(--font-size); font-weight: bold; display: none; z-index: 20; align-items: center; gap: 10px;
}
#status-text { color: var(--text-muted); font-size: 11px; margin-top: 6px; text-align: center; font-style: italic; line-height: 1.3; }

/* Asset Browser */
.asset-grid {
    display: grid; grid-template-columns: repeat(2, 1fr);
    gap: 6px; margin-top: 8px; max-height: 220px; overflow-y: auto;
}
.asset-btn {
    background: #fff; border: 1px solid var(--border-light); border-radius: var(--radius); padding: 4px;
    cursor: pointer; display: flex; flex-direction: column; align-items: center;
}
.asset-btn:hover { border-color: var(--primary-color); }
.asset-btn img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 2px; background: var(--bg-hover); }
.asset-btn span { font-size: 10px; margin-top: 4px; color: var(--text-main); }

/* Helpers */
.action-svg svg { width: 16px; height: 16px; fill: currentColor; }
#tool-select { flex: 0 0 32px; }
#tool-select svg { width: 14px; height: 14px; }

/* Project Manager Specifics */
#project-manager h3 {
    margin: 0 0 10px 0;
    font-size: 14px;
    border-bottom: 1px solid var(--border-subtle);
    padding-bottom: 5px;
}
.view-category { margin-bottom: 15px; }
.category-title {
    font-weight: bold; font-size: var(--font-size-sm); color: #555; margin-bottom: 5px;
    display: flex; align-items: center;
}
.category-title::before { content: '▼'; font-size: 8px; margin-right: 5px; }
.view-list { list-style: none; padding: 0; margin: 0; }
.view-item { padding: 4px 8px 4px 20px; font-size: var(--font-size); cursor: pointer; border-radius: var(--radius); }
.view-item:hover { background: var(--bg-hover); }
.view-item.active { background: var(--primary-color); color: white; }

/* Material Selection Panel */
#material-selector {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: 450px; max-width: 90vw; height: 500px; max-height: 80vh;
    background: #fff; border-radius: 12px; box-shadow: var(--shadow-lg);
    z-index: 9999; display: none; flex-direction: column; overflow: hidden;
    border: 1px solid var(--border-color); font-family: inherit;
}

.selector-header {
    padding: 15px 20px; background: var(--bg-light); border-bottom: 1px solid var(--border-light);
    display: flex; justify-content: space-between; align-items: center;
}
.selector-header h4 { margin: 0; color: var(--text-main); font-size: 1.1rem; }
.close-selector { background: none; border: none; font-size: 28px; cursor: pointer; color: #999; transition: color 0.2s; }
.close-selector:hover { color: var(--danger-color); }

.selector-content { padding: 20px; overflow-y: auto; flex-grow: 1; }
.cat-section { margin-bottom: 25px; }
.cat-name {
    font-weight: 600; margin-bottom: 12px; font-size: 14px; color: var(--primary-color);
    text-transform: uppercase; letter-spacing: 0.5px; border-bottom: 1px solid #f0f0f0; padding-bottom: 5px;
}

.material-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 15px; }
.material-item {
    cursor: pointer; text-align: center; border: 1px solid var(--border-light); padding: 8px;
    border-radius: 8px; transition: all 0.2s ease; background: #fafafa;
}
.material-item:hover { background: #fff; border-color: var(--primary-color); box-shadow: 0 4px 12px rgba(0,123,255,0.15); transform: translateY(-2px); }
.material-item img { width: 100%; height: 80px; object-fit: cover; border-radius: 6px; margin-bottom: 8px; background: var(--border-subtle); }
.material-item span { font-size: var(--font-size-sm); font-weight: 500; color: #444; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#material-selector-backdrop {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0,0,0,0.5); z-index: 9998; display: none;
}

/* Keeping existing styles */
#selection-marquee {
    border: 1px solid var(--accent-info);
    background: rgba(0, 170, 255, 0.2);
    position: fixed;
    pointer-events: none;
    display: none;
    z-index: 10000;
}
.marquee-intersect { border-style: dashed; background: rgba(40, 167, 69, 0.1); border-color: var(--success-color); }

/* New Loading Overlay Styles */
#loading-overlay {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: var(--overlay-bg);
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 30000;
    font-family: sans-serif;
    color: var(--text-main);
}
.spinner {
    width: 40px; height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid var(--accent-info);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 15px;
}
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* Level Management Styles */
.level-item-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 2px 0;
}
.level-height-input {
    width: 50px; font-size: 11px; padding: 2px; margin-left: 5px;
    border: 1px solid var(--border-color); border-radius: 3px;
}
#btn-add-level {
    width: 100%; margin-top: 8px; cursor: pointer;
    background: var(--bg-hover); border: 1px dashed #aaa;
    border-radius: var(--radius); padding: 4px; font-weight: bold; color: var(--text-muted);
}
#btn-add-level:hover { background: #e8e8e8; }

#level-dialog-backdrop {
    position: fixed; top:0; left:0; width: 100%; height:100%;
    background: rgba(0,0,0,0.3); display: none; z-index: 40000;
}
#level-dialog {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    background: white; padding: 20px; border-radius: 8px; box-shadow: var(--shadow-md);
    z-index: 40001; display: none; font-family: sans-serif; width: 250px;
}
#level-dialog h4 { margin-top: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 15px; }