/* BackYou CRM */
:root {
    --bg: #f4f5f9;
    --panel: #ffffff;
    --text: #1d2433;
    --muted: #6b7385;
    --line: #e4e7ee;
    --primary: #4f46e5;
    --primary-ink: #ffffff;
    --primary-soft: #eef0ff;
    --green: #059669;
    --green-soft: #e6f6ef;
    --red: #dc2626;
    --red-soft: #fdecec;
    --amber-soft: #fff6e0;
    --side: #151a2d;
    --side-ink: #c8cde0;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 2px 8px rgba(20, 24, 40, .04);
    --chat-in: #ffffff;
    --chat-out: #dcf8e6;
    --chat-bg: #eef1ec;
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1220;
        --panel: #181c2c;
        --text: #e7e9f2;
        --muted: #949bb2;
        --line: #2a3045;
        --primary: #7c74ff;
        --primary-soft: #262a4a;
        --green: #34d399;
        --green-soft: #12352a;
        --red: #f87171;
        --red-soft: #3a1c22;
        --amber-soft: #3a3016;
        --side: #0b0e1a;
        --shadow: 0 1px 2px rgba(0, 0, 0, .4);
        --chat-in: #232839;
        --chat-out: #1d4433;
        --chat-bg: #12151f;
    }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    display: flex;
    min-height: 100vh;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .75rem; line-height: 1.25; }
h2 { font-size: 1.05rem; }
h3 { font-size: .95rem; }
code { background: var(--primary-soft); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
small, .small { font-size: .85em; }
.muted { color: var(--muted); }
.text-red { color: var(--red); }
.grow { flex: 1; }
[hidden] { display: none !important; }

/* ---------- Barra lateral ---------- */
.sidebar {
    width: 230px;
    flex-shrink: 0;
    background: var(--side);
    color: var(--side-ink);
    display: flex;
    flex-direction: column;
    padding: 18px 12px;
    position: sticky;
    top: 0;
    height: 100vh;
}
.brand { font-size: 1.05rem; color: #fff; display: flex; align-items: center; gap: 8px; padding: 0 8px; }
.brand b { color: #a5a0ff; }
.brand-mark {
    width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
    background: linear-gradient(135deg, #6d5dfc, #22c3a6); color: #fff; font-weight: 800;
}
.brand-lg { font-size: 1.3rem; justify-content: center; margin-bottom: 1.25rem; color: var(--text); }
.tenant-name { margin: 18px 8px 10px; font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: #8c93ad; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a {
    display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
    color: var(--side-ink); text-decoration: none;
}
.sidebar nav a:hover { background: rgba(255, 255, 255, .06); }
.sidebar nav a.active { background: rgba(124, 116, 255, .22); color: #fff; }
.sidebar .ico { width: 18px; text-align: center; opacity: .85; }
.sidebar .badge { margin-left: auto; }
.nav-super { margin-top: 16px; padding-top: 12px; border-top: 1px solid rgba(255, 255, 255, .08); }
.me { margin-top: auto; display: flex; align-items: center; gap: 10px; padding: 10px 8px 0; border-top: 1px solid rgba(255, 255, 255, .08); }
.me div { flex: 1; min-width: 0; line-height: 1.2; }
.me b { color: #fff; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me small { color: #8c93ad; }
.me .link { color: #8c93ad; }

/* ---------- Principal ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
    display: flex; align-items: center; gap: 12px; padding: 16px 24px; background: var(--panel);
    border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 1.2rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.menu-btn { display: none; background: none; border: 0; font-size: 1.3rem; color: var(--text); cursor: pointer; }
.content { padding: 20px 24px 40px; }
.impersonate { background: var(--amber-soft); padding: 8px 24px; font-size: .9rem; }
.impersonate .link { margin-left: 8px; }

/* ---------- Componentes ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card-flush { padding: 0; overflow: hidden; }
.card-narrow { max-width: 620px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h3 { margin: 0; }
.stack > .card { margin-bottom: 16px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
.grid-2 > .card { margin-bottom: 0; }
.grid-wide-left { grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); }
.grid-wide-right { grid-template-columns: minmax(280px, 1fr) minmax(0, 2fr); }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel);
    color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { text-decoration: none; filter: brightness(.97); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: .85rem; }
.btn-block { width: 100%; }
.link { background: none; border: 0; padding: 0; color: var(--primary); cursor: pointer; font: inherit; }
.link-danger { color: var(--red); }
.inline { display: inline; }
.actions { display: flex; gap: 6px; align-items: center; justify-content: flex-end; flex-wrap: wrap; }

input, select, textarea {
    font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line);
    border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
input[type=color] { padding: 2px; height: 36px; width: 52px; }
input[type=checkbox] { width: auto; }
.input-xs { width: 64px; }
.input-sm { width: 100px; }
.input-title { font-weight: 700; font-size: 1rem; }
textarea { resize: vertical; }
.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: .85rem; }
.form-compact { gap: 8px; }
.form fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; display: flex; flex-direction: column; gap: 10px; }
.form legend { font-weight: 700; font-size: .85rem; padding: 0 4px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.row-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; align-items: center; }
.inline-form { display: flex; gap: 8px; align-items: center; }
.inline-form input { width: auto; flex: 1; min-width: 140px; }
.copy { display: flex; gap: 6px; margin-top: 4px; }
.copy input { font-family: ui-monospace, Menlo, monospace; font-size: .8rem; }
textarea.code { font-family: ui-monospace, Menlo, monospace; font-size: .8rem; margin-top: 8px; }
.hp { position: absolute; left: -9999px; }

.alert { padding: 10px 14px; border-radius: 10px; margin: 16px 24px 0; font-weight: 500; }
.content .alert, .auth-card .alert { margin: 0 0 14px; }
.alert-ok { background: var(--green-soft); color: var(--green); }
.alert-error { background: var(--red-soft); color: var(--red); }
.alert-warn { background: var(--amber-soft); }

.pill { display: inline-block; padding: 1px 8px; border-radius: 99px; background: var(--primary-soft); color: var(--primary); font-size: .75rem; font-weight: 600; white-space: nowrap; }
.pill-green { background: var(--green-soft); color: var(--green); }
.pill-red { background: var(--red-soft); color: var(--red); }
.tag { display: inline-block; padding: 0 7px; margin: 2px 4px 0 0; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); font-size: .72rem; color: var(--muted); }
.badge { background: #22c55e; color: #fff; border-radius: 99px; font-size: .72rem; font-weight: 700; padding: 0 7px; min-width: 20px; text-align: center; }
.avatar {
    width: 32px; height: 32px; border-radius: 50%; display: inline-grid; place-items: center; flex-shrink: 0;
    background: var(--primary-soft); color: var(--primary); font-weight: 700; font-size: .8rem;
}
.avatar-xs { width: 22px; height: 22px; font-size: .62rem; }
.avatar-lg { width: 52px; height: 52px; font-size: 1.1rem; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table select { width: auto; min-width: 110px; }
.table-click tbody tr { cursor: pointer; }
.table-click tbody tr:hover { background: var(--bg); }
.table-compact td { padding: 6px 4px; }
.num { text-align: right; white-space: nowrap; }
.row-off { opacity: .55; }

.toolbar { display: flex; gap: 10px; margin-bottom: 16px; align-items: center; flex-wrap: wrap; }
.toolbar input[type=search] { max-width: 380px; }
.toolbar select { width: auto; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 16px; }
.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.subnav { display: flex; gap: 4px; margin-bottom: 16px; overflow-x: auto; border-bottom: 1px solid var(--line); }
.subnav a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 600; text-decoration: none; }
.subnav a.active { color: var(--primary); border-color: var(--primary); }
.link-more { display: inline-block; margin-top: 10px; font-weight: 600; }
details summary { cursor: pointer; color: var(--primary); font-weight: 600; }
.mini-form { margin-top: 10px; }
.mini-form form { margin-top: 10px; }
.steps { padding-left: 1.2rem; }
.steps li { margin-bottom: 6px; }

/* ---------- Login ---------- */
body.auth { align-items: center; justify-content: center; padding: 16px; background: linear-gradient(160deg, var(--bg), var(--primary-soft)); }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 16px; padding: 28px; box-shadow: 0 10px 40px rgba(20, 24, 60, .12); display: flex; flex-direction: column; gap: 14px; }
.auth-card > label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: .85rem; }
.form-title { font-size: 1.3rem; text-align: center; }

/* ---------- Inicio ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.kpi small { color: var(--muted); font-weight: 600; }
.kpi b { font-size: 1.6rem; margin: 2px 0; }
.kpi span { color: var(--muted); font-size: .85rem; }
.kpi-green b { color: var(--green); }
.funnel { display: flex; flex-direction: column; gap: 8px; }
.funnel-row { display: grid; grid-template-columns: 140px 1fr auto; gap: 10px; align-items: center; font-size: .85rem; }
.funnel-bar { background: var(--bg); border-radius: 6px; height: 14px; overflow: hidden; }
.funnel-bar i { display: block; height: 100%; border-radius: 6px; }
.funnel-num { color: var(--muted); white-space: nowrap; }
.feed, .task-list, .timeline { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.feed li:last-child { border: 0; }
.feed small, .task-list small { display: block; color: var(--muted); }
.feed-ico { width: 22px; text-align: center; }

.task-list li { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.task-list li:last-child { border: 0; }
.task-list li.late b { color: var(--red); }
.task-list li.done b { text-decoration: line-through; color: var(--muted); }
.check { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--line); background: var(--panel); cursor: pointer; margin-top: 1px; padding: 0; }
.check:hover { border-color: var(--green); }
.check.checked { background: var(--green); border-color: var(--green); }

/* ---------- Tablero kanban ---------- */
.board { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 12px; align-items: flex-start; min-height: 60vh; }
.column { flex: 0 0 272px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; max-height: calc(100vh - 210px); }
.column header { padding: 12px 14px 10px; border-top: 4px solid var(--c); border-radius: var(--radius) var(--radius) 0 0; border-bottom: 1px solid var(--line); }
.column header b { display: block; }
.column header small { color: var(--muted); }
.column-body { padding: 10px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; min-height: 80px; }
.column-body.over { background: var(--primary-soft); }
.column-won .column-body, .column-lost .column-body { opacity: .85; }
.deal { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; cursor: grab; }
.deal:hover { border-color: var(--primary); }
.deal.dragging { opacity: .4; }
.deal-title { font-weight: 600; color: var(--text); display: block; }
.deal-contact { color: var(--muted); font-size: .85rem; }
.deal-meta { display: flex; align-items: center; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.deal-meta .avatar { margin-left: auto; }

/* ---------- Ficha de contacto ---------- */
.contact-grid { display: grid; grid-template-columns: 300px minmax(0, 1fr) 360px; gap: 16px; align-items: start; }
.contact-head { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
.contact-head h2 { margin: 0; }
.contact-chip { display: flex; gap: 10px; align-items: center; padding: 8px; border: 1px solid var(--line); border-radius: 10px; color: var(--text); margin-bottom: 14px; }
.contact-chip small { display: block; color: var(--muted); }
.deal-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); color: var(--text); }
.deal-row span:nth-child(2) { flex: 1; min-width: 0; }
.deal-row small { display: block; color: var(--muted); }
.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tabs button { background: none; border: 1px solid transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 600; color: var(--muted); }
.tabs button.active { background: var(--primary-soft); color: var(--primary); }
.composer { display: flex; flex-direction: column; gap: 8px; align-items: flex-end; margin-bottom: 18px; }
.composer > * { width: 100%; }
.composer .btn { width: auto; }
.timeline li { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.timeline small { color: var(--muted); }
.tl-ico { width: 24px; text-align: center; }
.tl-body { white-space: normal; word-break: break-word; }
.tl-stage .tl-body, .tl-system .tl-body { color: var(--muted); }

/* ---------- Chat WhatsApp ---------- */
.chat { display: flex; flex-direction: column; gap: 10px; }
.chat-log { background: var(--chat-bg); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 6px; height: 440px; overflow-y: auto; }
.msg { max-width: 82%; padding: 7px 10px; border-radius: 10px; box-shadow: 0 1px 1px rgba(0, 0, 0, .06); word-break: break-word; }
.msg small { display: block; text-align: right; color: var(--muted); font-size: .7rem; margin-top: 2px; }
.msg-in { background: var(--chat-in); align-self: flex-start; border-top-left-radius: 2px; }
.msg-out { background: var(--chat-out); align-self: flex-end; border-top-right-radius: 2px; }
.msg-failed { outline: 1px solid var(--red); }
.msg-error { color: var(--red) !important; text-align: left !important; }
.tick-read { color: #3b82f6; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; }
.chat-form textarea { flex: 1; }
.chat-template { flex-direction: column; align-items: stretch; }
.chat-template p { margin: 0; }
.chat-empty { text-align: center; margin: auto; }

.inbox { display: grid; grid-template-columns: 320px 1fr; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; height: calc(100vh - 130px); min-height: 480px; }
.inbox-list { border-right: 1px solid var(--line); overflow-y: auto; }
.inbox-search { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); }
.inbox-search select { width: auto; }
.conv { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); color: var(--text); text-decoration: none !important; }
.conv:hover, .conv.active { background: var(--bg); }
.conv-main { flex: 1; min-width: 0; }
.conv-main b, .conv-main small { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-main small { color: var(--muted); }
.conv.unread .conv-main small { color: var(--text); font-weight: 600; }
.conv-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; color: var(--muted); }
.inbox-chat { display: flex; flex-direction: column; padding: 12px; min-height: 0; }
.inbox-chat .chat { flex: 1; min-height: 0; }
.inbox-chat .chat-log { flex: 1; height: auto; }
.inbox-head { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.inbox-head div { flex: 1; line-height: 1.2; }
.inbox-head small { display: block; color: var(--muted); }
.inbox-back { display: none; font-size: 1.3rem; }
.pad { padding: 12px; }

/* ---------- Modal ---------- */
.modal { border: 0; border-radius: 16px; padding: 22px; width: min(520px, calc(100vw - 32px)); background: var(--panel); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal::backdrop { background: rgba(15, 18, 35, .5); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- Responsivo ---------- */
@media (max-width: 1280px) {
    .contact-grid { grid-template-columns: 280px minmax(0, 1fr); }
    .contact-grid .chat-card { grid-column: 1 / -1; }
}
@media (max-width: 1000px) {
    .kpis { grid-template-columns: repeat(2, 1fr); }
    .grid-2, .grid-wide-left, .grid-wide-right, .contact-grid { grid-template-columns: 1fr; }
    .grid-2 > .card { margin-bottom: 0; }
}
@media (max-width: 760px) {
    .sidebar { position: fixed; left: 0; top: 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; }
    .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0, 0, 0, .4); }
    .menu-btn { display: block; }
    .topbar { padding: 12px 16px; flex-wrap: wrap; }
    .content { padding: 16px; }
    .alert { margin: 12px 16px 0; }
    .kpis { gap: 10px; }
    .kpi b { font-size: 1.25rem; }
    .row { flex-direction: column; }
    .funnel-row { grid-template-columns: 1fr; gap: 2px; }
    .column { flex-basis: 82vw; }
    .inbox { grid-template-columns: 1fr; height: calc(100vh - 110px); }
    .inbox.has-chat .inbox-list { display: none; }
    .inbox:not(.has-chat) .inbox-chat { display: none; }
    .inbox-back { display: inline; }
}
