:root {
    --bg-dark: #0f172a;
    --bg-darker: #020617;
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.08);
    --accent: #6366f1;
    /* Indigo 500 */
    --accent-glow: rgba(99, 102, 241, 0.4);
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --grid-line: #334155;
    --font-family: 'Outfit', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-darker);
    background-image:
        radial-gradient(at 0% 0%, rgba(99, 102, 241, 0.15) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(168, 85, 247, 0.15) 0px, transparent 50%);
    color: var(--text-main);
    font-family: var(--font-family);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Glassmorphism Configuration */
.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}

/* Header */
header {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    z-index: 10;
    border-bottom: 1px solid var(--glass-border);
}

.header-left h1 {
    font-weight: 600;
    font-size: 1.25rem;
    letter-spacing: 0.5px;
}

.header-left h1 span {
    color: var(--accent);
    font-size: 0.8em;
    background: rgba(99, 102, 241, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    margin-left: 4px;
}

#project-select {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--glass-border);
    color: var(--accent);
    font-family: var(--font-family);
    font-size: 0.9rem;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 8px;
    margin-left: 20px;
    outline: none;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    transition: all 0.2s;
}

#project-select:hover {
    border-color: var(--accent);
    background: rgba(0, 0, 0, 0.5);
}

#project-select option {
    background: var(--bg-dark);
    color: var(--text-main);
}

.coords-display {
    font-family: monospace;
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Main Layout */
main {
    flex: 1;
    display: flex;
    position: relative;
    overflow: hidden;
}

.canvas-wrapper {
    flex: 1;
    position: relative;
    background-color: var(--bg-darker);
    /* Subtle grid pattern background for the void */
    background-image: linear-gradient(var(--grid-line) 1px, transparent 1px),
        linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
    background-size: 100px 100px;
    background-position: center;
    cursor: grab;
}

.canvas-wrapper:active {
    cursor: grabbing;
}

canvas {
    display: block;
}

/* HUD / Controls */
.canvas-controls {
    position: absolute;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 20px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 16px;
    z-index: 20;
}

button {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--glass-border);
    color: var(--text-main);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

button:hover {
    background: var(--accent);
    box-shadow: 0 0 12px var(--accent-glow);
    border-color: var(--accent);
}

.divider {
    width: 1px;
    height: 24px;
    background: var(--glass-border);
}

.layer-control {
    display: flex;
    align-items: center;
    gap: 12px;
}

.layer-control label {
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    font-size: 0.8rem;
    letter-spacing: 1px;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 200px;
    height: 6px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    background: var(--text-main);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
    transition: transform 0.1s;
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
    background: var(--accent);
}

input[type="number"] {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--glass-border);
    color: var(--text-main);
    padding: 6px;
    border-radius: 6px;
    width: 60px;
    text-align: center;
    font-family: monospace;
}

/* Sidebar */
.sidebar {
    width: 320px;
    border-left: 1px solid var(--glass-border);
    display: flex;
    flex-direction: column;
    z-index: 15;
}

.sidebar h2,
.sidebar h3 {
    padding: 20px;
    font-size: 1rem;
    font-weight: 600;
    border-bottom: 1px solid var(--glass-border);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.material-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
}

.material-item {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    margin-bottom: 4px;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.02);
    transition: all 0.2s;
    cursor: pointer;
    /* Interaction hint */
}

.material-item:hover {
    background: rgba(255, 255, 255, 0.05);
}

.material-item.disabled {
    opacity: 0.4;
    filter: grayscale(100%);
}

.color-swatch {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    margin-right: 12px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.material-name {
    flex: 1;
    font-size: 0.9rem;
}

.material-count {
    color: var(--text-muted);
    font-family: monospace;
    font-size: 0.9rem;
}

.info-panel {
    padding: 20px;
    border-top: 1px solid var(--glass-border);
    background: rgba(0, 0, 0, 0.1);
}

.stat-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 0.9rem;
}

.stat-row span:last-child {
    font-family: monospace;
    color: var(--accent);
}

.footer-info {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
}

.license-link {
    margin-top: 4px;
    opacity: 0.6;
}

/* Loading Overlay */
#loading-overlay {
    position: absolute;
    top: 60px;
    /* Below header */
    left: 0;
    width: 100%;
    height: calc(100% - 60px);
    background: rgba(2, 6, 23, 0.9);
    backdrop-filter: blur(4px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.hidden {
    display: none !important;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(99, 102, 241, 0.3);
    border-radius: 50%;
    border-top-color: var(--accent);
    animation: spin 1s ease-in-out infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

/* Legend */
#legend-toggle {
    position: absolute;
    top: 80px;
    right: 340px;
    /* Sidebar width + 20 */
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 50;
    transition: all 0.2s;
}

#legend-toggle:hover {
    background: var(--accent);
    color: white;
    transform: scale(1.1);
}

.icon {
    font-weight: bold;
    font-size: 1.2rem;
}

#legend-panel {
    position: absolute;
    top: 80px;
    right: 380px;
    width: 280px;
    border-radius: 12px;
    overflow: hidden;
    z-index: 51;
    animation: fadeIn 0.2s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.legend-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid var(--glass-border);
}

.legend-header h3 {
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--accent);
    margin: 0;
}

#close-legend {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    width: 24px;
    height: 24px;
    padding: 0;
    line-height: 1;
}

.legend-content {
    padding: 16px;
}

.legend-item {
    display: flex;
    align-items: center;
    /* Changed from start to center for better vert align */
    gap: 12px;
    margin-bottom: 16px;
}

.legend-item:last-child {
    margin-bottom: 0;
}

.symbol {
    width: 60px;
    height: 40px;
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--glass-border);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.desc strong {
    display: block;
    font-size: 0.9rem;
    margin-bottom: 2px;
    color: var(--text-main);
}

.desc p {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* Specific Symbols */
.grid-symbol {
    background-image:
        linear-gradient(90deg, transparent 45%, #fff 45%, #fff 55%, transparent 55%),
        linear-gradient(transparent 45%, white 45%, white 55%, transparent 55%);
    background-size: 20px 20px;
    opacity: 0.5;
}

.dimension-symbol {
    color: #ffd700;
    /* Gold */
    font-family: monospace;
    font-size: 0.8rem;
    font-weight: bold;
}

.arrow {
    font-size: 1rem;
    margin: 0 4px;
}

.layer-symbol {
    color: var(--accent);
    font-weight: bold;
    font-size: 0.85rem;
    font-family: monospace;
}

.controls-symbol {
    font-size: 1.2rem;
}

/* Login Overlay */
.login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(2, 6, 23, 0.95);
    backdrop-filter: blur(10px);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-box {
    width: 100%;
    max-width: 400px;
    padding: 40px;
    border-radius: 16px;
    text-align: center;
    background: rgba(30, 41, 59, 0.4);
}

.login-box h2 {
    color: var(--accent);
    margin-bottom: 10px;
    font-size: 1.5rem;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.login-box p {
    color: var(--text-muted);
    margin-bottom: 30px;
}

.input-group {
    display: flex;
    gap: 0;
    margin-bottom: 20px;
}

.login-box input {
    flex: 1;
    min-width: 0;
    /* Critical for flex shrinking */
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 12px 16px;
    border-radius: 8px 0 0 8px;
    /* Left corners only */
    border-right: none;
    /* Merge with button */
    font-size: 1rem;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}

.login-box input:focus {
    border-color: var(--accent);
}

.login-box button {
    width: auto;
    height: auto;
    /* Override global fixed height */
    padding: 12px 24px;
    /* Match input padding vertically */
    background: var(--accent);
    border: none;
    border-radius: 0 8px 8px 0;
    /* Right corners only */
    font-weight: 600;
    white-space: nowrap;
    /* Prevent button text wrapping */
}

@media (max-width: 480px) {
    .login-box {
        padding: 24px;
        width: 90%;
    }

    .input-group {
        flex-direction: row;
        /* Keep row even on mobile */
    }

    .login-box button {
        padding: 12px 16px;
        /* Slightly smaller padding on mobile */
    }
}

.login-box button:hover {
    background: #4f46e5;
    box-shadow: 0 0 15px rgba(99, 102, 241, 0.5);
}

.error-msg {
    color: #ef4444 !important;
    font-size: 0.9rem;
    margin-bottom: 0 !important;
    min-height: 1.5em;
    animation: shake 0.4s ease-in-out;
}

@keyframes shake {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-5px);
    }

    75% {
        transform: translateX(5px);
    }
}