:root {
  color-scheme: light;
  --sidebar-width: 220px;
  --tree-width: 280px;
  --agent-width: minmax(300px, 360px);
  --rail-width: 48px;
  --bg: #f6f4ef;
  --panel: #ffffff;
  --line: #d9d3c7;
  --text: #24211d;
  --muted: #746b60;
  --accent: #276d63;
  --accent-2: #8a4b38;
  --danger: #9f2d2d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
}

button,
input,
textarea {
  font: inherit;
}

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

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(360px, 1fr) var(--agent-width);
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.app.sidebar-collapsed {
  --sidebar-width: var(--rail-width);
}

.app.tree-collapsed {
  --tree-width: var(--rail-width);
}

.app.agent-collapsed {
  --agent-width: var(--rail-width);
}

.sidebar,
.agent {
  background: var(--panel);
  border-right: 1px solid var(--line);
  min-width: 0;
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
}

.agent {
  border-right: 0;
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.brand {
  padding: 14px 10px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 66px;
}

.brand-copy {
  min-width: 0;
}

.icon-button {
  width: 32px;
  height: 32px;
  flex: 0 0 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
}

h1,
h2,
h3 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 18px;
}

h2 {
  font-size: 16px;
}

h3 {
  font-size: 18px;
}

.brand span,
.agent-head span,
.label,
.status {
  color: var(--muted);
  font-size: 12px;
}

.book-list {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 10px;
}

.book-item {
  display: block;
  width: 100%;
  text-align: left;
  margin-bottom: 6px;
}

.book-item.active {
  background: #e4f0ec;
  border-color: var(--accent);
  color: #164a43;
}

.workspace {
  display: flex;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
}

.toolbar {
  height: 66px;
  flex: 0 0 66px;
  border-bottom: 1px solid var(--line);
  background: #fbfaf7;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 16px;
}

.document-title {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.current-path {
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 48vw;
}

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

.danger {
  color: var(--danger);
}

.content {
  display: grid;
  grid-template-columns: var(--tree-width) minmax(0, 1fr);
  min-height: 0;
  flex: 1;
  overflow: hidden;
}

.tree {
  overflow: auto;
  padding: 10px 8px;
  border-right: 1px solid var(--line);
  background: #fbfaf7;
}

.tree ul {
  list-style: none;
  margin: 0;
  padding-left: 14px;
}

.tree > ul {
  padding-left: 0;
}

.tree button {
  width: 100%;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 5px 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree button.active {
  color: var(--accent);
  background: #e4f0ec;
}

.folder {
  color: var(--accent-2);
  font-weight: 650;
}

.editor-wrap {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.reader {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 30px clamp(24px, 6vw, 72px);
  color: #1f1c18;
  background: #fffdf8;
  font-family: ui-serif, "Noto Serif CJK SC", "Songti SC", serif;
  font-size: 17px;
  line-height: 1.9;
}

.reader:empty::before {
  content: "打开文档后在这里阅读。";
  color: var(--muted);
}

.reader h1,
.reader h2,
.reader h3,
.reader h4 {
  margin: 1.4em 0 0.7em;
  color: #181511;
  line-height: 1.35;
}

.reader h1 {
  font-size: 28px;
}

.reader h2 {
  font-size: 23px;
}

.reader h3 {
  font-size: 19px;
}

.reader p {
  margin: 0 0 1em;
}

.reader ul,
.reader ol {
  margin: 0 0 1em;
  padding-left: 1.5em;
}

.reader blockquote {
  margin: 0 0 1em;
  padding: 0.2em 0 0.2em 1em;
  border-left: 3px solid #c9b9a7;
  color: #5d554c;
}

.reader code {
  border: 1px solid #e2d9cc;
  border-radius: 4px;
  background: #f7f1e8;
  padding: 0.05em 0.3em;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.9em;
}

.reader pre {
  margin: 0 0 1em;
  overflow: auto;
  border: 1px solid #e2d9cc;
  border-radius: 6px;
  background: #f7f1e8;
  padding: 12px;
}

.reader pre code {
  border: 0;
  background: transparent;
  padding: 0;
}

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

.editor-wrap.editing .reader {
  display: none;
}

.editor-wrap:not(.editing) #editor {
  display: none;
}

#editor {
  flex: 1;
  min-height: 0;
  resize: none;
  border: 0;
  outline: none;
  padding: 24px 28px;
  line-height: 1.85;
  color: #1f1c18;
  background: #fffdf8;
  font-family: ui-serif, "Noto Serif CJK SC", "Songti SC", serif;
  font-size: 17px;
}

.status {
  height: 30px;
  flex: 0 0 30px;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  padding: 0 12px;
  background: #fbfaf7;
}

.agent-head {
  padding: 14px 10px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex: 0 0 auto;
}

.agent-head > div {
  min-width: 0;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.quick-actions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 10px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.chat {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  line-height: 1.65;
  white-space: pre-wrap;
  background: #fff;
}

.message.user {
  border-color: #b9d6cf;
  background: #eef7f4;
}

.message.assistant {
  border-color: #dcc8bd;
  background: #fff9f4;
}

.agent-input {
  padding: 10px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  flex: 0 0 auto;
}

.app.sidebar-collapsed .brand {
  justify-content: center;
  padding-left: 8px;
  padding-right: 8px;
}

.app.sidebar-collapsed .brand-copy,
.app.sidebar-collapsed .book-list {
  display: none;
}

.app.tree-collapsed .tree {
  padding: 0;
  overflow: hidden;
}

.app.tree-collapsed .tree > * {
  display: none;
}

.app.tree-collapsed .document-title > div {
  display: none;
}

.app.agent-collapsed .agent {
  overflow: hidden;
}

.app.agent-collapsed .agent-head {
  height: 100%;
  justify-content: center;
  align-items: flex-start;
  padding-left: 8px;
  padding-right: 8px;
}

.app.agent-collapsed .agent-head > div,
.app.agent-collapsed .toggle,
.app.agent-collapsed .quick-actions,
.app.agent-collapsed .chat,
.app.agent-collapsed .agent-input {
  display: none;
}

#agentPrompt {
  min-height: 72px;
  max-height: 160px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
}

dialog {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
}

.dialog-body {
  width: min(520px, 90vw);
  padding: 18px;
}

.dialog-body input {
  width: 100%;
  margin: 14px 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.dialog-body menu {
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

@media (max-width: 1100px) {
  .app {
    --sidebar-width: 180px;
    --agent-width: minmax(280px, 320px);
    grid-template-columns: var(--sidebar-width) minmax(320px, 1fr) var(--agent-width);
  }

  .app.sidebar-collapsed {
    --sidebar-width: var(--rail-width);
  }

  .app.agent-collapsed {
    --agent-width: var(--rail-width);
  }
}

@media (max-width: 760px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(420px, 1fr) minmax(320px, 42vh);
    min-height: 100dvh;
    height: 100dvh;
  }

  .app.agent-collapsed {
    grid-template-rows: auto minmax(420px, 1fr) var(--rail-width);
  }

  .sidebar {
    max-height: 180px;
  }

  .content {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(120px, 28%) minmax(0, 1fr);
  }

  .app.tree-collapsed .content {
    grid-template-rows: var(--rail-width) minmax(0, 1fr);
  }

  .tree {
    max-height: 260px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  #editor {
    min-height: 0;
  }
}
