:root {
  --bg: #0f172a;
  --bg2: #1e293b;
  --bg3: #334155;
  --card: #1e293b;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent2: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Nastaliq Urdu", "Noto Sans Arabic", sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }
body { overscroll-behavior: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
img { max-width: 100%; display: block; }
[dir="rtl"] { font-family: "Noto Nastaliq Urdu", "Noto Sans Arabic", var(--font); }

#app { min-height: 100%; display: flex; flex-direction: column; padding-top: var(--safe-t); }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--bg); position: sticky; top: 0; z-index: 20;
  border-bottom: 1px solid rgba(148,163,184,.15);
}
.topbar h1 { font-size: 1.05rem; margin: 0; font-weight: 700; letter-spacing: -.2px; }
.topbar .actions { display: flex; gap: 6px; align-items: center; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; background: var(--bg2);
  display: grid; place-items: center; color: var(--text); font-size: 1.1rem;
}
.icon-btn:active { background: var(--bg3); }

/* Home */
.home { flex: 1; padding: 16px; padding-bottom: calc(88px + var(--safe-b)); }
.hero {
  background: linear-gradient(135deg, #1d4ed8 0%, #0ea5e9 55%, #22c55e 100%);
  border-radius: 18px; padding: 22px 18px; margin-bottom: 18px; position: relative; overflow: hidden;
}
.hero h2 { margin: 0 0 6px; font-size: 1.35rem; }
.hero p { margin: 0; opacity: .92; font-size: .92rem; }
.badge-pk {
  display: inline-block; margin-top: 12px; background: rgba(0,0,0,.25);
  padding: 4px 10px; border-radius: 999px; font-size: .75rem; font-weight: 600;
}
.grid-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 20px; }
.action-card {
  background: var(--card); border: 1px solid rgba(148,163,184,.12);
  border-radius: var(--radius); padding: 16px 14px; text-align: left;
  display: flex; flex-direction: column; gap: 8px; min-height: 110px;
}
.action-card .emoji { font-size: 1.6rem; }
.action-card strong { font-size: .95rem; }
.action-card span { font-size: .78rem; color: var(--muted); line-height: 1.35; }
.action-card.primary { background: linear-gradient(160deg, #1e3a8a, #1e293b); border-color: rgba(59,130,246,.35); }
.action-card:active { transform: scale(.98); }

.section-title { font-size: .85rem; color: var(--muted); margin: 8px 0 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.doc-list { display: flex; flex-direction: column; gap: 10px; }
.doc-item {
  display: flex; gap: 12px; align-items: center; background: var(--card);
  border-radius: 12px; padding: 10px; border: 1px solid rgba(148,163,184,.1);
}
.doc-thumb {
  width: 52px; height: 68px; border-radius: 8px; object-fit: cover; background: var(--bg3); flex-shrink: 0;
}
.doc-meta { flex: 1; min-width: 0; }
.doc-meta strong { display: block; font-size: .92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-meta small { color: var(--muted); font-size: .75rem; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; font-size: .9rem; }

/* Bottom nav */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: rgba(15,23,42,.94); backdrop-filter: blur(12px);
  border-top: 1px solid rgba(148,163,184,.15);
  padding: 6px 8px calc(6px + var(--safe-b));
}
.bottom-nav button {
  flex: 1; padding: 8px 4px; color: var(--muted); font-size: .7rem;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.bottom-nav button .nav-ico { font-size: 1.25rem; }
.bottom-nav button.active { color: var(--accent); }

/* Screens */
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.screen-body { flex: 1; overflow: auto; padding: 12px 16px; padding-bottom: calc(24px + var(--safe-b)); }
.screen.full .screen-body { padding: 0; overflow: hidden; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 16px; border-radius: 12px; font-weight: 600; font-size: .95rem;
  background: var(--bg3); color: var(--text);
}
.btn:active { opacity: .85; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.success { background: var(--accent2); color: #052e16; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid rgba(148,163,184,.25); }
.btn.block { width: 100%; }
.btn.sm { padding: 8px 12px; font-size: .85rem; border-radius: 10px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1; min-width: 0; }

/* Camera / crop */
.cam-wrap, .crop-wrap {
  position: relative; background: #000; flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
#camVideo, #previewImg, #cropCanvas {
  max-width: 100%; max-height: 100%; object-fit: contain;
}
#camVideo { width: 100%; height: 100%; object-fit: cover; }
.cam-overlay {
  position: absolute; inset: 0; pointer-events: none;
  border: 2px dashed rgba(59,130,246,.55); margin: 12% 8%; border-radius: 8px;
}
.cam-toolbar, .edit-toolbar, .crop-toolbar {
  display: flex; gap: 8px; padding: 12px 12px calc(12px + var(--safe-b));
  background: var(--bg); border-top: 1px solid rgba(148,163,184,.12);
  flex-wrap: wrap; align-items: center; justify-content: center;
}
.capture-btn {
  width: 68px; height: 68px; border-radius: 50%;
  background: #fff; border: 4px solid var(--accent); flex-shrink: 0;
}
.capture-btn:active { transform: scale(.94); }

#cropCanvas { touch-action: none; max-width: 100%; max-height: calc(100vh - 180px); }

.filter-chips { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0; width: 100%; }
.chip {
  flex-shrink: 0; padding: 8px 12px; border-radius: 999px; background: var(--bg2);
  font-size: .8rem; font-weight: 600; border: 1px solid transparent;
}
.chip.active { background: rgba(59,130,246,.2); border-color: var(--accent); color: #93c5fd; }

.pages-strip {
  display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px; background: var(--bg2);
}
.page-thumb {
  width: 56px; height: 72px; border-radius: 6px; object-fit: cover;
  border: 2px solid transparent; background: var(--bg3); flex-shrink: 0;
}
.page-thumb.active { border-color: var(--accent); }

/* Forms / modals */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: 6px; }
.field input, .field select {
  width: 100%; padding: 12px; border-radius: 10px; background: var(--bg2);
  border: 1px solid rgba(148,163,184,.2); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  width: 100%; max-width: 520px; background: var(--bg2); border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(18px + var(--safe-b)); max-height: 85vh; overflow: auto;
}
.modal h3 { margin: 0 0 12px; font-size: 1.1rem; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }

/* Signature */
.sig-canvas-wrap {
  background: #fff; border-radius: 12px; overflow: hidden; border: 1px solid rgba(148,163,184,.25);
}
#sigCanvas { width: 100%; height: 180px; display: block; touch-action: none; }

/* Search */
.search-bar {
  display: flex; gap: 8px; margin-bottom: 12px;
}
.search-bar input {
  flex: 1; padding: 12px 14px; border-radius: 12px; background: var(--bg2);
  border: 1px solid rgba(148,163,184,.15); outline: none;
}

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(76px + var(--safe-b)); transform: translateX(-50%);
  background: #0ea5e9; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: .85rem; font-weight: 600; z-index: 100; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; white-space: nowrap; max-width: 90vw; overflow: hidden; text-overflow: ellipsis;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

.lang-toggle {
  font-size: .75rem; font-weight: 700; padding: 6px 10px; border-radius: 8px;
  background: var(--bg2); color: #93c5fd;
}

.quality-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.quality-row .chip { text-align: center; }

.preview-sheet {
  background: #fff; border-radius: 8px; padding: 8px; margin: 12px auto;
  max-width: 320px; box-shadow: 0 8px 30px rgba(0,0,0,.35);
}
.preview-sheet img { width: 100%; height: auto; }

.spinner {
  width: 28px; height: 28px; border: 3px solid rgba(255,255,255,.2);
  border-top-color: #fff; border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-overlay {
  position: absolute; inset: 0; background: rgba(15,23,42,.55);
  display: grid; place-items: center; z-index: 5;
}

.footer-note { text-align: center; color: var(--muted); font-size: .75rem; margin-top: 20px; }
.footer-note a { color: #93c5fd; }

.cnic-badge {
  display: inline-flex; align-items: center; gap: 6px;
  background: rgba(34,197,94,.15); color: #86efac; padding: 4px 10px;
  border-radius: 999px; font-size: .75rem; font-weight: 600;
}
