:root {
  --bg: #0f0f1a;
  --panel: #1b1a3a;
  --panel2: #24234a;
  --navy: #3C3B6E;
  --red: #B22234;
  --text: #f5f5f7;
  --muted: #a0a0b8;
  --ok: #2ecc71;
  --danger: #c0392b;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  width: 100%; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation; user-select: none;
}
button { font: inherit; color: inherit; }
.hidden { display: none !important; }
#app { position: relative; width: 100%; height: 100%; display: flex; flex-direction: column; }

/* —— Top bar —— */
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  height: calc(60px + var(--safe-t)); padding: var(--safe-t) 8px 0 14px;
  background: var(--navy); border-bottom: 3px solid var(--red); flex-shrink: 0; z-index: 25;
}
.brand-link { background: none; border: 0; display: flex; align-items: center; height: 100%; min-width: 0; cursor: pointer; }
#brand-logo { height: 42px; width: auto; max-width: 100%; display: block; }
.top-actions { display: flex; gap: 2px; }
.icon-btn {
  width: 48px; height: 48px; border: 0; border-radius: 50%; background: transparent; color: #fff;
  font-size: 22px; font-weight: 700; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.icon-btn:active { background: rgba(255,255,255,0.14); }
#btn-help { font-size: 24px; }

/* —— Camera stage —— */
#stage { position: relative; flex: 1; background: #000; overflow: hidden; min-height: 0; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 2; }
#live-quad { fill: rgba(60, 59, 110, 0.22); stroke: #6fd3ff; stroke-width: 3; stroke-linejoin: round; transition: opacity .2s; }
#level {
  --bx: 0px; --by: 0px; position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px; z-index: 3; pointer-events: none;
  border-radius: 50%; border: 1.5px solid rgba(255,255,255,.55); background: radial-gradient(circle, transparent 7px, rgba(255,255,255,.45) 7px 8px, transparent 8.5px);
  transition: opacity .25s, border-color .2s;
}
#level b {
  position: absolute; left: 50%; top: 50%; width: 12px; height: 12px; margin: -6px 0 0 -6px; border-radius: 50%;
  background: rgba(255,255,255,.85); transform: translate(var(--bx), var(--by)); transition: transform .12s linear, background .2s;
}
#level.ok { border-color: #3ddc84; }
#level.ok b { background: #3ddc84; }
#level.far { opacity: .25; }
#stage.code-on #level { display: none; }
#flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; z-index: 10; transition: opacity 120ms linear; }
#flash.on { opacity: 0.9; transition: none; }
#status {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 8;
  background: rgba(0,0,0,0.72); padding: 10px 18px; border-radius: 999px; font-size: 15px; font-weight: 600;
  max-width: 92%; text-align: center; pointer-events: none;
}
#auto-badge {
  position: absolute; top: 12px; right: 12px; z-index: 9; border: 2px solid rgba(255,255,255,0.85);
  background: var(--red); color: #fff; font-weight: 800; font-size: 14px; padding: 8px 14px; border-radius: 999px; cursor: pointer;
}
#auto-badge #auto-hint { font-weight: 600; opacity: 0.9; margin-left: 4px; }
#stage.auto-on #status { top: 64px; }
.stage-btn {
  position: absolute; left: 12px; bottom: 12px; z-index: 6; width: 52px; height: 52px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,0.5); font-size: 22px; cursor: pointer;
}
#cam-empty { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 24px; text-align: center; color: var(--muted); z-index: 3; }

/* —— Bottom controls —— */
#controls {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; justify-items: center;
  padding: 14px 12px calc(14px + var(--safe-b)); background: var(--panel); flex-shrink: 0; z-index: 20;
}
.side-btn {
  display: flex; flex-direction: column; align-items: center; gap: 4px; border: 0; background: none; cursor: pointer;
  min-width: 84px; min-height: 76px; justify-content: center; border-radius: 14px; color: var(--text);
}
.side-btn:active { background: rgba(255,255,255,0.08); }
.side-ico {
  width: 52px; height: 52px; border-radius: 12px; background: var(--panel2); display: flex; align-items: center; justify-content: center;
  font-size: 24px; overflow: hidden; border: 2px solid rgba(255,255,255,0.15);
}
.side-ico img { width: 100%; height: 100%; object-fit: cover; }
.side-lbl { font-size: 13px; font-weight: 700; color: var(--muted); }
#btn-pages.has-pages .side-ico { border-color: #fff; }
#btn-pages.has-pages .side-lbl { color: #fff; }
#btn-shutter {
  position: relative; width: 92px; height: 92px; border-radius: 50%; border: 0; background: transparent; cursor: pointer;
}
#btn-shutter svg { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
#steady-ring { fill: none; stroke: rgba(255,255,255,0.9); stroke-width: 6; stroke-dasharray: 289; stroke-dashoffset: 0; }
#btn-shutter.auto #steady-ring { stroke: var(--red); stroke-dashoffset: var(--ring, 289); stroke-width: 8; }
#btn-shutter.auto::before { content: ''; position: absolute; inset: 4px; border-radius: 50%; border: 6px solid rgba(255,255,255,0.35); }
.shutter-core { position: absolute; inset: 13px; border-radius: 50%; background: #fff; transition: transform .1s; box-shadow: 0 4px 18px rgba(0,0,0,.4); }
#btn-shutter:active .shutter-core { transform: scale(0.9); }
#btn-shutter:disabled { opacity: .5; }

/* —— Buttons —— */
.btn {
  border: none; border-radius: 999px; font-size: 17px; font-weight: 700; cursor: pointer; color: var(--text);
  background: var(--navy); min-height: 56px; padding: 0 20px; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  text-decoration: none; transition: transform .1s, opacity .15s;
}
.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: .45; pointer-events: none; }
.btn-primary { background: var(--red); box-shadow: 0 6px 18px rgba(178,34,52,0.4); }
.btn-ghost { background: var(--panel2); }
.btn.wide { width: 100%; }
.btn.btn-buy {
  display: flex; width: 100%; min-height: 60px; font-size: 19px; margin-top: 18px;
  background: linear-gradient(135deg, var(--red), #d23a4e); color: #fff; border: 2px solid rgba(255,255,255,0.9);
  box-shadow: 0 6px 18px rgba(178,34,52,0.4);
}

/* —— Full screens —— */
.screen { position: absolute; inset: 0; z-index: 30; background: var(--bg); display: flex; flex-direction: column; }
.screen-head {
  display: flex; align-items: center; gap: 8px; padding: calc(6px + var(--safe-t)) 8px 6px; min-height: calc(62px + var(--safe-t));
  background: var(--navy); border-bottom: 3px solid var(--red); flex-shrink: 0;
}
.screen-head h2 { flex: 1; font-size: 19px; text-align: center; }
.head-spacer { width: 48px; }
.screen-body { flex: 1; overflow-y: auto; padding: 16px; padding-bottom: calc(24px + var(--safe-b)); }
.bar {
  display: flex; gap: 10px; padding: 12px 12px calc(12px + var(--safe-b)); background: var(--panel); flex-shrink: 0;
}
.bar .btn { flex: 1; padding: 0 10px; }
.bar .btn-primary { flex: 1.3; }
.note { color: var(--muted); text-align: center; padding: 8px 16px; font-size: 14px; min-height: 20px; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.45; margin-top: 6px; }
.hint.center { text-align: center; }
.warn { background: rgba(178,34,52,.18); border: 1px solid var(--red); padding: 12px; border-radius: 12px; margin-top: 12px; font-size: 14px; line-height: 1.4; }

/* Crop */
#crop-area { position: relative; flex: 1; min-height: 0; background: #000; overflow: hidden; touch-action: none; }
#crop-canvas { position: absolute; }
#crop-svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#crop-poly { fill: rgba(111, 211, 255, 0.14); stroke: #6fd3ff; stroke-width: 2.5; stroke-linejoin: round; }
.handle {
  position: absolute; width: 56px; height: 56px; margin: -28px 0 0 -28px; border-radius: 50%; z-index: 3; touch-action: none; cursor: grab;
}
.handle::after {
  content: ''; position: absolute; inset: 14px; border-radius: 50%; background: rgba(178,34,52,0.35); border: 3px solid #fff; box-shadow: 0 0 0 2px rgba(0,0,0,.4);
}
.handle.drag::after { background: rgba(111,211,255,.4); }
#loupe { position: absolute; width: 120px; height: 120px; border-radius: 50%; border: 3px solid #fff; z-index: 4; pointer-events: none; box-shadow: 0 4px 16px rgba(0,0,0,.6); }

/* Enhance */
#enh-view { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 12px; background: #000; }
#enh-canvas { max-width: 100%; max-height: 100%; box-shadow: 0 4px 20px rgba(0,0,0,.5); background: #fff; }
#filters { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; padding: 12px; background: var(--panel); }
.fchip { border: 2px solid transparent; border-radius: 12px; background: var(--panel2); padding: 6px 4px 8px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 5px; font-size: 12.5px; font-weight: 700; min-height: 96px; }
.fchip canvas { width: 54px; height: 64px; object-fit: cover; border-radius: 6px; background: #fff; }
.fchip[aria-checked="true"] { border-color: #fff; background: var(--navy); }

/* Doc */
#doc-title { flex: 1; background: none; border: 0; font-size: 19px; font-weight: 800; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-height: 48px; }
#doc-title .pen { opacity: .7; font-size: 16px; }
#page-list { list-style: none; flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.page-row { display: flex; align-items: center; gap: 10px; background: var(--panel); border-radius: 14px; padding: 8px; }
.page-row .pthumb { width: 64px; height: 84px; border-radius: 8px; overflow: hidden; background: #fff; flex-shrink: 0; border: 0; padding: 0; cursor: pointer; }
.page-row .pthumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.page-row .pnum { flex: 1; font-weight: 800; font-size: 17px; }
.page-row .pnum small { display: block; color: var(--muted); font-weight: 600; font-size: 12px; margin-top: 2px; }
.page-row .pbtn { width: 46px; height: 46px; border-radius: 12px; border: 0; background: var(--panel2); font-size: 18px; cursor: pointer; flex-shrink: 0; }
.page-row .pbtn:disabled { opacity: .3; }
.page-row .pbtn.del { background: rgba(192,57,43,.25); }

/* Library */
#doc-list { list-style: none; margin-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.doc-row { display: flex; align-items: center; gap: 12px; background: var(--panel); border-radius: 14px; padding: 8px; }
.doc-row .open { flex: 1; display: flex; align-items: center; gap: 12px; background: none; border: 0; text-align: left; cursor: pointer; min-width: 0; min-height: 76px; }
.doc-row .dthumb { width: 56px; height: 72px; border-radius: 8px; background: #fff; overflow: hidden; flex-shrink: 0; }
.doc-row .dthumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.doc-row .dname { font-weight: 800; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-row .dmeta { color: var(--muted); font-size: 13px; margin-top: 3px; }
.doc-row .dtxt { min-width: 0; }
.doc-row .pbtn { width: 46px; height: 46px; border-radius: 12px; border: 0; background: var(--panel2); font-size: 18px; cursor: pointer; flex-shrink: 0; }
.doc-row .pbtn.del { background: rgba(192,57,43,.25); }
.storage { margin-top: 20px; }
.meter { height: 8px; border-radius: 4px; background: var(--panel2); overflow: hidden; }
.meter span { display: block; height: 100%; width: 0; background: var(--ok); }
.meter span.high { background: var(--red); }

/* Sheets */
#scrim { position: absolute; inset: 0; background: rgba(0,0,0,.55); z-index: 40; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 41; background: var(--panel); border-radius: 22px 22px 0 0;
  max-height: 88%; display: flex; flex-direction: column; box-shadow: 0 -10px 40px rgba(0,0,0,.5); animation: up .18s ease-out;
}
.sheet.tall { height: 88%; }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 8px 6px 20px; border-bottom: 1px solid rgba(255,255,255,.08); }
.sheet-head h2 { font-size: 19px; }
.sheet-body { overflow-y: auto; padding: 16px; padding-bottom: calc(22px + var(--safe-b)); flex: 1; display: flex; flex-direction: column; gap: 14px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.span2 { grid-column: span 2; }
.tile { border: 0; border-radius: 16px; background: var(--panel2); min-height: 104px; font-size: 16px; font-weight: 800; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.tile span { font-size: 30px; font-weight: 800; }
.tile:active { background: var(--navy); }
.check { display: flex; align-items: center; gap: 10px; font-size: 15px; color: var(--muted); min-height: 44px; }
.check input { width: 22px; height: 22px; accent-color: var(--red); }

.toggle { display: flex; align-items: center; gap: 12px; min-height: 64px; cursor: pointer; }
.toggle > span { flex: 1; display: flex; flex-direction: column; gap: 3px; }
.toggle b { font-size: 17px; }
.toggle small { color: var(--muted); font-size: 13px; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i { width: 58px; height: 34px; border-radius: 17px; background: #3a3960; position: relative; flex-shrink: 0; transition: background .15s; }
.toggle i::after { content: ''; position: absolute; top: 3px; left: 3px; width: 28px; height: 28px; border-radius: 50%; background: #fff; transition: transform .15s; }
.toggle input:checked + i { background: var(--red); }
.toggle input:checked + i::after { transform: translateX(24px); }
.field { display: flex; flex-direction: column; gap: 8px; }
.field-lbl { font-weight: 700; color: var(--muted); font-size: 14px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chips button { flex: 1; min-height: 48px; border-radius: 12px; border: 2px solid transparent; background: var(--panel2); font-weight: 700; font-size: 14px; cursor: pointer; padding: 0 8px; }
.chips button[aria-checked="true"] { background: var(--navy); border-color: #fff; }
select { background: #12122a; border: 1px solid rgba(255,255,255,.15); color: var(--text); border-radius: 12px; padding: 12px; font-size: 16px; min-height: 52px; }

#ocr-text { flex: 1; min-height: 180px; width: 100%; resize: none; background: #12122a; color: var(--text); border: 1px solid rgba(255,255,255,.12); border-radius: 12px; padding: 12px; font-size: 15px; line-height: 1.45; user-select: text; font-family: inherit; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.row3 .btn { padding: 0 8px; }
.progress-bar { height: 10px; border-radius: 5px; background: var(--panel2); overflow: hidden; }
.progress-bar span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--red), #e05a6b); transition: width .2s; }

#help-list { list-style: none; display: flex; flex-direction: column; gap: 14px; }
#help-list li { display: flex; gap: 14px; align-items: flex-start; }
#help-list .hi { font-size: 26px; width: 40px; text-align: center; flex-shrink: 0; }
#help-list b { display: block; font-size: 16px; margin-bottom: 2px; }
#help-list p { color: #cfcfe0; font-size: 14.5px; line-height: 1.45; }

#toast {
  position: absolute; left: 50%; bottom: calc(110px + var(--safe-b)); transform: translateX(-50%); z-index: 70; pointer-events: none;
  background: rgba(0,0,0,.8); color: #fff; padding: 7px 14px; border-radius: 999px; font-size: 13px; font-weight: 600; white-space: nowrap;
}
#busy { position: absolute; inset: 0; z-index: 60; background: rgba(10,10,20,.72); display: flex; align-items: center; justify-content: center; }
.busy-box { background: var(--panel); border-radius: 18px; padding: 24px; width: min(320px, 84%); display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center; }
.busy-box .progress-bar { width: 100%; }
.spinner { width: 40px; height: 40px; border-radius: 50%; border: 4px solid rgba(255,255,255,.2); border-top-color: var(--red); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 700px) {
  .sheet { left: 50%; right: auto; width: 560px; transform: translateX(-50%); animation: none; }
  #page-list, .screen-body { max-width: 760px; width: 100%; margin: 0 auto; }
}
@media (orientation: landscape) and (max-height: 500px) {
  #app { flex-direction: row; flex-wrap: wrap; }
  #topbar { width: 100%; height: 48px; }
  #brand-logo { height: 32px; }
  #stage { height: calc(100% - 48px); }
  #controls { grid-template-columns: 1fr; grid-template-rows: 1fr auto 1fr; height: calc(100% - 48px); padding: 8px; }
  .side-btn { min-height: 64px; }
}
.icon-btn.pill { width: auto; padding: 0 16px; border-radius: 999px; font-size: 16px; background: rgba(255,255,255,0.14); height: 44px; }

/* —— Code mode (QR + barcodes) —— */
#mode-switch {
  position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); z-index: 6; display: flex; padding: 3px;
  background: rgba(0,0,0,0.55); border-radius: 999px; border: 1px solid rgba(255,255,255,0.25);
}
#mode-switch button {
  border: 0; background: none; color: #fff; font-weight: 800; font-size: 15px; min-width: 72px; min-height: 40px;
  padding: 0 14px; border-radius: 999px; cursor: pointer; opacity: .75;
}
#mode-switch button[aria-checked="true"] { background: #fff; color: var(--navy); opacity: 1; }
#code-frame, #code-hint, #code-box { display: none; }
#stage.code-on #code-frame, #stage.code-on #code-hint { display: block; }
#stage.code-on #code-box { display: inline; }
#stage.code-on #live-quad, #stage.code-on #auto-badge { display: none !important; }
#stage.code-on #status { top: 12px; }
#code-frame { position: absolute; left: 50%; top: 46%; width: min(64vw, 64vh, 300px); aspect-ratio: 1; transform: translate(-50%, -50%); z-index: 2; pointer-events: none; }
#code-frame i { position: absolute; width: 34px; height: 34px; border: 0 solid rgba(255,255,255,.9); }
#code-frame i:nth-child(1) { left: 0; top: 0; border-width: 4px 0 0 4px; border-radius: 10px 0 0 0; }
#code-frame i:nth-child(2) { right: 0; top: 0; border-width: 4px 4px 0 0; border-radius: 0 10px 0 0; }
#code-frame i:nth-child(3) { right: 0; bottom: 0; border-width: 0 4px 4px 0; border-radius: 0 0 10px 0; }
#code-frame i:nth-child(4) { left: 0; bottom: 0; border-width: 0 0 4px 4px; border-radius: 0 0 0 10px; }
#code-hint {
  position: absolute; left: 50%; bottom: 70px; transform: translateX(-50%); z-index: 3; pointer-events: none; white-space: nowrap;
  background: rgba(0,0,0,.55); padding: 6px 14px; border-radius: 999px; font-size: 14px; font-weight: 600; color: #fff;
}
#code-box { fill: rgba(46, 204, 113, 0.22); stroke: var(--ok); stroke-width: 4; stroke-linejoin: round; }
#controls.code-on .shutter-core::after {
  content: ''; position: absolute; inset: 20px; border-radius: 4px;
  background: conic-gradient(var(--navy) 25%, transparent 0 50%, var(--navy) 0 75%, transparent 0) 0 0 / 50% 50%;
}
.code-type { color: var(--muted); font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
.code-value {
  background: #12122a; border: 1px solid rgba(255,255,255,.12); border-radius: 12px; padding: 12px 14px; font-size: 17px; line-height: 1.4;
  word-break: break-word; white-space: pre-wrap; user-select: text; max-height: 32vh; overflow-y: auto;
}
.code-value.big { font-size: 22px; font-weight: 800; letter-spacing: .03em; }
.kv { display: flex; align-items: center; gap: 10px; background: #12122a; border-radius: 12px; padding: 8px 8px 8px 14px; margin-top: 8px; }
.kv > span { flex: 1; min-width: 0; }
.kv small { display: block; color: var(--muted); font-size: 12px; font-weight: 700; }
.kv b { display: block; font-size: 17px; word-break: break-all; user-select: text; }
.kv .pbtn { width: 46px; height: 46px; border-radius: 12px; border: 0; background: var(--panel2); font-size: 18px; cursor: pointer; flex-shrink: 0; }
.code-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.code-actions .btn { padding: 0 10px; font-size: 16px; }
.code-actions .btn.span2 { grid-column: span 2; }
.link-row {
  width: 100%; margin-top: 14px; display: flex; align-items: center; gap: 8px; min-height: 56px; padding: 0 16px; border: 0; border-radius: 14px;
  background: var(--panel); font-size: 16px; font-weight: 700; cursor: pointer; text-align: left;
}
.link-row span { color: var(--muted); font-weight: 600; flex: 1; }
.link-row b { font-size: 22px; color: var(--muted); }
#code-list { list-style: none; display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.code-row { display: flex; align-items: center; gap: 8px; background: var(--panel); border-radius: 14px; padding: 8px; }
.code-row .open { flex: 1; min-width: 0; background: none; border: 0; text-align: left; cursor: pointer; min-height: 56px; padding: 0 6px; }
.code-row .cval { font-weight: 800; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.code-row .cmeta { color: var(--muted); font-size: 13px; margin-top: 3px; }
.code-row .pbtn { width: 46px; height: 46px; border-radius: 12px; border: 0; background: var(--panel2); font-size: 18px; cursor: pointer; flex-shrink: 0; }
.code-row .pbtn.del { background: rgba(192,57,43,.25); }
