/* ==========================================================================
   墨云阁 · AI 助手增强层 (ink-ai.css)
   --------------------------------------------------------------------------
   本文件在 ai.css 与 ink-pages.css **之后**加载，只负责三件事：
     1. 图片输入（附件条、缩略图、拖拽态、灯箱）
     2. 回复里的图片渲染与下载
     3. 导出菜单 + 打印（存 PDF）版式
   全部使用 ink.css 的设计令牌；类名都是新命名的（ai-attach-* / ai-msg-* /
   ai-menu / ai-img-* / ai-print-*），ai.css 与 ink-pages.css 里都没有同名规则，
   因此基本不需要 !important。
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. 附件条（输入框上方）
   -------------------------------------------------------------------------- */
.ai-attach-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem 0;
    border-top: 1px solid var(--border-color);
    background: var(--card-bg);
}

.ai-attach-strip[hidden] { display: none; }

.ai-attach-item {
    position: relative;
    width: 56px;
    height: 56px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--paper-2);
    flex: 0 0 auto;
}

.ai-attach-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.ai-attach-del {
    position: absolute;
    top: 0;
    right: 0;
    width: 18px;
    height: 18px;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: inherit;
    font-size: 12px;
    line-height: 1;
    color: #fff;
    background: rgba(32, 33, 36, 0.72);
    border: none;
    border-radius: 0 0 0 var(--radius-sm);
    cursor: pointer;
}

.ai-attach-del:hover { background: var(--ink-accent); }

/* 图片处理中的占位环 */
.ai-attach-loading {
    display: block;
    width: 18px;
    height: 18px;
    margin: 19px auto;
    border: 2px solid var(--border-dark);
    border-top-color: var(--ink-accent);
    border-radius: 50%;
    animation: aiAttachSpin 0.7s linear infinite;
}

@keyframes aiAttachSpin { to { transform: rotate(360deg); } }

/* 附件按钮（输入区最左侧） */
.ai-attach-btn {
    width: 38px;
    height: 38px;
    padding: 0;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    background: none;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    transition: color 0.18s ease, background-color 0.18s ease, border-color 0.18s ease;
}

.ai-attach-btn svg { width: 17px; height: 17px; flex-shrink: 0; }

.ai-attach-btn:hover {
    color: var(--ink-accent);
    background: var(--ink-accent-soft);
    border-color: var(--ink-accent-line);
}

/* 拖拽到输入区时的高亮 */
.ai-chat-wrapper.is-dragover { border-color: var(--ink-accent-line); }

.ai-chat-wrapper.is-dragover::after {
    content: '松开即可添加图片';
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9375rem;
    font-weight: 550;
    color: var(--ink-accent);
    background: rgba(255, 255, 255, 0.88);
    pointer-events: none;
}

/* --------------------------------------------------------------------------
   2. 消息里的图片
   -------------------------------------------------------------------------- */
.ai-message.has-images { max-width: 92%; }

.ai-msg-images {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-top: 0.4rem;
}

.ai-msg-thumb {
    width: 84px;
    height: 84px;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--paper-2);
    cursor: zoom-in;
    flex: 0 0 auto;
}

.ai-msg-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.ai-msg-note {
    margin-top: 0.35rem;
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* 回复正文里的图片（markdown ![alt](url)） */
.ai-img-wrap {
    display: block;
    margin: 0.5rem 0;
}

.ai-img-wrap img {
    display: block;
    max-width: 100%;
    height: auto;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    cursor: zoom-in;
    background: var(--paper-2);
}

.ai-img-dl {
    margin-top: 0.35rem;
    padding: 0.2rem 0.6rem;
    font-family: inherit;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-secondary);
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
}

.ai-img-dl:hover {
    color: var(--ink-accent);
    background: var(--ink-accent-soft);
    border-color: var(--ink-accent-line);
}

/* --------------------------------------------------------------------------
   2b. 让对话卡片成为「固定高度的滚动区」
   --------------------------------------------------------------------------
   .ai-chat-wrapper 原本只有 flex: 1，高度由内容决定 —— 实测一条长回复就能
   把它撑到 2238px 且 overflow 为 0，也就是根本没有内部滚动条，全靠整页滚动。
   后果是：读长回复时输入框被顶到屏幕外，整页滑块长得离谱。
   这里给它一个受控高度，让 .ai-messages 真正内部滚动（输入区因此始终可见）。
   -------------------------------------------------------------------------- */
.ai-chat-wrapper {
    flex: 0 0 auto;
    height: clamp(420px, 72vh, 820px);
}

/* --------------------------------------------------------------------------
   3. 消息操作条（展开/收起 · 复制 · 导出）
   --------------------------------------------------------------------------
   放在气泡**顶部**：AI 写教案常有几千像素高，操作条在底部就要滚到最后
   才够得着。常驻显示（不做 hover 才出现），保证一眼能找到。
   -------------------------------------------------------------------------- */
.ai-msg-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.3rem;
    margin: 0 0 0.65rem;
    padding-bottom: 0.55rem;
    border-bottom: 1px solid var(--border-color);
}

.ai-act-btn {
    padding: 0.18rem 0.55rem;
    font-family: inherit;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: color 0.18s ease, background-color 0.18s ease, border-color 0.18s ease;
}

.ai-act-btn:hover {
    color: var(--ink-accent);
    background: var(--ink-accent-soft);
    border-color: var(--ink-accent-line);
}

.ai-act-btn.is-done {
    color: var(--ink-accent);
    border-color: var(--ink-accent-line);
}

/* 「展开全文」比另两个重一档，因为它是长回复里的主要动作 */
.ai-collapse-btn {
    color: var(--text-secondary);
    border-color: var(--border-dark);
}

.ai-collapse-btn[hidden] { display: none; }

/* --------------------------------------------------------------------------
   3b. 长回复折叠
   -------------------------------------------------------------------------- */
.ai-msg-body.is-collapsed {
    position: relative;
    max-height: min(560px, 55vh);
    overflow: hidden;
}

/* 底部渐隐：颜色要跟气泡底色一致（浅色 paper-2 / 深色 paper-3） */
.ai-msg-body.is-collapsed::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 88px;
    background: linear-gradient(to bottom, rgba(245, 245, 245, 0), var(--paper-2));
    pointer-events: none;
}

[data-theme="dark"] .ai-msg-body.is-collapsed::after {
    background: linear-gradient(to bottom, rgba(46, 47, 50, 0), var(--paper-3));
}

/* --------------------------------------------------------------------------
   3c. 「回到最新」浮标（长回复折叠后容易停在半空）
   -------------------------------------------------------------------------- */
.ai-input-area { position: relative; }

.ai-jump-latest {
    position: absolute;
    top: -44px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 4;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.3rem 0.8rem;
    font-family: inherit;
    font-size: 0.8125rem;
    font-weight: 550;
    color: var(--ink-accent);
    background: var(--card-bg);
    border: 1px solid var(--ink-accent-line);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    cursor: pointer;
    white-space: nowrap;
}

.ai-jump-latest[hidden] { display: none; }

.ai-jump-latest:hover {
    background: var(--ink-accent-soft);
    border-color: var(--ink-accent);
}

.ai-jump-latest svg { width: 13px; height: 13px; flex-shrink: 0; }

/* --------------------------------------------------------------------------
   4. 导出菜单（会话级工具栏 + 单条回复共用同一个浮层）
   -------------------------------------------------------------------------- */
.ai-menu {
    position: fixed;
    z-index: 60;
    min-width: 184px;
    padding: 0.3rem;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
}

.ai-menu[hidden] { display: none; }

.ai-menu button {
    display: block;
    width: 100%;
    padding: 0.45rem 0.6rem;
    font-family: inherit;
    font-size: 0.8125rem;
    font-weight: 500;
    text-align: left;
    color: var(--text-secondary);
    background: none;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    white-space: nowrap;
}

.ai-menu button:hover {
    color: var(--ink-accent);
    background: var(--ink-accent-soft);
}

.ai-menu-sep {
    height: 1px;
    margin: 0.25rem 0.3rem;
    background: var(--border-color);
}

/* 工具栏上的「导出对话」按钮 */
.ai-export-all {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.25rem 0.7rem;
    font-family: inherit;
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--text-secondary);
    background: none;
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    cursor: pointer;
}

.ai-export-all:hover {
    color: var(--ink-accent);
    background: var(--ink-accent-soft);
    border-color: var(--ink-accent-line);
}

.ai-export-all svg { width: 13px; height: 13px; flex-shrink: 0; }

/* --------------------------------------------------------------------------
   5. 灯箱（点击图片看大图）
   -------------------------------------------------------------------------- */
.ai-lightbox {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background: rgba(20, 20, 22, 0.86);
    cursor: zoom-out;
}

.ai-lightbox[hidden] { display: none; }

.ai-lightbox img {
    max-width: 100%;
    max-height: 100%;
    border-radius: var(--radius);
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.4);
}

.ai-lightbox-hint {
    position: absolute;
    bottom: 1.25rem;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 0.8125rem;
    color: rgba(255, 255, 255, 0.7);
}

/* --------------------------------------------------------------------------
   6. 打印 / 存 PDF
   -------------------------------------------------------------------------- */
.ai-print-root { display: none; }

@media print {
    /* 只留打印根节点 */
    body.ai-printing > *:not(.ai-print-root) { display: none !important; }
    body.ai-printing .ai-print-root {
        display: block !important;
        color: #000;
        background: #fff;
    }

    .ai-print-root { font-size: 12pt; line-height: 1.75; }
    .ai-print-root .ai-print-title {
        font-size: 17pt;
        font-weight: 700;
        margin: 0 0 0.2em;
    }
    .ai-print-root .ai-print-meta {
        font-size: 9pt;
        color: #666;
        margin: 0 0 1.2em;
        padding-bottom: 0.6em;
        border-bottom: 1px solid #ddd;
    }
    .ai-print-root h1 { font-size: 15pt; margin: 1em 0 0.4em; }
    .ai-print-root h2 { font-size: 13.5pt; margin: 1em 0 0.4em; }
    .ai-print-root h3 { font-size: 12pt; margin: 0.9em 0 0.35em; }
    .ai-print-root p { margin: 0 0 0.6em; }
    .ai-print-root ul, .ai-print-root ol { margin: 0 0 0.6em; padding-left: 1.4em; }
    .ai-print-root li { margin: 0.15em 0; }
    .ai-print-root pre {
        padding: 0.6em 0.8em;
        font-size: 10pt;
        white-space: pre-wrap;
        word-break: break-word;
        background: #f5f5f5;
        border: 1px solid #e0e0e0;
        border-radius: 4px;
    }
    .ai-print-root code { font-size: 10pt; }
    .ai-print-root blockquote {
        margin: 0.6em 0;
        padding-left: 0.8em;
        border-left: 3px solid #ccc;
        color: #444;
    }
    .ai-print-root img { max-width: 100%; height: auto; }
    .ai-print-root hr { border: none; border-top: 1px solid #ddd; margin: 1em 0; }
    .ai-print-root .ai-msg-actions,
    .ai-print-root .ai-img-dl { display: none !important; }

    /* 打印时不要页面底色与动画 */
    body { background: #fff !important; }
    @page { margin: 18mm 16mm; }
}

/* --------------------------------------------------------------------------
   7. 窄屏
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
    .ai-attach-strip { padding: 0.6rem 0.85rem 0; }
    .ai-attach-item { width: 48px; height: 48px; }
    .ai-attach-btn { width: 34px; height: 34px; }
    .ai-msg-thumb { width: 68px; height: 68px; }
    .ai-menu { min-width: 160px; }
}
