: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 h2#jig-heading {
    font-size: 1rem;
    letter-spacing: 0.12em;
    color: var(--accent);
    margin: 0 0 2px 2px;
    font-weight: 700;
}

#sidebar header {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

#sidebar header .header-row {
    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;
    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;
    padding-right: 30px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.9rem;
    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='%23888' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
}

#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);
}

#preview-steps > li {
    transition: background 0.3s, padding-left 0.3s;
    padding: 2px 4px;
    border-radius: 4px;
    cursor: pointer;
}
#preview-steps > li:hover {
    background: rgba(255, 255, 255, 0.1);
}
#preview-steps > li.step-active {
    background: var(--accent);
    color: #000;
    padding-left: 10px;
}
#preview-steps > li.step-active .preview-substeps,
#preview-steps > li.step-active .preview-substeps li {
    color: #000;
}
.preview-substeps {
    list-style-type: lower-alpha;
    padding-left: 18px;
    margin-top: 2px;
    line-height: 1.6;
    font-size: 0.78rem;
}

.preview-note {
    margin-top: 14px;
    font-size: 0.8rem;
    color: var(--text-dim);
    font-style: italic;
}

.preview-note a {
    color: #ffffff;
    text-decoration: underline;
    cursor: pointer;
}

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

.param-row.inactive {
    display: 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-checkbox {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    cursor: pointer;
    margin-right: 6px;
}

.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;
}

/* ─── Preview Playback Controls ───────────────────── */
#preview-controls {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 14px;
    background: rgba(30, 30, 46, 0.85);
    border: 1px solid var(--border);
    border-radius: 8px;
    backdrop-filter: blur(6px);
    z-index: 15;
}

#preview-play-pause {
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.25rem;
    cursor: pointer;
    padding: 2px 6px;
    line-height: 1;
}
#preview-play-pause:hover {
    color: var(--accent);
}

#preview-slider {
    width: 220px;
    accent-color: var(--accent);
}

/* ─── 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 .desktop-help { display: block; }
#nav-help .mobile-help { display: none; }
#nav-help.is-touch .desktop-help { display: none; }
#nav-help.is-touch .mobile-help { display: block; }

#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);
}

/* ─── Bottom-sheet grip (mobile) ──────────────────── */
#sidebar-grip {
    display: none;
}

/* ─── Sidebar Toggle (desktop) ────────────────────── */
#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 (min-width: 769px) {
    #sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #sidebar {
        transition: transform 0.25s ease;
    }

    #sidebar.collapsed {
        transform: translateX(-100%);
        position: absolute;
        height: 100vh;
        z-index: 25;
    }
}

@media (max-width: 768px) {
    #app {
        flex-direction: column;
    }

    #viewport {
        flex: 1;
        min-height: 0;
    }

    /* Sidebar becomes a bottom sheet */
    #sidebar {
        position: relative;
        width: 100%;
        min-width: 100%;
        max-height: 45vh;
        border-right: none;
        border-top: 1px solid var(--border);
        padding: 0 16px 16px;
        transition: max-height 0.3s ease, padding 0.3s ease;
        overflow-y: auto;
    }

    #sidebar.collapsed {
        max-height: 44px;
        padding-bottom: 0;
        overflow: hidden;
    }

    /* Grip handle — always visible at top of bottom sheet */
    #sidebar-grip {
        display: flex;
        justify-content: center;
        padding: 8px 0 4px;
        cursor: pointer;
        position: sticky;
        top: 0;
        z-index: 2;
        background: var(--surface);
    }

    .grip-bar {
        display: block;
        width: 40px;
        height: 4px;
        background: var(--text-dim);
        border-radius: 2px;
    }

    #sidebar header {
        padding-top: 4px;
    }

    /* Hide desktop toggle, open button not needed */
    #sidebar-toggle {
        display: none !important;
    }
    #sidebar-open {
        display: none !important;
    }

    /* Playback controls sit above the bottom sheet */
    #preview-controls {
        bottom: 8px;
    }

    #preview-slider {
        width: 140px;
    }
}

/* ─── Sidebar Open Button (viewport, desktop) ────── */
#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 (min-width: 769px) {
    #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;
}

/* ─── Sidebar Footer ─────────────────────────────── */
#sidebar-footer {
    margin-top: auto;
    padding-top: 12px;
    border-top: 1px solid var(--border);
    text-align: center;
}

#sidebar-footer a {
    color: var(--text-dim);
    text-decoration: none;
    font-size: 0.85rem;
    transition: color 0.2s;
}

#sidebar-footer a:hover {
    color: var(--accent);
}
