/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-width: 280px;
  --topbar-height: 48px;
  --bg: #f5f5f5;
  --bg-card: #ffffff;
  --bg-sidebar: #fafafa;
  --border: #e0e0e0;
  --text: #333333;
  --text-secondary: #888888;
  --text-muted: #aaaaaa;
  --primary: #1976d2;
  --primary-hover: #1565c0;
  --primary-light: #e3f2fd;
  --danger: #d32f2f;
  --danger-hover: #b71c1c;
  --success: #388e3c;
  --warning: #f57c00;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.12);
  --transition: 0.2s ease;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  font-size: 14px;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ===== Topbar ===== */
#topbar {
  height: var(--topbar-height);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  flex-shrink: 0;
  z-index: 10;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.logo {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
}

.current-path {
  font-size: 12px;
  color: var(--text-secondary);
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 4px;
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.search-box {
  display: flex;
  gap: 4px;
}

#search-input {
  width: 180px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
  transition: border var(--transition);
}

#search-input:focus {
  border-color: var(--primary);
}

#search-mode {
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
  background: var(--bg-card);
  outline: none;
}

/* ===== Buttons ===== */
.btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn:hover {
  background: var(--bg);
  border-color: #ccc;
}

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

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

.btn-sm { padding: 4px 8px; font-size: 12px; }
.btn-xs { padding: 2px 6px; font-size: 11px; }

/* ===== Main Layout ===== */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.tree-container {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

/* ===== Tree ===== */
.tree-node {
  user-select: none;
  font-size: 13px;
}

.tree-node-content {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 12px;
  cursor: pointer;
  transition: background var(--transition);
  white-space: nowrap;
}

.tree-node-content:hover {
  background: var(--border);
}

.tree-node-content.active {
  background: var(--primary-light);
  color: var(--primary);
}

.tree-node-content .icon {
  width: 16px;
  text-align: center;
  flex-shrink: 0;
  font-size: 12px;
}

.tree-node-content .name {
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-toggle {
  width: 16px;
  text-align: center;
  flex-shrink: 0;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform var(--transition);
}

.tree-toggle.expanded {
  transform: rotate(90deg);
}

.tree-children {
  display: none;
}

.tree-children.open {
  display: block;
}

/* ===== Content Area ===== */
#content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.view {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.view.hidden {
  display: none;
}

/* ===== File List ===== */
.file-list-header {
  display: flex;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.file-list-body {
  flex: 1;
  overflow-y: auto;
  padding: 0;
}

.file-item {
  display: flex;
  align-items: center;
  padding: 6px 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background var(--transition);
}

.file-item:hover {
  background: var(--primary-light);
}

.file-item .col-name {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.file-item .col-name .file-icon {
  font-size: 16px;
  flex-shrink: 0;
}

.file-item .col-name .file-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-item .col-size {
  width: 80px;
  text-align: right;
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.file-item .col-time {
  width: 160px;
  text-align: right;
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.file-item .col-actions {
  width: 140px;
  text-align: right;
  flex-shrink: 0;
  gap: 4px;
}

.file-item .col-actions .btn {
  visibility: hidden;
}

.file-item:hover .col-actions .btn {
  visibility: visible;
}

/* ===== Editor ===== */
.editor-toolbar {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  gap: 12px;
  flex-shrink: 0;
}

.editor-filename {
  font-weight: 600;
  font-size: 14px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.editor-status {
  font-size: 12px;
  color: var(--text-secondary);
}

.editor-actions {
  display: flex;
  gap: 4px;
}

#editor-textarea {
  flex: 1;
  width: 100%;
  border: none;
  resize: none;
  padding: 16px;
  font-family: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.6;
  outline: none;
  background: var(--bg-card);
  color: var(--text);
  tab-size: 2;
}

#editor-textarea:focus {
  background: #fefefe;
}

/* ===== Loading ===== */
.loading {
  padding: 24px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

.empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-card);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  min-width: 400px;
  max-width: 500px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

.modal-body {
  padding: 16px;
}

.modal-body label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--text-secondary);
}

.modal-body input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  margin-bottom: 8px;
}

.modal-body input:focus {
  border-color: var(--primary);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

/* ===== Toast ===== */
#toast-container {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease;
  max-width: 360px;
}

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

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #aaa; }

/* ===== Search Results ===== */
.search-results-header {
  padding: 8px 16px;
  background: #fff8e1;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-result-item {
  display: flex;
  align-items: center;
  padding: 6px 16px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background var(--transition);
  gap: 8px;
}

.search-result-item:hover {
  background: #fff8e1;
}

.search-result-item .path {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: auto;
}

.search-result-item .match-line {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 400px;
  font-family: monospace;
}

.col-matched {
  width: 60px;
  text-align: right;
  font-size: 12px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

/* ===== Image Preview Overlay ===== */
.image-preview-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  flex-direction: column;
  z-index: 200;
}

.image-preview-overlay.hidden { display: none; }

.image-preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  flex-shrink: 0;
}

.preview-filename {
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.preview-actions .btn {
  background: rgba(255,255,255,0.15);
  color: #fff;
  border-color: rgba(255,255,255,0.3);
  text-decoration: none;
}

.preview-actions .btn:hover {
  background: rgba(255,255,255,0.25);
}

.image-preview-body {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow: auto;
}

.image-preview-body img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.5);
}

/* ===== Markdown Preview ===== */
.md-preview-toolbar {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  gap: 12px;
  flex-shrink: 0;
}

.md-preview-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px 32px;
  background: var(--bg-card);
  line-height: 1.7;
  font-size: 15px;
  color: #24292e;
}

.md-preview-content h1 { font-size: 1.8em; border-bottom: 1px solid #eaecef; padding-bottom: 8px; margin: 24px 0 16px; font-weight: 600; }
.md-preview-content h2 { font-size: 1.5em; border-bottom: 1px solid #eaecef; padding-bottom: 6px; margin: 22px 0 14px; font-weight: 600; }
.md-preview-content h3 { font-size: 1.25em; margin: 18px 0 12px; font-weight: 600; }
.md-preview-content h4 { font-size: 1em; margin: 16px 0 10px; font-weight: 600; }
.md-preview-content h5, .md-preview-content h6 { font-size: 0.9em; margin: 14px 0 8px; font-weight: 600; color: #555; }

.md-preview-content p { margin: 0 0 16px; }
.md-preview-content a { color: var(--primary); text-decoration: none; }
.md-preview-content a:hover { text-decoration: underline; }

.md-preview-content ul, .md-preview-content ol { margin: 0 0 16px; padding-left: 28px; }
.md-preview-content li { margin: 4px 0; }
.md-preview-content li > ul, .md-preview-content li > ol { margin: 2px 0; }

.md-preview-content blockquote {
  margin: 0 0 16px;
  padding: 8px 16px;
  border-left: 4px solid var(--primary);
  background: #f6f8fa;
  color: #555;
}
.md-preview-content blockquote p:last-child { margin-bottom: 0; }

.md-preview-content code {
  font-family: 'Cascadia Code', 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
  font-size: 0.88em;
  padding: 2px 6px;
  background: #f0f0f0;
  border-radius: 3px;
}

.md-preview-content pre {
  margin: 0 0 16px;
  padding: 16px;
  background: #f6f8fa;
  border-radius: var(--radius);
  overflow-x: auto;
}
.md-preview-content pre code {
  padding: 0;
  background: none;
  font-size: 0.85em;
  line-height: 1.5;
}

.md-preview-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 16px;
  font-size: 14px;
}
.md-preview-content th, .md-preview-content td {
  padding: 8px 12px;
  border: 1px solid #dfe2e5;
  text-align: left;
}
.md-preview-content th {
  background: #f6f8fa;
  font-weight: 600;
}
.md-preview-content tr:nth-child(even) { background: #fafbfc; }

.md-preview-content hr {
  border: none;
  border-top: 1px solid #eaecef;
  margin: 24px 0;
}

.md-preview-content img {
  max-width: 100%;
  border-radius: 4px;
}

.md-preview-content strong { font-weight: 600; }
.md-preview-content em { font-style: italic; }

.md-preview-content > :first-child { margin-top: 0; }
.md-preview-content > :last-child { margin-bottom: 0; }

/* ===== Mermaid diagrams ===== */
#md-preview-content:not(.show-source) pre.mermaid {
  margin: 0 0 20px;
  padding: 0;
  background: transparent;
  border-radius: 0;
  text-align: center;
  overflow-x: auto;
}
#md-preview-content:not(.show-source) pre.mermaid svg {
  max-width: 100%;
  height: auto;
}
#md-toggle-source-btn {
  margin-left: 4px;
  padding-left: 12px;
  border-left: 1px solid var(--border);
  border-radius: 0;
}
