/**
 * AI 助手聊天浮窗样式
 * 紧凑初始态 → 流式生成时向上扩展
 */

/* 边框描线动画用自定义属性 */
@property --border-angle {
    syntax: '<angle>';
    initial-value: 0deg;
    inherits: false;
}

/* ============================================
   主容器：固定左下角，初始紧凑
   ============================================ */
.ai-chat-widget {
    position: fixed;
    bottom: 0;
    left: 75px;  /* main-nav 宽度 */
    width: 440px;
    min-width: 360px;
    z-index: 910;
    background: var(--color-bg-secondary);
    border: 0px solid var(--color-border);
    border-radius: var(--radius-md); 
    border-radius: 0; 
    box-shadow: 0 0 50px 20px rgba(0, 0, 0, 0.75);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: box-shadow 0.2s;
}

.ai-chat-widget:focus-within {
    box-shadow: 0 0 50px 20px rgba(0, 0, 0, 0.75);
}

/* ============================================
   右侧缩放手柄
   ============================================ */
.ai-chat-resize-handle {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    cursor: ew-resize;
    z-index: 10;
    background: transparent;
    transition: background 0.15s;
}

.ai-chat-resize-handle:hover,
.ai-chat-resize-handle.active {
    background: var(--color-primary);
    opacity: 1;
}

/* ============================================
   顶栏：极简，可拖拽
   ============================================ */
.ai-chat-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 10px;
    cursor: move;
    user-select: none;
    flex-shrink: 0;
    min-height: 28px;
    box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.1);
}

.ai-chat-topbar-title {
    font-size: 12px;
    color: var(--color-text-tertiary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 8px;
}

.ai-chat-topbar-actions {
    display: flex;
    gap: 2px;
    flex-shrink: 0;
}

.ai-chat-topbar-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: var(--color-text-quaternary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: all 0.15s;
}

.ai-chat-topbar-btn:hover {
    background: var(--color-bg-quaternary);
    color: var(--color-text-primary);
}

/* ============================================
   输出区：初始隐藏，生成时展开
   ============================================ */
.ai-chat-output {
    display: none;  /* JS 控制显隐 */
    flex-direction: column;
    gap: 12px;
    padding: 10px 20px 10px 12px;
    overflow-y: auto;
    scroll-behavior: smooth;
    border-bottom: 1px solid var(--color-border);
}

.ai-chat-output.visible {
    display: flex;
}

/* 自定义滚动条 */
.ai-chat-output::-webkit-scrollbar {
    width: 4px;
}
.ai-chat-output::-webkit-scrollbar-track {
    background: transparent;
}
.ai-chat-output::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 2px;
}

/* --- 消息气泡 --- */
.ai-chat-msg {
    animation: chatFadeIn 0.2s ease-out;
}

@keyframes chatFadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* 用户消息 */
.ai-chat-msg.user {
    align-self: flex-end;
    max-width: 88%;
}

.ai-chat-msg.user .msg-content {
    background: var(--color-bg-quaternary);
    color: var(--color-text-tertiary);
    border-radius: 8px 8px 0px 8px;
    padding: 10px;
    font-size: 12px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
    transition: background 0.3s;
}

/* 圆点高亮时对应用户消息背景 */
.ai-chat-msg.user.dot-active .msg-content {
    color: var(--color-text-primary);
    background: var(--color-tertiary);
}

/* 用户消息超过 4 行时折叠 */
.ai-chat-msg.user .msg-content.collapsible {
    max-height: calc(12px * 1.6 * 4);  /* 4 行高度 */
    overflow: hidden;
    cursor: pointer;
    position: relative;
    transition: max-height 0.5s ease;
}

.ai-chat-msg.user .msg-content.collapsible::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    background: linear-gradient(transparent 44%, var(--color-bg-dark) 100%);
    pointer-events: none;
    z-index: 1;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.ai-chat-msg.user .msg-content.collapsible.expanded {
    max-height: var(--scroll-h, 2000px);
}

.ai-chat-msg.user .msg-content.collapsible.expanded::after {
    opacity: 0;
}

/* AI 消息 */
.ai-chat-msg.assistant {
    align-self: flex-start;
    max-width: 95%;
}

.ai-chat-msg.assistant .msg-content {
    background: var(--color-bg-tertiary);
    color: var(--color-text-primary);
    border-radius: 8px 8px 8px 0px;
    padding: 10px 14px;
    font-size: 12px;
    line-height: 1.6;
    word-break: break-word;
}

/* Markdown 内排版 */
.ai-chat-msg.assistant .msg-content p { margin: 0 0 8px 0; }
.ai-chat-msg.assistant .msg-content p:last-child { margin-bottom: 0; }
.ai-chat-msg.assistant .msg-content ul,
.ai-chat-msg.assistant .msg-content ol { margin: 4px 0; padding-left: 20px; }
.ai-chat-msg.assistant .msg-content li { margin-bottom: 2px; }
.ai-chat-msg.assistant .msg-content h1,
.ai-chat-msg.assistant .msg-content h2,
.ai-chat-msg.assistant .msg-content h3 { margin: 10px 0 4px; font-size: 13px; font-weight: 600; }
.ai-chat-msg.assistant .msg-content blockquote {
    border-left: 3px solid var(--color-primary);
    margin: 6px 0; padding: 2px 10px;
    color: var(--color-text-secondary);
}
.ai-chat-msg.assistant .msg-content .code-block {
    margin: 6px 0; border-radius: 6px; overflow: hidden;
    border: 1px solid var(--color-border);
}
.ai-chat-msg.assistant .msg-content .code-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 4px 10px; background: var(--color-bg-quaternary); font-size: 11px;
}
.ai-chat-msg.assistant .msg-content pre {
    margin: 0; padding: 8px 10px; overflow-x: auto;
    font-size: 12px; line-height: 1.5; background: var(--color-bg-dark, #1e1e1e);
}
.ai-chat-msg.assistant .msg-content code {
    font-family: 'Consolas', 'Monaco', monospace; font-size: 12px;
}
.ai-chat-msg.assistant .msg-content p code {
    background: var(--color-bg-quaternary); padding: 1px 4px; border-radius: 3px;
}

/* 消息复制按钮 */
.ai-chat-msg .msg-actions {
    display: flex; gap: 4px; margin-top: 4px; opacity: 0; transition: opacity 0.15s;
}
.ai-chat-msg:hover .msg-actions { opacity: 1; }
.ai-chat-msg .msg-actions button {
    background: none; border: none; color: var(--color-text-quaternary);
    cursor: pointer; padding: 2px 6px; border-radius: 4px; font-size: 12px;
    display: flex; align-items: center; gap: 3px;
}
.ai-chat-msg .msg-actions button:hover {
    background: var(--color-bg-quaternary); color: var(--color-text-secondary);
}

/* --- 思考过程 --- */
.ai-chat-reasoning { margin-bottom: 6px; }
.ai-chat-reasoning-toggle {
    display: flex; align-items: center; gap: 4px; cursor: pointer;
    color: var(--color-text-tertiary); font-size: 12px; user-select: none;
}
.ai-chat-reasoning-toggle i { transition: transform 0.2s; }
.ai-chat-reasoning-toggle.collapsed i { transform: rotate(-90deg); }
.ai-chat-reasoning-content {
    font-size: 12px; color: var(--color-text-tertiary); line-height: 1.5;
    padding: 4px 8px; border-left: 2px solid var(--color-border); margin-top: 4px;
    max-height: 160px; overflow-y: auto; white-space: pre-wrap;
}
.ai-chat-reasoning-content.collapsed { display: none; }

/* --- 打字指示器 --- */
.ai-chat-typing {
    display: flex; align-items: center; gap: 4px; padding: 6px 0;
}
.ai-chat-typing-dot {
    width: 5px; height: 5px; border-radius: 50%;
    background: var(--color-text-quaternary);
    animation: typingBounce 1.2s ease-in-out infinite;
}
.ai-chat-typing-dot:nth-child(2) { animation-delay: 0.15s; }
.ai-chat-typing-dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-3px); opacity: 1; }
}

/* ============================================
   输入区
   ============================================ */
.ai-chat-input-section {
    padding: 10px;
    flex-shrink: 0;
}

.ai-chat-input-section textarea {
    width: 100%;
    box-sizing: border-box;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: 10px;
    font-size: var(--font-size-xxs);
    line-height: 1.5;
    resize: none;
    background: var(--color-bg-primary);
    color: var(--color-text-primary);
    outline: none;
    min-height: 42px;
    max-height: 180px;
    overflow-y: auto;
    transition: border-color 0.2s;
    font-family: inherit;
}

.ai-chat-input-section textarea:focus {
    border-color: var(--color-secondary);
}

.ai-chat-input-section textarea::placeholder {
    color: var(--color-text-quaternary);
}

/* ============================================
   底栏：预设按钮 + 发送
   ============================================ */
.ai-chat-bottom-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px 10px;
    flex-shrink: 0;
    gap: 8px;
}

.ai-chat-presets {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    flex: 1;
}

.ai-chat-preset-btn {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    border: 1px solid var(--color-border);
    border-radius: 99px;
    background: transparent;
    color: var(--color-text-tertiary);
    font-size: 11px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
    text-align: center;
    line-height: 2;
}

.ai-chat-preset-btn:hover {
    background: var(--color-primary);
    color: var(--color-on-primary);
    border-color: var(--color-primary);
}

.ai-chat-preset-btn.active {
    background: var(--color-primary);
    color: var(--color-on-primary);
    border-color: var(--color-primary);
}

.ai-chat-preset-btn i {
    font-size: 13px;
    color: var(--color-text-tertiary);
}

.ai-chat-preset-btn:hover i,
.ai-chat-preset-btn.active i {
    color: var(--color-on-primary);
}

.ai-chat-send-area {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
}

/* 发送按钮已使用全局 .btn-primary，无需额外基础样式 */

.ai-chat-stop-btn {
    height: 32px;
    padding: 0 12px;
    border: none;
    border-radius: 8px;
    background: var(--color-error, #ef4444);
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.15s;
}

.ai-chat-stop-btn:hover {
    opacity: 0.85;
}

/* ============================================
   生成中转圈动画
   ============================================ */
.ai-spin {
    display: inline-block;
    animation: aiSpin 1s linear infinite;
}

@keyframes aiSpin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

/* 顶栏标题生成中状态 */
.ai-chat-topbar-title .ai-spin {
    font-size: 12px;
    margin-right: 4px;
    vertical-align: middle;
}

/* 发送按钮生成中状态：转圈图标 + 灰色背景 */
#ai-chat-send.generating {
    background: var(--color-danger-secondary) !important;
    border-color: var(--color-danger-secondary) !important;
    padding: 0 12px;
    pointer-events: auto;
    opacity: 1 !important;
}

#ai-chat-send.generating .ai-spin {
    font-size: 16px;
}

/* ============================================
   最小化：只显示顶栏
   ============================================ */
.ai-chat-widget.minimized > *:not(.ai-chat-topbar) {
    display: none !important;
}

.ai-chat-widget.minimized .ai-chat-topbar {
    border-bottom: none;
}

/* ============================================
   打开 / 关闭动画
   ============================================ */
.ai-chat-widget.closing {
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.2s ease;
    transform: translateY(20px);
    opacity: 0;
}

/* ============================================
   AI 助手 — 写作页面吸引动画
   ============================================ */

/* 图标容器：动画时裁剪溢出，实现星星穿越圆形边界 */
.main-nav-icon.ai-nudge {
    overflow: hidden;
    position: relative;
}

/* 老虎机式 10 圈翻滚：加速→峰值→约 60% 减速→停住 */
.main-nav-icon.ai-nudge i {
    display: inline-block;
    animation: aiSlotSpin 4s linear 1;
    will-change: transform, filter, opacity;
}

@keyframes aiSlotSpin {
    0%   { transform: translateY(0) scaleY(1);      filter: blur(0);    opacity: 1; }

    /* ▸ Rev 1 — 慢启 (9%, 180ms) */
    4%   { transform: translateY(-170%) scaleY(1.2); filter: blur(1.5px); opacity: 0; }
    5%   { transform: translateY(170%) scaleY(1.2);  filter: blur(1.5px); opacity: 0; }
    9%   { transform: translateY(0) scaleY(1);      filter: blur(0);    opacity: 1; }

    /* ▸ Rev 2 — 加速 (7%, 140ms) */
    12%  { transform: translateY(-170%) scaleY(1.3); filter: blur(2px);   opacity: 0; }
    13%  { transform: translateY(170%) scaleY(1.3);  filter: blur(2px);   opacity: 0; }
    16%  { transform: translateY(0) scaleY(1);      filter: blur(0);    opacity: 1; }

    /* ▸ Rev 3 — 更快 (5%, 100ms) */
    18%  { transform: translateY(-170%) scaleY(1.5); filter: blur(2.5px); opacity: 0; }
    19%  { transform: translateY(170%) scaleY(1.5);  filter: blur(2.5px); opacity: 0; }
    21%  { transform: translateY(0) scaleY(1.1);    filter: blur(0);    opacity: 1; }

    /* ▸ Rev 4 — 峰值 (4%, 80ms) */
    23%  { transform: translateY(-170%) scaleY(1.7); filter: blur(3px);   opacity: 0; }
    24%  { transform: translateY(170%) scaleY(1.7);  filter: blur(3px);   opacity: 0; }
    25%  { transform: translateY(0) scaleY(1.2);    filter: blur(0);    opacity: 1; }

    /* ▸ Rev 5 — 峰值 (4%, 80ms) */
    27%  { transform: translateY(-170%) scaleY(1.8); filter: blur(6px);   opacity: 0; }
    28%  { transform: translateY(170%) scaleY(1.8);  filter: blur(6px);   opacity: 0; }
    29%  { transform: translateY(0) scaleY(1.2);    filter: blur(0);    opacity: 1; }

    /* ▸ Rev 6 — 峰值 (4%, 80ms) */
    31%  { transform: translateY(-170%) scaleY(1.8); filter: blur(9px);   opacity: 0; }
    32%  { transform: translateY(170%) scaleY(1.8);  filter: blur(9px);   opacity: 0; }
    33%  { transform: translateY(0) scaleY(1.2);    filter: blur(0);    opacity: 1; }

    /* ▸ Rev 7 — 峰值 (4%, 80ms) */
    35%  { transform: translateY(-170%) scaleY(1.8); filter: blur(6px);   opacity: 0; }
    36%  { transform: translateY(170%) scaleY(1.8);  filter: blur(6px);   opacity: 0; }
    37%  { transform: translateY(0) scaleY(1.2);    filter: blur(0);    opacity: 1; }

    /* ▸ Rev 8 — 开始减速 (8%, 160ms) */
    41%  { transform: translateY(-170%) scaleY(1.5); filter: blur(3px); opacity: 0; }
    42%  { transform: translateY(170%) scaleY(1.5);  filter: blur(3px); opacity: 0; }
    45%  { transform: translateY(0) scaleY(1.1);    filter: blur(0);    opacity: 1; }

    /* ▸ Rev 9 — 明显减速 (12%, 240ms) */
    51%  { transform: translateY(-170%) scaleY(1.3); filter: blur(2px);   opacity: 0; }
    52%  { transform: translateY(170%) scaleY(1.3);  filter: blur(2px);   opacity: 0; }
    57%  { transform: translateY(0) scaleY(1);      filter: blur(0);    opacity: 1; }

    /* ▸ Rev 10 — 最后一圈，缓慢停住 (18%, 360ms) */
    65%  { transform: translateY(-170%) scaleY(1.1); filter: blur(1px);   opacity: 0; }
    66%  { transform: translateY(170%) scaleY(1.1);  filter: blur(1px);   opacity: 0; }
    75%  { transform: translateY(0) scaleY(1);      filter: blur(0);    opacity: 1; }

    /* ▸ Settle — 轻微回弹停住 */
    85%  { transform: translateY(-2%) scaleY(1);    filter: blur(0);    opacity: 1; }
    100% { transform: translateY(0) scaleY(1);      filter: blur(0);    opacity: 1; }
}

/* 停住后顺时针描线 2px 边框，再淡出消隐 */
.main-nav-icon.ai-nudge::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    padding: 2px;
    background: conic-gradient(from -90deg, var(--color-primary) var(--border-angle), transparent 0);
    -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    opacity: 0;
    pointer-events: none;
    --border-angle: 0deg;
    animation: aiBorderDraw 1.2s ease-out 3s forwards,
               aiBorderFade 0.6s ease-out 5s forwards;
}

@keyframes aiBorderDraw {
    from { --border-angle: 0deg; opacity: 1; }
    to   { --border-angle: 360deg; opacity: 1; }
}

@keyframes aiBorderFade {
    from { --border-angle: 360deg; opacity: 1; }
    to   { --border-angle: 360deg; opacity: 0; }
}


/* 提示气泡：fixed 定位，JS 计算坐标 */
.ai-nudge-bubble {
    position: fixed;
    z-index: 1000;
    background: var(--color-primary);
    color: var(--color-on-primary);
    padding: 5px 14px;
    border-radius: 14px;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
    animation: nudgeBubbleIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
    pointer-events: none;
}

/* 气泡左侧小三角 */
.ai-nudge-bubble::before {
    content: '';
    position: absolute;
    left: -4px;
    top: 50%;
    transform: translateY(-50%);
    border: 5px solid transparent;
    border-right-color: var(--color-primary);
    border-left: none;
}

@keyframes nudgeBubbleIn {
    from { opacity: 0; transform: scale(0.5) translateX(-8px); }
    to   { opacity: 1; transform: scale(1) translateX(0); }
}

.ai-nudge-bubble.fade-out {
    transition: opacity 1s ease-out, transform 1s ease-out;
    opacity: 0;
    transform: translateY(-6px);
}

/* ============================================
   大纲圆点导航（历史消息快速定位）
   ============================================ */
.ai-chat-dot-nav {
    position: absolute;
    right: 4px;
    z-index: 5;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0 3px;
    gap: 2px;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

.ai-chat-dot-nav.visible {
    opacity: 1;
    pointer-events: auto;
}

.ai-chat-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--color-text-quaternary);
    cursor: pointer;
    transition: all 0.2s;
    opacity: 0.5;
    flex-shrink: 0;
    /* 扩大点击热区 */
    padding: 5px;
    background-clip: content-box;
}

.ai-chat-dot:hover,
.ai-chat-dot.active {
    background: var(--color-primary);
    background-clip: content-box;
    transform: scale(1.6);
    opacity: 1;
}

/* 历史消息加载指示器 */
.ai-chat-history-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px;
    color: var(--color-text-quaternary);
    font-size: 12px;
    flex-shrink: 0;
}

/* ============================================
   响应式
   ============================================ */
@media (max-width: 500px) {
    .ai-chat-widget {
        width: calc(100vw - 10px) !important;
        left: 5px !important;
        bottom: 0 !important;
        min-width: auto;
    }
}
