/**********************************
 * ChatWidget - 可复用 AI 对话组件样式
 * v1.2
 * 命名空间：cw-
 **********************************/

/* 根容器 */
.cw-root {
  --cw-accent: var(--accent, #0071e3);
  --cw-accent-hover: var(--accent-hover, #005bb5);
  --cw-bg: var(--bg, #f5f5f7);
  --cw-surface: var(--surface, #ffffff);
  --cw-surface-hover: var(--surface-hover, #f0f0f2);
  --cw-border: var(--border, #d2d2d7);
  --cw-text: var(--text, #1d1d1f);
  --cw-text-secondary: var(--text-secondary, #6e6e73);
  --cw-danger: var(--danger, #ff3b30);
  --cw-shadow: var(--shadow, 0 1px 4px rgba(0, 0, 0, 0.08));
  --cw-radius: var(--radius, 12px);

  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.cw-mode-video {
  border-radius: var(--cw-radius);
}

/* 状态条 */
.cw-status-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.cw-status-parsing {
  background: #dbeafe;
  color: #1e40af;
}

.cw-status-completed {
  background: #dcfce7;
  color: #166534;
}

.cw-status-failed {
  background: #fef2f2;
  color: #991b1b;
}

.cw-status-bar svg {
  flex-shrink: 0;
}

/* 英雄区 */
.cw-hero {
  text-align: center;
  margin-bottom: 24px;
  flex-shrink: 0;
}

.cw-hero-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 24px;
  color: var(--cw-accent);
}

.cw-hero-icon svg {
  width: 100%;
  height: 100%;
}

.cw-hero h1 {
  font-size: 28px;
  font-weight: 600;
  margin: 0 0 8px;
  color: var(--cw-text);
}

.cw-hero p {
  color: var(--cw-text-secondary);
  margin: 0;
  font-size: 14px;
}

/* 消息区 */
.cw-messages {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 20px;
  display: flex;
  flex-direction: column;
}

.cw-messages::-webkit-scrollbar {
  width: 6px;
}

.cw-messages::-webkit-scrollbar-thumb {
  background: var(--cw-border);
  border-radius: 3px;
}

/* 消息气泡 */
.cw-message {
  display: flex;
  flex-direction: column;
  max-width: 90%;
  margin-bottom: 16px;
  line-height: 1.6;
  animation: cw-message-in 0.25s ease both;
}

@keyframes cw-message-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.cw-message-user {
  align-self: flex-end;
}

.cw-message-assistant {
  align-self: flex-start;
}

.cw-message-bubble {
  padding: 12px 16px;
  border-radius: var(--cw-radius);
  background: var(--cw-surface);
  box-shadow: var(--cw-shadow);
  color: var(--cw-text);
  font-size: 14px;
  word-break: break-word;
}

.cw-message-user .cw-message-bubble {
  background: var(--cw-accent);
  color: #fff;
}

.cw-message-bubble p {
  margin: 0 0 8px;
}

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

.cw-message-bubble pre {
  background: var(--cw-bg);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  margin: 8px 0;
}

.cw-message-bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
}

.cw-message-bubble ul,
.cw-message-bubble ol {
  margin: 8px 0;
  padding-left: 22px;
}

.cw-message-bubble a {
  color: var(--cw-accent);
}

.cw-message-bubble img {
  max-width: 100%;
  border-radius: 8px;
}

.cw-message-sources {
  font-size: 12px;
  color: var(--cw-text-secondary);
  margin-top: 6px;
  margin-left: 4px;
}

/* AIRPG 叙事消息 */
.cw-message-narrative {
  max-width: 100%;
  width: 100%;
  align-self: flex-start;
  margin-bottom: 20px;
}

.cw-message-narrative .cw-narrative-bubble {
  background: transparent;
  box-shadow: none;
  color: var(--cw-text);
  padding: 0;
  font-size: 1.05rem;
  line-height: 1.8;
  font-family: ui-serif, Georgia, "Songti SC", serif;
  max-width: 680px;
}

.cw-message-narrative .cw-narrative-bubble p {
  margin: 0 0 1em;
}

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

/* AIRPG 控件容器 */
.cw-airpg-controls {
  flex-shrink: 0;
  padding: 0 0 16px;
}

/* 消息中的图片 */
.cw-message-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.cw-message-images img {
  max-width: 100%;
  max-height: 240px;
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

/* 输入面板 */
.cw-input-panel {
  background: var(--cw-surface);
  border: 1px solid var(--cw-border);
  border-radius: var(--cw-radius);
  box-shadow: var(--cw-shadow);
  padding: 12px;
  flex-shrink: 0;
  margin-top: auto;
}

.cw-input-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.cw-input {
  flex: 1;
  border: none;
  outline: none;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  max-height: 120px;
  min-height: 24px;
  background: transparent;
  width: 100%;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-y: auto;
  resize: none;
  color: var(--cw-text);
}

.cw-input:empty:before {
  content: attr(data-placeholder);
  color: var(--cw-text-secondary);
  pointer-events: none;
}

.cw-input.drag-over {
  background: var(--cw-bg);
  outline: 2px dashed var(--cw-accent);
  outline-offset: -2px;
  border-radius: 8px;
}

/* 工具栏 */
.cw-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}

.cw-toolbar-left {
  display: flex;
  align-items: center;
  gap: 4px;
}

.cw-toolbar-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 按钮 */
.cw-icon-btn,
.cw-send-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--cw-text-secondary);
  flex-shrink: 0;
  transition: all 0.15s;
}

.cw-icon-btn:hover {
  background: var(--cw-bg);
  color: var(--cw-text);
}

.cw-icon-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.cw-icon-btn svg {
  width: 18px;
  height: 18px;
}

.cw-send-btn {
  background: var(--cw-accent);
  color: #fff;
}

.cw-send-btn:hover {
  background: var(--cw-accent-hover);
}

.cw-send-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.cw-send-btn.cw-stop-btn {
  background: var(--cw-surface);
  color: var(--cw-text);
  border: 1px solid var(--cw-border);
}

.cw-send-btn.cw-stop-btn:hover {
  background: var(--cw-bg);
}

.cw-send-btn svg {
  width: 18px;
  height: 18px;
}

/* 自由聊天开关 */
.cw-free-chat-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  user-select: none;
  font-size: 13px;
  color: var(--cw-text-secondary);
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.15s;
}

.cw-free-chat-toggle:hover {
  background: var(--cw-bg);
}

.cw-free-chat-toggle input {
  display: none;
}

.cw-free-chat-slider {
  position: relative;
  width: 36px;
  height: 20px;
  background: var(--cw-border);
  border-radius: 20px;
  transition: background 0.15s;
  flex-shrink: 0;
}

.cw-free-chat-slider::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  transition: transform 0.15s;
}

.cw-free-chat-toggle input:checked + .cw-free-chat-slider {
  background: var(--cw-accent);
}

.cw-free-chat-toggle input:checked + .cw-free-chat-slider::before {
  transform: translateX(16px);
}

.cw-free-chat-toggle input:disabled + .cw-free-chat-slider {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 模型选择 */
.cw-model-select {
  padding: 6px 24px 6px 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--cw-text-secondary);
  font-size: 13px;
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236e6e73' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  max-width: 160px;
}

.cw-model-select:hover {
  background-color: var(--cw-bg);
}

.cw-model-select:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 文件 chip */
.cw-file-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 7px 3px 5px;
  background: var(--cw-bg);
  border: 1px solid var(--cw-border);
  border-radius: 6px;
  font-size: 12px;
  color: var(--cw-text-secondary);
  max-width: 200px;
  vertical-align: middle;
  margin-right: 4px;
}

.cw-file-chip:hover {
  background: var(--cw-surface-hover);
}

.cw-file-chip-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.cw-file-chip-icon svg {
  width: 12px;
  height: 12px;
}

.cw-file-chip-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 150px;
}

.cw-file-chip-remove {
  border: none;
  background: transparent;
  color: var(--cw-text-secondary);
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.cw-file-chip-remove:hover {
  color: var(--cw-danger);
}

.cw-file-chip-remove svg {
  width: 12px;
  height: 12px;
}

/* 会话列表 */
.cw-conversation-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cw-conversation-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--cw-text);
  font-size: 13px;
  transition: all 0.15s;
  user-select: none;
}

.cw-conversation-item:hover {
  background: var(--cw-bg);
}

.cw-conversation-item.active {
  background: var(--cw-bg);
  color: var(--cw-accent);
}

.cw-conversation-item-title {
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cw-conversation-tag {
  display: inline-flex;
  align-items: center;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--cw-accent);
  color: #fff;
  font-size: 11px;
  margin-left: 4px;
  flex-shrink: 0;
}

.cw-conversation-item-actions {
  display: none;
  align-items: center;
  gap: 4px;
}

.cw-conversation-item:hover .cw-conversation-item-actions {
  display: flex;
}

.cw-conversation-item-btn {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--cw-text-secondary);
  cursor: pointer;
  border-radius: 4px;
  padding: 0;
}

.cw-conversation-item-btn:hover {
  background: var(--cw-surface-hover);
  color: var(--cw-accent);
}

.cw-conversation-item-btn.delete:hover {
  color: var(--cw-danger);
}

.cw-conversation-item-btn svg {
  width: 12px;
  height: 12px;
}

.cw-conversation-empty {
  padding: 20px 8px;
  text-align: center;
  color: var(--cw-text-secondary);
  font-size: 13px;
}

/* 工具调用提示 */
.cw-tool-call-tip {
  align-self: flex-start;
  max-width: 90%;
  padding: 8px 12px;
  border-radius: 8px;
  background: #dbeafe;
  border: 1px solid #bfdbfe;
  color: #1e40af;
  font-size: 12.5px;
  line-height: 1.5;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
}

.cw-tool-call-tip svg {
  flex-shrink: 0;
  margin-top: 1px;
}

.cw-tool-call-tip strong {
  font-weight: 600;
}

/* 打字动画 */
.cw-typing {
  display: inline-flex;
  gap: 4px;
  padding: 6px 0;
}

.cw-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--cw-text-secondary);
  animation: cw-typing 1.4s infinite ease-in-out both;
}

.cw-typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.cw-typing span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes cw-typing {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* 压缩提示 */
.cw-compress-tip {
  font-size: 11px;
  color: var(--cw-text-secondary);
  margin-top: 6px;
  font-style: italic;
}

/* 空回复 */
.cw-empty-reply {
  color: var(--cw-text-secondary);
}

/* 错误文本 */
.cw-error-text {
  color: var(--cw-danger);
}

/* 隐藏输入框 */
.cw-hidden-input {
  display: none;
}

/* 建议问题（猜你想问） */
.cw-suggested-questions {
  align-self: flex-start;
  max-width: 90%;
  margin: 8px 0 16px 2px;
}

.cw-sq-label {
  font-size: 12px;
  color: var(--cw-text-secondary);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.cw-sq-label::before {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23f59e0b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'%3E%3C/circle%3E%3Cline x1='12' y1='8' x2='12' y2='12'%3E%3C/line%3E%3Cline x1='12' y1='16' x2='12.01' y2='16'%3E%3C/line%3E%3C/svg%3E") center/contain no-repeat;
}

.cw-sq-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.cw-sq-btn {
  padding: 7px 12px;
  border-radius: 16px;
  border: 1px solid #fde68a;
  background: #fffbeb;
  color: #92400e;
  font-size: 13px;
  line-height: 1.4;
  cursor: pointer;
  transition: all 0.15s;
  text-align: left;
  max-width: 100%;
}

.cw-sq-btn:hover {
  background: #fef3c7;
  border-color: #fcd34d;
}

.cw-sq-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 移动端适配 */
@media (max-width: 640px) {
  .cw-hero {
    padding: 28px 20px 14px;
    margin-bottom: 0;
  }

  .cw-hero-icon {
    width: 52px;
    height: 52px;
    margin: 0 auto 14px;
  }

  .cw-hero h1 {
    font-size: 20px;
    font-weight: 700;
    margin-bottom: 6px;
  }

  .cw-hero p {
    font-size: 13px;
    line-height: 1.5;
  }

  .cw-messages {
    padding: 8px 14px 12px;
  }

  .cw-message {
    max-width: 88%;
    margin-bottom: 12px;
  }

  .cw-message-bubble {
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 15px;
    line-height: 1.55;
    box-shadow: none;
  }

  .cw-message-assistant .cw-message-bubble {
    background: var(--cw-surface);
    border: 1px solid var(--cw-border);
    border-top-left-radius: 4px;
  }

  .cw-message-user .cw-message-bubble {
    border-top-right-radius: 4px;
  }

  .cw-message-sources {
    font-size: 11px;
    margin-top: 4px;
  }

  .cw-input-panel {
    position: fixed;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    left: 10px;
    right: 10px;
    padding: 8px 10px;
    border-radius: 20px;
    z-index: 100;
  }

  .cw-input-row {
    align-items: center;
    gap: 6px;
  }

  .cw-input {
    min-height: 22px;
    max-height: 110px;
    padding: 8px 4px;
    font-size: 15px;
  }

  .cw-icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
  }

  .cw-icon-btn svg {
    width: 20px;
    height: 20px;
  }

  .cw-send-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    box-shadow: 0 2px 6px rgba(0, 113, 227, 0.3);
  }

  .cw-send-btn svg {
    width: 18px;
    height: 18px;
  }

  .cw-toolbar {
    margin-top: 6px;
    padding: 0 2px;
  }

  .cw-free-chat-toggle {
    font-size: 11px;
    padding: 2px 4px;
    gap: 4px;
  }

  .cw-free-chat-slider {
    width: 30px;
    height: 17px;
  }

  .cw-free-chat-slider::before {
    width: 13px;
    height: 13px;
  }

  .cw-free-chat-toggle input:checked + .cw-free-chat-slider::before {
    transform: translateX(13px);
  }

  .cw-model-select {
    font-size: 11px;
    max-width: 110px;
    padding-right: 18px;
    background-position: right 4px center;
  }

  .cw-status-bar {
    font-size: 12px;
    padding: 8px 10px;
    margin-bottom: 8px;
  }
}

/**********************************
 * Companion 模式暖色配色覆盖
 **********************************/
.cw-mode-companion {
  --cw-bg: #FFFBF7;
  --cw-surface: #FFF6EF;
  --cw-surface-hover: #FFEDE4;
  --cw-border: #F2E4DA;
  --cw-text: #4A3F35;
  --cw-text-dim: #8B7D72;
  --cw-accent: #E8927C;
  --cw-accent-hover: #D87D66;
  --cw-accent-soft: #F4BFB1;
  --cw-accent-bg: #FFEBE5;
  --cw-ai-bubble: #FFF5EF;
  --cw-user-bubble: #E8927C;
  --cw-love: #E8788A;
  background: var(--cw-bg);
}

.cw-mode-companion .cw-message-bubble {
  border-radius: 18px;
}

.cw-mode-companion .cw-message-user .cw-message-bubble {
  background: var(--cw-user-bubble);
  color: #fff;
  border-top-right-radius: 4px;
}

.cw-mode-companion .cw-message-assistant .cw-message-bubble {
  background: var(--cw-ai-bubble);
  border: 1px solid var(--cw-border);
  border-top-left-radius: 4px;
}

.cw-mode-companion .cw-send-btn {
  background: var(--cw-accent);
  border-radius: 50%;
}

.cw-mode-companion .cw-send-btn:hover {
  background: var(--cw-accent-hover);
}

.cw-mode-companion .cw-input {
  border-radius: 24px;
  background: var(--cw-surface);
  border-color: var(--cw-border);
}

.cw-mode-companion .cw-input:focus {
  border-color: var(--cw-accent-soft);
}

.cw-mode-companion .cw-model-select {
  border-color: var(--cw-border);
  background: var(--cw-surface);
  color: var(--cw-text);
  border-radius: 18px;
}

.cw-mode-companion .cw-typing span {
  background: var(--cw-accent);
}
