:root {
  --ink: #1f2523;
  --muted: #66716d;
  --line: #cbd6d1;
  --paper: #ffffff;
  --workspace: #edf2ef;
  --panel: #ffffff;
  --accent: #0d8b72;
  --accent-dark: #0a6f5c;
  --danger: #c33c32;
  --bleed: #f45757;
  --derived-bg: #fdf5f5;
  --content-border: #1f2523;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, #f8fbf9 0, #f8fbf9 18%, transparent 18%),
    linear-gradient(180deg, #f3f7f4 0%, #e9efec 100%);
  font-family: Arial, "Microsoft YaHei", sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: minmax(320px, 390px) minmax(560px, 1fr);
  gap: 28px;
  align-items: start;
  width: min(1480px, calc(100% - 32px));
  margin: 0 auto;
  padding: 24px 0 36px;
}

.controls {
  position: sticky;
  top: 20px;
  display: grid;
  gap: 18px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 16px 40px rgba(31, 37, 35, 0.08);
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 26px;
  line-height: 1.2;
}

h2 {
  font-size: 16px;
  line-height: 1.2;
}

.hint,
.group-hint,
.preview-header p {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.field {
  display: grid;
  gap: 8px;
}

label,
.mini-label {
  color: var(--ink);
  font-size: 14px;
  font-weight: 700;
}

input,
select {
  min-height: 40px;
  width: 100%;
  border: 1px solid #b9c6c0;
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--ink);
  background: #ffffff;
}

.editor-group {
  display: grid;
  gap: 12px;
}

.group-header {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 12px;
}

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

.button {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  color: #ffffff;
  background: var(--accent);
  font-weight: 700;
}

.button:hover {
  background: var(--accent-dark);
}

.button:disabled {
  cursor: wait;
  opacity: 0.72;
}

.button.secondary {
  color: var(--ink);
  border-color: var(--line);
  background: #f7faf8;
}

.button.secondary:hover {
  background: #ecf4f0;
}

.color-sequence-editor,
.entry-editor-list {
  display: grid;
  gap: 10px;
  max-height: 28vh;
  overflow: auto;
  padding-right: 2px;
}

.sequence-editor-item,
.entry-editor {
  display: grid;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfdfc;
}

.sequence-editor-item {
  grid-template-columns: 30px 1fr 36px 36px;
}

.entry-editor {
  grid-template-columns: 30px 1fr 1fr 76px 36px;
}

.drag-handle {
  width: 30px;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f5f9f7;
  color: var(--muted);
  font-size: 16px;
  cursor: grab;
}

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

.sequence-editor-item.is-dragging,
.entry-editor.is-dragging {
  opacity: 0.5;
}

.color-picker {
  position: relative;
  display: grid;
  gap: 6px;
}

.color-picker-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.picker-label,
.number-label {
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
}

.color-trigger,
.swatch {
  border: 1.4px solid #111111;
  border-radius: 50%;
  padding: 0;
  background: var(--swatch-color);
  cursor: pointer;
}

.color-trigger {
  width: 26px;
  height: 26px;
}

.swatch {
  width: 22px;
  height: 22px;
}

.color-trigger.is-open,
.swatch.is-selected {
  box-shadow: 0 0 0 2px #ffffff, 0 0 0 4px var(--accent);
}

.swatch-popover {
  position: absolute;
  z-index: 20;
  top: calc(100% + 6px);
  right: 0;
  display: grid;
  grid-template-columns: repeat(4, 22px);
  gap: 8px;
  width: 144px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: 0 12px 26px rgba(31, 37, 35, 0.16);
}

.custom-color {
  grid-column: 1 / -1;
  display: grid;
  gap: 6px;
  padding-top: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.swatch-popover .custom-color input {
  min-height: 30px;
  padding: 2px;
  cursor: pointer;
}

.entry-number,
.sequence-editor-item .color-picker {
  min-width: 0;
}

.entry-number label {
  display: grid;
  gap: 6px;
  font-size: 12px;
}

.entry-number input {
  min-height: 36px;
  padding: 6px 8px;
}

.delete-item {
  min-width: 36px;
  min-height: 36px;
  border: 1px solid #e2b4b0;
  border-radius: 8px;
  color: var(--danger);
  background: #fff8f7;
  cursor: pointer;
  font-weight: 700;
}

.status-panel {
  display: grid;
  gap: 8px;
}

.status-message {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.5;
}

.status-message.info {
  border: 1px solid #bed7cd;
  background: #f3fbf7;
}

.status-message.error {
  border: 1px solid #e3b0a9;
  background: #fff4f2;
}

.preview-column {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: max-content;
  gap: 16px;
  align-items: start;
  justify-content: start;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 12px;
}

.preview-wrap {
  --preview-scale: 0.76;
  display: grid;
  justify-items: start;
  gap: 12px;
  min-width: max-content;
}

.preview-header {
  width: calc(148mm * var(--preview-scale));
  display: grid;
  gap: 4px;
}

.preview-card-frame {
  width: calc(148mm * var(--preview-scale));
  height: calc(210mm * var(--preview-scale));
  overflow: hidden;
}

.preview-card-stage {
  width: 148mm;
  height: 210mm;
  transform: scale(var(--preview-scale));
  transform-origin: top left;
}

.card-sheet {
  position: relative;
  width: 148mm;
  height: 210mm;
  background: var(--bleed);
  box-shadow: 0 18px 40px rgba(29, 45, 39, 0.16);
  overflow: hidden;
}

.finished-card {
  position: absolute;
  inset: 6mm;
  border-radius: 2mm;
  box-shadow: inset 0 0 0 0.2mm rgba(31, 37, 35, 0.14);
  overflow: hidden;
}

.card-background {
  position: absolute;
  inset: 0;
  background: var(--derived-bg);
}

.puzzle-header {
  position: absolute;
  inset: 0;
}

.logo-slot {
  position: absolute;
  left: 6mm;
  top: 3.5mm;
  width: 40mm;
  height: 20mm;
  display: flex;
  align-items: center;
  justify-content: flex-start;
}

.logo-slot img {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.stars {
  position: absolute;
  right: 6mm;
  top: 3.5mm;
  min-width: 34mm;
  height: 20mm;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 2mm;
}

.stars img {
  width: 8mm;
  height: 8mm;
  object-fit: contain;
}

.content-box {
  position: absolute;
  left: 6mm;
  width: 124mm;
  border: 0.6mm solid var(--content-border);
  border-radius: 2mm;
  background: #ffffff;
  overflow: hidden;
}

.content-box-inner {
  width: 100%;
  height: 100%;
  padding: 3mm;
  display: grid;
  place-items: center;
}

.puzzle-box {
  top: var(--box-top, 50.25mm);
  height: var(--box-height, 124mm);
}

.puzzle-box-inner {
  grid-template-rows: auto 1fr;
  gap: var(--palette-entries-gap, 4mm);
  align-content: center;
}

.answer-box {
  top: var(--box-top, 37mm);
  height: var(--box-height, 124mm);
}

.palette-row,
.answer-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sequence-gap, 5mm);
  width: 100%;
  min-width: 0;
}

.palette-dot,
.answer-dot,
.piece {
  border-radius: 50%;
  border: 0.6mm solid #111111;
  background: var(--piece-color);
}

.palette-dot,
.answer-dot {
  width: var(--sequence-dot-size, 10mm);
  height: var(--sequence-dot-size, 10mm);
  flex: 0 0 auto;
}

.entries-grid {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: var(--entries-gap, 4mm);
  width: 100%;
  height: 100%;
}

.entries-grid.two-columns {
  grid-template-columns: 1fr 1fr;
  column-gap: 4mm;
}

.entry-preview {
  display: grid;
  grid-template-columns: var(--piece-size, 13mm) var(--distance-width, 18mm) var(--piece-size, 13mm);
  align-items: center;
  justify-content: center;
  width: max-content;
  height: var(--entry-row-height, 16mm);
}

.piece {
  width: var(--piece-size, 13mm);
  height: var(--piece-size, 13mm);
}

.distance-mark {
  position: relative;
  width: var(--distance-width, 18mm);
  height: var(--piece-size, 13mm);
}

.distance-line {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--distance-line-width, 15mm);
  height: 0.6mm;
  transform: translate(-50%, -50%);
  background: #1f2523;
}

.distance-line::before,
.distance-line::after {
  content: "";
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 0.6mm;
  height: 4.8mm;
  background: #1f2523;
}

.distance-line::before {
  left: 0;
}

.distance-line::after {
  right: 0;
}

.distance-number {
  position: absolute;
  inset: 0 0 auto;
  top: 0.2mm;
  color: #111111;
  font-size: var(--distance-font-size, 15px);
  font-weight: 800;
  line-height: 1;
  text-align: center;
}

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

  .controls {
    position: static;
  }
}

@media (max-width: 760px) {
  .preview-wrap {
    --preview-scale: 0.68;
  }

  .actions,
  .group-header,
  .entry-editor,
  .sequence-editor-item {
    grid-template-columns: 1fr;
  }

  .group-header {
    display: grid;
  }
}

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

  .controls {
    display: none;
  }

  .app {
    display: block;
    width: auto;
    margin: 0;
    padding: 0;
  }

  .preview-column {
    display: block;
  }

  .preview-wrap {
    page-break-after: always;
  }

  .card-sheet {
    box-shadow: none;
  }
}
