:root {
    --bg: #f3f5f8;
    --surface: #ffffff;
    --surface-soft: #f8fafc;
    --text: #162033;
    --muted: #6b7485;
    --border: #e4e8ef;
    --primary: #5b50e6;
    --primary-dark: #473ccf;
    --sidebar: #111827;
    --sidebar-soft: #202a3b;
    --success: #159769;
    --danger: #d84e5d;
    --warning: #d78b19;
    --shadow: 0 12px 35px rgba(25, 35, 58, .07);
    --radius: 18px;
}

* {
    box-sizing: border-box;
}

html {
    color-scheme: light;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 15px;
    line-height: 1.5;
}

a {
    color: inherit;
    text-decoration: none;
}

button,
input,
select {
    font: inherit;
}

button,
select {
    cursor: pointer;
}

code,
pre {
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

code {
    padding: .12rem .36rem;
    border-radius: 5px;
    background: #edf0f6;
    color: #3f4860;
    font-size: .88em;
}

pre {
    overflow-x: auto;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-soft);
    color: #374151;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
}

.app-shell {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
    min-height: 100vh;
}

.sidebar {
    position: sticky;
    top: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    padding: 28px 20px 20px;
    background: var(--sidebar);
    color: #d9dfeb;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-mark {
    display: grid;
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    place-items: center;
    border-radius: 12px;
    background: linear-gradient(135deg, #756bf4, #4b40d1);
    color: #fff;
    font-size: 20px;
    font-weight: 800;
    box-shadow: 0 8px 22px rgba(91, 80, 230, .35);
}

.brand strong,
.brand small {
    display: block;
}

.brand strong {
    color: #fff;
    font-size: 17px;
    letter-spacing: -.01em;
}

.brand small {
    margin-top: 1px;
    color: #8f9aae;
    font-size: 11px;
}

.nav {
    display: grid;
    gap: 7px;
    margin-top: 42px;
}

.nav a {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 11px 13px;
    border-radius: 10px;
    color: #aeb8c9;
    font-weight: 600;
    transition: background .18s ease, color .18s ease;
}

.nav a span {
    width: 22px;
    color: #7f8ba0;
    font-size: 20px;
    text-align: center;
}

.nav a:hover,
.nav a.active {
    background: var(--sidebar-soft);
    color: #fff;
}

.nav a.active span {
    color: #9d96ff;
}

.sidebar-footer {
    margin-top: auto;
}

.main {
    min-width: 0;
    padding: 34px clamp(22px, 3.5vw, 54px) 54px;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    margin-bottom: 28px;
}

.topbar h1,
.section-heading h2,
.auth-card h1 {
    margin: 0;
    letter-spacing: -.035em;
}

.topbar h1 {
    font-size: clamp(27px, 3vw, 36px);
}

.eyebrow {
    margin: 0 0 4px;
    color: var(--primary);
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .14em;
    text-transform: uppercase;
}

.button {
    display: inline-flex;
    min-height: 42px;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 16px;
    border: 1px solid transparent;
    border-radius: 10px;
    font-weight: 700;
    line-height: 1.1;
    transition: transform .16s ease, border-color .16s ease, background .16s ease;
}

.button:hover {
    transform: translateY(-1px);
}

.button-primary {
    background: var(--primary);
    color: #fff;
    box-shadow: 0 8px 20px rgba(91, 80, 230, .21);
}

.button-primary:hover {
    background: var(--primary-dark);
}

.button-secondary {
    border-color: #d9deea;
    background: #fff;
    color: #455068;
}

.button-success {
    background: #e4f7f0;
    color: #087a52;
}

.button-ghost {
    border-color: #dce1ea;
    background: transparent;
    color: var(--muted);
}

.sidebar .button-ghost {
    border-color: #2e394d;
    color: #aeb8c9;
}

.button-danger-ghost {
    border-color: #f0d8dc;
    background: #fff;
    color: var(--danger);
}

.button-small {
    min-height: 34px;
    padding: 7px 11px;
    font-size: 13px;
}

.button-full {
    width: 100%;
}

.button-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 22px;
}

.alert {
    margin: 0 0 20px;
    padding: 13px 16px;
    border: 1px solid transparent;
    border-radius: 11px;
    font-weight: 600;
}

.alert-success {
    border-color: #bce8d7;
    background: #e9f8f2;
    color: #0a704c;
}

.alert-error {
    border-color: #f0c9cf;
    background: #fff0f2;
    color: #a53242;
}

.alert-warning {
    border-color: #efd9ad;
    background: #fff8e9;
    color: #8c5a0f;
}

.alert-scan {
    display: flex;
    align-items: center;
    gap: 11px;
    border-color: #d8d3ff;
    background: #f1efff;
    color: #4c42bd;
}

.scan-spinner {
    width: 17px;
    height: 17px;
    border: 2px solid rgba(91, 80, 230, .22);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: scan-spin .8s linear infinite;
}

@keyframes scan-spin {
    to { transform: rotate(360deg); }
}

.metric-grid {
    display: grid;
    grid-template-columns: repeat(5, minmax(150px, 1fr));
    gap: 14px;
    margin-bottom: 22px;
}

.metric-card {
    display: flex;
    min-width: 0;
    align-items: flex-start;
    gap: 12px;
    padding: 20px 18px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
    transition: transform .17s ease, box-shadow .17s ease;
}

.metric-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 15px 38px rgba(25, 35, 58, .1);
}

.metric-card > div {
    min-width: 0;
}

.metric-card strong,
.metric-card small,
.metric-label {
    display: block;
}

.metric-card strong {
    margin: 5px 0 3px;
    font-size: 30px;
    letter-spacing: -.04em;
}

.metric-card small {
    overflow: hidden;
    color: var(--muted);
    font-size: 11px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.metric-label {
    color: #465066;
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
}

.metric-dot {
    flex: 0 0 10px;
    width: 10px;
    height: 10px;
    margin-top: 5px;
    border-radius: 50%;
    background: #aab2bf;
    box-shadow: 0 0 0 5px #f0f2f6;
}

.metric-hot .metric-dot { background: #e14d5b; box-shadow: 0 0 0 5px #fcecef; }
.metric-new .metric-dot { background: #5b50e6; box-shadow: 0 0 0 5px #eeecff; }
.metric-warm .metric-dot { background: #e39a27; box-shadow: 0 0 0 5px #fdf2df; }
.metric-skip .metric-dot { background: #8a94a5; }
.metric-total .metric-dot { background: #1ba377; box-shadow: 0 0 0 5px #e5f7f1; }

.panel-card {
    min-width: 0;
    margin-bottom: 22px;
    padding: 24px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.section-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 20px;
}

.section-heading.compact {
    margin-bottom: 18px;
}

.section-heading h2 {
    font-size: 21px;
}

.count-label {
    display: inline-block;
    margin-left: 5px;
    color: var(--muted);
    font-size: 14px;
    font-weight: 600;
    vertical-align: middle;
}

.text-link {
    color: var(--primary);
    font-weight: 700;
}

.muted,
.page-indicator,
.form-help {
    color: var(--muted);
}

.page-indicator {
    font-size: 13px;
    font-weight: 600;
}

.table-wrap {
    overflow-x: auto;
    margin: 0 -24px -10px;
    padding: 0 24px 10px;
}

table {
    width: 100%;
    border-collapse: collapse;
    white-space: nowrap;
}

th,
td {
    padding: 14px 15px;
    border-bottom: 1px solid #edf0f4;
    text-align: left;
    vertical-align: middle;
}

th:first-child,
td:first-child {
    padding-left: 0;
}

th:last-child,
td:last-child {
    padding-right: 0;
}

th {
    color: #828b9b;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: .09em;
    text-transform: uppercase;
}

.sort-link {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    color: inherit;
}

.sort-link:hover {
    color: var(--primary);
}

.sort-link span {
    color: #a3aaba;
    font-size: 12px;
}

td {
    color: #414a5e;
    font-size: 13px;
}

tbody tr:last-child td {
    border-bottom: 0;
}

td strong {
    color: var(--text);
}

.cell-subtitle {
    display: block;
    max-width: 260px;
    overflow: hidden;
    margin-top: 3px;
    color: var(--muted);
    font-size: 11px;
    text-overflow: ellipsis;
}

.lead-name-cell {
    min-width: 220px;
    white-space: normal;
}

.contact-line {
    display: block;
    margin-bottom: 4px;
}

.rating {
    color: #9b6a09;
    font-weight: 700;
}

.score-pill,
.status-pill,
.segment,
.connection-badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    font-weight: 800;
}

.score-pill {
    min-width: 34px;
    justify-content: center;
    padding: 5px 9px;
    background: #eeecff;
    color: #4c42c8;
}

.status-pill {
    padding: 5px 9px;
    background: #f0f3f7;
    color: #566175;
    font-size: 11px;
    text-transform: capitalize;
}

.segment {
    padding: 5px 9px;
    font-size: 10px;
    letter-spacing: .025em;
    text-transform: uppercase;
}

.segment-hot { background: #fdebed; color: #b53241; }
.segment-new-business-candidate { background: #eeecff; color: #5045c8; }
.segment-warm { background: #fff2dc; color: #9c6409; }
.segment-skip { background: #eff1f5; color: #667084; }
.segment-default { background: #edf4f6; color: #536a70; }

.lead-state {
    display: inline-flex;
    align-items: center;
    padding: 5px 9px;
    border-radius: 999px;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: .025em;
    text-transform: uppercase;
}

.lead-state-interested { background: #e4f7f0; color: #087a52; }
.lead-state-archived { background: #f3f4f6; color: #747d8d; }
.lead-state-default { background: #f0f2f6; color: #697386; }

.lead-row-archived {
    opacity: .68;
    background: #fafbfc;
}

.lead-actions-cell {
    min-width: 210px;
}

.lead-action-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.lead-action-buttons form {
    margin: 0;
}

.empty-state {
    padding: 48px 20px;
    color: var(--muted);
    text-align: center;
}

.empty-state > span {
    display: block;
    margin-bottom: 10px;
    color: #abb3c2;
    font-size: 36px;
}

.empty-state h3,
.empty-state p {
    margin: 0;
}

.empty-state h3 {
    color: #3f485a;
}

.empty-state p {
    margin-top: 5px;
}

.filters-card {
    padding: 18px 20px;
}

.filters-form {
    display: grid;
    grid-template-columns: minmax(180px, 1.3fr) repeat(4, minmax(115px, .75fr)) minmax(120px, .8fr) 105px auto;
    align-items: end;
    gap: 11px;
}

label {
    display: block;
    color: #414b5f;
    font-size: 12px;
    font-weight: 750;
}

label > span:first-child {
    display: block;
    margin-bottom: 6px;
}

input,
select {
    width: 100%;
    min-height: 42px;
    padding: 9px 11px;
    border: 1px solid #dbe0e9;
    border-radius: 9px;
    outline: none;
    background: #fff;
    color: #263044;
    transition: border .15s ease, box-shadow .15s ease;
}

input:focus,
select:focus {
    border-color: #8b82ef;
    box-shadow: 0 0 0 3px rgba(91, 80, 230, .12);
}

input[readonly] {
    background: #f4f6f9;
    color: #6d7686;
}

select:disabled {
    cursor: not-allowed;
    opacity: .55;
}

.filter-actions {
    display: flex;
    gap: 7px;
}

.pagination {
    display: flex;
    justify-content: center;
    gap: 6px;
    margin-top: 22px;
}

.pagination a {
    display: grid;
    width: 37px;
    height: 37px;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 9px;
    background: #fff;
    color: #5d6677;
    font-weight: 700;
}

.pagination a.active {
    border-color: var(--primary);
    background: var(--primary);
    color: #fff;
}

.pagination a.disabled {
    pointer-events: none;
    opacity: .4;
}

.two-column-layout {
    display: grid;
    grid-template-columns: minmax(280px, .72fr) minmax(420px, 1.5fr);
    align-items: start;
    gap: 22px;
}

.form-card {
    position: sticky;
    top: 24px;
}

.stack-form {
    display: grid;
    gap: 15px;
}

.stack-form .button {
    margin-top: 3px;
}

.form-help {
    margin: 16px 0 0;
    font-size: 12px;
}

.query-list {
    display: grid;
    gap: 9px;
}

.query-item {
    display: grid;
    grid-template-columns: 10px minmax(0, 1fr) auto;
    align-items: center;
    gap: 14px;
    padding: 15px 16px;
    border: 1px solid #e7eaf0;
    border-radius: 12px;
    background: #fff;
}

.query-item.is-disabled {
    background: #f7f8fa;
    opacity: .75;
}

.query-status {
    width: 9px;
    height: 9px;
    border-radius: 50%;
}

.query-status.enabled { background: var(--success); box-shadow: 0 0 0 4px #e4f7f0; }
.query-status.disabled { background: #9ba3b0; box-shadow: 0 0 0 4px #eceef2; }

.query-copy {
    min-width: 0;
}

.query-copy strong,
.query-copy span {
    display: block;
}

.query-copy strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.query-copy span {
    margin-top: 2px;
    color: var(--muted);
    font-size: 11px;
}

.query-actions {
    display: flex;
    gap: 7px;
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 22px;
}

.settings-grid > .panel-card {
    height: calc(100% - 22px);
}

.switch-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 11px;
    background: var(--surface-soft);
}

.switch-row span,
.switch-row strong,
.switch-row small {
    display: block;
}

.switch-row small {
    margin-top: 3px;
    color: var(--muted);
    font-weight: 500;
}

.switch-row input {
    width: 22px;
    min-height: 22px;
    accent-color: var(--primary);
}

.connection-badge {
    padding: 5px 9px;
    font-size: 10px;
    text-transform: uppercase;
}

.connection-badge.connected { background: #e5f7f1; color: #0c7953; }
.connection-badge.disconnected { background: #fff1e5; color: #9b5c18; }

.inline-setting-form {
    display: grid;
    grid-template-columns: minmax(180px, .8fr) minmax(220px, 1.4fr) auto;
    align-items: end;
    gap: 12px;
    width: 100%;
}

.add-setting-form {
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border);
}

.settings-list {
    display: grid;
    gap: 12px;
    margin-top: 18px;
}

.setting-row {
    display: flex;
    align-items: end;
    gap: 10px;
}

.block-note {
    margin: 18px 0 0;
}

.auth-page {
    display: grid;
    min-height: 100vh;
    padding: 24px;
    place-items: center;
    background:
        radial-gradient(circle at 15% 20%, rgba(91, 80, 230, .13), transparent 28%),
        radial-gradient(circle at 85% 80%, rgba(24, 158, 114, .10), transparent 25%),
        var(--bg);
}

.auth-card {
    width: min(100%, 430px);
    min-width: 0;
    padding: 34px;
    border: 1px solid var(--border);
    border-radius: 22px;
    background: #fff;
    box-shadow: 0 25px 70px rgba(23, 33, 55, .12);
}

.auth-card .brand strong {
    color: var(--text);
}

.auth-heading {
    margin: 32px 0 22px;
}

.auth-heading h1,
.setup-card h1 {
    font-size: 30px;
}

.auth-heading p:last-child,
.setup-card > p:not(.eyebrow) {
    margin: 8px 0 0;
    color: var(--muted);
}

.setup-card {
    width: min(100%, 560px);
}

.status-icon {
    display: grid;
    width: 48px;
    height: 48px;
    margin-bottom: 22px;
    place-items: center;
    border-radius: 14px;
    font-size: 23px;
    font-weight: 900;
}

.status-icon.warning {
    background: #fff1d8;
    color: #a3650a;
}

@media (max-width: 1280px) {
    .metric-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .filters-form {
        grid-template-columns: repeat(4, minmax(140px, 1fr));
    }

    .filter-search {
        grid-column: span 2;
    }
}

@media (max-width: 900px) {
    .app-shell {
        display: block;
    }

    .sidebar {
        position: static;
        display: block;
        height: auto;
        padding: 18px 20px;
    }

    .nav {
        display: flex;
        overflow-x: auto;
        margin: 18px -5px 0;
        padding: 0 5px 2px;
    }

    .nav a {
        flex: 0 0 auto;
    }

    .sidebar-footer {
        display: none;
    }

    .main {
        padding-top: 25px;
    }

    .two-column-layout,
    .settings-grid {
        grid-template-columns: 1fr;
    }

    .form-card {
        position: static;
    }
}

@media (max-width: 680px) {
    .main {
        padding: 22px 14px 40px;
    }

    .topbar {
        align-items: stretch;
        flex-direction: column;
    }

    .scan-button {
        width: 100%;
    }

    .metric-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .metric-card {
        padding: 17px 14px;
    }

    .panel-card {
        padding: 19px 16px;
        border-radius: 15px;
    }

    .table-wrap {
        margin-right: -16px;
        margin-left: -16px;
        padding-right: 16px;
        padding-left: 16px;
    }

    .filters-form,
    .inline-setting-form {
        grid-template-columns: 1fr;
    }

    .filter-search {
        grid-column: auto;
    }

    .filter-actions .button {
        flex: 1;
    }

    .query-item {
        grid-template-columns: 10px minmax(0, 1fr);
    }

    .query-actions {
        grid-column: 2;
    }

    .setting-row {
        align-items: stretch;
        flex-direction: column;
    }

    .section-heading {
        align-items: flex-start;
    }
}

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

    .auth-card {
        padding: 25px 21px;
    }
}
