:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #d8dbe0;
  --text: #1c2024;
  --muted: #6b7280;
  --accent: #2563eb;
  --danger: #b42318;
  --warn: #92400e;
  --warn-bg: #fef3c7;
  --radius: 6px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.45 "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}

h1 { font-size: 17px; margin: 0; }
h2 { font-size: 13px; margin: 0 0 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-stats {
  display: flex;
  gap: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.topbar-actions { margin-left: auto; display: flex; gap: 8px; }

/* ---------- controls ---------- */

button {
  font: inherit;
  padding: 7px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
button:hover { background: #eef0f3; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #1d4ed8; }
button.subtle { color: var(--muted); }
button:disabled { opacity: .5; cursor: not-allowed; }

.layout { display: flex; align-items: flex-start; gap: 18px; padding: 18px; }

.panel {
  flex: 0 0 288px;
  position: sticky;
  top: 71px;
  max-height: calc(100vh - 90px);
  overflow-y: auto;
}

.group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.field { display: block; margin-bottom: 10px; }
.field > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.field.inline { display: flex; align-items: center; gap: 6px; margin: 0; }
.field.inline > span { margin: 0; white-space: nowrap; }

input[type="number"], input[type="text"], select {
  width: 100%;
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
input[type="color"] { width: 100%; height: 32px; padding: 2px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
input[type="range"] { width: 100%; }
.field.inline input[type="number"] { width: 68px; }

.row { display: flex; gap: 8px; }
.row > .field { flex: 1; }

.check { display: flex; align-items: center; gap: 7px; margin-bottom: 8px; cursor: pointer; }
.check input { margin: 0; }

.hint { color: var(--muted); font-size: 12px; margin: 4px 0; }
.centered { text-align: center; padding: 24px; }

.main { flex: 1; min-width: 0; }

/* ---------- dropzone ---------- */

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 26px;
  text-align: center;
  margin-bottom: 14px;
}
.dropzone p { margin: 4px 0; }
.dropzone.dragover { border-color: var(--accent); background: #eff4ff; }

.error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--danger);
  border-radius: var(--radius);
  padding: 10px 13px;
  margin-bottom: 14px;
}

.warnings {
  background: var(--warn-bg);
  border: 1px solid #fcd34d;
  color: var(--warn);
  border-radius: var(--radius);
  padding: 10px 13px;
  margin-bottom: 14px;
}
.warnings ul { margin: 0; padding-left: 18px; }
.warnings li + li { margin-top: 4px; }

/* ---------- image list ---------- */

.items {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 18px;
}
.items-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 10px; }
.items-head h2 { margin: 0; }
.items-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.item-list { list-style: none; margin: 0; padding: 0; }

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 7px;
  background: var(--surface);
}
.item.dragging { opacity: .4; }
.item.drop-target { border-color: var(--accent); border-style: dashed; }

.item-handle { cursor: grab; color: var(--muted); padding: 0 3px; user-select: none; }
.item-thumb {
  width: 62px; height: 40px;
  object-fit: contain;
  background: #f8f9fa;
  border: 1px solid var(--border);
  border-radius: 3px;
  flex: 0 0 auto;
}
.item-info { flex: 1 1 auto; min-width: 0; }
.item-name,
.item-meta { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-name { font-weight: 600; }
.item-meta { font-size: 12px; color: var(--muted); }

.badge-warn {
  display: inline-block;
  background: var(--warn-bg);
  color: var(--warn);
  border-radius: 3px;
  padding: 1px 6px;
  font-size: 11px;
  margin-left: 6px;
}

.item-copies { flex: 0 0 74px; width: 74px; }
.item-remove { flex: 0 0 auto; white-space: nowrap; }

/* ---------- sheet preview ---------- */

.preview-head { margin-bottom: 10px; }

/* Sheets are sized in real mm, so a large paper choice can exceed the pane.
   Scroll inside the preview rather than letting the page scroll sideways. */
.sheets {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  overflow-x: auto;
  padding-top: 24px;  /* room for the "Sheet n of m" caption */
}

.sheet {
  position: relative;
  background: #fff;
  box-shadow: 0 1px 5px rgba(0, 0, 0, .18);
  overflow: hidden;
  flex: 0 0 auto;
}

.sheet-label {
  position: absolute;
  top: -22px;
  left: 0;
  font-size: 12px;
  color: var(--muted);
}

.marks-layer { position: absolute; inset: 0; }
.mark-line { position: absolute; }
.mark-rect { position: absolute; border-style: solid; }

.label-img { position: absolute; display: block; }

/* ---------- print ----------
   Page containers are already sized in mm, so zeroing the @page margin makes
   the browser's output match the exported PDF exactly. Users must still print
   at 100% / "Actual size" rather than "Fit to page". */

@media print {
  .topbar, .panel, .dropzone, .items, .preview-head, .error, .warnings,
  .sheet-label, #empty-preview { display: none !important; }

  html, body { background: #fff; margin: 0; padding: 0; }
  .layout { display: block; padding: 0; gap: 0; }
  .main { width: 100%; }
  .sheets { display: block; gap: 0; padding: 0; overflow: visible; }

  /* The @page size rule is injected at runtime by render-dom.js, since it has
     to track the user's chosen paper size. */

  .sheet {
    box-shadow: none;
    break-after: page;
    page-break-after: always;
    break-inside: avoid;
    margin: 0;
    /* A sheet is exactly one page tall, so sub-pixel rounding could otherwise
       spill a blank page after each one. */
    overflow: hidden;
  }
  .sheet:last-child { break-after: auto; page-break-after: auto; }

  /* Cut marks are light greys that browsers would otherwise drop. */
  .marks-layer, .mark-line, .mark-rect, .label-img {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}
