/* ═══════════════════════════════════════════════════════════════════
   SIDEBAR — self-contained component
   States: expanded (default) / rail (desktop collapse) / drawer (mobile)
   Tokens consumed from the host app (:root in index.html).
   ═══════════════════════════════════════════════════════════════════ */

:root {
  --sb-expanded-w: 260px;
  --sb-rail-w: 72px;
  --sb-toggle-size: 28px;
  --sb-transition: .28s cubic-bezier(.2,.8,.2,1);
}

/* The host app sets --sidebar-w. We switch it based on state. */
body.sb-rail { --sidebar-w: var(--sb-rail-w); }

/* ── Backdrop (mobile drawer only) ── */
.sb-backdrop {
  position: fixed; inset: 0;
  background: rgba(3,5,12,0.55);
  backdrop-filter: blur(3px);
  z-index: 90;
  opacity: 0; pointer-events: none;
  transition: opacity .25s var(--ease,ease);
}
.sb-backdrop.show { opacity: 1; pointer-events: auto; }

/* ── Panel ── */
.sidebar {
  position: fixed; left: 0; top: 0; bottom: 0;
  width: var(--sb-expanded-w);
  background: linear-gradient(180deg, var(--bg-elev), var(--surface) 80%);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 100;
  backdrop-filter: blur(20px);
  transition: width var(--sb-transition), transform var(--sb-transition);
  overflow: visible;
}
.sidebar::after {
  content: '';
  position: absolute; top: 0; right: -1px;
  width: 1px; height: 100%;
  background: linear-gradient(180deg, transparent, var(--accent-soft) 20%, transparent 40%);
  opacity: .8; pointer-events: none;
}

/* Rail mode — desktop collapsed */
body.sb-rail .sidebar { width: var(--sb-rail-w); }

/* When rail, the whole sidebar interior becomes click-to-expand except
   for the explicit toggle button and nav items (which still navigate) */
body.sb-rail .sidebar { cursor: pointer; }
body.sb-rail .sidebar .sb-item,
body.sb-rail .sidebar .sb-toggle,
body.sb-rail .sidebar .sb-section { cursor: pointer; }

/* Main area shifts with sidebar width on desktop */
.main { transition: margin-left var(--sb-transition); }

/* ── Header with logo + toggle ── */
.sb-header {
  position: relative;
  display: flex; align-items: center; gap: 10px;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--divider);
  min-height: 60px;
}
.sb-logo { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.sb-logo-full {
  height: 32px; width: auto; max-width: 170px;
  object-fit: contain; flex-shrink: 0;
  filter: drop-shadow(0 4px 10px var(--accent-glow));
  background: #fff; padding: 4px 8px; border-radius: 8px;
  transition: opacity .2s var(--ease,ease);
}
.sb-logo-mark {
  height: 36px; width: 36px;
  object-fit: contain; flex-shrink: 0;
  background: #fff; padding: 3px; border-radius: 9px;
  box-shadow: 0 4px 10px var(--accent-glow);
  display: none;
}
.sb-logo-tag {
  font-size: 9.5px; color: var(--muted);
  font-weight: 700; letter-spacing: 1.1px; text-transform: uppercase;
  font-style: normal; white-space: nowrap;
  padding: 3px 7px; border-radius: 999px;
  background: var(--surface2); border: 1px solid var(--border);
}
body.sb-rail .sb-logo-full,
body.sb-rail .sb-logo-tag { display: none; }
body.sb-rail .sb-logo-mark { display: block; }
body.sb-rail .sb-logo { justify-content: center; }

/* Toggle button — always inside the sidebar, top-right corner */
.sb-toggle {
  width: var(--sb-toggle-size); height: var(--sb-toggle-size);
  border-radius: 8px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  color: var(--muted);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all .15s var(--ease,ease);
  flex-shrink: 0;
  padding: 0;
}
.sb-toggle:hover { background: var(--surface3); color: var(--accent2); border-color: var(--border-strong); }
.sb-toggle svg { width: 14px; height: 14px; transition: transform .28s var(--ease,ease); }
body.sb-rail .sb-toggle svg { transform: rotate(180deg); }
/* In rail mode put the toggle BELOW the logo (centered), since we need the header tight */
body.sb-rail .sb-header { flex-direction: column; padding: 12px 8px; gap: 8px; }

/* ── User chip ── */
.sb-user {
  display: flex; align-items: center; gap: 10px;
  margin: 10px; padding: .85rem 1rem;
  border-radius: var(--r-sm);
  background: var(--surface2);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-xs);
  transition: padding var(--sb-transition), margin var(--sb-transition);
}
.sb-user-avatar {
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 12px; font-weight: 700;
  flex-shrink: 0; box-shadow: 0 4px 10px var(--accent-glow);
}
.sb-user-body { flex: 1; min-width: 0; }
.sb-user-name {
  font-size: 13px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: -0.01em;
}
.sb-user-role {
  font-size: 10px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.9px;
  font-weight: 600; margin-top: 2px;
}
body.sb-rail .sb-user { padding: 6px; margin: 8px 8px 6px; justify-content: center; }
body.sb-rail .sb-user-body { display: none; }

/* ── Nav ── */
.sb-nav {
  flex: 1;
  padding: .25rem .5rem .5rem;
  overflow-y: auto;
  scrollbar-width: thin;
}
body.sb-rail .sb-nav { padding: .25rem 8px; }

.sb-section {
  font-size: 11px; color: var(--text-secondary);
  letter-spacing: 1.3px;
  margin: 10px 4px 2px;
  padding: 9px 12px;
  text-transform: uppercase; font-weight: 700;
  display: flex; align-items: center; justify-content: space-between; gap: 6px;
  cursor: pointer; user-select: none;
  border-radius: var(--r-sm);
  transition: all .15s var(--ease,ease);
  background: transparent;
  border: 1px solid transparent;
}
.sb-section:first-of-type { margin-top: 4px; }
.sb-section:hover { color: var(--text); background: var(--surface2); border-color: var(--border); }
.sb-section:active { transform: scale(.99); }
.sb-section .sb-chevron {
  width: 14px; height: 14px; flex-shrink: 0;
  color: var(--accent2); opacity: .75;
  transition: transform .25s var(--ease,ease);
}
.sb-section:hover .sb-chevron { opacity: 1; }
.sb-section.collapsed { color: var(--muted); }
.sb-section.collapsed .sb-chevron { transform: rotate(-90deg); color: var(--muted); }
body.sb-rail .sb-section {
  /* Show a subtle separator line instead of a label */
  margin: 10px 4px 6px; padding: 0;
  height: 1px;
  background: var(--divider);
  border: 0;
  pointer-events: none;
  overflow: hidden;
  color: transparent;
}
body.sb-rail .sb-section * { display: none; }

.sb-group {
  overflow: hidden;
  max-height: 1200px;
  transition: max-height .3s var(--ease,ease), opacity .2s var(--ease,ease), margin .2s var(--ease,ease);
  opacity: 1;
}
.sb-group.collapsed { max-height: 0; opacity: 0; margin-bottom: -2px; }
/* Section collapse is meaningless in rail mode — always show items */
body.sb-rail .sb-group.collapsed { max-height: 1200px; opacity: 1; margin-bottom: 0; }

.sb-item {
  display: flex; align-items: center; gap: 11px;
  padding: 8px 12px;
  border-radius: var(--r-sm);
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 13px; font-weight: 500;
  transition: background .15s var(--ease,ease), color .15s var(--ease,ease), padding var(--sb-transition), justify-content 0s;
  margin-bottom: 1px;
  position: relative;
  letter-spacing: -0.005em;
  /* Reset button defaults if used as <button> */
  background: transparent; border: 0; width: 100%; text-align: left; font-family: inherit;
}
.sb-item:hover { background: var(--surface2); color: var(--text); }
.sb-item.active {
  background: linear-gradient(90deg, var(--accent-soft), transparent 80%);
  color: var(--text); font-weight: 600;
}
.sb-item.active::before {
  content: '';
  position: absolute; left: 0; top: 6px; bottom: 6px;
  width: 3px; border-radius: 0 3px 3px 0;
  background: linear-gradient(180deg, var(--accent), var(--accent2));
  box-shadow: 0 0 12px var(--accent-glow);
}
.sb-item.active .sb-icon { color: var(--accent2); }

.sb-icon {
  width: 18px; height: 18px;
  color: var(--muted); flex-shrink: 0;
  transition: color .15s var(--ease,ease);
  display: inline-flex; align-items: center; justify-content: center;
}
.sb-icon svg { width: 18px; height: 18px; display: block; }
.sb-item:hover .sb-icon { color: var(--text); }

.sb-label {
  flex: 1; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: opacity .18s var(--ease,ease);
}
.sb-item-badge {
  margin-left: auto;
  font-size: 10px; padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent-soft); color: var(--accent2);
  font-weight: 700; flex-shrink: 0;
}

/* Rail: icon-only centered, labels/badges hidden, tooltip on hover */
body.sb-rail .sb-item {
  justify-content: center; gap: 0;
  padding: 10px 0; margin: 2px 4px;
  width: calc(100% - 8px);
  border-radius: 10px;
}
body.sb-rail .sb-item .sb-label,
body.sb-rail .sb-item .sb-item-badge { display: none; }
body.sb-rail .sb-item.active::before { left: -4px; top: 8px; bottom: 8px; }

/* Tooltip on rail hover — shows the label in a floating pill */
body.sb-rail .sb-item[data-label]::after {
  content: attr(data-label);
  position: absolute; left: calc(100% + 10px); top: 50%; transform: translateY(-50%);
  padding: 6px 11px; border-radius: 8px;
  background: var(--surface3); color: var(--text);
  border: 1px solid var(--border2);
  font-size: 12px; font-weight: 600; letter-spacing: -0.01em;
  white-space: nowrap;
  box-shadow: var(--shadow-md);
  opacity: 0; pointer-events: none;
  transition: opacity .18s var(--ease,ease), transform .18s var(--ease,ease);
  z-index: 200;
  transform-origin: left center;
}
body.sb-rail .sb-item:hover::after { opacity: 1; transform: translateY(-50%) translateX(2px); }

/* ── Footer (email + theme + sign out) ── */
.sb-foot {
  padding: .85rem;
  border-top: 1px solid var(--divider);
  margin-top: auto;
  flex-shrink: 0;
  display: flex; flex-direction: column; gap: 6px;
}
.sb-foot .sb-email {
  font-size: 11px; color: var(--muted);
  padding: 4px 8px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sb-foot .btn { width: 100%; }
body.sb-rail .sb-foot { padding: 8px; gap: 6px; }
body.sb-rail .sb-foot .sb-email { display: none; }
body.sb-rail .sb-foot .btn {
  padding: 8px 0;
  gap: 0;
}
body.sb-rail .sb-foot .btn > span:not(.sb-icon),
body.sb-rail .sb-foot .btn > svg + span { display: none; }

/* ── Mobile drawer mode ── */
@media (max-width: 1024px) {
  :root { --sidebar-w: 0px; }
  .sidebar {
    transform: translateX(-100%);
    width: var(--sb-expanded-w);
    box-shadow: var(--shadow-xl);
    cursor: default;
  }
  .sidebar.open { transform: translateX(0); }
  /* Rail mode is desktop-only; on mobile we always show expanded layout in the drawer */
  body.sb-rail .sidebar { width: var(--sb-expanded-w); cursor: default; }
  body.sb-rail .sb-logo-full, body.sb-rail .sb-logo-tag { display: unset; }
  body.sb-rail .sb-logo-mark { display: none; }
  body.sb-rail .sb-user-body { display: unset; }
  body.sb-rail .sb-section { margin: 10px 4px 2px; padding: 9px 12px; height: auto; background: transparent; color: var(--text-secondary); pointer-events: auto; overflow: visible; }
  body.sb-rail .sb-section * { display: unset; }
  body.sb-rail .sb-item { justify-content: flex-start; gap: 11px; padding: 8px 12px; margin-bottom: 1px; width: 100%; border-radius: var(--r-sm); }
  body.sb-rail .sb-item .sb-label,
  body.sb-rail .sb-item .sb-item-badge { display: unset; }
  body.sb-rail .sb-item[data-label]::after { display: none; }
  body.sb-rail .sb-foot { padding: .85rem; }
  body.sb-rail .sb-foot .sb-email { display: unset; }
  body.sb-rail .sb-foot .btn > span:not(.sb-icon) { display: unset; }
  body.sb-rail .main { margin-left: 0 !important; }
  /* Close (X) button in drawer header */
  .sb-toggle[data-action="close-sidebar"] { display: inline-flex; }
  .sb-toggle[data-action="toggle-rail"] { display: none; }
}
@media (min-width: 1025px) {
  .sb-toggle[data-action="close-sidebar"] { display: none; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .sidebar, .main, .sb-toggle svg, .sb-label, .sb-group, .sb-section { transition: none !important; }
}
