/* ============================================================
   DESIGN TOKENS
   ============================================================ */
:root {
  --bg: #F3F5FA;
  --surface: #FFFFFF;
  --surface-2: #F8F9FC;

  --sidebar-bg: #12142B;
  --sidebar-bg-hover: #1B1E3D;
  --sidebar-bg-active: #262A52;
  --sidebar-text: #A6A9C8;
  --sidebar-text-active: #FFFFFF;

  --primary: #4F46E5;
  --primary-dark: #4338CA;
  --primary-soft: #EEEDFC;
  --mint: #0EA5A0;
  --mint-soft: #E3F8F6;

  --text: #1B1E30;
  --text-muted: #6B7089;
  --text-faint: #9A9DB5;
  --border: #E7E8F2;
  --border-strong: #D6D8E8;

  --success: #17A15A;
  --success-soft: #E4F8ED;
  --warning: #D68A0E;
  --warning-soft: #FCF1DC;
  --danger: #D8394C;
  --danger-soft: #FBE6E9;
  --info: #2E6BE6;
  --info-soft: #E6EDFC;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px rgba(18, 20, 43, 0.06);
  --shadow-md: 0 8px 24px rgba(18, 20, 43, 0.08);
  --shadow-lg: 0 20px 48px rgba(18, 20, 43, 0.18);

  --font-ui: 'Cairo', 'Tajawal', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;

  --sidebar-w: 248px;
  --topbar-h: 64px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  max-width: 100%;
  overflow-x: hidden;
}
html { direction: rtl; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: 0; }
p { margin: 0; }
ul { list-style: none; margin: 0; padding: 0; }
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }
img { max-width: 100%; display: block; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ============================================================
   APP SHELL
   ============================================================ */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh;
  max-width: 100%;
  overflow-x: hidden;
}

.app-shell > div { min-width: 0; }

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  padding: 20px 14px;
  position: sticky;
  top: 0;
  min-height: 100vh;
  align-self: stretch;
  overflow-y: visible;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 24px;
}
.sidebar-brand .logo-mark {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--primary), var(--mint));
  flex-shrink: 0;
}
.sidebar-brand strong {
  color: #fff;
  font-size: 15px;
  letter-spacing: 0;
}
.sidebar-brand span {
  display: block;
  font-size: 11px;
  color: var(--text-faint);
}
.nav-group-label {
  font-size: 11px;
  color: var(--text-faint);
  padding: 16px 12px 6px;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.12s ease, color 0.12s ease;
}
.nav-link svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-link:hover { background: var(--sidebar-bg-hover); color: #fff; }
.nav-link.active { background: var(--sidebar-bg-active); color: var(--sidebar-text-active); }
.sidebar-footer {
  margin-top: auto;
  padding: 12px;
  font-size: 11px;
  color: var(--text-faint);
  border-top: 1px solid rgba(255,255,255,0.08);
}

/* ---------- Topbar ---------- */
.topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-left { display: flex; align-items: center; gap: 14px; }
.hamburger {
  display: none;
  background: none;
  border: none;
  padding: 6px;
  border-radius: var(--radius-sm);
}
.hamburger:hover { background: var(--surface-2); }
.hamburger svg { width: 22px; height: 22px; }
.page-title { font-size: 17px; }
.page-subtitle { font-size: 12.5px; color: var(--text-muted); margin-top: 2px; }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--mint));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 12.5px; font-weight: 600;
}

/* ---------- Content ---------- */
.content {
  padding: 24px;
  max-width: 1320px;
  width: 100%;
  min-width: 0;
  overflow-x: hidden;
  box-sizing: border-box;
}
.page { display: none; }
.page.active { display: block; animation: fadeIn 0.18s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.section-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.section-head h1 { font-size: 20px; }
.section-head .hint { font-size: 12.5px; color: var(--text-muted); margin-top: 3px; }
.section-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

/* ============================================================
   BUTTONS
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 600;
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
  white-space: nowrap;
}
.btn svg { width: 15px; height: 15px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-danger:hover { background: #f6d3d8; }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-icon { padding: 7px; border-radius: var(--radius-sm); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ============================================================
   CARDS / STATS
   ============================================================ */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  overflow: hidden;
  box-sizing: border-box;
}
.stat-value { overflow-wrap: anywhere; }
.stat-card .stat-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.stat-icon {
  width: 34px; height: 34px;
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
}
.stat-icon svg { width: 17px; height: 17px; }
.stat-value { font-size: 24px; font-weight: 700; letter-spacing: 0; }
.stat-label { font-size: 12.5px; color: var(--text-muted); }

.chart-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  margin-bottom: 24px;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px 20px;
  min-width: 0;
  overflow: hidden;
  box-sizing: border-box;
}
.panel canvas { max-width: 100%; display: block; }
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.panel-head h3 { font-size: 14px; }
.legend-dot { display:inline-block; width:8px; height:8px; border-radius:50%; margin-inline-end:6px; }

/* ============================================================
   TOOLBAR / SEARCH / FILTER
   ============================================================ */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.search-box {
  position: relative;
  flex: 1;
  min-width: 200px;
  max-width: 320px;
}
.search-box svg {
  position: absolute;
  inset-inline-start: 11px;
  top: 50%;
  transform: translateY(-50%);
  width: 15px; height: 15px;
  color: var(--text-faint);
}
.search-box input {
  width: 100%;
  padding: 9px 12px;
  padding-inline-start: 34px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13px;
}
.search-box input:focus { border-color: var(--primary); }
.select-filter {
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13px;
  color: var(--text);
}
.toolbar .spacer { flex: 1; }

/* ============================================================
   TABLE
   ============================================================ */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: auto;
}
table { width: 100%; border-collapse: collapse; min-width: 720px; }
thead th {
  text-align: start;
  font-size: 11.5px;
  color: var(--text-muted);
  font-weight: 600;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  white-space: nowrap;
}
tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.cell-main { display: flex; align-items: center; gap: 10px; }
.cell-thumb {
  width: 38px; height: 38px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface-2);
  border: 1px solid var(--border);
  flex-shrink: 0;
}
.cell-title { font-weight: 600; }
.cell-sub { font-size: 12px; color: var(--text-muted); }
.mono { font-family: var(--font-mono); font-size: 12px; color: var(--text-muted); direction: ltr; unicode-bidi: isolate; display: inline-block; }
.row-actions { display: flex; align-items: center; gap: 4px; justify-content: flex-end; }
.empty-row td { text-align: center; padding: 40px 16px; color: var(--text-faint); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warning { background: var(--warning-soft); color: var(--warning); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-muted { background: var(--surface-2); color: var(--text-muted); }

/* ============================================================
   PAGINATION (light)
   ============================================================ */
.table-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  font-size: 12px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

/* ============================================================
   MODAL
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 16, 33, 0.5);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 100;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 560px;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.16s ease;
}
.modal.modal-wide { max-width: 720px; }
@keyframes modalIn { from { opacity: 0; transform: translateY(10px) scale(0.98); } to { opacity: 1; transform: none; } }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 15px; }
.modal-close { background: none; border: none; padding: 6px; border-radius: var(--radius-sm); color: var(--text-muted); }
.modal-close:hover { background: var(--surface-2); }
.modal-close svg { width: 18px; height: 18px; }
.modal-body { padding: 20px 22px; max-height: 62vh; overflow-y: auto; }
.modal-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 22px;
  border-top: 1px solid var(--border);
}

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
@media (max-width: 560px) { .form-grid { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12.5px; font-weight: 600; color: var(--text); }
.field .optional { color: var(--text-faint); font-weight: 400; }
.field input, .field select, .field textarea {
  padding: 9px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--primary); }
.field textarea { resize: vertical; min-height: 80px; }
.field-hint { font-size: 11.5px; color: var(--text-faint); }
.image-upload {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 14px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.image-upload img { width: 52px; height: 52px; border-radius: 8px; object-fit: cover; }
.image-upload input[type=file] { font-size: 12px; }
.chip-input { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--primary-soft); color: var(--primary-dark);
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
}
.chip button { background: none; border: none; color: var(--primary-dark); font-size: 13px; line-height: 1; padding: 0; }

.additional-images-list { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 10px; }
.additional-image-item { position: relative; width: 64px; height: 64px; }
.additional-image-item img {
  width: 100%; height: 100%; border-radius: var(--radius-sm); object-fit: cover;
  border: 1px solid var(--border);
}
.additional-image-remove {
  position: absolute; top: -6px; left: -6px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--danger); color: #fff; border: 2px solid var(--surface);
  font-size: 13px; line-height: 1; display: flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0;
}
.additional-image-remove:hover { background: #b92e3f; }

/* ============================================================
   TOASTS
   ============================================================ */
.toast-stack {
  position: fixed;
  top: 18px;
  inset-inline-end: 18px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: min(340px, 90vw);
}
.toast {
  background: var(--text);
  color: #fff;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  box-shadow: var(--shadow-md);
  font-size: 13px;
  animation: toastIn 0.18s ease;
}
.toast.success { background: #0F3D28; }
.toast.error { background: #4A1620; }
.toast svg { width: 16px; height: 16px; margin-top: 1px; flex-shrink: 0; }
@keyframes toastIn { from { opacity: 0; transform: translateX(-12px); } to { opacity: 1; transform: none; } }

/* ============================================================
   CONFIRM DIALOG
   ============================================================ */
.confirm-icon {
  width: 44px; height: 44px; border-radius: 12px;
  background: var(--danger-soft); color: var(--danger);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 12px;
}
.confirm-icon svg { width: 22px; height: 22px; }

/* ============================================================
   SETTINGS PAGE
   ============================================================ */
.settings-card { max-width: 560px; }
.logo-preview {
  width: 72px; height: 72px; border-radius: 14px; object-fit: cover;
  border: 1px solid var(--border);
}

/* ============================================================
   MOBILE
   ============================================================ */
.sidebar-scrim {
  display: none;
  position: fixed; inset: 0; background: rgba(15,16,33,0.45); z-index: 39;
}
@media (max-width: 960px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset-inline-start: 0;
    top: 0;
    width: var(--sidebar-w);
    height: 100vh;
    min-height: 0;
    overflow-y: auto;
    transform: translateX(104%);
    transition: transform 0.2s ease;
    z-index: 40;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-scrim.open { display: block; }
  .hamburger { display: inline-flex; }
  .content { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .stat-grid { grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr)); }
  .topbar { padding: 0 14px; }
}

/* ---------------- حالة التحميل (Supabase) ---------------- */
.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--primary);
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------------- شاشة تسجيل دخول الإدارة ---------------- */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-logo {
  font-family: var(--font-ui);
  font-weight: 800;
  font-size: 20px;
  color: var(--primary);
  text-align: center;
  letter-spacing: 0.5px;
}
.auth-card h1 {
  font-size: 18px;
  text-align: center;
  margin: 0;
}
.auth-card .hint {
  text-align: center;
  margin: 0 0 6px;
}
.auth-error {
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 13px;
  text-align: center;
}
