:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f1f2f5;
  --line: #e3e5ea;
  --text: #1d1f24;
  --text-dim: #6b7078;
  --accent: #2f6fed;
  --accent-dim: #d7e3fd;
  --danger: #e0453b;
  --shadow: 0 1px 2px rgba(20, 20, 30, 0.04), 0 8px 24px rgba(20, 20, 30, 0.06);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  min-height: 100vh;
  padding: 32px 24px;
}

.page-header {
  max-width: 1420px;
  margin: 0 auto 24px;
  text-align: center;
}

h1 {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 6px;
  letter-spacing: -0.2px;
}

.sub {
  color: var(--text-dim);
  font-size: 13.5px;
  margin: 0 auto;
  max-width: 640px;
  line-height: 1.55;
}

.layout {
  max-width: 1420px;
  margin: 0 auto;
  display: flex;
  align-items: flex-start;
  gap: 24px;
}

.stage {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1 1 auto;
  min-width: 0;
}

.stage-top {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  min-height: 20px;
}

.hint {
  color: var(--text-dim);
  font-size: 12.5px;
}

.canvas-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    linear-gradient(45deg, #eceef2 25%, transparent 25%),
    linear-gradient(-45deg, #eceef2 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #eceef2 75%),
    linear-gradient(-45deg, transparent 75%, #eceef2 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0;
  background-color: #fafbfc;
  border: 1px solid var(--line);
  border-radius: 10px;
  min-height: 320px;
  overflow: hidden;
}

.canvas-wrap.empty {
  cursor: pointer;
  background:
    radial-gradient(circle at 50% 38%, #eef2fb 0%, #f6f8fc 55%, #fafbfd 100%);
  border: 1.5px dashed #c7cede;
  transition: border-color .18s, background .18s;
}

.canvas-wrap.empty:hover {
  border-color: var(--accent);
  background:
    radial-gradient(circle at 50% 38%, #e7edfd 0%, #f4f7fd 55%, #fafbfd 100%);
}

.canvas-wrap.empty canvas {
  display: none;
}

.upload-placeholder {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 0 24px;
  pointer-events: none;
}

.canvas-wrap.empty .upload-placeholder {
  display: flex;
}

.upload-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  margin-bottom: 10px;
  border-radius: 16px;
  background: linear-gradient(155deg, #eef3ff, #e2eaff);
  color: var(--accent);
  box-shadow: 0 1px 2px rgba(20, 20, 30, 0.05), 0 6px 16px rgba(47, 111, 237, 0.14);
  transition: transform .18s ease;
}

.canvas-wrap.empty:hover .upload-icon {
  transform: translateY(-2px) scale(1.04);
}

.upload-title {
  margin: 0;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text);
}

.upload-sub {
  margin: 0;
  font-size: 12.5px;
  color: var(--text-dim);
}

canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

.crop-overlay {
  position: absolute;
  pointer-events: none;
  display: none;
  touch-action: none;
}

.quad-dim {
  fill: #000;
  fill-opacity: 0.45;
  fill-rule: evenodd;
}

.quad-outline {
  fill: none;
  stroke: #fff;
  stroke-opacity: 0.9;
  stroke-width: 3.5;
  stroke-linejoin: round;
}

.quad-outline-inner {
  fill: none;
  stroke: var(--accent);
  stroke-width: 1.6;
  stroke-linejoin: round;
}

.handle-hit {
  fill: transparent;
  pointer-events: all;
  cursor: grab;
  touch-action: none;
}

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

.handle-dot {
  fill: #fff;
  stroke: var(--accent);
  stroke-width: 2;
  pointer-events: none;
}

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

button {
  font-family: inherit;
  font-size: 13.5px;
  padding: 9px 15px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .05s;
}

button:hover:not(:disabled) {
  border-color: #c7cbd4;
  background: #e9ebef;
}

button:active:not(:disabled) {
  transform: scale(0.98);
}

button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

button.primary:hover:not(:disabled) {
  background: #2760cf;
  border-color: #2760cf;
}

button.ghost {
  background: transparent;
}

button.ghost:hover:not(:disabled) {
  background: var(--panel-2);
}

button.danger {
  color: var(--danger);
  border-color: #f3c9c6;
}

#cancelCropBtn {
  color: var(--danger);
  border-color: #f3c9c6;
}

#cancelCropBtn:hover:not(:disabled) {
  background: #fbeae9;
}

.compare-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
}

.compare-btn svg {
  flex: none;
  opacity: 0.85;
}

.compare-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #1a4dbf;
}

.compare-btn.active svg {
  opacity: 1;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 16px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  position: sticky;
  top: 24px;
  width: 300px;
  flex: none;
  max-height: calc(100vh - 48px);
  overflow-y: auto;
}

.panel-drag {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 -4px;
  padding: 2px 4px 12px;
  border-bottom: 1px solid var(--line);
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  user-select: none;
}

.panel-drag .dots {
  display: none;
}

@media (max-width: 820px) {
  .layout {
    flex-direction: column;
  }

  .panel {
    position: static;
    width: 100%;
    max-height: none;
    margin-top: 20px;
  }
}

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

.field-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 13px;
}

.field-top span:first-child {
  color: var(--text);
}

.field-top span:last-child {
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.field-val {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.reset-icon {
  display: none;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
}

.reset-icon.visible {
  display: inline-flex;
}

.reset-icon:hover {
  background: var(--accent-dim);
}

.reset-icon:active {
  transform: scale(0.92);
}

.accordion-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: transparent;
  border: none;
  padding: 2px 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-dim);
  font-weight: 700;
  cursor: pointer;
  border-radius: 6px;
}

.accordion-toggle:hover {
  color: var(--text);
}

.accordion-toggle .chevron {
  flex: none;
  color: var(--text-dim);
  transition: transform .18s ease;
}

.accordion-toggle[aria-expanded="true"] .chevron {
  transform: rotate(180deg);
}

.accordion-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height .22s ease;
}

.accordion-body-inner {
  padding-bottom: 2px;
}

input[type=range] {
  -webkit-appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  outline: none;
}

input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line);
}

input[type=range]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px var(--line);
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13.5px;
}

.switch {
  position: relative;
  width: 38px;
  height: 22px;
  flex: none;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.switch .track {
  position: absolute;
  inset: 0;
  background: var(--line);
  border-radius: 12px;
  transition: background .15s;
}

.switch .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform .15s;
}

.switch input:checked+.track {
  background: var(--accent);
}

.switch input:checked+.track .knob {
  transform: translateX(16px);
}

.divider {
  height: 1px;
  background: var(--line);
  margin: 2px 0;
}

.panel h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-dim);
  font-weight: 700;
  margin: 0;
}

.download-row {
  display: flex;
  gap: 8px;
}

.download-row button {
  flex: 1;
}

footer {
  max-width: 1100px;
  margin: 22px auto 0;
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1.6;
}

/* ---- Pages strip ---- */

.pages-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.page-thumb,
.add-page-tile {
  width: 56px;
  height: 72px;
  border-radius: 8px;
  flex: none;
}

.page-thumb {
  position: relative;
  border: 2px solid var(--line);
  overflow: hidden;
  cursor: pointer;
  background: var(--panel-2);
}

.page-thumb.reorderable {
  cursor: grab;
}

.page-thumb.dragging {
  opacity: 0.4;
}

.page-thumb.drag-over {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim) inset;
}

.page-grip {
  position: absolute;
  left: 2px;
  top: 2px;
  width: 15px;
  height: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  background: rgba(20, 20, 30, 0.5);
  color: #fff;
}

.page-thumb.active {
  border-color: var(--accent);
}

.page-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.page-thumb .page-num {
  position: absolute;
  left: 3px;
  bottom: 2px;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 4px;
  padding: 0 4px;
  line-height: 1.4;
}

.page-thumb .page-del {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 16px;
  height: 16px;
  padding: 0;
  line-height: 1;
  font-size: 12px;
  border: none;
  border-radius: 50%;
  background: rgba(20, 20, 30, 0.55);
  color: #fff;
}

.page-thumb .page-del:hover {
  background: var(--danger);
}

.add-page-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  border: 1.5px dashed #c7cede;
  background: transparent;
  color: var(--text-dim);
  font-size: 10.5px;
}

.add-page-tile:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

/* ---- PDF arrangement modal ---- */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 30, 0.5);
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 100;
}

.modal-overlay.open {
  display: flex;
}

.modal {
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
  width: 96vw;
  max-width: 1600px;
  max-height: calc(100vh - 28px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal.modal-small {
  max-width: 380px;
}

.modal.modal-small .modal-hint {
  margin: 14px 20px 4px;
}

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

.modal-header h2 {
  font-size: 15px;
  margin: 0;
}

.modal-close {
  border: none;
  background: transparent;
  padding: 4px;
  color: var(--text-dim);
}

.modal-close:hover {
  color: var(--text);
  background: var(--panel-2);
}

.modal-hint {
  margin: 12px 20px 0;
  color: var(--text-dim);
  font-size: 12.5px;
  line-height: 1.5;
}

.modal-body {
  padding: 14px 20px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.out-page {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px;
}

.out-page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.out-page-title {
  font-size: 13px;
  font-weight: 700;
}

.out-page-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12.5px;
  color: var(--text-dim);
}

.out-page-controls select {
  font-family: inherit;
  font-size: 12.5px;
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
}

.out-margin-label {
  display: flex;
  align-items: center;
  gap: 6px;
}

.out-margin-label input[type=range] {
  width: 80px;
}

.out-page-main {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  flex-wrap: wrap;
}

.out-preview {
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #fff;
  touch-action: none;
  cursor: grab;
  flex: none;
}

.out-preview:active {
  cursor: grabbing;
}

.out-page-side {
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex: 0 0 300px;
  min-width: 260px;
}

.out-slot-label {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.out-slot-scale-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.out-slot-scale-row input[type=range] {
  flex: 1;
}

.out-slot-scale-row span {
  font-size: 12px;
  color: var(--text-dim);
  width: 36px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.out-slot-btn-row {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
}

.out-slot-btn-row button {
  flex: 1 1 0;
  min-width: 0;
  font-size: 11px;
  padding: 6px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.out-page-side>button {
  align-self: flex-start;
  font-size: 12px;
}

.out-page-sep {
  display: flex;
  justify-content: center;
}

.out-page-sep button {
  font-size: 12px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--line);
}