:root {
    /* Premium Space Palette */
    --bg-dark: #050510;
    --bg-space: #090a1a;
    --accent-blue: #3b82f6;
    --accent-purple: #8b5cf6;
    --accent-glow: rgba(139, 92, 246, 0.4);
    
    --glass-bg: rgba(15, 23, 42, 0.6);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-border-highlight: rgba(139, 92, 246, 0.5);
    
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    
    --danger: #ef4444;
    --success: #10b981;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-primary);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    position: relative;
}

/* --- Space Background --- */
.space-background {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 0;
    background: radial-gradient(circle at center, var(--bg-space) 0%, var(--bg-dark) 100%);
    overflow: hidden;
}

.stars {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background-image: 
        radial-gradient(1.5px 1.5px at 20px 30px, #ffffff, rgba(0,0,0,0)),
        radial-gradient(1px 1px at 80px 120px, #ffffff, rgba(0,0,0,0)),
        radial-gradient(2px 2px at 150px 60px, rgba(255,255,255,0.8), rgba(0,0,0,0)),
        radial-gradient(1.5px 1.5px at 250px 180px, #ffffff, rgba(0,0,0,0)),
        radial-gradient(1px 1px at 320px 40px, #ffffff, rgba(0,0,0,0));
    background-size: 400px 400px;
    animation: panStars 150s linear infinite;
    opacity: 0.6;
}

.nebula {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: 
        radial-gradient(circle at 15% 50%, rgba(59, 130, 246, 0.15) 0%, transparent 40%),
        radial-gradient(circle at 85% 30%, rgba(139, 92, 246, 0.15) 0%, transparent 40%);
    filter: blur(40px);
}

@keyframes panStars {
    from { background-position: 0 0; }
    to { background-position: -1000px 500px; }
}

/* --- Base Layout --- */
.app-container {
    display: flex;
    width: 100%;
    height: 100%;
    position: relative;
    z-index: 1;
}

/* --- Glassmorphism Utils --- */
.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
}

.glass-input {
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid var(--glass-border);
    color: var(--text-primary);
    border-radius: 12px;
    padding: 12px 16px;
    font-family: inherit;
    font-size: 0.95rem;
    transition: all 0.3s;
}
.glass-input:focus {
    outline: none;
    border-color: var(--accent-purple);
    box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.2);
}

.btn {
    padding: 12px 20px;
    border-radius: 12px;
    border: none;
    cursor: pointer;
    font-family: inherit;
    font-weight: 600;
    font-size: 0.95rem;
    transition: all 0.3s;
}
.primary-btn {
    background: linear-gradient(135deg, var(--accent-blue) 0%, var(--accent-purple) 100%);
    color: white;
    box-shadow: 0 4px 15px var(--accent-glow);
}
.primary-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(139, 92, 246, 0.6);
}
.secondary-btn {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    border: 1px solid var(--glass-border);
}
.secondary-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}
.danger-btn {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    border: 1px solid var(--danger);
}
.danger-btn:hover {
    background: var(--danger);
    color: white;
}

.icon-btn {
    background: none; border: none; color: var(--text-secondary);
    font-size: 1.5rem; cursor: pointer; transition: 0.2s;
    display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { color: var(--text-primary); transform: scale(1.1); }
.danger-icon:hover { color: var(--danger); }

/* --- Install Overlay --- */
.install-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(5, 5, 16, 0.85);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    z-index: 1000;
    display: flex; justify-content: center; align-items: center;
    transition: opacity 0.5s ease;
}
.install-overlay.hidden { opacity: 0; pointer-events: none; }

.glass-card {
    background: rgba(15, 23, 42, 0.5);
    border: 1px solid var(--glass-border);
    border-radius: 24px;
    padding: 40px;
    max-width: 700px;
    width: 90%;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    display: flex; flex-direction: column; gap: 25px;
}

.install-header { text-align: center; }
.app-logo-large { width: 70px; height: 70px; margin-bottom: 15px; animation: float 6s ease-in-out infinite; }
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
.install-header h2 { font-size: 1.8rem; margin-bottom: 8px; background: linear-gradient(135deg, var(--accent-blue), var(--accent-purple)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.install-header p { color: var(--text-secondary); font-size: 0.95rem; }

.model-selection-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; }
.glass-option {
    background: rgba(0,0,0,0.3); border: 1px solid var(--glass-border); border-radius: 16px; padding: 20px;
    cursor: pointer; transition: all 0.3s; position: relative; overflow: hidden;
}
.glass-option:hover {
    border-color: var(--accent-purple); box-shadow: 0 0 20px var(--accent-glow); background: rgba(139, 92, 246, 0.05); transform: translateY(-3px);
}
.glass-option.recommended { border-color: rgba(59, 130, 246, 0.4); }

.option-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.option-header h3 { font-size: 1.1rem; font-weight: 600; }
.badge { font-size: 0.75rem; padding: 4px 8px; border-radius: 20px; background: rgba(255,255,255,0.1); }
.badge.highlight { background: rgba(59, 130, 246, 0.2); color: #60a5fa; border: 1px solid rgba(59, 130, 246, 0.4); }
.model-desc { font-size: 0.85rem; color: var(--text-secondary); line-height: 1.5; }
.recommended-tag { position: absolute; bottom: 0; left: 0; right: 0; background: rgba(59, 130, 246, 0.2); color: #60a5fa; font-size: 0.7rem; text-align: center; padding: 4px; font-weight: bold; }

.install-progress-container {
    background: rgba(0,0,0,0.4); border-radius: 16px; padding: 20px; border: 1px solid var(--glass-border);
}
.progress-info { display: flex; justify-content: space-between; margin-bottom: 10px; font-weight: 500; font-size: 0.9rem; }
.progress-bar-bg { width: 100%; height: 8px; background: rgba(255,255,255,0.1); border-radius: 4px; overflow: hidden; }
.progress-bar-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent-blue), var(--accent-purple)); transition: width 0.3s; }
.progress-hint { margin-top: 10px; font-size: 0.8rem; color: var(--text-secondary); text-align: center; }

.error-rescue-panel {
    background: rgba(239, 68, 68, 0.1); border: 1px solid rgba(239, 68, 68, 0.3); border-radius: 16px; padding: 20px;
    display: flex; flex-direction: column; gap: 10px; text-align: center;
}
.error-rescue-panel h3 { color: #fca5a5; font-size: 1.1rem; }
.error-rescue-panel p { color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 10px; }

/* --- Sidebar --- */
.sidebar {
    width: 320px;
    border-right: 1px solid var(--glass-border);
    display: flex; flex-direction: column;
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 10;
}
.sidebar-header {
    padding: 20px; border-bottom: 1px solid var(--glass-border); display: flex; justify-content: space-between; align-items: center;
}
.sidebar-header h2 { font-size: 1.1rem; font-weight: 600; }
.sidebar-content { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
.knowledge-actions { display: flex; gap: 10px; padding: 20px; border-bottom: 1px solid var(--glass-border); }
.knowledge-actions .btn { padding: 8px 12px; font-size: 0.85rem; }

.knowledge-list {
    flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px;
}
.knowledge-item {
    background: rgba(0,0,0,0.3); border: 1px solid var(--glass-border); border-radius: 12px; padding: 15px; position: relative;
}
.knowledge-item h4 { font-size: 0.9rem; margin-bottom: 6px; color: var(--accent-blue); }
.knowledge-item p { font-size: 0.85rem; color: var(--text-secondary); }
.delete-knowledge { position: absolute; top: 12px; right: 12px; font-size: 1.1rem; }

.add-knowledge { padding: 20px; border-top: 1px solid var(--glass-border); display: flex; flex-direction: column; gap: 10px; background: rgba(0,0,0,0.2); }

/* --- Chat Area --- */
.chat-area { flex: 1; display: flex; flex-direction: column; position: relative; }
.chat-header {
    height: 65px; display: flex; justify-content: space-between; align-items: center; padding: 0 20px;
    border-bottom: 1px solid var(--glass-border); z-index: 5;
}
.header-left { display: flex; align-items: center; gap: 15px; }
.app-logo { width: 28px; height: 28px; }
.header-left h1 { font-size: 1.2rem; font-weight: 600; letter-spacing: 1px; }

.header-right { display: flex; align-items: center; }
.chat-status-bar { display: flex; align-items: center; gap: 15px; }
.status-indicator { font-size: 0.75rem; font-weight: bold; text-transform: uppercase; padding: 4px 10px; border-radius: 20px; }
.status-indicator.online { background: rgba(16, 185, 129, 0.1); color: var(--success); border: 1px solid rgba(16, 185, 129, 0.3); }
.status-indicator.offline { background: rgba(239, 68, 68, 0.1); color: var(--danger); border: 1px solid rgba(239, 68, 68, 0.3); }

.small-progress-container { display: flex; flex-direction: column; align-items: flex-end; width: 150px; }
#model-status-text { font-size: 0.75rem; color: var(--text-secondary); margin-bottom: 4px; }
.small-progress-bg { width: 100%; height: 4px; background: rgba(255,255,255,0.1); border-radius: 2px; overflow: hidden; }
.small-progress-fill { height: 100%; width: 0%; background: var(--accent-purple); transition: 0.3s; }

.chat-history {
    flex: 1; overflow-y: auto; padding: 30px; display: flex; flex-direction: column; gap: 20px; scroll-behavior: smooth;
    z-index: 2; /* above background */
}

/* Custom Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 3px; }

.message { display: flex; max-width: 85%; animation: slideUp 0.3s ease forwards; opacity: 0; transform: translateY(10px); }
@keyframes slideUp { to { opacity: 1; transform: translateY(0); } }
.user-msg { align-self: flex-end; }
.ai-msg, .system-msg { align-self: flex-start; }

.msg-content {
    padding: 14px 20px; border-radius: 16px; font-size: 1rem; line-height: 1.5;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.user-msg .msg-content {
    background: rgba(59, 130, 246, 0.15); border: 1px solid rgba(59, 130, 246, 0.3);
    border-bottom-right-radius: 4px;
}
.ai-msg .msg-content {
    background: rgba(15, 23, 42, 0.7); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border); border-bottom-left-radius: 4px;
}
.system-msg .msg-content {
    background: rgba(0,0,0,0.4); border: 1px dashed var(--glass-border); color: var(--text-secondary); font-size: 0.9rem;
}

.chat-input-area {
    padding: 20px; border-top: 1px solid var(--glass-border); display: flex; gap: 12px; align-items: flex-end; z-index: 5;
}
.input-wrapper { flex: 1; position: relative; }
.chat-input { width: 100%; max-height: 120px; }
.icon-primary-btn { width: 45px; height: 45px; padding: 0; display: flex; justify-content: center; align-items: center; border-radius: 12px; font-size: 1.2rem; }
.icon-danger-btn { background: var(--danger); width: 45px; height: 45px; padding: 0; display: flex; justify-content: center; align-items: center; border-radius: 12px; font-size: 1.2rem; color: white; }

.hidden { display: none !important; }

/* Responsive */
#open-sidebar-btn { display: none; }
@media (max-width: 768px) {
    .app-container { flex-direction: column; }
    .sidebar { position: absolute; height: 100%; width: 85%; transform: translateX(-100%); background: var(--bg-space); }
    .sidebar.open { transform: translateX(0); box-shadow: 10px 0 30px rgba(0,0,0,0.8); }
    #open-sidebar-btn { display: block; }
    .header-left h1 { font-size: 1rem; }
    #connection-status { display: none; }
    .small-progress-container { width: 120px; }
    .chat-history { padding: 15px; }
    .message { max-width: 95%; }
}

/* --- Performance Optimizations --- */
body.optimized-mode .stars,
body.optimized-mode .twinkling,
body.optimized-mode .nebula {
    display: none !important;
    animation: none !important;
}
body.optimized-mode .glass-panel,
body.optimized-mode .glass-card,
body.optimized-mode .glass-input,
body.optimized-mode .sidebar {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    background: var(--bg-space) !important;
}
