/**
 * RAG Pro v2 - Common Styles
 * Design Token System — single source of truth
 * Encoding: UTF-8
 */

/* ==========================================
   DESIGN TOKENS
   ========================================== */
:root {
    /* Brand */
    --color-primary:        #5b6ef5;
    --color-primary-dark:   #4254e8;
    --color-primary-light:  #1e2a4a;
    --color-secondary:      #764ba2;
    --gradient-primary:     linear-gradient(135deg, #5b6ef5 0%, #764ba2 100%);

    /* Semantic */
    --color-success:        #22c55e;
    --color-success-bg:     #052e16;
    --color-danger:         #f87171;
    --color-danger-bg:      #2d0a0a;
    --color-warning:        #fbbf24;
    --color-warning-bg:     #2d1f02;
    --color-info:           #60a5fa;
    --color-info-bg:        #0d2044;

    /* Surfaces — Dark Slate */
    --surface-page:         #0f172a;
    --surface-card:         #1e293b;
    --surface-subtle:       #162032;
    --surface-border:       #334155;
    --surface-border-light: #1e293b;

    /* Text */
    --text-primary:         #f1f5f9;
    --text-secondary:       #94a3b8;
    --text-muted:           #64748b;
    --text-white:           #ffffff;
    --text-on-primary:      #ffffff;

    /* Spacing */
    --sp-1: 4px;   --sp-2: 8px;   --sp-3: 12px;
    --sp-4: 16px;  --sp-5: 20px;  --sp-6: 24px;
    --sp-8: 32px;  --sp-10: 40px; --sp-12: 48px;

    /* Radius */
    --radius-sm:   6px;
    --radius-md:   10px;
    --radius-lg:   14px;
    --radius-xl:   20px;
    --radius-full: 9999px;

    /* Shadows */
    --shadow-xs:  0 1px 2px rgba(0,0,0,.3);
    --shadow-sm:  0 2px 4px rgba(0,0,0,.4);
    --shadow-md:  0 4px 12px rgba(0,0,0,.5);
    --shadow-lg:  0 8px 24px rgba(0,0,0,.6);
    --shadow-xl:  0 20px 40px rgba(0,0,0,.7);
    --shadow-primary: 0 4px 14px rgba(91,110,245,.4);

    /* Typography */
    --font-sans:  'Inter', 'Segoe UI', system-ui, sans-serif;
    --font-mono:  'JetBrains Mono', 'Fira Code', 'Courier New', monospace;

    /* Transitions */
    --ease-fast:   0.15s ease;
    --ease-normal: 0.25s ease;

    /* Layout */
    --sidebar-width:     220px;
    --header-height:     58px;
    --content-max-width: 1280px;
}

/* ==========================================
   RESET
   ========================================== */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }
html { font-size: 15px; -webkit-font-smoothing: antialiased; scroll-behavior: smooth; }
body {
    font-family: var(--font-sans);
    background: var(--surface-page);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
}
h1,h2,h3,h4,h5,h6 { font-weight: 600; line-height: 1.3; color: var(--text-primary); }
h1 { font-size: 22px; } h2 { font-size: 18px; } h3 { font-size: 16px; } h4 { font-size: 14px; }
p { color: var(--text-secondary); line-height: 1.6; }
a { color: var(--color-primary); text-decoration: none; transition: color var(--ease-fast); }
a:hover { color: var(--color-primary-dark); }

/* ==========================================
   APP SHELL LAYOUT (sidebar + content)
   ========================================== */
.app-shell {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
}

/* HEADER */
.app-header {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--header-height);
    background: var(--gradient-primary);
    color: var(--text-white);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--sp-6);
    box-shadow: var(--shadow-md);
    z-index: 1000;
}
.app-header .logo {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    font-size: 17px;
    font-weight: 700;
    color: var(--text-white);
    text-decoration: none;
}
.app-header .logo svg { width: 22px; height: 22px; flex-shrink: 0; }
.header-right {
    display: flex;
    align-items: center;
    gap: var(--sp-4);
}
.user-chip {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    background: rgba(255,255,255,0.18);
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--radius-full);
    font-size: 13px;
}
.user-chip .avatar {
    width: 26px; height: 26px;
    background: rgba(255,255,255,0.4);
    border-radius: var(--radius-full);
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 12px;
}
.user-chip .role-tag {
    background: rgba(255,215,0,0.25);
    color: #ffd700;
    padding: 1px 6px;
    border-radius: var(--radius-full);
    font-size: 10px; font-weight: 700;
}
.btn-logout {
    background: rgba(255,255,255,0.18);
    color: var(--text-white);
    border: 1px solid rgba(255,255,255,0.3);
    padding: var(--sp-2) var(--sp-4);
    border-radius: var(--radius-md);
    font-size: 13px; cursor: pointer;
    transition: all var(--ease-fast);
    font-family: inherit;
    font-weight: 500;
}
.btn-logout:hover { background: rgba(255,255,255,0.3); }

/* SIDEBAR */
.app-sidebar {
    position: fixed;
    top: var(--header-height); left: 0;
    width: var(--sidebar-width);
    height: calc(100vh - var(--header-height));
    background: var(--surface-card);
    border-right: 1px solid var(--surface-border);
    overflow-y: auto;
    z-index: 900;
    padding: var(--sp-4) 0;
    display: flex;
    flex-direction: column;
}
.sidebar-section { margin-bottom: var(--sp-2); }
.sidebar-label {
    padding: var(--sp-2) var(--sp-5);
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .08em;
    color: var(--text-muted);
}
.sidebar-link {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-2) var(--sp-5);
    font-size: 14px;
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: 0;
    transition: all var(--ease-fast);
    position: relative;
}
.sidebar-link:hover { background: var(--color-primary-light); color: var(--color-primary); }
.sidebar-link.active {
    background: var(--color-primary-light);
    color: var(--color-primary);
    font-weight: 600;
}
.sidebar-link.active::before {
    content: '';
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 3px;
    background: var(--color-primary);
    border-radius: 0 3px 3px 0;
}
.sidebar-link .nav-icon { width: 18px; height: 18px; flex-shrink: 0; opacity: .7; }
.sidebar-link.active .nav-icon { opacity: 1; }
.sidebar-footer {
    margin-top: auto;
    padding: var(--sp-4) var(--sp-5);
    border-top: 1px solid var(--surface-border);
    font-size: 11px;
    color: var(--text-muted);
}

/* MAIN CONTENT AREA */
.app-content {
    margin-left: var(--sidebar-width);
    margin-top: var(--header-height);
    min-height: calc(100vh - var(--header-height));
    padding: var(--sp-8);
}
.page-container {
    max-width: var(--content-max-width);
    margin: 0 auto;
}

/* PAGE HEADER */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--sp-8);
    flex-wrap: wrap;
    gap: var(--sp-4);
}
.page-header h1 { font-size: 22px; }
.page-header p { font-size: 14px; color: var(--text-muted); margin-top: 2px; }

/* ==========================================
   CARDS
   ========================================== */
.card {
    background: var(--surface-card);
    border-radius: var(--radius-lg);
    border: 1px solid var(--surface-border);
    box-shadow: var(--shadow-sm);
    padding: var(--sp-6);
    transition: box-shadow var(--ease-normal), transform var(--ease-normal);
}
.card:hover { box-shadow: var(--shadow-md); }
.card-clickable { cursor: pointer; }
.card-clickable:hover { transform: translateY(-2px); }

.section {
    background: var(--surface-card);
    border-radius: var(--radius-lg);
    border: 1px solid var(--surface-border);
    box-shadow: var(--shadow-sm);
    padding: var(--sp-6);
    margin-bottom: var(--sp-6);
}
.section-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    padding-bottom: var(--sp-4);
    margin-bottom: var(--sp-5);
    border-bottom: 1px solid var(--surface-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--sp-5);
}

/* ==========================================
   STATS GRID
   ========================================== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: var(--sp-5);
    margin-bottom: var(--sp-8);
}
.stat-card {
    background: var(--surface-card);
    border: 1px solid var(--surface-border);
    border-radius: var(--radius-lg);
    padding: var(--sp-5);
    box-shadow: var(--shadow-sm);
}
.stat-card .stat-icon { font-size: 20px; margin-bottom: var(--sp-2); }
.stat-card .stat-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--color-primary);
    margin-bottom: 2px;
    line-height: 1;
}
.stat-card .stat-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }

/* ==========================================
   BUTTONS
   ========================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    padding: 8px 18px;
    border: none;
    border-radius: var(--radius-md);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--ease-normal);
    font-family: inherit;
    text-decoration: none;
    white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: not-allowed; pointer-events: none; }
.btn-primary { background: var(--gradient-primary); color: var(--text-white); }
.btn-primary:hover:not(:disabled) { transform: translateY(-1px); box-shadow: var(--shadow-primary); }
.btn-secondary { background: var(--surface-subtle); color: var(--text-secondary); border: 1px solid var(--surface-border); }
.btn-secondary:hover:not(:disabled) { background: var(--color-primary-light); color: var(--color-primary); border-color: var(--color-primary); }
.btn-danger { background: var(--color-danger); color: var(--text-white); }
.btn-danger:hover:not(:disabled) { background: #b91c1c; }
.btn-ghost { background: transparent; color: var(--text-secondary); border: 1px solid transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--surface-subtle); border-color: var(--surface-border); }
.btn-sm { padding: 5px 12px; font-size: 12px; }
.btn-lg { padding: 12px 24px; font-size: 15px; }
.btn-block { width: 100%; }
.btn-icon { padding: 8px; border-radius: var(--radius-md); }
.btn-icon svg { width: 16px; height: 16px; }

/* ==========================================
   FORMS
   ========================================== */
.form-group { margin-bottom: var(--sp-5); }
.form-group label {
    display: block;
    font-weight: 600;
    font-size: 13px;
    margin-bottom: var(--sp-2);
    color: var(--text-primary);
}
.form-control {
    width: 100%;
    padding: 10px 14px;
    border: 1.5px solid var(--surface-border);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-family: inherit;
    transition: all var(--ease-normal);
    background: var(--surface-card);
    color: var(--text-primary);
}
.form-control:focus { outline: none; border-color: var(--color-primary); box-shadow: 0 0 0 3px rgba(91,110,245,.12); }
.form-control:disabled { background: var(--surface-subtle); cursor: not-allowed; color: var(--text-muted); }
textarea.form-control { resize: vertical; min-height: 90px; }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* Toggle switch */
.toggle-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--sp-3) 0;
    border-bottom: 1px solid var(--surface-border-light);
}
.toggle-row:last-child { border-bottom: none; }
.toggle-info strong { font-size: 13px; font-weight: 600; display: block; }
.toggle-info span { font-size: 12px; color: var(--text-muted); }
.toggle-switch { position: relative; width: 44px; height: 24px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
    position: absolute; inset: 0;
    background: #cbd5e1; border-radius: 12px; cursor: pointer;
    transition: background var(--ease-fast);
}
.toggle-slider::before {
    content: '';
    position: absolute;
    height: 18px; width: 18px;
    left: 3px; bottom: 3px;
    background: white; border-radius: 50%;
    transition: transform var(--ease-fast);
    box-shadow: var(--shadow-xs);
}
.toggle-switch input:checked + .toggle-slider { background: var(--color-primary); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(20px); }
.toggle-switch input:disabled + .toggle-slider { opacity: .5; cursor: not-allowed; }

/* ==========================================
   TABLES
   ========================================== */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
    background: var(--surface-subtle);
    padding: 12px var(--sp-4);
    text-align: left;
    font-weight: 600;
    color: var(--text-muted);
    border-bottom: 1px solid var(--surface-border);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .04em;
}
.table td { padding: 13px var(--sp-4); border-bottom: 1px solid var(--surface-border-light); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: var(--surface-subtle); }
.table-responsive { overflow-x: auto; }

/* ==========================================
   BADGES
   ========================================== */
.badge {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 3px 10px;
    border-radius: var(--radius-full);
    font-size: 11px; font-weight: 600;
}
.badge-primary   { background: var(--color-primary-light); color: var(--color-primary); }
.badge-success   { background: var(--color-success-bg);    color: var(--color-success); }
.badge-danger    { background: var(--color-danger-bg);     color: var(--color-danger); }
.badge-warning   { background: var(--color-warning-bg);    color: var(--color-warning); }
.badge-info      { background: var(--color-info-bg);       color: var(--color-info); }
.badge-neutral   { background: var(--surface-subtle);      color: var(--text-secondary); border: 1px solid var(--surface-border); }
.badge-admin     { background: #2d1f02; color: #fbbf24; }
.badge-user      { background: var(--color-primary-light); color: var(--color-primary); }
.badge-readonly  { background: #1e293b; color: #64748b; }

/* Project badges */
.badge-agent    { background: #1e1040; color: #a78bfa; }
.badge-shared   { background: #0c2340; color: #60a5fa; }

/* Status dot */
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.status-dot.online, .status-dot.enabled { background: var(--color-success); }
.status-dot.offline, .status-dot.disabled { background: var(--color-danger); }
.status-dot.warning { background: var(--color-warning); }

/* ==========================================
   MODALS
   ========================================== */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.7);
    backdrop-filter: blur(4px);
    display: none;
    align-items: center; justify-content: center;
    z-index: 2000; padding: var(--sp-5);
}
.modal-overlay.visible { display: flex; }
.modal {
    background: var(--surface-card);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-xl);
    max-width: 520px; width: 100%;
    max-height: 90vh; overflow-y: auto;
    animation: modal-in .2s ease;
}
@keyframes modal-in { from { opacity: 0; transform: translateY(-12px) scale(.98); } to { opacity: 1; transform: none; } }
.modal-header {
    padding: var(--sp-5) var(--sp-6);
    border-bottom: 1px solid var(--surface-border);
    display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { font-size: 16px; }
.modal-close {
    background: none; border: none;
    width: 30px; height: 30px;
    border-radius: var(--radius-md);
    cursor: pointer; font-size: 18px;
    color: var(--text-muted);
    display: flex; align-items: center; justify-content: center;
    transition: all var(--ease-fast);
}
.modal-close:hover { background: var(--surface-subtle); color: var(--text-primary); }
.modal-body { padding: var(--sp-6); }
.modal-footer { padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--surface-border); display: flex; justify-content: flex-end; gap: var(--sp-3); }

/* ==========================================
   TOAST
   ========================================== */
.toast {
    position: fixed; bottom: 24px; right: 24px;
    padding: 12px 20px;
    border-radius: var(--radius-lg);
    font-size: 14px; font-weight: 500;
    box-shadow: var(--shadow-lg);
    color: var(--text-white);
    animation: toast-in .25s ease;
    z-index: 3000; max-width: 360px;
}
@keyframes toast-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }
.toast.success { background: var(--color-success); }
.toast.error   { background: var(--color-danger); }
.toast.warning { background: var(--color-warning); }
.toast.info    { background: var(--color-info); }

/* ==========================================
   LOADING / SPINNER
   ========================================== */
.loading { display: none; text-align: center; padding: var(--sp-10); color: var(--text-muted); }
.loading.visible { display: block; }
.spinner {
    border: 3px solid var(--surface-border);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    width: 36px; height: 36px;
    animation: spin 0.8s linear infinite;
    margin: 0 auto var(--sp-4);
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ==========================================
   EMPTY STATE
   ========================================== */
.empty-state { text-align: center; padding: var(--sp-12) var(--sp-5); color: var(--text-muted); }
.empty-state .empty-icon { font-size: 48px; margin-bottom: var(--sp-4); opacity: .5; }
.empty-state h3 { color: var(--text-primary); margin-bottom: var(--sp-2); }
.empty-state p { font-size: 14px; max-width: 320px; margin: 0 auto var(--sp-5); }

/* ==========================================
   UPLOAD AREA
   ========================================== */
.upload-area {
    border: 2px dashed var(--surface-border);
    border-radius: var(--radius-lg);
    padding: var(--sp-10);
    text-align: center;
    cursor: pointer;
    transition: all var(--ease-normal);
}
.upload-area:hover,
.upload-area.dragover { border-color: var(--color-primary); background: var(--color-primary-light); }
.upload-area .upload-icon { font-size: 42px; margin-bottom: var(--sp-3); }
.upload-area p { font-size: 14px; }
.upload-area .browse { color: var(--color-primary); font-weight: 600; }
.progress-bar { width: 100%; height: 6px; background: var(--surface-border); border-radius: 3px; overflow: hidden; margin-top: var(--sp-4); display: none; }
.progress-bar.visible { display: block; }
.progress-fill { height: 100%; background: var(--gradient-primary); transition: width .3s ease; width: 0; }

/* ==========================================
   GRIDS
   ========================================== */
.grid      { display: grid; gap: var(--sp-5); }
.grid-2    { grid-template-columns: repeat(2, 1fr); }
.grid-3    { grid-template-columns: repeat(3, 1fr); }
.grid-4    { grid-template-columns: repeat(4, 1fr); }
.grid-auto { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }

/* ==========================================
   UTILITIES
   ========================================== */
.d-none    { display: none !important; }
.d-block   { display: block !important; }
.d-flex    { display: flex !important; }
.align-center { align-items: center !important; }
.gap-2     { gap: var(--sp-2) !important; }
.gap-3     { gap: var(--sp-3) !important; }
.gap-4     { gap: var(--sp-4) !important; }
.text-center { text-align: center !important; }
.text-right  { text-align: right !important; }
.text-muted  { color: var(--text-muted) !important; }
.text-danger { color: var(--color-danger) !important; }
.text-success{ color: var(--color-success) !important; }
.text-sm     { font-size: 12px !important; }
.text-xs     { font-size: 11px !important; }
.font-mono   { font-family: var(--font-mono) !important; }
.mt-2 { margin-top: var(--sp-2) !important; } .mt-3 { margin-top: var(--sp-3) !important; }
.mt-4 { margin-top: var(--sp-4) !important; } .mt-6 { margin-top: var(--sp-6) !important; }
.mb-2 { margin-bottom: var(--sp-2) !important; } .mb-3 { margin-bottom: var(--sp-3) !important; }
.mb-4 { margin-bottom: var(--sp-4) !important; } .mb-6 { margin-bottom: var(--sp-6) !important; }
.w-100 { width: 100% !important; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.break-all { word-break: break-all; }
.divider { border: none; border-top: 1px solid var(--surface-border); margin: var(--sp-5) 0; }

/* Code inline */
code {
    font-family: var(--font-mono);
    background: var(--surface-subtle);
    border: 1px solid var(--surface-border);
    padding: 1px 5px;
    border-radius: var(--radius-sm);
    font-size: 12px;
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--surface-subtle); }
::-webkit-scrollbar-thumb { background: #334155; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #475569; }

/* Animations */
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.fade-in { animation: fadeIn .25s ease; }

/* ==========================================
   RESPONSIVE
   ========================================== */
@media (max-width: 900px) {
    .app-sidebar { transform: translateX(-100%); transition: transform var(--ease-normal); }
    .app-sidebar.open { transform: none; }
    .app-content { margin-left: 0; padding: var(--sp-5); }
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 480px) {
    .app-header { padding: 0 var(--sp-4); }
    .app-content { padding: var(--sp-4); }
    .stats-grid { grid-template-columns: 1fr; }
}