/* ==========================================================================
   Telephony CRM  —  style.css
   Modern, responsive agent console. CSS custom properties + Grid/Flexbox.
   ========================================================================== */

:root {
    --bg:            #0f1729;
    --bg-2:          #131c30;
    --surface:       #ffffff;
    --surface-2:     #f7f9fc;
    --border:        #e4e8f0;
    --text:          #1f2937;
    --text-soft:     #6b7280;
    --primary:       #3b56f0;
    --primary-600:   #2f45cc;
    --danger:        #ef4444;
    --success:       #16a34a;
    --warn:          #f59e0b;
    --radius:        12px;
    --radius-sm:     8px;
    --shadow:        0 4px 20px rgba(16, 24, 40, 0.08);
    --shadow-lg:     0 12px 40px rgba(16, 24, 40, 0.18);
    --sidebar-w:     236px;
    --font:          -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    font-family: var(--font);
    color: var(--text);
    background: var(--surface-2);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* ---------- App shell (grid) ---------- */
.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    min-height: 100vh;
}

/* ---------- Sidebar (white, matches content area) ---------- */
.sidebar {
    background: var(--surface);
    color: var(--text);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 20px 14px;
    position: sticky;
    top: 0;
    height: 100vh;
}
.sidebar__brand {
    display: flex; align-items: center; gap: 10px;
    padding: 6px 8px 22px;
}
.brand__dot {
    width: 12px; height: 12px; border-radius: 50%;
    background: var(--primary);
    box-shadow: 0 0 0 4px rgba(59, 86, 240, 0.25);
}
.brand__name { font-size: 18px; color: var(--text); letter-spacing: .3px; }
.brand__name strong { color: var(--primary); }

/* Brand logo. content-box so `height` sizes the IMAGE, not image + padding
   (the global border-box would otherwise shrink it drastically). */
.brand__logo { display: block; height: 32px; width: auto; max-width: 100%; }
.sidebar__brand .brand__logo {
    box-sizing: content-box;
    height: 36px; width: auto; max-width: 100%;
}
.brand__logo--auth { box-sizing: content-box; height: 46px; margin: 0 auto; }

.sidebar__nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav__item {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 12px; border-radius: var(--radius-sm);
    color: var(--text-soft); text-decoration: none; font-weight: 500;
    transition: background .15s, color .15s;
}
.nav__item:hover { background: var(--surface-2); color: var(--text); }
.nav__item.is-active { background: var(--primary); color: #fff; }
.nav__ico { width: 20px; text-align: center; }
.nav__badge {
    margin-left: auto; background: var(--danger); color: #fff;
    font-size: 11px; font-weight: 700; border-radius: 999px;
    min-width: 20px; text-align: center; padding: 1px 6px;
}

.sidebar__foot { padding-top: 16px; border-top: 1px solid var(--border); }
.agent-chip { display: flex; align-items: center; gap: 10px; }
.agent-chip__avatar {
    width: 36px; height: 36px; border-radius: 50%;
    background: var(--primary); color: #fff;
    display: grid; place-items: center; font-weight: 700;
}
.agent-chip__meta { display: flex; flex-direction: column; }
.agent-chip__name { color: var(--text); font-size: 13px; }
.agent-chip__ext { color: var(--text-soft); font-size: 12px; }
.agent-chip__logout {
    margin-left: auto; color: var(--text-soft); text-decoration: none;
    font-size: 18px; width: 30px; height: 30px; border-radius: 8px;
    display: grid; place-items: center; transition: background .15s, color .15s;
}
.agent-chip__logout:hover { background: rgba(239,68,68,.12); color: var(--danger); }

/* ---------- Main ---------- */
.main { display: flex; flex-direction: column; min-width: 0; }

/* ---------- Topbar ---------- */
.topbar {
    display: flex; align-items: center; gap: 14px;
    padding: 14px 22px; background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.topbar__burger {
    display: none; border: 0; background: transparent;
    font-size: 20px; cursor: pointer; color: var(--text);
}
.search {
    position: relative; flex: 1; max-width: 420px;
}
.search__ico {
    position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
    opacity: .5; font-size: 14px;
}
.search__input {
    width: 100%; padding: 10px 12px 10px 36px;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    background: var(--surface-2); font-size: 14px; outline: none;
    transition: border .15s, box-shadow .15s;
}
.search__input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59,86,240,.15);
    background: #fff;
}
.topbar__filters { display: flex; gap: 10px; margin-left: auto; align-items: center; }

.select {
    padding: 9px 12px; border: 1px solid var(--border);
    border-radius: var(--radius-sm); background: #fff;
    font-size: 13px; color: var(--text); cursor: pointer; outline: none;
}
.select:focus { border-color: var(--primary); }

/* ---------- Buttons ---------- */
.btn {
    border: 0; border-radius: var(--radius-sm); cursor: pointer;
    font-size: 13px; font-weight: 600; padding: 10px 16px;
    transition: background .15s, transform .05s, opacity .15s;
}
.btn:active { transform: translateY(1px); }
.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover { background: var(--primary-600); }
.btn--danger { background: var(--danger); color: #fff; }
.btn--ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn--ghost:hover { background: #eef1f7; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- Views ---------- */
.view { display: none; padding: 22px; flex: 1; min-height: 0; }
.view.is-active { display: flex; flex-direction: column; }

/* ---------- Grid table ---------- */
.grid-wrap {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    overflow: auto; flex: 1;
}
.grid { width: 100%; border-collapse: collapse; min-width: 860px; }
.grid thead th {
    text-align: left; font-size: 12px; text-transform: uppercase;
    letter-spacing: .4px; color: var(--text-soft); font-weight: 700;
    padding: 14px 16px; background: var(--surface-2);
    position: sticky; top: 0; border-bottom: 1px solid var(--border);
}
.grid tbody td { padding: 13px 16px; border-bottom: 1px solid var(--border); }
.grid tbody tr { cursor: pointer; transition: background .12s; }
.grid tbody tr:hover { background: var(--surface-2); }
.grid tbody tr.is-selected { background: rgba(59,86,240,.06); }
.grid .lead-name { font-weight: 600; color: var(--text); }
.ta-right { text-align: right; }

/* Phone cell with inline dial button */
.phone-cell { display: inline-flex; align-items: center; gap: 8px; }

.dial-btn {
    border: 0; border-radius: 50%; width: 30px; height: 30px;
    background: rgba(22,163,74,.12); color: var(--success);
    cursor: pointer; font-size: 15px; line-height: 1;
    display: inline-grid; place-items: center;
    transition: background .15s, transform .1s;
}
.dial-btn:hover { background: var(--success); color: #fff; transform: scale(1.08); }

/* ---------- Badges ---------- */
.badge {
    display: inline-block; padding: 3px 10px; border-radius: 999px;
    font-size: 11px; font-weight: 700; text-transform: capitalize;
    background: var(--surface-2); color: var(--text-soft);
    border: 1px solid var(--border);
}
.badge--stage-new         { background:#eff6ff; color:#2563eb; border-color:#dbeafe; }
.badge--stage-contacted   { background:#f0fdf4; color:#16a34a; border-color:#dcfce7; }
.badge--stage-qualified   { background:#fefce8; color:#ca8a04; border-color:#fef9c3; }
.badge--stage-proposal    { background:#faf5ff; color:#9333ea; border-color:#f3e8ff; }
.badge--stage-negotiation { background:#fff7ed; color:#ea580c; border-color:#ffedd5; }
.badge--stage-won         { background:#ecfdf5; color:#059669; border-color:#d1fae5; }
.badge--stage-lost        { background:#fef2f2; color:#dc2626; border-color:#fee2e2; }

.dot { display:inline-block; width:8px; height:8px; border-radius:50%; margin-right:6px; }
.dot--connected     { background: var(--success); }
.dot--busy          { background: var(--warn); }
.dot--not_connected { background: var(--danger); }
.dot--follow_up     { background: var(--primary); }
.dot--dnd           { background: #6b7280; }
.dot--junk          { background: #111827; }
.dot--fresh         { background: #9ca3af; }
.dot--converted     { background: #059669; }

/* ---------- Pager ---------- */
.pager {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 4px 0;
}
.pager__info { color: var(--text-soft); font-size: 13px; }
.pager__controls { display: flex; gap: 8px; }

/* ---------- Panels / follow-ups ---------- */
.panel {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 22px; flex: 1;
}
.panel__title { margin: 0 0 16px; font-size: 18px; }
.muted { color: var(--text-soft); }

.followups { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.followups li {
    display: flex; align-items: center; gap: 14px;
    padding: 14px 16px; border: 1px solid var(--border);
    border-radius: var(--radius-sm); background: var(--surface-2);
}
.followups .fu__when { font-weight: 700; color: var(--primary); min-width: 150px; }
.followups .fu__name { font-weight: 600; }
.followups .fu__note { color: var(--text-soft); margin-left: auto; }

/* ==========================================================================
   Drawer (lead detail)
   ========================================================================== */
.drawer { position: fixed; inset: 0; z-index: 60; visibility: hidden; }
.drawer.is-open { visibility: visible; }
.drawer__scrim {
    position: absolute; inset: 0; background: rgba(15,23,41,.5);
    opacity: 0; transition: opacity .2s;
}
.drawer.is-open .drawer__scrim { opacity: 1; }
.drawer__panel {
    position: absolute; top: 0; right: 0; height: 100%;
    width: min(520px, 100%); background: var(--surface);
    box-shadow: var(--shadow-lg);
    transform: translateX(100%); transition: transform .25s ease;
    display: flex; flex-direction: column;
}
.drawer.is-open .drawer__panel { transform: translateX(0); }
.drawer__head {
    display: flex; align-items: flex-start; justify-content: space-between;
    padding: 20px 22px; border-bottom: 1px solid var(--border);
}
.drawer__title { margin: 0; font-size: 19px; }
.drawer__sub { margin: 4px 0 0; color: var(--text-soft); font-size: 13px; }
.drawer__close {
    border: 0; background: transparent; font-size: 26px; line-height: 1;
    cursor: pointer; color: var(--text-soft);
}
.drawer__close:hover { color: var(--text); }
.drawer__body { padding: 18px 22px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }

/* ---------- Cards ---------- */
.card {
    border: 1px solid var(--border); border-radius: var(--radius);
    padding: 16px 18px; background: var(--surface);
}
.card__title { margin: 0 0 14px; font-size: 14px; text-transform: uppercase;
    letter-spacing: .4px; color: var(--text-soft); }

.kv__row { display: flex; align-items: center; padding: 8px 0; border-bottom: 1px dashed var(--border); }
.kv__row:last-child { border-bottom: 0; }
.kv__k { width: 120px; color: var(--text-soft); font-size: 13px; }
.kv__v { flex: 1; display: inline-flex; align-items: center; gap: 10px; font-weight: 500; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field--inline { flex-direction: row; align-items: center; gap: 12px; }
.field--inline .field__label { margin: 0; }
.field__label { font-size: 13px; font-weight: 600; color: var(--text); }
.input {
    width: 100%; padding: 10px 12px; border: 1px solid var(--border);
    border-radius: var(--radius-sm); font-size: 14px; font-family: var(--font);
    outline: none; background: #fff; transition: border .15s, box-shadow .15s;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59,86,240,.15); }
.input--sm { width: 120px; }
textarea.input { resize: vertical; }

/* ---------- Disposition chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
    border: 1.5px solid var(--border); background: #fff;
    border-radius: 999px; padding: 8px 14px; cursor: pointer;
    font-size: 13px; font-weight: 600; color: var(--text-soft);
    transition: all .15s;
}
.chip:hover { border-color: var(--chip); color: var(--chip); }
.chip.is-active {
    background: var(--chip); border-color: var(--chip); color: #fff;
}

.form__actions { display: flex; justify-content: flex-end; }

/* ---------- History table ---------- */
.history-wrap { overflow-x: auto; }
.history { width: 100%; border-collapse: collapse; font-size: 13px; }
.history th {
    text-align: left; padding: 8px 10px; color: var(--text-soft);
    font-size: 11px; text-transform: uppercase; border-bottom: 1px solid var(--border);
}
.history td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.history td.notes { color: var(--text-soft); max-width: 160px; }

/* ==========================================================================
   Auth / login screen
   ========================================================================== */
.auth-body {
    min-height: 100vh; display: grid; place-items: center;
    background: linear-gradient(135deg, var(--bg) 0%, var(--bg-2) 100%);
    padding: 24px;
}
.auth-card {
    width: min(400px, 100%); background: var(--surface);
    border-radius: var(--radius); box-shadow: var(--shadow-lg);
    padding: 34px 30px;
}
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 22px; }
.auth-brand__name { font-size: 20px; color: var(--text); }
.auth-brand__name strong { color: var(--primary); }
.auth-title { margin: 0 0 4px; font-size: 22px; }
.auth-sub { margin: 0 0 22px; color: var(--text-soft); font-size: 14px; }
.auth-form { display: flex; flex-direction: column; gap: 16px; }
.auth-submit { width: 100%; padding: 12px; font-size: 15px; margin-top: 4px; }
.auth-error {
    background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca;
    padding: 11px 14px; border-radius: var(--radius-sm); font-size: 13px;
    margin-bottom: 18px;
}

/* ==========================================================================
   Modal (new / edit lead)
   ========================================================================== */
.modal { position: fixed; inset: 0; z-index: 70; visibility: hidden; }
.modal.is-open { visibility: visible; }
.modal__scrim {
    position: absolute; inset: 0; background: rgba(15,23,41,.5);
    opacity: 0; transition: opacity .2s;
}
.modal.is-open .modal__scrim { opacity: 1; }
.modal__dialog {
    position: absolute; left: 50%; top: 50%;
    transform: translate(-50%, -46%) scale(.98);
    width: min(640px, calc(100% - 32px)); max-height: calc(100vh - 48px);
    background: var(--surface); border-radius: var(--radius);
    box-shadow: var(--shadow-lg); display: flex; flex-direction: column;
    opacity: 0; transition: opacity .2s, transform .2s;
}
.modal.is-open .modal__dialog { transform: translate(-50%, -50%) scale(1); opacity: 1; }
.modal__head {
    flex: 0 0 auto;
    display: flex; align-items: center; justify-content: space-between;
    padding: 18px 22px; border-bottom: 1px solid var(--border);
}
.modal__title { margin: 0; font-size: 18px; }
.modal__close {
    border: 0; background: transparent; font-size: 26px; line-height: 1;
    cursor: pointer; color: var(--text-soft);
}
.modal__close:hover { color: var(--text); }
/* Body is the only scrollable region; header/footer stay fixed as flex rows. */
.modal__body { flex: 1 1 auto; min-height: 0; padding: 20px 22px; overflow-y: auto; }
.modal__foot {
    flex: 0 0 auto;
    display: flex; justify-content: flex-end; gap: 10px;
    padding: 16px 22px; border-top: 1px solid var(--border);
    background: var(--surface);
}

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.field--full { grid-column: 1 / -1; }
.req { color: var(--danger); }
.input:invalid:not(:placeholder-shown) { border-color: var(--danger); }

/* Compact confirm dialog (click-to-call) */
.modal__dialog--sm { width: min(420px, calc(100% - 32px)); }
.confirm-lead { margin: 0 0 14px; font-size: 15px; }
.confirm-explain {
    background: #eff4ff; border: 1px solid #dbe4ff; color: #33459c;
    border-radius: var(--radius-sm); padding: 12px 14px; font-size: 13px;
    margin-bottom: 18px; line-height: 1.5;
}
.field__hint { color: var(--text-soft); font-size: 12px; margin-top: 2px; }

@media (max-width: 560px) {
    .form-grid { grid-template-columns: 1fr; }
}

/* ==========================================================================
   Dialer popup
   ========================================================================== */
.dialer {
    position: fixed; right: 24px; bottom: 24px; z-index: 80;
    transform: translateY(24px); opacity: 0; pointer-events: none;
    transition: transform .2s, opacity .2s;
}
.dialer.is-open { transform: translateY(0); opacity: 1; pointer-events: auto; }
.dialer__card {
    width: 280px; background: var(--surface); border-radius: var(--radius);
    box-shadow: var(--shadow-lg); padding: 22px; text-align: center;
    border: 1px solid var(--border);
}
.dialer__pulse { display: grid; place-items: center; margin-bottom: 14px; }
.dialer__pulse span {
    width: 54px; height: 54px; border-radius: 50%;
    background: var(--success); color: #fff;
    display: grid; place-items: center; font-size: 24px;
    position: relative;
}
.dialer__pulse span::before { content: "\260E"; }
.dialer.is-ringing .dialer__pulse span { animation: pulse 1.2s infinite; }
@keyframes pulse {
    0%   { box-shadow: 0 0 0 0 rgba(22,163,74,.5); }
    70%  { box-shadow: 0 0 0 16px rgba(22,163,74,0); }
    100% { box-shadow: 0 0 0 0 rgba(22,163,74,0); }
}
.dialer__state { margin: 0 0 4px; font-weight: 700; }
.dialer__number { margin: 0 0 8px; color: var(--text-soft); font-size: 14px; }
.dialer__timer { font-size: 26px; font-weight: 700; letter-spacing: 1px; margin-bottom: 16px; }
.dialer__actions { display: flex; gap: 8px; }
.dialer__actions .btn { flex: 1; }

/* ---------- Spinner / loading ---------- */
.spinner {
    display: inline-block; width: 16px; height: 16px; vertical-align: middle;
    border: 2px solid var(--border); border-top-color: var(--primary);
    border-radius: 50%; animation: spin .7s linear infinite; margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.grid__loading, .grid__empty { padding: 40px; text-align: center; color: var(--text-soft); }

/* ---------- Toast ---------- */
.toast {
    position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
    background: var(--bg); color: #fff; padding: 12px 20px; border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg); font-size: 14px; z-index: 100; opacity: 0;
    transition: opacity .2s, transform .2s;
}
.toast.is-show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.is-error { background: var(--danger); }
.toast.is-success { background: var(--success); }

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 900px) {
    .app { grid-template-columns: 1fr; }
    .sidebar {
        position: fixed; left: 0; top: 0; z-index: 90;
        width: var(--sidebar-w); transform: translateX(-100%);
        transition: transform .25s;
    }
    .sidebar.is-open { transform: translateX(0); box-shadow: var(--shadow-lg); }
    .topbar__burger { display: block; }
    .topbar__filters { flex-wrap: wrap; }
    .topbar__filters .select { flex: 1; min-width: 120px; }
}
@media (max-width: 560px) {
    .topbar { flex-wrap: wrap; }
    .search { max-width: none; order: 3; flex-basis: 100%; }
    .drawer__panel { width: 100%; }
}
