/* ==========================================================================
   MERIT — CSS Variables & Theming
   ========================================================================== */

:root {
  /* Sidebar */
  --sidebar-bg: #1e1e2e;
  --sidebar-text: #cdd6f4;
  --sidebar-hover: #313244;
  --sidebar-active: #45475a;
  --sidebar-width: 250px;
  --sidebar-border: #313244;

  /* Main area — light mode defaults */
  --main-bg: #ffffff;
  --main-text: #1e1e2e;
  --main-secondary-text: #6c7086;
  --main-border: #e0e0e0;
  --main-code-bg: #f5f5f5;

  /* Accent */
  --accent: #4a9eff;
  --accent-hover: #3a8eef;
  --accent-subtle: rgba(74, 158, 255, 0.1);

  /* Top bar */
  --topbar-bg: #f8f9fa;
  --topbar-border: #e0e0e0;
  --topbar-height: 48px;

  /* Editor */
  --editor-bg: #ffffff;
  --editor-text: #1e1e2e;
  --editor-border: #ccc;

  /* Outline sidebar (right) */
  --outline-width: 240px;

  /* Misc */
  --radius: 6px;
  --transition: 0.2s ease;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;

  /* Scrollbar */
  --scrollbar-thumb: #585b70;
  --scrollbar-track: transparent;
}

/* Dark-mode overrides for the main area */
body.dark-mode {
  --main-bg: #1e1e2e;
  --main-text: #cdd6f4;
  --main-secondary-text: #a6adc8;
  --main-border: #313244;
  --main-code-bg: #181825;
  --topbar-bg: #181825;
  --topbar-border: #313244;
  --editor-bg: #181825;
  --editor-text: #cdd6f4;
  --editor-border: #45475a;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
}

/* ==========================================================================
   Reset & Base
   ========================================================================== */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--main-text);
  background: var(--main-bg);
}

/* ==========================================================================
   App Layout (CSS Grid)
   ========================================================================== */

#app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr var(--outline-width);
  grid-template-rows: var(--topbar-height) 1fr;
  grid-template-areas:
    "sidebar topbar  topbar"
    "sidebar main    outline";
  height: 100vh;
  width: 100vw;
}

body.outline-hidden {
  --outline-width: 0;
}

body.outline-hidden #outline-sidebar {
  display: none;
}

/* ==========================================================================
   Sidebar
   ========================================================================== */

#sidebar {
  grid-area: sidebar;
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--sidebar-border);
  overflow: hidden;
  position: relative;
  min-width: 180px;
}

#sidebar-header {
  padding: 14px 16px 10px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--main-secondary-text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

#sidebar-header .logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  letter-spacing: 0;
  text-transform: none;
  color: var(--sidebar-text);
}

#sidebar-header .logo svg {
  width: 20px;
  height: 20px;
  fill: var(--accent);
}

#file-tree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

/* Scrollbar styling */
#file-tree::-webkit-scrollbar {
  width: 6px;
}
#file-tree::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}
#file-tree::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 3px;
}

/* Tree items */
.tree-item {
  display: flex;
  align-items: center;
  padding: 4px 12px 4px calc(12px + var(--depth, 0) * 16px);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--sidebar-text);
  transition: background var(--transition);
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  gap: 6px;
  border-radius: 4px;
  margin: 1px 6px;
}

.tree-item:hover {
  background: var(--sidebar-hover);
}

.tree-item.active {
  background: var(--sidebar-active);
  color: #fff;
}

.tree-item.folder-selected {
  background: var(--sidebar-hover);
}

.tree-item .icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--main-secondary-text);
}

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

.tree-item .arrow {
  flex-shrink: 0;
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform var(--transition);
}

.tree-item .arrow svg {
  width: 10px;
  height: 10px;
}

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

.tree-item .label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-children {
  overflow: hidden;
}

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

/* New file button in sidebar */
#sidebar-actions {
  display: flex;
  gap: 4px;
  margin: 6px 10px;
}

#sidebar-actions button {
  display: flex;
  align-items: center;
  gap: 5px;
  flex: 1;
  padding: 5px 8px;
  background: var(--accent-subtle);
  color: var(--accent);
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 11.5px;
  cursor: pointer;
  transition: background var(--transition);
  white-space: nowrap;
}

#sidebar-actions button:hover {
  background: var(--accent);
  color: #fff;
}

#trash-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: auto 10px 10px;
  padding: 6px 12px;
  background: none;
  color: var(--sidebar-text);
  border: 1px solid var(--sidebar-border);
  border-radius: var(--radius);
  font-size: 12px;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity var(--transition), background var(--transition);
}

#trash-btn:hover {
  opacity: 1;
  background: var(--accent-subtle);
  color: var(--accent);
}

/* Resize handle */
#sidebar-resize {
  position: absolute;
  top: 0;
  right: -3px;
  width: 6px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
}

#sidebar-resize:hover,
#sidebar-resize.dragging {
  background: var(--accent);
  opacity: 0.4;
}

/* ==========================================================================
   Top Bar
   ========================================================================== */

#topbar {
  grid-area: topbar;
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--topbar-border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  z-index: 5;
}

#breadcrumb {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--main-secondary-text);
  flex-shrink: 0;
  min-width: 0;
}

#breadcrumb a {
  color: var(--main-secondary-text);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}

#breadcrumb a:hover {
  color: var(--accent);
}

#breadcrumb .sep {
  color: var(--main-secondary-text);
  opacity: 0.5;
  margin: 0 2px;
}

#breadcrumb .current {
  color: var(--main-text);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Spacer pushes right-side controls to the end */
.topbar-spacer {
  flex: 1;
}

/* Search */
#search-container {
  position: relative;
  width: 260px;
  flex-shrink: 0;
}

#search-input {
  width: 100%;
  padding: 6px 10px 6px 32px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  background: var(--main-bg);
  color: var(--main-text);
  font-size: 13px;
  outline: none;
  transition: border-color var(--transition);
}

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

#search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  color: var(--main-secondary-text);
}

#search-results {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--main-bg);
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow), 0 8px 24px rgba(0,0,0,0.12);
  max-height: 400px;
  overflow-y: auto;
  z-index: 100;
}

#search-results.visible {
  display: block;
}

.search-result-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--main-border);
  transition: background var(--transition);
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-item:hover {
  background: var(--accent-subtle);
}

.search-result-item .result-path {
  font-size: 13px;
  font-weight: 500;
  color: var(--main-text);
}

.search-result-item .result-snippet {
  font-size: 12px;
  color: var(--main-secondary-text);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.search-result-item .result-snippet mark {
  background: rgba(74, 158, 255, 0.25);
  color: inherit;
  padding: 0 1px;
  border-radius: 2px;
}

/* Top bar buttons */
.topbar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--main-secondary-text);
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
}

.topbar-btn:hover {
  background: var(--accent-subtle);
  color: var(--accent);
}

.topbar-btn.active {
  background: var(--accent-subtle);
  color: var(--accent);
}

.topbar-btn svg {
  width: 16px;
  height: 16px;
}

/* ==========================================================================
   Main Content Area
   ========================================================================== */

#main {
  grid-area: main;
  background: var(--main-bg);
  overflow-y: auto;
  padding: 32px 48px;
  position: relative;
}

#main::-webkit-scrollbar {
  width: 8px;
}
#main::-webkit-scrollbar-track {
  background: transparent;
}
#main::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 4px;
}

/* ==========================================================================
   Markdown Viewer
   ========================================================================== */

#viewer {
  max-width: 780px;
  margin: 0 auto;
}

#file-meta {
  font-size: 12px;
  color: var(--main-secondary-text);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}

#viewer-content {
  line-height: 1.75;
}

/* Typography */
#viewer-content h1 {
  font-size: 2em;
  font-weight: 700;
  margin: 0.6em 0 0.4em;
  border-bottom: 1px solid var(--main-border);
  padding-bottom: 0.3em;
}

#viewer-content h2 {
  font-size: 1.5em;
  font-weight: 600;
  margin: 1em 0 0.4em;
  border-bottom: 1px solid var(--main-border);
  padding-bottom: 0.25em;
}

#viewer-content h3 {
  font-size: 1.25em;
  font-weight: 600;
  margin: 0.8em 0 0.3em;
}

#viewer-content h4, #viewer-content h5, #viewer-content h6 {
  font-weight: 600;
  margin: 0.6em 0 0.3em;
}

#viewer-content p {
  margin: 0.6em 0;
}

#viewer-content a {
  color: var(--accent);
  text-decoration: none;
}

#viewer-content a:hover {
  text-decoration: underline;
}

#viewer-content a.wikilink {
  color: var(--accent);
  border-bottom: 1px dashed var(--accent);
}

#viewer-content a.wikilink:hover {
  border-bottom-style: solid;
  text-decoration: none;
}

#viewer-content a.wikilink.broken {
  color: #e06c75;
  border-bottom-color: #e06c75;
  opacity: 0.7;
}

#viewer-content ul, #viewer-content ol {
  margin: 0.5em 0;
  padding-left: 1.5em;
}

#viewer-content li {
  margin: 0.2em 0;
}

#viewer-content blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.8em 0;
  padding: 0.4em 1em;
  color: var(--main-secondary-text);
  background: var(--accent-subtle);
  border-radius: 0 var(--radius) var(--radius) 0;
}

#viewer-content code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--main-code-bg);
  padding: 2px 5px;
  border-radius: 3px;
}

#viewer-content pre {
  margin: 1em 0;
  padding: 16px;
  background: var(--main-code-bg);
  border-radius: var(--radius);
  overflow-x: auto;
  border: 1px solid var(--main-border);
}

#viewer-content pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.5;
}

#viewer-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
}

#viewer-content th, #viewer-content td {
  border: 1px solid var(--main-border);
  padding: 8px 12px;
  text-align: left;
}

#viewer-content th {
  background: var(--main-code-bg);
  font-weight: 600;
}

#viewer-content img {
  max-width: 100%;
  border-radius: var(--radius);
}

#viewer-content hr {
  border: none;
  border-top: 1px solid var(--main-border);
  margin: 1.5em 0;
}

/* PDF inline preview */
.pdf-preview {
  margin: 1em 0;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.pdf-preview-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--main-code-bg);
  border-bottom: 1px solid var(--main-border);
  font-size: 13px;
  font-weight: 500;
  color: var(--main-text);
}

.pdf-preview-header svg {
  flex-shrink: 0;
  color: #e06c75;
}

.pdf-preview-header span {
  flex: 1;
}

.pdf-preview-open {
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
}

.pdf-preview-open:hover {
  text-decoration: underline;
}

.pdf-preview-frame {
  width: 100%;
  height: 600px;
  border: none;
  display: block;
  background: #525659;
}

/* Attachment link icon */
a.attachment-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* Task lists */
#viewer-content input[type="checkbox"] {
  margin-right: 6px;
  cursor: pointer;
}

#viewer-content li.task-item {
  list-style: none;
  margin-left: -1.2em; /* compensate for the removed bullet */
}

/* ==========================================================================
   Backlinks
   ========================================================================== */

#backlinks {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--main-border);
}

#backlinks-header {
  font-size: 13px;
  color: var(--main-secondary-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
}

#backlinks-header:hover {
  color: var(--accent);
}

#backlinks-header .arrow {
  transition: transform var(--transition);
  display: inline-block;
}

#backlinks-header .arrow.expanded {
  transform: rotate(90deg);
}

#backlinks-list {
  margin-top: 8px;
  display: none;
}

#backlinks-list.visible {
  display: block;
}

#backlinks-list a {
  display: block;
  padding: 4px 0;
  color: var(--accent);
  text-decoration: none;
  font-size: 13.5px;
}

#backlinks-list a:hover {
  text-decoration: underline;
}

/* ==========================================================================
   Editor
   ========================================================================== */

#editor {
  max-width: 1400px;
  margin: 0 auto;
}

/* When the editor is the active panel, switch it to a flex column so the
   split area below the toolbar can fill all remaining vertical space. The
   inline `display: block` set by App.showPanel is overridden via !important. */
#editor.visible {
  display: flex !important;
  flex-direction: column;
  height: 100%;
}

#editor-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

#editor-textarea {
  width: 100%;
  min-height: 500px;
  padding: 16px;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.6;
  color: var(--editor-text);
  background: var(--editor-bg);
  border: 1px solid var(--editor-border);
  border-radius: var(--radius);
  resize: vertical;
  outline: none;
  tab-size: 2;
}

#editor-textarea:focus {
  border-color: var(--accent);
}

.editor-btn {
  padding: 6px 14px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
  transition: background var(--transition);
}

.editor-btn.primary {
  background: var(--accent);
  color: #fff;
}

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

.editor-btn.secondary {
  background: var(--main-code-bg);
  color: var(--main-text);
  border: 1px solid var(--main-border);
}

.editor-btn.secondary:hover {
  background: var(--main-border);
}

/* Upload snippet */
#upload-snippet {
  display: flex;
  align-items: center;
  margin-left: auto;
}

.upload-snippet-code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--main-code-bg);
  padding: 3px 8px;
  border-radius: 3px;
  border: 1px solid var(--main-border);
  color: var(--accent);
  user-select: all;
}

/* Drag over editor textarea */
#editor-textarea.drag-over {
  border-color: var(--accent);
  background: var(--accent-subtle);
}

/* ==========================================================================
   Diff View
   ========================================================================== */

#diff-view {
  max-width: 780px;
  margin: 20px auto 0;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  overflow: hidden;
}

#diff-view .diff-header {
  background: var(--main-code-bg);
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--main-secondary-text);
  border-bottom: 1px solid var(--main-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#diff-view .diff-content {
  padding: 0;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  overflow-x: auto;
}

.diff-line {
  padding: 1px 12px;
  white-space: pre-wrap;
  word-break: break-all;
}

.diff-line.added {
  background: rgba(80, 200, 120, 0.15);
  color: #50c878;
}

.diff-line.removed {
  background: rgba(224, 108, 117, 0.15);
  color: #e06c75;
}

.diff-line.context {
  color: var(--main-secondary-text);
}

/* ==========================================================================
   History Panel
   ========================================================================== */

#history-panel {
  max-width: 780px;
  margin: 0 auto;
}

#history-panel h2 {
  font-size: 1.3em;
  margin-bottom: 12px;
  color: var(--main-text);
}

.history-entry {
  padding: 10px 14px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  cursor: pointer;
  transition: background var(--transition);
}

.history-entry:hover {
  background: var(--accent-subtle);
}

.history-entry .history-date {
  font-size: 13px;
  font-weight: 500;
  color: var(--main-text);
}

.history-entry .history-message {
  font-size: 12px;
  color: var(--main-secondary-text);
  margin-top: 2px;
}

.history-diff {
  display: none;
  margin-top: 8px;
  padding: 12px;
  background: var(--main-code-bg);
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-x: auto;
}

.history-diff.visible {
  display: block;
}

/* ==========================================================================
   Context Menu
   ========================================================================== */

#context-menu {
  display: none;
  position: fixed;
  background: var(--main-bg);
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.15);
  z-index: 300;
  min-width: 160px;
  padding: 4px 0;
}

#context-menu.visible {
  display: block;
}

.ctx-item {
  padding: 6px 14px;
  font-size: 13px;
  color: var(--main-text);
  cursor: pointer;
  user-select: none;
}

.ctx-item:hover {
  background: var(--accent-subtle);
  color: var(--accent);
}

.ctx-danger:hover {
  background: rgba(224, 108, 117, 0.12);
  color: #e06c75;
}

.ctx-separator {
  height: 1px;
  background: var(--main-border);
  margin: 4px 0;
}

/* ==========================================================================
   Attachments Panel
   ========================================================================== */

#attachments-panel {
  max-width: 780px;
  margin: 0 auto;
}

.attachments-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.attachments-header h2 {
  margin: 0;
}

.attachments-count {
  font-size: 13px;
  color: var(--main-secondary-text);
}

.attachment-card {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 12px 14px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  transition: background var(--transition);
}

.attachment-card:hover {
  background: var(--accent-subtle);
}

.attachment-preview {
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  overflow: hidden;
  background: var(--main-code-bg);
}

.attachment-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.attachment-info {
  flex: 1;
  min-width: 0;
}

.attachment-name {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--main-text);
  word-break: break-all;
}

.attachment-meta {
  font-size: 11.5px;
  color: var(--main-secondary-text);
  margin-top: 2px;
}

.attachment-refs {
  margin-top: 6px;
  font-size: 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
}

.attachment-refs-label {
  color: var(--main-secondary-text);
}

.attachment-ref-link {
  color: var(--accent);
  text-decoration: none;
}

.attachment-ref-link:hover {
  text-decoration: underline;
}

.attachment-no-refs {
  color: var(--main-secondary-text);
  font-style: italic;
  font-size: 12px;
}

.attachment-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex-shrink: 0;
}

/* ==========================================================================
   Dialogs (prompt, move, confirm)
   ========================================================================== */

#prompt-dialog, #move-dialog, #confirm-dialog {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

#prompt-dialog.visible, #move-dialog.visible, #confirm-dialog.visible {
  display: flex;
}

#confirm-message {
  color: var(--main-secondary-text);
  font-size: 13.5px;
  margin-bottom: 16px;
}

.ctx-danger-btn {
  background: #e06c75 !important;
  border-color: #e06c75 !important;
}

.ctx-danger-btn:hover {
  background: #c85a63 !important;
}

#move-folder-list {
  max-height: 300px;
  overflow-y: auto;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 16px;
}

.move-folder-item {
  padding: 8px 12px;
  font-size: 13px;
  color: var(--main-text);
  cursor: pointer;
  border-bottom: 1px solid var(--main-border);
}

.move-folder-item:last-child {
  border-bottom: none;
}

.move-folder-item:hover {
  background: var(--accent-subtle);
  color: var(--accent);
}

/* ==========================================================================
   Trash Panel
   ========================================================================== */

#trash-panel {
  max-width: 780px;
  margin: 0 auto;
}

.trash-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.trash-header h2 {
  margin: 0;
}

.trash-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 6px;
  gap: 12px;
}

.trash-item:hover {
  background: var(--accent-subtle);
}

.trash-item-info {
  flex: 1;
  min-width: 0;
}

.trash-item-name {
  font-size: 13.5px;
  color: var(--main-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.trash-item-date {
  font-size: 11.5px;
  color: var(--main-secondary-text);
  margin-top: 2px;
}

.trash-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.dialog-box {
  background: var(--main-bg);
  border: 1px solid var(--main-border);
  border-radius: 8px;
  padding: 24px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 16px 48px rgba(0,0,0,0.2);
}

.dialog-box h3 {
  margin-bottom: 16px;
  color: var(--main-text);
}

.dialog-box input[type="text"] {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  background: var(--editor-bg);
  color: var(--editor-text);
  font-size: 14px;
  outline: none;
  margin-bottom: 16px;
}

.dialog-box input[type="text"]:focus {
  border-color: var(--accent);
}

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

/* ==========================================================================
   Welcome / Empty State
   ========================================================================== */

#welcome {
  text-align: center;
  padding-top: 18vh;
  color: var(--main-secondary-text);
}

#welcome .welcome-logo {
  width: 220px;
  height: auto;
  color: var(--main-text);
  opacity: 0.18;
  margin-bottom: 20px;
}

#welcome p {
  font-size: 14px;
  max-width: 400px;
  margin: 0 auto;
}

/* ==========================================================================
   Toast Notifications
   ========================================================================== */

#toast-container {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-radius: var(--radius);
  font-size: 13.5px;
  color: #fff;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  animation: toast-in 0.3s ease, toast-out 0.3s ease forwards;
  animation-delay: 0s, var(--toast-duration, 4s);
  max-width: 420px;
}

.toast.warning {
  background: #e5a00d;
}

.toast.error {
  background: #e06c75;
}

.toast.success {
  background: #50c878;
}

.toast-icon {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateX(40px); }
  to   { opacity: 1; transform: translateX(0); }
}

@keyframes toast-out {
  from { opacity: 1; transform: translateX(0); }
  to   { opacity: 0; transform: translateX(40px); }
}

/* ==========================================================================
   Loading Spinner
   ========================================================================== */

.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--main-border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

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

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 768px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
  }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    z-index: 50;
    transform: translateX(-100%);
    transition: transform var(--transition);
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.4);
    z-index: 49;
  }

  #sidebar-overlay.visible {
    display: block;
  }

  #mobile-menu-btn {
    display: flex;
  }

  #main {
    padding: 20px 16px;
  }

  /* Hide breadcrumb on mobile — too cramped to be useful, removing it
     gives the action buttons enough room. */
  #breadcrumb {
    display: none;
  }

  /* Search collapses to an icon button. The actual input lives in an
     overlay anchored under the topbar, toggled by JS via body.search-open. */
  #search-mobile-btn {
    display: flex;
  }
  #search-container {
    display: none;
    position: fixed;
    top: var(--topbar-height);
    left: 8px;
    right: 8px;
    width: auto;
    background: var(--main-bg);
    padding: 10px 12px;
    border: 1px solid var(--main-border);
    border-radius: var(--radius);
    box-shadow: 0 6px 20px rgba(0,0,0,0.15);
    z-index: 60;
  }
  body.search-open #search-container {
    display: block;
  }
  body.search-open #search-backdrop {
    display: block;
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.4);
    z-index: 49;
  }
  /* Free up a bit of horizontal space — show only the icon, drop the
     name text from the user menu button on small screens. */
  #user-menu-label {
    display: none;
  }
  #user-menu-btn {
    padding: 4px 8px;
  }
}

@media (min-width: 769px) {
  #mobile-menu-btn {
    display: none;
  }

  /* Desktop hides both the mobile-only search trigger and its backdrop.
     Wrapping these in a min-width query (instead of using a flat default)
     ensures the mobile rules in the max-width:768 block actually win on
     small screens — order-dependent cascading would otherwise hide the
     icon everywhere. */
  #search-mobile-btn,
  #search-backdrop {
    display: none;
  }

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

/* ==========================================================================
   Utility
   ========================================================================== */

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}

/* ==========================================================================
   User menu (topbar) and API tokens dialog
   ========================================================================== */

#user-menu {
  position: relative;
}

#user-menu.hidden {
  display: none;
}

#user-menu-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  font-size: 13px;
}

#user-menu-btn svg {
  width: 14px;
  height: 14px;
}

#user-menu-label {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#user-menu-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  min-width: 200px;
  background: var(--main-bg);
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  z-index: 150;
  padding: 6px 0;
}

#user-menu-dropdown.hidden {
  display: none;
}

#user-menu-header {
  padding: 6px 14px 10px;
  font-size: 12px;
  color: var(--main-secondary-text);
  border-bottom: 1px solid var(--main-border);
  margin-bottom: 4px;
  word-break: break-all;
}

.user-menu-item {
  display: block;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--main-text);
  cursor: pointer;
  text-decoration: none;
}

.user-menu-item:hover {
  background: var(--main-hover-bg, rgba(0,0,0,0.05));
}

.user-menu-separator {
  height: 1px;
  background: var(--main-border);
  margin: 4px 0;
}

#tokens-dialog {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

#tokens-dialog.visible {
  display: flex;
}

.dialog-box-wide {
  min-width: 480px;
  max-width: 92vw;
}

.dialog-hint {
  color: var(--main-secondary-text);
  font-size: 12.5px;
  margin: -4px 0 12px;
}

#tokens-new-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

#tokens-new-row input {
  flex: 1;
}

#tokens-new-result {
  margin-bottom: 12px;
}

#tokens-new-result.hidden {
  display: none;
}

#tokens-new-result label {
  display: block;
  font-size: 12px;
  color: var(--main-secondary-text);
  margin-bottom: 4px;
}

#tokens-new-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  background: var(--main-hover-bg, rgba(0,0,0,0.04));
}

#tokens-list {
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.tokens-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--main-border);
}

.tokens-row:last-child {
  border-bottom: none;
}

.tokens-row-info {
  flex: 1;
  min-width: 0;
}

.tokens-row-name {
  font-size: 13px;
  color: var(--main-text);
  margin-bottom: 2px;
}

.tokens-row-meta {
  font-size: 11.5px;
  color: var(--main-secondary-text);
}

.tokens-empty,
.tokens-loading {
  padding: 16px;
  text-align: center;
  font-size: 13px;
  color: var(--main-secondary-text);
}

/* ==========================================================================
   Admin dialog (role management)
   ========================================================================== */

#admin-dialog {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: center;
  justify-content: center;
}

#admin-dialog.visible {
  display: flex;
}

#admin-new-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

#admin-new-row input {
  flex: 1;
}

#admin-new-dirs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 10px 12px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 16px;
  max-height: 160px;
  overflow-y: auto;
}

.admin-dir-check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--main-text);
  cursor: pointer;
}

.admin-list-heading {
  font-size: 13px;
  font-weight: 600;
  color: var(--main-secondary-text);
  margin: 16px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

#admin-backup-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

#admin-backup-status {
  flex-basis: 100%;
  margin: 0;
}

#admin-roles-list {
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.admin-role-row {
  gap: 10px;
}

.admin-role-dirs {
  word-break: break-word;
}

.admin-ro-badge {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--main-hover-bg, rgba(0,0,0,0.06));
  color: var(--main-secondary-text);
}

/* ==========================================================================
   Picker dialog (insert link / attachment)
   ========================================================================== */

#picker-dialog {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
}

#picker-dialog.visible {
  display: flex;
}

#picker-dialog .dialog-box {
  width: 560px;
  max-width: 92vw;
}

#picker-input {
  width: 100%;
  margin-bottom: 10px;
}

#picker-list {
  max-height: 50vh;
  overflow-y: auto;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  margin-bottom: 12px;
}

.picker-row {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--main-border);
}

.picker-row:last-child {
  border-bottom: none;
}

.picker-row.active {
  background: var(--main-hover-bg, rgba(0,0,0,0.06));
}

.picker-row-label {
  font-size: 13.5px;
  color: var(--main-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.picker-row-sub {
  font-size: 11.5px;
  color: var(--main-secondary-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.picker-loading {
  padding: 20px;
  text-align: center;
  color: var(--main-secondary-text);
  font-size: 13px;
}

.picker-loading code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  background: var(--main-hover-bg, rgba(0,0,0,0.05));
  padding: 1px 5px;
  border-radius: 3px;
}

.history-event {
  display: inline-block;
  padding: 2px 8px;
  margin-right: 6px;
  border-radius: 4px;
  background: var(--accent-subtle);
  color: var(--accent);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Tree item action button — three dots, opens context menu. On pointer
   devices it appears only on hover for a clean look; on touch devices
   (no hover) it's always visible since long-press is iffy and obscure. */
.tree-item {
  -webkit-touch-callout: none; /* suppress iOS's link-preview popover */
  user-select: none;
}

.tree-item-more {
  margin-left: auto;
  flex-shrink: 0;
  background: transparent;
  border: none;
  color: inherit;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.7;
}

.tree-item-more svg {
  width: 14px;
  height: 14px;
}

.tree-item-more:hover {
  background: var(--sidebar-hover);
  opacity: 1;
}

@media (hover: hover) {
  .tree-item-more {
    opacity: 0;
  }
  .tree-item:hover .tree-item-more {
    opacity: 0.7;
  }
}

/* ==========================================================================
   Activity feed (welcome dashboard)
   ========================================================================== */

#activity-feed {
  margin-top: 36px;
  width: 100%;
  max-width: 720px;
  text-align: left;
}

#activity-feed h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--main-secondary-text);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 10px;
}

#activity-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--main-border);
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.activity-row {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 12px;
  padding: 8px 14px;
  background: var(--main-bg);
  font-size: 13px;
  align-items: baseline;
}

.activity-row.activity-clickable {
  cursor: pointer;
}

.activity-row.activity-clickable:hover {
  background: var(--main-hover-bg, rgba(0,0,0,0.04));
}

.activity-when {
  color: var(--main-secondary-text);
  font-size: 12px;
  white-space: nowrap;
}

.activity-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.activity-actor {
  font-weight: 600;
}

.activity-verb {
  color: var(--main-secondary-text);
  margin: 0 4px;
}

.activity-path {
  color: var(--accent);
}

.activity-empty,
.activity-loading {
  padding: 16px;
  text-align: center;
  color: var(--main-secondary-text);
  background: var(--main-bg);
}

/* ==========================================================================
   Graph view
   ========================================================================== */

#graph-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 10px 6px;
  padding: 6px 12px;
  background: none;
  color: var(--sidebar-text);
  border: 1px solid var(--sidebar-border);
  border-radius: var(--radius);
  font-size: 12px;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity var(--transition), background var(--transition);
}

#graph-btn:hover {
  opacity: 1;
  background: var(--accent-subtle);
  color: var(--accent);
}

#graph-panel {
  position: relative;
  width: 100%;
  height: calc(100vh - var(--topbar-height) - 40px);
  margin: 0;
  padding: 0;
  overflow: hidden;
}

#graph-toolbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  font-size: 12px;
  color: var(--main-secondary-text);
  background: linear-gradient(to bottom, var(--main-bg) 60%, transparent);
  z-index: 2;
  pointer-events: none;
}

#graph-stats {
  font-weight: 600;
}

.graph-spacer {
  flex: 1;
}

#graph-hint {
  opacity: 0.7;
}

#graph-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#graph-canvas:active {
  cursor: grabbing;
}

@media (max-width: 768px) {
  #graph-hint { display: none; }
  #graph-panel { height: calc(100vh - var(--topbar-height)); }
}

/* ==========================================================================
   Outline sidebar (right)
   ========================================================================== */

#outline-sidebar {
  grid-area: outline;
  border-left: 1px solid var(--main-border);
  background: var(--main-bg);
  overflow-y: auto;
  padding: 14px 12px 14px 16px;
  font-size: 13px;
}

#outline-header {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--main-secondary-text);
  margin-bottom: 8px;
}

.outline-item {
  padding: 4px 6px;
  cursor: pointer;
  border-radius: 4px;
  color: var(--main-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 12.5px;
  line-height: 1.4;
}

.outline-item:hover {
  background: var(--accent-subtle);
  color: var(--accent);
}

.outline-l1 { padding-left:  6px; font-weight: 600; }
.outline-l2 { padding-left: 18px; }
.outline-l3 { padding-left: 30px; }
.outline-l4 { padding-left: 42px; font-size: 12px; color: var(--main-secondary-text); }
.outline-l5 { padding-left: 54px; font-size: 11.5px; color: var(--main-secondary-text); }
.outline-l6 { padding-left: 66px; font-size: 11.5px; color: var(--main-secondary-text); }

.outline-empty {
  padding: 6px;
  color: var(--main-secondary-text);
  font-size: 12px;
  font-style: italic;
}

/* ==========================================================================
   Editor split-view (source + live preview)
   ========================================================================== */

#editor-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--main-border);
  flex: 1 1 auto;   /* fill remaining height inside the flex column */
  min-height: 0;     /* allow grid children to shrink within the flex item */
}

#editor-split.preview-hidden {
  grid-template-columns: 1fr;
}

#editor-split.preview-hidden #editor-preview {
  display: none;
}

#editor-textarea {
  background: var(--editor-bg);
  color: var(--editor-text);
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.6;
  padding: 16px 20px;
  /* Override the legacy 500px min-height so the textarea can stretch with
     the flex layout above. height:100% + min-height:0 lets it both grow
     to fill the cell and shrink/scroll instead of pushing the page. */
  min-height: 0;
  height: 100%;
  overflow: auto;
}

#editor-preview {
  background: var(--main-bg);
  height: 100%;
  overflow: auto;
  padding: 16px 24px;
  font-size: 14.5px;
  line-height: 1.65;
}

/* Use a sane vertical layout for the preview's typography */
#editor-preview h1, #editor-preview h2, #editor-preview h3 {
  margin: 16px 0 8px;
  line-height: 1.25;
}
#editor-preview h1 { font-size: 24px; }
#editor-preview h2 { font-size: 20px; }
#editor-preview h3 { font-size: 17px; }
#editor-preview p, #editor-preview ul, #editor-preview ol, #editor-preview blockquote, #editor-preview pre {
  margin-bottom: 12px;
}
#editor-preview img {
  max-width: 100%;
}
#editor-preview pre {
  background: var(--main-code-bg);
  padding: 10px 12px;
  border-radius: var(--radius);
  overflow-x: auto;
}
#editor-preview code {
  background: var(--main-code-bg);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 0.92em;
}
#editor-preview pre code {
  background: transparent;
  padding: 0;
}

.editor-toolbar-spacer {
  flex: 1;
}

/* The preview-hidden class is the single source of truth at all sizes
   above the mobile breakpoint — see also editor.js which persists the
   user's preference to localStorage. */

@media (max-width: 768px) {
  /* On phones the outline never fits, regardless of toggle state */
  #outline-sidebar { display: none; }
  #app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
  }
  #btn-outline { display: none; }
  /* Editor preview never shown on mobile — narrow screen makes it useless */
  #editor-preview { display: none; }
  #editor-split { grid-template-columns: 1fr; }
  #btn-toggle-preview { display: none; }
}

/* ==========================================================================
   Notifications bell
   ========================================================================== */

#notifications-wrap {
  position: relative;
}

#notifications-btn {
  position: relative;
}

#notifications-count {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#notifications-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 360px;
  max-height: 70vh;
  overflow-y: auto;
  background: var(--main-bg);
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0,0,0,0.15);
  z-index: 150;
}

#notifications-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--main-border);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--main-secondary-text);
}

#notifications-mark-all {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 12px;
  cursor: pointer;
}

.notifications-row {
  padding: 10px 14px;
  border-bottom: 1px solid var(--main-border);
  cursor: pointer;
  font-size: 13px;
  position: relative;
}

.notifications-row:hover {
  background: var(--main-hover-bg, rgba(0,0,0,0.04));
}

.notifications-row.unread {
  background: var(--accent-subtle);
}

.notifications-row-title {
  font-weight: 600;
  margin-bottom: 2px;
}

.notifications-row-body {
  color: var(--main-secondary-text);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.notifications-row-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 11px;
  color: var(--main-secondary-text);
}

.notifications-row-del {
  background: none;
  border: none;
  color: var(--main-secondary-text);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 0 4px;
}

.notifications-row-del:hover {
  color: #e06c75;
}

.notifications-empty {
  padding: 24px;
  text-align: center;
  color: var(--main-secondary-text);
  font-size: 13px;
}

/* ==========================================================================
   Subscribe button + presence banner + comments
   ========================================================================== */

#file-meta-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

#file-meta {
  flex: 1;
  font-size: 12px;
  color: var(--main-secondary-text);
}

#btn-subscribe[data-active="true"] {
  background: var(--accent-subtle);
  color: var(--accent);
}

#presence-banner {
  background: var(--accent-subtle);
  color: var(--accent);
  padding: 6px 12px;
  border-radius: var(--radius);
  font-size: 12.5px;
  margin-bottom: 12px;
}

#comments-fab {
  position: absolute;
  z-index: 50;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

#comments-panel {
  position: fixed;
  top: var(--topbar-height);
  right: 0;
  width: 360px;
  height: calc(100vh - var(--topbar-height));
  background: var(--main-bg);
  border-left: 1px solid var(--main-border);
  display: flex;
  flex-direction: column;
  z-index: 60;
  box-shadow: -4px 0 12px rgba(0,0,0,0.08);
}

#comments-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--main-border);
  font-weight: 600;
}

#comments-close {
  background: none;
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  color: var(--main-secondary-text);
}

#comments-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  min-height: 0; /* needed so the scrollable area shrinks instead of forcing the form off-screen */
}

.comments-row {
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  padding: 8px 10px;
  margin-bottom: 8px;
  font-size: 13px;
}

.comments-row-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--main-secondary-text);
  margin-bottom: 4px;
}

.comments-row-author {
  font-weight: 600;
  color: var(--main-text);
}

.comments-row-when {
  flex: 1;
}

.comments-row-del {
  background: none;
  border: none;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  color: var(--main-secondary-text);
}

.comments-row-anchor {
  font-style: italic;
  color: var(--main-secondary-text);
  font-size: 12px;
  background: var(--main-hover-bg, rgba(0,0,0,0.04));
  padding: 4px 8px;
  border-radius: 4px;
  margin-bottom: 6px;
  cursor: pointer;
}

.comments-row-anchor:hover {
  background: var(--accent-subtle);
  color: var(--accent);
}

.comments-row-text {
  white-space: pre-wrap;
}

.comments-empty {
  padding: 24px;
  text-align: center;
  color: var(--main-secondary-text);
  font-size: 13px;
}

#comments-form {
  /* Sits at the top of the panel (HTML order: header → form → list).
     Keeps the Save button visible even when the iOS keyboard pushes the
     viewport up. */
  border-bottom: 1px solid var(--main-border);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
}

#comments-text {
  width: 100%;
  min-height: 80px;
  padding: 8px;
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 13px;
  resize: vertical;
}

#comments-anchor-preview {
  background: var(--main-hover-bg, rgba(0,0,0,0.04));
  padding: 6px 10px;
  border-radius: 4px;
  font-style: italic;
  color: var(--main-secondary-text);
  font-size: 12px;
}

.comments-flash {
  background: rgba(255, 200, 60, 0.4);
  transition: background 1.5s ease-out;
}

.comment-anchor-mark {
  background: rgba(255, 200, 60, 0.25);
  border-bottom: 2px solid rgba(255, 170, 0, 0.5);
  cursor: pointer;
  border-radius: 2px;
  transition: background 0.2s;
}
.comment-anchor-mark:hover {
  background: rgba(255, 200, 60, 0.45);
}

.comments-row-flash {
  background: rgba(255, 200, 60, 0.25);
  transition: background 1.2s ease-out;
}

/* ==========================================================================
   Mentions popup
   ========================================================================== */

#mentions-popup {
  position: absolute;
  background: var(--main-bg);
  border: 1px solid var(--main-border);
  border-radius: var(--radius);
  box-shadow: 0 4px 14px rgba(0,0,0,0.15);
  z-index: 250;
  min-width: 240px;
  max-height: 240px;
  overflow-y: auto;
}

.mentions-row {
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
  border-bottom: 1px solid var(--main-border);
}

.mentions-row:last-child {
  border-bottom: none;
}

.mentions-row.active,
.mentions-row:hover {
  background: var(--accent-subtle);
}

.mentions-name {
  font-weight: 600;
}

.mentions-email {
  font-size: 11px;
  color: var(--main-secondary-text);
}

@media (max-width: 768px) {
  #notifications-dropdown { width: 100vw; max-width: 360px; right: -8px; }
  #comments-panel { width: 100vw; }
}
