:root {
    --ink: #182620;
    --ink-muted: #6b7770;
    --line: #dfe4e0;
    --bg: #f4f6f4;
    --surface: #ffffff;
    --accent: #2f6b4f;
    --accent-ink: #1c4531;
    --accent-soft: #e7f1ec;
    --warn-bg: #fdf2e3;
    --warn-ink: #8a5a12;
    --danger: #b33b3b;
    --danger-soft: #fbeaea;
    --shadow: 0 1px 2px rgba(24,38,32,.04), 0 6px 20px -10px rgba(24,38,32,.12);
    --sidebar-w: 224px;
}
* { box-sizing: border-box; }
body {
    margin: 0; background: var(--bg); color: var(--ink);
    font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-ink); }
button { font-family: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ===== app shell ===== */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: var(--sidebar-w); flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--line);
    display: flex; flex-direction: column; gap: 20px; padding: 20px 14px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); padding: 0 6px; }
.brand-mark {
    width: 34px; height: 34px; border-radius: 9px; background: var(--ink); color: #fff; flex-shrink: 0;
    display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; letter-spacing: .01em;
}
.brand-word { font-size: 13.5px; font-weight: 700; line-height: 1.15; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
    display: flex; align-items: center; gap: 12px; padding: 9px 10px; border-radius: 8px;
    color: var(--ink-muted); text-decoration: none; font-size: 14px; position: relative;
}
.nav-icon { width: 19px; height: 19px; flex-shrink: 0; }
.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
a.nav-item:hover:not(.active) { background: var(--bg); color: var(--ink); }
.nav-item.disabled { color: #b7bfba; cursor: default; }
.soon-badge {
    margin-left: auto; font-style: normal; font-size: 9.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
    color: #9aa39c; background: var(--bg); border-radius: 99px; padding: 2px 6px;
}

.account-block { display: flex; align-items: center; gap: 10px; padding: 8px 6px 2px; border-top: 1px solid var(--line); }
.avatar {
    width: 30px; height: 30px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-ink);
    display: flex; align-items: center; justify-content: center; font-size: 12.5px; font-weight: 700; flex-shrink: 0;
}
.account-block .me { font-size: 13px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout-btn {
    width: 30px; height: 30px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface);
    color: var(--ink-muted); display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0;
}
.logout-btn:hover { background: var(--bg); color: var(--danger); border-color: var(--danger-soft); }
.logout-btn svg { width: 16px; height: 16px; }

.app-main { flex: 1; min-width: 0; }
.topbar-mobile { display: none; }

.shell { max-width: 880px; margin: 0 auto; padding: 32px 24px 64px; }
.shell-bare { max-width: none; padding: 0; }
.page-head { margin-bottom: 22px; }
.page-title { font-size: 24px; font-weight: 700; margin: 0; letter-spacing: -.01em; }
.page-sub { margin: 2px 0 0; color: var(--ink-muted); font-size: 13.5px; text-transform: capitalize; }

/* ===== stat tiles ===== */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 22px; }
.stat-tile {
    background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px;
    box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 2px;
}
.stat-num { font-size: 28px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12.5px; color: var(--ink-muted); }

/* ===== cards / lists ===== */
.card-block { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card-block h2 { font-size: 14.5px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
.count { background: var(--accent); color: #fff; border-radius: 99px; font-size: 11.5px; padding: 1px 8px; font-variant-numeric: tabular-nums; }
.empty { color: var(--ink-muted); font-size: 14px; margin: 0; }
.lead-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.lead-list a { display: flex; gap: 12px; align-items: center; padding: 9px 8px; border-radius: 8px; text-decoration: none; color: var(--ink); }
.lead-list a:hover { background: var(--bg); }
.lead-list .avatar { background: var(--warn-bg); color: var(--warn-ink); }
.lead-list .avatar-active { background: var(--accent-soft); color: var(--accent-ink); }
.lead-name { font-weight: 600; font-size: 14px; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-dept { font-size: 11.5px; color: var(--accent-ink); background: var(--accent-soft); padding: 3px 9px; border-radius: 99px; white-space: nowrap; }
.lead-time { font-size: 12px; color: var(--ink-muted); white-space: nowrap; }

.placeholders h2 { margin-bottom: 10px; }
.placeholder-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.placeholder-grid div {
    background: var(--bg); border: 1px dashed var(--line); border-radius: 10px; padding: 16px 10px;
    text-align: center; color: #9aa39c; font-size: 12.5px; font-weight: 600;
}

/* ===== login ===== */
.login-wrap { display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 24px; background: var(--bg); }
.login-card {
    background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 36px 32px;
    width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow);
}
.login-mark {
    width: 44px; height: 44px; border-radius: 12px; background: var(--ink); color: #fff;
    display: flex; align-items: center; justify-content: center; font-size: 15px; font-weight: 700; margin-bottom: 6px;
}
.login-card h1 { margin: 0; font-size: 19px; }
.login-card .sub { margin: 0 0 10px; color: var(--ink-muted); font-size: 13.5px; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--ink-muted); font-weight: 600; }
.login-card input {
    padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; font-family: inherit;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
    margin-top: 10px; background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 11px;
    font-size: 14px; font-weight: 600; cursor: pointer;
}
.login-card button:hover { background: var(--accent-ink); }
.alert { background: var(--danger-soft); color: var(--danger); padding: 9px 11px; border-radius: 8px; font-size: 13px; }

/* ===== conversation ===== */
.conv-layout { display: grid; grid-template-columns: 1fr 300px; gap: 20px; align-items: start; }
.back-link { display: inline-block; margin-bottom: 12px; font-size: 13px; text-decoration: none; color: var(--ink-muted); font-weight: 600; }
.back-link:hover { color: var(--ink); }

.conv-status-bar {
    display: flex; align-items: center; gap: 10px; padding: 11px 16px; border-radius: 10px; font-size: 13.5px;
    margin-bottom: 14px; background: var(--warn-bg); color: var(--warn-ink);
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; flex-shrink: 0; }
.status-text { flex: 1; font-weight: 600; }
.conv-status-bar.status-staff_active { background: var(--accent-soft); color: var(--accent-ink); }
.conv-status-bar.status-closed { background: var(--bg); color: var(--ink-muted); }
.conv-status-bar button {
    border: none; background: var(--accent); color: #fff; border-radius: 7px; padding: 7px 14px; font-size: 13px;
    font-weight: 600; cursor: pointer; flex-shrink: 0;
}
.conv-status-bar button:hover { background: var(--accent-ink); }
.conv-status-bar button.secondary { background: transparent; color: var(--accent-ink); border: 1px solid currentColor; }

.thread {
    background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 18px;
    display: flex; flex-direction: column; gap: 12px; max-height: 58vh; overflow-y: auto; box-shadow: var(--shadow);
}
.msg { max-width: 82%; }
.msg .who { display: block; font-size: 10.5px; color: var(--ink-muted); margin-bottom: 3px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.msg p { margin: 0; padding: 9px 13px; border-radius: 12px; background: var(--bg); display: inline-block; white-space: pre-wrap; line-height: 1.45; font-size: 14px; }
.msg-customer { align-self: flex-start; }
.msg-ai p { background: var(--accent-soft); border-bottom-left-radius: 3px; }
.msg-staff { align-self: flex-end; text-align: right; }
.msg-staff p { background: var(--ink); color: #fff; border-bottom-right-radius: 3px; }
.msg-system { align-self: center; }
.msg-system p { background: transparent; color: var(--ink-muted); font-size: 12px; font-style: italic; padding: 2px 0; }

.reply-form { display: flex; gap: 8px; margin-top: 12px; }
.reply-form textarea {
    flex: 1; border: 1px solid var(--line); border-radius: 10px; padding: 11px 13px; font-family: inherit;
    font-size: 14px; resize: vertical; min-height: 46px;
}
.reply-form textarea:focus { border-color: var(--accent); }
.reply-form button {
    background: var(--accent); color: #fff; border: none; border-radius: 10px; padding: 0 20px; cursor: pointer; font-weight: 600;
}
.reply-form button:hover { background: var(--accent-ink); }

.conv-side { display: flex; flex-direction: column; gap: 12px; }
.side-block { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 15px 17px; box-shadow: var(--shadow); }
.side-block h3 { margin: 0 0 9px; font-size: 12px; color: var(--ink-muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 700; }
.side-block p { font-size: 13.5px; line-height: 1.5; margin: 0; }
.side-block .lead-dept { display: inline-block; margin-top: 10px; }
.side-block dl { margin: 0; font-size: 13.5px; }
.side-block dt { color: var(--ink-muted); margin-top: 8px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .02em; }
.side-block dt:first-child { margin-top: 0; }
.side-block dd { margin: 1px 0 0; font-weight: 600; }
.context-dump { font-size: 11px; white-space: pre-wrap; word-break: break-word; max-height: 160px; overflow: auto; background: var(--bg); padding: 9px; border-radius: 8px; }
.product-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 9px; font-size: 13.5px; }
.product-list li { display: flex; justify-content: space-between; gap: 8px; }
.product-list a { font-weight: 600; }
.product-list .price { color: var(--ink-muted); white-space: nowrap; font-variant-numeric: tabular-nums; }

/* ===== buttons ===== */
.btn {
    display: inline-flex; align-items: center; gap: 6px; border: none; border-radius: 8px; padding: 9px 16px;
    font-size: 13.5px; font-weight: 600; cursor: pointer; text-decoration: none; font-family: inherit;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-ink); }
.btn-primary:disabled { background: var(--line); color: var(--ink-muted); cursor: not-allowed; }
.btn-secondary { background: var(--surface); color: var(--accent-ink); border: 1px solid var(--line); }
.btn-secondary:hover { background: var(--bg); }
.btn-lg { padding: 12px 22px; font-size: 14.5px; }
.btn-sm { padding: 6px 12px; font-size: 12.5px; border-radius: 6px; }
.btn-icon {
    width: 28px; height: 28px; border-radius: 7px; border: 1px solid var(--line); background: var(--surface);
    color: var(--ink-muted); cursor: pointer; font-size: 13px; line-height: 1;
}
.btn-icon:hover { background: var(--bg); color: var(--ink); }
.btn-icon-danger:hover { background: var(--danger-soft); color: var(--danger); border-color: var(--danger-soft); }
.alert-info { background: var(--accent-soft); color: var(--accent-ink); }
.quote-new-form, .quote-cta { margin-bottom: 16px; }

/* ===== quote status chip ===== */
.quote-status {
    font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em;
    padding: 3px 9px; border-radius: 99px; white-space: nowrap;
}
.quote-status-draft { background: var(--warn-bg); color: var(--warn-ink); }
.quote-status-sent { background: var(--accent-soft); color: var(--accent-ink); }

/* ===== quote lines ===== */
.quote-lines { display: flex; flex-direction: column; gap: 10px; }
.quote-line { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.ql-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ql-badge {
    font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; color: var(--accent-ink);
    background: var(--accent-soft); padding: 2px 8px; border-radius: 99px; flex-shrink: 0;
}
.ql-badge-freetext { background: var(--bg); color: var(--ink-muted); }
.ql-desc { flex: 1; font-weight: 600; font-size: 14px; min-width: 160px; }
.ql-ref { display: block; font-size: 11.5px; color: var(--ink-muted); font-weight: 400; margin-top: 1px; }
.ql-total { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ql-row { display: flex; align-items: flex-end; gap: 14px; flex-wrap: wrap; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.ql-form { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; flex: 1; }
.ql-form label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--ink-muted); font-weight: 600; }
.ql-input {
    border: 1px solid var(--line); border-radius: 7px; padding: 7px 9px; font-size: 13.5px; font-family: inherit;
}
.ql-input-num { width: 82px; }
.ql-input-desc { flex: 1; min-width: 160px; }
.ql-input[readonly] { background: var(--bg); color: var(--ink-muted); }
.ql-actions { display: flex; gap: 6px; margin-left: auto; }
.ql-readout { font-size: 13px; color: var(--ink-muted); margin-top: 6px; }

/* ===== product search / freetext add ===== */
.quote-search-form { display: flex; gap: 8px; margin-bottom: 12px; }
.quote-search-form input { flex: 1; border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; font-size: 14px; font-family: inherit; }
.search-results { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.search-results li { display: flex; align-items: center; gap: 12px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; }
.search-results img { width: 40px; height: 40px; object-fit: contain; border-radius: 6px; background: var(--bg); flex-shrink: 0; }
.sr-body { flex: 1; min-width: 0; display: flex; flex-direction: column; font-size: 13.5px; }
.sr-meta { font-size: 12px; color: var(--ink-muted); }
.freetext-form { display: flex; gap: 8px; flex-wrap: wrap; }
.freetext-form input[type="text"] { flex: 1; min-width: 160px; }
.freetext-form input { border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; font-size: 14px; font-family: inherit; }
.freetext-form input[type="number"] { width: 110px; }

/* ===== totals ===== */
.quote-totals dl { margin: 0; display: flex; flex-direction: column; gap: 6px; }
.quote-totals dl > div { display: flex; justify-content: space-between; font-size: 13.5px; }
.quote-totals dt { color: var(--ink-muted); font-weight: 400; }
.quote-totals dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 600; }
.quote-totals .qt-total { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 8px; font-size: 16px; }
.quote-totals .qt-total dt, .quote-totals .qt-total dd { font-weight: 700; color: var(--ink); }

/* ===== quote text / sent banner / versions ===== */
.quote-text-form textarea {
    width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; font-family: inherit;
    font-size: 14px; resize: vertical; margin-bottom: 10px;
}
.quote-text-form textarea:disabled { background: var(--bg); color: var(--ink-muted); }
.quote-sent-banner {
    display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
    background: var(--accent-soft); color: var(--accent-ink); border-radius: 10px; padding: 14px 18px; font-size: 13.5px; margin-bottom: 16px;
}
.version-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; font-size: 13.5px; }
.version-list li { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); }
.version-list li:last-child { border-bottom: none; }

/* ===== responsive: sidebar becomes a bottom tab bar ===== */
@media (max-width: 860px) {
    .app-shell { flex-direction: column; }
    .sidebar {
        position: fixed; bottom: 0; left: 0; right: 0; top: auto; width: 100%; height: auto;
        flex-direction: row; align-items: stretch; gap: 0; padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
        border-right: none; border-top: 1px solid var(--line); z-index: 30; box-shadow: 0 -6px 20px -12px rgba(24,38,32,.2);
    }
    .sidebar .brand, .sidebar .account-block { display: none; }
    .nav { flex-direction: row; justify-content: space-around; flex: 1; }
    .nav-item {
        flex-direction: column; gap: 3px; padding: 7px 4px 6px; font-size: 10px; text-align: center; flex: 1; border-radius: 10px;
    }
    .nav-icon { width: 21px; height: 21px; }
    .nav-item.active { background: transparent; }
    .nav-item.active .nav-icon { color: var(--accent); }
    .soon-badge { display: none; }
    .topbar-mobile {
        display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; background: var(--surface);
        border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
    }
    .brand-compact { font-weight: 700; text-decoration: none; color: var(--ink); font-size: 14.5px; }
    .shell { padding: 20px 16px calc(80px + env(safe-area-inset-bottom)); }
    .conv-layout { grid-template-columns: 1fr; }
    .stat-row { grid-template-columns: repeat(2, 1fr); }
}
