/* ============================================================
   VisualLaTeX — 学术暖白主题
   设计令牌驱动，纯原生 CSS，零依赖
   ============================================================ */

/* 设计令牌 */
:root {
    /* 底色 */
    --bg: #f6f4ef;            /* 暖纸底色 */
    --surface: #ffffff;       /* 面板 */
    --surface-soft: #faf8f3;  /* 次级面板 */
    --border: #e6e1d6;        /* 暖边框 */
    --border-strong: #d6cfc0;

    /* 文字 */
    --text: #26231d;          /* 墨色正文 */
    --text-secondary: #6f695c;

    /* 强调色（氧化红） */
    --accent: #9d4432;
    --accent-hover: #83392a;
    --accent-soft: #f5e9e4;

    /* 成功（导出类按钮） */
    --success: #3f7a54;
    --success-hover: #346647;

    /* 暖深色（预览区 / 状态栏 / 弹窗头） */
    --ink-dark: #2b2822;

    /* 形状与投影 */
    --radius: 6px;
    --radius-lg: 10px;
    --shadow-sm: 0 1px 2px rgba(38, 35, 29, 0.06);
    --shadow-modal: 0 20px 60px rgba(38, 35, 29, 0.25);

    /* 动效 */
    --transition: all 0.18s ease;
}

/* 全局样式重置 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
    background-color: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

/* 主容器 */
.container {
    max-width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* 顶部标题栏（纸白 + 下边线） */
.header {
    background: var(--surface);
    color: var(--text);
    padding: 16px 30px;
    border-bottom: 3px solid var(--accent);
}

.header h1 {
    font-size: 22px;
    font-weight: 600;
    letter-spacing: 0.2px;
}

.header p {
    font-size: 14px;
    color: var(--text-secondary);
    margin-top: 4px;
}

/* 工具栏 */
.toolbar {
    background: var(--surface);
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    align-items: center;
}

.toolbar button {
    padding: 7px 14px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 14px;
    transition: var(--transition);
    font-weight: 500;
}

.toolbar button:hover {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent);
}

.toolbar button:active {
    transform: translateY(0);
    background: #eedcd4;
}

.toolbar button.primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

.toolbar button.primary:hover {
    background: var(--accent-hover);
    color: #fff;
}

.toolbar button.success {
    background: var(--success);
    color: #fff;
    border-color: var(--success);
}

.toolbar button.success:hover {
    background: var(--success-hover);
    color: #fff;
}

.toolbar .separator {
    width: 1px;
    height: 26px;
    background: var(--border);
    margin: 0 6px;
}

/* 主内容区域 - 双栏布局 */
.main-content {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* 左侧编辑区 */
.editor-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.panel-header {
    background: var(--surface-soft);
    padding: 10px 20px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 13px;
    letter-spacing: 0.4px;
    color: var(--text-secondary);
    text-transform: uppercase;
}

.editor-area {
    flex: 1;
    padding: 30px 36px;
    overflow-y: auto;
    outline: none;
    font-size: 16px;
    line-height: 1.8;
}

.editor-area:focus {
    background: #fdfcfa;
}

/* 编辑器内容样式 */
.editor-area h1 {
    font-size: 21px;
    font-weight: 700;
    margin: 20px 0 15px 0;
    color: var(--text);
}

.editor-area h2 {
    font-size: 19px;
    font-weight: 600;
    margin: 18px 0 12px 0;
    color: var(--text);
}

.editor-area h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 15px 0 10px 0;
    color: var(--text);
}

.editor-area p {
    margin: 10px 0;
    text-align: justify;
}

.editor-area ul, .editor-area ol {
    margin: 10px 0;
    padding-left: 30px;
}

.editor-area li {
    margin: 5px 0;
}

/* 行内公式（暖黄纸底） */
.editor-area .formula-inline {
    background: #faf3dd;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    color: #7a5c1e;
}

/* 行间公式（暖纸底 + 强调色左边线） */
.editor-area .formula-block {
    display: block;
    background: var(--surface-soft);
    padding: 12px;
    border-radius: var(--radius);
    margin: 15px 0;
    text-align: center;
    font-family: 'Courier New', monospace;
    color: var(--text);
    border-left: 4px solid var(--accent);
}

/* 原样保留块：表格/文献/代码清单等未可视化支持的 LaTeX 结构 */
.editor-area .raw-tex {
    background: #efece5;
    padding: 10px 12px;
    border-radius: var(--radius);
    margin: 15px 0;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    color: var(--text-secondary);
    border-left: 4px solid var(--border-strong);
    white-space: pre-wrap;
    word-break: break-word;
}

/* 行内 LaTeX 命令（\cite、\ref 等，只读原样保留） */
.editor-area .tex-cmd {
    background: #ece7dd;
    padding: 1px 5px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    color: var(--text-secondary);
}

.editor-area img {
    max-width: 100%;
    height: auto;
    margin: 15px 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

/* 右侧预览区 */
.preview-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--surface-soft);
}

.preview-area {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    font-family: 'Courier New', 'Consolas', monospace;
    font-size: 14px;
    line-height: 1.6;
    white-space: pre-wrap;
    word-wrap: break-word;
    background: var(--ink-dark);
    color: #d8d3c8;
}

/* 状态栏 */
.status-bar {
    background: var(--ink-dark);
    color: #a39d8f;
    padding: 8px 20px;
    font-size: 13px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.status-bar .status-item {
    display: flex;
    align-items: center;
    gap: 8px;
}

.status-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--success);
}

/* 键盘焦点环（可访问性） */
button:focus-visible,
input:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* 滚动条样式 */
::-webkit-scrollbar {
    width: 10px;
}

::-webkit-scrollbar-track {
    background: var(--surface-soft);
}

::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: 5px;
}

::-webkit-scrollbar-thumb:hover {
    background: #b5ab98;
}

/* 动效克制：用户偏好减弱动画时关闭过渡 */
@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation: none !important;
    }
}

/* 加载动画 */
.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spin 1s ease-in-out infinite;
}

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

/* ===== 公式输入弹窗 ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(38, 35, 29, 0.45);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-overlay.show {
    display: flex;
}

.modal-dialog {
    width: 720px;
    max-width: 92vw;
    max-height: 85vh;
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-modal);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--ink-dark);
    color: #fff;
}

.modal-title {
    font-size: 15px;
    font-weight: 600;
    white-space: nowrap;
    flex: 1;
}

.modal-close {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    line-height: 1;
    transition: var(--transition);
}

.modal-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

.modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.tab-panel {
    display: none;
}

.tab-panel.active {
    display: block;
}

/* 公式输入面板 */
.formula-mode-row {
    display: flex;
    gap: 16px;
    margin-bottom: 12px;
}

.mode-option {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    font-size: 14px;
}

.mode-option code {
    background: var(--surface-soft);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    border: 1px solid var(--border);
}

.symbol-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px;
    background: var(--surface-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 12px;
    max-height: 130px;
    overflow-y: auto;
}

.symbol-palette button {
    padding: 4px 10px;
    border: 1px solid var(--border-strong);
    background: var(--surface);
    border-radius: 5px;
    cursor: pointer;
    font-size: 13px;
    font-family: 'Courier New', monospace;
    color: var(--text);
    transition: var(--transition);
}

.symbol-palette button:hover {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}

#formulaInput {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    font-family: 'Courier New', monospace;
    font-size: 14px;
    resize: vertical;
    min-height: 90px;
    box-sizing: border-box;
    color: var(--text);
    background: var(--surface);
}

#formulaInput:focus {
    outline: none;
    border-color: var(--accent);
}

.modal-actions {
    margin-top: 12px;
    display: flex;
    justify-content: flex-end;
}

.btn-insert {
    padding: 10px 28px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 15px;
    cursor: pointer;
    transition: var(--transition);
}

.btn-insert:hover {
    background: var(--accent-hover);
}

/* ============================================================
   移动端 / 窄屏适配
   触发：宽度 ≤768px，或高度 ≤500px（横屏手机）
   桌面宽屏不受本节影响；分段控件在桌面隐藏
   ============================================================ */

.panel-tabs {
    display: none;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

@media (max-width: 768px), (max-height: 500px) {
    html {
        -webkit-text-size-adjust: 100%;
    }

    /* 移动浏览器地址栏收起/展开时 100vh 是错的，dvh 才对；前一行作回退 */
    .container {
        height: 100vh;
        height: 100dvh;
    }

    .header {
        padding: 10px 16px;
    }

    .header h1 {
        font-size: 18px;
    }

    .header p {
        font-size: 12px;
        margin-top: 2px;
    }

    /* 工具栏：17 个按钮收成一行横向滑动，触控高度 ≥44px */
    .toolbar {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
        scrollbar-width: none;
        padding: 8px 10px;
    }

    .toolbar::-webkit-scrollbar {
        display: none;
    }

    .toolbar button {
        flex: 0 0 auto;
        min-height: 44px;
        padding: 10px 12px;
        font-size: 14px;
        /* 缺这条时 CJK 标签会被压成一列字（实测 35px 宽 × 90px 高） */
        white-space: nowrap;
    }

    .toolbar .separator {
        flex: 0 0 1px;
    }

    /* 一屏一个面板，由底部 tab 切换 */
    .main-content {
        flex-direction: column;
    }

    .editor-panel,
    .preview-panel {
        flex: 1 1 auto;
        /* 关键：不设 min-height:0，flex 子项按内容高拒绝收缩，
           预览区会被 overflow:hidden 整块裁到屏幕外 */
        min-height: 0;
    }

    .editor-panel {
        border-right: none;
    }

    .preview-panel {
        display: none;
    }

    .main-content.show-preview .editor-panel {
        display: none;
    }

    .main-content.show-preview .preview-panel {
        display: flex;
    }

    /* 面板标题与 tab 文字重复，窄屏省出这 42px 给内容 */
    .panel-header {
        display: none;
    }

    .panel-tabs {
        display: flex;
    }

    .panel-tabs button {
        flex: 1;
        min-height: 44px;
        padding: 10px 12px;
        border: none;
        background: var(--surface);
        color: var(--text-secondary);
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        transition: var(--transition);
    }

    .panel-tabs button.active {
        color: var(--accent);
        box-shadow: inset 0 -3px 0 var(--accent);
    }

    .editor-area {
        padding: 18px 16px;
        overflow-y: auto;
        overscroll-behavior: contain;
    }

    .editor-area ul,
    .editor-area ol {
        padding-left: 22px;
    }

    .editor-area .formula-block {
        font-size: 14px;
        overflow-x: auto;
    }

    .preview-area {
        padding: 16px;
        font-size: 13px;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    .status-bar {
        padding: 8px 14px;
        font-size: 12px;
    }

    /* 公式弹窗贴底铺开，符号与输入区触控目标加大 */
    .modal-overlay {
        align-items: flex-end;
    }

    .modal-dialog {
        width: 100%;
        max-width: 100%;
        max-height: 92vh;
        max-height: 92dvh;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    }

    .modal-close {
        width: 44px;
        height: 44px;
    }

    .symbol-palette button {
        min-height: 44px;
        padding: 8px 12px;
    }

    /* iOS：输入框字号 <16px 会在聚焦时放大整页且回不去 */
    #formulaInput {
        font-size: 16px;
    }

    .btn-insert {
        min-height: 44px;
    }
}

/* 横屏手机：副标题让位给编辑区 */
@media (max-height: 500px) {
    .header p {
        display: none;
    }
}
