:root {
    --bg: #f3f5f7;
    --surface: #ffffff;
    --ink: #1c2430;
    --muted: #5e6875;
    --line: #e4e7eb;
    --accent: #0f3d4c;
    --alert: #8a3b3b;
    --alert-bg: #f8f1f1;
    --shadow: 0 10px 30px rgba(20, 28, 36, 0.08);
    --sidebar: 216px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }
body {
    font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    line-height: 1.45;
    color: var(--ink);
    background: var(--bg);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, textarea { font: inherit; color: inherit; }
h1, h2, p { margin: 0; }
h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.02em; }
h2 { font-size: 15px; font-weight: 600; }

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.skip {
    position: absolute;
    left: 12px;
    top: -40px;
    background: #fff;
    padding: 6px 10px;
    z-index: 50;
}
.skip:focus { top: 8px; }

.app {
    display: grid;
    grid-template-columns: var(--sidebar) 1fr;
    min-height: 100vh;
}

.sidebar {
    background: var(--surface);
    border-right: 1px solid var(--line);
    padding: 16px 12px;
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.brand {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 8px 14px;
    font-weight: 650;
}
.brand-mark {
    width: 16px;
    height: 16px;
    border-radius: 4px;
    background: var(--accent);
    flex: 0 0 auto;
}
.brand-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-height: 0;
    flex: 1;
}
.nav-link {
    display: block;
    padding: 8px 10px;
    border-radius: 8px;
    color: var(--ink);
    text-decoration: none;
}
.nav-link:hover { background: #f4f6f8; text-decoration: none; }
.nav-link.is-active { background: #eef3f5; color: var(--accent); font-weight: 600; }
.nav-end { margin-top: auto; }

.workspace { min-width: 0; }
.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 56px;
    padding: 10px 16px;
    background: rgba(255, 255, 255, 0.96);
    border-bottom: 1px solid var(--line);
}
.nav-toggle, .nav-backdrop { display: none; }

.search {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 440px;
    background: #f3f5f7;
    border: 1px solid transparent;
    border-radius: 8px;
    padding: 0 10px;
}
.search:focus-within { border-color: #c5d0d6; background: #fff; }
.search input {
    width: 100%;
    border: 0;
    background: transparent;
    padding: 8px 0;
    outline: none;
}
.search-icon { color: var(--muted); display: flex; }

.user-name {
    margin-left: auto;
    color: var(--muted);
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.main { padding: 20px 22px 48px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; margin-top: 2px; }
.note { color: var(--muted); margin: 0 0 12px; }
.back { margin: 0 0 10px; }

.banner {
    background: #f6f3ea;
    border: 1px solid #e6dcc4;
    color: #5c4a28;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 14px;
}
.banner a { color: inherit; font-weight: 600; }

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
    gap: 10px;
    margin-bottom: 16px;
}
.stat {
    display: flex;
    flex-direction: column;
    gap: 2px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 10px 12px;
    min-height: 72px;
    color: inherit;
    text-decoration: none;
}
a.stat:hover { border-color: #c9d2d8; text-decoration: none; }
.stat-value {
    font-size: 22px;
    font-weight: 600;
    letter-spacing: -0.03em;
    font-variant-numeric: tabular-nums;
}
.stat-label { color: var(--muted); font-size: 12px; }
.stat-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.stat-alert { background: var(--alert-bg); border-color: #eadada; }
.stat-alert .stat-value { color: var(--alert); }

.stack { display: flex; flex-direction: column; gap: 16px; }
.split {
    display: grid;
    grid-template-columns: minmax(0, 1.4fr) minmax(280px, 0.8fr);
    gap: 16px;
}
.panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px 16px;
    scroll-margin-top: 72px;
}
.panel h2 { margin-bottom: 12px; }
.count {
    display: inline-block;
    margin-left: 6px;
    color: var(--muted);
    font-weight: 500;
}

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; }
.table th {
    text-align: left;
    font-size: 11px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--muted);
    font-weight: 600;
    padding: 0 10px 8px;
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}
.table td {
    padding: 10px;
    border-bottom: 1px solid #eef0f2;
    vertical-align: middle;
}
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: #fafbfc; }
.table-wide { min-width: 760px; }
.clip {
    display: inline-block;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: bottom;
}

.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 32px;
    padding: 0 10px;
    border-radius: 6px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
}
.btn:hover { background: #f7f8f9; text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #16485a; }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: default; }
.phone { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }

.badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 7px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    background: #eef1f4;
    color: #3a4450;
    white-space: nowrap;
}
.badge[data-k="NEW"] { background: #e8eef5; color: #243e5a; }
.badge[data-k="CONTACTED"] { background: #eef1f4; color: #3a4450; }
.badge[data-k="QUALIFIED"] { background: #e7f0ea; color: #24523a; }
.badge[data-k="SITE_VISIT"] { background: #e7eef8; color: #2a4568; }
.badge[data-k="NEGOTIATION"] { background: #f6f1e6; color: #6d5424; }
.badge[data-k="BOOKED"] { background: #e8f2ef; color: #1f5a48; }
.badge[data-k="CLOSED"] { background: #e5f0e8; color: #1e5c34; }
.badge[data-k="LOST"],
.badge[data-k="DUE"] { background: #f6eaea; color: #7a3333; }
.badge[data-k="NURTURE"] { background: #f1eef5; color: #534266; }
.badge[data-k="HOT"] { background: #f8ece8; color: #8a3b2f; }
.badge[data-k="WARM"] { background: #f7f3e8; color: #7a5b28; }
.badge[data-k="COLD"] { background: #eef2f5; color: #3e5263; }
.badge[data-k="OPEN"] { background: #eef1f4; color: #3a4450; }

.empty { color: var(--muted); margin: 0; }
.is-overdue { color: var(--alert); font-weight: 600; }

.activity { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.activity li { padding-bottom: 12px; border-bottom: 1px solid #eef0f2; }
.activity li:last-child { border-bottom: 0; padding-bottom: 0; }
.activity p { margin-top: 3px; }
.activity-top { display: flex; justify-content: space-between; gap: 8px; }
.activity time { color: var(--muted); font-size: 12px; white-space: nowrap; }

.lead-title { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.lead-phone { font-size: 18px; margin: 8px 0 14px; }
.lead-hero .actions { margin: 14px 0 10px; }
.meta { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px 16px; margin: 0; }
.meta dt { font-size: 12px; color: var(--muted); }
.meta dd { margin: 2px 0 0; font-weight: 600; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field label { font-size: 13px; font-weight: 600; }
.field input, .field textarea, .field select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 10px;
    background: #fff;
}
.field input:focus, .field textarea:focus, .field select:focus { outline: 2px solid #d5e0e6; border-color: #b7c6ce; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.sheet-table { margin-top: 16px; }
.sheet-rename { display: flex; gap: 8px; align-items: center; }
.sheet-rename input { min-width: 0; }
.sheet-url { display: block; max-width: 280px; margin-top: 6px; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-actions { display: flex; gap: 8px; justify-content: flex-end; }
.sheet-actions form { margin: 0; }
.sheet-error { display: block; color: var(--alert); margin-top: 4px; }
.modal-wide { width: min(560px, 100%); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.credential { font-family: ui-monospace, Consolas, monospace; font-weight: 700; }
.sheet-link { display: flex; gap: 8px; align-items: center; margin: 0 0 12px; }
.sheet-link input { flex: 1; min-width: 0; }
.sheet td { padding: 0; vertical-align: top; }
.sheet input, .sheet select, .sheet textarea {
    width: 100%;
    border: 0;
    background: transparent;
    padding: 8px;
    min-height: 36px;
    border-radius: 0;
}
.sheet textarea { min-width: 180px; resize: vertical; }
.sheet select { min-width: 120px; }
.sheet input:focus, .sheet select:focus, .sheet textarea:focus { outline: 2px solid #d5e0e6; background: #fff; }
.sheet .is-saving { background: #f6f3ea; }
.sheet .sheet-readonly { padding: 8px; white-space: nowrap; }
.form-error { color: var(--alert); background: var(--alert-bg); border-radius: 8px; padding: 8px 10px; margin: 12px 0; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; }

.modal {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: grid;
    place-items: center;
    padding: 16px;
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(20, 24, 28, 0.45);
    border: 0;
    padding: 0;
}
.modal-card {
    position: relative;
    width: min(440px, 100%);
    background: #fff;
    border-radius: 12px;
    padding: 18px;
    box-shadow: var(--shadow);
}
.modal-card h2 { margin-bottom: 4px; }
.modal-lead { color: var(--muted); margin-bottom: 12px; }

.chat-dock { position: fixed; right: 16px; bottom: 16px; z-index: 55; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.chat-panel { width: min(320px, calc(100vw - 32px)); height: min(420px, calc(100vh - 96px)); background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); display: flex; flex-direction: column; overflow: hidden; }
.chat-head { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.chat-form { padding: 10px; border-top: 1px solid var(--line); display: flex; gap: 8px; }
.chat-form input { flex: 1; min-width: 0; }
.chat-panel select { margin: 8px 10px 0; }
.chat-log { flex: 1; overflow: auto; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.chat-line { display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
.chat-line.is-mine { align-items: flex-end; }
.chat-bubble { max-width: 85%; background: #f3f5f6; border-radius: 8px; padding: 6px 8px; white-space: pre-wrap; word-break: break-word; }
.chat-line.is-mine .chat-bubble { background: #e6f0f3; }
.chat-line time { color: var(--muted); font-size: 11px; }
.chat-badge { display: inline-block; min-width: 16px; margin-left: 4px; padding: 0 5px; border-radius: 99px; background: #fff; color: var(--accent); font-size: 11px; }
.chat-toggle { box-shadow: var(--shadow); }

.toaster {
    position: fixed;
    right: 16px;
    bottom: 64px;
    z-index: 60;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    width: min(320px, calc(100vw - 32px));
    background: #fff;
    border: 1px solid var(--line);
    border-left: 3px solid var(--accent);
    border-radius: 8px;
    padding: 10px 12px;
    box-shadow: var(--shadow);
}
.toast-error { border-left-color: var(--alert); }
.toast-info { border-left-color: #8a7340; }

.auth-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px 16px;
}
.auth-card {
    width: min(400px, 100%);
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 28px 24px;
}
.auth-kicker {
    font-size: 12px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 6px;
}
.auth-card h1 { margin-bottom: 6px; }
.auth-card .muted { margin-bottom: 16px; }
.auth-card form { margin-top: 8px; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 980px) {
    .split { grid-template-columns: 1fr; }
    .form-grid { grid-template-columns: 1fr; }
}

@media (max-width: 860px) {
    .app { grid-template-columns: 1fr; }
    .sidebar {
        position: fixed;
        z-index: 30;
        width: min(280px, 86vw);
        transform: translateX(-105%);
        transition: transform 0.15s ease;
    }
    .sidebar.is-open { transform: none; }
    .nav-toggle {
        display: inline-flex;
    }
    .nav-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(16, 20, 24, 0.35);
        border: 0;
        z-index: 25;
        padding: 0;
    }
    .topbar { flex-wrap: wrap; }
    .search { flex: 1 1 100%; max-width: none; order: 5; }
    .user-name { margin-left: 0; }
    .main { padding: 14px 12px 40px; }
}

@media (max-width: 720px) {
    .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .stat-wide { grid-column: span 2; }
    .table-followups thead { display: none; }
    .table-followups,
    .table-followups tbody,
    .table-followups tr,
    .table-followups td { display: block; width: 100%; }
    .table-followups tr {
        border: 1px solid var(--line);
        border-radius: 8px;
        padding: 10px;
        margin-bottom: 8px;
    }
    .table-followups td { border: 0; padding: 3px 0; }
    .table-followups tbody tr:hover td { background: transparent; }
    .table-followups td[data-label]::before {
        content: attr(data-label);
        display: block;
        font-size: 11px;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        color: var(--muted);
    }
    .table-followups td.actions-cell::before { content: none; }
    .clip { max-width: none; white-space: normal; }
    .actions .btn { min-height: 40px; }
    .toaster { left: 16px; right: 16px; }
    .toast { width: auto; }
}

@media (prefers-reduced-motion: reduce) {
    .sidebar { transition: none; }
}
