/*
 * JAGS Model Diagram Designer — styles.
 *
 * Layout: a drafting-table tool — code on the left, white paper canvas in the middle, inspector
 * on the right; stacks on narrow screens.
 *
 * Theming: light tokens are the default. Dark tokens apply when the OS prefers dark (unless the
 * user forced light via data-theme="light"), or when data-theme="dark" is set (see js/theme.js).
 * The paper and everything drawn on it keep light colours in both themes, to match the export.
 */
:root {
  color-scheme: light;
  --bg: #eaeef2;
  --panel: #f8fafb;
  --raised: #ffffff;
  --fg: #15202b;
  --muted: #5a6876;
  --line: #d3dbe2;
  --accent: #1b6a89;
  --accent-fg: #ffffff;
  --accent-soft: #dbebf2;
  --warn: #9a5b12;
  --warn-soft: #f7ecdc;
  --danger: #a83a28;
  --paper: #ffffff;
  --ink: #111111;
  --dot: #dfe5ea;
  /* UI drawn on top of the (always white) paper */
  --paper-accent: #1b6a89;
  --paper-muted: #5a6876;
  --paper-veil: rgba(255, 255, 255, 0.85);
  --toast-bg: #15202b;
  --ui: 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;
  --display: 'STIX Two Text', 'Times New Roman', Times, serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #0f141a;
    --panel: #151c23;
    --raised: #1b232c;
    --fg: #e2e8ee;
    --muted: #93a2b0;
    --line: #28323c;
    --accent: #6bbfdf;
    --accent-fg: #0b1a22;
    --accent-soft: #18323e;
    --warn: #e4b26a;
    --warn-soft: #2e2414;
    --danger: #e8806c;
    --dot: #e3e8ec;
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #0f141a;
  --panel: #151c23;
  --raised: #1b232c;
  --fg: #e2e8ee;
  --muted: #93a2b0;
  --line: #28323c;
  --accent: #6bbfdf;
  --accent-fg: #0b1a22;
  --accent-soft: #18323e;
  --warn: #e4b26a;
  --warn-soft: #2e2414;
  --danger: #e8806c;
  --dot: #e3e8ec;
  color-scheme: dark;
}

html,
body {
  height: 100%;
}
body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--ui);
  font-size: 14px;
  line-height: 1.45;
}
* {
  box-sizing: border-box;
}
button,
select,
input,
textarea {
  font: inherit;
  color: inherit;
}
[hidden] {
  display: none !important;
}
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app {
  height: 100%;
  display: grid;
  /* --code-w is set by dragging the code pane's edge */
  grid-template-columns: var(--code-w, minmax(260px, 330px)) minmax(0, 1fr) minmax(240px, 300px);
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 12px;
  padding-block: 12px;
  padding-inline: 16px;
}
.credits {
  grid-column: 1/-1;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
  /* credits in the middle, "Made by" at the right */
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: baseline;
}
.credits-text {
  grid-column: 2;
}
.made-by {
  grid-column: 3;
  justify-self: end;
  white-space: nowrap;
}
.credits a {
  color: inherit;
  text-decoration-color: var(--line);
  text-underline-offset: 2px;
}
.credits a:hover {
  color: var(--accent);
  text-decoration-color: currentColor;
}

.bar {
  grid-column: 1/-1;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-right: auto;
}
.brand svg {
  flex: none;
}
.brand h1 {
  margin: 0;
  font-family: var(--display);
  font-style: italic;
  font-weight: 500;
  font-size: 22px;
  letter-spacing: 0.01em;
  text-wrap: balance;
}
.brand small {
  color: var(--muted);
  font-size: 12px;
}
.tools {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
}
.sep {
  width: 1px;
  height: 22px;
  background: var(--line);
  margin-inline: 4px;
}

.btn {
  border: 1px solid var(--line);
  background: var(--raised);
  border-radius: 7px;
  padding: 6px 11px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1.2;
  white-space: nowrap;
}
.btn:hover {
  border-color: var(--accent);
}
.btn:disabled {
  opacity: 0.45;
  cursor: default;
  border-color: var(--line);
}
.btn.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
  font-weight: 500;
}
.btn.danger {
  color: var(--danger);
}
.btn.sm {
  padding: 4px 8px;
  font-size: 12px;
}

/* toolbar dropdown menus (see js/menus.js) */
.menu {
  position: relative;
}
.menu > .btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.menu > .btn .chev {
  transition: transform 0.15s;
}
.menu > .btn[aria-expanded='true'] .chev {
  transform: rotate(180deg);
}
.menu-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 20;
  min-width: 160px;
  display: flex;
  flex-direction: column;
  padding: 4px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.14);
}
.menu-list[hidden] {
  display: none;
}
/* a menu inside the scrolling inspector: the list opens in place instead of floating */
.menu.inline .menu-list {
  position: static;
  margin-top: 6px;
  box-shadow: none;
  max-height: 320px;
  overflow: auto;
}
.menu.inline [role='menuitem'] {
  white-space: normal;
}
.menu.end .menu-list {
  left: auto;
  right: 0;
}
.menu-list [role='menuitem'] {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border: 0;
  background: none;
  text-align: left;
  padding: 6px 10px;
  border-radius: 5px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.menu-list [role='menuitem']:hover,
.menu-list [role='menuitem']:focus-visible {
  background: var(--accent-soft);
  outline: none;
}
.menu-list .note {
  color: var(--muted);
  font-size: 12px;
}

/* File menu: a dot while the workspace has unsaved changes */
.dirty-dot {
  display: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
}
#btnFile.dirty .dirty-dot {
  display: inline-block;
}

/* dropping a workspace file on the page */
body.dropping .stage {
  outline: 2px dashed var(--accent);
  outline-offset: -6px;
}

/* "Unsaved changes" dialog */
.dialog {
  max-width: min(420px, calc(100vw - 32px));
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--raised);
  color: var(--fg);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.dialog::backdrop {
  background: rgba(0, 0, 0, 0.35);
}
.dialog h2 {
  margin: 0 0 6px;
  font-size: 16px;
  font-weight: 600;
}
.dialog p {
  margin: 0 0 16px;
  color: var(--muted);
}
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

/* icon-only buttons (undo / redo), sized to match the text buttons */
.btn.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 9px;
}
.btn.icon svg {
  display: block;
}

/* theme toggle: shows the icon for the current mode */
.btn.theme {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn.theme svg {
  display: none;
  flex: none;
}
.btn.theme[data-mode='system'] .i-system,
.btn.theme[data-mode='light'] .i-light,
.btn.theme[data-mode='dark'] .i-dark {
  display: block;
}

.pane {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.pane h2 {
  margin: 0;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--muted);
  font-weight: 600;
}
.code-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  flex: 1;
  min-height: 0;
}
/* the code editor: a line-number gutter beside the text area, inside one frame */
.editor {
  flex: 1;
  min-height: 240px;
  display: flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--raised);
  overflow: hidden;
}
.editor:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}
.gutter {
  flex: none;
  overflow: hidden;
  padding: 10px 6px 40px 8px;
  border-right: 1px solid var(--line);
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--muted);
  opacity: 0.75;
  text-align: right;
  font-variant-numeric: tabular-nums;
  user-select: none;
  -webkit-user-select: none;
}
.gutter div.err {
  color: var(--danger);
  opacity: 1;
  font-weight: 600;
}
#code {
  flex: 1;
  min-width: 0;
  width: 100%;
  resize: none;
  border: 0;
  background: transparent;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.55;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}
#code:focus-visible {
  outline: none;
}
#code.wrap {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#btnWrap[aria-pressed='true'] {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.pane.code .pane-head select {
  margin-left: auto;
  flex: 0 1 auto;
  min-width: 0;
}
.pane.code .pane-head .btn,
.pane.code .pane-head h2 {
  flex: none;
  white-space: nowrap;
}
select {
  border: 1px solid var(--line);
  background: var(--raised);
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 12.5px;
  max-width: 100%;
}
#msgs {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
  max-height: 30%;
  overflow: auto;
}
#msgs li {
  padding: 7px 9px;
  border-radius: 7px;
  background: var(--accent-soft);
}
#msgs li.warn {
  background: var(--warn-soft);
  color: var(--warn);
}
#msgs li.err {
  background: var(--warn-soft);
  color: var(--danger);
  font-weight: 500;
}
.msg-link {
  all: unset;
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.msg-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.sync-note {
  margin: 0;
  padding: 6px 9px;
  border-radius: 7px;
  font-size: 12px;
  background: var(--warn-soft);
  color: var(--warn);
}
.btn.attention {
  box-shadow: 0 0 0 3px var(--warn-soft);
}

/* drag handle on the code pane's right edge, sitting in the gap between the panes */
.pane.code {
  position: relative;
}
.splitter {
  position: absolute;
  top: 0;
  bottom: 0;
  right: -12px;
  width: 12px;
  cursor: col-resize;
  touch-action: none;
}
.splitter::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 5px;
  width: 2px;
  border-radius: 1px;
  background: var(--muted);
  opacity: 0.25;
  transition: opacity 0.15s;
}
.splitter:hover::after,
.splitter:focus-visible::after,
body.resizing .splitter::after {
  background: var(--accent);
  opacity: 0.7;
}
.splitter:focus-visible {
  outline: none;
}
body.resizing {
  cursor: col-resize;
  user-select: none;
  -webkit-user-select: none;
}

.stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  overflow: hidden;
  background-color: var(--paper);
  background-image: radial-gradient(var(--dot) 1px, transparent 1.2px);
  background-size: 22px 22px;
}
#canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}
#canvas.panning {
  cursor: grabbing;
}
#canvas .node {
  cursor: move;
}
#canvas .edge,
#canvas .edge-hit {
  cursor: pointer;
}
#canvas .selbox {
  fill: none;
  stroke: var(--paper-accent);
  stroke-width: 1.2;
  stroke-dasharray: 5 4;
}
#canvas .edge.selected line:not(.hit),
#canvas .edge.hover line:not(.hit) {
  stroke: var(--paper-accent);
}
#canvas .edge.selected polygon,
#canvas .edge.hover polygon {
  fill: var(--paper-accent);
}
#canvas .edge.hover:not(.selected) line:not(.hit) {
  stroke-width: 2.2;
}
.hint {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 11.5px;
  color: var(--paper-muted);
  background: var(--paper-veil);
  padding: 3px 7px;
  border-radius: 6px;
  pointer-events: none;
}
.zoom {
  position: absolute;
  right: 10px;
  bottom: 8px;
  font-size: 11.5px;
  color: var(--paper-muted);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
#loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--paper-muted);
  font-size: 13px;
  background: rgba(255, 255, 255, 0.7);
}
.toast {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: #fff;
  font-size: 12.5px;
  padding: 6px 12px;
  border-radius: 7px;
  pointer-events: none;
}

.insp {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: auto;
  flex: 1;
  min-height: 0;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.field > label,
.lbl {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  font-weight: 600;
}
.field textarea {
  width: 100%;
  min-height: 74px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--raised);
  padding: 7px 9px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
}
/* live preview under a LaTeX box */
.tex-preview {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 10px;
  border: 1px dashed var(--line);
  border-radius: 7px;
}
.tex-preview .math {
  line-height: 2;
}
.tex-preview .math svg {
  max-width: 100%;
}
.tex-preview [class*='anc-'] {
  color: var(--accent);
  fill: var(--accent);
}
.tex-preview .help,
.tex-preview .tex-error {
  margin: 0;
  font-size: 12px;
}
.tex-error {
  color: var(--danger);
}
.field input[type='text'] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--raised);
  padding: 5px 8px;
  font-size: 13px;
}
.row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.range {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  align-items: center;
}
.range .reset {
  border: 1px solid var(--line);
  background: var(--raised);
  border-radius: 6px;
  width: 24px;
  height: 22px;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}
.range .reset:hover:not(:disabled) {
  border-color: var(--accent);
}
.range .reset:disabled {
  opacity: 0.35;
  cursor: default;
}
.range input {
  width: 100%;
  accent-color: var(--accent);
}
.range output {
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  color: var(--muted);
  min-width: 3.2em;
  text-align: right;
}
.seg {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
}
.seg button {
  border: 0;
  background: var(--raised);
  padding: 5px 14px;
  cursor: pointer;
  font-family: var(--display);
  font-size: 16px;
}
.seg button[aria-pressed='true'] {
  background: var(--accent);
  color: var(--accent-fg);
}
/* a node shown as it looks in the diagram (arrow inspector From / To) */
.node-ref {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 10px;
  min-width: 0;
}
.node-ref .math {
  flex: 1 1 auto;
  min-width: 0;
  line-height: 1.9;
}
.node-ref .math svg {
  max-width: 100%; /* only matters for a single piece too wide to wrap */
}
.node-ref .dist-name {
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 8px;
}

/* "Points at" symbol buttons in the arrow inspector */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  min-height: 32px;
  padding: 3px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--raised);
  font-size: 12.5px;
  cursor: pointer;
}
.chip:hover {
  border-color: var(--accent);
}
.chip[aria-pressed='true'] {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px var(--accent);
}
.chip svg {
  display: block;
}
.kind {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}
.info {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--muted);
  word-break: break-word;
}
.help {
  font-size: 12.5px;
  color: var(--muted);
  margin: 0;
}
details.help summary {
  cursor: pointer;
  color: var(--accent);
}
details.help p {
  margin: 6px 0 0;
}
.help code {
  font-family: var(--mono);
  font-size: 11.5px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  color: var(--fg);
}
.arrows {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.arrows li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--raised);
  cursor: pointer;
}
.arrows li:hover {
  border-color: var(--accent);
}
.arrows li .node-ref {
  flex: 1;
}
.arrows .target {
  flex: none;
  display: inline-flex;
  padding: 1px 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
}
.arrows .sym {
  font-family: var(--display);
  font-size: 16px;
  width: 1.2em;
  text-align: center;
}
.divider {
  height: 1px;
  background: var(--line);
}

@media (max-width: 980px) {
  .app {
    height: auto;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
  }
  .stage {
    height: 62svh;
    min-height: 380px;
  }
  .pane.code {
    order: 3;
  }
  .pane.inspector {
    order: 4;
  }
  .credits {
    order: 5;
  }
  .editor {
    min-height: 300px;
  }
  .splitter {
    display: none;
  }
  .credits {
    grid-template-columns: minmax(0, 1fr);
  }
  .credits-text,
  .made-by {
    grid-column: 1;
    justify-self: center;
  }
}
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
