/*
 * Markdown Editor - 实时预览器样式
 * 基于 GitHub 风格设计，提供专业的代码编辑体验
 */

/* CSS 变量定义 - GitHub 风格 */
:root {
    /* GitHub 主色调 */
    --github-blue: #0969da;
    --github-green: #1a7f37;
    --github-red: #d1242f;
    --github-orange: #bc4c00;
    --github-purple: #8250df;
    --github-gray: #656d76;

    /* 编辑器专用色彩 */
    --editor-bg: #ffffff;
    --editor-panel-bg: #f6f8fa;
    --editor-border: #d0d7de;
    --editor-text: #24292f;
    --editor-text-muted: #656d76;
    --editor-accent: var(--github-blue);

    /* 间距系统 */
    --space-xs: 0.25rem;
    --space-sm: 0.5rem;
    --space-md: 1rem;
    --space-lg: 1.5rem;
    --space-xl: 2rem;

    /* 字体系统 */
    --font-mono: 'SFMono-Regular', 'Consolas', 'Liberation Mono', 'Menlo', monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;

    /* 动画 */
    --transition: all 0.2s cubic-bezier(0.25, 0.46, 0.45, 0.94);

    /* 圆角 */
    --radius-sm: 3px;
    --radius-md: 6px;
    --radius-lg: 8px;

    /* 阴影 */
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.12);
    --shadow-md: 0 3px 6px rgba(0, 0, 0, 0.15);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
}

/* 亮色主题变量（GitHub 默认） */
[data-theme="light"],
body[data-theme="light"] {
    --editor-bg: #ffffff;
    --editor-panel-bg: #f6f8fa;
    --editor-text: #24292f;
    --editor-text-muted: #656d76;
    --editor-border: #d0d7de;
    --editor-accent: var(--github-blue);
}

/* 暗色主题变量（GitHub Dark） */
[data-theme="dark"],
body[data-theme="dark"] {
    --editor-bg: #0d1117;
    --editor-panel-bg: #161b22;
    --editor-text: #f0f6fc;
    --editor-text-muted: #8b949e;
    --editor-border: #30363d;
    --editor-accent: #58a6ff;
    --github-blue: #58a6ff;
    --github-green: #3fb950;
    --github-red: #f85149;
    --github-orange: #d29922;
    --github-purple: #a5a5ff;
}

/* 全局重置 */
.markdown-editor-container * {
    box-sizing: border-box;
}

/* 主容器 */
.markdown-editor-container {
    min-height: 100vh;
    background: var(--editor-bg);
    color: var(--editor-text);
    font-family: var(--font-sans);
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

/* 头部区域 */
.editor-header {
    background: var(--editor-bg);
    border-bottom: 1px solid var(--editor-border);
    padding: var(--space-md) var(--space-xl);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: none;
}

.header-left {
    display: flex;
    align-items: center;
    gap: var(--space-lg);
}

.back-link {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    color: var(--editor-text-muted);
    text-decoration: none;
    padding: var(--space-sm) var(--space-md);
    border-radius: 8px;
    transition: var(--transition);
    border: 1px solid var(--editor-border);
}

.back-link:hover {
    color: var(--editor-accent);
    border-color: var(--editor-accent);
    transform: translateX(-2px);
}

.back-icon {
    width: 18px;
    height: 18px;
}

.editor-title {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    color: var(--editor-text);
}

.title-icon {
    font-size: 1.5rem;
}

/* 头部控制按钮 */
.header-controls {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.control-btn {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-sm) var(--space-md);
    background: var(--editor-bg);
    border: 1px solid var(--editor-border);
    border-radius: var(--radius-md);
    color: var(--editor-text);
    cursor: pointer;
    transition: var(--transition);
    font-size: 0.875rem;
    font-family: inherit;
}

.control-btn:hover {
    background: var(--editor-panel-bg);
    border-color: var(--editor-accent);
    color: var(--editor-accent);
}

.control-btn.active {
    background: var(--editor-accent);
    color: #ffffff;
    border-color: var(--editor-accent);
}

.control-btn svg {
    width: 16px;
    height: 16px;
}

/* 主题切换 */
.theme-toggle .moon-icon {
    display: none;
}

[data-theme="dark"] .theme-toggle .sun-icon {
    display: none;
}

[data-theme="dark"] .theme-toggle .moon-icon {
    display: block;
}

/* 布局切换 */
.layout-toggle .preview-icon {
    display: none;
}

.layout-preview .layout-toggle .split-icon {
    display: none;
}

.layout-preview .layout-toggle .preview-icon {
    display: block;
}

/* 导出下拉菜单 */
.export-dropdown {
    position: relative;
}

.export-menu {
    position: absolute;
    top: 100%;
    right: 0;
    background: var(--editor-panel-bg);
    border: 1px solid var(--editor-border);
    border-radius: 8px;
    padding: var(--space-sm);
    min-width: 200px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: var(--transition);
    z-index: 1000;
}

.export-dropdown:hover .export-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.export-option {
    display: block;
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    background: none;
    border: none;
    color: var(--editor-text);
    text-align: left;
    border-radius: 4px;
    cursor: pointer;
    transition: var(--transition);
    font-family: inherit;
}

.export-option:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--editor-accent);
}

/* 文件输入 */
.file-input {
    display: none;
}

/* 主编辑区域 */
.editor-main {
    flex: 1;
    display: flex;
    min-height: calc(100vh - 120px); /* 减去头部和状态栏高度 */
    position: relative;
}

/* 编辑器面板 */
.editor-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--editor-border);
    min-width: 0;
}

/* 预览面板 */
.preview-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* 仅预览模式 */
.layout-preview .editor-panel {
    display: none;
}

.layout-preview .preview-panel {
    border-left: none;
}

/* 面板头部 */
.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-md) var(--space-lg);
    background: var(--editor-panel-bg);
    border-bottom: 1px solid var(--editor-border);
}

.panel-title {
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    color: var(--editor-text);
}

.panel-icon {
    font-size: 1.2rem;
}

/* 编辑器统计 */
.editor-stats {
    display: flex;
    gap: var(--space-lg);
    font-size: 0.75rem;
    color: var(--editor-text-muted);
}

.stat-item {
    display: flex;
    gap: var(--space-xs);
}

.stat-label {
    opacity: 0.7;
}

/* 预览控制 */
.preview-controls {
    display: flex;
    gap: var(--space-xs);
}

/* 编辑器包装器 */
.editor-wrapper {
    flex: 1;
    position: relative;
    overflow: hidden;
    min-height: 500px; /* 确保最小高度 */
}

/* 原生 textarea 样式（作为 CodeMirror 的后备） */
#markdown-editor {
    width: 100%;
    height: 100%;
    min-height: 500px;
    border: none;
    outline: none;
    resize: none;
    font-family: var(--font-mono);
    font-size: 14px;
    line-height: 1.6;
    background: var(--editor-bg);
    color: var(--editor-text);
    padding: var(--space-md);
}

/* CodeMirror 样式覆盖 */
.CodeMirror {
    height: 100% !important;
    font-family: var(--font-mono) !important;
    font-size: 14px !important;
    line-height: 1.6 !important;
    background: var(--editor-bg) !important;
    color: var(--editor-text) !important;
}

.CodeMirror-gutters {
    background: var(--editor-panel-bg) !important;
    border-right: 1px solid var(--editor-border) !important;
}

.CodeMirror-linenumber {
    color: var(--editor-text-muted) !important;
    padding: 0 8px !important;
}

.CodeMirror-activeline-background {
    background: rgba(255, 255, 255, 0.05) !important;
}

.CodeMirror-cursor {
    border-left: 2px solid var(--editor-accent) !important;
}

.CodeMirror-selected {
    background: rgba(0, 212, 255, 0.2) !important;
}

/* Markdown 语法高亮 - GitHub 风格 */
.cm-header {
    color: var(--github-blue) !important;
    font-weight: bold !important;
}

.cm-strong {
    color: var(--editor-text) !important;
    font-weight: bold !important;
}

.cm-em {
    color: var(--editor-text) !important;
    font-style: italic !important;
}

.cm-link {
    color: var(--github-blue) !important;
}

.cm-url {
    color: var(--github-green) !important;
}

.cm-quote {
    color: var(--editor-text-muted) !important;
    font-style: italic !important;
}

.cm-code {
    background: var(--editor-panel-bg) !important;
    color: var(--github-red) !important;
    padding: 2px 4px !important;
    border-radius: 3px !important;
    border: 1px solid var(--editor-border) !important;
}

/* 分割器 */
.splitter {
    width: 4px;
    background: var(--editor-border);
    cursor: col-resize;
    position: relative;
    transition: var(--transition);
}

.splitter:hover {
    background: var(--editor-accent);
}

.splitter::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 20px;
    height: 40px;
    background: repeating-linear-gradient(
        90deg,
        transparent,
        transparent 2px,
        var(--editor-border) 2px,
        var(--editor-border) 4px
    );
    border-radius: 10px;
    opacity: 0;
    transition: var(--transition);
}

.splitter:hover::before {
    opacity: 1;
}

/* 预览包装器 */
.preview-wrapper {
    flex: 1;
    overflow: auto;
    background: var(--editor-bg);
    min-height: 500px; /* 确保最小高度 */
}

/* 预览内容 */
.preview-content {
    padding: var(--space-xl);
    max-width: none;
    font-family: var(--font-sans);
    line-height: 1.7;
    color: var(--editor-text);
}

/* Markdown 预览样式 - GitHub 风格 */
.preview-content h1,
.preview-content h2,
.preview-content h3,
.preview-content h4,
.preview-content h5,
.preview-content h6 {
    color: var(--editor-text);
    font-weight: 600;
    margin-top: 1.5rem;
    margin-bottom: 1rem;
    line-height: 1.25;
}

.preview-content h1 {
    font-size: 2rem;
    border-bottom: 1px solid var(--editor-border);
    padding-bottom: 0.3rem;
    margin-bottom: 1rem;
}

.preview-content h2 {
    font-size: 1.5rem;
    border-bottom: 1px solid var(--editor-border);
    padding-bottom: 0.3rem;
}

.preview-content h3 {
    font-size: 1.25rem;
}

.preview-content h4 {
    font-size: 1rem;
}

.preview-content h5 {
    font-size: 0.875rem;
}

.preview-content h6 {
    font-size: 0.85rem;
    color: var(--editor-text-muted);
}

.preview-content p {
    margin-bottom: 1rem;
    color: var(--editor-text);
}

.preview-content strong {
    color: var(--editor-text);
    font-weight: 600;
}

.preview-content em {
    color: var(--editor-text);
    font-style: italic;
}

.preview-content del {
    color: var(--editor-text-muted);
    text-decoration: line-through;
}

.preview-content code {
    background: var(--editor-panel-bg);
    color: var(--github-red);
    padding: 0.2em 0.4em;
    border-radius: 6px;
    font-family: var(--font-mono);
    font-size: 85%;
    border: 1px solid var(--editor-border);
}

.preview-content pre {
    background: var(--editor-panel-bg);
    border: 1px solid var(--editor-border);
    border-radius: 6px;
    padding: 16px;
    overflow-x: auto;
    margin: 16px 0;
    position: relative;
    font-size: 85%;
    line-height: 1.45;
}

.preview-content pre code {
    background: none;
    padding: 0;
    color: var(--editor-text);
    font-size: inherit;
    border: none;
}

.preview-content blockquote {
    border-left: 0.25em solid var(--editor-border);
    padding: 0 1em;
    margin: 16px 0;
    color: var(--editor-text-muted);
    background: none;
    border-radius: 0;
}

.preview-content ul,
.preview-content ol {
    padding-left: 2em;
    margin: 16px 0;
}

.preview-content li {
    margin-bottom: 0.25em;
}

.preview-content a {
    color: var(--github-blue);
    text-decoration: none;
    border-bottom: none;
    transition: var(--transition);
}

.preview-content a:hover {
    text-decoration: underline;
    color: var(--github-blue);
}

/* 表格样式 - GitHub 风格 */
.preview-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
    background: var(--editor-bg);
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid var(--editor-border);
    display: block;
    overflow-x: auto;
    white-space: nowrap;
}

.preview-content th,
.preview-content td {
    padding: 6px 13px;
    text-align: left;
    border: 1px solid var(--editor-border);
}

.preview-content th {
    background: var(--editor-panel-bg);
    color: var(--editor-text);
    font-weight: 600;
}

.preview-content tr:nth-child(2n) {
    background: var(--editor-panel-bg);
}

.preview-content tr:hover {
    background: rgba(0, 0, 0, 0.05);
}

/* 数学公式样式 - GitHub 风格 */
.katex {
    font-size: 1.1em !important;
    color: var(--editor-text) !important;
}

.katex-display {
    margin: 16px 0 !important;
    padding: 16px !important;
    background: var(--editor-panel-bg) !important;
    border-radius: 6px !important;
    border: 1px solid var(--editor-border) !important;
}

/* Mermaid 图表样式 - GitHub 风格 */
.mermaid-container {
    text-align: center;
    margin: 16px 0;
    background: var(--editor-panel-bg);
    padding: 16px;
    border-radius: 6px;
    border: 1px solid var(--editor-border);
}

.mermaid-container.mermaid-rendered {
    background: var(--editor-bg);
    padding: 8px;
}

.mermaid-container svg {
    max-width: 100%;
    height: auto;
}

/* 状态栏 - GitHub 风格 */
.status-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background: var(--editor-panel-bg);
    border-top: 1px solid var(--editor-border);
    font-size: 12px;
    color: var(--editor-text-muted);
    min-height: 22px;
}

.status-left,
.status-right {
    display: flex;
    gap: var(--space-lg);
}

.status-item {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
}

.status-icon {
    font-size: 0.9rem;
}

/* 加载覆盖层 */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: var(--transition);
}

.loading-overlay.show {
    opacity: 1;
    visibility: visible;
}

.loading-spinner {
    text-align: center;
    color: var(--editor-text);
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--editor-border);
    border-top: 3px solid var(--editor-accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto var(--space-md);
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* 通知提示 - GitHub 风格 */
.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    background: var(--editor-panel-bg);
    border: 1px solid var(--editor-border);
    border-radius: 6px;
    padding: 12px 16px;
    color: var(--editor-text);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    transform: translateX(100%);
    transition: var(--transition);
    max-width: 300px;
    font-size: 14px;
}

.toast.show {
    transform: translateX(0);
}

.toast.success {
    border-color: var(--github-green);
    background: var(--editor-panel-bg);
    border-left: 4px solid var(--github-green);
}

.toast.error {
    border-color: var(--github-red);
    background: var(--editor-panel-bg);
    border-left: 4px solid var(--github-red);
}

.toast.info {
    border-color: var(--github-blue);
    background: var(--editor-panel-bg);
    border-left: 4px solid var(--github-blue);
}

/* 响应式设计 */
@media (max-width: 1024px) {
    .editor-main {
        flex-direction: column;
    }

    .editor-panel,
    .preview-panel {
        flex: none;
        height: 50vh;
    }

    .splitter {
        width: 100%;
        height: 4px;
        cursor: row-resize;
    }

    .splitter::before {
        width: 40px;
        height: 20px;
        background: repeating-linear-gradient(
            0deg,
            transparent,
            transparent 2px,
            var(--editor-border) 2px,
            var(--editor-border) 4px
        );
    }
}

@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        gap: var(--space-md);
    }

    .header-controls {
        flex-wrap: wrap;
        justify-content: center;
    }

    .editor-stats {
        flex-direction: column;
        gap: var(--space-xs);
    }

    .status-bar {
        flex-direction: column;
        gap: var(--space-sm);
        text-align: center;
    }

    .preview-content {
        padding: var(--space-md);
    }

    .toast-container {
        left: var(--space-md);
        right: var(--space-md);
    }

    .toast {
        max-width: none;
    }
}

@media (max-width: 480px) {
    .editor-header {
        padding: var(--space-md);
    }

    .panel-header {
        padding: var(--space-sm) var(--space-md);
    }

    .editor-title {
        font-size: 1.25rem;
    }

    .control-btn {
        padding: var(--space-xs) var(--space-sm);
        font-size: 0.75rem;
    }

    .control-btn svg {
        width: 14px;
        height: 14px;
    }
}

/* 打印样式 */
@media print {
    .editor-header,
    .status-bar,
    .editor-panel {
        display: none !important;
    }

    .markdown-editor-container {
        background: white !important;
        color: black !important;
    }

    .preview-panel {
        border: none !important;
    }

    .preview-content {
        padding: 0 !important;
        color: black !important;
    }

    .preview-content h1,
    .preview-content h2,
    .preview-content h3,
    .preview-content h4,
    .preview-content h5,
    .preview-content h6 {
        color: black !important;
    }
}

/* 高对比度模式 */
@media (prefers-contrast: high) {
    :root {
        --editor-border: rgba(255, 255, 255, 0.3);
        --editor-text-muted: #cccccc;
    }
}

/* 减少动画 */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
/* 拖拽上传样式 - GitHub 风格 */
.markdown-editor-container.drag-over {
    position: relative;
}

.markdown-editor-container.drag-over::before {
    content: '拖拽 Markdown 文件到这里';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.8);
    border: 3px dashed var(--github-blue);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: 600;
    color: var(--github-blue);
    z-index: 10000;
    backdrop-filter: blur(10px);
}

/* 错误状态样式 - GitHub 风格 */
.error {
    color: var(--github-red);
    background: var(--editor-panel-bg);
    padding: 16px;
    border-radius: 6px;
    border: 1px solid var(--github-red);
    border-left: 4px solid var(--github-red);
    margin: 16px;
}

.mermaid-error {
    color: var(--github-red);
    background: var(--editor-panel-bg);
    padding: 12px;
    border-radius: 6px;
    border: 1px solid var(--github-red);
    border-left: 4px solid var(--github-red);
    text-align: center;
    font-family: var(--font-mono);
    font-size: 14px;
}

/* 全屏模式样式 */
.markdown-editor-container:fullscreen {
    background: var(--editor-bg);
}

.markdown-editor-container:fullscreen .editor-header {
    position: static;
}

/* 选择文本样式 - GitHub 风格 */
.markdown-editor-container ::selection {
    background: rgba(9, 105, 218, 0.3);
    color: var(--editor-text);
}

.markdown-editor-container ::-moz-selection {
    background: rgba(9, 105, 218, 0.3);
    color: var(--editor-text);
}

/* 焦点样式 */
.control-btn:focus,
.export-option:focus {
    outline: 2px solid var(--editor-accent);
    outline-offset: 2px;
}

/* 无障碍改进 */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* 加载动画增强 */
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.loading-overlay.show .loading-spinner p {
    animation: pulse 1.5s ease-in-out infinite;
}

/* 工具提示样式 */
[title] {
    position: relative;
}

[title]:hover::after {
    content: attr(title);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--editor-panel-bg);
    color: var(--editor-text);
    padding: var(--space-xs) var(--space-sm);
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
    z-index: 1000;
    border: 1px solid var(--editor-border);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}