:root {
  color-scheme: light;
  --bg: #f7f7f8;
  --panel: #ffffff;
  --text: #121314;
  --muted: #6d7175;
  --border: #e5e7eb;
  --primary: #111827;
  --accent: #2563eb;
}

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

body {
  font-family: "Inter", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

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

.app.sidebar-collapsed {
  grid-template-columns: 64px 1fr;
}

.sidebar {
  border-right: 1px solid var(--border);
  padding: 20px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  gap: 16px;
  transition: width 0.2s ease;
  overflow: hidden;
}

.sidebar.collapsed {
  width: 64px;
  padding: 16px 12px;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}


.sidebar-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}


.sidebar.collapsed .sidebar-content {
  display: none;
}

.brand {
  font-weight: 600;
  letter-spacing: 0.2px;
}

.sidebar.collapsed .brand {
  font-size: 14px;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  color: var(--muted);
}

.search {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
}

.history {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
  background: var(--panel);
  transition: border 0.2s ease;
  position: relative;
  flex: 0 0 auto;
  align-self: stretch;
  overflow: hidden;
}

.history-item.dragging {
  opacity: 0.5;
}

.history-outline {
  margin-top: 8px;
  padding: 8px 0 0 12px;
  display: none;
  gap: 6px;
  flex-direction: column;
  border-top: 1px dashed var(--border);
  max-height: 140px;
  overflow: auto;
}

.history-item.expanded .history-outline {
  display: flex;
}

.history-outline .outline-item {
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-outline .outline-item.level-2 {
  padding-left: 12px;
}

.history-item.drop-before {
  box-shadow: inset 0 2px 0 0 var(--accent);
}

.history-item.drop-after {
  box-shadow: inset 0 -2px 0 0 var(--accent);
}

.history-item:hover {
  border-color: var(--accent);
}

.history-item.active {
  border-color: var(--primary);
  background: #f3f4f6;
}

.history-item .title {
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 4px;
}

.history-item .meta {
  font-size: 12px;
  color: var(--muted);
}

.history-item .delete {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel);
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}

.history-item:hover .delete {
  opacity: 1;
  pointer-events: auto;
}

.history-item .delete:hover {
  color: #ef4444;
  border-color: #ef4444;
}

.main {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.toolbar button {
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.toolbar button.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.toolbar button.ghost {
  background: transparent;
}

.toolbar button.icon,
.sidebar button.icon {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.status {
  color: var(--muted);
  font-size: 13px;
}

.content {
  flex: 1;
  display: grid;
  grid-template-columns: var(--col-editor, 1.1fr) 6px var(--col-preview, 1fr);
  gap: 12px;
  padding: 16px;
  overflow: hidden;
  height: 100%;
}

section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.editor {
  min-height: 0;
  height: 100%;
}

.right-pane {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  height: 100%;
}

.right-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
  height: 100%;
}

.panel {
  display: none;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.panel.active {
  display: flex;
}

.node-content {
  border-top: 1px solid var(--border);
  flex: 2 1 66%;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.node-content.hidden {
  display: none;
}

#treePanel {
  display: none;
  flex: 1;
}

#treePanel.active {
  display: flex;
}

#treePanel .tree-view {
  flex: 1 1 34%;
  min-height: 200px;
}

.resizer {
  width: 6px;
  border-radius: 10px;
  background: transparent;
  cursor: col-resize;
  position: relative;
}

.resizer::after {
  content: "";
  position: absolute;
  top: 12px;
  bottom: 12px;
  left: 2px;
  width: 2px;
  background: var(--border);
  border-radius: 10px;
}

.content.tree-hidden .tree,
.content.tree-hidden .tree-resizer {
  display: none;
}

.section-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--muted);
  min-height: 44px;
  display: flex;
  align-items: center;
}

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

.header-icons {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.header-icons button {
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.editor-host {
  flex: 1;
  overflow: hidden;
  min-height: 0;
  height: 100%;
  display: flex;
}

.CodeMirror {
  height: 100%;
  font-size: 14px;
  font-family: "JetBrains Mono", "Fira Code", monospace;
  border-left: 1px solid var(--border);
  flex: 1;
}

.CodeMirror-scroll,
.CodeMirror-sizer {
  min-height: 100%;
}

.CodeMirror-scroll {
  height: 100%;
}

.CodeMirror-gutters {
  background: #f9fafb;
  border-right: 1px solid var(--border);
}

.preview-content {
  padding: 16px 16px 16px 24px;
  overflow: auto;
  flex: 1;
  min-height: 0;
}

.preview-content h1,
.preview-content h2,
.preview-content h3,
.preview-content h4,
.preview-content h5,
.preview-content h6 {
  margin: 14px 0 8px;
  color: #111827;
}

.preview-content p {
  margin-bottom: 10px;
  color: #1f2937;
}

.preview-content a {
  color: #2563eb;
}

.preview-content code {
  background: #f3f4f6;
  color: #0f172a;
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 12px;
}

.preview-content pre code {
  display: block;
  padding: 12px;
  overflow: auto;
}

.preview-content blockquote {
  border-left: 3px solid #cbd5f5;
  padding-left: 12px;
  color: #475569;
}

.preview-content hr {
  border: none;
  border-top: 1px solid #e5e7eb;
  margin: 16px 0;
}

.preview-content table {
  border-collapse: collapse;
  width: 100%;
  margin: 12px 0;
}

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

.tree-view {
  padding: 16px;
  overflow: auto;
  font-size: 14px;
  flex: 1;
  position: relative;
}

.tree-view.maximized {
  flex: 1 1 100%;
}

.tree-view.maximized + .node-content {
  display: none;
}

.tree-actions {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 6px;
  z-index: 2;
}

.tree-tooltip {
  position: absolute;
  background: rgba(17, 24, 39, 0.9);
  color: white;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 12px;
  max-width: 260px;
  pointer-events: none;
  transform: translate(-50%, -100%);
  white-space: nowrap;
}

.tree-actions button {
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 6px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.tree-svg {
  width: 100%;
  height: 100%;
}

.tree-boundary {
  fill: none;
  stroke: var(--border);
  stroke-dasharray: 3 4;
}

.tree-links path,
.tree-links {
  stroke: var(--border);
  stroke-width: 1;
  fill: none;
}

.tree-node {
  fill: var(--accent);
  cursor: pointer;
}

.tree-node.level-1 {
  fill: #2563eb;
}

.tree-node.level-2 {
  fill: #10b981;
}

.tree-node.level-3 {
  fill: #f59e0b;
}

.tree-node.level-4,
.tree-node.level-5,
.tree-node.level-6 {
  fill: #ef4444;
}

.tree-node.dimmed,
.tree-label-group.dimmed {
  opacity: 0.35;
}

.tree-node.root {
  fill: var(--primary);
}

.tree-label {
  font-size: 12px;
  fill: var(--text);
}

.tree-label-bg {
  fill: rgba(255, 255, 255, 0.85);
  stroke: var(--border);
}

.muted {
  color: var(--muted);
  font-size: 13px;
}

@media (max-width: 1200px) {
  .content {
    grid-template-columns: 1fr;
  }

  .app {
    grid-template-columns: 220px 1fr;
  }

  .resizer {
    display: none;
  }
}
