:root {
  color-scheme: light;
  --ink: #16211f;
  --muted: #66706d;
  --line: #d9dfdc;
  --panel: #f7f8f7;
  --paper: #ffffff;
  --accent: #146c63;
  --accent-strong: #0c514b;
  --accent-soft: #dcefeb;
  --warn: #b8582b;
  --shadow: 0 18px 55px rgba(38, 48, 45, 0.14);
  --paper-width: 210mm;
  --paper-height: 297mm;
  --page-margin: 10mm;
  --page-gap: 8mm;
  --caption-height: 5mm;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #e9eeec;
  color: var(--ink);
  font-family:
    "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", system-ui,
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

button,
select {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

button {
  min-height: 40px;
  padding: 0 14px;
  cursor: pointer;
}

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

button:hover:not(:disabled),
select:hover {
  border-color: #aab6b2;
}

.app-shell {
  display: grid;
  grid-template-columns: 330px minmax(0, 1fr);
  min-height: 100vh;
}

.panel {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 22px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 21px;
}

h1,
h2,
p {
  margin: 0;
}

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

.brand p,
.workspace-top p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.drop-zone {
  display: grid;
  place-items: center;
  min-height: 150px;
  border: 1px dashed #91a39e;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  text-align: center;
  padding: 18px;
  transition:
    border-color 150ms ease,
    background 150ms ease,
    transform 150ms ease;
}

.drop-zone:focus-visible {
  outline: 3px solid rgba(20, 108, 99, 0.28);
  outline-offset: 3px;
}

.drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--accent-soft);
  transform: translateY(-1px);
}

.drop-zone input {
  position: absolute;
  width: 1px;
  height: 1px;
  clip: rect(0 0 0 0);
  opacity: 0;
  pointer-events: none;
}

.drop-icon {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 30px;
  line-height: 1;
  margin-bottom: 8px;
}

.drop-zone small {
  margin-top: 4px;
  color: var(--muted);
}

.cancel-load {
  width: 100%;
  margin-top: 10px;
  border-color: #d7a58e;
  background: #fff7f2;
  color: var(--warn);
  font-weight: 700;
}

.toolbar {
  display: grid;
  grid-template-columns: 0.85fr 1.25fr 0.75fr;
  gap: 10px;
  margin: 16px 0 22px;
}

.toolbar button {
  min-width: 0;
  padding: 0 8px;
  white-space: nowrap;
}

.save-pdf {
  border-color: #86a9a3;
  color: var(--accent-strong);
  font-weight: 700;
}

.save-pdf:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.notice {
  margin: -8px 0 16px;
  border: 1px solid #bed8d2;
  border-radius: 8px;
  background: #edf7f5;
  padding: 9px 11px;
  color: var(--accent-strong);
  font-size: 12px;
  line-height: 1.45;
}

.notice.error {
  border-color: #e0b9a7;
  background: #fff5f0;
  color: #8f3c1e;
}

.notice.warning {
  border-color: #d8c892;
  background: #fff9e8;
  color: #7a5a12;
}

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

.primary:hover:not(:disabled) {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
}

.control-group,
.file-list-wrap {
  border-top: 1px solid var(--line);
  padding-top: 18px;
  margin-top: 18px;
}

.control-group h2,
.file-list-wrap h2 {
  margin-bottom: 12px;
  font-size: 14px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #edf1ef;
}

.segmented button {
  border-color: transparent;
  background: transparent;
}

.segmented button.active {
  background: #fff;
  color: var(--accent-strong);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.08);
  font-weight: 700;
}

select {
  width: 100%;
  min-height: 40px;
  padding: 0 12px;
}

.slider-row {
  display: grid;
  grid-template-columns: 68px 1fr 34px;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  color: var(--muted);
  font-size: 13px;
}

.slider-row input {
  width: 100%;
  accent-color: var(--accent);
}

.slider-row output {
  color: var(--ink);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 38px;
  color: var(--ink);
  font-size: 13px;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
}

.file-list {
  display: grid;
  gap: 8px;
  padding: 0;
  margin: 0;
  list-style: none;
}

.file-list li {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr) auto;
  align-items: center;
  gap: 5px;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 5px;
  transition: border-color 120ms ease, box-shadow 120ms ease, opacity 120ms ease;
}

.file-list li.dragging {
  opacity: 0.42;
}

.file-list li.drag-before {
  box-shadow: 0 -3px 0 var(--accent);
}

.file-list li.drag-after {
  box-shadow: 0 3px 0 var(--accent);
}

.reorder-handle {
  overflow: hidden;
  color: #94a19d;
  cursor: grab;
  font-size: 14px;
  letter-spacing: 0;
  white-space: nowrap;
}

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

.file-preview-trigger {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  min-width: 0;
  min-height: 32px;
  border: 0;
  padding: 0;
  text-align: left;
}

.file-preview-trigger:hover,
.file-preview-trigger:focus-visible {
  border-color: transparent !important;
  color: var(--accent-strong);
  outline: none;
}

.file-preview-trigger:focus-visible .file-index {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.file-index {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: #edf1ef;
  color: var(--muted);
  font-size: 12px;
}

.file-name {
  overflow: hidden;
  color: var(--ink);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-actions {
  display: grid;
  grid-template-columns: repeat(3, 26px);
  gap: 1px;
}

.move-file,
.remove-file {
  display: grid;
  place-items: center;
  min-height: 26px;
  width: 26px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
}

.move-file:hover:not(:disabled) {
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.remove-file {
  font-size: 18px;
}

.remove-file:hover {
  background: #f2dfd6;
  color: var(--warn);
}

.preview-dialog {
  width: min(920px, calc(100vw - 32px));
  max-width: none;
  max-height: calc(100vh - 32px);
  overflow: hidden;
  border: 0;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0;
  box-shadow: 0 24px 80px rgba(18, 28, 25, 0.34);
}

.preview-dialog::backdrop {
  background: rgba(18, 28, 25, 0.62);
}

.preview-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  min-height: 64px;
  border-bottom: 1px solid var(--line);
  padding: 13px 16px;
}

.preview-header > div {
  min-width: 0;
}

.preview-position {
  display: block;
  margin-bottom: 3px;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 700;
}

.preview-header h2 {
  overflow: hidden;
  font-size: 16px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-close {
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 36px;
  min-height: 36px;
  border: 0;
  padding: 0;
  color: var(--muted);
  font-size: 25px;
}

.preview-close:hover {
  background: #edf1ef;
  color: var(--ink);
}

.preview-body {
  display: grid;
  place-items: center;
  height: min(68vh, 680px);
  overflow: auto;
  background: #e9eeec;
  padding: 18px;
}

.preview-body img {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  border: 1px solid #cbd4d1;
  background: #fff;
  box-shadow: 0 8px 28px rgba(38, 48, 45, 0.14);
  object-fit: contain;
}

.preview-footer {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  min-height: 58px;
  border-top: 1px solid var(--line);
  padding: 9px 16px;
}

.preview-footer button:first-child {
  justify-self: start;
}

.preview-footer button:last-child {
  justify-self: end;
}

.preview-footer span {
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.confirm-dialog {
  width: min(410px, calc(100vw - 32px));
  max-width: none;
  overflow: hidden;
  border: 0;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0;
  box-shadow: 0 24px 80px rgba(18, 28, 25, 0.34);
}

.confirm-dialog::backdrop {
  background: rgba(18, 28, 25, 0.62);
}

.confirm-content {
  padding: 20px;
}

.confirm-content h2 {
  margin-bottom: 8px;
  font-size: 18px;
}

.confirm-content p {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

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

.danger {
  border-color: #b8582b;
  background: #b8582b;
  color: #fff;
  font-weight: 700;
}

.workspace {
  min-width: 0;
  padding: 26px;
}

.workspace-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1200px;
  margin: 0 auto 22px;
}

.workspace-top h2 {
  font-size: 22px;
}

.status-pill {
  min-width: 60px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  padding: 7px 12px;
  color: var(--muted);
  text-align: center;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: calc(100vh - 120px);
}

.empty-page {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  width: min(62vw, 430px);
  aspect-ratio: 210 / 297;
  border: 1px solid #d5dcda;
  background: #fff;
  padding: 32px;
  box-shadow: var(--shadow);
}

.empty-page span {
  border: 1px dashed #bdc8c5;
  border-radius: 6px;
  background:
    linear-gradient(#edf1ef 0 0) 18px 20px / 48% 8px no-repeat,
    linear-gradient(#edf1ef 0 0) 18px 40px / 68% 8px no-repeat,
    #fafbfb;
}

.sheets {
  display: grid;
  justify-items: center;
  gap: 28px;
}

.sheet {
  width: min(100%, var(--paper-width));
  aspect-ratio: 210 / 297;
  content-visibility: auto;
  background: var(--paper);
  box-shadow: var(--shadow);
  padding: var(--page-margin);
}

.sheet.landscape {
  width: min(100%, var(--paper-height));
  aspect-ratio: 297 / 210;
}

.sheet-grid {
  display: grid;
  width: 100%;
  height: 100%;
  gap: var(--page-gap);
}

.invoice-slot {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  margin: 0;
  overflow: visible;
  border: 1px solid #cbd4d1;
  background: #fff;
}

.invoice-slot img {
  display: block;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.invoice-slot figcaption {
  display: flex;
  flex: 0 0 var(--caption-height);
  align-items: center;
  height: var(--caption-height);
  overflow: hidden;
  border-top: 1px solid #e2e7e5;
  padding: 3px 6px;
  color: #66706d;
  font-size: 10px;
  line-height: 1.3;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.invoice-slot.hide-caption figcaption {
  display: none;
}

.loading {
  opacity: 0.65;
  cursor: progress;
  pointer-events: none;
}

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

  .panel {
    position: relative;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .workspace {
    padding: 18px 12px;
  }

  .workspace-top {
    align-items: flex-start;
  }
}

@media (max-width: 520px) {
  .panel {
    padding: 16px 14px 18px;
  }

  .brand {
    gap: 10px;
    margin-bottom: 16px;
  }

  .mark {
    width: 40px;
    height: 40px;
    font-size: 19px;
  }

  h1 {
    font-size: 18px;
  }

  .brand p {
    font-size: 12px;
  }

  .drop-zone {
    min-height: 122px;
    padding: 14px;
  }

  .drop-icon {
    width: 38px;
    height: 38px;
    margin-bottom: 6px;
    font-size: 27px;
  }

  .toolbar {
    margin: 12px 0 16px;
  }

  .control-group,
  .file-list-wrap {
    margin-top: 14px;
    padding-top: 14px;
  }

  .slider-row {
    grid-template-columns: 56px 1fr 28px;
    gap: 8px;
  }

  .slider-row output,
  .toggle-row {
    padding-right: 2px;
  }

  .file-list-wrap {
    max-height: min(42vh, 310px);
    overflow: auto;
    padding-bottom: 2px;
  }

  .workspace {
    padding: 16px 12px 30px;
  }

  .workspace-top {
    margin-bottom: 14px;
  }

  .workspace-top h2 {
    font-size: 20px;
  }

  .workspace-top p {
    font-size: 12px;
  }

  .status-pill {
    min-width: 52px;
    padding: 6px 10px;
  }

  .empty-state {
    min-height: 38vh;
  }

  .empty-page {
    width: min(100%, 270px);
    gap: 12px;
    padding: 22px;
  }

  .sheets {
    gap: 16px;
  }

  .sheet {
    box-shadow: 0 10px 28px rgba(38, 48, 45, 0.12);
  }

  .preview-dialog {
    width: calc(100vw - 20px);
    max-height: calc(100dvh - 20px);
  }

  .confirm-dialog {
    width: calc(100vw - 20px);
  }

  .preview-header {
    min-height: 58px;
    padding: 10px 12px;
  }

  .preview-header h2 {
    font-size: 14px;
  }

  .preview-body {
    height: min(60dvh, 560px);
    padding: 10px;
  }

  .preview-footer {
    min-height: 52px;
    gap: 6px;
    padding: 6px 10px;
  }

  .preview-footer button {
    min-height: 36px;
    padding: 0 9px;
    font-size: 13px;
  }
}

@page {
  size: A4 portrait;
  margin: 0;
}

@media print {
  :root {
    --paper-width: 210mm;
    --paper-height: 297mm;
  }

  body {
    background: #fff;
  }

  .panel,
  .workspace-top,
  .empty-state,
  .preview-dialog,
  .confirm-dialog {
    display: none !important;
  }

  .app-shell,
  .workspace,
  .sheets {
    display: block;
    min-height: 0;
    padding: 0;
    margin: 0;
  }

  .sheet,
  .sheet.landscape {
    width: var(--paper-width);
    height: var(--paper-height);
    aspect-ratio: auto;
    box-shadow: none;
    content-visibility: visible;
    break-after: page;
    page-break-after: always;
  }

  .sheet.landscape {
    width: var(--paper-height);
    height: var(--paper-width);
  }

  .sheet:last-child {
    break-after: auto;
    page-break-after: auto;
  }

  .invoice-slot {
    break-inside: avoid;
  }
}
