* { box-sizing: border-box; min-width: 0; }
body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Tahoma, Arial, sans-serif;
  background: #f4f6f5;
  color: #1c1c1c;
  direction: rtl;
}

/* ---------------- login ---------------- */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(160deg, #0a5 0%, #063 100%);
}
.login-card {
  background: #fff;
  border-radius: 16px;
  padding: 32px 28px;
  width: 90%;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0,0,0,.2);
}
.login-logo { font-size: 40px; }
.login-card h1 { margin: 6px 0 0; font-size: 22px; }
.login-card .subtitle { color: #666; margin: 2px 0 20px; }
.login-card form { text-align: right; }
.login-card label { display: block; font-size: 13px; color: #555; margin: 12px 0 4px; }
.login-card input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 10px;
  font-size: 16px;
}
.login-card button {
  width: 100%;
  margin-top: 20px;
  padding: 13px;
  border: none;
  border-radius: 10px;
  background: #0a5;
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}
.error-box {
  background: #fdecea;
  color: #b3261e;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  margin-bottom: 6px;
}

/* ---------------- app shell ---------------- */
.topbar {
  background: #0a5;
  color: #fff;
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar .brand { font-weight: 700; font-size: 17px; flex-shrink: 0; }
.topbar .logout { color: #eafff2; text-decoration: none; font-size: 14px; flex-shrink: 0; }
.topbar-select {
  flex: 1;
  max-width: 260px;
  margin: 0 8px;
  padding: 9px 10px;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  background: #fff;
  color: #1c1c1c;
}

.container { padding: 12px; max-width: 700px; margin: 0 auto; }

.search-card, .result-card {
  background: #fff;
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}

.field-row { margin-bottom: 12px; }
.field-row:last-child { margin-bottom: 0; }
.field-row label { display: block; font-size: 13px; color: #555; margin-bottom: 6px; }

/* ---------------- أزرار الفترة الزمنية ---------------- */
.period-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.period-btn {
  flex: 1 1 auto;
  min-width: 70px;
  padding: 9px 10px;
  border: 1px solid #ddd;
  border-radius: 20px;
  background: #f7f9f8;
  color: #444;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.period-btn.active {
  background: #0a5;
  color: #fff;
  border-color: #0a5;
}

/* ---------------- زر أفضل 15 صنف + تبويب المقياس ---------------- */
.toggle-btn {
  width: 100%;
  padding: 11px;
  border: 1px dashed #0a5;
  border-radius: 12px;
  background: #eef6f0;
  color: #0a5;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.toggle-btn.active { background: #0a5; color: #fff; border-style: solid; }
.toggle-arrow {
  display: inline-block;
  margin-inline-start: 4px;
  transition: transform .2s;
}
.toggle-btn.active .toggle-arrow { transform: rotate(180deg); }

/* ---------------- زر "كل البائعين" أسفل الصفحة ---------------- */
.all-sellers-btn {
  display: block;
  width: 100%;
  margin: 16px 0 24px;
  background: #0a5;
  color: #fff;
  border: none;
}

.metric-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}
.metric-btn {
  flex: 1 1 0;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 20px;
  background: #f7f9f8;
  color: #444;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.metric-btn.active { background: #0a5; color: #fff; border-color: #0a5; }

/* ---------------- جدول أفضل الأصناف ---------------- */
.item-name-cell { display: flex; align-items: center; gap: 6px; justify-content: flex-start; }
.item-thumb {
  width: 44px; height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: #f2f2f2;
  flex-shrink: 0;
}
.item-thumb-empty {
  width: 44px; height: 44px;
  border-radius: 8px;
  background: #f2f2f2;
  display: flex; align-items: center; justify-content: center;
  color: #bbb;
  font-size: 18px;
  flex-shrink: 0;
}
#topItemsTable td:first-child, #topItemsTable th:first-child { text-align: right; }
.item-thumb { cursor: pointer; }

/* ---------------- جدول اجمالي مبيعات الصنف + جدول أصنافه الفردية ---------------- */
#categoryTable td:first-child, #categoryTable th:first-child { text-align: right; }
#categoryItemsTable td:first-child, #categoryItemsTable th:first-child { text-align: right; }
#categoryTableWrap { max-height: 60vh; overflow-y: auto; }
#categoryItemsTableWrap { max-height: 60vh; overflow-y: auto; }
/* صف الإجمالي أسفل كل جدول (اجمالي مبيعات الصنف / أصناف الفئة الواحدة) */
tr.total-row td { font-weight: 800; background: #f7f9f8; border-top: 2px solid #e2e6e4; }
tr.total-row td:first-child { color: #444; }

/* ---------------- إظهار المزيد (أفضل 15 صنف) ---------------- */
.load-more-btn {
  width: 100%;
  margin-top: 10px;
  padding: 11px;
  border: 1px solid #ddd;
  border-radius: 12px;
  background: #f7f9f8;
  color: #0a5;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.load-more-btn:disabled { opacity: .6; cursor: default; }

/* ---------------- تفاصيل الصنف: صورة ضبابية بالخلفية + بيانات واضحة فوقها ---------------- */
.item-detail-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.75);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
.item-detail-card {
  position: relative;
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  border-radius: 16px;
  overflow: hidden;
  background: #222;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
}
/* الوضع الافتراضي: صورة كبيرة وواضحة (بدون تمويه) */
.item-detail-image-view {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  min-height: 240px;
  max-height: 85vh;
}
.item-detail-image-view img {
  display: block;
  max-width: 100%;
  max-height: 85vh;
  object-fit: contain;
}

.item-detail-data-view { position: relative; }
.item-detail-bg {
  position: absolute;
  inset: -25px;
  background-size: cover;
  background-position: center;
  filter: blur(22px) brightness(0.55);
  transform: scale(1.15);
}
.item-detail-content {
  position: relative;
  z-index: 1;
  padding: 16px;
  max-height: 85vh;
  overflow-y: auto;
  color: #fff;
}
.item-detail-header { padding-inline-end: 40px; padding-inline-start: 40px; margin-bottom: 8px; }
.item-detail-name { font-size: 16px; font-weight: 700; text-shadow: 0 1px 3px rgba(0,0,0,.6); }
.item-detail-summary { font-size: 13px; color: #eaf7ef; margin-top: 4px; text-shadow: 0 1px 3px rgba(0,0,0,.6); }
.item-detail-table-wrap {
  background: rgba(255,255,255,.14);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255,255,255,.2);
}
#itemDetailTable th {
  background: rgba(0,0,0,.35);
  color: #fff;
}
#itemDetailTable td {
  color: #fff;
  border-bottom: 1px solid rgba(255,255,255,.15);
}
#itemDetailTable .branch-value { color: #7CE8A0; }

/* زرّا الإغلاق (✕) وإظهار البيانات (📊) — فوق الصورة أو فوق طبقة البيانات */
.item-detail-btn {
  position: absolute;
  top: 14px;
  z-index: 3;
  background: rgba(0,0,0,.45);
  border: none;
  color: #fff;
  font-size: 18px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 1px 6px rgba(0,0,0,.35);
}
.lightbox-close { left: 14px; font-size: 20px; }
.item-detail-toggle-btn { right: 14px; }

/* ---------------- فترة محددة ---------------- */
.date-inputs {
  display: flex;
  gap: 10px;
}
.date-inputs > div { flex: 1 1 0; }
.date-inputs label { font-size: 12px; color: #777; margin-bottom: 3px; }
input[type=date] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 10px;
  font-size: 15px;
  background: #fff;
}

/* ---------------- مجموعات الفروع ---------------- */
.groups-company-block { margin-bottom: 10px; }
.groups-company-block:last-child { margin-bottom: 0; }
.groups-company-title {
  font-size: 12px;
  color: #888;
  font-weight: 700;
  margin-bottom: 6px;
}
.groups-checks {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}
.chk { display: flex; align-items: center; gap: 6px; font-size: 14px; color: #333; }
.chk input { width: 17px; height: 17px; }
.group-info-btn {
  border: none;
  background: #eef6f0;
  color: #0a5;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 11px;
  line-height: 18px;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
}
.group-info-list { margin: 0; padding: 0; }
.group-info-list .chk {
  padding: 10px 4px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
  justify-content: flex-start;
}
.group-info-list .chk:last-child { border-bottom: none; }
.group-info-actions {
  display: flex;
  gap: 10px;
  margin: 6px 0 10px;
}
.group-info-action-btn {
  flex: 1 1 0;
  border: 1px solid #0a5;
  background: #fff;
  color: #0a5;
  border-radius: 8px;
  padding: 8px 6px;
  font-size: 13px;
  cursor: pointer;
}
.group-info-action-btn:active { background: #eef6f0; }

.status { font-size: 13px; min-height: 18px; margin-top: 8px; }
.status.err { color: #b3261e; }
.status.ok { color: #0a5; }
.status.loading { color: #06c; }

/* ---------------- إجمالي (القيمة + الكمية جنبًا إلى جنب) ---------------- */
.total-cards {
  display: flex;
  gap: 10px;
  margin-bottom: 12px;
}
.total-card {
  flex: 1 1 0;
  min-width: 0;
  text-align: center;
  background: linear-gradient(160deg, #0a5 0%, #063 100%);
  color: #fff;
  border-radius: 14px;
  padding: 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
}
.total-label { font-size: 13px; opacity: .9; }
.total-value { font-size: 32px; font-weight: 800; margin: 4px 0; }
.total-sub { font-size: 12px; opacity: .8; }

/* ---------------- جدول الفروع ---------------- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid #eee;
  border-radius: 10px;
  -webkit-overflow-scrolling: touch;
}
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 11px 12px; text-align: center; border-bottom: 1px solid #f0f0f0; }
th { background: #f7f9f8; color: #444; font-weight: 700; position: sticky; top: 0; }
td:first-child, th:first-child { text-align: right; }
tbody tr { cursor: pointer; }
tbody tr:active { background: #f4f6f5; }
.branch-name-cell { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.branch-rank {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: #eef6f0;
  color: #0a5;
  font-size: 11px;
  font-weight: 700;
  flex-shrink: 0;
}
.branch-company-tag {
  font-size: 10.5px;
  color: #999;
  font-weight: 400;
  margin-inline-start: 5px;
}
.branch-value { font-weight: 700; color: #0a5; }
.chevron { color: #bbb; font-size: 12px; flex-shrink: 0; }

/* ---------------- نافذة أفضل البائعين ---------------- */
.scan-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.85);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
}
.scan-modal-inner {
  background: #111;
  border-radius: 14px;
  width: 100%;
  max-width: 420px;
  max-height: 82vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.scan-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  color: #fff;
  font-size: 14px;
  flex-shrink: 0;
}
.scan-close {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 18px;
  padding: 4px 8px;
  cursor: pointer;
}
.branch-action-inner { background: #fff; }
.branch-action-inner .scan-modal-header { color: #1c1c1c; border-bottom: 1px solid #eee; font-weight: 700; }
.branch-action-inner .scan-close { color: #666; }
.branch-action-body { padding: 14px; overflow-y: auto; }
#sellersTableWrap { max-height: 50vh; overflow-y: auto; }
#allSellersTableWrap { max-height: 60vh; overflow-y: auto; }

/* ---------------- نافذة طلب تحويل رصيد الصنف بين المعارض ---------------- */
/* تُفتَح هذه النافذة من فوق نافذة تفاصيل الصنف (z-index: 200) — لازم يكون
   ترتيبها أعلى منها (وليس 100 الافتراضية لبقية نوافذ .scan-modal) وإلا
   تظهر خلفها ولا يمكن الوصول إليها إطلاقًا. */
#branchTransferModal { z-index: 250; }
.branch-transfer-actions { display: flex; flex-direction: column; gap: 10px; }
.branch-action-btn {
  display: block;
  text-align: center;
  padding: 13px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 700;
  text-decoration: none;
}
.branch-action-btn.call { background: #eaf2ff; color: #0a58ca; }
.branch-action-btn.whats { background: #e7f9ee; color: #128c3e; }
.branch-action-btn.mail { background: #fff3e0; color: #b45c00; }
.branch-action-btn.group {
  background: #f1eefc; color: #5b3fb5;
  border: none; font-family: inherit; width: 100%;
}
.branch-action-note { font-size: 12px; color: #999; text-align: center; padding: 8px 4px 0; }

/* ---------------- زر ونافذتا مقارنة المبيعات بالعام الماضي ---------------- */
.yoy-btn {
  border-color: #7a2ecc;
  background: #f6eefc;
  color: #7a2ecc;
}
.yoy-btn.active { background: #7a2ecc; color: #fff; border-style: solid; }
.yoy-grand-note { font-size: 12.5px; color: #666; text-align: center; padding: 2px 4px 10px; }
#yoyTableWrap { max-height: 55vh; overflow-y: auto; }
#yoyTable th, #yoyTable td { font-size: 12.5px; padding: 9px 7px; white-space: nowrap; }
/* عمود اسم المعرض يبقى ثابتًا (لا يتحرك) أثناء السحب الأفقي لمشاهدة بقية
   الأعمدة — يظهر دائمًا بأقصى اليمين (أول عمود بجدول RTL) */
#yoyTable th:first-child, #yoyTable td:first-child {
  position: sticky;
  right: 0;
  z-index: 1;
  background: #fff;
  white-space: normal;
}
#yoyTable thead th:first-child { z-index: 2; background: #f7f9f8; }
.pct-up { color: #0a5; font-weight: 700; }
.pct-down { color: #d33; font-weight: 700; }

/* نافذة التفاصيل اليومية تُفتح من فوق نافذة المقارنة (z-index أعلى من الافتراضي
   100 لبقية نوافذ .scan-modal) وإلا تظهر خلفها بالخطأ */
#yoyDetailModal { z-index: 150; }
.yoy-detail-inner { max-width: 96vw; }
#yoyDetailTableWrap { max-height: 62vh; overflow-y: auto; }
#yoyDetailTable th, #yoyDetailTable td { white-space: nowrap; padding: 8px 6px; font-size: 12.5px; }

@media (max-width: 400px) {
  .total-value { font-size: 26px; }
  .period-btn { font-size: 12px; padding: 8px; }
  th, td { padding: 9px 8px; font-size: 13px; }
}
