/* ============ Design tokens ============ */
:root {
    --bg: #f7f7f5;
    --surface: #ffffff;
    --surface-sunken: #f0efeb;
    --border: #e3e1da;
    --ink: #1c1c1a;
    --ink-muted: #6b6a63;
    --ink-faint: #9c9a91;

    --accent: #2b4c3f;
    --accent-hover: #233f34;
    --accent-soft: #e7ede9;

    --red: #b3432f;
    --red-soft: #f6e5e0;
    --amber: #9a6b1f;
    --amber-soft: #f5ecd9;
    --green: #3a6b4a;
    --green-soft: #e4ede6;
    --violet: #5c4f8a;
    --violet-soft: #ebe8f5;
    --cyan: #2c6b73;
    --cyan-soft: #e2eef0;

    --radius-sm: 5px;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(28,28,26,0.06);

    --font: 'Inter', -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { font-weight: 600; letter-spacing: -0.01em; color: var(--ink); }
h2 { font-size: 20px; margin: 0 0 16px; }
h3 { font-size: 15px; margin: 28px 0 12px; color: var(--ink); }

table { font-variant-numeric: tabular-nums; }

/* ============ Layout ============ */
.shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 216px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 20px 14px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 8px 22px;
    text-decoration: none;
}
.brand:hover { text-decoration: none; }
.brand-mark {
    width: 34px;
    height: 34px;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
    flex-shrink: 0;
}
.brand-text {
    font-weight: 600;
    font-size: 13px;
    line-height: 1.25;
    color: var(--ink);
}

.side-nav { display: flex; flex-direction: column; gap: 18px; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-group-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--ink-faint);
    padding: 8px 10px 4px;
}
.nav-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    color: var(--ink-muted);
    font-size: 13.5px;
    font-weight: 500;
    text-decoration: none;
    transition: background 0.12s, color 0.12s;
}
.nav-link:hover { background: var(--surface-sunken); color: var(--ink); text-decoration: none; }
.nav-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-badge {
    min-width: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--surface-sunken);
    color: var(--ink-muted);
    font-size: 11px;
    font-weight: 700;
    text-align: center;
    line-height: 18px;
}
.nav-badge-red { background: var(--red-soft); color: var(--red); }

.sidebar-footer {
    margin-top: auto;
    padding-top: 16px;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.sidebar-username { font-size: 12.5px; color: var(--ink-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.logout-btn {
    border: none;
    background: none;
    color: var(--ink-faint);
    font-size: 12px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
}
.logout-btn:hover { background: var(--surface-sunken); color: var(--ink); }

.content {
    flex: 1;
    padding: 32px 40px;
    max-width: 1200px;
}

/* ============ Buttons & links ============ */
.btn-link {
    display: inline-block;
    padding: 8px 16px;
    background: var(--accent);
    color: #fff;
    text-decoration: none;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    font-weight: 500;
    margin-bottom: 16px;
    border: none;
    cursor: pointer;
}
.btn-link:hover { background: var(--accent-hover); text-decoration: none; }

.call-btn {
    border: none;
    background: var(--green-soft);
    color: var(--green);
    border-radius: var(--radius-sm);
    padding: 3px 8px;
    cursor: pointer;
    font-size: 13px;
    margin-left: 6px;
}
.call-btn:hover { filter: brightness(0.95); }

.calc-btn {
    padding: 5px 10px;
    border: none;
    background: var(--accent);
    color: #fff;
    border-radius: var(--radius-sm);
    font-size: 12px;
    cursor: pointer;
}
.calc-btn:hover { background: var(--accent-hover); }

/* ============ Search & toolbar ============ */
.search-input, .search-form input {
    width: 100%;
    max-width: 480px;
    display: block;
    padding: 9px 13px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: var(--font);
    margin-bottom: 16px;
    background: var(--surface);
    color: var(--ink);
}
.search-input:focus, .search-form input:focus {
    outline: none;
    border-color: var(--accent);
}
.search-form { display: flex; gap: 8px; }
.search-form button {
    padding: 9px 18px;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    font-size: 14px;
    margin-bottom: 16px;
    height: fit-content;
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 12px;
}

.view-toggle {
    display: flex;
    gap: 2px;
    background: var(--surface-sunken);
    border-radius: var(--radius-sm);
    padding: 3px;
}
.view-toggle a {
    padding: 6px 14px;
    border-radius: 4px;
    font-size: 13px;
    text-decoration: none;
    color: var(--ink-muted);
}
.view-toggle a.active {
    background: var(--surface);
    color: var(--ink);
    font-weight: 600;
    box-shadow: var(--shadow);
}
.show-all-toggle {
    font-size: 13px;
    color: var(--ink-muted);
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
}

/* ============ Tables ============ */
.data-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}
.data-table th, .data-table td {
    text-align: left;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 13.5px;
}
.data-table th {
    background: var(--surface-sunken);
    font-weight: 600;
    color: var(--ink-muted);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-sunken); }
.data-table td.stock-ok, .data-table td.stock-zero { font-weight: 600; font-variant-numeric: tabular-nums; }
.data-table td.stock-ok { color: var(--green); }
.data-table td.stock-zero { color: var(--red); }
.data-table tr.ok td:nth-child(2) { color: var(--green); }
.data-table tr.error td:nth-child(2) { color: var(--red); }

.count-info {
    color: var(--ink-muted);
    font-size: 12px;
    padding: 10px 14px;
    border-top: 1px solid var(--border);
    background: var(--surface-sunken);
    display: flex;
    justify-content: space-between;
}

/* ============ Cards / info grid ============ */
.stat-grid { display: flex; gap: 14px; margin-bottom: 28px; }
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 26px;
}
.stat-number { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 12.5px; color: var(--ink-muted); margin-top: 2px; }

.info-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 10px;
    max-width: 800px;
    margin-bottom: 24px;
}
.info-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 16px;
}
.info-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--ink-faint);
    margin-bottom: 4px;
    font-weight: 600;
}
.info-value { font-size: 14px; }

/* ============ Badges / status pills ============ */
.badge {
    display: inline-block;
    padding: 6px 14px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    margin-bottom: 16px;
}
.badge-ok { background: var(--green-soft); color: var(--green); }
.badge-error { background: var(--red-soft); color: var(--red); max-width: 620px; }
.badge-warn { background: var(--amber-soft); color: var(--amber); max-width: 620px; }

.status-pill {
    display: inline-block;
    padding: 3px 11px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
}
.status-not_paid, .status-not_shipped { background: var(--red-soft); color: var(--red); }
.status-partially_paid, .status-partially_shipped { background: var(--amber-soft); color: var(--amber); }
.status-paid, .status-shipped { background: var(--green-soft); color: var(--green); }
/* «Статус счёта» (задача #91) */
.status-negotiation { background: var(--amber-soft); color: var(--amber); }
.status-in_work { background: var(--accent-soft); color: var(--accent); }
.status-not_relevant { background: var(--border); color: var(--ink-faint); }
.status-draft { background: var(--surface-sunken); color: var(--ink-faint); }
.status-synced { background: var(--green-soft); color: var(--green); }
.status-cancelled { background: var(--red-soft); color: var(--red); }
.status-edo_done { background: var(--green-soft); color: var(--green); }
.status-edo_pending { background: var(--amber-soft); color: var(--amber); }
.status-edo_rejected { background: var(--red-soft); color: var(--red); }
.status-edo_none { background: var(--surface-sunken); color: var(--ink-faint); }

.activity-dot {
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--green);
    margin-right: 7px;
}

/* ============ Timeline ============ */
.timeline { display: flex; flex-direction: column; gap: 8px; max-width: 720px; }
.timeline-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 11px 16px;
    display: flex;
    gap: 16px;
    align-items: baseline;
}
.timeline-date { font-size: 12px; color: var(--ink-faint); white-space: nowrap; min-width: 108px; font-variant-numeric: tabular-nums; }
.timeline-body { font-size: 13.5px; }
.timeline-tag {
    display: inline-block;
    font-size: 11px;
    font-weight: 600;
    padding: 2px 9px;
    border-radius: 20px;
    margin-right: 8px;
}
.tag-invoice { background: var(--cyan-soft); color: var(--cyan); }
.tag-note { background: var(--amber-soft); color: var(--amber); }
.tag-bitrix { background: var(--violet-soft); color: var(--violet); }
.tag-call { background: var(--cyan-soft); color: var(--cyan); }
.tag-email { background: #dbeafe; color: #1d4ed8; }
.tag-shipment { background: var(--green-soft); color: var(--green); }

.email-unread { font-weight: 600; }
.email-unread td:nth-child(4) a { font-weight: 700; }
.email-thread { margin: 20px 0; padding: 12px 16px; background: var(--surface-sunken); border-radius: var(--radius); font-size: 13px; }
.email-thread a { display: block; padding: 4px 0; }
.email-html-frame { width: 100%; min-height: 300px; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.attachment-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 6px 12px;
    margin: 4px 8px 4px 0;
    font-size: 12.5px;
    text-decoration: none;
    color: var(--ink);
}
.attachment-chip:hover { background: var(--accent-soft); text-decoration: none; }
.timeline-status { color: var(--ink-faint); font-size: 12px; }

/* ============ Forms ============ */
.form-row {
    margin-bottom: 14px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    max-width: 420px;
}
.form-row label {
    font-size: 12.5px;
    color: var(--ink-muted);
    font-weight: 600;
}
.form-row select, .form-row input, .form-row textarea {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: var(--font);
    background: var(--surface);
    color: var(--ink);
}
.form-row select:focus, .form-row input:focus, .form-row textarea:focus {
    outline: none;
    border-color: var(--accent);
}
.form-row textarea { resize: vertical; }
.form-actions button[type="submit"] {
    padding: 10px 22px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
}
.form-actions button[type="submit"]:hover { background: var(--accent-hover); }

.status-form select {
    padding: 7px 11px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font);
    background: var(--surface);
    color: var(--ink);
}

.note-form-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 18px 20px;
    margin-bottom: 24px;
}
.note-form-box h3 { margin-top: 0; font-size: 14.5px; }

#items-table select, #items-table input { padding: 6px 8px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; width: 100%; }
#items-table select { min-width: 260px; }
#items-table input[type="number"] { width: 100px; }
.remove-row { border: none; background: var(--red-soft); color: var(--red); border-radius: 4px; width: 28px; height: 28px; cursor: pointer; font-size: 16px; }
#add-row { margin: 10px 0 20px; padding: 8px 14px; border: 1px dashed var(--ink-faint); background: var(--surface); border-radius: var(--radius-sm); cursor: pointer; color: var(--ink-muted); }

/* ============ Modal ============ */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(28,28,26,0.45);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal-box {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 24px;
    width: 90%;
    max-width: 420px;
    position: relative;
}
.modal-box h3 { margin-top: 0; margin-bottom: 16px; }
.modal-close {
    position: absolute;
    top: 14px;
    right: 14px;
    border: none;
    background: var(--surface-sunken);
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    color: var(--ink-muted);
}
.contractor-results {
    display: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-top: 4px;
    max-height: 180px;
    overflow-y: auto;
    background: var(--surface);
}
.contractor-item { padding: 8px 12px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border); }
.contractor-item:hover { background: var(--accent-soft); color: var(--accent); }
.contractor-item:last-child { border-bottom: none; }
#kp-price-type { padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 14px; width: 100%; }

/* ============ Product grid ============ */
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 14px;
}
.product-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
}
.product-image {
    width: 100%;
    aspect-ratio: 1;
    background: var(--surface-sunken);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    margin-bottom: 8px;
}
.product-image img { width: 100%; height: 100%; object-fit: contain; }
.no-image { color: var(--ink-faint); font-size: 12px; }
.product-name { font-size: 13px; font-weight: 600; line-height: 1.35; }
.product-meta { font-size: 12px; color: var(--ink-muted); margin-top: 4px; }
.product-stock { font-size: 12px; font-weight: 600; color: var(--green); margin-top: 6px; }
.product-stock.stock-zero { color: var(--red); }

/* ============ Prices page ============ */
.price-toolbar {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    padding: 12px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.pct-group { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.pct-label { font-weight: 600; }
.pct-input { width: 50px; padding: 5px 6px; border: 1px solid var(--border); border-radius: 5px; font-size: 13px; }

/* ============ Misc ============ */
code {
    background: var(--surface-sunken);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12.5px;
}

.dashboard-columns {
    display: grid;
    grid-template-columns: 1.3fr 1fr;
    gap: 32px;
    align-items: start;
}
@media (max-width: 900px) {
    .dashboard-columns { grid-template-columns: 1fr; }
}
details summary::-webkit-details-marker { display: none; }

/* ============ Mobile ============ */
/* топбар, нижнее меню и подложка бокового меню — только на узких экранах */
.mobile-topbar { display: none; }
.nav-backdrop { display: none; }
.bottom-nav { display: none; }

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

    .mobile-topbar {
        display: flex;
        align-items: center;
        gap: 12px;
        position: sticky;
        top: 0;
        z-index: 150;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        padding: 10px 14px;
    }
    .nav-toggle {
        border: 1px solid var(--border);
        background: var(--surface);
        color: var(--ink);
        font-size: 18px;
        line-height: 1;
        width: 38px;
        height: 38px;
        border-radius: var(--radius-sm);
        cursor: pointer;
        flex-shrink: 0;
    }
    .mobile-brand {
        display: flex;
        align-items: center;
        gap: 10px;
        font-weight: 600;
        font-size: 14px;
        color: var(--ink);
        text-decoration: none;
    }

    /* сайдбар -> выезжающая слева панель */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 264px;
        max-width: 84vw;
        z-index: 300;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 0 0 0 100vw rgba(0,0,0,0); /* placeholder, backdrop handles dim */
    }
    .shell.nav-open .sidebar { transform: translateX(0); }
    .shell.nav-open .nav-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 250;
        background: rgba(28,28,26,0.45);
    }

    .nav-link { padding: 11px 12px; font-size: 14px; }

    .content {
        padding: 16px 14px;
        padding-bottom: calc(16px + 58px + env(safe-area-inset-bottom, 0px));
        max-width: 100%;
    }

    /* нижнее меню — 4 основных раздела, всегда под рукой большим пальцем */
    .bottom-nav {
        display: flex;
        position: fixed;
        left: 0; right: 0; bottom: 0;
        z-index: 200;
        background: var(--surface);
        border-top: 1px solid var(--border);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    .bottom-nav-link {
        flex: 1 1 0;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        padding: 7px 4px 8px;
        color: var(--ink-faint);
        font-size: 11px;
        font-weight: 500;
        text-decoration: none;
        position: relative;
    }
    .bottom-nav-link:hover { text-decoration: none; }
    .bottom-nav-link.active { color: var(--accent); font-weight: 600; }
    .bn-icon { font-size: 19px; line-height: 1; }
    .bottom-nav-link .nav-badge {
        position: absolute;
        top: 2px;
        right: calc(50% - 22px);
        min-width: 15px;
        padding: 0 4px;
        font-size: 9.5px;
        line-height: 15px;
    }

    h2 { font-size: 18px; margin-bottom: 12px; }
    h3 { margin: 20px 0 10px; }

    /* таблицы — горизонтальная прокрутка внутри своего контейнера */
    .data-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: none;
    }
    .data-table > thead,
    .data-table > tbody { display: table; width: 100%; }
    .data-table th, .data-table td { padding: 9px 10px; }

    /* карточки/сетки в одну колонку */
    .stat-grid { flex-wrap: wrap; gap: 10px; }
    .stat-card { flex: 1 1 100%; padding: 14px 18px; }
    .stat-number { font-size: 22px; }
    .info-grid { grid-template-columns: 1fr; max-width: 100%; }

    /* тулбар с кнопками действий — переносим */
    .toolbar > div { flex-wrap: wrap; gap: 8px; }
    .toolbar .btn-link { margin-bottom: 0; }

    /* формы и поиск на всю ширину */
    .form-row { max-width: 100%; }
    .search-input, .search-form input { max-width: 100%; }
    .note-form-box { padding: 14px; }

    /* конструктор строк счёта */
    #items-table select { min-width: 0; }
    #items-table select, #items-table input { font-size: 14px; }

    /* таймлайн — вертикально */
    .timeline-item { flex-direction: column; gap: 4px; }
    .timeline-date { min-width: 0; }

    /* фильтр-бары в шаблонах (селекты во всю ширину) */
    .tflt, .flt, .oflt, .price-toolbar { gap: 8px; }
    .tflt > div, .flt > div, .oflt > div { flex: 1 1 100%; }
    .tflt select, .tflt input,
    .flt select, .flt input,
    .oflt select, .oflt input { width: 100%; min-width: 0; }

    .toolbar { gap: 8px; }
}

/* выбор адресатов письма (задача #133) */
.recip { display:flex; flex-wrap:wrap; gap:6px; align-items:center; }
.recip-chip { display:inline-flex; align-items:center; gap:6px; padding:4px 10px; border:1px solid var(--border); border-radius:999px; font-size:13px; cursor:pointer; background:var(--surface); max-width:100%; }
.recip-chip:has(input:checked) { border-color:var(--accent); background:var(--accent-soft); }
.recip-chip input { margin:0; }
.recip-email { color:var(--ink-faint); font-size:12px; overflow-wrap:anywhere; }
.recip-extra { flex:1 1 220px; min-width:0; }

/* Маркировка «Честный знак» (задача #161) — в номенклатуре и на карточке товара */
.mark-badge { display:inline-block; padding:1px 6px; border-radius:4px; font-size:11px; font-weight:700;
              letter-spacing:.03em; background:var(--surface-sunken); color:var(--ink); border:1px solid var(--ink-faint);
              vertical-align:middle; cursor:help; }
