/**
 * Electronic Module Simulator - Futuristic Theme
 * Minimalist & Modern Design
 */

/* ============================================
   CSS Variables - Theme Configuration
   ============================================ */
:root {
    /* Colors */
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121a;
    --bg-tertiary: #1a1a25;
    --bg-module: #15151f;
    
    --accent-primary: #00f0ff;
    --accent-secondary: #7b2dff;
    --accent-success: #00ff88;
    --accent-warning: #ffaa00;
    --accent-danger: #ff3366;
    
    --text-primary: #ffffff;
    --text-secondary: #8888aa;
    --text-muted: #555566;
    
    --border-color: #2a2a3a;
    --border-glow: rgba(0, 240, 255, 0.3);
    
    /* Effects */
    --glow-primary: 0 0 20px rgba(0, 240, 255, 0.4);
    --glow-success: 0 0 20px rgba(0, 255, 136, 0.6);
    --glow-danger: 0 0 15px rgba(255, 51, 102, 0.5);
    
    /* Transitions */
    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s ease;
    --transition-slow: 0.4s ease;
    
    /* Sizing */
    --toolbar-width: 240px;
    --header-height: 60px;
    --border-radius: 8px;
    --border-radius-sm: 4px;
}

/* ============================================
   Reset & Base Styles
   ============================================ */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
}

/* ============================================
   Layout
   ============================================ */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* Header */
.app-header {
    height: var(--header-height);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    position: relative;
    z-index: 100;
}

.app-header::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--accent-primary), transparent);
    opacity: 0.5;
}

.app-logo {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    border-radius: var(--border-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.logo-text {
    font-size: 18px;
    font-weight: 600;
    letter-spacing: 0.5px;
}

.logo-text span {
    color: var(--accent-primary);
}

/* Controls */
.app-controls {
    display: flex;
    align-items: center;
    gap: 16px;
}

.control-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.control-btn:hover {
    background: var(--bg-module);
    border-color: var(--accent-primary);
    box-shadow: var(--glow-primary);
}

.control-btn.active {
    background: rgba(0, 240, 255, 0.1);
    border-color: var(--accent-primary);
}

.control-btn.danger:hover {
    border-color: var(--accent-danger);
    box-shadow: var(--glow-danger);
}

.control-btn .icon {
    font-size: 14px;
}

/* Control Separator */
.control-separator {
    width: 1px;
    height: 24px;
    background: var(--border-color);
    margin: 0 4px;
}

/* Speed Control */
.speed-control {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    background: var(--bg-tertiary);
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

.speed-control label {
    font-size: 12px;
    color: var(--text-secondary);
}

.speed-slider {
    width: 80px;
    height: 4px;
    -webkit-appearance: none;
    appearance: none;
    background: var(--bg-primary);
    border-radius: 2px;
    cursor: pointer;
}

.speed-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 14px;
    height: 14px;
    background: var(--accent-primary);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 0 10px var(--accent-primary);
}

.speed-value {
    font-size: 12px;
    color: var(--accent-primary);
    min-width: 40px;
    text-align: right;
}

/* Main Content Area */
.app-main {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ============================================
   Toolbar / Sidebar
   ============================================ */
.app-sidebar {
    width: var(--toolbar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.sidebar-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
}

#module-toolbar {
    flex: 1;
    padding: 12px;
    overflow-y: auto;
}

.toolbar-category {
    margin-bottom: 20px;
}

.category-title {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 1.5px;
    padding: 8px 12px;
    margin-bottom: 8px;
}

.category-items {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.toolbar-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: var(--border-radius);
    color: var(--text-primary);
    font-size: 13px;
    cursor: pointer;
    transition: all var(--transition-fast);
    text-align: left;
}

.toolbar-item:hover {
    background: var(--bg-tertiary);
    border-color: var(--border-color);
}

.toolbar-item:active {
    transform: scale(0.98);
}

.toolbar-item .item-icon {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-module);
    border-radius: var(--border-radius-sm);
    font-size: 14px;
}

.toolbar-item .item-name {
    flex: 1;
}

/* ============================================
   Simulation Canvas
   ============================================ */
.canvas-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--bg-primary);
}

#simulation-canvas {
    width: 10000px;
    height: 10000px;
    position: absolute;
    top: 0;
    left: 0;
    background-image: 
        radial-gradient(circle at 1px 1px, var(--border-color) 1px, transparent 0);
    background-size: 30px 30px;
    transform-origin: 0 0;
    transition: none;
}

/* Canvas grid overlay */
#simulation-canvas::before {
    content: '';
    position: absolute;
    inset: 0;
    background: 
        linear-gradient(to right, rgba(0, 240, 255, 0.02) 1px, transparent 1px),
        linear-gradient(to bottom, rgba(0, 240, 255, 0.02) 1px, transparent 1px);
    background-size: 30px 30px;
    pointer-events: none;
}

/* Zoom Indicator */
.zoom-indicator {
    position: absolute;
    bottom: 20px;
    right: 20px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: 8px 16px;
    font-size: 12px;
    font-weight: 600;
    color: var(--accent-primary);
    pointer-events: none;
    z-index: 1000;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* ============================================
   Module Styles - Base
   ============================================ */
.module {
    position: absolute;
    background: var(--bg-module);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    transition: box-shadow var(--transition-fast), border-color var(--transition-fast);
    z-index: 50;
}

.module:hover {
    border-color: rgba(0, 240, 255, 0.3);
    z-index: 51;
}

.module.dragging {
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5), var(--glow-primary);
    border-color: var(--accent-primary);
    z-index: 100;
}

.module.group-dragging {
    box-shadow: 0 4px 30px rgba(0, 240, 255, 0.4);
    border-color: rgba(0, 240, 255, 0.5);
    z-index: 99;
    opacity: 0.9;
}

.module.snapping {
    border-color: var(--accent-success) !important;
    box-shadow: 0 0 30px rgba(0, 255, 136, 0.6), 0 8px 40px rgba(0, 0, 0, 0.5);
}

/* Power states */
.module.unpowered {
    opacity: 0.5;
    border-color: var(--text-muted);
}

.module.unpowered .module-name {
    color: var(--text-muted);
}

.module.powered {
    border-color: var(--accent-success);
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.2);
}

.module-header {
    display: none;
}

.module-name {
    display: none;
}

.module-delete {
    display: none;
}

.module-body {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

/* ============================================
   Ports
   ============================================ */
.module-inputs,
.module-outputs {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 52;
}

.module-inputs {
    left: 24px;
}

.module-outputs {
    right: 24px;
}

.port {
    width: 12px;
    height: 12px;
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: 50%;
    cursor: pointer;
    transition: all var(--transition-fast);
    position: relative;
    z-index: 52;
}

.port::after {
    content: '';
    position: absolute;
    inset: 2px;
    background: var(--bg-tertiary);
    border-radius: 50%;
    transition: all var(--transition-fast);
}

.port:hover {
    border-color: var(--accent-primary);
    transform: scale(1.3);
}

.port.active {
    border-color: var(--accent-success);
    box-shadow: 0 0 8px rgba(0, 255, 136, 0.4);
}

.port.active::after {
    background: var(--accent-success);
}

.input-port {
    border-color: var(--accent-secondary);
}

.output-port {
    border-color: var(--accent-primary);
}

/* ============================================
   Magnetic Connectors
   ============================================ */
.magnetic-connector {
    position: absolute;
    width: 12px;
    height: 12px;
    background: var(--bg-primary);
    border: 2px solid var(--accent-warning);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-fast);
    z-index: 50;
    opacity: 0.7;
}

.magnetic-connector::before {
    content: '';
    width: 4px;
    height: 4px;
    background: var(--accent-warning);
    border-radius: 50%;
}

.magnetic-connector.connected {
    border-color: var(--accent-success);
    background: rgba(0, 255, 136, 0.15);
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.3);
    opacity: 1;
}

.magnetic-connector.connected::before {
    background: var(--accent-success);
}

.magnetic-connector.connector-left {
    left: -6px;
}

.magnetic-connector.connector-right {
    right: -6px;
}

.magnetic-connector.connector-top {
    top: -6px;
}

.magnetic-connector.connector-bottom {
    bottom: -6px;
}

/* Default centering for connectors without custom offset */
.magnetic-connector.connector-left:not([style*="top"]),
.magnetic-connector.connector-right:not([style*="top"]) {
    top: 50%;
    transform: translateY(-50%);
}

.magnetic-connector.connector-top:not([style*="left"]),
.magnetic-connector.connector-bottom:not([style*="left"]) {
    left: 50%;
    transform: translateX(-50%);
}

/* ============================================
   Wire Styles
   ============================================ */
#wire-layer {
    z-index: 200;
    pointer-events: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.wire {
    fill: none;
    stroke: #00ff88;
    stroke-width: 3;
    stroke-linecap: round;
    pointer-events: auto;
    cursor: grab;
}

.wire:hover {
    stroke-width: 4 !important;
}

.wire:active {
    cursor: grabbing;
}

.wire.temp-wire {
    stroke: var(--accent-primary);
    stroke-dasharray: 8, 4;
    stroke-width: 2.5;
    opacity: 0.6;
    pointer-events: none;
    animation: dash 0.5s linear infinite;
}

/* Wire Waypoints */
.wire-waypoint {
    pointer-events: auto;
    cursor: grab;
    transition: r 0.15s ease, filter 0.15s ease;
}

.wire-waypoint:hover {
    r: 8 !important;
    filter: drop-shadow(0 0 8px rgba(255, 102, 153, 0.9)) !important;
}

.wire-waypoint:active {
    cursor: grabbing;
}

@keyframes dash {
    to {
        stroke-dashoffset: -12;
    }
}

/* ============================================
   Wire Tooltip
   ============================================ */
.wire-tooltip {
    position: fixed;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: 8px 12px;
    z-index: 10000;
    pointer-events: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(0, 240, 255, 0.2);
    display: none;
    font-size: 12px;
    min-width: 100px;
}

.tooltip-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 2px 0;
}

.tooltip-label {
    font-size: 14px;
    opacity: 0.8;
}

.tooltip-value {
    font-family: 'Courier New', monospace;
    font-weight: 600;
    color: var(--accent-primary);
}

/* ============================================
   Context Menu
   ============================================ */
.context-menu {
    position: fixed;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(0, 240, 255, 0.1);
    padding: 4px;
    z-index: 1000;
    min-width: 150px;
    display: none;
}

.context-menu.active {
    display: block;
}

.context-menu-item {
    padding: 8px 12px;
    color: var(--text-primary);
    font-size: 13px;
    cursor: pointer;
    border-radius: var(--border-radius-sm);
    display: flex;
    align-items: center;
    gap: 10px;
    transition: all var(--transition-fast);
}

.context-menu-item:hover {
    background: var(--bg-tertiary);
}

.context-menu-item.danger {
    color: var(--accent-danger);
}

.context-menu-item.danger:hover {
    background: rgba(255, 51, 102, 0.1);
}

.context-menu-item .icon {
    font-size: 14px;
    width: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ============================================
   Scrollbar Styles
   ============================================ */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--border-color);
}

/* ============================================
   Responsive Adjustments
   ============================================ */
@media (max-width: 768px) {
    .app-sidebar {
        width: 200px;
    }
    
    .app-header {
        padding: 0 16px;
    }
    
    .control-btn span:not(.icon) {
        display: none;
    }
    
    .speed-control label {
        display: none;
    }
}

/* ============================================
   Utility Classes
   ============================================ */
.hidden {
    display: none !important;
}

.no-select {
    user-select: none;
}

/* ============================================
   Skeleton Layer (imported skeletons)
   ============================================ */
.skeleton-layer {
    pointer-events: auto;
    cursor: move;
    transition: box-shadow 0.2s;
}

.skeleton-layer.selected {
    outline: 2px dashed rgba(0, 240, 255, 0.5);
    outline-offset: 4px;
}

.skeleton-layer.dragging {
    opacity: 0.9;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.4);
}

/* Module selection indicator */
.module.selected {
    outline: 2px solid rgba(0, 240, 255, 0.6);
    outline-offset: 2px;
}

/* Skeleton blocks - plastic and cable */
.skeleton-block {
    border-radius: 2px;
    opacity: 0.8;
}

.skeleton-plastic {
    background: rgba(255, 255, 255, 0.25);
    border: 1px solid rgba(255, 255, 255, 0.4);
}

.skeleton-cable {
    background: rgba(255, 165, 0, 0.35);
    border: 1px solid rgba(255, 165, 0, 0.5);
}

/* Skeleton Magnetic Connectors - styled like module connectors */
.skeleton-magnetic-connector {
    background: var(--bg-primary, #0a0a0f);
    border: 2px solid var(--accent-warning, #ffaa00);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.9;
    box-shadow: 0 0 10px rgba(255, 165, 0, 0.5);
    animation: skeleton-magnet-pulse 2s ease-in-out infinite;
}

.skeleton-magnet-dot {
    width: 4px;
    height: 4px;
    background: var(--accent-warning, #ffaa00);
    border-radius: 50%;
}

/* Connected state for skeleton magnets */
.skeleton-magnetic-connector.connected {
    border-color: var(--accent-success, #00ff88);
    background: rgba(0, 255, 136, 0.15);
    box-shadow: 0 0 15px rgba(0, 255, 136, 0.6);
    opacity: 1;
    animation: none;
}

.skeleton-magnetic-connector.connected .skeleton-magnet-dot {
    background: var(--accent-success, #00ff88);
}

/* Powered state for skeleton magnets (receiving power through cable network) */
.skeleton-magnetic-connector.powered {
    border-color: var(--accent-success, #00ff88);
    background: rgba(0, 255, 136, 0.2);
    box-shadow: 0 0 20px rgba(0, 255, 136, 0.8);
    opacity: 1;
    animation: skeleton-magnet-powered 1s ease-in-out infinite;
}

.skeleton-magnetic-connector.powered .skeleton-magnet-dot {
    background: var(--accent-success, #00ff88);
    box-shadow: 0 0 6px rgba(0, 255, 136, 1);
}

@keyframes skeleton-magnet-powered {
    0%, 100% {
        box-shadow: 0 0 15px rgba(0, 255, 136, 0.6);
    }
    50% {
        box-shadow: 0 0 25px rgba(0, 255, 136, 1);
    }
}

@keyframes skeleton-magnet-pulse {
    0%, 100% {
        box-shadow: 0 0 8px rgba(255, 165, 0, 0.4);
        border-color: var(--accent-warning, #ffaa00);
    }
    50% {
        box-shadow: 0 0 20px rgba(255, 165, 0, 0.8);
        border-color: rgba(255, 165, 0, 1);
    }
}

/* ==================== Server Status Indicator ==================== */

.server-status {
    display: flex;
    align-items: center;
    padding: 0 8px;
    cursor: default;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    transition: background 0.3s;
}

.status-dot.online {
    background: #00ff88;
    box-shadow: 0 0 6px rgba(0, 255, 136, 0.5);
}

.status-dot.offline {
    background: #666;
}

/* ==================== User Indicator ==================== */

.user-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 11px;
    color: #888;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.06);
    cursor: default;
    white-space: nowrap;
}

.user-indicator.logged-in {
    color: #00ff88;
    background: rgba(0, 255, 136, 0.06);
    border-color: rgba(0, 255, 136, 0.15);
}

.user-icon {
    font-size: 13px;
}

.user-label {
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ==================== Modal Styles ==================== */

.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10000;
}

.modal-content {
    background: var(--bg-secondary, #1e293b);
    border-radius: 12px;
    width: 500px;
    max-width: 90vw;
    max-height: 70vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-header h3 {
    margin: 0;
    color: #fff;
}

.modal-close {
    background: none;
    border: none;
    color: #fff;
    font-size: 24px;
    cursor: pointer;
    padding: 0 4px;
}

.modal-body {
    padding: 16px 20px;
    overflow-y: auto;
    color: #ccc;
}

.circuit-list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    margin-bottom: 8px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.08);
}

.circuit-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.circuit-info strong {
    color: #fff;
}

.circuit-info small {
    color: #888;
}

.circuit-actions {
    display: flex;
    gap: 8px;
}

.circuit-actions button {
    padding: 6px 14px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
}

.btn-load-circuit {
    background: #00ff88;
    color: #1a1a2e;
}

.btn-load-circuit:hover {
    background: #00cc6a;
}

.btn-delete-circuit {
    background: rgba(255, 60, 60, 0.2);
    color: #ff6666;
    border: 1px solid rgba(255, 60, 60, 0.3) !important;
}

.btn-delete-circuit:hover {
    background: rgba(255, 60, 60, 0.4);
}

/* Modal toolbar with action buttons */
.modal-toolbar {
    display: flex;
    gap: 10px;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-action-btn {
    padding: 8px 16px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.08);
    color: #ccc;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}

.modal-action-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}

.modal-action-btn.primary {
    background: rgba(0, 255, 136, 0.15);
    color: #00ff88;
    border-color: rgba(0, 255, 136, 0.3);
}

.modal-action-btn.primary:hover {
    background: rgba(0, 255, 136, 0.3);
}

.btn-download-item {
    background: rgba(100, 180, 255, 0.15);
    color: #64b4ff;
    border: 1px solid rgba(100, 180, 255, 0.25) !important;
    font-size: 14px;
    padding: 4px 10px;
    line-height: 1;
}

.btn-download-item:hover {
    background: rgba(100, 180, 255, 0.3);
}
