* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  color: #111827;
  background: #eef2f7;
}
h1 { font-size: 1.3rem; margin: 0 0 .4rem; }
h2 { font-size: 1.05rem; margin: 0 0 .6rem; }
[hidden] { display: none !important; }
body.no-scroll { overflow: hidden; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  padding: .7rem 1rem; background: #1e3a5f; color: #fff;
}
.brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 1.15rem; }
.top-sub { display: block; font-size: .8rem; color: #cbd5e1; }
.top-nav { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.nav-user { font-size: .88rem; color: #e2e8f0; }
.top-link, .top-btn {
  color: #fff; text-decoration: none; border: 1px solid rgba(255,255,255,.35);
  border-radius: .4rem; padding: .32rem .7rem; font-size: .9rem; background: transparent;
  min-height: 2.1rem;
}
.top-link.primary-pill { background: #2563eb; border-color: #2563eb; font-weight: 600; }

.conn { font-size: .82rem; padding: .2rem .6rem; border-radius: 1rem; background: #6b7280; white-space: nowrap; }
.conn.ok { background: #15803d; }
.conn.bad { background: #b91c1c; }

.wrap { max-width: 780px; margin: 0 auto; padding: .8rem; }
.wrap.narrow { max-width: 440px; padding-top: 2rem; }
.wrap.wide { max-width: 1180px; }
.narrow-card { max-width: 420px; margin: 2rem auto; }
.card { background: #fff; border-radius: .65rem; padding: 1rem; margin-bottom: .8rem; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.card.warn { background: #fff7e6; border: 1px solid #f0b429; }
.hero .lead { color: #374151; margin: .4rem 0 1rem; }
.clean-list { padding-left: 1.2rem; margin: 0; }
.clean-list li { margin: .35rem 0; color: #374151; }

.stack { display: flex; flex-direction: column; gap: .6rem; }
label, .label { display: block; font-weight: 600; margin: .1rem 0 .25rem; }
.inline-label { display: inline-block; margin: 0; font-weight: 600; }
.field { margin-bottom: .75rem; }
label.check { display: flex; align-items: center; gap: .5rem; font-weight: normal; margin: .45rem 0; }
label.check input { width: 1.2rem; height: 1.2rem; flex: none; }
input, select, button { font: inherit; color: inherit; }
input[type=text], input[type=email], input[type=password], input[type=number], select {
  width: 100%; min-height: 2.55rem; padding: .45rem .6rem; border: 1px solid #cbd5e1; border-radius: .4rem;
  background: #fff; font-weight: normal;
}
select:disabled { background: #f3f4f6; color: #6b7280; }
button, a.button {
  min-height: 2.5rem; padding: .45rem .9rem; border: 1px solid #cbd5e1; border-radius: .4rem;
  background: #f8fafc; cursor: pointer; text-decoration: none; display: inline-flex;
  align-items: center; justify-content: center; color: #111827;
}
button:disabled { opacity: .5; cursor: default; }
button.primary, a.button.primary { background: #1f6feb; border-color: #1f6feb; color: #fff; font-weight: 600; }
button.danger { background: #dc2626; border-color: #dc2626; color: #fff; font-weight: 600; }
button.big { width: 100%; padding: .8rem; font-size: 1.1rem; }
button.link { background: none; border: none; color: #1f6feb; padding: .3rem 0; min-height: 0; text-decoration: underline; }
button.small { min-height: 2.1rem; padding: .28rem .65rem; font-size: .86rem; }

.row { display: flex; gap: .55rem; align-items: center; }
.row.wrap-row { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.between h2 { margin: 0; }
.row.percent { margin-top: .4rem; max-width: 10rem; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
@media (max-width: 760px) { .grid, .dash-grid { grid-template-columns: 1fr; } }

.stepper { display: flex; }
.stepper input { text-align: center; border-radius: 0; border-left: none; border-right: none; -moz-appearance: textfield; }
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper button { width: 2.8rem; flex: none; font-size: 1.2rem; padding: 0; }
.stepper button:first-child { border-radius: .4rem 0 0 .4rem; }
.stepper button:last-child { border-radius: 0 .4rem .4rem 0; }

.segmented { display: flex; border: 1px solid #cbd5e1; border-radius: .4rem; overflow: hidden; }
.segmented label { flex: 1; margin: 0; font-weight: normal; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; text-align: center; padding: .5rem .3rem; cursor: pointer; background: #fff; border-left: 1px solid #cbd5e1; }
.segmented label:first-child span { border-left: none; }
.segmented input:checked + span { background: #1f6feb; color: #fff; font-weight: 600; }

.filepick {
  display: block; text-align: center; padding: 1rem .6rem; border: 2px dashed #94a3b8; border-radius: .6rem;
  background: #f8fafc; cursor: pointer; font-weight: normal;
}
.filepick-button {
  display: inline-block; padding: .6rem 1.15rem; border-radius: .45rem; background: #1f6feb; color: #fff; font-weight: 600;
}
.filepick .hint { display: block; margin-top: .5rem; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.files, .jobs, .notes { list-style: none; margin: .6rem 0 0; padding: 0; }
.files li { display: flex; align-items: center; gap: .5rem; padding: .45rem .5rem; border: 1px solid #e5e7eb; border-radius: .45rem; margin-bottom: .4rem; }
.files li.active { border-color: #1f6feb; background: #eef4ff; }
.files .file-main { flex: 1; min-width: 0; text-align: left; background: none; border: none; padding: 0; min-height: 0; display: block; }
.files .file-name { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.files .file-meta { display: block; font-size: .85rem; color: #64748b; }
.files .file-meta.error { color: #b91c1c; white-space: normal; }
.files .remove { flex: none; width: 2.3rem; min-height: 2.3rem; padding: 0; font-size: 1.2rem; }
.files .download { flex: none; font-size: .84rem; padding: .35rem .55rem; border: 1px solid #cbd5e1; border-radius: .4rem; color: #1f6feb; text-decoration: none; background: #fff; }

.scan-open {
  display: flex; align-items: center; justify-content: center; gap: .55rem; width: 100%; margin-top: .6rem;
  padding: .75rem; font-weight: 600; background: #eef4ff; border: 2px solid #1f6feb; color: #1e40af; border-radius: .55rem;
}
.scan-open svg { width: 1.45rem; height: 1.45rem; flex: none; }

.area-preview { position: sticky; top: 0; z-index: 10; }
.area-preview .placeholder { display: none; }
.area-preview.empty .placeholder { display: block; }
.area-preview.empty .sheet-stage, .area-preview.empty .pager, .area-preview.empty #expandPreview { display: none; }
.sheet-stage {
  height: 30vh; min-height: 190px; display: flex; align-items: center; justify-content: center;
  background: #d9dde3; border-radius: .45rem; padding: .6rem; touch-action: pan-y;
}
.sheet-frame { position: relative; width: 140px; height: 198px; flex: none; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.25); }
.sheet-frame img { display: block; width: 100%; height: 100%; object-fit: contain; }
.sheet-frame img.stale { opacity: .35; }
.sheet-busy {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: rgba(30,58,95,.85); color: #fff; padding: .3rem .7rem; border-radius: 1rem; font-size: .85rem;
}
.area-preview.expanded { position: static; }
.area-preview.expanded .sheet-stage { height: 78vh; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: .4rem; margin-top: .5rem; }
.pager button { min-width: 5.2rem; }
.side-label { text-align: center; font-size: .9rem; font-weight: 600; }
.notes li { font-size: .88rem; color: #b45309; padding: .15rem 0; }
.center { text-align: center; }

.jobs li { padding: .6rem 0; border-top: 1px solid #e5e7eb; }
.jobs li:first-child { border-top: none; }
.job-head { display: flex; justify-content: space-between; gap: .5rem; align-items: center; }
.job-name { font-weight: 600; word-break: break-word; }
.job-code { display: inline-block; font-family: ui-monospace, Consolas, monospace; font-weight: 700; background: #eff6ff; color: #1d4ed8; padding: .1rem .45rem; border-radius: .3rem; margin-right: .4rem; }
.job-meta { color: #64748b; font-size: .85rem; margin-top: .15rem; }
.job-note { font-size: .88rem; margin-top: .25rem; }
.job-actions { display: flex; gap: .4rem; margin-top: .4rem; flex-wrap: wrap; }
.badge { font-size: .78rem; padding: .15rem .55rem; border-radius: 1rem; background: #e5e7eb; white-space: nowrap; font-weight: 600; }
.badge.pending_approval { background: #fef3c7; color: #92400e; }
.badge.queued, .badge.dispatched { background: #e0e7ff; color: #3730a3; }
.badge.printing { background: #dbeafe; color: #1e40af; }
.badge.submitted, .badge.completed { background: #dcfce7; color: #166534; }
.badge.failed, .badge.rejected { background: #fee2e2; color: #991b1b; }
.badge.cancelled { background: #f3f4f6; color: #374151; }

.note-problem { color: #b45309; }
.note-error { color: #b91c1c; }
.msg { min-height: 1.2rem; margin: .6rem 0 0; white-space: pre-line; }
.msg.error { color: #b91c1c; }
.msg.ok { color: #15803d; }
.hint { color: #64748b; font-size: .88rem; margin: .35rem 0 0; }
.hint.problem { color: #b45309; font-weight: 600; }
.hint.ready { color: #15803d; }

.qr-box { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
.qr-box img { width: 150px; height: 150px; border: 1px solid #e5e7eb; border-radius: .5rem; background: #fff; }
.mono-box {
  font-family: ui-monospace, Consolas, monospace; font-size: .84rem; padding: .45rem .6rem;
  background: #f1f5f9; border-radius: .35rem; word-break: break-all; margin: .4rem 0;
}

.modal-backdrop {
  position: fixed; inset: 0; z-index: 200; background: rgba(15, 23, 42, .6);
  display: flex; align-items: center; justify-content: center; padding: 1rem;
}
.modal-card {
  width: 100%; max-width: 480px; background: #fff; border-radius: .7rem; padding: 1.15rem;
  box-shadow: 0 12px 30px rgba(0,0,0,.25); max-height: 90vh; overflow-y: auto;
}
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }

/* Full-screen camera scanner */
.scanner { position: fixed; inset: 0; z-index: 150; display: flex; flex-direction: column; background: #15181d; color: #f2f4f7; }
.scanner-bar { display: flex; align-items: center; gap: .6rem; padding: .55rem .8rem; background: #0d0f12; }
.scanner-bar h2 { flex: 1; margin: 0; font-size: 1.05rem; text-align: center; }
.bar-button { background: none; border: 1px solid #4b5563; color: #f2f4f7; min-height: 2.3rem; }
.bar-count { min-width: 4.5rem; text-align: right; font-size: .9rem; color: #cbd5e1; }
.scan-step { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: .6rem; }
.scan-start { margin: auto; width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: .8rem; }
.scan-big { display: block; text-align: center; padding: 1.1rem; border-radius: .6rem; font-weight: 600; font-size: 1.1rem; background: #2a2f37; border: 1px solid #4b5563; cursor: pointer; }
.scan-big.primary { background: #1f6feb; border-color: #1f6feb; color: #fff; }
.scan-tips { color: #cbd5e1; font-size: .92rem; padding-left: 1.2rem; margin: .4rem 0 0; }
.scan-hint { margin: 0 0 .5rem; text-align: center; font-size: .95rem; color: #e5e7eb; }
.scan-hint.warn { color: #fbbf24; }
.crop-stage, .result-stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.crop-box { position: relative; touch-action: none; user-select: none; -webkit-user-select: none; flex: none; }
.crop-box img { display: block; width: 100%; height: 100%; pointer-events: none; }
.crop-svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.crop-shade { fill: rgba(0, 0, 0, .55); }
.crop-poly { fill: rgba(31, 111, 235, .12); stroke: #4f9dff; stroke-width: 2.5; stroke-linejoin: round; }
.crop-poly.bad { stroke: #f87171; }
.crop-handle { position: absolute; width: 34px; height: 34px; margin: -17px 0 0 -17px; border-radius: 50%; background: rgba(31, 111, 235, .35); border: 3px solid #fff; box-shadow: 0 0 0 2px #1f6feb; touch-action: none; cursor: grab; }
.crop-handle.active { background: rgba(31, 111, 235, .7); }
.loupe { position: absolute; width: 120px; height: 120px; border-radius: 50%; border: 3px solid #fff; pointer-events: none; box-shadow: 0 4px 14px rgba(0,0,0,.6); background-repeat: no-repeat; background-color: #000; }
.loupe::before, .loupe::after { content: ""; position: absolute; background: #4f9dff; }
.loupe::before { left: 50%; top: 20%; bottom: 20%; width: 2px; margin-left: -1px; }
.loupe::after { top: 50%; left: 20%; right: 20%; height: 2px; margin-top: -1px; }
.result-stage img { max-width: 100%; max-height: 100%; background: #fff; box-shadow: 0 2px 12px rgba(0,0,0,.6); }
.scan-actions { display: flex; gap: .5rem; margin-top: .55rem; }
.scan-actions button { flex: 1; background: #2a2f37; border-color: #4b5563; color: #f2f4f7; padding: .5rem .3rem; }
.scan-actions button.primary { background: #1f6feb; border-color: #1f6feb; font-weight: 600; }
.segmented.dark { margin-top: .55rem; border-color: #4b5563; }
.segmented.dark span { background: #2a2f37; color: #f2f4f7; border-left-color: #4b5563; }
.segmented.dark input:checked + span { background: #1f6feb; }
.scan-pages { display: flex; gap: .45rem; overflow-x: auto; padding: .45rem .6rem; background: #0d0f12; }
.scan-page { position: relative; flex: none; width: 54px; height: 72px; padding: 0; min-height: 0; border: 2px solid #4b5563; background: #fff; }
.scan-page.current { border-color: #4f9dff; }
.scan-page img { width: 100%; height: 100%; object-fit: contain; display: block; }
.scan-page span { position: absolute; right: 2px; bottom: 2px; background: rgba(0,0,0,.7); color: #fff; font-size: .7rem; padding: 0 .25rem; border-radius: .2rem; }
.scan-error { margin: 0; padding: .55rem .8rem; background: #7f1d1d; color: #fff; font-size: .92rem; }
.scan-busy { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.55); }
.scan-busy span { background: #1f2937; padding: .7rem 1.1rem; border-radius: 2rem; font-weight: 600; }

@media (min-width: 960px) {
  .print-area {
    display: grid; grid-template-columns: minmax(0, 1fr) 420px; gap: 0 1rem; align-items: start;
    grid-template-areas: "preview files" "preview settings" "preview print";
  }
  .area-files { grid-area: files; }
  .area-settings { grid-area: settings; }
  .area-print { grid-area: print; }
  .area-preview { grid-area: preview; top: 1rem; }
  .area-preview .sheet-stage { height: calc(100vh - 230px); min-height: 420px; }
  .area-preview.expanded { position: sticky; }
  .area-preview.expanded .sheet-stage { height: calc(100vh - 230px); }
  .area-preview.empty .placeholder { padding: 6rem 1rem; text-align: center; }
  #expandPreview { display: none; }
}
@media (max-width: 959px) {
  .area-preview.empty { display: none; }
}
