:root {
  --bg: #0b1220;
  --panel: #121a2b;
  --panel-2: #1a2438;
  --line: #26334d;
  --text: #e8eefc;
  --muted: #93a1bd;
  --accent: #14b8a6;
  --accent-ink: #032824;
  --danger: #f87171;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none; -webkit-user-select: none; user-select: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.view { position: fixed; inset: 0; display: none; flex-direction: column; }
.view.active { display: flex; }

/* ---------- Thanh trên ---------- */
.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--panel); border-bottom: 1px solid var(--line); min-height: 56px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 19px; letter-spacing: .2px; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }
.bar-right { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; color: var(--muted); display: grid; place-items: center; }
.icon-btn:active { background: var(--panel-2); }
.ed-title { font-weight: 600; font-size: 15px; text-align: center; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Trang chính ---------- */
.home-main { flex: 1; overflow-y: auto; padding: 16px; -webkit-overflow-scrolling: touch; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 12px; }
.thumb {
  position: relative; aspect-ratio: 3 / 4; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; padding: 0; overflow: hidden;
}
.thumb:active { transform: scale(.97); }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb span {
  position: absolute; left: 6px; bottom: 6px; background: rgba(0,0,0,.72); color: #fff;
  font-size: 12px; font-weight: 700; border-radius: 999px; padding: 2px 8px;
}
.empty { text-align: center; color: var(--muted); padding: 10vh 12px 0; max-width: 420px; margin: 0 auto; }
.empty h2 { color: var(--text); font-size: 20px; margin: 12px 0 6px; }
.empty-ico { width: 72px; height: 72px; stroke-width: 1.2; color: var(--accent); }

.actions {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px);
  background: var(--panel); border-top: 1px solid var(--line);
}
.fab {
  width: 68px; height: 68px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; box-shadow: 0 6px 20px rgba(20,184,166,.35);
}
.fab svg { width: 30px; height: 30px; stroke-width: 2; }
.fab:active { transform: scale(.94); }

/* ---------- Nút ---------- */
.btn {
  border: 0; border-radius: 12px; padding: 10px 14px; font-weight: 600; min-height: 46px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none;
  font-size: 15px;
}
.btn.sm { min-height: 38px; padding: 6px 14px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.secondary { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn:disabled { opacity: .4; }
.btn:active:not(:disabled) { filter: brightness(1.15); }

/* ---------- Camera ---------- */
#camera { background: #000; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cam-guide {
  position: absolute; left: 8%; right: 8%; top: 14%; bottom: 22%;
  border: 2px dashed rgba(255,255,255,.35); border-radius: 12px; pointer-events: none;
}
.cam-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; transition: opacity .25s; }
.cam-flash.on { opacity: .8; transition: none; }
.cam-top {
  position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
}
.round-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgba(0,0,0,.5); color: #fff;
  display: grid; place-items: center;
}
.round-btn.on { background: #facc15; color: #000; }
.cam-bottom {
  position: absolute; bottom: 0; left: 0; right: 0; display: grid; grid-template-columns: 1fr auto 1fr;
  align-items: center; padding: 20px 16px calc(env(safe-area-inset-bottom) + 24px);
  background: linear-gradient(transparent, rgba(0,0,0,.7));
}
.shutter {
  width: 78px; height: 78px; border-radius: 50%; border: 5px solid #fff; background: rgba(255,255,255,.25);
  box-shadow: inset 0 0 0 4px rgba(0,0,0,.4);
}
.shutter:active { background: #fff; }
.cam-side {
  justify-self: center; background: rgba(0,0,0,.45); border: 0; color: #fff; border-radius: 12px;
  padding: 8px 12px; font-size: 13px; line-height: 1.25;
}

/* ---------- Editor ---------- */
#editor { background: #05080f; }
.stage { flex: 1; position: relative; overflow: hidden; min-height: 0; }
.stage.center { display: flex; align-items: center; justify-content: center; padding: 16px; }
#cropCanvas { position: absolute; inset: 0; touch-action: none; }
#resultCanvas { max-width: 100%; max-height: 100%; box-shadow: 0 6px 30px rgba(0,0,0,.6); background: #fff; }
.tools {
  background: var(--panel); border-top: 1px solid var(--line);
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 12px);
}
.hint { margin: 0 0 8px; text-align: center; font-size: 13px; color: var(--muted); }
.tool-row { display: flex; gap: 8px; overflow-x: auto; padding: 2px; justify-content: center; flex-wrap: wrap; }
.tool-row + .tool-row { margin-top: 8px; }
.chip {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 8px 14px;
  font-size: 14px; white-space: nowrap; min-height: 38px;
}
.chip.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: flex-end;
  justify-content: center; z-index: 20; animation: fade .15s;
}
.sheet {
  width: 100%; max-width: 520px; background: var(--panel); border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(env(safe-area-inset-bottom) + 16px); border-top: 1px solid var(--line);
  animation: up .2s ease-out; -webkit-user-select: text; user-select: text;
}
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.sheet-btn {
  display: block; width: 100%; text-align: left; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 13px 14px; margin-bottom: 8px; font-size: 15px;
}
.sheet-btn.danger { color: var(--danger); }
.sheet-btn.muted { text-align: center; background: transparent; }
.field { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.field input, .field select {
  display: block; width: 100%; margin-top: 4px; background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px; font-size: 16px; color: var(--text);
}
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.pdf-result { background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px 12px; margin-bottom: 12px; }
.pdf-result p { margin: 0 0 10px; font-size: 14px; color: var(--muted); }

/* ---------- Busy & toast ---------- */
.busy {
  position: fixed; inset: 0; z-index: 30; background: rgba(5,8,15,.7); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 12px; color: var(--text);
}
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 110px); transform: translateX(-50%);
  background: #f1f5f9; color: #0b1220; padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  z-index: 40; max-width: calc(100% - 32px); text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,.4);
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(40px); opacity: .5; } }
