/* ── Claude 風格介面 ── */
:root {
  --bg: #f9f9f8;
  --sidebar: #f4f0eb;
  --surface: #ffffff;
  --surface-hover: #f0ede8;
  --border: #e5e0d8;
  --primary: #d97941;       /* Claude 橙色 */
  --primary-hover: #c46b33;
  --text: #1a1a1a;
  --text-muted: #6b6b6b;
  --text-light: #9a9a9a;
  --user-bubble: #f4f0eb;
  --ai-bubble: #ffffff;
  --radius-sm: 6px;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow: 0 1px 6px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 20px rgba(0,0,0,0.10);
}

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

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

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ════════════════════════════════════════
   LAYOUT  — sidebar + main
════════════════════════════════════════ */
.app-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ── */
.sidebar {
  width: 290px;
  min-width: 290px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem 0;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 1.25rem 1rem;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
}
.sidebar-logo .logo-icon {
  width: 28px; height: 28px;
  background: var(--primary);
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  color: white; font-size: 0.9rem; font-weight: 700;
}

.sidebar-new-chat {
  margin: 0 0.75rem 0.5rem;
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  color: var(--text-muted);
  cursor: pointer;
  border: none; background: none;
  width: calc(100% - 1.5rem);
  text-align: left;
  transition: background 0.15s;
}
.sidebar-new-chat:hover { background: var(--surface-hover); }

.sidebar-section { padding: 0 0.75rem; margin-top: 0.5rem; }
.sidebar-section-label {
  font-size: 0.72rem; font-weight: 600; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--text-light);
  padding: 0.4rem 0.5rem 0.25rem;
}
.sidebar-item {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.5rem 0.7rem;
  border-radius: var(--radius-sm);
  font-size: 0.88rem; color: var(--text-muted);
  cursor: pointer; border: none; background: none;
  width: 100%; text-align: left;
  transition: background 0.15s;
  text-decoration: none;
}
.sidebar-item:hover { background: var(--surface-hover); color: var(--text); text-decoration: none; }
.sidebar-item.active { background: var(--surface); color: var(--text); font-weight: 500; }

/* ── 歷史對話列表 ── */
#history-section { overflow-y: auto; max-height: 40vh; }
.conv-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.45rem 0.7rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  gap: 0.4rem;
}
.conv-item:hover { background: var(--surface-hover); }
.conv-item.active { background: var(--surface); }
.conv-title {
  flex: 1; min-width: 0;
  font-size: 0.85rem; color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-item.active .conv-title { color: var(--text); font-weight: 500; }
.conv-del-btn {
  display: none;
  background: none; border: none;
  color: var(--text-light); font-size: 1rem;
  cursor: pointer; padding: 0 0.2rem; line-height: 1;
  flex-shrink: 0;
}
.conv-item:hover .conv-del-btn { display: block; }
.conv-del-btn:hover { color: #c62828; }
.conv-title-input {
  flex: 1; min-width: 0;
  font-size: 0.85rem; font-family: inherit;
  border: 1px solid var(--primary); border-radius: 4px;
  padding: 0.1rem 0.3rem; outline: none;
  background: var(--surface); color: var(--text);
}

.sidebar-bottom {
  margin-top: auto;
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}
.sidebar-user {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.5rem 0.5rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
}
.sidebar-user:hover { background: var(--surface-hover); }
.user-avatar {
  width: 30px; height: 30px; 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;
}
.user-info { flex: 1; overflow: hidden; }
.user-email { font-size: 0.8rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-credits {
  font-size: 0.75rem; font-weight: 600;
  color: var(--primary);
}
.user-credits.low { color: #d32f2f; }

/* ── Main content ── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* ── Top bar ── */
.topbar {
  height: 52px; min-height: 52px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar-title {
  font-size: 0.95rem; font-weight: 600; color: var(--text);
}
.topbar-right { display: flex; align-items: center; gap: 0.75rem; }

.btn-ghost {
  background: none; border: 1px solid var(--border);
  color: var(--text-muted); border-radius: var(--radius-sm);
  padding: 0.35rem 0.85rem; font-size: 0.85rem;
  cursor: pointer; transition: all 0.15s;
}
.btn-ghost:hover { background: var(--surface-hover); color: var(--text); }

.btn-primary {
  background: var(--primary); color: white;
  border: none; border-radius: var(--radius-sm);
  padding: 0.4rem 1rem; font-size: 0.88rem;
  cursor: pointer; transition: background 0.15s; font-weight: 500;
}
.btn-primary:hover { background: var(--primary-hover); }

/* ════════════════════════════════════════
   CHAT AREA
════════════════════════════════════════ */
.chat-area {
  flex: 1;
  overflow-y: auto;
  padding: 2rem 0;
  display: flex;
  flex-direction: column;
}

/* 歡迎畫面（尚無對話） */
.welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 2.5rem 1.5rem 2rem;
  max-width: 740px;
  width: 100%;
  margin: 0 auto;
}
.welcome-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.welcome-logo {
  width: 48px; height: 48px;
  background: var(--primary);
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  color: white; font-size: 1.5rem; font-weight: 700;
  flex-shrink: 0;
}
.welcome-title { font-size: 1.3rem; font-weight: 700; margin-bottom: 0.2rem; }
.welcome-subtitle { color: var(--text-muted); font-size: 0.85rem; line-height: 1.5; }

/* 說明卡片 */
.intro-card {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.intro-card p {
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.75;
}
.intro-highlight {
  background: #fdf5ef;
  border-left: 3px solid var(--primary);
  padding: 0.6rem 0.9rem;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text) !important;
  font-size: 0.9rem;
  line-height: 1.75;
}

/* 建議問題 */
.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  width: 100%;
}
.suggestion-btn {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0.75rem 1rem;
  text-align: left; cursor: pointer; font-size: 0.85rem;
  color: var(--text-muted); transition: all 0.15s;
  line-height: 1.4;
}
.suggestion-btn:hover {
  border-color: var(--primary); color: var(--text);
  box-shadow: var(--shadow);
}

/* 對話訊息 */
.messages-list {
  display: flex; flex-direction: column;
  max-width: 740px; width: 100%;
  margin: 0 auto; padding: 0 1.5rem;
  gap: 0;
}

.message-row {
  display: flex;
  padding: 0.5rem 0;
  gap: 1rem;
}

.message-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.75rem; font-weight: 700; flex-shrink: 0;
  margin-top: 2px;
}
.message-avatar.ai {
  background: var(--primary); color: white;
}
.message-avatar.user {
  background: #6b6b6b; color: white;
}

.message-body { flex: 1; min-width: 0; }
.message-name {
  font-size: 0.8rem; font-weight: 600; color: var(--text-muted);
  margin-bottom: 0.3rem;
}
.message-text {
  font-size: 0.95rem; line-height: 1.7; color: var(--text);
}
.message-text p { margin-bottom: 0.75rem; }
.message-text p:last-child { margin-bottom: 0; }
.message-text ul, .message-text ol { padding-left: 1.5rem; margin-bottom: 0.75rem; }
.message-text li { margin-bottom: 0.25rem; }
.message-text strong { font-weight: 600; }
.message-text code {
  background: #f0ede8; padding: 0.1em 0.4em;
  border-radius: 4px; font-size: 0.9em; font-family: monospace;
}
.message-text pre {
  background: #f0ede8; padding: 1rem; border-radius: var(--radius-sm);
  overflow-x: auto; margin-bottom: 0.75rem;
}
.message-text pre code { background: none; padding: 0; }

.sources-row {
  display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.5rem;
}
.source-chip {
  font-size: 0.75rem; color: var(--primary);
  background: #fdf3ec; border: 1px solid #f5d5be;
  border-radius: 20px; padding: 0.15rem 0.6rem;
}

/* 打字指示器 */
.typing-dots {
  display: flex; gap: 4px; align-items: center; padding: 0.25rem 0;
}
.typing-dots span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-light); animation: dot-bounce 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dot-bounce { 0%,60%,100%{transform:translateY(0)} 30%{transform:translateY(-6px)} }

/* ════════════════════════════════════════
   INPUT BAR
════════════════════════════════════════ */
.input-area {
  padding: 0.75rem 1.5rem 1rem;
  background: var(--bg);
}

.input-box {
  max-width: 740px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.input-box:focus-within {
  border-color: #c8b8a0;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}

.input-box textarea {
  border: none; outline: none; resize: none;
  background: transparent;
  padding: 0.85rem 1rem 0.35rem;
  font-size: 0.95rem; font-family: inherit;
  color: var(--text);
  min-height: 52px; max-height: 200px;
  line-height: 1.5;
}
.input-box textarea::placeholder { color: var(--text-light); }

.input-bottom-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.3rem 0.6rem 0.5rem;
}

.input-actions {
  display: flex; align-items: center;
  gap: 0.5rem;
}

.input-hint { font-size: 0.75rem; color: var(--text-light); }

.send-btn {
  width: 32px; height: 32px;
  background: var(--primary); color: white;
  border: none; border-radius: 8px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 0.15s; flex-shrink: 0;
}
.send-btn:hover { background: var(--primary-hover); }
.send-btn:disabled { background: var(--border); cursor: not-allowed; }
.send-btn svg { width: 16px; height: 16px; }

/* ── 模式選擇器 ── */
.mode-picker { position: relative; }

.mode-picker-btn {
  display: flex; align-items: center; gap: 0.35rem;
  padding: 0.28rem 0.65rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.78rem; color: var(--text-secondary);
  cursor: pointer; font-family: inherit;
  transition: border-color 0.15s, color 0.15s;
}
.mode-picker-btn:hover { border-color: var(--primary); color: var(--primary); }
.mode-picker-btn svg { opacity: 0.6; transition: transform 0.2s; }
.mode-picker.open .mode-picker-btn svg { transform: rotate(180deg); }

.mode-dropdown {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  min-width: 240px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  overflow: hidden;
  z-index: 100;
}
.mode-picker.open .mode-dropdown { display: block; }

.mode-option {
  display: flex; flex-direction: column; align-items: flex-start;
  width: 100%; padding: 0.7rem 1rem;
  background: transparent; border: none;
  cursor: pointer; font-family: inherit; text-align: left;
  transition: background 0.1s;
  border-bottom: 1px solid var(--border);
}
.mode-option:last-child { border-bottom: none; }
.mode-option:hover { background: #faf8ff; }
.mode-option.active { background: #f5f0ff; }

.mode-option-name {
  font-size: 0.85rem; font-weight: 600; color: var(--text-primary);
  margin-bottom: 0.2rem;
}
.mode-option.active .mode-option-name { color: var(--primary); }

.mode-option-desc {
  font-size: 0.75rem; color: var(--text-light); line-height: 1.4;
}

.input-footer {
  text-align: center; font-size: 0.72rem; color: var(--text-light);
  margin-top: 0.5rem;
}

/* ════════════════════════════════════════
   低點數橫幅
════════════════════════════════════════ */
#low-credits-banner {
  max-width: 740px; margin: 0 auto 0.5rem;
  background: #fff8f0; border: 1px solid #f5d5be;
  border-radius: var(--radius-sm); padding: 0.6rem 1rem;
  font-size: 0.83rem; color: #b05a1e;
  display: flex; align-items: center; gap: 0.5rem;
}
#low-credits-banner a { color: var(--primary); font-weight: 600; }

/* ════════════════════════════════════════
   MODAL
════════════════════════════════════════ */
.modal-overlay {
  display: none; position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,0.45); backdrop-filter: blur(3px);
  align-items: center; justify-content: center;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--surface); border-radius: var(--radius);
  padding: 2rem; width: 400px; max-width: 95vw;
  box-shadow: var(--shadow-md); position: relative;
}
.modal-close {
  position: absolute; top: 1rem; right: 1rem;
  background: none; border: none; cursor: pointer;
  color: var(--text-light); font-size: 1.2rem; line-height: 1;
  padding: 0.2rem;
  border-radius: 4px;
  transition: background 0.15s;
}
.modal-close:hover { background: var(--surface-hover); color: var(--text); }

.modal h2 { font-size: 1.25rem; font-weight: 600; margin-bottom: 0.25rem; }
.modal .modal-sub { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1.5rem; }

.tab-row {
  display: flex; border-bottom: 1px solid var(--border); margin-bottom: 1.25rem;
}
.tab-btn {
  flex: 1; padding: 0.55rem; text-align: center;
  font-size: 0.9rem; color: var(--text-muted);
  background: none; border: none; border-bottom: 2px solid transparent;
  margin-bottom: -1px; cursor: pointer; transition: color 0.15s;
}
.tab-btn.active { color: var(--text); border-bottom-color: var(--primary); font-weight: 500; }

.field { margin-bottom: 1rem; }
.field label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 0.3rem; font-weight: 500; }
.field input {
  width: 100%; padding: 0.6rem 0.85rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.9rem; outline: none; transition: border 0.15s;
  background: var(--bg);
}
.field input:focus { border-color: var(--primary); background: white; }

.modal-btn {
  width: 100%; background: var(--primary); color: white;
  border: none; border-radius: var(--radius-sm); padding: 0.7rem;
  font-size: 0.95rem; font-weight: 500; cursor: pointer; margin-top: 0.25rem;
  transition: background 0.15s;
}
.modal-btn:hover { background: var(--primary-hover); }
.modal-btn:disabled { background: var(--border); cursor: not-allowed; }

.modal-error { font-size: 0.82rem; color: #c62828; margin-top: 0.5rem; }

/* ════════════════════════════════════════
   RWD
════════════════════════════════════════ */
@media (max-width: 680px) {
  .sidebar { display: none; }
  .messages-list { padding: 0 1rem; }
  .input-area { padding: 0.5rem 1rem 0.75rem; }
}

/* ════════════════════════════════════════
   客服視窗
════════════════════════════════════════ */
.support-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.25);
  z-index: 999;
}
.support-window {
  position: fixed;
  bottom: 1.5rem; right: 1.5rem;
  width: 340px; height: 480px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  z-index: 1000;
  display: flex; flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--border);
}
.support-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.85rem 1rem;
  background: var(--primary);
  color: white;
  font-weight: 600; font-size: 0.9rem;
}
.support-header button {
  background: none; border: none; color: white;
  font-size: 1.2rem; cursor: pointer; line-height: 1;
}
.support-messages {
  flex: 1; overflow-y: auto;
  padding: 1rem; display: flex; flex-direction: column; gap: 0.6rem;
}
.support-msg {
  max-width: 85%; padding: 0.55rem 0.8rem;
  border-radius: var(--radius); font-size: 0.875rem; line-height: 1.6;
  word-break: break-word;
}
.support-msg-ai {
  background: var(--surface-hover); align-self: flex-start;
}
.support-msg-user {
  background: var(--primary); color: white; align-self: flex-end;
}
.support-msg-ai p { margin: 0 0 0.4rem; }
.support-msg-ai p:last-child { margin-bottom: 0; }
.support-msg-ai ul, .support-msg-ai ol { margin: 0.3rem 0 0.4rem 1.2rem; padding: 0; }
.support-msg-ai li { margin-bottom: 0.2rem; }
.support-msg-ai strong { font-weight: 600; }
.support-msg-ai code { background: rgba(0,0,0,0.06); padding: 0.1rem 0.3rem; border-radius: 3px; font-size: 0.82em; }
.support-thinking { opacity: 0.5; font-style: italic; }
.support-input-row {
  display: flex; gap: 0.5rem;
  padding: 0.75rem; border-top: 1px solid var(--border);
}
.support-input-row input {
  flex: 1; border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 0.45rem 0.75rem;
  font-size: 0.875rem; outline: none;
}
.support-input-row input:focus { border-color: var(--primary); }
.support-input-row button {
  background: var(--primary); color: white;
  border: none; border-radius: var(--radius-sm);
  padding: 0.45rem 0.9rem; font-size: 0.875rem;
  cursor: pointer; font-weight: 500;
}
.support-input-row button:hover { background: var(--primary-hover); }
