:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #dbeafe;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Tajawal', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  direction: rtl;
  font-size: 15px;
  min-height: 100vh;
}

img { max-width: 100%; display: block; }

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
}

/* ─── Top bar ─── */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar .brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
}
.topbar .brand-mark, .topbar .brand-mark-img {
  width: 32px; height: 32px;
  display: grid; place-items: center;
}
.topbar .brand-mark {
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  border-radius: 8px;
  color: white;
  font-weight: 800;
}
.topbar .brand-mark-img { object-fit: contain; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a {
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  text-decoration: none;
  font-weight: 500;
}
.topbar nav a.active, .topbar nav a:hover {
  background: var(--primary-soft);
  color: var(--primary-dark);
}
.topbar .logout {
  background: transparent;
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
}

.container { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* ─── Login ─── */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 4px 16px rgba(0,0,0,.04);
}
.login-card h1 {
  text-align: center;
  margin-bottom: 4px;
}
.login-card .subtitle {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 24px;
}

/* ─── Form fields ─── */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  margin-bottom: 6px;
  font-weight: 600;
  font-size: 14px;
}
.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color 0.15s;
}
.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.field textarea { min-height: 80px; resize: vertical; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field .hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* ─── Buttons ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 18px;
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
  font-size: 15px;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: white; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ─── Scanner ─── */
.scanner-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 16px;
}
.scanner-video {
  width: 100%;
  aspect-ratio: 4/3;
  background: #000;
  object-fit: cover;
}
.scanner-overlay {
  text-align: center;
  padding: 20px;
  color: var(--text-muted);
}
.scanner-controls {
  display: flex;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}

.barcode-input {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}
.barcode-input input {
  flex: 1;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
}

.upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  background: var(--surface);
}
.upload-area:hover, .upload-area.dragover {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.upload-area input { display: none; }
.upload-area .icon { font-size: 36px; margin-bottom: 8px; }

/* ─── Review ─── */
.review-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.confidence-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  padding: 10px 14px;
  background: var(--primary-soft);
  border-radius: var(--radius-sm);
  font-size: 14px;
}
.confidence-bar .badge {
  background: var(--primary);
  color: white;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
}

.images-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.images-grid .img-slot {
  aspect-ratio: 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
}
.images-grid .img-slot img {
  width: 100%; height: 100%; object-fit: cover;
}
.images-grid .img-slot .remove {
  position: absolute;
  top: 4px; inset-inline-start: 4px;
  width: 24px; height: 24px;
  background: rgba(0,0,0,.6);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}

.price-section {
  background: var(--bg);
  padding: 14px;
  border-radius: var(--radius-sm);
  margin: 12px 0;
}
.price-section .ref-price {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 6px;
}
.price-section .suggested {
  color: var(--success);
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  text-decoration: underline dotted;
}

.qty-input {
  display: flex;
  align-items: center;
  gap: 8px;
}
.qty-input button {
  width: 36px; height: 36px;
  border: 1px solid var(--border);
  background: white;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 700;
  font-size: 18px;
}
.qty-input input {
  width: 70px;
  text-align: center;
}

/* ─── Dashboard ─── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.stat-card .label { color: var(--text-muted); font-size: 13px; margin-bottom: 4px; }
.stat-card .value { font-size: 22px; font-weight: 700; }

.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.toolbar input, .toolbar select {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.toolbar input { flex: 1; min-width: 180px; }

.products-table {
  display: grid;
  gap: 10px;
}
.product-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  display: grid;
  grid-template-columns: 70px 1fr auto;
  gap: 12px;
  align-items: center;
}
.product-row .thumb {
  width: 70px; height: 70px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.product-row .thumb img { width: 100%; height: 100%; object-fit: cover; }
.product-row .meta { min-width: 0; }
.product-row .name { font-weight: 600;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.product-row .small { font-size: 13px; color: var(--text-muted); }
.product-row .actions { display: flex; gap: 6px; }
.product-row .actions button {
  padding: 6px 10px;
  font-size: 13px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: white;
  cursor: pointer;
}

.status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.status-draft { background: #fef3c7; color: #92400e; }
.status-active { background: #dcfce7; color: #166534; }
.status-sold { background: #e0e7ff; color: #3730a3; }
.status-archived { background: #e5e7eb; color: #374151; }

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: 20px;
  inset-inline-start: 50%;
  transform: translateX(50%);
  background: var(--text);
  color: white;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  z-index: 200;
  opacity: 0;
  transition: opacity .25s;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
.toast.warn { background: var(--warning); }

/* ─── Confirm modal (replaces window.confirm — phone-friendly) ─── */
.qb-confirm {
  position: fixed; inset: 0; z-index: 300;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.qb-confirm__backdrop {
  position: absolute; inset: 0;
  background: rgba(15, 23, 42, .55);
  backdrop-filter: blur(2px);
}
.qb-confirm__card {
  position: relative;
  background: var(--surface);
  border-radius: 14px;
  padding: 22px 20px 18px;
  max-width: 380px; width: 100%;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.qb-confirm__msg {
  font-size: 15px; font-weight: 600;
  margin-bottom: 16px; line-height: 1.55;
  color: var(--text);
}
.qb-confirm__actions {
  display: flex; gap: 8px; justify-content: flex-end;
}
.qb-confirm__actions .btn {
  min-height: 44px; padding: 0 18px; font-size: 14px;
}

/* ─── Stage pills variants (in case dashboards reuse later) ─── */
.btn-warn { background: var(--warning); color: white; }

/* ════════════════════════════════════════════
   تحسينات الموبايل — لوحة الإدارة
   ════════════════════════════════════════════ */

/* iOS: منع تكبير الصفحة عند الكتابة */
input, select, textarea { font-size: 16px; }

/* المساحة الآمنة لـ iPhone */
@supports (padding: max(0px)) {
  .topbar { padding-inline-start: max(14px, env(safe-area-inset-left)); padding-inline-end: max(14px, env(safe-area-inset-right)); }
  .container { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
}

@media (max-width: 600px) {
  /* Topbar مضغوط */
  .topbar {
    padding: 10px 12px;
    gap: 6px;
    flex-wrap: wrap;
  }
  .topbar .brand { font-size: 14px; gap: 6px; }
  .topbar .brand-mark { width: 28px; height: 28px; font-size: 14px; }
  .topbar .brand span { font-size: 13px; }
  .topbar nav a { padding: 7px 10px; font-size: 13px; }
  .topbar .logout {
    padding: 6px 10px;
    font-size: 12px;
  }

  .container { padding: 12px; }

  /* Login */
  .login-card { padding: 22px 20px; }
  .login-card h1 { font-size: 22px; }

  /* الحقول — تكدّس بدلاً من الصف */
  .field-row { grid-template-columns: 1fr; gap: 0; }

  /* الماسح */
  .scanner-video { aspect-ratio: 1 / 1; }
  .scanner-controls { padding: 10px; gap: 6px; }
  .scanner-controls .btn { flex: 1; padding: 10px; font-size: 13px; }

  .barcode-input input { padding: 12px; font-size: 16px; }
  .barcode-input .btn { padding: 12px 14px; }

  .upload-area { padding: 18px; }
  .upload-area .icon { font-size: 30px; }

  /* المراجعة */
  .review-card { padding: 14px; margin-bottom: 12px; }
  .confidence-bar { padding: 10px 12px; font-size: 13px; }

  .images-grid {
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
    gap: 6px;
  }

  /* أزرار الكمية أكبر للمس */
  .qty-input button { width: 40px; height: 40px; font-size: 20px; }
  .qty-input input { width: 64px; height: 40px; }

  .price-section { padding: 12px; }

  /* الداشبورد */
  .stats-grid { gap: 8px; grid-template-columns: repeat(2, 1fr); }
  .stat-card { padding: 12px; }
  .stat-card .label { font-size: 12px; }
  .stat-card .value { font-size: 19px; }

  .toolbar { gap: 6px; }
  .toolbar input, .toolbar select { padding: 10px; font-size: 14px; }
  .toolbar input { flex-basis: 100%; min-width: 0; }

  .product-row {
    padding: 8px;
    gap: 8px;
    grid-template-columns: 60px 1fr;
  }
  .product-row .thumb { width: 60px; height: 60px; }
  .product-row .actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
    border-top: 1px solid var(--border);
    padding-top: 6px;
    margin-top: 4px;
  }
  .product-row .name { font-size: 14px; }
  .product-row .small { font-size: 12px; }

  /* أزرار اللمس */
  .btn { min-height: 44px; padding: 11px 16px; }

  /* أيقونات نصية في الأزرار الأساسية */
  .btn { font-size: 14px; }
}

/* شاشات صغيرة جداً */
@media (max-width: 360px) {
  .topbar nav a { padding: 6px 8px; font-size: 12px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}
