@charset "UTF-8";

/* Mermaid 流程图样式 */

.kb-mermaid {
    position: relative;
    display: block;
    margin: 1.25rem 0;
    padding: 1rem;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    background: #ffffff;
    overflow: hidden;
    text-align: center;
}

.kb-mermaid-viewport {
    width: 100%;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
}

.kb-mermaid svg {
    max-width: 100%;
    height: auto;
    shape-rendering: geometricPrecision;
    text-rendering: geometricPrecision;
}

/* 与代码块复制按钮一致的右上角图表操作区 */
.kb-mermaid-actions {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    z-index: 15;
    display: flex;
    gap: 0.375rem;
    pointer-events: auto;
}

.kb-mermaid-fullscreen-btn,
.kb-mermaid-copy-btn,
.kb-mermaid-zoom-btn,
.kb-mermaid-zoom-level,
.kb-mermaid-fullscreen-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    min-height: 32px !important;
    padding: 0;
    border: 1px solid color-mix(in srgb, var(--kb-paper-line, #d1d5db) 78%, transparent);
    border-radius: 6px;
    background: color-mix(in srgb, var(--kb-paper-panel, #ffffff) 88%, transparent);
    color: var(--kb-ink-soft, #4b5563);
    box-shadow: 0 4px 12px rgba(31, 41, 55, 0.10);
    cursor: pointer;
    transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.kb-mermaid-fullscreen-btn:hover,
.kb-mermaid-fullscreen-btn:focus-visible,
.kb-mermaid-copy-btn:hover,
.kb-mermaid-copy-btn:focus-visible,
.kb-mermaid-zoom-btn:hover:not(:disabled),
.kb-mermaid-zoom-btn:focus-visible,
.kb-mermaid-zoom-level:hover:not(:disabled),
.kb-mermaid-zoom-level:focus-visible,
.kb-mermaid-fullscreen-close:hover,
.kb-mermaid-fullscreen-close:focus-visible {
    border-color: var(--kb-accent, #6366f1);
    background: var(--kb-active-bg, #eef2ff);
    color: var(--kb-accent-strong, #4f46e5);
    outline: none;
    transform: translateY(-1px);
}

.kb-mermaid-fullscreen-btn i,
.kb-mermaid-copy-btn i,
.kb-mermaid-zoom-btn i,
.kb-mermaid-fullscreen-close i {
    font-size: 18px;
    line-height: 1;
}

/* 复制成功的两秒反馈：与代码块复制键的 .copied 同色 */
.kb-mermaid-copy-btn.copied,
.kb-mermaid-copy-btn.copied:hover {
    border-color: var(--color-success, #22c55e);
    background: var(--color-success-light, #dcfce7);
    color: var(--color-success-dark, #15803d);
}

.kb-paper-app .kb-mermaid-zoom-btn:disabled,
.kb-paper-app .kb-mermaid-zoom-level:disabled,
.kb-mermaid-zoom-btn:disabled,
.kb-mermaid-zoom-level:disabled {
    /* 不降低整个按钮透明度，避免深色主题下刷新图标与背景一起变暗 */
    opacity: 1;
    border-color: var(--kb-control-disabled-border, rgba(107, 114, 128, 0.2)) !important;
    background: var(--kb-control-disabled-bg, #f3f4f6) !important;
    color: var(--kb-control-disabled-text, #6b7280) !important;
    box-shadow: none !important;
    cursor: not-allowed;
    transform: none;
}

.kb-mermaid-zoom-level {
    width: 60px;
    padding-inline: 0.375rem;
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* 未渲染完成时给出占位高度，避免内容跳动 */
.kb-mermaid:not(.kb-mermaid-rendered) {
    min-height: 80px;
}

.kb-mermaid:not(.kb-mermaid-rendered)::after {
    content: '流程图渲染中…';
    display: block;
    padding: 1.5rem 0;
    color: #9ca3af;
    font-size: 0.875rem;
}

[data-theme="dark"] .kb-mermaid,
.theme-dark .kb-mermaid {
    border-color: #374151;
    background: #1f2937;
}

/* 渲染失败降级：保留源码可见 */
.kb-mermaid-error {
    text-align: left;
    border-color: #fca5a5;
    background: #fef2f2;
}

[data-theme="dark"] .kb-mermaid-error,
.theme-dark .kb-mermaid-error {
    border-color: #7f1d1d;
    background: #2a1616;
}

.kb-mermaid-error-tip {
    margin-bottom: 0.5rem;
    color: #b91c1c;
    font-size: 0.875rem;
}

[data-theme="dark"] .kb-mermaid-error-tip,
.theme-dark .kb-mermaid-error-tip {
    color: #fca5a5;
}

.kb-mermaid-source {
    margin: 0;
    padding: 0.75rem;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.04);
    color: inherit;
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 0.8125rem;
    line-height: 1.6;
    white-space: pre-wrap;
    word-break: break-word;
}

[data-theme="dark"] .kb-mermaid-source,
.theme-dark .kb-mermaid-source {
    background: rgba(255, 255, 255, 0.06);
}

/* 编辑器内：提示可双击修改源码 */
.ql-editor .kb-mermaid {
    position: relative;
    cursor: pointer;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.ql-editor .kb-mermaid:hover {
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.12);
}

.ql-editor .kb-mermaid:hover::before {
    /* 使用 Unicode 转义，避免静态服务器未声明 CSS charset 时中文提示乱码 */
    content: '\53cc\51fb\8fdb\884c\7f16\8f91';
    position: absolute;
    top: 6px;
    right: 48px;
    padding: 2px 8px;
    border-radius: 4px;
    background: #6366f1;
    color: #ffffff;
    font-size: 0.75rem;
    line-height: 1.5;
    pointer-events: none;
}

/* AI 助手气泡内：气泡本身就有内边距，正文行距也比详情页紧，图块跟着收一点 */
.ai-message-content .kb-mermaid,
.ai-step-content .kb-mermaid {
    margin: 8px 0;
    padding: 0.75rem;
}

/* 降级源码要挡住 `.ai-message-content pre` 的深色代码块底色：
   那条规则只给了背景没给前景（`.kb-mermaid-source` 是 color: inherit），
   命中后就是深底压深字，源码等于看不见 */
.ai-message-content .kb-mermaid-source,
.ai-step-content .kb-mermaid-source {
    background: rgba(0, 0, 0, 0.04);
    color: inherit;
}

[data-theme="dark"] .ai-message-content .kb-mermaid-source,
[data-theme="dark"] .ai-step-content .kb-mermaid-source,
.theme-dark .ai-message-content .kb-mermaid-source,
.theme-dark .ai-step-content .kb-mermaid-source {
    background: rgba(255, 255, 255, 0.06);
}

/* mermaid 源码编辑弹窗 */
.kb-mermaid-editor {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 0.5rem;
    min-height: 0;
}

.kb-mermaid-editor textarea {
    flex: 1;
    width: 100%;
    min-height: 0;
    padding: 1rem;
    border: 1px solid var(--kb-paper-line, #d1d5db);
    border-radius: 8px;
    background: var(--kb-paper-panel, #ffffff);
    color: var(--kb-ink, #111827);
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 0.875rem;
    line-height: 1.6;
    resize: none;
}

.kb-mermaid-editor textarea:focus {
    border-color: var(--kb-accent, #6366f1);
    outline: none;
    box-shadow: 0 0 0 3px var(--kb-accent-soft, rgba(99, 102, 241, 0.12));
}

[data-theme="dark"] .kb-mermaid-editor textarea,
.theme-dark .kb-mermaid-editor textarea {
    border-color: var(--kb-paper-line, #4b5563);
    background: var(--kb-paper-panel, #111827);
    color: var(--kb-ink, #f9fafb);
}

.kb-mermaid-editor-hint {
    color: var(--kb-ink-muted, #6b7280);
    font-size: 0.8125rem;
}

/* Mermaid 编辑弹窗沿用全站纸张主题，避免 footer 落回 Tailwind 灰蓝色 */
.utils-dialog[data-dialog-type="kb-mermaid-editor"] .dialog-content {
    max-width: min(64rem, calc(100vw - 2rem)) !important;
}

.utils-dialog[data-dialog-type="kb-mermaid-editor"] .dialog-content > .custom-scrollbar {
    display: flex;
    flex-direction: column;
}

.utils-dialog[data-dialog-type="kb-mermaid-editor"] .dialog-footer {
    background: var(--kb-paper-soft, #f9fafb) !important;
    color: var(--kb-ink, #111827) !important;
    border-color: var(--kb-paper-line, #e5e7eb) !important;
}

.utils-dialog[data-dialog-type="kb-mermaid-editor"] .dialog-footer button {
    min-height: 40px;
    border-radius: 8px;
    font-family: inherit;
}

.utils-dialog[data-dialog-type="kb-mermaid-editor"] .dialog-footer button:not(.bg-primary) {
    border-color: var(--kb-paper-line, #d1d5db) !important;
    background: var(--kb-paper-panel, #ffffff) !important;
    color: var(--kb-ink-soft, #374151) !important;
}

.utils-dialog[data-dialog-type="kb-mermaid-editor"] .dialog-footer button:not(.bg-primary):hover {
    background: var(--kb-active-bg, #f3f4f6) !important;
    color: var(--kb-ink, #111827) !important;
}

/* Mermaid 全屏预览 */
body.kb-mermaid-fullscreen-open {
    overflow: hidden !important;
}

.kb-mermaid-fullscreen {
    /* 顶栏要为 macOS 交通灯让出的高度。默认 0，桌面端由 desktop.css 按平台与窗口全屏
       状态统一改写（图表全屏预览用的是同一个变量）——顶栏高度和图的可用高度都从这里算 */
    --kb-fullscreen-titlebar: 0px;
    position: fixed;
    inset: 0;
    z-index: 10050;
    display: flex;
    flex-direction: column;
    background: color-mix(in srgb, var(--kb-paper-panel, #ffffff) 96%, transparent);
    color: var(--kb-ink, #111827);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
}

.kb-mermaid-fullscreen-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* 全局 box-sizing 是 border-box，让位高度必须同时加进 min-height，
       只加 padding 的话顶栏总高不变、内容被往下挤出去 */
    min-height: calc(64px + var(--kb-fullscreen-titlebar));
    padding: calc(0.75rem + var(--kb-fullscreen-titlebar)) 1.25rem 0.75rem;
    border-bottom: 1px solid var(--kb-paper-line, #e5e7eb);
    background: var(--kb-paper-panel, #ffffff);
    flex: 0 0 auto;
}

.kb-mermaid-fullscreen-header h2 {
    margin: 0;
    color: inherit;
    font-family: inherit;
    font-size: 1.125rem;
    font-weight: 600;
}

.kb-mermaid-fullscreen-header-actions,
.kb-mermaid-zoom-controls {
    display: flex;
    align-items: center;
    gap: 0.375rem;
}

.kb-mermaid-fullscreen-header-actions {
    margin-left: 1rem;
}

.kb-mermaid-zoom-controls {
    padding-right: 0.625rem;
    margin-right: 0.25rem;
    border-right: 1px solid var(--kb-paper-line, #e5e7eb);
}

.kb-mermaid-fullscreen-stage {
    flex: 1;
    min-height: 0;
    padding: 1.25rem;
    overflow: hidden;
    overscroll-behavior: contain;
    touch-action: none;
}

.kb-mermaid-fullscreen-diagram {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 100%;
    min-height: 100%;
    margin: 0;
    padding: 1.5rem;
    overflow: visible;
    border: 0;
    background: transparent;
}

.kb-mermaid-fullscreen-diagram svg {
    flex: 0 0 auto;
    width: auto;
    min-width: min(100%, 960px);
    max-width: none;
    height: auto;
    /* 顶栏为交通灯变高多少，图的可用高度就要让回去多少，否则底部被 stage 裁掉 */
    max-height: calc(100dvh - 8.5rem - var(--kb-fullscreen-titlebar));
    cursor: zoom-in;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.kb-mermaid-fullscreen-stage.kb-mermaid-fullscreen-zoomed .kb-mermaid-fullscreen-diagram svg {
    cursor: grab;
}

.kb-mermaid-fullscreen-stage.kb-mermaid-fullscreen-dragging .kb-mermaid-fullscreen-diagram svg {
    cursor: grabbing;
}

.kb-mermaid-fullscreen-diagram .kb-mermaid-viewport {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 100%;
    min-height: 100%;
    overflow: visible;
}

@media (max-width: 1023px) {
    .kb-mermaid {
        padding: 0.75rem;
    }

    .kb-mermaid-fullscreen-header {
        min-height: calc(56px + var(--kb-fullscreen-titlebar));
        padding: calc(0.625rem + var(--kb-fullscreen-titlebar)) 0.875rem 0.625rem;
    }

    .kb-mermaid-fullscreen-header h2 {
        font-size: 1rem;
    }

    .kb-mermaid-fullscreen-header-actions {
        margin-left: 0.5rem;
    }

    .kb-mermaid-zoom-controls {
        gap: 0.25rem;
        padding-right: 0.375rem;
        margin-right: 0;
    }

    .kb-mermaid-zoom-level {
        width: 52px;
    }

    .kb-mermaid-fullscreen-stage {
        padding: 0.75rem;
    }

    .kb-mermaid-fullscreen-diagram {
        padding: 0.75rem;
    }
}
