:root {
  --bg-main: #0b0f19;
  --bg-sidebar: #0e1526;
  --bg-card: #151f32;
  --bg-input: #1a263d;
  --border-color: #24324d;
  --border-focus: #4f46e5;
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent-primary: #6366f1;
  --accent-hover: #4f46e5;
  --accent-user-bubble: #2563eb;
  --accent-bot-bubble: #172136;
  --status-healthy: #10b981;
  --status-warning: #f59e0b;
  --status-error: #ef4444;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.6);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* 應用程式全域佈局 */
.app-layout {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* ── 側邊欄 ── */
.sidebar {
  width: 280px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: var(--transition);
  z-index: 10;
}

.sidebar-header {
  padding: 1.25rem 1rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-icon {
  font-size: 1.6rem;
  background: linear-gradient(135deg, #4f46e5, #06b6d4);
  border-radius: var(--radius-sm);
  padding: 4px 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-info {
  display: flex;
  flex-direction: column;
}

.brand-title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: #fff;
}

.brand-badge {
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  color: #38bdf8;
  letter-spacing: 0.5px;
}

.btn-new-chat {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  background: linear-gradient(135deg, var(--accent-primary), #4338ca);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.35);
  transition: var(--transition);
}

.btn-new-chat:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.5);
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.5rem;
}

.section-title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0.25rem 0.75rem;
  margin-bottom: 0.5rem;
  letter-spacing: 0.5px;
}

.conversation-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.conversation-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.875rem;
  cursor: pointer;
  transition: var(--transition);
  position: relative;
}

.conversation-item:hover {
  background-color: var(--bg-card);
  color: var(--text-primary);
}

.conversation-item.active {
  background-color: var(--bg-input);
  color: #fff;
  font-weight: 500;
  border-left: 3px solid var(--accent-primary);
}

.conv-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.conv-actions {
  display: none;
  gap: 0.25rem;
}

.conversation-item:hover .conv-actions {
  display: flex;
}

.btn-conv-action {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  font-size: 0.75rem;
  border-radius: 4px;
}

.btn-conv-action:hover {
  color: var(--status-error);
  background-color: rgba(239, 68, 68, 0.1);
}

.sidebar-footer {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: rgba(11, 15, 25, 0.4);
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  overflow: hidden;
}

.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background-color: var(--bg-input);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-color);
}

.user-details {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.user-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
}

.user-email {
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.footer-actions {
  display: flex;
  gap: 0.25rem;
}

.btn-icon-only {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1rem;
  padding: 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition);
}

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

/* ── 主對話區 ── */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
  background-color: var(--bg-main);
}

.chat-header {
  height: 60px;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: rgba(14, 21, 38, 0.8);
  backdrop-filter: blur(12px);
  z-index: 5;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.agent-selector-wrapper {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.selector-label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.agent-select {
  background-color: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.4rem 0.8rem;
  font-size: 0.875rem;
  outline: none;
  cursor: pointer;
  transition: var(--transition);
}

.agent-select:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.health-badge {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.65rem;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 500;
  background-color: rgba(16, 185, 129, 0.1);
  color: var(--status-healthy);
  border: 1px solid rgba(16, 185, 129, 0.2);
}

.badge-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: currentColor;
}

.health-badge.unhealthy {
  background-color: rgba(239, 68, 68, 0.1);
  color: var(--status-error);
  border-color: rgba(239, 68, 68, 0.2);
}

.conn-status {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--status-healthy);
  box-shadow: 0 0 8px var(--status-healthy);
}

/* 訊息區域 */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* 歡迎區塊 */
.welcome-banner {
  max-width: 720px;
  margin: 3rem auto 1rem;
  text-align: center;
  padding: 2.5rem;
  background: linear-gradient(180deg, rgba(21, 31, 50, 0.8), rgba(15, 23, 42, 0.4));
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(8px);
}

.welcome-icon {
  font-size: 3rem;
  margin-bottom: 0.75rem;
}

.welcome-banner h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: #fff;
  margin-bottom: 0.5rem;
}

.welcome-banner p {
  color: var(--text-secondary);
  font-size: 0.95rem;
  margin-bottom: 1.5rem;
}

.quick-prompts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.75rem;
  text-align: left;
}

.quick-prompt-card {
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  padding: 0.85rem 1rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: var(--transition);
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
}

.quick-prompt-card:hover {
  background-color: #223250;
  border-color: var(--accent-primary);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.qp-icon {
  font-size: 1.25rem;
}

.qp-text {
  display: flex;
  flex-direction: column;
}

.qp-title {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
}

.qp-desc {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* 訊息串流清單 */
.messages-feed {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}

.message-row {
  display: flex;
  gap: 0.85rem;
  width: 100%;
}

.message-row.user {
  flex-direction: row-reverse;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 1.1rem;
  background-color: var(--bg-card);
  border: 1px solid var(--border-color);
}

.message-bubble {
  max-width: 80%;
  padding: 0.9rem 1.2rem;
  border-radius: var(--radius-lg);
  font-size: 0.925rem;
  line-height: 1.6;
  position: relative;
  box-shadow: var(--shadow-sm);
  word-break: break-word;
}

.message-row.user .message-bubble {
  background-color: var(--accent-user-bubble);
  color: #fff;
  border-bottom-right-radius: var(--radius-sm);
}

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

/* Markdown 格式樣式 */
.message-bubble p {
  margin-bottom: 0.75rem;
}
.message-bubble p:last-child {
  margin-bottom: 0;
}

.message-bubble ul, .message-bubble ol {
  margin-left: 1.25rem;
  margin-bottom: 0.75rem;
}

.message-bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.75rem 0;
  font-size: 0.85rem;
}

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

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

.message-bubble pre {
  background-color: #0b1120;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  overflow-x: auto;
  margin: 0.75rem 0;
}

.message-bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85em;
  padding: 0.2em 0.4em;
  background-color: rgba(255, 255, 255, 0.08);
  border-radius: 4px;
}

.message-bubble pre code {
  background: none;
  padding: 0;
}

/* 附件標籤在泡泡中 */
.bubble-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}

.bubble-attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.5rem;
  background-color: rgba(0, 0, 0, 0.25);
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  color: #e2e8f0;
}

/* 打字狀態指示器 */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  max-width: 860px;
  margin: 0 auto;
  padding: 0.5rem 0;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--accent-primary);
  animation: pulse 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes pulse {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

.typing-text {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-left: 0.5rem;
}

/* 底部輸入列 */
.chat-footer {
  padding: 1rem 1.5rem;
  background-color: rgba(14, 21, 38, 0.9);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border-color);
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

.attachment-preview-bar {
  margin-bottom: 0.5rem;
}

.attachment-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.attachment-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.65rem;
  background-color: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  color: var(--text-primary);
}

.chip-remove {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.chip-remove:hover {
  color: var(--status-error);
}

.input-box-wrapper {
  display: flex;
  align-items: flex-end;
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 0.4rem 0.6rem;
  transition: var(--transition);
}

.input-box-wrapper:focus-within {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.25);
}

.btn-icon-input {
  background: none;
  border: none;
  font-size: 1.25rem;
  padding: 0.5rem;
  cursor: pointer;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: var(--transition);
}

.btn-icon-input:hover {
  color: var(--accent-primary);
}

.chat-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-size: 0.95rem;
  padding: 0.6rem 0.75rem;
  resize: none;
  max-height: 150px;
  line-height: 1.4;
  font-family: inherit;
}

.btn-send {
  background: linear-gradient(135deg, var(--accent-primary), #4338ca);
  color: #fff;
  border: none;
  padding: 0.55rem 1rem;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  transition: var(--transition);
}

.btn-send:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
}

.footer-note {
  text-align: center;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}

/* 拖曳覆蓋層 */
.drop-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(11, 15, 25, 0.85);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  border: 2px dashed var(--accent-primary);
}

.drop-modal {
  text-align: center;
  color: #fff;
}

.drop-icon {
  font-size: 3.5rem;
  display: block;
  margin-bottom: 1rem;
  animation: bounce 1s infinite alternate;
}

@keyframes bounce {
  from { transform: translateY(0); }
  to { transform: translateY(-10px); }
}

/* 模態視窗 (Modal) */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-card {
  background-color: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 2rem;
  width: 90%;
  max-width: 440px;
  box-shadow: var(--shadow-lg);
  position: relative;
}

.modal-large {
  max-width: 800px;
  max-height: 85vh;
  overflow-y: auto;
}

.modal-header {
  margin-bottom: 1.5rem;
  display: flex;
  flex-direction: column;
  position: relative;
}

.modal-large .modal-header {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.btn-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 4px;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.login-icon {
  font-size: 1.8rem;
}

.login-sub {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  text-align: left;
}

.form-group label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.form-input {
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.65rem 0.85rem;
  color: var(--text-primary);
  font-size: 0.9rem;
  outline: none;
  transition: var(--transition);
}

.form-input:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.btn-block {
  width: 100%;
  padding: 0.75rem;
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent-primary), #4338ca);
  color: #fff;
  border: none;
  cursor: pointer;
}

.alert-error {
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius-md);
  background-color: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  font-size: 0.85rem;
}

/* 管理後台樣式 */
.admin-tabs {
  display: flex;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 1.25rem;
}

.tab-btn {
  background: none;
  border: none;
  padding: 0.5rem 1rem;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: var(--transition);
}

.tab-btn.active {
  color: var(--accent-primary);
  border-bottom-color: var(--accent-primary);
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

.admin-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.admin-table th, .admin-table td {
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--border-color);
  text-align: left;
}

.admin-table th {
  color: var(--text-muted);
  font-weight: 600;
}

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
  border-radius: var(--radius-sm);
  background-color: var(--accent-primary);
  color: #fff;
  border: none;
  cursor: pointer;
}

.btn-secondary {
  background-color: var(--bg-input);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 0.5rem 1rem;
  border-radius: var(--radius-md);
  cursor: pointer;
}

.btn-danger {
  background-color: rgba(239, 68, 68, 0.2);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.3);
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.discover-group {
  display: flex;
  gap: 0.5rem;
}

.discover-group .form-input {
  flex: 1;
}

.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .mobile-only {
    display: inline-block;
  }
  .sidebar {
    position: absolute;
    left: -280px;
    height: 100%;
  }
  .sidebar.open {
    left: 0;
  }
}
