/*
 * BaseLock App Shell
 * Hover-to-expand sidebar (Intentra pattern), topbar (mobile), content wrapper,
 * page header, skip link. All colors via app-tokens.css variables.
 */

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Restore [hidden] semantics — some component rules (e.g. .bl-btn { display: flex })
   would otherwise override the UA `display: none` from the `hidden` attribute. */
[hidden] {
  display: none !important;
}

/* App-shell pages set a data-route attribute on <body>; landing pages don't.
   Apply the themed body background only inside the authenticated app, so the
   landing CSS stays in charge of the marketing surface. */
body[data-route] {
  background: var(--bl-bg-secondary);
  color: var(--bl-text);
  margin: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Bigger, friendlier stat values — and a softer placeholder while data loads. */
.bl-stat__value {
  font-variant-numeric: tabular-nums;
}

.bl-stat__value:where(:not([data-loaded])):empty::after,
.bl-stat__value:where(:not([data-loaded]))[data-pending="true"]::after {
  content: "—";
  color: var(--bl-text-muted);
  letter-spacing: 0.04em;
}

/* ─── Skip link (accessibility) ─────────────────────────────────────────── */
.skip-link {
  position: fixed;
  top: var(--bl-space-2);
  left: var(--bl-space-2);
  z-index: 9999;
  padding: var(--bl-space-2) var(--bl-space-4);
  background: var(--bl-primary);
  color: #fff;
  border-radius: var(--bl-radius-sm);
  font-family: var(--bl-font);
  font-size: 0.875rem;
  font-weight: 600;
  text-decoration: none;
  transform: translateY(-200%);
  transition: transform 0.15s ease;
}

.skip-link:focus {
  transform: translateY(0);
  outline: 2px solid #fff;
  outline-offset: 2px;
}

/* ─── App layout root ────────────────────────────────────────────────────── */
.bl-app {
  display: flex;
  min-height: 100vh;
  background: var(--bl-bg-secondary);
  font-family: var(--bl-font);
  color: var(--bl-text);
}

/* ─── Sidebar (hover-to-expand) ──────────────────────────────────────────── */
.bl-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--bl-sidebar-w-collapsed);
  height: 100vh;
  background: var(--bl-surface);
  border-right: 1px solid var(--bl-border);
  display: flex;
  flex-direction: column;
  z-index: 200;
  overflow: hidden;
  transition: width 0.16s cubic-bezier(0.2, 0, 0, 1), box-shadow 0.16s cubic-bezier(0.2, 0, 0, 1);
}

/* Faint inner shadow on the right edge gives the sidebar a sense of depth
   without competing with content — especially helpful in light mode where the
   sidebar bg and the page bg are close. */
.bl-sidebar::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 1px;
  background: linear-gradient(
    180deg,
    transparent 0,
    var(--bl-border) 12%,
    var(--bl-border) 88%,
    transparent 100%
  );
  pointer-events: none;
}

.bl-sidebar:hover {
  width: var(--bl-sidebar-w-expanded);
  box-shadow: var(--bl-shadow-md);
}

/* Force expanded when explicitly opened (mobile drawer) */
.bl-sidebar.is-open {
  width: var(--bl-sidebar-w-expanded);
}

/* ─── Sidebar logo ───────────────────────────────────────────────────────── */
.bl-sidebar__logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 0;
  height: var(--bl-topbar-h);
  border-bottom: 1px solid var(--bl-border-light);
  text-decoration: none;
  color: var(--bl-text);
  flex-shrink: 0;
  transition: gap 0.16s cubic-bezier(0.2, 0, 0, 1), padding 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-sidebar__logo,
.bl-sidebar.is-open .bl-sidebar__logo {
  justify-content: flex-start;
  gap: var(--bl-space-3);
  padding: 0 var(--bl-space-5);
}

.bl-sidebar__logo:focus-visible {
  outline: 2px solid var(--bl-primary);
  outline-offset: -2px;
}

.bl-sidebar__logo-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  color: var(--bl-primary);
  font-size: 1.125rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bl-sidebar__logo-text {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--bl-text);
  white-space: nowrap;
  letter-spacing: -0.015em;
  opacity: 0;
  width: 0;
  overflow: hidden;
  transition: opacity 0.12s ease, width 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-sidebar__logo-text,
.bl-sidebar.is-open .bl-sidebar__logo-text {
  opacity: 1;
  width: auto;
}

/* ─── Sidebar nav ────────────────────────────────────────────────────────── */
.bl-sidebar__nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--bl-space-3);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--bl-space-1);
  scrollbar-width: thin;
  scrollbar-color: var(--bl-border) transparent;
}

/* Force ul/li in sidebar nav to fill width (defeats landing styles.css `nav { align-items: center }` leak) */
.bl-sidebar__nav ul,
.bl-sidebar__nav li {
  width: 100%;
}

.bl-sidebar__nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.bl-sidebar__nav::-webkit-scrollbar {
  width: 4px;
}

.bl-sidebar__nav::-webkit-scrollbar-track {
  background: transparent;
}

.bl-sidebar__nav::-webkit-scrollbar-thumb {
  background: var(--bl-border);
  border-radius: 2px;
}

/* ─── Nav section divider ────────────────────────────────────────────────── */
.bl-nav-divider {
  height: 1px;
  background: var(--bl-border-light);
  margin: var(--bl-space-3) var(--bl-space-2);
  flex-shrink: 0;
}

/* ─── Nav section label ──────────────────────────────────────────────────── */
.bl-nav-section {
  padding: 0;
  height: 0;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--bl-text-muted);
  white-space: nowrap;
  overflow: hidden;
  opacity: 0;
  transition: opacity 0.12s ease, height 0.16s cubic-bezier(0.2, 0, 0, 1), padding 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-nav-section,
.bl-sidebar.is-open .bl-nav-section {
  opacity: 1;
  height: auto;
  padding: var(--bl-space-3) var(--bl-space-2) var(--bl-space-1);
}

/* ─── Nav item ───────────────────────────────────────────────────────────── */
.bl-nav-item {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 8px;
  border-radius: 6px;
  color: var(--bl-text-secondary);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  transition: background 0.12s ease, color 0.12s ease, gap 0.16s cubic-bezier(0.2, 0, 0, 1), padding 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-nav-item,
.bl-sidebar.is-open .bl-nav-item {
  justify-content: flex-start;
  gap: 12px;
  padding: 8px 12px;
}

.bl-nav-item:hover {
  background: var(--bl-bg-secondary);
  color: var(--bl-text);
}

.bl-nav-item:focus-visible {
  outline: 2px solid var(--bl-primary);
  outline-offset: 2px;
}

.bl-nav-item.is-active,
.bl-nav-item[aria-current="page"] {
  background: var(--bl-bg-secondary);
  color: var(--bl-text);
  font-weight: 600;
}

.bl-nav-item.is-active::before,
.bl-nav-item[aria-current="page"]::before {
  content: "";
  position: absolute;
  left: -4px;
  top: 8px;
  bottom: 8px;
  width: 2px;
  border-radius: 2px;
  background: var(--bl-primary);
}

.bl-nav-item.is-active .bl-nav-item__icon,
.bl-nav-item[aria-current="page"] .bl-nav-item__icon {
  color: var(--bl-primary);
}

.bl-nav-item__icon {
  width: 18px;
  text-align: center;
  flex-shrink: 0;
  font-size: 0.875rem;
  color: var(--bl-text-muted);
  transition: color 0.12s ease;
}

.bl-nav-item:hover .bl-nav-item__icon {
  color: var(--bl-text-secondary);
}

.bl-nav-item__label {
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  width: 0;
  transition: opacity 0.12s ease, width 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-nav-item__label,
.bl-sidebar.is-open .bl-nav-item__label {
  opacity: 1;
  width: auto;
  flex: 1;
}

.bl-nav-item__badge {
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--bl-danger);
  color: #fff;
  line-height: 1.4;
  flex-shrink: 0;
  opacity: 0;
  width: 0;
  overflow: hidden;
  padding-left: 0;
  padding-right: 0;
  transition: opacity 0.12s ease, width 0.16s cubic-bezier(0.2, 0, 0, 1), padding 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-nav-item__badge,
.bl-sidebar.is-open .bl-nav-item__badge {
  opacity: 1;
  width: auto;
  padding: 2px 6px;
}

/* ─── Sidebar footer ─────────────────────────────────────────────────────── */
.bl-sidebar__footer {
  padding: var(--bl-space-3);
  border-top: 1px solid var(--bl-border-light);
  display: flex;
  flex-direction: column;
  gap: var(--bl-space-1);
  flex-shrink: 0;
}

/* ─── Theme toggle ───────────────────────────────────────────────────────── */
.bl-theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 10px 8px;
  border-radius: var(--bl-radius-md);
  color: var(--bl-text-secondary);
  font-size: 0.9375rem;
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  font-family: inherit;
  transition: background 0.12s ease, color 0.12s ease, gap 0.16s cubic-bezier(0.2, 0, 0, 1), padding 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-theme-toggle,
.bl-sidebar.is-open .bl-theme-toggle {
  justify-content: flex-start;
  gap: var(--bl-space-3);
  padding: 10px 12px;
}

.bl-theme-toggle:hover {
  background: var(--bl-bg-secondary);
  color: var(--bl-text);
}

.bl-theme-toggle:focus-visible {
  outline: 2px solid var(--bl-primary);
  outline-offset: 2px;
}

.bl-theme-toggle__icon {
  width: 20px;
  text-align: center;
  flex-shrink: 0;
  font-size: 1rem;
  color: var(--bl-text-muted);
}

.bl-theme-toggle__label {
  opacity: 0;
  width: 0;
  overflow: hidden;
  white-space: nowrap;
  transition: opacity 0.12s ease, width 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-theme-toggle__label,
.bl-sidebar.is-open .bl-theme-toggle__label {
  opacity: 1;
  width: auto;
}

/* Show sun icon in dark mode, moon icon in light mode */
.bl-theme-toggle__dark-icon {
  display: inline;
}

.bl-theme-toggle__light-icon {
  display: none;
}

html[data-theme="dark"] .bl-theme-toggle__dark-icon {
  display: none;
}

html[data-theme="dark"] .bl-theme-toggle__light-icon {
  display: inline;
}

/* ─── User menu ──────────────────────────────────────────────────────────── */
.bl-user-menu {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 8px;
  border-radius: var(--bl-radius-md);
  cursor: pointer;
  transition: background 0.12s ease, gap 0.16s cubic-bezier(0.2, 0, 0, 1), padding 0.16s cubic-bezier(0.2, 0, 0, 1);
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
  color: var(--bl-text);
}

.bl-sidebar:hover .bl-user-menu,
.bl-sidebar.is-open .bl-user-menu {
  justify-content: flex-start;
  gap: var(--bl-space-3);
  padding: 8px 12px;
}

.bl-user-menu:hover {
  background: var(--bl-bg-secondary);
}

.bl-user-menu:focus-visible {
  outline: 2px solid var(--bl-primary);
  outline-offset: 2px;
}

.bl-user-menu__avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bl-primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 13px;
  flex-shrink: 0;
}

.bl-user-menu__text {
  min-width: 0;
  overflow: hidden;
  opacity: 0;
  width: 0;
  transition: opacity 0.12s ease, width 0.16s cubic-bezier(0.2, 0, 0, 1);
}

.bl-sidebar:hover .bl-user-menu__text,
.bl-sidebar.is-open .bl-user-menu__text {
  opacity: 1;
  width: auto;
  flex: 1;
}

.bl-user-menu__name {
  font-size: 13px;
  font-weight: 600;
  color: var(--bl-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bl-user-menu__email {
  font-size: 11px;
  color: var(--bl-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── User popup menu ────────────────────────────────────────────────────── */
.bl-user-popup {
  display: none;
  position: absolute;
  bottom: calc(100% + 4px);
  left: var(--bl-space-3);
  right: var(--bl-space-3);
  background: var(--bl-surface);
  border: 1px solid var(--bl-border);
  border-radius: var(--bl-radius-md);
  box-shadow: var(--bl-shadow-lg);
  padding: 4px;
  z-index: 250;
}

.bl-user-popup.is-open {
  display: block;
}

.bl-user-popup__item {
  display: flex;
  align-items: center;
  gap: var(--bl-space-3);
  padding: 8px 12px;
  border-radius: var(--bl-radius-sm);
  color: var(--bl-danger);
  font-size: 0.875rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
  transition: background 0.12s ease;
}

.bl-user-popup__item:hover {
  background: var(--bl-bg-secondary);
}

.bl-sidebar__footer {
  position: relative;
}

/* ─── Topbar (mobile only, <1024px) ─────────────────────────────────────── */
.bl-topbar {
  display: none;
}

@media (max-width: 1024px) {
  .bl-topbar {
    display: flex;
    align-items: center;
    gap: var(--bl-space-3);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--bl-topbar-h);
    padding: 0 var(--bl-space-4);
    background: var(--bl-surface);
    border-bottom: 1px solid var(--bl-border);
    z-index: 300;
  }

  .bl-sidebar {
    transform: translateX(-100%);
    width: var(--bl-sidebar-w-expanded);
    transition: transform 0.25s ease;
    box-shadow: var(--bl-shadow-lg);
  }

  .bl-sidebar:hover {
    width: var(--bl-sidebar-w-expanded);
  }

  .bl-sidebar.is-open {
    transform: translateX(0);
  }

  /* On mobile, labels stay visible (drawer is always full-width) */
  .bl-sidebar .bl-sidebar__logo-text,
  .bl-sidebar .bl-nav-item__label,
  .bl-sidebar .bl-nav-item__badge,
  .bl-sidebar .bl-nav-section,
  .bl-sidebar .bl-theme-toggle__label,
  .bl-sidebar .bl-user-menu__text {
    opacity: 1;
    width: auto;
  }

  /* On mobile, restore left-aligned layout for nav rows */
  .bl-sidebar .bl-nav-item {
    justify-content: flex-start;
    gap: 12px;
    padding: 8px 12px;
  }

  .bl-sidebar .bl-nav-section {
    height: auto;
    padding: var(--bl-space-3) var(--bl-space-2) var(--bl-space-1);
  }

  .bl-sidebar .bl-theme-toggle {
    justify-content: flex-start;
    gap: var(--bl-space-3);
    padding: 10px 12px;
  }

  .bl-sidebar .bl-user-menu {
    justify-content: flex-start;
    gap: var(--bl-space-3);
    padding: 8px 12px;
  }

  /* Hide drawer logo on mobile — topbar shows brand already */
  .bl-sidebar__logo {
    display: none;
  }

  /* Drawer content starts below the fixed topbar */
  .bl-sidebar__nav {
    padding-top: calc(var(--bl-topbar-h) + var(--bl-space-3));
  }

  .bl-content {
    margin-left: 0 !important;
    padding-top: calc(var(--bl-topbar-h) + var(--bl-space-6)) !important;
  }
}

.bl-topbar__hamburger {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  color: var(--bl-text-secondary);
  border-radius: var(--bl-radius-sm);
  cursor: pointer;
  font-size: 1.125rem;
  transition: background 0.12s ease;
}

.bl-topbar__hamburger:hover {
  background: var(--bl-bg-secondary);
}

.bl-topbar__hamburger:focus-visible {
  outline: 2px solid var(--bl-primary);
  outline-offset: 2px;
}

.bl-topbar__brand {
  display: flex;
  align-items: center;
  gap: var(--bl-space-2);
  flex: 1;
  text-decoration: none;
  color: var(--bl-text);
  font-weight: 600;
  font-size: 1rem;
}

.bl-topbar__brand-icon {
  color: var(--bl-primary);
  font-size: 1.25rem;
}

.bl-topbar__avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bl-primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 13px;
  flex-shrink: 0;
}

/* ─── Mobile sidebar overlay ─────────────────────────────────────────────── */
.bl-sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.4);
  z-index: 190;
  animation: bl-fade-in 0.18s ease;
}

.bl-sidebar-overlay.is-visible {
  display: block;
}

@keyframes bl-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ─── Content area ───────────────────────────────────────────────────────── */
.bl-content {
  margin-left: var(--bl-sidebar-w-collapsed);
  min-height: 100vh;
  padding: var(--bl-space-8);
  display: flex;
  flex-direction: column;
  gap: var(--bl-space-6);
  max-width: 1440px;
  margin-right: auto;
}

/* On wide viewports, center the content within the area to the right of the sidebar. */
@media (min-width: 1600px) {
  .bl-content {
    margin-left: calc(var(--bl-sidebar-w-collapsed) + (100vw - var(--bl-sidebar-w-collapsed) - 1440px) / 2);
  }
}

@media (max-width: 1280px) {
  .bl-content {
    padding: var(--bl-space-6);
  }
}

@media (max-width: 768px) {
  .bl-content {
    padding: var(--bl-space-4);
  }
}

/* ─── Page header ────────────────────────────────────────────────────────── */
.bl-page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--bl-space-4);
  flex-wrap: wrap;
}

.bl-page-header__titles {
  display: flex;
  flex-direction: column;
  gap: var(--bl-space-1);
}

.bl-page-header h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--bl-text);
  letter-spacing: -0.015em;
  line-height: 1.25;
}

.bl-page-header__subtitle,
.bl-page-header > p {
  font-size: 0.875rem;
  color: var(--bl-text-secondary);
  margin: 0;
}

/* When a page header has h1 and p as direct children (no __titles wrapper),
   stack them on the left instead of letting `justify-content: space-between`
   flex them to opposite edges. */
.bl-page-header > h1,
.bl-page-header > h1 + p {
  flex-basis: 100%;
  margin: 0;
}

.bl-page-header__actions {
  display: flex;
  align-items: center;
  gap: var(--bl-space-2);
  flex-shrink: 0;
  flex-wrap: wrap;
}

@media (max-width: 640px) {
  .bl-page-header {
    flex-direction: column;
    align-items: stretch;
  }

  .bl-page-header__actions {
    justify-content: flex-start;
  }
}

/* ═══════════════════════════════════════════════════════════════════════════
   RAIL-MODE ALIGNMENT POLISH
   --
   The default sidebar is rail (72px wide) and expands on hover to
   240px. The icons in rail mode were misaligned and too small; this
   block centres them in the rail and bumps the visual weight so
   collapsed-mode is readable at a glance.
   ═══════════════════════════════════════════════════════════════════════════ */
@media (min-width: 1025px) {
  /* Rail icons: larger + centered + comfortable hit target. */
  .bl-sidebar:not(:hover):not(.is-open) .bl-nav-item {
    justify-content: center;
    padding: 10px 0;
    gap: 0;
  }
  .bl-sidebar:not(:hover):not(.is-open) .bl-nav-item__icon {
    width: 22px;
    font-size: 1.0625rem;
  }
  .bl-sidebar:not(:hover):not(.is-open) .bl-sidebar__logo {
    justify-content: center;
    padding: 0;
  }
  .bl-sidebar:not(:hover):not(.is-open) .bl-sidebar__logo-icon {
    width: 26px;
    height: 26px;
    font-size: 1.25rem;
  }
  /* In rail mode, the theme-toggle and user-menu both collapse to a
     single centred icon. Use identical padding so the moon/sun icon
     and the user avatar are horizontally aligned in the footer. */
  .bl-sidebar:not(:hover):not(.is-open) .bl-theme-toggle,
  .bl-sidebar:not(:hover):not(.is-open) .bl-user-menu {
    justify-content: center;
    padding: 8px 0;
    gap: 0;
  }
  /* Shrink the avatar slightly so it visually matches the 20px icon
     in the theme toggle, keeping both centred on the same vertical
     axis in the 72px rail. */
  .bl-sidebar:not(:hover):not(.is-open) .bl-user-menu__avatar {
    width: 26px;
    height: 26px;
    font-size: 11px;
  }
}
