:root {
  --bg: #f7f8fa;
  --sidebar: #1a1d24;
  --sidebar-hover: #252932;
  --sidebar-active: #2f3540;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --text: #1f2328;
  --text-sub: #6b7280;
  --border: #e5e7eb;
  --card: #ffffff;
  --success: #10b981;
  --danger: #ef4444;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.6 -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* ============ 侧栏 ============ */
.sidebar {
  width: 260px;
  background: var(--sidebar);
  color: #c9cdd4;
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  flex-shrink: 0;
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 8px 8px 20px; }
.logo {
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, #6366f1, #2563eb);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 18px;
}
.brand-name { color: #fff; font-weight: 600; font-size: 14px; }
.brand-sub { font-size: 11px; color: #8b909a; }

.btn-new {
  width: 100%; padding: 10px;
  background: var(--sidebar-hover);
  color: #e5e7eb; border: 1px solid #333842;
  border-radius: 10px; cursor: pointer; font-size: 13px;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  transition: background .15s;
}
.btn-new:hover { background: var(--sidebar-active); }

.section-title {
  font-size: 11px; color: #6f7580; margin: 20px 8px 8px;
  text-transform: uppercase; letter-spacing: .5px;
}

.conv-list { list-style: none; padding: 0; margin: 0; flex: 1; overflow-y: auto; }
.conv-list li {
  padding: 9px 10px; border-radius: 8px; cursor: pointer;
  font-size: 13px; color: #c9cdd4; margin-bottom: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: background .12s;
}
.conv-list li:hover { background: var(--sidebar-hover); }
.conv-list li.active { background: var(--sidebar-active); color: #fff; }

.sidebar-footer { padding-top: 12px; border-top: 1px solid #2a2e37; }
.user-chip { display: flex; align-items: center; gap: 8px; font-size: 12px; color: #c9cdd4; padding: 4px 8px; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #2563eb);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600;
}

/* 遮罩（默认隐藏） */
.sidebar-mask { display: none; }

/* 汉堡按钮（桌面隐藏） */
.btn-menu {
  display: none;
  background: none; border: none; padding: 6px; cursor: pointer;
  color: var(--text); border-radius: 8px;
  align-items: center; justify-content: center;
}
.btn-menu:hover { background: #f3f4f6; }

/* ============ 主区域 ============ */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: 56px; background: #fff; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; padding: 0 20px; gap: 12px;
  flex-shrink: 0;
}
.tabs { display: flex; gap: 4px; }
.tab {
  background: none; border: none; padding: 6px 14px; border-radius: 8px;
  font-size: 13px; color: var(--text-sub); cursor: pointer; font-weight: 500;
  transition: all .15s; -webkit-tap-highlight-color: transparent;
}
.tab:hover { background: #f3f4f6; }
.tab.active { background: #eff6ff; color: var(--primary); }
.topbar-right {
  margin-left: auto; display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-sub);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success); }

.panel { display: none; flex: 1; min-height: 0; flex-direction: column; }
.panel.active { display: flex; }

/* ============ 对话 ============ */
.messages {
  flex: 1; overflow-y: auto; padding: 28px 10%;
  display: flex; flex-direction: column; gap: 20px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}
.welcome { text-align: center; margin: auto; max-width: 460px; color: var(--text-sub); }
.welcome-icon {
  width: 56px; height: 56px; border-radius: 16px; margin: 0 auto 16px;
  background: linear-gradient(135deg, #6366f1, #2563eb);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 26px;
}
.welcome h2 { color: var(--text); font-size: 20px; margin: 0 0 8px; }
.welcome p { font-size: 13px; line-height: 1.8; }

.msg { display: flex; gap: 10px; max-width: 80%; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 600; color: #fff;
}
.msg.user .msg-avatar { background: linear-gradient(135deg, #6366f1, #2563eb); }
.msg.assistant .msg-avatar { background: linear-gradient(135deg, #10b981, #059669); }
.msg-bubble {
  padding: 11px 15px; border-radius: 14px; white-space: pre-wrap;
  word-break: break-word; font-size: 14px; line-height: 1.7;
}
.msg.user .msg-bubble { background: var(--primary); color: #fff; border-top-right-radius: 4px; }
.msg.assistant .msg-bubble { background: #fff; border: 1px solid var(--border); border-top-left-radius: 4px; }

.typing-dots { display: inline-flex; gap: 3px; }
.typing-dots span {
  width: 6px; height: 6px; border-radius: 50%; background: #b0b5bd;
  animation: blink 1.2s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100% { opacity: .3; } 40% { opacity: 1; } }

.cursor::after {
  content: "▍"; color: var(--primary);
  animation: blink .9s step-end infinite;
}

/* 输入框 */
.composer {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 14px 10%; background: #fff; border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border);
  border-radius: 12px; padding: 10px 14px; font: inherit;
  max-height: 160px; outline: none; transition: border .15s;
  line-height: 1.5;
}
.composer textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }

.btn-send {
  width: 44px; height: 44px; border-radius: 12px; border: none;
  background: var(--primary); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .1s;
  flex-shrink: 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  z-index: 2;
}
.btn-send:hover { background: var(--primary-hover); }
.btn-send:active { transform: scale(.94); }
.btn-send:disabled { opacity: .5; cursor: not-allowed; }

/* ============ 记忆 / 画像公共 ============ */
.panel-head {
  padding: 20px 10% 0; display: flex; align-items: center;
  justify-content: space-between; gap: 16px; flex-wrap: wrap;
}
.panel-head h2 { font-size: 18px; margin: 0; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar input {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px;
  min-width: 220px; outline: none; font: inherit;
}
.toolbar input:focus { border-color: var(--primary); }
.btn {
  padding: 8px 14px; border-radius: 8px; border: none;
  background: var(--primary); color: #fff; cursor: pointer; font-size: 13px;
  transition: background .15s; touch-action: manipulation;
}
.btn:hover { background: var(--primary-hover); }
.btn.ghost { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: #f3f4f6; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.version {
  font-size: 12px; color: var(--text-sub);
  background: #eef2f7; padding: 4px 10px; border-radius: 20px;
}

/* 记忆卡片 */
.mem-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 14px; padding: 20px 10%; overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.mem-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; position: relative;
  transition: box-shadow .15s, transform .15s;
}
.mem-card:hover { box-shadow: 0 4px 20px rgba(0,0,0,.06); transform: translateY(-1px); }
.mem-card .mem-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.mem-type {
  font-size: 11px; padding: 2px 8px; border-radius: 20px;
  background: #eff6ff; color: var(--primary); font-weight: 500;
}
.mem-imp { font-size: 11px; color: #f59e0b; }
.mem-content { font-size: 13px; line-height: 1.6; color: var(--text); word-break: break-word; }
.mem-meta { display: flex; gap: 12px; margin-top: 10px; font-size: 11px; color: var(--text-sub); }
.mem-del {
  position: absolute; top: 8px; right: 8px;
  background: none; border: none; color: var(--text-sub); cursor: pointer;
  opacity: 0; transition: opacity .15s; font-size: 18px;
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.mem-card:hover .mem-del { opacity: 1; }
.mem-del:hover { color: var(--danger); background: #fef2f2; }
.mem-score { font-size: 11px; color: var(--success); }

/* 画像 */
.profile-card {
  margin: 20px 10%; background: #fff; border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px; overflow-y: auto;
}
.profile-nick {
  font-size: 22px; font-weight: 600; margin: 0 0 6px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.profile-nick .badge {
  font-size: 11px; font-weight: 400; background: #eff6ff; color: var(--primary);
  padding: 2px 8px; border-radius: 20px;
}
.profile-attrs { margin: 18px 0; display: flex; flex-wrap: wrap; gap: 8px; }
.attr-tag {
  background: #f3f4f6; padding: 6px 12px; border-radius: 20px;
  font-size: 12px; color: var(--text);
}
.attr-tag b { color: var(--primary); font-weight: 500; }
.profile-summary-title {
  font-size: 12px; color: var(--text-sub); text-transform: uppercase;
  letter-spacing: .5px; margin: 24px 0 10px;
}
.profile-summary {
  font-size: 14px; line-height: 1.9; color: var(--text);
  white-space: pre-wrap; padding: 16px; background: #f9fafb;
  border-radius: 10px; border-left: 3px solid var(--primary);
}

.empty { text-align: center; color: var(--text-sub); padding: 40px; grid-column: 1/-1; }

/* ============ 移动端 ============ */
@media (max-width: 820px) {
  /* 汉堡按钮显示 */
  .btn-menu { display: flex; }

  /* 侧栏变抽屉：默认移出屏幕 */
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 78vw;
    max-width: 300px;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,.2);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }

  /* 遮罩 */
  .sidebar-mask {
    display: block;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 20;
    opacity: 0; pointer-events: none;
    transition: opacity .25s;
  }
  body.sidebar-open .sidebar-mask { opacity: 1; pointer-events: auto; }

  .topbar { padding: 0 12px; gap: 8px; }
  .tab { padding: 6px 10px; font-size: 13px; }
  .topbar-right { display: none; }

  .messages { padding: 16px 14px; gap: 14px; }
  .welcome { margin: auto 0; }
  .welcome h2 { font-size: 18px; }
  .msg { max-width: 90%; }
  .msg-avatar { width: 28px; height: 28px; font-size: 11px; }
  .msg-bubble { padding: 10px 13px; font-size: 14px; }

  .composer {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
  }
  .composer textarea {
    font-size: 16px;
    padding: 10px 12px;
    border-radius: 10px;
  }
  .btn-send { width: 44px; height: 44px; border-radius: 10px; }

  .panel-head { padding: 16px 14px 0; }
  .panel-head h2 { font-size: 16px; }
  .toolbar { width: 100%; }
  .toolbar input { min-width: 0; flex: 1; font-size: 16px; }
  .toolbar .btn { padding: 8px 12px; }

  .mem-grid {
    grid-template-columns: 1fr;
    padding: 14px;
    gap: 12px;
  }
  .mem-card .mem-del { opacity: 1; }

  .profile-card { margin: 14px; padding: 18px; }
  .profile-nick { font-size: 18px; }

  .messages, .mem-grid, .profile-card {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
}

/* 超小屏 */
@media (max-width: 380px) {
  .tab { padding: 6px 8px; font-size: 12px; }
  .messages { padding: 12px 10px; }
  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px)); }
}
/* ===== 反馈按钮 ===== */
.msg-body { display: flex; flex-direction: column; gap: 6px; max-width: 100%; }
.feedback { display: flex; gap: 6px; padding-left: 2px; }
.fb-btn {
  background: #fff; border: 1px solid var(--border); border-radius: 16px;
  padding: 2px 10px; font-size: 13px; cursor: pointer;
  transition: all .15s; line-height: 1.4;
}
.fb-btn:hover { background: #f3f4f6; border-color: #d1d5db; }
.fb-reasons { display: flex; gap: 6px; flex-wrap: wrap; }
.fb-reason {
  background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c;
  border-radius: 16px; padding: 2px 10px; font-size: 12px; cursor: pointer;
}
.fb-reason:hover { background: #fee2e2; }
.fb-done { font-size: 12px; color: var(--success); }

/* ===== 记忆分层标签 ===== */
.mem-layer {
  font-size: 10px; padding: 1px 7px; border-radius: 10px;
  font-weight: 600; letter-spacing: .3px;
}
.mem-layer-core    { background: #dbeafe; color: #1e40af; }
.mem-layer-style   { background: #fef3c7; color: #92400e; }
.mem-layer-context { background: #e0e7ff; color: #3730a3; }
.mem-conf { font-size: 11px; color: var(--text-sub); }

/* ===== 画像规则 ===== */
.profile-rules {
  margin: 0; padding-left: 24px; line-height: 1.9;
  font-size: 14px; color: var(--text);
}
.profile-rules li { margin-bottom: 4px; }
/* ===== 正在回忆状态 ===== */
.thinking-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text-sub);
  font-size: 13px;
}
.thinking-dots {
  display: inline-flex;
  gap: 3px;
}
.thinking-dots span {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--primary);
  animation: think-blink 1.2s infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: .2s; }
.thinking-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes think-blink {
  0%, 80%, 100% { opacity: .3; transform: scale(.8); }
  40% { opacity: 1; transform: scale(1); }
}
.mail-settings {
  margin: 0 10% 40px; background: #fff; border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px 24px;
}
.mail-settings h3 { margin: 0 0 6px; font-size: 16px; }
.mail-hint { color: var(--text-sub); font-size: 13px; margin: 0 0 14px; }
.mail-row { display: flex; gap: 8px; flex-wrap: wrap; }
.mail-row input {
  flex: 1; min-width: 200px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px; font: inherit;
}
.mail-status { margin-top: 10px; font-size: 13px; }
.mail-status.ok { color: var(--success); }
.mail-status.err { color: var(--danger); }
/* ===== 邮件设置开关 ===== */
.mail-toggle {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.toggle-label {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}
.toggle-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  margin-top: 2px;
  accent-color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}
.toggle-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.toggle-text b {
  font-size: 14px;
  color: var(--text);
  font-weight: 500;
}
.toggle-text small {
  font-size: 12px;
  color: var(--text-sub);
  line-height: 1.5;
}