:root {
  --ink: #334155;
  --ink-strong: #0f172a;
  --muted: #64748b;
  --line: #dbe3ee;
  --bg: #f5f7fb;
  --card: #ffffff;
  --brand: #4f46e5;
  --brand-soft: #eef2ff;
  --danger: #dc2626;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--sans);
  color: var(--ink);
  background:
    linear-gradient(var(--bg), var(--bg)) padding-box,
    var(--bg);
  background-image:
    linear-gradient(rgba(79,70,229,.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(79,70,229,.05) 1px, transparent 1px);
  background-size: 24px 24px;
  display: flex;
  flex-direction: column;
}
button { font: inherit; cursor: pointer; }

/* top bar */
.bar {
  display: flex; align-items: center; gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,.85);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; }
.mark { display: inline-grid; grid-template-columns: 1fr 1fr; gap: 3px; width: 22px; height: 22px; }
.mark i { border: 2px solid var(--brand); border-radius: 4px; }
.mark i:last-child { background: var(--brand); }
.word { font-family: var(--mono); font-weight: 600; font-size: 17px; color: var(--ink-strong); letter-spacing: -.02em; }
.word b { color: var(--brand); }

.modes { display: flex; padding: 3px; background: #eef1f6; border-radius: 10px; margin-left: auto; }
.modes button {
  border: 0; background: transparent; padding: 6px 14px; border-radius: 8px;
  color: var(--muted); font-size: 13px; transition: background .15s, color .15s;
}
body[data-mode="edit"] [data-set-mode="edit"],
body[data-mode="split"] [data-set-mode="split"],
body[data-mode="preview"] [data-set-mode="preview"] {
  background: var(--card); color: var(--brand); box-shadow: 0 1px 2px rgba(15,23,42,.08);
}
.actions { display: flex; gap: 8px; }
.actions button {
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  padding: 6px 12px; border-radius: 8px; font-size: 13px;
  transition: border-color .15s, color .15s, transform .1s;
}
.actions button:hover { border-color: var(--brand); color: var(--brand); }
.actions button:active { transform: translateY(1px); }
.actions .danger:hover { border-color: var(--danger); color: var(--danger); }

/* stage */
.stage {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
  padding: 16px 20px 20px;
}
body[data-mode="edit"] .stage { grid-template-columns: 1fr; }
body[data-mode="edit"] .out { display: none; }
body[data-mode="preview"] .stage { grid-template-columns: minmax(0, 860px); justify-content: center; }
body[data-mode="preview"] .src { display: none; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
  box-shadow: 0 1px 0 rgba(15,23,42,.03), 0 8px 24px -16px rgba(15,23,42,.18);
}
.card-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 9px 16px; border-bottom: 1px dashed var(--line);
  font-family: var(--mono); font-size: 12px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--brand);
}
.card-head .meta { color: var(--muted); text-transform: none; letter-spacing: 0; }
#input {
  flex: 1; border: 0; outline: 0; resize: none; padding: 16px 18px;
  font-family: var(--mono); font-size: 13.5px; line-height: 1.7; color: var(--ink-strong);
  background: transparent; tab-size: 2;
}
#preview { flex: 1; overflow: auto; padding: 24px 32px 48px; }

/* rendered markdown */
.markdown-body { font-size: 15.5px; line-height: 1.75; color: var(--ink-strong); word-wrap: break-word; }
.markdown-body > *:first-child { margin-top: 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { line-height: 1.3; margin: 1.6em 0 .6em; font-weight: 700; }
.markdown-body h1 { font-size: 1.9em; padding-bottom: .3em; border-bottom: 2px solid var(--brand-soft); }
.markdown-body h2 { font-size: 1.45em; padding-bottom: .25em; border-bottom: 1px solid var(--line); }
.markdown-body h3 { font-size: 1.2em; }
.markdown-body a { color: var(--brand); text-decoration: none; border-bottom: 1px solid rgba(79,70,229,.3); }
.markdown-body a:hover { border-bottom-color: var(--brand); }
.markdown-body img { max-width: 100%; border-radius: 8px; }
.markdown-body blockquote {
  margin: 1em 0; padding: .6em 1em; color: var(--ink);
  border-left: 3px solid var(--brand); background: var(--brand-soft); border-radius: 0 8px 8px 0;
}
.markdown-body blockquote > :first-child { margin-top: 0; }
.markdown-body blockquote > :last-child { margin-bottom: 0; }
.markdown-body code { font-family: var(--mono); font-size: .88em; background: #f1f5f9; padding: .15em .4em; border-radius: 5px; }
.markdown-body pre { position: relative; background: #f8fafc; border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; overflow: auto; }
.markdown-body pre code { background: none; padding: 0; font-size: 13px; line-height: 1.6; }
.markdown-body pre .copy {
  position: absolute; top: 8px; right: 8px; font-size: 11px; padding: 3px 8px;
  border: 1px solid var(--line); background: var(--card); border-radius: 6px; color: var(--muted);
  opacity: 0; transition: opacity .15s;
}
.markdown-body pre:hover .copy { opacity: 1; }
.markdown-body pre .lang { position: absolute; bottom: 6px; right: 10px; font: 11px var(--mono); color: #94a3b8; }
.markdown-body table { border-collapse: collapse; display: block; overflow: auto; margin: 1em 0; }
.markdown-body th, .markdown-body td { border: 1px solid var(--line); padding: 6px 13px; }
.markdown-body th { background: #f1f5f9; font-weight: 600; }
.markdown-body tr:nth-child(2n) td { background: #fafbfd; }
.markdown-body hr { border: 0; border-top: 1px dashed var(--line); margin: 2em 0; }
.markdown-body ul.contains-task-list, .markdown-body li.task-list-item { list-style: none; }
.markdown-body ul.contains-task-list { padding-left: 1.2em; }
.markdown-body input[type="checkbox"] { accent-color: var(--brand); margin-right: .5em; }
.markdown-body .katex-display { overflow-x: auto; overflow-y: hidden; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--ink-strong); color: #fff; padding: 8px 16px; border-radius: 8px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* narrow screens: stack panes */
@media (max-width: 820px) {
  .bar { flex-wrap: wrap; padding: 10px 12px; gap: 10px; }
  .modes { margin-left: auto; }
  .actions { width: 100%; overflow-x: auto; }
  .actions button { flex: 1 0 auto; }
  .stage { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; padding: 10px; gap: 10px; }
  body[data-mode="edit"] .stage, body[data-mode="preview"] .stage { grid-template-rows: 1fr; grid-template-columns: 1fr; }
  #preview { padding: 18px 18px 40px; }
}

/* ---- v4: grouped toolbar, toggles, dark mode, mermaid, drop, print ---- */
.actions { flex-wrap: wrap; gap: 6px 10px; }
.actions .group { display: flex; gap: 4px; }
.actions .group + .group { padding-left: 10px; border-left: 1px solid var(--line); }
.actions .primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.actions .primary:hover { color: #fff; filter: brightness(1.08); }
.actions .toggle[aria-pressed="true"] { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }
.bar { flex-wrap: wrap; }

.markdown-body .mermaid { display: flex; justify-content: center; margin: 1.2em 0; overflow-x: auto; }
.markdown-body .mermaid svg { max-width: 100%; height: auto; }
.markdown-body .mermaid-error { color: var(--danger); font: 12px var(--mono); white-space: pre-wrap; }

#dropHint {
  position: fixed; inset: 0; z-index: 20; display: none; place-items: center;
  background: rgba(79,70,229,.12); backdrop-filter: blur(2px);
}
#dropHint span {
  padding: 18px 28px; border: 2px dashed var(--brand); border-radius: 14px;
  background: var(--card); color: var(--brand); font-weight: 500;
}
body.dragging #dropHint { display: grid; }

body.dark {
  --ink: #cbd5e1; --ink-strong: #e2e8f0; --muted: #94a3b8; --line: #2a3446;
  --bg: #0b1120; --card: #111827; --brand: #818cf8; --brand-soft: rgba(129,140,248,.14);
}
body.dark .bar { background: rgba(17,24,39,.85); }
body.dark .modes { background: #1f2937; }
body.dark .markdown-body code { background: #1e293b; }
body.dark .markdown-body pre { background: #0f172a; }
body.dark .markdown-body th { background: #1e293b; }
body.dark .markdown-body tr:nth-child(2n) td { background: #0f1729; }
body.dark #toast { background: #e2e8f0; color: #0f172a; }
body.dark { background-image:
  linear-gradient(rgba(129,140,248,.06) 1px, transparent 1px),
  linear-gradient(90deg, rgba(129,140,248,.06) 1px, transparent 1px); }

@media print {
  @page { margin: 16mm; }
  body, body.dark { background: #fff !important; display: block; }
  .bar, .src, .card-head, #toast, #dropHint { display: none !important; }
  .stage { display: block !important; padding: 0 !important; }
  .card.out { display: block !important; border: 0; box-shadow: none; overflow: visible; }
  #preview { overflow: visible; padding: 0; }
  body.dark .markdown-body { color: #0f172a; }
  .markdown-body pre { white-space: pre-wrap; break-inside: avoid; }
  .markdown-body pre .copy { display: none; }
  .markdown-body table, .markdown-body .mermaid, .markdown-body img { break-inside: avoid; }
}
body.dark .actions button { background: #1a2335; border-color: #334155; color: #e2e8f0; }
body.dark .actions button:hover { border-color: var(--brand); color: #c7d2fe; }
body.dark .actions .primary { background: #6366f1; border-color: #6366f1; color: #fff; }
body.dark .actions .toggle[aria-pressed="true"] { background: rgba(129,140,248,.22); border-color: var(--brand); color: #c7d2fe; }
body.dark .modes button { color: #94a3b8; }
body.dark[data-mode] .modes button[data-set-mode] { box-shadow: none; }
body.dark[data-mode="edit"] [data-set-mode="edit"],
body.dark[data-mode="split"] [data-set-mode="split"],
body.dark[data-mode="preview"] [data-set-mode="preview"] { background: #334155; color: #e0e7ff; }
.markdown-body pre code.hljs { background: transparent; padding: 0; }

/* ---- v8: JSON mode ---- */
.kinds { display: flex; padding: 3px; background: #eef1f6; border-radius: 10px; }
.kinds button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; color: var(--muted); font: 600 12.5px var(--mono); }
body[data-kind="md"] [data-set-kind="md"], body[data-kind="json"] [data-set-kind="json"] {
  background: var(--brand); color: #fff;
}
body.dark .kinds { background: #1f2937; }
body[data-kind="md"] .json-only, body[data-kind="json"] .md-only { display: none !important; }
body[data-kind="md"] .json-view { display: none; }
body[data-kind="json"] #preview { display: none; }

.json-view {
  flex: 1; overflow: auto; padding: 16px 20px 40px;
  font: 13px/1.7 var(--mono); color: var(--ink-strong);
}
.json-view details > summary { list-style: none; cursor: pointer; border-radius: 4px; }
.json-view details > summary::-webkit-details-marker { display: none; }
.json-view details > summary::before {
  content: "▸"; display: inline-block; width: 1em; margin-left: -1em; color: var(--muted);
  transition: transform .12s;
}
.json-view details[open] > summary::before { transform: rotate(90deg); }
.json-view summary:hover { background: var(--brand-soft); }
.json-view .j-row { padding-left: 1.1em; }
.json-view > .j-row { padding-left: 1em; }
.json-view .j-kids { border-left: 1px dashed var(--line); margin-left: -.55em; padding-left: .55em; }
.json-view .j-key { color: #7c3aed; cursor: pointer; }
.json-view .j-idx { color: var(--muted); cursor: pointer; }
.json-view .j-key:hover, .json-view .j-idx:hover { text-decoration: underline; }
.json-view .j-str { color: #059669; word-break: break-all; }
.json-view .j-num { color: #2563eb; }
.json-view .j-bool { color: #d97706; }
.json-view .j-null { color: #94a3b8; font-style: italic; }
.json-view .j-brace { color: var(--muted); }
.json-view .j-count { color: var(--muted); font-size: 11px; margin: 0 .5em; }
.json-view details[open] > summary .j-count, .json-view details[open] > summary .j-close-inline { display: none; }
.json-view .j-close { padding-left: 0; }
.json-view .j-tag {
  margin-left: .6em; padding: 0 6px; border-radius: 4px; font-size: 10.5px;
  background: var(--brand-soft); color: var(--brand);
}
.json-error {
  margin: 4px 0 12px; padding: 12px 14px; border-radius: 10px;
  background: rgba(220,38,38,.08); border: 1px solid rgba(220,38,38,.35); color: var(--danger);
  font: 13px/1.6 var(--mono); white-space: pre-wrap;
}
.json-note {
  margin: 0 0 10px; padding: 8px 12px; border-radius: 8px; font-size: 12px;
  background: rgba(217,119,6,.1); color: #b45309; font-family: var(--sans);
}
.json-empty { color: var(--muted); font-family: var(--sans); }
body.dark .json-view .j-key { color: #c4b5fd; }
body.dark .json-view .j-str { color: #6ee7b7; }
body.dark .json-view .j-num { color: #93c5fd; }
body.dark .json-view .j-bool { color: #fcd34d; }
body.dark .json-note { color: #fcd34d; }

/* ---- v9: compact toolbar on phones: one horizontally scrollable row ---- */
@media (max-width: 820px) {
  .bar { gap: 8px; }
  .brand .word { display: none; }
  .kinds, .modes { flex-shrink: 0; }
  .modes { margin-left: auto; }
  .actions {
    flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none;
    -webkit-overflow-scrolling: touch; padding-bottom: 2px;
    mask-image: linear-gradient(90deg, #000 92%, transparent);
  }
  .actions::-webkit-scrollbar { display: none; }
  .actions .group { flex-shrink: 0; }
  .actions button { flex: 0 0 auto; padding: 6px 10px; }
  .card-head { padding: 7px 12px; }
  #input { font-size: 14px; padding: 12px 14px; }
}
