:root {
  --red: #b3121f;
  --red-dark: #8c0d17;
  --red-soft: #fbe9ea;
  --ink: #1d1b19;
  --muted: #6b6560;
  --line: #e3ded8;
  --bg: #f6f4f1;
  --card: #ffffff;
  --ok: #1f7a3d;
  --ok-soft: #e5f3ea;
  --warn: #9a6200;
  --warn-soft: #fff4dc;
  --blue: #1f4fd1;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #f1eeea; --muted: #a9a29b; --line: #3a3532; --bg: #171514; --card: #221f1d;
    --red-soft: #3a1518; --ok-soft: #13301e; --warn-soft: #3a2c10; --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); min-height: 100%; }
body { font-size: 16px; line-height: 1.45; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--red); }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px; background: var(--card); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; letter-spacing: .2px; }
.brand img { width: 28px; height: 28px; border-radius: 7px; }
.page { max-width: 720px; margin: 0 auto; padding: 16px 16px calc(96px + var(--safe-b)); }
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.tiny { font-size: 12px; }
h2 { font-size: 20px; margin: 4px 0 8px; }
h3 { font-size: 16px; margin: 0 0 6px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card.tight { padding: 12px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 0 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--card); font-weight: 600; cursor: pointer;
  text-decoration: none; color: var(--ink); -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:active { background: var(--red-dark); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--red); }
.btn.block { width: 100%; }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.iconbtn { text-decoration: none; color: var(--ink); width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); display: inline-grid; place-items: center; font-size: 20px; cursor: pointer; }

label.field { display: block; }
label.field > span { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
input[type=text], input[type=email], input[type=number], input[inputmode], textarea, select {
  width: 100%; min-height: 48px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--card);
}
input:focus-visible, button:focus-visible, select:focus-visible, a:focus-visible { outline: 3px solid rgba(179, 18, 31, .35); outline-offset: 2px; }
.code-input { font-size: 28px; letter-spacing: 10px; text-align: center; font-variant-numeric: tabular-nums; }

.choice { display: flex; gap: 12px; align-items: flex-start; padding: 14px; border: 2px solid var(--line); border-radius: 12px; cursor: pointer; background: var(--card); }
.choice input { margin-top: 4px; width: 20px; height: 20px; accent-color: var(--red); }
.choice:has(input:checked) { border-color: var(--red); background: var(--red-soft); }

.switch { display: flex; align-items: center; gap: 12px; }
.switch input { width: 22px; height: 22px; accent-color: var(--red); }

.pill { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; background: var(--line); color: var(--ink); white-space: nowrap; }
.pill.review { background: var(--ok-soft); color: var(--ok); }
.pill.processing, .pill.queued { background: var(--warn-soft); color: var(--warn); }
.pill.failed { background: var(--red-soft); color: var(--red); }
.pill.draft { background: var(--line); }

.banner { padding: 12px 14px; border-radius: 12px; background: var(--warn-soft); color: var(--warn); font-size: 14px; }
.banner.ok { background: var(--ok-soft); color: var(--ok); }
.banner.err { background: var(--red-soft); color: var(--red); }
.banner.dev { background: #e8eefc; color: #1f3d8a; }

.fab {
  position: fixed; right: 16px; bottom: calc(18px + var(--safe-b)); z-index: 30; min-height: 56px; padding: 0 22px; border-radius: 28px;
  box-shadow: 0 6px 20px rgba(179, 18, 31, .35);
}
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; padding: 12px 16px calc(12px + var(--safe-b));
  background: var(--card); border-top: 1px solid var(--line); display: flex; gap: 10px;
}
.bottombar .btn { flex: 1; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + var(--safe-b)); z-index: 100;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: calc(100% - 32px); box-shadow: var(--shadow);
}

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 400px; }
.hero-title { font-size: 26px; font-weight: 800; margin: 12px 0 4px; line-height: 1.2; }

/* ---------- job list ---------- */
.job { display: flex; gap: 12px; align-items: center; text-decoration: none; color: inherit; }
.job .thumb { width: 48px; height: 48px; border-radius: 10px; background: var(--red-soft); color: var(--red); display: grid; place-items: center; font-weight: 800; flex: none; }
.job .meta { flex: 1; min-width: 0; }
.job .title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; padding: 40px 16px; }

/* ---------- new job ---------- */
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.fileitem { position: relative; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: var(--card); aspect-ratio: 3 / 4; }
.fileitem img { width: 100%; height: 100%; object-fit: cover; display: block; }
.fileitem .doc { display: grid; place-items: center; height: 100%; padding: 8px; font-size: 12px; text-align: center; color: var(--muted); }
.fileitem .x { position: absolute; top: 4px; right: 4px; width: 30px; height: 30px; border-radius: 15px; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 16px; cursor: pointer; }
.fileitem .bar { position: absolute; left: 0; bottom: 0; height: 5px; background: var(--red); transition: width .2s; }
.pdfrow { display: flex; gap: 12px; align-items: center; }
.pdfrow .ico { width: 44px; height: 54px; border-radius: 6px; background: var(--red-soft); color: var(--red); display: grid; place-items: center; font-weight: 800; font-size: 12px; flex: none; }

/* ---------- capture tips + camera ---------- */
.overlay { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .55); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; background: var(--card); border-radius: 20px 20px 0 0;
  padding: 18px 16px calc(18px + var(--safe-b)); box-shadow: 0 -8px 30px rgba(0,0,0,.25);
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: -6px auto 12px; }
.tips-guide { position: relative; aspect-ratio: 4 / 3; border-radius: 14px; background: #2a2724; overflow: hidden; margin-bottom: 14px; }
.tips-guide .paper { position: absolute; inset: 16% 13%; background: #f3efe7; border-radius: 3px; transform: rotate(-1.5deg); box-shadow: 0 4px 12px rgba(0,0,0,.4); }
.tips-guide .paper::before { content: ""; position: absolute; inset: 12%; border: 2px solid #9a948c; border-right-width: 0; }
.tips-guide .paper::after { content: ""; position: absolute; left: 30%; top: 40%; width: 34%; height: 22%; border: 3px solid #555; border-radius: 40% 60% 50% 50%; border-color: #3b3b3b transparent transparent #3b3b3b; transform: rotate(8deg); }
.guide-frame { position: absolute; inset: 8%; pointer-events: none; }
.guide-frame i { position: absolute; width: 34px; height: 34px; border: 4px solid #fff; filter: drop-shadow(0 0 2px rgba(0,0,0,.6)); }
.guide-frame i:nth-child(1) { top: 0; left: 0; border-right: 0; border-bottom: 0; border-top-left-radius: 8px; }
.guide-frame i:nth-child(2) { top: 0; right: 0; border-left: 0; border-bottom: 0; border-top-right-radius: 8px; }
.guide-frame i:nth-child(3) { bottom: 0; left: 0; border-right: 0; border-top: 0; border-bottom-left-radius: 8px; }
.guide-frame i:nth-child(4) { bottom: 0; right: 0; border-left: 0; border-top: 0; border-bottom-right-radius: 8px; }
.tips { list-style: none; padding: 0; margin: 0 0 14px; display: grid; gap: 10px; }
.tips li { display: flex; gap: 12px; align-items: flex-start; }
.tips .ti { width: 36px; height: 36px; border-radius: 10px; background: var(--red-soft); color: var(--red); display: grid; place-items: center; flex: none; font-size: 18px; }
.tips b { display: block; }

.camera { position: fixed; inset: 0; z-index: 70; background: #000; display: flex; flex-direction: column; }
.camera video { flex: 1; width: 100%; min-height: 0; object-fit: cover; background: #000; }
.camera .guide-frame { inset: calc(env(safe-area-inset-top, 0px) + 70px) 6% 140px; }
.camera .hint { position: absolute; top: calc(env(safe-area-inset-top, 0px) + 14px); left: 12px; right: 12px; text-align: center; color: #fff; font-size: 14px; text-shadow: 0 1px 3px #000; display: flex; gap: 10px; align-items: center; }
.camera .hint span { flex: 1; }
.camera .controls { position: absolute; left: 0; right: 0; bottom: 0; padding: 18px 24px calc(22px + var(--safe-b)); display: flex; align-items: center; justify-content: space-between; background: linear-gradient(transparent, rgba(0,0,0,.6)); }
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 5px solid #fff; background: rgba(255,255,255,.25); cursor: pointer; }
.shutter:active { background: #fff; }
.camera .count { color: #fff; font-weight: 700; min-width: 70px; }
.camera .btn { background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.3); }

/* ---------- processing ---------- */
.steps { list-style: none; margin: 0; padding: 0; }
.steps li { display: flex; gap: 12px; align-items: center; padding: 10px 0; }
.steps .dot { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line); display: grid; place-items: center; font-size: 14px; flex: none; }
.steps li.done .dot { background: var(--ok); border-color: var(--ok); color: #fff; }
.steps li.now .dot { border-color: var(--red); border-top-color: transparent; animation: spin 1s linear infinite; }
.steps li.now { font-weight: 700; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
.progress > div { height: 100%; background: var(--red); transition: width .4s; }
.progress.indeterminate > div { width: 35%; animation: slide 1.4s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(300%); } }

/* ---------- job tabs ---------- */
.tabs { display: flex; gap: 6px; padding: 8px 16px; background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 61px; z-index: 19; overflow-x: auto; }
.tabs a { flex: 1; text-align: center; padding: 8px 10px; border-radius: 10px; text-decoration: none; color: var(--muted); font-weight: 600; white-space: nowrap; font-size: 15px; }
.tabs a.on { background: var(--red-soft); color: var(--red); }
.tabs .n { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--red); color: #fff; font-size: 12px; margin-left: 4px; }

/* ---------- question cards ---------- */
.qcard .crops { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.qcard canvas { width: 100%; aspect-ratio: 1; border-radius: 10px; background: #eee; display: block; }
.qcard .cap { font-size: 12px; color: var(--muted); text-align: center; margin-top: 4px; }
.qcard .ask { font-size: 20px; font-weight: 700; margin: 4px 0; }
.opts { display: grid; gap: 8px; margin-top: 12px; }
.opt { justify-content: flex-start; text-align: left; min-height: 54px; }
.opt img { height: 34px; width: 48px; object-fit: contain; background: #fff; border-radius: 6px; }
.opt.best { border-color: var(--red); border-width: 2px; }
.opt .tag { margin-left: auto; font-size: 12px; color: var(--red); font-weight: 700; }
.qprogress { display: flex; gap: 4px; margin-bottom: 10px; }
.qprogress i { flex: 1; height: 4px; border-radius: 2px; background: var(--line); }
.qprogress i.done { background: var(--ok); }
.qprogress i.now { background: var(--red); }

/* ---------- review overlay ---------- */
.review-screen { position: fixed; inset: 0; display: flex; flex-direction: column; background: #3a3633; }
.review-screen .topbar { position: relative; }
.viewer { position: relative; flex: 1; overflow: hidden; touch-action: none; background: #57514c; cursor: grab; }
.viewer.moving { cursor: crosshair; }
.stage { position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform; }
.stage img, .stage svg { position: absolute; left: 0; top: 0; width: 100%; height: 100%; user-select: none; -webkit-user-drag: none; }
.stage .photo { background: #fff; }
.stage .clean { mix-blend-mode: multiply; }
.stage svg .hit { fill: transparent; cursor: pointer; }
.stage svg .unsure-ring { fill: rgba(255, 200, 0, .28); stroke: #f0a800; stroke-width: 2.5; animation: pulse 1.4s ease-in-out infinite; }
.stage svg .sel-ring { fill: rgba(31, 79, 209, .12); stroke: #1f4fd1; stroke-width: 2.5; stroke-dasharray: 5 3; }
@keyframes pulse { 50% { opacity: .35; } }
.reviewbar { background: var(--card); border-top: 1px solid var(--line); padding: 10px 16px calc(10px + var(--safe-b)); display: grid; gap: 8px; }
.slider { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; font-size: 13px; font-weight: 600; color: var(--muted); }
.slider input { width: 100%; accent-color: var(--red); height: 32px; }
.chips { display: flex; gap: 8px; overflow-x: auto; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 0 12px; min-height: 36px; border-radius: 18px; border: 1px solid var(--line); background: var(--card); font-size: 14px; font-weight: 600; white-space: nowrap; cursor: pointer; }
.chip.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.chip.on { background: var(--red-soft); color: var(--red); border-color: transparent; }
.zoom { position: absolute; right: 12px; top: 12px; display: grid; gap: 8px; z-index: 5; }
.movebar { position: absolute; left: 12px; right: 12px; top: 12px; z-index: 6; background: #1f4fd1; color: #fff; border-radius: 12px; padding: 10px 12px; display: flex; gap: 8px; align-items: center; font-size: 14px; box-shadow: var(--shadow); }
.movebar span { flex: 1; }
.movebar .btn { min-height: 38px; background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.35); }
.movebar .btn.primary { background: #fff; color: #1f4fd1; }

.fix h3 { display: flex; align-items: center; gap: 8px; }
.fix .sect { margin-top: 14px; }
.fix .sect > .label { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; margin-bottom: 8px; }
.toolgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.toolbtn { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 6px; border-radius: 12px; border: 2px solid var(--line); background: var(--card); cursor: pointer; font-size: 13px; font-weight: 600; text-align: center; }
.toolbtn img { height: 36px; max-width: 100%; object-fit: contain; background: #fff; border-radius: 4px; }
.toolbtn.on { border-color: var(--red); background: var(--red-soft); }
.dirs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.dirs .btn.on { border-color: var(--red); background: var(--red-soft); color: var(--red); }
.cropview { width: 100%; max-width: 220px; aspect-ratio: 1; border-radius: 10px; display: block; margin: 0 auto; background: #eee; }

/* ---------- export ---------- */
.issue { display: flex; gap: 10px; align-items: flex-start; padding: 10px 0; border-top: 1px solid var(--line); font-size: 15px; }
.issue:first-child { border-top: 0; }
.issue .w { color: var(--warn); font-size: 18px; line-height: 1.2; }
.result { border: 2px solid var(--ok); }

/* ---------- settings ---------- */
.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: 15px; }
table.sh { width: 100%; border-collapse: collapse; font-size: 14px; }
table.sh td, table.sh th { padding: 8px 4px; border-top: 1px solid var(--line); text-align: left; }
table.sh th { color: var(--muted); font-weight: 600; border-top: 0; }

@media (min-width: 900px) {
  .page { padding-top: 24px; }
  .sheet { border-radius: 20px; margin-bottom: 24px; }
  .overlay { align-items: center; }
}
