:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --ink: #1d2a26;
  --muted: #6b7a75;
  --line: #e2e6e4;
  --brand: #0f6b5c;
  --brand-2: #14917c;
  --brand-soft: #e3f3ef;
  --gold: #c8962d;
  --gold-soft: #fbf3e0;
  --red: #c0392b;
  --red-soft: #fdecea;
  --blue: #2c6db3;
  --blue-soft: #e8f0fa;
  --purple: #7a4cc2;
  --purple-soft: #f1ebfa;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(20, 40, 35, .07);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Cairo", "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.6;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
button { font-family: inherit; cursor: pointer; }

/* ── الشريط العلوي ── */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: linear-gradient(90deg, var(--brand), var(--brand-2));
  color: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
}
.brand { font-weight: 800; font-size: 19px; white-space: nowrap; }
.brand-icon { font-size: 22px; }
.company-switch {
  display: flex; background: rgba(255,255,255,.15); border-radius: 999px; padding: 3px;
  margin-inline-start: auto;
}
.company-switch button {
  border: 0; background: transparent; color: #fff; padding: 6px 16px; border-radius: 999px;
  font-weight: 600; font-size: 14px; white-space: nowrap;
}
.company-switch button.active { background: #fff; color: var(--brand); }
.logout { color: #fff; opacity: .85; text-decoration: none; font-size: 14px; }
.user-box { display: flex; align-items: center; gap: 12px; white-space: nowrap; }
.user-name { font-size: 14px; font-weight: 600; }
.top-link { color: #fff; font-size: 13px; background: rgba(255,255,255,.18); padding: 4px 12px; border-radius: 999px; text-decoration: none; }
.cur-pill { font-size: 12px; background: rgba(255,255,255,.18); padding: 3px 10px; border-radius: 999px; }
.cur-select { font: inherit; font-size: 13px; border: 0; border-radius: 8px; padding: 3px 6px; color: var(--brand); font-weight: 700; }
.company-switch.single button { cursor: default; }
.orig-val { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
.logout:hover { opacity: 1; }

.wrap { max-width: 1180px; margin: 0 auto; padding: 18px 16px 40px; }

/* ── البحث ── */
.search-box { margin-bottom: 16px; }
.search-field {
  position: relative; display: flex; align-items: center; gap: 8px;
  background: var(--card); border: 2px solid var(--line); border-radius: 999px;
  padding: 6px 6px 6px 6px; box-shadow: var(--shadow);
  transition: border-color .15s;
}
.search-field:focus-within { border-color: var(--brand-2); }
.search-icon { font-size: 20px; padding-inline-start: 12px; }
#phoneInput {
  flex: 1; border: 0; outline: 0; font: inherit; font-size: 20px; font-weight: 700;
  letter-spacing: 1px; background: transparent; min-width: 0; direction: ltr; text-align: right;
}
#phoneInput::placeholder { font-size: 15px; font-weight: 400; letter-spacing: 0; direction: rtl; }
.btn-primary {
  border: 0; background: var(--brand); color: #fff; font-weight: 700; font-size: 15px;
  padding: 10px 26px; border-radius: 999px;
}
.btn-primary:hover { background: var(--brand-2); }
.btn-primary:disabled { opacity: .6; cursor: wait; }

.suggest-list {
  position: absolute; top: calc(100% + 6px); inset-inline: 0; z-index: 30;
  list-style: none; margin: 0; padding: 6px; background: #fff;
  border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.12);
  max-height: 340px; overflow-y: auto;
}
.suggest-list li {
  display: flex; justify-content: space-between; gap: 12px; padding: 8px 12px; border-radius: 8px; cursor: pointer;
}
.suggest-list li.active, .suggest-list li:hover { background: var(--brand-soft); }
.suggest-list .s-phone { font-weight: 700; direction: ltr; }
.suggest-list .s-name { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggest-list .s-co { font-size: 12px; color: var(--brand); white-space: nowrap; }

.status {
  padding: 12px 16px; border-radius: 12px; margin-bottom: 16px; background: var(--blue-soft); color: var(--blue);
  font-weight: 600;
}
.status.error { background: var(--red-soft); color: var(--red); }
.status.loading::before {
  content: ""; display: inline-block; width: 14px; height: 14px; margin-inline-end: 8px; vertical-align: -2px;
  border: 2px solid currentColor; border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.welcome { text-align: center; padding: 60px 16px; }
.welcome-emoji { font-size: 56px; }
.welcome h2 { margin: 8px 0 4px; }

/* ── 1. رأس العميل ── */
.hero {
  display: flex; gap: 18px; align-items: center;
  background: linear-gradient(120deg, #0f6b5c 0%, #1a8a76 55%, #28a48d 100%);
  color: #fff; border-radius: var(--radius); padding: 22px 24px; box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.hero-avatar {
  width: 74px; height: 74px; flex: 0 0 74px; border-radius: 50%;
  background: rgba(255,255,255,.18); display: grid; place-items: center; font-size: 34px; font-weight: 800;
  border: 3px solid rgba(255,255,255,.5);
}
.hero-main { min-width: 0; flex: 1; }
.hero-name-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.hero h1 { margin: 0; font-size: 30px; font-weight: 800; line-height: 1.3; }
.hero-sub { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin-top: 2px; }
.hero .phone { font-size: 20px; font-weight: 700; letter-spacing: 1px; }
.hero .muted { color: rgba(255,255,255,.8); }
.other-names { font-size: 13px; color: rgba(255,255,255,.8); margin-top: 2px; }
.tier-details { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.tier-details span {
  font-size: 12px; background: rgba(255,255,255,.15); border-radius: 999px; padding: 1px 10px;
}
.hero-side { display: flex; gap: 10px; flex: 0 0 auto; }
.kpi {
  background: rgba(255,255,255,.14); border-radius: 12px; padding: 10px 14px; min-width: 110px; text-align: center;
}
.kpi-v { font-size: 20px; font-weight: 800; direction: ltr; white-space: nowrap; }
.kpi-l { font-size: 12px; color: rgba(255,255,255,.85); }
.pref-group + .pref-group { margin-top: 8px; padding-top: 8px; border-top: 1px dashed rgba(0,0,0,.12); }
.pref-group-label { font-size: 12px; font-weight: 700; color: var(--muted); width: 100%; }
.tier-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 14px; font-weight: 800; padding: 3px 14px; border-radius: 999px;
  background: #fff; color: var(--brand);
}
.tier-badge.vip { background: linear-gradient(90deg, #e9c46a, #c8962d); color: #3b2a05; }
.tier-badge.special { background: #fff; color: var(--purple); }
.tier-badge.new { background: #e8f0fa; color: var(--blue); }
.tier-badge.inactive { background: #eceff1; color: #607d8b; }
.tier-badge.none { background: #eceff1; color: #607d8b; }

/* ── 2. البطاقات ── */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 16px; }
.stat {
  background: var(--card); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
  border-top: 4px solid var(--brand); position: relative;
}
.stat-avg { border-top-color: var(--gold); }
.stat-last { border-top-color: var(--blue); }
.stat-branch { border-top-color: var(--purple); }
.stat-icon { position: absolute; top: 12px; inset-inline-end: 14px; font-size: 24px; opacity: .85; }
.stat-label { color: var(--muted); font-weight: 600; font-size: 14px; }
.stat-value { font-size: 26px; font-weight: 800; margin-top: 2px; line-height: 1.35; }
.stat-foot { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* ── البطاقات العامة ── */
.card { background: var(--card); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card-title { margin: 0 0 12px; font-size: 18px; font-weight: 800; }
.card-title-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.history-summary { display: flex; gap: 8px; flex-wrap: wrap; }
.history-summary span { font-size: 13px; background: var(--bg); border-radius: 999px; padding: 2px 12px; }

/* ── 3. التفضيلات ── */
.pref-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.pref { border-radius: 12px; padding: 12px 14px; }
.pref-color { background: #fff4ec; }
.pref-size { background: var(--blue-soft); }
.pref-item { background: var(--purple-soft); }
.pref-head { font-weight: 700; margin-bottom: 8px; }
.pref-icon { font-size: 18px; }
.pref-body { display: flex; flex-wrap: wrap; gap: 8px; }
#prefColors, #prefSizes, #prefItems { display: block; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; background: #fff; border-radius: 999px;
  padding: 4px 12px; font-weight: 700; font-size: 15px; box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.chip.top { outline: 2px solid currentColor; }
.pref-color .chip.top { color: #b35a1f; }
.pref-size .chip.top { color: var(--blue); }
.pref-item .chip.top { color: var(--purple); }
.chip small { color: var(--muted); font-weight: 600; font-size: 12px; }
.chip .chip-text { color: var(--ink); }
.swatch { width: 16px; height: 16px; border-radius: 50%; border: 1px solid rgba(0,0,0,.2); flex: 0 0 16px; }
.size-chip { min-width: 44px; justify-content: center; font-size: 17px; }
.empty { color: var(--muted); font-size: 14px; }
.tip {
  margin-top: 12px; padding: 10px 14px; border-radius: 10px; background: var(--gold-soft); color: #6b4e12;
  font-size: 14px;
}
.tip:empty { display: none; }

/* ── 4. الجدول ── */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14px; }
thead th {
  position: sticky; top: 0; background: #f7f9f8; text-align: right; font-weight: 700; color: #3d4b47;
  padding: 10px 12px; border-bottom: 2px solid var(--line); white-space: nowrap;
}
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
tbody tr.inv-row { cursor: pointer; }
tbody tr.inv-row:hover td { background: #f3faf8; }
tbody tr.inv-row.open td { background: var(--brand-soft); }
td.num { direction: ltr; text-align: right; font-variant-numeric: tabular-nums; }
td.neg { color: var(--red); }
.type-pill { font-size: 12px; font-weight: 700; padding: 2px 10px; border-radius: 999px; }
.type-sale { background: var(--brand-soft); color: var(--brand); }
.type-return { background: var(--red-soft); color: var(--red); }
.type-exchange { background: var(--gold-soft); color: #8a6212; }
.type-other { background: #eceff1; color: #607d8b; }
.co-pill { font-size: 12px; padding: 1px 8px; border-radius: 6px; background: var(--bg); }
.caret { color: var(--muted); transition: transform .15s; display: inline-block; }
tr.open .caret { transform: rotate(-90deg); }
tr.lines-row > td { background: #fafcfb; padding: 8px 12px 14px; white-space: normal; }
.lines-table { font-size: 13px; background: #fff; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.lines-table th { position: static; background: #f1f4f3; padding: 6px 10px; }
.lines-table td { padding: 6px 10px; }
.lines-table tr.ret td { color: var(--red); }
/* صورة الصنف */
.thumb-cell { width: 76px; }
.thumb {
  width: 64px; height: 64px; object-fit: cover; border-radius: 10px; background: var(--bg); display: block;
  border: 1px solid var(--line); cursor: zoom-in;
}
.thumb.missing { cursor: default; display: grid; place-items: center; font-size: 11px; color: var(--muted); text-align: center; line-height: 1.2; }
.lightbox {
  position: fixed; inset: 0; z-index: 100; background: rgba(10, 20, 18, .88);
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 16px;
}
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 80vh; border-radius: 12px; background: #fff; object-fit: contain; }
.lb-caption { color: #fff; margin-top: 10px; font-weight: 700; text-align: center; }
.lb-close {
  position: absolute; top: 12px; inset-inline-start: 12px; width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: rgba(255,255,255,.2); color: #fff; font-size: 20px;
}
tr.total-row td { font-weight: 800; background: #f7f9f8; border-top: 2px solid #e2e6e4; }
tr.total-row td:first-child { color: #444; }

/* ── إدارة المستخدمين ── */
.admin-tools { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.admin-tools input[type=search] { flex: 1; min-width: 200px; font: inherit; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; }
.chk { font-size: 14px; display: flex; gap: 6px; align-items: center; }
.btn-sm { padding: 6px 16px; font-size: 14px; }
.users-table td { vertical-align: middle; }
.users-table tr.u-on td { background: #f3faf8; }
.u-name { font-weight: 700; }
.u-branches { white-space: normal !important; min-width: 220px; }
.br-chip { display: inline-block; font-size: 11px; background: var(--bg); border-radius: 6px; padding: 0 6px; margin: 1px 2px; }
.br-chip.qa { background: #f6e7ee; color: #7a1f45; font-weight: 700; }
.adm-input, .adm-select { font: inherit; font-size: 13px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; min-width: 120px; }
.big-chk { width: 20px; height: 20px; }
.save-msg { display: block; margin-top: 2px; }
.save-msg.ok { color: var(--brand); }
.save-msg.err { color: var(--red); white-space: normal; }

/* ── الدخول ── */
.login-body { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(135deg, #0f6b5c, #28a48d); }
.login-card {
  background: #fff; padding: 32px 28px; border-radius: 18px; width: min(360px, 92vw); text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,.2);
}
.login-logo { font-size: 48px; }
.login-card h1 { margin: 4px 0 0; }
.login-card label { display: block; text-align: right; margin-top: 14px; font-weight: 600; font-size: 14px; }
.login-card input {
  width: 100%; margin-top: 4px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font: inherit;
}
.login-card .btn-primary { width: 100%; margin-top: 20px; padding: 12px; }
.alert { background: var(--red-soft); color: var(--red); padding: 8px 12px; border-radius: 10px; margin-top: 12px; font-size: 14px; }

/* ── أصناف الفاتورة على الهاتف: بطاقة لكل صنف والصورة كبيرة ── */
@media (max-width: 640px) {
  tr.lines-row > td { padding: 8px 6px 12px; }
  .lines-table, .lines-table tbody, .lines-table tr, .lines-table td { display: block; width: 100%; }
  .lines-table { border: 0; background: transparent; }
  /* جدول الفواتير أعرض من الشاشة (يُمرر أفقياً) — نثبت بطاقات الأصناف على عرض الشاشة */
  .lines-table { position: sticky; right: 0; width: calc(100vw - 92px) !important; max-width: calc(100vw - 92px); }
  .lines-table thead { display: none; }
  .lines-table tr.line {
    display: grid; grid-template-columns: 96px 1fr; column-gap: 12px; row-gap: 2px;
    background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 10px;
  }
  .lines-table tr.line td {
    padding: 0; border: 0; white-space: normal; display: flex; justify-content: space-between; align-items: baseline;
    gap: 8px; direction: rtl; text-align: left; min-width: 0;
  }
  .lines-table tr.line td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; flex: 0 0 auto; }
  .lines-table tr.line td.thumb-cell { grid-column: 1; grid-row: 1 / span 9; width: auto; display: block; }
  .lines-table tr.line td.thumb-cell::before { content: none; }
  .lines-table tr.line td:not(.thumb-cell) { grid-column: 2; }
  .lines-table tr.line td.l-item { font-size: 16px; font-weight: 800; }
  .lines-table tr.line td.l-item::before { content: none; }
  .lines-table tr.line td.l-value { font-weight: 800; font-size: 15px; border-top: 1px dashed var(--line); padding-top: 4px; margin-top: 2px; }
  .lines-table .thumb { width: 96px; height: 96px; }
  .lines-table tr.total-row { display: flex; justify-content: space-between; background: #f7f9f8; border-radius: 10px; padding: 8px 12px; margin-bottom: 6px; }
  .lines-table tr.total-row td { display: inline; width: auto; padding: 0; border: 0; background: none; }
  td.num { text-align: left; }
  .user-name { display: none; }
}

/* ── الجوال ── */
@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; padding: 10px 12px; gap: 8px; }
  .company-switch { order: 3; width: 100%; margin: 0; justify-content: space-between; }
  .company-switch button { flex: 1; padding: 6px 8px; }
  .logout { margin-inline-start: auto; }
  .stats, .pref-grid { grid-template-columns: 1fr; }
  .hero { padding: 16px; gap: 12px; flex-wrap: wrap; }
  .hero-side { width: 100%; }
  .kpi { flex: 1; min-width: 0; padding: 8px 6px; }
  .kpi-v { font-size: 16px; }
  .hero-avatar { width: 56px; height: 56px; flex-basis: 56px; font-size: 26px; }
  .hero h1 { font-size: 23px; }
  .stat-value { font-size: 22px; }
  #phoneInput { font-size: 18px; }
  .btn-primary { padding: 10px 18px; }
}
