:root {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-card: rgba(22, 33, 62, 0.7);
    --bg-card-solid: #16213e;
    --accent: #4a9eff;
    --accent-hover: #3a7fd9;
    --accent-glow: rgba(74, 158, 255, 0.3);
    --text: #e0e0e0;
    --text-dim: #8892a0;
    --danger: #ff4757;
    --success: #2ed573;
    --border: rgba(255, 255, 255, 0.1);
    --glass-border: rgba(255, 255, 255, 0.05);
    
    --shadow-sm: 0 4px 6px rgba(0, 0, 0, 0.1);
    --shadow-md: 0 10px 15px rgba(0, 0, 0, 0.2);
    --shadow-lg: 0 20px 25px rgba(0, 0, 0, 0.3);
    --shadow-glow: 0 0 15px var(--accent-glow);
    
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --font-main: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

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

body {
    font-family: var(--font-main);
    background: radial-gradient(circle at center, var(--bg-secondary) 0%, var(--bg-primary) 100%);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

.hidden {
    display: none !important;
}

.glass {
    background: var(--bg-card);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    box-shadow: var(--shadow-lg);
}

/* Typography */
h1, h2, h3, h4, h5, h6 {
    color: #ffffff;
    font-weight: 600;
}

/* Forms & Inputs */
input, textarea, select {
    width: 100%;
    padding: 10px 12px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-family: inherit;
    font-size: 14px;
    transition: var(--transition);
}

input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow);
}

.input-group {
    margin-bottom: 16px;
}

.input-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--text-dim);
    font-weight: 500;
}

/* Buttons */
button {
    cursor: pointer;
    font-family: inherit;
    border: none;
    transition: var(--transition);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent), #684aff);
    color: white;
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: 500;
    width: 100%;
    box-shadow: var(--shadow-glow);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 20px rgba(104, 74, 255, 0.4);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-outline {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
    padding: 8px 16px;
    border-radius: 6px;
    font-size: 13px;
}

.btn-outline:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(74, 158, 255, 0.1);
}

.btn-danger {
    background: rgba(255, 71, 87, 0.1);
    color: var(--danger);
    border: 1px solid rgba(255, 71, 87, 0.3);
    padding: 8px 16px;
    border-radius: 6px;
}

.btn-danger:hover {
    background: var(--danger);
    color: white;
}

.btn-icon {
    background: transparent;
    color: var(--text-dim);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

/* Loader */
#global-loader {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--bg-primary);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255,255,255,0.1);
    border-radius: 50%;
    border-top-color: var(--accent);
    animation: spin 1s ease-in-out infinite;
}

/* Login Screen */
#login-screen {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.login-card {
    width: 100%;
    max-width: 400px;
    padding: 40px;
    border-radius: 16px;
    text-align: center;
    animation: slideUp 0.5s ease-out;
}

.login-card p {
    color: var(--text-dim);
    margin-bottom: 30px;
}

.error-msg {
    color: var(--danger);
    font-size: 13px;
    margin-bottom: 15px;
    animation: shake 0.4s ease-in-out;
}

/* App Layout */
#app-screen {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Top Bar */
.top-bar {
    height: 56px;
    background: var(--bg-card-solid);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 20px;
    z-index: 100;
}

.logo {
    font-weight: 700;
    font-size: 18px;
    margin-right: 30px;
    background: linear-gradient(to right, var(--accent), #a855f7);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.tabs-container {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none; /* Firefox */
}

.tabs-container::-webkit-scrollbar {
    display: none;
}

.tab {
    padding: 6px 16px;
    border-radius: 20px;
    background: rgba(255, 255, 255, 0.05);
    color: var(--text-dim);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    user-select: none;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

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

.tab.active {
    background: var(--accent-glow);
    color: var(--accent);
    border: 1px solid rgba(74, 158, 255, 0.5);
    box-shadow: 0 0 10px rgba(74, 158, 255, 0.2);
}

.tab .tab-close {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    opacity: 0;
    transition: var(--transition);
}

.tab:hover .tab-close {
    opacity: 1;
}

.tab .tab-close:hover {
    background: rgba(255, 71, 87, 0.8);
    color: white;
}

.top-bar-actions {
    display: flex;
    gap: 12px;
    margin-left: 20px;
}

/* Canvas Area */
.canvas-wrapper {
    flex: 1;
    position: relative;
    overflow: hidden;
    background-image: radial-gradient(rgba(255, 255, 255, 0.1) 1px, transparent 1px);
    background-size: 20px 20px;
    background-position: 0 0;
}

#canvas-container {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    transform-origin: 0 0;
    /* transition: transform 0.1s; /* smooth zoom - can be laggy */
}

#connections-layer {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 1;
    overflow: visible;
}

#nodes-layer {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 2;
}

/* Nodes */
.node {
    position: absolute;
    width: 240px;
    background: var(--bg-card);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border-radius: 12px;
    border: 1px solid var(--glass-border);
    border-left: 4px solid var(--node-color, var(--accent));
    box-shadow: var(--shadow-sm);
    pointer-events: auto;
    cursor: grab;
    user-select: none;
    transition: box-shadow 0.2s, border-color 0.2s;
    animation: fadeIn 0.3s ease-out;
}

.node:hover {
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3), 0 0 10px rgba(255, 255, 255, 0.05);
}

.node:active {
    cursor: grabbing;
    opacity: 0.9;
}

.node-header {
    padding: 12px 16px 8px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.node-title {
    font-size: 14px;
    font-weight: 600;
    color: white;
    word-break: break-word;
    width: 100%;
}

.node-actions {
    position: absolute;
    top: -28px;
    right: 0;
    display: flex;
    gap: 6px;
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 10;
}

.node:hover .node-actions {
    opacity: 1;
}

.node-btn {
    width: 24px;
    height: 24px;
    border-radius: 6px;
    background: var(--bg-card-solid);
    color: var(--text-dim);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    box-shadow: var(--shadow-sm);
}

.node-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

.node-btn.delete:hover {
    background: var(--danger);
}

.node-body {
    padding: 0 16px 12px;
}

.node-desc {
    font-size: 12px;
    color: var(--text-dim);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.port {
    width: 12px;
    height: 12px;
    background: var(--bg-secondary);
    border: 2px solid var(--node-color, var(--accent));
    border-radius: 50%;
    position: absolute;
    cursor: crosshair;
    transition: transform 0.2s, background 0.2s;
    z-index: 10;
}

.port:hover {
    transform: scale(1.3);
    background: var(--node-color, var(--accent));
}

.port-top { top: -6px; left: 50%; transform: translateX(-50%); }
.port-top:hover { transform: translateX(-50%) scale(1.3); }

.port-bottom { bottom: -6px; left: 50%; transform: translateX(-50%); }
.port-bottom:hover { transform: translateX(-50%) scale(1.3); }

.port-left { left: -6px; top: 50%; transform: translateY(-50%); }
.port-left:hover { transform: translateY(-50%) scale(1.3); }

.port-right { right: -6px; top: 50%; transform: translateY(-50%); }
.port-right:hover { transform: translateY(-50%) scale(1.3); }

.custom-out { right: -6px; transform: translateY(-50%); }
.custom-out:hover { transform: translateY(-50%) scale(1.3); }

.port-label {
    position: absolute;
    left: 18px; /* Moved outside to the right to avoid overlapping node title */
    top: 50%;
    transform: translateY(-50%);
    font-size: 11px;
    color: var(--text);
    white-space: nowrap;
    background: var(--bg-card-solid);
    padding: 2px 6px;
    border-radius: 4px;
    pointer-events: none;
    border: 1px solid var(--glass-border);
    z-index: 20;
    box-shadow: var(--shadow-sm);
}

/* SVG Lines */
.connection-line {
    fill: none;
    stroke: var(--text-dim);
    stroke-width: 2;
    transition: stroke 0.3s;
    pointer-events: auto;
    cursor: pointer;
}

.connection-line:hover {
    stroke: var(--danger);
    stroke-width: 3;
}

.connection-line.temp {
    stroke: var(--accent);
    stroke-dasharray: 5, 5;
    pointer-events: none;
}

/* Controls & FAB */
.canvas-controls {
    position: absolute;
    bottom: 20px;
    left: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--bg-card);
    padding: 8px;
    border-radius: 12px;
    border: 1px solid var(--border);
    backdrop-filter: blur(8px);
}

.fab {
    position: absolute;
    bottom: 30px;
    right: 30px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent), #a855f7);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 15px var(--accent-glow);
    z-index: 100;
    transition: transform 0.3s, box-shadow 0.3s;
    animation: pulse 2s infinite;
}

.fab:hover {
    transform: scale(1.1);
    box-shadow: 0 8px 25px rgba(74, 158, 255, 0.6);
    animation: none;
}

.fab:active {
    transform: scale(0.95);
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    transform: translateY(20px) scale(0.95);
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-overlay.active .modal-content {
    transform: translateY(0) scale(1);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid var(--border);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
}

.modal-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* Color Picker */
.color-picker {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 8px;
}

.color-option {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid transparent;
    transition: transform 0.2s;
}

.color-option:hover {
    transform: scale(1.1);
}

.color-option.selected {
    border-color: white;
    box-shadow: 0 0 0 2px var(--bg-card), 0 0 0 4px var(--accent);
}

/* Admin Table */
.admin-table {
    width: 100%;
    border-collapse: collapse;
}

.admin-table th, .admin-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

.admin-table th {
    font-size: 12px;
    text-transform: uppercase;
    color: var(--text-dim);
}

.badge {
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
}

.badge.admin {
    background: rgba(168, 85, 247, 0.2);
    color: #c084fc;
}

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

@keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    50% { transform: translateX(5px); }
    75% { transform: translateX(-5px); }
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(74, 158, 255, 0.4); }
    70% { box-shadow: 0 0 0 15px rgba(74, 158, 255, 0); }
    100% { box-shadow: 0 0 0 0 rgba(74, 158, 255, 0); }
}

/* Responsive */
@media (max-width: 768px) {
    .top-bar {
        padding: 0 10px;
    }
    
    .logo {
        margin-right: 15px;
        font-size: 16px;
    }
    
    .fab {
        bottom: 20px;
        right: 20px;
        width: 50px;
        height: 50px;
    }
    
    .modal-content {
        height: 100%;
        max-height: 100%;
        border-radius: 0;
    }
}

/* Flow Animation */
.connection-line.flow-anim {
    stroke-dasharray: 8, 8;
    animation: flowLineAnimation 0.8s linear infinite;
    stroke: var(--accent);
    filter: drop-shadow(0 0 4px var(--accent-glow));
}

@keyframes flowLineAnimation {
    to {
        stroke-dashoffset: -16;
    }
}

/* Selection and Multi-select */
.selection-box {
    position: absolute;
    border: 1px solid var(--accent);
    background: rgba(74, 158, 255, 0.15);
    pointer-events: none;
    z-index: 9999;
}

.node.selected {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-glow), var(--shadow-lg);
}

.connection-line.selected {
    stroke: var(--accent) !important;
    filter: drop-shadow(0 0 6px var(--accent-glow));
}
