:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --border: #d9dee7;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --accent-soft: #e8f0ff;
  --danger: #dc2626;
  --shadow: 0 12px 28px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 9px 13px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

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

button:disabled {
  cursor: not-allowed;
  border-color: var(--border);
  background: #e5e7eb;
  color: #8b94a5;
}

button.secondary {
  border-color: #bfd0f8;
  background: var(--accent-soft);
  color: var(--accent);
}

button.secondary:hover {
  border-color: var(--accent);
}

button.danger,
.danger {
  border-color: #fecaca;
  background: #fff1f2;
  color: var(--danger);
}

input,
select {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
}

input[type="range"] {
  width: 100%;
  min-height: auto;
  padding: 0;
}

input[type="color"] {
  min-width: 48px;
  padding: 3px;
}

.app {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 18px 20px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.topbar h1 {
  margin: 0 0 6px;
  font-size: 24px;
  letter-spacing: 0;
}

.topbar p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.mode-switch {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
}

.mode-btn {
  min-width: 92px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.mode-btn:hover,
.mode-btn.active {
  background: var(--accent);
  color: #fff;
}

.tool-view {
  display: none;
}

.tool-view.active {
  display: block;
}

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

.controls {
  margin-top: 16px;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(145px, 1fr));
  gap: 14px;
  align-items: end;
}

.controls label,
.dimension-control,
.actions,
.single-actions label,
.size-row label {
  display: flex;
  flex-direction: column;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.collage-drop-zone {
  min-height: 94px;
  grid-column: span 2;
}

.dimension-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.dimension-row label {
  min-width: 0;
}

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

.workspace {
  margin-top: 16px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 16px;
}

.stage-wrap,
.sidebar,
.single-panel {
  padding: 16px;
}

.stage-header,
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  color: var(--muted);
}

.stage-header strong,
.sidebar-head h2 {
  margin: 0;
  color: var(--text);
  font-size: 16px;
}

.sidebar-head span,
.stage-header span {
  font-size: 12px;
}

.canvas-scroll {
  overflow: auto;
  min-height: 520px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  background: #eef2f6;
}

#canvas {
  display: block;
  width: min(100%, 1000px);
  height: auto;
  margin: 0 auto;
  border-radius: 8px;
  background: #fff;
  cursor: grab;
  touch-action: none;
}

#canvas.sorting {
  cursor: grabbing;
}

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

.thumb-list.empty {
  min-height: 180px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--muted);
}

.thumb-item {
  display: grid;
  grid-template-columns: 78px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}

.thumb-item.dragging {
  opacity: 0.55;
}

.thumb-item img {
  width: 78px;
  height: 78px;
  border-radius: 6px;
  object-fit: cover;
  background: #e5e7eb;
}

.thumb-info {
  min-width: 0;
  display: grid;
  gap: 8px;
}

.thumb-info span {
  overflow: hidden;
  color: var(--text);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thumb-actions {
  display: flex;
  gap: 6px;
}

.thumb-actions button {
  min-height: 30px;
  padding: 5px 9px;
  font-size: 12px;
}

.thumb-actions button[data-action="delete"] {
  border-color: #fecaca;
  background: #fff1f2;
  color: var(--danger);
}

.single-view {
  margin-top: 16px;
}

.single-panel {
  display: grid;
  gap: 16px;
}

.single-upload {
  display: grid;
}

.drop-zone {
  min-height: 118px;
  display: grid;
  place-items: center;
  gap: 6px;
  border: 2px dashed var(--border);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
  text-align: center;
}

.drop-zone strong {
  color: var(--accent);
}

.drop-zone input {
  display: none;
}

.drop-zone.dragging,
.drop-zone:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.single-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  gap: 16px;
}

.single-preview {
  min-width: 0;
}

.preview-box {
  min-height: 460px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  background: #eef2f6;
}

#singlePreviewCanvas {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  background: #fff;
}

.single-actions {
  align-self: start;
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: none;
}

.modal.open {
  display: block;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
}

.crop-dialog {
  position: relative;
  z-index: 1;
  width: min(1180px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  margin: 16px auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.25);
}

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

.crop-head h2 {
  margin: 0 0 4px;
  font-size: 18px;
}

.crop-head p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.icon-btn {
  width: 36px;
  min-height: 36px;
  padding: 0;
  border-color: var(--border);
  background: #fff;
  color: var(--text);
  font-size: 22px;
}

.icon-btn:hover {
  background: var(--panel-2);
}

.crop-body {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 260px;
  gap: 16px;
  padding: 16px;
  overflow: auto;
}

.crop-stage {
  position: relative;
  min-height: 420px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f1f5f9;
}

#cropBaseCanvas,
#cropOverlayCanvas {
  display: block;
  max-width: 100%;
  height: auto;
}

#cropOverlayCanvas {
  position: absolute;
  cursor: crosshair;
}

.crop-side {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

.side-title {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.size-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.ratio-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.ratio-chip {
  min-height: 30px;
  border-color: var(--border);
  background: #fff;
  color: var(--muted);
  padding: 5px 9px;
  font-size: 12px;
}

.ratio-chip.active,
.ratio-chip:hover {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.crop-mini-preview {
  display: grid;
}

#cropPreviewCanvas {
  max-width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-2);
}

.crop-actions {
  margin-top: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  z-index: 30;
  transform: translateX(-50%) translateY(12px);
  opacity: 0;
  pointer-events: none;
  border-radius: 8px;
  padding: 10px 14px;
  background: #111827;
  color: #fff;
  font-size: 13px;
  transition: 0.2s ease;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

@media (max-width: 980px) {
  .topbar,
  .workspace,
  .single-main,
  .crop-body {
    grid-template-columns: 1fr;
  }

  .topbar {
    display: grid;
  }

  .mode-switch {
    width: 100%;
  }

  .workspace {
    display: grid;
  }

  .canvas-scroll,
  .preview-box {
    min-height: 360px;
  }

  .crop-dialog {
    width: calc(100vw - 16px);
    max-height: calc(100vh - 16px);
    margin: 8px auto;
  }

  .crop-stage {
    min-height: 300px;
  }
}

@media (max-width: 620px) {
  .app {
    padding: 10px;
  }

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

  .collage-drop-zone {
    grid-column: auto;
  }

  .thumb-item {
    grid-template-columns: 64px minmax(0, 1fr);
  }

  .thumb-item img {
    width: 64px;
    height: 64px;
  }
}
