:root {
  color-scheme: dark;

  --surface: #000000;
  --surface-2: #0f0f0f;
  --surface-3: #1a1a1a;
  --ink: #ffffff;
  --muted: #b3b3b3;
  --border: #3a3a3a;
  --accent: #ffffff;

  --control-bg: var(--accent);
  --control-ink: #000000;
  --control-border: var(--accent);

  --font-mono: "JetBrains Mono", "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
  --fs-base: 14px;
  --lh-base: 1.5;

  --h1-size: 32px;
  --h1-weight: 700;
  --h1-tracking: 0.04em;

  --h2-size: 18px;
  --h2-weight: 600;

  --label-size: 12px;
  --label-tracking: 0.08em;

  --radius: 0px;
  --border-width: 1px;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 28px;

  --seq-1: #15223f;
  --seq-2: #1e3a6d;
  --seq-3: #275aa5;
  --seq-4: #2f85d3;
  --seq-5: #6bb6ff;
  --seq-6: #f3d35c;
  --seq-7: #f0a23b;
  --seq-8: #d45a33;
  --seq-9: #b5292e;

  --div-1: #053061;
  --div-2: #2166ac;
  --div-3: #4393c3;
  --div-4: #92c5de;
  --div-5: #f7f7f7;
  --div-6: #f4a582;
  --div-7: #d6604d;
  --div-8: #b2182b;
  --div-9: #67001f;

  --viz-bg: var(--surface);
  --viz-link: rgba(255, 255, 255, 0.10);
  --viz-link-strong: rgba(255, 255, 255, 0.16);
  --viz-stroke: rgba(255, 255, 255, 0.22);
  --viz-label: var(--ink);
  --viz-node: #000000;
  --viz-node-fill: #000000;
  --viz-node-stroke: var(--viz-stroke);
  --viz-highlight: var(--seq-5);
}

body.theme-invert {
  color-scheme: light;

  --surface: #ffffff;
  --surface-2: #f4f4f4;
  --surface-3: #e9e9e9;
  --ink: #111111;
  --muted: #4a4a4a;
  --border: #d9d9d9;
  --accent: #000000;

  --control-bg: var(--accent);
  --control-ink: #ffffff;
  --control-border: var(--accent);

  --viz-link: rgba(17, 17, 17, 0.10);
  --viz-link-strong: rgba(17, 17, 17, 0.16);
  --viz-stroke: rgba(17, 17, 17, 0.22);
  --viz-highlight: var(--seq-4);
}

body.dark-mode,
html.dark-mode {
  color-scheme: light;

  --surface: #ffffff;
  --surface-2: #f4f4f4;
  --surface-3: #e9e9e9;
  --ink: #111111;
  --muted: #4a4a4a;
  --border: #d9d9d9;
  --accent: #000000;

  --control-bg: var(--accent);
  --control-ink: #ffffff;
  --control-border: var(--accent);

  --viz-link: rgba(17, 17, 17, 0.10);
  --viz-link-strong: rgba(17, 17, 17, 0.16);
  --viz-stroke: rgba(17, 17, 17, 0.22);
  --viz-highlight: var(--seq-4);
}

html,
body {
  height: 100%;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  transition: none;
  border-radius: 0;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: var(--fs-base);
  line-height: var(--lh-base);
  color: var(--ink);
  background: var(--surface);
}

h1 {
  margin: 0;
  font-size: var(--h1-size);
  font-weight: var(--h1-weight);
  letter-spacing: var(--h1-tracking);
  text-transform: uppercase;
}

h2,
h3 {
  margin: 0;
  font-size: var(--h2-size);
  font-weight: var(--h2-weight);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 36px;
  padding: 10px 12px;
  border: var(--border-width) solid var(--control-border);
  background: var(--control-bg);
  color: var(--control-ink);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 120ms linear, background-color 120ms linear, border-color 120ms linear;
}

button:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

input[type="search"],
input[type="text"],
input[type="email"],
input[type="url"],
input[type="number"],
select,
textarea {
  height: 34px;
  padding: 8px 10px;
  border: var(--border-width) solid var(--control-border);
  background: var(--control-bg);
  color: var(--control-ink);
  transition: color 120ms linear, background-color 120ms linear, border-color 120ms linear;
}

textarea {
  min-height: 80px;
  height: auto;
  resize: none;
}

input::placeholder,
textarea::placeholder {
  color: var(--muted);
}

.glass-surface,
.panel {
  border: var(--border-width) solid var(--border);
  background: var(--surface-2);
}

.is-hidden {
  display: none !important;
}

.toggle-btn {
  width: 36px;
  min-width: 36px;
  height: 36px;
  min-height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  line-height: 1;
  letter-spacing: 0;
  text-transform: none;
}

.toggle-btn .icon {
  margin: 0;
}

#edit-toggle,
#settings-toggle,
#info-toggle {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.toggle-btn--active,
.toggle-btn[aria-pressed="true"] {
  background: var(--control-bg);
  color: var(--control-ink);
  border-color: var(--control-ink);
  text-decoration: none;
}

.toggle-btn--disabled,
.toggle-btn:disabled {
  color: var(--muted);
  border-color: var(--border);
  background: var(--surface-2);
  cursor: not-allowed;
}

#controls-wrapper {
  position: fixed;
  top: var(--space-6);
  right: var(--space-6);
  z-index: 60;
  display: grid;
  gap: var(--space-2);
  width: 36px;
}

#controls-wrapper .toggle-btn,
#controls-wrapper #view-style-toggle {
  position: static !important;
}

#fullscreen-toggle {
  margin-top: var(--space-3);
}

#view-style-toggle {
  position: relative;
}

#view-style-toggle span {
  line-height: 1;
}

#view-style-menu {
  position: absolute;
  top: 0;
  right: calc(100% + var(--space-2));
  min-width: 200px;
  padding: var(--space-3);
  display: grid;
  gap: var(--space-2);
  z-index: 95;
}

.dropdown-item,
.palette-btn,
.export-btn,
.info-menu-topic-btn,
#search-bar button,
.settings-close-btn,
#close-editor,
#info-menu-back {
  width: 100%;
  justify-content: flex-start;
  text-align: left;
}

#palette-menu,
.export-bar {
  position: fixed;
  top: var(--space-6);
  right: calc(var(--space-6) + 52px);
  width: 280px;
  z-index: 80;
  display: grid;
  gap: var(--space-2);
  padding: var(--space-3);
}

.palette-title,
.settings-title,
.legend-title {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.legend {
  display: grid;
  gap: var(--space-2);
}

.legend-scale {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  border: var(--border-width) solid var(--border);
}

.legend-swatch {
  height: 10px;
}

.legend-swatch:nth-child(1) { background: var(--seq-1); }
.legend-swatch:nth-child(2) { background: var(--seq-2); }
.legend-swatch:nth-child(3) { background: var(--seq-3); }
.legend-swatch:nth-child(4) { background: var(--seq-4); }
.legend-swatch:nth-child(5) { background: var(--seq-5); }
.legend-swatch:nth-child(6) { background: var(--seq-6); }
.legend-swatch:nth-child(7) { background: var(--seq-7); }
.legend-swatch:nth-child(8) { background: var(--seq-8); }
.legend-swatch:nth-child(9) { background: var(--seq-9); }

.legend--diverging .legend-swatch:nth-child(1) { background: var(--div-1); }
.legend--diverging .legend-swatch:nth-child(2) { background: var(--div-2); }
.legend--diverging .legend-swatch:nth-child(3) { background: var(--div-3); }
.legend--diverging .legend-swatch:nth-child(4) { background: var(--div-4); }
.legend--diverging .legend-swatch:nth-child(5) { background: var(--div-5); }
.legend--diverging .legend-swatch:nth-child(6) { background: var(--div-6); }
.legend--diverging .legend-swatch:nth-child(7) { background: var(--div-7); }
.legend--diverging .legend-swatch:nth-child(8) { background: var(--div-8); }
.legend--diverging .legend-swatch:nth-child(9) { background: var(--div-9); }

#search-toggle,
#info-toggle {
  position: fixed;
  left: var(--space-6);
  z-index: 70;
}

#search-toggle {
  top: var(--space-6);
}

#info-toggle {
  bottom: 56px;
}

#search-bar {
  position: fixed;
  top: var(--space-6);
  left: calc(var(--space-6) + 52px);
  z-index: 85;
  width: min(520px, calc(100vw - 180px));
  padding: var(--space-4);
  display: grid;
  gap: var(--space-2);
}

#search-bar > div {
  display: flex;
  gap: var(--space-2);
}

#search-bar input {
  width: 100%;
}

#search-results {
  display: none;
  max-height: 220px;
  overflow-y: auto;
  border: var(--border-width) solid var(--border);
  background: var(--surface);
  padding: var(--space-2);
  font-size: 13px;
}

.search-result {
  padding: var(--space-1) var(--space-2);
  border-bottom: var(--border-width) solid var(--border);
  cursor: pointer;
}

.search-result:last-child {
  border-bottom: 0;
}

.search-result:hover {
  background: var(--surface-3);
}

.search-highlight {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

#editor-container {
  position: fixed;
  top: 0;
  left: 0;
  width: min(52vw, 760px);
  height: 100vh;
  z-index: 110;
  border-right: var(--border-width) solid var(--border);
  background: var(--surface-2);
}

#markdown-editor {
  width: 100%;
  height: 100%;
  min-height: 100%;
  padding: var(--space-6);
  border: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.6;
  tab-size: 2;
}

#close-editor {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  width: auto;
}

#settings-menu {
  position: fixed;
  top: var(--space-6);
  right: calc(var(--space-6) + 52px);
  width: min(420px, calc(100vw - 140px));
  z-index: 100;
  padding: var(--space-4);
  display: grid;
  gap: var(--space-4);
}

.settings-slider-group {
  display: grid;
  gap: var(--space-1);
}

.settings-slider-group label {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.settings-slider-group input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: var(--ink);
}

#settings-menu input[type="number"] {
  width: 100%;
}

#settings-menu select {
  width: 100%;
}

#settings-menu .settings-close-btn {
  justify-self: start;
  width: auto;
}

#info-menu {
  position: fixed;
  left: calc(var(--space-6) + 52px);
  bottom: 56px;
  width: min(520px, calc(100vw - 180px));
  max-height: 72vh;
  z-index: 100;
}

.info-menu-content {
  padding: var(--space-4);
  overflow-y: auto;
  max-height: 72vh;
  display: grid;
  gap: var(--space-3);
}

.info-menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}

#info-menu-title {
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.info-menu-close-btn {
  width: 36px;
  min-width: 36px;
  padding: 0;
}

#info-menu-list {
  display: grid;
  gap: var(--space-2);
}

#info-menu-md-content {
  display: grid;
  gap: var(--space-3);
}

#info-menu-md-content h1,
#info-menu-md-content h2,
#info-menu-md-content h3,
#info-menu-md-content p,
#info-menu-md-content li,
#info-menu-md-content code,
#info-menu-md-content pre {
  color: var(--ink);
  font-family: var(--font-mono);
}

#info-menu-md-content a {
  color: var(--ink);
  text-decoration: underline;
}

#info-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  min-height: 40px;
  z-index: 40;
  border-top: var(--border-width) solid var(--border);
  background: var(--surface-2);
  color: var(--ink);
  padding: var(--space-2) var(--space-6);
  pointer-events: none;
  white-space: pre-line;
  font-size: 13px;
  line-height: 1.4;
}

span.path {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-thickness: 1px;
}

svg {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--viz-bg);
}

.link {
  stroke: var(--viz-link);
  stroke-width: 1.5px;
}

.link.type-regulates { stroke: var(--div-8); }
.link.type-contracts { stroke: var(--seq-7); }
.link.type-influences { stroke: var(--seq-4); }
.link.type-supports { stroke: var(--seq-6); }
.link.type-default { stroke: var(--viz-link-strong); }

.link-label {
  font-size: 13px;
  fill: var(--muted);
  pointer-events: none;
  opacity: 0;
  transition: opacity 120ms linear, color 120ms linear;
}

.link-label--visible,
.show-relations .link-label {
  opacity: 1 !important;
}

.node circle {
  stroke: var(--viz-node-stroke);
  stroke-width: 1px;
}

.node.center circle {
  stroke-width: 2px;
  stroke: var(--ink);
}

.node text {
  font-family: var(--font-mono);
  font-size: 15px;
  fill: var(--viz-label);
  font-weight: 500;
  pointer-events: none;
  text-anchor: middle;
  opacity: 0;
  paint-order: stroke fill;
  stroke: var(--surface);
  stroke-width: 0.75px;
  stroke-opacity: 1;
}

.node.selected text,
.node:hover text {
  opacity: 1 !important;
}

.node.manual-selected circle {
  stroke: var(--ink);
  stroke-width: 3px;
}

.node.manual-selected text {
  opacity: 1 !important;
}

.tree-view-compact .node text {
  font-size: 11px !important;
}

.tree-view-highlight .node:hover text {
  opacity: 1 !important;
}

.tree-view-highlight .node:hover circle {
  stroke-width: 2px;
  stroke: var(--ink);
}

.chart-axis,
.chart-domain {
  stroke: var(--border);
  fill: none;
}

.chart-grid,
.chart-tick line {
  stroke: var(--muted);
  fill: none;
}

@media (max-width: 720px) {
  #controls-wrapper {
    top: var(--space-4);
    right: var(--space-4);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    width: 36px;
    gap: var(--space-1);
  }

  #fullscreen-toggle {
    margin-top: 0;
  }

  #import-toggle { order: 1; }
  #edit-toggle { order: 2; }
  #graphmode-toggle { order: 3; }
  #showall-toggle { order: 4; }
  #relations-toggle { order: 5; }
  #settings-toggle { order: 6; }
  #palette-toggle { order: 7; }
  #darkmode-toggle { order: 8; }
  #sticky-nodes-toggle { order: 9; }
  #cluster-toggle { order: 10; }
  #view-style-toggle { order: 11; }
  #fullscreen-toggle { order: 12; }
  #export-toggle { order: 13; }
  #clear-toggle { order: 14; }

  #search-toggle,
  #info-toggle {
    left: var(--space-4);
  }

  #search-toggle {
    top: var(--space-4);
  }

  #info-toggle {
    bottom: 52px;
  }

  #palette-menu,
  .export-bar,
  #settings-menu,
  #search-bar,
  #info-menu {
    left: var(--space-4);
    right: var(--space-4);
    width: auto;
  }

  #palette-menu,
  .export-bar,
  #settings-menu {
    top: calc(var(--space-4) + 84px);
  }

  #search-bar {
    top: calc(var(--space-4) + 44px);
  }

  #info-menu {
    bottom: 52px;
  }

  #editor-container {
    width: 100vw;
  }

  #info-bar {
    padding-left: var(--space-4);
    padding-right: var(--space-4);
    font-size: 12px;
  }
}
