/* ═══════════════════════════════════════════
   Yuunagi Library AI Chat
   ═══════════════════════════════════════════ */

/* ── Global: [hidden] must always win over any display rule ────────────── */
[hidden] {
  display: none !important;
}

/* ── Keyframes ──────────────────────────────────────────────────────────── */
@keyframes yuu-panel-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
@keyframes yuu-msg-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
@keyframes yuu-tool-in {
  from {
    opacity: 0;
    transform: translateX(-6px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
@keyframes yuu-dot-bounce {
  0%,
  60%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* ── Root + FAB ────────────────────────────────────────────────────────── */
.yuu-ai {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 9999;
}

.yuu-ai-fab {
  width: 52px;
  height: 52px;
  border: none;
  border-radius: 16px;
  background: var(--color-link, #2563eb);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    transform 0.2s,
    box-shadow 0.2s;
}
.yuu-ai-fab:hover {
  transform: scale(1.06);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
}
.yuu-ai[data-state="open"] .yuu-ai-fab {
  display: none;
}

/* ── Panel ──────────────────────────────────────────────────────────────── */
.yuu-ai-panel {
  display: none;
  width: min(420px, calc(100vw - 32px));
  height: min(600px, calc(100vh - 40px));
  background: var(--body-background, #fff);
  color: var(--body-font-color, #333);
  border: 1px solid var(--gray-200, #e5e7eb);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.15);
  flex-direction: column;
}
.yuu-ai[data-state="open"] .yuu-ai-panel {
  display: flex;
  animation: yuu-panel-in 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}

/* ── Header ─────────────────────────────────────────────────────────────── */
.yuu-ai-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--gray-200, #e5e7eb);
  background: var(--gray-100, #f9fafb);
  flex-shrink: 0;
}
.yuu-ai-brand {
  display: flex;
  align-items: center;
  gap: 8px;
}
.yuu-ai-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #0fb9b1;
  box-shadow: 0 0 0 3px rgba(15, 185, 177, 0.12);
  flex-shrink: 0;
}
.yuu-ai-title {
  font-size: 14px;
  font-weight: 600;
}
.yuu-ai-subtitle {
  font-size: 11px;
  color: var(--gray-400, #9ca3af);
  margin-left: 2px;
}
.yuu-ai-actions {
  display: flex;
  gap: 2px;
}
.yuu-ai-actions button {
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--gray-500, #6b7280);
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s;
}
.yuu-ai-actions button:hover {
  background: var(--gray-200, #e5e7eb);
}

/* ── Main (drawer container) ────────────────────────────────────────────── */
.yuu-ai-main {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* ── Empty state ────────────────────────────────────────────────────────── */
.yuu-ai-empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 24px;
  text-align: center;
}
.yuu-ai-empty-icon {
  font-size: 28px;
  margin-bottom: 12px;
  opacity: 0.5;
}
.yuu-ai-empty h2 {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 700;
}
.yuu-ai-empty p {
  margin: 0 0 16px;
  color: var(--gray-400, #9ca3af);
  font-size: 13px;
  line-height: 1.6;
  max-width: 280px;
}
.yuu-ai-prompts {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 320px;
}
.yuu-ai-prompts button {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--gray-200, #e5e7eb);
  border-radius: 10px;
  background: var(--body-background, #fff);
  color: var(--body-font-color, #333);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s;
}
.yuu-ai-prompts button:hover {
  border-color: var(--color-link, #2563eb);
}

/* ── Messages ───────────────────────────────────────────────────────────── */
.yuu-ai-messages {
  height: 100%;
  overflow-y: auto;
  padding: 16px;
}
.yuu-ai-message {
  max-width: 88%;
  margin-bottom: 12px;
  padding: 10px 14px;
  border-radius: 12px;
  animation: yuu-msg-in 0.28s ease-out;
  font-size: 14px;
  line-height: 1.6;
  word-wrap: break-word;
}
.yuu-ai-message.user {
  margin-left: auto;
  border-bottom-right-radius: 4px;
  background: var(--color-link, #2563eb);
  color: #fff;
}
.yuu-ai-message.assistant {
  margin-right: auto;
  border-bottom-left-radius: 4px;
  background: var(--gray-100, #f3f4f6);
}
.yuu-msg-content p {
  margin: 0 0 8px;
}
.yuu-msg-content em {
  animation: yuu-dot-bounce 1.4s ease-in-out infinite;
  display: inline-block;
}
.yuu-msg-content p:last-child {
  margin-bottom: 0;
}
.yuu-msg-content h2,
.yuu-msg-content h3,
.yuu-msg-content h4 {
  margin: 8px 0 4px;
  font-size: 15px;
}
.yuu-msg-content pre {
  background: #1f2937;
  color: #e5e7eb;
  padding: 10px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 13px;
  margin: 8px 0;
}
/* KaTeX 公式防溢出：超宽公式横向滚动，不破坏布局 */
.yuu-msg-content .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  padding: 2px 0;
}
.yuu-msg-content .katex {
  max-width: 100%;
}
.yuu-msg-content .katex > .katex-html {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  display: inline-block;
}
.yuu-msg-content code {
  font-family: monospace;
  font-size: 13px;
}
.yuu-msg-content :not(pre) > code {
  background: var(--gray-200, #e5e7eb);
  padding: 1px 5px;
  border-radius: 3px;
}

/* ── Drawer (history / settings) — must be inside main ─────────────────── */
.yuu-ai-drawer {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  padding: 16px;
  background: var(--body-background, #fff);
  overflow-y: auto;
}
.yuu-ai-drawer[hidden] {
  display: none;
}
.yuu-ai-drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  font-size: 15px;
}
.yuu-ai-drawer-head button {
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--gray-400, #9ca3af);
}

.yuu-ai-drawer label {
  margin: 10px 0 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-500, #6b7280);
}
.yuu-ai-drawer input,
.yuu-ai-drawer select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--gray-300, #d1d5db);
  border-radius: 8px;
  font-size: 13px;
  background: var(--body-background, #fff);
  color: var(--body-font-color, #333);
  outline: none;
}
.yuu-ai-drawer input:focus,
.yuu-ai-drawer select:focus {
  border-color: var(--color-link, #2563eb);
}

/* ── History list ────────────────────────────────────────────────────────── */
.yuu-history-empty {
  text-align: center;
  font-size: 13px;
  color: var(--gray-400, #9ca3af);
  padding: 24px 0;
  line-height: 1.6;
}
.yuu-history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  border: 1px solid transparent;
}
.yuu-history-item:hover {
  background: var(--gray-100, #f3f4f6);
  border-color: var(--gray-200, #e5e7eb);
}
.yuu-history-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.yuu-history-date {
  font-size: 11px;
  color: var(--gray-400, #9ca3af);
  white-space: nowrap;
}
.yuu-history-del {
  border: none;
  background: none;
  font-size: 15px;
  cursor: pointer;
  color: var(--gray-400, #9ca3af);
  visibility: hidden;
}
.yuu-history-item:hover .yuu-history-del {
  visibility: visible;
}
.yuu-history-del:hover {
  color: #dc2626;
}

/* ── Settings form ─────────────────────────────────────────────────────── */
.yuu-ai-field {
  margin-bottom: 10px;
}
.yuu-ai-field label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--gray-500, #6b7280);
}
.yuu-ai-field input,
.yuu-ai-field select {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--gray-300, #d1d5db);
  border-radius: 8px;
  font-size: 13px;
  background: var(--body-background, #fff);
  color: var(--body-font-color, #333);
  outline: none;
}
.yuu-ai-field input:focus,
.yuu-ai-field select:focus {
  border-color: var(--color-link, #2563eb);
}

/* Checkbox row — one clickable label, no split layout */
.yuu-ai-check-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 14px 0 12px;
  cursor: pointer;
  width: 100%;
}
.yuu-ai-check-row input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.yuu-ai-check-box {
  width: 20px;
  height: 20px;
  border-radius: 6px;
  flex-shrink: 0;
  border: 2px solid var(--gray-300, #d1d5db);
  background: var(--body-background, #fff);
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    border-color 0.15s,
    background 0.15s;
  margin-top: 1px;
}
.yuu-ai-check-box::after {
  content: "";
  display: none;
  width: 10px;
  height: 6px;
  border-left: 2px solid #fff;
  border-bottom: 2px solid #fff;
  transform: rotate(-45deg) translate(1px, -1px);
}
.yuu-ai-check-row input:checked + .yuu-ai-check-box {
  background: #0fb9b1;
  border-color: #0fb9b1;
}
.yuu-ai-check-row input:checked + .yuu-ai-check-box::after {
  display: block;
}
.yuu-ai-check-row input:focus-visible + .yuu-ai-check-box {
  box-shadow: 0 0 0 3px rgba(15, 185, 177, 0.2);
}
.yuu-ai-check-text strong {
  display: block;
  font-size: 13px;
  font-weight: 600;
}
.yuu-ai-check-text small {
  display: block;
  font-size: 11px;
  color: var(--gray-400, #9ca3af);
  margin-top: 2px;
}

/* Settings action buttons */
.yuu-ai-settings-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}
.yuu-ai-save-btn,
.yuu-ai-test-btn,
.yuu-ai-clear-btn {
  height: 34px;
  padding: 0 14px;
  border-radius: 10px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition:
    opacity 0.15s,
    transform 0.15s;
}
.yuu-ai-save-btn {
  border: none;
  background: #0f8f84;
  color: #fff;
}
.yuu-ai-save-btn:hover {
  opacity: 0.85;
}
.yuu-ai-test-btn {
  border: 1px solid var(--gray-300, #d1d5db);
  background: transparent;
  color: var(--body-font-color, #333);
}
.yuu-ai-test-btn:hover {
  border-color: #0fb9b1;
  color: #0fb9b1;
}
.yuu-ai-test-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.yuu-ai-clear-btn {
  border: 1px solid transparent;
  background: transparent;
  color: var(--gray-400, #9ca3af);
}
.yuu-ai-clear-btn:hover {
  color: #dc2626;
}

/* Test connection status */
.yuu-ai-test-status {
  margin-top: 8px;
  font-size: 12px;
  min-height: 18px;
}
.yuu-ai-test-status[data-status="idle"] {
  color: var(--gray-400, #9ca3af);
}
.yuu-ai-test-status[data-status="loading"] {
  color: var(--gray-500, #6b7280);
}
.yuu-ai-test-status[data-status="success"] {
  color: #0f8f84;
}
.yuu-ai-test-status[data-status="error"] {
  color: #dc2626;
}

/* ── Composer ────────────────────────────────────────────────────────────── */
.yuu-ai-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--gray-200, #e5e7eb);
  background: var(--body-background, #fff);
  flex-shrink: 0;
}
.yuu-ai-composer textarea {
  flex: 1;
  min-height: 38px;
  max-height: 120px;
  resize: none;
  padding: 8px 12px;
  border: 1px solid var(--gray-300, #d1d5db);
  border-radius: 10px;
  font-size: 14px;
  line-height: 1.5;
  outline: none;
  background: var(--body-background, #fff);
  color: var(--body-font-color, #333);
  font-family: inherit;
}
.yuu-ai-composer textarea:focus {
  border-color: var(--color-link, #2563eb);
}
.yuu-ai-send {
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 10px;
  background: var(--color-link, #2563eb);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s;
}
.yuu-ai-send:hover {
  opacity: 0.85;
}
.yuu-ai-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
/* #4 停止生成形态：send 按钮变为停止（方形 ■），可点击中止流式 */
.yuu-ai-send.yuu-ai-stop {
  background: var(--color-text-muted, #6b7280);
}
.yuu-ai-send.yuu-ai-stop::after {
  content: "";
  display: block;
  width: 12px;
  height: 12px;
  background: #fff;
  border-radius: 2px;
}
.yuu-ai-send.yuu-ai-stop svg {
  display: none; /* 隐藏发送箭头，露出 ::after 方块 */
}

/* ── Dark mode ──────────────────────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  .yuu-ai-panel {
    background: #1a1a2e;
    border-color: #2d2d44;
  }
  .yuu-ai-header {
    background: #16213e;
    border-bottom-color: #2d2d44;
  }
  .yuu-ai-title {
    color: #e0e0e0;
  }
  .yuu-ai-message.assistant {
    background: #16213e;
    color: #e0e0e0;
  }
  .yuu-ai-composer {
    background: #1a1a2e;
    border-top-color: #2d2d44;
  }
  .yuu-ai-composer textarea {
    background: #16213e;
    color: #e0e0e0;
    border-color: #2d2d44;
  }
  .yuu-ai-drawer {
    background: #1a1a2e;
  }
  .yuu-ai-drawer input,
  .yuu-ai-drawer select {
    background: #16213e;
    color: #e0e0e0;
    border-color: #2d2d44;
  }
  .yuu-ai-prompts button {
    background: #16213e;
    color: #e0e0e0;
    border-color: #2d2d44;
  }
  .yuu-ai-actions button {
    color: #888;
  }
  .yuu-ai-actions button:hover {
    background: #2d2d44;
  }
  .yuu-history-item:hover {
    background: #16213e;
    border-color: #2d2d44;
  }
}

/* ── Debug card ──────────────────────────────────────────────────────────── */
.yuu-debug-card {
  margin: 8px 0;
  border: 1px solid #f0c040;
  border-radius: 8px;
  background: #fffdf0;
  font-size: 12px;
  overflow: hidden;
}
.yuu-debug-card summary {
  padding: 8px 12px;
  cursor: pointer;
  font-weight: 600;
  color: #8a6d14;
  background: #fff8e0;
  user-select: none;
}
.yuu-debug-card summary:hover {
  background: #fff3c0;
}
.yuu-debug-section {
  padding: 8px 12px;
  border-top: 1px solid #f0e0a0;
}
.yuu-debug-section h4 {
  margin: 0 0 6px;
  font-size: 12px;
  color: #8a6d14;
}
.yuu-debug-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}
.yuu-debug-table th,
.yuu-debug-table td {
  padding: 3px 6px;
  text-align: left;
  border-bottom: 1px solid #f0e0a0;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.yuu-debug-table th {
  color: #8a6d14;
  font-weight: 600;
}
.yuu-debug-used {
  background: #f0fff0;
}
.yuu-debug-ctx {
  margin: 6px 0;
  padding: 6px 8px;
  background: #fff;
  border: 1px solid #e8e0c0;
  border-radius: 4px;
}
.yuu-debug-ctx strong {
  display: block;
  margin-bottom: 2px;
}
.yuu-debug-ctx pre {
  margin: 4px 0 0;
  font-size: 11px;
  color: #555;
  white-space: pre-wrap;
  word-break: break-all;
}
.yuu-debug-ctx-meta {
  font-size: 10px;
  color: #999;
}
.yuu-debug-prompt {
  margin: 4px 0 0;
  font-size: 10px;
  color: #666;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 200px;
  overflow-y: auto;
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
  .yuu-debug-card {
    background: #2a2a1a;
    border-color: #8a6d14;
  }
  .yuu-debug-card summary {
    background: #3a3a20;
    color: #f0c040;
  }
  .yuu-debug-card summary:hover {
    background: #4a4a28;
  }
  .yuu-debug-section {
    border-top-color: #5a5020;
  }
  .yuu-debug-section h4 {
    color: #f0c040;
  }
  .yuu-debug-table th,
  .yuu-debug-table td {
    border-bottom-color: #5a5020;
  }
  .yuu-debug-table th {
    color: #f0c040;
  }
  .yuu-debug-used {
    background: #1a3020;
  }
  .yuu-debug-ctx {
    background: #2a2a1a;
    border-color: #5a5020;
  }
  .yuu-debug-ctx pre {
    color: #aaa;
  }
  .yuu-debug-ctx-meta {
    color: #888;
  }
  .yuu-debug-prompt {
    color: #aaa;
  }
}

/* ── Thinking & Tool Trail (agent) ──────────────────────────────────────── */
.yuu-thinking {
  margin: 4px 0 8px;
  border: 1px solid #d0d0e0;
  border-radius: 8px;
  background: #f8f8fc;
  font-size: 13px;
  overflow: hidden;
}
.yuu-thinking summary {
  padding: 6px 10px;
  cursor: pointer;
  color: #666;
  user-select: none;
  list-style: none;
}
.yuu-thinking summary::-webkit-details-marker {
  display: none;
}
.yuu-thinking-body {
  padding: 8px 12px;
  border-top: 1px solid #e0e0f0;
  color: #888;
  line-height: 1.6;
}
.yuu-tool-trail {
  margin: 4px 0;
  font-size: 12px;
}
.yuu-tool-step {
  padding: 4px 8px;
  margin: 2px 0;
  background: #f0f7ff;
  border-left: 3px solid #4a9eff;
  border-radius: 0 4px 4px 0;
  color: #555;
  animation: yuu-tool-in 0.2s ease-out;
}
.yuu-tool-step code {
  background: #e0eaff;
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 11px;
}

@media (prefers-color-scheme: dark) {
  .yuu-thinking {
    border-color: #3a3a4a;
    background: #2a2a3a;
  }
  .yuu-thinking summary {
    color: #999;
  }
  .yuu-thinking-body {
    border-top-color: #3a3a4a;
    color: #777;
  }
  .yuu-tool-step {
    background: #1e2a3a;
    border-left-color: #4a9eff;
    color: #aaa;
  }
  .yuu-tool-step code {
    background: #2a3a4a;
    color: #b0c8e8;
  }
}

/* ── Mobile ─────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .yuu-ai {
    right: 0;
    bottom: 0;
  }
  .yuu-ai-panel {
    width: 100vw;
    height: 100dvh;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
  }
}
