:root {
  color-scheme: dark;
  --mb-bg: #0b0f14;
  --mb-fg: #e6e8ee;
  --mb-muted: #a0a6b3;
  --mb-border: #2a3240;
  --mb-surface: #121826;
  --mb-surface-2: #0f1520;
  --mb-hover: #1a2333;
}

:root[data-theme="light"] {
  color-scheme: light;
  --mb-bg: #ffffff;
  --mb-fg: #111827;
  --mb-muted: #6b7280;
  --mb-border: #e5e7eb;
  --mb-surface: #f3f4f6;
  --mb-surface-2: #ffffff;
  --mb-hover: #e5e7eb;
}

html, body {
  height: 100%;
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--mb-bg);
  color: var(--mb-fg);
}

.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100vh;
}

/* All Notes: full-width list */
.page-notes .app {
  grid-template-columns: 1fr;
}

.page-notes .sidebar {
  border-right: none;
}

.sidebar {
  border-right: 1px solid var(--mb-border);
  display: flex;
  flex-direction: column;
  background: var(--mb-surface-2);
}

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

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

.admin-link {
  font-size: 12px;
  color: var(--mb-muted);
  text-decoration: none;
  padding: 6px 8px;
  border-radius: 6px;
}

.admin-link:hover {
  background: var(--mb-hover);
  color: var(--mb-fg);
}

.brand {
  font-weight: 600;
}

.note-list {
  list-style: none;
  padding: 8px;
  margin: 0;
  overflow: auto;
  flex: 1;
}

.btn {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 8px 10px;
  background: var(--mb-surface);
  color: inherit;
  cursor: pointer;
}

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

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.note-item {
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
}

.note-item:hover {
  background: var(--mb-hover);
}

.note-item.active {
  border-color: var(--mb-border);
  background: var(--mb-hover);
}

.note-item.deleted {
  opacity: 0.6;
}

.folder {
  margin-top: 10px;
}

.folder-head {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  font-weight: 600;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  gap: 8px;
}

.folder-head:hover {
  background: var(--mb-hover);
}

.folder-toggle {
  appearance: none;
  border: none;
  background: transparent;
  padding: 0;
  margin: 0;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
  text-align: left;
}

.folder-actions {
  display: flex;
  gap: 6px;
}

.folder-action {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 4px 8px;
  background: var(--mb-surface);
  color: inherit;
  cursor: pointer;
  font-weight: 500;
}

.folder-action:hover {
  background: var(--mb-hover);
}

.folder-notes {
  list-style: none;
  padding-left: 10px;
  margin: 6px 0 0;
  display: grid;
  gap: 2px;
}

.folder-empty {
  padding-left: 10px;
  margin: 6px 0 0;
  opacity: 0.7;
  font-size: 13px;
}

.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  background: var(--mb-bg);
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  padding: 12px;
  border-bottom: 1px solid var(--mb-border);
  align-items: center;
}

.title-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.title-row .title {
  flex: 1;
}

.title {
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  background: var(--mb-surface-2);
  color: inherit;
}

.status {
  font-size: 12px;
  color: var(--mb-muted);
  white-space: nowrap;
}

.mode-pill {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--mb-border);
  background: var(--mb-surface);
  color: var(--mb-muted);
}

body.is-editing .mode-pill {
  color: var(--mb-fg);
}

body.is-editing .editor {
  outline: 2px solid var(--mb-border);
  outline-offset: -2px;
}

.editor {
  overflow: auto;
}

/* CodeMirror base */
.cm-editor {
  height: 100%;
}

.comments {
  border-top: 1px solid var(--mb-border);
  padding: 12px;
  display: grid;
  gap: 10px;
}

.backlinks {
  border-top: 1px solid var(--mb-border);
  padding: 12px;
  display: grid;
  gap: 8px;
}

.backlinks-title {
  font-weight: 600;
}

.backlinks-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.backlink-pill {
  border: 1px solid var(--mb-border);
  border-radius: 999px;
  padding: 6px 10px;
  background: var(--mb-surface);
  color: inherit;
  cursor: pointer;
}

.backlink-pill:hover {
  background: var(--mb-hover);
}

.comments-title {
  font-weight: 600;
}

.comments-list {
  max-height: 180px;
  overflow: auto;
  display: grid;
  gap: 8px;
}

.comment {
  border: 1px solid var(--mb-border);
  border-radius: 8px;
  padding: 8px 10px;
}

.comment-head {
  font-size: 12px;
  color: var(--mb-muted);
  margin-bottom: 4px;
}

.comment-body {
  white-space: pre-wrap;
}

.comment-compose {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.comment-compose input,
.comment-compose textarea {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 8px 10px;
  font: inherit;
  background: var(--mb-surface-2);
  color: inherit;
}

/* CodeMirror tuning for dark UI */
.cm-editor {
  background: transparent;
  color: inherit;
}

.cm-editor .cm-content {
  caret-color: var(--mb-fg) !important;
}

.cm-editor.cm-focused .cm-cursor {
  border-left: 2px solid var(--mb-fg) !important;
}

.cm-editor .cm-dropCursor {
  border-left: 2px solid var(--mb-fg) !important;
}

.comment-compose textarea {
  resize: vertical;
  min-height: 38px;
}

@media (max-width: 820px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
    min-height: 100vh;
  }

  /* Editor page: no sidebar on mobile (content-only) */
  .page-editor .sidebar {
    display: none !important;
    pointer-events: none;
    visibility: hidden;
  }

  .page-editor .app {
    grid-template-rows: 1fr;
  }


  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--mb-border);
  }

  .sidebar-top {
    flex-wrap: wrap;
    gap: 10px;
  }

  .sidebar-actions {
    flex-wrap: wrap;
  }

  .note-list {
    max-height: 34vh;
  }

  .page-notes .note-list {
    max-height: none;
  }

  .page-notes .app {
    height: 100vh;
    min-height: 100vh;
  }

  .main {
    height: auto;
    min-height: 60vh;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: start;
  }

  .title-row {
    flex-wrap: wrap;
  }

  .title-row .title {
    min-width: 180px;
  }

  .status {
    white-space: normal;
  }

  .editor {
    min-height: 48vh;
  }

  .cm-editor {
    height: auto;
    min-height: 48vh;
  }

  .comment-compose {
    grid-template-columns: 1fr;
  }
}

.auth {
  max-width: 420px;
  margin: 80px auto;
  padding: 16px;
}

.auth-form {
  display: grid;
  gap: 10px;
}

.auth-form input {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 10px;
  font: inherit;
  background: var(--mb-surface-2);
  color: inherit;
}

.auth-form button {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 10px;
  background: var(--mb-surface);
  color: inherit;
  cursor: pointer;
}

.auth-hint {
  margin-top: 12px;
  font-size: 12px;
  color: var(--mb-muted);
}

.admin {
  max-width: 900px;
  margin: 40px auto;
  padding: 16px;
}

.admin-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.admin-section {
  margin-top: 24px;
}

.admin-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.admin-row input {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 10px;
  font: inherit;
  background: var(--mb-surface-2);
  color: inherit;
}

.admin-row button,
.admin-top button {
  border: 1px solid var(--mb-border);
  border-radius: 6px;
  padding: 10px;
  background: var(--mb-surface);
  color: inherit;
  cursor: pointer;
}

.admin-list {
  display: grid;
  gap: 6px;
}

.admin-item {
  border: 1px solid var(--mb-border);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 12px;
  white-space: pre-wrap;
}
