:root {
  --accent: #f0f4ff;
  --bg: #f6f7fb;
  --border: #cfd5e4;
  --canvas-bg: #ffffff;
  --grid-major: #dfe4ee;
  --grid-minor: #eff2f7;
  --handle-fill: #ffffff;
  --muted: #6b7280;
  --primary: #4a6cf7;
  --select: #4a6cf7;
  --select-fill: rgba(74, 108, 247, 0.08);
  --surface: #ffffff;
  --text: #1f2630;

  /* Vaporwave Palette */
  --vw-orange: #FF5E00;
  --vw-yellow: #FFE600;
  --vw-fuchsia: #FF00FF;
  --vw-blue: #00FFFF;
  --vw-purple: #8A2BE2;
  --vw-green: #39FF14;
  --vw-red: #FF2D55;
}

/*
 * Diagram colour tokens. Shapes store token names (ink, paper, vw-blue,
 * tint-blue, ...) so a diagram follows the theme; exports resolve them through
 * the hidden .fd-theme-light / .fd-theme-dark probes.
 */
:root,
.fd-theme-light {
  --fd-ink: #1f2630;
  --fd-muted: #6b7280;
  --fd-paper: #ffffff;
  --fd-tint-blue: rgba(10, 126, 164, 0.14);
  --fd-tint-fuchsia: rgba(196, 0, 196, 0.12);
  --fd-tint-green: rgba(18, 145, 58, 0.14);
  --fd-tint-orange: rgba(211, 84, 0, 0.14);
  --fd-tint-purple: rgba(138, 43, 226, 0.13);
  --fd-tint-red: rgba(224, 36, 74, 0.13);
  --fd-tint-yellow: rgba(255, 214, 0, 0.28);
  --fd-vw-blue: #0a7ea4;
  --fd-vw-fuchsia: #c400c4;
  --fd-vw-green: #12913a;
  --fd-vw-orange: #e06000;
  --fd-vw-purple: #8a2be2;
  --fd-vw-red: #e0244a;
  --fd-vw-yellow: #b08d00;
}

body.dark,
.fd-theme-dark {
  --fd-ink: #e5e7eb;
  --fd-muted: #9ca3af;
  --fd-paper: #0b1221;
  --fd-tint-blue: rgba(0, 255, 255, 0.12);
  --fd-tint-fuchsia: rgba(255, 0, 255, 0.12);
  --fd-tint-green: rgba(57, 255, 20, 0.1);
  --fd-tint-orange: rgba(255, 94, 0, 0.14);
  --fd-tint-purple: rgba(183, 124, 255, 0.14);
  --fd-tint-red: rgba(255, 45, 85, 0.14);
  --fd-tint-yellow: rgba(255, 230, 0, 0.13);
  --fd-vw-blue: var(--vw-blue);
  --fd-vw-fuchsia: var(--vw-fuchsia);
  --fd-vw-green: var(--vw-green);
  --fd-vw-orange: var(--vw-orange);
  --fd-vw-purple: #b77cff;
  --fd-vw-red: var(--vw-red);
  --fd-vw-yellow: var(--vw-yellow);
}

body.dark {
  --accent: #1f2937;
  --bg: #111827;
  --border: #374151;
  --canvas-bg: #0b1221;
  --grid-major: #1d2840;
  --grid-minor: #131c2e;
  --handle-fill: #0b1221;
  --primary: #8b9cf9;
  --select: #8b9cf9;
  --select-fill: rgba(139, 156, 249, 0.1);
  --surface: #0f172a;
  --text: #ffffff;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ---------------------------------------------------------------- Header */
.fd-header {
  margin-bottom: 0;
  padding: 0.75rem 0;
}

.fd-header-inner {
  align-items: center;
  display: flex;
  justify-content: space-between;
  padding: 0 1rem;
}

.fd-header h1 {
  font-size: 1.35rem;
  white-space: nowrap;
}

.header-left {
  align-items: center;
  display: flex;
  gap: 1.5rem;
  min-width: 0;
}

.back-link {
  align-items: center;
  color: var(--muted);
  display: flex;
  font-size: 0.875rem;
  font-weight: 600;
  gap: 0.5rem;
  text-decoration: none;
  transition: color 0.2s;
}

.back-link:hover {
  color: var(--primary);
}

.doc-name {
  background: none;
  border: 1px solid transparent;
  border-radius: 0.4rem;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  min-width: 0;
  padding: 0.25rem 0.4rem;
  width: 16rem;
}

.doc-name:hover,
.doc-name:focus {
  border-color: var(--border);
  color: var(--text);
  outline: none;
}

.header-actions {
  align-items: center;
  display: flex;
  flex-direction: row;
  gap: 0.5rem;
}

/* --------------------------------------------------------------- Toolbar */
.fd-toolbar {
  align-items: center;
  background-color: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  padding: 0.5rem 1rem;
}

.tool-group {
  align-items: center;
  display: flex;
  gap: 0.35rem;
}

.tool-btn {
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.35rem 0.7rem;
  transition: border-color 0.15s, background-color 0.15s;
  white-space: nowrap;
}

.tool-btn:hover:not(:disabled) {
  border-color: var(--primary);
}

.tool-btn:disabled {
  cursor: default;
  opacity: 0.45;
}

.tool-btn.primary-tool {
  background-color: var(--primary);
  border-color: var(--primary);
  color: #ffffff;
}

.tool-btn[aria-pressed="true"] {
  background-color: var(--accent);
  border-color: var(--primary);
}

.icon-tool {
  font-size: 1rem;
  line-height: 1;
  min-width: 2rem;
}

.tool-label {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
}

.tool-check {
  align-items: center;
  color: var(--text);
  cursor: pointer;
  display: flex;
  font-size: 0.8rem;
  font-weight: 600;
  gap: 0.3rem;
}

.tool-select {
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  color: var(--text);
  font-family: inherit;
  font-size: 0.8rem;
  padding: 0.3rem 0.4rem;
}

.menu-wrap {
  position: relative;
}

.popup-menu {
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  min-width: 14rem;
  padding: 0.3rem;
  position: absolute;
  z-index: 50;
}

.popup-menu.hidden {
  display: none;
}

.popup-menu button {
  background: none;
  border: none;
  border-radius: 0.35rem;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.82rem;
  padding: 0.45rem 0.6rem;
  text-align: left;
}

.popup-menu button:hover:not(:disabled) {
  background-color: var(--accent);
}

.popup-menu button:disabled {
  cursor: default;
  opacity: 0.45;
}

.popup-menu .menu-sub {
  color: var(--muted);
  display: block;
  font-size: 0.72rem;
}

.popup-menu hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.25rem 0;
}

.context-menu {
  position: fixed;
}

/* ------------------------------------------------------------- Workspace */
.fd-workspace {
  display: flex;
  flex: 1;
  min-height: 0;
}

/* --------------------------------------------------------------- Palette */
.fd-palette {
  background-color: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  width: 236px;
}

.palette-head {
  border-bottom: 1px solid var(--border);
  padding: 0.5rem;
}

.filter-input,
.text-input {
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  box-sizing: border-box;
  color: var(--text);
  font-family: inherit;
  font-size: 0.82rem;
  padding: 0.4rem 0.5rem;
  width: 100%;
}

.palette-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.25rem 0.5rem 0.75rem;
}

.palette-cat {
  margin-top: 0.4rem;
}

.palette-cat-head {
  align-items: center;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 700;
  gap: 0.35rem;
  letter-spacing: 0.04em;
  padding: 0.35rem 0.1rem;
  text-transform: uppercase;
  width: 100%;
}

.palette-cat-head:hover {
  color: var(--text);
}

.palette-caret {
  display: inline-block;
  transition: transform 0.15s;
  width: 0.8rem;
}

.palette-cat.collapsed .palette-caret {
  transform: rotate(-90deg);
}

.palette-cat.collapsed .palette-grid {
  display: none;
}

.palette-grid {
  display: grid;
  gap: 0.35rem;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.palette-tile {
  align-items: center;
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  color: var(--text);
  cursor: grab;
  display: flex;
  flex-direction: column;
  font-family: inherit;
  font-size: 0.62rem;
  gap: 0.15rem;
  padding: 0.3rem 0.15rem 0.25rem;
  transition: border-color 0.15s;
  user-select: none;
}

.palette-tile:hover {
  border-color: var(--primary);
}

.palette-tile svg {
  height: 36px;
  max-width: 56px;
  overflow: visible;
  width: 100%;
}

.palette-tile span {
  line-height: 1.15;
  max-width: 100%;
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.palette-foot {
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.72rem;
  padding: 0.5rem 0.75rem;
}

/* ---------------------------------------------------------------- Stage */
.fd-stage {
  background-color: var(--canvas-bg);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  position: relative;
}

.fd-canvas {
  cursor: default;
  display: block;
  height: 100%;
  outline: none;
  touch-action: none;
  user-select: none;
  width: 100%;
}

.fd-canvas.panning {
  cursor: grabbing;
}

.grid-line {
  fill: none;
  stroke: var(--grid-minor);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.grid-line-major {
  fill: none;
  stroke: var(--grid-major);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.grid-rect.hidden {
  display: none;
}

.fd-node {
  cursor: move;
}

.fd-hit {
  fill: none;
  pointer-events: stroke;
  stroke: transparent;
}

.fd-hit-body {
  fill: transparent;
  stroke: none;
}

.fd-edge {
  cursor: pointer;
}

.fd-edge .fd-hit.seg-h {
  cursor: ns-resize;
}

.fd-edge .fd-hit.seg-v {
  cursor: ew-resize;
}

.fd-group-hit {
  cursor: move;
}

.fd-label {
  cursor: move;
}

.sel-box {
  fill: none;
  pointer-events: none;
  stroke: var(--select);
  stroke-dasharray: 4 3;
}

.sel-group {
  fill: var(--select-fill);
  pointer-events: none;
  stroke: var(--select);
  stroke-dasharray: 6 4;
}

.sel-edge {
  fill: none;
  opacity: 0.35;
  pointer-events: none;
  stroke: var(--select);
  stroke-linecap: round;
  stroke-linejoin: round;
}

.handle {
  fill: var(--handle-fill);
  stroke: var(--select);
}

.handle-nw,
.handle-se {
  cursor: nwse-resize;
}

.handle-ne,
.handle-sw {
  cursor: nesw-resize;
}

.handle-n,
.handle-s {
  cursor: ns-resize;
}

.handle-e,
.handle-w {
  cursor: ew-resize;
}

.handle-rot {
  cursor: grab;
  fill: var(--select);
}

.handle-move {
  cursor: move;
}

.handle-seg {
  cursor: move;
  fill: var(--select);
}

.handle-curve {
  cursor: move;
  fill: var(--vw-fuchsia);
  stroke: var(--handle-fill);
}

.handle-line {
  pointer-events: none;
  stroke: var(--select);
}

.port-dot {
  cursor: crosshair;
  fill: var(--handle-fill);
  stroke: var(--vw-fuchsia);
}

.port-dot:hover,
.port-dot.hot {
  fill: var(--vw-fuchsia);
}

.marquee {
  fill: var(--select-fill);
  pointer-events: none;
  stroke: var(--select);
  stroke-dasharray: 4 3;
}

.draw-preview {
  fill: none;
  pointer-events: none;
  stroke: var(--vw-fuchsia);
  stroke-dasharray: 6 4;
}

.snap-target {
  fill: none;
  pointer-events: none;
  stroke: var(--vw-fuchsia);
}

.inline-editor {
  background-color: var(--surface);
  border: 1px solid var(--primary);
  border-radius: 0.3rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  color: var(--text);
  font-family: Inter, Arial, sans-serif;
  font-size: 14px;
  min-width: 6rem;
  outline: none;
  padding: 0.2rem 0.35rem;
  position: absolute;
  resize: none;
  text-align: center;
  z-index: 20;
}

.inline-editor.hidden {
  display: none;
}

.empty-hint {
  color: var(--muted);
  left: 50%;
  max-width: 26rem;
  pointer-events: none;
  position: absolute;
  text-align: center;
  top: 40%;
  transform: translate(-50%, -50%);
}

.empty-hint.hidden {
  display: none;
}

.empty-hint h2 {
  color: var(--text);
  font-size: 1.2rem;
  margin: 0 0 0.4rem;
}

.empty-hint p {
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0;
}

.zoom-badge {
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.35rem;
  bottom: 0.6rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.15rem 0.45rem;
  pointer-events: none;
  position: absolute;
  right: 0.6rem;
}

/* ------------------------------------------------------------- Inspector */
.fd-inspector {
  background-color: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  width: 280px;
}

.props-panel {
  flex: 1;
  overflow-y: auto;
  padding: 0.25rem 0.75rem 1rem;
}

.panel-title {
  align-items: center;
  color: var(--muted);
  display: flex;
  font-size: 0.72rem;
  font-weight: 700;
  justify-content: space-between;
  letter-spacing: 0.04em;
  padding: 0.6rem 0 0.35rem;
  text-transform: uppercase;
}

.props-section {
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.6rem;
}

.props-section:last-child {
  border-bottom: none;
}

.prop-row {
  align-items: center;
  display: grid;
  font-size: 0.78rem;
  gap: 0.5rem;
  grid-template-columns: 5.2rem 1fr;
  margin-top: 0.4rem;
}

.prop-row > label,
.prop-row > span:first-child {
  color: var(--muted);
  font-weight: 600;
}

.prop-row input[type="text"],
.prop-row input[type="number"],
.prop-row select,
.prop-row textarea {
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.35rem;
  box-sizing: border-box;
  color: var(--text);
  font-family: inherit;
  font-size: 0.78rem;
  min-width: 0;
  padding: 0.28rem 0.4rem;
  width: 100%;
}

.prop-row textarea {
  min-height: 2.6rem;
  resize: vertical;
}

.prop-pair {
  display: grid;
  gap: 0.35rem;
  grid-template-columns: 1fr 1fr;
}

.prop-hint {
  color: var(--muted);
  font-size: 0.7rem;
  line-height: 1.4;
  margin: 0.35rem 0 0;
}

.prop-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.6rem;
}

.swatches {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.swatch {
  border: 1px solid var(--border);
  border-radius: 50%;
  cursor: pointer;
  height: 1.15rem;
  padding: 0;
  width: 1.15rem;
}

.swatch.active {
  box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--primary);
}

.swatch-none {
  background: linear-gradient(135deg, transparent 45%, var(--vw-red) 45%, var(--vw-red) 55%, transparent 55%);
}

.swatch-custom {
  background-color: transparent;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  cursor: pointer;
  height: 1.3rem;
  padding: 0;
  width: 1.8rem;
}

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0;
  text-transform: none;
}

.edge-label-row {
  align-items: center;
  display: grid;
  gap: 0.3rem;
  grid-template-columns: 1fr 3.4rem auto;
  margin-top: 0.35rem;
}

.edge-label-row input {
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.35rem;
  color: var(--text);
  font-family: inherit;
  font-size: 0.78rem;
  min-width: 0;
  padding: 0.28rem 0.4rem;
}

/* --------------------------------------------------------------- History */
.history-panel {
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  max-height: 40%;
  padding: 0 0.75rem 0.5rem;
}

.history-panel.hidden {
  display: none;
}

.history-list {
  font-size: 0.78rem;
  list-style: none;
  margin: 0;
  overflow-y: auto;
  padding: 0;
}

.history-list li {
  border-radius: 0.3rem;
  cursor: pointer;
  padding: 0.25rem 0.4rem;
}

.history-list li:hover {
  background-color: var(--accent);
}

.history-list li.current {
  background-color: var(--select-fill);
  color: var(--primary);
  font-weight: 600;
}

.history-list li.future {
  color: var(--muted);
  font-style: italic;
}

/* --------------------------------------------------------------- Dialogs */
.dialog {
  background-color: var(--surface);
  color: var(--text);
}

.dialog-wide {
  max-height: 85vh;
  max-width: 640px;
  overflow-y: auto;
}

.dialog-text {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  margin: -0.75rem 0 1.25rem;
}

.dialog-actions-split {
  gap: 0.5rem;
}

.form-grid {
  align-items: center;
  display: grid;
  font-size: 0.85rem;
  gap: 0.6rem 1rem;
  grid-template-columns: auto 1fr;
  margin-bottom: 1.5rem;
}

.help-table {
  border-collapse: collapse;
  font-size: 0.85rem;
  margin-bottom: 1.25rem;
  width: 100%;
}

.help-table th,
.help-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.4rem 0.5rem;
  text-align: left;
  vertical-align: top;
}

.help-table th {
  font-weight: 600;
  white-space: nowrap;
}

.help-table td {
  color: var(--muted);
}

kbd {
  background-color: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  font-family: inherit;
  font-size: 0.75rem;
  padding: 0 0.3rem;
}

.toast {
  background-color: var(--text);
  border-radius: 0.5rem;
  bottom: 1.5rem;
  color: var(--surface);
  font-size: 0.85rem;
  left: 50%;
  padding: 0.55rem 1rem;
  position: fixed;
  transform: translateX(-50%);
  z-index: 200;
}

.toast.hidden {
  display: none;
}

.theme-probe {
  display: none;
}
