:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --radius: 9px;
  --radius-sm: 7px;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-soft: rgba(99, 102, 241, 0.12);
  --danger: #ef4444;
  --bar-h: 54px;
  --tool-h: 46px;
  --status-h: 30px;
  --transition: 0.18s ease;
}

:root[data-theme="light"] {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --editor-bg: #ffffff;
  --preview-bg: #ffffff;
  --text: #1f2328;
  --text-soft: #57606a;
  --text-muted: #8b949e;
  --border: #e4e7ec;
  --border-strong: #d3d8e0;
  --hover: rgba(15, 23, 42, 0.055);
  --active: rgba(99, 102, 241, 0.1);
  --code-bg: #f2f4f7;
  --code-inline: #eef0f4;
  --pre-bg: #f6f8fa;
  --shadow: 0 8px 30px rgba(15, 23, 42, 0.12);
  --scroll-thumb: #cfd5df;
}

:root[data-theme="dark"] {
  --bg: #0e1015;
  --panel: #151922;
  --editor-bg: #12151d;
  --preview-bg: #0e1015;
  --text: #e6e8ee;
  --text-soft: #a7b0c0;
  --text-muted: #6b7484;
  --border: #232936;
  --border-strong: #313a4a;
  --hover: rgba(255, 255, 255, 0.06);
  --active: rgba(129, 140, 248, 0.16);
  --code-bg: #1b2130;
  --code-inline: #232a38;
  --pre-bg: #171c27;
  --shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
  --scroll-thumb: #333c4d;
  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-soft: rgba(129, 140, 248, 0.16);
}

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

html, body { height: 100%; }

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  font-family: var(--font-sans);
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font-family: inherit; color: inherit; cursor: pointer; border: none; background: none; }
svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.app-bar {
  flex: none;
  height: var(--bar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: 8px;
  font-weight: 800; font-size: 17px; color: #fff;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.35);
}
.brand-name { font-weight: 650; font-size: 15.5px; letter-spacing: 0.2px; white-space: nowrap; }

.app-actions { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: var(--radius-sm);
  color: var(--text-soft);
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn:active { transform: scale(0.94); }

#themeToggle .ico-sun { display: none; }
:root[data-theme="dark"] #themeToggle .ico-moon { display: none; }
:root[data-theme="dark"] #themeToggle .ico-sun { display: block; }

.pill-btn {
  height: 32px;
  padding: 0 13px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  transition: filter var(--transition);
}
.pill-btn:hover { filter: brightness(0.96); }

.install-btn {
  display: flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 14px;
  border-radius: 999px;
  font-size: 13px; font-weight: 600; color: #fff;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.4);
}
.install-btn svg { width: 16px; height: 16px; }
.install-btn:hover { filter: brightness(1.06); }

.segmented {
  display: flex;
  padding: 3px;
  gap: 2px;
  background: var(--hover);
  border-radius: 9px;
}
.segmented button {
  width: 34px; height: 28px;
  display: grid; place-items: center;
  border-radius: 6px;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
}
.segmented button svg { width: 17px; height: 17px; }
.segmented button:hover { color: var(--text); }
.segmented button.active { background: var(--panel); color: var(--accent); box-shadow: 0 1px 3px rgba(0,0,0,0.12); }

.menu-wrap { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 190px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 11px;
  box-shadow: var(--shadow);
  z-index: 50;
  animation: menuIn 0.14s ease;
}
@keyframes menuIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }
.menu button {
  width: 100%;
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 11px;
  border-radius: 8px;
  font-size: 13.5px;
  text-align: left;
  transition: background var(--transition);
}
.menu button:hover { background: var(--hover); }
.menu button em { font-style: normal; font-size: 12px; color: var(--text-muted); font-family: var(--font-mono); }

.toolbar {
  flex: none;
  height: var(--tool-h);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 14px;
  overflow-x: auto;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.tool {
  flex: none;
  min-width: 34px; height: 32px;
  display: grid; place-items: center;
  padding: 0 6px;
  border-radius: var(--radius-sm);
  color: var(--text-soft);
  transition: background var(--transition), color var(--transition);
}
.tool:hover { background: var(--hover); color: var(--text); }
.tool:active { transform: scale(0.93); }
.tool svg { width: 18px; height: 18px; }
.ico-txt { font-size: 13px; font-weight: 700; letter-spacing: 0.3px; line-height: 1; }
.tool-sep { flex: none; width: 1px; height: 20px; margin: 0 5px; background: var(--border); }

.workspace {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.pane { min-width: 0; min-height: 0; display: flex; overflow: hidden; }
.editor-pane { flex: 1 1 50%; background: var(--editor-bg); }
.preview-pane { flex: 1 1 50%; background: var(--preview-bg); }

.splitter {
  flex: none;
  width: 1px;
  background: var(--border);
  cursor: col-resize;
  position: relative;
  transition: background var(--transition);
}
.splitter::after {
  content: "";
  position: absolute;
  inset: 0 -4px;
}
.splitter:hover, .splitter.dragging { background: var(--accent); }

#source {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  padding: 26px 30px 60px;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 14.5px;
  line-height: 1.75;
  tab-size: 2;
}
#source::placeholder { color: var(--text-muted); }

.wysiwyg {
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  padding: 26px 30px 60px;
  outline: none;
  overflow: auto;
  color: var(--text);
  font-size: 15.5px;
  line-height: 1.75;
}
.wysiwyg:empty::before { content: "在这里输入内容…"; color: var(--text-muted); }

.preview-pane { overflow: auto; }
#preview { padding: 26px 34px 80px; max-width: 860px; margin: 0 auto; width: 100%; }

.markdown-body { color: var(--text); font-size: 15.5px; line-height: 1.78; word-wrap: break-word; }
.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.5em 0 0.6em;
  font-weight: 680;
  line-height: 1.3;
  letter-spacing: 0.1px;
}
.markdown-body h1 { font-size: 2em; padding-bottom: 0.3em; border-bottom: 1px solid var(--border); }
.markdown-body h2 { font-size: 1.55em; padding-bottom: 0.28em; border-bottom: 1px solid var(--border); }
.markdown-body h3 { font-size: 1.3em; }
.markdown-body h4 { font-size: 1.12em; }
.markdown-body h5 { font-size: 1em; }
.markdown-body h6 { font-size: 0.92em; color: var(--text-soft); }
.markdown-body p { margin: 0 0 1em; }
.markdown-body a { color: var(--accent); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }
.markdown-body strong { font-weight: 680; }
.markdown-body ul, .markdown-body ol { margin: 0 0 1em; padding-left: 1.6em; }
.markdown-body li { margin: 0.28em 0; }
.markdown-body li > ul, .markdown-body li > ol { margin: 0.3em 0; }
.markdown-body input[type="checkbox"] { margin-right: 0.5em; vertical-align: middle; accent-color: var(--accent); }
.markdown-body blockquote {
  margin: 0 0 1em;
  padding: 0.4em 1em;
  color: var(--text-soft);
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
}
.markdown-body blockquote > *:last-child { margin-bottom: 0; }
.markdown-body code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  padding: 0.16em 0.4em;
  border-radius: 5px;
  background: var(--code-inline);
}
.markdown-body pre {
  margin: 0 0 1.1em;
  padding: 16px 18px;
  overflow: auto;
  background: var(--pre-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  line-height: 1.6;
}
.markdown-body pre code {
  padding: 0;
  background: none;
  font-size: 13.5px;
  color: var(--text);
  white-space: pre;
}
.markdown-body hr { height: 1px; border: none; background: var(--border); margin: 1.8em 0; }
.markdown-body img { max-width: 100%; height: auto; border-radius: 8px; }
.markdown-body table { border-collapse: collapse; width: auto; max-width: 100%; margin: 0 0 1.1em; display: block; overflow-x: auto; }
.markdown-body th, .markdown-body td { padding: 8px 14px; border: 1px solid var(--border-strong); }
.markdown-body th { background: var(--code-bg); font-weight: 650; }
.markdown-body tr:nth-child(2n) td { background: var(--hover); }
.markdown-body kbd {
  font-family: var(--font-mono); font-size: 0.82em;
  padding: 0.15em 0.45em;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--code-bg);
}
.markdown-body del { color: var(--text-muted); }

.statusbar {
  flex: none;
  height: var(--status-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--panel);
  border-top: 1px solid var(--border);
  user-select: none;
}
.save-status { display: flex; align-items: center; gap: 6px; }
.save-status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); }
.save-status.saved::before { background: #22c55e; }
.save-status.saving::before { background: #f59e0b; }

::-webkit-scrollbar { width: 11px; height: 11px; }
::-webkit-scrollbar-thumb { background: var(--scroll-thumb); border-radius: 8px; border: 3px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: var(--border-strong); background-clip: content-box; }
::-webkit-scrollbar-track { background: transparent; }

body.view-editor .preview-pane,
body.view-editor .splitter { display: none; }
body.view-editor .editor-pane { flex: 1 1 100%; }
body.view-preview .editor-pane,
body.view-preview .splitter { display: none; }
body.view-preview .preview-pane { flex: 1 1 100%; }

body.split-custom .editor-pane { flex: none; }

::selection { background: var(--accent-soft); }

@media (max-width: 720px) {
  .brand-name { display: none; }
  .app-bar { padding: 0 10px; gap: 6px; }
  .app-actions { gap: 4px; }
  #source, .wysiwyg { padding: 18px 16px 50px; }
  #preview { padding: 18px 16px 70px; }
  .toolbar { padding: 0 8px; }
  body.view-split .workspace { flex-direction: column; }
  body.view-split .editor-pane { flex: 1 1 50%; border-bottom: 1px solid var(--border); }
  body.view-split .splitter { display: none; }
  body.view-split .preview-pane { flex: 1 1 50%; }
  .install-btn span { display: none; }
  .install-btn { padding: 0 10px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
