:root {
  --bg: #f3f5f9; --surface: #ffffff; --surface2: #e9edf5; --text: #17202e; --muted: #68748a;
  --line: #dce2ec; --brand: #2f6bff; --brand-ink: #ffffff; --danger: #e5484d; --shadow: 0 2px 10px rgba(20, 30, 60, .10);
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10141c; --surface: #1a202b; --surface2: #252d3b; --text: #e8ecf3; --muted: #94a0b5;
    --line: #2d3646; --brand: #5b8cff; --shadow: 0 2px 10px rgba(0, 0, 0, .4);
  }
}
:root[data-theme="dark"] {
  --bg: #10141c; --surface: #1a202b; --surface2: #252d3b; --text: #e8ecf3; --muted: #94a0b5;
  --line: #2d3646; --brand: #5b8cff; --shadow: 0 2px 10px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none;
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- khung ---------- */
#home, .layer { position: fixed; inset: 0; display: flex; flex-direction: column; background: var(--bg); }
.layer { z-index: 10; }
.topbar {
  display: flex; align-items: center; gap: 6px; padding: calc(8px + var(--sat)) 10px 8px; min-height: calc(54px + var(--sat));
  background: var(--surface); border-bottom: 1px solid var(--line); flex: none;
}
.topbar .title { flex: 1; min-width: 0; font-weight: 650; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px; }
.topbar .title.tap { cursor: pointer; }
.brand { display: flex; align-items: center; gap: 8px; flex: 1; font-weight: 750; font-size: 19px; padding-left: 4px; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; flex: none; }
.icon-btn:active, .icon-btn.on { background: var(--surface2); }
.icon-btn.on { color: var(--brand); }
.text-btn { padding: 8px 12px; border-radius: 10px; color: var(--brand); font-weight: 600; }
.text-btn:active { background: var(--surface2); }
.body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.wrap { max-width: 980px; margin: 0 auto; padding: 12px; }

.tabbar { display: flex; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: var(--sab); flex: none; }
.tabbar button { flex: 1; padding: 8px 0 7px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11.5px; color: var(--muted); }
.tabbar button.on { color: var(--brand); font-weight: 600; }

.fab {
  position: absolute; right: 18px; bottom: calc(76px + var(--sab)); width: 62px; height: 62px; border-radius: 50%; z-index: 3;
  background: var(--brand); color: var(--brand-ink); display: grid; place-items: center; box-shadow: 0 6px 18px rgba(47, 107, 255, .45);
}
.fab .ic { width: 28px; height: 28px; }
.fab:active { transform: scale(.95); }

/* ---------- trang chủ ---------- */
.search { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; height: 44px; }
.search input { flex: 1; border: 0; outline: 0; background: none; min-width: 0; }
.search .ic { color: var(--muted); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 6px 13px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 13.5px; white-space: nowrap; }
.chip.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.chip.ghost { border-style: dashed; color: var(--muted); }

.docs { display: grid; gap: 12px; padding: 10px 0 110px; }
.docs.grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.doc-card { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; position: relative; cursor: pointer; display: flex; flex-direction: column; }
.doc-card .thumb { aspect-ratio: 3 / 3.6; background: var(--surface2); position: relative; overflow: hidden; }
.doc-card .thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; display: block; }
.doc-card .count { position: absolute; right: 6px; bottom: 6px; background: rgba(0, 0, 0, .62); color: #fff; font-size: 11.5px; padding: 1px 7px; border-radius: 999px; }
.doc-card .meta { padding: 8px 34px 9px 10px; min-width: 0; }
.doc-card .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card .sub { font-size: 12px; color: var(--muted); }
.doc-card .tags { font-size: 11.5px; color: var(--brand); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-card .more { position: absolute; right: 0; bottom: 4px; width: 34px; height: 40px; color: var(--muted); }
.doc-card .tick { position: absolute; left: 8px; top: 8px; width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff; background: rgba(0, 0, 0, .3); display: grid; place-items: center; color: transparent; z-index: 2; }
.doc-card.sel .tick { background: var(--brand); color: #fff; }
.doc-card.sel { outline: 2px solid var(--brand); }
.tick .ic { width: 16px; height: 16px; stroke-width: 3; }
.docs.list { grid-template-columns: 1fr; gap: 8px; }
.docs.list .doc-card { flex-direction: row; align-items: center; }
.docs.list .thumb { width: 64px; height: 78px; aspect-ratio: auto; flex: none; }
.docs.list .count { display: none; }
.docs.list .meta { flex: 1; padding: 8px 40px 8px 12px; }
.docs.list .more { top: 50%; bottom: auto; transform: translateY(-50%); }

.empty { text-align: center; color: var(--muted); padding: 60px 20px; }
.empty .ic { width: 56px; height: 56px; stroke-width: 1.2; opacity: .6; }
.empty b { display: block; color: var(--text); font-size: 16px; margin: 8px 0 4px; }

.selbar { display: flex; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: var(--sab); flex: none; }
.selbar button, .toolbar button { flex: 1; padding: 8px 2px 7px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11.5px; min-width: 58px; }
.selbar button:active, .toolbar button:active { background: var(--surface2); }
.toolbar button.on { color: var(--brand); }
.toolbar { display: flex; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: var(--sab); flex: none; overflow-x: auto; scrollbar-width: none; }
.toolbar::-webkit-scrollbar { display: none; }
.danger { color: var(--danger); }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); padding: 16px 12px; display: flex; flex-direction: column; align-items: flex-start; gap: 8px; text-align: left; }
.tile .badge { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand); }
.tile b { font-size: 14.5px; }
.tile span { font-size: 12px; color: var(--muted); }
.tile:active { transform: scale(.98); }
h3.sec { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 4px 8px; }
.card { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 13px 14px; border-bottom: 1px solid var(--line); min-height: 52px; width: 100%; text-align: left; }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row small { display: block; color: var(--muted); font-size: 12px; }
.row select { background: var(--surface2); border: 0; border-radius: 8px; padding: 7px 8px; max-width: 170px; }

/* ---------- tài liệu ---------- */
.pages { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; padding-bottom: 20px; }
.page-card { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; position: relative; cursor: pointer; }
.page-card img { width: 100%; aspect-ratio: 3 / 4; object-fit: contain; background: var(--surface2); display: block; }
.page-card .foot { display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 2px 10px; font-size: 12.5px; color: var(--muted); }
.page-card .foot .icon-btn { width: 32px; height: 32px; }
.tagline { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.tagline .chip { padding: 3px 10px; font-size: 12.5px; }

/* ---------- xem trang ---------- */
.dark { background: #0b0d12; color: #fff; }
.dark .topbar, .dark .toolbar { background: #151922; border-color: #262c38; color: #fff; }
.stage { flex: 1; min-height: 0; position: relative; display: grid; place-items: center; padding: 12px; overflow: hidden; touch-action: none; }
.stage > img, .stage > canvas { max-width: 100%; max-height: 100%; object-fit: contain; display: block; box-shadow: 0 4px 24px rgba(0, 0, 0, .5); background: #fff; }
.stage.pan { touch-action: pan-y; }
.nav-arrow { position: absolute; top: 50%; transform: translateY(-50%); width: 42px; height: 42px; border-radius: 50%; background: rgba(255, 255, 255, .14); display: grid; place-items: center; color: #fff; }
.nav-arrow.l { left: 8px; } .nav-arrow.r { right: 8px; }
.note-strip { background: #fff7d6; color: #5c4a00; padding: 8px 14px; font-size: 13px; white-space: pre-wrap; max-height: 80px; overflow: auto; flex: none; }

/* ---------- cắt ---------- */
.crop-wrap { position: relative; touch-action: none; }
.crop-wrap canvas { width: 100%; height: 100%; display: block; }
.crop-wrap svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.crop-wrap polygon { fill: rgba(47, 107, 255, .16); stroke: #4d86ff; stroke-width: 2.5; vector-effect: non-scaling-stroke; }
.handle { position: absolute; width: 48px; height: 48px; margin: -24px 0 0 -24px; display: grid; place-items: center; touch-action: none; cursor: grab; }
.handle::after { content: ""; width: 22px; height: 22px; border-radius: 50%; background: #fff; border: 4px solid #2f6bff; box-shadow: 0 1px 6px rgba(0, 0, 0, .5); }
.loupe { position: absolute; top: 10px; left: 10px; width: 116px; height: 116px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 2px 12px rgba(0, 0, 0, .6); z-index: 5; background: #000; }
.loupe.right { left: auto; right: 10px; }
.footbar { display: flex; gap: 10px; padding: 10px 12px calc(10px + var(--sab)); background: #151922; flex: none; align-items: center; }
.btn { flex: 1; padding: 12px 14px; border-radius: 12px; background: var(--surface2); font-weight: 600; text-align: center; }
.btn.primary { background: var(--brand); color: var(--brand-ink); }
.btn.red { background: var(--danger); color: #fff; }
.dark .btn:not(.primary) { background: #262c38; color: #fff; }
.btn:active { filter: brightness(.92); }

/* ---------- bộ lọc ---------- */
.filter-strip { display: flex; gap: 10px; overflow-x: auto; padding: 10px 12px; background: #151922; flex: none; scrollbar-width: none; }
.filter-strip::-webkit-scrollbar { display: none; }
.filter-strip button { flex: none; width: 74px; font-size: 12px; color: #aeb7c7; }
.filter-strip canvas { width: 74px; height: 92px; object-fit: cover; border-radius: 8px; border: 2px solid transparent; display: block; margin-bottom: 4px; background: #fff; }
.filter-strip button.on { color: #fff; font-weight: 600; }
.filter-strip button.on canvas { border-color: #4d86ff; }
.adjust { background: #151922; padding: 6px 16px; flex: none; display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; align-items: center; font-size: 12.5px; color: #aeb7c7; }
input[type=range] { width: 100%; accent-color: var(--brand); }

/* ---------- chú thích ---------- */
.mk-opts { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: #151922; flex: none; }
.swatch { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #555; flex: none; }
.swatch.on { border-color: #fff; box-shadow: 0 0 0 2px #4d86ff; }
.sigpad { background: #fff; border-radius: 12px; width: 100%; aspect-ratio: 2 / 1; touch-action: none; display: block; border: 1px dashed #9aa5b8; }
.sig-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; margin: 6px 0 12px; }
.sig-item { position: relative; background: #fff; border: 1px solid var(--line); border-radius: 10px; height: 72px; display: grid; place-items: center; cursor: pointer; }
.sig-item img { max-width: 90%; max-height: 60px; }
.sig-item .x { position: absolute; top: -8px; right: -8px; width: 24px; height: 24px; border-radius: 50%; background: var(--danger); color: #fff; display: grid; place-items: center; }
.sig-item .x .ic { width: 14px; height: 14px; }

/* ---------- máy ảnh ---------- */
.cam { background: #000; color: #fff; }
.cam video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.cam-top { position: relative; z-index: 2; display: flex; align-items: center; padding: calc(8px + var(--sat)) 10px 8px; gap: 6px; }
.cam-hint { flex: 1; text-align: center; font-size: 13.5px; text-shadow: 0 1px 3px #000; }
.cam-mid { flex: 1; position: relative; z-index: 1; display: grid; place-items: center; text-align: center; padding: 24px; }
.cam-fallback { background: rgba(20, 24, 32, .92); border-radius: 16px; padding: 20px; max-width: 340px; display: grid; gap: 10px; }
.cam-frame { width: min(78vw, 420px); aspect-ratio: 1.586; border: 2px dashed rgba(255, 255, 255, .85); border-radius: 14px; }
.cam-frame.sq { aspect-ratio: 1; width: min(62vw, 300px); }
.cam-modes { position: relative; z-index: 2; display: flex; justify-content: center; gap: 4px; padding: 6px; }
.cam-modes button { padding: 6px 14px; border-radius: 999px; font-size: 13.5px; color: #cfd6e2; text-shadow: 0 1px 3px #000; }
.cam-modes button.on { background: #fff; color: #111; font-weight: 600; text-shadow: none; }
.cam-bottom { position: relative; z-index: 2; display: flex; align-items: center; justify-content: space-around; padding: 10px 16px calc(18px + var(--sab)); background: linear-gradient(transparent, rgba(0, 0, 0, .6)); }
.cam-bottom .side { width: 84px; display: grid; place-items: center; gap: 2px; font-size: 11.5px; }
.shutter { width: 72px; height: 72px; border-radius: 50%; border: 5px solid #fff; background: rgba(255, 255, 255, .28); position: relative; }
.shutter::after { content: ""; position: absolute; inset: 5px; border-radius: 50%; background: #fff; }
.shutter:active::after { inset: 9px; }
.cam-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; z-index: 3; transition: opacity .25s; }
.pill { background: var(--brand); color: #fff; border-radius: 999px; padding: 7px 14px; font-weight: 600; font-size: 13.5px; }

/* ---------- sheet / hộp thoại ---------- */
.sheet-layer { background: transparent; justify-content: flex-end; z-index: 20; }
.backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); animation: fade .15s; }
.sheet { position: relative; background: var(--surface); color: var(--text); border-radius: 18px 18px 0 0; padding: 8px 8px calc(10px + var(--sab)); max-height: 82vh; overflow-y: auto; width: 100%; max-width: 560px; margin: 0 auto; animation: up .18s ease-out; }
.sheet-title { padding: 10px 12px 8px; font-weight: 650; color: var(--muted); font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-item { display: flex; align-items: center; gap: 14px; padding: 13px 12px; width: 100%; text-align: left; border-radius: 10px; }
.sheet-item:active { background: var(--surface2); }
.dlg-layer { background: transparent; justify-content: center; align-items: center; z-index: 20; padding: 20px; }
.dlg { position: relative; background: var(--surface); color: var(--text); border-radius: 16px; padding: 18px; width: 100%; max-width: 420px; max-height: 88vh; overflow-y: auto; animation: fade .15s; }
.dlg h4 { margin: 0 0 10px; font-size: 16.5px; }
.dlg p { margin: 0 0 14px; color: var(--muted); white-space: pre-wrap; word-break: break-word; }
.dlg .field { width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 11px 12px; outline: 0; margin-bottom: 14px; }
.dlg .field:focus { border-color: var(--brand); }
.dlg textarea.field { min-height: 130px; resize: vertical; }
.dlg .btns { display: flex; gap: 10px; }
.textview { flex: 1; margin: 12px; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 12px; outline: 0; resize: none; line-height: 1.55; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

#busy { position: fixed; inset: 0; z-index: 90; background: rgba(0, 0, 0, .45); display: grid; place-items: center; }
.busy-box { background: var(--surface); border-radius: 14px; padding: 20px 26px; display: grid; justify-items: center; gap: 12px; min-width: 180px; max-width: 80vw; text-align: center; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 4px solid var(--surface2); border-top-color: var(--brand); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#toast { position: fixed; left: 50%; bottom: calc(96px + var(--sab)); transform: translateX(-50%); z-index: 100; background: rgba(20, 24, 32, .94); color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 13.5px; max-width: 86vw; text-align: center; }
#lock { position: fixed; inset: 0; z-index: 99; background: var(--bg); display: grid; place-items: center; padding: 24px; }
.lock-box { display: grid; gap: 14px; justify-items: center; width: 100%; max-width: 300px; text-align: center; }
.lock-box input { width: 100%; text-align: center; font-size: 24px; letter-spacing: .4em; padding: 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); outline: 0; }
