:root {
  --bg: #eef3f7;
  --panel: #ffffff;
  --text: #1f2937;
  --muted: #64748b;
  --border: #dce4ec;
  --border-soft: #e8eef4;
  --primary: #1769aa;
  --primary-dark: #0f4f86;
  --danger: #b42318;
  --danger-bg: #fff1f0;
  --field-bg: #f8fafc;
  --ok-bg: #eef8f1;
  --warn-bg: #fff7e6;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  font-size: 14px;
}

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

button {
  min-height: 30px;
  padding: 0 10px;
  border: 1px solid #bac8d6;
  border-radius: 4px;
  color: #213547;
  background: #fff;
  cursor: pointer;
}

button:hover {
  border-color: var(--primary);
  color: var(--primary-dark);
}

.primary-button {
  color: #fff;
  background: var(--primary);
  border-color: var(--primary);
}

.primary-button:hover {
  color: #fff;
  background: var(--primary-dark);
}

.app-header {
  display: flex;
  height: 58px;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 18px;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.app-header h1 {
  margin: 0 0 2px;
  font-size: 19px;
  font-weight: 700;
}

.app-header p,
.panel-header p {
  margin: 0;
  color: var(--muted);
}

.header-actions,
.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(340px, 0.52fr) minmax(680px, 1fr);
  gap: 10px;
  height: calc(100vh - 58px);
  padding: 10px;
  overflow: hidden;
}

.preview-stack {
  display: grid;
  min-height: 0;
  grid-template-rows: 280px minmax(0, 1fr);
  gap: 10px;
}

body.form-expanded .preview-stack {
  grid-template-rows: minmax(0, 1fr) 0;
}

body.flow-expanded .preview-stack {
  grid-template-rows: 0 minmax(0, 1fr);
}

body.form-expanded .flow-panel,
body.flow-expanded .form-panel {
  padding: 0;
  overflow: hidden;
  border-width: 0;
}

.panel {
  display: flex;
  min-height: 0;
  flex-direction: column;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: 6px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}

.panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.compact-header {
  margin-bottom: 8px;
}

.panel-header h2 {
  margin: 0;
  font-size: 16px;
}

.ghost-button {
  min-height: 28px;
  padding: 0 9px;
  color: var(--primary-dark);
  background: #f8fbfe;
  border-color: var(--border);
}

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

.mode-switch {
  display: inline-flex;
  width: fit-content;
  padding: 3px;
  background: #edf3f8;
  border: 1px solid var(--border);
  border-radius: 5px;
}

.mode-button {
  min-width: 54px;
  min-height: 28px;
  border: 0;
  background: transparent;
}

.mode-button.active {
  color: #fff;
  background: var(--primary);
}

.compact-buttons {
  gap: 6px;
}

.compact-buttons button {
  min-height: 28px;
  padding: 0 8px;
  font-size: 13px;
}

.zoom-slider {
  display: inline-flex;
  align-items: center;
  width: 120px;
  min-height: 28px;
}

.zoom-slider input {
  width: 100%;
  accent-color: var(--primary);
}

#ruleInput {
  flex: 1;
  width: 100%;
  min-height: 0;
  padding: 12px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  outline: none;
  line-height: 1.55;
  color: #172033;
  background: #fbfdff;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
}

#ruleInput:focus,
.field-control:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(23, 105, 170, 0.12);
}

.error-box {
  max-height: 116px;
  overflow: auto;
  margin-top: 10px;
  padding: 9px 11px;
  color: var(--danger);
  background: var(--danger-bg);
  border: 1px solid #ffccc7;
  border-radius: 4px;
  white-space: pre-line;
}

.form-preview {
  min-height: 0;
  overflow: auto;
  padding: 0 4px 2px 0;
}

.form-card {
  max-width: 540px;
  margin: 0 auto;
  padding: 10px;
  background: #fff;
  border: 1px solid var(--border-soft);
  border-radius: 6px;
}

.category-bar {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 7px 9px;
  margin-bottom: 7px;
  background: var(--field-bg);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
}

.category-bar label,
.field-label {
  display: block;
  margin-bottom: 4px;
  font-weight: 600;
}

.category-bar label {
  margin: 0;
}

.required-mark {
  margin-left: 4px;
  color: var(--danger);
}

.category-summary {
  margin: 0 0 7px;
  padding: 7px 9px;
  color: #23415c;
  background: #edf6ff;
  border-left: 3px solid var(--primary);
  border-radius: 3px;
  line-height: 1.45;
}

.tips-list {
  margin: 5px 0 0;
  padding-left: 18px;
  color: #455a70;
}

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

.field-item {
  padding: 8px;
  background: var(--field-bg);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
}

.field-item.full-width {
  grid-column: 1 / -1;
}

.field-control {
  width: 100%;
  min-height: 30px;
  padding: 5px 8px;
  border: 1px solid #cbd5e1;
  border-radius: 4px;
  background: #fff;
  outline: none;
}

.readonly-control {
  min-height: 30px;
  padding: 6px 8px;
  color: #23415c;
  background: #eef6ff;
  border: 1px solid #cfe0f0;
  border-radius: 4px;
  line-height: 1.4;
}

textarea.field-control {
  min-height: 58px;
  resize: vertical;
}

.number-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
}

.unit-tag {
  min-width: 38px;
  padding: 5px 7px;
  color: #31516c;
  text-align: center;
  background: #e8eef4;
  border-radius: 4px;
}

.field-help {
  margin-top: 5px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.cascade-stack {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.message-boxes {
  display: grid;
  gap: 6px;
  margin-top: 8px;
}

.message-box {
  padding: 7px 9px;
  border-radius: 4px;
  font-size: 12px;
  line-height: 1.45;
}

.message-box.pass {
  background: var(--ok-bg);
  border: 1px solid #b7dfc1;
}

.message-box.missing {
  background: var(--warn-bg);
  border: 1px solid #f6d08f;
}

.flow-panel {
  min-height: 0;
}

.flow-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 7px;
}

.flow-toolbar .compact-buttons:first-child {
  flex: 1 1 auto;
}

.mermaid-source {
  display: none;
}

.mermaid-card {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: grab;
}

.mermaid-card.dragging {
  cursor: grabbing;
}

.flow-canvas {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.flow-canvas svg {
  display: block;
  max-width: none;
}

.flow-canvas svg .nodeLabel,
.flow-canvas svg .edgeLabel,
.flow-canvas svg foreignObject,
.flow-canvas svg span {
  font-size: 16px !important;
  line-height: 1.35 !important;
}

.flow-canvas svg .node rect,
.flow-canvas svg .node polygon,
.flow-canvas svg .node circle,
.flow-canvas svg .node ellipse {
  stroke-width: 1.4px !important;
}

.zoom-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  min-width: 54px;
  padding: 4px 8px;
  color: #22435f;
  text-align: center;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.08);
}

.empty-state {
  padding: 18px;
  color: var(--muted);
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: 4px;
  background: #fbfdff;
}

@media (max-width: 1180px) {
  body {
    overflow: auto;
  }

  .workspace {
    grid-template-columns: 1fr;
    height: auto;
    min-height: calc(100vh - 58px);
    overflow: visible;
  }

  .editor-panel {
    min-height: 620px;
  }

  .preview-stack {
    min-height: 760px;
  }

  body.form-expanded .preview-stack,
  body.flow-expanded .preview-stack {
    grid-template-rows: 1fr 1fr;
  }
}

@media (max-width: 720px) {
  .app-header {
    height: auto;
    align-items: flex-start;
    flex-direction: column;
  }

  .workspace {
    height: auto;
    padding: 10px;
  }

  .flow-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .field-grid,
  .category-bar,
  .cascade-stack {
    grid-template-columns: 1fr;
  }
}
