/* CSS 變數定義與色彩系統 */
:root {
  --bg-color: #0b0c16;
  --panel-bg: rgba(18, 19, 38, 0.65);
  --panel-border: rgba(255, 255, 255, 0.08);
  
  --text-primary: #f3f4f6;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  
  --primary-glow: linear-gradient(135deg, #7c3aed, #db2777);
  --primary-hover: linear-gradient(135deg, #8b5cf6, #ec4899);
  --accent-color: #a78bfa;
  --danger-color: #ef4444;
  --success-color: #10b981;
  
  --chat-user-bg: linear-gradient(135deg, #6d28d9, #be185d);
  --chat-ai-bg: rgba(255, 255, 255, 0.06);
  --input-bg: rgba(10, 10, 20, 0.8);
  
  --font-sans: 'Inter', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --shadow-lg: 0 10px 30px rgba(0, 0, 0, 0.5);
  --radius-lg: 16px;
  --radius-md: 10px;
}

/* 全域樣式重設 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-color);
  color: var(--text-primary);
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  position: relative;
}

/* 背景發光光暈 (微動畫) */
.bg-glow {
  position: fixed;
  border-radius: 50%;
  filter: blur(120px);
  z-index: -1;
  opacity: 0.45;
  pointer-events: none;
}

.bg-glow-1 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, #7c3aed 0%, rgba(124, 58, 237, 0) 70%);
  top: -100px;
  right: -50px;
  animation: glowMove1 20s infinite alternate ease-in-out;
}

.bg-glow-2 {
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, #db2777 0%, rgba(219, 39, 119, 0) 70%);
  bottom: -150px;
  left: -100px;
  animation: glowMove2 25s infinite alternate ease-in-out;
}

@keyframes glowMove1 {
  0% { transform: translate(0, 0) scale(1); }
  100% { transform: translate(-100px, 80px) scale(1.2); }
}

@keyframes glowMove2 {
  0% { transform: translate(0, 0) scale(1.1); }
  100% { transform: translate(120px, -50px) scale(0.9); }
}

/* 應用程式主容器 */
.app-container {
  max-width: 1300px;
  width: 95%;
  height: calc(100vh - 40px);
  min-height: 580px;
  margin: 20px auto;
  display: flex;
  flex-direction: column;
  background: var(--panel-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

/* 頂部導覽列 */
.main-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 24px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(10, 11, 22, 0.4);
}

.logo {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.logo-highlight {
  background: var(--primary-glow);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.nav-tabs {
  display: flex;
  gap: 8px;
}

.nav-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  font-size: 0.95rem;
  font-weight: 500;
  padding: 10px 18px;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

.nav-btn.active {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.12);
  color: var(--text-primary);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.model-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.model-select {
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  outline: none;
  cursor: pointer;
  transition: border-color 0.2s;
}

.model-select:focus {
  border-color: var(--accent-color);
}

/* 主內容區 */
.main-content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  min-height: 0;
}

.tab-panel {
  display: none;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.tab-panel.active {
  display: flex;
  flex-direction: column;
  opacity: 1;
  pointer-events: auto;
}

/* Icons 樣式輔助 */
.icon {
  width: 18px;
  height: 18px;
}

.icon-small {
  width: 14px;
  height: 14px;
}

.icon-title {
  width: 22px;
  height: 22px;
  vertical-align: middle;
  margin-right: 8px;
  color: var(--accent-color);
}

/* ----------------------------------------------------
 * 客服聊天室排版
 * ---------------------------------------------------- */
.chat-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100%;
}

/* 聊天側邊欄 */
.chat-sidebar {
  border-right: 1px solid var(--panel-border);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  background: rgba(10, 11, 22, 0.25);
}

.btn-primary-glow {
  background: var(--primary-glow);
  border: none;
  color: var(--text-primary);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: 0 4px 15px rgba(219, 39, 119, 0.3);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.btn-primary-glow:hover {
  background: var(--primary-hover);
  box-shadow: 0 6px 20px rgba(219, 39, 119, 0.45);
  transform: translateY(-1px);
}

.session-list-container {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
}

.session-list-container h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 1px;
}

.session-list {
  list-style: none;
  overflow-y: auto;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-right: 4px;
}

/* 美化滾動條 */
::-webkit-scrollbar {
  width: 8px; /* 微幅調寬以利滑鼠抓取 */
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.35); /* 顯著調高對比度，在深色背景下清晰可見 */
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.6); /* 懸浮時高亮 */
}

/* Firefox 滾動條樣式相容性支援 */
.chat-messages, .session-list, .knowledge-list-container, .admin-layout {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.35) transparent;
}

.session-item {
  padding: 12px 14px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s ease;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid transparent;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.session-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.session-item.active {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.1);
}

.session-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
  flex-grow: 1;
}

.session-title {
  font-size: 0.88rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.btn-delete-session {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0;
  transition: all 0.2s;
}

.session-item:hover .btn-delete-session {
  opacity: 1;
}

.btn-delete-session:hover {
  color: var(--danger-color);
  background: rgba(239, 68, 68, 0.1);
}

/* 聊天主視窗 */
.chat-window {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: rgba(10, 11, 22, 0.1);
}

.chat-messages {
  flex-grow: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 0; /* 關鍵：解鎖 Flexbox 子項目的最小高度限制，防止內容將容器撐大 */
}

.message {
  display: flex;
  gap: 16px;
  max-width: 85%;
  animation: messageFadeIn 0.3s ease-out forwards;
  opacity: 0;
  transform: translateY(12px);
}

@keyframes messageFadeIn {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

.message.system-msg {
  align-self: flex-start;
}

.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}

.user-msg .avatar {
  background: linear-gradient(135deg, #db2777, #7c3aed);
  color: #fff;
}

.system-msg .avatar {
  background: #2563eb;
  color: #fff;
}

.message-bubble {
  padding: 14px 18px;
  border-radius: 18px;
  line-height: 1.55;
  font-size: 0.95rem;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

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

.system-msg .message-bubble {
  background: var(--chat-ai-bg);
  border: 1px solid var(--panel-border);
  color: var(--text-primary);
  border-top-left-radius: 4px;
}

.message-bubble p {
  margin-bottom: 8px;
}

.message-bubble p:last-child {
  margin-bottom: 0;
}

.message-bubble code {
  background: rgba(0, 0, 0, 0.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.85em;
  color: var(--accent-color);
}

/* AI 思考中動畫 */
.typing-indicator {
  display: flex;
  gap: 4px;
  align-items: center;
  height: 20px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  background-color: var(--text-secondary);
  border-radius: 50%;
  display: inline-block;
  animation: typingBounce 1.4s infinite ease-in-out both;
}

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

@keyframes typingBounce {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* 輸入區域 */
.chat-input-area {
  padding: 20px 24px;
  border-top: 1px solid var(--panel-border);
  display: flex;
  gap: 12px;
  align-items: flex-end;
  background: rgba(10, 11, 22, 0.3);
}

#chat-input {
  flex-grow: 1;
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 14px 18px;
  color: var(--text-primary);
  font-size: 0.95rem;
  outline: none;
  resize: none;
  font-family: var(--font-sans);
  max-height: 120px;
  transition: border-color 0.2s;
}

#chat-input:focus {
  border-color: var(--accent-color);
}

.send-button {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-md);
  background: var(--primary-glow);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(219, 39, 119, 0.2);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  flex-shrink: 0;
}

.send-button:hover {
  background: var(--primary-hover);
  box-shadow: 0 4px 18px rgba(219, 39, 119, 0.35);
  transform: translateY(-1px);
}

.send-button svg {
  width: 20px;
  height: 20px;
  transform: rotate(0deg);
  transition: transform 0.2s;
}

/* ----------------------------------------------------
 * 知識庫管理排版
 * ---------------------------------------------------- */
.admin-layout {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 24px;
  padding: 24px;
  height: 100%;
  overflow-y: auto;
}

.admin-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.admin-card h2 {
  font-size: 1.2rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--panel-border);
  padding-bottom: 12px;
}

/* 新增知識表單 */
.knowledge-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

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

.form-group input,
.form-group textarea {
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  color: var(--text-primary);
  font-size: 0.92rem;
  outline: none;
  font-family: var(--font-sans);
  transition: border-color 0.2s;
}

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

.btn-submit {
  background: var(--primary-glow);
  border: none;
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 14px;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.3s;
  margin-top: 10px;
}

.btn-submit:hover {
  background: var(--primary-hover);
  box-shadow: 0 4px 15px rgba(219, 39, 119, 0.3);
}

/* 知識列表 */
.knowledge-list-container {
  flex-grow: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-right: 6px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 250px;
  color: var(--text-muted);
  gap: 14px;
}

.icon-large {
  width: 50px;
  height: 50px;
}

.article-item {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: all 0.2s;
  animation: messageFadeIn 0.3s ease-out;
}

.article-item:hover {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(255, 255, 255, 0.12);
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
}

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

.btn-delete-article {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  transition: all 0.2s;
}

.btn-delete-article:hover {
  color: var(--danger-color);
  background: rgba(239, 68, 68, 0.1);
}

.article-content-text {
  font-size: 0.88rem;
  color: var(--text-secondary);
  line-height: 1.5;
  white-space: pre-wrap;
  background: rgba(0, 0, 0, 0.15);
  padding: 10px;
  border-radius: 6px;
  max-height: 120px;
  overflow-y: auto;
}

.article-footer {
  font-size: 0.72rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}

/* ----------------------------------------------------
 * 頁尾與 Widget 提示
 * ---------------------------------------------------- */
.app-footer {
  width: 95%;
  max-width: 1300px;
  margin: 0 auto 20px auto;
  text-align: center;
}

.widget-tip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: rgba(18, 19, 38, 0.5);
  border: 1px solid var(--panel-border);
  padding: 10px 18px;
  border-radius: 30px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  box-shadow: var(--shadow-lg);
  backdrop-filter: blur(10px);
}

#embed-code {
  background: rgba(0, 0, 0, 0.3);
  padding: 4px 10px;
  border-radius: 6px;
  color: var(--accent-color);
  font-family: monospace;
  font-size: 0.82rem;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

/* 響應式佈局微調 */
@media (max-width: 1024px) {
  .chat-layout {
    grid-template-columns: 220px 1fr;
  }
  .admin-layout {
    grid-template-columns: 1fr;
    height: auto;
    overflow-y: visible;
  }
}

@media (max-width: 768px) {
  .chat-layout {
    grid-template-columns: 1fr;
    height: calc(100vh - 200px);
  }
  .chat-sidebar {
    display: none; /* 在手機上隱藏側邊欄以提升聊天空間 */
  }
  .main-header {
    flex-direction: column;
    gap: 12px;
    padding: 14px;
  }
  .nav-tabs {
    width: 100%;
    justify-content: center;
  }
}

/* ----------------------------------------------------
 * Widget 嵌入模式樣式覆寫
 * ---------------------------------------------------- */
body.widget-mode {
  background: transparent;
  min-height: 100vh;
  margin: 0;
  padding: 0;
}

body.widget-mode .bg-glow {
  display: none; /* 在 iframe 中隱藏背景光暈 */
}

body.widget-mode .app-container {
  width: 100%;
  height: 100vh;
  margin: 0;
  border: none;
  border-radius: 0;
  box-shadow: none;
  background: rgba(11, 12, 22, 0.98);
}

body.widget-mode .main-header,
body.widget-mode .app-footer,
body.widget-mode .chat-sidebar {
  display: none !important;
}

body.widget-mode .chat-layout {
  grid-template-columns: 1fr;
  height: 100vh;
}

/* ----------------------------------------------------
 * 檔案拖曳上傳區樣式 (Drag & Drop Zone)
 * ---------------------------------------------------- */
.drop-zone {
  border: 2px dashed rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-md);
  padding: 24px;
  background: rgba(255, 255, 255, 0.01);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  cursor: pointer;
  text-align: center;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
}

.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent-color);
  background: rgba(167, 139, 250, 0.05);
  box-shadow: 0 0 15px rgba(167, 139, 250, 0.15);
}

.drop-zone.dragover .icon-upload {
  transform: translateY(-4px) scale(1.05);
  color: var(--accent-color);
}

.icon-upload {
  width: 36px;
  height: 36px;
  color: var(--text-secondary);
  transition: all 0.3s ease;
}

.drop-zone-text {
  font-size: 0.88rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

.browse-link {
  color: var(--accent-color);
  text-decoration: underline;
  font-weight: 600;
}

/* 載入中動畫 */
.animate-spin {
  animation: spin 1s linear infinite;
}

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


