:root {
  color-scheme: light;
  --bg: #f5f6f2;
  --panel: #ffffff;
  --ink: #17201c;
  --muted: #66706b;
  --line: #d7ddd5;
  --blue: #1d64c8;
  --green: #12815f;
  --red: #cf3e33;
  --amber: #b87314;
  --shadow: 0 12px 34px rgba(26, 36, 31, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

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

button,
.file-button {
  border: 1px solid var(--blue);
  background: var(--blue);
  color: #fff;
  border-radius: 6px;
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover,
.file-button:hover {
  filter: brightness(0.96);
}

button.ghost,
.file-button.ghost {
  background: #fff;
  color: var(--ink);
  border-color: var(--line);
}

.file-button input {
  display: none;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(620px, 1fr) 420px;
  gap: 16px;
  min-height: 100vh;
  padding: 16px;
}

.simple-shell {
  display: grid;
  grid-template-columns: minmax(720px, 980px);
  justify-content: center;
  align-content: start;
  gap: 10px;
  padding: 10px;
}

.workspace,
.side-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.workspace {
  min-width: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
}

.simple-shell .workspace {
  min-height: calc(100vh - 180px);
}

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

.simple-shell .topbar {
  position: relative;
  justify-content: center;
  text-align: center;
  padding: 12px 16px 8px;
}

.simple-shell .topbar h1 {
  font-size: 18px;
}

.simple-shell .top-actions {
  position: absolute;
  right: 24px;
  top: 18px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.25;
}

h2 {
  font-size: 15px;
  line-height: 1.2;
  margin-bottom: 10px;
}

.subtle {
  color: var(--muted);
  margin-top: 4px;
}

.top-actions,
.stage-toolbar,
.legend-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.stage-toolbar {
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
}

.simple-toolbar {
  justify-content: center;
  gap: 14px;
  padding: 10px 16px 12px;
}

.simple-toolbar::before {
  content: "拖拽框选图例区域，框完后点击提交服务器生成矩阵";
  font-size: 17px;
  color: var(--ink);
}

.simple-toolbar .segmented,
.simple-toolbar .range-label,
.simple-toolbar #fitZoom,
.simple-toolbar #clearActiveBox,
.simple-toolbar .auto-check {
  display: none;
}

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

.segmented button {
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  background: #fff;
  color: var(--ink);
}

.segmented button:last-child {
  border-right: 0;
}

.segmented button.active {
  background: var(--green);
  color: #fff;
}

.range-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.watermark-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: auto;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.watermark-toggle input {
  width: auto;
}

.canvas-wrap {
  position: relative;
  overflow: auto;
  min-height: 420px;
  background:
    linear-gradient(45deg, #eef1ec 25%, transparent 25%),
    linear-gradient(-45deg, #eef1ec 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #eef1ec 75%),
    linear-gradient(-45deg, transparent 75%, #eef1ec 75%);
  background-color: #fbfcfa;
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}

.simple-shell .canvas-wrap {
  min-height: calc(100vh - 260px);
  background: #f3f3f3;
}

#imageCanvas {
  display: block;
  background: #fff;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 18px;
  pointer-events: none;
}

.side-panel {
  overflow: auto;
  max-height: calc(100vh - 32px);
}

.simple-panel {
  max-height: none;
  box-shadow: none;
}

.simple-panel .panel-section {
  padding: 10px 12px;
}

.simple-panel h2 {
  margin-bottom: 6px;
}

.simple-panel #legendText {
  min-height: 86px;
}

.openrouter-result {
  min-height: 120px;
  max-height: 260px;
  overflow: auto;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfa;
  color: var(--muted);
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.matrix-meta {
  color: var(--muted);
  margin-bottom: 8px;
}

.matrix-preview-wrap {
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

#matrixCanvas {
  display: block;
  image-rendering: pixelated;
}

.hidden-controls {
  display: none !important;
}

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

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

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
}

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

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid rgba(29, 100, 200, 0.2);
  border-color: var(--blue);
}

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

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

.check-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.check-row label {
  display: inline-flex;
  grid-auto-flow: column;
  align-items: center;
  gap: 6px;
}

.check-row input {
  width: auto;
}

#legendText {
  min-height: 150px;
  resize: vertical;
  font-family: Consolas, "Courier New", monospace;
}

.legend-actions {
  margin-top: 8px;
}

.legend-preview {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  max-height: 170px;
  overflow: auto;
}

.legend-item {
  display: grid;
  grid-template-columns: 32px 1fr auto auto;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid #edf0eb;
}

.swatch {
  width: 28px;
  height: 22px;
  border: 1px solid #aeb7b0;
  border-radius: 4px;
}

.command {
  min-height: 74px;
  margin-top: 8px;
  resize: vertical;
  font-family: Consolas, "Courier New", monospace;
}

.summary {
  display: grid;
  gap: 6px;
  color: var(--muted);
}

.summary strong {
  color: var(--ink);
}

.summary .error {
  color: var(--red);
}

.summary .warning {
  color: var(--amber);
}

.summary .success {
  color: var(--green);
  font-weight: 700;
}

@media (max-width: 1080px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .side-panel {
    max-height: none;
  }
}

@media (max-width: 680px) {
  .app-shell {
    padding: 8px;
  }

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

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