:root {
  --ink: #0f172a;
  --muted: #64748b;
  --line: #dbe4f0;
  --paper: #eef2f9;
  --white: #ffffff;
  --primary: #2563eb;
  --primary-dark: #16305e;
  --primary-tint: #e0ecfe;
  --gold: #b99145;
  --blue: #2f5f8f;
  --rose: #b85159;
  --shadow: 0 18px 55px rgba(15, 23, 42, .11);
}

* { box-sizing: border-box; }

html { background: var(--paper); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 10% 0%, rgba(37, 99, 235, .12), transparent 28rem),
    linear-gradient(180deg, #f8fafc, var(--paper));
}

button, input, textarea { font: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--white);
  color: #374841;
}

button:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 3px solid rgba(37, 99, 235, .24);
  outline-offset: 2px;
}

.topbar {
  min-height: 74px;
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 12px clamp(18px, 4vw, 60px);
  background: rgba(255, 255, 255, .93);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 11px;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
}

.brand-mark img { width: 100%; height: 100%; object-fit: contain; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: 15px; }
.brand small { margin-top: 2px; font-size: 10px; color: var(--muted); }

.nav {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px;
  border-radius: 14px;
  background: #edf2ef;
}

.nav button, .nav a {
  min-height: 38px;
  border: 0;
  border-radius: 10px;
  padding: 0 12px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  font-weight: 900;
  color: #66736d;
  background: transparent;
  text-decoration: none;
}

.nav button.active, .nav a.active {
  background: var(--white);
  color: var(--primary);
  box-shadow: 0 2px 9px rgba(15, 40, 90, .09);
}

.nav svg, .actions svg, .tip svg, .code-box svg { width: 16px; height: 16px; }

.workspace {
  width: min(100%, 1380px);
  margin: 0 auto;
  padding: 28px 22px 56px;
  display: grid;
  grid-template-columns: minmax(340px, .82fr) minmax(0, 1.18fr);
  gap: 18px;
  align-items: start;
}

.editor, .preview-panel {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 255, 255, .97);
  box-shadow: var(--shadow);
}

.editor { padding: 24px; }
.preview-panel { padding: 18px; }

.section-head h1, .preview-head h2 {
  margin: 0;
  line-height: 1.08;
  letter-spacing: 0;
}

.section-head h1 { font-size: clamp(26px, 3vw, 38px); }
.preview-head h2 { font-size: 20px; }
.section-head p:not(.kicker) { color: var(--muted); font-size: 12px; line-height: 1.6; }

.kicker {
  margin: 0 0 7px;
  color: var(--primary);
  font-size: 10px;
  font-weight: 950;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.field {
  display: grid;
  gap: 7px;
  margin-top: 15px;
  color: #3c4d45;
  font-size: 11px;
  font-weight: 900;
}

.field input, .field textarea {
  width: 100%;
  border: 1px solid #cbd8d1;
  border-radius: 12px;
  background: #fbfdfc;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
}

.field input {
  height: 46px;
  padding: 0 13px;
}

.field textarea {
  resize: vertical;
  min-height: 170px;
  padding: 12px 13px;
  line-height: 1.45;
}

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

.raw-model {
  display: none;
}

.structured-editor {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-top: 15px;
}

.item-editor {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fbfdfc;
  overflow: hidden;
}

.item-head {
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  background: #f7faf8;
}

.item-head h3 {
  margin: 0;
  color: #2e4038;
  font-size: 12px;
}

.item-head p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.35;
}

.mini-add {
  flex: 0 0 auto;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 10px;
  padding: 0 10px;
  background: var(--primary-tint);
  color: var(--primary);
  font-size: 10px;
  font-weight: 950;
}

.mini-add svg,
.delete-row svg {
  width: 14px;
  height: 14px;
}

.item-list {
  display: grid;
  gap: 8px;
  padding: 10px;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(84px, .7fr) minmax(120px, 1fr) minmax(120px, 1.2fr) 38px;
  gap: 7px;
  align-items: end;
  padding: 9px;
  border: 1px solid #e2eae6;
  border-radius: 11px;
  background: #ffffff;
}

.item-row.support-row {
  grid-template-columns: minmax(120px, .9fr) minmax(160px, 1.4fr) minmax(100px, .7fr) 38px;
}

.item-row.connection-row {
  grid-template-columns: minmax(84px, .9fr) 82px minmax(84px, .9fr) 82px minmax(88px, .8fr) 38px;
}

.item-row label {
  display: grid;
  gap: 4px;
  color: #54635d;
  font-size: 8px;
  font-weight: 950;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.item-row input,
.item-row select {
  width: 100%;
  height: 36px;
  border: 1px solid #cbd8d1;
  border-radius: 9px;
  background: #fbfdfc;
  color: var(--ink);
  padding: 0 9px;
  font-size: 11px;
  font-weight: 750;
}

.delete-row {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  color: #9b333b;
  background: #fff3f4;
}

.tip {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  margin-top: 14px;
  padding: 12px;
  border-radius: 12px;
  background: var(--primary-tint);
  color: #1d3f72;
  font-size: 11px;
  line-height: 1.45;
}

.guide-box {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #fbfdfc;
  overflow: hidden;
}

.guide-box summary {
  min-height: 40px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 12px;
  cursor: pointer;
  color: #35463f;
  font-size: 10px;
  font-weight: 950;
}

.guide-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 0 12px 12px;
}

.guide-grid span {
  min-height: 42px;
  padding: 9px;
  border-radius: 10px;
  background: #f3f7f5;
  color: #52635b;
  font-size: 10px;
  line-height: 1.35;
}

.guide-grid b {
  display: block;
  margin-bottom: 2px;
  color: #1d3f72;
}

.actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
  gap: 9px;
  margin-top: 17px;
}

.actions button, .actions .inline-field {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border-radius: 12px;
  padding: 0 12px;
  font-size: 10px;
  font-weight: 950;
}

.actions .primary {
  border: 0;
  color: #fff;
  background: linear-gradient(135deg, #3b82f6, var(--primary-dark));
  box-shadow: 0 10px 22px rgba(37, 99, 235, .19);
}

.actions .inline-field {
  border: 1px solid var(--line);
  background: var(--white);
  color: #374841;
}

.actions .inline-field select,
.actions .inline-field input {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 10px;
  font-weight: 950;
  color: inherit;
  padding: 0;
  min-width: 0;
}

.actions .inline-field input[type="number"] {
  width: 32px;
  text-align: center;
}

.actions .inline-field select:focus-visible,
.actions .inline-field input:focus-visible {
  outline: none;
}

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

#template-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  height: 30px;
  padding: 0 10px;
  border-radius: 999px;
  background: var(--primary-tint);
  color: var(--primary);
  font-size: 10px;
  font-weight: 950;
}

.canvas-wrap {
  min-height: 610px;
  display: grid;
  place-items: center;
  overflow: auto;
  border: 1px solid #dfe8e3;
  border-radius: 14px;
  background:
    linear-gradient(#f8fbf9 1px, transparent 1px),
    linear-gradient(90deg, #f8fbf9 1px, transparent 1px),
    #ffffff;
  background-size: 22px 22px;
}

#chart-preview {
  min-width: 760px;
  padding: 18px;
}

.org-svg {
  width: 100%;
  max-width: 1060px;
  height: auto;
  display: block;
}

.node rect, .note rect, .timeline-node rect {
  filter: drop-shadow(0 7px 14px rgba(17, 40, 31, .11));
}

.node-title {
  font-size: 15px;
  font-weight: 900;
  fill: #17251f;
}

.node-role {
  font-size: 11px;
  font-weight: 700;
  fill: #607067;
}

.small-label {
  font-size: 10px;
  font-weight: 900;
  fill: #607067;
}

.chart-title {
  font-size: 24px;
  font-weight: 950;
  fill: #13211c;
}

.chart-context {
  font-size: 12px;
  font-weight: 800;
  fill: #65746d;
}

.connector {
  stroke: #9cafb6;
  stroke-width: 2.2;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.dashed { stroke-dasharray: 6 6; }

.arrowed {
  marker-end: url(#arrow);
}

.edge-label {
  font-size: 10px;
  font-weight: 900;
  fill: #5c6d66;
  paint-order: stroke;
  stroke: #ffffff;
  stroke-width: 4px;
  stroke-linejoin: round;
}

.flow-node,
.draggable-node {
  cursor: grab;
  touch-action: none;
}

.flow-node.dragging,
.draggable-node.dragging {
  cursor: grabbing;
  opacity: .9;
}

.flow-node text,
.draggable-node text {
  user-select: none;
  pointer-events: none;
}

.draggable-node:hover,
.flow-node:hover {
  outline: none;
}

.code-box {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: #fbfdfc;
  overflow: hidden;
}

.code-box summary {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 13px;
  cursor: pointer;
  color: #3b4b44;
  font-size: 10px;
  font-weight: 950;
}

.code-box pre {
  margin: 0;
  max-height: 260px;
  overflow: auto;
  padding: 13px;
  border-top: 1px solid var(--line);
  color: #20322b;
  font-size: 11px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%) translateY(80px);
  opacity: 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--primary-dark);
  color: #fff;
  font-size: 11px;
  font-weight: 850;
  transition: .2s ease;
  box-shadow: var(--shadow);
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 1060px) {
  .workspace { grid-template-columns: 1fr; }
  .canvas-wrap { min-height: 520px; }
}

@media (max-width: 720px) {
  .topbar { min-height: 64px; padding: 10px 12px; }
  .brand small, .nav span { display: none; }
  .nav button, .nav a { width: 39px; padding: 0; justify-content: center; }
  .workspace { padding: 16px 12px 40px; }
  .editor, .preview-panel { padding: 15px; border-radius: 15px; }
  .field-grid, .actions, .guide-grid, .item-row, .item-row.support-row, .item-row.connection-row { grid-template-columns: 1fr; }
  .delete-row { width: 100%; }
  #chart-preview { min-width: 700px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
  }
}
