:root {
  color-scheme: light;
  --bg: #f4f1eb;
  --surface: #fffdf8;
  --surface-strong: #ffffff;
  --ink: #1e2724;
  --muted: #65716d;
  --line: #d9ded5;
  --accent: #0f766e;
  --accent-dark: #0b5d57;
  --gold: #c17d10;
  --shadow: 0 22px 60px rgba(24, 38, 33, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(15, 118, 110, 0.12), transparent 34%),
    linear-gradient(315deg, rgba(193, 125, 16, 0.14), transparent 32%),
    var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Microsoft YaHei", sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  min-height: 100vh;
  padding: 24px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  gap: 18px;
  max-width: 1320px;
  min-height: calc(100vh - 48px);
  margin: 0 auto;
}

.control-panel,
.preview-panel {
  background: rgba(255, 253, 248, 0.88);
  border: 1px solid rgba(217, 222, 213, 0.9);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.control-panel {
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 24px;
}

.brand-block h1,
.preview-header h2,
.legend-header h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand-block h1 {
  font-size: 32px;
  line-height: 1.12;
}

.brand-block p:last-child {
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.7;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.upload-zone {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 176px;
  padding: 22px;
  border: 1px dashed #9baaa4;
  border-radius: 8px;
  background: #f8fbf6;
  cursor: pointer;
  text-align: center;
  transition:
    border-color 160ms ease,
    background 160ms ease,
    transform 160ms ease;
}

.upload-zone:hover {
  background: #edf7f4;
  border-color: var(--accent);
  transform: translateY(-1px);
}

.source-preview {
  display: none;
  width: 100%;
  max-height: 210px;
  margin-bottom: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  object-fit: contain;
}

.upload-zone.has-preview {
  align-content: start;
  min-height: 260px;
  padding: 12px;
  background: #ffffff;
  border-style: solid;
}

.upload-zone.has-preview .source-preview {
  display: block;
}

.upload-zone.has-preview .upload-icon {
  width: 30px;
  height: 30px;
  margin-bottom: 6px;
  font-size: 22px;
}

.upload-zone.has-preview strong {
  font-size: 15px;
}

.upload-zone input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.upload-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-bottom: 10px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: 30px;
  line-height: 1;
}

.upload-zone strong {
  font-size: 18px;
}

.upload-zone small {
  margin-top: 7px;
  color: var(--muted);
}

.image-tools {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.62);
}

.image-tools summary {
  min-height: 42px;
  padding: 11px 14px;
  cursor: pointer;
  color: var(--ink);
  font-weight: 900;
}

.tool-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 0 14px 12px;
}

.tool-grid label {
  display: grid;
  gap: 6px;
  min-width: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.tool-grid label > span {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.tool-grid output {
  color: var(--accent);
  font-weight: 900;
}

.tool-grid input,
.tool-grid select {
  width: 100%;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  padding: 0 10px;
  font-weight: 800;
}

.tool-grid input[type="range"] {
  padding: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.tool-actions {
  padding: 0 14px 14px;
}

.tool-actions button {
  width: 100%;
  min-height: 38px;
  border: 1px solid #efd39f;
  border-radius: 8px;
  background: #fff7e8;
  color: #8d5707;
  cursor: pointer;
  font-weight: 900;
}

.settings-grid,
.editor-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.settings-grid label,
.editor-grid label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.settings-grid input,
.settings-grid select,
.editor-grid select {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  color: var(--ink);
  padding: 0 12px;
  font-weight: 800;
}

.settings-grid input:focus,
.settings-grid select:focus,
.editor-grid select:focus,
.toggle-row input:focus-visible {
  outline: 3px solid rgba(15, 118, 110, 0.18);
  border-color: var(--accent);
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-weight: 700;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.actions button,
.batch-actions button,
.history-actions button {
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 900;
}

#generateBtn {
  background: var(--accent);
  color: white;
}

#generateBtn:hover {
  background: var(--accent-dark);
}

#downloadBtn {
  background: #fff7e8;
  border-color: #efd39f;
  color: #8d5707;
}

#downloadBtn:disabled {
  cursor: not-allowed;
  opacity: 0.46;
}

.editor-panel {
  display: grid;
  gap: 14px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.62);
}

.panel-heading h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0;
}

.edit-mode {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.edit-mode label {
  position: relative;
}

.edit-mode input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.edit-mode span {
  display: grid;
  place-items: center;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--muted);
  cursor: pointer;
  font-weight: 900;
}

.edit-mode input:checked + span {
  border-color: var(--accent);
  background: #e4f5f1;
  color: var(--accent-dark);
}

.editor-grid {
  grid-template-columns: 1fr;
}

.batch-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.history-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.history-actions button {
  min-height: 34px;
  background: white;
  border-color: var(--line);
  color: var(--ink);
  font-size: 13px;
}

.batch-actions button {
  background: #fff7e8;
  border-color: #efd39f;
  color: #8d5707;
}

.batch-actions > button:first-of-type {
  background: var(--accent);
  color: white;
}

.batch-actions button:disabled,
.history-actions button:disabled,
.editor-grid select:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.editor-status {
  min-height: 22px;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.hint {
  margin: auto 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.65;
}

.preview-panel {
  display: grid;
  grid-template-rows: auto minmax(320px, 1fr) auto;
  overflow: hidden;
}

.preview-header,
.legend-section {
  padding: 22px;
}

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  border-bottom: 1px solid var(--line);
}

.preview-header h2 {
  font-size: 24px;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.stats span,
#legendCount {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: white;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.zoom-actions {
  display: inline-grid;
  grid-template-columns: 34px minmax(58px, auto) 34px;
  gap: 6px;
  align-items: center;
}

.zoom-actions button {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  font-size: 13px;
  font-weight: 900;
  cursor: pointer;
}

.zoom-actions button:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent-dark);
}

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

.label-toggle-btn {
  min-height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  cursor: pointer;
  font-size: 13px;
  font-weight: 900;
}

.label-toggle-btn[aria-pressed="true"] {
  border-color: #efd39f;
  background: #fff7e8;
  color: #8d5707;
}

.canvas-wrap {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 420px;
  padding: 28px;
  overflow: auto;
  background:
    linear-gradient(90deg, rgba(30, 39, 36, 0.04) 1px, transparent 1px),
    linear-gradient(rgba(30, 39, 36, 0.04) 1px, transparent 1px);
  background-size: 24px 24px;
}

canvas {
  max-width: none;
  height: auto;
  border: 1px solid #bfc9c3;
  border-radius: 4px;
  background: white;
  cursor: crosshair;
  box-shadow: 0 14px 32px rgba(32, 45, 39, 0.12);
  touch-action: none;
}

canvas.is-panning {
  cursor: grabbing;
}

.empty-state {
  position: absolute;
  inset: 22px;
  display: grid;
  place-content: center;
  gap: 8px;
  text-align: center;
  color: var(--muted);
  pointer-events: none;
}

.empty-state strong {
  color: var(--ink);
  font-size: 20px;
}

.legend-section {
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.48);
}

.legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.legend-header h2 {
  font-size: 19px;
}

.legend-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 10px;
  max-height: 220px;
  overflow: auto;
}

.legend-item {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 10px;
  min-height: 46px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.swatch {
  width: 28px;
  height: 28px;
  border: 1px solid rgba(0, 0, 0, 0.16);
  border-radius: 50%;
}

.legend-code {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.legend-code strong {
  overflow-wrap: anywhere;
  font-size: 13px;
}

.legend-code span,
.legend-total {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.legend-total {
  white-space: nowrap;
}

@media (max-width: 900px) {
  body {
    background: var(--bg);
  }

  .app-shell {
    padding: 10px 10px 132px;
  }

  .workspace {
    grid-template-columns: 1fr;
    min-height: auto;
    gap: 10px;
  }

  .control-panel {
    gap: 12px;
    padding: 14px;
    box-shadow: none;
  }

  .brand-block h1 {
    font-size: 24px;
  }

  .brand-block p:last-child {
    margin-top: 6px;
    line-height: 1.45;
  }

  .upload-zone {
    min-height: 112px;
    padding: 12px;
  }

  .upload-zone.has-preview {
    min-height: 150px;
  }

  .source-preview {
    max-height: 108px;
    margin-bottom: 8px;
  }

  .image-tools summary {
    min-height: 38px;
    padding: 9px 12px;
  }

  .tool-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    padding: 0 12px 10px;
  }

  .tool-grid label {
    gap: 4px;
    font-size: 11px;
  }

  .tool-grid input,
  .tool-grid select {
    height: 34px;
    padding: 0 7px;
    font-size: 12px;
  }

  .tool-actions {
    padding: 0 12px 12px;
  }

  .tool-actions button {
    min-height: 34px;
    font-size: 12px;
  }

  .settings-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
  }

  .settings-grid label {
    gap: 5px;
    font-size: 12px;
  }

  .settings-grid input {
    height: 38px;
    padding: 0 8px;
  }

  .actions {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .actions button {
    min-height: 40px;
  }

  .hint {
    display: none;
  }

  .editor-panel {
    position: fixed;
    right: 10px;
    bottom: 10px;
    left: 10px;
    z-index: 20;
    grid-template-columns: 74px minmax(0, 1fr) 86px;
    align-items: end;
    gap: 8px;
    padding: 10px;
    border-color: rgba(15, 118, 110, 0.22);
    background: rgba(255, 253, 248, 0.96);
    box-shadow: 0 14px 44px rgba(24, 38, 33, 0.22);
  }

  .editor-panel .eyebrow,
  .editor-status {
    display: none;
  }

  .panel-heading h2 {
    font-size: 15px;
  }

  .edit-mode {
    grid-column: 1;
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .edit-mode span {
    min-height: 31px;
    font-size: 12px;
  }

  .editor-grid {
    grid-column: 2;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .editor-grid label {
    gap: 4px;
    font-size: 11px;
  }

  .editor-grid select {
    height: 34px;
    padding: 0 6px;
    font-size: 12px;
  }

  .batch-actions {
    grid-column: 3;
    gap: 6px;
  }

  .history-actions {
    gap: 5px;
  }

  .history-actions button,
  .batch-actions button {
    min-height: 31px;
    font-size: 12px;
  }

  .batch-actions > button {
    display: none;
  }

  .batch-actions > button:first-of-type {
    display: block;
    font-size: 0;
  }

  .batch-actions > button:first-of-type::after {
    content: "替换";
    font-size: 12px;
  }

  .preview-header {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
    padding: 14px;
  }

  .preview-header h2 {
    font-size: 20px;
  }

  .stats {
    justify-content: flex-start;
  }

  .zoom-actions {
    grid-template-columns: 32px 56px 32px;
  }

  .zoom-actions button {
    min-height: 30px;
    padding: 0 8px;
    font-size: 12px;
  }

  .label-toggle-btn {
    min-height: 30px;
    padding: 0 10px;
    font-size: 12px;
  }

  .preview-panel {
    min-height: 72vh;
    box-shadow: none;
  }

  .canvas-wrap {
    min-height: 58vh;
    padding: 12px;
    place-items: start center;
  }

  .legend-section {
    padding: 14px;
  }

  .legend-list {
    max-height: 130px;
  }
}

@media (max-width: 520px) {
  .app-shell {
    padding: 8px 8px 140px;
  }

  .settings-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .tool-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .canvas-wrap {
    min-height: 54vh;
    padding: 10px;
  }

  .legend-list {
    grid-template-columns: 1fr;
  }

  .editor-panel {
    right: 8px;
    bottom: 8px;
    left: 8px;
    grid-template-columns: 62px minmax(0, 1fr) 76px;
    padding: 8px;
  }

  .panel-heading h2 {
    font-size: 13px;
  }

  .editor-grid {
    gap: 5px;
  }

  .editor-grid label {
    font-size: 10px;
  }

  .editor-grid select {
    height: 32px;
    font-size: 11px;
  }

  .history-actions button,
  .batch-actions button {
    min-height: 30px;
    font-size: 11px;
  }

  .batch-actions > button:first-of-type::after {
    font-size: 11px;
  }
}

@media (max-width: 390px) {
  .editor-panel {
    grid-template-columns: 1fr;
  }

  .panel-heading {
    display: none;
  }

  .edit-mode,
  .editor-grid,
  .batch-actions {
    grid-column: 1;
  }

  .edit-mode {
    grid-template-columns: 1fr 1fr;
  }

  .batch-actions {
    grid-template-columns: 1fr 1fr;
  }

  .history-actions {
    grid-column: 1 / -1;
  }
}
