/* === 基本樣式 === */
*, *::before, *::after {
    box-sizing: border-box;
}

html {
    margin: 0;
    padding: 0;
    overflow-x: hidden;
    overflow-y: auto;
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: #f4f7f6;
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* === 頁面整體布局 === */
.page-wrapper {
    display: flex;
    width: 100%;
    max-width: 100%;
    min-height: 100vh;
    overflow: hidden;
    box-sizing: border-box;
    position: relative;
}

/* === 左側邊欄樣式 (與 agent_advanced 一致) === */
.sidebar {
    width: 280px;
    background: #ffffff;
    color: #1f2937;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #e5e7eb;
    flex-shrink: 0;
    height: 100vh;
    overflow: hidden;
    transition: width 0.3s ease, min-width 0.3s ease, padding 0.3s ease;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #ffffff;
}

.sidebar-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
    color: #1f2937;
}

.btn-refresh {
    background: #3498db;
    border: none;
    color: #ffffff;
    width: 32px;
    height: 32px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-refresh:hover {
    background: #2980b9;
}

.sidebar-search {
    padding: 15px 20px;
    border-bottom: 1px solid #e5e7eb;
    background: #ffffff;
}

.sidebar-search input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    background-color: #f9fafb;
    color: #1f2937;
    font-size: 14px;
    box-sizing: border-box;
    transition: all 0.2s;
}

.sidebar-search input::placeholder {
    color: #9ca3af;
}

.sidebar-search input:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
    background-color: #ffffff;
}

/* === 檔案樹樣式 === */
.file-tree {
    flex: 1;
    overflow-y: auto;
    padding: 10px 0;
    background: #ffffff;
}

.file-item {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    cursor: pointer;
    transition: all 0.2s;
    border-left: 3px solid transparent;
    color: #374151;
}

.file-item:hover {
    background-color: #f3f4f6;
    color: #1f2937;
}

.file-item.active {
    background-color: #3498db;
    color: #ffffff;
    border-left-color: #2980b9;
}

.file-item.loading {
    opacity: 0.6;
    pointer-events: none;
}

.file-icon {
    margin-right: 10px;
    font-size: 16px;
}

.file-name {
    flex: 1;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-meta {
    font-size: 11px;
    color: #6b7280;
}

.file-item.active .file-meta {
    color: rgba(255, 255, 255, 0.8);
}

.tag-count {
    background-color: #e5e7eb;
    color: #374151;
    padding: 2px 6px;
    border-radius: 10px;
    font-size: 11px;
}

.file-item.active .tag-count {
    background-color: rgba(255, 255, 255, 0.2);
    color: #ffffff;
}

.btn-delete-file {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 20px;
    padding: 4px 6px;
    margin-left: 8px;
    opacity: 0;
    transition: opacity 0.2s, transform 0.15s;
    flex-shrink: 0;
}

.file-item:hover .btn-delete-file {
    opacity: 0.6;
    color: #e74c3c;
}

.btn-delete-file:hover {
    opacity: 1 !important;
    color: #c0392b;
    transform: scale(1.2);
}

.file-item.deleting {
    opacity: 0.4;
    pointer-events: none;
}

.loading-files {
    text-align: center;
    padding: 40px 20px;
    color: #6b7280;
}

.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: #6b7280;
}

.empty-state p {
    margin: 10px 0 0;
    font-size: 14px;
}

/* === 側邊欄折疊按鈕 (與 agent_advanced 一致) === */
.sidebar-toggle-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 280px;
    z-index: 101;
    width: 24px;
    height: 64px;
    background: #4a5568;
    border: none;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 14px;
    transition: left 0.3s ease, background 0.2s ease;
    box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
}

.sidebar-toggle-btn:hover {
    background: #3498db;
    transform: translateY(-50%) scale(1.05);
}

.sidebar-toggle-btn svg {
    width: 18px;
    height: 18px;
    transition: transform 0.3s ease;
}

/* 側邊欄折疊狀態 */
.sidebar.collapsed {
    width: 0;
    min-width: 0;
    overflow: hidden;
    padding: 0;
    border: none;
}

.sidebar.collapsed + .sidebar-toggle-btn {
    left: 0;
}

.sidebar.collapsed + .sidebar-toggle-btn svg {
    transform: rotate(180deg);
}

/* === 主內容區 === */
.main-content {
    flex: 1 1 0;
    min-width: 0; /* 防止 flex item 溢出 */
    width: 100%; /* 使用 flex 自動計算 */
    max-width: 100%;
    padding: 20px;
    background-color: #f4f7f6;
    overflow-y: auto;
    overflow-x: hidden;
    transition: all 0.3s ease;
    box-sizing: border-box;
}

/* 側邊欄折疊時的主內容區寬度 - flex: 1 會自動處理 */
.sidebar.collapsed ~ .main-content,
body.sidebar-collapsed .main-content {
    width: 100%;
    max-width: 100%;
}

.container {
    width: 100%;
    max-width: 100%;
    margin: 0;
    background-color: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
    overflow: hidden;
    /* 防止 flex 子元素溢出 */
    min-width: 0;
}

h1 {
    text-align: center;
    color: #2c3e50;
    margin-bottom: 30px;
    font-size: 24px;
}

.input-group {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.input-group input[type="text"] {
    flex: 1 1 200px;
    min-width: 0;
    padding: 12px;
    font-size: 16px;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    transition: all 0.2s;
    box-sizing: border-box;
}

.input-group input[type="text"]:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.btn {
    padding: 12px 24px;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 16px;
    transition: background-color 0.2s;
    white-space: nowrap;
    box-sizing: border-box;
}

.btn-primary {
    background-color: #3498db;
}

.btn-primary:hover {
    background-color: #2980b9;
}

.btn-success {
    background-color: #27ae60;
}

.btn-success:hover {
    background-color: #219a52;
}

button:disabled {
    background-color: #bdc3c7;
    cursor: not-allowed;
}

.loading {
    display: none;
    text-align: center;
    margin: 20px 0;
    color: #6b7280;
}

.spinner {
    display: inline-block;
    width: 30px;
    height: 30px;
    border: 3px solid rgba(0, 0, 0, .1);
    border-radius: 50%;
    border-top-color: #3498db;
    animation: spin 1s ease-in-out infinite;
    margin-bottom: 10px;
}

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

/* === Editor Area === */
#editor-container {
    margin-top: 30px;
    display: none;
    max-width: 100%;
    overflow: hidden;
    box-sizing: border-box;
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e5e7eb;
    flex-wrap: wrap;
    gap: 10px;
}

.editor-header h2 {
    margin: 0;
    font-size: 1.2em;
    color: #374151;
}

.filename-edit-container {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    flex: 1;
    min-width: 0;
}

.filename-edit-container label {
    font-size: 1.1em;
    color: #374151;
    font-weight: 600;
    flex-shrink: 0;
}

#filename-input {
    font-size: 1.1em;
    padding: 6px 12px;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    min-width: 200px;
    max-width: 100%;
    flex: 1;
    transition: border-color 0.2s, box-shadow 0.2s;
    box-sizing: border-box;
}

#filename-input:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.15);
}

#filename-input.modified {
    border-color: #f39c12;
    background-color: #fffbf0;
}

.error {
    color: #e74c3c;
    padding: 15px;
    background-color: #fdeaea;
    border-radius: 6px;
    margin-top: 20px;
    display: none;
    border: 1px solid #f5c6cb;
}

.success-info {
    color: #27ae60;
    background-color: #e8f8f0;
    padding: 15px;
    border-radius: 6px;
    margin-top: 10px;
    display: none;
    border: 1px solid #c3e6cb;
}

/* === Metadata Editor Styles === */
#metadata-container {
    margin-top: 20px;
    padding: 20px;
    background-color: #f9fafb;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
    display: none;
    box-sizing: border-box;
    max-width: 100%;
    overflow: hidden;
}

.metadata-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e5e7eb;
}

.metadata-header h3 {
    margin: 0;
    color: #374151;
    font-size: 1.1em;
}

.metadata-table {
    width: 100%;
    max-width: 100%;
    border-collapse: collapse;
    margin-bottom: 15px;
    table-layout: fixed;
    box-sizing: border-box;
}

.metadata-table th,
.metadata-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #e5e7eb;
    overflow: hidden;
    text-overflow: ellipsis;
}

.metadata-table th {
    background-color: #f3f4f6;
    font-weight: 600;
    color: #374151;
}

.metadata-table input[type="text"] {
    width: 100%;
    padding: 8px;
    border: 1px solid #e5e7eb;
    border-radius: 4px;
    font-size: 14px;
    box-sizing: border-box;
}

.metadata-table input[type="text"]:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.btn-delete-tag {
    background-color: #e74c3c;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: background-color 0.2s;
}

.btn-delete-tag:hover {
    background-color: #c0392b;
}

.btn-add-tag {
    background-color: #3498db;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.2s;
}

.btn-add-tag:hover {
    background-color: #2980b9;
}

.add-tag-row {
    display: flex;
    gap: 10px;
    margin-top: 10px;
    flex-wrap: wrap;
    box-sizing: border-box;
}

.add-tag-row input {
    flex: 1;
    min-width: 120px;
    padding: 8px;
    border: 1px solid #e5e7eb;
    border-radius: 4px;
    box-sizing: border-box;
}

.add-tag-row input:focus {
    outline: none;
    border-color: #3498db;
    box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.metadata-info {
    font-size: 12px;
    color: #6b7280;
    margin-top: 10px;
}

.tag-badge {
    display: inline-block;
    background-color: #3498db;
    color: white;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 12px;
    margin-right: 5px;
}

.readonly-tag {
    background-color: #6b7280;
}

/* 未儲存修改指示器 */
.unsaved-indicator {
    color: #f39c12;
    margin-left: 10px;
    font-size: 12px;
}

/* === SimpleMDE 編輯器樣式 === */
.CodeMirror,
.CodeMirror-scroll,
.CodeMirror-sizer,
.editor-toolbar,
.editor-preview,
.editor-preview-side {
    max-width: 100% !important;
    box-sizing: border-box !important;
}

.CodeMirror-sizer {
    min-width: 0 !important;
}

/* === SimpleMDE Fullscreen/Side-by-Side 覆寫 === */
.editor-toolbar.fullscreen {
    left: 280px !important;
    width: calc(100% - 280px) !important;
    transition: left 0.3s ease, width 0.3s ease;
}

.CodeMirror-fullscreen {
    left: 280px !important;
    width: calc(100% - 280px) !important;
    transition: left 0.3s ease, width 0.3s ease;
}

.editor-preview-side {
    left: calc(280px + 50%) !important;
    width: calc(50% - 140px) !important;
    transition: left 0.3s ease, width 0.3s ease;
}

.CodeMirror-fullscreen ~ .editor-preview-active-side {
    left: calc(280px + 50%) !important;
    width: calc(50% - 140px) !important;
    transition: left 0.3s ease, width 0.3s ease;
}

.CodeMirror-fullscreen,
.editor-preview-side {
    overflow: auto !important;
}

/* 側邊欄折疊時的 SimpleMDE 調整 */
body.sidebar-collapsed .editor-toolbar.fullscreen {
    left: 0 !important;
    width: 100% !important;
}

body.sidebar-collapsed .CodeMirror-fullscreen {
    left: 0 !important;
    width: 100% !important;
}

body.sidebar-collapsed .editor-preview-side {
    left: 50% !important;
    width: 50% !important;
}

body.sidebar-collapsed .CodeMirror-fullscreen ~ .editor-preview-active-side {
    left: 50% !important;
    width: 50% !important;
}

/* === 響應式設計 === */
@media (max-width: 768px) {
    .sidebar-toggle-btn {
        display: none;
    }

    .sidebar {
        position: fixed;
        top: 0;
        left: -280px;
        height: 100vh;
        z-index: 1000;
        transition: left 0.3s ease;
    }

    .sidebar.active {
        left: 0;
    }

    .sidebar.collapsed {
        width: 280px !important;
        min-width: 280px !important;
    }

    .main-content {
        margin-left: 0 !important;
        padding: 15px;
    }

    .container {
        padding: 20px;
    }

    .input-group {
        flex-wrap: wrap;
    }

    .input-group input[type="text"] {
        min-width: 100%;
    }

    .editor-toolbar.fullscreen {
        left: 0 !important;
        width: 100% !important;
    }

    .CodeMirror-fullscreen {
        left: 0 !important;
        width: 100% !important;
    }

    .editor-preview-side {
        left: 50% !important;
        width: 50% !important;
    }
}
