================================================================================
 Z4PC Vault — Chrome Extension  |  إضافة كروم لخزنة كلمات المرور Z4PC
 End-to-End Encrypted Password Vault with Autofill — v1.7.1
================================================================================

FIXED IN 1.7.1 — Edit button + Update prompt after login:
- 🐛 Edit button: editing an entry whose Site is empty (entries that
  arrived from a CSV/TXT import without a URL) could never be saved —
  the edit form's Site field was marked "required", so the browser
  silently blocked the save with no message and the entry kept its old
  values. Site is optional in the vault format, so the form no longer
  demands it: editing any entry now saves and persists.
- 🐛 "Update password?" prompt missing after a login with a changed
  password: on multi-step logins (email step, then password step) the
  username step is often still in the page but hidden, and the capture
  only looked at visible fields — so the username was lost, the new
  password was treated as a brand-new account (Add-new duplicate) and
  no Update was ever offered for the existing account. The capture now
  also checks the hidden fields for the typed username, so the rules
  apply exactly: same username + different password → "🔄 Update";
  different username (or none saved for the site) → "💾 Add new";
  identical password → no prompt at all.
- 🔄 The popup's captured-account bar now labels its button by the
  action — "🔄 Update password" when it will update the existing
  entry, "💾 Save captured account" when it will add a new one (the
  in-page banner already did this).

إصلاحات في 1.7.1 — زر التعديل وسؤال تحديث كلمة المرور:
- 🐛 زر التعديل: تعديل حساب موقعه فاضي (حسابات جت من استيراد CSV/TXT
  من غير رابط) ماكانش بيتحفظ أبداً — خانة الموقع في نموذج التعديل
  كانت «مطلوبة»، فالمتصفح كان بيمنع الحفظ بصمت من غير أي رسالة
  والحساب يفضل بقيمه القديمة. الموقع اختياري أصلاً في صيغة الخزنة،
  فالنموذج مش بيطلبه دلوقتي: تعديل أي حساب بيتحفظ ويثبت.
- 🐛 سؤال «تحديث كلمة المرور؟» ماكانش بيظهر بعد الدخول بكلمة مرور
  متغيرة: في تسجيلات الدخول متعددة الخطوات (خطوة الإيميل وبعدين خطوة
  كلمة المرور) خطوة اسم المستخدم بتفضل في الصفحة بس مخفية، والالتقاط
  كان بيدوّر في الخانات الظاهرة بس — فاسم المستخدم كان بيضيع، وكلمة
  المرور الجديدة كانت بتتعامل كحساب جديد تماماً (نسخة مكررة) ومفيش
  تحديث بيُعرض للحساب الموجود أبداً. الالتقاط دلوقتي بيشيك كمان على
  الخانات المخفية عشان يلاقي اسم المستخدم المكتوب، فالقواعد بتتطبق
  بالحرف: نفس اسم المستخدم + كلمة مرور مختلفة → «🔄 تحديث»؛ اسم
  مستخدم مختلف (أو مفيش حساب محفوظ للموقع) → «💾 إضافة جديد»؛ نفس
  كلمة المرور بالحرف → مفيش سؤال أصلاً.
- 🔄 شريط الحساب الملتقط في البوب-أب بقى بيكتب على زراره الفعل نفسه
  — «🔄 تحديث كلمة المرور» لما هيحدّث الحساب الموجود، و«💾 حفظ
  الحساب الملتقط» لما هيضيف واحد جديد (البانر داخل الصفحات كان
  بيعمل كده أصلاً).

NEW IN 1.7.0 — Final polish release (full feature verification):
Every extension feature was re-verified end-to-end against the real
code (vault crypto, capture, autofill, health check, CSV import/export,
server sync + restore, auto-lock, EN/AR interface) and these defects
were found and fixed:
- 🐛 CSV import duplicates: importing a file whose sites are full URLs
  (e.g. a Chrome export with "https://www.github.com/login") no longer
  duplicates accounts already stored under the bare host
  ("github.com") — import de-duplication now compares sites by host,
  the same way capture-save always did.
- 🐛 The popup no longer offers to "save" a captured account that is
  already stored with the identical password.
- 🐛 The in-page 🔐 autofill picker no longer claims the vault is
  locked (or that no accounts exist) after you unlock the vault or
  save an account without reloading the page — it refreshes first.
- 🐛 Small consistency fixes: creating a new vault now refreshes the
  sync status line and the captured-account bar immediately, and the
  Options password-health section refreshes right after downloading a
  vault or wiping the local one.
- No format changes: the encrypted vault format, the pinned extension
  ID and all settings are unchanged — this update installs over 1.6.x
  and your vault just opens as before.

جديد في 1.7.0 — إصدار التحسينات الأخير (فحص شامل لكل المزايا):
تمت إعادة فحص كل مزايا الإضافة فعلياً على الكود الحقيقي (تشفير
الخزنة، الالتقاط، التعبئة التلقائية، فحص صحة كلمات المرور، استيراد/
تصدير CSV، المزامنة مع السيرفر والاستعادة، القفل التلقائي، واجهة
العربي/الإنجليزي) وتم إصلاح العيوب دي:
- 🐛 تكرار الحسابات عند الاستيراد: استيراد ملف مواقعُه روابط كاملة
  (زي تصدير كروم "https://www.github.com/login") مش هيكرّر حسابات
  محفوظة أصلاً بالدومين بس ("github.com") — منع التكرار بقى يقارن
  المواقع بالهوست زي ما الحفظ عند الالتقاط كان بيعمل دايماً.
- 🐛 البوب-أب مش هيعرض «حفظ» حساب ملتقط وهو أصلاً محفوظ بنفس كلمة
  المرور بالحرف.
- 🐛 قائمة التعبئة 🔐 داخل الصفحات مش هتقول «الخزنة مقفولة» (أو مفيش
  حسابات) بعد ما تفتح الخزنة أو تحفظ حساب من غير إعادة تحميل الصفحة
  — القائمة بتتحدّث الأول دلوقتي.
- 🐛 إصلاحات اتساق صغيرة: إنشاء خزنة جديدة بيحدّث سطر حالة المزامنة
  وشريط الحساب الملتقط فوراً، وقسم صحة كلمات المرور في الإعدادات
  بيتحدّث بعد تنزيل خزنة أو مسح المحلية مباشرة.
- مفيش أي تغيير في الصيغ: صيغة الخزنة المشفرة ومعرّف الإضافة الثابت
  وكل الإعدادات زي ما هي — التحديث بينزل فوق 1.6.x وخزنتك بتفتح
  عادي زي قبل كده.

FIXED IN 1.6.1 — Auto-lock "Never" now sticks:
- 🐛 Bug: setting Options → Auto-lock to "Never" reverted to 5 minutes.
  Cause: saving the server URL replaced the whole shared settings bucket
  and silently wiped the auto-lock choice (and the other capture toggles).
  Saving the server URL now MERGES into the settings, so Auto-lock —
  including "Never" (0) — persists across saves, reloads and restarts,
  and the value you pick is exactly the value the vault enforces.
- إصلاح في 1.6.1: إعداد القفل التلقائي «أبداً» بقى يثبت — حفظ رابط
  السيرفر كان بيمسح باقي الإعدادات ويرجّع القفل لـ ٥ دقائق؛ دلوقتي
  الحفظ بيندمج مع الإعدادات الموجودة من غير ما يمسح حاجة.

NEW IN 1.6.0 — Full bilingual support (English default + Arabic):
- 🌐 The whole extension is now bilingual: English (EN) is the DEFAULT
  language, Arabic (عربي) is fully supported everywhere — the popup,
  the Options page, the in-page save/update banner, the autofill
  picker, and the signup password generator.
- How to switch: use the small "EN | عربي" switch in the popup footer,
  or Options → Language. The switch takes effect immediately (the
  current view re-renders in the new language, no reload needed) and
  the layout flips direction automatically (LTR for English, RTL for
  Arabic).
- The choice is saved per-browser in chrome.storage.local ('z4_lang')
  and survives updates, because the extension ID is pinned.
- All existing Arabic copy was kept as the Arabic translation; new
  natural English copy was written for every string (233 keys in each
  language, verified identical by tests/test_ext_i18n.js).

جديد في 1.6.0 — دعم كامل للغتين (الإنجليزية افتراضياً + العربية):
- 🌐 الإضافة بقت بلغتين بالكامل: الإنجليزية (EN) هي اللغة الافتراضية،
  والعربية مدعومة في كل مكان — البوب-أب وصفحة الإعدادات وبانر
  الحفظ/التحديث داخل الصفحات وقائمة التعبئة التلقائية ومولّد
  كلمات مرور التسجيل.
- إزاي تبدّل: من مفتاح "EN | عربي" الصغير في أسفل البوب-أب، أو من
  الإعدادات → اللغة. التبديل بيشتغل فوراً (الشاشة الحالية بتتعرض من
  جديد باللغة الجديدة من غير إعادة تحميل) واتجاه الصفحة بيتبدّل
  تلقائياً (LTR للإنجليزية وRTL للعربية).
- الاختيار بيتحفظ لكل متصفح في chrome.storage.local ('z4_lang')
  وبيفضل موجود بعد التحديثات لأن معرّف الإضافة ثابت.

جديد في 1.5.1 (مهم):
- 🔑 هوية ثابتة للإضافة: المانيفست فيه دلوقتي مفتاح توقيع ثابت (key)،
  فمعرّف الإضافة (Extension ID) بقى ثابت مهما اتغيّر مكان المجلد اللي
  بتفك فيه النسخة الجديدة. يعني من هنا ورايح: أي تحديث هينزل فوق نفس
  الإضافة وخزنتك المحلية هتفضل موجودة — مش هتظهر إضافة جديدة فاضية.
  ⚠️ ملحوظة مرة واحدة بس: التحديث ده نفسه (من 1.5.0 إلى 1.5.1) هيظهر
  كأنه إضافة جديدة مرة أخيرة، لأن النسخة الحالية عندك معرّفها جاي من
  مسار المجلد. ما تشيلش النسخة القديمة غير لما تتأكد إن الخزنة رجعت
  في الجديدة (من اللوحة أو من ملف CSV) — بعد كده المشكلة دي انتهت
  للأبد.
- ☁️ شاشة الفتح في أي تثبيت جديد بقى فيها شرح واضح لاستعادة الخزنة من
  اللوحة: سجّل دخولك في z4pc.com من نفس المتصفح بنفس الحساب، وادخل
  كلمة السر الرئيسية — كلمات السر بترجع تلقائياً من السيرفر.
- NEW IN 1.5.1: the manifest now carries a fixed signing key, so the
  extension ID (gnnkpkcjfbjkainjcfloidafhdbimlam) stays the same no
  matter which folder you unpack future updates into — updates install
  over the same extension and your local vault survives. One-time note:
  THIS update still appears as a new extension once (your current build
  has a path-derived ID); keep the old copy until the vault is confirmed
  restored in the new one, then remove the old copy.

جديد في 1.5:
- 🩺 فحص صحة كلمات المرور: فحص محلي بالكامل على الحسابات بعد فك
  التشفير فقط (لا يلمس النص المشفر ولا يُرسل لأي مكان) — يكتشف كلمات
  المرور القصيرة (أقل من ٨) والضعيفة (أقل من ١٢ أو أقل من ٣ أنواع
  أحرف أو من قائمة كلمات شائعة) والمكررة بين حساباتك. البوب-أب يعرض
  ملخصاً وشارة ⚠️ بجانب كل حساب فيه مشكلة، وصفحة الإعدادات فيها قسم
  «🩺 صحة كلمات المرور» بالتفاصيل (الاسم + الموقع + نوع المشكلة فقط —
  كلمات المرور نفسها لا تظهر أبداً).
- 🚀 روابط سريعة في البوب-أب (والخزنة مفتوحة): «🖥️ أجهزتي» و
  «📧 البريد المؤقت» و«🖱️ التحكم عن بُعد» تفتح صفحات اللوحة مباشرة
  (التحكم عن بُعد يبدأ من صفحة الأجهزة).
- NEW IN 1.5: local-only password health check (short / weak / common
  / reused — computed on decrypted entries in memory, never on the
  encrypted blob, never sent anywhere) with a popup summary banner,
  per-entry ⚠️ badges, and a grouped details section in Options;
  plus quick-launch buttons to Devices / Temp Mail / Remote Control.

جديد في 1.4:
- بوابات كلمة المرور فقط (من غير اسم مستخدم — زي صفحات الأدمن المحمية
  بكلمة مرور واحدة) بتتلتقط وتتحفظ دلوقتي، وكانت بتضيع قبل كده.
- الالتقاط بقى شغال كمان في الصفحات اللي مفيهاش <form> أصلاً (زرار
  جافاسكريبت + خانة باسورد): Enter أو ضغطة الزرار بتسجّل الحساب.
- فورم تغيير كلمة المرور: بيتحفظ الباسورد الجديد مش القديم.
- (1.3.1 أصلحت خطأ Chrome: Default locale was specified, but _locales
  subtree is missing.)

[العربية]

ما هذه الإضافة؟
--------------
رفيقة لوحة z4pc.com وبرنامج Z4PC على ويندوز: خزنة كلمات مرور مشفرة بالكامل
من الطرف إلى الطرف (End-to-End) مع تعبئة تلقائية (Autofill) في المواقع.

التشفير من الطرف إلى الطرف — يعني إيه؟
--------------------------------------
- كل حساباتك تُشفَّر على جهازك أنت قبل ما تتحرك لأي مكان، بمفتاح مشتق من
  كلمة السر الرئيسية بتاعتك:
    PBKDF2-HMAC-SHA256 × 200,000 دورة  +  AES-256-GCM
- كلمة السر الرئيسية والمفتاح لا يغادران جهازك أبداً، ولا يُرسلان للسيرفر
  لا عند الفتح ولا عند المزامنة.
- السيرفر (z4pc.com) يخزن ويرسل "نصاً مشفراً" فقط (Encrypted Blob) لا يستطيع
  هو ولا أي حد قراءته من غير كلمة السر الرئيسية.
- النتيجة المهمة: لو نسيت كلمة السر الرئيسية، لا توجد أي طريقة لاسترجاع
  الخزنة — لا من السيرفر ولا منّا. احفظها في مكان آمن.

صيغة الخزنة (نفس صيغة لوحة الويب وبرنامج البايثون بالضبط):
  {"v":1,"kdf":"PBKDF2-SHA256","iter":200000,
   "salt":"<base64: 16 بايت>","iv":"<base64: 12 بايت>",
   "data":"<base64: نص AES-256-GCM المشفر>"}
  والنص المفكوك قائمة حسابات:
  [{id, name, site, username, secondary_username, password, note, updated}]
  (الحسابات القديمة بدون name/secondary_username تُفتح عادي: الاسم يأخذ
  قيمة الموقع افتراضياً، واسم المستخدم الإضافي يكون فارغاً.)

التثبيت (وضع المطوّر):
---------------------
1) افتح في كروم:  chrome://extensions
2) فعّل "وضع المطوّر / Developer Mode" من الأعلى.
3) اضغط "تحميل إضافة غير مضغوطة / Load unpacked".
4) اختر مجلد هذه الإضافة (المجلد الذي يحتوي manifest.json).
5) ثبّت أيقونة الدرع 🛡️ في شريط الأدوات لسهولة الوصول.

الاستخدام:
----------
- أول مرة: "إنشاء خزنة جديدة" واختر كلمة سر رئيسية قوية (٨+ أحرف)،
  أو افتح خزنة موجودة لو عندك واحدة محلية/على السيرفر.
- قسم "لهذا الموقع / For this site" يعرض حسابات الموقع الحالي أولاً،
  وزر "تعبئة ⚡" يملأ اسم المستخدم وكلمة المرور في صفحة الدخول.
- أزرار نسخ سريعة لاسم المستخدم وكلمة المرور لكل حساب.
- القفل: زر 🔒 يقفل فوراً ويمسح المفتاح من الذاكرة، وهناك قفل تلقائي
  بعد ٥ دقائق من عدم الاستخدام.

استيراد / تصدير كلمات المرور (CSV / TXT) — جديد في 1.1:
------------------------------------------------------
- من صفحة الإعدادات ⚙️ (وفي البوب-أب تصدير CSV واستيراد سريع):
  * تصدير CSV أو TXT بالأعمدة:
      name,website,username,secondary_username,password,notes
    (ملف CSV يبدأ بـ UTF-8 BOM حتى يظهر العربي صح في Excel،
    وملف TXT يفصل بين الأعمدة بـ Tab.)
  * ⚠️ ملفات CSV/TXT نصّية غير مشفرة: التصدير يتطلب خزنة مفتوحة +
    تحذير صريح + إعادة إدخال كلمة السر الرئيسية (يتم التحقق منها بفك
    تشفير الخزنة فعلياً). امسح الملف فور الانتهاء من استخدامه.
  * الاستيراد يفهم: ملفات Z4PC، وملفات Chrome
    (name,url,username,password,note)، وملفات بدون ترويسة بنفس ترتيب
    الأعمدة، وفواصل , أو Tab أو ; تُكتشف تلقائياً، وحقول مقتبسة فيها
    فواصل أو أسطر جديدة.
  * الدمج: الحساب المكرر (نفس الموقع + اسم المستخدم) يتم تخطّيه،
    وتظهر لك حصيلة (أُضيف كم / تُرك كم). الحسابات المستوردة تأخذ
    معرّفات وطوابع زمنية جديدة، وبعد الاستيراد ارفع الخزنة للسيرفر
    من زر "رفع الخزنة" حتى تتزامن.
- تصدير/استيراد ملف الخزنة المشفر (.z4vault.json) كما هو — من الإعدادات.

المزامنة مع السيرفر:
--------------------
- المزامنة (رفع/تنزيل) تحتاج أن تكون مسجلاً دخولك في لوحة z4pc.com
  من نفس متصفح كروم — الإضافة تستخدم جلسة الدخول (الكوكيز) الخاصة بك.
- لو غيّرت رابط السيرفر من صفحة الإعدادات، ستطلب الإضافة إذن الوصول
  لهذا السيرفر فقط (Optional Host Permission).
- البديل من غير سيرفر: من الإعدادات صدّر ملف الخزنة المشفر
  (.z4vault.json) واستورده على أي جهاز آخر — الملف مشفر ولا يُفتح
  إلا بكلمة السر الرئيسية.

ملحوظات أمان:
-------------
- المفتاح المشتق يُحفظ أثناء الفتح في chrome.storage.session فقط
  (ذاكرة مؤقتة خاصة بالإضافة، تُمسح بإغلاق المتصفح أو القفل).
- انسخ كلمة مرور من الخزنة؟ امسحها من الحافظة بعد الاستخدام.
- راجع دائماً أنك على الموقع الصحيح قبل التعبئة التلقائية.

================================================================================

[English]

What is this?
-------------
Companion to the z4pc.com panel and the Z4PC Windows agent: an
end-to-end encrypted password vault with site autofill.

End-to-end encryption, in short:
- Entries are encrypted ON YOUR DEVICE with a key derived from your
  master password: PBKDF2-HMAC-SHA256 (200,000 iterations) + AES-256-GCM.
- The master password and the key NEVER leave the device and are NEVER
  sent to the server — sync moves only the encrypted blob string.
- The server cannot read your vault. If you forget the master password,
  the vault is unrecoverable by design. Keep it somewhere safe.

Vault format (identical to the web panel and the Python agent):
  {"v":1,"kdf":"PBKDF2-SHA256","iter":200000,"salt":"<base64 16 bytes>",
   "iv":"<base64 12 bytes>","data":"<base64 AES-256-GCM ciphertext>"}
  Plaintext = JSON array: [{id, name, site, username, secondary_username,
  password, note, updated}] — older entries without name/secondary_username
  still load (name defaults to site, secondary to "").

Install (Developer Mode):
  1. Open chrome://extensions
  2. Enable "Developer Mode"
  3. Click "Load unpacked" and select this folder (contains manifest.json)
  4. Pin the shield icon to the toolbar.

Password import / export (CSV / TXT) — new in 1.1:
  From the Options page (the popup also has CSV export + quick import):
  - Export columns, exactly:
      name,website,username,secondary_username,password,notes
    CSV carries a UTF-8 BOM (Arabic renders correctly in Excel);
    TXT is tab-separated.
  - Exports are PLAIN TEXT secrets: they require an unlocked vault, an
    explicit warning confirmation, AND re-entering the master password
    (verified by actually decrypting the vault). Delete the file as soon
    as you are done with it.
  - Import understands Z4PC files, Chrome exports
    (name,url,username,password,note), header-less files in the canonical
    column order, comma/tab/semicolon delimiters (auto-detected), and
    quoted fields containing commas or newlines.
  - Merge: duplicates (same site + username) are skipped and a summary
    (added / skipped) is shown. Imported entries get fresh ids and
    timestamps; afterwards use "Upload vault" to sync them.
  - The encrypted .z4vault.json export/import is unchanged.

Sync requirements:
  Sync (upload/download) requires being logged into the z4pc.com panel
  in the SAME Chrome browser — the extension uses your panel session
  cookie (fetch with credentials:include). Alternatively, export/import
  the encrypted .z4vault.json file from the Options page.

Security notes:
  - While unlocked, the derived key lives only in chrome.storage.session
    (in-memory, extension-only; wiped on browser close, Lock, or the
    5-minute auto-lock enforced by the background service worker).
  - Always check you are on the real site before autofilling.
================================================================================

================================================================================
NEW IN 1.2 — Avira-style capture & autofill (reads pages, saves what you
type, fills it back for you)
================================================================================
The extension now watches login/signup forms the way Avira Password
Manager does — via a content script (detect.js + content.js) on
http/https pages:

CAPTURE — when you submit a form containing a username + password:
  - Nothing is read while you type. Values are read ONLY at submit
    (form submit event, with a submit-button click fallback), then sent
    to the background worker.
  - The background keeps the capture as a PENDING SAVE in
    chrome.storage.session — memory-only, never storage.local, never
    disk, never the network — and it expires after 10 minutes.
  - On the next page load (or in-page for SPA logins) a Shadow-DOM
    banner asks: حفظ / مش دلوقتي / أبداً لهذا الموقع
    (Save / Not now / Never for this site).
  - The save is completed ONLY while the vault is unlocked. While
    locked, the banner explains the vault is locked and the pending
    save waits; after you Unlock, the popup shows a one-click bar:
    «حفظ الحساب الملتقط: user@site».
  - If the site+username already exist with a DIFFERENT password, the
    banner becomes an update prompt: «كلمة المرور اتغيرت — تحديث؟»
    If the stored password is identical, no prompt appears at all.
  - «أبداً لهذا الموقع» adds the hostname to a never-save list
    (Options page shows it, with remove buttons).
  - Signup forms: the 🔐 picker offers «توليد كلمة قوية» — a 16-char
    mixed password generated with crypto.getRandomValues, filled into
    the password + confirm fields; submitting then offers to save it.

AUTOFILL — on a detected login form:
  - The background returns matching entries ONLY while unlocked
    (locked vault => the picker just says "unlock first").
  - If the "autofill_single" setting is on (default) and EXACTLY ONE
    entry matches the site, it is filled silently (native value setter
    + input/change events, so React/Angular forms notice).
  - A 🔐 button is pinned inside the username field's edge; clicking
    it opens a picker of the matching accounts — click one to fill.

SECURITY MODEL / LIMITS:
  - OTP / one-time-code inputs are never captured or treated as
    passwords (detect.js explicitly excludes them, as are newsletter /
    email-only forms — they are not credential forms).
  - The content script NEVER runs on your own Z4PC panel origin (the
    server_url from Settings is excluded at runtime), so your master
    password form is never a capture target.
  - Captured plaintext exists only in page memory at submit time and
    in chrome.storage.session as a pending save (same trust domain as
    the unlocked key). Locking or the 5-minute auto-lock wipes session
    storage, pending saves included.
  - Everything is optional: Options → "Capture & Autofill" has
    switches for save prompts, signup capture, and silent autofill —
    all default ON, all can be turned OFF. Turning save prompts off
    disables capture entirely.
  - Always check you are on the real site before accepting a save or
    an autofill: like every password manager, this fills the origin
    you are actually visiting.
================================================================================
  - One deliberate exception: pending saves survive a manual Lock /
    auto-lock (vault.js clearSession preserves only the pending-save
    map), because their whole point is "unlock, then save". They are
    still bounded by the 10-minute TTL, dismissed/never-saved at any
    time, and wiped when the browser closes.
