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

:root {
  --bg: #F5F0E8;
  --card: #FFFDF7;
  --text: #3D3229;
  --text-secondary: #8B7E6A;
  --border: #E0D5C5;
  --accent: #C76F3B;
  --accent-hover: #A85A2E;
  --success: #6B8E5A;
  --error: #B84A4A;
  --highlight: #D4A574;
  --shadow: rgba(61, 50, 41, 0.08);
}

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ===== 頂部列 ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px var(--shadow);
  z-index: 10;
}

.logo {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 1.3em;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.5px;
}

.menu-btn {
  display: none;
  background: none;
  border: none;
  font-size: 1.3em;
  color: var(--text-secondary);
  cursor: pointer;
}

/* ===== 主體 ===== */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===== 側邊欄 ===== */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.upload-btn {
  width: 100%;
  padding: 10px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.95em;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s;
}

.upload-btn:hover {
  background: var(--accent-hover);
}

/* 分類 */
.categories {
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  border-bottom: 1px solid var(--border);
}

.cat-btn {
  padding: 5px 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 16px;
  font-size: 0.8em;
  font-family: inherit;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.cat-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.cat-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.cat-btn span {
  font-size: 0.85em;
  opacity: 0.7;
}

/* 文檔列表 */
.doc-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.doc-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
  position: relative;
}

.doc-item:hover {
  background: var(--bg);
}

.doc-item.active {
  background: var(--highlight);
  background: rgba(212, 165, 116, 0.2);
}

.doc-icon {
  font-size: 1.4em;
  flex-shrink: 0;
}

.doc-info {
  flex: 1;
  min-width: 0;
}

.doc-name {
  font-size: 0.85em;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-meta {
  font-size: 0.72em;
  color: var(--text-secondary);
  margin-top: 2px;
}

.doc-delete {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.9em;
  opacity: 0;
  transition: opacity 0.15s;
  padding: 4px;
}

.doc-item:hover .doc-delete {
  opacity: 1;
}

.doc-delete:hover {
  color: var(--error);
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
}

.empty-state p {
  margin-bottom: 6px;
}

.empty-state .hint {
  font-size: 0.8em;
  opacity: 0.7;
}

/* ===== 問答區 ===== */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  gap: 8px;
}

.welcome-icon {
  font-size: 3em;
  margin-bottom: 8px;
}

.welcome h2 {
  font-family: Georgia, serif;
  font-size: 1.4em;
  color: var(--text);
  font-weight: 600;
}

.welcome p {
  font-size: 0.9em;
}

/* 對話 */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  animation: fadeIn 0.3s ease;
}

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

.msg-user {
  align-self: flex-end;
}

.msg-user .msg-content {
  background: var(--accent);
  color: #fff;
  border-radius: 16px 16px 4px 16px;
  padding: 10px 16px;
  font-size: 0.92em;
  line-height: 1.5;
  display: inline-block;
  max-width: 80%;
  float: right;
}

.msg-ai {
  align-self: flex-start;
}

.msg-ai .msg-content {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px 16px 16px 4px;
  padding: 14px 18px;
  font-size: 0.92em;
  line-height: 1.65;
  box-shadow: 0 1px 4px var(--shadow);
  max-width: 85%;
}

.msg-source {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 0.78em;
  color: var(--text-secondary);
}

.msg-source span {
  display: block;
  margin-top: 3px;
}

.msg-loading {
  display: flex;
  gap: 4px;
  padding: 14px 18px;
}

.msg-loading span {
  width: 6px;
  height: 6px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}

.msg-loading span:nth-child(2) { animation-delay: 0.2s; }
.msg-loading span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* 輸入區 */
.input-area {
  padding: 16px 20px;
  background: var(--card);
  border-top: 1px solid var(--border);
}

.input-wrapper {
  display: flex;
  gap: 10px;
  max-width: 720px;
  margin: 0 auto;
  align-items: flex-end;
}

textarea {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 0.95em;
  font-family: inherit;
  color: var(--text);
  background: var(--bg);
  resize: none;
  outline: none;
  line-height: 1.5;
  max-height: 120px;
  transition: border-color 0.2s;
}

textarea:focus {
  border-color: var(--accent);
}

textarea::placeholder {
  color: var(--text-secondary);
  opacity: 0.6;
}

.send-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 1.1em;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
  transform: scale(1.05);
}

.send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.input-hint {
  text-align: center;
  font-size: 0.75em;
  color: var(--text-secondary);
  opacity: 0.6;
  margin-top: 8px;
}

/* ===== 上傳進度 ===== */
.upload-progress {
  position: fixed;
  inset: 0;
  background: rgba(61, 50, 41, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.progress-card {
  background: var(--card);
  border-radius: 16px;
  padding: 30px;
  min-width: 320px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.12);
  text-align: center;
}

.progress-card h3 {
  font-family: Georgia, serif;
  margin-bottom: 16px;
  font-size: 1.1em;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 10px;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  width: 0%;
  transition: width 0.3s;
}

.progress-text {
  font-size: 0.85em;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.cancel-btn {
  padding: 8px 20px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-secondary);
  font-family: inherit;
  cursor: pointer;
}

.cancel-btn:hover {
  border-color: var(--error);
  color: var(--error);
}

/* ===== 手機適配 ===== */
@media (max-width: 768px) {
  .menu-btn {
    display: block;
  }

  .sidebar {
    position: fixed;
    top: 53px;
    left: 0;
    bottom: 0;
    width: 280px;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,0.1);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .chat-area {
    width: 100%;
  }

  .message .msg-content {
    max-width: 90% !important;
  }
}
