:root {
    --bg-body: #f1f5f9;
    --bg-sidebar: #ffffff;
    --bg-panel: #ffffff;
    --bg-input: #f8fafc;
    --bg-hover: #f1f5f9;
    --border-color: #e2e8f0;
    
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --secondary: #64748b;
    --secondary-hover: #475569;
    --danger: #ef4444;
    --success: #10b981;
    --warning: #f59e0b;
    
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.5;
}

/* Layout */
#app {
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    display: flex;
}

.hidden { display: none !important; }

/* Auth View */
.auth-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f1f5f9 0%, #cbd5e1 100%);
}

.auth-card {
    background: var(--bg-panel);
    padding: 2rem;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 400px;
    border: 1px solid var(--border-color);
}

.auth-header { text-align: center; margin-bottom: 2rem; }
.auth-header h1 { margin: 0; font-size: 1.5rem; color: var(--primary); }
.auth-header p { margin: 0.5rem 0 0; color: var(--text-secondary); }

.auth-actions { margin-top: 1.5rem; display: flex; flex-direction: column; gap: 0.5rem; }

/* Main Layout */
.layout {
    display: flex;
    width: 100%;
    height: 100%;
}

.sidebar {
    width: 240px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.logo {
    height: 60px;
    display: flex;
    align-items: center;
    padding: 0 1.5rem;
    font-weight: bold;
    font-size: 1.25rem;
    color: var(--primary);
    border-bottom: 1px solid var(--border-color);
}
.logo-icon {
    background: var(--primary);
    color: white;
    padding: 2px 6px;
    border-radius: 4px;
    margin-right: 8px;
    font-size: 0.9rem;
}

.nav-menu {
    flex: 1;
    padding: 1rem 0;
    overflow-y: auto;
}

.nav-item {
    display: flex;
    align-items: center;
    padding: 0.75rem 1.5rem;
    color: var(--text-secondary);
    text-decoration: none;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}
.nav-item:hover { background: rgba(0,0,0,0.05); color: var(--text-primary); }
.nav-item.active { background: rgba(59, 130, 246, 0.1); color: var(--primary); border-left-color: var(--primary); }
.nav-item .icon { margin-right: 10px; width: 20px; text-align: center; }

.user-profile {
    padding: 1rem;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.user-info { display: flex; align-items: center; gap: 10px; }
.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 12px;
    background: linear-gradient(135deg, #60a5fa 0%, var(--primary) 60%, #2563eb 100%);
    border: 1px solid rgba(37, 99, 235, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    color: white;
    box-shadow: 0 4px 10px rgba(37, 99, 235, 0.18);
}
.user-details { display: flex; flex-direction: column; }
#user-name { font-weight: 500; font-size: 0.9rem; }
#user-role { font-size: 0.7rem; }

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-body);
}

.top-bar {
    height: 60px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-body);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    flex-shrink: 0;
}
.top-bar h2 { margin: 0; font-size: 1.1rem; font-weight: 600; }

#content-area {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
}

/* Common Components */
.view-section { height: 100%; display: flex; flex-direction: column; }

.panel {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(0,0,0,0.02);
}
.panel-header h3 { margin: 0; font-size: 1rem; font-weight: 600; }

.panel-body { padding: 1.5rem; flex: 1; overflow-y: auto; }
.panel-footer { padding: 1rem 1.5rem; border-top: 1px solid var(--border-color); background: #f8fafc; }

.no-padding { padding: 0 !important; }
.mt-4 { margin-top: 1rem; }

/* Grid System */
.grid-layout { display: flex; gap: 1.5rem; height: 100%; }
.col-4 { flex: 4; display: flex; flex-direction: column; }
.col-6 { flex: 6; display: flex; flex-direction: column; }
.col-8 { flex: 8; display: flex; flex-direction: column; }

.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
.stat-card {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    padding: 1.5rem;
    border-radius: var(--radius-md);
}
.stat-label { color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 0.5rem; }
.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--primary); }

/* Forms */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; margin-bottom: 0.5rem; color: var(--text-secondary); font-size: 0.9rem; }
.help-text { font-size: 0.8rem; color: var(--text-muted); margin-bottom: 0.5rem; }

input, select, textarea {
    width: 100%;
    padding: 0.6rem 0.8rem;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); }
textarea.code-editor { font-family: ui-monospace, monospace; font-size: 0.9rem; line-height: 1.4; background: #1e293b; color: #f8fafc; }

.form-row { display: flex; gap: 1rem; }
.form-row .col { flex: 1; }
.form-check { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; }
.form-check input { width: auto; }
.form-check label { margin: 0; cursor: pointer; }
.input-group { display: flex; gap: 0.5rem; }

/* Buttons */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 0.5rem 1rem;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s;
    background: transparent;
    color: var(--text-primary);
}
.btn-sm { padding: 0.25rem 0.75rem; font-size: 0.85rem; }
.btn-block { width: 100%; }

.btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: var(--secondary); color: white; border-color: var(--secondary); }
.btn-secondary:hover { background: var(--secondary-hover); }

.btn-danger { background: rgba(239, 68, 68, 0.1); color: var(--danger); border-color: rgba(239, 68, 68, 0.3); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.2); }

.btn-text { color: var(--text-secondary); }
.btn-text:hover { color: var(--text-primary); text-decoration: underline; }

.btn-icon {
    background: #ffffff;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
}
.btn-icon:hover {
    background: var(--bg-hover);
    color: var(--primary);
    border-color: rgba(59, 130, 246, 0.35);
}
#btn-logout.btn-icon {
    width: 36px;
    height: 36px;
    border-radius: 999px;
}
#btn-logout.btn-icon:hover {
    background: rgba(239, 68, 68, 0.08);
    border-color: rgba(239, 68, 68, 0.35);
    color: #dc2626;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* List Group */
.list-group { display: flex; flex-direction: column; }
.list-item {
    padding: 0.8rem 1rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.1s;
}
.list-item:hover { background: var(--bg-hover); }
.list-item.active { background: rgba(59, 130, 246, 0.1); border-left: 3px solid var(--primary); padding-left: calc(1rem - 3px); }
.list-item-title { font-weight: 500; margin-bottom: 0.2rem; }
.list-item-subtitle { font-size: 0.8rem; color: var(--text-muted); }

/* Table */
.table-responsive { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 0.8rem 1rem; text-align: left; border-bottom: 1px solid var(--border-color); }
.table th { color: var(--text-secondary); font-weight: 500; font-size: 0.85rem; background: rgba(0,0,0,0.03); }
.table tr:hover { background: var(--bg-hover); }

/* Badge */
.badge {
    display: inline-block;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 700;
    background: rgba(100, 116, 139, 0.10);
    color: #475569;
    border: 1px solid rgba(100, 116, 139, 0.18);
}
.badge-xs { font-size: 0.7rem; padding: 0.1rem 0.45rem; }
.badge.role-admin {
    background: rgba(59, 130, 246, 0.12);
    color: #2563eb;
    border-color: rgba(59, 130, 246, 0.25);
}
.badge.role-user {
    background: rgba(100, 116, 139, 0.10);
    color: #475569;
    border-color: rgba(100, 116, 139, 0.18);
}
.badge.status-running { background: rgba(59, 130, 246, 0.2); color: #2563eb; }
.badge.status-waiting { background: rgba(245, 158, 11, 0.2); color: #d97706; }
.badge.status-done { background: rgba(16, 185, 129, 0.2); color: #059669; }
.badge.status-failed { background: rgba(239, 68, 68, 0.2); color: #dc2626; }
.badge.status-paused { background: rgba(100, 116, 139, 0.2); color: #475569; }

/* Empty State */
.empty-state { padding: 3rem; text-align: center; color: var(--text-muted); font-style: italic; }

/* Modal */
.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 1000; display: flex; align-items: center; justify-content: center; }
.modal-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.3); backdrop-filter: blur(2px); }
.modal-content {
    position: relative;
    background: var(--bg-panel);
    width: 100%;
    max-width: 500px;
    max-height: 90vh;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}
.modal-lg { max-width: 900px; height: 80vh; }
.modal-header { padding: 1rem 1.5rem; border-bottom: 1px solid var(--border-color); display: flex; align-items: center; justify-content: space-between; }
.modal-header h3 { margin: 0; }
.modal-body { padding: 1.5rem; overflow-y: auto; }
.modal-footer { padding: 1rem 1.5rem; border-top: 1px solid var(--border-color); display: flex; justify-content: flex-end; gap: 0.8rem; }
.btn-close { background: none; border: none; font-size: 1.5rem; color: var(--text-secondary); cursor: pointer; line-height: 1; }
.btn-close:hover { color: var(--text-primary); }

.file-drop-zone {
    border: 2px dashed var(--border-color);
    border-radius: var(--radius-md);
    padding: 2rem;
    text-align: center;
    color: var(--text-secondary);
    cursor: pointer;
    position: relative;
    transition: all 0.2s;
}
.file-drop-zone:hover { border-color: var(--primary); background: rgba(59, 130, 246, 0.05); }
.file-drop-zone input { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }

/* Logs */
.log-container { flex: 1; background: #0f172a; padding: 1rem; overflow: auto; font-family: monospace; font-size: 0.85rem; color: #d1d5db; white-space: pre-wrap; word-break: break-all; }
.display-flex-col { display: flex; flex-direction: column; height: 100%; }
.detail-toolbar { padding: 0.8rem 1rem; border-bottom: 1px solid var(--border-color); display: flex; align-items: center; justify-content: space-between; background: rgba(0,0,0,0.03); }
.info-group { display: flex; gap: 1rem; font-size: 0.9rem; color: var(--text-secondary); }
.info-group b { color: var(--text-primary); }

/* Form Msg */
.form-msg { font-size: 0.9rem; margin-top: 0.5rem; min-height: 1.2em; }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }

/* ========== Template Library Layout ========== */
.tpl-container {
    display: flex;
    height: 100%;
    overflow: hidden;
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
}

.tpl-sidebar {
    width: 260px;
    background: #fff;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.tpl-sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.tpl-sidebar-header h3 { margin: 0; font-size: 1rem; }

.tpl-list {
    flex: 1;
    overflow-y: auto;
}

.tpl-list-item {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.1s;
}
.tpl-list-item:hover { background: var(--bg-hover); }
.tpl-list-item.active {
    background: rgba(59, 130, 246, 0.08);
    border-left: 3px solid var(--primary);
}
.tpl-item-name { font-weight: 500; font-size: 0.9rem; margin-bottom: 4px; }
.tpl-item-meta { display: flex; justify-content: space-between; color: var(--text-muted); font-size: 0.75rem; }

.tpl-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: #fff;
}

.tpl-header {
    padding: 10px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fff;
    height: 60px;
    flex-shrink: 0;
}
.tpl-title-edit { display: flex; align-items: center; gap: 10px; }
.tpl-actions { display: flex; gap: 8px; }

.tpl-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Config Panel */
.config-panel {
    padding: 16px 20px;
    background: var(--bg-input);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    gap: 20px;
    flex-shrink: 0;
}

.config-group h4 {
    margin: 0 0 8px 0;
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.field-container {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    min-height: 32px;
}

.field-chip {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 2px 8px;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--primary);
}
.field-chip .remove {
    cursor: pointer;
    color: var(--text-muted);
    font-size: 1.1rem;
    line-height: 1;
}
.field-chip .remove:hover { color: var(--danger); }

/* Chat Interface */
.chat-interface {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    background: #f8fafc;
}

.chat-workspace {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.msg-row {
    display: flex;
    gap: 12px;
    max-width: 85%;
    width: auto;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.msg-row.user {
    margin-left: auto;
    flex-direction: row-reverse;
}

.msg-row.system {
    margin: 0 auto;
    max-width: 600px;
    justify-content: center;
    width: 100%;
}

.msg-row.assistant {
    margin-right: auto;
}

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.msg-row.user .msg-avatar {
    background: #3b82f6;
    color: white;
}

.msg-row.assistant .msg-avatar {
    background: #10b981;
    color: white;
}

.msg-row.system .msg-avatar {
    background: #64748b;
    color: white;
    font-size: 1rem;
}

.msg-content-wrapper {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
    max-width: 100%;
    align-items: flex-start;
}

.msg-row.user .msg-content-wrapper {
    align-items: flex-end;
}

.msg-row.system .msg-content-wrapper {
    align-items: center;
    width: 100%;
}

.msg-bubble {
    padding: 8px 12px;
    border-radius: 10px;
    font-size: 0.9rem;
    line-height: 1.5;
    white-space: pre-line;
    position: relative;
    border: 1px solid transparent;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    word-break: break-word;
    min-width: 20px;
}

.msg-bubble:hover {
    box-shadow: 0 4px 6px rgba(0,0,0,0.05);
    transform: translateY(-1px);
}

.msg-bubble.editing {
    outline: 2px solid var(--primary);
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1);
    z-index: 2;
}

.msg-row.user .msg-bubble {
    background: #3b82f6;
    color: white;
    border-top-right-radius: 2px;
}

.msg-row.assistant .msg-bubble {
    background: #ffffff;
    border-color: #e2e8f0;
    color: #1e293b;
    border-top-left-radius: 2px;
}

.msg-row.system .msg-bubble {
    background: #f1f5f9;
    border: 1px solid #cbd5e1;
    color: #475569;
    font-size: 0.85rem;
    padding: 8px 16px;
    text-align: center;
}

.msg-meta {
    display: flex;
    gap: 8px;
    font-size: 0.75rem;
    color: var(--text-muted);
    opacity: 0;
    transition: opacity 0.2s;
    align-items: center;
    height: 24px;
    padding: 0 4px;
}

.msg-row:hover .msg-meta {
    opacity: 1;
}

/* Actions in meta */
.msg-meta button {
    padding: 2px;
    border-radius: 4px;
    color: var(--text-muted);
}
.msg-meta button:hover {
    background: #e2e8f0;
    color: var(--text-primary);
}

.chat-input-zone {
    border-top: 1px solid var(--border-color);
    padding: 20px 24px;
    background: #ffffff;
    flex-shrink: 0;
    box-shadow: 0 -4px 12px rgba(0,0,0,0.03);
    z-index: 10;
}

.chat-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.role-switcher {
    display: flex;
    background: #f1f5f9;
    padding: 3px;
    border-radius: 8px;
}

.role-btn {
    padding: 6px 16px;
    font-size: 0.85rem;
    border: none;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    color: #64748b;
    font-weight: 500;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 6px;
}

.role-btn:hover {
    color: #334155;
    background: rgba(255,255,255,0.5);
}

.role-btn.active {
    background: #ffffff;
    color: var(--primary);
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.role-btn.active[data-role="user"] { color: #2563eb; }
.role-btn.active[data-role="assistant"] { color: #059669; }
.role-btn.active[data-role="system"] { color: #475569; }

.input-wrapper {
    position: relative;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: #fff;
    transition: all 0.2s;
    margin-bottom: 12px;
    overflow: hidden;
}

.input-wrapper:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.chat-textarea {
    width: 100%;
    border: none;
    background: transparent;
    padding: 16px;
    resize: none;
    height: 100px;
    font-size: 0.95rem;
    outline: none;
    line-height: 1.5;
    font-family: inherit;
}

.input-actions {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
}

.msg-var {
    color: var(--primary);
    font-family: monospace;
    background: rgba(59, 130, 246, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.85em;
}

/* Test Modal Split View */
.test-modal-body {
    flex: 1;
    display: flex;
    overflow: hidden;
    height: 100%;
}

.test-config-sidebar {
    width: 320px;
    padding: 20px;
    border-right: 1px solid var(--border-color);
    background: var(--bg-input);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.test-preview-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #0f172a; /* Dark mode for code */
    color: #e2e8f0;
}

.test-preview-header {
    display: flex;
    background: #1e293b;
    border-bottom: 1px solid #334155;
}

.test-tab {
    padding: 12px 24px;
    cursor: pointer;
    font-size: 0.9rem;
    color: #94a3b8;
    border-bottom: 2px solid transparent;
}
.test-tab:hover { color: #f1f5f9; background: rgba(255,255,255,0.05); }
.test-tab.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    background: rgba(59, 130, 246, 0.1);
}

.test-content-pane {
    flex: 1;
    overflow: auto;
    padding: 20px;
    font-family: monospace;
    font-size: 0.9rem;
}

.var-input-group {
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 8px 12px;
    margin-bottom: 8px;
}
.var-label {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 4px;
    font-family: monospace;
}
.var-input {
    border: none;
    background: transparent;
    padding: 0;
    width: 100%;
    font-size: 0.9rem;
    color: var(--text-primary);
}
.var-input:focus { outline: none; }