:root {
  --primary: #1a3a5c;
  --primary-light: #1e88e5;
  --accent: #c41230;
  --accent-hover: #a80e28;
  --bg: #f5f6f8;
  --card-bg: #ffffff;
  --text: #2d3436;
  --text-light: #666;
  --border: #dfe6e9;
  --border-light: #e8e8e8;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.15);
}

/* ============================================================
   CSS Reset & Base
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
  background: #f0f2f5;
  color: #333;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button { cursor: pointer; border: none; outline: none; font-family: inherit; }
input, textarea { font-family: inherit; outline: none; }

/* ============================================================
   Page Container
   ============================================================ */
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.page { display: none; flex-direction: column; height: 100%; overflow: hidden; }
.page.active { display: flex; }

/* ============================================================
   Home Page - 会话管理中心
   ============================================================ */
#homePage {
  background: #f5f6f8;
  overflow-y: auto;
}
.home-header {
  width: 100%;
  background:
    linear-gradient(135deg, #1a3a5c 0%, #15304d 30%, #0d2137 70%, #0a1a2e 100%);
  padding: 48px 24px 40px;
  text-align: center;
  color: #fff;
  border-radius: 0 0 32px 32px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.25);
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}
.home-header::before {
  content: '';
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background-image:
    repeating-linear-gradient(0deg, transparent, transparent 40px, rgba(255,255,255,0.015) 40px, rgba(255,255,255,0.015) 41px),
    repeating-linear-gradient(90deg, transparent, transparent 60px, rgba(255,255,255,0.01) 60px, rgba(255,255,255,0.01) 61px);
  pointer-events: none;
}
.home-header::after {
  content: '';
  position: absolute; top: -80px; right: -80px;
  width: 280px; height: 280px;
  background: radial-gradient(circle, rgba(66,165,245,0.12) 0%, transparent 70%);
  pointer-events: none;
  border-radius: 50%;
}
.home-header .icon { font-size: 52px; margin-bottom: 10px; display: block; animation: float 3s ease-in-out infinite; position: relative; z-index: 1; }
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}
.home-header h1 { font-size: 26px; font-weight: 700; letter-spacing: 2px; margin-bottom: 8px; position: relative; z-index: 1; }
.home-header .subtitle { font-size: 14px; color: rgba(255,255,255,0.7); font-weight: 300; max-width: 380px; margin: 0 auto; position: relative; z-index: 1; }

/* Session List */
/* Feature Highlight Cards */
.feature-cards {
  display: flex; gap: 10px; padding: 16px 16px 0; flex-shrink: 0;
}
.feature-card {
  flex: 1; text-align: center; padding: 14px 8px;
  background: #fff; border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  font-size: 12px; color: #555; line-height: 1.5;
}
.feature-card .fc-icon { font-size: 22px; display: block; margin-bottom: 4px; }
.feature-card .fc-label { font-weight: 500; }

/* Powered by footer */
.home-footer-line {
  text-align: center; padding: 8px 16px 16px;
  font-size: 11px; color: #bbb;
  flex-shrink: 0;
}

.home-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 100px;
  -webkit-overflow-scrolling: touch;
}
.session-section-title {
  font-size: 13px; font-weight: 600; color: #888;
  margin-bottom: 10px; padding-left: 4px;
  display: flex; align-items: center; gap: 6px;
}
.session-card {
  background: #fff; border-radius: 12px; padding: 14px 16px;
  margin-bottom: 10px; box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  display: flex; align-items: center; gap: 12px;
  transition: all 0.2s; cursor: default;
  border: 1.5px solid transparent;
}
.session-card:hover { box-shadow: 0 3px 12px rgba(0,0,0,0.1); }
.session-card.active-session { border-color: #1976d2; background: #f8faff; }
.session-info { flex: 1; min-width: 0; }
.session-info .session-title {
  font-size: 14px; font-weight: 600; color: #222; margin-bottom: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-meta {
  font-size: 11px; color: #999; display: flex; gap: 12px; flex-wrap: wrap;
}
.session-status {
  font-size: 11px; padding: 2px 8px; border-radius: 10px; font-weight: 500;
}
.session-status.active { background: #e8f5e9; color: #2e7d32; }
.session-status.ended { background: #f5f5f5; color: #888; }
.session-actions { display: flex; gap: 6px; flex-shrink: 0; }
.session-actions button {
  padding: 6px 12px; border-radius: 16px; font-size: 12px; font-weight: 500;
  white-space: nowrap; transition: all 0.15s;
}
.btn-continue { background: #1976d2; color: #fff; }
.btn-continue:hover { background: #1565c0; }
.btn-end-session { background: #fce4ec; color: #c62828; }
.btn-end-session:hover { background: #f8bbd0; }
.btn-view { background: #f5f5f5; color: #555; }
.btn-view:hover { background: #e0e0e0; }
.btn-delete { background: #fff; color: #c62828; border: 1px solid #ef9a9a !important; }
.btn-delete:hover { background: #ffebee; }

/* Empty State */
.empty-state {
  text-align: center; padding: 40px 20px; color: #aaa;
}
.empty-state .empty-icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 13px; line-height: 1.6; }

/* Home Bottom Buttons */
.home-bottom {
  position: sticky; bottom: 0; left: 0; right: 0;
  background: linear-gradient(transparent, #f5f6f8 20%, #f5f6f8);
  padding: 16px 20px 20px; display: flex; flex-direction: column; gap: 16px;
  flex-shrink: 0;
}
.home-bottom .btn-main {
  width: 100%; padding: 18px 28px; font-size: 16px; font-weight: 600;
  border-radius: 14px; display: flex; align-items: center;
  justify-content: center; gap: 8px; transition: all 0.25s;
  box-shadow: 0 4px 14px rgba(0,0,0,0.1);
}
.home-bottom .btn-main:active { transform: scale(0.97); }
.home-bottom .btn-new-chat {
  background: #c41230; color: #fff;
  box-shadow: 0 6px 20px rgba(196,18,48,0.35);
}
.home-bottom .btn-new-chat:hover {
  background: #a80e28;
  box-shadow: 0 10px 28px rgba(196,18,48,0.45);
  transform: translateY(-2px);
}
.home-bottom .btn-kb {
  background: #fff; color: #1a3a5c;
  border: 2px solid #1a3a5c !important;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}
.home-bottom .btn-kb:hover {
  background: #f0f4f8;
  box-shadow: 0 6px 18px rgba(0,0,0,0.12);
  transform: translateY(-1px);
}

/* ============================================================
   Chat Page
   ============================================================ */
#chatPage { background: #f5f6f8; }
.chat-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: #fff !important; border-bottom: 1px solid #e8e8e8;
  box-shadow: 0 1px 4px rgba(0,0,0,0.04); z-index: 9999 !important; flex-shrink: 0;
  position: sticky; top: 0;
  visibility: visible !important; opacity: 1 !important;
  min-height: 44px;
}
.btn-exit {
  background: #f5f5f5; color: #666; font-size: 15px; font-weight: 500;
  padding: 6px 12px; border-radius: 18px; display: flex; align-items: center; gap: 4px;
  flex-shrink: 0;
}
.btn-exit:hover { background: #e0e0e0; }
.chat-title-area {
  flex: 1; text-align: center; font-size: 14px; font-weight: 600; color: #333;
  min-width: 0; display: flex; align-items: center; justify-content: center; gap: 4px;
}
.chat-title-area .title-text { cursor: pointer; padding: 2px 6px; border-radius: 4px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title-area .title-text:hover { background: #f0f0f0; }
.chat-title-area .title-text:empty::after { content: '点击编辑标题'; color: #ccc; font-weight: 400; }
.chat-title-edit { font-size: 14px; font-weight: 600; border: 1.5px solid #1976d2; border-radius: 6px; padding: 3px 8px; text-align: center; width: 140px; background: #fff; }
.chat-header .actions { display: flex; gap: 6px; flex-shrink: 0; }
.chat-header .actions button {
  padding: 6px 12px; border-radius: 18px; font-size: 12px; font-weight: 500;
  display: flex; align-items: center; gap: 4px; transition: all 0.2s; white-space: nowrap;
}
.btn-summary { background: #e8f5e9; color: #2e7d32; }
.btn-summary:hover { background: #c8e6c9; }
/* Action bar below AI messages */
.msg-actions {
  display: flex; gap: 6px; justify-content: flex-end; margin-top: 6px;
  padding-top: 6px; border-top: 1px dashed #e8e8e8;
}
.msg-actions button {
  padding: 4px 10px; border-radius: 12px; font-size: 11px; font-weight: 500;
  display: flex; align-items: center; gap: 3px; transition: all 0.15s;
}
.btn-copy-msg { background: #f5f5f5; color: #555; }
.btn-copy-msg:hover { background: #e0e0e0; }
.btn-export-tbl { background: #e8f5e9; color: #2e7d32; }
.btn-export-tbl:hover { background: #c8e6c9; }
.btn-new-inchat { background: #e3f2fd; color: #1565c0; }
.btn-new-inchat:hover { background: #bbdefb; }

/* Read-only Banner */
.readonly-banner {
  background: #fff3e0; color: #e65100; font-size: 12px;
  text-align: center; padding: 8px; border-bottom: 1px solid #ffe0b2;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}

/* Messages Area */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-thumb { background: #ccc; border-radius: 2px; }

.message { display: flex; max-width: 85%; animation: msgIn 0.3s ease-out; }
@keyframes msgIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.message.user { align-self: flex-end; }
.message.assistant { align-self: flex-start; }
.msg-bubble {
  padding: 12px 16px; border-radius: 18px; font-size: 14px; line-height: 1.65;
  word-break: break-word; white-space: pre-wrap;
}
.message.user .msg-bubble {
  background: linear-gradient(135deg, #e3f2fd, #bbdefb);
  color: #1a237e; border-bottom-right-radius: 4px;
}
.message.assistant .msg-bubble {
  background: #fff; color: #333; border: 1px solid #e8e8e8;
  border-bottom-left-radius: 4px; box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.msg-bubble .label { font-size: 11px; font-weight: 600; margin-bottom: 4px; opacity: 0.7; }
.msg-bubble .content strong { color: #1565c0; }

/* Loading Dots */
.typing-indicator { display: flex; gap: 4px; padding: 14px 18px; align-items: center; }
.typing-indicator span {
  width: 8px; height: 8px; border-radius: 50%; background: #90a4ae;
  animation: bounce 1.4s ease-in-out infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-10px); }
}

/* Input Area */
.chat-input-area {
  padding: 10px 12px; background: #fff; border-top: 1px solid #e8e8e8;
  display: flex; gap: 6px; align-items: center; flex-shrink: 0;
  box-shadow: 0 -1px 4px rgba(0,0,0,0.04);
}
.chat-input-area input {
  flex: 1; padding: 10px 14px; border: 1.5px solid #e0e0e0; border-radius: 22px;
  font-size: 14px; transition: border-color 0.2s; background: #f8f9fa;
}
.chat-input-area input:focus { border-color: #1976d2; background: #fff; }
.btn-file { width: 38px; height: 38px; border-radius: 50%; background: #f5f5f5; color: #666; font-size: 18px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; position:relative }
.btn-file:hover { background: #e0e0e0; }
.btn-file.loading { background: #fff3e0; color: #e65100; cursor: wait; pointer-events: none; }
.btn-file:disabled { background: #f5f5f5; color: #ccc; cursor: not-allowed; pointer-events: none; }
.ocr-progress{width:100%;height:6px;background:#e0e0e0;border-radius:3px;margin:6px 0;overflow:hidden;display:none}
.ocr-progress .bar{height:100%;background:var(--accent,#c0392b);border-radius:3px;transition:width .3s}
.ocr-progress-text{font-size:11px;color:var(--sub,#888);text-align:center}
.btn-voice {
  width: 38px; height: 38px; border-radius: 50%; background: #f5f5f5; color: #666;
  font-size: 18px; display: flex; align-items: center; justify-content: center;
  transition: all 0.2s; flex-shrink: 0; position: relative;
}
.btn-voice:hover { background: #e0e0e0; }
.btn-voice.recording {
  background: #e53935; color: #fff;
  animation: pulse-ring 1.5s ease-out infinite;
}
@keyframes pulse-ring {
  0% { box-shadow: 0 0 0 0 rgba(229,57,53,0.5); }
  70% { box-shadow: 0 0 0 14px rgba(229,57,53,0); }
  100% { box-shadow: 0 0 0 0 rgba(229,57,53,0); }
}
.btn-send {
  width: 40px; height: 40px; border-radius: 50%; background: #1976d2; color: #fff;
  font-size: 16px; display: flex; align-items: center; justify-content: center;
  transition: all 0.2s; flex-shrink: 0;
}
.btn-send:hover { background: #1565c0; }
.btn-send:active { transform: scale(0.92); }
.btn-send:disabled { background: #bdbdbd; cursor: not-allowed; }

/* ============================================================
   Modal Base
   ============================================================ */
.modal-overlay {
  display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.5); z-index: 100; justify-content: center; align-items: center;
  padding: 20px; animation: fadeIn 0.2s;
}
.modal-overlay.active { display: flex; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: #fff; border-radius: 16px; width: 100%; max-width: 700px;
  max-height: 85vh; display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2); animation: slideUp 0.3s ease-out;
}
@keyframes slideUp { from { opacity: 0; transform: translateY(30px); } to { opacity: 1; transform: translateY(0); } }
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid #eee; flex-shrink: 0;
}
.modal-header h3 { font-size: 17px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.modal-close { font-size: 22px; background: none; color: #999; width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; }
.modal-close:hover { background: #f0f0f0; color: #333; }
.modal-body { flex: 1; overflow-y: auto; padding: 16px 20px; -webkit-overflow-scrolling: touch; }
.modal-footer { padding: 12px 20px; border-top: 1px solid #eee; display: flex; gap: 10px; justify-content: flex-end; flex-shrink: 0; }

/* KB Modal */
.kb-search {
  width: 100%; padding: 10px 14px; border: 1.5px solid #e0e0e0; border-radius: 10px;
  font-size: 14px; margin-bottom: 12px; background: #f8f9fa;
}
.kb-search:focus { border-color: #1976d2; background: #fff; }
.kb-tree { font-size: 13px; }
.kb-tree-item { margin: 2px 0; }
.kb-tree-header {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px;
  border-radius: 8px; cursor: pointer; transition: background 0.15s;
}
.kb-tree-header:hover { background: #f5f5f5; }
.kb-tree-header .arrow { font-size: 10px; transition: transform 0.2s; width: 16px; text-align: center; color: #999; }
.kb-tree-header .arrow.open { transform: rotate(90deg); }
.kb-tree-header .item-icon { font-size: 16px; }
.kb-tree-header .item-title { font-weight: 500; }
.kb-tree-header .item-badge { font-size: 11px; color: #999; margin-left: auto; }
.kb-tree-children { display: none; margin-left: 22px; border-left: 1.5px solid #e8e8e8; padding-left: 8px; }
.kb-tree-children.open { display: block; }
.kb-content-pane {
  background: #fafbfc; border: 1px solid #e8e8e8; border-radius: 10px;
  padding: 14px 16px; margin-top: 12px; font-size: 13px; line-height: 1.7;
  max-height: 300px; overflow-y: auto; white-space: pre-wrap; display: none;
}
.kb-content-pane.active { display: block; }
.kb-content-pane h4 { color: #1565c0; margin-bottom: 8px; }
.kb-no-results { text-align: center; padding: 30px; color: #999; }

/* Toast */
#toastContainer {
  position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
  z-index: 200; display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
.toast {
  padding: 12px 20px; border-radius: 10px; font-size: 14px; color: #fff;
  animation: toastIn 0.3s ease-out, toastOut 0.3s ease-in forwards;
  box-shadow: 0 4px 14px rgba(0,0,0,0.15); pointer-events: auto;
  white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.success { background: #2e7d32; }
.toast.error { background: #c62828; }
.toast.info { background: #1565c0; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-20px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { 0%, 70% { opacity: 1; } 100% { opacity: 0; transform: translateY(-10px); } }

/* File Card Float */
.file-card-modal { max-width: 400px; }
.file-card-info {
  display: flex; align-items: center; gap: 10px; padding: 12px;
  background: #f8f9fa; border-radius: 10px; margin-bottom: 12px;
  font-size: 13px;
}
.file-card-info .fc-icon { font-size: 32px; }
.file-card-info .fc-details { flex: 1; min-width: 0; }
.file-card-info .fc-name { font-weight: 600; color: #222; word-break: break-all; }
.file-card-info .fc-meta { font-size: 11px; color: #999; margin-top: 2px; }
.file-card-textarea {
  width: 100%; padding: 10px 12px; border: 1.5px solid #e0e0e0; border-radius: 10px;
  font-size: 14px; resize: vertical; min-height: 72px; font-family: inherit;
  line-height: 1.6;
}
.file-card-textarea:focus { border-color: #1976d2; }

/* Attachment Card inside user bubble */
.attachment-card {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; background: rgba(255,255,255,0.7);
  border-radius: 8px; margin-bottom: 8px; font-size: 12px;
}
.attachment-card .att-icon { font-size: 20px; flex-shrink: 0; }
.attachment-card .att-info { flex: 1; min-width: 0; }
.attachment-card .att-name { font-weight: 600; color: #222; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attachment-card .att-size { color: #888; font-size: 11px; }

/* File Menu Dropdown */
.file-menu {
  display: none; position: absolute; bottom: 48px; left: 0;
  background: #fff; border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,0.15);
  padding: 6px; min-width: 160px; z-index: 50;
}
.file-menu.show { display: block; }
.file-menu-item {
  display: flex; align-items: center; gap: 8px; padding: 10px 14px;
  border-radius: 8px; cursor: pointer; font-size: 14px; transition: background 0.15s;
}
.file-menu-item:hover { background: #f0f0f0; }
.menu-icon { font-size: 18px; }

/* Responsive */
@media (max-width: 480px) {
  .home-header { padding: 32px 16px 28px; border-radius: 0 0 24px 24px; }
  .home-header h1 { font-size: 20px; letter-spacing: 1px; }
  .home-header .icon { font-size: 42px; }
  .home-header .subtitle { font-size: 12px; }
  .feature-cards { gap: 6px; padding: 12px 10px 0; }
  .feature-card { padding: 10px 4px; font-size: 11px; }
  .feature-card .fc-icon { font-size: 18px; }
  .home-bottom .btn-main { font-size: 15px; padding: 15px 20px; }
  .chat-header { padding: 8px 6px; gap: 4px; }
  .chat-header .actions button { font-size: 11px; padding: 5px 6px; }
  .chat-title-area .title-text { max-width: 80px; font-size: 11px; }
  .chat-title-edit { width: 80px !important; }
  /* Icon-only compact buttons */
  .btn-exit { padding: 5px 8px; font-size: 13px; width: auto; min-width: 36px; }
  .btn-new-inchat { padding: 5px 8px; font-size: 13px; width: auto; min-width: 36px; }
  .btn-label { display: none; }
  .btn-icon { font-size: 16px; }
  .message { max-width: 92%; }
  .modal { max-height: 90vh; border-radius: 12px 12px 0 0; margin-top: auto; }
  @keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
  .chat-input-area input { padding: 8px 12px; font-size: 13px; }
}
@media (min-width: 768px) {
  .home-body { max-width: 520px; margin: 0 auto; }
  .home-bottom { max-width: 520px; margin: 0 auto; }
  .message { max-width: 70%; }
  .chat-messages { padding: 20px 40px; }
  .chat-input-area { padding: 12px 24px; }
  .modal { max-width: 750px; }
}