:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #e3e8ef;
    --text: #1f2937;
    --muted: #6b7280;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;
    --draft: #6b7280;
    --placed: #2563eb;
    --accepted: #16a34a;
    --rejected: #dc2626;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }

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

.hidden { display: none !important; }
.screen { min-height: 100vh; }

/* ---------- Авторизация ---------- */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
}
.login-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 32px;
    width: 100%;
    max-width: 400px;
}
.login-logo { display: block; width: 72px; height: 72px; margin: 0 auto 12px; border-radius: 16px; }
.login-card h1 { margin: 0 0 8px; font-size: 22px; text-align: center; }
.login-desc { color: var(--muted); margin: 0 0 24px; text-align: center; font-size: 14px; }

label { display: block; margin-bottom: 16px; }
label span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }

input[type="text"],
input[type="password"],
input[type="search"],
input[type="number"] {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    background: #fff;
    color: var(--text);
}
input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.error { color: var(--danger); font-size: 13px; margin-bottom: 12px; }
.error-text { color: var(--danger); }

/* ---------- Кнопки ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border: 1px solid transparent;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    background: #eef2f7;
    color: var(--text);
    transition: background .15s, border-color .15s;
}
.btn:hover { background: #e2e8f0; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #15803d; }
.btn-danger { background: #fee2e2; color: var(--danger); }
.btn-danger:hover { background: #fecaca; }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-sm { padding: 6px 10px; font-size: 13px; }
.btn-block { width: 100%; }

/* ---------- Шапка ---------- */
.app-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 20px;
    flex-wrap: wrap;
}
.nav-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    padding: 0;
    border: 1px solid var(--border);
    background: transparent;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text);
    flex-shrink: 0;
}
.nav-toggle svg { width: 20px; height: 20px; }
.nav-toggle .icon-close { display: none; }
.nav-toggle.open .icon-menu { display: none; }
.nav-toggle.open .icon-close { display: inline-block; }
.app-header__brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.app-logo { width: 32px; height: 32px; border-radius: 8px; display: block; flex-shrink: 0; }
.app-header__title { font-weight: 700; font-size: 18px; }
.app-nav { display: flex; gap: 6px; flex: 1; }
.nav-btn {
    border: none;
    background: transparent;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    color: var(--muted);
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.nav-btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.nav-btn.active { background: #eef2ff; color: var(--primary); }
.app-header__user { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; }

/* ---------- Основная область ---------- */
.app-main { max-width: 960px; margin: 0 auto; padding: 20px; }
.view { display: none; }
.view.active { display: block; }

.view-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.view h2 { margin: 0; font-size: 20px; }
.search-box { position: relative; width: 100%; max-width: 320px; }
.search-box .search-input { padding-right: 36px; }
.search-clear {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    width: 26px;
    height: 26px;
    padding: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    border-radius: 50%;
    color: var(--muted);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
}
.search-clear:hover { background: var(--bg); color: var(--text); }

/* ---------- Списки и карточки ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px 16px;
}
.empty { color: var(--muted); text-align: center; padding: 24px; }

.order-card {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}
.order-card__title { font-weight: 600; }
.order-card__meta { color: var(--muted); font-size: 13px; margin-top: 2px; }
.order-card__side { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.order-card__actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Бейджи статусов ---------- */
.badge {
    display: inline-block;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
}
.badge-draft    { background: var(--draft); }
.badge-placed   { background: var(--placed); }
.badge-accepted { background: var(--accepted); }
.badge-rejected { background: var(--rejected); }
.editor-status { margin-bottom: 14px; }

/* ---------- Таблица ---------- */
.table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.table { width: 100%; border-collapse: collapse; table-layout: fixed; }
/* Две колонки: «Наименование» занимает всё оставшееся место, «Цена» —
   фиксированной ширины у правого края.
   Ширины заданы процентами (в сумме 100%): при table-layout: fixed остаток
   ширины распределяется между ВСЕМИ колонками пропорционально, поэтому
   заданные в px значения на узком экране «раздуваются». Процентов ровно на
   100%, распределять нечего — пропорции неизменны на любом экране.
   Единица измерения отдельной колонкой не выводится: она показана строкой
   ниже наименования (см. .product-unit).
   Доля цены 25% подобрана с учётом отступов, одинаковых с «Наименованием»
   (14px слева и справа, на мобильном 8px): для самого длинного значения
   «8 450,00 ₸» нужно около 62px текста, то есть 90px колонки. При экране
   390px колонка получает 90px, при 360px — 83px, и текст помещается целиком
   без переноса. */
.table .col-name  { width: 75%; }
.table .col-price { width: 25%; }
.table th, .table td {
    text-align: left;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
}
.table th { background: #f8fafc; color: var(--muted); font-size: 13px; }
.table tr:last-child td { border-bottom: none; }
/* Наименование переносится, а не растягивает колонку */
.table td.col-name { overflow-wrap: anywhere; }
/* Единица измерения под наименованием — как вторичная подпись */
.table .product-name { font-weight: 400; }
.table .product-unit { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* ---------- Редактор заказа ---------- */
.item-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.item-row__info { flex: 1; min-width: 160px; }
.item-row__name { font-weight: 600; }
.item-row__meta { color: var(--muted); font-size: 13px; }
/* Количество и единица измерения — в одном блоке с кнопкой «Удалить»,
   прижаты к правому краю строки (margin-left: auto отодвигает от описания). */
.item-row__actions { display: flex; align-items: center; gap: 12px; margin-left: auto; flex: none; }
.item-row__qty { display: flex; align-items: center; gap: 6px; flex: none; }
/* Поле количества не растягивается на всю ширину (иначе единица уходит вниз) */
.item-row__qty .qty-input { width: 72px; flex: none; padding: 8px 10px; text-align: center; }
.item-row__qty .item-row__unit { white-space: nowrap; }
/* В режиме просмотра кнопки «Удалить» нет — блок остаётся у правого края */
.item-row--view .item-row__qty { justify-content: flex-end; }
.product-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

/* ---------- Цены и суммы ---------- */

/* Цена в каталоге: правое выравнивание внутри колонки.
   Отступы (padding) такие же, как у «Наименования» — они заданы общим
   правилом .table th, .table td и здесь НЕ переопределяются. */
.table .col-price { text-align: right; white-space: nowrap; }
.table td.col-price { font-variant-numeric: tabular-nums; font-weight: 600; }

/* Сумма заказа в карточке списка */
.order-card__sum { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.25; }
.order-card__sum-value { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.order-card__sum-hint { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

/* Сумма по позиции в редакторе */
.item-row__sum { font-weight: 600; font-variant-numeric: tabular-nums; }
.item-row__unit { color: var(--muted); font-size: 13px; }
.qty-input--invalid { border-color: var(--danger) !important; box-shadow: 0 0 0 3px rgba(220, 38, 38, .12) !important; }

/* Итог заказа: под списком позиций, виден и при просмотре, и при редактировании */
.editor-total {
    display: flex;
    align-items: baseline;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 14px;
    padding: 12px 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.editor-total__label { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .03em; }
.editor-total__value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }

.editor-actions { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; align-items: center; }
.editor-actions__sum { margin-right: auto; color: var(--muted); font-size: 14px; }
.editor-actions__sum strong { color: var(--text); font-size: 16px; font-variant-numeric: tabular-nums; }
#editor-add { margin-top: 18px; }
#editor-add h3 { font-size: 15px; margin: 0 0 10px; }
#editor-add .search-box { margin-bottom: 10px; }

/* ---------- Профиль ---------- */
.profile-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.profile-row {
    display: flex;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    gap: 16px;
}
.profile-row:last-child { border-bottom: none; }
.profile-row span:first-child { color: var(--muted); flex-shrink: 0; }

/* ---------- Toast ---------- */
.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: #111827;
    color: #fff;
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 14px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, .2);
    z-index: 100;
    max-width: 90vw;
}
.toast-error { background: var(--danger); }

/* ---------- Пагинация ---------- */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    margin-top: 16px;
    flex-wrap: wrap;
}
.pagination__btn {
    min-width: 38px;
    height: 38px;
    padding: 0 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    color: var(--text);
    transition: background .15s, border-color .15s;
}
.pagination__btn:hover:not(:disabled) { background: var(--bg); }
.pagination__btn.active { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.pagination__btn:disabled { opacity: .4; cursor: not-allowed; }
.pagination__gap { color: var(--muted); padding: 0 4px; }

/* ---------- Адаптивность ---------- */
@media (max-width: 768px) {
    .app-header { padding: 10px 14px; gap: 10px; }
    .nav-toggle { display: inline-flex; }
    .app-header__brand { flex: 1; min-width: 0; }
    .app-nav {
        display: none;
        flex-basis: 100%;
        width: 100%;
        flex-direction: column;
        gap: 2px;
        padding-top: 10px;
        margin-top: 4px;
        border-top: 1px solid var(--border);
    }
    .app-nav.open { display: flex; }
    .nav-btn { width: 100%; justify-content: flex-start; }
    #header-user { max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    .app-main { padding: 14px; }
    .search-box { max-width: 100%; }
    .login-card { padding: 24px; }
    /* Ширины колонок «Единица» и «Цена» заданы в px и не меняются на узком
       экране (остаток ширины забирает колонка-распорка). Уменьшаем только
       внутренние отступы, чтобы текст помещался. */
    .table th, .table td { padding: 10px 8px; }

    .order-card { flex-direction: column; align-items: stretch; }
    .order-card__side { justify-content: space-between; }
    .order-card__sum { align-items: flex-start; }
    /* Поле количества и единица остаются в одну строку, поле чуть narrower */
    .item-row__qty .qty-input { width: 62px; }
    .product-row { flex-wrap: wrap; }
    .editor-total { justify-content: space-between; }
    .editor-total__value { font-size: 18px; }
}

@media (max-width: 400px) {
    #header-user { display: none; }
    .app-header__title { font-size: 16px; }
    .btn { padding: 9px 14px; }
}
