/* =====================================================================
   PrevCS · Sidebar navigation
   ===================================================================== */

.sidebar {
  /* Light mode: the bg is now a card surface, so the on-sidebar text token
     (a near-white meant for the old dark panel) must flip to dark ink. All
     the color-mix(... --text-on-sidebar ...) rules below inherit this. */
  --text-on-sidebar: var(--text-primary);
  position: sticky;
  top: calc(var(--header-height) + var(--content-gap));
  height: calc(100dvh - var(--header-height) - (var(--content-gap) * 2));
  max-height: calc(100dvh - var(--header-height) - (var(--content-gap) * 2));
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-self: start;
  background: var(--bg-card);
  color: var(--text-on-sidebar);
  border-radius: var(--radius-lg);
  border: 1px solid var(--hair);
  /* A touch more elevation + an accent hairline at the top so the sidebar
     reads as distinct from the content cards without being heavy. */
  box-shadow: var(--shadow);
  width: var(--sidebar-width);
  transition: width var(--transition-slow), padding var(--transition-slow);
  overflow: hidden;
}
/* Once collapsed, lift overflow:hidden so the footer tooltip can escape
   the sidebar bounds. The width transition is finished by then. Also bump
   z-index so the tooltip paints over sibling content (kpi cards, etc) —
   .sidebar has position:sticky which traps z-index inside its stacking
   context, so this must live on the sidebar itself. */
/* Subtle accent hairline along the top edge — a small identity cue that
   separates the sidebar from the plain content cards. */
.sidebar::before {
  content: "";
  position: absolute;
  top: 0;
  /* Inset past the card's corner radius: when collapsed the sidebar lifts
     overflow:hidden (footer tooltip) and border-radius stops clipping this
     bar, so it must never reach the rounded corners on its own. */
  left: var(--radius-lg);
  right: var(--radius-lg);
  height: 2px;
  background: linear-gradient(90deg, var(--accent), color-mix(in oklab, var(--accent) 30%, transparent));
  opacity: .55;
  pointer-events: none;
  border-radius: 0 0 999px 999px;
}

.sidebar.is-collapsed { overflow: visible; z-index: 50; }
.sidebar.is-collapsed {
  width: var(--sidebar-width-collapsed);
  padding: 18px 10px;
}

body[data-theme="dark"] .sidebar {
  /* Dark panel: keep the light ink token it was originally designed for. */
  --text-on-sidebar: #F2F1EC;
  background: var(--bg-2);
  border-color: var(--hair-2);
  box-shadow: 0 1px 0 rgba(255,255,255,.03), 0 18px 40px -16px rgba(0,0,0,.6);
}

.sidebar__head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 0 6px;
  min-width: 0;
}
.sidebar.is-collapsed .sidebar__head {
  justify-content: center;
  padding: 0;
  gap: 0;
}
.sidebar__title {
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: color-mix(in oklab, var(--text-on-sidebar) 60%, transparent);
  font-weight: var(--font-weight-medium);
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  transition: opacity var(--transition-fast), max-width var(--transition-slow);
  max-width: 200px;
}
.sidebar.is-collapsed .sidebar__title {
  opacity: 0;
  max-width: 0;
}

/* burger */
.burger {
  width: 26px; height: 26px;
  position: relative;
  cursor: pointer;
  display: inline-block;
  flex: 0 0 auto;
}
.burger input { position: absolute; inset: 0; opacity: 0; cursor: pointer; padding: 0; border: 0; width: 100%; height: 100%; }
.burger span {
  position: absolute;
  left: 4px; right: 4px;
  height: 1.5px;
  background: var(--text-on-sidebar);
  border-radius: 2px;
  transition: transform var(--transition-fast), opacity var(--transition-fast);
}
.burger span:nth-child(2) { top: 8px; }
.burger span:nth-child(3) { top: 13px; }
.burger span:nth-child(4) { top: 18px; }
/* When the menu is EXPANDED (sidebar not collapsed → checkbox unchecked)
   the burger morphs into an ✕ — clicking it will close the menu.
   When collapsed, it stays as ☰ — clicking it will open the menu. */
.burger:not(:has(input:checked)) span:nth-child(2) { transform: translateY(5px) rotate(45deg); }
.burger:not(:has(input:checked)) span:nth-child(3) { opacity: 0; }
.burger:not(:has(input:checked)) span:nth-child(4) { transform: translateY(-5px) rotate(-45deg); }

/* nav */
.sidebar__nav {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  gap: 2px;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 2px;
  position: relative;
}

/* El highlight deslizante (.nav-hl) vive en styles.css: lo comparten el
   sidebar y el dropdown del header. */
.menu-link {
  display: grid;
  grid-template-columns: 22px 1fr;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  font-size: 15.5px;
  color: color-mix(in oklab, var(--text-on-sidebar) 80%, transparent);
  transition: background var(--transition-fast), color var(--transition-fast);
}
.menu-link:hover {
  background: color-mix(in oklab, var(--text-on-sidebar) 6%, transparent);
  color: var(--text-on-sidebar);
}
/* Activo neutro (no acento): una superficie gris apenas más oscura que el
   panel, con la tinta a full. Funciona igual en claro y oscuro porque el
   mix se hace contra --text-on-sidebar, que ya invierte por tema. */
.menu-link.is-active {
  background: color-mix(in oklab, var(--text-on-sidebar) 9%, transparent);
  color: var(--text-on-sidebar);
  font-weight: var(--font-weight-medium);
}
.menu-link.is-active .menu-link__icon { color: var(--text-on-sidebar); }
.menu-link__icon {
  display: inline-grid; place-items: center;
  width: 22px; height: 22px;
  color: color-mix(in oklab, var(--text-on-sidebar) 65%, transparent);
}
.menu-link__icon svg { width: 20px; height: 20px; }
.menu-link__label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity var(--transition-fast);
}
.sidebar.is-collapsed .menu-link__label { opacity: 0; pointer-events: none; }
/* When collapsed, center the icon column within the link so each icon
   sits exactly on the sidebar's vertical centerline (matching the burger
   above). Symmetric padding + justify-content:center kills the 12px
   left-padding offset that pulled icons left of the burger. The 0-width
   label column must also have its `gap` zeroed — otherwise the gap
   still claims 12px of width and pushes the icon off-axis. */
.sidebar.is-collapsed .menu-link {
  grid-template-columns: 22px 0fr;
  justify-content: center;
  padding: 10px 0;
  gap: 0;
}

/* ─── Stock: botón desplegable + sublista de espacios ─────────────── */
.menu-link--toggle {
  grid-template-columns: 22px 1fr auto;
  width: 100%;
  border: 0;
  background: none;
  font: inherit;
  font-size: 15.5px;
  text-align: left;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}
.menu-link__chevron {
  display: inline-grid;
  place-items: center;
  width: 16px; height: 16px;
  color: color-mix(in oklab, var(--text-on-sidebar) 45%, transparent);
  transition: transform var(--transition-normal), opacity var(--transition-fast);
}
.menu-link__chevron svg { width: 16px; height: 16px; }
.menu-link--toggle.is-open .menu-link__chevron { transform: rotate(180deg); }
.sidebar.is-collapsed .menu-link--toggle { grid-template-columns: 22px 0fr; }
.sidebar.is-collapsed .menu-link__chevron { display: none; }

/* El alto se anima con grid-template-rows 0fr→1fr: no necesita medir el
   contenido y no rompe si cambia la cantidad de espacios. */
.menu-sub {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows var(--transition-slow), opacity var(--transition-fast);
  opacity: 0;
}
.menu-sub > .menu-sub__inner {
  overflow: hidden;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding-left: 17px;
  /* Guía vertical tenue, al estilo de los árboles de navegación limpios. */
  margin-left: 17px;
  border-left: 1px solid color-mix(in oklab, var(--text-on-sidebar) 12%, transparent);
}
.menu-sub.is-open {
  grid-template-rows: 1fr;
  opacity: 1;
}
.menu-sub.is-open > .menu-sub__inner { padding-top: 2px; padding-bottom: 2px; }
.sidebar.is-collapsed .menu-sub { grid-template-rows: 0fr; opacity: 0; }

.menu-sub__link {
  display: grid;
  grid-template-columns: 6px 1fr;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 14.5px;
  color: color-mix(in oklab, var(--text-on-sidebar) 68%, transparent);
  transition: background var(--transition-fast), color var(--transition-fast);
  min-width: 0;
}
.menu-sub__link:hover {
  background: color-mix(in oklab, var(--text-on-sidebar) 6%, transparent);
  color: var(--text-on-sidebar);
}
.menu-sub__link.is-active {
  background: color-mix(in oklab, var(--text-on-sidebar) 9%, transparent);
  color: var(--text-on-sidebar);
  font-weight: var(--font-weight-medium);
}
.menu-sub__dot {
  width: 5px; height: 5px;
  border-radius: 999px;
  background: currentColor;
  opacity: .45;
}
.menu-sub__link.is-active .menu-sub__dot { opacity: .8; }
.menu-sub__label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.menu-sub__empty {
  margin: 0;
  padding: 8px 10px;
  font-size: 13.5px;
  color: color-mix(in oklab, var(--text-on-sidebar) 50%, transparent);
}

@media (prefers-reduced-motion: reduce) {
  .menu-sub, .menu-link__chevron { transition: none; }
}

/* footer slot — menu secundario (Administrar cuenta / Ayuda).
   La línea divisoria respira lo mismo arriba que abajo (10px). */
.sidebar__foot {
  /* Simetrico: la separacion sobre la linea es la misma que debajo. */
  margin-top: auto;
  padding: 10px 0 0;
  border-top: 1px solid color-mix(in oklab, var(--text-on-sidebar) 12%, transparent);
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
}
.sidebar.is-collapsed .sidebar__foot { padding: 10px 0 0; }

/* ─── Mobile backdrop (default: not rendered) ─────────────── */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 16, 0.42);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 90;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition-slow);
}
.sidebar-backdrop.is-active {
  opacity: 1;
  pointer-events: auto;
}

@media (max-width: 900px) {
  /* Drawer: hidden off-canvas by default, slides in on toggle */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    max-height: 100dvh;
    width: min(82vw, 304px);
    max-width: 100%;
    padding: 22px 16px;
    border-radius: 0 var(--radius-xl) var(--radius-xl) 0;
    border-right: 1px solid var(--hair);
    z-index: 100;
    transform: translateX(-101%);
    transition: transform var(--transition-slow), box-shadow var(--transition-slow);
    box-shadow: none;
    /* Scroll lives on the nav so the user-menu dropdown can escape upward
       without being clipped by the drawer's overflow box. */
    overflow: visible;
  }
  /* Cancel desktop is-collapsed quirks on mobile */
  .sidebar.is-collapsed {
    width: min(82vw, 304px);
    padding: 22px 16px;
    overflow: visible;
  }
  .sidebar.is-collapsed .sidebar__title,
  .sidebar.is-collapsed .menu-link__label { opacity: 1; max-width: none; pointer-events: auto; }
  .sidebar.is-collapsed .menu-link { grid-template-columns: 22px 1fr; gap: 12px; padding: 10px 12px; justify-content: start; }
  .sidebar.is-collapsed .menu-link--toggle { grid-template-columns: 22px 1fr auto; }
  .sidebar.is-collapsed .menu-link__chevron { display: inline-grid; }
  .sidebar.is-collapsed .menu-sub.is-open { grid-template-rows: 1fr; opacity: 1; }
  .sidebar.is-collapsed .sidebar__head { justify-content: space-between; padding: 0 6px; gap: 8px; }

  .sidebar.is-drawer-open {
    transform: translateX(0);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.18);
  }
  body[data-theme="dark"] .sidebar.is-drawer-open {
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.5);
  }

  .sidebar-backdrop { display: block; }

  /* Hide the in-drawer burger; the topbar burger is the single toggle */
  .sidebar__head .burger { display: none; }
  .sidebar__head { padding: 4px 6px 6px; }

  /* Lock body scroll while drawer is open */
  body.is-drawer-open { overflow: hidden; }
}

/* =====================================================================
   User-menu when relocated inside the mobile sidebar (.user-menu--in-sidebar)
   ===================================================================== */
.sidebar__user-slot {
  margin-top: auto;
  padding: 6px 4px 0;
}
.sidebar__user-slot:empty { display: none; }

.user-menu--in-sidebar { width: 100%; }

.user-menu--in-sidebar > .user-chip {
  width: 100%;
  justify-content: flex-start;
  gap: 10px;
  padding: 8px 14px 8px 6px;
  background: color-mix(in oklab, var(--text-on-sidebar) 6%, transparent);
  border-color: color-mix(in oklab, var(--text-on-sidebar) 14%, transparent);
  color: var(--text-on-sidebar);
}
.user-menu--in-sidebar > .user-chip:hover {
  background: color-mix(in oklab, var(--text-on-sidebar) 11%, transparent);
  border-color: color-mix(in oklab, var(--text-on-sidebar) 22%, transparent);
}
.user-menu--in-sidebar > .user-chip .user-chip__label {
  color: var(--text-on-sidebar);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Panel opens upward, full-width of the slot */
.user-menu--in-sidebar .user-menu__panel {
  top: auto;
  bottom: calc(100% + 8px);
  left: 0;
  right: 0;
  min-width: 0;
  transform: translateY(4px);
}
.user-menu--in-sidebar.is-open .user-menu__panel,
.user-menu--in-sidebar[open] .user-menu__panel { transform: none; }
.user-menu--in-sidebar.is-closing .user-menu__panel { transform: translateY(4px); }

/* The drawer's overflow would otherwise clip the dropdown — allow the
   panel to paint over the surrounding chrome while the chip-row still
   sits cleanly at the bottom. */
@media (max-width: 900px) {
  .sidebar { overflow: visible; }
  .sidebar__nav { overflow-y: auto; overflow-x: hidden; flex: 1 1 auto; min-height: 0; }
}
