:root {
  --panel: #f6f5f1;
  --panel-2: #ecebe5;
  --ink: #1b1b19;
  --ink-2: #4a4944;
  --muted: #7d7b73;
  --line: #dcd9cf;
  --field: #ffffff;
  --accent: #e8572a;
  --ok: #3f8f5b;
  --warn: #c98a12;
  --bad: #d0402a;
  --mat: #27322c;
  --mat-grid: rgba(255, 255, 255, 0.045);
  --mat-grid-major: rgba(255, 255, 255, 0.08);
  --mat-ink: #d9e0da;
  --mat-muted: #8c9a91;
  --radius: 5px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --panel: #1d1e1c;
    --panel-2: #262724;
    --ink: #ecebe6;
    --ink-2: #c3c1b9;
    --muted: #8d8b83;
    --line: #34352f;
    --field: #141513;
    --mat: #1a221e;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--panel);
  color: var(--ink);
  font: 13px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
input[type=number] { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.app {
  display: grid;
  grid-template-columns: 312px 1fr;
  height: 100vh;
}

/* ---------- panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  min-width: 0; /* long printer names ellipsize instead of widening the column on phones */
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 16px 18px 14px;
  border-bottom: 1px solid var(--line);
}
.brand-mark {
  width: 26px; height: 26px; flex: none;
  fill: none; stroke: var(--ink); stroke-width: 1.4;
}
.brand-mark rect { fill: var(--accent); stroke: none; }
.brand-text { flex: 1; min-width: 0; }
.seg-sm button { padding: 3px 8px; font-size: 11.5px; }
.help-btn {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: transparent;
  color: var(--ink-2);
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
}
.help-btn:hover { border-color: var(--ink-2); color: var(--ink); }
.brand-name { margin: 0; font-weight: 650; font-size: 15px; letter-spacing: -0.01em; }
.noscript { margin: 0; padding: 12px 18px; background: var(--accent); color: #fff; }
.brand-sub { color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.panel-body { flex: 1; overflow-y: auto; }

.group {
  padding: 14px 18px 16px;
  border-bottom: 1px solid var(--line);
}
.group h2 {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.01em;
}
.group h3 {
  margin: 16px 0 7px;
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-2);
}

.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.45;
}

/* file */
.file-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  padding: 10px 12px;
  border: 1px dashed var(--muted);
  border-radius: var(--radius);
  background: transparent;
  text-align: left;
}
.file-btn:hover { border-color: var(--ink); background: var(--panel-2); }
.file-btn.has-file { border-style: solid; border-color: var(--line); background: var(--field); }
.file-name { font-weight: 550; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta { color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }

.file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--field);
}
.file-card-text { display: flex; flex-direction: column; gap: 2px; flex: 1; min-width: 0; }
.file-actions { display: flex; gap: 2px; flex: none; }
.link-btn {
  padding: 4px 7px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--ink-2);
  font-size: 12px;
}
.link-btn:hover { background: var(--panel-2); color: var(--ink); }
.link-danger:hover { color: var(--bad); }

/* segmented */
.seg {
  display: flex;
  padding: 2px;
  border-radius: var(--radius);
  background: var(--panel-2);
  border: 1px solid var(--line);
}
.seg button {
  flex: 1;
  padding: 5px 8px;
  border: 0;
  border-radius: 3px;
  background: transparent;
  color: var(--ink-2);
  white-space: nowrap;
}
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed=true] {
  background: var(--field);
  color: var(--ink);
  font-weight: 550;
  box-shadow: 0 0 0 1px var(--line), 0 1px 2px rgba(0, 0, 0, 0.06);
}

.seg-dark { background: rgba(255, 255, 255, 0.06); border-color: rgba(255, 255, 255, 0.1); }
.seg-dark button { color: var(--mat-muted); padding: 4px 14px; }
.seg-dark button:hover { color: var(--mat-ink); }
.seg-dark button[aria-pressed=true] { background: rgba(255, 255, 255, 0.14); color: #fff; box-shadow: none; }

/* fields */
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.field {
  position: relative;
  min-width: 0;
  display: flex;
  align-items: center;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--field);
}
.field:focus-within { border-color: var(--ink-2); }
.field > span {
  padding-left: 9px;
  color: var(--muted);
  font-size: 11.5px;
  white-space: nowrap;
}
.field input {
  flex: 1;
  width: 100%;
  min-width: 0;
  height: 100%;
  padding: 0 4px 0 8px;
  border: 0;
  background: transparent;
  text-align: right;
  outline: none;
}
.field em { padding-right: 9px; color: var(--muted); font-style: normal; font-size: 11.5px; }
.field input::-webkit-inner-spin-button,
.field input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.field input[type=number] { -moz-appearance: textfield; }
.field.invalid { border-color: var(--bad); }
.field-inline { margin-top: 10px; }
.select-field select {
  flex: 1;
  min-width: 0;
  height: 100%;
  padding: 0 9px 0 8px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  text-align: right;
  text-align-last: right;
  outline: none;
  cursor: pointer;
}
.toggle-crop { margin-top: 12px; }
/* a hint continued below its toggle, past the 30px track and 10px gap */
.toggle-extra { margin: 2px 0 0 40px; color: var(--muted); font-size: 11.5px; line-height: 1.4; }
.toggle-extra small { display: block; margin-bottom: 2px; font-size: inherit; }
.toggle-extra .inline-link { color: var(--ink-2); }
.row-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.row-head .link-btn { margin-right: -7px; }
.row-head .link-btn:disabled { opacity: 0.4; cursor: default; background: transparent; }
.hint-tight { margin-top: 0; }
.calib-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-top: 20px !important;
  padding-top: 14px;
  border-top: 1px dashed var(--line);
}

.chips { margin-top: 12px; }
.chips-label { display: block; margin-bottom: 6px; color: var(--muted); font-size: 11.5px; }
.chips-row { display: flex; gap: 4px; }
.chips-row button {
  flex: 1;
  height: 26px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: transparent;
  font-variant-numeric: tabular-nums;
}
.chips-row button:hover:not(:disabled) { border-color: var(--ink-2); }
.chips-row button[aria-pressed=true] { background: var(--ink); border-color: var(--ink); color: var(--panel); }

.btn-row { display: flex; gap: 8px; margin-top: 8px; }
.btn {
  flex: 1;
  height: 30px;
  /* outlined on the panel, unlike the white fields next to it */
  border: 1px solid var(--ink-2);
  border-radius: var(--radius);
  background: transparent;
  font-weight: 500;
}
.btn:disabled, .chips-row button:disabled { opacity: 0.45; cursor: default; }
.btn:hover:not(:disabled) { background: var(--panel-2); }
.btn-wide { width: 100%; margin-top: 10px; }

/* toggle */
.toggle {
  position: relative; /* contains the hidden checkbox, or it lengthens the page and focus scrolls to it */
  display: grid;
  grid-template-columns: 30px 1fr;
  gap: 10px;
  align-items: start;
  padding: 4px 0;
  cursor: pointer;
}
.toggle + .toggle { margin-top: 6px; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track {
  position: relative;
  width: 30px; height: 18px;
  margin-top: 1px;
  border-radius: 9px;
  background: var(--line);
  transition: background 0.15s;
}
.toggle-track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  transition: transform 0.15s;
}
.toggle input:checked + .toggle-track { background: var(--accent); }
.toggle input:checked + .toggle-track::after { transform: translateX(12px); }
.toggle input:focus-visible + .toggle-track { outline: 2px solid var(--accent); outline-offset: 1px; }
.toggle-text small { display: block; margin-top: 2px; color: var(--muted); font-size: 11.5px; line-height: 1.4; }

/* foldable section (printer) */
.fold summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  list-style: none;
  cursor: pointer;
}
.fold summary::-webkit-details-marker { display: none; }
.fold summary h2 { margin: 0; flex: 0 0 auto; white-space: nowrap; }
.fold summary h2::before {
  content: "";
  display: inline-block;
  width: 0; height: 0;
  margin-right: 7px;
  border-left: 4px solid var(--muted);
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  vertical-align: 1px;
  transition: transform 0.15s;
}
.fold[open] summary h2::before { transform: rotate(90deg); }
.fold[open] summary { margin-bottom: 10px; }
.fold-state { color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; }
.fold summary { gap: 12px; }
.fold summary .fold-state { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.steps {
  margin: 0 0 12px;
  padding-left: 18px;
  color: var(--ink-2);
  font-size: 11.5px;
  line-height: 1.45;
}
.steps li + li { margin-top: 4px; }

/* footer */
.panel-foot {
  padding: 14px 18px 16px;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.consent {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  margin-bottom: 10px;
  color: var(--ink-2);
  font-size: 11.5px;
  line-height: 1.4;
}
.consent input { flex: 0 0 auto; margin: 1px 0 0; accent-color: var(--ink); cursor: pointer; }
.consent label { cursor: pointer; }
.inline-link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--ink);
  font-size: inherit;
  text-decoration: underline;
}
.inline-link:hover { color: var(--accent); }
.primary {
  width: 100%;
  height: 38px;
  border: 0;
  border-radius: var(--radius);
  background: var(--ink);
  color: var(--panel);
  font-weight: 600;
}
.primary:hover:not(:disabled) { background: var(--accent); color: #fff; }
.primary:disabled { opacity: 0.35; cursor: default; }
.panel-foot .hint { margin-top: 8px; text-align: center; }
.credit { margin: 10px 0 0; text-align: center; font-size: 11px; }
.credit a { color: var(--muted); text-decoration: none; }
.credit a:hover { color: var(--ink); text-decoration: underline; }

/* ---------- workspace ---------- */

.work {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background-color: var(--mat);
  background-image:
    linear-gradient(var(--mat-grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--mat-grid-major) 1px, transparent 1px),
    linear-gradient(var(--mat-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--mat-grid) 1px, transparent 1px);
  background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;
  color: var(--mat-ink);
}

.work-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 52px;
  padding: 10px 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  background: rgba(0, 0, 0, 0.18);
}

.status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 18px;
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  color: var(--mat-muted);
}
.status b { color: var(--mat-ink); font-weight: 550; }
.status .err { color: #ff8e72; }
.dpi { display: inline-flex; align-items: center; gap: 6px; }
.dpi::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--ok); }
.dpi.warn::before { background: var(--warn); }
.dpi.bad::before { background: var(--bad); }

.stage { position: relative; flex: 1; min-height: 0; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.stage.dragging-file::after {
  content: "";
  position: absolute;
  inset: 14px;
  border: 2px dashed var(--accent);
  border-radius: 8px;
  pointer-events: none;
}

.stage-hint {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  margin: 0;
  color: var(--mat-muted);
  font-size: 12px;
  text-align: center;
  pointer-events: none;
}

.empty {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: min(440px, calc(100% - 48px));
  padding: 48px 24px;
  border: 1px dashed rgba(255, 255, 255, 0.25);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.12);
  color: var(--mat-ink);
}
.empty:hover { border-color: rgba(255, 255, 255, 0.5); }
.empty-title { font-size: 15px; font-weight: 550; }
.empty-sub { color: var(--mat-muted); }
.empty[hidden] { display: none; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 12px);
  padding: 8px 14px;
  border-radius: var(--radius);
  background: #111;
  color: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* help dialog */
.help {
  width: min(540px, calc(100% - 32px));
  max-height: calc(100% - 48px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.3);
}
.help::backdrop { background: rgba(10, 12, 11, 0.5); }
.help-body { padding: 18px 22px 20px; font-size: 13px; line-height: 1.5; }
.help-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.help-head h2 { margin: 0; font-size: 15px; font-weight: 650; }
.help-head .link-btn { margin-right: -7px; }
.help h3 { margin: 16px 0 6px; font-size: 12px; font-weight: 650; }
.help p { margin: 0; color: var(--ink-2); }
.help ol, .help ul { margin: 0; padding-left: 18px; color: var(--ink-2); }
.help li + li { margin-top: 4px; }
.help-foot { margin-top: 16px !important; font-size: 11.5px; }
.help-foot a { color: var(--muted); }
.help-foot a:hover { color: var(--ink); }

[hidden] { display: none !important; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 70vh; height: auto; }
  .panel { border-right: 0; }
  .panel-body { overflow: visible; }
}

/* ---------- browser print: the sheets, one canvas per A4 page (see fillPrintSheets) ---------- */
.print-sheets { display: none; }
@media print {
  @page { size: A4 portrait; margin: 0; }
  body.printing-sheets { margin: 0; background: #fff; }
  body.printing-sheets > :not(.print-sheets) { display: none !important; }
  body.printing-sheets .print-sheets { display: block; }
  .print-sheets canvas { display: block; width: 210mm; height: 297mm; }
  .print-sheets canvas + canvas { break-before: page; }
}

/* ---------- 404.html ---------- */
.notfound-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 16px;
  background: var(--mat);
}
.notfound {
  width: 100%;
  max-width: 380px;
  padding: 28px 28px 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
}
.notfound-brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; }
.notfound .notfound-code {
  margin: 28px 0 4px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  font-variant-numeric: tabular-nums;
}
.notfound h1 { margin: 0 0 10px; font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
.notfound p { margin: 0; color: var(--ink-2); }
.notfound .notfound-alt { margin-top: 2px; color: var(--muted); }
.notfound-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  margin-top: 22px;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-weight: 500;
  text-decoration: none;
}
.notfound-btn:hover { filter: brightness(1.06); }
