* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: #f7f4ef;
  color: #2b2620;
}

/* 左侧栏固定不动、底部输入框固定不动，只有中间对话内容跟随浏览器原生滚动条滚动，
   所以页面不再整体锁 100vh 高度，也不给 .messages 单独开局部滚动框 */
.layout {
  min-height: 100vh;
}

/* ---------- 左侧栏 ---------- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 260px;
  background: #efe8da;
  border-right: 1px solid #e0d5bd;
  display: flex;
  flex-direction: column;
  padding: 12px;
  overflow-y: auto;
}

.sidebar-brand {
  padding: 6px 8px 14px;
  font-size: 16px;
  font-weight: 600;
  color: #6b3f21;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid #d9cfba;
  border-radius: 10px;
  font-size: 14px;
  color: #2b2620;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
}
.new-chat-btn:hover {
  background: #fbf7ee;
  border-color: #6b3f21;
}
.new-chat-btn:active {
  background: #f1ece1;
}

.history-section {
  margin-top: 18px;
  flex: 1;
  overflow-y: auto;
}

.history-label {
  font-size: 12px;
  color: #a08f77;
  padding: 4px 10px 8px;
}

.history-empty {
  padding: 8px 10px;
  font-size: 12.5px;
  color: #a08f77;
}

.history-item {
  display: block;
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13.5px;
  color: #2b2620;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: none;
  background: none;
  text-align: left;
  font-family: inherit;
}
.history-item:hover {
  background: #e5ded2;
}
.history-item.active {
  background: #dfd5bf;
  font-weight: 600;
}

.sidebar-footer {
  border-top: 1px solid #e0d5bd;
  padding: 10px 8px 4px;
  font-size: 13px;
  color: #8a7f6e;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

/* ---------- 右侧主区 ---------- */
.main {
  margin-left: 351px;
  margin-right: 91px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-width: 0;
  /* 底部留出固定输入框的空间，避免最后一条消息被遮住 */
  padding-bottom: 96px;
}

/* 欢迎态：居中展示 */
.welcome-wrap {
  flex: 1;
  width: 100%;
  max-width: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
}
.welcome-wrap.hidden {
  display: none;
}

.welcome-title {
  font-size: 24px;
  color: #6b3f21;
  margin: 0 0 12px;
}

.welcome-desc {
  font-size: 14.5px;
  color: #6b5f4d;
  line-height: 1.7;
  margin: 0 0 28px;
  max-width: 100%;
  word-break: break-all;
  white-space: normal;
  overflow-wrap: break-word;
}

.example-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 16px;
  width: 100%;
}

.example-card {
  flex: 0 0 auto;
  text-align: center;
  padding: 13px 14px;
  background: #fff;
  border: 1px solid #e5ded2;
  border-radius: 10px;
  font-size: 13.5px;
  color: #4a4234;
  cursor: pointer;
  line-height: 1.5;
  font-family: inherit;
}
.example-card:hover {
  border-color: #6b3f21;
  background: #fbf7ee;
}

/* 对话态 */
.chat-view {
  display: none;
  width: 100%;
  max-width: none;
  flex-direction: column;
}
.chat-view.active {
  display: flex;
}

.messages {
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* 头像 + 气泡一行，气泡本身占满整行宽度（align-self: stretch），
   靠文字对齐（text-align）区分用户/assistant，视觉右边缘保持对齐 */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.message-row.user {
  flex-direction: row-reverse;
}

.avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
}

.avatar-assistant {
  background: #6b3f21;
  color: #fff;
}

.avatar-user {
  background: #e5ded2;
  color: #6b5f4d;
}

.avatar-user svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.message {
  display: block;
  padding: 10px 14px;
  border-radius: 12px;
  line-height: 1.6;
  font-size: 15px;
  word-break: break-word;
  white-space: pre-wrap;
}

.message.assistant {
  width: 100%;
  text-align: left;
  background: #fff;
  border: 1px solid #e5ded2;
}

.message.user {
  max-width: 50%;
  text-align: left;
  background: #6b3f21;
  color: #fff;
  margin-left: auto;
}

.message p {
  margin: 0 0 0.5em 0;
}

.message p:last-child {
  margin-bottom: 0;
}

.message.error {
  align-self: center;
  background: #fbe6e6;
  color: #a33;
  border: 1px solid #eec;
}

.message.notice {
  align-self: center;
  background: #eef6ea;
  color: #3f7a3f;
  border: 1px solid #cfe8cb;
  font-size: 13.5px;
  cursor: pointer;
  text-decoration: underline;
}

/* markdown 内容排版 */
.message.assistant :first-child {
  margin-top: 0;
}
.message.assistant :last-child {
  margin-bottom: 0;
}
.message.assistant h1,
.message.assistant h2,
.message.assistant h3 {
  color: #6b3f21;
  margin: 4px 0 2px;
  font-size: 17px;
}
.message.assistant p {
  margin: 0 0 0.2em 0;
}
.message.assistant strong {
  color: #6b3f21;
}
.message.assistant blockquote {
  margin: 2px 0;
  padding: 4px 12px;
  border-left: 3px solid #d9b98a;
  background: #fbf7ee;
  color: #5a4f3f;
}
.message.assistant ul,
.message.assistant ol {
  margin: 2px 0;
  padding-left: 22px;
}
.message.assistant code {
  background: #f1ece1;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

.feedback-bar {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: center;
}

.feedback-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  color: #999;
  position: relative;
  transition: all 0.2s ease;
  line-height: 1;
}

.feedback-btn:hover {
  background: rgba(0, 0, 0, 0.05);
}

/* SVG 线框图标 */
.feedback-btn svg {
  display: block;
  width: 16px;
  height: 16px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
}

/* active 状态：淡灰填充 */
.feedback-btn.active {
  color: #666;
  background: #f0f0f0;
}

.feedback-btn.active svg {
  fill: #666;
  stroke: #666;
}

/* 复制成功状态：打勾图标 */
.feedback-btn.copy-btn.copied svg {
  stroke: #666;
}

/* Tooltip 提示（黑色背景样式） */
.feedback-btn::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 100;
}

.feedback-btn:hover::after {
  opacity: 1;
}

.feedback-comment-box {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.feedback-comment-input {
  font-family: inherit;
  font-size: 13px;
  padding: 8px 10px;
  border: 1px solid #e5ded2;
  border-radius: 8px;
  resize: vertical;
  min-height: 50px;
}

.feedback-comment-submit {
  align-self: flex-end;
  background: #6b3f21;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 12.5px;
  cursor: pointer;
}

.typing-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: #6b3f21;
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.9s step-start infinite;
}
@keyframes blink {
  50% { opacity: 0; }
}

.message.thinking {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #8a7f6e;
}
.thinking-text {
  font-size: 15px;
}
.thinking-dots {
  display: inline-flex;
  gap: 3px;
}
.thinking-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #8a7f6e;
  animation: thinking-bounce 1.2s ease-in-out infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes thinking-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ---------- 输入区：固定在屏幕底部，不随对话内容滚动 ---------- */
.chat-form {
  position: fixed;
  left: 351px;
  right: 91px;
  bottom: 0;
  display: flex;
  justify-content: center;
  padding: 0 0 20px 0;
  background: linear-gradient(to top, #f7f4ef 60%, rgba(247, 244, 239, 0));
}
.chat-form > .composer-box {
  width: calc(100% - 42px);
  max-width: none;
  margin-left: 42px;
}
.main:has(.chat-view.active) .chat-form > .composer-box {
  max-width: none;
}

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: #fff;
  border: 1px solid #d9cfba;
  border-radius: 14px;
  padding: 8px 8px 8px 16px;
}
.composer-box:focus-within {
  border-color: #6b3f21;
}

#question {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  padding: 8px 0;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  min-height: 24px;
  max-height: 160px;
  overflow-y: auto;
}

#send-btn {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #6b3f21;
  color: #fff;
  border: none;
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.link-btn {
  background: none;
  border: none;
  color: #6b3f21;
  font-size: 13px;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  font-family: inherit;
}

.hidden {
  display: none !important;
}

/* 登录/注册弹窗 */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(43, 38, 32, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal {
  position: relative;
  background: #fff;
  border-radius: 14px;
  padding: 28px 24px 20px;
  max-width: 360px;
  width: 100%;
}

.modal.profile-modal {
  max-width: 480px;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 14px;
  background: none;
  border: none;
  font-size: 20px;
  color: #a08f77;
  cursor: pointer;
}

.modal h2 {
  margin: 0 0 8px;
  font-size: 18px;
  color: #6b3f21;
}

.modal-hint {
  margin: 0 0 16px;
  font-size: 13px;
  color: #8a7f6e;
  line-height: 1.5;
}

#auth-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#auth-form input {
  padding: 10px 12px;
  border: 1px solid #d9cfba;
  border-radius: 8px;
  font-size: 15px;
  font-family: inherit;
}

#auth-submit {
  margin-top: 4px;
  padding: 10px;
  background: #6b3f21;
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
}

.auth-error {
  color: #a33;
  font-size: 13px;
  margin: 0;
}

.auth-switch {
  margin: 16px 0 0;
  font-size: 13px;
  color: #8a7f6e;
  text-align: center;
}

/* ---------- 个人资料页 ---------- */
.profile-page {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 40px 16px;
}

.profile-card {
  width: 100%;
  max-width: 480px;
  background: #fff;
  border: 1px solid #e5ded2;
  border-radius: 14px;
  padding: 24px 28px 32px;
}

.profile-title {
  margin: 12px 0 20px;
  color: #6b3f21;
  font-size: 20px;
}

.avatar-edit-row {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.avatar-preview {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  object-fit: cover;
  background: #e5ded2;
}

.avatar-preview-wrap {
  position: relative;
  flex-shrink: 0;
}

.avatar-preview-wrap.has-avatar {
  cursor: pointer;
}

.avatar-preview-wrap .avatar-overlay {
  position: absolute;
  inset: 0;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}

.avatar-preview-wrap.has-avatar:hover .avatar-overlay,
.avatar-preview-wrap.has-avatar:focus-within .avatar-overlay,
.avatar-preview-wrap.has-avatar.overlay-on .avatar-overlay {
  opacity: 1;
  pointer-events: auto;
}

.btn-secondary {
  background: #fff;
  border: 1px solid #d9cfba;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13.5px;
  color: #2b2620;
  cursor: pointer;
  font-family: inherit;
}
.btn-secondary:hover {
  border-color: #6b3f21;
}

.btn-primary {
  background: #6b3f21;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 15px;
  cursor: pointer;
  margin-top: 4px;
}

.profile-hint {
  margin: 6px 0 0;
  font-size: 12px;
  color: #a08f77;
}

.profile-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.profile-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: #6b5f4d;
}

.profile-form input,
.profile-form select,
.profile-form textarea {
  padding: 9px 12px;
  border: 1px solid #d9cfba;
  border-radius: 8px;
  font-size: 14.5px;
  font-family: inherit;
  resize: vertical;
}

.profile-message {
  font-size: 13px;
  color: #3f7a3f;
  margin: 0;
}
.profile-message.error {
  color: #a33;
}

/* 汉堡按钮（桌面端隐藏） */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 1001;
  background: #fff;
  border: 1px solid #e5ded2;
  border-radius: 8px;
  width: 40px;
  height: 40px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  padding: 0;
}

.sidebar-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  background: #6b3f21;
  border-radius: 1px;
}

/* 侧边栏遮罩层 */
.sidebar-overlay {
  display: none;
}

/* ---------- 移动端适配 ---------- */
@media (max-width: 768px) {
  .sidebar-toggle {
    display: flex;
  }

  .sidebar-overlay {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 40;
    transition: opacity 0.3s ease;
  }

  .sidebar-overlay.hidden {
    display: none;
  }
  .sidebar {
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 10px rgba(0,0,0,0.1);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  /* 侧栏在移动端默认藏在屏幕外，主区域和输入框不再需要为它预留空间 */
  .main {
    margin-left: 0;
    margin-right: 0;
  }
  .chat-form {
    left: 0;
    right: 0;
    padding: 0 5px 20px;
  }

  .chat-form > .composer-box {
    margin-left: 0;
    width: 100%;
  }

  .example-grid {
    padding: 0 5px;
  }

  .example-card {
    width: 100%;
  }

  .welcome-wrap {
    padding: 0 5px;
  }

  .chat-view {
    max-width: 100%;
    padding: 0 5px;
  }

  .messages {
    padding: 0 5px;
  }

  /* 移动端：头像和内容分两行，内容宽度100% */
  .message-row {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 6px;
  }

  .message-row.user {
    flex-direction: column !important;
    align-items: flex-end !important;
  }

  .message {
    max-width: 100%;
  }

  .message.user,
  .message.assistant {
    max-width: 100%;
  }

  .message.user {
    text-align: left;
  }

  .modal.profile-modal {
    max-width: 90%;
  }
}

/* Toast 提示 */
.toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 12px 24px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  z-index: 10000;
  font-size: 14px;
  animation: fadeInOut 3s forwards;
}

@keyframes fadeInOut {
  0%, 10% { opacity: 0; transform: translateX(-50%) translateY(-10px); }
  20%, 80% { opacity: 1; transform: translateX(-50%) translateY(0); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-10px); }
}
