/* === Base Reset & Variables === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #16213e;
  --bg-input: #1e1e38;
  --accent-blue: #0f3460;
  --accent-bright: #4a9eff;
  --accent-red: #e94560;
  --text-primary: #e8e8f0;
  --text-secondary: #8888a8;
  --text-muted: #5a5a78;
  --border-color: #2a2a48;
  --msg-user-bg: #0f3460;
  --msg-bot-bg: #1e1e35;
  --msg-bot-border: #2a2a4a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: 0.2s ease;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

.hidden { display: none !important; }

/* === Pages === */
.page { width: 100%; height: 100%; }

/* === Login === */
#login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-tertiary) 50%, var(--bg-primary) 100%);
}

.login-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow);
}

.login-brand {
  text-align: center;
  margin-bottom: 36px;
}

.brand-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 8px;
}

.login-brand h1 {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 4px;
  color: var(--text-primary);
}

.tagline {
  color: var(--text-secondary);
  font-size: 14px;
  margin-top: 4px;
  letter-spacing: 1px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 15px;
  transition: border-color var(--transition);
  outline: none;
}

.form-group input:focus {
  border-color: var(--accent-bright);
}

.btn-primary {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent-blue), var(--accent-bright));
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity var(--transition);
  letter-spacing: 0.5px;
  margin-top: 8px;
}

.btn-primary:hover { opacity: 0.9; }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.error-msg {
  color: var(--accent-red);
  font-size: 13px;
  text-align: center;
  margin-top: 12px;
  min-height: 20px;
}

.login-footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 32px;
}

/* === Chat Layout === */
#chat-page {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Header */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon-sm { font-size: 28px; }

.header-left h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 3px;
}

.header-tagline {
  font-size: 11px;
  color: var(--text-secondary);
  letter-spacing: 0.5px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.btn-ghost {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-ghost:hover {
  border-color: var(--accent-bright);
  color: var(--text-primary);
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
}

#user-display {
  font-size: 13px;
  color: var(--text-secondary);
}

.btn-logout {
  padding: 8px;
  border: none;
}

.btn-logout:hover { color: var(--accent-red); border-color: transparent; }

/* Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }

.welcome-message {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-secondary);
}

.welcome-icon {
  font-size: 64px;
  display: block;
  margin-bottom: 16px;
}

.welcome-message h2 {
  font-size: 28px;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.welcome-message p { font-size: 15px; margin-top: 4px; }

.welcome-sub {
  color: var(--text-muted);
  font-size: 14px !important;
  margin-top: 16px !important;
}

/* Message Bubbles */
.message {
  display: flex;
  flex-direction: column;
  max-width: 80%;
  animation: fadeIn 0.25s ease;
}

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

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.message-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.message.user .message-label { text-align: right; }

.message-bubble {
  padding: 14px 18px;
  border-radius: var(--radius);
  line-height: 1.6;
  font-size: 14.5px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message.user .message-bubble {
  background: var(--msg-user-bg);
  border-bottom-right-radius: 4px;
  color: #e0eaff;
}

.message.assistant .message-bubble {
  background: var(--msg-bot-bg);
  border: 1px solid var(--msg-bot-border);
  border-bottom-left-radius: 4px;
  color: var(--text-primary);
}

/* Markdown content inside bot messages */
.message-bubble h1, .message-bubble h2, .message-bubble h3 {
  margin: 16px 0 8px;
  color: var(--accent-bright);
}

.message-bubble h1:first-child, .message-bubble h2:first-child, .message-bubble h3:first-child {
  margin-top: 0;
}

.message-bubble p { margin: 8px 0; }
.message-bubble p:first-child { margin-top: 0; }
.message-bubble p:last-child { margin-bottom: 0; }

.message-bubble ul, .message-bubble ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message-bubble li { margin: 4px 0; }

.message-bubble code {
  background: rgba(255,255,255,0.08);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

.message-bubble pre {
  background: #0d0d1a;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 14px;
  margin: 10px 0;
  overflow-x: auto;
  position: relative;
}

.message-bubble pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.5;
}

.message-bubble blockquote {
  border-left: 3px solid var(--accent-bright);
  padding-left: 14px;
  margin: 10px 0;
  color: var(--text-secondary);
}

.message-bubble table {
  border-collapse: collapse;
  margin: 10px 0;
  width: 100%;
  font-size: 13px;
}

.message-bubble th, .message-bubble td {
  border: 1px solid var(--border-color);
  padding: 8px 12px;
  text-align: left;
}

.message-bubble th {
  background: rgba(255,255,255,0.05);
  font-weight: 600;
}

.message-bubble a {
  color: var(--accent-bright);
  text-decoration: none;
}

.message-bubble a:hover { text-decoration: underline; }

/* Typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 28px 8px;
  color: var(--text-muted);
  font-size: 13px;
  flex-shrink: 0;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-bright);
  animation: pulse 1.4s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.1); }
}

/* Input Area */
.chat-input-area {
  padding: 16px 24px 12px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 8px 12px;
  transition: border-color var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent-bright);
}

#chat-input {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  max-height: 150px;
  padding: 4px 0;
}

#chat-input::placeholder { color: var(--text-muted); }

.btn-send {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--accent-bright);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}

.btn-send:hover { background: #3a8aee; }
.btn-send:disabled { opacity: 0.3; cursor: not-allowed; }

.input-hint {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 8px;
}

/* === Responsive === */
@media (max-width: 768px) {
  .chat-header { padding: 10px 16px; }
  .header-left h1 { font-size: 16px; letter-spacing: 2px; }
  .header-tagline { display: none; }
  #new-chat-btn span { display: none; }
  .chat-messages { padding: 16px; }
  .message { max-width: 90%; }
  .chat-input-area { padding: 12px 16px 8px; }
  .login-card { margin: 20px; padding: 36px 28px; }
  #user-display { display: none; }
}

@media (max-width: 480px) {
  .welcome-message { padding: 40px 16px; }
  .welcome-icon { font-size: 48px; }
  .welcome-message h2 { font-size: 22px; }
}

/* File Upload Styles */
.drag-over {
  outline: 3px dashed var(--accent-bright) !important;
  outline-offset: -3px;
  opacity: 0.9;
}

.btn-file {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

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

.file-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 24px;
  border-bottom: 1px solid var(--border-color);
}

.file-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 6px 10px;
  max-width: 250px;
}

.file-thumb {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 4px;
}

.file-icon {
  font-size: 20px;
  width: 32px;
  text-align: center;
}

.file-name {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 150px;
}

.file-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
}

.file-remove:hover {
  color: var(--accent-red);
}

.msg-file {
  margin-bottom: 8px;
}

.msg-image {
  max-width: 300px;
  max-height: 300px;
  border-radius: 8px;
  cursor: pointer;
}

.msg-image:hover {
  opacity: 0.9;
}

.msg-file-name {
  font-size: 13px;
  color: var(--text-secondary);
  display: inline-block;
  padding: 4px 8px;
  background: rgba(255,255,255,0.05);
  border-radius: 4px;
}

/* === Sidebar === */
#chat-page { display: flex; flex-direction: row; }

.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: all 0.2s ease;
  overflow: hidden;
}

.sidebar.collapsed { width: 0; min-width: 0; border-right: none; }

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 2px;
  flex: 1;
}

.btn-icon {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.btn-icon:hover { background: rgba(255,255,255,0.08); color: var(--text-primary); }

.sidebar-open-btn { margin-right: 8px; }

.sidebar-new-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 12px 12px 8px;
  padding: 10px;
  border: 1px dashed var(--border-color);
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.sidebar-new-chat:hover {
  border-color: var(--accent-bright);
  color: var(--accent-bright);
  background: rgba(74, 158, 255, 0.05);
}

.sidebar-search {
  position: relative;
  margin: 4px 12px 8px;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.sidebar-search input {
  width: 100%;
  padding: 8px 10px 8px 32px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 12px;
  outline: none;
  transition: border-color 0.2s ease;
}

.sidebar-search input:focus { border-color: var(--accent-bright); }
.sidebar-search input::placeholder { color: var(--text-muted); }

.sidebar-section { flex: 1; overflow-y: auto; padding: 0 0 8px; min-height: 0; }

.sidebar-section-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 12px 16px 6px;
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  z-index: 1;
}

.sidebar-list { padding: 0 8px; }

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s ease;
  position: relative;
}

.sidebar-item:hover { background: rgba(255,255,255,0.05); }
.sidebar-item.active { background: rgba(74, 158, 255, 0.12); }

.sidebar-item-content { flex: 1; min-width: 0; }

.sidebar-item-title {
  font-size: 13px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-item-meta {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-item-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  opacity: 0;
  transition: all 0.15s ease;
  padding: 2px 4px;
}

.sidebar-item:hover .sidebar-item-delete { opacity: 1; }
.sidebar-item-delete:hover { color: var(--accent-red); }

.sidebar-empty {
  padding: 12px 16px;
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

.file-type-icon {
  font-size: 18px;
  flex-shrink: 0;
  width: 24px;
  text-align: center;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.user-menu-sidebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.user-menu-sidebar #user-display {
  font-size: 13px;
  color: var(--text-secondary);
}

/* Chat main area */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
}

/* Override old layout */
#chat-page.hidden { display: none !important; }
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

/* Mobile sidebar */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    z-index: 100;
    box-shadow: 4px 0 24px rgba(0,0,0,0.5);
  }
  .sidebar.collapsed { transform: translateX(-100%); }
  .sidebar-open-btn { display: flex !important; }
}
