/* ===== CRM Module ===== */

.crm-app {
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 72px);
}

/* ===== Topbar ===== */
.crm-topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 64px;
    padding: 0 32px;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
}

.crm-topbar-left {
    display: flex;
    align-items: center;
    gap: 32px;
}

.crm-title {
    font-size: 22px;
    font-weight: 600;
    color: var(--color-text-dark);
}

.crm-tabs {
    display: flex;
    gap: 4px;
}

.crm-tab {
    padding: 8px 16px;
    border: none;
    background: none;
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text-muted);
    cursor: pointer;
    border-radius: 8px;
    transition: background 0.12s, color 0.12s;
}
.crm-tab:hover { background: var(--color-bg-soft); color: var(--color-text-body); }
.crm-tab.active {
    background: #EFF6FF;
    color: var(--color-primary);
    font-weight: 600;
}
[data-theme="dark"] .crm-tab.active { background: rgba(37,99,235,0.15); }

.crm-topbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.crm-search { position: relative; }
.crm-search-input {
    width: 300px;
    height: 40px;
    padding: 0 14px 0 38px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    color: var(--color-text-body);
    background: var(--color-bg);
}
.crm-search-input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
.crm-search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--color-text-muted);
    pointer-events: none;
}

.crm-icon-btn {
    width: 40px;
    height: 40px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    background: var(--color-bg);
    cursor: pointer;
    color: var(--color-text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.15s, color 0.15s;
}
.crm-icon-btn:hover { border-color: var(--color-primary); color: var(--color-primary); }

.crm-add-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 40px;
}

/* ===== Tab content wrapper ===== */
.crm-tab-content {
    display: none;
    flex: 1;
}
.crm-tab-content.active { display: flex; }

/* ===== Contacts view ===== */
.crm-contacts-view {
    display: flex;
    flex: 1;
    min-height: 0;
}

/* Filter sidebar */
.crm-sidebar {
    width: 260px;
    flex-shrink: 0;
    background: var(--color-bg-soft);
    border-right: 1px solid var(--color-border);
    padding: 24px;
    overflow-y: auto;
}

.crm-filter-group { margin-bottom: 20px; }
.crm-filter-label {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-muted);
    margin-bottom: 10px;
}

.crm-filter-list { list-style: none; }
.crm-filter-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 0;
    font-size: 14px;
    color: var(--color-text-body);
    cursor: pointer;
}
.crm-filter-item input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: #2563EB;
    cursor: pointer;
}
.crm-filter-count {
    margin-left: auto;
    font-size: 12px;
    color: var(--color-text-muted);
    background: var(--color-bg);
    border-radius: 10px;
    padding: 1px 8px;
}

/* Main area */
.crm-main {
    flex: 1;
    padding: 24px 32px;
    overflow-y: auto;
    min-width: 0;
}

.crm-subheader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}
.crm-list-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--color-text-dark);
}
.crm-list-count {
    font-weight: 400;
    color: var(--color-text-muted);
}

/* Contacts table */
.crm-table-wrap {
    border: 1px solid var(--color-border);
    border-radius: 12px;
    overflow: hidden;
}

.crm-table {
    width: 100%;
    border-collapse: collapse;
}

.crm-table thead th {
    height: 48px;
    padding: 0 16px;
    text-align: left;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-muted);
    background: var(--color-bg-soft);
    border-bottom: 1px solid var(--color-border);
}

.crm-table tbody tr {
    height: 56px;
    cursor: pointer;
    transition: background 0.12s;
}
.crm-table tbody tr:hover { background: var(--color-bg-soft); }
.crm-table tbody tr:not(:last-child) td { border-bottom: 1px solid var(--color-border); }

.crm-table td {
    padding: 0 16px;
    font-size: 14px;
    color: var(--color-text-body);
}

/* Contact cell */
.crm-contact-cell {
    display: flex;
    align-items: center;
    gap: 12px;
}
.crm-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #2563EB;
    color: #fff;
    font-size: 12px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.crm-avatar.lg { width: 64px; height: 64px; font-size: 24px; }
.crm-avatar.sm { width: 24px; height: 24px; font-size: 10px; }

.crm-contact-text { display: flex; flex-direction: column; }
.crm-contact-name { font-size: 14px; font-weight: 500; color: var(--color-text-body); }
.crm-contact-company { font-size: 12px; color: var(--color-text-muted); }

/* Badges */
.crm-badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 16px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.crm-badge.status-lead     { background: #EFF6FF; color: #2563EB; }
.crm-badge.status-customer { background: #ECFDF5; color: #16A34A; }
.crm-badge.status-partner  { background: #F5F3FF; color: #7C3AED; }
.crm-badge.status-inactive { background: #F1F5F9; color: #64748B; }

[data-theme="dark"] .crm-badge.status-lead     { background: rgba(37,99,235,0.15); }
[data-theme="dark"] .crm-badge.status-customer { background: rgba(22,163,74,0.15); }
[data-theme="dark"] .crm-badge.status-partner  { background: rgba(124,58,237,0.15); }
[data-theme="dark"] .crm-badge.status-inactive { background: rgba(100,116,139,0.15); }

.crm-badge.prio-high   { background: #FEE2E2; color: #DC2626; }
.crm-badge.prio-normal { background: #EFF6FF; color: #2563EB; }
.crm-badge.prio-low    { background: #F1F5F9; color: #64748B; }

[data-theme="dark"] .crm-badge.prio-high   { background: rgba(220,38,38,0.15); }
[data-theme="dark"] .crm-badge.prio-normal { background: rgba(37,99,235,0.15); }
[data-theme="dark"] .crm-badge.prio-low    { background: rgba(100,116,139,0.15); }

/* Tag chip */
.crm-tag {
    display: inline-flex;
    align-items: center;
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
    background: var(--color-bg-soft);
    color: var(--color-text-body);
    border: 1px solid var(--color-border);
}
.crm-tag.vip { background: #FFFBEB; color: #D97706; border-color: transparent; }
.crm-tag.new { background: #ECFDF5; color: #16A34A; border-color: transparent; }
[data-theme="dark"] .crm-tag.vip { background: rgba(217,119,6,0.15); }
[data-theme="dark"] .crm-tag.new { background: rgba(22,163,74,0.15); }

/* Contact row actions */
.crm-contact-action {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--color-text-body);
}
.crm-contact-action svg { color: var(--color-text-muted); flex-shrink: 0; }

.crm-row-actions { display: flex; gap: 4px; }
.crm-row-btn {
    width: 30px;
    height: 30px;
    border: none;
    border-radius: 6px;
    background: none;
    cursor: pointer;
    color: var(--color-text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
}
.crm-row-btn:hover { color: var(--color-primary); background: var(--color-bg-soft); }

/* ===== Contact detail view ===== */
.crm-detail {
    display: none;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.crm-detail.active { display: flex; }

.crm-detail-body {
    padding: 24px 32px;
    overflow-y: auto;
}

.crm-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    font-weight: 500;
    color: var(--color-primary);
    background: none;
    border: none;
    cursor: pointer;
    margin-bottom: 20px;
    padding: 4px 0;
}
.crm-back-btn:hover { text-decoration: underline; }

.crm-detail-grid {
    display: grid;
    grid-template-columns: 360px 1fr;
    gap: 24px;
}

/* Cards */
.crm-card {
    background: var(--color-card-bg);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 16px;
}

.crm-card-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-dark);
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

/* Profile card header */
.crm-profile-head {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--color-border);
    margin-bottom: 20px;
}
.crm-profile-name {
    font-size: 22px;
    font-weight: 700;
    color: var(--color-text-dark);
    margin-top: 12px;
}
.crm-profile-company {
    font-size: 15px;
    font-weight: 500;
    color: var(--color-text-muted);
    margin-bottom: 10px;
}
.crm-profile-badges { display: flex; gap: 8px; }

/* Field list */
.crm-field {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid var(--color-border);
    font-size: 14px;
}
.crm-field:last-child { border-bottom: none; }
.crm-field-label { color: var(--color-text-muted); }
.crm-field-value { font-weight: 500; color: var(--color-text-body); text-align: right; }

/* Business info */
.crm-biz-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}
.crm-biz-item {
    padding: 12px;
    background: var(--color-bg-soft);
    border-radius: 8px;
}
.crm-biz-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-text-muted);
    margin-bottom: 4px;
}
.crm-biz-value {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-dark);
}

/* Tags list */
.crm-tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
}
.crm-add-tag-btn {
    background: none;
    border: 1px dashed var(--color-border);
    border-radius: 12px;
    padding: 3px 10px;
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    color: var(--color-text-muted);
    cursor: pointer;
}
.crm-add-tag-btn:hover { color: var(--color-primary); border-color: var(--color-primary); }

/* Activity timeline */
.crm-timeline { list-style: none; position: relative; padding-left: 32px; }
.crm-timeline::before {
    content: '';
    position: absolute;
    left: 15px;
    top: 10px;
    bottom: 10px;
    width: 2px;
    background: var(--color-border);
}

.crm-timeline-item {
    position: relative;
    padding-bottom: 20px;
}
.crm-timeline-item:last-child { padding-bottom: 0; }

.crm-timeline-icon {
    position: absolute;
    left: -32px;
    top: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #EFF6FF;
    color: #2563EB;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--color-bg);
}
.crm-timeline-icon.green  { background: #ECFDF5; color: #16A34A; }
.crm-timeline-icon.orange { background: #FFF7ED; color: #F97316; }
.crm-timeline-icon.purple { background: #F5F3FF; color: #7C3AED; }

[data-theme="dark"] .crm-timeline-icon        { background: rgba(37,99,235,0.2); }
[data-theme="dark"] .crm-timeline-icon.green  { background: rgba(22,163,74,0.2); }
[data-theme="dark"] .crm-timeline-icon.orange { background: rgba(249,115,22,0.2); }
[data-theme="dark"] .crm-timeline-icon.purple { background: rgba(124,58,237,0.2); }

.crm-timeline-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}
.crm-timeline-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-dark);
}
.crm-timeline-date {
    font-size: 12px;
    color: var(--color-text-muted);
}
.crm-timeline-body {
    font-size: 14px;
    color: var(--color-text-body);
    line-height: 1.5;
}

/* Notes */
.crm-note-textarea {
    width: 100%;
    min-height: 120px;
    padding: 12px 14px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    color: var(--color-text-body);
    background: var(--color-bg);
    resize: vertical;
}
.crm-note-textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}
.crm-note-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 10px;
}

/* Tasks */
.crm-task-list { list-style: none; }
.crm-task {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid var(--color-border);
}
.crm-task:last-child { border-bottom: none; }
.crm-task-check {
    width: 18px;
    height: 18px;
    accent-color: #2563EB;
    flex-shrink: 0;
    cursor: pointer;
}
.crm-task-name { flex: 1; font-size: 14px; color: var(--color-text-body); }
.crm-task-name.done { text-decoration: line-through; color: var(--color-text-muted); }
.crm-task-date { font-size: 12px; color: var(--color-text-muted); }

/* Attachments */
.crm-attach-list { list-style: none; }
.crm-attach-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
    border-bottom: 1px solid var(--color-border);
    font-size: 14px;
    color: var(--color-text-body);
}
.crm-attach-item:last-child { border-bottom: none; }
.crm-attach-item svg { flex-shrink: 0; }
.crm-attach-name { flex: 1; }
.crm-attach-size { font-size: 12px; color: var(--color-text-muted); }

/* ===== Pipeline (Kanban) ===== */
.crm-pipeline {
    flex: 1;
    padding: 24px;
    overflow-x: auto;
    overflow-y: hidden;
    min-height: 0;
}

.crm-board {
    display: flex;
    gap: 16px;
    height: 100%;
    min-height: 500px;
}

.crm-column {
    width: 280px;
    flex-shrink: 0;
    background: var(--color-bg-soft);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 16px;
    display: flex;
    flex-direction: column;
    max-height: 100%;
}

.crm-column-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--color-border);
}

.crm-column-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--color-text-dark);
    display: flex;
    align-items: center;
    gap: 8px;
}

.crm-column-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #64748B;
}
.crm-column-dot.new        { background: #0EA5E9; }
.crm-column-dot.contacted  { background: #2563EB; }
.crm-column-dot.qualified  { background: #7C3AED; }
.crm-column-dot.proposal   { background: #F59E0B; }
.crm-column-dot.won        { background: #16A34A; }
.crm-column-dot.lost       { background: #DC2626; }

.crm-column-count {
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    color: var(--color-text-muted);
    font-size: 12px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 10px;
}

.crm-column-body {
    flex: 1;
    overflow-y: auto;
    min-height: 100px;
}

.crm-column-add {
    margin-top: 12px;
    padding: 8px;
    background: none;
    border: 1px dashed var(--color-border);
    border-radius: 8px;
    color: var(--color-text-muted);
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: 100%;
}
.crm-column-add:hover { color: var(--color-primary); border-color: var(--color-primary); }

/* Lead card */
.crm-lead {
    background: var(--color-card-bg);
    border: 1px solid var(--color-border);
    border-radius: 10px;
    padding: 14px;
    margin-bottom: 10px;
    cursor: grab;
    transition: box-shadow 0.15s, transform 0.15s;
}
.crm-lead:hover {
    box-shadow: 0 4px 12px var(--color-shadow);
    transform: translateY(-1px);
}
.crm-lead.dragging {
    opacity: 0.5;
    cursor: grabbing;
}

.crm-lead-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 8px;
}
.crm-lead-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text-dark);
}
.crm-lead-company {
    font-size: 12px;
    color: var(--color-text-muted);
    margin-bottom: 10px;
}

.crm-lead-prio {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
    margin-top: 4px;
}
.crm-lead-prio.high   { background: #DC2626; }
.crm-lead-prio.normal { background: #2563EB; }
.crm-lead-prio.low    { background: #64748B; }

.crm-lead-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
}
.crm-lead-value {
    font-weight: 600;
    color: var(--color-primary);
}
.crm-lead-date { color: var(--color-text-muted); }

.crm-lead-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--color-border);
}

/* ===== Add contact modal ===== */
.crm-modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 1200;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.crm-modal-overlay.open { display: flex; }

.crm-modal {
    width: 600px;
    max-width: 100%;
    max-height: 90vh;
    background: var(--color-bg);
    border-radius: 16px;
    box-shadow: 0 24px 48px rgba(0,0,0,0.2);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.crm-modal-header {
    padding: 24px 32px;
    border-bottom: 1px solid var(--color-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.crm-modal-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--color-text-dark);
}
.crm-modal-close {
    background: none;
    border: none;
    font-size: 28px;
    line-height: 1;
    color: var(--color-text-muted);
    cursor: pointer;
}
.crm-modal-close:hover { color: var(--color-text-dark); }

.crm-modal-body {
    padding: 24px 32px;
    overflow-y: auto;
    flex: 1;
}

.crm-form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

.crm-field-full { grid-column: span 2; }

.crm-form-label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: var(--color-text-body);
    margin-bottom: 6px;
}

.crm-input, .crm-select, .crm-textarea {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    color: var(--color-text-body);
    background: var(--color-bg);
}
.crm-textarea { min-height: 80px; resize: vertical; }
.crm-input:focus, .crm-select:focus, .crm-textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.crm-modal-footer {
    padding: 16px 32px;
    border-top: 1px solid var(--color-border);
    display: flex;
    gap: 12px;
    justify-content: flex-end;
}

/* ===== Responsive ===== */
@media (max-width: 1024px) {
    .crm-detail-grid { grid-template-columns: 1fr; }
    .crm-table td:nth-child(4),
    .crm-table th:nth-child(4),
    .crm-table td:nth-child(5),
    .crm-table th:nth-child(5) { display: none; }
}

@media (max-width: 768px) {
    .crm-topbar { flex-direction: column; gap: 12px; height: auto; padding: 16px; }
    .crm-topbar-left { flex-direction: column; gap: 12px; width: 100%; }
    .crm-topbar-right { width: 100%; flex-wrap: wrap; }
    .crm-search-input { width: 100%; }

    .crm-contacts-view { flex-direction: column; }
    .crm-sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid var(--color-border);
        max-height: 240px;
    }
    .crm-main { padding: 16px; }

    .crm-table td:nth-child(3),
    .crm-table th:nth-child(3),
    .crm-table td:nth-child(6),
    .crm-table th:nth-child(6) { display: none; }

    .crm-biz-grid { grid-template-columns: 1fr; }
    .crm-form-grid { grid-template-columns: 1fr; }
    .crm-field-full { grid-column: span 1; }
}
