:root {
  /* light theme (default): white + royal blue, after the LiKeiBai icon */
  --bg: #eef4ff;
  --panel-grad-a: #ffffff;
  --panel-grad-b: #f2f7ff;
  --card: #ffffff;
  --card-edge: #d9e4f8;
  --panel-2: #f6f9ff;
  --panel-2-edge: #dbe6fa;
  --input-bg: #ffffff;
  --input-edge: #c8d7f2;
  --text: #0b1c3f;
  --muted: #5a6b8c;
  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --cyan: #0e7490;
  --indigo: #4f46e5;
  --amber: #b45309;
  --pink: #be185d;
  --green: #047857;
  --tag-amber-text: #ffffff;
  --tag-pink-text: #ffffff;
  --active-bg: #e8f0ff;
  --active-ring: rgba(37, 99, 235, 0.22);
  --btn-grad-a: #3b82f6;
  --btn-grad-b: #1d4ed8;
  --btn-text: #ffffff;
  --btn-shadow: rgba(29, 78, 216, 0.28);
  --ghost-edge: #c8d7f2;
  --ghost-text: #475569;
  --log-bg: #f7faff;
  --code-text: #1e3a8a;
  --log-stmt: #0369a1;
  --log-out: #0b1c3f;
  --log-note: #8a6a00;
  --stage-a: #e7f0ff;
  --stage-b: #f8fbff;
  --viewport-bg: #ffffff;
  --overlay: rgba(255, 255, 255, 0.88);
  --overlay-edge: #dbe6fa;
  --label: #0f2a5c;
  --label-strong: #06122b;
  --label-muted: #47546e;
  --label-ref: #075985;
  --label-id: #1d4ed8;
  --label-shadow: 0 1px 3px rgba(255, 255, 255, 0.9);
  /* scales the floating word labels (values, types, names) above the 3D nodes */
  --lbl-scale: 1;
  /* scales only the address text (id 0x… / → 0x…) independently */
  --lbl-addr-scale: 1;
  /* scales only the shared-ref tags (shared · n refs) independently */
  --lbl-tag-scale: 1;
  --hint-text: #47546e;
  --hint-bg: rgba(255, 255, 255, 0.78);
  --err-bg: rgba(220, 38, 38, 0.07);
  --err-edge: rgba(220, 38, 38, 0.35);
  --err-text: #b91c1c;
  --ok-bg: rgba(4, 120, 87, 0.12);
  --ok-edge: rgba(4, 120, 87, 0.35);
  --ok-text: #047857;
  --no-bg: rgba(190, 24, 93, 0.12);
  --no-edge: rgba(190, 24, 93, 0.35);
  --no-text: #be185d;
  --note-bg: rgba(217, 119, 6, 0.1);
  --note-text: #92400e;
  --cell: #f8fafc;
  --cell-ref: #e0f2fe;
  --cell-shared: #fbcfe8;
  --plaque-text: #1d4ed8;
  --plaque-caption: #38455f;
  --sw-orange: #f59e0b;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --warn-code-bg: #fde68a;
  --shadow: rgba(20, 45, 95, 0.1);
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

html[data-theme="dark"] {
  --bg: #060a13;
  --panel-grad-a: #0b1322;
  --panel-grad-b: #080d18;
  --card: #0e1628;
  --card-edge: #1e2a44;
  --panel-2: #0b1424;
  --panel-2-edge: #1d2942;
  --input-bg: #0c1526;
  --input-edge: #26324a;
  --text: #dce5f5;
  --muted: #8b9bb8;
  --accent: #3b82f6;
  --accent-2: #60a5fa;
  --cyan: #22d3ee;
  --indigo: #818cf8;
  --amber: #fbbf24;
  --pink: #f472b6;
  --green: #34d399;
  --tag-amber-text: #451a03;
  --tag-pink-text: #500724;
  --active-bg: #0c2130;
  --active-ring: rgba(34, 211, 238, 0.25);
  --btn-grad-a: #22d3ee;
  --btn-grad-b: #0ea5b7;
  --btn-text: #04121a;
  --btn-shadow: rgba(34, 211, 238, 0.22);
  --ghost-edge: #3b4a66;
  --ghost-text: #64748b;
  --log-bg: #08101f;
  --code-text: #dbeafe;
  --log-stmt: #a5f3fc;
  --log-out: #e2e8f0;
  --log-note: #fde68a;
  --stage-a: #0b1526;
  --stage-b: #060a13;
  --viewport-bg: #070b16;
  --overlay: rgba(14, 22, 40, 0.85);
  --overlay-edge: #1d2942;
  --label: #e2e8f0;
  --label-strong: #f8fafc;
  --label-muted: #8b9bb8;
  --label-ref: #67e8f9;
  --label-id: #7dd3fc;
  --label-shadow: 0 1px 4px rgba(0, 0, 0, 0.85);
  --hint-text: #64748b;
  --hint-bg: rgba(7, 11, 22, 0.6);
  --err-bg: rgba(244, 63, 94, 0.12);
  --err-edge: rgba(244, 63, 94, 0.45);
  --err-text: #fda4af;
  --ok-bg: rgba(52, 211, 153, 0.18);
  --ok-edge: rgba(52, 211, 153, 0.4);
  --ok-text: #34d399;
  --no-bg: rgba(244, 114, 182, 0.15);
  --no-edge: rgba(244, 114, 182, 0.4);
  --no-text: #f472b6;
  --note-bg: rgba(251, 191, 36, 0.08);
  --note-text: #fde68a;
  --cell: #f8fafc;
  --cell-ref: #cffafe;
  --cell-shared: #f9a8d4;
  --plaque-text: #e2e8f0;
  --plaque-caption: #cbd5e1;
  --sw-orange: #f59e0b;
  --warn-bg: #7c2d12;
  --warn-text: #ffedd5;
  --warn-code-bg: #451a03;
  --shadow: rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

#filewarn {
  padding: 12px 16px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 14px;
  line-height: 1.5;
}
#filewarn code {
  background: var(--warn-code-bg);
  padding: 1px 6px;
  border-radius: 5px;
  font-family: var(--mono);
}

#app {
  display: grid;
  grid-template-columns: 400px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------------- left panel ---------------- */

#panel {
  overflow-y: auto;
  border-right: 1px solid var(--card-edge);
  background: linear-gradient(180deg, var(--panel-grad-a) 0%, var(--panel-grad-b) 100%);
  padding: 18px 16px 28px;
}

.brand .kicker {
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--cyan);
  font-weight: 700;
}
.brand h1 {
  margin: 6px 0 8px;
  font-size: 22px;
  line-height: 1.2;
}
.brand p {
  margin: 0 0 4px;
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--muted);
}
.brand .also { margin: 8px 0 0; font-size: 11.5px; }
.brand .also a { color: var(--accent-2); font-weight: 700; text-decoration: none; }
.brand .also a:hover { text-decoration: underline; }
.brand code, .tiny code {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent-2);
  background: var(--panel-2);
  padding: 1px 5px;
  border-radius: 5px;
}

.card {
  margin-top: 16px;
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-radius: 12px;
  padding: 12px 12px 14px;
}
.card h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 10px;
  font-size: 13px;
  letter-spacing: 0.02em;
  color: var(--text);
}
.card h2 .num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 6px;
  background: var(--panel-2);
  border: 1px solid var(--input-edge);
  color: var(--cyan);
  font-size: 11px;
  font-weight: 800;
}

/* ---------------- input + statement builder ---------------- */

#listInput, #nameInput, #stmt, .row select {
  width: 100%;
  padding: 9px 10px;
  border-radius: 9px;
  border: 1px solid var(--input-edge);
  background: var(--input-bg);
  color: var(--text);
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.5;
}
#listInput { resize: vertical; min-height: 46px; }

.field { display: block; margin-top: 9px; }
.field-label {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.mode.disabled { opacity: 0.45; }
#listInput:focus, #nameInput:focus, #stmt:focus, .row select:focus {
  outline: none;
  border-color: var(--cyan);
}

.presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 9px; }
.chip {
  border: 1px solid var(--input-edge);
  background: var(--input-bg);
  color: var(--muted);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 11px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.chip:hover { border-color: var(--accent); color: var(--text); }

.err {
  margin-top: 8px;
  padding: 7px 9px;
  border-radius: 8px;
  background: var(--err-bg);
  border: 1px solid var(--err-edge);
  color: var(--err-text);
  font-size: 11.5px;
  font-family: var(--mono);
  line-height: 1.5;
  white-space: pre-wrap;
}

.tiny { margin: 9px 0 0; font-size: 11px; color: var(--muted); line-height: 1.5; }

.modes { display: flex; flex-direction: column; gap: 8px; }

.mode {
  display: flex;
  flex-direction: column;
  gap: 3px;
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--input-edge);
  background: var(--input-bg);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
.mode:hover { border-color: var(--accent); }
.mode.active {
  border-color: var(--cyan);
  background: var(--active-bg);
  box-shadow: inset 0 0 0 1px var(--active-ring), 0 4px 18px var(--shadow);
}
.mode code {
  font-family: var(--mono);
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
}
.mode span { font-size: 11px; color: var(--muted); line-height: 1.4; }

.row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 9px;
}
.row label { font-size: 12px; color: var(--muted); }
.row select { flex: 0 0 auto; width: auto; }
.row input { flex: 1; min-width: 0; }

button.primary, button.ghost {
  padding: 9px 14px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.15s, border-color 0.15s;
}
button.primary {
  border: none;
  background: linear-gradient(180deg, var(--btn-grad-a), var(--btn-grad-b));
  color: var(--btn-text);
}
button.primary:hover { filter: brightness(1.1); }
button.ghost {
  margin-left: auto;
  border: 1px solid var(--ghost-edge);
  background: transparent;
  color: var(--muted);
}
button.ghost:hover { border-color: var(--ghost-text); color: var(--text); }

/* ---------------- identity + values ---------------- */

.ikv {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 7px 9px;
  border-radius: 9px;
  background: var(--panel-2);
  border: 1px solid var(--panel-2-edge);
  margin-bottom: 7px;
}
.ikv code {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text);
  word-break: break-all;
}
.badge {
  margin-left: auto;
  padding: 2px 9px;
  border-radius: 999px;
  font-family: var(--mono);
  font-size: 11.5px;
  font-weight: 700;
}
.badge.ok { background: var(--ok-bg); color: var(--ok-text); border: 1px solid var(--ok-edge); }
.badge.no { background: var(--no-bg); color: var(--no-text); border: 1px solid var(--no-edge); }
.badge.ghost { background: var(--panel-2); color: var(--muted); border: 1px solid var(--panel-2-edge); }
.idchips {
  width: 100%;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--muted);
}
.idchips b { color: var(--label-id); font-weight: 600; }

.values {
  margin: 4px 0 0;
  padding: 9px 10px;
  border-radius: 9px;
  background: var(--log-bg);
  border: 1px solid var(--panel-2-edge);
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.65;
  color: var(--code-text);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 170px;
  overflow-y: auto;
}

.log {
  margin: 0;
  height: 190px;
  overflow-y: auto;
  padding: 9px 10px;
  border-radius: 9px;
  background: var(--log-bg);
  border: 1px solid var(--panel-2-edge);
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}
.log .lg.stmt { color: var(--log-stmt); }
.log .lg.out  { color: var(--log-out); }
.log .lg.note { color: var(--amber); }
.log .lg + .lg { margin-top: 2px; }

/* ---------------- stage ---------------- */

#stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: radial-gradient(120% 90% at 60% 0%, var(--stage-a) 0%, var(--stage-b) 60%);
}

#explain {
  margin: 14px 16px 0;
  padding: 11px 14px;
  border-radius: 11px;
  background: var(--overlay);
  border: 1px solid var(--card-edge);
  border-left: 3px solid var(--cyan);
  font-size: 13px;
  line-height: 1.55;
  color: var(--text);
  min-height: 46px;
}

#viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 12px 16px 16px;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--card-edge);
  background: var(--viewport-bg);
}

#viewport canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 1;
  cursor: grab;
}
#viewport canvas:active { cursor: grabbing; }

#labels {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  overflow: hidden;
}

.lbl {
  position: absolute;
  left: 0;
  top: 0;
  white-space: nowrap;
  color: var(--label);
  text-shadow: var(--label-shadow);
  will-change: transform;
}
.lbl.var {
  font-family: var(--mono);
  font-size: calc(19px * var(--lbl-scale));
  font-weight: 800;
  color: var(--plaque-text);
  letter-spacing: calc(0.4px * var(--lbl-scale));
}
.lbl.caption { font-family: var(--mono); font-size: calc(10.5px * var(--lbl-scale)); color: var(--plaque-caption); }
.lbl.caption.amber { color: var(--amber); }
.lbl.small { font-family: var(--mono); font-size: calc(10.5px * var(--lbl-scale)); color: var(--label-muted); }
.lbl.cell { font-family: var(--mono); font-size: calc(14px * var(--lbl-scale)); font-weight: 600; color: var(--cell); }
.lbl.cell.ref { font-size: calc(10.5px * var(--lbl-addr-scale)); font-weight: 600; color: var(--cell-ref); }
/* shared slots stay readable on the block: white text with a pink halo */
.lbl.cell.ref.shared { color: #ffffff; text-shadow: 0 0 7px rgba(219, 39, 119, 0.95), 0 1px 3px rgba(2, 8, 23, 0.8); }
.lbl.cell, .lbl.cell.ref, .lbl.cell.ref.shared { text-shadow: 0 1px 3px rgba(2, 8, 23, 0.75); }
.lbl.idx { font-size: calc(10.5px * var(--lbl-scale)); opacity: 0.95; }
.lbl.id { font-family: var(--mono); font-size: calc(10px * var(--lbl-addr-scale)); color: var(--label-id); opacity: 0.92; }
.lbl.tag {
  font-size: calc(10.5px * var(--lbl-tag-scale));
  font-weight: 700;
  padding: calc(2px * var(--lbl-tag-scale)) calc(9px * var(--lbl-tag-scale));
  border-radius: 999px;
  text-shadow: none;
}
.lbl.tag.amber { background: var(--amber); color: var(--tag-amber-text); }
.lbl.tag.pink { background: var(--pink); color: var(--tag-pink-text); }
.lbl.flash { animation: lblflash 0.9s ease-out; }

@keyframes lblflash {
  0% { filter: brightness(2.2); }
  100% { filter: brightness(1); }
}

#legend {
  position: absolute;
  left: 12px;
  bottom: 12px;
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--overlay);
  border: 1px solid var(--overlay-edge);
  font-size: 11px;
  color: var(--muted);
  backdrop-filter: blur(6px);
}
#legend span { display: flex; align-items: center; gap: 7px; }
.sw {
  display: inline-block;
  width: 16px;
  height: 4px;
  border-radius: 2px;
}
.sw.amber { background: var(--amber); }
.sw.cyan { background: var(--cyan); }
.sw.orange { background: var(--sw-orange); }
.sw.pink { background: var(--pink); }

/* -------- theme toggle -------- */
.theme-toggle {
  position: absolute;
  top: 12px;
  right: 14px;
  z-index: 6;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--overlay-edge);
  background: var(--overlay);
  color: var(--text);
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
  backdrop-filter: blur(6px);
}
.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }

.label-size {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 14px;
  border: 1px solid var(--overlay-edge);
  background: var(--overlay);
  color: var(--text);
  font-size: 11px;
  font-weight: 700;
  backdrop-filter: blur(6px);
}
.label-size .ls-row { display: flex; align-items: center; gap: 7px; }
.label-size .ls-key {
  width: 26px;
  text-align: center;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 700;
}
.label-size input[type="range"] { width: 96px; accent-color: var(--accent); cursor: pointer; }
.label-size output { min-width: 34px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.label-size .ls-btn {
  margin-top: 2px;
  padding: 5px 9px;
  border-radius: 8px;
  border: 1px solid var(--overlay-edge);
  background: var(--input-bg);
  color: var(--text);
  font-size: 10.5px;
  font-weight: 700;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.label-size .ls-btn:hover { border-color: var(--accent); color: var(--accent); }
.label-size .ls-btn.off { color: var(--muted); border-style: dashed; }

/* the refs toggle hides the floating “shared · n refs” pills */
#labels.hide-ref-tags .lbl.tag.pink { display: none; }

#hint {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 3;
  font-size: 11px;
  color: var(--hint-text);
  background: var(--hint-bg);
  padding: 5px 10px;
  border-radius: 8px;
}

/* ---------------- responsive ---------------- */

@media (max-width: 1020px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(420px, 62vh);
    height: auto;
    overflow: visible;
  }
  #panel { border-right: none; border-bottom: 1px solid var(--card-edge); }
  html, body { height: auto; }
}
