:root {
    /* Base Variables (overridden by themes) */
    --bg-main: #ffffff;
    --bg-panel: rgba(255, 255, 255, 0.85);
    --border-color: rgba(0, 0, 0, 0.2);
    --text-main: #000000;
    --text-muted: #555555;
    --accent: #0055ff;
    --accent-hover: #0044cc;
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.15);
    --glass-blur: blur(12px);
    --btn-bg: rgba(255, 255, 255, 0.8);
    --btn-hover: rgba(240, 240, 240, 1);
}

/* Blueprint Theme - Light (Lightbox) */
[data-color-theme="blueprint"][data-brightness="light"] {
    --bg-main: #ffffff;
    --bg-panel: rgba(240, 248, 255, 0.85);
    --border-color: rgba(10, 61, 145, 0.2);
    --text-main: #0a3d91;
    --text-muted: #4a75b5;
    --accent: #0055ff;
    --accent-hover: #0044cc;
    --shadow: 0 8px 32px 0 rgba(10, 61, 145, 0.15);
    --btn-bg: rgba(255, 255, 255, 0.8);
    --btn-hover: rgba(240, 248, 255, 1);
}

/* Blueprint Theme - Dark */
[data-color-theme="blueprint"][data-brightness="dark"] {
    --bg-main: #0a3d91;
    --bg-panel: rgba(0, 40, 100, 0.7);
    --border-color: rgba(255, 255, 255, 0.3);
    --text-main: #ffffff;
    --text-muted: #b3d4ff;
    --accent: #4da6ff;
    --accent-hover: #1a8cff;
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.4);
    --btn-bg: rgba(255, 255, 255, 0.1);
    --btn-hover: rgba(255, 255, 255, 0.25);
}

/* Holodeck Theme - Light */
[data-color-theme="holodeck"][data-brightness="light"] {
    --bg-main: #e2e8f0;
    --bg-panel: rgba(226, 232, 240, 0.85);
    --border-color: rgba(214, 158, 46, 0.4);
    --text-main: #2d3748;
    --text-muted: #718096;
    --accent: #d69e2e;
    --accent-hover: #b7791f;
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.15);
    --btn-bg: rgba(255, 255, 255, 0.6);
    --btn-hover: rgba(255, 255, 255, 0.9);
}

/* Holodeck Theme - Dark */
[data-color-theme="holodeck"][data-brightness="dark"] {
    --bg-main: #1a202c;
    --bg-panel: rgba(26, 32, 44, 0.75);
    --border-color: rgba(214, 158, 46, 0.5);
    --text-main: #f7fafc;
    --text-muted: #a0aec0;
    --accent: #ecc94b;
    --accent-hover: #d69e2e;
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.5);
    --btn-bg: rgba(255, 255, 255, 0.1);
    --btn-hover: rgba(255, 255, 255, 0.2);
}

/* Amber CRT Theme - Light */
[data-color-theme="amber"][data-brightness="light"] {
    --bg-main: #fffcf0;
    --bg-panel: rgba(255, 248, 230, 0.85);
    --border-color: rgba(255, 176, 0, 0.4);
    --text-main: #664400;
    --text-muted: #997733;
    --accent: #ffb000;
    --accent-hover: #cc8800;
    --shadow: 0 8px 32px 0 rgba(150, 100, 0, 0.15);
    --btn-bg: rgba(255, 255, 255, 0.8);
    --btn-hover: rgba(255, 245, 215, 1);
}

/* Amber CRT Theme - Dark */
[data-color-theme="amber"][data-brightness="dark"] {
    --bg-main: #1a1100;
    --bg-panel: rgba(20, 15, 0, 0.8);
    --border-color: rgba(255, 176, 0, 0.5);
    --text-main: #ffcc66;
    --text-muted: #cc9933;
    --accent: #ffb000;
    --accent-hover: #e69900;
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.6);
    --btn-bg: rgba(255, 176, 0, 0.1);
    --btn-hover: rgba(255, 176, 0, 0.2);
}

/* Matrix Theme - Light */
[data-color-theme="matrix"][data-brightness="light"] {
    --bg-main: #f0fff0;
    --bg-panel: rgba(230, 255, 230, 0.85);
    --border-color: rgba(0, 255, 65, 0.4);
    --text-main: #004d00;
    --text-muted: #008000;
    --accent: #00cc00;
    --accent-hover: #009900;
    --shadow: 0 8px 32px 0 rgba(0, 100, 0, 0.15);
    --btn-bg: rgba(255, 255, 255, 0.8);
    --btn-hover: rgba(235, 255, 235, 1);
}

/* Matrix Theme - Dark */
[data-color-theme="matrix"][data-brightness="dark"] {
    --bg-main: #0d1a0d;
    --bg-panel: rgba(10, 20, 10, 0.85);
    --border-color: rgba(0, 255, 65, 0.5);
    --text-main: #b3ffb3;
    --text-muted: #4dff4d;
    --accent: #00ff41;
    --accent-hover: #00cc33;
    --shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.6);
    --btn-bg: rgba(0, 255, 65, 0.1);
    --btn-hover: rgba(0, 255, 65, 0.25);
}

body { 
    margin: 0; 
    overflow: hidden; 
    font-family: 'Inter', sans-serif;
    color: var(--text-main);
    transition: background-color 0.3s;
    user-select: none;
    -webkit-user-select: none;
    background-color: var(--bg-main);
}
input, textarea {
    user-select: text;
    -webkit-user-select: text;
}

/* Panel Base */
.glass-panel {
    background: var(--bg-panel);
    backdrop-filter: var(--glass-blur);
    -webkit-backdrop-filter: var(--glass-blur);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow);
    border-radius: 12px;
}

/* Toolbars */
.toolbar {
    position: absolute;
    z-index: 100;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
}

#top-left-toolbar { top: 16px; left: 16px; }
#top-toolbar { top: 16px; left: 50%; transform: translateX(-50%); }
#left-sidebar { 
    /* Stops above the variables panel and status bar, scrolling when the tools outgrow it. */
    max-height: calc(100vh - 80px - 150px); overflow-y: auto;
    top: 80px; 
    left: 16px; 
    flex-direction: column; 
    padding: 12px;
}
#left-sidebar .toolbar-group { flex-direction: column; }

.separator { width: 1px; height: 24px; background: var(--border-color); margin: 0 4px; }
#left-sidebar .separator { width: 24px; height: 1px; margin: 4px 0; }

.toolbar-group { display: flex; align-items: center; gap: 8px; }

.toolbar-label {
    font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px;
    color: var(--text-muted); font-weight: 600; text-align: center; width: 100%;
}

/* Buttons */
.tool-btn {
    display: flex; align-items: center; justify-content: center; gap: 8px;
    padding: 8px 14px; background: var(--btn-bg); border: 1px solid var(--border-color);
    border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 500;
    color: var(--text-main); white-space: nowrap; transition: all 0.2s ease; user-select: none;
}
.icon-btn { padding: 10px; border-radius: 50%; width: 40px; height: 40px; }
.tool-btn:hover { background: var(--btn-hover); transform: translateY(-1px); }
.tool-btn:active { transform: translateY(1px); }
.tool-btn.active {
    background: var(--accent); color: #fff; border-color: var(--accent);
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.tool-btn.active:hover { background: var(--accent-hover); }

/* In Holodeck dark, active text is dark grey */
[data-color-theme="holodeck"][data-brightness="dark"] .tool-btn.active { color: #1a202c; }

.tool-select {
    padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border-color);
    font-size: 13px; background: var(--bg-panel); color: var(--text-main); cursor: pointer; outline: none; min-width: 80px;
}
.tool-select option { background: var(--bg-main); color: var(--text-main); }

#canvas-container {
    width: 100vw; height: 100vh; position: absolute; top: 0; left: 0; z-index: 1;
}

/* Status Bar */
#status-bar {
    position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
    z-index: 100; padding: 8px 20px; font-size: 13px; color: var(--text-main);
    border-radius: 20px; display: flex; align-items: center; gap: 8px;
}
.status-icon { color: var(--accent); }

/* Theme Toggles */
#theme-toggles {
    position: absolute; top: 16px; right: 16px; z-index: 100; display: flex; gap: 8px; align-items: center;
}

/* Right Sidebar */
#right-sidebar {
    position: absolute;
    top: 60px;
    right: 20px;
    width: 250px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 100;
    /* Stops above the ViewCube, which is anchored bottom-right in the same column.
       Verified by test/measure-layout. */
    max-height: calc(100vh - 330px);
    overflow-y: auto;
    /* hide scrollbar for clean look */
    -ms-overflow-style: none;
    scrollbar-width: none;
}
#right-sidebar::-webkit-scrollbar {
    display: none;
}

/* Properties Panel */
#properties-panel {
    padding: 16px;
    display: none;
    flex-direction: column;
    gap: 12px;
}
#properties-panel h3 {
    margin: 0 0 10px 0;
    font-size: 16px;
    font-weight: 500;
}
#preview-container {
    width: 100%;
    height: 150px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 6px;
    border: 1px solid var(--border);
    margin-bottom: 8px;
}
.prop-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
}
.prop-row input[type="text"] {
    width: 70px;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 4px;
    border-radius: 4px;
    text-align: right;
}
.prop-row select {
    width: 100%;
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 6px;
    border-radius: 4px;
}
.prop-row select option {
    background: var(--bg-main);
    color: var(--text-main);
}

/* Segmented Control */
.segmented-control {
    display: flex;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 6px;
    border: 1px solid var(--border);
    overflow: hidden;
    width: 100%;
}
.segmented-control input[type="radio"] {
    display: none;
}
.segmented-control label {
    flex: 1;
    text-align: center;
    padding: 6px 0;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-muted);
    transition: all 0.2s;
    user-select: none;
}
.segmented-control label:hover {
    background: rgba(255, 255, 255, 0.05);
}
.segmented-control input[type="radio"]:checked + label {
    background: var(--accent);
    color: #fff;
    font-weight: 500;
}

/* Variables Panel */
#variables-panel {
    position: absolute;
    bottom: 20px;
    left: 20px;
    width: 250px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 100;
}
#variables-panel h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
}
#variables-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 200px;
    overflow-y: auto;
}
.var-row {
    display: flex;
    gap: 6px;
    align-items: center;
}
.var-row input {
    background: rgba(0,0,0,0.2);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 4px;
    border-radius: 4px;
    flex: 1;
    min-width: 0;
}
.var-row button {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
}
.var-row button:hover {
    color: #ff4444;
}
#add-var-btn {
    background: rgba(255,255,255,0.1);
    border: 1px solid var(--border);
    color: var(--text-main);
    padding: 6px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 500;
}
#add-var-btn:hover {
    background: rgba(255,255,255,0.2);
}

/* Selection Box */
.selectBox {
    border: 1px solid var(--accent);
    background-color: var(--accent);
    opacity: 0.3;
    position: fixed; pointer-events: none; z-index: 100;
}

/* ViewCube Wrapper */
#viewcube-wrapper {
    position: absolute;
    bottom: 20px;
    right: 20px;
    z-index: 100;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 12px;
}
#viewcube-container {
    width: 100px;
    height: 100px;
    cursor: pointer;
}
#projection-toggle {
    font-size: 11px;
    padding: 6px 12px;
}

/* Dimension Labels */
.dimension-label {
    background: transparent;
    border: none;
    color: var(--accent);
    font-size: 20px;
    font-weight: bold;
    text-shadow: 2px 2px 0 #000, -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000, 0px 2px 0 #000, 0px -2px 0 #000, 2px 0px 0 #000, -2px 0px 0 #000;
    width: 80px;
    text-align: center;
    pointer-events: auto;
    outline: none;
    font-family: 'Inter', sans-serif;
}
.dimension-label:focus {
    background: rgba(0, 0, 0, 0.7);
    border-bottom: 2px solid var(--accent);
}

/* Touch devices get tap targets that a finger can actually hit. Set from JS on the body,
   so it applies regardless of viewport width — a tablet is wide and still touch-driven. */
body.touch-mode .tool-btn { min-width: 40px; min-height: 40px; padding: 8px 12px; }
body.touch-mode .icon-btn { width: 40px; height: 40px; padding: 8px; }
body.touch-mode .tool-select { min-height: 40px; }

/* Narrow screens. Every panel here is absolutely positioned, so the layout is a set of
   reserved bands rather than a flow: two rows across the top, the creation tools down the
   left under them, and a bottom strip for the status bar with the viewcube and variables
   sitting clear above it. Measured with test/measure-layout so nothing overlaps. */
/* Mid-size screens: the file toolbar is wide enough to run into the centred tool row, so the
   tool row drops beneath it, and the creation tools move down to match. */
@media (max-width: 1500px) and (min-width: 769px) {
    #top-toolbar { top: 80px; left: 16px; transform: none; }
    #left-sidebar { top: 144px; max-height: calc(100vh - 144px - 150px); }
}

@media (max-width: 768px) {
    .tool-btn { padding: 6px 10px; font-size: 11px; }
    .icon-btn { width: 32px; height: 32px; padding: 6px; }
    .toolbar { gap: 6px; padding: 6px 8px; }
    .toolbar-group { gap: 4px; }
    #top-left-toolbar .separator { display: none; }

    /* Row 1: file/history on the left, theme and panel toggles on the right. */
    #top-left-toolbar { top: 8px; left: 8px; }
    #theme-toggles { top: 8px; right: 8px; gap: 4px; }
    #color-theme-select { min-width: 0; width: 60px; font-size: 10px; padding: 4px 6px; }

    /* Row 2: transform and boolean tools, free to wrap onto a second line. The row offsets
       leave space for the taller touch-mode buttons below. */
    #top-toolbar {
        top: 70px; left: 8px; right: 8px; transform: none;
        justify-content: center; flex-wrap: wrap;
    }

    /* Creation tools run down the left, below both top rows, scrolling rather than growing
       into the variables panel. */
    #left-sidebar { top: 132px; left: 8px; max-height: calc(100vh - 330px); overflow-y: auto; }

    /* Properties/BOM/lifecycle: narrower, and capped so it cannot reach the bottom strip. */
    #right-sidebar { top: 132px; bottom: auto; right: 8px; width: 168px; max-height: 38vh; }

    /* Bottom strip, stacked above the status bar. */
    #viewcube-wrapper {
        bottom: 52px; right: 8px;
        transform: scale(0.8); transform-origin: bottom right;
    }
    #variables-panel { bottom: 52px; left: 8px; width: 150px; max-height: 26vh; overflow-y: auto; }
    #status-bar {
        left: 8px; right: 8px; bottom: 8px; transform: none;
        justify-content: center; text-align: center; padding: 8px 12px; font-size: 11px;
    }

    /* Touch targets give up a little size here: three toolbars have to share ~500px. */
    body.touch-mode .tool-btn { min-width: 36px; min-height: 36px; padding: 6px 8px; }
    body.touch-mode .icon-btn { width: 36px; height: 36px; padding: 6px; }
}


/* Dashboard Project Card */
.project-card {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    color: white;
    cursor: pointer;
    transition: all 0.2s;
}
.project-card:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: var(--primary-color);
    transform: translateY(-2px);
}
.project-card img {
    width: 100%;
    height: 150px;
    object-fit: cover;
    border-radius: 4px;
    background: #000;
}
.project-card h3 {
    margin: 0;
    font-size: 16px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.project-card .meta {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.7);
    display: flex;
    justify-content: space-between;
}

/* Dialogs & drop overlay */
.dialog-overlay {
    position: fixed; inset: 0; z-index: 20000; display: flex; align-items: center; justify-content: center;
    background: rgba(0, 0, 0, 0.45); padding: 16px;
}
.dialog { width: min(420px, 100%); max-height: 90vh; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; color: var(--text-main); }
.dialog h3 { margin: 0; }
.dialog-message { margin: 0; color: var(--text-muted); font-size: 13px; }
.dialog-row { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.dialog-row small { color: var(--text-muted); }
.dialog-row input[type="checkbox"] { align-self: flex-start; }
.dialog-row input:not([type="checkbox"]), .dialog-row select {
    background: var(--btn-bg); color: var(--text-main); border: 1px solid var(--border-color); border-radius: 6px; padding: 8px; font: inherit;
}
.dialog.invalid .dialog-row input:invalid { border-color: #e53e3e; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dialog-actions .tool-btn { width: auto; padding: 8px 16px; }
.dialog-actions .primary { background: var(--accent); color: #fff; }

#drop-overlay {
    position: fixed; inset: 12px; z-index: 19000; display: none; align-items: center; justify-content: center;
    border: 3px dashed var(--accent); border-radius: 16px; background: rgba(0, 0, 0, 0.35); color: #fff;
    font-size: 20px; pointer-events: none;
}
#drop-overlay.active { display: flex; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.prop-row.triple { gap: 4px; }
.prop-row.triple label { width: 38px; flex: none; }
.prop-row.triple input[type="text"] { width: 0; flex: 1; min-width: 0; padding: 4px 2px; }

#drag-hud {
    position: fixed; z-index: 15000; display: none; pointer-events: none; white-space: nowrap;
    padding: 4px 8px; border-radius: 6px; font: 12px/1.3 'Inter', sans-serif; color: var(--text-main);
    background: var(--bg-panel); border: 1px solid var(--border-color); box-shadow: var(--shadow);
}
#drag-hud.active { display: block; }

.measure-label {
    padding: 2px 6px; border-radius: 4px; font: 600 12px 'Inter', sans-serif; white-space: nowrap;
    color: #fff; background: rgba(0, 0, 0, 0.75); border: 1px solid var(--accent);
}
.measure-label small { display: block; font-weight: 400; opacity: 0.8; }

body.busy, body.busy * { cursor: progress !important; }
body.busy #status-bar .status-icon { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.dialog.wide { width: min(560px, 100%); }
.dialog h4 { margin: 8px 0 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); }
.shortcut-list { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; font-size: 13px; }
.shortcut-list dt { white-space: nowrap; }
.shortcut-list dd { margin: 0; }
kbd {
    display: inline-block; min-width: 1.4em; padding: 1px 6px; text-align: center; font: 600 11px 'Inter', sans-serif;
    border: 1px solid var(--border-color); border-bottom-width: 2px; border-radius: 4px; background: var(--btn-bg); color: var(--text-main);
}

#restore-banner {
    position: fixed; top: 140px; left: 50%; transform: translateX(-50%); z-index: 18000; display: none; align-items: center; gap: 12px;
    padding: 10px 16px; max-width: calc(100vw - 32px); color: var(--text-main); font-size: 13px;
}
#restore-banner.active { display: flex; flex-wrap: wrap; justify-content: center; }
#restore-banner .tool-btn { width: auto; padding: 6px 12px; }

/* No keyboard to list shortcuts for on a phone, and the room is needed by the toolbars. */
@media (max-width: 768px) { #btn-help { display: none; } }
body.touch-mode #btn-help { display: none; }
