/* ==========================================================================
   Sidebar Navigation - Rebuilt Drawer System
   Desktop: docked + true full hide
   Mobile (< md): off-canvas drawer
   ========================================================================== */

:root {
    --sidebar-docked-width: var(--sidebar-width, 280px);
    --header-height: 64px;
    --sidebar-indent: 14px;
    --sidebar-item-pad-x: 1rem;
    --sidebar-item-pad-y: 0.75rem;

    --sidebar-bg: var(--bg-sidebar, #0f172a);
    --sidebar-text: #94a3b8;
    --sidebar-active-accent: var(--primary, #2563eb);
    --sidebar-hover-bg: var(--bg-sidebar-hover, #1e293b);

    --z-overlay: 1020;
    --z-sidebar: 1030;
    --z-topbar: 1040;
}

@media (max-width: 991.98px) {
    :root { --header-height: 56px; }
}

html,
body {
    height: 100%;
    min-height: 100%;
}

body {
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden;
}

body:not(.has-sidebar) {
    --sidebar-width: 0px;
}

html.no-sidebar,
body.no-sidebar,
body.sidebar-collapsed {
    --sidebar-width: 0px;
}

#wrapper {
    width: 100%;
    height: 100dvh;
    min-height: 100dvh;
    margin: 0 !important;
    padding: 0 !important;
    display: flex;
    overflow-x: hidden;
}

#sidebar-wrapper {
    width: var(--sidebar-docked-width);
    flex-shrink: 0;
    background-color: var(--sidebar-bg);
    color: var(--sidebar-text);
    min-height: 100vh;
    min-height: 100dvh;
    height: 100vh;
    height: 100dvh;
    position: sticky;
    top: 0;
    z-index: var(--z-sidebar);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 2px 0 8px rgba(0,0,0,0.05);
    transition: width 0.22s ease, transform 0.22s ease, opacity 0.22s ease, visibility 0.22s ease, margin 0.22s ease;
}

html.no-sidebar #sidebar-wrapper,
body.no-sidebar #sidebar-wrapper,
body.sidebar-collapsed #sidebar-wrapper {
    width: 0 !important;
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
    box-shadow: none;
    border-right: 0 !important;
    overflow: hidden;
}

 #sidebar-wrapper.hidden {
    width: 0 !important;
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
    box-shadow: none !important;
    border-right: 0 !important;
    overflow: hidden;
 }

body.no-sidebar #sidebar-wrapper.border-end,
html.no-sidebar #sidebar-wrapper.border-end,
body.sidebar-collapsed #sidebar-wrapper.border-end {
    border-right: 0 !important;
}

#sidebar-wrapper .list-group {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

#page-content-wrapper {
    flex: 1;
    min-width: 0;
    height: 100dvh;
    min-height: 0;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    margin: 0 !important;
    overflow: hidden;
}

body.has-topbar #page-content-wrapper { padding-top: var(--header-height) !important; }

#page-content-wrapper > main {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding-top: 0 !important;
    margin-top: 0 !important;
}

#page-content-wrapper > footer {
    flex: 0 0 auto;
}

.app-content { 
    padding: 1.25rem 2rem 2rem !important; 
    width: 100%; 
    max-width: 100%; 
}

#page-content-wrapper .detail-page {
    min-height: 100% !important;
}

.topbar-title { max-width: 280px; }
.topbar-search { max-width: 520px; }
.topbar #sidebarToggle { width: 40px; height: 40px; }

@media (min-width: 992px) {
    .topbar #sidebarToggle { width: 35px; height: 35px; }
}

@media (max-width: 991.98px) {
    .app-content { padding: 0 1rem 1rem !important; }
    .topbar-title { max-width: 220px; }
    .topbar-search { max-width: 360px; }
}

@media (max-width: 575.98px) {
    .app-content { padding: 0 0.75rem 0.75rem !important; }
    .topbar-title { max-width: 160px; }
}

@media (max-width: 767.98px) {
    .topbar-center { display: none !important; }
    .topbar-right .btn,
    .topbar-right .nav-link { min-height: 44px; }
    .topbar-right .nav-link { display: flex; align-items: center; justify-content: center; }
}

html.with-sidebar #page-content-wrapper,
body.with-sidebar #page-content-wrapper {
    /* Layout handled by flex container now */
}

html.no-sidebar #page-content-wrapper,
body.no-sidebar #page-content-wrapper,
body.sidebar-collapsed #page-content-wrapper {
    /* Layout handled by flex container now */
}

#sidebarOverlay,
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: var(--z-overlay);
    visibility: hidden;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease, visibility 0.2s ease;
}

body.sidebar-open #sidebarOverlay,
body.sidebar-open .sidebar-overlay {
    visibility: visible;
    pointer-events: auto;
    opacity: 1;
}

@media (min-width: 992px) {
    #sidebarOverlay,
    .sidebar-overlay {
        display: none !important;
    }
}

@media (max-width: 991.98px) {
    #wrapper {
        display: flex;
    }

    #sidebar-wrapper {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(86vw, var(--sidebar-docked-width));
        transform: translateX(-100%);
        visibility: hidden;
        pointer-events: none;
        z-index: 1050;
        box-shadow: 4px 0 18px rgba(0,0,0,0.18);
        transition: transform 0.3s ease;
    }

    body.sidebar-open #sidebar-wrapper {
        transform: translateX(0) !important;
        width: min(86vw, var(--sidebar-docked-width)) !important;
        visibility: visible !important;
        opacity: 1 !important;
        pointer-events: auto !important;
    }

    body.sidebar-open {
        overflow: hidden;
    }

    .sidebar-overlay { z-index: 1035; }

    #sidebar-wrapper .list-group-item {
        white-space: normal;
        overflow-wrap: anywhere;
    }

    .mobile-close-btn {
        display: block !important;
    }

    #page-content-wrapper {
        margin-left: 0 !important;
    }
}

#sidebar-wrapper .sidebar-heading {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 1.5rem;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    color: #fff;
    position: sticky;
    top: 0;
    z-index: 10;
    background-color: var(--sidebar-bg);
    overflow: hidden;
    white-space: nowrap;
}

#sidebar-wrapper .sidebar-brand-text {
    font-family: var(--font-family-heading, 'Inter', sans-serif);
    font-weight: 700;
    font-size: 1.15rem;
    letter-spacing: -0.01em;
}

#sidebar-wrapper .sidebar-section-label {
    padding: 1.25rem 1.25rem 0.5rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    font-weight: 700;
    color: rgba(255,255,255,0.4);
    letter-spacing: 0.05em;
    font-family: var(--font-family-base, 'Inter', sans-serif);
}

#sidebar-wrapper .list-group-item {
    background: transparent;
    color: var(--sidebar-text);
    border: none;
    padding: var(--sidebar-item-pad-y) var(--sidebar-item-pad-x) !important;
    padding-left: calc(var(--sidebar-item-pad-x) + (var(--sidebar-level, 0) * var(--sidebar-indent))) !important;
    display: flex;
    align-items: center;
    font-weight: 500;
    font-size: 0.9rem;
    transition: background-color 0.15s ease, color 0.15s ease;
    gap: 0.875rem;
    white-space: nowrap;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    border-radius: var(--radius-md, 0.5rem);
    margin: 0.125rem 0.75rem !important;
}

#sidebar-wrapper .list-group-item > .d-flex {
    min-width: 0;
    flex: 1 1 auto;
}

#sidebar-wrapper .list-group-item i {
    font-size: 1.15rem;
    width: 24px;
    text-align: center;
    color: var(--sidebar-text);
    transition: color 0.15s ease;
}

#sidebar-wrapper .list-group-item:hover {
    background-color: var(--sidebar-hover-bg);
    color: #fff;
}

#sidebar-wrapper .list-group-item:hover i {
    color: #fff;
}

#sidebar-wrapper .list-group-item.active,
#sidebar-wrapper .active-parent {
    background-color: var(--sidebar-hover-bg);
    color: #fff !important;
    position: relative;
}

#sidebar-wrapper .list-group-item.active::before,
#sidebar-wrapper .active-parent::before {
    content: '';
    position: absolute;
    left: -0.75rem;
    top: 0;
    bottom: 0;
    width: 3px;
    background-color: var(--sidebar-active-accent);
    border-radius: 0 4px 4px 0;
}

#sidebar-wrapper .list-group-item.active i,
#sidebar-wrapper .active-parent i {
    color: var(--sidebar-active-accent) !important;
}

#sidebar-wrapper .submenu {
    background-color: rgba(0,0,0,0.2);
    overflow: hidden;
}

#sidebar-wrapper .submenu .list-group-item {
    padding-left: calc(var(--sidebar-item-pad-x) + (var(--sidebar-level, 1) * var(--sidebar-indent))) !important;
    font-size: 0.9rem;
}

#sidebar-wrapper .submenu-toggle .bi-chevron-down {
    font-size: 0.8rem !important;
    margin-left: auto;
    width: auto;
    transition: transform 0.2s ease;
}

#sidebar-wrapper .submenu-toggle[aria-expanded="true"] .bi-chevron-down {
    transform: rotate(180deg);
}

#sidebar-wrapper .submenu-toggle-btn {
    color: inherit;
    opacity: 0.85;
}

#sidebar-wrapper .submenu-toggle-btn:hover {
    opacity: 1;
}

#sidebar-wrapper .submenu-toggle-btn[aria-expanded="true"] .bi-chevron-down {
    transform: rotate(180deg);
}

.topbar {
    position: fixed;
    top: 0 !important;
    right: 0;
    left: 0;
    height: var(--header-height);
    z-index: var(--z-topbar);
    background-color: var(--bg-card, #fff);
    transition: left 0.22s ease, width 0.22s ease, transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    margin: 0 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    overflow: visible;
    border-bottom: 1px solid var(--border-color);
    box-shadow: var(--shadow-xs);
}

.topbar-inner {
    height: 100%;
}

.topbar-left,
.topbar-center,
.topbar-right {
    min-width: 0;
}

.topbar-search {
    z-index: 1;
}

.topbar-title {
    color: var(--text-main);
    letter-spacing: -0.01em;
}

.topbar-icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 9999px;
    background: color-mix(in srgb, var(--bg-body) 70%, var(--bg-card));
    border: 1px solid var(--border-color);
    color: var(--text-main);
    box-shadow: var(--shadow-xs);
}

.topbar-icon-btn:hover {
    background: color-mix(in srgb, var(--bg-body) 55%, var(--bg-card));
}

.topbar-icon-link {
    width: 40px;
    height: 40px;
    border-radius: 9999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: relative;
    color: var(--text-main);
}

.topbar-icon-link:hover {
    background: color-mix(in srgb, var(--bg-body) 70%, var(--bg-card));
}

.topbar-branch {
    border-color: var(--border-color);
    background: var(--bg-body);
}

.topbar-branch .form-select {
    cursor: pointer;
    max-width: 220px;
    padding-left: 0;
    color: var(--text-main);
}

@media (min-width: 992px) {
    body.with-sidebar .topbar,
    html.with-sidebar .topbar {
        left: var(--sidebar-docked-width, 280px);
        width: calc(100% - var(--sidebar-docked-width, 280px));
    }

    html.no-sidebar .topbar,
    html.sidebar-collapsed .topbar,
    body.no-sidebar .topbar,
    body.sidebar-collapsed .topbar {
        left: 0;
        width: 100%;
    }
}

.message-center-dropdown {
    z-index: calc(var(--z-topbar) + 5);
}

.topbar-chat-dropdown {
    width: min(460px, 92vw);
    border-radius: 14px;
    overflow: hidden;
}

.topbar-chat-head {
    background: color-mix(in srgb, var(--bg-card) 92%, var(--primary-light));
}

.topbar-chat-body {
    max-height: min(520px, calc(100vh - 120px));
    overflow: hidden;
}

.topbar-chat-list {
    max-height: 380px;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.topbar-chat-item {
    width: 100%;
    border: 0;
    background: transparent;
    padding: 0.75rem 0.85rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-align: left;
}

.topbar-chat-item:hover {
    background: color-mix(in srgb, var(--bg-body) 55%, var(--bg-card));
}

.topbar-chat-actions {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
}

.topbar-chat-reply {
    border-radius: 9999px;
    width: 34px;
    height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.topbar-chat-item-unread .topbar-chat-title {
    font-weight: 700;
}

.topbar-chat-avatar {
    width: 38px;
    height: 38px;
    border-radius: 9999px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    background: color-mix(in srgb, var(--primary) 12%, var(--bg-card));
    color: var(--primary);
    border: 1px solid color-mix(in srgb, var(--primary) 25%, var(--border-color));
    flex: 0 0 auto;
}

.topbar-chat-title {
    font-size: 0.9rem;
    color: var(--text-main);
    line-height: 1.1;
}

.topbar-chat-snippet {
    font-size: 0.82rem;
    color: var(--text-muted);
    line-height: 1.2;
}

.topbar-chat-time {
    font-size: 0.72rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.topbar-chat-thread-head {
    background: var(--bg-card);
}

.topbar-chat-messages {
    max-height: 300px;
    overflow-y: auto;
    padding: 0.75rem 0.85rem;
    background: color-mix(in srgb, var(--bg-body) 70%, var(--bg-card));
}

.topbar-chat-msg {
    display: flex;
    flex-direction: column;
    margin-bottom: 0.65rem;
}

.topbar-chat-msg.mine {
    align-items: flex-end;
}

.topbar-chat-msg.theirs {
    align-items: flex-start;
}

.topbar-chat-msg-meta {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.topbar-chat-msg-bubble {
    max-width: 80%;
    padding: 0.55rem 0.65rem;
    border-radius: 12px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    color: var(--text-main);
    font-size: 0.88rem;
    line-height: 1.25;
    word-break: break-word;
    white-space: pre-wrap;
}

.topbar-chat-msg.mine .topbar-chat-msg-bubble {
    background: color-mix(in srgb, var(--primary) 12%, var(--bg-card));
    border-color: color-mix(in srgb, var(--primary) 30%, var(--border-color));
}

.topbar-chat-composer textarea {
    resize: none;
}

@media (max-width: 575.98px) {
    .message-center-dropdown {
        width: min(92vw, 420px) !important;
        max-width: 92vw !important;
    }
}

.mobile-close-btn {
    display: none;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1.5rem;
    margin-left: auto;
    cursor: pointer;
}

.sidebar-footer {
    margin-top: auto;
    padding: 1rem;
    border-top: 1px solid rgba(255,255,255,0.05);
    background-color: var(--sidebar-bg);
}

body.workspace-fullscreen #page-content-wrapper {
    padding-top: 0 !important;
}

body.workspace-fullscreen #sidebar-wrapper {
    transform: translateX(-100%) !important;
}

body.workspace-fullscreen .topbar,
body.workspace-fullscreen .topbar {
    transform: translateY(-100%) !important;
}

body.workspace-fullscreen {
    --sidebar-width: 0px;
}

body.workspace-fullscreen footer,
body.pos-screen footer,
body.chat-screen footer {
    display: none !important;
}

body.workspace-fullscreen .container-fluid,
body.pos-screen .container-fluid,
body.chat-screen .container-fluid {
    padding: 0 !important;
}

body.workspace-fullscreen main,
body.pos-screen main,
body.chat-screen main {
    height: 100vh !important;
    overflow-y: auto;
}
