:root {
  color-scheme: light;
  --bg: #f5f1e8;
  --panel: #fffaf1;
  --paper: #ffffff;
  --ink: #1e2525;
  --muted: #6d746f;
  --line: #d8d2c3;
  --line-strong: #b7ae9c;
  --accent: #1f7a78;
  --accent-dark: #165c5b;
  --warm: #c75f3b;
  --soft: #e7f2ee;
  --shadow: 0 18px 44px rgba(38, 45, 42, 0.12);
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: var(--bg);
  display: grid;
  grid-template-columns: 360px 1fr;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  border-radius: 6px;
  color: #ffffff;
  background: var(--accent);
  padding: 9px 14px;
  cursor: pointer;
  white-space: nowrap;
}

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

button.ghost {
  color: var(--ink);
  background: #eee6d7;
}

button.ghost:hover {
  background: #e0d6c3;
}

.library {
  height: 100vh;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
}

.library__top {
  padding: 20px 18px 12px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.library h1 {
  margin: 0;
  font-size: 23px;
  letter-spacing: 0;
}

.library p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.searchBox {
  display: grid;
  gap: 7px;
  padding: 0 18px 12px;
  color: var(--muted);
  font-size: 13px;
}

.searchBox input,
.titleFields input,
.titleFields select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  padding: 10px 11px;
  outline: none;
}

.searchBox input:focus,
.titleFields input:focus,
.titleFields select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 120, 0.15);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  padding: 0 18px 14px;
}

.chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fffdf8;
  color: var(--ink);
  padding: 7px 10px;
  font-size: 12px;
}

.chip.is-active {
  color: #ffffff;
  border-color: var(--accent);
  background: var(--accent);
}

.productList {
  overflow: auto;
  padding: 0 12px 20px;
  display: grid;
  gap: 10px;
}

.productCard {
  width: 100%;
  min-height: 104px;
  display: grid;
  grid-template-columns: 86px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 6px 16px rgba(38, 45, 42, 0.06);
}

.productCard:hover {
  border-color: var(--line-strong);
}

.productCard img {
  width: 86px;
  height: 86px;
  border-radius: 5px;
  object-fit: cover;
  background: #ddd6c8;
}

.productCard__text {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.productCard strong,
.productCard .name,
.productCard small {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.productCard strong {
  font-size: 14px;
}

.productCard .name {
  color: var(--muted);
  font-size: 13px;
}

.productCard small {
  color: var(--warm);
  font-size: 12px;
}

.productCard button {
  align-self: stretch;
  padding: 0 10px;
}

.emptyState {
  padding: 22px 12px;
  color: var(--muted);
  text-align: center;
}

.workspace {
  min-width: 0;
  height: 100vh;
  overflow: auto;
}

.toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(245, 241, 232, 0.95);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
}

.titleFields {
  display: grid;
  grid-template-columns: minmax(190px, 280px) minmax(180px, 230px) minmax(82px, 110px) minmax(82px, 110px) minmax(92px, 112px);
  gap: 12px;
  align-items: end;
}

.titleFields label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
}

.titleFields .toggleField {
  grid-template-columns: 36px auto;
  align-items: center;
  justify-content: start;
  gap: 5px 8px;
}

.toggleField span {
  grid-column: 1 / -1;
}

.toggleField input {
  width: 36px;
  height: 20px;
  margin: 0;
  accent-color: var(--accent);
}

.toggleField b {
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
}

.toolbarButtons {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

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

.importPanel {
  margin: 18px auto 0;
  width: min(100% - 36px, var(--paper-screen-w, 1500px));
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 12px 30px rgba(38, 45, 42, 0.1);
}

.importPanel__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 12px;
}

.importPanel h3 {
  margin: 0;
  font-size: 18px;
}

.importPanel p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.importPanel textarea {
  width: 100%;
  min-height: 190px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  font: 14px/1.5 "Microsoft YaHei", Consolas, monospace;
  outline: none;
}

.importPanel textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 120, 0.15);
}

.importPanel__actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}

#importStatus {
  color: var(--muted);
  font-size: 13px;
}

.pagesContainer {
  display: grid;
  gap: 22px;
  padding: 0 0 40px;
}

.printPage {
  width: min(100%, var(--paper-screen-w, 1500px));
  min-height: calc(100vh - 92px);
  margin: 0 auto;
  padding: 28px 28px 48px;
  background: var(--paper);
  box-shadow: var(--shadow);
}

body[data-paper="A4"][data-orientation="landscape"] {
  --paper-screen-w: 1122px;
}

body[data-paper="A4"][data-orientation="portrait"] {
  --paper-screen-w: 794px;
}

body[data-paper="A3"][data-orientation="landscape"] {
  --paper-screen-w: 1586px;
}

body[data-paper="A3"][data-orientation="portrait"] {
  --paper-screen-w: 1122px;
}

.printTitle {
  margin-bottom: 22px;
}

.print-only {
  display: none;
}

.pageTitleEditor {
  display: grid;
  gap: 5px;
}

.pageTitleEditor span {
  color: var(--muted);
  font-size: 12px;
}

.pageTitleEditor input {
  width: 100%;
  padding: 4px 0 7px;
  border: 0;
  border-bottom: 1px solid transparent;
  border-radius: 0;
  color: var(--ink);
  background: transparent;
  font: 700 28px/1.2 "Microsoft YaHei", sans-serif;
}

.pageTitleEditor input:hover,
.pageTitleEditor input:focus {
  border-bottom-color: var(--accent);
  outline: none;
}

.printTitle h2 {
  margin: 0;
  font-size: 28px;
  letter-spacing: 0;
}

.printTitle p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.rackBoard {
  min-height: 620px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 26px;
}

.rack {
  width: 220px;
  display: grid;
  gap: 10px;
}

.rack.is-over .rackVisual {
  outline: 3px solid rgba(31, 122, 120, 0.24);
  outline-offset: 6px;
}

.rackHeader {
  min-height: 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.rackTitle {
  min-width: 0;
}

.rackTitle strong {
  display: block;
  font-size: 15px;
}

.rackTitle span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.rackActions {
  display: flex;
  gap: 5px;
}

.rackActions button {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 5px;
  color: var(--ink);
  background: #eadfce;
}

.rackActions button:hover {
  background: #ded0bb;
}

.rackSelect {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  padding: 8px;
}

.rackSlots {
  display: grid;
  gap: 3px;
  background: #050505;
}

.rackVisual {
  display: grid;
  gap: 3px;
  overflow: hidden;
  padding: 3px;
  border: 0;
  border-radius: 2px;
  background: #050505;
}

.rackEyebrow {
  width: 100%;
  aspect-ratio: 1070 / 355;
  display: block;
  object-fit: fill;
  background: #07334b;
}

.slot {
  position: relative;
  width: var(--slot-width, 100%);
  aspect-ratio: var(--slot-aspect, 1 / 1);
  justify-self: start;
  min-height: 72px;
  overflow: hidden;
  border: 1px dashed var(--line-strong);
  border-radius: 0;
  background: rgba(255, 255, 255, 0.72);
}

.slot::before {
  content: attr(data-size);
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #9b9284;
  font-size: 13px;
  pointer-events: none;
}

.slot.is-filled {
  border-style: solid;
  border-color: transparent;
  background: transparent;
}

.slot.is-filled::before {
  content: "";
}

.slot.is-over {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 120, 0.14);
}

.tileImage {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  background: #ddd6c8;
}

.mobileUndo {
  display: none;
}

.rackProductLabels {
  display: grid;
  gap: 5px;
  margin-top: 2px;
}

.rackProductLabel {
  min-height: 36px;
  display: grid;
  align-content: center;
  gap: 1px;
  padding: 5px 8px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 5px;
}

.rackProductLabel strong,
.rackProductLabel span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rackProductLabel strong {
  font-size: 12px;
}

.rackProductLabel span {
  color: var(--muted);
  font-size: 11px;
}

.slotControls {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 5px;
}

.slotControls button {
  height: 26px;
  min-width: 26px;
  padding: 0 7px;
  border-radius: 5px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.84);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}

.slotControls button:hover {
  background: #ffffff;
}

.toast {
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 20;
  max-width: 360px;
  padding: 12px 14px;
  border-radius: 8px;
  color: #ffffff;
  background: #222c2b;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.18s ease, transform 0.18s ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 980px) {
  body {
    grid-template-columns: 1fr;
  }

  .library {
    height: auto;
    max-height: 44vh;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .workspace {
    height: auto;
  }

  .importPanel {
    width: calc(100% - 24px);
  }

  .toolbar {
    position: static;
    align-items: stretch;
    flex-direction: column;
  }

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

  .toolbarButtons {
    justify-content: flex-start;
  }

  .mobileUndo {
    position: fixed;
    right: 16px;
    bottom: 18px;
    z-index: 18;
    width: 54px;
    height: 54px;
    display: grid;
    place-items: center;
    padding: 0;
    border: 2px solid rgba(255, 255, 255, 0.88);
    border-radius: 50%;
    font-size: 28px;
    line-height: 1;
    box-shadow: 0 10px 26px rgba(23, 36, 34, 0.3);
  }

  .mobileUndo:disabled {
    cursor: not-allowed;
    opacity: 0.42;
  }
}

@media print {
  @page {
    size: A4 landscape;
    margin: 10mm;
  }

  body {
    display: block;
    background: #ffffff;
  }

  .library,
  .print-hidden,
  .rackSelect,
  .rackActions,
  .slotControls {
    display: none !important;
  }

  .print-only {
    display: block;
  }

  .workspace {
    height: auto;
    overflow: visible;
  }

  .printPage {
    width: 100%;
    min-height: auto;
    padding: 0;
    box-shadow: none;
    break-after: page;
    page-break-after: always;
  }

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

  .rackBoard {
    gap: 14px;
  }

  .rack {
    width: 172px;
    break-inside: avoid;
  }

  .rackHeader {
    min-height: 28px;
  }

  .rackTitle span {
    display: none;
  }

  .slot {
    aspect-ratio: var(--slot-aspect, 1 / 1);
    min-height: 0;
    border: 0;
  }

  .rackProductLabel {
    border-color: #d5d5d5;
  }
}
