:root {
  --bg: #f3efe7;
  --panel: #efe3cf;
  --accent: #2b574a;
  --accent-2: #d17a3a;
  --text: #1b1a17;
  --wire: #2b3c63;
  --topbar-height: 48px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  background: radial-gradient(circle at 20% 20%, #fff9f1 0, #f3efe7 50%, #e8dfcf 100%);
  color: var(--text);
  min-height: 100vh;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

html {
  background: radial-gradient(circle at 20% 20%, #fff9f1 0, #f3efe7 50%, #e8dfcf 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  height: 100lvh;
  background: #ffffff;
  z-index: -1;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-height);
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 2px solid #d6c3a4;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.brand-title {
  font-weight: 800;
  font-size: 1.4rem;
  letter-spacing: 0.5px;
}

.brand-subtitle {
  font-size: 0.85rem;
  opacity: 0.75;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.runtime {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  white-space: nowrap;
}

.runtime-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.runtime-row .runtime {
  flex: 1 1 auto;
  min-width: 0;
}

.runtime input {
  width: 100%;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid #9c8c73;
}

.controls button {
  border: 2px solid var(--accent);
  background: var(--accent);
  color: #fff;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
}

.controls button#clearBtn {
  border-color: var(--accent-2);
  background: var(--accent-2);
}

.controls button#deleteWireBtn.active {
  border-color: #1f2d2a;
  background: #1f2d2a;
}

.status {
  font-size: 0.9rem;
  opacity: 0.8;
}



.layout {
  display: grid;
  grid-template-columns: 240px 1fr minmax(260px, 25%);
  grid-template-rows: 1fr;
  grid-template-areas:
    "library workspace right";
  min-height: 0;
  height: calc(100vh - var(--topbar-height));
  flex: 1;
  gap: 12px;
  padding: 12px;
  overflow: hidden;
}

.panel-carousel {
  display: contents;
}

.toolbox {
  grid-area: library;
}

.right-column {
  grid-area: right;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  overflow: auto;
}

.right-column > .panel-card {
  flex-shrink: 0;
}

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

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

.codegen-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.codegen-row .param {
  flex: 1 1 auto;
  min-width: 0;
}

.param-inline {
  flex: 0 0 auto;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}

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

.margin-output {
  background: #f6eddc;
  border: 1px solid #d6c3a4;
  border-radius: 8px;
  padding: 10px;
  font-size: 12px;
  white-space: pre-wrap;
  max-height: 220px;
  overflow: auto;
}

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

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

.sim-panel .secondary {
  width: 100%;
}

.runtime-row .secondary {
  padding: 8px 12px;
  white-space: nowrap;
  width: auto;
  flex: 0 0 33%;
  margin-top: 0;
}

.button-row {
  display: flex;
  gap: 8px;
}

.button-row .secondary,
.button-row .danger {
  width: auto;
  flex: 1;
  margin-top: 0;
}

.inspector {
  grid-area: properties;
}

.variables-panel {
  grid-area: variables;
}

.debug-panel {
  grid-area: debug;
}

.toolbox {
  padding: 16px;
  background: linear-gradient(180deg, #efe3cf 0, #e1d0b7 100%);
  border: 2px solid #d6c3a4;
  border-radius: 12px;
  max-height: 100%;
  overflow: auto;
}

.toolbox h2,
.panel-carousel h2,
.panel-carousel h3 {
  margin: 0 0 8px 0;
  font-size: 1.1rem;
}

.tool-group {
  margin-bottom: 10px;
  border: 1px solid #c6b391;
  border-radius: 10px;
  background: #f7eddf;
  padding: 6px 8px;
}

.tool-group > summary {
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  margin-bottom: 6px;
}

.tool-group > summary::-webkit-details-marker {
  display: none;
}

.tool-group > summary::after {
  content: "▾";
  float: right;
  font-size: 12px;
  opacity: 0.7;
}

.tool-group[open] > summary::after {
  content: "▴";
}

.toolbox .tool {
  width: 100%;
  padding: 10px;
  margin-bottom: 8px;
  border: 1px solid #c6b391;
  background: #f7eddf;
  cursor: pointer;
  border-radius: 6px;
}

.toolbox .hint {
  font-size: 0.85rem;
  opacity: 0.8;
}

.panel-card {
  padding: 12px;
  background: #f7eddf;
  border: 1px solid #c6b391;
  border-radius: 8px;
}

.debug-panel h3 {
  margin: 0 0 6px;
}

#debugLog {
  background: #1b1a17;
  color: #f5e8d4;
  padding: 8px;
  border-radius: 6px;
  max-height: 160px;
  overflow: auto;
  font-size: 12px;
  white-space: pre-wrap;
}

.debug-panel .secondary {
  margin: 0 0 8px;
  width: 100%;
  border: 2px solid #2b2a27;
  background: #2b2a27;
  color: #f5e8d4;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
}

.secondary {
  margin-top: 10px;
  width: 100%;
  border: 2px solid #2b574a;
  background: #2b574a;
  color: #fff;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
}

.danger {
  margin-top: 10px;
  width: 100%;
  border: 2px solid #7c2c2c;
  background: #7c2c2c;
  color: #fff;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
}

.workspace {
  min-height: 0;
  display: flex;
  flex-direction: column;
  grid-area: workspace;
}

#canvas {
  position: relative;
  height: 100%;
  flex: 1;
  min-height: 0;
  user-select: none;
  border: 2px dashed #cbb79a;
  background-color: #ffffff;
  background-image:
    linear-gradient(to right, rgba(0, 0, 0, 0.06) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0, 0, 0, 0.06) 1px, transparent 1px);
  background-size: var(--grid-size, 10px) var(--grid-size, 10px);
  background-position: var(--grid-offset-x, 0px) var(--grid-offset-y, 0px);
  overflow: hidden;
}

#canvas svg {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.floating-controls {
  position: absolute;
  top: 14px;
  right: 14px;
  display: flex;
  gap: 10px;
  z-index: 5;
  flex-wrap: wrap;
}

.floating-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  background: #f7e9d5;
  border: 2px solid #3c3c3c;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 4px 0 #3c3c3c;
}

.floating-btn#runBtn {
  background: #2b574a;
  border-color: #2b574a;
  box-shadow: 0 4px 0 #1d3b32;
}

.floating-btn#runBtn svg {
  stroke: #fff;
}

.floating-btn#runBtn:active {
  box-shadow: 0 2px 0 #1d3b32;
}


.floating-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #3c3c3c;
}

.floating-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: #3c3c3c;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#svgCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.svg-block .block-body {
  fill: #fff7ec;
  stroke: #3c3c3c;
  stroke-width: 2;
}

.svg-block.selected .block-body,
.svg-block.selected .sum-circle,
.svg-block.selected .gain-triangle,
.svg-block.selected .file-icon-stroke {
  stroke: #d17a3a;
  stroke-width: 3;
}

.svg-block.loop-highlight .block-body,
.svg-block.loop-highlight .sum-circle,
.svg-block.loop-highlight .gain-triangle,
.svg-block.loop-highlight .file-icon-stroke {
  stroke: #2b7a78;
  stroke-width: 3;
}

.svg-block .block-text {
  font-size: 14px;
  font-weight: 700;
  fill: #1b1a17;
}

.resize-handle {
  fill: #f7e9d5;
  stroke: #3c3c3c;
  stroke-width: 2;
  cursor: nwse-resize;
}

.block-text.label-large {
  font-size: 28px;
}

.math-foreign {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  color: #1b1a17;
}

.math-foreign mjx-container {
  font-size: 40px;
  color: #1b1a17;
}

.gain-math .math-foreign,
.gain-math .math-foreign mjx-container {
  font-size: 28px;
}

.constant-math .math-foreign,
.constant-math .math-foreign mjx-container {
  font-size: 34px;
}

.ss-math .math-foreign,
.ss-math .math-foreign mjx-container,
.dss-math .math-foreign,
.dss-math .math-foreign mjx-container {
  font-size: 20px;
}

.delay-math .math-foreign,
.delay-math .math-foreign mjx-container {
  font-size: 30px;
}

.label-math .math-foreign,
.label-math .math-foreign mjx-container {
  font-size: 16px;
}

.label-node {
  fill: none;
  stroke: #3c3c3c;
  stroke-width: 2;
}

.pid-math .math-foreign,
.pid-math .math-foreign mjx-container,
.zoh-math .math-foreign,
.zoh-math .math-foreign mjx-container,
.foh-math .math-foreign,
.foh-math .math-foreign mjx-container {
  font-size: 21px;
}

.svg-block[data-rotation="180"] .upright {
  transform-box: fill-box;
  transform-origin: center;
  transform: rotate(180deg);
}

.svg-block[data-rotation="180"] foreignObject.upright {
  transform-box: fill-box;
  transform-origin: center;
  transform: rotate(180deg);
}

.block-icon {
  stroke: #3c3c3c;
  stroke-width: 2.4;
  fill: none;
}

.block-icon text {
  fill: #3c3c3c;
  font-size: 9px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
}

.source-axis,
.step-axis {
  stroke: #3c3c3c;
  stroke-width: 2;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.source-plot,
.step-plot {
  stroke: #3c3c3c;
  stroke-width: 2;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-math {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #3c3c3c;
}

.file-icon-stroke {
  fill: none;
  stroke: #3c3c3c;
  stroke-width: 2.5;
  stroke-linejoin: round;
}

.file-icon-label {
  font-size: 14px;
  font-weight: 700;
  fill: #1b1a17;
}

.drag-handle {
  fill: transparent;
  cursor: grab;
  touch-action: none;
}

.port {
  fill: #f5c97b;
  stroke: #3c3c3c;
  stroke-width: 2;
  opacity: 0;
}

.ports-visible .port {
  opacity: 1;
}

.port-hit {
  fill: transparent;
  stroke: transparent;
  pointer-events: all;
}

.port.pending {
  fill: #85c18b;
}

.wire {
  stroke: var(--wire);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  color: var(--wire);
  fill: none;
  pointer-events: stroke;
}

.wire-hit {
  stroke: transparent;
  stroke-width: 12;
  fill: none;
  pointer-events: stroke;
}

.wire.selected {
  stroke: #d17a3a;
  stroke-width: 2;
  color: #d17a3a;
}

.wire.wire-error {
  stroke: #dc2626;
  stroke-width: 2;
  color: #dc2626;
}

.wire.loop-highlight {
  stroke: #2b7a78;
  stroke-width: 3;
  color: #2b7a78;
}

.wire-arrow {
  fill: currentColor;
}

.selection-rect {
  fill: none;
  stroke: #2b574a;
  stroke-width: 2;
  stroke-dasharray: 6 4;
  pointer-events: none;
}

.selection-marquee {
  fill: rgba(43, 87, 74, 0.1);
  stroke: #2b574a;
  stroke-width: 2;
  stroke-dasharray: 6 4;
  pointer-events: none;
}

.sum-circle {
  fill: #f7eddf;
  stroke: #3c3c3c;
  stroke-width: 2;
}

.sum-line {
  stroke: #3c3c3c;
  stroke-width: 2;
}

.sum-sign {
  fill: #3c3c3c;
  font-size: 28px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
}

.gain-triangle {
  fill: #f7eddf;
  stroke: #3c3c3c;
  stroke-width: 2;
}

.gain-text {
  font-size: 19px;
  font-weight: 700;
  fill: #1b1a17;
  dominant-baseline: middle;
}

.scope-plot {
  fill: #121212;
  stroke: #3c3c3c;
  stroke-width: 1;
}

.scope-axis {
  stroke: #b7b7b7;
  stroke-width: 1.5;
}

.scope-tick {
  stroke: #b7b7b7;
  stroke-width: 1.2;
}

.scope-path {
  stroke: #7fd1b9;
  stroke-width: 2;
  fill: none;
}

.scope-path-1 { stroke: #f5e642; }
.scope-path-2 { stroke: #ff49c9; }
.scope-path-3 { stroke: #39d7ff; }

.scope-cursor {
  stroke: #ffffff;
  stroke-width: 2.5;
}

.scope-dot {
  fill: #f5c97b;
}

.scope-label {
  fill: #ffffff;
  font-size: 11px;
}

.dashed-plot {
  stroke-dasharray: 4 3;
}

.scope-dot-1 { fill: #f5e642; }
.scope-dot-2 { fill: #ff49c9; }
.scope-dot-3 { fill: #39d7ff; }

.scope-label-1 { fill: #f5e642; }
.scope-label-2 { fill: #ff49c9; }
.scope-label-3 { fill: #39d7ff; }


.inspector {
  margin: 0;
}

.variables-panel {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 100%;
  overflow: auto;
}

.variables-panel textarea {
  width: 100%;
  min-height: 120px;
  max-height: 160px;
  resize: vertical;
  font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  padding: 8px;
  border: 1px solid #3c3c3c;
  border-radius: 6px;
  background: #fdf7ef;
}

.variables-preview {
  font-size: 12px;
  color: #3c3c3c;
  line-height: 1.4;
  white-space: pre-wrap;
  margin-top: 6px;
}

.inspector h3 {
  margin: 0 0 8px 0;
  font-size: 1rem;
}

.inspector-body {
  font-size: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.inspector .button-row {
  margin-top: 10px;
}

.param {
  display: flex;
  flex-direction: column;
  gap: 4px;
  white-space: nowrap;
  align-self: flex-start;
}

.param > input,
.param > select {
  width: 100%;
}

#errorBox {
  margin: 6px 0 0 0;
  background: #fff3f2;
  border: 1px solid #d86b5f;
  color: #7a1d14;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 0.85rem;
}

@media (max-width: 900px) {
  html, body {
    touch-action: pan-y;
    overflow: hidden;
    overscroll-behavior: none;
    background: #ffffff;
  }

  .topbar {
    display: none;
  }

  .layout {
    display: flex;
    flex-direction: column;
    min-height: 0;
    height: 100svh;
    height: 100dvh;
    height: 100lvh;
    padding: 0;
    gap: 0;
    position: relative;
  }

  .runtime input {
    width: 60px;
  }

  .panel-carousel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    order: 3;
    gap: 12px;
    padding: 8px 10px;
    background: var(--panel);
    border-top: 2px solid #d6c3a4;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x mandatory;
    scroll-padding: 0 10px;
    -webkit-overflow-scrolling: touch;
    height: calc(var(--carousel-height, 64px) + env(safe-area-inset-bottom));
    touch-action: none;
    overscroll-behavior-x: contain;
    scroll-behavior: smooth;
    scroll-padding: 0 10px;
    transition: none;
    z-index: 30;
  }

  .right-column {
    display: contents;
  }

  .panel-carousel.dragging {
    scroll-behavior: auto;
    cursor: grabbing;
  }

  .panel-carousel > * {
    min-width: 85vw;
    max-width: 85vw;
    scroll-snap-align: start;
    scroll-snap-stop: always;
    flex: 0 0 auto;
    height: calc(var(--carousel-height, 64px) - 16px);
    overflow: hidden;
    touch-action: none;
  }

  .panel-carousel .right-column > * {
    min-width: 85vw;
    max-width: 85vw;
    scroll-snap-align: start;
    scroll-snap-stop: always;
    flex: 0 0 auto;
    height: calc(var(--carousel-height, 64px) - 16px);
    overflow: hidden;
    touch-action: none;
  }

  .panel-carousel.collapsed {
    height: calc(var(--carousel-height, 64px) + env(safe-area-inset-bottom));
    padding-top: 6px;
    padding-bottom: 6px;
    transition: none;
  }

  .panel-carousel.collapsed > * {
    height: calc(var(--carousel-height, 64px) - 16px);
    overflow: hidden;
  }

  .panel-carousel.collapsed .right-column > * {
    height: calc(var(--carousel-height, 64px) - 16px);
    overflow: hidden;
  }

  .panel-carousel.collapsed > * > :not(h2):not(h3) {
    display: none;
  }

  .panel-carousel.collapsed .right-column > * > :not(h2):not(h3) {
    display: none;
  }

  .panel-carousel.collapsed h2,
  .panel-carousel.collapsed h3 {
    margin: 0;
    font-size: 1rem;
  }

  .toolbox {
    border: 1px solid #c6b391;
  }

  .panel-carousel .toolbox {
    background: #f7eddf;
    overflow: auto;
    touch-action: pan-y;
  }

  .panel-carousel .variables-panel {
    overflow: auto;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }

  .workspace {
    order: 2;
    flex: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: calc(var(--carousel-height, 64px) + env(safe-area-inset-bottom));
    padding: 8px 10px;
    height: calc(100lvh - var(--carousel-height, 64px) - env(safe-area-inset-bottom));
  }

  #canvas {
    height: 100%;
  }
}
