:root {
  --layout-topbar-height: 56px;
  --layout-sidebar-width: 240px;
  --layout-sidebar-collapsed: 64px;
  --layout-mobile-nav-height: 60px;
  --layout-content-gap-top: 0.25rem;
  --layout-content-gap-side: 0.625rem;
  --layout-content-gap-bottom: 0.5rem;
  --layout-content-gap-mobile-top: 0.25rem;
  --layout-content-gap-mobile-side: 0.5rem;
  --layout-content-gap-mobile-bottom: 0.5rem;
  --layout-chat-gap-top: 0.25rem;
  --layout-chat-gap-side: 0.5rem;
  --layout-chat-gap-bottom: 0.5rem;
  --layout-sidebar-bg: #ffffff;
  --layout-sidebar-border: var(--border, #e1e5ef);
  --layout-topbar-bg: #ffffff;
  --layout-mobile-nav-bg: #ffffff;
  --layout-item-hover: rgba(74, 111, 165, 0.06);
  --layout-item-active: rgba(74, 111, 165, 0.1);
  --layout-item-active-border: var(--color-primary, #4a6fa5);
  --layout-transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

:where(
  .app-topbar__toggle,
  .app-topbar__logout,
  .app-topbar__font,
  .app-sidebar__item,
  .app-sidebar__back,
  .app-mobile-nav__item,
  .app-mobile-nav__more-item
):focus-visible {
  outline: none;
  box-shadow: var(--focus-ring-shadow, 0 0 0 3px rgba(165, 184, 217, 0.35));
}

/* ================================================================
   App Layout Foundation
   ================================================================ */
html,
body.has-admin-layout {
  overflow-x: hidden;
}

body.has-admin-layout .app-content {
  transition: padding var(--layout-transition);
  max-width: none;
  margin-left: 0;
  margin-right: 0;
}

@media (min-width: 992px) {
  body.has-admin-layout .app-content {
    padding-top: calc(var(--layout-topbar-height) + var(--layout-content-gap-top)) !important;
    padding-left: calc(var(--layout-sidebar-width) + var(--layout-content-gap-side)) !important;
    padding-right: var(--layout-content-gap-side) !important;
    padding-bottom: var(--layout-content-gap-bottom) !important;
  }

  body.has-admin-layout.sidebar-collapsed .app-content {
    padding-left: calc(var(--layout-sidebar-collapsed) + var(--layout-content-gap-side)) !important;
  }
}

@media (max-width: 991.98px) {
  body.has-admin-layout {
    --layout-topbar-height: 48px;
    --layout-mobile-nav-height: 58px;
    --layout-content-gap-mobile-top: 0.125rem;
    --layout-content-gap-mobile-side: 0.375rem;
    --layout-content-gap-mobile-bottom: 0.25rem;
  }

  body.has-admin-layout .app-content {
    padding-top: calc(var(--layout-topbar-height) + var(--layout-content-gap-mobile-top)) !important;
    padding-left: var(--layout-content-gap-mobile-side) !important;
    padding-right: var(--layout-content-gap-mobile-side) !important;
    padding-bottom: calc(var(--layout-mobile-nav-height) + var(--layout-content-gap-mobile-bottom)) !important;
  }
}

/* ================================================================
   Chat Page Exceptions (chat-redesign.css conflicts)
   ================================================================ */
body.has-admin-layout.page-chat .app-content {
  display: flex;
  flex-direction: column;
  padding: 0.75rem 0.75rem 1rem !important;
}

@media (min-width: 992px) {
  body.has-admin-layout.page-chat .app-content {
    padding-top: calc(var(--layout-topbar-height) + var(--layout-chat-gap-top)) !important;
    padding-left: calc(var(--layout-sidebar-width) + var(--layout-chat-gap-side)) !important;
    padding-right: var(--layout-chat-gap-side) !important;
    padding-bottom: var(--layout-chat-gap-bottom) !important;
  }
  body.has-admin-layout.sidebar-collapsed.page-chat .app-content {
    padding-left: calc(var(--layout-sidebar-collapsed) + var(--layout-chat-gap-side)) !important;
  }
}

@media (max-width: 991.98px) {
  body.has-admin-layout.page-chat .app-content {
    padding: 0 !important;
    padding-top: var(--layout-topbar-height) !important;
    padding-bottom: var(--layout-mobile-nav-height) !important;
  }
}

/* ================================================================
   Top Bar
   ================================================================ */
.app-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--layout-topbar-height);
  background: var(--layout-topbar-bg);
  border-bottom: 1px solid var(--layout-sidebar-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  z-index: 1030;
  gap: 0.75rem;
}

.app-topbar__left,
.app-topbar__right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.app-topbar__toggle {
  display: none !important;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-md, 8px);
  background: transparent;
  color: var(--text-sub, #5c6372);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: var(--transition-base);
}

.app-topbar__toggle:hover {
  background: var(--layout-item-hover);
  color: var(--color-primary);
}

@media (min-width: 992px) {
  .app-topbar__toggle[data-desktop-toggle] {
    display: inline-flex !important;
  }
}

@media (max-width: 991.98px) {
  .app-topbar__toggle[data-mobile-toggle] {
    display: inline-flex !important;
  }

  .app-topbar {
    gap: 0.45rem;
    padding: 0 0.5rem;
  }

  .app-topbar__left,
  .app-topbar__right {
    gap: 0.45rem;
  }

  .app-topbar__toggle {
    width: 32px;
    height: 32px;
    border-radius: 7px;
  }
}

.app-topbar__brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-main, #1e2430);
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
  white-space: nowrap;
}

.app-topbar__brand i {
  color: var(--color-primary);
  font-size: 1.15rem;
}

.app-topbar__title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-topbar__spacer {
  flex: 1;
}

/* User area */
.app-topbar__user {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.app-topbar__role {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.25rem 0.7rem;
  background: var(--bg-body, #f6f7fb);
  border: 1px solid var(--layout-sidebar-border);
  border-radius: var(--radius-pill, 999px);
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-sub);
  white-space: nowrap;
}

.app-topbar__role i {
  color: var(--color-primary);
  font-size: 0.75rem;
}

.app-topbar__logout {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: var(--radius-md, 8px);
  background: transparent;
  color: var(--text-muted, #8b92a3);
  cursor: pointer;
  transition: var(--transition-base);
}

.app-topbar__logout:hover {
  background: rgba(210, 85, 90, 0.08);
  color: var(--color-danger);
}

/* ================================================================
   Sidebar (Desktop)
   ================================================================ */
.app-sidebar {
  position: fixed;
  top: var(--layout-topbar-height);
  left: 0;
  bottom: 0;
  width: var(--layout-sidebar-width);
  background: var(--layout-sidebar-bg);
  border-right: 1px solid var(--layout-sidebar-border);
  display: flex;
  flex-direction: column;
  z-index: 1020;
  transition: width var(--layout-transition);
  overflow: hidden;
}

body.sidebar-collapsed .app-sidebar {
  width: var(--layout-sidebar-collapsed);
}

@media (max-width: 991.98px) {
  .app-sidebar {
    transform: translateX(-100%);
    transition: transform var(--layout-transition);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.08);
  }

  body:not(.sidebar-open) .app-sidebar {
    visibility: hidden;
    pointer-events: none;
  }

  body.sidebar-open .app-sidebar {
    transform: translateX(0);
    visibility: visible;
    pointer-events: auto;
  }
}

/* Scrollable nav area */
.app-sidebar__scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.5rem 0.75rem;
  scrollbar-width: thin;
}

body.sidebar-collapsed .app-sidebar__scroll {
  padding: 0.5rem;
}

.app-sidebar__scroll::-webkit-scrollbar {
  width: 4px;
}

.app-sidebar__scroll::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 4px;
}

.app-sidebar:hover .app-sidebar__scroll::-webkit-scrollbar-thumb {
  background: var(--border);
}

/* Sidebar sections */
.app-sidebar__section-title {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 0.75rem 0.75rem 0.35rem;
  white-space: nowrap;
  opacity: 1;
  transition: opacity var(--layout-transition);
}

body.sidebar-collapsed .app-sidebar__section-title {
  display: none;
}

/* Nav items */
.app-sidebar__nav {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  margin-bottom: 0.5rem;
}

.app-sidebar__item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius-md, 8px);
  color: var(--text-sub);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease;
  position: relative;
  white-space: nowrap;
}

body.sidebar-collapsed .app-sidebar__item {
  justify-content: center;
  gap: 0;
  padding: 0.6rem 0;
}

.app-sidebar__item:hover {
  background: var(--layout-item-hover);
  color: var(--color-primary);
}

.app-sidebar__item.is-active {
  background: var(--layout-item-active);
  color: var(--color-primary);
  font-weight: 600;
}

.app-sidebar__item.is-active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 18px;
  background: var(--layout-item-active-border);
  border-radius: 0 3px 3px 0;
}

.app-sidebar__item i {
  width: 20px;
  text-align: center;
  font-size: 0.95rem;
  flex-shrink: 0;
}

.app-sidebar__text {
  white-space: nowrap;
  opacity: 1;
  transition: opacity var(--layout-transition);
}

body.sidebar-collapsed .app-sidebar__text {
  display: none;
}

/* Badge */
.app-sidebar__badge {
  margin-left: auto;
  flex-shrink: 0;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  background: var(--color-danger);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity var(--layout-transition);
}

body.sidebar-collapsed .app-sidebar__badge {
  position: absolute;
  top: 6px;
  right: 6px;
  margin-left: 0;
  min-width: 14px;
  height: 14px;
  font-size: 0.6rem;
  padding: 0 3px;
}

/* Sidebar footer */
.app-sidebar__footer {
  padding: 0.75rem;
  border-top: 1px solid var(--layout-sidebar-border);
  max-height: 76px;
  overflow: hidden;
  opacity: 1;
  transition:
    opacity var(--layout-transition),
    padding var(--layout-transition),
    max-height var(--layout-transition),
    border-color var(--layout-transition);
}

body.sidebar-collapsed .app-sidebar__footer {
  max-height: 0;
  padding: 0 0.5rem;
  border-top-color: transparent;
  opacity: 0;
  pointer-events: none;
}

.app-sidebar__back {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.5rem;
  border: 1px solid var(--layout-sidebar-border);
  border-radius: var(--radius-md, 8px);
  background: var(--bg-body, #f6f7fb);
  color: var(--text-sub);
  font-size: 0.85rem;
  font-weight: 500;
  text-decoration: none;
  transition: var(--transition-base);
}

.app-sidebar__back:hover {
  background: var(--layout-item-hover);
  color: var(--color-primary);
  border-color: var(--color-primary-light);
}

.app-sidebar-tooltip {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1045;
  max-width: 220px;
  padding: 0.42rem 0.62rem;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: var(--radius-md, 8px);
  background: rgba(30, 36, 48, 0.96);
  box-shadow: 0 10px 28px rgba(30, 36, 48, 0.18);
  color: #fff;
  font-size: 0.82rem;
  font-weight: 600;
  line-height: 1.25;
  opacity: 0;
  pointer-events: none;
  transform: translate(-4px, -50%);
  transition: opacity 0.12s ease, transform 0.12s ease;
  white-space: nowrap;
}

.app-sidebar-tooltip::before {
  content: '';
  position: absolute;
  top: 50%;
  left: -5px;
  width: 10px;
  height: 10px;
  background: rgba(30, 36, 48, 0.96);
  transform: translateY(-50%) rotate(45deg);
}

.app-sidebar-tooltip.is-visible {
  opacity: 1;
  transform: translate(0, -50%);
}

@media (max-width: 991.98px) {
  .app-sidebar-tooltip {
    display: none;
  }
}

/* ================================================================
   Mobile Bottom Navigation
   ================================================================ */
.app-mobile-nav {
  display: none;
}

/* Keep the mobile "more" sheet out of desktop document flow. */
.app-mobile-nav__more-sheet {
  display: none;
}

.app-mobile-nav__more-sheet[aria-hidden="true"] {
  display: none;
}

@media (max-width: 991.98px) {
  .app-mobile-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--layout-mobile-nav-height);
    background: var(--layout-mobile-nav-bg);
    border-top: 1px solid var(--layout-sidebar-border);
    z-index: 1030;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .app-mobile-nav::-webkit-scrollbar {
    display: none;
  }

  .app-mobile-nav__inner {
    display: flex;
    align-items: stretch;
    min-width: 100%;
    padding: 0 0.15rem;
  }

  .app-mobile-nav__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-width: 56px;
    max-width: 72px;
    flex: 1 0 auto;
    padding: 0.25rem 0.2rem;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.6rem;
    font-weight: 500;
    position: relative;
    border: none;
    background: none;
    cursor: pointer;
    transition: color 0.15s ease;
  }

  .app-mobile-nav__item.is-active {
    color: var(--color-primary);
    font-weight: 600;
  }

  .app-mobile-nav__item.is-active::after {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 22px;
    height: 2px;
    background: var(--color-primary);
    border-radius: 0 0 3px 3px;
  }

  .app-mobile-nav__item i {
    font-size: 1rem;
    line-height: 1;
  }

  .app-mobile-nav__label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

  .app-mobile-nav__divider {
    width: 1px;
    background: var(--layout-sidebar-border);
    margin: 0.45rem 0.1rem;
    flex-shrink: 0;
  }

  .app-mobile-nav__badge {
    position: absolute;
    top: 2px;
    right: 6px;
    min-width: 15px;
    height: 15px;
    padding: 0 4px;
    background: var(--color-danger);
    color: #fff;
    font-size: 0.6rem;
    font-weight: 700;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* More sheet */
  .app-mobile-nav__more-sheet {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1040;
    flex-direction: column;
    justify-content: flex-end;
  }

  .app-mobile-nav__more-sheet.is-open {
    display: flex;
  }

  .app-mobile-nav__more-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(18, 25, 38, 0.45);
  }

  .app-mobile-nav__more-panel {
    position: relative;
    background: var(--bg-card, #fff);
    border-radius: 16px 16px 0 0;
    padding: 0.7rem;
    max-height: 66vh;
    overflow-y: auto;
    animation: sheetSlideUp 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  }

  @keyframes sheetSlideUp {
    from {
      transform: translateY(100%);
    }
    to {
      transform: translateY(0);
    }
  }

  .app-mobile-nav__more-handle {
    width: 34px;
    height: 3px;
    background: var(--border);
    border-radius: 999px;
    margin: 0 auto 0.65rem;
  }

  .app-mobile-nav__more-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-main);
    margin-bottom: 0.45rem;
    padding-left: 0.25rem;
  }

  .app-mobile-nav__more-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.35rem;
  }

  .app-mobile-nav__more-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    padding: 0.5rem 0.2rem;
    border-radius: var(--radius-md, 8px);
    color: var(--text-sub);
    text-decoration: none;
    font-size: 0.68rem;
    font-weight: 500;
    transition: background 0.15s ease;
  }

  .app-mobile-nav__more-item.is-active {
    background: var(--layout-item-active);
    color: var(--color-primary);
    font-weight: 600;
  }

  .app-mobile-nav__more-item i {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-body, #f6f7fb);
    border-radius: 12px;
    font-size: 0.95rem;
  }

  .app-mobile-nav__more-item.is-active i {
    background: var(--layout-item-active);
  }
}

/* ================================================================
   Overlay
   ================================================================ */
.app-sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay, rgba(18, 25, 38, 0.35));
  z-index: 1015;
}

@media (max-width: 991.98px) {
  body.sidebar-open .app-sidebar-overlay {
    display: block;
  }
}

/* ================================================================
   Font Selector (moved to sidebar footer or topbar)
   ================================================================ */
.app-topbar__font {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0.15rem 0.25rem;
  border-radius: 6px;
  transition: background 0.15s ease;
}

.app-topbar__font:hover {
  background: var(--layout-item-hover);
}

@media (max-width: 991.98px) {
  .app-topbar__font {
    display: none;
  }
}

/* ================================================================
   Responsive Utilities
   ================================================================ */
@media (max-width: 991.98px) {
  .app-topbar__title {
    font-size: 0.88rem;
  }

  .app-topbar__role {
    display: none;
  }
}

@media (max-width: 575.98px) {
  .app-topbar {
    padding: 0 0.45rem;
  }
}

/* ================================================================
   Defensive: hide old cached navbar & unknown injected elements
   ================================================================ */

/* Hide old Bootstrap navbar if still cached by browser */
body.has-admin-layout .navbar,
body.has-admin-layout .app-navbar,
body.has-admin-layout .navbar-collapse,
body.has-admin-layout .navbar-nav,
body.has-admin-layout .navbar-toggler {
  display: none !important;
}

/* ================================================================
   Page-specific fixes
   ================================================================ */

/* ---- Settings page: sidebar must clear fixed topbar ---- */
body.has-admin-layout .settings-sidebar {
  top: var(--layout-topbar-height);
  height: calc(100vh - var(--layout-topbar-height));
}

body.has-admin-layout .settings-layout {
  min-height: calc(100vh - var(--layout-topbar-height));
}

/* ---- Categories pages: these screens do not use .app-content ---- */
@media (min-width: 992px) {
  body.has-admin-layout.page-categories .categories-page-wrapper,
  body.has-admin-layout.page-categories > .container-fluid {
    box-sizing: border-box;
    padding-top: calc(var(--layout-topbar-height) + var(--layout-content-gap-top)) !important;
    padding-left: calc(var(--layout-sidebar-width) + var(--layout-content-gap-side)) !important;
    padding-right: var(--layout-content-gap-side) !important;
    padding-bottom: var(--layout-content-gap-bottom) !important;
  }

  body.has-admin-layout.sidebar-collapsed.page-categories .categories-page-wrapper,
  body.has-admin-layout.sidebar-collapsed.page-categories > .container-fluid {
    padding-left: calc(var(--layout-sidebar-collapsed) + var(--layout-content-gap-side)) !important;
  }
}

@media (max-width: 991.98px) {
  body.has-admin-layout.page-categories .categories-page-wrapper,
  body.has-admin-layout.page-categories > .container-fluid {
    box-sizing: border-box;
    padding-top: calc(var(--layout-topbar-height) + var(--layout-content-gap-mobile-top)) !important;
    padding-left: var(--layout-content-gap-mobile-side) !important;
    padding-right: var(--layout-content-gap-mobile-side) !important;
    padding-bottom: calc(var(--layout-mobile-nav-height) + var(--layout-content-gap-mobile-bottom)) !important;
  }
}

body.has-admin-layout.page-categories .categories-page-wrapper {
  max-width: none !important;
  margin: 0 !important;
  overflow-x: clip;
}

body.has-admin-layout.page-categories > .container-fluid {
  margin-top: 0 !important;
}

body.has-admin-layout.page-categories #categoriesList.row {
  --bs-gutter-x: 0.75rem;
  max-width: 100%;
  margin-left: 0;
  margin-right: 0;
}

@media (max-width: 991.98px) {
  /* Push page-specific mobile toggles below our topbar so they don't overlap */
  body.has-admin-layout .mobile-menu-toggle {
    top: calc(var(--layout-topbar-height) + 0.5rem);
  }
}

/* ---- Chat page: hide global nav on mobile so chat owns the viewport ---- */
@media (max-width: 991.98px) {
  body.has-admin-layout.page-chat .app-topbar,
  body.has-admin-layout.page-chat .app-mobile-nav {
    display: none !important;
  }

  body.has-admin-layout.page-chat .app-content {
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  body.has-admin-layout.page-chat .app-sidebar-overlay {
    display: none !important;
  }
}

/* ---- Chat page desktop: preserve chat-redesign.css overflow behaviour ---- */
@media (min-width: 992px) {
  body.has-admin-layout.page-chat .app-content {
    overflow: hidden;
    min-height: 0;
  }
}
