:root {
    --bg: #1e1e2e;
    --surface: #2a2a3e;
    --surface-hover: #35354a;
    --border: #3a3a50;
    --text: #e0e0e0;
    --text-dim: #888;
    --accent: #deb887;
    --accent-hover: #eac896;
    --accent-dim: #a08060;
    --error: #e06060;
    --sidebar-width: 300px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
    height: 100vh;
}

#app {
    display: flex;
    height: 100vh;
}

/* ─── Sidebar ─────────────────────────────────────── */
#sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

#sidebar header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

#sidebar h1 {
    font-size: 1.3rem;
    color: var(--accent);
    font-weight: 600;
}

#jig-select {
    flex: 1;
    padding: 6px 10px;
    background: var(--bg);
    color: var(--accent);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23a08060' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 30px;
    margin-right: 8px;
}

#jig-select:focus {
    outline: 1px solid var(--accent-dim);
}

#jig-select:hover {
    border-color: var(--accent-dim);
}

#sidebar h2 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-dim);
    margin-bottom: 10px;
}

#sidebar label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-dim);
    margin-bottom: 4px;
}

#sidebar select {
    width: 100%;
    padding: 8px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.9rem;
    cursor: pointer;
}

#sidebar select:focus {
    outline: 1px solid var(--accent-dim);
}

/* ─── Part Checkboxes ─────────────────────────────── */
#part-checkboxes {
    margin-top: 10px;
}

#part-checkboxes.hidden {
    display: none;
}

.part-group-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin-top: 8px;
    margin-bottom: 2px;
}

.part-checkbox {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 3px 0;
    font-size: 0.82rem;
    cursor: pointer;
}

.part-checkbox input[type="checkbox"] {
    accent-color: var(--accent);
    margin-right: 6px;
}

/* ─── Sidebar Tabs ────────────────────────────────── */
#sidebar-tabs {
    display: flex;
    gap: 2px;
    background: var(--bg);
    border-radius: 6px;
    padding: 3px;
}

#sidebar-tabs .tab {
    flex: 1;
    padding: 7px 0;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-dim);
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

#sidebar-tabs .tab:hover {
    color: var(--text);
    background: var(--surface-hover);
}

#sidebar-tabs .tab.active {
    background: var(--surface-hover);
    color: var(--accent);
    font-weight: 600;
}

.tab-panel {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.tab-panel.hidden {
    display: none !important;
}

/* ─── Preview Info ────────────────────────────────── */
.preview-info p {
    font-size: 0.85rem;
    color: var(--text-dim);
    line-height: 1.5;
    margin-bottom: 12px;
}

.preview-info ol {
    font-size: 0.82rem;
    color: var(--text-dim);
    padding-left: 20px;
    line-height: 1.8;
}

.preview-info ol li::marker {
    color: var(--accent-dim);
}

/* ─── Parameter Controls ──────────────────────────── */
.param-row {
    margin-bottom: 14px;
    transition: opacity 0.2s;
}

.param-row.inactive {
    opacity: 0.3;
    pointer-events: none;
}

.param-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.param-label {
    font-size: 0.85rem;
    color: var(--text-dim);
}

.param-input-wrap {
    display: flex;
    align-items: center;
    gap: 3px;
}

.param-number {
    width: 64px;
    padding: 3px 6px;
    background: var(--bg);
    color: var(--accent);
    border: 1px solid var(--border);
    border-radius: 3px;
    font-size: 0.8rem;
    font-variant-numeric: tabular-nums;
    text-align: right;
    -moz-appearance: textfield;
}

.param-number::-webkit-inner-spin-button,
.param-number::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.param-number:focus {
    outline: 1px solid var(--accent-dim);
    border-color: var(--accent-dim);
}

.param-unit {
    font-size: 0.75rem;
    color: var(--text-dim);
    min-width: 18px;
}

.param-row input[type="range"] {
    width: 100%;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--border);
    border-radius: 2px;
    outline: none;
}

.param-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

.param-row input[type="range"]::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

/* ─── Buttons ─────────────────────────────────────── */
.btn-group {
    display: flex;
    gap: 8px;
}

.btn-primary, .btn-secondary {
    padding: 8px 14px;
    border: none;
    border-radius: 4px;
    font-size: 0.85rem;
    cursor: pointer;
    flex: 1;
    transition: background 0.15s;
}

.btn-primary {
    background: var(--accent-dim);
    color: var(--bg);
    font-weight: 600;
}

.btn-primary:hover {
    background: var(--accent);
}

.btn-secondary {
    background: var(--border);
    color: var(--text-dim);
}

.btn-secondary:hover {
    background: var(--surface-hover);
    color: var(--text);
}

/* ─── Viewport ────────────────────────────────────── */
#viewport {
    flex: 1;
    position: relative;
}

#canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* ─── Loading Overlay ─────────────────────────────── */
#loading {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: rgba(30, 30, 46, 0.8);
    gap: 12px;
    font-size: 0.9rem;
    color: var(--text-dim);
    z-index: 10;
}

#loading.hidden, #error.hidden {
    display: none;
}

.hidden {
    display: none !important;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* ─── Nav Help ────────────────────────────────────── */
#nav-help {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 15;
    background: rgba(30, 30, 46, 0.85);
    border: 1px solid var(--border);
    border-radius: 8px;
    backdrop-filter: blur(6px);
    font-size: 0.78rem;
    color: var(--text);
    user-select: none;
}

#nav-help summary {
    cursor: pointer;
    list-style: none;
    text-align: center;
    width: 28px;
    height: 28px;
    line-height: 28px;
    font-weight: 700;
    color: var(--muted);
    border-radius: 8px;
}

#nav-help summary .open { display: none; }
#nav-help summary .closed { display: inline; }
#nav-help[open] summary .open { display: inline; }
#nav-help[open] summary .closed { display: none; }

#nav-help summary::-webkit-details-marker { display: none; }

#nav-help[open] summary {
    border-bottom: 1px solid var(--border);
    border-radius: 8px 8px 0 0;
    width: auto;
    padding: 0 10px;
}

#nav-help ul {
    list-style: none;
    margin: 0;
    padding: 8px 12px;
}

#nav-help li {
    padding: 2px 0;
    white-space: nowrap;
}

#nav-help kbd {
    display: inline-block;
    min-width: 70px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 6px;
    margin-right: 6px;
    font-family: inherit;
    font-size: 0.75rem;
    color: var(--accent);
}

/* ─── Sidebar Toggle (mobile) ─────────────────────── */
#sidebar-toggle {
    display: none;
    background: none;
    color: var(--text-dim);
    border: none;
    width: 32px;
    height: 32px;
    font-size: 1.3rem;
    cursor: pointer;
    line-height: 1;
    border-radius: 4px;
    flex-shrink: 0;
}

#sidebar-toggle:hover {
    background: var(--surface-hover);
    color: var(--text);
}

@media (max-width: 768px) {
    #sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100vh;
        z-index: 25;
        transition: transform 0.25s ease;
    }

    #sidebar.collapsed {
        transform: translateX(-100%);
    }

    /* Floating open button when sidebar is hidden */
    #sidebar.collapsed ~ #viewport #sidebar-open {
        display: flex;
    }
}

/* ─── Sidebar Open Button (viewport, mobile) ─────── */
#sidebar-open {
    display: none;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 20;
    background: var(--surface);
    color: var(--accent);
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 38px;
    height: 38px;
    font-size: 1.3rem;
    cursor: pointer;
    line-height: 1;
    align-items: center;
    justify-content: center;
}

#sidebar-open:hover {
    background: var(--surface-hover);
}

@media (max-width: 768px) {
    #sidebar.collapsed ~ #viewport #sidebar-open {
        display: flex;
    }
}

/* ─── Error Toast ─────────────────────────────────── */
#error {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--error);
    color: white;
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 0.85rem;
    max-width: 80%;
    z-index: 20;
}
