/* Grid background for the workspace */
.bg-grid {
    background-color: #f8fafc;
    background-image: 
        linear-gradient(to right, #e2e8f0 1px, transparent 1px),
        linear-gradient(to bottom, #e2e8f0 1px, transparent 1px);
    background-size: 20px 20px;
}

.components-sidebar {
    width: 16rem;
    min-width: 16rem;
    transition: width 0.18s ease, min-width 0.18s ease;
}

.components-sidebar.is-collapsed {
    width: 3.5rem;
    min-width: 3.5rem;
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.sidebar-title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-toggle {
    flex: 0 0 auto;
    width: 1.75rem;
    height: 1.75rem;
    border: 1px solid rgba(255, 255, 255, 0.35);
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.12);
    color: #ffffff;
    font-size: 1.35rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.components-sidebar.is-collapsed .sidebar-content,
.components-sidebar.is-collapsed .sidebar-title {
    display: none;
}

.components-sidebar.is-collapsed .sidebar-header {
    justify-content: center;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
}

.components-sidebar.is-collapsed .sidebar-toggle {
    transform: rotate(180deg);
}

.draggable-item:not([data-type="textNode"]) {
    min-height: 64px;
    justify-content: center;
}

.draggable-item {
    position: relative;
    touch-action: none;
}

.draggable-item[data-tooltip]::after {
    content: attr(data-tooltip);
    position: absolute;
    left: 50%;
    bottom: calc(100% + 6px);
    transform: translateX(-50%);
    z-index: 60;
    padding: 4px 7px;
    border-radius: 6px;
    background: #0f172a;
    color: #ffffff;
    font-size: 11px;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.12s ease, transform 0.12s ease;
}

.draggable-item[data-tooltip]:hover::after,
.draggable-item[data-tooltip]:focus-visible::after {
    opacity: 1;
    transform: translate(-50%, -2px);
}

.draggable-item:not([data-type="textNode"]) .gate-icon {
    margin-bottom: 0;
}

#workspace {
    touch-action: none;
    overflow: hidden;
    cursor: grab;
}

#workspace.is-panning {
    cursor: grabbing;
}

#wires {
    pointer-events: none;
}

#canvas,
#wires {
    transform-origin: 0 0;
}

#canvas {
    will-change: transform;
}

.zoom-controls {
    position: absolute;
    right: 16px;
    top: 16px;
    z-index: 60;
    display: inline-flex;
    align-items: center;
    gap: 2px;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.92);
    box-shadow: 0 8px 20px rgba(15, 23, 42, 0.12);
    padding: 3px;
}

.zoom-controls button {
    min-width: 2.15rem;
    height: 2rem;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: #0f172a;
    font-size: 0.875rem;
    font-weight: 700;
    line-height: 1;
}

.zoom-controls button:hover {
    background: #e2e8f0;
}

#zoom-reset-btn {
    min-width: 3.5rem;
    font-size: 0.78rem;
}

.sim-status {
    position: absolute;
    top: 16px;
    left: 16px;
    z-index: 50;
    max-width: min(26rem, calc(100% - 32px));
    border: 1px solid #f59e0b;
    border-radius: 8px;
    background: #fffbeb;
    color: #92400e;
    box-shadow: 0 8px 20px rgba(15, 23, 42, 0.12);
    padding: 8px 10px;
    font-size: 13px;
    font-weight: 600;
}

.palette-drag-ghost {
    position: fixed;
    z-index: 100;
    pointer-events: none;
    opacity: 0.9;
    transform: translate(-50%, -50%);
    box-shadow: 0 16px 30px rgba(15, 23, 42, 0.22);
}

/* Base Node style */
.node {
    position: absolute;
    user-select: none;
    cursor: grab;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
}

.node-input,
.node-output {
    padding: 2px;
}

.node:active {
    cursor: grabbing;
}

/* Node Editable Labels */
.node-label {
    margin-top: 6px;
    width: 100%;
    min-width: 60px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 3px;
    text-align: center;
    font-size: 12px;
    font-weight: 600;
    color: #475569;
    cursor: text;
    z-index: 40;
    transition: background 0.2s, border-color 0.2s;
}

.node-label:hover {
    border: 1px dashed #cbd5e1;
}

.node-label:focus {
    border: 1px solid #3b82f6;
    outline: none;
    background: #ffffff;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

/* Text Node (Free Label) */
.text-node {
    background: rgba(255, 255, 255, 0.6);
    border: 1px dashed #cbd5e1;
    border-radius: 4px;
    padding: 10px; /* Padding acts as a drag handle */
    min-width: 60px;
    min-height: 40px;
    display: flex;
    transition: border-color 0.2s, background 0.2s;
    backdrop-filter: blur(2px);
    cursor: grab;
}

.text-node:hover {
    border-color: #3b82f6;
    background: rgba(255, 255, 255, 0.9);
}

.text-input {
    width: 100%;
    height: 100%;
    min-width: 40px;
    min-height: 20px;
    resize: both;
    overflow: auto;
    background: transparent;
    border: none;
    outline: none;
    color: #334155;
    font-size: 16px;
    font-family: inherit;
    line-height: 1.4;
    cursor: text;
}

/* Wrapper for components */
.gate-wrapper {
    position: relative;
    width: 80px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.switch-wrapper {
    position: relative;
    width: 70px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.bulb-wrapper {
    position: relative;
    width: 60px;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Gate SVG styling */
.gate-svg {
    fill: #ffffff;
    stroke: #334155;
    stroke-width: 2px;
    filter: drop-shadow(0 2px 3px rgba(0,0,0,0.1));
    width: 100%;
    height: 100%;
}

.gate-leg {
    fill: none;
    stroke: #334155;
    stroke-width: 2px;
    stroke-linecap: round;
    filter: drop-shadow(0 2px 3px rgba(0,0,0,0.1));
}

/* Switch SVG Elements */
.switch-bg {
    transition: fill 0.2s ease, stroke 0.2s ease;
    stroke: #64748b;
    stroke-width: 2;
}

.switch-leg {
    stroke: #64748b;
    stroke-width: 2;
    stroke-linecap: round;
    transition: stroke 0.2s ease;
}

.switch-knob {
    transition: cx 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    stroke: #64748b;
    stroke-width: 2;
    filter: drop-shadow(0 2px 2px rgba(0,0,0,0.2));
}

.switch-wrapper:hover .switch-bg {
    stroke: #475569;
}

/* Bulb SVG Elements */
.bulb-body {
    transition: fill 0.2s ease, stroke 0.2s ease, filter 0.2s ease;
}

/* Logic State colors */
.state-on .gate-svg {
    stroke: #10b981;
    fill: #ecfdf5;
}

.state-on .gate-leg {
    stroke: #10b981;
}

.state-on .switch-bg {
    fill: #10b981;
    stroke: #059669;
}

.state-on .switch-leg {
    stroke: #059669;
}

.state-on .switch-knob {
    cx: 45;
    stroke: #059669;
}

.state-on .bulb-body {
    fill: #fcd34d;
    stroke: #f59e0b;
    filter: drop-shadow(0 0 10px rgba(251, 191, 36, 0.8));
}

.state-incomplete .gate-svg,
.state-incomplete .switch-bg,
.state-incomplete .bulb-body {
    stroke: #d97706;
    stroke-dasharray: 5 4;
}

.state-incomplete .gate-leg,
.state-incomplete .switch-leg {
    stroke: #d97706;
    stroke-dasharray: 5 4;
}

/* Ports */
.port {
    position: absolute;
    width: 28px;
    height: 28px;
    background: transparent;
    border: 0;
    border-radius: 999px;
    cursor: crosshair;
    z-index: 20;
    touch-action: none;
    transition: transform 0.1s;
}

.port::before {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 1;
    width: 12px;
    height: 12px;
    border: 2px solid #64748b;
    border-radius: 50%;
    background: #ffffff;
    box-shadow: 0 0 0 2px rgba(248, 250, 252, 0.95);
    transform: translate(-50%, -50%);
    transition: background 0.1s, border-color 0.1s, box-shadow 0.1s;
}

.port:hover::before {
    background: #e2e8f0;
    border-color: #334155;
    box-shadow: 0 0 0 3px rgba(147, 197, 253, 0.55);
}
.port.active::before {
    background: #3b82f6;
    border-color: #2563eb;
}

/* Dynamic port positioning and legs */
.port::after {
    content: '';
    position: absolute;
    background: #64748b;
    pointer-events: none;
    z-index: 0;
}

.port-left { left: -18px; transform: translateY(-50%); }
.port-left:hover { transform: translateY(-50%) scale(1.2); }
.port-left::after {
    width: 10px;
    height: 3px;
    top: 50%;
    left: 14px;
    transform: translateY(-50%);
}

.port-right { right: -18px; top: 50%; transform: translateY(-50%); }
.port-right:hover { transform: translateY(-50%) scale(1.2); }
.port-right::after {
    width: 10px;
    height: 3px;
    top: 50%;
    right: 14px;
    transform: translateY(-50%);
}

.port-bottom { bottom: -18px; left: 50%; transform: translateX(-50%); }
.port-bottom:hover { transform: translateX(-50%) scale(1.2); }
.port-bottom::after {
    width: 3px;
    height: 10px;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
}

.node-input .port-right {
    right: -17px;
}

.node-output .port-bottom {
    bottom: -17px;
}

.state-on .port {
    border-color: #059669;
}

.state-on .port::before {
    border-color: #059669;
}

.state-on .port::after {
    background: #059669;
}

.state-incomplete .port::before {
    border-color: #d97706;
}

.state-incomplete .port::after {
    background: #d97706;
}

/* SVG Wire Paths */
.wire-path {
    fill: none;
    stroke: #64748b;
    stroke-width: 4;
    stroke-linecap: round;
    pointer-events: stroke;
    cursor: pointer;
    transition: opacity 0.2s ease, stroke 0.2s ease;
}
.wire-path.active {
    stroke: #10b981;
}
.wire-path.incomplete {
    stroke: #d97706;
    stroke-dasharray: 7,7;
    opacity: 0.78;
}
.wire-path:hover {
    opacity: 0.75;
}
.wire-path-drawing {
    fill: none;
    stroke: #3b82f6;
    stroke-width: 4;
    stroke-dasharray: 6,6;
    animation: dash 1s linear infinite;
    pointer-events: none;
}

@keyframes dash {
    to { stroke-dashoffset: -12; }
}
