/* Оформление взято с эталонного Mini App (roles_export): тёмная тема, сине-
   фиолетовый акцент, карточки на тонкой обводке, вкладки-пилюли, модалки как
   размытая подложка с панелью по центру.
   Палитра вынесена в переменные — перекрасить можно, не трогая разметку. */

:root {
    --bg: #04080f;
    --surface: #0a0e18;
    --surface2: #101828;
    --stroke: rgba(255, 255, 255, .08);
    --txt: #e8ebf5;
    --muted: rgba(232, 235, 245, .55);
    --accent: #7b5cff;
    --accent2: #33d4ff;
    --green: rgba(40, 220, 138, .88);
    --red: rgba(239, 68, 68, .88);
    --orange: #e0af68;
    --ls-bold: .3px;

    /* производные от акцента — чтобы не рассыпать rgba() по всему файлу */
    --accent-bg: rgba(123, 92, 255, .12);
    --accent-line: rgba(123, 92, 255, .30);
    --accent-grad: linear-gradient(135deg, rgba(123, 92, 255, .18), rgba(51, 212, 255, .10));
    --accent-grad-hi: linear-gradient(135deg, rgba(123, 92, 255, .26), rgba(51, 212, 255, .14));
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--txt);
    font-size: 14px;
}

/* Боковое смещение в Mini App — это «увёл пальцем и увидел изнанку» */
html, body { overflow-x: hidden; overscroll-behavior: none; }
button { -webkit-tap-highlight-color: transparent; font-family: inherit; }
[hidden] { display: none !important; }

/* Оболочка во всю высоту окна: прокручивается только содержимое, шапка и
   вкладки стоят. --app-height ставит JS из viewportStableHeight — 100vh
   внутри Telegram врёт. Ширину ограничиваем: в Telegram Desktop окно шире
   телефона, и растянутые на весь экран карточки выглядят сломанными. */
#app {
    display: flex;
    flex-direction: column;
    height: var(--app-height, 100dvh);
    max-width: 620px;
    margin: 0 auto;
}

/* ─── Шапка ─────────────────────────────────────────────────── */

.header {
    flex: none;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: rgba(4, 8, 15, .85);
    border-bottom: 1px solid var(--stroke);
    backdrop-filter: blur(18px);
}
.header-right { display: flex; align-items: center; gap: 10px; min-width: 0; }

.user-btn {
    display: flex;
    align-items: center;
    gap: 7px;
    min-width: 0;
    padding: 6px 12px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface2);
    color: var(--txt);
    font-weight: 700;
    letter-spacing: var(--ls-bold);
    cursor: default;
}
.user-btn__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-btn__role {
    flex: none;
    padding: 2px 6px;
    border-radius: 6px;
    background: var(--accent-bg);
    border: 1px solid var(--accent-line);
    color: var(--txt);
    font-size: 10px;
    letter-spacing: .5px;
    text-transform: uppercase;
}

.header-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex: none;
    padding: 4px 8px;
    border-radius: 6px;
    background: var(--surface2);
    font-size: 11px;
    font-weight: 700;
    color: var(--muted);
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status-dot.ok { background: var(--green); }
.status-dot.bad { background: var(--red); }

/* ─── Вкладки ───────────────────────────────────────────────── */

.tabs {
    flex: none;
    display: flex;
    gap: 4px;
    padding: 8px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--stroke);
}
.tab {
    flex: 1;
    min-width: 0;
    padding: 10px 6px;
    border: 1px solid transparent;
    border-radius: 10px;
    background: none;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
    letter-spacing: var(--ls-bold);
    cursor: pointer;
    transition: color .2s;
}
.tab[aria-selected="true"] {
    color: var(--txt);
    background: var(--accent-bg);
    border-color: var(--accent-line);
}

.content {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
}
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ─── Карточки и секции ─────────────────────────────────────── */

.card {
    padding: 14px;
    margin-bottom: 14px;
    border: 1px solid var(--stroke);
    border-radius: 14px;
    background: var(--surface);
}

.section { margin-bottom: 20px; }
.section:last-child { margin-bottom: 0; }
.section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 12px;
}
.section-title {
    font-size: 12px;
    font-weight: 800;
    letter-spacing: .6px;
    text-transform: uppercase;
    color: var(--muted);
}

.muted { color: var(--muted); font-size: 12px; }
.empty-state {
    padding: 34px 20px;
    border: 1px dashed var(--stroke);
    border-radius: 12px;
    text-align: center;
    color: var(--muted);
}
.hint {
    padding: 10px 12px;
    margin-bottom: 12px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: rgba(255, 255, 255, .03);
    color: var(--muted);
    font-size: 12px;
    line-height: 1.45;
}

/* ─── Формы ─────────────────────────────────────────────────── */

.form-section { margin-bottom: 12px; }
.form-section:last-child { margin-bottom: 0; }
.form-label {
    display: block;
    margin-bottom: 6px;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .6px;
    text-transform: uppercase;
    color: var(--muted);
}
.form-label em { font-style: normal; font-weight: 600; text-transform: none; letter-spacing: 0; opacity: .7; }

/* 16px, иначе iOS зумит страницу при фокусе и обратно не отъезжает */
.form-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: rgba(255, 255, 255, .03);
    color: var(--txt);
    font-family: inherit;
    font-size: 16px;
    font-weight: 600;
}
.form-input:focus { outline: none; border-color: rgba(123, 92, 255, .40); }
.form-input:disabled { opacity: .45; cursor: not-allowed; }
.form-input::placeholder { color: var(--muted); }
textarea.form-input { resize: vertical; min-height: 60px; line-height: 1.45; }

.form-note { margin: 6px 0 0; font-size: 11.5px; line-height: 1.45; color: var(--muted); }
.form-note.warn { color: var(--orange); }

.form-error {
    margin: 10px 0 0;
    padding: 9px 12px;
    border: 1px solid rgba(239, 68, 68, .22);
    border-radius: 10px;
    background: rgba(239, 68, 68, .08);
    color: var(--red);
    font-size: 12.5px;
    font-weight: 700;
}

/* ─── Кнопки ────────────────────────────────────────────────── */

.btn {
    width: 100%;
    padding: 12px 18px;
    border: 1px solid var(--accent-line);
    border-radius: 11px;
    background: var(--accent-grad);
    color: var(--txt);
    font-family: inherit;
    font-size: 13px;
    font-weight: 800;
    letter-spacing: var(--ls-bold);
    cursor: pointer;
}
.btn:hover:not(:disabled) { background: var(--accent-grad-hi); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-sm { width: auto; padding: 8px 12px; font-size: 12px; }
.btn-danger { background: rgba(239, 68, 68, .08); border-color: rgba(239, 68, 68, .22); color: var(--red); }
.btn-danger:hover:not(:disabled) { background: rgba(239, 68, 68, .14); }

.btn-mini {
    padding: 5px 10px;
    border: 1px solid var(--stroke);
    border-radius: 8px;
    background: var(--surface2);
    color: var(--muted);
    font-family: inherit;
    font-size: 11px;
    font-weight: 700;
    cursor: pointer;
}
.btn-mini:hover:not(:disabled) { color: var(--txt); border-color: var(--accent-line); }
.btn-mini:disabled { opacity: .4; cursor: not-allowed; }
.btn-mini.danger { color: var(--red); border-color: rgba(239, 68, 68, .22); }
.btn-mini.danger:hover:not(:disabled) { background: rgba(239, 68, 68, .14); border-color: rgba(239, 68, 68, .35); }

.btn-add {
    flex: none;
    padding: 0 16px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface2);
    color: var(--txt);
    font-family: inherit;
    font-weight: 800;
    cursor: pointer;
}
.btn-add:hover:not(:disabled) { border-color: var(--accent-line); }
.btn-add:disabled { opacity: .4; cursor: not-allowed; }

.btn-row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

/* ─── Свой чекбокс (браузерный не используем, §9 ТЗ) ────────── */

.chk {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface);
    color: var(--muted);
    font-family: inherit;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
}
.chk:hover { border-color: var(--accent-line); }
.chk.is-on { background: rgba(123, 92, 255, .10); border-color: rgba(123, 92, 255, .40); color: var(--txt); }
.chk-box {
    width: 16px;
    height: 16px;
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--muted);
    border-radius: 4px;
}
.chk.is-on .chk-box { background: var(--accent); border-color: var(--accent); }
.chk.is-on .chk-box::after { content: "✓"; color: #fff; font-size: 11px; font-weight: 800; }

/* ─── Поиск по каталогу (свой select) ───────────────────────── */

.combobox { position: relative; }
.combobox .form-input { padding-right: 34px; }
.combobox-clear {
    position: absolute;
    top: 50%;
    right: 8px;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    border: 0;
    border-radius: 6px;
    background: none;
    color: var(--muted);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
}
.combobox-clear:hover { background: var(--surface2); color: var(--txt); }

.combobox-list {
    position: absolute;
    z-index: 50;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    max-height: 46dvh;
    overflow-y: auto;
    margin: 0;
    padding: 4px;
    list-style: none;
    border: 1px solid var(--stroke);
    border-radius: 12px;
    background: var(--surface2);
    box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.combobox-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border-radius: 8px;
    cursor: pointer;
}
.combobox-option[aria-selected="true"], .combobox-option:hover { background: var(--accent-bg); }
.opt-name { flex: 1; font-weight: 600; }
.opt-meta { flex: none; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.combobox-option.no-suppliers .opt-meta { color: var(--orange); }
.combobox-empty { padding: 12px 10px; color: var(--muted); font-size: 12.5px; }

/* ─── Количество ────────────────────────────────────────────── */

.qty-row { display: flex; gap: 8px; }
.qty-row .form-input { flex: 1; font-variant-numeric: tabular-nums; }
.qty-unit {
    flex: none;
    display: inline-flex;
    align-items: center;
    padding: 0 12px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface2);
    color: var(--muted);
    font-weight: 700;
}

/* ─── Строки: блокнот позиций и позиции заявки ──────────────── */

.lines {
    border: 1px solid var(--stroke);
    border-radius: 12px;
    background: rgba(255, 255, 255, .02);
    overflow: hidden;
}
.line {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 10px 12px;
    border-bottom: 1px solid var(--stroke);
}
.line:last-child { border-bottom: 0; }
.line-name { flex: 1; min-width: 0; font-weight: 600; }
.line-qty { flex: none; font-weight: 800; font-variant-numeric: tabular-nums; }
.line-sub { width: 100%; font-size: 11.5px; color: var(--muted); }
.line-sub.warn { color: var(--orange); }
.line-del {
    flex: none;
    width: 24px;
    height: 24px;
    border: 0;
    border-radius: 7px;
    background: none;
    color: var(--muted);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
}
.line-del:hover { background: rgba(239, 68, 68, .14); color: var(--red); }
.lines-empty { padding: 22px 14px; text-align: center; color: var(--muted); font-size: 12.5px; }
.line-actions { display: flex; gap: 6px; width: 100%; flex-wrap: wrap; }

/* ─── Ярлыки состояний ──────────────────────────────────────── */

.tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border: 1px solid var(--stroke);
    border-radius: 8px;
    background: var(--surface2);
    color: var(--txt);
    font-size: 12px;
    font-weight: 700;
}
.tag-accent { background: var(--accent-bg); border-color: var(--accent-line); }
.tag-green { background: rgba(40, 220, 138, .10); border-color: rgba(40, 220, 138, .28); color: var(--green); }
.tag-red { background: rgba(239, 68, 68, .10); border-color: rgba(239, 68, 68, .28); color: var(--red); }
.tag-orange { background: rgba(224, 175, 104, .10); border-color: rgba(224, 175, 104, .28); color: var(--orange); }
.tag-off { background: rgba(120, 120, 120, .10); border-color: rgba(120, 120, 120, .28); color: var(--muted); }
.tag-sm { padding: 2px 7px; font-size: 10.5px; letter-spacing: .4px; text-transform: uppercase; }
.id-chip { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; color: var(--muted); }

/* ─── Карточка заявки / сделки / сотрудника ─────────────────── */

.item-card {
    padding: 12px;
    margin-bottom: 8px;
    border: 1px solid var(--stroke);
    border-radius: 12px;
    background: var(--surface);
}
.item-head {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 6px;
}
.item-title { font-weight: 800; letter-spacing: var(--ls-bold); }
.item-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    margin-bottom: 8px;
    color: var(--muted);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}
.item-meta .warn { color: var(--orange); }
.item-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* Черновик сообщения поставщику — единственный блок с акцентной заливкой:
   это то, что реально уйдёт наружу */
.message-box {
    margin: 8px 0;
    padding: 10px 12px;
    border: 1px solid var(--accent-line);
    border-radius: 10px;
    background: var(--accent-bg);
    font-size: 12.5px;
    line-height: 1.5;
    white-space: pre-wrap;
}
.why-box {
    margin: 8px 0;
    padding: 9px 12px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: rgba(255, 255, 255, .03);
    color: var(--muted);
    font-size: 11.5px;
    line-height: 1.5;
}

/* ─── Переключатель роли (сегменты) ─────────────────────────── */

.role-row {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 3px;
    border: 1px solid var(--stroke);
    border-radius: 11px;
    background: rgba(255, 255, 255, .03);
}
.role-opt {
    flex: 1 1 30%;
    padding: 8px 6px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: none;
    color: var(--muted);
    font-family: inherit;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
}
.role-opt.active {
    background: linear-gradient(135deg, rgba(123, 92, 255, .20), rgba(51, 212, 255, .08));
    border-color: var(--accent-line);
    color: var(--txt);
}
.role-hint { margin: 6px 0 0; font-size: 11.5px; color: var(--muted); }

/* ─── Модалки ───────────────────────────────────────────────── */

/* Вид как в эталоне, но на <dialog>: у него из коробки ловушка фокуса, Esc и
   inert-фон. Внешне не отличается — всё оформление описано здесь. */
.modal {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: none;
    height: 100%;
    max-height: none;
    margin: 0;
    padding: 20px;
    border: 0;
    background: transparent;
    color: var(--txt);
    overflow: hidden;
}
.modal:not([open]) { display: none; }
.modal[open] { display: flex; align-items: center; justify-content: center; }
.modal::backdrop { background: rgba(0, 0, 0, .30); backdrop-filter: blur(22px); }

.modal-panel {
    width: min(420px, 100%);
    max-height: 100%;
    display: flex;
    flex-direction: column;
    padding: 20px;
    border: 1px solid var(--stroke);
    border-radius: 18px;
    background: rgba(10, 14, 24, .97);
    box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.modal-title { margin: 0 0 14px; font-size: 15px; font-weight: 800; letter-spacing: var(--ls-bold); }
.modal-body { flex: 1; min-height: 0; overflow-y: auto; font-size: 13.5px; line-height: 1.5; }
.modal-body p { margin: 0 0 10px; }
.modal-list { margin: 10px 0 0; padding: 0; list-style: none; }
.modal-list li {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 7px 0;
    border-bottom: 1px solid var(--stroke);
    font-size: 12.5px;
}
.modal-list li:last-child { border-bottom: 0; }
.modal-list b { font-variant-numeric: tabular-nums; }

.modal-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--stroke);
}
.modal-head .id-chip { margin-left: auto; }

.modal-actions { flex: none; display: flex; gap: 8px; margin-top: 16px; }
.modal-btn {
    flex: 1;
    padding: 11px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface2);
    color: var(--txt);
    font-family: inherit;
    font-size: 13px;
    font-weight: 800;
    cursor: pointer;
}
.modal-btn.yes { background: var(--accent-bg); border-color: var(--accent-line); }
.modal-btn.no { color: var(--muted); }
/* Необратимое подтверждается красной кнопкой, а не акцентной (§12 ТЗ) */
.modal-btn.danger { background: rgba(239, 68, 68, .14); border-color: rgba(239, 68, 68, .35); color: var(--red); }

/* ─── Тост ──────────────────────────────────────────────────── */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(24px + env(safe-area-inset-bottom));
    z-index: 1000;
    max-width: min(420px, calc(100vw - 32px));
    padding: 12px 22px;
    border: 1px solid var(--stroke);
    border-radius: 14px;
    background: rgba(10, 14, 24, .92);
    color: var(--txt);
    font-weight: 700;
    font-size: 13px;
    text-align: center;
    transform: translateX(-50%) translateY(300px);
    transition: transform .3s ease;
}
.toast.show { transform: translateX(-50%) translateY(0); }
.toast.success { border-color: rgba(40, 220, 138, .35); }
.toast.error { border-color: rgba(239, 68, 68, .35); color: var(--red); }

/* ─── Экран «доступа нет» ───────────────────────────────────── */

.no-access {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100dvh;
    padding: 40px 28px;
    text-align: center;
}
.no-access-title { margin-bottom: 12px; font-size: 20px; font-weight: 800; color: var(--red); }
.no-access-text { color: var(--muted); line-height: 1.5; }
.no-access-id {
    margin-top: 18px;
    padding: 8px 14px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface2);
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 13px;
}

@media (max-width: 360px) {
    .tab { font-size: 11.5px; padding: 10px 3px; }
    .content { padding-left: 12px; padding-right: 12px; }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}

/* ─── Выбор поставщика для одной позиции ────────────────────── */

/* Свои radio в языке эталона: у него чекбоксы-строки, здесь то же самое,
   но кружком — выбрать можно только одного. */
.picker {
    margin: 8px 0;
    padding: 10px;
    border: 1px solid var(--stroke);
    border-radius: 12px;
    background: rgba(255, 255, 255, .03);
}
.picker-title {
    margin-bottom: 8px;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: .6px;
    text-transform: uppercase;
    color: var(--muted);
}
.pick-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    width: 100%;
    margin-bottom: 4px;
    padding: 9px 10px;
    border: 1px solid var(--stroke);
    border-radius: 10px;
    background: var(--surface);
    color: var(--txt);
    font-family: inherit;
    text-align: left;
    cursor: pointer;
}
.pick-row:hover:not(.is-disabled) { border-color: var(--accent-line); }
.pick-row.is-on { background: rgba(123, 92, 255, .10); border-color: rgba(123, 92, 255, .40); }
.pick-row.is-disabled { opacity: .45; cursor: not-allowed; }
.pick-mark {
    flex: none;
    width: 16px;
    height: 16px;
    margin-top: 2px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--muted);
    border-radius: 50%;
}
.pick-row.is-on .pick-mark { border-color: var(--accent); }
.pick-row.is-on .pick-mark::after {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
}
.pick-body { display: grid; gap: 2px; min-width: 0; }
.pick-name { font-size: 13px; font-weight: 700; }
.pick-reason { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Предупреждение по позиции: тот же блок, что и обоснование, но янтарный */
.why-box.warn {
    border-color: rgba(224, 175, 104, .28);
    background: rgba(224, 175, 104, .08);
    color: var(--orange);
}
