/* ───────── SAFE CRM — mobile-first UI ───────── */
:root {
    --crm-primary: #4f46e5;
    --crm-bg: #f5f6fa;
    --crm-surface: #ffffff;
    --crm-border: #e6e8ef;
    --crm-muted: #6b7280;
    --crm-sidebar-w: 248px;
    --crm-topbar-h: 58px;
    --crm-bottom-h: 64px;
    --bs-primary: #4f46e5;
    --bs-primary-rgb: 79, 70, 229;
    --bs-link-color: #4f46e5;
    --bs-link-color-rgb: 79, 70, 229;
    --bs-link-hover-color: #4338ca;
    --bs-border-radius: .6rem;
    --bs-body-font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}
[data-bs-theme=dark] { --crm-bg: #0f1117; --crm-surface: #171a23; --crm-border: #262a36; --crm-muted: #9ca3af; }

body { background: var(--crm-bg); font-size: .95rem; -webkit-tap-highlight-color: transparent; }
a { text-decoration: none; }
.btn-primary { --bs-btn-bg: #4f46e5; --bs-btn-border-color: #4f46e5; --bs-btn-hover-bg: #4338ca; --bs-btn-hover-border-color: #4338ca; --bs-btn-active-bg: #3730a3; }
.btn-outline-primary { --bs-btn-color: #4f46e5; --bs-btn-border-color: #4f46e5; --bs-btn-hover-bg: #4f46e5; --bs-btn-hover-border-color: #4f46e5; --bs-btn-active-bg: #4f46e5; }
.text-primary { color: #4f46e5 !important; }
.text-pre { white-space: pre-wrap; }
.min-w-0 { min-width: 0; }
.card { border-color: var(--crm-border); background: var(--crm-surface); box-shadow: 0 1px 2px rgba(16, 24, 40, .04); }
.card-header { background: transparent; font-weight: 600; border-color: var(--crm-border); }
.dot { display: inline-block; width: .55rem; height: .55rem; border-radius: 50%; margin-right: .35rem; vertical-align: middle; }

/* ───────── Top bar ───────── */
.app-topbar {
    position: sticky; top: 0; z-index: 1030; height: var(--crm-topbar-h); display: flex; align-items: center; gap: .6rem;
    padding: 0 .75rem; padding-top: env(safe-area-inset-top); background: var(--crm-surface); border-bottom: 1px solid var(--crm-border);
}
.brand { display: flex; align-items: center; gap: .5rem; color: inherit; font-size: 1.05rem; white-space: nowrap; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; background: linear-gradient(135deg, #4f46e5, #7c3aed); color: #fff; display: grid; place-items: center; font-size: 1.05rem; }
.btn-icon { width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; font-size: 1.35rem; padding: 0; border: 0; }
.btn-icon:hover { background: rgba(79, 70, 229, .08); }
.top-search { position: relative; flex: 1; max-width: 560px; margin-left: auto; display: none; }
.top-search > i { position: absolute; left: .8rem; top: 50%; transform: translateY(-50%); color: var(--crm-muted); }
.top-search input { width: 100%; border: 1px solid var(--crm-border); background: var(--crm-bg); border-radius: 999px; padding: .45rem 1rem .45rem 2.2rem; outline: none; color: inherit; }
.top-search input:focus { border-color: #a5b4fc; box-shadow: 0 0 0 .2rem rgba(79, 70, 229, .12); }
.search-results { width: 100%; max-height: 70vh; overflow: auto; }
.search-results.show { display: block; }
.avatar-btn { width: 38px; height: 38px; border-radius: 50%; border: 0; background: #eef2ff; color: #4338ca; font-weight: 700; font-size: .85rem; }
.mobile-search { background: var(--crm-surface); border-bottom: 1px solid var(--crm-border); }
@media (min-width: 768px) { .top-search { display: block; } .app-topbar { padding: 0 1.25rem; } }
@media (max-width: 767.98px) { .app-topbar .brand { margin-right: auto; } .brand-text b { display: none; } }

/* ───────── Shell & sidebar ───────── */
.app-shell { display: flex; min-height: calc(100vh - var(--crm-topbar-h)); }
.app-main { flex: 1; min-width: 0; padding: 1rem .85rem calc(var(--crm-bottom-h) + 1.5rem + env(safe-area-inset-bottom)); }
@media (min-width: 992px) {
    .app-main { padding: 1.5rem 2rem 2rem; }
    .app-sidebar { width: var(--crm-sidebar-w); flex-shrink: 0; position: sticky; top: var(--crm-topbar-h); height: calc(100vh - var(--crm-topbar-h)); overflow-y: auto; border-right: 1px solid var(--crm-border); background: var(--crm-surface); }
}
.sidebar-nav { padding: .75rem .6rem 2rem; width: 100%; }
.sidebar-nav .nav-link { display: flex; align-items: center; gap: .7rem; padding: .5rem .75rem; border-radius: .55rem; color: inherit; font-weight: 500; opacity: .85; }
.sidebar-nav .nav-link i { font-size: 1.1rem; width: 1.2rem; text-align: center; opacity: .8; }
.sidebar-nav .nav-link:hover { background: rgba(79, 70, 229, .07); opacity: 1; }
.sidebar-nav .nav-link.active { background: #eef2ff; color: #4338ca; opacity: 1; }
[data-bs-theme=dark] .sidebar-nav .nav-link.active { background: rgba(99, 102, 241, .2); color: #c7d2fe; }
.nav-section { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--crm-muted); padding: 1rem .75rem .35rem; font-weight: 600; }

/* ───────── Bottom nav (mobile) ───────── */
.app-bottomnav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1030; height: calc(var(--crm-bottom-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    display: grid; grid-template-columns: repeat(5, 1fr); align-items: center; background: var(--crm-surface); border-top: 1px solid var(--crm-border); box-shadow: 0 -4px 16px rgba(0, 0, 0, .04);
}
.app-bottomnav a { display: flex; flex-direction: column; align-items: center; gap: 2px; color: var(--crm-muted); font-size: .7rem; font-weight: 500; }
.app-bottomnav a i { font-size: 1.3rem; }
.app-bottomnav a.active { color: #4f46e5; }
.fab { justify-self: center; width: 56px; height: 56px; margin-top: -26px; border-radius: 50%; border: 4px solid var(--crm-bg); background: linear-gradient(135deg, #4f46e5, #7c3aed); color: #fff; font-size: 1.6rem; box-shadow: 0 8px 20px rgba(79, 70, 229, .4); }
.quick-sheet { height: auto !important; max-height: 75vh; border-radius: 1.25rem 1.25rem 0 0; padding-bottom: env(safe-area-inset-bottom); }

/* ───────── Quick tiles ───────── */
.quick-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; }
@media (min-width: 576px) { .quick-grid { grid-template-columns: repeat(6, 1fr); } }
@media (min-width: 1200px) { .quick-grid { grid-template-columns: repeat(12, 1fr); } }
.quick-tile { display: flex; flex-direction: column; align-items: center; gap: .35rem; padding: .6rem .2rem; border-radius: .9rem; color: inherit; text-align: center; transition: background .15s, transform .1s; }
.quick-tile:hover { background: var(--crm-surface); }
.quick-tile:active { transform: scale(.95); }
.quick-tile.wide { flex-direction: row; justify-content: flex-start; padding: .65rem .8rem; background: var(--crm-surface); border: 1px solid var(--crm-border); text-align: left; }
.qt-icon { width: 48px; height: 48px; border-radius: 14px; display: grid; place-items: center; color: #fff; font-size: 1.3rem; box-shadow: 0 4px 10px rgba(0, 0, 0, .08); flex-shrink: 0; }
.qt-icon.sm { width: 32px; height: 32px; border-radius: 9px; font-size: .95rem; }
.qt-label { font-size: .75rem; font-weight: 600; line-height: 1.15; }
.quick-tile.wide .qt-label { font-size: .85rem; }

/* ───────── KPI cards ───────── */
.kpi-card { display: block; height: 100%; padding: 1rem; border-radius: 1rem; background: var(--crm-surface); border: 1px solid var(--crm-border); color: inherit; position: relative; overflow: hidden; transition: transform .12s, box-shadow .12s; }
.kpi-card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(16, 24, 40, .08); }
.kpi-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--kpi); }
.kpi-icon { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; color: var(--kpi); background: color-mix(in srgb, var(--kpi) 14%, transparent); font-size: 1.15rem; margin-bottom: .5rem; }
.kpi-title { font-size: .78rem; color: var(--crm-muted); font-weight: 600; }
.kpi-value { font-size: 1.45rem; font-weight: 700; line-height: 1.2; word-break: break-word; }
.kpi-sub { font-size: .75rem; color: var(--crm-muted); margin-top: .15rem; }
.stat-tile { background: var(--crm-surface); border: 1px solid var(--crm-border); border-radius: .85rem; padding: .75rem .9rem; height: 100%; }
.stat-label { font-size: .75rem; color: var(--crm-muted); font-weight: 600; }
.stat-value { font-size: 1.25rem; font-weight: 700; }

/* ───────── Page header & filters ───────── */
.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: .75rem; margin-bottom: 1rem; }
.page-header h1 { font-size: 1.4rem; font-weight: 700; margin: 0; }
.page-header p { font-size: .85rem; }
@media (max-width: 575.98px) { .page-header p { display: none; } .page-header h1 { font-size: 1.25rem; } }
.filter-bar { display: flex; gap: .5rem; margin-bottom: .75rem; flex-wrap: wrap; align-items: center; }
.filter-bar .input-group { flex: 1 1 260px; }
.filter-bar .form-select { flex: 0 1 200px; }
.chip-scroll { display: flex; gap: .4rem; overflow-x: auto; padding-bottom: .25rem; scrollbar-width: none; }
.chip-scroll::-webkit-scrollbar { display: none; }
.chip { flex-shrink: 0; display: inline-flex; align-items: center; gap: .25rem; padding: .35rem .8rem; border-radius: 999px; border: 1px solid var(--crm-border); background: var(--crm-surface); color: inherit; font-size: .82rem; font-weight: 500; white-space: nowrap; cursor: pointer; }
.chip:hover { border-color: #a5b4fc; }
.chip.active { background: #4f46e5; border-color: #4f46e5; color: #fff; }
.chip.active .badge { background: rgba(255, 255, 255, .25) !important; color: #fff !important; }

/* ───────── Tables → cards on mobile ───────── */
.table { --bs-table-bg: transparent; margin-bottom: 0; }
.table thead th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--crm-muted); font-weight: 600; border-bottom-width: 1px; white-space: nowrap; }
.table td { vertical-align: middle; }
tr[data-href] { cursor: pointer; }
.row-actions { white-space: nowrap; }
@media (max-width: 767.98px) {
    .table-mobile thead { display: none; }
    .table-mobile, .table-mobile tbody, .table-mobile tr, .table-mobile td { display: block; width: 100%; }
    .table-mobile tr { padding: .75rem .9rem; border-bottom: 1px solid var(--crm-border); position: relative; }
    .table-mobile td { border: 0; padding: .1rem 0; display: flex; gap: .5rem; font-size: .85rem; }
    .table-mobile td::before { content: attr(data-label); color: var(--crm-muted); font-size: .75rem; min-width: 92px; flex-shrink: 0; }
    .table-mobile td.cell-title { font-size: 1rem; padding-right: 5.5rem; }
    .table-mobile td.cell-title::before, .table-mobile td.row-actions::before, .table-mobile td:empty { display: none; }
    .table-mobile td.row-actions { position: absolute; top: .55rem; right: .6rem; width: auto; }
    .table-mobile td:has(> :empty:only-child) { display: none; }
}

/* ───────── Forms ───────── */
.form-card { max-width: 1100px; }
.form-label { font-size: .82rem; font-weight: 600; margin-bottom: .25rem; }
.form-control, .form-select { border-color: var(--crm-border); }
@media (max-width: 767.98px) { .form-control, .form-select { font-size: 16px; padding: .55rem .75rem; } }
.form-section-title { font-weight: 700; font-size: .9rem; margin-bottom: .75rem; display: flex; align-items: center; gap: .4rem; }
.form-section-title .step { width: 24px; height: 24px; border-radius: 50%; background: #4f46e5; color: #fff; display: grid; place-items: center; font-size: .8rem; }
.form-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }
.sticky-actions { position: sticky; bottom: calc(var(--crm-bottom-h) + env(safe-area-inset-bottom)); background: color-mix(in srgb, var(--crm-bg) 92%, transparent); backdrop-filter: blur(6px); padding: .65rem 0; margin-top: .5rem; z-index: 5; }
@media (min-width: 992px) { .sticky-actions { bottom: 0; } }
.cfg-row { display: flex; align-items: center; gap: .5rem; }
.cfg-row .form-control-color { width: 42px; flex-shrink: 0; padding: .2rem; }
.cfg-order { width: 80px !important; flex-shrink: 0; }
.cfg-row-wrap { flex-wrap: wrap; }
@media (max-width: 767.98px) { .cfg-row { flex-wrap: wrap; } .cfg-row > .form-control:not(.cfg-order):not(.form-control-color) { flex: 1 1 60%; } }
.type-picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: .4rem; }
.type-picker label { display: flex; flex-direction: column; align-items: center; gap: .15rem; padding: .5rem .25rem; font-size: .75rem; }
.type-picker label i { font-size: 1.2rem; }
.channel-tabs .nav-link { border-radius: 999px; }
.channel-tabs .nav-link.active.wa { background: #25d366; }
.channel-tabs .nav-link.active.sms { background: #f97316; }

/* ───────── Record header & quick actions ───────── */
.record-header { display: flex; gap: .85rem; align-items: flex-start; margin-bottom: 1rem; }
.record-header h1 { font-size: 1.4rem; font-weight: 700; margin: 0; }
.record-header-actions { display: flex; gap: .4rem; flex-wrap: wrap; justify-content: flex-end; }
.avatar { width: 56px; height: 56px; border-radius: 50%; background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; display: grid; place-items: center; font-size: 1.4rem; font-weight: 700; flex-shrink: 0; }
.avatar-square { border-radius: 16px; }
@media (max-width: 575.98px) { .avatar { width: 46px; height: 46px; font-size: 1.15rem; } .record-header h1 { font-size: 1.2rem; } }
.quick-actions { display: flex; gap: .5rem; overflow-x: auto; padding: .25rem 0; scrollbar-width: none; }
.quick-actions::-webkit-scrollbar { display: none; }
.qa-btn { flex-shrink: 0; display: flex; flex-direction: column; align-items: center; gap: .2rem; min-width: 70px; padding: .55rem .6rem; border-radius: .85rem; background: var(--crm-surface); border: 1px solid var(--crm-border); color: inherit; font-size: .72rem; font-weight: 600; }
.qa-btn i { font-size: 1.25rem; }
.qa-btn:active { transform: scale(.95); }
.qa-call i { color: #0ea5e9; } .qa-wa i { color: #25d366; } .qa-chat i { color: #128c7e; } .qa-sms i { color: #f97316; } .qa-email i { color: #6366f1; } .qa-log i { color: #64748b; } .qa-task i { color: #f59e0b; }
.kv dt { font-weight: 500; color: var(--crm-muted); font-size: .82rem; }
.kv dd { margin-bottom: .45rem; }

/* ───────── Timeline ───────── */
.timeline { list-style: none; margin: 0; padding: 0; position: relative; }
.timeline::before { content: ""; position: absolute; left: 15px; top: 4px; bottom: 4px; width: 2px; background: var(--crm-border); }
.timeline li { display: flex; gap: .75rem; padding-bottom: 1rem; position: relative; }
.tl-icon { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-size: .85rem; flex-shrink: 0; z-index: 1; box-shadow: 0 0 0 4px var(--crm-surface); }
.tl-body { flex: 1; min-width: 0; padding-top: .3rem; }
.tl-detail { white-space: pre-wrap; word-break: break-word; }

/* ───────── Kanban ───────── */
.kanban { display: flex; gap: .75rem; overflow-x: auto; padding-bottom: 1rem; scroll-snap-type: x mandatory; min-height: 60vh; }
.kanban-col { flex: 0 0 280px; background: color-mix(in srgb, var(--crm-border) 45%, transparent); border-radius: .9rem; display: flex; flex-direction: column; scroll-snap-align: start; max-height: calc(100vh - 220px); }
@media (max-width: 575.98px) { .kanban-col { flex-basis: 84vw; } }
.kanban-head { padding: .7rem .8rem; border-top: 4px solid; border-radius: .9rem .9rem 0 0; background: var(--crm-surface); }
.kanban-body { padding: .5rem; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: .5rem; min-height: 80px; }
.kanban-col.drag-over .kanban-body { background: rgba(79, 70, 229, .08); border-radius: 0 0 .9rem .9rem; }
.kanban-card { background: var(--crm-surface); border: 1px solid var(--crm-border); border-radius: .7rem; padding: .65rem .75rem; cursor: grab; box-shadow: 0 1px 2px rgba(0, 0, 0, .04); }
.kanban-card.dragging { opacity: .5; }
.stage-path { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.stage-path::-webkit-scrollbar { display: none; }
.stage-step { flex: 1 0 auto; margin: 0; }
.stage-step button { width: 100%; border: 0; padding: .5rem .9rem; font-size: .78rem; font-weight: 600; background: var(--crm-border); color: var(--crm-muted); clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 50%, calc(100% - 10px) 100%, 0 100%, 10px 50%); white-space: nowrap; }
.stage-step:first-child button { clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 50%, calc(100% - 10px) 100%, 0 100%); border-radius: .5rem 0 0 .5rem; }
.stage-step.done button { background: color-mix(in srgb, var(--c) 30%, transparent); color: inherit; }
.stage-step.current button { background: var(--c); color: #fff; }

/* ───────── Activities, messages ───────── */
.activity-row { display: flex; align-items: center; gap: .65rem; padding: .7rem .9rem; }
.activity-row.overdue { border-left: 3px solid #ef4444; }
.check-btn { border: 0; background: none; font-size: 1.35rem; color: var(--crm-muted); padding: 0; line-height: 1; }
.check-btn:hover { color: #22c55e; }
.check-btn.done { color: #22c55e; }
.act-icon { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; color: #fff; flex-shrink: 0; }
.msg-row.inbound { background: rgba(14, 165, 233, .05); }
.msg-preview { background: var(--crm-bg); border-radius: .6rem; padding: .75rem; }
.phone-preview { white-space: pre-wrap; padding: .85rem 1rem; border-radius: 1rem; background: var(--crm-bg); font-size: .9rem; min-height: 80px; }
.phone-preview.whatsapp { background: #dcf8c6; color: #111; border-top-left-radius: .25rem; }
.phone-preview.sms { background: #e9e9eb; color: #111; }
.phone-preview.email { background: var(--crm-surface); border: 1px solid var(--crm-border); }
.template-preview { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-wrap; }
.template-card:hover, .product-card:hover { border-color: #a5b4fc; }

/* ───────── Admin field rules ───────── */
.field-readonly .form-control, .field-readonly .form-select { background-color: var(--crm-bg); color: var(--crm-muted); }
.field-readonly select, .field-readonly input[type=checkbox], .field-readonly input[type=radio], .field-readonly .btn-check + label { pointer-events: none; }
.field-readonly > .form-label::after, .field-readonly .form-check-label::after { content: " \F47B"; font-family: bootstrap-icons; font-size: .75em; color: var(--crm-muted); }
.field-required > .form-label::after { content: " *"; color: #dc3545; }
.customise-link { display: inline-flex; align-items: center; gap: .3rem; font-size: .78rem; color: var(--crm-muted); margin-bottom: .5rem; }
.customise-link:hover { color: #4f46e5; }
.ff-row { display: grid; grid-template-columns: minmax(150px, 1.3fr) auto auto minmax(120px, 1fr) minmax(110px, .8fr); gap: .6rem; align-items: center; }
.ff-row.is-hidden .ff-name, .ff-row.is-hidden .ff-label, .ff-row.is-hidden .ff-req { opacity: .45; }
@media (max-width: 767.98px) { .ff-row { grid-template-columns: 1fr auto; } .ff-label, .ff-default { grid-column: span 1; } .ff-req { justify-self: end; } }

/* ───────── Misc ───────── */
.empty-state { text-align: center; padding: 3rem 1rem; }
.empty-state > i { font-size: 3rem; color: #c7d2fe; }
.empty-state h5 { margin-top: .75rem; font-weight: 700; }
.toast-container { z-index: 1090; margin-top: var(--crm-topbar-h); }
.auth-body { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(circle at 20% 20%, #e0e7ff, transparent 40%), radial-gradient(circle at 80% 80%, #f3e8ff, transparent 40%), var(--crm-bg); padding: 1rem; }
.auth-card { width: 100%; max-width: 400px; background: var(--crm-surface); border-radius: 1.25rem; padding: 2rem 1.75rem; box-shadow: 0 20px 50px rgba(79, 70, 229, .12); }
.auth-card .brand-mark { width: 52px; height: 52px; font-size: 1.5rem; border-radius: 14px; }
