/* ========================================
   PreviewItem 预览面板 — CSS Module
   ======================================== */

.previewPanel {
    position: relative;
    width: var(--preview-panel-width);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    border-left: 1px solid var(--overlay-light);
    overflow: hidden;
    transition: width var(--duration-slow) var(--ease-out),
                border-color var(--duration-slow) var(--ease-out);
    z-index: 10;
}

.previewPanelCollapsed {
    width: 0;
    border-left: none;
}

.previewPanelInner {
    display: flex;
    flex-direction: column;
    width: var(--preview-panel-width);
    min-width: var(--preview-panel-width);
    height: 100%;
    min-height: 0;
    opacity: 1;
    transition: opacity var(--duration-fast) var(--ease-out);
}

.previewPanelCollapsed .previewPanelInner {
    opacity: 0;
    pointer-events: none;
}

.previewBodyShell {
    flex: 1;
    min-height: 0;
    position: relative;
    margin: var(--panel-edge-gap);
}

.previewPanelBody {
    height: 100%;
    overflow: auto;
    position: relative;
    border-radius: var(--panel-card-radius);
    background: var(--panel-card-bg);
    border: var(--panel-card-border);
    box-shadow: var(--panel-card-shadow);
}

/* HTML 文件预览：外部网页访问交给内置浏览器，右侧面板只承载本地预览文档。 */
.previewPanelBody :global(.preview-html-frame) {
    display: block;
    width: 100%;
    height: 100%;
    min-height: 100%;
    border: 0;
    border-radius: inherit;
    background: #fff;
}

/* Markdown 预览 */
.previewPanelBody :global(.preview-markdown) {
    padding: var(--space-lg) var(--space-lg) var(--space-md);
    font-size: var(--editor-markdown-font-size);
    font-weight: 400;
    line-height: var(--editor-markdown-line-height);
    font-family: var(--editor-markdown-font-family, var(--font-serif));
}

.previewPanelBody :global(.markdown-cover-drop-host .preview-markdown) {
    padding-top: calc(var(--space-xl) + var(--space-lg));
}

.previewPanelBody :global(.markdown-cover) {
    position: relative;
    width: 100%;
    min-height: 160px;
    max-height: 720px;
    overflow: hidden;
    background: var(--bg-card);
    user-select: none;
    touch-action: none;
}

.previewPanelBody :global(.markdown-cover::after) {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    border: 1px solid color-mix(in srgb, var(--accent) 58%, transparent);
    box-shadow:
        inset 0 0 0 999px color-mix(in srgb, var(--accent) 8%, transparent),
        inset 0 -2px 0 color-mix(in srgb, var(--accent) 72%, transparent);
    opacity: 0;
    transition: opacity var(--duration-fast) var(--ease-out);
}

.previewPanelBody :global(.markdown-cover.markdown-cover-drop-active::after) {
    opacity: 1;
}

.previewPanelBody :global(.markdown-cover img) {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
    cursor: grab;
}

.previewPanelBody :global(.markdown-cover img:active) {
    cursor: grabbing;
}

.previewPanelBody :global(.markdown-cover-resize) {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 10px;
    cursor: ns-resize;
}

.previewPanelBody :global(.markdown-cover-resize::after) {
    content: "";
    position: absolute;
    left: 50%;
    bottom: 3px;
    width: 56px;
    height: 2px;
    transform: translateX(-50%);
    border-radius: 999px;
    background: var(--overlay-medium, rgba(0, 0, 0, 0.16));
    opacity: 0;
    transition: opacity var(--duration-fast);
}

.previewPanelBody :global(.markdown-cover:hover .markdown-cover-resize::after) {
    opacity: 0.8;
}

.previewPanelBody :global(.markdown-cover-drop-host) {
    position: relative;
    min-height: 100%;
}

.previewPanelBody :global(.markdown-cover-drop-rail) {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    z-index: 2;
    height: 0;
    pointer-events: none;
}

.previewPanelBody :global(.markdown-cover-drop-rail::before) {
    content: "";
    position: absolute;
    left: var(--space-lg);
    right: var(--space-lg);
    top: 10px;
    height: 2px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--accent) 72%, transparent);
    box-shadow: 0 0 0 8px color-mix(in srgb, var(--accent) 8%, transparent);
    opacity: 0;
    transform: scaleX(0.96);
    transition:
        opacity var(--duration-fast) var(--ease-out),
        transform var(--duration-fast) var(--ease-out);
}

.previewPanelBody :global(.markdown-cover-drop-rail.markdown-cover-drop-rail-active::before) {
    opacity: 1;
    transform: scaleX(1);
}

.previewPanelBody :global(.markdown-cover-menu) {
    position: fixed;
    z-index: 1000;
    min-width: 176px;
    padding: 4px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--overlay-light);
    background: var(--bg-card);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
}

.previewPanelBody :global(.markdown-cover-menu button) {
    display: block;
    width: 100%;
    padding: 7px 9px;
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text);
    font-size: 0.78rem;
    text-align: left;
    cursor: default;
}

.previewPanelBody :global(.markdown-cover-menu button:hover) {
    background: var(--overlay-light);
}

.previewPanelBody :global(.markdown-cover-menu button.markdown-cover-menu-danger) {
    margin-top: 4px;
    border-top: 1px solid var(--overlay-light);
    border-radius: 0 0 var(--radius-sm) var(--radius-sm);
    color: var(--danger, #b45a5a);
}

.previewPanelBody :global(.preview-markdown) h1 {
    text-align: center;
    font-size: var(--editor-markdown-h1-font-size);
    font-weight: 700;
}

.previewPanelBody :global(.preview-markdown.markdown-has-cover) h1 {
    text-align: left;
}

.previewPanelBody :global(.preview-markdown) h2 {
    font-size: var(--editor-markdown-h2-font-size);
    font-weight: 600;
}

.previewPanelBody :global(.preview-markdown) h3 {
    font-size: var(--editor-markdown-h3-font-size);
    font-weight: 600;
}

.previewPanelBody :global(.preview-markdown) h4 {
    font-size: var(--editor-markdown-h4-font-size);
    font-weight: 600;
}

.previewPanelBody :global(.preview-markdown) h5 {
    font-size: var(--editor-markdown-h5-font-size);
    font-weight: 600;
}

.previewPanelBody :global(.preview-markdown) h6 {
    font-size: var(--editor-markdown-h6-font-size);
    font-weight: 600;
}

.previewPanelBody :global(.preview-markdown) strong {
    font-weight: 700;
}

/* 代码预览 */
.previewPanelBody :global(.preview-code) {
    padding: var(--space-md);
    font-family: var(--font-mono);
    font-size: 0.82rem;
    line-height: 1.6;
    overflow-x: auto;
    white-space: pre;
    color: var(--text);
    margin: 0;
}

/* docx 预览 */
.previewPanelBody :global(.preview-docx) {
    padding: var(--space-md) var(--space-lg);
    line-height: 1.8;
    overflow-y: auto;
}

.previewPanelBody :global(.preview-docx) table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.8em 0;
}

.previewPanelBody :global(.preview-docx) th,
.previewPanelBody :global(.preview-docx) td {
    border: 1px solid var(--overlay-light);
    padding: 6px 10px;
}

/* 图片预览 */
.previewPanelBody :global(.preview-image) {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    display: block;
    margin: auto;
}

/* PDF 预览：略微放大让查看器深色背景被圆角裁切掉 */
.previewPanelBody :global(.preview-pdf) {
    width: calc(100% + 8px);
    height: calc(100% + 8px);
    margin: -4px;
    border: none;
    border-radius: inherit;
    overflow: hidden;
}

/* CSV 表格预览 */
.previewPanelBody :global(.preview-csv) {
    padding: var(--space-sm);
    overflow: auto;
    height: 100%;
}

.previewPanelBody :global(.preview-csv) table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    font-family: var(--font-mono);
}

.previewPanelBody :global(.preview-csv) th,
.previewPanelBody :global(.preview-csv) td {
    padding: 6px 10px;
    border: 1px solid var(--overlay-light);
    text-align: left;
    white-space: nowrap;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.previewPanelBody :global(.preview-csv) thead th {
    position: sticky;
    top: 0;
    background: var(--tool-bg);
    font-weight: 600;
    z-index: 1;
}

.previewPanelBody :global(.preview-csv) tbody tr:hover {
    background: var(--overlay-subtle);
}

/* 文件信息卡 */
.previewPanelBody :global(.preview-file-info) {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    gap: var(--space-md);
    padding: var(--space-xl);
    text-align: center;
}

.previewPanelBody :global(.preview-file-icon) {
    width: 56px;
    height: 56px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
}

.previewPanelBody :global(.preview-file-icon) svg {
    width: 48px;
    height: 48px;
}

.previewPanelBody :global(.preview-file-name) {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text);
    word-break: break-all;
}

.previewPanelBody :global(.preview-file-ext) {
    font-size: 0.82rem;
    color: var(--text-muted);
}

.previewPanelBody :global(.preview-file-open-btn) {
    margin-top: var(--space-sm);
    padding: 0.5rem 1.5rem;
    border: none;
    border-radius: var(--radius-md);
    background: var(--accent);
    color: #fff;
    font-size: 0.88rem;
    cursor: default;
    transition: opacity var(--duration-fast);
}

.previewPanelBody :global(.preview-file-open-btn):hover {
    opacity: 0.85;
}

/* ── Preview 编辑器（CodeMirror 6）── */

:global(.preview-editor) {
    height: 100%;
    overflow: hidden;
}

:global(.preview-editor .cm-editor) {
    height: 100%;
    background: transparent;
    position: relative;
}

:global(.preview-editor .cm-editor.cm-focused) {
    outline: none;
}

:global(.preview-editor.mode-markdown .cm-editor.cm-markdown-cover-rail-active::before) {
    content: "";
    position: absolute;
    left: var(--editor-markdown-content-padding-x);
    right: var(--editor-markdown-content-padding-x);
    top: var(--space-sm);
    height: 2px;
    z-index: 12;
    border-radius: 999px;
    background: color-mix(in srgb, var(--accent) 72%, transparent);
    box-shadow: 0 0 0 8px color-mix(in srgb, var(--accent) 8%, transparent);
    pointer-events: none;
}

:global(.preview-editor .cm-gutters) {
    background: transparent;
    border-right: 1px solid var(--overlay-light);
    color: var(--text-muted);
    font-size: 0.75rem;
}

:global(.preview-editor .cm-activeLineGutter) {
    background: transparent;
    color: var(--text);
}

:global(.preview-editor .cm-activeLine) {
    background: var(--overlay-subtle);
}

:global(.preview-editor .cm-selectionBackground) {
    background: var(--accent-light, rgba(100, 140, 200, 0.15)) !important;
}

:global(.preview-editor.mode-markdown .cm-editor) {
    --editor-markdown-selection-bg: color-mix(in srgb, var(--accent) 20%, transparent);
}

:global(.preview-editor.mode-markdown .cm-selectionBackground) {
    background: var(--editor-markdown-selection-bg) !important;
}

:global(.preview-editor.mode-markdown .cm-content::selection),
:global(.preview-editor.mode-markdown .cm-content *::selection) {
    background: var(--editor-markdown-selection-bg);
    color: var(--text);
    text-shadow: none;
}

:global(.preview-editor .cm-cursor) {
    border-left-color: var(--text);
}

.markdownEditorStatus {
    position: absolute;
    right: 12px;
    bottom: 12px;
    z-index: 5;
    pointer-events: none;
    padding: 5px 9px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--overlay-light);
    background: var(--bg-glass, rgba(255, 253, 247, 0.72));
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    box-shadow: 0 4px 14px var(--shadow, rgba(0, 0, 0, 0.08));
    color: var(--text-muted);
    font-family: var(--font-ui);
    font-size: 0.72rem;
    line-height: 1.2;
    white-space: nowrap;
}

/* ── Markdown 模式 ── */

:global(.cm-center-line) {
    text-align: center;
}

:global(.cm-hr-widget) {
    display: inline-block;
    width: 80%;
    vertical-align: middle;
    border-top: 1px solid var(--border);
}

:global(.cm-list-bullet) {
    display: inline-block;
    width: 0.4em;
    margin-right: 0.45em;
}

:global(.cm-list-bullet)::after {
    content: '';
    display: block;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background-color: currentColor;
    opacity: 0.45;
}

:global(.cm-checkbox) {
    cursor: default;
    accent-color: var(--accent);
    margin-right: 0.3em;
    vertical-align: middle;
    position: relative;
    top: -1px;
}

:global(.cm-blockquote-line) {
    border-left: 3px solid var(--accent);
    padding-left: 1em;
    background: var(--overlay-subtle);
}

:global(.cm-codeblock-line) {
    background: var(--overlay-light);
}

:global(.cm-codeblock-toolbar) {
    float: right;
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);
    margin-right: 0;
    font-family: var(--font-ui);
    user-select: none;
    -webkit-user-select: none;
}

:global(.cm-codeblock-lang) {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text-muted);
    pointer-events: none;
    user-select: none;
}

:global(.cm-codeblock-copy-btn) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-xs);
    position: relative;
    width: 2rem;
    min-height: 24px;
    padding: 0;
    border: 0;
    border-radius: 0 var(--radius-sm) 0 var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-family: var(--font-ui);
    font-size: 0.7rem;
    line-height: 1;
    cursor: default;
    opacity: 0.45;
    transition:
        opacity var(--duration-fast) var(--ease-out),
        color var(--duration-fast) var(--ease-out);
}

:global(.cm-codeblock-copy-btn:hover),
:global(.cm-codeblock-copy-btn:focus-visible),
:global(.cm-codeblock-copy-btn[data-copied="true"]) {
    opacity: 1;
    color: var(--accent);
}

:global(.cm-codeblock-copy-icon) {
    width: 13px;
    height: 13px;
    flex: 0 0 auto;
}

:global(.cm-codeblock-copy-label) {
    position: absolute;
    top: 50%;
    right: calc(100% + var(--space-xs));
    transform: translateY(-50%);
    padding: 0.1rem 0.35rem;
    border-radius: var(--radius-sm);
    background: var(--bg-glass, var(--bg-card));
    color: var(--accent);
    font-size: 0.68rem;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity var(--duration-fast) var(--ease-out);
}

:global(.cm-codeblock-copy-btn[data-copied="true"] .cm-codeblock-copy-label) {
    opacity: 1;
}

:global(.cm-image-widget) {
    display: block;
    max-width: 100%;
    margin: var(--space-xs) 0;
}

:global(.cm-image-widget img) {
    max-width: 100%;
    border-radius: var(--radius-sm);
}

:global(.cm-image-fallback) {
    color: var(--text-muted);
    font-style: italic;
    font-size: 0.85em;
}

:global(.cm-link-text) {
    color: var(--link);
    text-decoration: underline;
    text-decoration-color: rgba(var(--link-rgb), 0.55);
    text-underline-offset: 2px;
}

:global(.cm-table-widget) {
    margin: var(--space-xs) 0;
    overflow-x: auto;
}

:global(.cm-table-widget table) {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
    font-family: var(--editor-markdown-font-family, var(--font-serif));
}

:global(.cm-table-widget th),
:global(.cm-table-widget td) {
    border: 1px solid var(--border);
    padding: 0.3em 0.6em;
    text-align: left;
}

:global(.cm-table-widget th) {
    background: var(--overlay-light);
    font-weight: 600;
}

:global(.cm-table-widget th:focus),
:global(.cm-table-widget td:focus) {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
    background: var(--overlay-subtle);
}

:global(.cm-table-widget th[contenteditable]),
:global(.cm-table-widget td[contenteditable]) {
    cursor: text;
}

:global(.cm-mermaid-widget) {
    display: block;
    margin: var(--space-xs) 0;
    padding: var(--space-sm);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--overlay-subtle);
    cursor: text;
    overflow-x: auto;
}

:global(.cm-mermaid-widget:hover),
:global(.cm-mermaid-widget:focus) {
    background: var(--overlay-light);
}

:global(.cm-mermaid-widget .mermaid-source[hidden]) {
    display: none;
}

:global(.cm-mermaid-widget .mermaid-rendered) {
    overflow-x: auto;
}

:global(.cm-mermaid-widget .mermaid-rendered svg) {
    display: block;
    max-width: 100%;
    height: auto;
    margin: 0 auto;
}

:global(.cm-mermaid-widget.is-error .mermaid-rendered) {
    margin-bottom: var(--space-xs);
    color: var(--coral);
    font-size: 0.82em;
}

/* CSV 表格 widget（编辑器内渲染） */
:global(.cm-csv-table-widget) {
    overflow-x: auto;
}

:global(.cm-csv-table-widget table) {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85em;
    font-family: var(--editor-markdown-font-family, var(--font-serif));
}

:global(.cm-csv-table-widget th),
:global(.cm-csv-table-widget td) {
    border: 1px solid var(--border);
    padding: 0.35em 0.6em;
    text-align: left;
    white-space: nowrap;
    max-width: 360px;
    overflow: hidden;
    text-overflow: ellipsis;
}

:global(.cm-csv-table-widget th) {
    background: var(--overlay-light);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 1;
}

:global(.cm-csv-table-widget th:focus),
:global(.cm-csv-table-widget td:focus) {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
    background: var(--overlay-subtle);
    white-space: normal;
    overflow: visible;
}

:global(.cm-csv-table-widget th[contenteditable]),
:global(.cm-csv-table-widget td[contenteditable]) {
    cursor: text;
}

:global(.cm-csv-table-widget tbody tr:hover) {
    background: var(--overlay-subtle);
}

/* 拖拽中禁 transition */
:global(body.resizing) .previewPanel,
:global(body.resizing) .previewPanelInner {
    transition: none !important;
}
