:root {
  --bg-primary: #090d16;
  --bg-secondary: #0f172a;
  --bg-card: rgba(30, 41, 59, 0.75);
  --bg-card-hover: rgba(51, 65, 85, 0.85);
  --border-color: rgba(148, 163, 184, 0.18);
  --border-focus: #38bdf8;
  --text-primary: #f8fafc;
  --text-muted: #94a3b8;
  --primary: #38bdf8;
  --primary-hover: #0284c7;
  --primary-glow: rgba(56, 189, 248, 0.25);
  --accent: #818cf8;
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans TC", sans-serif;
}

[data-theme="light"] {
  --bg-primary: #f8fafc;
  --bg-secondary: #f1f5f9;
  --bg-card: rgba(255, 255, 255, 0.95);
  --bg-card-hover: #f1f5f9;
  --border-color: rgba(203, 213, 225, 0.8);
  --border-focus: #0284c7;
  --text-primary: #0f172a;
  --text-muted: #64748b;
  --primary: #0284c7;
  --primary-glow: rgba(2, 132, 199, 0.15);
}

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

html, body {
  height: 100%;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 頂部導覽列 (固定頂端) */
.site-header {
  height: 60px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  backdrop-filter: blur(12px);
  flex-shrink: 0;
  z-index: 100;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 700;
  font-size: 1.12rem;
}

.brand-badge {
  background: var(--primary-glow);
  color: var(--primary);
  border: 1px solid var(--primary);
  font-size: 0.72rem;
  padding: 0.2rem 0.55rem;
  border-radius: 9999px;
  font-weight: 600;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.9rem;
  font-size: 0.86rem;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.2s ease;
  font-family: inherit;
}

.btn-primary {
  background: linear-gradient(135deg, var(--primary), #2563eb);
  color: #fff;
  box-shadow: 0 4px 12px var(--primary-glow);
}
.btn-primary:hover {
  opacity: 0.92;
  transform: translateY(-1px);
}

.btn-outline {
  background: transparent;
  border-color: var(--border-color);
  color: var(--text-primary);
}
.btn-outline:hover {
  background: var(--bg-card-hover);
  border-color: var(--primary);
}

.btn-admin {
  background: rgba(245, 158, 11, 0.15);
  border-color: rgba(245, 158, 11, 0.4);
  color: var(--warning);
}
.btn-admin:hover {
  background: rgba(245, 158, 11, 0.25);
}

/* =========================================================================
   核心版型：左邊 1/3 (本次回答依據規章出處) + 右邊 2/3 (問答對話與回答正文)
   ========================================================================= */
.main-container {
  flex: 1;
  display: flex;
  height: calc(100vh - 60px);
  overflow: hidden;
  position: relative;
}

/* 左側欄：約佔欄寬的 1/3 (32% ~ 34%)，專門展示「本次回答依據規章出處」 */
.source-sidebar {
  width: 33.333%;
  min-width: 300px;
  max-width: 440px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.source-sidebar-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
  background: rgba(15, 23, 42, 0.6);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.source-sidebar-title {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 0.45rem;
}
.source-match-badge {
  font-size: 0.75rem;
  color: var(--success);
  font-weight: 700;
  background: rgba(52, 211, 153, 0.12);
  border: 1px solid rgba(52, 211, 153, 0.3);
  padding: 0.15rem 0.5rem;
  border-radius: 9999px;
}

.source-sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.source-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 1rem;
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.source-card:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
}

.source-card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 0.4rem;
}
.source-card-doc {
  font-weight: 700;
  font-size: 0.88rem;
  color: #38bdf8;
  line-height: 1.4;
}
.source-card-sim {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--success);
  background: rgba(52, 211, 153, 0.12);
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  flex-shrink: 0;
}
.source-card-sec {
  font-size: 0.8rem;
  font-weight: 600;
  color: #bae6fd;
  margin-bottom: 0.5rem;
}
.source-card-body {
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.6;
  background: rgba(15, 23, 42, 0.4);
  padding: 0.65rem 0.8rem;
  border-radius: 6px;
  border: 1px solid rgba(148, 163, 184, 0.1);
  white-space: pre-wrap;
}

.source-empty-state {
  text-align: center;
  margin: auto;
  padding: 2rem 1rem;
  color: var(--text-muted);
  font-size: 0.88rem;
  line-height: 1.6;
}

/* 右側問答區：約佔欄寬的 2/3 (66.666%) */
.chat-main-column {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg-primary);
  overflow: hidden;
}

/* 對話獨立滾動區域 (底部保留 120px 空間，絕不遮擋回答) */
.chat-scroll-area {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.75rem 130px 1.75rem;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* 頂部快捷問題膠囊列 */
.top-pills-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-color);
}
.question-pill {
  padding: 0.4rem 0.8rem;
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.2);
  color: var(--text-primary);
  border-radius: 9999px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s ease;
  line-height: 1.3;
}
.question-pill:hover {
  background: rgba(56, 189, 248, 0.22);
  border-color: var(--primary);
  transform: translateY(-1px);
}

/* 歡迎橫幅 */
.welcome-hero {
  text-align: center;
  padding: 2rem 1rem 1rem 1rem;
}
.welcome-hero h2 {
  font-size: 1.75rem;
  margin-bottom: 0.6rem;
  background: linear-gradient(135deg, #fff, var(--primary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.welcome-hero p {
  color: var(--text-muted);
  line-height: 1.6;
  font-size: 0.94rem;
  max-width: 600px;
  margin: 0 auto;
}

/* 對話訊息泡泡 */
.message-bubble {
  display: flex;
  gap: 1rem;
  width: 100%;
  animation: fadeIn 0.25s ease;
}

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

.message-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
}
.message-user .message-avatar {
  background: linear-gradient(135deg, var(--primary), var(--accent));
}

.message-content {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 1.3rem 1.6rem;
  border-radius: 12px;
  line-height: 1.8;
  font-size: 0.96rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}
.message-user .message-content {
  flex: none;
  max-width: 80%;
  background: rgba(56, 189, 248, 0.15);
  border-color: rgba(56, 189, 248, 0.35);
  color: #fff;
}

.answer-text {
  white-space: pre-wrap;
  word-break: break-word;
}
.answer-text h3 {
  font-size: 1.08rem;
  margin: 1.1rem 0 0.4rem 0;
  color: var(--primary);
}
.answer-text ul, .answer-text ol {
  padding-left: 1.4rem;
  margin-bottom: 0.75rem;
}
.answer-text li {
  margin-bottom: 0.35rem;
}

/* =========================================================================
   核心需求 1：提問框永遠維持在畫面最底部，絕不隨頁面滾動而移動
   ========================================================================= */
.chat-bottom-fixed-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(15, 23, 42, 0.92);
  border-top: 1px solid var(--border-color);
  backdrop-filter: blur(20px);
  padding: 1rem 1.5rem 1.25rem 1.5rem;
  z-index: 95;
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.35);
}

.chat-input-wrapper {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}

.chat-input {
  flex: 1;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 0.9rem 1.2rem;
  color: var(--text-primary);
  font-size: 0.95rem;
  outline: none;
  font-family: inherit;
  transition: all 0.2s ease;
}
.chat-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
}

/* 彈窗樣式 */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1.5rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  width: 100%;
  max-width: 480px;
  padding: 2rem;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.form-group label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}
.form-control {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  color: var(--text-primary);
  outline: none;
  font-size: 0.92rem;
  font-family: inherit;
}
.form-control:focus {
  border-color: var(--primary);
}

/* 後台控制面板 */
.admin-modal-card {
  max-width: 920px;
  height: 82vh;
}
.admin-tabs {
  display: flex;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 0.5rem;
}
.admin-tab {
  padding: 0.5rem 1.1rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px;
}
.admin-tab.active {
  background: var(--bg-card);
  color: var(--primary);
}

.admin-tab-content {
  flex: 1;
  overflow-y: auto;
  display: none;
  flex-direction: column;
  gap: 1rem;
}
.admin-tab-content.active {
  display: flex;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.admin-table th, .admin-table td {
  padding: 0.8rem;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}
.admin-table th {
  color: var(--text-muted);
  font-weight: 600;
}

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

@media (max-width: 860px) {
  .source-sidebar { display: none; }
  .chat-main-column { width: 100%; }
}
