:root {
  color-scheme: light;
  --bg: #f5f5f2;
  --surface: #ffffff;
  --surface-soft: #efeee9;
  --surface-strong: #e4e1d8;
  --text: #20201d;
  --muted: #686862;
  --muted-strong: #4a4a45;
  --border: #d8d5cb;
  --accent: #2f6f63;
  --accent-strong: #214f47;
  --accent-soft: #dcebe6;
  --danger: #a33d31;
  --code-bg: #f0f0ec;
  --shadow: 0 18px 50px rgba(36, 34, 27, 0.1);
  --sidebar-width: 320px;
  --reader-width: clamp(700px, 62vw, 920px);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171817;
  --surface: #20221f;
  --surface-soft: #292c28;
  --surface-strong: #353a34;
  --text: #efeee7;
  --muted: #aaa79e;
  --muted-strong: #d3d0c7;
  --border: #3d413b;
  --accent: #8bcbbd;
  --accent-strong: #b6e3d8;
  --accent-soft: #243d38;
  --danger: #f09a90;
  --code-bg: #171917;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input { font: inherit; }
button, label { -webkit-tap-highlight-color: transparent; }
a { color: var(--accent-strong); }

input[type="file"] {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.sidebar-toggle {
  position: fixed;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
}

body.sidebar-collapsed .app-shell {
  grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  gap: 18px;
  height: 100vh;
  padding: 22px 18px;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--surface);
}

body.sidebar-collapsed .sidebar {
  width: 0;
  padding-right: 0;
  padding-left: 0;
  border-right: 0;
  transform: translateX(-100%);
}

.brand-row, .section-title-row, .topbar, .topbar-left, .topbar-actions {
  display: flex;
  align-items: center;
}

.brand-row { justify-content: space-between; gap: 12px; }

.eyebrow, .doc-meta p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.eyebrow { text-transform: uppercase; letter-spacing: 0.08em; }
.brand-row h1, .section-title-row h2 { margin: 0; letter-spacing: 0; }
.brand-row h1 { margin-top: 3px; font-size: 24px; line-height: 1.15; }
.section-title-row { justify-content: space-between; margin-bottom: 8px; }
.section-title-row h2 { color: var(--muted-strong); font-size: 13px; font-weight: 700; }
.muted-count { min-width: 24px; color: var(--muted); font-size: 12px; text-align: right; }

.import-button, .primary-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 42px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #ffffff;
  font-weight: 700;
  cursor: pointer;
}

[data-theme="dark"] .import-button,
[data-theme="dark"] .primary-action { color: #10201d; }

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--muted);
}

.search-wrap input {
  min-width: 0;
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
}

.panel-section { min-height: 0; }
.doc-list, .outline-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow: auto;
  padding-right: 3px;
}

.doc-list { max-height: 34vh; }
.outline-section { display: flex; flex: 1; flex-direction: column; min-height: 160px; }
.outline-list { min-height: 0; }

.doc-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 32px;
  align-items: center;
  gap: 9px;
  width: 100%;
  min-height: 54px;
  padding: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
}

.doc-item:hover, .doc-item.is-active { border-color: var(--border); background: var(--surface-soft); }
.doc-select {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  align-items: center;
  gap: 9px;
  min-width: 0;
  min-height: 42px;
  padding: 2px 0 2px 4px;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.doc-icon, .doc-remove { display: inline-flex; align-items: center; justify-content: center; color: var(--muted); }
.doc-remove {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  cursor: pointer;
}
.doc-remove:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.doc-text { min-width: 0; }
.doc-text strong, .doc-text small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-text strong { font-size: 14px; line-height: 1.35; }
.doc-text small { margin-top: 3px; color: var(--muted); font-size: 12px; }

.outline-list a {
  display: block;
  overflow: hidden;
  padding: 6px 8px;
  border-radius: 7px;
  color: var(--muted-strong);
  font-size: 13px;
  line-height: 1.35;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.outline-list a:hover { background: var(--surface-soft); color: var(--text); }
.outline-list a[data-depth="2"] { padding-left: 18px; }
.outline-list a[data-depth="3"] { padding-left: 30px; }
.outline-list a[data-depth="4"], .outline-list a[data-depth="5"], .outline-list a[data-depth="6"] { padding-left: 42px; }

.workspace { min-width: 0; min-height: 100vh; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  justify-content: space-between;
  gap: 14px;
  min-height: 68px;
  padding: 12px clamp(16px, 4vw, 44px);
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(14px);
}

.topbar-left, .topbar-actions { gap: 10px; min-width: 0; }
.doc-meta { min-width: 0; }
.doc-meta strong {
  display: block;
  overflow: hidden;
  max-width: min(58vw, 720px);
  font-size: 16px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.icon-button:hover { background: var(--surface-soft); }
.mobile-only { display: none; }

.reader-wrap {
  display: grid;
  width: min(100%, var(--reader-width));
  min-height: calc(100vh - 68px);
  margin: 0 auto;
  padding: clamp(22px, 5vw, 56px) clamp(18px, 4vw, 34px) 72px;
}

.reader-wrap.is-dragging {
  outline: 2px dashed var(--accent);
  outline-offset: -16px;
  background: color-mix(in srgb, var(--accent-soft) 42%, transparent);
}
.reader { width: 100%; }

.empty-state {
  align-self: center;
  justify-self: center;
  width: min(100%, 560px);
  padding: 38px 30px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
}

.empty-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 54px;
  height: 54px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.empty-state h2 {
  margin: 18px 0 10px;
  font-size: clamp(24px, 5vw, 34px);
  line-height: 1.15;
  letter-spacing: 0;
}

.empty-state p {
  margin: 0 auto 22px;
  max-width: 430px;
  color: var(--muted-strong);
  font-size: 15px;
  line-height: 1.75;
}

.primary-action { min-width: 190px; padding: 0 16px; }
.scrim { display: none; }

.sync-panel {
  display: grid;
  gap: 9px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-soft);
}

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

.sync-controls,
.sync-actions {
  display: grid;
  gap: 8px;
}

.sync-controls input {
  width: 100%;
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: 0;
  background: var(--surface);
  color: var(--text);
}

.sync-actions {
  grid-template-columns: 1fr 1fr;
}

.soft-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}

.soft-button:hover {
  background: var(--surface-strong);
}

.markdown-body {
  font-family: ui-serif, Georgia, "Times New Roman", "Noto Serif SC", "Songti SC", serif;
  font-size: 17px;
  line-height: 1.82;
  overflow-wrap: anywhere;
}

.markdown-body > *:first-child { margin-top: 0; }

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin: 1.8em 0 0.75em;
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-weight: 800;
  line-height: 1.28;
  letter-spacing: 0;
}

.markdown-body h1 {
  padding-bottom: 0.35em;
  border-bottom: 1px solid var(--border);
  font-size: clamp(30px, 6vw, 46px);
}

.markdown-body h2 {
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--border);
  font-size: clamp(24px, 4vw, 32px);
}

.markdown-body h3 { font-size: 22px; }
.markdown-body h4 { font-size: 19px; }
.markdown-body h5, .markdown-body h6 { font-size: 17px; }

.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body blockquote,
.markdown-body pre,
.markdown-body table { margin: 0 0 1.15em; }

.markdown-body ul, .markdown-body ol { padding-left: 1.55em; }
.markdown-body li + li { margin-top: 0.3em; }

.markdown-body blockquote {
  padding: 0.6em 1em;
  border-left: 4px solid var(--accent);
  background: var(--surface-soft);
  color: var(--muted-strong);
}

.markdown-body code {
  padding: 0.12em 0.34em;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--code-bg);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.88em;
}

.markdown-body pre {
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--code-bg);
  line-height: 1.6;
}

.markdown-body pre code {
  padding: 0;
  border: 0;
  background: transparent;
  font-size: 0.9em;
}

.markdown-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.4em auto;
  border-radius: 8px;
}

.markdown-body table {
  display: block;
  width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
}

.markdown-body th, .markdown-body td {
  padding: 9px 11px;
  border: 1px solid var(--border);
  vertical-align: top;
}

.markdown-body th { background: var(--surface-soft); font-weight: 700; }
.markdown-body hr { margin: 2em 0; border: 0; border-top: 1px solid var(--border); }
.markdown-body mark { padding: 0 0.2em; background: #ffe08a; color: #1d1b15; }

.markdown-body input[type="checkbox"] {
  position: static;
  width: auto;
  height: auto;
  margin-right: 0.45em;
  opacity: 1;
  pointer-events: auto;
}

.empty-text {
  padding: 10px 8px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

@media (max-width: 860px) {
  .app-shell { display: block; }
  .mobile-only { display: inline-flex; }
  #openSidebar {
    display: inline-flex;
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
  }
  #closeSidebar { display: inline-flex; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    width: min(88vw, var(--sidebar-width));
    transform: translateX(-102%);
    transition: transform 180ms ease;
    box-shadow: var(--shadow);
  }

  body.sidebar-open .sidebar,
  #sidebarToggle:checked ~ .app-shell .sidebar { transform: translateX(0); }
  body.sidebar-collapsed .sidebar { width: min(88vw, var(--sidebar-width)); padding: 22px 18px; border-right: 1px solid var(--border); }

  .scrim {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: none;
    background: rgba(0, 0, 0, 0.38);
  }

  body.sidebar-open .scrim,
  #sidebarToggle:checked ~ .app-shell .scrim {
    display: block;
  }

  .topbar { min-height: 62px; }
  .doc-meta strong { max-width: calc(100vw - 180px); font-size: 15px; }
  .reader-wrap { min-height: calc(100vh - 62px); padding: 22px 18px 54px; }
  .markdown-body { font-size: 16px; line-height: 1.78; }
  .doc-list { max-height: 28vh; }
}

@media (min-width: 861px) {
  #closeSidebar { display: inline-flex; }
  body.sidebar-collapsed #openSidebar { display: inline-flex; }
}

@media (max-width: 520px) {
  .topbar { padding: 10px 12px; }
  .topbar-actions { gap: 7px; }
  .icon-button { width: 38px; height: 38px; flex-basis: 38px; }
  .doc-meta p { display: none; }
  .doc-meta strong { max-width: calc(100vw - 168px); }
  .empty-state { padding: 30px 20px; }
  .empty-state h2 { font-size: 25px; }
  .markdown-body h3 { font-size: 20px; }
  .markdown-body table { font-size: 14px; }
}
