:root {
  --bg: #111418;
  --panel: #1b1f25;
  --line: #2c323b;
  --text: #f2f4f7;
  --muted: #9aa3ae;
  --accent: #2ea66f;
  --accent-text: #ffffff;
  --warn: #f2b84b;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* Top bar */
.topbar { position: relative; z-index: 3; display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: calc(10px + var(--safe-t)) 12px 10px; }
.camera .topbar { position: absolute; top: 0; left: 0; right: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,.55), transparent); }
.topbar.solid { background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 650; letter-spacing: .2px; }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; border: 1px solid rgba(255,255,255,.35);
  background: rgba(0,0,0,.3); font-weight: 700; }
.top-right { display: flex; align-items: center; gap: 8px; }
.install-btn { display: flex; align-items: center; gap: 5px; height: 36px; padding: 0 12px; border-radius: 18px;
  border: 0; background: var(--accent); color: var(--accent-text); font-size: 14px; font-weight: 650; cursor: pointer; }
.install-btn svg { width: 18px; height: 18px; }
.text-btn { background: none; border: 0; padding: 8px 6px; color: var(--muted); min-width: 72px; text-align: left; }
.text-btn.primary { color: var(--accent); font-weight: 650; text-align: right; }

/* Camera */
.camera { background: #000; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.guide { position: absolute; left: 50%; top: 46%; transform: translate(-50%, -50%);
  width: min(78vw, calc(62vh / 1.4142)); aspect-ratio: 210 / 297;
  border: 2px dashed rgba(255,255,255,.75); border-radius: 6px; pointer-events: none;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.28); display: flex; align-items: flex-end; justify-content: center;
  transition: border-color .2s; }
.guide span { margin-bottom: -32px; font-size: 13px; color: rgba(255,255,255,.85); text-align: center; max-width: 90vw; line-height: 1.3; }
.guide.found { border-color: transparent; box-shadow: 0 0 0 9999px rgba(0,0,0,.18); }
.cam-status { position: absolute; left: 0; right: 0; bottom: calc(130px + var(--safe-b)); margin: 0;
  text-align: center; font-size: 14px; color: #fff; text-shadow: 0 1px 3px #000; padding: 0 16px; }
.cam-status.ok { color: #7ee2b0; }
.cam-controls { position: absolute; left: 0; right: 0; bottom: 0; z-index: 3;
  display: flex; align-items: center; justify-content: space-around;
  padding: 18px 16px calc(22px + var(--safe-b)); background: linear-gradient(to top, rgba(0,0,0,.7), transparent); }
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 5px solid #fff; background: rgba(255,255,255,.25);
  padding: 0; cursor: pointer; transition: transform .1s; }
.shutter:active { transform: scale(.92); background: rgba(255,255,255,.6); }
.shutter:disabled { opacity: .5; }
.side-btn { width: 64px; display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: none; border: 0; color: #fff; font-size: 12px; cursor: pointer; }
.side-btn svg { width: 28px; height: 28px; }
.side-btn.placeholder { visibility: hidden; }
.badge { min-width: 28px; height: 28px; border-radius: 14px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 700; font-size: 15px; }

/* Adjust */
.adjust { background: #000; }
.adjust-area { flex: 1; position: relative; touch-action: none; }
#adjust-canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.hint { margin: 0; padding: 12px 16px calc(14px + var(--safe-b)); text-align: center; color: var(--muted);
  font-size: 14px; background: var(--panel); border-top: 1px solid var(--line); }

/* Result */
.result { background: var(--bg); }
.preview-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 16px; }
.paper { max-width: 100%; max-height: 100%; background: #fff; box-shadow: 0 6px 24px rgba(0,0,0,.5); border-radius: 2px; }
.thumbs { display: flex; gap: 10px; overflow-x: auto; padding: 0 16px 10px; min-height: 0; }
.thumbs:empty { display: none; }
.thumb { position: relative; flex: 0 0 auto; border: 2px solid transparent; border-radius: 4px; padding: 0; background: none; }
.thumb.active { border-color: var(--accent); }
.thumb img { display: block; height: 64px; border-radius: 2px; background: #fff; }
.thumb .del { position: absolute; top: -8px; right: -8px; width: 22px; height: 22px; border-radius: 50%;
  border: 0; background: #e5484d; color: #fff; font-size: 14px; line-height: 22px; padding: 0; }
.page-tools { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 0 16px 8px; }
.sheet-info { font-size: 13px; color: var(--muted); }
.sheet-info:empty { display: none; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 16px; padding: 5px 12px; font-size: 13px; cursor: pointer; }
.back-row { padding: 0 16px 10px; }
.back-row:has(> [hidden]) { display: none; }
.btn.accent-outline { background: transparent; border: 2px solid var(--accent); color: var(--accent); }
.thumb .cap { display: block; font-size: 11px; color: var(--muted); text-align: center; margin-top: 3px; }

/* Mode switch */
.seg { display: flex; background: rgba(0,0,0,.45); border: 1px solid rgba(255,255,255,.25); border-radius: 20px; padding: 3px; }
.seg button { border: 0; background: none; color: #fff; font-size: 14px; font-weight: 600; padding: 6px 14px; border-radius: 16px; cursor: pointer; }
.seg button.active { background: #fff; color: #111418; }
.guide.card { aspect-ratio: 85.6 / 54; width: min(84vw, calc(46vh * 1.585)); }

.sheet h3 { margin: 14px 0 4px; font-size: 15px; }
.help-p { margin: 0 0 6px; font-size: 15px; }
.switch.disabled { opacity: .45; }
.options { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; padding: 4px 16px 10px; }
.switch { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text); cursor: pointer; user-select: none; }
.switch input { display: none; }
.switch span { width: 38px; height: 22px; border-radius: 11px; background: var(--line); position: relative; transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.actions { display: flex; gap: 10px; padding: 6px 16px calc(16px + var(--safe-b)); }

.btn { flex: 1; padding: 13px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel);
  font-weight: 600; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.wide { width: 100%; }
.btn:active { filter: brightness(1.15); }

/* Help dialog */
.sheet { border: 0; border-radius: 16px; background: var(--panel); color: var(--text); padding: 20px;
  width: min(92vw, 420px); }
.sheet::backdrop { background: rgba(0,0,0,.6); }
.sheet h2 { margin: 0 0 10px; font-size: 19px; }
.sheet ol { padding-left: 20px; margin: 0 0 12px; }
.sheet li { margin-bottom: 8px; }
.muted { color: var(--muted); font-size: 14px; margin: 8px 0; }
.help-foot { margin: 14px 0 0; text-align: center; font-size: 13px; color: var(--muted); }
.help-foot a { color: var(--muted); }
.help-foot + .help-foot { margin-top: 6px; }
.help-foot.kh a { color: #e0a64a; font-weight: 650; text-decoration: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Busy + toast */
.busy { position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.65); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 14px; }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255,255,255,.25); border-top-color: #fff;
  animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(110px + var(--safe-b)); transform: translateX(-50%); z-index: 11;
  background: #000c; color: #fff; padding: 10px 16px; border-radius: 20px; font-size: 14px; max-width: 90vw; text-align: center; }
