:root {
  --bg-color: #f9fafb;
  --bg-panel: #ffffff;
  --bg-hover: #f3f4f6;
  --text-main: #111827;
  --text-muted: #6b7280;
  --border-color: #e5e7eb;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-full: 9999px;
  --icon-size: 1.25rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-color: #111827;
    --bg-panel: #1f2937;
    --bg-hover: #374151;
    --text-main: #f9fafb;
    --text-muted: #9ca3af;
    --border-color: #374151;
    --primary: #60a5fa;
    --primary-hover: #93c5fd;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.5);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.5);
  }
}

* {
  box-sizing: border-box;
}

html {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.5;
  color: var(--text-main);
  background-color: var(--bg-color);
  -webkit-font-smoothing: antialiased;
}

body {
  margin: 0;
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

svg {
  width: var(--icon-size);
  height: var(--icon-size);
  display: block;
}

/* Header & Nav */
.head {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-color);
  position: sticky;
  top: 0;
  z-index: 50;
}

.head-container {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  max-width: 1200px;
  margin: 0 auto;
  width: 100%;
}

@media (prefers-color-scheme: dark) {
  .head {
    background-color: rgba(31, 41, 55, 0.8);
  }
}

.daemon-home {
  color: var(--text-muted);
  padding: 0.5rem;
  border-radius: var(--radius-md);
  transition: all 0.2s;
}

.daemon-home:hover {
  color: var(--text-main);
  background-color: var(--bg-hover);
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  flex: 1;
  min-width: 0;
  font-size: 0.875rem;
  font-weight: 500;
  overflow: hidden;
  white-space: nowrap;
}

.breadcrumb a {
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-md);
  transition: all 0.2s;
}

.breadcrumb a:hover {
  color: var(--text-main);
  background-color: var(--bg-hover);
}

.breadcrumb b {
  color: var(--text-main);
  padding: 0.25rem 0.5rem;
  background-color: var(--bg-hover);
  border-radius: var(--radius-md);
}

.breadcrumb .separator {
  color: var(--text-muted);
  opacity: 0.5;
}

/* Toolbar Controls */
.toolbox {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.control {
  color: var(--text-muted);
  padding: 0.5rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
}

.control:hover {
  color: var(--text-main);
  background-color: var(--bg-hover);
}

.upload-file input {
  display: none;
}

.upload-file label {
  cursor: pointer;
  display: flex;
}

/* Searchbar */
.searchbar {
  display: flex;
  align-items: center;
  background-color: var(--bg-hover);
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  padding: 0.25rem 0.75rem;
  transition: all 0.2s;
  width: 240px;
}

.searchbar:focus-within {
  background-color: var(--bg-panel);
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.searchbar .icon {
  color: var(--text-muted);
  margin-right: 0.5rem;
}

.searchbar #search {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-main);
  font-size: 0.875rem;
  min-width: 0;
}

.searchbar #search::placeholder {
  color: var(--text-muted);
}

/* Right Toolbox */
.toolbox-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.login-btn,
.logout-btn,
.save-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  color: var(--text-main);
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: var(--shadow-sm);
  font-size: 0.875rem;
  font-weight: 500;
}

.login-btn:hover,
.logout-btn:hover,
.save-btn:hover {
  background-color: var(--bg-hover);
}

/* Main Content */
.main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

/* Tables */
.table-container {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

th {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-panel);
}

th a {
  color: inherit;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

th a:hover {
  color: var(--text-main);
}

td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
  font-size: 0.875rem;
}

tbody tr {
  transition: background-color 0.15s;
}

tbody tr:hover {
  background-color: var(--bg-hover);
}

tbody tr:last-child td {
  border-bottom: none;
}

/* Specific Cells */
.cell-icon {
  width: 2.5rem;
  padding-right: 0;
  color: var(--text-muted);
}

.cell-icon svg {
  width: 1.25rem;
  height: 1.25rem;
}

.cell-name a {
  color: var(--text-main);
  text-decoration: none;
  font-weight: 500;
  display: block;
}

.cell-name a:hover {
  color: var(--primary);
  text-decoration: underline;
}

.cell-mtime {
  color: var(--text-muted);
  white-space: nowrap;
  width: 11rem;
}

.cell-size {
  color: var(--text-muted);
  white-space: nowrap;
  width: 6rem;
  text-align: right;
}

/* Actions in Table */
.cell-actions {
  width: 10rem;
  padding-left: 0;
}

.actions-wrapper {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.25rem;
  opacity: 0;
  transition: opacity 0.2s;
}

tbody tr:hover .actions-wrapper {
  opacity: 1;
}

.action-btn {
  color: var(--text-muted);
  padding: 0.4rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
}

.action-btn:hover {
  color: var(--text-main);
  background-color: var(--border-color);
}

.action-btn a {
  color: inherit;
  display: flex;
}

/* Empty State */
.empty-folder {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--text-muted);
  background-color: var(--bg-panel);
  border: 1px dashed var(--border-color);
  border-radius: var(--radius-lg);
  margin-bottom: 1rem;
}

/* Editor */
.editor {
  width: 100%;
  height: calc(100vh - 150px);
  padding: 1.5rem;
  background-color: var(--bg-panel);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.875rem;
  line-height: 1.5;
  resize: none;
  outline: none;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.editor:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.not-editable {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
  background-color: var(--bg-panel);
  border: 1px dashed var(--border-color);
  border-radius: var(--radius-lg);
}

/* ========== Editor Page: Split Layout ========== */

.split-layout {
  display: flex;
  height: calc(100vh - 120px);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--bg-panel);
}

/* File Tree Sidebar */
.file-tree {
  width: 240px;
  min-width: 160px;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  background: var(--bg-color);
  border-right: 1px solid var(--border-color);
  overflow: hidden;
  transition: width 0.2s ease, min-width 0.2s ease;
}

.file-tree.collapsed {
  width: 0;
  min-width: 0;
  border-right: none;
}

.file-tree-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0.75rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-panel);
}

.file-tree-collapse {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background 0.15s;
}

.file-tree-collapse:hover {
  background: var(--bg-hover);
}

.file-tree-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.25rem 0;
}

/* Tree Nodes */
.tree-node {
  display: flex;
  align-items: center;
  padding: 3px 8px 3px 0;
  cursor: pointer;
  font-size: 0.8rem;
  color: var(--text-main);
  white-space: nowrap;
  transition: background 0.1s;
}

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

.tree-node.active {
  background: rgba(59, 130, 246, 0.08);
  color: var(--primary);
}

.tree-node-icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 4px;
}

.tree-node-icon svg {
  width: 14px;
  height: 14px;
}

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

.tree-children {
  overflow: hidden;
}

.tree-children.collapsed {
  display: none;
}

.tree-indent {
  flex-shrink: 0;
  width: 16px;
}

/* Resizer */
.file-tree-resizer {
  width: 4px;
  cursor: col-resize;
  background: transparent;
  transition: background 0.15s;
  flex-shrink: 0;
}

.file-tree-resizer:hover,
.file-tree-resizer.dragging {
  background: var(--primary);
}

/* Code Panel */
.code-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.code-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.4rem 0.75rem;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-panel);
  gap: 0.5rem;
}

.code-toolbar-left {
  display: flex;
  gap: 2px;
  background: var(--bg-hover);
  border-radius: 6px;
  padding: 2px;
}

.mode-btn {
  padding: 4px 14px;
  font-size: 0.8rem;
  font-weight: 500;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.15s;
}

.mode-btn.active {
  background: var(--bg-panel);
  color: var(--text-main);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

#btnSidebar svg {
  display: block;
}

#btnSidebar.active {
  background: var(--bg-panel);
  color: var(--primary);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.code-toolbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.code-toolbar-right .download-file,
.code-toolbar-right .move-file,
.code-toolbar-right .delete-file {
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}

.code-toolbar-right .download-file:hover,
.code-toolbar-right .move-file:hover,
.code-toolbar-right .delete-file:hover {
  color: var(--text-main);
  background: var(--bg-hover);
}

.code-toolbar-right svg {
  width: 16px;
  height: 16px;
}

.code-toolbar-right .save-btn {
  display: flex;
  align-items: center;
  padding: 4px 8px;
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: all 0.15s;
}

.code-toolbar-right .save-btn:hover {
  opacity: 0.9;
}

.code-toolbar-right .save-btn svg {
  width: 14px;
  height: 14px;
}

/* Code Content Area */
.code-content {
  flex: 1;
  overflow: auto;
  position: relative;
}

/* ─── Code Viewer (mdocs-style) ─────────────────────── */

.code-viewer {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg-panel);
}

.code-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--bg-hover);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.code-lang-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

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

.code-action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.code-action-btn:hover {
  background: var(--border-color);
  color: var(--text-main);
}

.code-action-btn.copied {
  border-color: #22c55e;
}

.code-viewer-body {
  flex: 1;
  overflow: auto;
  padding: 4px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  tab-size: 4;
}

.code-line {
  display: flex;
  min-height: 1.6em;
  padding-right: 16px;
  white-space: pre;
}

.code-line:hover {
  background: var(--bg-hover);
}

.code-line-num {
  flex-shrink: 0;
  text-align: right;
  padding-right: 12px;
  margin-right: 12px;
  color: var(--text-muted);
  opacity: 0.5;
  user-select: none;
  border-right: 1px solid var(--border-color);
  font-family: inherit;
  font-size: inherit;
}

.code-line-content {
  flex: 1;
  min-width: 0;
}

/* Editor textarea (in edit mode) */
.code-content .editor {
  width: 100%;
  height: 100%;
  padding: 1rem 1.25rem;
  background-color: var(--bg-panel);
  color: var(--text-main);
  border: none;
  border-radius: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  tab-size: 4;
  resize: none;
  outline: none;
  box-shadow: none;
}

/* Responsive */
@media (max-width: 768px) {
  .head-container {
    flex-wrap: wrap;
    padding: 0.75rem 1rem;
  }
  
  .searchbar {
    width: 100%;
    order: 4;
    margin-top: 0.5rem;
  }

  .toolbox-right {
    margin-left: auto;
  }

  .cell-mtime, .cell-size {
    display: none;
  }

  .actions-wrapper {
    opacity: 1; /* Always show actions on mobile */
  }

  .main {
    padding: 1rem;
  }
}

/* ─── Markdown Viewer (reading mode) ─────────────────── */

.md-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--bg-hover);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.md-lang-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.md-viewer-body {
  flex: 1;
  overflow: auto;
  padding: 32px 48px 48px;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
}

/* Typography */
.md-viewer-body h1,
.md-viewer-body h2,
.md-viewer-body h3,
.md-viewer-body h4,
.md-viewer-body h5,
.md-viewer-body h6 {
  margin-top: 24px;
  margin-bottom: 16px;
  font-weight: 600;
  line-height: 1.25;
  color: var(--text-main);
}

.md-viewer-body h1 {
  font-size: 1.75em;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 8px;
}

.md-viewer-body h2 {
  font-size: 1.4em;
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 6px;
}

.md-viewer-body h3 { font-size: 1.2em; }
.md-viewer-body h4 { font-size: 1.05em; }

.md-viewer-body p {
  margin-bottom: 1rem;
  line-height: 1.7;
  color: var(--text-main);
}

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

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

/* Lists */
.md-viewer-body ul,
.md-viewer-body ol {
  margin-bottom: 1rem;
  padding-left: 2rem;
  line-height: 1.7;
}

.md-viewer-body li {
  margin-bottom: 0.25rem;
}

.md-viewer-body li > ul,
.md-viewer-body li > ol {
  margin-bottom: 0;
}

/* Blockquote */
.md-viewer-body blockquote {
  border-left: 4px solid var(--border-color);
  padding: 0 16px;
  margin: 0 0 1rem;
  color: var(--text-muted);
}

/* Code inline */
.md-viewer-body code {
  background-color: var(--bg-hover);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.88em;
  color: var(--text-main);
}

/* Code blocks */
.md-viewer-body pre.md-code-block {
  margin: 16px 0;
  padding: 16px;
  border-radius: 8px;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  overflow-x: auto;
}

.md-viewer-body pre.md-code-block code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  font-size: 0.82rem;
  line-height: 1.6;
  color: inherit;
}

/* Tables */
.md-viewer-body .table-wrapper {
  overflow-x: auto;
  margin: 16px 0;
}

.md-viewer-body table {
  border-collapse: collapse;
  width: auto;
  max-width: 100%;
  font-size: 0.88rem;
}

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

.md-viewer-body th {
  background-color: var(--bg-hover);
  font-weight: 600;
  color: var(--text-main);
}

.md-viewer-body tr:nth-child(even) td {
  background-color: var(--bg-color);
}

/* Images */
.md-viewer-body img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
}

/* Horizontal rule */
.md-viewer-body hr {
  border: 0;
  border-top: 1px solid var(--border-color);
  margin: 24px 0;
}

/* Inline task checkboxes */
.md-viewer-body input[type="checkbox"] {
  margin-right: 6px;
  vertical-align: middle;
}

/* ========== Viewer Modal ========== */

.viewer-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
}

.viewer-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.15);
}

.viewer-modal-content {
  position: absolute;
  top: 61px;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-color);
}

.viewer-modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 10;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  color: var(--text-muted);
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.viewer-modal-close:hover {
  color: var(--text-main);
  background: var(--bg-hover);
}

.viewer-modal-close svg {
  width: 18px;
  height: 18px;
}

.viewer-modal .split-layout {
  height: 100%;
  border-radius: 0;
  border: none;
}