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

:root {
  --primary: #0f172a;
  --primary-light: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #f59e0b;
  --danger: #ef4444;
  --success: #10b981;
  --warning: #f59e0b;
  --bg-body: #f1f5f9;
  --bg-card: #ffffff;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 16px;
  --shadow-lg: 0 10px 30px -5px rgba(0,0,0,0.1);
  --transition: 0.3s ease;
  --sidebar-w: 260px;
}

[data-theme="dark"] {
  --primary: #e2e8f0;
  --primary-light: #60a5fa;
  --bg-body: #0b1120;
  --bg-card: #1e293b;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --border: #334155;
  --shadow-lg: 0 10px 30px -5px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Tajawal', sans-serif;
  background-color: var(--bg-body);
  color: var(--text-main);
  line-height: 1.6;
  transition: background 0.3s, color 0.3s;
}
a { text-decoration: none; color: inherit; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
table { width: 100%; border-collapse: collapse; }

/* =========================================
   تنسيقات عامة (UI Elements)
   ========================================= */
.card { background: var(--bg-card); border-radius: var(--radius); padding: 24px; margin-bottom: 24px; box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.card h2 { font-size: 1.2rem; margin-bottom: 20px; color: var(--text-main); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 20px; border-radius: 8px; font-weight: 600; background: var(--primary-light); color: #fff; transition: 0.2s; border: none; font-size: 0.95rem; }
.btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
.btn.full { width: 100%; margin-top: 10px; }
.btn.sm { padding: 6px 12px; font-size: 0.85rem; }
.btn.ghost { background: transparent; color: var(--text-main); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--bg-body); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.danger:hover { background: #dc2626; }
.btn.ok { background: var(--success); color: #fff; }
.iconbtn { width: 40px; height: 40px; border-radius: 50%; background: var(--bg-body); color: var(--text-main); display: flex; align-items: center; justify-content: center; }

input, select, textarea { width: 100%; padding: 12px 16px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-body); color: var(--text-main); font-family: inherit; font-size: 0.95rem; transition: border-color 0.2s; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary-light); box-shadow: 0 0 0 3px rgba(59,130,246,0.2); }
label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 8px; color: var(--text-main); }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 20px; }

.tw { overflow-x: auto; }
th, td { padding: 16px; text-align: right; border-bottom: 1px solid var(--border); }
th { background: var(--bg-body); color: var(--text-muted); font-size: 0.85rem; font-weight: 600; }
tr:hover { background: var(--bg-body); }
.badge { display: inline-block; padding: 4px 10px; border-radius: 99px; font-size: 0.8rem; font-weight: 500; }
.st-lead { background: #e0f2fe; color: #0369a1; }
.st-negotiating { background: #fef3c7; color: #b45309; }
.st-closed_won { background: #fce7f3; color: #be185d; }
.st-installed { background: #d1fae5; color: #047857; }

.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; gap: 10px; flex-wrap: wrap; }
.search { position: relative; flex: 1; min-width: 200px; }
.search input { padding-right: 40px; }
.search svg { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--text-muted); }

dialog { border: none; border-radius: var(--radius); padding: 0; background: var(--bg-card); color: var(--text-main); width: 90%; max-width: 500px; margin: auto; box-shadow: var(--shadow-lg); }
dialog::backdrop { background: rgba(0,0,0,0.6); backdrop-filter: blur(4px); }
.dh { padding: 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.db { padding: 20px; max-height: 70vh; overflow-y: auto; }
.df { padding: 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }
.fgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
.fgrid .w { grid-column: 1 / -1; }

/* =========================================
   لوحة التحكم (Admin)
   ========================================= */
body:not(.public) { display: flex; min-height: 100vh; }
.sidebar { width: var(--sidebar-w); background: var(--primary); color: #fff; display: flex; flex-direction: column; position: fixed; top: 0; right: 0; bottom: 0; z-index: 1000; transition: transform 0.3s; overflow-y: auto; }
.brand { padding: 24px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid rgba(255,255,255,.1); }
.brand-mark { width: 40px; height: 40px; background: var(--accent); border-radius: 8px; display: flex; align-items: center; justify-content: center; font-weight: bold; color: #fff; }
.nav { padding: 20px 10px; flex: 1; }
.nav a { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-radius: 8px; margin-bottom: 4px; color: #94a3b8; transition: 0.2s; font-weight: 500; }
.nav a:hover, .nav a.on { background: rgba(255,255,255,.1); color: #fff; }
.side-foot { padding: 20px; border-top: 1px solid rgba(255,255,255,.1); }
.side-foot .btn { background: rgba(255,255,255,0.1); color: #fff; width: 100%; border: 1px solid rgba(255,255,255,0.2); }
.side-foot .btn:hover { background: rgba(255,255,255,0.2); }

.main { flex: 1; margin-right: var(--sidebar-w); padding: 24px; transition: margin 0.3s; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; background: var(--bg-card); padding: 16px 24px; border-radius: var(--radius); box-shadow: var(--shadow-lg); border: 1px solid var(--border); }
.top-r { display: flex; align-items: center; gap: 16px; }
.menu-btn { display: none; font-size: 1.5rem; color: var(--text-main); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat { background: var(--bg-card); padding: 20px; border-radius: var(--radius); box-shadow: var(--shadow-lg); border-right: 4px solid var(--primary-light); display: flex; flex-direction: column; justify-content: center; }
.stat.hot { border-right-color: var(--accent); }
.stat .n { font-size: 2rem; font-weight: 700; }
.stat .l { color: var(--text-muted); font-size: 0.9rem; }

.overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 999; display: none; }
.overlay.show { display: block; }

/* =========================================
   صفحة الهبوط (BotUI)
   ========================================= */
body.public { display: block; background: var(--bg-body); }
.top-bar { background: var(--primary); color: #fff; text-align: center; padding: 12px; font-size: 0.95rem; position: sticky; top: 0; z-index: 1000; }
.theme-toggle { position: fixed; top: 60px; left: 20px; z-index: 1000; background: var(--bg-card); color: var(--text-main); border: 1px solid var(--border); border-radius: 50%; width: 45px; height: 45px; box-shadow: var(--shadow-lg); display: flex; align-items: center; justify-content: center; }

.botui-container { width: 100%; max-width: 480px; margin: 20px auto; background: var(--bg-card); border-radius: 24px; box-shadow: var(--shadow-lg); overflow: hidden; display: flex; flex-direction: column; height: 85vh; border: 1px solid var(--border); }
.botui-header { background: var(--primary); color: #fff; padding: 20px; display: flex; align-items: center; gap: 12px; }
.botui-header .avatar { width: 45px; height: 45px; background: var(--accent); border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 1.2rem; }
.botui-header .info { display: flex; flex-direction: column; }
.botui-header .status { font-size: 0.8rem; opacity: 0.8; display: flex; align-items: center; gap: 4px; }
.botui-messages { flex: 1; padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; background: var(--bg-body); scroll-behavior: smooth; }
.botui-message { max-width: 80%; padding: 12px 16px; border-radius: 16px; font-size: 0.95rem; line-height: 1.5; animation: fadeInUp 0.3s ease; }
.botui-message.bot { background: var(--bg-card); border: 1px solid var(--border); align-self: flex-start; border-bottom-right-radius: 4px; }
.botui-message.user { background: var(--primary-light); color: #fff; align-self: flex-end; border-bottom-left-radius: 4px; }
.botui-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.botui-action-btn { background: var(--bg-card); border: 1px solid var(--primary-light); color: var(--primary-light); padding: 8px 16px; border-radius: 20px; font-size: 0.9rem; cursor: pointer; transition: all 0.2s; }
.botui-action-btn:hover { background: var(--primary-light); color: #fff; }
.botui-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-top: 10px; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); }
.botui-card h4 { margin: 0 0 8px; color: var(--text-main); font-size: 1.1rem; }
.botui-card .price { color: var(--accent); font-weight: 800; font-size: 1.2rem; margin-bottom: 8px; }
.botui-card ul { padding-right: 20px; margin: 0 0 12px; font-size: 0.9rem; color: var(--text-muted); }
.botui-card button { width: 100%; padding: 10px; background: var(--primary-light); color: #fff; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; }
.botui-form { background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin-top: 10px; }
.botui-form input { width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-body); color: var(--text-main); }
.botui-form button { width: 100%; padding: 10px; background: var(--success); color: #fff; border: none; border-radius: 8px; cursor: pointer; font-weight: 600; }
.botui-input-area { padding: 16px; background: var(--bg-card); border-top: 1px solid var(--border); display: flex; gap: 10px; }
.botui-input-area input { flex: 1; padding: 12px 16px; border: 1px solid var(--border); border-radius: 30px; outline: none; background: var(--bg-body); color: var(--text-main); font-family: inherit; }
.botui-input-area button { width: 45px; height: 45px; border-radius: 50%; background: var(--primary-light); color: #fff; border: none; display: flex; align-items: center; justify-content: center; cursor: pointer; }

@keyframes fadeInUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* =========================================
   التجاوب مع الجوال (Responsive)
   ========================================= */
@media (max-width: 768px) {
  /* لوحة التحكم */
  .sidebar { transform: translateX(100%); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-right: 0; padding: 16px; }
  .menu-btn { display: block; }
  .topbar { padding: 12px 16px; margin-bottom: 16px; }
  .fgrid { grid-template-columns: 1fr; }

  /* الجداول تتحول إلى بطاقات */
  table, thead, tbody, th, td, tr { display: block; }
  thead { display: none; }
  tr { margin-bottom: 16px; background: var(--bg-card); border: 1px solid var(--border); border-radius: 8px; padding: 12px; box-shadow: var(--shadow-lg); }
  td { display: flex; justify-content: space-between; border-bottom: 1px dashed var(--border); padding: 8px 0; }
  td::before { content: attr(data-l); font-weight: 600; color: var(--text-muted); font-size: 0.9rem; }
  .cb { display: none; } /* إخفاء مربع الاختيار في الجوال */

  /* صفحة الهبوط */
  .botui-container { height: 90vh; margin: 10px; }
}

/* =========================================
   تنسيق قائمة الإشعارات (Notifications Dropdown)
   ========================================= */
.bell {
    position: relative;
    display: inline-block;
}

.bell summary {
    list-style: none;
    cursor: pointer;
}

.bell summary::-webkit-details-marker {
    display: none;
}

.bell .drop {
    position: absolute;
    top: 50px;
    left: 0; /* تفتح من اليسار لتتناسب مع اتجاه RTL */
    width: 320px;
    max-width: 90vw;
    background: var(--bg-card);
    border-radius: 12px;
    box-shadow: 0 10px 30px -5px rgba(0,0,0,0.2);
    border: 1px solid var(--border);
    z-index: 1000;
    max-height: 400px;
    overflow-y: auto;
    text-align: right;
}

.bell .drop-h {
    padding: 16px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 700;
    color: var(--text-main);
    background: var(--bg-card);
    position: sticky;
    top: 0;
}

.bell .nitem {
    padding: 16px;
    border-bottom: 1px solid var(--border);
    transition: background 0.2s;
}

.bell .nitem:hover {
    background: var(--bg-body);
}

.bell .nitem.new {
    background: #f0f9ff; /* لون خلفية للإشعارات الجديدة */
}

.bell .nitem b {
    display: block;
    margin-bottom: 4px;
    color: var(--text-main);
    font-size: 0.95rem;
}

.bell .nitem small {
    display: block;
    color: var(--text-muted);
    font-size: 0.85rem;
    line-height: 1.4;
    margin-bottom: 8px;
}

.bell .nitem .row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.bell .nitem .btn.sm {
    padding: 4px 10px;
    font-size: 0.8rem;
}

/* النقطة الحمراء على الجرس */
.bell .dot {
    position: absolute;
    top: -2px;
    right: -2px;
    background: var(--danger);
    color: #fff;
    font-size: 0.7rem;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

/* إخفاء القائمة عند النقر خارجها */
.bell[open] .drop {
    display: block;
}