/* ═══════════════════════════════════════════════════
   CZElectro — Layout Styles
   Sidebar, workspace, toolbar, status bar, grid,
   wire layer, context menu, tooltip, instructions
   ═══════════════════════════════════════════════════ */

/* ── Sidebar ── */
#sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 300px;
    min-width: 300px;
    background: var(--bg-secondary);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    /* border-right: 1px solid var(--border-subtle); */
    display: flex;
    flex-direction: column;
    z-index: 50;
    box-shadow: 5px 0 40px rgba(0, 0, 0, 0.6);
}

#sidebar-header {
    padding: 20px 20px 16px;
    border-bottom: 1px solid var(--border-subtle);
    text-align: center;
    background: linear-gradient(180deg, var(--sidebar-header-gradient) 0%, transparent 100%);
}

.logo-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 6px;
}

.logo-icon {
    width: 36px;
    height: 36px;
    object-fit: contain;
    filter: brightness(1.4) drop-shadow(0 0 6px rgba(150, 180, 255, 0.5));
    flex-shrink: 0;
    transition: filter 0.3s;
}

[data-theme="light"] .logo-icon {
    filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.15));
}

.title {
    font-size: 1.15rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--accent), #22d3ee);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: 1.5px;
    text-transform: uppercase;
}

.subtitle {
    font-size: 0.7rem;
    color: var(--text-muted);
    letter-spacing: 2px;
    text-transform: uppercase;
}

/* Category tabs */
.category-tabs {
    display: flex;
    gap: 4px;
    padding: 12px 16px 0;
}

.cat-tab {
    flex: 1;
    padding: 7px 0;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    font-size: 0.7rem;
    font-weight: 600;
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: all 0.25s var(--transition-smooth);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.cat-tab:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-secondary);
}

.cat-tab.active {
    background: rgba(74, 222, 128, 0.12);
    color: var(--accent);
}

/* Sidebar search */
.sidebar-search {
    padding: 10px 16px 4px;
}

.sidebar-search input {
    width: 100%;
    padding: 9px 14px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 0.8rem;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.sidebar-search input::placeholder {
    color: var(--text-muted);
}

.sidebar-search input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

/* Empty search result */
.sidebar-empty {
    padding: 24px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-style: italic;
}

/* Component list */
#component-list {
    flex: 1;
    overflow-y: auto;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#component-list::-webkit-scrollbar {
    width: 5px;
}

#component-list::-webkit-scrollbar-track {
    background: transparent;
}

#component-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.15);
    border-radius: 4px;
}

.sidebar-item {
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 14px;
    cursor: grab;
    transition: all 0.3s var(--transition-smooth);
    position: relative;
    overflow: hidden;
    flex-shrink: 0;
}

.sidebar-item::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, var(--accent-glow), transparent);
    opacity: 0;
    transition: opacity 0.3s;
}

.sidebar-item:hover {
    border-color: var(--border-hover);
    transform: translateX(4px);
    box-shadow: 0 4px 20px rgba(74, 222, 128, 0.1);
}

.sidebar-item:hover::before {
    opacity: 1;
}

.sidebar-item:active {
    cursor: grabbing;
    transform: scale(0.97);
}

.item-icon {
    width: 52px;
    height: 52px;
    flex-shrink: 0;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.5));
    position: relative;
    z-index: 1;
}

.item-details {
    display: flex;
    flex-direction: column;
    gap: 3px;
    position: relative;
    z-index: 1;
}

.item-name {
    font-size: 0.82rem;
    font-weight: 700;
    color: #fff;
}

.item-spec {
    font-size: 0.68rem;
    color: var(--text-muted);
    font-family: 'Cascadia Code', 'Consolas', 'SF Mono', 'Menlo', monospace;
}

/* ── Workspace ── */
#workspace-container {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
    background-color: var(--bg-workspace);
}

#workspace {
    width: 5000px;
    height: 5000px;
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
}

/* Pre-render cache: static visual placeholder before JS hydration */
#preload-cache {
    pointer-events: none;
}
#preload-cache .terminal {
    display: none;
}
#preload-cache .power-on-off-badge,
#preload-cache .broken-badge {
    pointer-events: none;
}

/* Grid overlay */
#grid-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 0;
    opacity: 1;
}

/* Wire SVG layer — lives inside #workspace so coordinates match component positions */
#wire-layer {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 5;
    pointer-events: none;
    overflow: visible;
}

.wire-hit-area {
    pointer-events: stroke;
    cursor: grab;
    stroke: transparent;
    fill: none;
    stroke-width: 18;
}

.wire-hit-area:active {
    cursor: grabbing;
}

/* Draggable control-point handles — hidden by default, revealed on hover */
.wire-handle {
    pointer-events: all;
    cursor: grab;
    fill: rgba(148, 163, 184, 0);
    stroke: rgba(255, 255, 255, 0);
    stroke-width: 1.5;
    transition: fill 0.25s ease, stroke 0.25s ease, r 0.15s ease, opacity 0.25s ease;
    opacity: 0;
}

/* Show handles when hovering the wire group */
.wire-group:hover .wire-handle {
    fill: var(--wire-handle-fill);
    stroke: var(--wire-handle-stroke);
    opacity: 1;
}

/* Individual handle hover — glow accent */
.wire-handle:hover {
    fill: var(--accent);
    stroke: #fff;
    opacity: 1;
    r: 7;
    filter: drop-shadow(0 0 6px rgba(74, 222, 128, 0.6));
}

/* Grabbing state */
.wire-handle:active {
    cursor: grabbing;
    fill: #4ade80;
    stroke: #fff;
    r: 6;
    filter: drop-shadow(0 0 10px rgba(74, 222, 128, 0.8));
}

.real-wire {
    pointer-events: none;
    fill: none;
    transition: stroke 0.3s, filter 0.3s;
}

/* Subtle highlight on wire hover — not aggressive red */
.wire-group:hover .real-wire {
    filter: drop-shadow(0 0 6px rgba(148, 163, 184, 0.5));
}

.wire-group:hover .wire-hit-area {
    stroke: rgba(148, 163, 184, 0.08);
}

/* Selected wire handles — always visible with accent glow */
.wire-handle.handle-selected {
    fill: #22d3ee;
    stroke: #fff;
    stroke-width: 2;
    opacity: 1;
    r: 6;
    filter: drop-shadow(0 0 8px rgba(34, 211, 238, 0.7));
}

.wire-handle.handle-selected:hover {
    fill: #67e8f9;
    r: 7;
    filter: drop-shadow(0 0 10px rgba(34, 211, 238, 0.9));
}

/* ── Touch devices: visual styling for large SVG touch targets ── */
/* Handle geometry (r attribute) is set in wire.js for reliable hit-testing. */
/* CSS only controls visual appearance — fill, opacity, stroke. */
@media (pointer: coarse) {
    .wire-handle {
        opacity: 1;
        fill: rgba(148, 163, 184, 0.12);
        stroke: rgba(255, 255, 255, 0.08);
        stroke-width: 1;
    }

    .wire-handle:active {
        fill: rgba(74, 222, 128, 0.4);
        stroke: rgba(255, 255, 255, 0.5);
        filter: drop-shadow(0 0 8px rgba(74, 222, 128, 0.6));
    }

    .wire-handle.handle-selected {
        fill: rgba(34, 211, 238, 0.25);
        stroke: rgba(255, 255, 255, 0.3);
        stroke-width: 1.5;
        filter: drop-shadow(0 0 6px rgba(34, 211, 238, 0.5));
    }

    .wire-hit-area {
        stroke-width: 24;
    }
}

/* Pan mode cursor */
#workspace-container.mode-pan {
    cursor: grab;
}

#workspace-container.mode-pan:active {
    cursor: grabbing;
}

/* ── Toolbar ── */
#toolbar {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    background: var(--bg-secondary);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    z-index: 30;
    box-shadow: var(--shadow-heavy);
    align-items: center;
    max-width: calc(100vw - 340px);
    overflow: visible;
    flex-wrap: nowrap;
    scrollbar-width: none;
}

#toolbar::-webkit-scrollbar {
    display: none;
}

.tb-btn {
    width: 38px;
    height: 38px;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    background: rgba(255, 255, 255, 0.04);
    color: var(--text-secondary);
    font-size: 16px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.tb-btn:hover {
    background: rgba(74, 222, 128, 0.15);
    color: var(--accent);
    border-color: var(--border-hover);
}

.tb-btn.active {
    background: rgba(74, 222, 128, 0.2);
    color: var(--accent);
    border-color: var(--accent);
}

.tb-btn:disabled {
    opacity: 0.3;
    pointer-events: none;
    cursor: default;
}

.tb-sep {
    width: 1px;
    background: var(--border-subtle);
    margin: 4px 2px;
    flex-shrink: 0;
}

.tb-label {
    font-size: 0.72rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    padding: 0 6px;
    font-family: 'Cascadia Code', 'Consolas', 'SF Mono', 'Menlo', monospace;
    font-weight: 600;
    flex-shrink: 0;
}

/* Desktop: scroll wrapper handles button overflow, toolbar stays overflow:visible */
.tb-scroll {
    display: flex;
    gap: 6px;
    align-items: center;
    overflow-x: auto;
    flex: 1;
    min-width: 0;
    scrollbar-width: none;
}

.tb-scroll::-webkit-scrollbar {
    display: none;
}

.tb-btn-settings {
    flex-shrink: 0;
}

/* Language toggle — reuses theme toggle pattern */
.lang-opt {
    padding: 6px 12px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
    line-height: 1;
    font-weight: 600;
}

.lang-opt:hover {
    background: rgba(128, 128, 128, 0.1);
    color: var(--text-primary);
}

.lang-opt.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 2px 8px var(--accent-glow);
}

/* ── Status Bar (bottom) ── */
#status-bar {
    position: absolute;
    bottom: 16px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 12px;
    padding: 8px 16px;
    background: var(--bg-secondary);
    backdrop-filter: blur(var(--glass-blur));
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    z-index: 30;
    box-shadow: var(--shadow-heavy);
    font-size: 0.72rem;
    font-family: 'Cascadia Code', 'Consolas', 'SF Mono', 'Menlo', monospace;
    flex-wrap: wrap;
}

#circuit-loops {
    display: flex;
    gap: 8px;
    align-items: center;
}

#circuit-loops .status-item {
    background: rgba(74, 222, 128, 0.06);
    border: 1px solid rgba(74, 222, 128, 0.15);
    border-radius: 6px;
    padding: 2px 8px;
    font-size: 0.68rem;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--text-secondary);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
}

.status-dot.live {
    background: var(--accent);
    box-shadow: 0 0 8px var(--accent-glow);
}

.status-dot.warn {
    background: var(--warning);
    box-shadow: 0 0 8px rgba(245, 158, 11, 0.4);
}

.status-dot.danger {
    background: var(--danger);
    box-shadow: 0 0 8px rgba(239, 68, 68, 0.4);
}

/* ── Tooltip ── */
.tooltip {
    position: fixed;
    padding: 8px 14px;
    background: var(--tooltip-bg);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.75rem;
    pointer-events: none;
    z-index: 200;
    backdrop-filter: blur(8px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6);
    opacity: 0;
    transition: opacity 0.15s;
    white-space: nowrap;
}

.tooltip.visible {
    opacity: 1;
}

/* ── Context Menu ── */
.ctx-menu {
    position: fixed;
    min-width: 180px;
    background: var(--ctx-bg);
    backdrop-filter: blur(var(--glass-blur));
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: 6px;
    z-index: 300;
    box-shadow: var(--shadow-heavy);
    animation: ctxIn 0.15s ease-out;
}

@keyframes ctxIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.ctx-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    white-space: nowrap;
    color: var(--text-primary);
    font-size: 0.8rem;
    cursor: pointer;
    transition: background 0.15s;
}

.ctx-item:hover {
    background: rgba(74, 222, 128, 0.12);
}

.ctx-item.danger {
    color: var(--danger);
}

.ctx-item.danger:hover {
    background: rgba(239, 68, 68, 0.12);
}

.ctx-sep {
    height: 1px;
    background: var(--border-subtle);
    margin: 4px 8px;
}

/* Variant badge — shown next to component name when multiple variants exist */
.variant-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    margin-left: 6px;
    font-size: 0.65rem;
    font-weight: 700;
    color: #fff;
    background: linear-gradient(135deg, var(--accent), #059669);
    border-radius: 9px;
    vertical-align: middle;
    line-height: 1;
    cursor: context-menu;
    box-shadow: 0 1px 4px rgba(74, 222, 128, 0.3);
    transition: transform 0.15s, box-shadow 0.15s;
}

.variant-badge:hover {
    transform: scale(1.15);
    box-shadow: 0 2px 8px rgba(74, 222, 128, 0.5);
}

/* Context menu: title row (non-clickable header) */
.ctx-item.ctx-title {
    font-weight: 700;
    font-size: 0.75rem;
    color: var(--accent);
    cursor: default;
    opacity: 0.9;
    pointer-events: none;
}

/* Context menu: currently active variant */
.ctx-item.ctx-active {
    color: var(--accent);
    font-weight: 600;
    background: rgba(74, 222, 128, 0.08);
}

/* Context menu: spec text (muted) */
.ctx-spec {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-left: auto;
    padding-left: 12px;
    opacity: 0.7;
}

/* Variant menu scrollbar */
.variant-menu {
    scrollbar-width: thin;
    scrollbar-color: rgba(148, 163, 184, 0.3) transparent;
}

.variant-menu::-webkit-scrollbar {
    width: 5px;
}

.variant-menu::-webkit-scrollbar-track {
    background: transparent;
}

.variant-menu::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.3);
    border-radius: 3px;
}

.variant-menu::-webkit-scrollbar-thumb:hover {
    background: rgba(148, 163, 184, 0.5);
}

/* Copy toast */
.copy-toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--toast-bg);
    color: var(--accent);
    padding: 10px 20px;
    border-radius: 8px;
    border: 1px solid var(--accent-glow);
    font-size: 0.8rem;
    font-family: 'Cascadia Code', monospace;
    z-index: 9999;
    animation: toastIn 0.3s ease, toastOut 0.3s ease 2.2s forwards;
    box-shadow: var(--shadow-heavy);
}

@keyframes toastIn {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@keyframes toastOut {
    to {
        opacity: 0;
        transform: translateX(-50%) translateY(-10px);
    }
}

/* ── Instructions Panel ── */
#instructions {
    position: absolute;
    bottom: 16px;
    right: 16px;
    background: var(--bg-secondary);
    backdrop-filter: blur(var(--glass-blur));
    border: 1px solid var(--border-subtle);
    padding: 16px 20px;
    border-radius: var(--radius-lg);
    color: var(--text-secondary);
    font-size: 0.78rem;
    line-height: 1.7;
    z-index: 40;
    box-shadow: var(--shadow-heavy);
    max-width: 300px;
    transition: opacity 0.3s, transform 0.3s;
}

#instructions.hidden {
    opacity: 0;
    transform: translateY(10px);
    pointer-events: none;
}

#instructions strong {
    color: var(--accent);
}

/* ── Selection Rectangle ── */
.selection-rect {
    position: absolute;
    border: 1.5px dashed var(--accent);
    background: var(--selection-bg);
    pointer-events: none;
    z-index: 100;
    border-radius: 2px;
}

/* ── Sidebar Toggle Button ── */
.sidebar-toggle {
    position: fixed;
    top: 16px;
    left: 300px;
    z-index: 60;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border-subtle);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    background: var(--bg-secondary);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    color: var(--text-secondary);
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: left 0.35s var(--transition-smooth),
        background 0.2s, color 0.2s,
        box-shadow 0.2s;
    box-shadow: 4px 0 15px rgba(0, 0, 0, 0.3);
    border-left: none;
    line-height: 1;
}

.sidebar-toggle:hover {
    background: rgba(74, 222, 128, 0.15);
    color: var(--accent);
    box-shadow: 4px 0 20px rgba(74, 222, 128, 0.15);
}

/* Disable transition during initial load */
#sidebar.no-transition,
#sidebar.no-transition~.sidebar-toggle,
#sidebar.no-transition~#workspace-container {
    transition: none !important;
}

/* Sidebar collapsed state */
#sidebar {
    transition: transform 0.35s var(--transition-smooth),
        box-shadow 0.35s;
}

#sidebar.collapsed {
    transform: translateX(-100%);
    box-shadow: none;
    overflow: hidden;
}

#sidebar.collapsed~.sidebar-toggle {
    left: 0;
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

/* Sidebar overlay backdrop (mobile) */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9;
    opacity: 0;
    transition: opacity 0.3s;
    backdrop-filter: blur(2px);
}

.sidebar-overlay.visible {
    display: block;
    opacity: 1;
}

/* ── Responsive ── */

/* Tablet */
@media (max-width: 1024px) {
    #sidebar {
        width: 280px;
        min-width: 280px;
    }

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

    .sidebar-toggle {
        left: 280px;
    }

    #sidebar.collapsed~.sidebar-toggle {
        left: 0;
    }

    .item-icon {
        width: 44px;
        height: 44px;
    }

    #toolbar {
        padding: 6px 8px;
        gap: 4px;
    }

    .tb-btn {
        width: 34px;
        height: 34px;
        font-size: 14px;
    }
}

/* Mobile */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 300px;
        min-width: 300px;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.35s var(--transition-smooth);
    }

    #sidebar.open {
        transform: translateX(0);
    }

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

    .sidebar-toggle {
        left: 0;
        top: 0;
        z-index: 101;
        width: 44px;
        height: 44px;
        font-size: 18px;
        border: none;
        border-right: 1px solid var(--border-subtle);
        border-bottom: 1px solid var(--border-subtle);
        border-radius: 0;
        background: var(--bg-secondary);
        backdrop-filter: blur(var(--glass-blur));
        -webkit-backdrop-filter: blur(var(--glass-blur));
        color: var(--text-secondary);
        box-shadow: none;
        transition: left 0.35s var(--transition-smooth),
            border-radius 0.35s var(--transition-smooth),
            background 0.2s, color 0.2s;
    }

    .sidebar-toggle:hover,
    .sidebar-toggle:active {
        background: rgba(74, 222, 128, 0.15);
        color: var(--accent);
    }

    #sidebar.open~.sidebar-toggle {
        left: 300px;
        border-radius: 0 0 var(--radius-sm) 0;
    }

    /* Dim toolbar & status bar when sidebar is open — matches overlay backdrop */
    #sidebar.open~#workspace-container #toolbar,
    #sidebar.open~#workspace-container #status-bar {
        opacity: 0.3;
        pointer-events: none;
        filter: blur(2px);
        transition: opacity 0.3s, filter 0.3s;
    }

    .item-icon {
        width: 40px;
        height: 40px;
    }

    .sidebar-item {
        padding: 10px 12px;
        gap: 10px;
    }

    .item-name {
        font-size: 0.78rem;
    }

    #toolbar {
        position: fixed;
        top: 0;
        left: 44px;
        right: 0;
        transform: none;
        padding: 0;
        gap: 0;
        flex-wrap: nowrap;
        overflow: visible;
        max-width: none;
        height: 44px;
        border-radius: 0;
        border-top: none;
        border-left: none;
        z-index: 55;
        align-items: center;
        transition: opacity 0.3s, filter 0.3s;
    }

    #toolbar::-webkit-scrollbar {
        display: none;
    }

    .tb-scroll {
        display: flex;
        gap: 3px;
        padding: 4px 8px;
        overflow-x: auto;
        overflow-y: hidden;
        flex: 1;
        min-width: 0;
        height: 100%;
        align-items: center;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-x;
        scrollbar-width: none;
    }

    .tb-scroll::-webkit-scrollbar {
        display: none;
    }

    /* Settings stays fixed at right edge — mirrors sidebar toggle exactly */
    #toolbar .tb-btn-settings {
        border: none;
        border-left: 1px solid var(--border-subtle);
        border-bottom: 1px solid var(--border-subtle);
        border-radius: 0;
        height: 44px;
        width: 44px;
        flex-shrink: 0;
        background: var(--bg-secondary);
        backdrop-filter: blur(var(--glass-blur));
        -webkit-backdrop-filter: blur(var(--glass-blur));
        color: var(--text-secondary);
        font-size: 18px;
        box-shadow: none;
    }

    #toolbar .tb-btn-settings:hover,
    #toolbar .tb-btn-settings:active {
        background: rgba(74, 222, 128, 0.15);
        color: var(--accent);
    }

    #toolbar .tb-btn-settings.active {
        background: rgba(74, 222, 128, 0.2);
        color: var(--accent);
    }

    .tb-sep-settings {
        display: none;
    }

    .tb-btn {
        width: 34px;
        height: 34px;
        font-size: 14px;
        flex-shrink: 0;
    }

    .tb-sep {
        width: 1px;
        min-width: 1px;
        height: 20px;
        margin: 0 1px;
        flex-shrink: 0;
    }

    .tb-label {
        font-size: 0.6rem;
        padding: 0 3px;
        flex-shrink: 0;
    }

    #status-bar {
        bottom: 0;
        left: 0;
        right: 0;
        transform: none;
        border-radius: 12px 12px 0 0;
        padding: 6px 10px;
        padding-bottom: max(6px, env(safe-area-inset-bottom));
        font-size: 0.65rem;
        gap: 4px 8px;
        max-width: 100%;
        flex-wrap: wrap;
        overflow: hidden;
        transition: opacity 0.3s, filter 0.3s;
    }

    #status-bar .status-item {
        flex-shrink: 0;
    }

    #status-bar .sim-controls {
        border-left: none;
        border-top: 1px solid var(--border-subtle);
        margin-left: 0;
        padding: 4px 0 0;
        width: 100%;
        flex-wrap: wrap;
        gap: 3px;
    }

    #status-bar .sim-label {
        font-size: 10px;
        margin-right: 2px;
    }

    #status-bar .sim-btn {
        padding: 4px 8px;
        font-size: 10px;
        min-height: 28px;
    }

    #status-bar .sim-divider {
        display: none;
    }

    #status-bar .sim-day,
    #status-bar .sim-night {
        font-size: 9px;
        padding: 4px 6px;
    }

    #status-bar .sim-panel {
        border-left: none;
        border-top: 1px solid var(--border-subtle);
        margin-left: 0;
        padding: 4px 0 0;
        width: 100%;
        font-size: 9px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    #status-bar .sim-panel::-webkit-scrollbar {
        display: none;
    }

    #circuit-loops {
        flex-wrap: wrap;
        gap: 4px;
    }

    #instructions {
        display: none;
    }

    /* ctx-menu overflow handled by JS reposition */
}

/* Small Mobile */
@media (max-width: 480px) {
    #sidebar {
        width: 260px;
        min-width: 260px;
    }

    #sidebar.open~.sidebar-toggle {
        left: 260px;
    }

    .sidebar-toggle {
        width: 38px;
        height: 38px;
        font-size: 16px;
    }

    .tb-btn-settings {
        height: 38px;
        width: 38px;
    }

    #toolbar {
        left: 38px;
        height: 38px;
    }

    .tb-btn {
        width: 30px;
        height: 30px;
        font-size: 12px;
    }

    .tb-sep {
        height: 16px;
        margin: 0 1px;
    }

    .tb-label {
        font-size: 0.55rem;
        padding: 0 2px;
    }

    .sidebar-item {
        padding: 8px 10px;
        gap: 8px;
    }

    .item-icon {
        width: 36px;
        height: 36px;
    }

    .item-name {
        font-size: 0.72rem;
    }

    .item-spec {
        font-size: 0.6rem;
    }

    .category-tabs {
        padding: 8px 10px 0;
        gap: 2px;
    }

    .cat-tab {
        font-size: 0.62rem;
        padding: 5px 0;
    }

    #sidebar-header {
        padding: 14px 14px 12px;
    }

    .title {
        font-size: 1rem;
    }

    .subtitle {
        font-size: 0.6rem;
    }

    #status-bar {
        padding: 4px 8px;
        padding-bottom: max(4px, env(safe-area-inset-bottom));
        font-size: 0.6rem;
        gap: 3px 6px;
    }

    #status-bar .sim-controls {
        gap: 2px;
    }

    #status-bar .sim-btn {
        padding: 3px 6px;
        font-size: 9px;
        min-height: 26px;
    }

    #status-bar .sim-label {
        font-size: 9px;
    }

    #status-bar .sim-day,
    #status-bar .sim-night {
        font-size: 8px;
        padding: 3px 5px;
    }
}

/* ═══════════════════════════════════════════════════
   Settings Panel
   ═══════════════════════════════════════════════════ */
/* Settings anchor wrapper — keeps dropdown attached to button */
.settings-anchor {
    position: relative;
    flex-shrink: 0;
}

.settings-panel {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    width: 260px;
    background: var(--bg-secondary);
    backdrop-filter: blur(var(--glass-blur));
    -webkit-backdrop-filter: blur(var(--glass-blur));
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    z-index: 200;
    box-shadow: var(--shadow-heavy);
    animation: settingsIn 0.2s ease-out;
    overflow: hidden;
}

.settings-panel.hidden {
    display: none;
}

@keyframes settingsIn {
    from {
        opacity: 0;
        transform: translateY(-8px) scale(0.97);
    }

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

.settings-header {
    padding: 14px 18px 10px;
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-subtle);
    letter-spacing: 0.5px;
}

.settings-group {
    padding: 12px 18px 16px;
}

.settings-group-title {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 12px;
}

.settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0;
}

.settings-row+.settings-row {
    border-top: 1px solid var(--border-subtle);
}

.settings-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

/* Theme toggle buttons */
.settings-theme-toggle {
    display: flex;
    gap: 0;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--border-subtle);
}

.theme-opt {
    padding: 6px 14px;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
    line-height: 1;
}

.theme-opt:hover {
    background: rgba(128, 128, 128, 0.1);
    color: var(--text-primary);
}

.theme-opt.active {
    background: var(--accent);
    color: #fff;
    box-shadow: 0 2px 8px var(--accent-glow);
}

/* Toggle switch */
.settings-switch {
    position: relative;
    display: inline-block;
    width: 42px;
    height: 24px;
    cursor: pointer;
}

.settings-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.settings-slider {
    position: absolute;
    inset: 0;
    background: var(--text-muted);
    border-radius: 24px;
    transition: background 0.25s;
}

.settings-slider::before {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: transform 0.25s;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

.settings-switch input:checked+.settings-slider {
    background: var(--accent);
}

.settings-switch input:checked+.settings-slider::before {
    transform: translateX(18px);
}

/* Mobile settings panel */
@media (max-width: 768px) {
    .settings-panel {
        width: 240px;
    }
}

/* ── Import JSON Modal ── */
.import-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}
.import-modal-overlay.hidden {
    display: none;
}
.import-modal {
    background: var(--bg-primary, #0f172a);
    border: 1px solid var(--border, #334155);
    border-radius: 12px;
    padding: 20px;
    width: 520px;
    max-width: 90vw;
    max-height: 80vh;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.import-modal-header {
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary, #e2e8f0);
    font-family: 'Inter', 'Segoe UI', sans-serif;
}
.import-json-textarea {
    width: 100%;
    height: 280px;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid var(--border-subtle, #1e293b);
    border-radius: 8px;
    color: #a5f3fc;
    font-family: 'Cascadia Code', 'Consolas', 'SF Mono', monospace;
    font-size: 11px;
    padding: 12px;
    resize: vertical;
    outline: none;
    transition: border-color 0.2s;
    box-sizing: border-box;
}
.import-json-textarea:focus {
    border-color: var(--accent, #4ade80);
}
.import-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}
.import-btn {
    padding: 8px 20px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.2s;
    font-family: 'Inter', 'Segoe UI', sans-serif;
}
.import-btn-cancel {
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-secondary, #94a3b8);
    border-color: var(--border-subtle, #1e293b);
}
.import-btn-cancel:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary, #e2e8f0);
}
.import-btn-confirm {
    background: rgba(239, 68, 68, 0.8);
    color: #fff;
    border-color: rgba(239, 68, 68, 0.6);
}
.import-btn-confirm:hover {
    filter: brightness(1.15);
    box-shadow: 0 0 12px rgba(239, 68, 68, 0.3);
}
.import-btn-merge {
    background: var(--accent, #4ade80);
    color: #000;
    border-color: var(--accent, #4ade80);
}
.import-btn-merge:hover {
    filter: brightness(1.15);
    box-shadow: 0 0 12px rgba(74, 222, 128, 0.3);
}