/* Prevent iOS auto-zoom on focus for interactive elements (requires >= 16px) */
@media (max-width: 767px) {
  .tiptap,
  .nested-editor {
    font-size: 16px;
  }

  input,
  textarea,
  select {
    font-size: 16px !important;
  }
}

/* Sidebar state - controlled by class on html element for instant load */
@media (max-width: 767px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(88vw, 22rem);
    max-width: 22rem;
    z-index: 120;
    box-shadow: 10px 0 24px rgba(0, 0, 0, 0.45);
    transform: translateX(-105%);
    transition: transform 0.18s ease;
  }

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

  .sidebar-open #sidebarBackdrop {
    opacity: 1;
    pointer-events: auto;
  }
}

@media (min-width: 768px) {
  :root {
    --sidebar-width: 16rem;
  }

  .sidebar-panel {
    width: 0;
    border-right-width: 0;
    box-shadow: none;
  }

  .sidebar-open .sidebar-panel {
    width: var(--sidebar-width);
    border-right-width: 0;
    box-shadow:
      inset -1px 0 0 rgba(255, 255, 255, 0.04),
      6px 0 18px rgba(0, 0, 0, 0.18);
  }

  .sidebar-resize-handle {
    flex: 0 0 10px;
    width: 10px;
    min-width: 10px;
    cursor: col-resize;
    background: transparent;
    transition: background-color 0.15s ease;
    position: relative;
    z-index: 20;
    display: none;
    touch-action: none;
  }

  .sidebar-open .sidebar-resize-handle {
    display: block;
  }

  .sidebar-resize-handle::before {
    content: '';
    position: absolute;
    left: 4px;
    top: 0;
    bottom: 0;
    width: 1px;
    background: rgba(255, 255, 255, 0.12);
    opacity: 0.5;
    transition: opacity 0.15s ease, background-color 0.15s ease;
  }

  .sidebar-resize-handle:hover {
    background: transparent;
  }

  .sidebar-resize-handle:hover::before {
    opacity: 0.85;
    background: rgba(255, 255, 255, 0.2);
  }

  .sidebar-resizing .sidebar-panel {
    transition: none !important;
  }

  .sidebar-resizing,
  .sidebar-resizing * {
    cursor: col-resize !important;
    user-select: none !important;
  }
}

/* Base styles */
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Consistent in-app tooltip for icon-only buttons */
.tooltip-trigger {
  position: relative;
}

.tooltip-trigger::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  top: calc(100% + 8px);
  transform: translateX(-50%) translateY(-2px);
  pointer-events: none;
  opacity: 0;
  background: rgba(10, 10, 10, 0.96);
  color: #e5e7eb;
  border: 1px solid #2a2a2a;
  border-radius: 6px;
  font-size: 11px;
  line-height: 1.2;
  padding: 4px 7px;
  white-space: nowrap;
  z-index: 140;
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.tooltip-trigger:hover::after,
.tooltip-trigger:focus-visible::after {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#sidebarNotes:focus {
  outline: none;
}

#sidebarNotes .sidebar-row-active {
  box-shadow: inset 0 0 0 1px rgba(59, 130, 246, 0.7);
  background: rgba(59, 130, 246, 0.12);
  color: #e5e7eb;
}

#sidebarNotes .sidebar-row-selected:not(.sidebar-row-active) {
  background: rgba(59, 130, 246, 0.07);
  box-shadow: inset 0 0 0 1px rgba(59, 130, 246, 0.35);
  color: #d1d5db;
}

/* List item content clamp */
.item-content {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Editor styles */
.tiptap {
  outline: none;
  min-height: 200px;
}

.tiptap p {
  margin: 0.75rem 0;
  line-height: 1.6;
}

.tiptap p:first-child {
  margin-top: 0;
}

.tiptap ul, .tiptap ol {
  margin: 0.75rem 0;
  padding-left: 1.5rem;
}

.tiptap li {
  margin: 0.25rem 0;
}

.table-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: 0 0 0.6rem;
}

.table-toolbar.hidden {
  display: none;
}

.table-toolbar button {
  border: 1px solid #2a2a2a;
  background: #1a1a1a;
  color: #d1d5db;
  border-radius: 5px;
  font-size: 0.75rem;
  padding: 0.2rem 0.45rem;
  line-height: 1.3;
  cursor: pointer;
}

.table-toolbar button:hover:not(:disabled) {
  border-color: #3b82f6;
  color: #f3f4f6;
}

.table-toolbar button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tiptap table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
  margin: 0.5rem 0;
}

.tiptap table th,
.tiptap table td {
  border: 1px solid #2a2a2a;
  padding: 0.35rem 0.5rem;
  vertical-align: top;
}

.tiptap table th {
  background: #1f2937;
}

.tiptap p.is-editor-empty:first-child::before {
  color: #666;
  content: attr(data-placeholder);
  float: left;
  height: 0;
  pointer-events: none;
}

/* Gap cursor - allows clicking between blocks */
.tiptap .ProseMirror-gapcursor {
  display: none;
  pointer-events: none;
  position: absolute;
}

.tiptap .ProseMirror-gapcursor:after {
  content: '';
  display: block;
  position: absolute;
  top: -2px;
  width: 20px;
  border-top: 2px solid #3b82f6;
  animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite;
}

@keyframes ProseMirror-cursor-blink {
  to {
    visibility: hidden;
  }
}

.tiptap.ProseMirror-focused .ProseMirror-gapcursor {
  display: block;
}

/* Block drag handle */
.block-drag-handle {
  position: absolute;
  left: -24px;
  top: 0;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #666;
  cursor: grab;
  opacity: 0;
  transition: opacity 0.15s ease;
  border-radius: 3px;
}

.block-drag-handle:hover {
  background: #252525;
  color: #999;
}

.block-drag-handle:active {
  cursor: grabbing;
}

.group:hover .block-drag-handle {
  opacity: 1;
}

/* Nested editor for embeds */
.nested-editor {
  outline: none;
  min-height: 50px;
}

.nested-editor p {
  margin: 0.5rem 0;
  line-height: 1.5;
}

.nested-editor p:first-child {
  margin-top: 0;
}

.nested-editor p:last-child {
  margin-bottom: 0;
}

.nested-editor p.is-editor-empty:first-child::before {
  color: #666;
  content: attr(data-placeholder);
  float: left;
  height: 0;
  pointer-events: none;
}

/* Wikilink styles - rendered mode (unfocused) */
.wikilink {
  color: #3b82f6;
  background: none;
  padding: 0;
  border-radius: 0;
  text-decoration: none;
  text-decoration: underline;
  cursor: pointer;
}

.wikilink:hover {
  background: none;
}

.wikilink-broken {
  color: #ef4444;
  background: none;
}

.wikilink-broken:hover {
  background: none;
}

/* Wikilink markdown mode (focused line) - looks like plain text */
.wikilink-markdown {
  color: #9ca3af;
  cursor: default;
}

.wikilink-markdown.wikilink-broken {
  color: #f87171;
}

/* Embed node styles - minimal blockquote style */
.embed-block {
  position: relative;
}

.embed-block .nested-editor {
  min-height: auto;
}

.embed-block .nested-editor p {
  margin: 0;
}

/* Markdown live preview decorations */
.md-hidden {
  font-size: 0;
  opacity: 0;
  width: 0;
  display: inline;
}

.md-heading {
  font-weight: 600;
  color: #e5e5e5;
}

.md-heading-marker {
  color: #93c5fd;
  background: rgba(59, 130, 246, 0.18);
  border-radius: 4px;
  padding: 0 0.25em;
  margin-right: 0.2em;
  font-family: ui-monospace, monospace;
  font-size: 0.8em;
  font-weight: 600;
}

.md-heading-space {
  opacity: 0.5;
}

.md-h1 { font-size: 1.75em; }
.md-h2 { font-size: 1.5em; }
.md-h3 { font-size: 1.25em; }
.md-h4 { font-size: 1.1em; }
.md-h5 { font-size: 1em; }
.md-h6 { font-size: 0.9em; }

.md-bold {
  font-weight: 600;
}

.md-italic {
  font-style: italic;
}

.md-code {
  font-family: ui-monospace, monospace;
  background: rgba(255, 255, 255, 0.1);
  padding: 0.1em 0.3em;
  border-radius: 3px;
  font-size: 0.9em;
}

.md-strike {
  text-decoration: line-through;
  opacity: 0.7;
}

.md-link {
  color: #60a5fa;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

.md-link:hover {
  color: #93c5fd;
}

.md-link-missing {
  color: #fca5a5;
}

.md-link-wrapper {
  display: inline-flex;
  flex-direction: column;
  gap: 0.45rem;
  max-width: 100%;
  margin: 0.15rem 0;
}

.md-link-preview {
  display: block;
  width: min(100%, 560px);
}

.md-link-preview-image {
  display: block;
  max-width: 100%;
  max-height: 280px;
  object-fit: contain;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  background: #111;
}

.md-link-preview-video {
  display: block;
  width: 100%;
  max-height: 280px;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  background: #111;
}

.md-link-preview-audio {
  display: block;
  width: min(100%, 420px);
}

.md-blockquote {
  border-left: 2px solid #3b82f6;
  padding-left: 1em;
  color: #9ca3af;
}

.md-list-marker {
  color: #666;
}

.tiptap p.md-list-item {
  margin: 0.25rem 0;
}

.md-table-hidden-row {
  display: none;
}

.md-table-wrapper {
  display: block;
  overflow-x: auto;
  margin: 0.25rem 0 0.5rem;
}

.md-table {
  border-collapse: collapse;
  width: 100%;
  min-width: 360px;
  font-size: 0.92em;
}

.md-table th,
.md-table td {
  border: 1px solid #2a2a2a;
  padding: 0.4rem 0.55rem;
  text-align: left;
  vertical-align: top;
}

.md-table th {
  background: #1f2937;
  color: #e5e7eb;
  font-weight: 600;
}

.md-table td {
  background: #141414;
  color: #d1d5db;
}

/* Link suggestion dropdown */
.suggestion-list {
  position: absolute;
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  max-height: 200px;
  overflow-y: auto;
  z-index: 100;
}

.suggestion-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid #2a2a2a;
  display: flex;
  align-items: flex-start;
}

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

.suggestion-item:hover, .suggestion-item.is-selected {
  background: #252525;
}

.suggestion-item-icon {
  flex-shrink: 0;
  font-size: 14px;
}

.suggestion-item-title {
  font-size: 14px;
  color: #e5e5e5;
}

.suggestion-item-description {
  font-size: 12px;
  color: #888;
  margin-top: 2px;
}

.suggestion-item-preview {
  font-size: 12px;
  color: #666;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Canvas node styles */
.canvas-node {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  overflow: hidden;
}

.canvas-node canvas {
  display: block;
  cursor: crosshair;
}

.canvas-node.fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  border-radius: 0;
}

.canvas-toolbar {
  display: flex;
  gap: 4px;
  padding: 8px;
  background: #252525;
  border-top: 1px solid #2a2a2a;
}

.canvas-toolbar button {
  padding: 4px 8px;
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  border-radius: 4px;
  color: #999;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.canvas-toolbar button:hover {
  background: #2a2a2a;
  color: #fff;
}

.canvas-toolbar button.active {
  background: #3b82f6;
  border-color: #3b82f6;
  color: #fff;
}

/* Query node styles */
.query-node {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  padding: 12px;
}

.query-node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid #2a2a2a;
}

.query-node-results {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.query-node-result {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: #252525;
  border-radius: 4px;
  color: #e5e5e5;
  text-decoration: none;
  font-size: 13px;
  transition: background 0.15s ease;
}

.query-node-result:hover {
  background: #2a2a2a;
}

.query-node-empty {
  color: #666;
  font-size: 13px;
  text-align: center;
  padding: 16px;
}

body.attachment-modal-open {
  overflow: hidden;
}

/* Scrollbar styles */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #333;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #444;
}
