/**
 * ai-page.css - AI聊天页面样式
 */

/* ========== AI页面容器 ========== */
.ai-page {
  padding: 0 !important;
  height: calc(100vh - 60px);
  max-height: calc(100vh - 60px);
  overflow: hidden !important;
  position: relative;
  background: var(--bg-white);
}

/* AI页面激活时使用flex布局 */
.ai-page.active {
  display: flex !important;
}

/* AI页面进入动效 */
.ai-page.active .ai-main {
  animation: aiPageFadeIn 0.3s ease;
}

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

/* AI页面时隐藏主容器溢出并移除padding */
.main:has(.ai-page.active),
.main.ai-page-active {
  overflow: hidden;
  max-height: calc(100vh - 56px);
  padding: 0 !important;
}

/* ========== 左侧边栏 ========== */
.ai-sidebar {
  width: 260px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  height: 100%;
  overflow: hidden;
  transition: margin-left 0.3s ease;
}

.ai-sidebar.collapsed { margin-left: -260px; }

.ai-sidebar-header { padding: 12px; }

.ai-new-chat-btn {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  text-align: left;
}

.ai-new-chat-btn:hover { background: var(--bg); transform: translateY(-1px); }
.ai-new-chat-btn:active { transform: translateY(0); }

.ai-history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.ai-history-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 2px;
  font-size: 13px;
  color: var(--text);
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.ai-history-item:hover,
.ai-history-item.active { background: var(--border); }

.ai-history-item:active { transform: scale(0.98); }

.ai-history-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-history-item-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  font-size: 14px;
  transition: opacity 0.15s;
}

.ai-history-item:hover .ai-history-item-delete { opacity: 1; }
.ai-history-item-delete:hover { color: var(--text); }

/* ========== 主聊天区域 ========== */
.ai-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  height: 100%;
  overflow: hidden;
  /* 确保内容不会溢出到页面 */
  max-height: 100%;
}

/* ========== 欢迎界面 ========== */
.ai-welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  background: var(--bg-white);
  overflow: hidden;
}

.ai-welcome h1 {
  font-size: 28px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 32px;
}

.ai-suggestions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  max-width: 600px;
}

.ai-suggestion-btn {
  padding: 14px 18px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.ai-suggestion-btn:hover {
  background: var(--border);
  border-color: var(--text-muted);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}

.ai-suggestion-btn:active {
  transform: translateY(0);
}

/* ========== 消息列表 ========== */
.ai-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 40px 140px;
  display: none;
  background: var(--bg-white);
  scroll-behavior: auto;
  /* 确保滚动条始终显示，避免展开内容时抖动 */
  scrollbar-gutter: stable;
}

.ai-messages.has-messages { display: block; }

.ai-message {
  max-width: 800px;
  margin: 0 auto 20px;
  padding: 16px 20px;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.7;
}

.ai-message.user {
  background: var(--bg);
  max-width: 70%;
  width: fit-content;
  margin-left: auto;
  margin-right: calc(50% - 400px);
  border-radius: 20px;
  padding: 12px 18px;
}

.ai-message.assistant {
  background: transparent;
  padding-left: 0;
  padding-right: 0;
}

.ai-message-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}

.ai-message-avatar {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  background: transparent;
  border: none;
}

.ai-message-content {
  white-space: pre-wrap;
  word-wrap: break-word;
}

/* ========== 思考过程 ========== */
.ai-thinking-block {
  margin-bottom: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.ai-thinking-block summary {
  cursor: pointer;
  color: var(--text-light);
  font-size: 13px;
  font-weight: 500;
  padding: 12px;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
}

.ai-thinking-block summary::-webkit-details-marker { display: none; }
.ai-thinking-block summary::before {
  content: '▶';
  font-size: 10px;
  transition: transform 0.2s ease;
}

.ai-thinking-block[open] summary::before { transform: rotate(90deg); }

.ai-thinking-content {
  padding: 0 12px 12px;
  border-top: 1px dashed var(--border);
  color: var(--text-light);
  font-size: 13px;
  white-space: pre-wrap;
  max-height: 300px;
  overflow-y: auto;
}

/* 移除动画避免抖动 */

/* ========== 输入区域 ========== */
.ai-input-area {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px 40px 20px;
}

.ai-input-wrapper {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg);
  border-radius: 24px;
  padding: 8px 12px;
  transition: background 0.2s;
}

.ai-input-wrapper:focus-within { background: var(--border); }

.ai-input-divider {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

.ai-input-wrapper textarea {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  max-height: 150px;
  color: var(--text);
  padding: 4px 8px;
  overflow: hidden;
}

.ai-input-wrapper textarea:focus { outline: none; }
.ai-input-wrapper textarea::placeholder { color: var(--text-muted); }

.ai-input-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-light);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
  font-size: 16px;
}

.ai-input-btn:hover { background: var(--border); color: var(--text); }

.ai-input-btn.ai-thinking-btn.active {
  color: var(--text);
  background: transparent;
  border: 1px solid var(--text);
}

.ai-input-btn.ai-send-btn { background: var(--text); color: var(--bg-white); }
.ai-input-btn.ai-send-btn:hover { background: var(--primary-dark); }
.ai-input-btn.ai-send-btn:disabled { background: var(--text-muted); cursor: not-allowed; }

.send-icon { font-size: 14px; font-weight: 600; }

/* ========== 消息操作 ========== */
.ai-msg-actions {
  display: flex;
  gap: 4px;
  margin-top: 12px;
  opacity: 0;
  transition: opacity 0.2s;
}

.ai-message.assistant:hover .ai-msg-actions { opacity: 1; }

.ai-msg-action-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-light);
  width: 32px;
  height: 32px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: all 0.15s;
}

.ai-msg-action-btn:hover {
  background: var(--bg-white);
  border-color: var(--text-light);
  color: var(--text);
}

/* ========== Markdown内容样式 ========== */
.md-content {
  font-size: 15px;
  line-height: 1.8;
  color: var(--text);
}

.md-content .md-heading {
  margin: 20px 0 12px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.4;
}

.md-content .md-h1 {
  font-size: 1.6em;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.md-content .md-h2 {
  font-size: 1.4em;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.md-content .md-h3 { font-size: 1.2em; }
.md-content .md-h4 { font-size: 1.1em; }
.md-content .md-h5,
.md-content .md-h6 { font-size: 1em; }

.md-content .md-p { margin: 12px 0; }
.md-content .md-p:first-child { margin-top: 0; }
.md-content .md-p:last-child { margin-bottom: 0; }

.md-content .md-list {
  margin: 12px 0;
  padding-left: 24px;
}

.md-content .md-list li {
  margin: 6px 0;
  line-height: 1.7;
}

.md-content ul.md-list { list-style-type: disc; }
.md-content ol.md-list { list-style-type: decimal; }

.md-content .md-hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 20px 0;
}

.md-content strong { font-weight: 600; color: var(--text); }
.md-content em { font-style: italic; }

.md-content .inline-code {
  background: #f4f4f4;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SF Mono', Monaco, 'Courier New', monospace;
  font-size: 0.9em;
  color: #333;
}

.md-content .code-block {
  background: #f4f4f4;
  color: #333;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 16px 0;
  font-family: 'SF Mono', Monaco, 'Courier New', monospace;
  font-size: 13px;
  line-height: 1.5;
}

.md-content .code-block code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

/* ========== 响应式 ========== */
@media (max-width: 900px) {
  .ai-message.user { margin-right: 20px; }
}

@media (max-width: 768px) {
  /* 平板端：侧边栏默认隐藏，使用collapsed类控制 */
  .ai-sidebar { 
    margin-left: -260px;
  }
  .ai-sidebar:not(.collapsed) {
    margin-left: 0;
  }
  .ai-suggestions { grid-template-columns: 1fr; }
  .ai-welcome h1 { font-size: 22px; }
  .ai-message.user { max-width: 85%; }
}

/* ========== 手机端适配 (≤480px) ========== */
@media (max-width: 480px) {
  .ai-page {
    height: calc(100vh - 56px);
    max-height: calc(100vh - 56px);
    flex-direction: row;
  }
  
  /* 手机端侧边栏：在内部左侧，挤压右边内容 */
  .ai-sidebar {
    display: flex !important;
    position: relative;
    width: 0;
    margin-left: 0 !important;
    flex-shrink: 0;
    overflow: hidden;
    transition: width 0.25s ease;
    border-right: none;
    z-index: 1;
  }
  
  .ai-sidebar.show {
    width: 120px;
    border-right: 1px solid var(--border);
  }
  
  .ai-sidebar-header {
    padding: 8px;
    min-width: 120px;
  }
  
  .ai-new-chat-btn {
    padding: 8px 10px;
    font-size: 12px;
    border-radius: 6px;
  }
  
  .ai-history-list {
    padding: 4px;
    min-width: 120px;
  }
  
  .ai-history-item {
    padding: 6px 8px;
    font-size: 11px;
    border-radius: 6px;
  }
  
  /* 主区域 */
  .ai-main {
    flex: 1;
    min-width: 0;
  }
  
  .ai-welcome {
    padding: 16px 12px;
  }
  
  .ai-welcome h1 {
    font-size: 18px;
    margin-bottom: 20px;
  }
  
  .ai-suggestions {
    grid-template-columns: 1fr;
    gap: 8px;
    width: 100%;
    max-width: none;
    padding: 0;
  }
  
  .ai-suggestion-btn {
    padding: 10px 12px;
    font-size: 12px;
    border-radius: 8px;
  }
  
  .ai-messages {
    padding: 12px 10px 100px;
  }
  
  .ai-message {
    max-width: 100%;
    padding: 10px 12px;
    font-size: 13px;
    border-radius: 10px;
  }
  
  .ai-message.user {
    max-width: 88%;
    margin-right: 0;
    padding: 8px 12px;
    border-radius: 14px;
  }
  
  .ai-message.assistant {
    padding-left: 0;
    padding-right: 0;
  }
  
  .ai-message-header {
    font-size: 12px;
    margin-bottom: 4px;
  }
  
  .ai-message-avatar {
    width: 20px;
    height: 20px;
    font-size: 10px;
  }
  
  .ai-input-area {
    padding: 8px 10px 12px;
    background: linear-gradient(transparent, var(--bg-white) 20%);
  }
  
  .ai-input-wrapper {
    padding: 6px 10px;
    border-radius: 18px;
    gap: 3px;
  }
  
  .ai-input-wrapper textarea {
    font-size: 14px;
    padding: 4px 6px;
  }
  
  .ai-input-btn {
    width: 28px;
    height: 28px;
    font-size: 13px;
  }
  
  .ai-input-divider {
    height: 16px;
    margin: 0 2px;
  }
  
  .ai-thinking-block summary {
    padding: 8px;
    font-size: 11px;
  }
  
  .ai-thinking-content {
    padding: 0 8px 8px;
    font-size: 11px;
    max-height: 150px;
  }
  
  .ai-msg-actions {
    opacity: 1;
    margin-top: 6px;
  }
  
  .ai-msg-action-btn {
    width: 24px;
    height: 24px;
    font-size: 11px;
  }
  
  .md-content {
    font-size: 13px;
    line-height: 1.6;
  }
  
  .md-content .md-heading {
    margin: 12px 0 8px;
  }
  
  .md-content .md-h1 { font-size: 1.3em; }
  .md-content .md-h2 { font-size: 1.2em; }
  .md-content .md-h3 { font-size: 1.1em; }
  
  .md-content .code-block {
    padding: 10px;
    font-size: 11px;
    border-radius: 6px;
  }
  
  .md-content .md-list {
    padding-left: 18px;
  }
}

/* ========== 深色模式 ========== */
:root.dark .ai-page,
:root.dark .ai-welcome,
:root.dark .ai-messages { background: #212121; }

:root.dark .ai-sidebar { background: #171717; }

:root.dark .ai-new-chat-btn {
  background: #2d2d2d;
  border-color: #3d3d3d;
  color: #e0e0e0;
}

:root.dark .ai-new-chat-btn:hover { background: #3d3d3d; }

:root.dark .ai-history-item { color: #e0e0e0; }
:root.dark .ai-history-item:hover,
:root.dark .ai-history-item.active { background: #2d2d2d; }

:root.dark .ai-welcome h1,
:root.dark .ai-message-header { color: #e0e0e0; }

:root.dark .ai-suggestion-btn,
:root.dark .ai-message.user {
  background: #2d2d2d;
  border-color: #3d3d3d;
  color: #e0e0e0;
}

:root.dark .ai-suggestion-btn:hover { background: #3d3d3d; }

:root.dark .ai-message.assistant .ai-message-avatar {
  background: transparent;
  color: inherit;
  border: none;
}

:root.dark .ai-thinking-block {
  background: #2d2d2d;
  border-color: #3d3d3d;
}

:root.dark .ai-thinking-block summary { color: #aaa; }
:root.dark .ai-thinking-content { border-top-color: #3d3d3d; color: #999; }

:root.dark .ai-input-wrapper { background: #2d2d2d; }
:root.dark .ai-input-wrapper:focus-within { background: #3d3d3d; }
:root.dark .ai-input-divider { background: #444; }
:root.dark .ai-input-wrapper textarea { color: #e0e0e0; }
:root.dark .ai-input-wrapper textarea::placeholder { color: #888; }

:root.dark .ai-input-btn { color: #aaa; }
:root.dark .ai-input-btn:hover { background: #3d3d3d; color: #e0e0e0; }
:root.dark .ai-input-btn.ai-thinking-btn.active { color: #e0e0e0; border-color: #e0e0e0; }
:root.dark .ai-input-btn.ai-send-btn { background: #e0e0e0; color: #212121; }
:root.dark .ai-input-btn.ai-send-btn:hover { background: #fff; }

:root.dark .md-content .code-block { background: #2d2d2d; color: #e0e0e0; }
:root.dark .md-content .inline-code { background: #3d3d3d; color: #e0e0e0; }
