/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg-base: #000000;
  --bg-surface: #0a0a0a;
  --bg-surface-hover: #171717;
  --bg-surface-active: #262626;
  
  --border-light: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.15);
  
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-subtle: #475569;
  
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-glow: rgba(59, 130, 246, 0.2);
  
  --accent: #8b5cf6;
  --success: #10b981;
  --danger: #ef4444;
  
  --radius-sm: 6px;
  --radius-md: 12px;
  --radius-lg: 20px;
  --radius-full: 9999px;
  
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
  
  --sidebar-width: 280px;
}

html { font-size: 16px; }
body {
  font-family: var(--font-sans);
  background-color: var(--bg-base);
  color: var(--text-main);
  min-height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--primary); text-decoration: none; }
button { font-family: var(--font-sans); }

/* Custom Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 10px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-subtle); }

.hidden { display: none !important; }

/* ===== LOGIN SCREEN (Futuristic) ===== */
.login-screen {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% -20%, #1a1a2e 0%, var(--bg-base) 60%);
}
.login-card {
  width: 100%; max-width: 400px;
  background: rgba(10, 10, 10, 0.6);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  padding: 40px;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);
  animation: fadeUp 0.6s ease-out;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
.login-logo { text-align: center; margin-bottom: 32px; }
.logo-icon { font-size: 42px; margin-bottom: 16px; filter: drop-shadow(0 0 10px var(--primary-glow)); }
.login-logo h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.5px; }
.login-logo p { color: var(--text-muted); font-size: 0.875rem; margin-top: 6px; }

.input-group { margin-bottom: 20px; }
.input-group label {
  display: block; font-size: 0.75rem; font-weight: 600;
  color: var(--text-muted); text-transform: uppercase;
  letter-spacing: 0.05em; margin-bottom: 8px;
}
.input-group input, .input-group select {
  width: 100%; padding: 12px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-main); font-size: 0.95rem;
  transition: var(--transition); outline: none;
}
.input-group input:focus, .input-group select:focus {
  border-color: var(--primary); background: #000;
  box-shadow: 0 0 0 4px var(--primary-glow);
}
.login-error { color: var(--danger); font-size: 0.85rem; min-height: 20px; margin-bottom: 12px; }

.btn-login {
  width: 100%; padding: 14px;
  background: var(--text-main); color: var(--bg-base);
  border: none; border-radius: var(--radius-md);
  font-size: 0.95rem; font-weight: 600;
  cursor: pointer; transition: var(--transition);
  display: flex; align-items: center; justify-content: center;
}
.btn-login:hover { background: #e2e8f0; transform: translateY(-1px); }

/* ===== MAIN APP LAYOUT ===== */
.main-app { display: flex; height: 100vh; animation: fadeIn 0.4s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-width); background: var(--bg-surface);
  border-right: 1px solid var(--border-light);
  display: flex; flex-direction: column; flex-shrink: 0;
  z-index: 50; transition: var(--transition);
}
.sidebar-header {
  padding: 20px; display: flex; align-items: center; justify-content: space-between;
}
.sidebar-logo { display: flex; align-items: center; gap: 12px; font-weight: 600; font-size: 1.1rem; }
.btn-new-chat {
  width: 36px; height: 36px; border-radius: var(--radius-full);
  background: var(--bg-surface-active); border: 1px solid var(--border-strong);
  color: var(--text-main); cursor: pointer; display: grid; place-items: center;
  transition: var(--transition);
}
.btn-new-chat:hover { background: var(--text-main); color: var(--bg-base); }

.sidebar-search { padding: 0 20px 16px; }
.sidebar-search input {
  width: 100%; padding: 10px 14px; border-radius: var(--radius-full);
  background: var(--bg-base); border: 1px solid var(--border-light);
  color: var(--text-main); font-size: 0.85rem; outline: none; transition: var(--transition);
}
.sidebar-search input:focus { border-color: var(--primary); }

.sidebar-chats { flex: 1; overflow-y: auto; padding: 0 12px; }
.chat-item {
  display: flex; align-items: center; padding: 12px 16px;
  border-radius: var(--radius-md); cursor: pointer;
  margin-bottom: 4px; transition: var(--transition);
  color: var(--text-muted);
}
.chat-item:hover { background: var(--bg-surface-hover); color: var(--text-main); }
.chat-item.active { background: var(--bg-surface-active); color: var(--text-main); font-weight: 500; }
.chat-item-title { flex: 1; font-size: 0.875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-delete {
  opacity: 0; background: none; border: none; color: var(--text-subtle);
  cursor: pointer; font-size: 1.1rem; transition: var(--transition);
}
.chat-item:hover .chat-item-delete { opacity: 1; }
.chat-item-delete:hover { color: var(--danger); }

.sidebar-footer { padding: 16px; border-top: 1px solid var(--border-light); position: relative; }
.user-profile {
  display: flex; align-items: center; gap: 12px; padding: 10px;
  border-radius: var(--radius-md); cursor: pointer; transition: var(--transition);
}
.user-profile:hover { background: var(--bg-surface-hover); }
.user-avatar {
  width: 32px; height: 32px; border-radius: var(--radius-full);
  background: var(--text-main); color: var(--bg-base);
  display: grid; place-items: center; font-weight: 700; font-size: 0.85rem;
}
.user-info { flex: 1; min-width: 0; }
.user-name { font-size: 0.875rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 0.7rem; color: var(--text-subtle); text-transform: uppercase; letter-spacing: 0.5px; }

/* User Menu Dropdown */
.user-menu {
  position: absolute; bottom: calc(100% + 8px); left: 16px; right: 16px;
  background: rgba(20, 20, 20, 0.8); backdrop-filter: blur(20px);
  border: 1px solid var(--border-strong); border-radius: var(--radius-md);
  padding: 6px; z-index: 100; box-shadow: 0 10px 25px rgba(0,0,0,0.5);
}
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 12px; background: none; border: none; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 0.85rem; cursor: pointer; transition: var(--transition);
}
.menu-item:hover { background: var(--border-light); color: var(--text-main); }
.menu-item-danger:hover { background: rgba(239, 68, 68, 0.1); color: var(--danger); }
.menu-divider { height: 1px; background: var(--border-light); margin: 4px 0; }

/* ===== MAIN CONTENT ===== */
.main-content { flex: 1; display: flex; flex-direction: column; position: relative; background: var(--bg-base); }

/* Top Bar */
.top-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 24px; position: absolute; top: 0; left: 0; right: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.8), transparent);
  z-index: 10; pointer-events: none;
}
.top-bar > * { pointer-events: auto; }
.model-selector select {
  background: rgba(20, 20, 20, 0.6); backdrop-filter: blur(10px);
  border: 1px solid var(--border-strong); border-radius: var(--radius-full);
  padding: 8px 16px; color: var(--text-main); font-size: 0.85rem; font-weight: 500;
  cursor: pointer; outline: none; transition: var(--transition); appearance: none;
}
.model-selector select:hover { background: rgba(30, 30, 30, 0.8); }
.status-indicator { display: flex; align-items: center; gap: 6px; font-size: 0.75rem; font-weight: 500; color: var(--text-muted); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); box-shadow: 0 0 10px var(--success); }
.status-dot.offline { background: var(--danger); box-shadow: 0 0 10px var(--danger); }

/* Chat Area */
.chat-area { flex: 1; overflow-y: auto; padding: 80px 0 40px; scroll-behavior: smooth; }

/* Welcome Screen */
.welcome-screen { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.welcome-logo { font-size: 3rem; margin-bottom: 24px; opacity: 0.8; }
.welcome-screen h2 { font-size: 2rem; font-weight: 600; letter-spacing: -0.5px; margin-bottom: 12px; }
.welcome-screen p { color: var(--text-muted); font-size: 1.1rem; margin-bottom: 40px; }
.welcome-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; max-width: 600px; width: 100%; padding: 0 20px; }
.welcome-card {
  background: var(--bg-surface); border: 1px solid var(--border-light);
  border-radius: var(--radius-md); padding: 20px; cursor: pointer;
  transition: var(--transition); text-align: left;
}
.welcome-card:hover { border-color: var(--border-strong); background: var(--bg-surface-hover); transform: translateY(-2px); }
.wc-icon { font-size: 1.25rem; margin-bottom: 12px; opacity: 0.7; }
.wc-text { font-size: 0.875rem; color: var(--text-muted); line-height: 1.5; }

/* Messages */
.messages-container { max-width: 800px; margin: 0 auto; padding: 0 24px; }
.message { display: flex; gap: 20px; margin-bottom: 40px; opacity: 0; animation: fadeUp 0.4s ease forwards; }
.message-avatar {
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  display: grid; place-items: center; font-size: 1rem; flex-shrink: 0;
}
.message.user .message-avatar { background: var(--bg-surface-active); color: var(--text-main); }
.message.assistant .message-avatar { background: var(--text-main); color: var(--bg-base); }
.message-body { flex: 1; min-width: 0; padding-top: 6px; }
.message-name { font-weight: 600; font-size: 0.9rem; margin-bottom: 8px; color: var(--text-main); }
.message-content { font-size: 1rem; line-height: 1.7; color: var(--text-muted); }
.message-content p { margin-bottom: 16px; }
.message-content p:last-child { margin-bottom: 0; }
.message-content pre {
  background: var(--bg-surface); border: 1px solid var(--border-light);
  border-radius: var(--radius-md); padding: 16px; margin: 16px 0;
  overflow-x: auto; font-family: var(--font-mono); font-size: 0.875rem; color: var(--text-main);
}
.message-content code { font-family: var(--font-mono); font-size: 0.875rem; background: var(--bg-surface); padding: 3px 6px; border-radius: 4px; color: var(--primary); }
.message-content pre code { background: none; padding: 0; color: inherit; }
.message-content ul, .message-content ol { padding-left: 24px; margin: 16px 0; }
.message-content li { margin-bottom: 8px; }
.message-content strong { color: var(--text-main); font-weight: 600; }

/* Custom Attachment UI injected by JS */
.msg-attachment-card {
  display: inline-flex; align-items: center; gap: 12px;
  background: var(--bg-surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-md); padding: 12px 16px; margin: 8px 0 16px;
  max-width: 100%;
}
.mac-icon { font-size: 1.5rem; }
.mac-info { display: flex; flex-direction: column; overflow: hidden; }
.mac-name { font-size: 0.875rem; font-weight: 500; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mac-type { font-size: 0.7rem; color: var(--text-subtle); text-transform: uppercase; letter-spacing: 0.5px; }

.typing-indicator { display: flex; gap: 6px; padding: 8px 0; }
.typing-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--text-subtle); animation: typing 1.4s infinite; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%, 100% { transform: translateY(0); opacity: 0.4; } 50% { transform: translateY(-4px); opacity: 1; } }

/* ===== INPUT AREA ===== */
.input-area {
  padding: 0 24px 32px; background: linear-gradient(to top, var(--bg-base) 60%, transparent);
}
.input-container {
  max-width: 800px; margin: 0 auto; position: relative;
}
.attachments-preview {
  display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px;
}
.attachments-preview.empty { display: none; }
.attachment-item {
  background: var(--bg-surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-full); padding: 6px 12px 6px 6px;
  display: flex; align-items: center; gap: 8px; font-size: 0.8rem; font-weight: 500;
}
.attachment-item img { width: 24px; height: 24px; object-fit: cover; border-radius: 50%; }
.attachment-item .ai-icon { width: 24px; height: 24px; border-radius: 50%; background: var(--bg-surface-active); display: grid; place-items: center; font-size: 0.7rem; }
.attachment-name { max-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-remove-attachment { background: none; border: none; color: var(--text-subtle); cursor: pointer; padding: 2px; }
.btn-remove-attachment:hover { color: var(--danger); }

.input-wrapper {
  background: var(--bg-surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg); padding: 12px 16px;
  display: flex; align-items: flex-end; gap: 12px;
  box-shadow: 0 10px 30px -10px rgba(0,0,0,0.5);
  transition: var(--transition);
}
.input-wrapper:focus-within { border-color: var(--text-subtle); background: var(--bg-surface-hover); }

.btn-attach {
  background: none; border: none; color: var(--text-subtle);
  cursor: pointer; padding: 6px; border-radius: var(--radius-sm);
  display: grid; place-items: center; transition: var(--transition);
}
.btn-attach:hover { color: var(--text-main); background: var(--bg-surface-active); }

#messageInput {
  flex: 1; background: transparent; border: none; color: var(--text-main);
  font-family: var(--font-sans); font-size: 0.95rem; line-height: 1.5;
  resize: none; outline: none; max-height: 200px; padding: 6px 0;
}
#messageInput::placeholder { color: var(--text-subtle); }

.btn-send, .btn-stop {
  width: 36px; height: 36px; border-radius: var(--radius-md); border: none;
  display: grid; place-items: center; cursor: pointer; transition: var(--transition); flex-shrink: 0;
}
.btn-send { background: var(--text-main); color: var(--bg-base); }
.btn-send:hover { background: #fff; transform: scale(1.05); }
.btn-send:disabled { opacity: 0.3; cursor: not-allowed; transform: none; }
.btn-stop { background: var(--bg-surface-active); color: var(--text-main); border: 1px solid var(--border-strong); }
.btn-stop:hover { border-color: var(--danger); color: var(--danger); }

.input-footer {
  display: flex; justify-content: space-between; margin-top: 12px;
  font-size: 0.75rem; color: var(--text-subtle); padding: 0 8px;
}

/* ===== MODALS ===== */
.modal { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center; }
.modal-overlay { position: absolute; inset: 0; background: rgba(0,0,0,0.8); backdrop-filter: blur(4px); }
.modal-content {
  position: relative; background: var(--bg-surface); border: 1px solid var(--border-light);
  border-radius: var(--radius-lg); width: 90vw; max-height: 85vh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5); animation: fadeUp 0.3s ease;
}
.modal-sm { max-width: 400px; } .modal-md { max-width: 600px; } .modal-lg { max-width: 900px; }

.modal-header { padding: 20px 24px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border-light); }
.modal-header h2 { font-size: 1.1rem; font-weight: 600; }
.modal-close { background: none; border: none; color: var(--text-muted); font-size: 1.5rem; cursor: pointer; }
.modal-body { padding: 24px; overflow-y: auto; }

.modal-tabs { display: flex; gap: 8px; padding: 16px 24px 0; border-bottom: 1px solid var(--border-light); }
.tab-btn {
  background: none; border: none; color: var(--text-muted); font-size: 0.875rem; font-weight: 500;
  padding: 8px 16px; cursor: pointer; border-bottom: 2px solid transparent; transition: var(--transition);
}
.tab-btn:hover { color: var(--text-main); }
.tab-btn.active { color: var(--text-main); border-bottom-color: var(--text-main); }
.tab-panel { display: none; } .tab-panel.active { display: block; }

/* Admin UI */
.panel-toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.btn-primary {
  background: var(--text-main); color: var(--bg-base); padding: 8px 16px;
  border: none; border-radius: var(--radius-sm); font-weight: 500; font-size: 0.85rem;
  cursor: pointer; transition: var(--transition);
}
.btn-primary:hover { background: #e2e8f0; }

.table-wrapper { overflow-x: auto; border: 1px solid var(--border-light); border-radius: var(--radius-md); }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.data-table th { padding: 12px 16px; text-align: left; color: var(--text-subtle); text-transform: uppercase; font-size: 0.7rem; letter-spacing: 0.05em; background: rgba(255,255,255,0.02); border-bottom: 1px solid var(--border-light); }
.data-table td { padding: 12px 16px; border-bottom: 1px solid var(--border-light); color: var(--text-muted); }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: rgba(255,255,255,0.02); color: var(--text-main); }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: rgba(255,255,255,0.02); border: 1px solid var(--border-light); border-radius: var(--radius-md); padding: 20px; }
.stat-label { font-size: 0.75rem; color: var(--text-subtle); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }
.stat-value { font-size: 1.5rem; font-weight: 600; color: var(--text-main); }

.badge { padding: 4px 8px; border-radius: var(--radius-full); font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.badge-green { background: rgba(16, 185, 129, 0.1); color: var(--success); }
.badge-red { background: rgba(239, 68, 68, 0.1); color: var(--danger); }
.badge-blue { background: rgba(59, 130, 246, 0.1); color: var(--primary); }

.toast {
  position: fixed; bottom: 24px; right: 24px; z-index: 1000;
  background: var(--bg-surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-md); padding: 16px 20px; font-size: 0.875rem;
  box-shadow: 0 10px 25px rgba(0,0,0,0.5); animation: fadeUp 0.3s ease;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
