/* ============================================================
   BAILU Agent Studio — chat.css
   调试聊天面板 / 消息 / Markdown / 工具卡片 / 输入区 / Trace
   ============================================================ */

.chat-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
    position: relative;
    background: var(--bg);
    overflow: hidden;
    /* 软键盘遮挡量：由 chat.js 通过 visualViewport 量出后写入，桌面端恒为 0 */
    padding-bottom: var(--kb-inset, 0px);
}

/* ------- 消息区 ------- */
.chat-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    /* 流式吸底由 JS 每帧写 scrollTop；若这里保留 smooth，
       每次写入都会重启一段滚动动画，移动端持续掉帧且会与用户手势打架。
       用户点「回到底部」时由 JS 显式传 behavior:'smooth'。 */
    scroll-behavior: auto;
    /* 滚到顶/底时不把滚动链传给外层（编辑器整页 / 移动端下拉刷新） */
    overscroll-behavior: contain;
}

/* 长会话：顶部「展开更早」入口 */
.chat-load-earlier {
    align-self: center;
    padding: 7px 16px;
    border-radius: 99px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-2);
    font-size: 12px;
    box-shadow: var(--shadow-1);
    transition: color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}

.chat-load-earlier:hover {
    color: var(--primary);
    border-color: var(--primary-soft-2);
}

.chat-msgs {
    max-width: 780px;
    margin: 0 auto;
    padding: 22px 20px 12px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.msg {
    display: flex;
    gap: 11px;
    min-width: 0;
    /* 长会话优化：视口外消息跳过渲染与布局 */
    content-visibility: auto;
    contain-intrinsic-size: auto 90px;
}

/* 入场动画只给最新一条：整屏重建（加载会话 / 重新生成）时
   几十上百条同时跑动画，在手机上是一次明显的掉帧。 */
.chat-msgs>.msg:last-child {
    animation: rise-in 0.3s var(--ease);
}

.msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    margin-top: 2px;
    color: #fff;
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.22), 0 2px 5px rgba(16, 24, 40, 0.1);
    user-select: none;
}

.msg-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.msg-name {
    font-size: 11.5px;
    font-weight: 650;
    color: var(--text-3);
    letter-spacing: 0.02em;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* 用户消息：右侧气泡 */
.msg.user {
    flex-direction: row-reverse;
}

.msg.user .msg-main {
    align-items: flex-end;
}

.msg-bubble {
    background: var(--chat-user-bg);
    color: var(--chat-user-text);
    padding: 10px 15px;
    border-radius: 16px 16px 4px 16px;
    font-size: 13.5px;
    line-height: 1.65;
    max-width: min(560px, 86%);
    white-space: pre-wrap;
    word-break: break-word;
    box-shadow: 0 2px 8px var(--primary-glow);
}

.msg-images {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.msg-images img {
    max-width: 180px;
    max-height: 180px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-1);
    cursor: zoom-in;
}

/* 助手消息 */
.msg.assistant .msg-main {
    gap: 10px;
}

.msg-blocks {
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-width: 0;
}

/* 文本块（markdown） */
.block-text {
    font-size: 13.8px;
    line-height: 1.75;
    color: var(--text);
    word-break: break-word;
    min-width: 0;
    /* 流式期间该子树每次都整体重建，把它的内部布局与相邻消息隔离。
       它是 .msg-blocks 的 flex item，本来就不参与外边距合并，加 contain 不改变排版。 */
    contain: layout style;
}

.block-text .stream-caret {
    display: inline-block;
    width: 8px;
    height: 16px;
    background: var(--primary);
    border-radius: 2px;
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: blink-caret 0.9s ease infinite;
}

/* ------- Markdown 内容 ------- */
.md> :first-child {
    margin-top: 0 !important;
}

.md> :last-child {
    margin-bottom: 0 !important;
}

.md p {
    margin: 8px 0;
}

.md h1,
.md h2,
.md h3,
.md h4 {
    margin: 18px 0 8px;
    font-weight: 700;
    letter-spacing: -0.01em;
    line-height: 1.4;
}

.md h1 {
    font-size: 19px;
}

.md h2 {
    font-size: 17px;
}

.md h3 {
    font-size: 15px;
}

.md h4 {
    font-size: 14px;
}

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

.md li {
    margin: 3px 0;
}

.md li>p {
    margin: 2px 0;
}

.md blockquote {
    margin: 10px 0;
    padding: 6px 14px;
    border-left: 3px solid var(--primary-soft-2);
    background: var(--primary-soft);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    color: var(--text-2);
}

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

.md a {
    color: var(--primary);
    font-weight: 500;
}

.md img {
    max-width: min(420px, 100%);
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-2);
    margin: 8px 0;
    cursor: zoom-in;
}

/* 图片加载体验：占位底色 + 完成淡入 */
.md img:not(.img-loaded),
.msg-images img:not(.img-loaded) {
    min-height: 88px;
    min-width: 120px;
    background: var(--surface-3);
    opacity: 0.4;
}

.md img.img-loaded,
.msg-images img.img-loaded {
    opacity: 1;
    animation: fade-in 0.35s ease;
}

.img-error {
    display: inline-flex;
    align-items: center;
    gap: 9px;
    padding: 10px 14px;
    margin: 8px 0;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    color: var(--text-3);
    font-size: 12.5px;
}

.img-error .icon {
    color: var(--text-3);
}

.md code {
    font-family: var(--font-mono);
    font-size: 12.5px;
    background: var(--surface-3);
    border: 1px solid var(--border);
    padding: 1px 6px;
    border-radius: 6px;
    word-break: break-all;
}

.md table {
    border-collapse: collapse;
    margin: 10px 0;
    font-size: 12.5px;
    width: 100%;
    display: block;
    overflow-x: auto;
    overscroll-behavior-x: contain;
}

.md th,
.md td {
    border: 1px solid var(--border);
    padding: 6px 12px;
    text-align: left;
}

.md th {
    background: var(--surface-3);
    font-weight: 650;
    white-space: nowrap;
}

.md tr:nth-child(even) td {
    background: var(--surface-2);
}

/* 代码块 */
.md-code {
    margin: 10px 0;
    border-radius: var(--radius-md);
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.06);
    background: var(--code-bg);
    box-shadow: var(--shadow-1);
}

.md-code-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 7px 13px;
    background: rgba(255, 255, 255, 0.04);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.md-code-lang {
    font-family: var(--font-mono);
    font-size: 11px;
    color: #7f8ea8;
    letter-spacing: 0.04em;
}

.md-copy {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 11.5px;
    color: #91a0ba;
    padding: 3px 8px;
    border-radius: 6px;
    transition: all var(--t-fast) var(--ease);
}

.md-copy:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #d6e0f0;
}

.md-copy .icon {
    width: 12px;
    height: 12px;
}

.md-code pre {
    margin: 0;
    padding: 13px 15px;
    overflow-x: auto;
    /* 代码块横向滚动到头时不要把手势传给消息区 / 整页 */
    overscroll-behavior-x: contain;
}

.md-code pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 12.5px;
    line-height: 1.65;
    color: var(--code-text);
    font-family: var(--font-mono);
    word-break: normal;
    white-space: pre;
}

/* 语法高亮 token */
.tok-kw {
    color: #c792ea;
}

.tok-str {
    color: #9ce09c;
}

.tok-num {
    color: #f2a86f;
}

.tok-com {
    color: #5f708c;
    font-style: italic;
}

.tok-fn {
    color: #7db8ff;
}

.tok-prop {
    color: #8fd6e4;
}

/* ------- 思考过程块 ------- */
.block-reasoning {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    overflow: hidden;
}

.block-reasoning summary {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 13px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-3);
    cursor: pointer;
    user-select: none;
    list-style: none;
    transition: background var(--t-fast) var(--ease);
}

.block-reasoning summary::-webkit-details-marker {
    display: none;
}

.block-reasoning summary:hover {
    background: var(--surface-3);
}

.block-reasoning summary .icon {
    width: 13px;
    height: 13px;
    color: var(--primary);
    opacity: 0.7;
}

.block-reasoning summary .chev {
    margin-left: auto;
    transition: transform var(--t) var(--ease);
    color: var(--text-3);
    opacity: 0.7;
}

.block-reasoning[open] summary .chev {
    transform: rotate(180deg);
}

.block-reasoning.thinking summary .icon {
    animation: pulse-soft 1.2s ease infinite;
}

.reasoning-body {
    padding: 4px 14px 12px;
    font-size: 12.5px;
    line-height: 1.75;
    color: var(--text-3);
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 300px;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-top: 1px dashed var(--border);
    margin: 0 0;
    padding-top: 10px;
}

/* ------- 工具调用卡片 ------- */
.block-tool {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    overflow: hidden;
    box-shadow: var(--shadow-1);
}

.tool-card-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 13px;
    cursor: pointer;
    user-select: none;
    transition: background var(--t-fast) var(--ease);
}

.tool-card-head:hover {
    background: var(--surface-2);
}

.tool-card-ico {
    width: 26px;
    height: 26px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: none;
    background: var(--primary-soft);
    color: var(--primary);
}

.tool-card-ico .icon {
    width: 13px;
    height: 13px;
}

.block-tool.running .tool-card-ico {
    background: var(--warn-soft);
    color: var(--warn);
}

.block-tool.error .tool-card-ico {
    background: var(--err-soft);
    color: var(--err);
}

.tool-card-name {
    font-family: var(--font-mono);
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text);
}

.tool-card-summary {
    flex: 1;
    min-width: 0;
    font-size: 11.5px;
    color: var(--text-3);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-family: var(--font-mono);
}

.tool-card-status {
    display: flex;
    align-items: center;
    gap: 7px;
    flex: none;
}

.tool-card-time {
    font-family: var(--font-mono);
    font-size: 10.5px;
    color: var(--text-3);
}

.tool-card-chev {
    color: var(--text-3);
    transition: transform var(--t) var(--ease);
}

.block-tool.open .tool-card-chev {
    transform: rotate(180deg);
}

.tool-card-body {
    display: none;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
    padding: 11px 13px;
    flex-direction: column;
    gap: 9px;
}

.block-tool.open .tool-card-body {
    display: flex;
}

.tool-io-label {
    display: flex;
    align-items: center;
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: 0.06em;
    color: var(--text-3);
    text-transform: uppercase;
}

.tool-io-copy {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: 6px;
    color: var(--text-3);
    transition: all var(--t-fast) var(--ease);
}

.tool-io-copy:hover {
    background: var(--surface-3);
    color: var(--text);
}

.tool-io-copy .icon {
    width: 12px;
    height: 12px;
}

.tool-io-pre {
    font-family: var(--font-mono);
    font-size: 11.5px;
    line-height: 1.6;
    background: var(--code-bg);
    color: var(--code-text);
    border-radius: var(--radius-sm);
    padding: 9px 11px;
    overflow: auto;
    overscroll-behavior: contain;
    max-height: 240px;
    white-space: pre-wrap;
    word-break: break-word;
    margin: 0;
}

/* ------- 消息操作条 ------- */
.msg-actions {
    display: flex;
    gap: 2px;
    transition: opacity var(--t-fast) var(--ease);
    margin-top: -3px;
}

/* 悬停浮现只在有真实指针的设备上生效；触摸端常驻可见，否则复制/重新生成不可达 */
@media (hover: hover) {
    .msg-actions {
        opacity: 0;
    }

    .msg:hover .msg-actions,
    .msg-actions:focus-within {
        opacity: 1;
    }
}

.msg-actions .icon-btn {
    width: 26px;
    height: 26px;
}

.msg-actions .icon-btn .icon {
    width: 13px;
    height: 13px;
}

/* 重新生成仅在最后一条助手消息上可用 */
.msg [data-act-regen] {
    display: none;
}

.msg.assistant:last-child [data-act-regen] {
    display: inline-flex;
}

/* ------- 迭代分隔 ------- */
.block-iter {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--text-3);
    text-transform: uppercase;
    margin: 2px 0;
}

.block-iter::before,
.block-iter::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* ------- 错误块 ------- */
.block-error {
    display: flex;
    gap: 9px;
    padding: 11px 14px;
    border-radius: var(--radius-md);
    background: var(--err-soft);
    border: 1px solid transparent;
    color: var(--err);
    font-size: 12.5px;
    line-height: 1.6;
    word-break: break-word;
}

.block-error .icon {
    flex: none;
    margin-top: 2px;
}

/* ------- 开场 / 建议 ------- */
.chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 44px 24px 20px;
    gap: 14px;
    animation: rise-in 0.45s var(--ease);
}

.chat-welcome .avatar {
    width: 64px;
    height: 64px;
    border-radius: 20px;
    font-size: 34px;
}

.chat-welcome-name {
    font-size: 17px;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.chat-welcome-desc {
    font-size: 13px;
    color: var(--text-2);
    max-width: 440px;
    line-height: 1.7;
    white-space: pre-wrap;
}

.chat-suggestions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 6px;
    width: 100%;
    max-width: 440px;
}

.chat-suggestions .chip {
    justify-content: space-between;
    width: 100%;
    padding: 9px 15px;
    border-radius: var(--radius-md);
}

.chat-suggestions .chip .icon {
    width: 13px;
    height: 13px;
    opacity: 0.5;
}

/* ------- 回到底部按钮 ------- */
.scroll-bottom {
    position: absolute;
    right: 20px;
    /* 输入区高度由 chat.js 的 ResizeObserver 写入 --composer-h（旧实现每次 scroll
       都读 offsetHeight 再写 style.bottom，滚动中反复触发强制同步布局）；
       --kb-inset 让按钮跟着软键盘一起上移。 */
    bottom: calc(var(--composer-h, 116px) + var(--kb-inset, 0px) + 14px);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-2);
    box-shadow: var(--shadow-pop);
    z-index: 20;
    opacity: 0;
    transform: translateY(8px);
    pointer-events: none;
    transition: opacity var(--t) var(--ease), transform var(--t) var(--ease),
        background var(--t-fast) var(--ease);
}

.scroll-bottom.show {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.scroll-bottom:hover {
    background: var(--surface-2);
    color: var(--primary);
    border-color: var(--primary-soft-2);
}

.scroll-bottom .icon {
    width: 16px;
    height: 16px;
}

/* ------- 输入区 ------- */
.chat-input-zone {
    flex: none;
    padding: 10px 20px 16px;
    max-width: 780px;
    margin: 0 auto;
    width: 100%;
}

.chat-attachments {
    display: flex;
    gap: 8px;
    padding-bottom: 8px;
    flex-wrap: wrap;
}

.chat-attach-thumb {
    position: relative;
    width: 52px;
    height: 52px;
    border-radius: var(--radius-sm);
    overflow: hidden;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-1);
}

.chat-attach-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chat-attach-thumb .rm {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-attach-thumb .rm .icon {
    width: 9px;
    height: 9px;
}

/* 音频 / 视频附件芯片（发送前预览区） */
.chat-attach-file {
    position: relative;
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 210px;
    height: 52px;
    padding: 0 26px 0 10px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface);
    box-shadow: var(--shadow-1);
    color: var(--text-2);
    font-size: 12px;
}

.chat-attach-file .icon {
    flex: none;
    color: var(--primary);
}

.chat-attach-file .nm {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-attach-file .rm {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-attach-file .rm .icon {
    width: 9px;
    height: 9px;
    color: #fff;
}

/* 消息气泡内的音/视频回放 */
.msg-audio {
    width: 260px;
    max-width: 100%;
    height: 40px;
    margin-bottom: 6px;
}

.msg-video {
    display: block;
    max-width: 320px;
    max-height: 240px;
    border-radius: var(--radius-sm);
    margin-bottom: 6px;
    background: #000;
}

/* 音/视频的 base64 不进 localStorage，刷新后历史里只剩这条占位芯片 */
.msg-attach-gone {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    max-width: 240px;
    padding: 7px 11px;
    margin-bottom: 6px;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-md);
    background: var(--surface-2);
    color: var(--text-3);
    font-size: 12px;
}

.msg-attach-gone .icon {
    flex: none;
    width: 13px;
    height: 13px;
}

.msg-attach-gone .nm {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.msg-attach-gone .tag {
    flex: none;
    opacity: 0.75;
}

.chat-input-box {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 9px 10px 9px 15px;
    box-shadow: var(--shadow-2);
    transition: border-color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}

.chat-input-box:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft), var(--shadow-2);
}

.chat-input-box.dragging {
    border-color: var(--primary);
    border-style: dashed;
    background: var(--primary-soft);
    box-shadow: 0 0 0 3px var(--primary-soft), var(--shadow-2);
}

.chat-textarea {
    flex: 1;
    border: none;
    background: none;
    resize: none;
    outline: none;
    font-size: 13.5px;
    line-height: 1.6;
    max-height: 160px;
    padding: 5px 0;
    color: var(--text);
}

.chat-textarea::placeholder {
    color: var(--text-3);
}

.chat-input-actions {
    display: flex;
    align-items: center;
    gap: 5px;
    flex: none;
}

.chat-attach-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    color: var(--text-2);
    background: var(--surface-3);
}

.chat-attach-btn:hover {
    color: var(--primary);
    background: var(--primary-soft);
}

.chat-attach-btn .icon {
    width: 18px;
    height: 18px;
    stroke-width: 2.25;
}

.chat-send {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--t-fast) var(--ease);
    box-shadow: 0 2px 8px var(--primary-glow);
}

.chat-send:hover {
    background: var(--primary-strong);
}

.chat-send:disabled {
    opacity: 0.4;
    pointer-events: none;
    box-shadow: none;
}

.chat-send.stop {
    background: var(--err);
    box-shadow: 0 2px 8px rgba(217, 45, 32, 0.3);
}

.chat-send .icon {
    width: 15px;
    height: 15px;
}

.chat-input-hint {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 6px 0;
    font-size: 11px;
    color: var(--text-3);
}

.chat-input-hint .right {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* ------- Trace 抽屉 ------- */
.trace-drawer {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(400px, 92%);
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: var(--shadow-3);
    z-index: 40;
    display: flex;
    flex-direction: column;
    transform: translateX(105%);
    transition: transform var(--t-slow) var(--ease);
}

.trace-drawer.open {
    transform: translateX(0);
}

.trace-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 13px 16px;
    border-bottom: 1px solid var(--border);
    flex: none;
}

.trace-title {
    font-size: 13.5px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

.trace-title .icon {
    width: 15px;
    height: 15px;
    color: var(--primary);
}

.trace-body {
    flex: 1;
    overflow-y: auto;
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.trace-summary {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}

.trace-stat {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 9px 11px;
}

.trace-stat .k {
    font-size: 10.5px;
    color: var(--text-3);
    font-weight: 600;
    letter-spacing: 0.03em;
}

.trace-stat .v {
    font-family: var(--font-mono);
    font-size: 14px;
    font-weight: 650;
    color: var(--text);
    margin-top: 2px;
}

.trace-steps {
    display: flex;
    flex-direction: column;
    position: relative;
    padding-left: 18px;
}

.trace-steps::before {
    content: "";
    position: absolute;
    left: 5px;
    top: 10px;
    bottom: 10px;
    width: 2px;
    background: var(--border);
    border-radius: 2px;
}

.trace-step {
    position: relative;
    padding: 7px 0 7px 8px;
}

.trace-step::before {
    content: "";
    position: absolute;
    left: -17.5px;
    top: 14px;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--surface);
    border: 2px solid var(--primary);
}

.trace-step.tool::before {
    border-color: var(--warn);
}

.trace-step.err::before {
    border-color: var(--err);
}

.trace-step-head {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12.5px;
    font-weight: 600;
}

.trace-step-head .t {
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: 10.5px;
    color: var(--text-3);
    font-weight: 400;
}

.trace-step-sub {
    font-size: 11.5px;
    color: var(--text-3);
    font-family: var(--font-mono);
    margin-top: 1px;
    word-break: break-all;
}

/* ------- 图片灯箱 ------- */
.lightbox {
    position: fixed;
    inset: 0;
    background: rgba(5, 8, 14, 0.88);
    z-index: 400;
    animation: fade-in var(--t) var(--ease);
    overscroll-behavior: contain;
    user-select: none;
    -webkit-user-select: none;
}

.lightbox.closing {
    animation: fade-in var(--t) var(--ease) reverse forwards;
}

.lb-stage {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    touch-action: none;
    cursor: zoom-out;
}

.lb-img {
    max-width: calc(100vw - 48px);
    max-height: calc(100vh - 120px);
    border-radius: var(--radius-sm);
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.55);
    opacity: 0;
    transition: opacity 0.25s ease;
    will-change: transform;
}

.lb-img.lb-ready {
    opacity: 1;
    animation: scale-in 0.22s var(--ease);
}

.lb-img.pannable {
    cursor: grab;
}

.lb-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.lb-loading .spinner {
    border-color: rgba(255, 255, 255, 0.2);
    border-top-color: #fff;
}

.lb-error {
    display: flex;
    align-items: center;
    gap: 9px;
    color: #ffb1a8;
    font-size: 13.5px;
    background: rgba(0, 0, 0, 0.45);
    padding: 12px 20px;
    border-radius: 99px;
}

/* 顶栏 */
.lb-topbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 18px;
    z-index: 2;
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.45), transparent);
    pointer-events: none;
}

.lb-counter {
    color: rgba(255, 255, 255, 0.85);
    font-size: 13px;
    font-family: var(--font-mono);
    background: rgba(0, 0, 0, 0.4);
    padding: 5px 12px;
    border-radius: 99px;
    pointer-events: auto;
}

.lb-tools {
    display: flex;
    align-items: center;
    gap: 4px;
    background: rgba(16, 20, 28, 0.72);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 4px;
    pointer-events: auto;
    margin-left: auto;
}

.lb-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.82);
    transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}

.lb-btn:hover {
    background: rgba(255, 255, 255, 0.14);
    color: #fff;
}

.lb-btn .icon {
    width: 15px;
    height: 15px;
}

.lb-zoom-label {
    min-width: 52px;
    text-align: center;
    color: rgba(255, 255, 255, 0.9);
    font-size: 12px;
    font-family: var(--font-mono);
    border-radius: 8px;
    height: 32px;
    padding: 0 4px;
}

.lb-zoom-label:hover {
    background: rgba(255, 255, 255, 0.14);
}

.lb-sep {
    width: 1px;
    height: 18px;
    background: rgba(255, 255, 255, 0.14);
    margin: 0 3px;
}

/* 左右切换 */
.lb-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(16, 20, 28, 0.6);
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: rgba(255, 255, 255, 0.85);
    z-index: 2;
    transition: all var(--t-fast) var(--ease);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.lb-nav:hover {
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
}

.lb-nav .icon {
    width: 20px;
    height: 20px;
}

.lb-prev {
    left: 18px;
}

.lb-next {
    right: 18px;
}

@media (max-width: 720px) {
    .lb-img {
        max-width: calc(100vw - 16px);
        max-height: calc(100vh - 130px);
    }

    .lb-prev {
        left: 8px;
    }

    .lb-next {
        right: 8px;
    }

    .lb-topbar {
        padding: 10px;
    }
}

/* iOS 安全区（无 env 支持的浏览器忽略该行，保留上方基础 padding）。
   键盘弹出时 --kb-inset 已经把输入区抬到键盘上方，此时 home indicator
   在键盘下面，安全区补白应当让位，否则输入区上方会多出一条空白。 */
.chat-input-zone {
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
}

.chat-panel.kb-open .chat-input-zone {
    padding-bottom: 16px;
}

/* ============================================================
   移动端适配
   ============================================================ */

@media (max-width: 980px) {

    /* 编辑器里调试面板被压到单列，抽屉铺满更好用 */
    .trace-drawer {
        width: min(400px, 100%);
    }
}

@media (max-width: 720px) {
    .chat-msgs {
        padding: 16px 14px 10px;
        gap: 16px;
    }

    .chat-input-zone {
        padding: 8px 12px 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }

    .chat-panel.kb-open .chat-input-zone {
        padding-bottom: 10px;
    }

    /* iOS：输入控件字号 <16px 会触发聚焦自动缩放 */
    .input,
    .textarea,
    .chat-textarea,
    .prompt-textarea,
    .select-search input {
        font-size: 16px;
    }

    /* 键盘占掉大半屏时，输入框自身不该再吃掉剩下的可视区 */
    .chat-textarea {
        max-height: 108px;
    }

    /* 软键盘上 Enter 是换行键，发送靠按钮（见 chat.js），提示语无意义 */
    .chat-kbd-hint {
        display: none;
    }

    .chat-input-hint {
        justify-content: flex-end;
        padding-top: 5px;
    }

    .msg-bubble {
        max-width: 88%;
        font-size: 14px;
    }

    .block-text {
        font-size: 14px;
    }

    .chat-welcome {
        padding: 28px 18px 16px;
    }

    /* 工具卡片头在窄屏放不下「图标+名称+摘要+状态+耗时+箭头」一行 */
    .tool-card-head {
        flex-wrap: wrap;
        row-gap: 4px;
        padding: 9px 11px;
    }

    .tool-card-summary {
        order: 3;
        flex-basis: 100%;
        white-space: normal;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .tool-card-name {
        flex: 1;
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .tool-io-pre,
    .reasoning-body {
        max-height: 200px;
    }

    /* 抽屉铺满并避让安全区 */
    .trace-drawer {
        width: 100%;
    }

    .trace-body {
        padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    }

    .trace-summary {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .scroll-bottom {
        right: 12px;
    }
}

@media (max-width: 480px) {
    .chat-msgs {
        padding: 14px 10px 8px;
    }

    .msg {
        gap: 8px;
    }

    .msg-bubble {
        max-width: 92%;
    }

    .msg-images img {
        max-width: 132px;
        max-height: 132px;
    }

    .trace-summary {
        grid-template-columns: 1fr 1fr;
    }
}

/* ------- 触摸设备：可点区域 ≥ 44px ------- */
@media (hover: none) {

    /* 视觉尺寸不变，用透明伪元素把命中区撑到 44px，避免影响桌面密度。
       （.chat-attach-thumb .rm 不在此列：缩略图 overflow:hidden 会把伪元素
       连同其命中区一起裁掉，改为直接放大按钮本身。） */
    .msg-actions .icon-btn,
    .chat-input-actions .icon-btn,
    .trace-head .icon-btn,
    .tool-io-copy,
    .md-copy {
        position: relative;
    }

    .msg-actions .icon-btn::after,
    .chat-input-actions .icon-btn::after,
    .trace-head .icon-btn::after,
    .tool-io-copy::after,
    .md-copy::after {
        content: "";
        position: absolute;
        top: 50%;
        left: 50%;
        width: 44px;
        height: 44px;
        transform: translate(-50%, -50%);
    }

    .chat-attach-thumb .rm,
    .chat-attach-file .rm {
        width: 24px;
        height: 24px;
    }

    .chat-attach-thumb .rm .icon,
    .chat-attach-file .rm .icon {
        width: 11px;
        height: 11px;
    }

    .chat-send,
    .chat-attach-btn {
        width: 44px;
        height: 44px;
    }

    .scroll-bottom {
        width: 44px;
        height: 44px;
    }

    /* 可折叠区块的点击靶子 */
    .tool-card-head {
        min-height: 44px;
    }

    .block-reasoning summary {
        min-height: 44px;
        padding: 11px 13px;
    }

    .chat-suggestions .chip {
        min-height: 44px;
    }

    .chat-load-earlier {
        min-height: 40px;
    }
}

/* base.css 的全局降级只压 animation-duration（!important），对 blink-caret 这类
   infinite 动画来说仍在持续排帧；这里直接把 animation-name 置空彻底停掉。 */
@media (prefers-reduced-motion: reduce) {

    .chat-msgs>.msg:last-child,
    .chat-welcome {
        animation: none;
    }

    .block-text .stream-caret {
        animation: none;
    }

    .trace-drawer {
        transition: none;
    }
}

/* ============================================================
   CAD 图纸预览卡片（ui/cad-view.js）
   图纸区固定纸面白底（明暗主题一致），卡片外壳跟随主题
   ============================================================ */

.cad-embed {
    margin: 10px 0;
}

.cad-card {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    overflow: hidden;
    box-shadow: var(--shadow-1);
}

.block-tool .cad-card {
    border: none;
    border-top: 1px solid var(--border);
    border-radius: 0;
    box-shadow: none;
}

.cad-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
    min-width: 0;
}

.cad-head-ico {
    width: 15px;
    height: 15px;
    color: var(--primary);
    flex: none;
}

.cad-head-title {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.cad-head-size {
    font-size: 11px;
    color: var(--text-3);
    white-space: nowrap;
    flex: none;
}

.cad-toolbar {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-left: auto;
    flex: none;
}

.cad-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 24px;
    padding: 0 7px;
    border: 1px solid var(--border);
    border-radius: var(--radius-xs);
    background: var(--surface);
    color: var(--text-2);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    transition: color var(--t-fast) var(--ease), border-color var(--t-fast) var(--ease);
}

.cad-btn .icon {
    width: 12.5px;
    height: 12.5px;
}

.cad-btn:hover,
.cad-btn.active {
    color: var(--primary);
    border-color: var(--primary-soft-2);
}

.cad-viewport {
    position: relative;
    background: #fff;
    touch-action: pan-y;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
}

.cad-viewport.cad-grabbing {
    cursor: grabbing;
}

.cad-svg {
    display: block;
    width: 100%;
    height: auto;
    max-height: min(66vh, 560px);
}

/* 角落缩放控件 */
.cad-zoom {
    position: absolute;
    right: 8px;
    bottom: 8px;
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 3px;
    border-radius: var(--radius-sm);
    background: rgba(22, 24, 29, 0.78);
    opacity: 0;
    transition: opacity var(--t-fast) var(--ease);
    pointer-events: none;
}

.cad-viewport:hover .cad-zoom,
.cad-viewport:fullscreen .cad-zoom,
.cad-viewport.cad-fs-fallback .cad-zoom {
    opacity: 1;
    pointer-events: auto;
}

.cad-zoom-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: #e8eaf0;
    cursor: pointer;
}

.cad-zoom-btn:hover {
    background: rgba(255, 255, 255, 0.14);
}

.cad-zoom-btn .icon {
    width: 13px;
    height: 13px;
}

.cad-zoom-label {
    min-width: 40px;
    text-align: center;
    font-size: 11px;
    color: #e8eaf0;
    font-variant-numeric: tabular-nums;
}

/* 全屏（原生 + CSS 伪全屏回退） */
.cad-viewport:fullscreen {
    background: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cad-viewport:fullscreen .cad-svg {
    max-height: 100vh;
    width: 100%;
    height: 100%;
}

.cad-viewport.cad-fs-fallback {
    position: fixed;
    inset: 0;
    z-index: 1200;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cad-viewport.cad-fs-fallback .cad-svg {
    max-height: 100vh;
    width: 100%;
    height: 100%;
}

body.cad-fs-lock {
    overflow: hidden;
}

.cad-zoom .cad-zoom-btn[data-cad-fs-exit] {
    display: none;
}

.cad-viewport:fullscreen [data-cad-fs-exit],
.cad-viewport.cad-fs-fallback [data-cad-fs-exit] {
    display: inline-flex;
}

/* JSON 面板 */
.cad-json-panel {
    border-top: 1px solid var(--border);
    background: var(--surface-2);
}

.cad-json-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 12px;
    font-size: 11.5px;
    color: var(--text-3);
}

.cad-json-pre {
    margin: 0;
    padding: 8px 12px 12px;
    max-height: 240px;
    overflow: auto;
    font-family: var(--font-mono, ui-monospace, monospace);
    font-size: 11.5px;
    line-height: 1.55;
    color: var(--text-2);
    white-space: pre-wrap;
    word-break: break-all;
}

/* 流式绘制中 / 解析失败 */
.cad-loading {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius-md);
    background: var(--primary-soft);
    color: var(--text-2);
    font-size: 12.5px;
}

.cad-error {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md) var(--radius-md) 0 0;
    color: var(--danger, #e5484d);
    font-size: 12.5px;
}

.cad-error .icon {
    width: 14px;
    height: 14px;
    flex: none;
}

@media (max-width: 640px) {
    .cad-head {
        flex-wrap: wrap;
        row-gap: 6px;
    }

    .cad-toolbar {
        margin-left: 0;
        flex-basis: 100%;
    }

    .cad-btn {
        height: 30px;
    }

    .cad-svg {
        max-height: 54vh;
    }
}
