@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;500;600;700;800&display=swap');

:root {
  --navy: #2C3479;
  --navy-dark: #212868;
  --navy-light: #3a44a0;
  --red: #EE3435;
  --red-dark: #c82828;
  --black: #1D1D21;
  --bg: #EAEAF0;
  --card: #ffffff;
  --danger: #EE3435;
  --danger-bg: #fff0f0;
  --warn: #b45309;
  --text: #1D1D21;
  --muted: #6b7280;
  --border: #d8d9e8;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Cairo', 'IBM Plex Sans Arabic', sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }

/* ===== شاشة الدخول ===== */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--navy);
  position: relative;
  overflow: hidden;
}
.login-screen::before {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    135deg,
    transparent,
    transparent 60px,
    rgba(255,255,255,.03) 60px,
    rgba(255,255,255,.03) 61px
  );
}
.login-card {
  background: var(--card);
  border-radius: 16px;
  padding: 44px 40px;
  width: 380px;
  box-shadow: 0 24px 64px rgba(0,0,0,.35);
  position: relative;
  z-index: 1;
}
.login-brand {
  text-align: center;
  margin-bottom: 28px;
}
.login-logo {
  height: 72px;
  object-fit: contain;
  margin-bottom: 12px;
}
.login-logo-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 64px; height: 64px;
  background: var(--navy);
  color: white;
  border-radius: 14px;
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 12px;
}
.login-brand h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--navy);
  margin: 0 0 4px;
}
.login-brand p { color: var(--muted); font-size: 13px; margin: 0; }
.login-divider {
  height: 3px;
  background: var(--red);
  border-radius: 2px;
  width: 48px;
  margin: 12px auto 0;
}

#login-form label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin: 16px 0 6px;
  color: var(--navy);
}
#login-form input {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: #f8f8fb;
  transition: border-color .2s;
}
#login-form input:focus { outline: none; border-color: var(--navy); background: #fff; }
#login-form button {
  width: 100%;
  margin-top: 22px;
  padding: 13px;
  background: var(--red);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  transition: background .2s;
}
#login-form button:hover { background: var(--red-dark); }
.error-msg { color: var(--red); font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ===== التطبيق ===== */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: 248px;
  background: var(--navy);
  color: white;
  display: flex;
  flex-direction: column;
  padding: 0;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-header {
  padding: 20px 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-logo-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.sidebar-logo {
  height: 38px;
  object-fit: contain;
  filter: brightness(0) invert(1);
}
.sidebar-logo-fallback {
  width: 38px; height: 38px;
  background: var(--red);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 800;
  color: white;
  flex-shrink: 0;
}
.sidebar-brand-name {
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  line-height: 1.3;
}
.sidebar-brand-sub {
  font-size: 11px;
  color: rgba(255,255,255,.5);
}
.user-card {
  background: rgba(255,255,255,.07);
  border-radius: 8px;
  padding: 10px 12px;
}
.user-name { font-size: 14px; font-weight: 600; color: #fff; }
.user-role { font-size: 11px; color: rgba(255,255,255,.55); margin-top: 2px; }

nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  padding: 14px 12px;
}
.nav-btn {
  text-align: right;
  background: transparent;
  border: none;
  color: rgba(255,255,255,.7);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13.5px;
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
  transition: background .15s, color .15s;
  display: flex;
  align-items: center;
  gap: 8px;
}
.nav-btn:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav-btn.active { background: var(--red); color: white; font-weight: 600; }

.nav-section-label {
  font-size: 10px;
  font-weight: 700;
  color: rgba(255,255,255,.3);
  text-transform: uppercase;
  letter-spacing: .08em;
  padding: 10px 12px 4px;
  margin-top: 4px;
}

.sidebar-footer {
  padding: 14px 12px;
  border-top: 1px solid rgba(255,255,255,.08);
}
.logout-btn {
  width: 100%;
  background: transparent;
  border: 1px solid rgba(255,255,255,.15);
  color: rgba(255,255,255,.65);
  padding: 9px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  transition: all .2s;
}
.logout-btn:hover { border-color: var(--red); color: var(--red); }

/* ===== المحتوى ===== */
.content { flex: 1; padding: 32px 40px; max-width: 1020px; }

.page-header {
  display: flex;
  align-items: center;
  gap: 0;
  margin-bottom: 24px;
}
.page-header-bar {
  width: 4px;
  height: 28px;
  background: var(--red);
  border-radius: 2px;
  margin-left: 12px;
}
.content h2 {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--navy);
}
.hint { color: var(--muted); font-size: 13px; }

/* بحث */
.search-row { display: flex; gap: 8px; margin-bottom: 20px; }
.search-row input[type="text"] { flex: 1; }
.search-row input, .search-row button, #ai-input, #ai-send-btn {
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: #fff;
}
.search-row input:focus, #ai-input:focus { outline: none; border-color: var(--navy); }
.search-row button, #ai-send-btn, .secondary-btn {
  background: var(--navy);
  color: white;
  border: none;
  cursor: pointer;
  font-weight: 600;
  font-family: inherit;
  transition: background .2s;
}
.search-row button:hover, #ai-send-btn:hover { background: var(--navy-dark); }
.search-row input[type="number"] { width: 90px; }
.search-row select { width: 170px; background: white; }

.category-badge {
  display: inline-block;
  background: #e8eaf8;
  color: var(--navy);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 9px;
  border-radius: 999px;
  vertical-align: middle;
}

/* كروت المنتجات */
.results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.product-card {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: 0 2px 8px rgba(44,52,121,.06);
  transition: box-shadow .2s;
}
.product-card:hover { box-shadow: 0 4px 16px rgba(44,52,121,.12); }
.product-card h4 { margin: 0 0 4px; font-size: 14.5px; font-weight: 700; color: var(--navy); }
.product-card .price-line { display: flex; justify-content: space-between; font-size: 13px; padding: 4px 0; border-bottom: 1px solid #f0f0f6; }
.product-card .price-line:last-of-type { border-bottom: none; }
.product-card .price-line .label { color: var(--muted); }
.product-card .price-line .val { font-weight: 600; color: var(--navy); }
.stock-alert { margin-top: 10px; background: #fffbeb; color: var(--warn); font-size: 12px; padding: 8px 10px; border-radius: 6px; border-right: 3px solid var(--warn); }
.margin-line { color: var(--navy); font-weight: 700; }

/* AI */
.ai-chat {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  min-height: 320px;
  max-height: 440px;
  overflow-y: auto;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(44,52,121,.06);
}
.chat-msg { margin-bottom: 12px; max-width: 80%; padding: 10px 14px; border-radius: 10px; font-size: 14px; line-height: 1.7; }
.chat-msg.user { background: var(--navy); color: white; margin-left: auto; border-bottom-left-radius: 2px; }
.chat-msg.bot { background: #f0f1f8; color: var(--black); margin-right: auto; border-bottom-right-radius: 2px; }
.ai-input-row { display: flex; gap: 8px; }
.ai-input-row input { flex: 1; }

/* قوائم */
.list-box { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.list-item {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  box-shadow: 0 1px 4px rgba(44,52,121,.05);
  transition: box-shadow .2s;
}
.list-item:hover { box-shadow: 0 3px 10px rgba(44,52,121,.1); }
.list-item.danger { border-color: var(--red); background: var(--danger-bg); }
.list-item .meta { font-size: 12px; color: var(--muted); margin-top: 3px; }
.list-item .actions button { margin-inline-start: 6px; padding: 7px 14px; border-radius: 6px; border: none; cursor: pointer; font-family: inherit; font-size: 13px; font-weight: 600; transition: opacity .15s; }
.list-item .actions button:hover { opacity: .85; }
.btn-approve { background: var(--navy); color: white; }
.btn-reject { background: var(--red); color: white; }
.secondary-btn { margin-bottom: 10px; padding: 9px 18px; border-radius: 8px; }

/* قسم التصنيف */
.category-group { border: none; margin-bottom: 12px; }
.category-group > summary { list-style: none; }
.category-group > summary::-webkit-details-marker { display: none; }

/* إدارة المنتجات */
.add-product-box { background: var(--card); border: 1.5px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 18px; box-shadow: 0 1px 4px rgba(44,52,121,.05); }
.grid-form { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.grid-form input { padding: 10px 12px; border: 1.5px solid var(--border); border-radius: 8px; font-family: inherit; background: #f8f8fb; transition: border-color .2s; }
.grid-form input:focus { outline: none; border-color: var(--navy); background: #fff; }
.grid-form button { grid-column: 1 / -1; padding: 11px; background: var(--navy); color: white; border: none; border-radius: 8px; font-weight: 700; cursor: pointer; font-family: inherit; }
.checkbox-label { display: flex; align-items: center; gap: 6px; font-size: 13px; }

/* ربط الأعمدة */
.mapping-grid { grid-template-columns: repeat(2, 1fr); }
.mapping-label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.mapping-label input { padding: 10px 12px; border: 1.5px solid var(--border); border-radius: 8px; font-family: inherit; color: var(--text); background: #f8f8fb; }

/* رفع الملفات */
.import-box { background: var(--card); border: 1.5px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; box-shadow: 0 1px 4px rgba(44,52,121,.05); }
.import-box h3 { margin: 0 0 8px; font-size: 15px; font-weight: 700; color: var(--navy); }
.import-box input[type="file"] { display: block; margin: 10px 0; }
.import-box button { padding: 9px 20px; background: var(--navy); color: white; border: none; border-radius: 8px; cursor: pointer; font-weight: 700; font-family: inherit; transition: background .2s; }
.import-box button:hover { background: var(--navy-dark); }
.import-result { margin-top: 12px; font-size: 13.5px; }

/* تنبيهات */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 18px;
}
.section-tag {
  font-size: 11px;
  font-weight: 700;
  color: var(--red);
  text-transform: uppercase;
  letter-spacing: .06em;
}

/* ===== واجهة الشات الذكي ===== */

/* السيكشن الخاص بالشات يأخذ كل الارتفاع */
#view-ai.view {
  padding: 0 !important;
  flex: 1;
  display: flex !important;
  overflow: hidden;
  height: calc(100vh - 0px);
}
#view-ai.view.hidden { display: none !important; }

/* سايدبار المحادثات */
.chat-sidebar {
  width: 240px; flex-shrink: 0;
  background: var(--navy);
  display: flex; flex-direction: column;
  overflow: hidden;
  border-left: 1px solid rgba(255,255,255,.07);
}
.chat-sidebar-header {
  padding: 20px 16px 14px;
  border-bottom: 1px solid rgba(255,255,255,.1);
  text-align: center;
}
.chat-sidebar-logo {
  height: 44px; object-fit: contain;
  display: block; margin: 0 auto 10px;
}
.chat-sidebar-title { color: #fff; font-weight: 700; font-size: .95rem; }
.chat-sidebar-sub { color: rgba(255,255,255,.45); font-size: .72rem; margin-top: 2px; }

.new-chat-btn {
  margin: 12px 14px;
  padding: 10px;
  background: var(--red);
  color: #fff; border: none;
  border-radius: 9px;
  font-family: 'Cairo', sans-serif;
  font-size: .88rem; font-weight: 700;
  cursor: pointer;
  transition: background .15s;
  text-align: center;
}
.new-chat-btn:hover { background: var(--red-dark); }

.chat-history-label {
  padding: 4px 16px 6px;
  font-size: .68rem; color: rgba(255,255,255,.35);
  letter-spacing: .05em;
  text-transform: uppercase;
}
.chat-history-list {
  flex: 1; overflow-y: auto;
  padding: 4px 10px 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.chat-history-list::-webkit-scrollbar { width: 4px; }
.chat-history-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 4px; }

.chat-history-item {
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,.12);
  color: rgba(255,255,255,.75);
  font-size: .82rem;
  cursor: pointer;
  transition: background .15s, color .15s;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
}
.chat-history-item:hover { background: rgba(255,255,255,.08); color: #fff; }
.chat-history-item.active { background: rgba(255,255,255,.14); color: #fff; border-color: var(--red); }
.chat-item-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-delete-btn {
  flex-shrink: 0; width: 20px; height: 20px; border: none; border-radius: 50%;
  background: rgba(255,255,255,.1); color: rgba(255,255,255,.5); font-size: .65rem;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.chat-delete-btn:hover { background: #dc2626; color: #fff; }

/* منطقة المحادثة */
.chat-main {
  flex: 1; display: flex; flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}
.chat-topbar {
  padding: 16px 24px 12px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(44,52,121,.06);
}
.chat-topbar-title { font-weight: 700; font-size: 1rem; color: var(--navy); }
.chat-topbar-hint { font-size: .75rem; color: var(--muted); margin-top: 2px; }

/* منطقة الرسائل */
.ai-chat-area {
  flex: 1; overflow-y: auto;
  padding: 20px 24px;
  display: flex; flex-direction: column; gap: 16px;
}
.ai-chat-area::-webkit-scrollbar { width: 5px; }
.ai-chat-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* رسائل المستخدم */
.chat-msg.user {
  align-self: flex-end;
  background: var(--navy);
  color: #fff;
  padding: 12px 16px;
  border-radius: 16px 4px 16px 16px;
  max-width: 70%;
  font-size: .92rem;
  line-height: 1.6;
  box-shadow: 0 2px 8px rgba(44,52,121,.2);
}

/* ردود المساعد */
.chat-msg.bot {
  align-self: flex-start;
  display: flex; gap: 10px; align-items: flex-start;
  max-width: 75%;
}
.chat-bot-avatar {
  width: 34px; height: 34px; flex-shrink: 0;
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(44,52,121,.1);
}
.chat-bot-avatar img { width: 22px; height: 22px; object-fit: contain; }
.chat-bot-avatar span { font-size: .75rem; font-weight: 800; color: var(--navy); }
.chat-bot-bubble {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 4px 16px 16px 16px;
  font-size: .92rem;
  line-height: 1.7;
  box-shadow: 0 1px 4px rgba(44,52,121,.06);
  overflow-wrap: break-word;
  word-wrap: break-word;
  min-width: 0;
}

/* مؤشر الكتابة */
.chat-typing .chat-bot-bubble {
  display: flex; gap: 5px; align-items: center;
  padding: 14px 18px;
}
.typing-dot {
  width: 7px; height: 7px;
  background: var(--muted);
  border-radius: 50%;
  animation: typingBounce .9s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* حقل الإدخال */
.chat-input-wrap {
  padding: 14px 20px;
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex; gap: 10px; align-items: center;
}
.chat-input {
  flex: 1;
  padding: 11px 16px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-family: 'Cairo', sans-serif;
  font-size: .9rem;
  background: #fafafa;
  color: var(--text);
  transition: border-color .15s;
}
.chat-input:focus { outline: none; border-color: var(--navy); background: #fff; }
.chat-send-btn {
  padding: 11px 22px;
  background: var(--red);
  color: #fff; border: none;
  border-radius: 10px;
  font-family: 'Cairo', sans-serif;
  font-size: .9rem; font-weight: 700;
  cursor: pointer;
  transition: background .15s;
  white-space: nowrap;
}
.chat-send-btn:hover { background: var(--red-dark); }
.chat-send-btn:disabled { opacity: .5; cursor: not-allowed; }

/* رسالة الترحيب */
.chat-welcome {
  text-align: center;
  margin: auto;
  padding: 40px 20px;
}
.chat-welcome img { height: 60px; object-fit: contain; margin-bottom: 16px; opacity: .7; }
.chat-welcome h3 { color: var(--navy); font-size: 1.1rem; font-weight: 700; margin-bottom: 8px; }
.chat-welcome p { color: var(--muted); font-size: .85rem; line-height: 1.7; }

@media (max-width: 720px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; flex-direction: row; overflow-x: auto; padding: 0; }
  .sidebar-header { display: none; }
  nav { flex-direction: row; padding: 8px; }
  .nav-btn { font-size: 12px; padding: 8px; }
  .content { padding: 16px; }
  .grid-form { grid-template-columns: 1fr 1fr; }
}
