* {
  box-sizing: border-box;
}

:root {
  --bg: #f5f4f1;
  --surface: #ffffff;
  --surface-2: #faf9f7;
  --border: #e8e6e1;
  --text: #2c2a26;
  --text-muted: #6b6560;
  --accent: #7c9a82;
  --accent-hover: #6a866f;
  --accent-soft: rgba(124, 154, 130, 0.15);
  --blue-soft: rgba(59, 130, 246, 0.12);
  --blue: #3b82f6;
  --success: #4a7c59;
  --error: #c75c5c;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.08);
  --sidebar-w: 260px;
}

body {
  margin: 0;
  font-family: "Segoe UI", "PingFang SC", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh;
  line-height: 1.6;
  overflow: hidden;
}

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  max-height: 100vh;
  max-height: 100dvh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-head {
  padding: 20px 16px 12px;
}

.logo {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
}

.btn-new-chat {
  margin: 0 12px 16px;
  padding: 10px 14px;
  width: calc(100% - 24px);
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.2s;
}

.btn-new-chat:hover {
  background: var(--accent-hover);
}

.icon-plus {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-radius: 2px;
  position: relative;
}

.icon-plus::before,
.icon-plus::after {
  content: "";
  position: absolute;
  background: currentColor;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.icon-plus::before {
  width: 8px;
  height: 2px;
}

.icon-plus::after {
  width: 2px;
  height: 8px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 16px;
}

.conv-list-empty {
  padding: 12px 8px;
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.conv-group-title {
  font-size: 0.75rem;
  color: var(--text-muted);
  padding: 12px 8px 6px;
  font-weight: 500;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  position: relative;
  text-align: left;
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
}

.conv-item:hover {
  background: var(--surface-2);
}

.conv-item.active {
  background: var(--blue-soft);
  color: var(--blue);
}

.conv-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-item-more {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 1rem;
  line-height: 1;
  opacity: 0;
  transition: opacity 0.2s;
}

.conv-item:hover .conv-item-more,
.conv-item-more:focus {
  opacity: 1;
}

@media (max-width: 768px) {
  .conv-item-more {
    opacity: 1;
  }
}

.conv-item-more:hover {
  background: var(--border);
  color: var(--text);
}

.sidebar-link {
  margin: 12px;
  padding: 10px 14px;
  display: block;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
  width: calc(100% - 24px);
}

.sidebar-link:hover {
  background: var(--surface-2);
  color: var(--text);
}

/* Dropdown */
.dropdown {
  display: none;
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-hover);
  z-index: 300;
  min-width: 120px;
}

.dropdown.show {
  display: block;
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 10px 14px;
  border: none;
  background: none;
  color: var(--error);
  font-size: 0.9rem;
  cursor: pointer;
  text-align: left;
  border-radius: 6px;
}

.dropdown-item:hover {
  background: rgba(199, 92, 92, 0.1);
}

/* 删除确认弹层：最上层 */
.confirm-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 9999;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.confirm-overlay.show {
  display: flex;
}

.confirm-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  padding: 20px 24px;
  min-width: 260px;
  max-width: 90vw;
}

.confirm-msg {
  margin: 0 0 20px;
  font-size: 0.95rem;
  color: var(--text);
}

.confirm-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.confirm-btn {
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 0.9rem;
  cursor: pointer;
  border: none;
}

.confirm-cancel {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.confirm-cancel:hover {
  background: var(--border);
}

.confirm-ok {
  background: var(--error);
  color: #fff;
}

.confirm-ok:hover {
  background: #b04a4a;
}

/* Chat main：grid 两行，下方为可滚动聊天区 */
.chat-main {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr;
  overflow: hidden;
}

.btn-sidebar-toggle {
  display: none;
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 150;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
}

.btn-sidebar-toggle:hover {
  background: var(--surface-2);
}

.icon-menu {
  display: block;
  width: 20px;
  height: 2px;
  background: currentColor;
  box-shadow: 0 6px 0 currentColor, 0 -6px 0 currentColor;
}

.chat-view {
  grid-row: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-messages {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 24px;
  -webkit-overflow-scrolling: touch;
}

.chat-message {
  margin-bottom: 8px;
  width: max-content;
  max-width: 78%;
}

.chat-message.user {
  margin-left: auto;
  margin-right: 0;
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: flex-start;
  gap: 8px;
}

.chat-message.user .role {
  flex-shrink: 0;
  order: 2;
  margin-bottom: 0;
  align-self: flex-start;
}

.chat-message.user .content {
  order: 1;
}

.chat-message.assistant {
  margin-left: 0;
  margin-right: auto;
}

.chat-message .role {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-weight: 500;
}

.chat-message.assistant .role {
  display: block;
}

.chat-message .content {
  display: block;
  width: max-content;
  max-width: 100%;
  height: fit-content;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.95rem;
  line-height: 1.4;
  padding: 6px 10px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
}

.chat-message.user .content {
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.chat-message.assistant .content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
}

/* Markdown 消息内容：去掉首尾空隙，气泡高度贴合文字 */
.chat-message .content.markdown-body {
  line-height: 1.4;
}

.chat-message .content.markdown-body > *:first-child {
  margin-top: 0;
}

.chat-message .content.markdown-body > *:last-child {
  margin-bottom: 0;
}

/* marked 会把内容包在 <p> 里，单行时改为 inline 避免块级多出的行高 */
.chat-message .content.markdown-body p {
  margin: 0;
  padding: 0;
}

.chat-message .content.markdown-body p:only-of-type {
  display: inline;
}

.chat-message .content.markdown-body p + p {
  margin-top: 0.35em;
}

.chat-message .content.markdown-body h1,
.chat-message .content.markdown-body h2,
.chat-message .content.markdown-body h3,
.chat-message .content.markdown-body h4 {
  margin: 0.45em 0 0.25em;
  font-weight: 600;
  line-height: 1.25;
}

.chat-message .content.markdown-body h1 { font-size: 1.05em; }
.chat-message .content.markdown-body h2 { font-size: 1em; }
.chat-message .content.markdown-body h3,
.chat-message .content.markdown-body h4 { font-size: 0.95em; }

.chat-message .content.markdown-body ul,
.chat-message .content.markdown-body ol {
  margin: 0.25em 0;
  padding-left: 1.3em;
}

.chat-message .content.markdown-body li {
  margin: 0.1em 0;
}

.chat-message .content.markdown-body blockquote {
  margin: 0.3em 0;
  padding: 0.15em 0 0.15em 0.6em;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

.chat-message .content.markdown-body code {
  padding: 0.1em 0.3em;
  font-size: 0.85em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: ui-monospace, "Cascadia Code", "Source Code Pro", Menlo, monospace;
}

.chat-message .content.markdown-body .code-block-wrap {
  position: relative;
  margin: 0.4em 0;
}

.chat-message .content.markdown-body .btn-copy-code {
  position: absolute;
  top: 6px;
  right: 8px;
  padding: 4px 10px;
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  z-index: 1;
}

.chat-message .content.markdown-body .btn-copy-code:hover {
  color: var(--text);
  background: var(--surface-2);
}

.chat-message .content.markdown-body pre {
  margin: 0;
  padding: 28px 10px 8px 10px;
  overflow-x: auto;
  max-height: 16em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.82rem;
  line-height: 1.45;
}

.chat-message .content.markdown-body pre code {
  padding: 0;
  background: none;
  border: none;
  font-size: inherit;
}

.chat-message .content.markdown-body a {
  color: var(--blue);
  text-decoration: none;
}

.chat-message .content.markdown-body a:hover {
  text-decoration: underline;
}

.chat-message .content.markdown-body table {
  border-collapse: collapse;
  font-size: 0.9em;
  margin: 0.5em 0;
}

.chat-message .content.markdown-body th,
.chat-message .content.markdown-body td {
  padding: 6px 10px;
  border: 1px solid var(--border);
}

.chat-message .content.markdown-body th {
  background: var(--surface-2);
  font-weight: 600;
}

.chat-message .content.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1em 0;
}

.chat-message.assistant .content:empty::after {
  content: "正在回答…";
  color: var(--text-muted);
  animation: blink 1s ease-in-out infinite;
}

@keyframes blink {
  50% { opacity: 0.5; }
}

.chat-message.error .content {
  color: var(--error);
}

/* Thinking block */
.thinking-block {
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  overflow: hidden;
}

.thinking-header {
  padding: 10px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--text-muted);
  user-select: none;
}

.thinking-header:hover {
  background: rgba(0, 0, 0, 0.03);
}

.thinking-header .label {
  font-weight: 500;
  color: var(--text);
}

.thinking-header .arrow {
  transition: transform 0.2s;
}

.thinking-block.collapsed .thinking-header .arrow {
  transform: rotate(-90deg);
}

.thinking-content {
  padding: 12px 14px 14px;
  border-top: 1px solid var(--border);
  font-size: 0.875rem;
  line-height: 1.6;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

.thinking-block.collapsed .thinking-content {
  display: none;
}

.thinking-content ul {
  margin: 0;
  padding-left: 1.2em;
}

.chat-footer {
  flex-shrink: 0;
  padding: 12px 24px 20px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.chat-form textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  resize: none;
  min-height: 44px;
  max-height: 120px;
  margin-bottom: 8px;
  transition: border-color 0.2s;
}

.chat-form textarea::placeholder {
  color: var(--text-muted);
}

.chat-form textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.chat-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.mode-toggle {
  display: flex;
  gap: 4px;
}

.mode-btn {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  border-radius: 8px;
  font-size: 0.875rem;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.mode-btn:hover {
  color: var(--text);
  border-color: #d4d2cc;
}

.mode-btn.active {
  background: var(--blue-soft);
  border-color: var(--blue);
  color: var(--blue);
  font-weight: 500;
}

.btn-send {
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-weight: 500;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-send:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-send:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.footer-hint {
  margin: 6px 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Ingest view */
.ingest-view {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.ingest-view .ingest-card {
  max-width: 560px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.ingest-view .ingest-card h2 {
  margin: 0 0 12px;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.ingest-view label {
  display: block;
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-bottom: 8px;
}

.ingest-view textarea {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  resize: vertical;
  min-height: 160px;
  margin-bottom: 12px;
}

.ingest-view .btn-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.btn-back-chat {
  padding: 10px 16px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-weight: 500;
  font-size: 0.9rem;
  cursor: pointer;
}

.btn-back-chat:hover {
  background: var(--border);
}

.ingest-view .ingest-hint {
  font-size: 0.875rem;
  color: var(--text-muted);
}

.ingest-view .ingest-hint.success {
  color: var(--success);
}

.ingest-view .ingest-hint.error {
  color: var(--error);
}

.ingest-view .btn-row button {
  padding: 10px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-weight: 500;
  font-size: 0.9rem;
  cursor: pointer;
}

.ingest-view .btn-row button:hover:not(:disabled) {
  background: var(--accent-hover);
}

.ingest-view .btn-row button:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

/* 移动端：侧边栏折叠，点击展开为左侧浮层；底部安全区避免被控制栏遮挡 */
@media (max-width: 768px) {
  body.sidebar-open {
    overflow: hidden;
  }

  .btn-sidebar-toggle {
    display: flex;
  }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 199;
    transition: opacity 0.2s;
  }

  .sidebar-backdrop.show {
    display: block;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 200;
    width: min(var(--sidebar-w), 85vw);
    max-width: 320px;
    transform: translateX(-100%);
    transition: transform 0.25s ease-out;
    box-shadow: none;
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-hover);
  }

  .chat-main {
    width: 100%;
  }

  .chat-message {
    max-width: 90%;
  }
}
