/* ------------------------------------------------------------------ base */
:root {
  --sans: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', 'PingFang SC',
    'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  --mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
  --r-sm: 6px;
  --r-md: 9px;
  --dur: 140ms;
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

[data-scheme='light'] {
  --page: #ecebe6;
  --chrome: #f8f6f1;
  --panel: #fdfcfa;
  --code-bg: #faf8f4;
  --border: #e2ddd2;
  --border-strong: #d0cabd;
  --fg: #1b1a17;
  --fg-soft: #433f38;
  --muted: #6f6b62;
  --faint: #9a958b;
  --accent: #b4562f;
  --accent-fg: #fff8f3;
  --accent-soft: #f4e4d9;
  --danger: #a8322a;
  --danger-soft: #f6e2df;
  --shadow: 0 14px 34px -16px rgb(27 26 23 / 0.28), 0 2px 8px -3px rgb(27 26 23 / 0.12);
  --tk-keyword: #a24a24;
  --tk-arrow: #326b82;
  --tk-string: #5a7333;
  --tk-number: #7a55a3;
  --tk-node: #1b1a17;
  --tk-punct: #8a8478;
  --tk-comment: #a39d92;
  --grid-dot: rgb(27 26 23 / 0.055);
  --selection: rgb(180 86 47 / 0.18);
}

[data-scheme='dark'] {
  --page: #0c0e11;
  --chrome: #121418;
  --panel: #16181d;
  --code-bg: #131519;
  --border: #252930;
  --border-strong: #333842;
  --fg: #e7e9ee;
  --fg-soft: #c3c8d2;
  --muted: #8e94a2;
  --faint: #626874;
  --accent: #e0865c;
  --accent-fg: #1c1109;
  --accent-soft: #3a2619;
  --danger: #e26a5f;
  --danger-soft: #3a2020;
  --shadow: 0 18px 40px -18px rgb(0 0 0 / 0.75), 0 2px 10px -4px rgb(0 0 0 / 0.6);
  --tk-keyword: #e0865c;
  --tk-arrow: #7fb3c9;
  --tk-string: #a3b878;
  --tk-number: #b39ad8;
  --tk-node: #e7e9ee;
  --tk-punct: #6d7480;
  --tk-comment: #5f6570;
  --grid-dot: rgb(231 233 238 / 0.05);
  --selection: rgb(224 134 92 / 0.24);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--page);
  color: var(--fg);
  font: 14px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

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

:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 65%, transparent);
  outline-offset: 1px;
}

.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.ico {
  width: 14px;
  height: 14px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ------------------------------------------------------------------ shell */
.app {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100dvh;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: 56px;
  padding: 0 14px 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--chrome);
}

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

.mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--panel);
  color: var(--accent);
}

.mark .ico {
  width: 17px;
  height: 17px;
  stroke-width: 1.9;
}

.brand-text h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 620;
  letter-spacing: 0.01em;
}

.brand-text p {
  margin: 1px 0 0;
  font: 11px/1.2 var(--mono);
  color: var(--muted);
}

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

.field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.field-label {
  font-size: 11.5px;
  color: var(--muted);
}

.select {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.select select {
  appearance: none;
  height: 30px;
  max-width: 180px;
  padding: 0 26px 0 9px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--panel);
  color: var(--fg-soft);
  font-size: 12.5px;
  cursor: pointer;
  text-overflow: ellipsis;
}

.select select:hover {
  border-color: var(--muted);
}

.select .ico {
  position: absolute;
  right: 7px;
  pointer-events: none;
  color: var(--muted);
  width: 13px;
  height: 13px;
}

/* ------------------------------------------------------------------ buttons */
.btn,
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--panel);
  color: var(--fg-soft);
  font-size: 12.5px;
  font-weight: 520;
  white-space: nowrap;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease),
    color var(--dur) var(--ease);
}

.btn:hover,
.icon-btn:hover {
  border-color: var(--muted);
  color: var(--fg);
}

.btn:active,
.icon-btn:active {
  background: var(--chrome);
}

.btn[disabled],
.icon-btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.btn-ghost:hover {
  background: var(--panel);
  border-color: var(--border);
  color: var(--fg);
}

.btn-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-fg);
}

.btn-primary:hover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 88%, var(--fg));
  color: var(--accent-fg);
}

.icon-btn {
  width: 30px;
  padding: 0;
}

.ico-trailing {
  width: 12px;
  height: 12px;
  opacity: 0.75;
}

/* ------------------------------------------------------------------ panes */
.panes {
  display: grid;
  /* The editor column is driven by --editor-split (percent) and can be dragged. */
  grid-template-columns: minmax(220px, var(--editor-split, 48%)) 8px minmax(280px, 1fr);
  min-height: 0;
}

.splitter {
  position: relative;
  z-index: 10;
  background: var(--border);
  cursor: col-resize;
  touch-action: none;
  transition: background var(--dur) var(--ease);
}

.splitter::after {
  content: '';
  position: absolute;
  inset: 0 -4px;
}

.splitter:hover,
.splitter:focus-visible,
.splitter[data-dragging='true'] {
  background: var(--accent);
  outline: none;
}

.pane {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
}

.pane-editor {
  background: var(--code-bg);
}

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 48px;
  padding: 0 12px 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--chrome);
}

.pane-title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 0 1 auto;
  white-space: nowrap;
}

.pane-title h2 {
  margin: 0;
  font-size: 12.5px;
  font-weight: 620;
  letter-spacing: 0.04em;
}

.pane-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  flex: 0 0 auto;
  justify-content: flex-end;
}

.pane-head > .pane-title h2 {
  flex: none;
}

/* The buttons matter more than the counters; drop those first. */
@media (max-width: 1180px) {
  .pane-title .meta {
    display: none;
  }
}

/* Below this the toolbar cannot sit beside the title; stack it rather than let
   the labels break in half. */
@media (max-width: 780px) {
  .pane-head {
    height: auto;
    flex-wrap: wrap;
    row-gap: 6px;
    padding-top: 6px;
    padding-bottom: 6px;
  }

  .pane-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .pane-actions .btn {
    flex: 1 1 auto;
  }
}

.badge {
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: color-mix(in srgb, var(--accent) 70%, var(--fg));
  font: 11px/1.6 var(--mono);
  white-space: nowrap;
}

.badge[data-tone='error'] {
  background: var(--danger-soft);
  color: var(--danger);
}

.meta {
  font: 11.5px/1.4 var(--mono);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pane-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: 32px;
  padding: 0 16px;
  border-top: 1px solid var(--border);
  background: var(--chrome);
}

.hint {
  font: 11.5px/1.4 var(--mono);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hint[data-tone='error'] {
  color: var(--danger);
}

/* ------------------------------------------------------------------ editor */
.editor {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

.gutter {
  overflow: hidden;
  padding: 14px 10px 14px 14px;
  text-align: right;
  font: 13px/20px var(--mono);
  color: var(--faint);
  user-select: none;
  scrollbar-width: none;
  border-right: 1px solid var(--border);
  background: color-mix(in srgb, var(--code-bg) 92%, var(--border));
}

.gutter span {
  display: block;
  height: 20px;
}

.code-area {
  position: relative;
  min-width: 0;
  overflow: hidden;
}

.highlight,
#input {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 14px 18px;
  font: 13px/20px var(--mono);
  white-space: pre;
  tab-size: 2;
  border: 0;
}

.highlight {
  overflow: hidden;
  pointer-events: none;
  color: var(--fg-soft);
  scrollbar-width: none;
}

#input {
  overflow: auto;
  resize: none;
  background: transparent;
  color: transparent;
  caret-color: var(--accent);
  outline: none;
  z-index: 1;
}

#input::selection {
  background: var(--selection);
  color: transparent;
}

#input::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

#input::-webkit-scrollbar-thumb {
  border: 3px solid transparent;
  border-radius: 8px;
  background-clip: content-box;
  background-color: var(--border-strong);
}

.tk-keyword {
  color: var(--tk-keyword);
  font-weight: 560;
}

.tk-arrow {
  color: var(--tk-arrow);
}

.tk-string {
  color: var(--tk-string);
}

.tk-number {
  color: var(--tk-number);
}

.tk-node {
  color: var(--tk-node);
  font-weight: 560;
}

.tk-punct {
  color: var(--tk-punct);
}

.tk-comment {
  color: var(--tk-comment);
  font-style: italic;
}

/* ------------------------------------------------------------------ preview */
.preview-canvas {
  position: relative;
  display: block;
  min-height: 0;
  /* No padding: the absolutely positioned host cannot take part in it, so the
     stage origin has to be the canvas's own box. Spacing is added to the pan
     offsets in JS instead. */
  padding: 0;
  /* Panning is done with a transform, so the box itself never scrolls and the
     zoom anchor can always be honoured exactly. */
  overflow: hidden;
  background-color: var(--pele-bg, #fbfaf7);
  background-image: radial-gradient(var(--grid-dot) 1px, transparent 1px);
  background-size: 16px 16px;
  background-position: -1px -1px;
  transition: background-color var(--dur) var(--ease);
}

.diagram-host {
  position: absolute;
  top: 0;
  left: 0;
  /* No will-change: the layer would be rasterised once and then stretched,
     which is what made zoomed diagrams look soft. */
  color: var(--pele-fg, #1b1a17);
  font-family: var(--pele-font);
}

.diagram-host > svg {
  display: block;
  max-width: none !important;
  max-height: none !important;
}

.diagram-host svg {
  display: block;
  margin: 0;
}

.preview-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 4px;
  padding: 24px;
  text-align: center;
  color: var(--pele-muted, #6f6b62);
  pointer-events: none;
}

.preview-placeholder p {
  margin: 0 0 6px;
  font-size: 13px;
}

.preview-placeholder .muted {
  font-size: 11.5px;
  opacity: 0.8;
}

.preview-placeholder[hidden] {
  display: none;
}

/* ------------------------------------------------------------------ menus */
.menu-wrap,
.options-wrap {
  position: relative;
  display: inline-flex;
}

.menu,
.popover {
  position: absolute;
  top: calc(100% + 7px);
  right: 0;
  z-index: 40;
  padding: 6px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: var(--panel);
  box-shadow: var(--shadow);
  transform-origin: top right;
  animation: pop-in 120ms var(--ease);
}

.menu {
  min-width: 214px;
}

.popover {
  width: 268px;
  padding: 10px;
}

.popover[hidden],
.menu[hidden] {
  display: none;
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: translateY(-3px) scale(0.99);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  width: 100%;
  padding: 7px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--fg-soft);
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.menu-item:hover {
  background: var(--chrome);
  color: var(--fg);
}

.kbd {
  font: 11px/1.3 var(--mono);
  color: var(--faint);
  white-space: nowrap;
}

.pop-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 5px 3px;
}

.pop-label {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}

.pop-out {
  min-width: 40px;
  font: 11.5px/1.4 var(--mono);
  color: var(--fg-soft);
  text-align: right;
}

.pop-sep {
  height: 1px;
  margin: 6px -4px;
  background: var(--border);
}

.seg {
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
}

.seg button {
  padding: 4px 9px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 500 12px/1.5 var(--mono);
  cursor: pointer;
}

.seg button + button {
  border-left: 1px solid var(--border);
}

.seg button[aria-pressed='true'] {
  background: var(--accent-soft);
  color: color-mix(in srgb, var(--accent) 72%, var(--fg));
}

input[type='range'] {
  flex: 1;
  height: 18px;
  accent-color: var(--accent);
  background: transparent;
}

.switch {
  appearance: none;
  position: relative;
  width: 34px;
  height: 19px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--chrome);
  cursor: pointer;
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}

.switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--faint);
  transition: transform var(--dur) var(--ease), background var(--dur) var(--ease);
}

.switch:checked {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.switch:checked::after {
  transform: translateX(15px);
  background: var(--accent);
}

/* ------------------------------------------------------------------ toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  z-index: 80;
  max-width: min(90vw, 460px);
  padding: 9px 15px;
  border-radius: 999px;
  background: var(--fg);
  color: var(--page);
  font-size: 12.5px;
  font-weight: 500;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateX(-50%) translateY(10px);
  transition: opacity 160ms var(--ease), transform 160ms var(--ease);
  pointer-events: none;
}

.toast[data-show='true'] {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast[data-kind='error'] {
  background: var(--danger);
  color: #fff;
}

/* ------------------------------------------------------------------ tabs */
.tabs {
  display: none;
  gap: 4px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--chrome);
}

.tab {
  padding: 5px 12px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 12.5px;
  cursor: pointer;
}

.tab[aria-pressed='true'] {
  border-color: var(--border-strong);
  background: var(--panel);
  color: var(--fg);
}

/* ------------------------------------------------------------------ responsive */
@media (max-width: 940px) {
  .btn-label {
    display: none;
  }

  #docsBtn {
    width: 30px;
    padding: 0;
  }

  .app {
    grid-template-rows: auto auto minmax(0, 1fr);
  }

  .topbar {
    height: auto;
    flex-wrap: wrap;
    padding: 9px 12px;
    gap: 8px;
  }

  .topbar-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .tabs {
    display: flex;
  }

  .panes {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Stacked layout: the splitter has nothing to divide. */
  .splitter {
    display: none;
  }

  .app[data-view='editor'] .pane-preview {
    display: none;
  }

  .app[data-view='preview'] .pane-editor {
    display: none;
  }

  .pane-head {
    height: auto;
    min-height: 44px;
    flex-wrap: wrap;
    padding: 7px 12px;
  }

  .preview-canvas {
    padding: 16px;
  }

  .select select {
    max-width: 138px;
  }

  .btn,
  .icon-btn {
    height: 30px;
  }
}

@media (max-width: 560px) {
  .field-label {
    display: none;
  }

  .pane-actions .btn {
    padding: 0 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 1ms !important;
    transition-duration: 1ms !important;
  }
}

/* ------------------------------------------------------------------ embed */
[data-embed='true'] body {
  background: transparent;
}

[data-embed='true'] .app {
  grid-template-rows: minmax(0, 1fr);
}

[data-embed='true'] .topbar,
[data-embed='true'] .tabs,
[data-embed='true'] .pane-head,
[data-embed='true'] .pane-foot,
[data-embed='true'] .pane-editor {
  display: none;
}

[data-embed='true'] .panes {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);
}

[data-embed='true'] .preview-canvas {
  --stage-inset: 12px;
}

.pop-keys {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 4px 2px;
  font: 10.5px/1.4 var(--mono);
  color: var(--faint);
}

.diagram-host .pele-zoom {
  opacity: 0.55;
  transition: opacity var(--dur) var(--ease);
}

.diagram-host .pele-zoom:hover,
.diagram-host .pele-zoom:focus-within {
  opacity: 1;
}

.pop-keys {
  overflow: hidden;
  padding-right: 6px;
}

/* ------------------------------------------------------------------ zoom */
.pane-preview {
  position: relative;
}

.preview-canvas[data-zoomable='true'] {
  cursor: grab;
}

.preview-canvas[data-panning='true'] {
  cursor: grabbing;
  user-select: none;
}

.diagram-host {
  /* No transition: zoom is applied as one discrete transform, and anchoring
     reads the live rect, so an in-flight animation would skew the correction
     (and make the zoom feel laggy). */
  transition: none;
}

.preview-zoom {
  position: absolute;
  right: 12px;
  /* clear the 32px status line at the bottom of the pane */
  bottom: 44px;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(6px);
  box-shadow: 0 4px 14px -8px rgb(0 0 0 / 0.35);
  opacity: 0.75;
  transition: opacity var(--dur) var(--ease);
}

.preview-zoom:hover,
.preview-zoom:focus-within {
  opacity: 1;
}

.zoom-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 26px;
  padding: 0 5px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--fg-soft);
  font: 500 11.5px/1 var(--mono);
  cursor: pointer;
}

.zoom-btn:hover {
  background: var(--chrome);
  color: var(--fg);
}

.zoom-level {
  min-width: 48px;
}

[data-embed='true'] .preview-zoom {
  display: none;
}

/* pele's own zoom controls are not used: the app renders at natural size and
   scales the container, so the diagram never gets clipped. */
.diagram-host .pele-zoom {
  display: none !important;
}
