:root {
  --desktop: #cfd6df;
  --chrome: #eceef2;
  --chrome-mid: #d9dde5;
  --chrome-dark: #8d98a8;
  --panel: #f7f8fa;
  --paper: #ffffff;
  --ink: #17202b;
  --muted: #596676;
  --line: #a9b3c0;
  --line-soft: #dce1e8;
  --title: #24537d;
  --title-dark: #183853;
  --accent: #0d716b;
  --accent-2: #2f6092;
  --warn: #b36b00;
  --danger: #b84535;
  --ok: #2f7a45;
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  font-family: "Segoe UI", Tahoma, Arial, sans-serif;
  background: #b9c2cf;
  color: var(--ink);
  letter-spacing: 0;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) 25px;
  border: 1px solid #788492;
  background: var(--desktop);
}

.topbar {
  display: grid;
  grid-template-rows: 30px 24px 36px;
  border-bottom: 1px solid #7f8794;
  background: var(--chrome);
  z-index: 10;
}

.title-strip {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 8px;
  background: linear-gradient(90deg, #132f49, var(--title) 62%, #2a617d);
  color: #fff;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-mark {
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.62);
  border-radius: 3px;
  background: linear-gradient(#173e5e, #0d2438);
  color: white;
  font-size: 10px;
  font-weight: 800;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 13px;
  line-height: 1;
  font-weight: 700;
}

.brand p {
  color: rgba(255, 255, 255, 0.82);
  font-size: 10px;
  margin-top: 2px;
}

.window-controls {
  display: flex;
  gap: 5px;
  flex: none;
}

.window-controls span {
  width: 20px;
  height: 16px;
  border: 1px solid rgba(255, 255, 255, 0.48);
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.14);
}

.menu-strip {
  display: flex;
  align-items: center;
  gap: 1px;
  padding: 0 6px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(#fafbfc, #eef1f5);
  overflow-x: auto;
  scrollbar-width: thin;
}

.menu-item {
  position: relative;
  height: 100%;
  display: flex;
  align-items: center;
}

.menu-strip button {
  height: 21px;
  padding: 0 10px;
  border: 1px solid transparent;
  background: transparent;
  color: #111827;
  font-size: 12px;
}

.menu-strip button:hover,
.menu-strip button.active {
  border-color: var(--line);
  border-radius: 3px;
  background: #fff;
}

.menu-dropdown {
  position: absolute;
  left: 0;
  top: 23px;
  z-index: 30;
  display: none;
  width: 222px;
  border: 1px solid #697586;
  border-radius: 3px;
  background: #f8fafc;
  box-shadow: 0 12px 26px rgba(17, 24, 39, 0.24);
  padding: 4px;
}

.menu-dropdown.open {
  display: block;
}

.menu-dropdown button {
  width: 100%;
  height: 24px;
  justify-content: flex-start;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 2px;
  background: transparent;
  text-align: left;
}

.menu-dropdown button:hover,
.menu-dropdown button:focus {
  border-color: #9fb1c7;
  background: #e7f1fb;
}

.menu-separator {
  height: 1px;
  margin: 4px 2px;
  background: #c8d0da;
}

.recent-heading {
  padding: 2px 8px 4px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
}

.recent-projects {
  display: grid;
  gap: 1px;
}

.recent-projects .empty-recent {
  padding: 5px 8px;
  color: var(--muted);
  font-size: 11px;
}

.command-strip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 7px;
  border-top: 1px solid #fff;
  background: linear-gradient(#f6f7f9, #d8dde5);
  overflow-x: auto;
  scrollbar-width: thin;
}

.toolbar-group,
.top-actions,
.mini-actions,
.inline-controls,
.backend-actions {
  display: flex;
  align-items: center;
  gap: 5px;
}

.toolbar-group {
  min-height: 26px;
  padding: 2px 5px;
  border: 1px solid #b4bdc9;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.35);
}

.tool-button,
.button,
.ghost,
.file-button,
.tab {
  min-width: 0;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0 9px;
  border: 1px solid #8e97a5;
  border-radius: 4px;
  background: linear-gradient(#fff, #dfe3e9);
  color: #111827;
  font-size: 12px;
  white-space: nowrap;
}

.tool-button:hover,
.button:hover,
.ghost:hover,
.file-button:hover,
.tab:hover {
  background: linear-gradient(#fff, #eef2f6);
}

.tool-button.strong,
.button.primary,
.tab.active,
.tool-button.active {
  border-color: #0a5c56;
  background: linear-gradient(#139087, #0a6964);
  color: #fff;
}

.studio {
  position: relative;
  min-height: 0;
  padding: 6px;
  background: #bfc6d1;
  overflow: hidden;
}

.panel,
.canvas-panel {
  border: 1px solid #737f8f;
  border-radius: 5px;
  background: var(--panel);
  box-shadow: 0 8px 22px rgba(17, 24, 39, 0.16), 0 1px 0 rgba(255, 255, 255, 0.7) inset;
}

.canvas-panel {
  position: absolute;
  inset: 6px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 1;
}

.left-panel {
  position: absolute;
  left: 14px;
  right: auto;
  top: 58px;
  bottom: 12px;
  z-index: 4;
  width: 38px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 160ms ease;
}

.right-panel {
  position: absolute;
  top: 58px;
  right: 14px;
  bottom: 12px;
  z-index: 5;
  width: 38px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 160ms ease;
}

.left-panel:hover,
.left-panel:focus-within,
.left-panel.pinned {
  width: 252px;
}

.right-panel:hover,
.right-panel:focus-within,
.right-panel.pinned {
  width: 278px;
}

.left-panel:not(.pinned):not(:hover):not(:focus-within) .section,
.right-panel:not(.pinned):not(:hover):not(:focus-within) .tabs,
.right-panel:not(.pinned):not(:hover):not(:focus-within) .tab-panel {
  display: none;
}

.left-panel.pinned .section,
.right-panel.pinned .tabs,
.right-panel.pinned .tab-panel {
  display: block;
}

.dock-title {
  height: 22px;
  display: flex;
  align-items: center;
  padding: 0 8px;
  border-bottom: 1px solid #778191;
  background: linear-gradient(90deg, #234b73, #4e739c);
  color: white;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  user-select: none;
}

.left-panel:not(.pinned):not(:hover):not(:focus-within) .dock-title,
.right-panel:not(.pinned):not(:hover):not(:focus-within) .dock-title {
  flex: 1;
  height: auto;
  justify-content: center;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  padding: 8px 0;
  letter-spacing: 0;
  background: linear-gradient(180deg, #224d73, #16626d);
}

.left-panel.pinned .dock-title,
.right-panel.pinned .dock-title {
  flex: none;
  height: 22px;
  justify-content: flex-start;
  writing-mode: horizontal-tb;
  transform: none;
  padding: 0 8px;
  background: linear-gradient(90deg, #234b73, #4e739c);
}

.section {
  min-height: 0;
  padding: 8px;
  border-right: 1px solid var(--line);
  overflow: auto;
}

.section:last-child {
  border-right: 0;
}

.left-panel .section {
  border-right: 0;
  border-bottom: 1px solid var(--line);
}

.left-panel .section:last-child {
  border-bottom: 0;
}

.syntax-section {
  flex: 1 1 190px;
  background: #f8f8f9;
}

.left-panel .compact {
  flex: 0 0 auto;
}

.data-section {
  flex: 1 1 170px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 7px;
}

h2 {
  margin-bottom: 7px;
  color: #2f3845;
  font-size: 12px;
  font-weight: 700;
}

.section-title h2 {
  margin-bottom: 0;
}

textarea,
input {
  width: 100%;
  border: 1px solid #8e97a5;
  border-radius: 3px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

textarea:focus,
input:focus {
  border-color: #1f4f8f;
  box-shadow: inset 0 0 0 1px #1f4f8f;
}

textarea {
  resize: none;
  min-height: 112px;
  padding: 8px;
  font-family: "Cascadia Mono", "Courier New", monospace;
  font-size: 12px;
  line-height: 1.42;
}

.data-section textarea,
#resultsInput,
#backendOutput {
  min-height: 92px;
}

#backendOutput {
  min-height: 420px;
  margin-top: 8px;
}

input {
  height: 27px;
  padding: 4px 6px;
  font-size: 12px;
}

label span {
  display: block;
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 11px;
}

.ghost {
  height: 23px;
  padding: 0 8px;
  font-size: 11px;
}

.file-button {
  position: relative;
  overflow: hidden;
  height: 23px;
  font-size: 11px;
}

.file-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
}

.full {
  width: 100%;
}

.builder-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
}

.builder-grid .full {
  grid-column: 1 / -1;
}

.inline-controls {
  align-items: end;
  margin-top: 7px;
}

.inline-controls label {
  flex: 1;
}

.canvas-toolbar {
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 4px 9px;
  border-bottom: 1px solid #778191;
  background: linear-gradient(#fbfcfd, #e0e5ec);
}

.summary-strip {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  min-width: 0;
  scrollbar-width: thin;
}

.stat,
.fit-card {
  min-width: 55px;
  border: 1px solid #a3adba;
  border-radius: 3px;
  background: linear-gradient(#fff, #f3f6f9);
  padding: 3px 6px;
}

.stat b,
.fit-card b {
  display: block;
  font-size: 13px;
  line-height: 1;
}

.stat span,
.fit-card span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 10px;
}

.canvas-path {
  color: #425061;
  font-family: "Cascadia Mono", "Courier New", monospace;
  font-size: 10px;
  flex: 0 1 auto;
  min-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.diagram-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(69, 78, 92, 0.075) 1px, transparent 1px),
    linear-gradient(rgba(69, 78, 92, 0.075) 1px, transparent 1px),
    linear-gradient(#ffffff, #fbfcfd);
  background-size: 24px 24px;
}

.diagram-wrap::before,
.diagram-wrap::after {
  content: "";
  position: absolute;
  z-index: 0;
  pointer-events: none;
  background: #e5e7eb;
}

.diagram-wrap::before {
  left: 0;
  top: 0;
  right: 0;
  height: 20px;
  border-bottom: 1px solid #b8c0cc;
}

.diagram-wrap::after {
  left: 0;
  top: 0;
  bottom: 0;
  width: 20px;
  border-right: 1px solid #b8c0cc;
}

#diagram {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
  touch-action: none;
}

.edge {
  fill: none;
  stroke: #384554;
  stroke-width: 2.4;
}

.edge-loading {
  stroke: #2d6193;
}

.edge-regression {
  stroke: #087a72;
}

.edge-covariance {
  stroke: #bd4f40;
  stroke-width: 2;
}

.edge.selected {
  stroke-width: 4;
}

.arrow-head {
  fill: currentColor;
  color: #3f4752;
}

.edge-label {
  font-size: 14px;
  font-weight: 700;
  fill: var(--ink);
  paint-order: stroke;
  stroke: #fff;
  stroke-width: 5px;
  stroke-linejoin: round;
  pointer-events: none;
}

.node {
  cursor: grab;
}

.node:active {
  cursor: grabbing;
}

.node rect,
.node ellipse {
  fill: #ffffff;
  stroke: #1f2937;
  stroke-width: 2;
  filter: drop-shadow(0 3px 3px rgba(17, 24, 39, 0.2));
}

.node-latent ellipse {
  fill: #eaf7f6;
  stroke: #08615c;
}

.node.selected rect,
.node.selected ellipse {
  stroke: var(--warn);
  stroke-width: 4;
}

.node-label {
  fill: var(--ink);
  font-size: 16px;
  font-weight: 800;
  pointer-events: none;
}

.node-label.small {
  font-size: 12px;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 3px;
  padding: 6px;
  border-bottom: 1px solid var(--line);
  background: #e5e8ee;
}

.tab {
  height: 26px;
  padding: 0 6px;
  font-size: 11px;
}

.tab-panel {
  display: none;
  flex: 1;
  min-height: 0;
  padding: 9px;
  overflow: auto;
}

.tab-panel.active {
  display: block;
}

.selection-card,
.issue,
.empty-state {
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: #fff;
  padding: 8px;
}

.selection-card {
  border-left: 4px solid var(--accent);
}

.selection-card span {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
}

.selection-card h3 {
  margin-top: 3px;
  font-size: 16px;
}

.selection-card p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.issue {
  display: grid;
  gap: 3px;
  border-left: 4px solid var(--accent-2);
}

.issue b {
  font-size: 12px;
}

.issue span,
.empty-state {
  color: var(--muted);
  font-size: 11px;
}

.issue.error {
  border-left-color: var(--danger);
}

.issue.warning {
  border-left-color: var(--warn);
}

.issue.ok {
  border-left-color: var(--ok);
}

.table-wrap {
  overflow: auto;
  margin-bottom: 14px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 3px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
  background: #fff;
}

th,
td {
  padding: 6px 7px;
  text-align: left;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: middle;
}

th {
  position: sticky;
  top: 0;
  color: #344052;
  font-weight: 700;
  background: #eef0f4;
}

tr:last-child td {
  border-bottom: 0;
}

.kind-dot {
  width: 8px;
  height: 8px;
  display: inline-block;
  margin-right: 6px;
  border-radius: 999px;
  background: var(--accent-2);
}

.kind-dot.latent {
  background: var(--accent);
}

.fit-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin: 9px 0 14px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 34px;
  z-index: 20;
  transform: translate(-50%, 12px);
  opacity: 0;
  pointer-events: none;
  border: 1px solid #1f2937;
  border-radius: 4px;
  background: #111827;
  color: #fff;
  padding: 8px 12px;
  font-size: 12px;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: rgba(17, 24, 39, 0.34);
  padding: 18px;
}

.dialog-backdrop[hidden] {
  display: none;
}

.dialog {
  width: min(420px, calc(100vw - 36px));
  border: 1px solid #4b5563;
  border-radius: 5px;
  background: #f8fafc;
  box-shadow: 0 22px 48px rgba(17, 24, 39, 0.35), inset 0 1px 0 #fff;
  overflow: hidden;
}

.dialog-titlebar {
  min-height: 32px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  background: linear-gradient(90deg, #132f49, var(--title) 62%, #2a617d);
  color: #fff;
}

.dialog-titlebar h2 {
  margin: 0;
  color: #fff;
  font-size: 13px;
}

.dialog p {
  padding: 16px 16px 8px;
  color: #273244;
  font-size: 13px;
  line-height: 1.45;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 16px 16px;
}

.dialog-actions .button {
  min-width: 86px;
}

.dialog-open .toast {
  display: none;
}

.statusbar {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(130px, 0.8fr) minmax(160px, 1fr) minmax(76px, auto);
  gap: 2px;
  border-top: 1px solid #7f8794;
  background: var(--chrome);
  color: #2f3845;
  font-size: 11px;
}

.statusbar span {
  display: flex;
  align-items: center;
  min-width: 0;
  padding: 0 8px;
  border-right: 1px solid var(--line);
  box-shadow: inset 1px 1px 0 #fff;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.statusbar span:last-child {
  border-right: 0;
}

@media (max-width: 1160px) {
  .right-panel:hover,
  .right-panel:focus-within,
  .right-panel.pinned {
    width: 252px;
  }

  .left-panel:hover,
  .left-panel:focus-within,
  .left-panel.pinned {
    width: 240px;
  }

  .data-section {
    display: none;
  }
}

@media (max-width: 820px) {
  body {
    overflow: hidden;
  }

  .app {
    height: 100vh;
    min-height: 0;
    grid-template-rows: auto minmax(0, 1fr) 25px;
  }

  .menu-strip,
  .command-strip {
    flex-wrap: nowrap;
    height: auto;
  }

  .menu-strip button {
    padding: 0 8px;
  }

  .studio {
    position: relative;
    min-height: 0;
    overflow: hidden;
  }

  .canvas-panel,
  .left-panel,
  .right-panel {
    position: absolute;
  }

  .canvas-panel {
    inset: 6px;
    height: auto;
    min-height: 0;
  }

  .left-panel {
    left: 14px;
    top: 58px;
    bottom: 12px;
    width: 36px;
    display: flex;
  }

  .right-panel {
    top: 58px;
    right: 14px;
    bottom: 12px;
    width: 36px;
  }

  .left-panel:hover,
  .left-panel:focus-within,
  .left-panel.pinned {
    width: min(252px, calc(100vw - 56px));
  }

  .right-panel:hover,
  .right-panel:focus-within,
  .right-panel.pinned {
    width: min(278px, calc(100vw - 56px));
  }

  .left-panel:not(.pinned):not(:hover):not(:focus-within) .section,
  .right-panel:not(.pinned):not(:hover):not(:focus-within) .tabs,
  .right-panel:not(.pinned):not(:hover):not(:focus-within) .tab-panel {
    display: none;
  }

  .left-panel.pinned .section,
  .right-panel.pinned .tabs,
  .right-panel.pinned .tab-panel {
    display: block;
  }

  .left-panel:not(.pinned):not(:hover):not(:focus-within) .dock-title,
  .right-panel:not(.pinned):not(:hover):not(:focus-within) .dock-title {
    flex: 1;
    height: auto;
    justify-content: center;
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    padding: 8px 0;
  }

  .left-panel.pinned .dock-title,
  .right-panel.pinned .dock-title {
    flex: none;
    height: 22px;
    justify-content: flex-start;
    writing-mode: horizontal-tb;
    transform: none;
    padding: 0 8px;
  }

  .section {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .statusbar {
    grid-template-columns: minmax(0, 1.2fr) minmax(80px, 0.8fr) minmax(92px, 1fr) 72px;
  }
}
