:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --ink: #111827;
  --ink-soft: #374151;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-strong: #cbd5e1;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --danger: #dc2626;
  --warn: #d97706;
  --danger-bg: #fef2f2;
  --warn-bg: #fffbeb;
  --shadow: 0 8px 24px rgba(16, 24, 40, 0.14), 0 2px 6px rgba(16, 24, 40, 0.08);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --topbar-h: 48px;

  --tk-comment: #9ca3af;
  --tk-dkey: #7c3aed;
  --tk-dval: #374151;
  --tk-hash: #9ca3af;
  --tk-group: #0f172a;
  --tk-when: #0369a1;
  --tk-label: #111827;
  --tk-attr: #b45309;
  --tk-bang: #dc2626;
  --tk-details: #6b7280;
  --sel: rgba(37, 99, 235, 0.18);
  --active-line: rgba(37, 99, 235, 0.06);
}

html[data-theme="dark"] {
  --bg: #0b1220;
  --panel: #0f172a;
  --panel-2: #111a2e;
  --ink: #e5e7eb;
  --ink-soft: #cbd5e1;
  --muted: #94a3b8;
  --line: #1e293b;
  --line-strong: #334155;
  --accent: #3b82f6;
  --danger: #f87171;
  --warn: #fbbf24;
  --danger-bg: rgba(248, 113, 113, 0.12);
  --warn-bg: rgba(251, 191, 36, 0.12);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.4);

  --tk-comment: #64748b;
  --tk-dkey: #c4b5fd;
  --tk-dval: #cbd5e1;
  --tk-hash: #64748b;
  --tk-group: #f1f5f9;
  --tk-when: #7dd3fc;
  --tk-label: #e5e7eb;
  --tk-attr: #fcd34d;
  --tk-bang: #fca5a5;
  --tk-details: #94a3b8;
  --sel: rgba(96, 165, 250, 0.3);
  --active-line: rgba(96, 165, 250, 0.08);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--sans);
  font-size: 13px;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
a { color: var(--accent); }
code, kbd { font-family: var(--mono); font-size: 12px; }
kbd { background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 14px; letter-spacing: -0.01em; margin-right: 8px; }
.brand svg { width: 20px; height: 20px; }
.spacer { flex: 1; }
.status { color: var(--muted); font-size: 12px; margin-right: 6px; white-space: nowrap; transition: opacity 0.15s; }
.status.flash { color: var(--ink); font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 30px; padding: 0 10px;
  border: 1px solid var(--line); border-radius: 7px;
  background: var(--panel); color: var(--ink-soft);
  font-size: 12.5px; font-weight: 500; white-space: nowrap;
  transition: background 0.1s, border-color 0.1s;
}
.btn:hover { background: var(--panel-2); border-color: var(--line-strong); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.icon { width: 30px; padding: 0; justify-content: center; }
.btn.active { background: var(--panel-2); border-color: var(--accent); color: var(--accent); }
.btn svg { width: 15px; height: 15px; }
.btn .caret { opacity: 0.6; font-size: 10px; }

.zoom { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 7px; height: 30px; overflow: hidden; }
.zoom button { height: 100%; padding: 0 8px; color: var(--ink-soft); }
.zoom button:hover { background: var(--panel-2); }
.zoom .zoom-val { min-width: 44px; text-align: center; font-size: 12px; color: var(--muted); border-left: 1px solid var(--line); border-right: 1px solid var(--line); height: 100%; display: inline-flex; align-items: center; justify-content: center; }

.menu { position: relative; }
.menu-list {
  position: absolute; top: calc(100% + 6px); right: 0; min-width: 220px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); padding: 6px; z-index: 30; display: none;
}
.menu.open .menu-list { display: block; }
.menu-list button, .menu-list a {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; text-align: left; padding: 8px 10px; border-radius: 6px; color: var(--ink);
  font-size: 13px; text-decoration: none;
}
.menu-list button:hover, .menu-list a:hover { background: var(--panel-2); }
.menu-list .hint { color: var(--muted); font-size: 11px; }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; }
.menu-list .section { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; padding: 6px 10px 2px; }

.share-panel { width: 340px; padding: 12px; }
.share-panel p { color: var(--muted); font-size: 12px; margin: 4px 0 10px; line-height: 1.5; }
.share-panel .row { display: flex; gap: 8px; margin-bottom: 8px; }
.share-panel .row .btn { flex: 1; justify-content: center; }
.share-panel input { width: 100%; font: 12px var(--mono); padding: 7px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--panel-2); color: var(--ink-soft); }

/* ---------------------------------------------------------------- layout */
.split { flex: 1; display: flex; min-height: 0; }
.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.editor-pane { width: 42%; flex: 0 0 auto; background: var(--panel); border-right: 1px solid var(--line); }
.chart-pane { flex: 1; background: var(--bg); }
.splitter { width: 6px; flex: 0 0 auto; cursor: col-resize; background: transparent; margin-left: -3px; margin-right: -3px; z-index: 5; }
.splitter:hover, .splitter.dragging { background: var(--accent); opacity: 0.35; }

body.view .editor-pane, body.view .splitter { display: none; }
body.view .topbar .editor-only { display: none; }
body:not(.view) .view-only { display: none; }

/* ---------------------------------------------------------------- editor */
.editor {
  flex: 1; min-height: 0; position: relative; display: flex;
  font: 13px/20px var(--mono);
  tab-size: 2;
}
.gutter {
  flex: 0 0 auto; width: 44px; padding: 12px 0; overflow: hidden;
  background: var(--panel); border-right: 1px solid var(--line);
  color: var(--muted); text-align: right; user-select: none;
  position: relative;
}
.gutter-inner { will-change: transform; }
.gutter .g { padding-right: 10px; height: 20px; position: relative; font-size: 11.5px; }
.gutter .g.error { color: var(--danger); font-weight: 700; }
.gutter .g.warning { color: var(--warn); font-weight: 700; }
.gutter .g.error::before, .gutter .g.warning::before {
  content: ""; position: absolute; left: 6px; top: 7px; width: 6px; height: 6px; border-radius: 50%; background: currentColor;
}
.editor-scroll { flex: 1; position: relative; min-width: 0; overflow: hidden; }
.backdrop, .editor textarea {
  position: absolute; top: 0; left: 0; margin: 0; padding: 12px 14px 12px 12px;
  font: inherit; line-height: inherit; tab-size: inherit;
  white-space: pre; overflow-wrap: normal; word-break: normal;
  border: 0; outline: 0;
}
.backdrop { pointer-events: none; color: var(--ink); overflow: visible; will-change: transform; min-width: 100%; min-height: 100%; width: max-content; }
.backdrop .ln { display: inline; }
.backdrop .ln-error { text-decoration: underline wavy var(--danger); text-decoration-skip-ink: none; text-underline-offset: 3px; }
.backdrop .ln-warning { text-decoration: underline wavy var(--warn); text-decoration-skip-ink: none; text-underline-offset: 3px; }
.editor textarea {
  background: transparent; color: transparent; caret-color: var(--ink);
  resize: none; overflow: auto; width: 100%; height: 100%;
}
.editor textarea::selection { background: var(--sel); }
.editor textarea::placeholder { color: var(--muted); }

.tk-comment { color: var(--tk-comment); font-style: italic; }
.tk-dkey { color: var(--tk-dkey); font-weight: 600; }
.tk-dval { color: var(--tk-dval); }
.tk-hash { color: var(--tk-hash); }
.tk-group { color: var(--tk-group); font-weight: 700; }
.tk-when { color: var(--tk-when); font-weight: 600; }
.tk-colon { color: var(--tk-hash); }
.tk-label { color: var(--tk-label); }
.tk-attr { color: var(--tk-attr); }
.tk-bang { color: var(--tk-bang); font-weight: 700; }
.tk-details { color: var(--tk-details); }
.tk-plain { color: var(--ink); }

.problems {
  flex: 0 0 auto; max-height: 140px; overflow: auto;
  border-top: 1px solid var(--line); background: var(--panel-2);
  font-size: 12px;
}
.problems:empty { display: none; }
.problems button {
  display: flex; gap: 10px; align-items: baseline; width: 100%; text-align: left;
  padding: 6px 12px; border-bottom: 1px solid var(--line); color: var(--ink-soft);
}
.problems button:last-child { border-bottom: 0; }
.problems button:hover { background: var(--panel); }
.problems .lnum { font-family: var(--mono); color: var(--muted); min-width: 34px; font-size: 11px; }
.problems .sev { font-weight: 700; text-transform: uppercase; font-size: 10px; letter-spacing: 0.05em; min-width: 52px; }
.problems .error .sev { color: var(--danger); }
.problems .warning .sev { color: var(--warn); }

/* ---------------------------------------------------------------- chart */
.chart-scroll { flex: 1; overflow: auto; padding: 20px 24px 28px; }
#chart { display: inline-block; min-width: 100%; }
#chart svg { display: block; border-radius: 12px; box-shadow: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04); }
#chart svg .item { cursor: default; }
#chart svg .item.has-details, #chart svg .item[data-link] { cursor: pointer; }
#chart svg .item:hover rect, #chart svg .item:hover path { filter: brightness(1.07); }
#chart svg .item.dim { opacity: 0.35; }
#chart svg .dep.dim { opacity: 0.2; }
#chart svg .group-label { cursor: pointer; }
#chart svg .group-label:hover text { text-decoration: underline; }
#chart svg text { user-select: none; }
.empty-chart { color: var(--muted); text-align: center; padding: 80px 20px; font-size: 14px; line-height: 1.6; }
.empty-chart code { background: var(--panel); padding: 2px 6px; border-radius: 4px; border: 1px solid var(--line); }

/* ---------------------------------------------------------------- popover */
.popover {
  position: fixed; z-index: 50; max-width: 360px; min-width: 200px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); padding: 10px 12px; font-size: 12.5px; line-height: 1.5; color: var(--ink);
}
.popover[hidden] { display: none; }
.popover .pt { font-weight: 600; margin-bottom: 2px; display: flex; gap: 8px; align-items: center; }
.popover .swatch { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }
.popover .pm { color: var(--muted); font-size: 12px; }
.popover .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.popover .tag { background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; padding: 0 8px; font-size: 11px; color: var(--ink-soft); }
.popover .pd { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line); color: var(--ink-soft); white-space: pre-wrap; }
.popover .pa { margin-top: 8px; display: flex; gap: 10px; font-size: 12px; }
.popover .pa a, .popover .pa button { color: var(--accent); text-decoration: none; padding: 0; }
.popover .pa a:hover, .popover .pa button:hover { text-decoration: underline; }

/* ---------------------------------------------------------------- help dialog */
dialog.help {
  width: min(760px, 92vw); max-height: 88vh; padding: 0; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--ink); box-shadow: var(--shadow);
}
dialog.help::backdrop { background: rgba(15, 23, 42, 0.45); }
.help-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); border-radius: 14px 14px 0 0; }
.help-head h2 { font-size: 15px; }
.help-body { padding: 8px 20px 20px; overflow: auto; max-height: calc(88vh - 56px); line-height: 1.55; }
.help-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 20px 0 8px; }
.help-body p { margin: 6px 0; color: var(--ink-soft); }
.help-body table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.help-body td { padding: 6px 8px; border-top: 1px solid var(--line); vertical-align: top; }
.help-body td:first-child { font-family: var(--mono); white-space: nowrap; color: var(--tk-when); width: 42%; }
.help-body pre { font: 12px/1.6 var(--mono); background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; overflow: auto; }
.help-body .cols { display: grid; grid-template-columns: 1fr 1fr; gap: 0 24px; }
.help-body .keys td:first-child { color: var(--ink); }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 860px) {
  .split { flex-direction: column; }
  .editor-pane { width: 100% !important; height: 42vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .splitter { display: none; }
  .topbar .hide-narrow { display: none; }
  .help-body .cols { grid-template-columns: 1fr; }
}

/* ---------------------------------------------------------------- print */
@media print {
  @page { size: landscape; margin: 0.4in; }
  html, body { height: auto; overflow: visible; background: #fff; }
  .topbar, .editor-pane, .splitter, .popover, .problems { display: none !important; }
  .split { display: block; }
  .chart-pane { display: block; background: #fff; }
  .chart-scroll { overflow: visible; padding: 0; }
  #chart { display: block; }
  #chart svg { max-width: 100%; height: auto; box-shadow: none; border-radius: 0; }
}
