:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #dde1e7;
  --text: #1a1d23;
  --muted: #646b78;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #e8efff;
  --on-accent: #ffffff;
  --ok: #15803d;
  --ok-bg: #e3f6ea;
  --warn: #92600a;
  --warn-bg: #fdf1d3;
  --bad: #b42318;
  --bad-bg: #fde7e4;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%);
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1e222b;
    --border: #2b303b;
    --text: #e8eaee;
    --muted: #9099a8;
    --accent: #5b8cff;
    --accent-hover: #7aa1ff;
    --accent-soft: #1b2744;
    --on-accent: #0b1020;
    --ok: #5fd68a;
    --ok-bg: #14301f;
    --warn: #f0c35a;
    --warn-bg: #3a2e12;
    --bad: #ff8d82;
    --bad-bg: #3d1a17;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.page { max-width: 1040px; margin: 0 auto; padding: 32px 16px 56px; }

.hero h1 { margin: 0 0 6px; font-size: 1.75rem; letter-spacing: -0.02em; }
.hero p { margin: 0 0 24px; color: var(--muted); max-width: 62ch; }

a { color: var(--accent); }

/* ---------- Tombol ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 9px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn:focus-visible, .icon-btn:focus-visible, .thumb:focus-visible, .dropzone:focus-visible,
summary:focus-visible, .numbers:focus-visible, select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger-ghost { color: var(--bad); }
.btn-icon { padding: 2px 10px; font-size: 1.25rem; line-height: 1.2; }

.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  width: 32px; height: 32px; border-radius: 8px; cursor: pointer;
  display: inline-grid; place-items: center;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; }

/* ---------- Dropzone ---------- */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 32px 16px;
  text-align: center;
  transition: border-color .15s, background .15s;
}
.dropzone.dragging { border-color: var(--accent); background: var(--accent-soft); }
.dz-icon { width: 40px; height: 40px; color: var(--accent); }
.dz-title { margin: 6px 0 14px; font-size: 1.05rem; font-weight: 600; }
.dz-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.dz-hint { margin: 14px 0 0; color: var(--muted); font-size: .85rem; }

/* ---------- Notice ---------- */
.notice {
  margin-top: 12px; padding: 10px 14px; border-radius: 10px;
  background: var(--warn-bg); color: var(--warn); font-size: .9rem;
}
.notice.error { background: var(--bad-bg); color: var(--bad); }
.notice.info { background: var(--accent-soft); color: var(--accent); }

/* ---------- Pengaturan ---------- */
.settings {
  margin-top: 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0 16px;
}
.settings summary { cursor: pointer; padding: 12px 0; font-weight: 600; }
.settings[open] summary { border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.settings-grid {
  display: grid; gap: 16px 24px; padding-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field small, .field-check small { color: var(--muted); font-size: .8rem; font-weight: 400; display: block; }
.field-check { flex-direction: row; align-items: flex-start; gap: 10px; }
.field-check input { margin-top: 4px; width: 16px; height: 16px; flex: none; }
select, input[type="number"], .numbers {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
}
input[type="number"] { width: 90px; }

/* ---------- Toolbar ---------- */
.toolbar {
  margin-top: 16px; padding: 14px 16px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  position: sticky; top: 8px; z-index: 5;
}
.toolbar[hidden] { display: none; }
.summary { margin-bottom: 12px; }
.summary-text { font-weight: 600; margin-bottom: 6px; font-variant-numeric: tabular-nums; }
.summary-text .dim { color: var(--muted); font-weight: 400; }
progress { appearance: none; width: 100%; height: 8px; border: 0; border-radius: 99px; overflow: hidden; background: var(--surface-2); display: block; }
progress::-webkit-progress-bar { background: var(--surface-2); }
progress::-webkit-progress-value { background: var(--accent); transition: width .2s; }
progress::-moz-progress-bar { background: var(--accent); }
.toolbar-row { display: flex; flex-wrap: wrap; gap: 12px 24px; justify-content: space-between; align-items: center; }
.btn-group, .export { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.inline-check { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); font-size: .85rem; }
.inline-select { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); font-size: .85rem; }
.inline-select select { padding: 6px 8px; font-size: .85rem; }

/* ---------- Hasil ---------- */
.empty { text-align: center; color: var(--muted); padding: 28px 0 8px; }
.results { list-style: none; margin: 16px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.results:empty { display: none; }

.row {
  display: grid; align-items: center; gap: 4px 14px;
  grid-template-columns: 56px minmax(0, 1.1fr) minmax(0, 1.4fr) 92px auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; box-shadow: var(--shadow);
  content-visibility: auto; contain-intrinsic-size: auto 76px;
}
.row[data-status="error"] { border-color: var(--bad); }
.row[data-status="processing"] { border-color: var(--accent); }

.thumb {
  appearance: none; padding: 0; border: 1px solid var(--border); border-radius: 8px;
  width: 56px; height: 56px; overflow: hidden; background: var(--surface-2);
  cursor: pointer; display: grid; place-items: center; color: var(--muted);
}
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb img[hidden], .thumb-ph[hidden] { display: none; }
.thumb-ph { width: 28px; height: 28px; }

.info { min-width: 0; }
.name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { color: var(--muted); font-size: .8rem; }
.row[data-status="error"] .status { color: var(--bad); }
.row[data-status="done"] .status { color: var(--ok); }
.row-bar { position: relative; margin-top: 5px; height: 4px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.row-bar::after {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 35%; border-radius: 99px;
  background: var(--accent); animation: row-slide 1.1s ease-in-out infinite;
}
@keyframes row-slide { from { transform: translateX(-100%); } to { transform: translateX(300%); } }
.row:not([data-status="processing"]) .row-bar { display: none; }

.result { min-width: 0; }
.numbers { width: 100%; font-family: var(--mono); font-size: 1rem; letter-spacing: .02em; }
.numbers:disabled { background: var(--surface-2); color: var(--muted); }
.note { font-size: .78rem; color: var(--muted); min-height: 0; }
.note:empty { display: none; }
.note.bad { color: var(--bad); }

.conf { text-align: center; }
.badge {
  display: inline-block; min-width: 56px; padding: 2px 8px; border-radius: 99px;
  font-size: .8rem; font-weight: 600; font-variant-numeric: tabular-nums;
  background: var(--surface-2); color: var(--muted);
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge:empty { display: none; }

.actions { display: flex; gap: 2px; }

/* ---------- Dialog ---------- */
.viewer { border: 0; padding: 0; border-radius: var(--radius); background: var(--surface); color: var(--text); width: min(92vw, 860px); max-height: 92vh; }
.viewer::backdrop { background: rgb(0 0 0 / 55%); }
.viewer-inner { display: flex; flex-direction: column; max-height: 92vh; margin: 0; }
.viewer-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.viewer-head strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-body { padding: 14px; overflow: auto; }
.viewer-body img { max-width: 100%; max-height: 62vh; display: block; margin: 0 auto 12px; border-radius: 8px; }
.viewer-body pre { white-space: pre-wrap; word-break: break-word; background: var(--surface-2); padding: 10px; border-radius: 8px; font-family: var(--mono); font-size: .85rem; }

/* ---------- Scan webcam ---------- */
.scanner { border: 0; padding: 0; border-radius: var(--radius); background: var(--surface); color: var(--text); width: min(94vw, 760px); max-height: 94vh; }
.scanner::backdrop { background: rgb(0 0 0 / 60%); }
.scanner-inner { display: flex; flex-direction: column; max-height: 94vh; overflow: auto; }
.scanner-stage { position: relative; background: #000; aspect-ratio: 16 / 9; }
.scanner-stage video { width: 100%; height: 100%; object-fit: contain; display: block; } /* tidak dicermin: angka harus terbaca normal */
.scan-guide {
  position: absolute; inset: 22% 8%; border: 2px dashed rgb(255 255 255 / 70%); border-radius: 12px;
  pointer-events: none; box-shadow: 0 0 0 9999px rgb(0 0 0 / 22%);
}
.scan-flash { position: absolute; inset: 0; background: rgb(34 197 94 / 55%); opacity: 0; pointer-events: none; }
.scan-flash.on { animation: scan-flash .6s ease-out; }
@keyframes scan-flash { from { opacity: 1; } to { opacity: 0; } }
.scan-status { margin: 0; padding: 10px 14px; font-weight: 600; min-height: 3em; background: var(--surface-2); }
.scan-status[data-kind="ok"] { color: var(--ok); background: var(--ok-bg); }
.scan-status[data-kind="warn"] { color: var(--warn); background: var(--warn-bg); }
.scan-status[data-kind="error"] { color: var(--bad); background: var(--bad-bg); }
.scanner-controls { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; padding: 12px 14px; }
.scanner-controls select { max-width: 100%; }
.scanner-controls .btn-primary { margin-left: auto; }
.scan-tip { margin: 0; padding: 0 14px 10px; color: var(--muted); font-size: .85rem; }
.scan-export { display: flex; flex-wrap: wrap; gap: 8px 10px; align-items: center; padding: 0 14px 10px; }
.scan-count { margin-right: auto; font-weight: 600; font-variant-numeric: tabular-nums; }
.scan-found { list-style: none; margin: 0; padding: 0 14px 14px; display: flex; flex-direction: column; gap: 6px; }
.scan-found:empty { display: none; }
.scan-found li { font-family: var(--mono); background: var(--ok-bg); color: var(--ok); border-radius: 8px; padding: 6px 10px; }

.footer { margin-top: 36px; color: var(--muted); font-size: .8rem; text-align: center; }

/* ---------- Layar kecil ---------- */
@media (max-width: 720px) {
  .page { padding-top: 20px; }
  .row {
    grid-template-columns: 56px minmax(0, 1fr) auto;
    grid-template-areas:
      "thumb info    actions"
      "thumb result  conf";
  }
  .thumb { grid-area: thumb; align-self: start; }
  .info { grid-area: info; }
  .result { grid-area: result; }
  .conf { grid-area: conf; text-align: right; }
  .actions { grid-area: actions; }
  .toolbar { position: static; }
  .toolbar-row, .btn-group, .export { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
