/* ============================================================
   الخطوط والمتغيرات العامة
   ============================================================ */
@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;800&display=swap');

:root {
  --bg: #EEF3EF;
  --ink: #1E2E27;
  --ink-soft: #4B5C53;
  --surface: #FFFFFF;
  --line: #D3DED7;
  --accent: #D9A441;
  --accent-ink: #4A3308;
  --brand: #1F4A3B;
  --brand-dark: #17372B;
  --danger: #C1502E;

  --radius-s: 6px;
  --radius-m: 10px;
  --shadow-1: 0 1px 2px rgba(20, 40, 30, 0.06);
  --shadow-2: 0 10px 24px rgba(20, 40, 30, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  direction: ltr;
  max-width: 100%;
  overflow-x: hidden;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Cairo', 'Segoe UI', sans-serif;
  line-height: 1.5;
}

/* ============================================================
   الهيدر
   ============================================================ */
header {
  background: var(--brand);
  padding: 14px 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

header input#search_input {
  appearance: none;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-family: inherit;
  font-size: 14px;
  padding: 9px 14px;
  border-radius: 999px;
}

header input#search_input::placeholder {
  color: rgba(255, 255, 255, 0.75);
}

header input#search_input:focus-visible {
  outline: none;
  border-color: var(--accent);
  background: rgba(255, 255, 255, 0.18);
}

/* بما إن #location_menu_btn بقى زرار حقيقي جوه <header>، هو بياخد شكل "header button" (accent pill) تلقائي
   زي زرار "☰ Categories" بالظبط، ومحتاجين بس نمنع أسماء الأماكن الطويلة من كسر شكل الزرار */
.location_filter {
  max-width: 180px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.location_menu {
  position: relative;
}

header button {
  appearance: none;
  border: none;
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-ink);
  font-family: inherit;
  font-weight: 600;
  font-size: 15px;
  padding: 10px 20px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: filter 0.15s ease;
}

#add_productbtn::before {
  content: '+';
  font-size: 18px;
  line-height: 1;
}

header button:hover {
  filter: brightness(1.06);
}

/* ============================================================
   بوب أب تعريفي (بيظهر مرة واحدة بس لكل زائر)
   ============================================================ */
.intro_popup {
  position: fixed;
  inset: 0;
  z-index: 200;
  /* أعلى من أي عنصر تاني في الصفحة (بما فيه قايمة إضافة منتج والقوائم المنسدلة) */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 30, 23, 0.6);
}

.intro_card {
  width: min(92vw, 380px);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-2);
  padding: 32px 24px;
  text-align: center;
}

.intro_avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  margin-bottom: 14px;
  border-radius: 50%;
  background: var(--bg);
  font-size: 32px;
}

.intro_name {
  margin: 0 0 6px;
  font-size: 20px;
  font-weight: 800;
  color: var(--brand-dark);
}

.intro_meta {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--ink-soft);
}

.intro_bio {
  margin: 0 0 22px;
  font-size: 14px;
  line-height: 1.7;
  color: var(--ink);
}

#intro_popup_close_btn {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: var(--radius-s);
  background: var(--brand);
  color: #fff;
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s ease;
}

#intro_popup_close_btn:hover {
  background: var(--brand-dark);
}

/* ============================================================
   نافذة إضافة منتج (Popup)
   ============================================================ */
.add_product_div {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 30, 23, 0.55);
}

.add_product_div form {
  background: var(--surface);
  width: min(92vw, 420px);
  max-height: 90vh;
  overflow-y: auto;
  border: none;
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-2);
  padding: 28px 24px;
}

#close_add_product_btn {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--bg);
  color: var(--ink-soft);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#close_add_product_btn:hover {
  background: var(--line);
  color: var(--ink);
}

.add_product_div h1 {
  margin: 0 0 20px;
  font-size: 22px;
  font-weight: 800;
  color: var(--brand-dark);
}

.add_product_div .title,
.add_product_div .description {
  text-align: right;
  margin-bottom: 16px;
}

.add_product_div label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-soft);
}

.add_product_div input[type="text"],
.add_product_div input:not([type]),
.add_product_div select,
.add_product_div textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  font-family: inherit;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
  resize: vertical;
}

.add_product_div input:focus-visible,
.add_product_div select:focus-visible,
.add_product_div textarea:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}

.custom-button {
  display: inline-block;
  padding: 9px 16px;
  border-radius: var(--radius-s);
  background: var(--bg);
  border: 1px dashed var(--line);
  color: var(--brand-dark);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
}

#imagePicker {
  margin-top: 10px;
  font-size: 13px;
  color: var(--ink-soft);
}

.add_product_div button[type="button"]#confirm_add_productbtn {
  margin-top: 8px;
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: var(--radius-s);
  background: var(--brand);
  color: #fff;
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.add_product_div button[type="button"]#confirm_add_productbtn:hover {
  background: var(--brand-dark);
}

.add_product_div button[type="button"]#confirm_add_productbtn:disabled {
  cursor: not-allowed;
  opacity: 0.85;
}

/* حالة التحميل: النص بيتشال (في الجافاسكريبت) وبدل منه دايرة بتلف في نص الزرار */
.add_product_div button[type="button"]#confirm_add_productbtn.is_loading {
  position: relative;
}

.add_product_div button[type="button"]#confirm_add_productbtn.is_loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 18px;
  margin: -9px 0 0 -9px;
  border: 2px solid rgba(255, 255, 255, 0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: confirm_btn_spin 0.7s linear infinite;
}

@keyframes confirm_btn_spin {
  to {
    transform: rotate(360deg);
  }
}

/* ============================================================
   عرض المنتجات
   ============================================================ */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 24px 60px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 18px;
}

.product_card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 4px solid var(--accent);
  border-radius: var(--radius-m);
  padding: 14px;
  box-shadow: var(--shadow-1);
  cursor: pointer;
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.product_card:hover {
  box-shadow: var(--shadow-2);
  transform: translateY(-2px);
}

.product_image {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: var(--radius-s);
  margin-bottom: 10px;
}

.product_card h3 {
  margin: 0 0 6px;
  font-size: 16px;
  font-weight: 800;
  color: var(--brand-dark);
}

.product_card p {
  margin: 0;
  font-size: 14px;
  color: var(--ink-soft);
  line-height: 1.5;
}

/* ============================================================
   أزرار تعديل / حذف المنتج (لصاحب المنتج بس)
   ============================================================ */

/* عشان الـ attribute اسمه hidden يشتغل حتى لو العنصر عليه display تاني */
[hidden] {
  display: none !important;
}

#edit_image_hint {
  display: block;
  margin-top: 8px;
  font-size: 12px;
  color: var(--ink-soft);
}

.product_actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.product_actions button {
  padding: 5px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-soft);
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.product_actions button:hover {
  background: var(--bg);
  color: var(--ink);
}

.product_actions button.is_danger:hover {
  color: var(--danger);
  border-color: var(--danger);
}

/* ============================================================
   شعار الموقع
   ============================================================ */
#site_logo {
  margin: 0;
  color: var(--accent);
  font-size: 18px;
  white-space: nowrap;
}

/* ============================================================
   قائمة التصنيفات في الهيدر
   ============================================================ */
.category_menu {
  position: relative;
}

.category_dropdown,
.location_dropdown {
  position: absolute;
  top: calc(100% + 8px);
  left: auto;
  /* محاذاة القائمة لليسار مع الزر لتتوسع نحو اليمين داخل الشاشة */
  right: 0;
  /* إلغاء محاذاة اليمين */
  min-width: 220px;
  max-width: calc(100vw - 32px);
  max-height: 70vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  box-shadow: var(--shadow-2);
  padding: 6px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.category_dropdown>button,
.location_dropdown>button,
.category_menu_item>button,
.category_submenu button {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* توزيع النص وسهم القائمة الفرعية على أطراف الزر */
  text-align: right;
  white-space: normal;
  word-break: break-word;
  overflow-wrap: anywhere;
  line-height: 1.4;
}

.category_dropdown>button,
.location_dropdown>button {
  text-align: right;
  padding: 8px 10px;
  border: none;
  border-radius: var(--radius-s);
  background: transparent;
  color: var(--ink);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}

.category_dropdown>button:hover,
.location_dropdown>button:hover {
  background: var(--bg);
}

.category_dropdown>button.is_active,
.location_dropdown>button.is_active {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

/* التصنيف الرئيسي اللي ليه تصنيفات فرعية: زرار toggle بيفتح/يقفل قايمة تحته */
.category_menu_item {
  display: flex;
  flex-direction: column;
}

.category_menu_item>button {
  width: 100%;
  text-align: right;
  padding: 8px 10px;
  border: none;
  border-radius: var(--radius-s);
  background: transparent;
  color: var(--ink);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}

.category_menu_item>button:hover {
  background: var(--bg);
}

.category_menu_item>button.is_active {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

/* قايمة التصنيفات الفرعية اللي بتظهر تحت التصنيف الرئيسي لما يتفتح */
.category_submenu {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 2px 4px 4px 0;
  padding-right: 14px;
  border-right: 2px solid var(--line);
}

.category_submenu button {
  text-align: right;
  padding: 7px 10px;
  border: none;
  border-radius: var(--radius-s);
  background: transparent;
  color: var(--ink-soft);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}

.category_submenu button:hover {
  background: var(--bg);
  color: var(--ink);
}

.category_submenu button.is_active {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

/* تصنيف المنتج + التصنيف الفرعي + حالة المنتج داخل الفورم */
.category_field,
.subcategory_field,
.condition_field {
  text-align: right;
  margin-bottom: 16px;
}

/* شارة التصنيف (رئيسي + فرعي) اللي بتظهر فوق كل كارت منتج */
.category_badge {
  display: inline-flex;
  margin-bottom: 6px;
  margin-left: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--brand-dark);
  font-size: 11px;
  font-weight: 700;
}

/* شارة حالة المنتج (ممتاز / جيد جدًا / ...) جمب شارة التصنيف */
.condition_badge {
  display: inline-flex;
  margin-bottom: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 11px;
  font-weight: 700;
}

/* حقول نوع التبادل وتفاصيله والمكان داخل الفورم */
.exchange_type_field,
.exchange_details_field,
.location_field {
  text-align: right;
  margin-bottom: 16px;
}

/* شارة نوع التبادل: لون مختلف حسب النوع (مجاني / تبادل / سعر) */
.exchange_badge {
  display: inline-flex;
  margin-bottom: 6px;
  margin-left: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
}

.exchange_badge_free {
  background: #E3F2E8;
  color: #1F4A3B;
}

.exchange_badge_trade {
  background: #E8F0FA;
  color: #1F4A7A;
}

.exchange_badge_price {
  background: #FCEBD9;
  color: #8A4B08;
}

/* شارة حالة المنتج: متاح (أخضر) أو تم التبادل (رمادي) */
.status_badge {
  display: inline-flex;
  margin-bottom: 6px;
  margin-left: 6px;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
}

.status_available {
  background: #E3F2E8;
  color: #1F4A3B;
}

.status_done {
  background: #E7E7E7;
  color: #55555F;
}

/* تعتيم الكارت كامل لما المنتج يبقى "تم التبادل"، عشان يبان بصريًا خارج التداول */
.product_card.is_completed {
  opacity: 0.65;
}

/* ============================================================
   شريط الصور المتحركة (Hero Slideshow)
   ============================================================ */
.hero_slideshow {
  position: relative;
  width: 100%;
  height: 280px;
  overflow: hidden;
}

.hero_slide {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 20px;
  text-align: center;
  color: #fff;
  opacity: 0;
  transition: opacity 0.8s ease;
}

.hero_slide.is_active {
  opacity: 1;
}

.hero_slide:nth-child(1) {
  background: linear-gradient(135deg, var(--brand), var(--brand-dark));
}

.hero_slide:nth-child(2) {
  background: linear-gradient(135deg, #2f6b52, #1f4a3b);
}

.hero_slide:nth-child(3) {
  background: linear-gradient(135deg, #3a5c46, #17372b);
}

.hero_icon {
  font-size: 48px;
  line-height: 1;
}

.hero_slide h2 {
  margin: 0;
  font-size: clamp(18px, 3vw, 26px);
}

.hero_slide p {
  margin: 0;
  max-width: 480px;
  font-size: 14px;
  opacity: 0.9;
}

/* ============================================================
   عداد الأثر البيئي (Eco-Impact Tracker)
   ============================================================ */
.eco_impact_bar {
  max-width: 1100px;
  margin: 24px auto 0;
  padding: 0 24px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.eco_stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-top: 4px solid var(--brand);
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-1);
  text-align: center;
}

.eco_stat_icon {
  font-size: 28px;
  line-height: 1;
}

.eco_stat_number {
  font-size: 28px;
  font-weight: 800;
  color: var(--brand-dark);
}

.eco_stat_label {
  font-size: 13px;
  color: var(--ink-soft);
}

/* ============================================================
   تنسيقات شاشات الموبايل والتابلت (Responsive Design)
   ============================================================ */
@media (max-width: 768px) {
  header {
    flex-wrap: wrap;
    /* يسمح بنزول العناصر لسطر جديد بدل الانقشاط */
    justify-content: space-between;
    padding: 10px 14px;
    gap: 8px;
  }

  /* شريط البحث ينزل في سطر مستقل بعرض كامل */
  header input#search_input {
    width: 100%;
    order: 3;
    margin-top: 4px;
  }

  /* تصغير أحجام الأزرار لتتناسب مع الشاشة */
  header button {
    padding: 8px 14px;
    font-size: 13px;
  }

  /* في الموبايل بنفتح القايمتين في نص الشاشة مهما كان مكان الزرار، بدل ما نعلّقهم تحته */
  .category_dropdown,
  .location_dropdown {
    position: fixed;
    top: 50%;
    left: 50%;
    right: auto;
    transform: translate(-50%, -50%);
    width: min(90vw, 320px);
    max-width: min(90vw, 320px);
    max-height: 80vh;
  }


  /* ضبط ارتفاع الهيرو سلايد شو في الموبايل */
  .hero_slideshow {
    height: 210px;
  }

  .hero_icon {
    font-size: 36px;
  }

  /* ضبط شبكة المنتجات لتظهر كارتين في السطر بدلاً من الانقشاط */
  main {
    padding: 16px 12px;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
  }

  /* عداد الأثر البيئي: عمود واحد بدل تلات أعمدة في الشاشات الصغيرة */
  .eco_impact_bar {
    grid-template-columns: 1fr;
    padding: 0 14px;
  }

  .location_filter {
    max-width: 120px;
  }
}