/*
 * Оболонка сторінки (docs/UI-Redesign-TZ.md, розділ 4).
 *   ≥ 1200px    — бічна панель 248px; згортається до рейки 72px (стан у localStorage).
 *   768–1199px  — рейка 72px; кнопка внизу рейки відкриває повну панель поверх сторінки.
 *   < 768px     — панелі немає: нижній таб-бар і «Ще» (bottom sheet).
 * Класи на <html> ставить shell.js (і вбудований скрипт у <head>, щоб не було стрибка):
 *   sb-rail     — місце під рейку (72px);
 *   sb-compact  — панель показана рейкою (тільки іконки, групи — випадаючими панелями);
 *   sb-overlay  — на планшеті панель відкрита поверх сторінки.
 */

/* ============================== каркас ============================== */

.app-main-wrap {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
}

.app-main {
    flex: 1 0 auto;
    padding: var(--sp-6);
}

.app-content {
    max-width: 1440px;
    margin-inline: auto;
}

.app-footer {
    padding: var(--sp-4) var(--sp-6);
    color: var(--ink-400);
    font-size: var(--fs-12);
}

@media (min-width: 768px) {
    .app-shell .app-main-wrap { padding-left: var(--sidebar-w); }
    .sb-rail .app-shell .app-main-wrap { padding-left: var(--rail-w); }
}

/* ============================== бічна панель ============================== */

.app-sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 1035;
    display: flex;
    flex-direction: column;
    width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--line);
    transition: width var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

@media (max-width: 767.98px) {
    .app-sidebar,
    .app-backdrop { display: none; }
}

.sb-head {
    display: flex;
    align-items: center;
    flex: none;
    height: var(--topbar-h);
    padding: 0 var(--sp-4) 0 20px;
}

.sb-logo {
    display: inline-flex;
    align-items: center;
    border-radius: var(--r-sm);
}

.sb-logo img {
    display: block;
    width: auto;
    height: 34px;
}

.sb-nav {
    flex: 1 1 auto;
    padding: var(--sp-2) var(--sp-3) var(--sp-3);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.sb-foot {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex: none;
    padding: var(--sp-2) var(--sp-3) var(--sp-2) 20px;
    border-top: 1px solid var(--line);
}

.sb-version {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    color: var(--ink-400);
    font-size: var(--fs-12);
    text-decoration: none;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sb-version:hover { color: var(--ink-600); text-decoration: underline; }

.icon-btn {
    display: inline-grid;
    place-items: center;
    flex: none;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 0;
    border-radius: var(--r-md);
    background: transparent;
    color: var(--ink-600);
    font-size: 18px;
    line-height: 1;
    transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.icon-btn:hover { background: var(--canvas); color: var(--ink-900); }

.sb-toggle .bi { transition: transform var(--dur) var(--ease); }
.sb-compact .sb-toggle .bi { transform: rotate(180deg); }

/* ---------- дерево меню ---------- */

.nav-tree,
.nav-sub-list {
    margin: 0;
    padding: 0;
    list-style: none;
}

.nav-tree > li + li { margin-top: 2px; }

.nav-row {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    width: 100%;
    min-height: 40px;
    padding: 8px 12px;
    border: 0;
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--ink-600);
    font: inherit;
    font-size: var(--fs-14);
    font-weight: var(--fw-medium);
    line-height: 1.3;
    text-align: left;
    text-decoration: none;
    transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.nav-row:hover {
    background: var(--canvas);
    color: var(--ink-900);
}

.nav-icon {
    flex: none;
    width: 20px;
    font-size: 18px;
    line-height: 1;
    text-align: center;
}

.nav-text {
    flex: 1 1 auto;
    min-width: 0;
}

.nav-caret {
    flex: none;
    font-size: 12px;
    transition: transform var(--dur) var(--ease);
}

.nav-row[aria-expanded="true"] .nav-caret { transform: rotate(180deg); }

.nav-group.has-active > .nav-row { color: var(--ink-900); }

.nav-row.is-active {
    background: var(--brand-50);
    color: var(--brand-700);
}

/* 3px жовтий індикатор активного пункту біля краю панелі */
.nav-row.is-active::before {
    content: "";
    position: absolute;
    top: 8px;
    bottom: 8px;
    left: -12px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--accent-400);
}

.nav-badge {
    flex: none;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 10px;
    background: var(--brand-600);
    color: #fff;
    font-size: var(--fs-12);
    font-weight: var(--fw-semibold);
    font-variant-numeric: tabular-nums;
    line-height: 20px;
    text-align: center;
}

/* Акордеон: висота через grid-template-rows, закриті пункти не доступні з клавіатури */
.nav-sub {
    display: grid;
    grid-template-rows: 0fr;
    visibility: hidden;
    transition: grid-template-rows var(--dur) var(--ease), visibility var(--dur) var(--ease);
}

.nav-group.is-open > .nav-sub {
    grid-template-rows: 1fr;
    visibility: visible;
}

.nav-sub > .nav-sub-list {
    min-height: 0;
    overflow: hidden;
}

.nav-sub .nav-row {
    min-height: 36px;
    margin-top: 2px;
    padding-left: 44px;
    font-weight: var(--fw-regular);
}

.nav-sub .nav-row.is-active { font-weight: var(--fw-medium); }

.nav-flyout-title { display: none; }

/* ---------- рейка 72px ---------- */

.sb-compact .app-sidebar { width: var(--rail-w); }

.sb-compact .sb-head {
    justify-content: center;
    padding: 0;
}

.sb-compact .sb-logo img {
    height: auto;
    width: 60px;
}

.sb-compact .sb-nav { overflow: visible; }

.sb-compact .sb-foot {
    justify-content: center;
    padding: var(--sp-2) 0;
}

.sb-compact .sb-version,
.sb-compact .nav-text,
.sb-compact .nav-caret { display: none; }

.sb-compact .nav-tree > li > .nav-row {
    justify-content: center;
    width: 48px;
    height: 44px;
    margin-inline: auto;
    padding: 0;
}

.sb-compact .nav-icon { font-size: 20px; }

.sb-compact .nav-group.has-active > .nav-row {
    background: var(--brand-50);
    color: var(--brand-700);
}

.sb-compact .nav-group.has-active > .nav-row::before {
    content: "";
    position: absolute;
    top: 8px;
    bottom: 8px;
    left: -12px;
    width: 3px;
    border-radius: 0 3px 3px 0;
    background: var(--accent-400);
}

.sb-compact .nav-tree > li > .nav-row .nav-badge {
    position: absolute;
    top: 3px;
    right: 1px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border: 2px solid var(--surface);
    border-radius: 8px;
    font-size: 10px;
    line-height: 12px;
    box-sizing: content-box;
}

/* Тултіп для пунктів без підменю */
.sb-compact .nav-tree > li > a.nav-row::after {
    content: attr(data-tip);
    position: absolute;
    top: 50%;
    left: calc(100% + 12px);
    z-index: 5;
    padding: 4px 8px;
    border-radius: var(--r-sm);
    background: var(--ink-900);
    color: #fff;
    font-size: var(--fs-12);
    font-weight: var(--fw-medium);
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-50%);
    transition: opacity var(--dur-fast) var(--ease), visibility var(--dur-fast) var(--ease);
}

.sb-compact .nav-tree > li > a.nav-row:hover::after,
.sb-compact .nav-tree > li > a.nav-row:focus-visible::after {
    opacity: 1;
    visibility: visible;
}

/* Група в рейці — випадаюча панель справа (наведення, фокус або клік) */
.sb-compact .nav-group { position: relative; }

.sb-compact .nav-sub {
    position: absolute;
    top: -6px;
    left: 100%;
    z-index: 5;
    display: block;
    padding-left: 12px;
    opacity: 0;
    transition: opacity var(--dur-fast) var(--ease), visibility var(--dur-fast) var(--ease);
}

.sb-compact .nav-group:hover > .nav-sub,
.sb-compact .nav-group:focus-within > .nav-sub,
.sb-compact .nav-group.is-flyout > .nav-sub {
    visibility: visible;
    opacity: 1;
}

.sb-compact .nav-sub > .nav-sub-list {
    min-width: 232px;
    padding: 6px;
    overflow: visible;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-pop);
}

.sb-compact .nav-flyout-title {
    display: block;
    padding: 6px 10px 4px;
}

.sb-compact .nav-sub .nav-row {
    padding-left: 10px;
}

.sb-compact .nav-sub .nav-row .nav-text { display: block; }
.sb-compact .nav-sub .nav-row.is-active::before { display: none; }

/* ---------- планшет: повна панель поверх сторінки ---------- */

.sb-overlay .app-sidebar { box-shadow: var(--shadow-pop); }

.app-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1034;
    background: rgba(15, 27, 42, .32);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur) var(--ease), visibility var(--dur) var(--ease);
}

.sb-overlay .app-backdrop {
    opacity: 1;
    visibility: visible;
}

/* ============================== верхня панель ============================== */

.app-topbar {
    position: sticky;
    top: 0;
    z-index: 1020;
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    height: var(--topbar-h);
    padding: 0 var(--sp-6);
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.tb-logo {
    display: none;
    flex: none;
    border-radius: var(--r-sm);
}

.tb-logo img {
    display: block;
    width: auto;
    height: 30px;
}

.app-guest .tb-logo { display: inline-flex; }

.tb-crumbs {
    flex: 1 1 auto;
    min-width: 0;
}

.tb-crumbs ol {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
    white-space: nowrap;
}

.tb-crumbs li {
    min-width: 0;
    overflow: hidden;
    color: var(--ink-600);
    font-size: var(--fs-14);
    text-overflow: ellipsis;
}

.tb-crumbs li + li::before {
    content: "/";
    margin-right: 6px;
    color: var(--ink-400);
}

.tb-crumbs li:last-child {
    flex: 0 1 auto;
    color: var(--ink-900);
    font-weight: var(--fw-semibold);
}

.tb-crumbs li:not(:last-child) { flex: none; }

.tb-crumbs a {
    color: inherit;
    text-decoration: none;
}

.tb-crumbs a:hover { color: var(--ink-900); text-decoration: underline; }

.tb-spacer { flex: 1 1 auto; }

.tb-actions {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    flex: none;
}

/* Пошук за інв. / заводським № */
.tb-search {
    position: relative;
    width: clamp(220px, 22vw, 320px);
}

.tb-search form {
    position: relative;
    margin: 0;
}

.tb-search .bi-search {
    position: absolute;
    top: 50%;
    left: 12px;
    color: var(--ink-400);
    font-size: 14px;
    transform: translateY(-50%);
    pointer-events: none;
}

.tb-search-input {
    width: 100%;
    height: 38px;
    padding: 0 34px 0 34px;
    border: 1px solid transparent;
    border-radius: var(--r-md);
    background: var(--canvas);
    color: var(--ink-900);
    font-size: var(--fs-14);
    transition: border-color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

.tb-search-input::placeholder { color: var(--ink-400); }
.tb-search-input::-webkit-search-cancel-button { display: none; }

.tb-search-input:hover { border-color: var(--line); }

.tb-search-input:focus {
    border-color: var(--brand-600);
    background: var(--surface);
    box-shadow: 0 0 0 2px var(--brand-100);
    outline: 0;
}

.tb-search kbd {
    position: absolute;
    top: 50%;
    right: 8px;
    min-width: 20px;
    padding: 1px 6px;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: var(--surface);
    color: var(--ink-400);
    font-family: inherit;
    font-size: var(--fs-12);
    line-height: 16px;
    text-align: center;
    transform: translateY(-50%);
    pointer-events: none;
}

.tb-search-input:focus + kbd { display: none; }

.tb-search-btn { display: none; }

.tb-search.is-loading .bi-search,
.search-modal-box.is-loading .search-modal-field > .bi { color: var(--brand-600); }

/* Підказки пошуку за номером (js/search.js) */
.search-results {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 1050;
    width: max(100%, 440px);
    max-height: min(70vh, 520px);
    padding: 6px;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-pop);
}

.search-results.is-inline {
    position: static;
    width: auto;
    max-height: none;
    margin-top: var(--sp-3);
    padding: 0;
    border: 0;
    box-shadow: none;
}

.search-item {
    display: grid;
    gap: 2px;
    padding: 10px 12px;
    border-radius: var(--r-md);
    color: var(--ink-900);
    text-decoration: none;
}

.search-item:hover,
.search-item[aria-selected="true"] {
    background: var(--brand-50);
    color: var(--ink-900);
}

.search-item-top {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    min-width: 0;
}

.search-item-num {
    flex: none;
    font-weight: var(--fw-semibold);
    font-variant-numeric: tabular-nums;
}

.search-item-type {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    color: var(--ink-600);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.search-item .status-badge { flex: none; }

.search-item-addr {
    overflow: hidden;
    font-size: var(--fs-13);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.search-item-meta {
    color: var(--ink-600);
    font-size: var(--fs-12);
}

.search-item mark {
    padding: 0;
    border-radius: 2px;
    background: var(--brand-100);
    color: inherit;
}

.search-note {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    padding: 12px;
    color: var(--ink-600);
    font-size: var(--fs-13);
}

.search-foot {
    margin-top: 4px;
    padding: 8px 12px 4px;
    border-top: 1px solid var(--line);
    color: var(--ink-400);
    font-size: var(--fs-12);
}

.search-modal-box.is-open .search-modal-hint { display: none; }

/* Меню користувача */
.user-trigger {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    max-width: 240px;
    height: 40px;
    padding: 0 8px 0 4px;
    border: 0;
    border-radius: var(--r-md);
    background: transparent;
    color: var(--ink-900);
    font-size: var(--fs-14);
    font-weight: var(--fw-medium);
    transition: background-color var(--dur-fast) var(--ease);
}

.user-trigger:hover,
.user-trigger[aria-expanded="true"] { background: var(--canvas); }

.user-trigger-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-trigger .bi-chevron-down {
    color: var(--ink-400);
    font-size: 12px;
}

.user-avatar-btn {
    display: none;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: none;
}

.user-menu.dropdown-menu {
    width: 240px;
    --bs-dropdown-border-radius: var(--r-lg);
}

.user-menu-head {
    display: grid;
    gap: 2px;
    padding: 8px 10px 10px;
}

.user-menu-name {
    overflow: hidden;
    font-weight: var(--fw-semibold);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-menu-email {
    overflow: hidden;
    color: var(--ink-600);
    font-size: var(--fs-13);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-menu-head .role-badge {
    justify-self: start;
    margin-top: 6px;
}

.user-menu .bi,
.sheet-row .bi {
    flex: none;
    width: 20px;
    font-size: 16px;
    text-align: center;
}

.user-menu form { margin: 0; }

.text-bad,
.text-bad:hover,
.dropdown-item.text-bad:hover { color: var(--status-bad); }

/* Планшет: пошук стискається до іконки, ім'я в меню користувача ховається */
@media (max-width: 1199.98px) {
    .tb-search { display: none; }
    .tb-search-btn { display: inline-grid; }
}

@media (max-width: 991.98px) {
    .user-trigger-name,
    .user-trigger .bi-chevron-down { display: none; }

    .user-trigger { padding: 0 4px; }
}

/* ============================== мобільний ============================== */

.app-tabbar { display: none; }

@media (max-width: 767.98px) {
    .app-topbar {
        height: var(--topbar-h-mobile);
        padding: 0 var(--sp-2) 0 var(--sp-4);
        gap: var(--sp-1);
    }

    .tb-logo { display: inline-flex; }
    .tb-logo img { height: 28px; }

    .app-shell .tb-crumbs { display: none; }
    .app-shell .tb-actions { margin-left: auto; }

    .app-topbar .tb-scan,
    .user-trigger { display: none; }

    .user-avatar-btn { display: inline-grid; place-items: center; width: 44px; height: 44px; }

    .app-main { padding: var(--sp-4); }
    .app-footer { padding: var(--sp-4); }

    .app-shell .app-main-wrap {
        padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    }

    /* Нижній таб-бар: 5 пунктів, «Сканувати» по центру */
    .app-tabbar {
        position: fixed;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 1030;
        display: grid;
        grid-auto-columns: 1fr;
        grid-auto-flow: column;
        height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
        padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
        background: var(--surface);
        border-top: 1px solid var(--line);
    }
}

.tab {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-width: 0;
    min-height: 44px;
    padding: 0 2px;
    border: 0;
    background: none;
    color: var(--ink-600);
    font-size: 11px;
    font-weight: var(--fw-medium);
    line-height: 1.2;
    text-decoration: none;
}

.tab > .bi { font-size: 20px; line-height: 1; }

.tab-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tab.is-active { color: var(--brand-700); }

.tab.is-active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    width: 28px;
    height: 3px;
    margin-left: -14px;
    border-radius: 0 0 3px 3px;
    background: var(--accent-400);
}

.tab-scan { color: var(--ink-900); }

.tab-scan-btn {
    display: grid;
    place-items: center;
    width: 48px;
    height: 32px;
    border-radius: var(--r-md);
    background: var(--brand-600);
    color: #fff;
    font-size: 18px;
}

.tab-scan.is-active::before { display: none; }
.tab-scan.is-active .tab-scan-btn { background: var(--brand-700); }

.tab-badge {
    position: absolute;
    top: 5px;
    left: calc(50% + 4px);
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border: 2px solid var(--surface);
    border-radius: 8px;
    background: var(--brand-600);
    color: #fff;
    font-size: 10px;
    font-weight: var(--fw-semibold);
    font-variant-numeric: tabular-nums;
    line-height: 12px;
    text-align: center;
    box-sizing: content-box;
}

/* ============================== bottom sheet («Ще», меню користувача) ============================== */

.app-sheet.offcanvas-bottom {
    height: auto;
    max-height: 88vh;
    max-height: 88dvh;
    border: 0;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    padding-bottom: env(safe-area-inset-bottom);
    box-shadow: var(--shadow-pop);
}

.sheet-handle {
    width: 36px;
    height: 4px;
    margin: 8px auto 0;
    border-radius: 2px;
    background: var(--line);
    flex: none;
}

.app-sheet .offcanvas-header {
    padding: 8px var(--sp-4) 4px;
}

.sheet-title {
    margin: 0;
    font-size: var(--fs-16);
    font-weight: var(--fw-semibold);
}

.app-sheet .offcanvas-body {
    padding: 0 var(--sp-2) var(--sp-4);
}

.sheet-section + .sheet-section { margin-top: var(--sp-2); }

.sheet-section .overline {
    padding: 12px 12px 4px;
}

.sheet-list {
    margin: 0;
    padding: 0;
    list-style: none;
}

.sheet-row {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    width: 100%;
    min-height: 48px;
    padding: 0 12px;
    border: 0;
    border-radius: var(--r-md);
    background: none;
    color: var(--ink-900);
    font-size: var(--fs-14);
    font-weight: var(--fw-medium);
    text-align: left;
    text-decoration: none;
}

.sheet-row:hover { background: var(--canvas); color: var(--ink-900); }

.sheet-row .bi { color: var(--ink-600); font-size: 18px; }

.sheet-row.is-active { background: var(--brand-50); color: var(--brand-700); }
.sheet-row.is-active .bi { color: var(--brand-700); }

.sheet-row.text-bad,
.sheet-row.text-bad .bi { color: var(--status-bad); }

.sheet-row .nav-badge { margin-left: auto; }

.sheet-user {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    padding: 12px;
}

.sheet-user-text { min-width: 0; }

.sheet-card {
    margin: var(--sp-4) var(--sp-2) 0;
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    padding: var(--sp-1);
}

.sheet-version {
    padding: var(--sp-3) 12px 0;
    color: var(--ink-400);
    font-size: var(--fs-12);
}

.sheet-version a { color: inherit; }

/* ============================== пошук на весь екран (планшет, мобільний) ============================== */

.search-modal .modal-dialog {
    max-width: 560px;
    margin-top: 72px;
}

.search-modal .modal-content { padding: var(--sp-4); }

.search-modal-row {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
}

.search-modal-field {
    position: relative;
    flex: 1 1 auto;
}

.search-modal-field > .bi {
    position: absolute;
    top: 50%;
    left: 14px;
    color: var(--ink-400);
    transform: translateY(-50%);
    pointer-events: none;
}

.search-modal-field .form-control {
    min-height: 48px;
    padding-left: 40px;
    font-size: 16px;
}

.search-modal-hint {
    margin: var(--sp-3) 0 0;
    color: var(--ink-600);
    font-size: var(--fs-13);
}

@media (max-width: 767.98px) {
    .search-modal .modal-dialog { margin: 0; max-width: none; }
    .search-modal .modal-content { padding: var(--sp-3) var(--sp-3) var(--sp-4); }
}
