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

/* ==========================================================================
   CSS DESIGN TOKENS (LIGHT MODE DEFAULT & DARK MODE)
   ========================================================================== */
:root {
    --bg-body: #f8fafc;
    --bg-surface: #ffffff;
    --bg-surface-secondary: #f1f5f9;
    --bg-surface-tertiary: #e2e8f0;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border-color: #e2e8f0;
    --border-focus: #0284c7;

    --primary: #0284c7;
    --primary-hover: #0369a1;
    --primary-light: #e0f2fe;
    --primary-text: #0369a1;

    --success: #10b981;
    --success-light: #d1fae5;
    --success-text: #065f46;

    --warning: #f59e0b;
    --warning-light: #fef3c7;
    --warning-text: #92400e;

    --danger: #ef4444;
    --danger-light: #fee2e2;
    --danger-text: #991b1b;

    --purple: #8b5cf6;
    --purple-light: #ede9fe;
    --purple-text: #5b21b6;

    --orange: #f97316;
    --orange-light: #ffedd5;
    --orange-text: #9a3412;

    --teal: #14b8a6;
    --teal-light: #ccfbf1;
    --teal-text: #115e59;

    --sidebar-width: 260px;
    --sidebar-collapsed-width: 76px;
    --header-height: 64px;

    --sidebar-bg: #ffffff;
    --sidebar-text: #475569;
    --sidebar-heading: #94a3b8;
    --sidebar-hover: #f1f5f9;
    --sidebar-active-bg: #0284c7;
    --sidebar-active-text: #ffffff;
    --sidebar-border: #e2e8f0;

    --header-bg: rgba(255, 255, 255, 0.95);
    --header-border: #e2e8f0;

    --card-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.06), 0 1px 2px -1px rgba(0, 0, 0, 0.04);
    --card-shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.07), 0 4px 6px -4px rgba(0, 0, 0, 0.04);
    --modal-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.15), 0 8px 10px -6px rgba(0, 0, 0, 0.1);

    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 18px;

    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
    --bg-body: #0b0f19;
    --bg-surface: #131b2e;
    --bg-surface-secondary: #1e293b;
    --bg-surface-tertiary: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #64748b;
    --border-color: #1e293b;
    --border-focus: #38bdf8;

    --primary: #38bdf8;
    --primary-hover: #0ea5e9;
    --primary-light: rgba(56, 189, 248, 0.15);
    --primary-text: #7dd3fc;

    --success: #34d399;
    --success-light: rgba(52, 211, 153, 0.15);
    --success-text: #6ee7b7;

    --warning: #fbbf24;
    --warning-light: rgba(251, 191, 36, 0.15);
    --warning-text: #fde68a;

    --danger: #f87171;
    --danger-light: rgba(248, 113, 113, 0.15);
    --danger-text: #fca5a5;

    --purple: #a78bfa;
    --purple-light: rgba(167, 139, 250, 0.15);
    --purple-text: #c4b5fd;

    --orange: #fb923c;
    --orange-light: rgba(251, 146, 60, 0.15);
    --orange-text: #fdba74;

    --teal: #2dd4bf;
    --teal-light: rgba(45, 212, 191, 0.15);
    --teal-text: #5eead4;

    --sidebar-bg: #131b2e;
    --sidebar-text: #94a3b8;
    --sidebar-heading: #64748b;
    --sidebar-hover: #1e293b;
    --sidebar-active-bg: #0284c7;
    --sidebar-active-text: #ffffff;
    --sidebar-border: #1e293b;

    --header-bg: rgba(19, 27, 46, 0.95);
    --header-border: #1e293b;

    --card-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3), 0 2px 4px -2px rgba(0, 0, 0, 0.2);
    --card-shadow-hover: 0 10px 15px -3px rgba(0, 0, 0, 0.4);
}

/* ==========================================================================
   GLOBAL RESET & TYPOGRAPHY
   ========================================================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Tajawal', sans-serif;
    font-size: 0.925rem;
    line-height: 1.6;
    background-color: var(--bg-body);
    color: var(--text-primary);
    direction: rtl;
    text-align: right;
    overflow-x: hidden;
    transition: background-color 0.25s ease, color 0.25s ease;
}

a {
    color: var(--primary);
    text-decoration: none;
    transition: var(--transition);
}

a:hover {
    color: var(--primary-hover);
}

/* ==========================================================================
   LAYOUT STRUCTURE (RIGHT SIDEBAR + MAIN CONTENT)
   ========================================================================== */
.crm-wrapper {
    display: flex;
    min-height: 100vh;
    width: 100%;
}

/* Fixed Right Sidebar */
.crm-sidebar {
    width: var(--sidebar-width);
    height: 100vh;
    position: fixed;
    top: 0;
    right: 0;
    background-color: var(--sidebar-bg);
    border-left: 1px solid var(--sidebar-border);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    transition: var(--transition);
    overflow: hidden;
}

.crm-sidebar-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.25rem;
    border-bottom: 1px solid var(--sidebar-border);
}

.crm-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-decoration: none;
    color: var(--text-primary);
}

.crm-brand-icon {
    width: 36px;
    height: 36px;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, #0284c7 0%, #0369a1 100%);
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    font-weight: 800;
    box-shadow: 0 2px 8px rgba(2, 132, 199, 0.35);
}

.crm-brand-info h4 {
    font-size: 1.05rem;
    font-weight: 800;
    margin: 0;
    line-height: 1.2;
    color: var(--text-primary);
}

.crm-brand-info span {
    font-size: 0.72rem;
    color: var(--text-muted);
    display: block;
    font-weight: 500;
}

.sidebar-toggle-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 1.15rem;
    cursor: pointer;
    padding: 0.35rem;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.sidebar-toggle-btn:hover {
    background-color: var(--bg-surface-secondary);
    color: var(--primary);
}

/* Sidebar Navigation Menu */
.crm-sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 0.75rem;
    scrollbar-width: thin;
    scrollbar-color: var(--bg-surface-tertiary) transparent;
}

.crm-nav-group {
    margin-bottom: 1.25rem;
}

.crm-nav-heading {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--sidebar-heading);
    padding: 0.35rem 0.75rem;
    margin-bottom: 0.25rem;
}

.crm-nav-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius-md);
    color: var(--sidebar-text);
    font-weight: 500;
    font-size: 0.88rem;
    text-decoration: none;
    margin-bottom: 0.2rem;
    transition: var(--transition);
}

.crm-nav-item-content {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.crm-nav-icon {
    font-size: 1.1rem;
    width: 20px;
    text-align: center;
    color: var(--text-secondary);
    transition: var(--transition);
}

.crm-nav-item:hover {
    background-color: var(--sidebar-hover);
    color: var(--primary);
}

.crm-nav-item:hover .crm-nav-icon {
    color: var(--primary);
}

.crm-nav-item.active {
    background-color: var(--sidebar-active-bg);
    color: var(--sidebar-active-text);
    font-weight: 700;
    box-shadow: 0 4px 10px rgba(2, 132, 199, 0.3);
}

.crm-nav-item.active .crm-nav-icon {
    color: #ffffff;
}

.crm-nav-item .badge {
    font-size: 0.72rem;
    font-weight: 700;
    padding: 0.25rem 0.5rem;
    border-radius: 20px;
}

/* Sidebar Collapsed Mode */
body.sidebar-collapsed .crm-sidebar {
    width: var(--sidebar-collapsed-width);
}

body.sidebar-collapsed .crm-brand-info,
body.sidebar-collapsed .crm-nav-heading,
body.sidebar-collapsed .crm-nav-text,
body.sidebar-collapsed .crm-nav-item .badge {
    display: none;
}

body.sidebar-collapsed .crm-nav-item {
    justify-content: center;
    padding: 0.75rem 0;
}

body.sidebar-collapsed .crm-nav-item-content {
    justify-content: center;
}

body.sidebar-collapsed .crm-main-content {
    margin-right: var(--sidebar-collapsed-width);
}

/* Main Content Area */
.crm-main-content {
    flex: 1;
    margin-right: var(--sidebar-width);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    transition: var(--transition);
}

/* Top Header */
.crm-header {
    height: var(--header-height);
    background-color: var(--header-bg);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--header-border);
    position: sticky;
    top: 0;
    z-index: 900;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.75rem;
}

.crm-header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex: 1;
    max-width: 500px;
}

.global-search-trigger {
    width: 100%;
    background-color: var(--bg-surface-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.5rem 1rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    font-size: 0.875rem;
    transition: var(--transition);
}

.global-search-trigger:hover {
    border-color: var(--border-focus);
    background-color: var(--bg-surface);
    color: var(--text-primary);
}

.search-shortcut {
    font-size: 0.7rem;
    background: var(--bg-surface-tertiary);
    color: var(--text-secondary);
    padding: 0.15rem 0.45rem;
    border-radius: 4px;
    font-family: monospace;
}

.crm-header-right {
    display: flex;
    align-items: center;
    gap: 0.85rem;
}

.header-action-btn {
    background: var(--bg-surface-secondary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    width: 38px;
    height: 38px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.05rem;
    cursor: pointer;
    position: relative;
    transition: var(--transition);
}

.header-action-btn:hover {
    background-color: var(--bg-surface-tertiary);
    color: var(--primary);
}

.header-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 18px;
    height: 18px;
    background-color: var(--danger);
    color: #ffffff;
    font-size: 0.65rem;
    font-weight: 800;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--header-bg);
}

.user-profile-btn {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-md);
    transition: var(--transition);
}

.user-profile-btn:hover {
    background-color: var(--bg-surface-secondary);
}

.user-avatar-circle {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0284c7 0%, #0f172a 100%);
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.9rem;
}

.user-meta-info {
    text-align: right;
    line-height: 1.2;
}

.user-meta-name {
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--text-primary);
}

.user-meta-role {
    font-size: 0.72rem;
    color: var(--text-muted);
}

/* Page Body Container */
.crm-page-container {
    flex: 1;
    padding: 1.75rem;
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.page-title {
    font-size: 1.45rem;
    font-weight: 800;
    color: var(--text-primary);
    margin: 0;
}

.page-subtitle {
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-top: 0.2rem;
}

/* ==========================================================================
   CARDS & KPI STATS
   ========================================================================== */
.crm-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--card-shadow);
    padding: 1.25rem;
    margin-bottom: 1.5rem;
    transition: var(--transition);
}

.crm-card:hover {
    box-shadow: var(--card-shadow-hover);
}

.crm-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.crm-card-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.kpi-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem;
    border-radius: var(--radius-lg);
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    box-shadow: var(--card-shadow);
    transition: var(--transition);
}

.kpi-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--card-shadow-hover);
}

.kpi-content h6 {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.35rem;
}

.kpi-value {
    font-size: 1.65rem;
    font-weight: 900;
    color: var(--text-primary);
    line-height: 1.2;
}

.kpi-change {
    font-size: 0.78rem;
    font-weight: 600;
    margin-top: 0.35rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.kpi-change.positive {
    color: var(--success);
}

.kpi-change.negative {
    color: var(--danger);
}

.kpi-icon-wrap {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
}

.kpi-icon-blue { background-color: var(--primary-light); color: var(--primary); }
.kpi-icon-green { background-color: var(--success-light); color: var(--success); }
.kpi-icon-amber { background-color: var(--warning-light); color: var(--warning); }
.kpi-icon-purple { background-color: var(--purple-light); color: var(--purple); }

/* ==========================================================================
   BADGES & PILLS
   ========================================================================== */
.crm-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.76rem;
    font-weight: 700;
    padding: 0.3rem 0.65rem;
    border-radius: 20px;
}

.badge-soft-primary   { background-color: var(--primary-light); color: var(--primary-text); }
.badge-soft-success   { background-color: var(--success-light); color: var(--success-text); }
.badge-soft-warning   { background-color: var(--warning-light); color: var(--warning-text); }
.badge-soft-danger    { background-color: var(--danger-light); color: var(--danger-text); }
.badge-soft-purple    { background-color: var(--purple-light); color: var(--purple-text); }
.badge-soft-orange    { background-color: var(--orange-light); color: var(--orange-text); }
.badge-soft-teal      { background-color: var(--teal-light); color: var(--teal-text); }
.badge-soft-secondary { background-color: var(--bg-surface-secondary); color: var(--text-secondary); }

.crm-badge-score {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.25rem 0.6rem;
    border-radius: 6px;
    font-size: 0.76rem;
    font-weight: 800;
}

.badge-score-hot  { background-color: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
.badge-score-warm { background-color: #fef3c7; color: #92400e; border: 1px solid #fde68a; }
.badge-score-cold { background-color: #e0f2fe; color: #0369a1; border: 1px solid #bae6fd; }

/* ==========================================================================
   TABLES & DATAGRID
   ========================================================================== */
.crm-table-container {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--card-shadow);
    overflow: hidden;
}

.crm-table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
    text-align: right;
}

.crm-table th {
    background-color: var(--bg-surface-secondary);
    color: var(--text-secondary);
    font-weight: 700;
    font-size: 0.8rem;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.crm-table td {
    padding: 0.95rem 1rem;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-primary);
    font-size: 0.88rem;
    vertical-align: middle;
}

.crm-table tbody tr {
    transition: var(--transition);
}

.crm-table tbody tr:hover {
    background-color: var(--bg-surface-secondary);
}

/* ==========================================================================
   BUTTONS & FORMS
   ========================================================================== */
.btn-crm-primary {
    background: linear-gradient(135deg, #0284c7 0%, #0369a1 100%);
    color: #ffffff;
    border: none;
    padding: 0.55rem 1.25rem;
    border-radius: var(--radius-md);
    font-weight: 700;
    font-size: 0.88rem;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(2, 132, 199, 0.3);
    transition: var(--transition);
}

.btn-crm-primary:hover {
    background: linear-gradient(135deg, #0369a1 0%, #075985 100%);
    color: #ffffff;
    transform: translateY(-1px);
    box-shadow: 0 4px 10px rgba(2, 132, 199, 0.4);
}

.btn-crm-secondary {
    background-color: var(--bg-surface-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 0.55rem 1.15rem;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 0.88rem;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    transition: var(--transition);
}

.btn-crm-secondary:hover {
    background-color: var(--bg-surface-tertiary);
    color: var(--text-primary);
}

.form-control-crm, .form-select-crm {
    width: 100%;
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.55rem 0.85rem;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.88rem;
    transition: var(--transition);
}

.form-control-crm:focus, .form-select-crm:focus {
    outline: none;
    border-color: var(--border-focus);
    box-shadow: 0 0 0 3px var(--primary-light);
}

/* ==========================================================================
   KANBAN BOARD (OPPORTUNITIES PIPELINE)
   ========================================================================== */
.kanban-board-container {
    display: flex;
    gap: 1rem;
    overflow-x: auto;
    padding-bottom: 1.5rem;
    scrollbar-width: thin;
}

.kanban-column {
    min-width: 300px;
    max-width: 320px;
    background-color: var(--bg-surface-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    display: flex;
    flex-direction: column;
    height: calc(100vh - 200px);
}

.kanban-header {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.kanban-header-title {
    font-size: 0.9rem;
    font-weight: 800;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.kanban-cards-wrapper {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.kanban-card {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.95rem;
    box-shadow: var(--card-shadow);
    cursor: grab;
    transition: var(--transition);
}

.kanban-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--card-shadow-hover);
    border-color: var(--border-focus);
}

/* ==========================================================================
   TIMELINE (LEAD & CUSTOMER 360)
   ========================================================================== */
.activity-timeline {
    position: relative;
    padding-right: 2rem;
}

.activity-timeline::before {
    content: '';
    position: absolute;
    right: 8px;
    top: 5px;
    bottom: 5px;
    width: 2px;
    background-color: var(--border-color);
}

.timeline-item {
    position: relative;
    margin-bottom: 1.5rem;
}

.timeline-bullet {
    position: absolute;
    right: -2rem;
    top: 4px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background-color: var(--bg-surface);
    border: 3px solid var(--primary);
    z-index: 2;
}

.timeline-box {
    background-color: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.85rem 1rem;
    box-shadow: var(--card-shadow);
}

/* ==========================================================================
   RESPONSIVE QUERIES
   ========================================================================== */
@media (max-width: 1024px) {
    .crm-sidebar {
        width: var(--sidebar-collapsed-width);
    }
    .crm-brand-info,
    .crm-nav-heading,
    .crm-nav-text,
    .crm-nav-item .badge {
        display: none;
    }
    .crm-nav-item {
        justify-content: center;
        padding: 0.75rem 0;
    }
    .crm-main-content {
        margin-right: var(--sidebar-collapsed-width);
    }
}

@media (max-width: 768px) {
    .crm-sidebar {
        right: -100%;
    }
    body.sidebar-open .crm-sidebar {
        right: 0;
        width: var(--sidebar-width);
    }
    body.sidebar-open .crm-brand-info,
    body.sidebar-open .crm-nav-heading,
    body.sidebar-open .crm-nav-text {
        display: block;
    }
    .crm-main-content {
        margin-right: 0 !important;
    }
    .crm-page-container {
        padding: 1rem;
    }
}
