/* ---------- tokens ---------- */
:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f0f1f3;
  --border: #dfe1e5;
  --text: #1c1e21;
  --muted: #646a73;
  --accent: #3b6ef5;
  --accent-text: #ffffff;
  --accent-soft: #e6edfe;
  --danger: #c93a3a;
  --ok: #1f8a4c;
  --sel: #dce6ff;
  --tok-key: #8a3ab9;
  --tok-string: #0b7a3e;
  --tok-number: #1b5fd1;
  --tok-bool: #b5560b;
  --tok-null: #8a8f98;
  --code-bg: #f3f4f6;
  --shadow: 0 6px 24px rgba(15, 20, 30, 0.12);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --radius: 8px;
  --topbar: 48px;
  --sidebar: 216px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #131417;
    --surface: #1b1d21;
    --surface-2: #24272c;
    --border: #33373e;
    --text: #e6e7e9;
    --muted: #9aa0a9;
    --accent: #6b93ff;
    --accent-text: #0d1220;
    --accent-soft: #232f4d;
    --danger: #ff7b7b;
    --ok: #5fd08f;
    --sel: #2a3a63;
    --tok-key: #d39cf5;
    --tok-string: #8bd49c;
    --tok-number: #82aaff;
    --tok-bool: #f4a85e;
    --tok-null: #8a8f98;
    --code-bg: #24272c;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #131417;
  --surface: #1b1d21;
  --surface-2: #24272c;
  --border: #33373e;
  --text: #e6e7e9;
  --muted: #9aa0a9;
  --accent: #6b93ff;
  --accent-text: #0d1220;
  --accent-soft: #232f4d;
  --danger: #ff7b7b;
  --ok: #5fd08f;
  --sel: #2a3a63;
  --tok-key: #d39cf5;
  --tok-string: #8bd49c;
  --tok-number: #82aaff;
  --tok-bool: #f4a85e;
  --tok-null: #8a8f98;
  --code-bg: #24272c;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.mono { font-family: var(--mono); font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.strong { font-weight: 600; }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }
.error-text { color: var(--danger); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- shell ---------- */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  grid-template-rows: var(--topbar) minmax(0, 1fr);
  grid-template-areas: 'top top' 'side main';
  height: 100vh;
  height: 100dvh;
}
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 650; font-size: 15px; }
.brand-mark { display: grid; place-items: center; width: 26px; height: 26px; border-radius: 7px; background: var(--accent); color: var(--accent-text); font-size: 14px; }
.topbar-title { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.topbar-title:not(:empty)::before { content: '/'; margin-right: 10px; opacity: 0.5; }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border: 1px solid transparent; border-radius: var(--radius); background: transparent; cursor: pointer; font-size: 16px; }
.icon-btn:hover { background: var(--surface-2); }
.menu-btn { display: none; }

.sidebar { grid-area: side; overflow-y: auto; padding: 12px 10px; background: var(--surface); border-right: 1px solid var(--border); }
.nav-filter { width: 100%; padding: 7px 10px; margin-bottom: 10px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); }
.nav-group { margin-top: 14px; }
.nav-heading { padding: 0 8px 4px; font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 6px; color: var(--text); text-decoration: none; }
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-icon { display: inline-grid; place-items: center; min-width: 26px; height: 22px; border-radius: 5px; background: var(--surface-2); font: 600 11px var(--mono); color: var(--muted); }
.nav-item.active .nav-icon { background: var(--accent); color: var(--accent-text); }
.scrim { display: none; }

.view { grid-area: main; min-width: 0; min-height: 0; overflow: auto; }
.loading, .empty { padding: 48px 24px; text-align: center; color: var(--muted); }
.empty .btn { margin-top: 8px; }

/* ---------- home ---------- */
.home { max-width: 960px; margin: 0 auto; padding: 32px 24px 48px; }
.home h1 { margin: 0 0 4px; font-size: 26px; }
.home-filter { width: 100%; max-width: 380px; margin: 18px 0 20px; padding: 9px 12px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.tool-card { display: flex; gap: 14px; padding: 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); text-decoration: none; transition: border-color 0.12s, transform 0.12s; }
.tool-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.tool-card-icon { flex: none; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 9px; background: var(--accent-soft); color: var(--accent); font: 700 14px var(--mono); }
.tool-card-name { font-weight: 600; margin-bottom: 2px; }
.tool-card-desc { color: var(--muted); font-size: 13px; }

/* ---------- shared tool chrome ---------- */
.tool { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.divider { width: 1px; height: 22px; margin: 0 4px; background: var(--border); }
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 11px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; font-size: 13px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.small { height: 26px; padding: 0 8px; font-size: 12px; }
.select { height: 30px; padding: 0 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); font-size: 13px; }
.segmented { display: inline-flex; padding: 2px; border-radius: 7px; background: var(--surface-2); }
.segmented button { height: 26px; padding: 0 12px; border: 0; border-radius: 5px; background: transparent; color: var(--muted); cursor: pointer; font-size: 13px; }
.segmented button[aria-checked='true'] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }
.pane { min-width: 0; min-height: 0; overflow: auto; background: var(--surface); }
.statusbar { display: flex; align-items: center; gap: 4px; padding: 5px 12px; border-top: 1px solid var(--border); background: var(--surface); font-size: 12px; min-height: 28px; }
.statusbar.ok { color: var(--ok); }
.statusbar.error { color: var(--danger); }
.linklike { padding: 0; border: 0; background: none; color: inherit; font-weight: 600; text-decoration: underline; cursor: pointer; }
textarea { tab-size: 4; }

.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 50; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; pointer-events: none; }
.toast { max-width: min(420px, calc(100vw - 32px)); padding: 9px 14px; border-radius: var(--radius); background: var(--text); color: var(--bg); box-shadow: var(--shadow); font-size: 13px; animation: toast-in 0.15s ease-out; transition: opacity 0.3s, transform 0.3s; }
.toast.error { background: var(--danger); color: #fff; }
.toast.out { opacity: 0; transform: translateY(6px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* ---------- split workspaces (markdown + json) ---------- */
.md-workspace, .json-workspace { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 1px; background: var(--border); }
.md-workspace[data-mode='edit'], .md-workspace[data-mode='preview'],
.json-workspace[data-mode='text'], .json-workspace[data-mode='tree'] { grid-template-columns: 1fr; }
.md-workspace[data-mode='edit'] .md-preview-pane,
.md-workspace[data-mode='preview'] .md-edit-pane,
.json-workspace[data-mode='text'] .json-tree-pane,
.json-workspace[data-mode='tree'] .json-text-pane { display: none; }

/* markdown */
.md-edit-pane { display: flex; overflow: hidden; }
.md-input { flex: 1; width: 100%; padding: 16px 18px; border: 0; outline: none; resize: none; background: var(--surface); line-height: 1.6; }
.md-workspace[data-mode='edit'] .md-input { max-width: 900px; margin: 0 auto; }
.md-preview { padding: 16px 24px 48px; }
.md-workspace[data-mode='preview'] .md-preview { max-width: 820px; margin: 0 auto; padding: 32px 32px 80px; }

.prose { font-size: 15px; line-height: 1.65; overflow-wrap: break-word; }
.prose > :first-child { margin-top: 0; }
.prose h1, .prose h2, .prose h3, .prose h4 { margin: 1.4em 0 0.5em; line-height: 1.25; }
.prose h1 { font-size: 1.9em; padding-bottom: 0.25em; border-bottom: 1px solid var(--border); }
.prose h2 { font-size: 1.45em; padding-bottom: 0.2em; border-bottom: 1px solid var(--border); }
.prose h3 { font-size: 1.2em; }
.prose p, .prose ul, .prose ol, .prose blockquote, .prose pre, .prose table { margin: 0 0 1em; }
.prose ul, .prose ol { padding-left: 1.6em; }
.prose li + li { margin-top: 0.25em; }
.prose li > input[type='checkbox'] { margin: 0 0.4em 0 -1.3em; vertical-align: middle; }
.prose li:has(> input[type='checkbox']) { list-style: none; }
.prose code { padding: 0.15em 0.35em; border-radius: 4px; background: var(--code-bg); font: 0.88em var(--mono); }
.prose pre { padding: 12px 14px; border-radius: var(--radius); background: var(--code-bg); overflow: auto; }
.prose pre code { padding: 0; background: none; font-size: 13px; }
.prose blockquote { padding: 0 1em; border-left: 3px solid var(--border); color: var(--muted); }
.prose table { border-collapse: collapse; display: block; overflow-x: auto; }
.prose th, .prose td { padding: 6px 12px; border: 1px solid var(--border); }
.prose th { background: var(--surface-2); }
.prose img { max-width: 100%; }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2em 0; }

/* json: text pane */
.json-text-pane { overflow: hidden; display: flex; }
.editor-wrap { flex: 1; display: flex; min-width: 0; }
.gutter { position: relative; flex: none; min-width: 44px; padding: 10px 8px 60px 6px; overflow: hidden; background: var(--surface-2); color: var(--muted); text-align: right; line-height: 20px; user-select: none; }
.gutter-nums { white-space: pre; }
.gutter-error { position: absolute; left: 0; right: 0; height: 20px; margin-top: 10px; background: color-mix(in srgb, var(--danger) 25%, transparent); border-left: 3px solid var(--danger); }
.json-input { flex: 1; min-width: 0; padding: 10px 12px; border: 0; outline: none; resize: none; background: var(--surface); line-height: 20px; white-space: pre; overflow: auto; }

/* json: tree pane */
.json-tree-pane { display: flex; flex-direction: column; overflow: hidden; }
.tree-bar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--border); }
.path-chip { max-width: 55%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 2px 8px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface-2); cursor: pointer; font-size: 12px; }
.json-tree { flex: 1; overflow: auto; padding: 6px 0 40px; line-height: 22px; }
.json-tree.stale { opacity: 0.45; }
.tree-row { position: relative; display: flex; align-items: center; padding-left: calc(var(--depth) * 18px + 6px); padding-right: 64px; white-space: nowrap; cursor: default; }
.tree-row:hover { background: var(--surface-2); }
.tree-row.selected { background: var(--sel); }
.tree-row.closing { cursor: default; }
.tree-row.closing:hover { background: none; }
.twisty, .twisty-space { flex: none; width: 18px; }
.twisty { height: 18px; padding: 0; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 11px; }
.t-key { color: var(--tok-key); }
.t-index { color: var(--muted); }
.t-punct, .t-count { color: var(--muted); white-space: pre; }
.t-count { font-style: italic; font-size: 12px; }
.t-val { overflow: hidden; text-overflow: ellipsis; }
.t-string { color: var(--tok-string); }
.t-number { color: var(--tok-number); }
.t-boolean { color: var(--tok-bool); }
.t-null { color: var(--tok-null); }
.t-key, .t-val { cursor: text; border-radius: 3px; }
.t-key:hover, .t-val:hover { outline: 1px dashed var(--border); }
.row-actions { position: absolute; right: 8px; display: none; gap: 2px; }
.tree-row:hover > .row-actions, .tree-row.selected > .row-actions { display: flex; }
.mini { width: 22px; height: 20px; padding: 0; border: 1px solid var(--border); border-radius: 4px; background: var(--surface); color: var(--muted); cursor: pointer; line-height: 1; }
.mini:hover { color: var(--text); }
.inline-edit { height: 22px; padding: 0 4px; border: 1px solid var(--accent); border-radius: 3px; background: var(--surface); outline: none; }
.more { margin: 4px 0 4px calc(var(--depth) * 18px + 24px); }

/* ---------- paste ---------- */
.paste-body { flex: 1; min-height: 0; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.paste-input { flex: 1 0 280px; width: 100%; padding: 12px 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); resize: vertical; line-height: 1.5; outline: none; }
.paste-input:focus { border-color: var(--accent); }
.section-title { margin: 16px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.paste-item { display: flex; align-items: center; gap: 12px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); margin-bottom: 6px; }
.paste-preview { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.paste-view { flex: 1; min-height: 0; display: flex; overflow: auto; background: var(--surface); line-height: 20px; }
.paste-lines { flex: none; padding: 12px 10px 40px; background: var(--surface-2); color: var(--muted); text-align: right; white-space: pre; user-select: none; position: sticky; left: 0; }
.paste-text { flex: 1; margin: 0; padding: 12px 14px 40px; font: inherit; white-space: pre; }
.paste-view.wrap .paste-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.paste-view.wrap .paste-lines { display: none; }

/* ---------- narrow screens ---------- */
@media (max-width: 800px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: 'top' 'main'; }
  .menu-btn { display: inline-block; }
  .sidebar { position: fixed; z-index: 30; top: var(--topbar); bottom: 0; left: 0; width: min(280px, 85vw); transform: translateX(-100%); transition: transform 0.18s ease-out; box-shadow: var(--shadow); }
  .drawer-open .sidebar { transform: none; }
  .drawer-open .scrim { display: block; position: fixed; inset: var(--topbar) 0 0 0; z-index: 20; background: rgba(0, 0, 0, 0.35); }
  /* one row that scrolls sideways, so the editor keeps the screen */
  .toolbar { padding: 8px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .toolbar > * { flex: none; }
  .toolbar > .spacer { flex: 1 0 8px; }
  .home { padding: 20px 16px 40px; }
  .md-workspace[data-mode='split'], .json-workspace[data-mode='split'] { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .md-workspace[data-mode='preview'] .md-preview { padding: 20px 16px 60px; }
}

/* ---------- syntax highlighting ---------- */
.syntax-workspace[data-mode='source'], .syntax-workspace[data-mode='output'] { grid-template-columns: 1fr; }
.syntax-workspace[data-mode='source'] .syntax-out-pane,
.syntax-workspace[data-mode='output'] .md-edit-pane { display: none; }
.code-input { white-space: pre; overflow: auto; tab-size: 4; }
.syntax-out-pane { display: flex; flex-direction: column; overflow: hidden; }
.syntax-bar { flex-wrap: wrap; background: var(--surface); }
.syntax-output { flex: 1; overflow: auto; }
.syntax-output > div { min-height: 100%; width: max-content; min-width: 100%; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; cursor: pointer; }
@media (max-width: 800px) {
  .syntax-workspace[data-mode='split'] { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
}

/* ---------- badges, chips ---------- */
.nav-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--danger); color: #fff; font: 600 11px/20px var(--sans); text-align: center; }
.menu-btn { position: relative; }
.menu-btn.has-badge::after { content: ''; position: absolute; top: 6px; right: 6px; width: 8px; height: 8px; border-radius: 50%; background: var(--danger); }
.chip { padding: 1px 7px; border-radius: 9px; font-size: 11px; font-weight: 600; }
.chip.new { background: var(--accent-soft); color: var(--accent); }

/* ---------- dropdown menu ---------- */
.dropdown { position: relative; display: inline-flex; }
.caret { font-size: 10px; opacity: 0.7; }
.menu { position: absolute; top: calc(100% + 4px); right: 0; z-index: 40; min-width: 200px; padding: 4px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.menu-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 7px 8px; border: 0; border-radius: 5px; background: none; color: var(--text); text-align: left; cursor: pointer; font-size: 13px; }
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-2); outline: none; }

/* ---------- modal ---------- */
.modal { width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); padding: 0; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.modal.wide { width: min(980px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(0, 0, 0, 0.45); }
.modal[open] { display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; padding: 12px 12px 12px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { flex: 1; margin: 0; font-size: 17px; }
.modal-body { overflow: auto; padding: 0 20px 20px; }

/* markdown guide */
.guide-cols { position: sticky; top: 0; z-index: 1; display: grid; grid-template-columns: 1fr 1fr; gap: 16px; padding: 10px 0 8px; background: var(--surface); border-bottom: 1px solid var(--border); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.guide-section { margin: 20px 0 4px; font-size: 15px; }
.guide-row { display: grid; grid-template-columns: 1fr 1fr; grid-template-areas: 'label label' 'src out'; gap: 4px 16px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.guide-label { grid-area: label; display: flex; align-items: center; justify-content: space-between; font-size: 13px; color: var(--muted); }
.guide-src { grid-area: src; margin: 0; padding: 10px 12px; border-radius: 6px; background: var(--code-bg); white-space: pre-wrap; overflow-wrap: anywhere; }
.guide-out { grid-area: out; font-size: 14px; }
.guide-out > :last-child { margin-bottom: 0; }
@media (max-width: 640px) {
  .guide-row { grid-template-columns: 1fr; grid-template-areas: 'label' 'src' 'out'; }
  .guide-cols { display: none; }
}
