/* ==================== CSS Variables & Reset ==================== */
:root {
  --primary: #10a37f;
  --primary-hover: #0e8c6b;
  --primary-light: #e6f7f2;
  --bg-main: #ffffff;
  --bg-sidebar: #f7f7f8;
  --bg-chat: #f5f5f5;
  --text-primary: #1a1a2e;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border-light: #e5e7eb;
  --border-medium: #d1d5db;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-full: 9999px;
  --transition: all 0.2s ease;
  --sidebar-width: 280px;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans SC', sans-serif;
  background: var(--bg-main);
  color: var(--text-primary);
  height: 100vh; overflow: hidden;
}

/* ==================== Layout ==================== */
.app-layout {
  display: flex; height: 100vh; width: 100vw;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-light);
  display: flex; flex-direction: column;
  height: 100vh;
  overflow: hidden;
}
.sidebar-header {
  padding: 16px; border-bottom: 1px solid var(--border-light);
  display: flex; align-items: center; justify-content: space-between;
}
.sidebar-header .logo {
  font-size: 1.1rem; font-weight: 700; color: var(--primary);
  display: flex; align-items: center; gap: 6px;
}
.sidebar-header .logo-sub { font-size: 0.7rem; color: var(--text-muted); font-weight: 400; }
.new-chat-btn {
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-medium); background: white;
  cursor: pointer; font-size: 1.2rem; color: var(--text-secondary);
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.new-chat-btn:hover { background: var(--primary-light); border-color: var(--primary); color: var(--primary); }

/* Conversation list */
.conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  padding: 10px 12px; border-radius: var(--radius-sm);
  cursor: pointer; transition: var(--transition);
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 2px; position: relative;
}
.conv-item:hover { background: #e5e7eb; }
.conv-item.active { background: var(--primary-light); }
.conv-item .conv-icon { font-size: 0.9rem; color: var(--text-muted); flex-shrink: 0; }
.conv-item .conv-title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 0.85rem; color: var(--text-primary);
}
.conv-item .conv-actions {
  display: none; gap: 2px; flex-shrink: 0;
}
.conv-item:hover .conv-actions { display: flex; }
.conv-action-btn {
  width: 24px; height: 24px; border: none; background: transparent;
  border-radius: 4px; cursor: pointer; font-size: 0.7rem;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted);
}
.conv-action-btn:hover { background: #d1d5db; color: var(--text-primary); }
.conv-action-btn.del:hover { color: #ef4444; }

/* Sidebar footer (user area) */
.sidebar-footer {
  padding: 12px 16px; border-top: 1px solid var(--border-light);
}

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

/* Chat header */
.chat-header {
  padding: 12px 24px; border-bottom: 1px solid var(--border-light);
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0; background: white;
}
.chat-header-left { display: flex; align-items: center; gap: 12px; }
.mobile-menu-btn { display: none; background: none; border: none; font-size: 1.2rem; cursor: pointer; padding: 4px; }
.chat-header h2 { font-size: 1rem; font-weight: 600; color: var(--text-primary); }
.chat-header-right { display: flex; align-items: center; gap: 8px; }

/* Physician mode selector */
.physician-selector select {
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-medium);
  background: var(--bg-main);
  font-size: 0.8rem;
  color: var(--text-primary);
  cursor: pointer;
  outline: none;
  transition: all 0.2s ease;
}
.physician-selector select:hover {
  border-color: var(--primary);
}
.physician-selector select.mode-zhongjing {
  border-color: #8B4513;
  background: #FFF8F0;
  color: #8B4513;
  font-weight: 600;
}

/* Mode toggle */
.mode-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  cursor: pointer; user-select: none; padding: 4px 8px;
  border-radius: var(--radius-full); background: var(--bg-sidebar);
}
.mode-label { font-size: 0.75rem; color: var(--text-muted); transition: var(--transition); }
.mode-label.active { color: var(--primary); font-weight: 600; }
.toggle-track {
  position: relative; width: 36px; height: 20px;
  background: var(--border-medium); border-radius: 10px;
  transition: var(--transition);
}
.toggle-track.active { background: var(--primary); }
.toggle-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; background: white;
  border-radius: 50%; transition: var(--transition);
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}
.toggle-thumb.active { left: 18px; }

/* Chat messages */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 24px;
  background: var(--bg-chat); position: relative;
}
.message { margin-bottom: 20px; display: flex; }
.message.user { justify-content: flex-end; }
.message.assistant { justify-content: flex-end; }
.message-content {
  padding: 12px 18px;
  border-radius: var(--radius-lg); line-height: 1.7;
  word-wrap: break-word; font-size: 0.9rem;
}
.user .message-content {
  max-width: 80%;
  background: var(--primary); color: white;
  border-bottom-right-radius: 4px;
}
.assistant .message-content {
  width: 100%;
  background: white; color: var(--text-primary);
  border: 1px solid var(--border-light);
  border-bottom-right-radius: 4px;
  box-shadow: var(--shadow-sm);
}
/* Markdown 表格样式 */
.message-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 0.85rem;
  font-family: "SF Mono", "Menlo", "Monaco", "Courier New", "Noto Sans SC", monospace;
  overflow-x: auto;
  display: block;
}
.message-content th,
.message-content td {
  border: 1px solid #9ca3af;
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
  white-space: pre;
}
.message-content th {
  background-color: #e5e7eb;
  font-weight: 600;
}
.message-content tr:nth-child(even) {
  background-color: #f3f4f6;
}
.user .message-content table {
  color: white;
  border-color: rgba(255,255,255,0.3);
}
.user .message-content th {
  background-color: rgba(255,255,255,0.15);
  border-color: rgba(255,255,255,0.3);
}
.user .message-content td {
  border-color: rgba(255,255,255,0.3);
}
.user .message-content tr:nth-child(even) {
  background-color: rgba(255,255,255,0.08);
}

.message-time { font-size: 0.65rem; opacity: 0.6; margin-top: 4px; }
.user .message-time { text-align: right; color: rgba(255,255,255,0.7); }
.assistant .message-time { color: var(--text-muted); }

/* Agent indicator */
.agent-indicator {
  text-align: center; padding: 6px; font-size: 0.75rem; color: var(--text-muted);
  display: none; align-items: center; justify-content: center; gap: 6px;
}
.agent-indicator.active { display: flex; }
.status-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--border-medium);
}
.status-dot.connected { background: #22c55e; }
.status-dot.disconnected { background: #ef4444; }

/* Suggestion Bar */
.suggestion-bar {
  display: none; flex-wrap: wrap; gap: 8px;
  padding: 8px 24px; background: var(--primary-light);
  border-top: 1px solid var(--border-light);
}
.suggestion-btn {
  padding: 6px 14px; background: white; color: var(--primary);
  border: 1px solid var(--primary); border-radius: var(--radius-full);
  cursor: pointer; font-size: 0.82rem; transition: var(--transition);
  white-space: normal; text-align: left; max-width: 100%;
  line-height: 1.4;
}
.suggestion-btn:hover { background: var(--primary); color: white; }

/* Input area */
.chat-input-area {
  padding: 16px 24px; background: white;
  border-top: 1px solid var(--border-light); flex-shrink: 0;
}
.input-wrapper {
  display: flex; gap: 8px; align-items: flex-end;
  background: var(--bg-sidebar); border: 1px solid var(--border-light);
  border-radius: var(--radius-lg); padding: 8px 12px;
  transition: var(--transition);
}
.input-wrapper:focus-within { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(16,163,127,0.15); }
.input-wrapper textarea {
  flex: 1; border: none; background: transparent;
  font-size: 0.9rem; outline: none; resize: none;
  font-family: inherit; line-height: 1.5; max-height: 120px;
  color: var(--text-primary);
}
.input-wrapper textarea::placeholder { color: var(--text-muted); }
.send-btn {
  padding: 8px 16px; background: var(--primary); color: white;
  border: none; border-radius: var(--radius-sm); cursor: pointer;
  font-size: 0.85rem; transition: var(--transition);
  white-space: nowrap; height: 36px;
}
.send-btn:hover { background: var(--primary-hover); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Disclaimer */
.disclaimer {
  text-align: center; padding: 8px; background: #fffbeb;
  color: #b45309; font-size: 0.75rem;
  border-top: 1px solid #fde68a; flex-shrink: 0;
}

/* ==================== Auth Modal ==================== */
.auth-overlay {
  display: none; position: fixed; z-index: 2000;
  inset: 0; background: rgba(0,0,0,0.5);
  align-items: center; justify-content: center;
}
.auth-overlay.show { display: flex; }
.auth-modal {
  background: white; border-radius: var(--radius-lg);
  padding: 32px; width: 380px; max-width: 90vw;
  box-shadow: var(--shadow-lg); position: relative;
}
.auth-modal h2 { text-align: center; margin-bottom: 20px; color: var(--text-primary); font-size: 1.3rem; }
.auth-close {
  position: absolute; right: 16px; top: 16px;
  background: none; border: none; font-size: 22px;
  cursor: pointer; color: var(--text-muted);
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.auth-close:hover { background: var(--bg-sidebar); color: var(--text-primary); }
.auth-tabs {
  display: flex; margin-bottom: 20px;
  border-bottom: 2px solid var(--border-light);
}
.auth-tab {
  flex: 1; text-align: center; padding: 10px;
  cursor: pointer; color: var(--text-muted); font-size: 0.9rem;
  border-bottom: 2px solid transparent; margin-bottom: -2px;
  transition: var(--transition);
}
.auth-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.auth-form { display: none; }
.auth-form.active { display: block; }
.auth-form label { display: block; margin-bottom: 4px; color: var(--text-secondary); font-size: 0.8rem; }
.auth-form input {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border-medium); border-radius: var(--radius-sm);
  font-size: 0.9rem; outline: none; margin-bottom: 12px;
}
.auth-form input:focus { border-color: var(--primary); }
.auth-phone-row { display: flex; gap: 8px; }
.auth-phone-row input { flex: 1; }
.sms-btn {
  padding: 10px 12px; background: var(--primary); color: white;
  border: none; border-radius: var(--radius-sm); cursor: pointer;
  font-size: 0.8rem; white-space: nowrap; height: 42px;
}
.sms-btn:disabled { background: var(--border-medium); cursor: not-allowed; }
.auth-submit {
  width: 100%; padding: 12px; background: var(--primary);
  color: white; border: none; border-radius: var(--radius-sm);
  font-size: 1rem; cursor: pointer; margin-top: 4px;
}
.auth-submit:disabled { opacity: 0.6; cursor: not-allowed; }
.auth-error { color: #ef4444; font-size: 0.8rem; margin-bottom: 10px; display: none; }
.auth-error.show { display: block; }

/* ==================== User Profile (Sidebar Footer) ==================== */
.sidebar-user {
  display: flex; align-items: center; gap: 8px; cursor: pointer;
  padding: 4px; border-radius: var(--radius-sm);
  transition: var(--transition); position: relative;
}
.sidebar-user:hover { background: #e5e7eb; }
.user-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--primary); display: flex; align-items: center;
  justify-content: center; color: white; font-size: 0.8rem;
  font-weight: 600; flex-shrink: 0; overflow: hidden;
}
.user-avatar img { width: 100%; height: 100%; object-fit: cover; }
.user-name { font-size: 0.85rem; color: var(--text-primary); flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-dropdown {
  display: none; position: absolute; bottom: 44px; left: 0;
  background: white; border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg); min-width: 160px; z-index: 100;
  overflow: hidden;
}
.user-dropdown.show { display: block; }
.user-dropdown-item {
  padding: 10px 14px; color: var(--text-primary); font-size: 0.85rem;
  cursor: pointer; transition: background 0.15s; display: flex;
  align-items: center; gap: 8px;
}
.user-dropdown-item:hover { background: var(--bg-sidebar); }
.user-dropdown-item.logout { color: #ef4444; }
.auth-btn {
  width: 100%; padding: 8px; background: var(--primary);
  color: white; border: none; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 0.85rem;
}
.auth-btn:hover { background: var(--primary-hover); }

/* ==================== Scroll-to-bottom button ==================== */
.scroll-bottom-btn {
  position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--primary); color: white; border: none;
  border-radius: 20px; padding: 8px 20px; font-size: 0.8rem;
  cursor: pointer; box-shadow: 0 2px 12px rgba(0,0,0,0.2);
  z-index: 10; transition: opacity 0.3s; white-space: nowrap;
  animation: fadeInUp 0.3s ease;
}
.scroll-bottom-btn:hover { background: var(--primary-hover); }
@keyframes fadeInUp {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ==================== Toast ==================== */
.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: var(--text-primary); color: white;
  padding: 10px 20px; border-radius: var(--radius-full);
  font-size: 0.85rem; z-index: 3000; opacity: 1;
  transition: opacity 0.3s; pointer-events: none;
}

/* ==================== Loading ==================== */
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
.loading-spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top: 2px solid white; border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.loading-dots::after {
  content: ''; animation: dots 1.5s infinite;
}
@keyframes dots {
  0% { content: ''; }
  33% { content: '.'; }
  66% { content: '..'; }
  100% { content: '...'; }
}

/* ==================== PDF Modal ==================== */
.pdf-modal { display: none; position: fixed; z-index: 1000; inset: 0; background: #000; }
.pdf-modal.show { display: block; }
.pdf-modal-content { display: flex; flex-direction: column; height: 100%; }
.pdf-modal-header {
  padding: 10px 16px; background: #2c3e50; color: white;
  display: flex; justify-content: space-between; align-items: center; flex-shrink: 0;
}
.pdf-modal-title { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.pdf-page-badge { background: rgba(255,255,255,0.15); padding: 4px 10px; border-radius: var(--radius-full); font-size: 0.75rem; }
.pdf-close { background: none; border: none; color: white; font-size: 1.5rem; cursor: pointer; padding: 4px 8px; }
.pdf-close:hover { background: rgba(255,255,255,0.1); border-radius: 50%; }
.pdf-container { flex: 1; overflow: auto; background: #2c2c2c; display: flex; justify-content: center; align-items: flex-start; padding: 20px; }
.pdf-container canvas { display: block; box-shadow: 0 4px 20px rgba(0,0,0,0.3); }
.pdf-modal-footer { padding: 10px 16px; background: #2c3e50; flex-shrink: 0; }
.pdf-controls { display: flex; justify-content: center; align-items: center; gap: 8px; flex-wrap: wrap; }
.pdf-control-btn {
  padding: 8px 14px; background: #3498db; color: white;
  border: none; border-radius: var(--radius-full); cursor: pointer; font-size: 0.85rem;
}
.pdf-control-btn:disabled { background: #7f8c8d; cursor: not-allowed; opacity: 0.5; }
.pdf-zoom-level { background: #34495e; padding: 6px 12px; border-radius: var(--radius-full); color: white; font-size: 0.75rem; }

/* ==================== Responsive ==================== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed; z-index: 100; left: 0; top: 0;
    transform: translateX(-100%); transition: transform 0.3s ease;
    box-shadow: none;
  }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }
  .sidebar-backdrop {
    display: none; position: fixed; z-index: 99;
    inset: 0; background: rgba(0,0,0,0.4);
  }
  .sidebar-backdrop.show { display: block; }
  .mobile-menu-btn { display: block !important; }
  .message-content { max-width: 90%; }
  .message-content table { font-size: 0.75rem; }
  .message-content th,
  .message-content td { padding: 4px 6px; white-space: normal; }
  .chat-messages { padding: 16px; }
  .chat-header { padding: 10px 16px; }
  .chat-input-area { padding: 12px 16px; }
  .auth-modal { padding: 24px 20px; }
}

@media (max-width: 480px) {
  .message-content { max-width: 95%; font-size: 0.85rem; }
  .chat-header h2 { font-size: 0.9rem; }
  .mode-label { font-size: 0.7rem; }
}
