:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #f1f3f9;
  --line: #e3e6ef;
  --text: #141824;
  --muted: #646b7d;
  --accent: #5b5bf7;
  --accent-ink: #ffffff;
  --accent-soft: #ecebff;
  --ok: #138a52;
  --ok-soft: #e3f6ec;
  --warn: #9a6700;
  --danger: #c4314b;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 24, 36, .04), 0 8px 24px rgba(20, 24, 36, .06);
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a21;
    --panel-2: #1e222b;
    --line: #2a2f3a;
    --text: #eceef3;
    --muted: #9aa1b2;
    --accent: #7c7cff;
    --accent-soft: #25264a;
    --ok: #4cd08e;
    --ok-soft: #17322a;
    --warn: #e7b343;
    --danger: #ff6b81;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
[hidden] { display: none !important; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
h1, h2 { margin: 0; letter-spacing: -.01em; text-wrap: balance; }
h1 { font-size: clamp(22px, 3.2vw, 30px); }
h2 { font-size: 19px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
kbd { font: 12px var(--mono); padding: 1px 6px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 6px; background: var(--panel); }

.wrap { width: min(1100px, 100% - 32px); margin-inline: auto; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar-inner { display: flex; align-items: center; justify-content: space-between; gap: 16px; height: 60px; }
.brand { display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; font-weight: 650; font-size: 17px; }
.brand-mark { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: #fff; }
.brand-mark svg { width: 18px; height: 18px; stroke-width: 2.4; }
.stats { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.pill { font-size: 12.5px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); padding: 4px 10px; border-radius: 999px; white-space: nowrap; }
.pill b { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }

main { padding: 28px 0 64px; display: grid; gap: 28px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font: inherit; font-weight: 550; cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--panel-2); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .icon-btn:focus-visible, .search input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn-ghost { background: transparent; }
.btn-lg { height: 52px; padding: 0 24px; font-size: 16px; border-radius: 12px; }
.btn:disabled { opacity: .6; cursor: progress; }
.icon-btn { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent; color: var(--muted); cursor: pointer; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

/* Capture */
.dropzone {
  margin: 10px; padding: clamp(32px, 6vw, 64px) 24px; border: 2px dashed var(--line); border-radius: 12px;
  text-align: center; display: grid; justify-items: center; gap: 14px; transition: border-color .15s, background .15s;
}
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone p { margin: 0; max-width: 52ch; }
.dz-art { width: 72px; height: 72px; border-radius: 20px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); margin-bottom: 4px; }
.dz-art svg { width: 40px; height: 40px; stroke-width: 2.4; }
.actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 8px; }
.hint { font-size: 13px; color: var(--muted); }

/* Processing */
.processing { padding: 20px; display: grid; grid-template-columns: minmax(0, 1fr) 260px; gap: 24px; align-items: center; }
.proc-media { position: relative; overflow: hidden; border-radius: 12px; background: var(--panel-2); max-height: 420px; display: grid; place-items: center; }
.proc-media img { max-width: 100%; max-height: 420px; display: block; filter: saturate(.85); }
.scanline { position: absolute; inset: 0; background: linear-gradient(180deg, transparent 0 46%, color-mix(in srgb, var(--accent) 55%, transparent) 50%, transparent 54% 100%); animation: scan 1.8s ease-in-out infinite; mix-blend-mode: screen; }
@keyframes scan { 0% { transform: translateY(-50%); } 100% { transform: translateY(50%); } }
.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; }
.steps li { display: flex; align-items: center; gap: 12px; color: var(--muted); font-weight: 500; }
.steps li::before { content: ""; width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line); flex: none; }
.steps li.active { color: var(--text); }
.steps li.active::before { border-color: var(--accent); border-top-color: transparent; animation: spin .8s linear infinite; }
.steps li.done { color: var(--text); }
.steps li.done::before { border-color: var(--ok); background: var(--ok) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 12l5 5 9-10'/%3E%3C/svg%3E") center/14px no-repeat; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Result */
.result { padding: 20px; display: grid; gap: 18px; animation: rise .25s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } }
.result-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.eyebrow { margin: 0 0 2px; font-size: 12.5px; font-weight: 600; color: var(--ok); text-transform: uppercase; letter-spacing: .06em; }
.result-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.result-grid { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 20px; align-items: start; }
.result-media { margin: 0; display: grid; gap: 8px; position: sticky; top: 80px; }
.result-media img { width: 100%; border-radius: 12px; border: 1px solid var(--line); background: var(--panel-2); max-height: 420px; object-fit: contain; }
.result-body { display: grid; gap: 14px; min-width: 0; }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { border: 1px solid var(--line); border-radius: 12px; padding: 12px 12px 10px 14px; background: var(--panel-2); min-width: 0; }
.field.found { border-color: color-mix(in srgb, var(--ok) 45%, var(--line)); background: var(--ok-soft); }
.field-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.field-label { font-size: 12px; font-weight: 650; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.field-state { font-size: 12px; color: var(--muted); }
.field.found .field-state { color: var(--ok); font-weight: 600; }
.field-row { display: flex; align-items: center; gap: 2px; margin-top: 4px; min-height: 34px; }
.field-value { flex: 1; min-width: 0; font: 14px/1.4 var(--mono); overflow-wrap: anywhere; }
.field:not(.found) .field-value { color: var(--muted); font-family: inherit; font-style: italic; }
.field:not(.found) .icon-btn { display: none; }
.field-input { width: 100%; margin-top: 6px; height: 36px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font: 14px var(--mono); }
.editing .field-row { display: none; }

.textbox { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
.restext {
  margin: 0; padding: 16px; width: 100%; min-height: 180px; max-height: 60vh; overflow: auto;
  font: 15px/1.6 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text);
  background: transparent; border: 0; border-radius: 12px; resize: vertical;
}
textarea.restext { outline: none; min-height: 260px; }
.edit-bar { display: flex; justify-content: flex-end; gap: 8px; }

/* History */
.history { display: grid; gap: 16px; }
.history-head { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }
.search { position: relative; display: flex; align-items: center; width: min(340px, 100%); }
.search svg { position: absolute; left: 12px; color: var(--muted); width: 18px; height: 18px; }
.search input { width: 100%; height: 40px; padding: 0 12px 0 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font: inherit; }
.cards { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.card {
  display: grid; grid-template-rows: 120px auto; overflow: hidden; cursor: pointer; text-align: left;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  transition: transform .15s, border-color .15s;
}
.card:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.card.active { border-color: var(--accent); }
.card-img { background: var(--panel-2) center/cover no-repeat; position: relative; }
.card-del { position: absolute; top: 8px; right: 8px; background: color-mix(in srgb, var(--panel) 85%, transparent); backdrop-filter: blur(6px); opacity: 0; transition: opacity .15s; }
.card:hover .card-del, .card:focus-within .card-del { opacity: 1; }
.card-del:hover { color: var(--danger); }
@media (hover: none) { .card-del { opacity: 1; } }
.card-body { padding: 12px 14px 14px; display: grid; gap: 8px; min-width: 0; }
.card-text { margin: 0; font-size: 14px; line-height: 1.45; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.card-foot { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.tag { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--ok-soft); color: var(--ok); }
.tag.src { background: var(--panel-2); color: var(--muted); font-weight: 500; }
.card-foot time { margin-left: auto; }
.empty { text-align: center; padding: 32px 0; margin: 0; }
.more { display: flex; justify-content: center; }

/* Camera */
.camera { position: fixed; inset: 0; z-index: 50; background: #000; display: grid; }
.camera video { width: 100%; height: 100%; object-fit: cover; grid-area: 1 / 1; }
.cam-frame { grid-area: 1 / 1; position: relative; margin: 12vh 8vw 22vh; pointer-events: none; }
.cam-frame span { position: absolute; width: 34px; height: 34px; border: 3px solid rgba(255, 255, 255, .9); }
.cam-frame span:nth-child(1) { top: 0; left: 0; border-right: 0; border-bottom: 0; border-top-left-radius: 10px; }
.cam-frame span:nth-child(2) { top: 0; right: 0; border-left: 0; border-bottom: 0; border-top-right-radius: 10px; }
.cam-frame span:nth-child(3) { bottom: 0; left: 0; border-right: 0; border-top: 0; border-bottom-left-radius: 10px; }
.cam-frame span:nth-child(4) { bottom: 0; right: 0; border-left: 0; border-top: 0; border-bottom-right-radius: 10px; }
.cam-bar { position: absolute; left: 0; right: 0; bottom: 0; padding: 24px 24px calc(28px + env(safe-area-inset-bottom)); display: flex; align-items: center; justify-content: space-around; background: linear-gradient(transparent, rgba(0, 0, 0, .6)); }
.cam-btn { width: 52px; height: 52px; border-radius: 50%; border: 0; display: grid; place-items: center; color: #fff; background: rgba(255, 255, 255, .16); backdrop-filter: blur(8px); cursor: pointer; }
.cam-btn svg { width: 24px; height: 24px; }
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; background: rgba(255, 255, 255, .25); cursor: pointer; padding: 0; position: relative; }
.shutter::after { content: ""; position: absolute; inset: 5px; border-radius: 50%; background: #fff; transition: transform .1s; }
.shutter:active::after { transform: scale(.88); }
.cam-tip { position: absolute; top: calc(16px + env(safe-area-inset-top)); left: 0; right: 0; margin: 0; text-align: center; color: #fff; font-size: 14px; text-shadow: 0 1px 4px rgba(0, 0, 0, .6); }
.flash { position: fixed; inset: 0; background: #fff; z-index: 60; animation: flash .35s ease-out forwards; pointer-events: none; }
@keyframes flash { to { opacity: 0; } }

/* Toasts */
.toasts { position: fixed; z-index: 70; left: 50%; bottom: 24px; transform: translateX(-50%); display: grid; gap: 8px; width: min(420px, calc(100% - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow); font-size: 14px; animation: rise .2s ease-out; }
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 820px) {
  .processing { grid-template-columns: 1fr; }
  .result-grid { grid-template-columns: 1fr; }
  .result-media { position: static; }
  .result-media img { max-height: 260px; }
  .fields { grid-template-columns: 1fr; }
  .stats .pill:nth-child(n+2) { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
