:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1e242c;
  --muted: #66717f;
  --line: #d9dee7;
  --accent: #1769aa;
  --danger: #b42318;
  --ok: #13795b;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.mobile-only {
  display: none;
}

button, input, select, textarea {
  font: inherit;
}

button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
}

button:disabled {
  opacity: .55;
  cursor: not-allowed;
}

.ghost {
  background: #fff;
  color: var(--text);
  border-color: var(--line);
}

.danger {
  background: #fff;
  color: var(--danger);
  border-color: #efb5ad;
}

.icon-button {
  width: 34px;
  height: 34px;
  padding: 0;
}

.mount-btn {
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 16px;
  flex-shrink: 0;
}

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.login-panel {
  width: min(380px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 24px;
  display: grid;
  gap: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
}

.brand-logo {
  flex-shrink: 0;
  border-radius: 9px;
}

.login-panel h1 {
  margin: 0;
  font-size: 24px;
}

.toolbar-logo {
  flex-shrink: 0;
  border-radius: 6px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
}

input, select, textarea {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
}

.shell {
  min-height: 100vh;
  height: 100vh;
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
}

.shell.collapsed {
  grid-template-columns: 0 1fr;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.shell.collapsed .sidebar {
  border-right: 0;
}

.toolbar, .detail-header {
  min-height: 58px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}

.toolbar {
  position: relative;
}

.toolbar strong {
  flex: 1;
}

.sidebar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.meta-panel {
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #fafbfc;
  padding-top: 4px;
}

#toggle-meta[aria-expanded="true"] {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.selectors {
  position: relative;
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 8px 16px 8px;
}

.mount-picker {
  position: relative;
  min-width: 0;
  flex: 1;
}

.mount-select-button {
  position: relative;
  width: 100%;
  min-height: 56px;
  display: grid;
  gap: 2px;
  padding: 8px 34px 8px 10px;
  border-color: var(--line);
  background: #fff;
  color: var(--text);
  text-align: left;
}

.mount-select-name,
.mount-option-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.mount-select-path,
.mount-option-path {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.mount-select-arrow {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
}

.mount-options {
  position: absolute;
  z-index: 25;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 280px;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 14px 34px rgba(30, 36, 44, .18);
}

.mount-option {
  width: 100%;
  display: grid;
  gap: 2px;
  padding: 9px 10px;
  border: 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--text);
  text-align: left;
}

.mount-option:last-child {
  border-bottom: 0;
}

.mount-option:hover,
.mount-option.active {
  background: #eef5fb;
}

.mount-edit-btn {
  width: 38px;
  min-height: 56px;
  padding: 0;
  flex-shrink: 0;
}

.search {
  margin: 0 16px 10px;
}


.meta-row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  padding: 0 16px 8px;
  font-size: 12px;
}

.folder-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 8px;
  font-size: 12px;
  color: var(--muted);
}

.folder-path {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.feedback, .status, .error {
  color: var(--danger);
}

.feedback {
  padding: 0 16px 8px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: start center;
  padding: 72px 16px 16px;
  background: rgba(30, 36, 44, .35);
}

.modal {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 50px rgba(30, 36, 44, .22);
}

.modal-form {
  display: grid;
  gap: 12px;
  padding: 16px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-header h2 {
  margin: 0;
  font-size: 18px;
}

.form-note {
  margin: -4px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.form-error {
  min-height: 20px;
  margin: 0;
}

.readonly-path {
  display: grid;
  gap: 4px;
  color: var(--muted);
}

.readonly-path strong {
  color: var(--text);
  font-weight: 500;
  word-break: break-all;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.modal-actions button {
  min-width: 72px;
}

.doc-list {
  list-style: none;
  padding: 0;
  margin: 0;
  overflow: auto;
}

.doc-item {
  border-top: 1px solid var(--line);
  padding: 11px 16px;
  cursor: pointer;
}

.doc-item:hover, .doc-item.active {
  background: #eef5fb;
}

.doc-item strong {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-item span, #doc-path {
  color: var(--muted);
  font-size: 12px;
  word-break: break-all;
}

.detail {
  min-width: 0;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
}

.detail-header {
  background: var(--panel);
  justify-content: space-between;
}

.document-heading {
  min-width: 0;
  flex: 1;
}

.detail-header h2 {
  margin: 0;
  font-size: 18px;
}

.detail-header p {
  margin: 2px 0 0;
}

.actions {
  position: relative;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.mode-switch,
.secondary-actions {
  display: flex;
  gap: 8px;
}

.mode-switch .active {
  background: #eef5fb;
  color: var(--accent);
  border-color: #9fc4e2;
}

.status {
  min-height: 28px;
  padding: 6px 18px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.empty {
  margin: auto;
  color: var(--muted);
}

.preview-wrap {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  overflow: hidden;
  position: relative;
}

.detail.toc-panel-open .preview-wrap {
  grid-template-columns: minmax(0, 980px) minmax(220px, 260px);
}

.preview {
  overflow: auto;
  min-height: 0;
  padding: 24px 32px 80px;
  max-width: 980px;
  width: 100%;
}

.preview-wrap > .preview,
.split .preview {
  height: 100%;
  max-width: none;
}

.preview h1, .preview h2, .preview h3,
.preview h4, .preview h5, .preview h6 {
  line-height: 1.25;
  margin-top: 1.4em;
  margin-bottom: .4em;
}

.preview h1 { font-size: 1.9em; border-bottom: 1px solid var(--line); padding-bottom: .3em; }
.preview h2 { font-size: 1.5em; border-bottom: 1px solid var(--line); padding-bottom: .2em; }
.preview h3 { font-size: 1.2em; }

.preview p { margin: .6em 0 .8em; }

.preview p,
.preview li,
.preview td,
.preview th {
  overflow-wrap: anywhere;
}

.preview a { color: var(--accent); text-decoration: none; }
.preview a:hover { text-decoration: underline; }

.preview ul, .preview ol {
  padding-left: 1.8em;
  margin: .4em 0 .8em;
}

.preview li { margin: .25em 0; }

.preview li input[type="checkbox"] {
  margin-right: .4em;
}

.preview blockquote {
  margin: .8em 0;
  padding: .4em 1em;
  border-left: 4px solid var(--line);
  color: var(--muted);
  background: #f9fbfd;
  border-radius: 0 4px 4px 0;
}

.preview blockquote p { margin: .2em 0; }

.preview pre, .diff {
  background: #101820;
  color: #e8eef6;
  border-radius: 6px;
  padding: 14px;
  overflow: auto;
}

.preview pre code {
  background: none;
  padding: 0;
  font-size: .9em;
}

.preview.json-preview {
  padding: 18px 24px 80px;
  max-width: none;
}

.json-viewer {
  margin: 0;
  min-height: 100%;
  white-space: pre;
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.preview .json-viewer code {
  display: block;
  background: none;
  color: inherit;
  padding: 0;
  font: inherit;
}

.json-error {
  margin: 0 0 12px;
  padding: 10px 12px;
  border: 1px solid #efb5ad;
  border-radius: 6px;
  background: #fff4f2;
  color: var(--danger);
}

.json-viewer-invalid {
  border: 1px solid #efb5ad;
}

.json-key { color: #8ad5ff; }
.json-string { color: #a7e3a0; }
.json-number { color: #ffcf70; }
.json-boolean { color: #ff9f9f; }
.json-null { color: #c9b6ff; }

.preview code {
  background: #eef1f6;
  color: #c7254e;
  padding: .15em .35em;
  border-radius: 4px;
  font: 87.5%/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.preview table {
  border-collapse: collapse;
  width: 100%;
  margin: .8em 0;
  font-size: .95em;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
}

.table-scroll table {
  width: max-content;
  min-width: 100%;
}

.preview th, .preview td {
  border: 1px solid var(--line);
  padding: 7px 12px;
  text-align: left;
}

.preview th {
  background: #f0f4f8;
  font-weight: 600;
}

.preview tr:nth-child(even) td {
  background: #f9fbfd;
}

.preview hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 1.5em 0;
}

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

.toc-panel-toggle {
  position: absolute;
  top: 16px;
  right: 24px;
  z-index: 20;
  box-shadow: 0 8px 24px rgba(30, 36, 44, .14);
}

.toc-panel {
  align-self: start;
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 132px);
  margin: 24px 24px 24px 0;
  padding: 4px 0 12px 14px;
  overflow: auto;
  border-left: 1px solid var(--line);
}

.toc-panel-header {
  margin: 0 0 8px 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.toc-title {
  flex: 1;
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
}

.toc-panel-close {
  padding: 4px 8px;
  font-size: 12px;
}

.toc-list {
  display: grid;
  gap: 2px;
}

.toc-link {
  width: 100%;
  min-height: 28px;
  border: 0;
  border-left: 3px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  padding: 5px 8px;
  text-align: left;
  line-height: 1.35;
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toc-link:hover,
.toc-link.active {
  background: #eef5fb;
  color: var(--text);
  border-left-color: var(--accent);
}

.toc-level-3 {
  padding-left: 20px;
}

.toc-float {
  display: none;
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 30;
  box-shadow: 0 8px 24px rgba(30, 36, 44, .18);
}

.detail.toc-ready.toc-editing .toc-float {
  display: block;
}

.toc-backdrop {
  position: fixed;
  inset: 0;
  z-index: 39;
  background: rgba(16, 24, 32, .28);
}

.toc-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  width: min(360px, calc(100vw - 32px));
  background: var(--panel);
  border-left: 1px solid var(--line);
  box-shadow: -12px 0 32px rgba(30, 36, 44, .18);
  display: flex;
  flex-direction: column;
}

.toc-drawer-header {
  min-height: 56px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.toc-drawer-header h2 {
  margin: 0;
  font-size: 16px;
}

.toc-drawer .toc-list {
  overflow: auto;
  padding: 10px 14px 18px;
}

.split {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(280px, 1fr);
}

#editor {
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  resize: none;
  padding: 18px;
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.diff {
  margin: 18px;
  flex: 1;
}

@media (max-width: 1120px) {
  .preview-wrap {
    grid-template-columns: minmax(0, 1fr);
  }
  .toc-panel,
  .toc-panel-toggle {
    display: none;
  }
  .detail.toc-ready.toc-previewing .toc-float {
    display: block;
  }
}

@media (max-width: 900px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  button,
  input,
  select {
    min-height: 44px;
  }

  .mobile-only {
    display: inline-grid;
    place-items: center;
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
  }

  .shell {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    grid-template-columns: 1fr;
  }

  .sidebar {
    width: 100%;
    height: 100%;
    min-height: 0;
    border-right: 0;
    border-bottom: 0;
  }

  .shell.mobile-detail-visible .sidebar {
    display: none;
  }

  .shell:not(.mobile-detail-visible) .detail {
    display: none;
  }

  .shell.collapsed:not(.mobile-detail-visible) .sidebar {
    display: flex;
  }

  .toolbar {
    min-height: 60px;
    gap: 6px;
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  }

  .toolbar #collapse-sidebar,
  .toolbar-logo {
    display: none;
  }

  .toolbar strong {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sidebar-actions {
    display: none;
    position: absolute;
    z-index: 35;
    top: calc(100% - 2px);
    right: 12px;
    width: 164px;
    padding: 6px;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: 0 14px 34px rgba(30, 36, 44, .18);
  }

  .sidebar-actions.open {
    display: flex;
  }

  .sidebar-actions button,
  .sidebar-actions .mount-btn {
    width: 100%;
    min-height: 44px;
    text-align: left;
  }

  .sidebar-actions #add-mount::after { content: " 新增挂载点"; }
  .sidebar-actions #remove-mount::after { content: " 移除挂载点"; }
  .sidebar-actions #toggle-meta::after { content: " 挂载点与目录"; }

  .search {
    margin: 8px 12px;
  }

  .doc-list {
    flex: 1;
    min-height: 0;
    overscroll-behavior: contain;
  }

  .doc-item {
    min-height: 56px;
    padding: 10px 14px;
  }

  .detail {
    height: 100%;
    min-height: 0;
    overflow: hidden;
    background: var(--panel);
  }

  .detail-header {
    min-height: 0;
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    gap: 8px;
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  }

  #expand-sidebar {
    display: none !important;
  }

  .document-heading h2 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 16px;
  }

  .document-heading p {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .actions {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
  }

  .mode-switch {
    gap: 0;
  }

  .mode-switch button {
    min-height: 40px;
    padding: 7px 10px;
    border-radius: 0;
  }

  .mode-switch button:first-child {
    border-radius: 6px 0 0 6px;
  }

  .mode-switch button:last-child {
    margin-left: -1px;
    border-radius: 0 6px 6px 0;
  }

  #save {
    min-height: 40px;
    padding-inline: 10px;
  }

  #save:disabled {
    display: none;
  }

  .secondary-actions {
    display: none;
    position: absolute;
    z-index: 35;
    top: calc(100% + 6px);
    right: 0;
    width: 132px;
    padding: 6px;
    flex-direction: column;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    box-shadow: 0 14px 34px rgba(30, 36, 44, .18);
  }

  .secondary-actions.open {
    display: flex;
  }

  .secondary-actions button {
    width: 100%;
    text-align: left;
  }

  .status {
    min-height: 0;
    padding: 5px 14px;
    font-size: 12px;
  }

  .split {
    grid-template-columns: 1fr;
  }

  .split .preview {
    display: none;
  }

  .preview {
    padding: 16px 16px calc(72px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
  }

  #editor {
    min-height: 0;
    height: 100%;
    border-right: 0;
    border-bottom: 0;
    padding: 16px 14px calc(24px + env(safe-area-inset-bottom));
  }

  .diff {
    min-height: 0;
    margin: 12px;
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
    overflow: auto;
  }

  .toc-float {
    right: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom));
    min-height: 44px;
  }

  .detail.toc-ready.toc-editing .toc-float {
    display: none;
  }

  .toc-drawer {
    top: auto;
    left: 0;
    right: 0;
    width: auto;
    max-height: 72vh;
    max-height: 72dvh;
    padding-bottom: env(safe-area-inset-bottom);
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .modal-backdrop {
    overflow: auto;
    padding: calc(16px + env(safe-area-inset-top)) 12px calc(16px + env(safe-area-inset-bottom));
  }

  .modal {
    margin: auto;
  }
}

@media (max-width: 520px) {
  .detail-header {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
  }

  .mode-switch {
    margin-right: auto;
  }
}
