:root {
  --bg: #f4f5f7; --surface: #fff; --text: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --accent: #0e9f8e; --accent-ink: #fff; --accent-soft: #d5f3ef; --warn: #f59e0b; --danger: #dc2626;
  --ok: #22c55e; --radius: 16px;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0b1220; --surface: #141c2e; --text: #e8edf5; --muted: #94a3b8; --line: #243049; --accent-soft: #0f3b38; }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body { overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; flex: none; }
[hidden] { display: none !important; }

.screen { position: fixed; inset: 0; display: none; flex-direction: column; background: var(--bg); }
.screen.active { display: flex; }
.screen.dark { background: #000; color: #fff; --surface: #111; --line: #2a2a2a; --muted: #a3a3a3; }

.appbar { display: flex; align-items: center; gap: 6px; padding: calc(10px + var(--safe-t)) 10px 10px; min-height: 56px; }
.appbar .title { flex: 1; font-size: 18px; margin: 0 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand { display: flex; align-items: center; gap: 10px; font-size: 20px; margin: 0 6px; letter-spacing: -.01em; }
.brand img { border-radius: 8px; }
.spacer { flex: 1; }
.content { flex: 1; overflow-y: auto; padding: 4px 14px 120px; -webkit-overflow-scrolling: touch; }
.sub { color: var(--muted); margin: 0 4px 12px; font-size: 14px; }

.icon-btn { display: inline-grid; place-items: center; width: 44px; height: 44px; border: 0; border-radius: 50%; background: transparent; position: relative; }
.icon-btn:active { background: rgba(127,127,127,.18); }
.icon-btn.lg { width: 54px; height: 54px; }
.glass { background: rgba(0,0,0,.42) !important; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); color: #fff; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 0; border-radius: 999px; padding: 12px 18px; font-weight: 600; background: var(--surface); color: var(--text); box-shadow: 0 1px 0 var(--line) inset, 0 0 0 1px var(--line) inset; }
.btn.primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 18px rgba(14,159,142,.35); }
.btn.secondary { background: var(--surface); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.danger { color: var(--danger); }
.btn.big { padding: 16px 24px; font-size: 17px; }
.btn.sm { padding: 8px 16px; }
.btn:disabled { opacity: .5; }
.btn:active { transform: scale(.98); }
.link { border: 0; background: none; color: var(--accent); font-weight: 700; padding: 4px 8px; }

.fab-row { position: absolute; left: 0; right: 0; bottom: 0; display: flex; gap: 10px; justify-content: center; align-items: center; padding: 14px 14px calc(18px + var(--safe-b)); background: linear-gradient(transparent, var(--bg) 35%); }

/* Danh sách tài liệu */
.doc-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.doc-card { background: var(--surface); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); text-align: left; padding: 0; display: flex; flex-direction: column; }
.doc-card .thumb { aspect-ratio: 3/4; background: #ccd3dd center/cover no-repeat; position: relative; }
.doc-card .meta { padding: 10px 12px 12px; }
.doc-card .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card .info { color: var(--muted); font-size: 12px; margin-top: 2px; }
.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty h2 { color: var(--text); font-size: 20px; margin: 18px 0 6px; }
.empty-art { width: 96px; height: 96px; margin: 0 auto; border-radius: 28px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.empty-art svg { width: 48px; height: 48px; }

/* Camera */
.cam-stage { position: absolute; inset: 0; overflow: hidden; touch-action: manipulation; }
#video { width: 100%; height: 100%; object-fit: cover; background: #000; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.cam-top { position: absolute; top: 0; left: 0; right: 0; display: flex; gap: 8px; align-items: center; padding: calc(10px + var(--safe-t)) 12px 0; }
.pill { border: 0; border-radius: 999px; padding: 10px 14px; font-size: 12px; font-weight: 800; letter-spacing: .06em; }
.pill.on { background: var(--accent) !important; }
.cam-status { position: absolute; left: 0; right: 0; bottom: calc(132px + var(--safe-b)); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.hint { background: rgba(0,0,0,.6); padding: 9px 16px; border-radius: 999px; font-weight: 600; font-size: 15px; transition: background .2s; }
.hint.ok { background: rgba(22,163,74,.9); }
.hint.warn { background: rgba(180,120,0,.85); }
.meter { display: flex; align-items: center; gap: 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; background: rgba(0,0,0,.45); padding: 5px 10px; border-radius: 999px; }
.meter .bar { width: 110px; height: 6px; border-radius: 3px; background: rgba(255,255,255,.2); overflow: hidden; }
.meter .bar i { display: block; height: 100%; width: 0; background: var(--warn); transition: width .12s, background .2s; }
.meter .bar i.ok { background: var(--ok); }
.badge { font-size: 12px; background: rgba(0,0,0,.55); padding: 4px 10px; border-radius: 999px; color: #ddd; }
.cam-bottom { position: absolute; left: 0; right: 0; bottom: 0; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; justify-items: center; padding: 16px 20px calc(22px + var(--safe-b)); background: linear-gradient(transparent, rgba(0,0,0,.65)); }
.shutter { width: 84px; height: 84px; border: 0; background: none; position: relative; padding: 0; }
.shutter .ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.shutter .track { stroke: rgba(255,255,255,.85); stroke-width: 5; fill: none; }
.shutter .progress { stroke: var(--ok); stroke-width: 6; fill: none; stroke-dasharray: 289; stroke-dashoffset: 289; transition: stroke-dashoffset .1s linear; }
.shutter .core { position: absolute; inset: 11px; border-radius: 50%; background: #fff; transition: transform .1s; }
.shutter:active .core { transform: scale(.9); }
.shutter.busy .core { background: #9ca3af; }
.thumb-stack { width: 60px; height: 74px; border: 2px solid #fff; border-radius: 10px; background: #222; padding: 0; position: relative; overflow: visible; }
.thumb-stack:disabled { opacity: .35; }
.thumb-stack img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; display: block; }
.thumb-stack img:not([src]) { visibility: hidden; }
.thumb-stack .count { position: absolute; top: -9px; right: -9px; min-width: 24px; height: 24px; border-radius: 12px; background: var(--accent); color: #fff; font-size: 13px; font-weight: 700; display: grid; place-items: center; padding: 0 6px; }
.thumb-stack .done-label { position: absolute; bottom: -20px; left: 0; right: 0; font-size: 12px; font-weight: 600; text-align: center; }
.focus-ring { position: absolute; width: 74px; height: 74px; margin: -37px 0 0 -37px; border: 2px solid #ffd54a; border-radius: 12px; opacity: 0; pointer-events: none; transform: scale(1.4); transition: opacity .2s, transform .25s; }
.focus-ring.show { opacity: 1; transform: scale(1); }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.go { animation: flash .35s ease-out; }
@keyframes flash { 0% { opacity: .85; } 100% { opacity: 0; } }
.cam-error { position: absolute; inset: 0; background: #0b0b0b; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 30px; gap: 6px; }
.cam-error svg { width: 46px; height: 46px; color: var(--warn); }
.cam-error p { color: #bbb; max-width: 420px; line-height: 1.5; }
.cam-error .row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 10px; }

/* Lưới trang */
.page-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 14px; }
.page-card { position: relative; border: 0; padding: 0; background: var(--surface); border-radius: 12px; overflow: hidden; aspect-ratio: 3/4; box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 0 0 1px var(--line); }
.page-card img { width: 100%; height: 100%; object-fit: contain; background: #e5e7eb; display: block; }
.page-card .num { position: absolute; left: 8px; bottom: 8px; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; }
.page-card .warn { position: absolute; right: 8px; top: 8px; background: var(--warn); color: #000; border-radius: 999px; padding: 3px; display: grid; }
.page-card .warn svg { width: 16px; height: 16px; }
.page-card .proc { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(255,255,255,.55); }
@media (prefers-color-scheme: dark) { .page-card .proc { background: rgba(0,0,0,.45); } }

.actionbar { display: flex; justify-content: space-around; gap: 4px; padding: 8px 8px calc(8px + var(--safe-b)); background: var(--surface); border-top: 1px solid var(--line); }
.actionbar.dark { background: #0a0a0a; border-color: #222; }
.act { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; border: 0; background: none; padding: 8px 4px; border-radius: 12px; font-size: 12px; font-weight: 600; }
.act:active { background: rgba(127,127,127,.15); }
.act.accent { color: var(--accent); }
.act:disabled { opacity: .35; }

/* Trình chỉnh sửa */
.warning { display: flex; align-items: center; gap: 8px; margin: 0 12px 8px; padding: 8px 12px; background: rgba(245,158,11,.18); color: #fcd34d; border-radius: 12px; font-size: 14px; }
.warning svg { width: 18px; height: 18px; }
.warning span { flex: 1; }
.seg { display: flex; margin: 0 auto 6px; background: #1d1d1d; border-radius: 999px; padding: 3px; }
.seg button { border: 0; background: none; padding: 7px 16px; border-radius: 999px; font-size: 14px; font-weight: 600; color: #aaa; }
.seg button.active { background: #fff; color: #000; }
.ed-stage { flex: 1; position: relative; min-height: 0; display: grid; place-items: center; padding: 14px; }
.crop-wrap { position: relative; }
#crop-canvas { display: block; }
#crop-svg { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; overflow: visible; stroke: none; fill: none; }
#crop-svg .poly { fill: rgba(14,159,142,.18); stroke: #2dd4bf; stroke-width: 2; }
#crop-svg .handle { fill: rgba(255,255,255,.2); stroke: #fff; stroke-width: 2.5; }
#crop-svg .handle.active { fill: rgba(45,212,191,.55); }
#loupe { position: absolute; top: -10px; left: -10px; width: 128px; height: 128px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 6px 20px rgba(0,0,0,.5); pointer-events: none; background: #000; }
#loupe.right { left: auto; right: -10px; }
.result-wrap { position: absolute; inset: 14px; display: flex; align-items: center; justify-content: center; overflow: hidden; }
#result-img { max-width: 100%; max-height: 100%; object-fit: contain; box-shadow: 0 6px 30px rgba(0,0,0,.6); background: #fff; }
.ed-tools { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 8px 12px 12px; }
.ed-tools .row, #tools-crop { flex-direction: row; justify-content: center; display: flex; gap: 10px; }
.tool { display: inline-flex; align-items: center; gap: 6px; border: 1px solid #333; background: #161616; border-radius: 999px; padding: 9px 14px; font-size: 14px; }
.tool svg { width: 18px; height: 18px; }
.chips { display: flex; gap: 8px; overflow-x: auto; max-width: 100%; padding-bottom: 2px; }
.chip { border: 1px solid #333; background: #161616; color: #ddd; border-radius: 12px; padding: 0; overflow: hidden; display: flex; flex-direction: column; align-items: center; width: 74px; flex: none; font-size: 12px; font-weight: 600; }
.chip img { width: 74px; height: 74px; object-fit: cover; background: #333; display: block; }
.chip span { padding: 5px 0; }
.chip.active { border-color: var(--accent); color: #fff; background: #0f3b38; }

.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid rgba(127,127,127,.3); border-top-color: var(--accent); animation: spin .8s linear infinite; }
.result-wrap .spinner { position: absolute; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Hộp thoại */
dialog.sheet { border: 0; padding: 0; width: min(560px, 100%); max-height: 92vh; margin: auto auto 0; border-radius: 22px 22px 0 0; background: var(--surface); color: var(--text); }
@media (min-width: 640px) { dialog.sheet { margin: auto; border-radius: 22px; } }
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog form { padding: 18px 18px calc(16px + var(--safe-b)); display: flex; flex-direction: column; gap: 12px; max-height: 92vh; overflow-y: auto; }
dialog h3 { margin: 4px 2px; font-size: 19px; }
.formats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.fmt { display: flex; flex-direction: column; border: 1.5px solid var(--line); border-radius: 14px; padding: 10px 12px; cursor: pointer; position: relative; }
.fmt input { position: absolute; opacity: 0; }
.fmt b { font-size: 15px; }
.fmt small { color: var(--muted); font-size: 12px; margin-top: 2px; }
.fmt:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.opts { display: grid; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 600; }
.field select, .field input { font-size: 15px; color: var(--text); padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-weight: 400; }
.switch { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; padding: 4px 0; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; margin-top: 1px; }
fieldset { border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px 14px; display: grid; gap: 10px; margin: 0; }
legend { font-weight: 700; padding: 0 6px; }
.note { color: var(--muted); font-size: 13px; margin: 0; }
.progress-text { min-height: 20px; font-size: 14px; color: var(--muted); }
.progress-text .btn { margin-top: 8px; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.dlg-actions .ghost:first-child { margin-right: auto; }

.toast { position: fixed; left: 50%; bottom: calc(100px + var(--safe-b)); transform: translate(-50%, 20px); background: rgba(15,23,42,.94); color: #fff; padding: 11px 18px; border-radius: 12px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; max-width: 90vw; text-align: center; z-index: 100; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
