*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  background-color: #f4f5f7;
  color: #1f2933;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: #ffffff;
  border-bottom: 1px solid #e1e4ea;
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.app-main {
  flex: 1;
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 16px;
  padding: 16px 24px 24px;
}

.panel {
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.06);
  padding: 16px 18px 18px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.panel-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #4b5563;
}

.panel-left {
  gap: 16px;
}

.upload-card {
  position: relative;
  border-radius: 12px;
  border: 1px dashed #cbd2e1;
  background: #f9fafb;
  padding: 18px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
}

.upload-card:hover {
  border-color: #6366f1;
  background: #eef2ff;
  box-shadow: 0 4px 10px rgba(129, 140, 248, 0.25);
}

.upload-card input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.upload-placeholder {
  pointer-events: none;
}

.upload-icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: #e0e7ff;
  color: #4338ca;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  margin: 0 auto 8px;
}

.upload-text {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
}

.upload-subtext {
  font-size: 12px;
  color: #6b7280;
}

.original-preview {
  display: none;
  flex-direction: column;
  gap: 8px;
}

.original-preview.visible {
  display: flex;
}

.preview-box {
  border-radius: 10px;
  border: 1px solid #e5e7eb;
  background: #f9fafb;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  max-height: 260px;
}

.preview-box img {
  max-width: 100%;
  max-height: 240px;
  object-fit: contain;
  border-radius: 8px;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 8px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-label {
  font-size: 13px;
  font-weight: 500;
  color: #4b5563;
}

.control-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.control-inline input[type="number"] {
  width: 100px;
}

.control-hint {
  font-size: 11px;
  color: #9ca3af;
}

input[type="range"] {
  width: 100%;
}

.chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  border-radius: 999px;
  border: 1px solid #e5e7eb;
  background: #f9fafb;
  font-size: 12px;
  padding: 4px 10px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.chip-selected {
  background: #4f46e5;
  border-color: #4338ca;
  color: #ffffff;
  box-shadow: 0 3px 8px rgba(79, 70, 229, 0.4);
}

input[type="number"],
input[type="text"],
select {
  border-radius: 8px;
  border: 1px solid #d1d5db;
  padding: 6px 8px;
  font-size: 12px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="number"]:focus,
input[type="text"]:focus,
select:focus {
  border-color: #4f46e5;
  box-shadow: 0 0 0 1px rgba(79, 70, 229, 0.35);
}

.btn {
  border-radius: 999px;
  border: 1px solid #d1d5db;
  padding: 6px 14px;
  font-size: 13px;
  background: #ffffff;
  color: #111827;
  cursor: pointer;
  transition: background-color 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.btn:hover:not(:disabled) {
  background: #f3f4ff;
  border-color: #6366f1;
  box-shadow: 0 3px 10px rgba(99, 102, 241, 0.35);
  transform: translateY(-1px);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: none;
}

.btn-primary {
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  border-color: #4f46e5;
  color: #ffffff;
}

.btn-secondary {
  background: #eef2ff;
  border-color: #c7d2fe;
  color: #3730a3;
}

.btn:disabled {
  background: #f3f4f6;
  border-color: #e5e7eb;
  color: #9ca3af;
  cursor: not-allowed;
  box-shadow: none;
}

.status-text {
  margin-top: 4px;
  font-size: 12px;
  min-height: 14px;
  color: #6b7280;
}

.status-text.status-error {
  color: #b91c1c;
}

.status-text.status-success {
  color: #16a34a;
}

.panel-right {
  overflow: hidden;
}

.preview-wrapper {
  flex: 1;
  min-height: 260px;
  max-height: 520px;
  border-radius: 12px;
  border: 1px solid #e5e7eb;
  background: radial-gradient(circle at top, #e0f2fe 0, #f9fafb 55%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
  overflow: auto;
}

#pixelCanvas {
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.18);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.pixel-info {
  font-size: 12px;
  color: #6b7280;
  min-height: 16px;
  margin-bottom: 8px;
}

.palette-section {
  border-top: 1px solid #e5e7eb;
  padding-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 340px;
}

.palette-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #4b5563;
}

.palette-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 6px;
  overflow-y: auto;
  padding-right: 4px;
}

.palette-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background-color 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease;
  font-size: 11px;
}

.palette-item:hover {
  background: #f3f4ff;
}

.palette-item.selected {
  border-color: #4f46e5;
  background: #eef2ff;
  box-shadow: 0 3px 8px rgba(79, 70, 229, 0.35);
}

.palette-swatch {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  border: 1px solid rgba(0, 0, 0, 0.18);
  flex-shrink: 0;
}

.palette-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 1.3;
}

.palette-brand-code {
  font-size: 11px;
  font-weight: 600;
  color: #2563eb;
  letter-spacing: 0.03em;
}

.palette-hex {
  font-size: 11px;
  color: #6b7280;
}

.palette-count {
  margin-left: auto;
  color: #6b7280;
}

/* ── 品牌选择器 ─────────────────────────────────────────────────────────── */
.brand-select-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.brand-select-row select {
  flex: 1;
  padding: 6px 10px;
  font-size: 13px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  color: #1f2937;
  cursor: pointer;
  outline: none;
  transition: border-color 0.15s;
}

.brand-select-row select:focus {
  border-color: #6366f1;
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.15);
}

.brand-hint {
  font-size: 11px;
  color: #2563eb;
  white-space: nowrap;
  font-weight: 500;
}

.brand-note {
  font-size: 11px;
  color: #9ca3af;
  margin-top: 3px;
  line-height: 1.4;
}

.palette-tools {
  border-top: 1px dashed #e5e7eb;
  padding-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 12px;
}

.tool-block {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tool-title {
  font-weight: 500;
  color: #4b5563;
}

.tool-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.tool-text {
  color: #9ca3af;
}

/* ── 编辑工具栏 ─────────────────────────────────────────────────────────── */
.edit-toolbar {
  display: none;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}

.edit-toolbar.visible {
  display: flex;
}

.btn-tool {
  padding: 4px 11px;
  font-size: 12px;
  line-height: 1.5;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
  color: #374151;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  white-space: nowrap;
  user-select: none;
}

.btn-tool:hover:not(:disabled) {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.btn-tool.btn-tool-active {
  background: #ede9fe;
  border-color: #7c3aed;
  color: #5b21b6;
  font-weight: 600;
}

.btn-tool:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.tool-divider {
  width: 1px;
  height: 18px;
  background: #d1d5db;
  flex-shrink: 0;
  margin: 0 2px;
}

/* 区域选中高亮提示文字 */
.selection-hint {
  font-size: 11px;
  color: #6366f1;
  margin-left: 4px;
  font-weight: 500;
}

/* ── 预览模式按钮激活态 ─────────────────────────────────────────────────── */
.btn.btn-active {
  background: #ede9fe;
  border-color: #7c3aed;
  color: #5b21b6;
  font-weight: 600;
}

/* ── 缩放栏 ─────────────────────────────────────────────────────────────── */
.zoom-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: #f8fafc;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  margin-bottom: 6px;
  font-size: 12px;
}

.zoom-bar-label {
  font-size: 12px;
  color: #9ca3af;
  font-weight: 500;
  margin-right: 2px;
}

.zoom-level-text {
  font-size: 12px;
  color: #6b7280;
  min-width: 40px;
  text-align: center;
}

/* ── 品牌颜色选择器 ──────────────────────────────────────────────────────── */
.brand-picker-btn {
  white-space: nowrap;
}

.brand-color-dropdown {
  position: relative;
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.10);
  padding: 8px;
  margin-top: 4px;
  max-height: 240px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 20;
}

.brand-color-search-input {
  width: 100%;
  padding: 5px 8px;
  font-size: 12px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  outline: none;
  box-sizing: border-box;
  flex-shrink: 0;
}

.brand-color-search-input:focus {
  border-color: #6366f1;
}

.brand-color-list {
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(76px, 1fr));
  gap: 4px;
}

.bc-item {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 5px;
  border-radius: 6px;
  border: 1.5px solid transparent;
  cursor: pointer;
  font-size: 11px;
  transition: background 0.12s, border-color 0.12s;
  user-select: none;
}

.bc-item:hover {
  background: #f3f4ff;
  border-color: #c7d2fe;
}

.bc-item.bc-selected {
  background: #eef2ff;
  border-color: #4f46e5;
}

.bc-swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,0.15);
  flex-shrink: 0;
}

.bc-code {
  font-family: ui-monospace, monospace;
  font-size: 11px;
  color: #374151;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── swatch-select 组件 ─────────────────────────────────────────────────── */
.swatch-select {
  position: relative;
  flex: 1;
  min-width: 0;
}

.ss-trigger {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 7px;
  border: 1px solid #d1d5db;
  border-radius: 7px;
  background: #fff;
  cursor: pointer;
  font-size: 11px;
  user-select: none;
  transition: border-color 0.12s;
  white-space: nowrap;
  overflow: hidden;
}

.ss-trigger:hover {
  border-color: #6366f1;
}

.ss-swatch {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,0.15);
  flex-shrink: 0;
  display: none;
}

.ss-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #374151;
}

.ss-arrow {
  color: #9ca3af;
  font-size: 10px;
  flex-shrink: 0;
}

.ss-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 130px;
  max-width: 200px;
  max-height: 180px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  z-index: 30;
  padding: 4px;
}

.ss-dropdown.ss-open {
  display: block;
}

.ss-option {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 11px;
  transition: background 0.1s;
  white-space: nowrap;
}

.ss-option:hover {
  background: #f3f4ff;
}

.ss-option-swatch {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  border: 1px solid rgba(0,0,0,0.15);
  flex-shrink: 0;
}

.replace-tool-row {
  align-items: center;
  gap: 4px;
  flex-wrap: nowrap;
}

/* ── 导出模态框 ─────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-box {
  background: #ffffff;
  border-radius: 16px;
  padding: 24px;
  width: 360px;
  max-width: 92vw;
  box-shadow: 0 20px 50px rgba(0,0,0,0.20);
  display: flex;
  flex-direction: column;
  gap: 0;
}

.modal-title {
  font-size: 16px;
  font-weight: 600;
  color: #111827;
  margin-bottom: 16px;
}

.modal-body {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
}

.modal-option-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 12px;
  border: 1.5px solid #e5e7eb;
  border-radius: 12px;
  background: #f9fafb;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  text-align: center;
}

.modal-option-btn:hover {
  border-color: #6366f1;
  background: #eef2ff;
  box-shadow: 0 4px 14px rgba(99,102,241,0.25);
}

.modal-option-icon {
  font-size: 28px;
}

.modal-option-label {
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
}

.modal-option-desc {
  font-size: 11px;
  color: #6b7280;
  line-height: 1.4;
}

/* ── 响应式 ─────────────────────────────────────────────────────────────── */
@media (max-width: 960px) {
  .app-main {
    grid-template-columns: 1fr;
  }
}

