:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #dbe3ec;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.12);
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --editor-bg: #ffffff;
  --editor-text: #111827;
  --topbar-bg: rgba(255, 255, 255, 0.92);
  --json-key: #2563eb;
  --json-string: #15803d;
  --json-number: #b45309;
  --json-boolean: #7c3aed;
  --json-null: #6b7280;
  --json-punctuation: #374151;
  --json-toggle-color: #6b7280;
}

body[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #111827;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --border: #243244;
  --accent: #60a5fa;
  --accent-soft: rgba(96, 165, 250, 0.16);
  --danger: #f97066;
  --danger-soft: rgba(249, 112, 102, 0.16);
  --editor-bg: #0f172a;
  --editor-text: #e5e7eb;
  --topbar-bg: rgba(17, 24, 39, 0.9);
  --json-key: #7dd3fc;
  --json-string: #86efac;
  --json-number: #fcd34d;
  --json-boolean: #c4b5fd;
  --json-null: #9ca3af;
  --json-punctuation: #d1d5db;
  --json-toggle-color: #6b7280;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: linear-gradient(180deg, #ffffff 0%, var(--bg) 100%);
  font-family: "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}

body[data-theme="dark"] {
  background: linear-gradient(180deg, #0f172a 0%, var(--bg) 100%);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 64px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--topbar-bg);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

.brand p {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--muted);
}

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

button {
  min-height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

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

.layout {
  --split-ratio: 50%;
  --split-ratio-mobile: 50%;
  min-height: calc(100vh - 64px);
  padding: 12px;
  display: grid;
  grid-template-columns: var(--split-ratio) 8px calc(100% - var(--split-ratio) - 32px);
  gap: 12px;
  overflow: hidden;
}

html[data-layout-mode="editor"] .layout,
.layout[data-layout-mode="editor"] {
  grid-template-columns: 1fr 8px;
}

html[data-layout-mode="preview"] .layout,
.layout[data-layout-mode="preview"] {
  grid-template-columns: 8px 1fr;
}

.panel {
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.splitter {
  border: 0;
  background: transparent;
  padding: 0;
  cursor: col-resize;
  position: relative;
  touch-action: none;
}

.splitter::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  width: 2px;
  height: 100%;
  border-radius: 999px;
  background: var(--border);
}

.splitter:hover::before,
.splitter:focus-visible::before {
  background: var(--accent);
}

.splitter-dot {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 6px;
  height: 36px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 22%, var(--border));
}

body[data-theme="dark"] .splitter::before {
  background: #334155;
}

body[data-theme="dark"] .splitter-dot {
  background: rgba(96, 165, 250, 0.45);
}

html[data-layout-mode="editor"] #previewPanel,
.layout[data-layout-mode="editor"] #previewPanel {
  display: none;
}

html[data-layout-mode="preview"] #editorPanel,
.layout[data-layout-mode="preview"] #editorPanel {
  display: none;
}

.panel-title {
  min-height: 42px;
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
}

.status-badge {
  max-width: 42%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-radius: 999px;
  padding: 3px 8px;
  background: var(--accent-soft);
  color: var(--accent);
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.status-badge.is-valid {
  background: #dcfce7;
  color: #15803d;
}

.status-badge.is-invalid {
  background: var(--danger-soft);
  color: var(--danger);
}

.status-badge.is-loading {
  background: var(--accent-soft);
  color: var(--accent);
}

.status-badge.is-loading::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: currentColor;
  animation: statusPulse 1s ease-in-out infinite;
}

@keyframes statusPulse {
  0%, 100% {
    opacity: 0.35;
    transform: scale(0.85);
  }
  50% {
    opacity: 1;
    transform: scale(1.15);
  }
}

body[data-theme="dark"] .status-badge.is-valid {
  background: rgba(34, 197, 94, 0.18);
  color: #86efac;
}

body[data-theme="dark"] .status-badge.is-invalid {
  background: rgba(249, 112, 102, 0.18);
  color: #f97066;
}

#editor {
  width: 100%;
  flex: 1;
  border: 0;
  resize: none;
  padding: 14px;
  color: var(--editor-text);
  background: var(--editor-bg);
  font-size: 14px;
  line-height: 1.65;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  tab-size: 2;
}

#editor:focus {
  outline: none;
}

/* ---------- json tree ---------- */

.json-tree {
  flex: 1;
  overflow: auto;
  padding: 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 14px;
  line-height: 1.65;
  color: var(--editor-text);
  background: var(--editor-bg);
  user-select: text;
}

.json-line {
  min-height: 1.65em;
  white-space: pre;
}

.json-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 1.65em;
  margin-right: 2px;
  cursor: pointer;
  color: var(--json-toggle-color);
  font-size: 11px;
  vertical-align: middle;
  user-select: none;
  border-radius: 3px;
}

.json-toggle:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.json-collapsed-info {
  color: var(--muted);
  font-style: italic;
}

.json-key {
  color: var(--json-key);
}

.json-string {
  color: var(--json-string);
}

.json-number {
  color: var(--json-number);
}

.json-boolean {
  color: var(--json-boolean);
}

.json-null {
  color: var(--json-null);
}

.json-punctuation {
  color: var(--json-punctuation);
}

@media (max-width: 960px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .actions {
    justify-content: flex-start;
  }

  .layout {
    min-height: calc(100vh - 150px);
    grid-template-columns: 1fr;
  }

  html[data-layout-mode="editor"] .layout,
  html[data-layout-mode="preview"] .layout,
  .layout[data-layout-mode="editor"],
  .layout[data-layout-mode="preview"] {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
  }

  html[data-layout-mode="split"] .layout,
  .layout[data-layout-mode="split"] {
    grid-template-rows: var(--split-ratio-mobile) 8px calc(100% - var(--split-ratio-mobile) - 32px);
  }

  .splitter {
    height: 8px;
    width: 100%;
    cursor: row-resize;
  }

  .splitter::before {
    width: 100%;
    height: 2px;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
  }

  .splitter-dot {
    width: 36px;
    height: 6px;
  }

  .panel {
    min-height: 45vh;
  }

  html[data-layout-mode="split"] .layout .panel,
  .layout[data-layout-mode="split"] .panel {
    min-height: 0;
  }
}
