:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #eef2f5;
  --surface-strong: #e5e9ed;
  --ink: #17202a;
  --muted: #667085;
  --line: #d7dee6;
  --accent: #0f766e;
  --accent-strong: #115e59;
  --warn: #b45309;
  --danger: #b42318;
  --focus: #2563eb;
  --shadow: 0 12px 30px rgba(20, 30, 43, 0.12);
  font-family:
    Inter, "Segoe UI", "Microsoft JhengHei", "PingFang TC", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-width: 320px;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
}

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

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

button:hover {
  border-color: #b5c0ca;
  background: #f9fbfc;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-rows: 58px minmax(0, 1fr);
  height: 100vh;
  min-height: 100vh;
  overflow: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(190px, 280px) minmax(0, 1fr) auto;
  grid-template-areas: "brand tabs actions";
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(10px);
}

.brand {
  grid-area: brand;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  font-weight: 700;
}

.brand-mark {
  display: grid;
  width: 32px;
  height: 32px;
  place-items: center;
  border-radius: 6px;
  background: #132b32;
  color: #e7f8f5;
  letter-spacing: 0;
}

.brand-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabs {
  grid-area: tabs;
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
}

.tab-button,
.action-button,
.tool-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 12px;
  white-space: nowrap;
}

.tab-button {
  background: transparent;
  border-color: transparent;
}

.tab-button.active {
  background: var(--surface-soft);
  border-color: var(--line);
  color: var(--accent-strong);
}

.action-button.active {
  border-color: #99d2cc;
  background: #e6f6f3;
  color: var(--accent-strong);
}

.top-actions {
  grid-area: actions;
  display: flex;
  align-items: center;
  gap: 8px;
}

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

.primary:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.danger {
  border-color: #f1b4ad;
  color: var(--danger);
}

.workspace {
  display: grid;
  grid-template-columns: 188px minmax(420px, 1fr) 360px;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.tool-rail {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 14px;
  padding: 14px;
  border-right: 1px solid var(--line);
  background: var(--surface);
}

.rail-section {
  display: grid;
  gap: 8px;
}

.rail-title {
  padding: 0 2px 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.rail-hint {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.tool-button {
  justify-content: flex-start;
  width: 100%;
  height: 42px;
  padding: 0 11px;
  background: #fbfcfd;
}

.tool-button span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool-button.active {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.canvas-area {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.canvas-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: #fbfcfd;
  min-width: 0;
}

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

.doc-title input {
  width: min(380px, 46vw);
  min-width: 160px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 9px;
  background: transparent;
  font-weight: 700;
}

.doc-title input:hover,
.doc-title input:focus {
  border-color: var(--line);
  background: var(--surface);
}

.status-line {
  color: var(--muted);
  font-size: 12px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.canvas-viewport {
  display: grid;
  min-height: 0;
  align-items: start;
  justify-items: center;
  overflow: auto;
  padding: 22px 28px 42px;
  background:
    linear-gradient(90deg, rgba(148, 163, 184, 0.12) 1px, transparent 1px),
    linear-gradient(rgba(148, 163, 184, 0.12) 1px, transparent 1px);
  background-size: 18px 18px;
}

.label-stage {
  display: grid;
  place-items: center;
  width: min(100%, 940px);
  padding: 20px;
  border: 1px solid var(--line);
  background: #dde3e9;
  box-shadow: var(--shadow);
}

.label-svg {
  display: block;
  width: 100%;
  max-width: 900px;
  max-height: 66vh;
  background: #fff;
  border: 1px solid #c9d2dc;
}

.label-object {
  cursor: move;
}

.selection-box {
  fill: none;
  stroke: var(--focus);
  stroke-dasharray: 120 80;
  stroke-width: 150;
  pointer-events: none;
}

.resize-handle {
  fill: #ffffff;
  stroke: var(--focus);
  stroke-width: 170;
  pointer-events: all;
}

.rotate-stem {
  stroke: var(--focus);
  stroke-width: 130;
  pointer-events: none;
}

.rotate-handle {
  cursor: grab;
  fill: #ffffff;
  stroke: var(--focus);
  stroke-width: 170;
  pointer-events: all;
}

.rotate-handle:active {
  cursor: grabbing;
}

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

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

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

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

.object-hit {
  fill: transparent;
  stroke: transparent;
  pointer-events: all;
}

.canvas-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  border-top: 1px solid var(--line);
  background: #fbfcfd;
  color: var(--muted);
  font-size: 12px;
}

.context-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: #ffffff;
  min-width: 0;
  overflow: hidden;
}

.context-summary {
  display: grid;
  min-width: 0;
  gap: 2px;
}

.context-summary strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.context-summary span {
  color: var(--muted);
  font-size: 12px;
}

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

.side-panel {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  min-height: 0;
  border-left: 1px solid var(--line);
  background: var(--surface);
}

.inspector-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 13px 16px 10px;
  border-bottom: 1px solid var(--line);
}

.inspector-header div {
  display: grid;
  min-width: 0;
  gap: 2px;
}

.inspector-header strong {
  font-size: 14px;
}

.inspector-header span {
  color: var(--muted);
  font-size: 12px;
}

.inspector-header button {
  min-height: 30px;
  padding: 0 9px;
  font-size: 12px;
}

.inspector-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: #fbfcfd;
}

.inspector-tab {
  min-height: 34px;
  padding: 0 8px;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

.inspector-tab.active {
  border-color: var(--line);
  background: #ffffff;
  color: var(--accent-strong);
  font-weight: 700;
}

.panel-scroll {
  overflow: auto;
  padding: 16px;
}

.section {
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 18px;
}

.section:last-child {
  border-bottom: 0;
  margin-bottom: 0;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 12px;
  color: #263342;
  font-size: 13px;
  font-weight: 750;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.field,
.wide-field {
  display: grid;
  gap: 5px;
}

.wide-field {
  grid-column: 1 / -1;
}

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

.field input,
.field select,
.wide-field input,
.wide-field select,
.wide-field textarea,
.operator-field input,
.operator-field select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 9px;
  background: #fff;
  color: var(--ink);
}

.wide-field textarea {
  min-height: 74px;
  resize: vertical;
}

.object-list,
.variable-list,
.queue-list {
  display: grid;
  gap: 8px;
}

.list-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.list-row.active {
  border-color: var(--focus);
  background: #eff6ff;
}

.row-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  font-weight: 650;
}

.row-meta {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.panel-actions {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  background: #fbfcfd;
}

.panel-actions button {
  flex: 1;
}

.arrange-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.arrange-grid.compact {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.icon-command {
  display: inline-grid;
  min-height: 38px;
  place-items: center;
  padding: 0;
}

.field input[readonly] {
  background: #f8fafc;
  color: var(--muted);
}

.operator-shell {
  display: grid;
  grid-template-columns: minmax(330px, 440px) minmax(340px, 1fr);
  gap: 18px;
  min-height: 0;
  overflow: auto;
  padding: 18px;
}

.operator-panel,
.preview-panel {
  min-width: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.operator-panel {
  padding: 16px;
}

.operator-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.operator-title {
  min-width: 0;
}

.operator-title h1 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 21px;
  line-height: 1.2;
}

.operator-title p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.agent-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid #f1d29f;
  border-radius: 999px;
  padding: 6px 9px;
  color: var(--warn);
  background: #fff8eb;
  font-size: 12px;
  white-space: nowrap;
}

.operator-form {
  display: grid;
  gap: 12px;
}

.operator-field {
  display: grid;
  gap: 6px;
}

.operator-field label {
  font-size: 13px;
  font-weight: 650;
}

.form-footer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 140px;
  gap: 10px;
  align-items: end;
  margin-top: 6px;
}

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

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}

.preview-canvas {
  display: grid;
  place-items: center;
  min-height: 300px;
  overflow: auto;
  padding: 24px;
  background: var(--surface-soft);
}

.preview-canvas .label-svg {
  width: min(100%, 720px);
  max-height: 56vh;
}

.queue-panel {
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  background: #fbfcfd;
}

.queue-item {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 9px;
  background: #fff;
  font-size: 12px;
}

.queue-item strong {
  display: block;
  margin-bottom: 3px;
  font-size: 13px;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: 140px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  padding: 18px;
}

.hidden-file {
  display: none;
}

#printSheet {
  display: none;
}

@media (max-width: 1050px) {
  .app-shell {
    grid-template-rows: auto minmax(0, 1fr);
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "brand actions"
      "tabs tabs";
    gap: 8px 12px;
    height: auto;
    padding: 8px 10px;
  }

  .tabs {
    justify-content: flex-start;
  }

  .top-actions {
    justify-content: flex-end;
  }

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

  .side-panel {
    grid-column: 1 / -1;
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .panel-scroll {
    max-height: 42vh;
  }

  .context-bar {
    grid-template-columns: 1fr;
  }

  .inline-arrange {
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .operator-shell {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .topbar {
    grid-template-columns: 1fr;
    height: auto;
    padding: 10px;
  }

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

  .tabs,
  .top-actions {
    justify-content: stretch;
  }

  .tab-button,
  .action-button {
    flex: 1;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .tool-rail {
    flex-direction: row;
    justify-content: flex-start;
    overflow-x: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .rail-section {
    grid-auto-flow: column;
    grid-auto-columns: max-content;
    align-items: center;
  }

  .rail-title,
  .rail-hint {
    display: none;
  }

  .tool-button {
    flex: 0 0 auto;
    width: auto;
    min-width: 92px;
  }

  .canvas-toolbar,
  .canvas-footer,
  .form-footer {
    grid-template-columns: 1fr;
    flex-direction: column;
    align-items: stretch;
  }

  .field-grid {
    grid-template-columns: 1fr;
  }

  .doc-title input {
    width: 100%;
  }

  .operator-shell {
    padding: 10px;
  }
}

@media print {
  body {
    background: #fff;
  }

  #app {
    display: none;
  }

  #printSheet {
    display: grid;
    min-height: 100vh;
    place-items: center;
  }

  #printSheet .label-svg {
    width: auto;
    max-width: 100vw;
    max-height: 100vh;
    border: 0;
  }
}
