:root {
  color-scheme: light;
  --red: #9b1c1c;
  --red-dark: #711414;
  --ink: #202124;
  --muted: #62676f;
  --line: #d9dde3;
  --paper: #f4f5f7;
  --focus: #0b66c3;
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-width: 0;
  font-family: "Microsoft YaHei UI", "PingFang SC", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}
button, input, select { font: inherit; }
button {
  min-height: 44px;
  border: 0;
  border-radius: 9px;
  padding: 10px 16px;
  color: white;
  background: var(--red);
  font-weight: 700;
  cursor: pointer;
}
button:hover { background: var(--red-dark); }
button:disabled { opacity: .55; cursor: wait; }
button.quiet { color: var(--red); background: #f7eaea; }
button.danger { background: #b00020; }
button:focus-visible, input:focus-visible, select:focus-visible, [tabindex]:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 14px max(18px, env(safe-area-inset-right)) 14px max(18px, env(safe-area-inset-left));
  color: white;
  background: linear-gradient(135deg, #741313, #ad2929);
  box-shadow: 0 3px 14px #0003;
}
.topbar h1 { margin: 0; font-size: clamp(1.25rem, 5vw, 1.8rem); }
.eyebrow { margin: 0 0 3px; font-size: .78rem; opacity: .85; }
main { width: min(1180px, 100%); margin: 0 auto; padding: 18px; }
.card {
  margin: 0 0 18px;
  padding: clamp(16px, 4vw, 26px);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: white;
  box-shadow: 0 4px 18px #28303d10;
}
.card h2 { margin-top: 0; }
.narrow { max-width: 560px; margin-inline: auto; }
.stack { display: grid; gap: 14px; }
label { display: grid; gap: 7px; font-weight: 700; }
input, select {
  min-width: 0;
  width: 100%;
  min-height: 44px;
  border: 1px solid #aeb4bd;
  border-radius: 8px;
  padding: 9px 11px;
  background: white;
}
.alert {
  position: sticky;
  top: 86px;
  z-index: 15;
  margin-bottom: 16px;
  border: 2px solid #bb1231;
  border-radius: 10px;
  padding: 14px;
  color: #710014;
  background: #ffe8ed;
}
.alert p { margin: 8px 0; }
.alert pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.notice { border-left: 5px solid #d98b00; padding: 12px; background: #fff6dc; }
.status-line { font-weight: 700; }
.status-line.offline { color: #9b1c1c; }
.progress-panel { margin-top: 16px; }
progress { width: 100%; height: 22px; accent-color: var(--red); }
.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.stamp-selection-inline {
  margin: 16px 0;
  padding: 14px;
  border: 2px solid #ead7d7;
  border-radius: 12px;
  background: #fffafa;
}
.stamp-card[aria-checked="true"] {
  outline: 3px solid var(--red);
  outline-offset: 2px;
}
.stamp-card .test-marker {
  color: #9b1c1c;
  font-weight: 800;
}
#preview-progress {
  border-left: 5px solid var(--red);
  padding: 12px 14px;
  background: #fff7f7;
}
#preview-progress progress:not([value]) {
  animation: preview-pulse 1.4s ease-in-out infinite;
}
@keyframes preview-pulse {
  0%, 100% { opacity: .55; }
  50% { opacity: 1; }
}
.stage-track {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  font-size: .78rem;
  text-align: center;
}
.stage-track span { padding: 7px 2px; border-radius: 6px; background: #eceff3; }
.stamp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-top: 12px;
}
.stamp-card {
  display: grid;
  justify-items: center;
  gap: 8px;
  min-width: 0;
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: white;
  color: var(--ink);
}
.stamp-card[aria-checked="true"] { border-color: var(--red); background: #fff4f4; }
.stamp-card img { width: 120px; height: 126px; object-fit: contain; }
.toolbar { display: flex; flex-wrap: wrap; align-items: end; gap: 10px; margin-bottom: 14px; }
.toolbar label { min-width: 150px; }
.page-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(245px, 1fr));
  gap: 14px;
  min-width: 0;
  margin: 14px 0;
}
.page-card {
  min-width: 0;
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fafafa;
}
.page-card h3 { margin: 0 0 8px; }
.page-card.has-normal { border-color: #1764c0; }
.page-card.has-stitch { box-shadow: inset 0 0 0 3px #d6921a; }
.page-canvas {
  position: relative;
  min-width: 0;
  width: 100%;
  overflow: hidden;
  border: 1px solid #aeb4bd;
  background: #e5e8ed;
  touch-action: pan-y pinch-zoom;
  aspect-ratio: 210 / 297;
  display: grid;
  place-items: center;
}
.page-card.is-positioning {
  border-color: var(--red);
  box-shadow: 0 0 0 3px #9b1c1c22;
}
.page-card.is-positioning .page-canvas {
  border: 3px solid var(--red);
  background: #fff3f3;
}
.page-card.is-positioning .stamp-marker { border-style: dashed; }
.position-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.position-actions .position-start,
.position-actions .position-remove { grid-column: 1 / -1; }
.history-filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
  gap: 10px;
  align-items: end;
}
.history-list { display: grid; gap: 12px; margin-top: 14px; }
.history-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  background: #fafafa;
}
.history-item h3 { margin: 0 0 8px; overflow-wrap: anywhere; }
.history-meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px 14px; }
.history-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.history-actions button { flex: 1 1 150px; }
.delete-dialog { max-width: 560px; padding: 20px; }
.page-canvas img {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  object-fit: contain;
}
.stamp-marker {
  position: absolute;
  width: 70px;
  aspect-ratio: 1;
  translate: -50% -50%;
  border: 3px solid #d01414;
  border-radius: 50%;
  background: #ff00001a;
  box-shadow: 0 0 0 2px white;
  touch-action: none;
}
.page-controls { display: grid; gap: 8px; margin-top: 9px; }
.page-view-button { width: 100%; }
.checkline { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.checkline input { width: 22px; min-height: 22px; }
.rotation-dial {
  position: relative;
  width: 66px;
  height: 66px;
  margin: 2px auto;
  border: 2px solid #777;
  border-radius: 50%;
  background: radial-gradient(circle, white 0 58%, #eef1f4 59%);
  touch-action: none;
}
.rotation-dial::after {
  content: "";
  position: absolute;
  left: 30px;
  top: 5px;
  width: 4px;
  height: 27px;
  border-radius: 2px;
  background: var(--red);
  transform-origin: 2px 28px;
  transform: rotate(var(--angle, 0deg));
}
.rotation-dial output { position: absolute; inset: 22px 0 auto; text-align: center; font-size: .75rem; }
.actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.download-link { display: inline-block; margin-top: 10px; font-weight: 700; color: #0759a5; }
#stitch-preview img { display: block; max-width: 100%; margin: 12px auto; }
#stitch-preview::before {
  content: "骑缝章整章重组示意（不是单页结果）";
  display: block;
  margin-top: 14px;
  font-weight: 800;
}
.page-preview-frame {
  width: 100%;
  max-width: 100%;
  aspect-ratio: 210 / 297;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid #aeb4bd;
  background: #e5e8ed;
}
.page-preview-frame img {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.page-card img, #stitch-preview img { cursor: zoom-in; }
#preview-viewer {
  width: 100vw;
  max-width: none;
  height: 100dvh;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: #101114;
  color: white;
}
#preview-viewer::backdrop { background: #000d; }
.viewer-shell { display: grid; grid-template-rows: auto auto 1fr auto; height: 100%; min-width: 0; }
.viewer-toolbar {
  grid-row: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
  background: #1c1f24;
}
#viewer-composite-note { grid-row: 2; }
.viewer-toolbar button { min-height: 40px; padding: 7px 10px; }
.viewer-toolbar .quiet { color: white; background: #3a3f47; }
#viewer-page-state { margin-right: auto; }
.viewer-stage {
  grid-row: 3;
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  touch-action: none;
}
.viewer-stage img {
  position: absolute;
  left: 50%;
  top: 50%;
  display: block;
  max-width: none;
  max-height: none;
  transform-origin: center;
  user-select: none;
  -webkit-user-drag: none;
  will-change: transform;
}
.viewer-nav {
  grid-row: 4;
  display: flex;
  gap: 10px;
  justify-content: center;
  padding: 8px 10px max(8px, env(safe-area-inset-bottom));
  background: #1c1f24;
}
.viewer-nav button { flex: 1; max-width: 220px; }
#viewer-composite-note { margin: 8px 10px; color: #332000; }
footer { padding: 24px 18px calc(24px + env(safe-area-inset-bottom)); text-align: center; color: var(--muted); }
@media (max-width: 520px) {
  main { padding: 10px; overflow-x: hidden; }
  .topbar { position: relative; }
  .alert { top: 8px; }
  .page-grid { grid-template-columns: minmax(0, 1fr); }
  .card { padding: 14px; border-radius: 10px; }
  .stage-track { grid-template-columns: 1fr; text-align: left; }
  .toolbar > * { width: 100%; }
  .actions button { width: 100%; }
  .history-filters { grid-template-columns: minmax(0, 1fr); }
  .history-actions { display: grid; grid-template-columns: minmax(0, 1fr); }
  .viewer-toolbar { font-size: .82rem; }
  .viewer-toolbar button { flex: 1 1 auto; }
}
