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

:root {
  --primary: #357a5b;
  --primary-hover: #2f6e52;
  --primary-light: #e3f2e7;
  --bg: #eef6f0;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-hover: #cbd5e1;
  --text: #1e293b;
  --text-muted: #64748b;
  --text-light: #94a3b8;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --sidebar-width: 260px;
  --header-h: 60px;
  --input-h: 72px;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-xs: 6px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
  --shadow: 0 4px 6px -1px rgba(0,0,0,.07), 0 2px 4px -1px rgba(0,0,0,.04);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.08), 0 4px 6px -2px rgba(0,0,0,.04);
  --transition: 200ms ease;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ===== Toast Notifications ===== */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  color: white;
  box-shadow: var(--shadow-lg);
  pointer-events: all;
  animation: slideIn 0.25s ease;
  max-width: 320px;
}

.toast-success { background: var(--success); }
.toast-error   { background: var(--danger); }
.toast-info    { background: var(--primary); }

@keyframes slideIn {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ===== Login Screen ===== */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(135deg, #ace1af 0%, #88c9a1 100%);
}

.login-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg), 0 20px 60px rgba(0,0,0,.12);
  text-align: center;
}

.login-logo {
  width: 140px;
  height: 140px;
  margin: 0 auto 16px;
  display: block;
  filter: drop-shadow(0 2px 8px rgba(0,0,0,.1));
}

.login-title {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 28px;
}

.login-input {
  width: 100%;
  padding: 13px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  color: var(--text);
  background: var(--surface-2);
  outline: none;
  transition: border-color var(--transition);
  letter-spacing: 0.05em;
  margin-bottom: 12px;
}

.login-input:focus {
  border-color: var(--primary);
  background: var(--surface);
}

.btn-primary {
  width: 100%;
  padding: 13px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background var(--transition), transform var(--transition), box-shadow var(--transition);
  margin-bottom: 12px;
}

.btn-primary:hover { background: var(--primary-hover); box-shadow: var(--shadow); }
.btn-primary:active { transform: scale(0.99); }
.btn-primary:disabled { opacity: 0.65; cursor: not-allowed; }

.error-text {
  color: var(--danger);
  font-size: 13px;
  min-height: 20px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,.4);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Chat Screen Layout ===== */
#chat-screen {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar overlay for mobile */
#sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 100;
  backdrop-filter: blur(2px);
}

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform var(--transition), width var(--transition);
  z-index: 101;
}

#sidebar.collapsed {
  width: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 16px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sidebar-header h2 {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.btn-upload {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  border: 1.5px dashed var(--border-hover);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--transition);
  text-align: center;
}

.btn-upload:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

.file-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.empty-state {
  padding: 24px 16px;
  text-align: center;
  color: var(--text-muted);
}

.empty-icon { font-size: 32px; display: block; margin-bottom: 8px; }

.empty-state p {
  font-size: 13px;
  line-height: 1.5;
  margin-bottom: 4px;
}

.empty-hint {
  font-size: 12px !important;
  color: var(--text-light) !important;
}

.file-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-xs);
  transition: background var(--transition);
  cursor: default;
}

.file-item:hover { background: var(--surface-2); }

.file-icon { font-size: 18px; flex-shrink: 0; }

.file-info { flex: 1; min-width: 0; }

.file-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-meta {
  font-size: 11px;
  color: var(--text-light);
}

.file-remove {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--text-light);
  cursor: pointer;
  border-radius: 4px;
  font-size: 16px;
  line-height: 1;
  transition: all var(--transition);
  opacity: 0;
}

.file-item:hover .file-remove { opacity: 1; }
.file-remove:hover { background: #fee2e2; color: var(--danger); }

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

.btn-ghost-sm {
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: var(--radius-xs);
  transition: all var(--transition);
  width: 100%;
  text-align: left;
}

.btn-ghost-sm:hover {
  background: var(--surface-2);
  color: var(--text);
}

/* ===== Main Chat ===== */
#chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

/* Header */
#chat-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: var(--radius-xs);
  transition: all var(--transition);
  flex-shrink: 0;
}

.icon-btn:hover { background: var(--bg); color: var(--text); }

.header-title {
  flex: 1;
  font-weight: 700;
  font-size: 16px;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-icon { width: 30px; height: 30px; }

.btn-ghost {
  border: 1px solid var(--border);
  background: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: var(--radius-xs);
  transition: all var(--transition);
  white-space: nowrap;
}

.btn-ghost:hover {
  background: var(--bg);
  border-color: var(--border-hover);
  color: var(--text);
}

/* Messages container */
#messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  scroll-behavior: smooth;
}

#messages {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Typing indicator */
#typing-indicator {
  max-width: 760px;
  margin: 0 auto 8px;
  padding: 0 20px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.typing-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px 18px 18px 4px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 4px;
  box-shadow: var(--shadow-sm);
}

.dot {
  width: 7px;
  height: 7px;
  background: var(--text-light);
  border-radius: 50%;
  animation: bounce 1.2s ease-in-out infinite;
}

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

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%            { transform: translateY(-6px); }
}

/* Messages */
.message {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  max-width: 85%;
}

.ta-message { align-self: flex-start; }
.user-message { align-self: flex-end; flex-direction: row-reverse; }

.msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.msg-avatar img {
  width: 30px;
  height: 30px;
}

.user-message .msg-avatar {
  background: var(--primary);
  font-size: 14px;
  font-weight: 700;
  color: white;
}

.msg-bubble {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-content {
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 14.5px;
  line-height: 1.65;
  word-break: break-word;
}

.ta-message .msg-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px 18px 18px 4px;
  box-shadow: var(--shadow-sm);
  color: var(--text);
}

.user-message .msg-content {
  background: var(--primary);
  color: white;
  border-radius: 18px 18px 4px 18px;
}

.msg-label {
  font-size: 11px;
  color: var(--text-light);
  padding: 0 4px;
}

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

/* Markdown styles inside TA messages */
.ta-message .msg-content p { margin-bottom: 10px; }
.ta-message .msg-content p:last-child { margin-bottom: 0; }

.ta-message .msg-content h1,
.ta-message .msg-content h2,
.ta-message .msg-content h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 14px 0 6px;
  color: var(--text);
}

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

.ta-message .msg-content li { margin-bottom: 4px; }

.ta-message .msg-content code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  color: #be185d;
}

.ta-message .msg-content pre {
  background: #1e293b;
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
}

.ta-message .msg-content pre code {
  background: none;
  border: none;
  padding: 0;
  color: #e2e8f0;
  font-size: 13px;
}

.ta-message .msg-content blockquote {
  border-left: 3px solid var(--primary);
  padding-left: 12px;
  color: var(--text-muted);
  margin: 8px 0;
}

.ta-message .msg-content strong { font-weight: 700; }
.ta-message .msg-content em { font-style: italic; }

.ta-message .msg-content table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
  margin: 10px 0;
}

.ta-message .msg-content th,
.ta-message .msg-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.ta-message .msg-content th {
  background: var(--surface-2);
  font-weight: 600;
}

/* ===== Input Area ===== */
#input-area {
  padding: 12px 20px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  align-self: center;
}

.textarea-wrapper {
  flex: 1;
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  transition: border-color var(--transition);
}

.textarea-wrapper:focus-within {
  border-color: var(--primary);
  background: var(--surface);
}

#msg-input {
  width: 100%;
  border: none;
  background: none;
  outline: none;
  font-size: 14.5px;
  color: var(--text);
  resize: none;
  line-height: 1.5;
  max-height: 160px;
  overflow-y: auto;
  font-family: inherit;
}

#msg-input::placeholder { color: var(--text-light); }

.btn-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all var(--transition);
  box-shadow: var(--shadow-sm);
}

.btn-send:hover:not(:disabled) { background: var(--primary-hover); box-shadow: var(--shadow); }
.btn-send:active:not(:disabled) { transform: scale(0.95); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ===== Responsive / Mobile ===== */
@media (max-width: 640px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    transform: translateX(-100%);
    width: var(--sidebar-width);
  }

  #sidebar.mobile-open {
    transform: translateX(0);
  }

  .login-card { padding: 36px 24px; }

  #messages-container { padding: 16px 12px; }

  #input-area { padding: 10px 12px; }

  .message { max-width: 95%; }
}
