:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --primary-subtle: #eef2ff;
    --secondary: #64748b;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --info: #06b6d4;
    --dark: #0f172a;
    --card-bg: #ffffff;
    --body-bg: #f8fafc;
    --border-color: #e2e8f0;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.08), 0 2px 4px -2px rgba(0,0,0,0.04);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.04);
}

body {
    font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--body-bg);
    color: var(--text-main);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Sidebar & Navbar */
.sidebar {
    width: 260px;
    background: #0f172a;
    color: #cbd5e1;
    min-height: 100vh;
    transition: all 0.3s ease;
}

.sidebar .nav-link {
    color: #cbd5e1;
    padding: 0.75rem 1rem;
    border-radius: var(--radius-sm);
    margin-bottom: 0.25rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    transition: all 0.2s ease;
}

.sidebar .nav-link:hover, .sidebar .nav-link.active {
    color: #ffffff;
    background: rgba(255, 255, 255, 0.12);
}

.sidebar .nav-link.active {
    background: var(--primary);
    color: #ffffff;
    font-weight: 600;
}

.sidebar-heading {
    font-size: 0.72rem;
    letter-spacing: 0.08em;
    color: #94a3b8 !important;
    font-weight: 700;
    text-transform: uppercase;
}

.sidebar .user-role {
    font-size: 0.75rem;
    color: #93c5fd !important;
    font-weight: 500;
}

.sidebar hr {
    border-color: rgba(255, 255, 255, 0.12);
    opacity: 1;
}

.sidebar-logout-btn {
    color: #ef4444 !important;
    padding: 6px 8px;
    border-radius: 6px;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    font-size: 1rem;
}

.sidebar-logout-btn:hover {
    color: #ff6b6b !important;
    background: rgba(239, 68, 68, 0.15);
    transform: scale(1.1);
}

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

/* Header */
.top-navbar {
    background: #ffffff;
    border-bottom: 1px solid var(--border-color);
    padding: 0.85rem 1.5rem;
}

/* Cards */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

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

.card-header {
    background: transparent;
    border-bottom: 1px solid var(--border-color);
    padding: 1rem 1.25rem;
    font-weight: 600;
}

/* Stat Cards */
.stat-card {
    border-radius: var(--radius-md);
    padding: 1.25rem;
    background: #ffffff;
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
}

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
}

/* Badges */
.badge-custom {
    padding: 0.35rem 0.65rem;
    border-radius: 20px;
    font-weight: 500;
    font-size: 0.75rem;
}

/* Variable badges */
.variable-badge {
    cursor: pointer;
    background: #eef2ff;
    color: #4f46e5;
    border: 1px solid #c7d2fe;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    font-size: 0.8rem;
    font-family: monospace;
    display: inline-block;
    margin: 2px;
    transition: all 0.15s ease;
}

.variable-badge:hover {
    background: #4f46e5;
    color: #ffffff;
}

/* Custom Buttons */
.btn-primary {
    background-color: var(--primary);
    border-color: var(--primary);
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    border-color: var(--primary-hover);
}

/* Switch styling */
.form-switch .form-check-input {
    width: 2.5em;
    height: 1.25em;
    cursor: pointer;
}

.form-switch .form-check-input:checked {
    background-color: var(--success);
    border-color: var(--success);
}

/* Follow-up Timeline */
.timeline-step {
    position: relative;
    padding-left: 2.5rem;
    margin-bottom: 1.5rem;
}

.timeline-step::before {
    content: '';
    position: absolute;
    left: 1rem;
    top: 2rem;
    bottom: -1rem;
    width: 2px;
    background: var(--border-color);
}

.timeline-step:last-child::before {
    display: none;
}

.timeline-number {
    position: absolute;
    left: 0;
    top: 0;
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 0.9rem;
    box-shadow: 0 0 0 4px #e0e7ff;
}

/* Rich Text & Image Responsive Containment */
.followup-message-preview,
.rich-text-preview {
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-sm);
    padding: 1rem 1.25rem;
    font-size: 0.94rem;
    line-height: 1.7;
    color: var(--text-main);
    word-break: break-word;
    overflow-wrap: anywhere;
    min-height: auto;
    overflow: visible;
}

.followup-message-preview p,
.rich-text-preview p {
    margin-bottom: 0.85rem;
    line-height: 1.7;
}

.followup-message-preview p:last-child,
.rich-text-preview p:last-child {
    margin-bottom: 0;
}

.followup-message-preview img,
.rich-text-preview img,
.ql-editor img,
.quill-edit-box img,
.quill-editor-box img {
    max-width: 100% !important;
    height: auto !important;
    max-height: 420px !important;
    object-fit: contain !important;
    border-radius: var(--radius-sm);
    display: block !important;
    clear: both !important;
    margin: 14px 0 !important;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
    border: 1px solid #e2e8f0;
}

/* Messages thread */
.message-bubble {
    padding: 1rem 1.25rem;
    border-radius: var(--radius-md);
    margin-bottom: 1rem;
}

.message-incoming {
    background: #f1f5f9;
    border: 1px solid #e2e8f0;
    margin-right: 2rem;
}

.message-outgoing {
    background: #eef2ff;
    border: 1px solid #c7d2fe;
    margin-left: 2rem;
}

/* ==========================================================
   Comprehensive Device & OS Responsiveness (Mobile, Tablet, Desktop)
   ========================================================== */

/* Mobile & Tablet Drawer (< 992px) */
@media (max-width: 991.98px) {
    .sidebar {
        position: fixed;
        top: 0;
        bottom: 0;
        left: -290px;
        width: 280px;
        max-width: 85vw;
        height: 100vh;
        z-index: 1050;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    .sidebar.show {
        left: 0;
    }

    .sidebar-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(15, 23, 42, 0.6);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
        z-index: 1040;
        display: none;
        opacity: 0;
        transition: opacity 0.3s ease;
    }

    .sidebar-backdrop.show {
        display: block;
        opacity: 1;
    }

    .main-content {
        width: 100%;
        min-width: 0;
    }

    .top-navbar {
        padding: 0.65rem 1rem;
    }
}

/* Responsive Global Rules */
html, body {
    max-width: 100vw;
    overflow-x: hidden;
}

*, *::before, *::after {
    box-sizing: border-box;
}

.min-w-0 {
    min-width: 0 !important;
}

.badge {
    white-space: nowrap;
    max-width: 100%;
}

.table-responsive {
    width: 100%;
    margin-bottom: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.list-group-item {
    min-width: 0;
}

.list-group-item .text-truncate {
    max-width: 100%;
}

/* Medium Screen & Tablet Optimizations (576px - 1199px) */
@media (max-width: 1199.98px) {
    .stat-card {
        padding: 1rem;
    }
    .stat-card .fs-4 {
        font-size: 1.35rem !important;
    }
}

/* Small Smartphone (< 576px) */
@media (max-width: 575.98px) {
    body {
        font-size: 0.88rem;
    }

    .container, .container-fluid {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }

    main {
        padding: 0.75rem !important;
    }

    .top-navbar {
        padding: 0.5rem 0.75rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .top-navbar h5 {
        font-size: 1rem;
        max-width: 55vw;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .card {
        margin-bottom: 1rem;
    }

    .card-header, .card-body, .card-footer {
        padding: 0.85rem !important;
    }

    .stat-card {
        padding: 0.85rem !important;
    }

    .stat-icon {
        width: 38px;
        height: 38px;
        font-size: 0.95rem;
    }

    .table th, .table td {
        padding: 0.5rem 0.4rem;
        font-size: 0.8rem;
    }

    .btn {
        padding: 0.38rem 0.7rem;
        font-size: 0.82rem;
    }

    /* Prevent iOS auto-zoom on form controls */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    select,
    textarea {
        font-size: 16px !important;
    }

    .lead-dropzone-box {
        padding: 1.25rem 0.75rem !important;
    }

    .lead-dropzone-box h6 {
        font-size: 0.9rem !important;
    }

    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }

    .message-incoming {
        margin-right: 0.5rem;
    }

    .message-outgoing {
        margin-left: 0.5rem;
    }
}

/* Smooth Touch Scrolling on iOS & Android */
.table-responsive, .sidebar, main {
    -webkit-overflow-scrolling: touch;
}

/* Safe area insets for notched phones (iPhone X/11/12/13/14/15/16) */
@supports (padding: max(0px)) {
    body {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }
    .top-navbar {
        padding-top: max(0.65rem, env(safe-area-inset-top));
    }
    .sidebar {
        padding-top: max(1rem, env(safe-area-inset-top));
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }
}

