/* ===== 基础样式和重置 ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4a90d9;
    --primary-dark: #3a7bc8;
    --secondary-color: #6c757d;
    --danger-color: #dc3545;
    --success-color: #28a745;
    --warning-color: #ffc107;
    
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-tertiary: #0f3460;
    --bg-hover: #1e3a5f;
    
    --text-primary: #eaeaea;
    --text-secondary: #a0a0a0;
    --text-muted: #6c757d;
    
    --border-color: #2a3f5f;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    
    --sidebar-width: 280px;
    --toolbar-height: 56px;
    --quick-toolbar-height: 48px;
    
    --font-mono: 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    touch-action: manipulation;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

/* ===== 布局 ===== */
#app {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* ===== 侧边栏 ===== */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    z-index: 100;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h1 {
    font-size: 1.2rem;
    font-weight: 600;
}

.sidebar-actions {
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    border-bottom: 1px solid var(--border-color);
}

.file-list-container {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}

.btn-small {
    padding: 8px 12px;
    font-size: 0.85rem;
}

.file-list-container h3 {
    font-size: 0.75rem;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 8px;
    letter-spacing: 0.5px;
}

.file-list {
    list-style: none;
}

.file-list li {
    padding: 10px 12px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: background-color 0.2s;
    font-size: 0.9rem;
    margin-bottom: 4px;
}

.file-list li:hover {
    background-color: var(--bg-hover);
}

.file-list li.active {
    background-color: var(--bg-tertiary);
}

.file-list li .filename {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-list li .file-actions {
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s;
}

.file-list li .file-icon {
    margin-right: 8px;
    font-size: 1rem;
}

.file-list li:hover .file-actions {
    opacity: 1;
}

.file-list li .file-actions button {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    font-size: 0.8rem;
}

.file-list li .file-actions button:hover {
    color: var(--text-primary);
}

/* ===== 遮罩层 ===== */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 90;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
}

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

/* ===== 主内容区 ===== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* ===== 工具栏 ===== */
.toolbar {
    height: var(--toolbar-height);
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 12px;
    gap: 12px;
}

.toolbar-title {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.toolbar-title span:first-child {
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.save-status {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.save-status.saving::before {
    content: '保存中...';
}

.save-status.saved::before {
    content: '已保存';
}

.save-status.unsaved::before {
    content: '未保存';
    color: var(--warning-color);
}

.toolbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ===== 编辑器容器 ===== */
.editor-container {
    flex: 1;
    display: flex;
    overflow: hidden;
    position: relative;
}

.editor-wrapper,
.preview-wrapper {
    flex: 1;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

.editor-wrapper {
    background-color: var(--bg-primary);
}

.preview-wrapper {
    background-color: var(--bg-secondary);
    padding: 20px;
}

.preview-wrapper.hidden {
    display: none;
}

/* ===== 编辑器 ===== */
#editor {
    width: 100%;
    height: 100%;
    border: none;
    outline: none;
    resize: none;
    background-color: transparent;
    color: var(--text-primary);
    font-family: var(--font-mono);
    font-size: 15px;
    line-height: 1.8;
    padding: 20px;
    tab-size: 4;
    -webkit-overflow-scrolling: touch;
}

#editor::placeholder {
    color: var(--text-muted);
}

/* ===== Markdown 预览样式 ===== */
.markdown-body {
    color: var(--text-primary);
    font-size: 15px;
    line-height: 1.8;
    max-width: 800px;
    margin: 0 auto;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
    margin-top: 24px;
    margin-bottom: 16px;
    font-weight: 600;
    line-height: 1.25;
}

.markdown-body h1 {
    font-size: 2em;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.3em;
}

.markdown-body h2 {
    font-size: 1.5em;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.3em;
}

.markdown-body h3 {
    font-size: 1.25em;
}

.markdown-body p {
    margin-bottom: 16px;
}

.markdown-body a {
    color: var(--primary-color);
    text-decoration: none;
}

.markdown-body a:hover {
    text-decoration: underline;
}

.markdown-body strong {
    font-weight: 600;
}

.markdown-body em {
    font-style: italic;
}

.markdown-body code {
    background-color: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 3px;
    font-family: var(--font-mono);
    font-size: 0.9em;
}

.markdown-body pre {
    background-color: var(--bg-tertiary);
    padding: 16px;
    border-radius: 6px;
    overflow-x: auto;
    margin-bottom: 16px;
}

.markdown-body pre code {
    background: none;
    padding: 0;
}

.markdown-body ul,
.markdown-body ol {
    margin-bottom: 16px;
    padding-left: 2em;
}

.markdown-body li {
    margin-bottom: 4px;
}

.markdown-body blockquote {
    border-left: 4px solid var(--primary-color);
    padding-left: 16px;
    margin-bottom: 16px;
    color: var(--text-secondary);
}

.markdown-body hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 24px 0;
}

.markdown-body img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
}

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 16px;
}

.markdown-body th,
.markdown-body td {
    border: 1px solid var(--border-color);
    padding: 8px 12px;
    text-align: left;
}

.markdown-body th {
    background-color: var(--bg-tertiary);
    font-weight: 600;
}

/* ===== 快捷工具栏 ===== */
.quick-toolbar {
    height: var(--quick-toolbar-height);
    background-color: var(--bg-secondary);
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 0 12px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.quick-btn {
    min-width: 40px;
    height: 36px;
    padding: 0 12px;
    background-color: var(--bg-tertiary);
    border: none;
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    flex-shrink: 0;
}

.quick-btn:hover {
    background-color: var(--bg-hover);
}

.quick-btn:active {
    transform: scale(0.95);
}

/* ===== 按钮样式 ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 16px;
    border: none;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-dark);
}

.btn-secondary {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
}

.btn-secondary:hover {
    background-color: var(--bg-hover);
}

.btn-danger {
    background-color: var(--danger-color);
    color: white;
}

.btn-danger:hover {
    background-color: #c82333;
}

.btn-text {
    background: none;
    color: var(--text-secondary);
    padding: 8px 12px;
}

.btn-text:hover {
    color: var(--text-primary);
}

.icon-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.2rem;
    cursor: pointer;
    border-radius: 6px;
    transition: background-color 0.2s;
}

.icon-btn:hover {
    background-color: var(--bg-hover);
}

/* ===== 弹窗样式 ===== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
}

.modal.show {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background-color: var(--bg-secondary);
    border-radius: 12px;
    width: 100%;
    max-width: 420px;
    max-height: 90vh;
    overflow: auto;
    box-shadow: var(--shadow);
}

.modal-small {
    max-width: 320px;
}

.modal-header {
    padding: 20px 20px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 4px;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-content form {
    padding: 20px;
}

.modal-content p {
    padding: 0 20px 20px;
    color: var(--text-secondary);
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-size: 1rem;
    -webkit-appearance: none;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary-color);
}

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

.form-actions .btn {
    flex: 1;
}

/* ===== Toast 提示 ===== */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    padding: 12px 24px;
    border-radius: 8px;
    box-shadow: var(--shadow);
    z-index: 300;
    font-size: 0.9rem;
    opacity: 0;
    transition: transform 0.3s, opacity 0.3s;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

.toast.success {
    background-color: var(--success-color);
    color: white;
}

.toast.error {
    background-color: var(--danger-color);
    color: white;
}

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

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--bg-tertiary);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--border-color);
}

/* ===== 移动端适配 ===== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        transform: translateX(-100%);
    }
    
    .sidebar.open {
        transform: translateX(0);
    }
    
    .mobile-only {
        display: flex !important;
    }
    
    .editor-wrapper,
    .preview-wrapper {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }
    
    .preview-wrapper {
        transform: translateX(100%);
        transition: transform 0.3s ease;
    }
    
    .preview-wrapper.show {
        transform: translateX(0);
    }
    
    .preview-wrapper.hidden {
        display: block;
        transform: translateX(100%);
    }
    
    #editor {
        font-size: 16px; /* 防止 iOS 缩放 */
        padding: 16px;
    }
    
    .markdown-body {
        font-size: 16px;
        padding: 0;
    }
    
    .quick-toolbar {
        gap: 6px;
    }
    
    .quick-btn {
        min-width: 36px;
        height: 32px;
        padding: 0 8px;
    }
}

@media (min-width: 769px) {
    .mobile-only {
        display: none !important;
    }
    
    #closeSidebar {
        display: none;
    }
}

/* ===== 加载动画 ===== */
@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.loading::after {
    content: '';
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid transparent;
    border-top-color: currentColor;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-left: 8px;
}

/* ===== 选中文字样式 ===== */
::selection {
    background-color: var(--primary-color);
    color: white;
}

/* ===== 焦点样式 ===== */
*:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
    outline-offset: 0;
}
