/* ============================================
   SIDEBAR — BEM naming reference
   ============================================
   .app-shell              — page layout (sidebar + main)
   .sidebar                 — aside container
   .sidebar__header         — logo row + collapse toggle
   .sidebar__brand          — logo image + title link
   .sidebar__logo           — brand text link
   .sidebar__toggle         — collapse button
   .sidebar__toggle-icon     — chevron inside toggle

   .sidebar-nav             — primary navigation
   .sidebar-nav__group      — menu section
   .sidebar-nav__group--folded    — section links hidden (localStorage)
   .sidebar-nav__group--animated  — enable expand/collapse transition
   .sidebar-nav__title      — collapsible section heading
   .sidebar-nav__title-label — title text (first letter shown when sidebar collapsed)
   .sidebar-nav__chevron    — chevron in section heading
   .sidebar-nav__body       — animated wrapper for section links
   .sidebar-nav__inner      — inner clip container
   .sidebar-nav__divider    — horizontal rule between link groups
   .sidebar-nav__link       — nav item (pairs with .active from route helper)
   .sidebar-nav__icon       — SVG icon in nav link
   .sidebar-nav__label      — text label in nav link

   .sidebar-user            — user card in sidebar
   .sidebar-user--trigger   — opens profile popup
   .sidebar-user__avatar    — initials circle
   .sidebar-user__meta      — name / casino / role column
   .sidebar-user__name
   .sidebar-user__casino
   .sidebar-user__role
   .sidebar-user__switcher  — casino switcher wrapper
   .sidebar-user__switcher--badge
   .sidebar-user__switcher--selector

   .sidebar-user-menu       — profile popup content
   .sidebar-user-menu__action
   .sidebar-user-menu__action--danger
   .sidebar-user-menu__icon

   html.sidebar-collapsed   — global narrow sidebar (localStorage)
   data-sidebar-tip        — tooltip text when sidebar collapsed (#sidebar-tip-bubble)
   ============================================ */

/* --- Tokens (shared: sidebar + user profile popup) --- */
:root {
  --sidebar-width-expanded: 263px;
  --sidebar-width-collapsed: 72px;
  --sidebar-item-radius: 12px;
  --sidebar-item-padding-x: 16px;
  --sidebar-icon-size: 24px;
  --sidebar-icon-gap: 12px;
}

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

/* --- App shell --- */
.app-shell {
  align-items: stretch;
  max-width: 2000px;
  margin: 0 auto;
  width: 100%;
  flex-direction: row;
  display: flex;
  padding: 0 1rem;
  box-sizing: border-box;
  min-height: 100vh;
}

/* --- Sidebar container --- */
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-height: 100dvh;
  width: var(--sidebar-width);
  max-width: var(--sidebar-width);
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface-primary);
  padding: 1rem 0;
  flex-shrink: 0;
  transition: width 0.2s ease-out, max-width 0.2s ease-out;
  scrollbar-width: thin;
  scrollbar-color: var(--neutral-700) transparent;
}

.sidebar::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
  background: var(--neutral-700);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
  background: var(--neutral-600);
}

/* --- Header --- */
.sidebar__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sidebar__brand {
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.sidebar__brand img {
  height: 36px;
  width: 36px;
  border: none;
  border-radius: 8px;
  object-fit: contain;
}

.sidebar__logo {
  color: var(--text-primary);
  text-decoration: none;
  letter-spacing: -0.01em;
  transition: color 0.15s ease;
  font-weight: 900;
  font-size: 1.4rem;
}

.sidebar__logo:hover {
  text-decoration: none;
}

.sidebar__toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--neutral-800);
  color: #ffffff;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, transform 0.15s ease;
}

.sidebar__toggle:hover {
  background: var(--neutral-800);
  color: #ffffff;
}

.sidebar__toggle:active {
  transform: scale(0.97);
}

.sidebar__toggle-icon {
  display: block;
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  stroke: currentColor;
  transition: transform 0.2s ease-out;
}

/* --- Navigation --- */
.sidebar-nav {
  width: 100%;
  flex: 1;
  min-height: 0;
  flex-shrink: 0;
  font-family: Inter, "Inter Fallback", ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
  -webkit-font-smoothing: antialiased;
  box-sizing: border-box;
  gap: 1rem;
}

.sidebar-nav__group {
  margin-bottom: 4px;
}

.sidebar-nav__title {
  display: flex !important;
  align-items: center;
  justify-content: space-between;
  color: var(--text-primary);
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 16px var(--sidebar-item-padding-x) 8px;
  cursor: pointer;
  transition: color 150ms ease;
  user-select: none;
}

.sidebar-nav__title:hover {
  color: var(--text-secondary) !important;
}

.sidebar-nav__title-label {
  min-width: 0;
}

.sidebar-nav__chevron {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  transition: transform 200ms ease-out;
  flex-shrink: 0;
  margin-left: 8px;
}

.sidebar-nav__group--folded .sidebar-nav__chevron {
  transform: rotate(-90deg);
}

.sidebar-nav__body {
  display: grid;
  grid-template-rows: 1fr;
}

.sidebar-nav__group--folded .sidebar-nav__body {
  grid-template-rows: 0fr;
}

.sidebar-nav__inner {
  min-height: 0;
  overflow: hidden;
  opacity: 1;
}

.sidebar-nav__group--folded .sidebar-nav__inner {
  opacity: 0;
}

.sidebar-nav__group--animated .sidebar-nav__body {
  transition: grid-template-rows 200ms ease-out;
}

.sidebar-nav__group--animated .sidebar-nav__inner {
  transition: opacity 180ms ease-out;
}

.sidebar-nav__divider {
  margin: 8px 0;
  border-bottom: 1px solid var(--border-light);
}

.sidebar-nav__link {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  color: var(--text-secondary);
  white-space: nowrap;
  font-size: 1rem;
  font-weight: 500;
  line-height: 24px;
  padding: 14px var(--sidebar-item-padding-x);
  border-radius: var(--sidebar-item-radius);
  text-decoration: none;
  transition: color 150ms ease, background-color 150ms ease;
  cursor: pointer;
  position: relative;
  margin-bottom: 2px;
}

.sidebar-nav__link:hover {
  color: var(--text-primary);
  background: var(--surface-secondary);
  text-decoration: none;
}

.sidebar-nav__link.active {
  color: var(--text-primary);
  background: var(--surface-secondary);
  font-weight: 500;
}

.sidebar-nav__link.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 24px;
  background: var(--violet-600);
  border-radius: 0 3px 3px 0;
}

.sidebar-nav__icon {
  width: var(--sidebar-icon-size);
  height: var(--sidebar-icon-size);
  margin-right: var(--sidebar-icon-gap);
  flex-shrink: 0;
  stroke: currentColor;
  transition: stroke 0.15s ease;
}

.sidebar-nav__label {
  flex: 1;
}

/* --- User card --- */
.sidebar-user--trigger {
  background: none;
  border: none;
  cursor: pointer;
  transition: background 0.15s ease;
}

.sidebar-user--trigger:hover {
  background: var(--surface-hover, rgba(255, 255, 255, 0.05));
}

.sidebar-user {
  display: flex;
  align-items: center;
  padding: 12px var(--sidebar-item-padding-x);
  gap: 12px;
  border-radius: var(--sidebar-item-radius);
  border: 1px solid var(--border-medium);
}

.sidebar-user__avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--amber-600);
  color: var(--btn-on-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1rem;
  text-transform: uppercase;
  flex-shrink: 0;
}

.sidebar-user__meta {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.sidebar-user__name {
  color: var(--text-primary);
  font-weight: 500;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-user__role,
.sidebar-user__casino {
  color: var(--text-tertiary);
  font-size: 0.75rem;
  font-weight: 400;
}

.sidebar-user__casino {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: var(--line1);
}

.sidebar-user__switcher--badge {
  min-width: 0;
  overflow: hidden;
}

.sidebar-user__switcher--badge .sidebar-user__casino {
  display: block;
  max-width: 100%;
}

.sidebar-user__switcher--selector {
  width: 100%;
}

.sidebar-user__switcher--selector .sidebar-user__casino {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-primary);
  white-space: normal;
  overflow: visible;
  text-overflow: unset;
}

.sidebar-user__switcher--selector select {
  width: 100%;
}

.sidebar-user-menu__label {
  flex: 1;
}

/* --- User popup --- */
.sidebar-user-menu {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 260px;
}

.sidebar-user-menu__action {
  display: flex;
  align-items: center;
  padding: 12px var(--sidebar-item-padding-x);
  border-radius: var(--sidebar-item-radius);
  transition: color 0.15s ease, background-color 0.15s ease;
  cursor: pointer;
  gap: 12px;
  text-decoration: none;
  color: var(--text-secondary);
}

.sidebar-user-menu__action:hover {
  color: var(--text-primary);
  background: var(--surface-secondary);
  text-decoration: none;
}

.sidebar-user-menu__action--danger {
  justify-content: flex-start;
  color: var(--red-600);
  border: 1px solid var(--red-400);
  min-height: 44px;
  outline: none;
}

.sidebar-user-menu__action--danger:hover {
  color: var(--red-600);
  background: rgba(239, 68, 68, 0.1);
}

.sidebar-user-menu__icon {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  flex-shrink: 0;
}

/* --- Collapsed sidebar (desktop) — scoped to .sidebar so popup is unaffected --- */
html.sidebar-collapsed .sidebar {
  --sidebar-width: var(--sidebar-width-collapsed);
}

html.sidebar-collapsed .sidebar .sidebar__toggle-icon {
  transform: rotate(180deg);
}

html.sidebar-collapsed .sidebar .sidebar__header {
  flex-direction: column;
  padding: 0 12px;
  gap: 12px;
}

html.sidebar-collapsed .sidebar .sidebar__brand {
  display: none;
}

html.sidebar-collapsed .sidebar .sidebar__logo {
  display: none;
}

html.sidebar-collapsed .sidebar .sidebar-user__meta {
  display: none;
}

html.sidebar-collapsed .sidebar .sidebar-user--trigger {
  justify-content: center;
  padding: 12px 8px;
}

html.sidebar-collapsed .sidebar .sidebar-nav__title {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap: 4px;
  padding: 10px 8px;
  letter-spacing: 0;
  margin-bottom: 4px;
  border-radius: var(--sidebar-item-radius);
  background: var(--surface-tertiary);
}

html.sidebar-collapsed .sidebar .sidebar-nav__title:hover {
  background: var(--surface-secondary);
  border-color: var(--border-medium);
}

html.sidebar-collapsed .sidebar .sidebar-nav__title-label {
  display: block;
  line-height: 1;
  flex-shrink: 0;
}

html.sidebar-collapsed .sidebar .sidebar-nav__chevron {
  width: 12px;
  height: 12px;
  margin-left: 0;
  flex-shrink: 0;
  opacity: 0.75;
}

html.sidebar-collapsed .sidebar .sidebar-nav__link {
  justify-content: center;
  padding-left: var(--sidebar-item-padding-x);
  padding-right: var(--sidebar-item-padding-x);
}

html.sidebar-collapsed .sidebar .sidebar-nav__icon {
  margin-right: 0;
}

html.sidebar-collapsed .sidebar .sidebar-nav__label {
  display: none;
}

/* Sidebar tooltips (collapsed mode, separate from global #tip-bubble) */
#sidebar-tip-bubble {
  --sidebar-tip-padding-y: 13px;
  --sidebar-tip-padding-x: 22px;
  position: fixed;
  z-index: 100000;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  background: var(--tip-bg, #1a1a2e);
  color: var(--tip-text, #e2e8f0);
  border: 1px solid var(--tip-border, #3a3a5c);
  border-radius: var(--radius-md, 7px);
  padding-block: var(--sidebar-tip-padding-y);
  padding-inline: var(--sidebar-tip-padding-x);
  font-size: 1rem;
  font-weight: 500;
  line-height: 24px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s ease;
  box-shadow: var(--tip-shadow, 0 4px 16px rgba(0, 0, 0, 0.45));
}

#sidebar-tip-bubble.visible {
  opacity: 1;
}

html.sidebar-collapsed .sidebar [data-sidebar-tip] {
  cursor: pointer;
}

/* --- Accessibility --- */
@media (prefers-reduced-motion: reduce) {
  .sidebar,
  .sidebar__toggle-icon,
  .sidebar-nav__body,
  .sidebar-nav__inner,
  .sidebar-nav__chevron {
    transition: none;
  }
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .app-shell {
    flex-direction: column;
  }

  .sidebar {
    position: static;
    max-height: none;
    overflow-y: visible;
  }

  .sidebar__toggle {
    display: none;
  }

  html.sidebar-collapsed .sidebar {
    --sidebar-width: var(--sidebar-width-expanded);
  }

  html.sidebar-collapsed .sidebar .sidebar__brand {
    display: flex;
  }

  html.sidebar-collapsed .sidebar .sidebar__logo {
    display: block;
  }

  html.sidebar-collapsed .sidebar .sidebar-user__meta {
    display: flex;
  }

  html.sidebar-collapsed .sidebar .sidebar-user--trigger {
    justify-content: flex-start;
    padding: 12px var(--sidebar-item-padding-x);
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__title {
    flex-direction: row;
    justify-content: space-between;
    padding: 16px var(--sidebar-item-padding-x) 8px;
    letter-spacing: 0.08em;
    margin: 0;
    background: transparent;
    border: none;
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__title-label {
    text-align: left;
    line-height: inherit;
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__chevron {
    width: 16px;
    height: 16px;
    margin-left: 8px;
    opacity: 1;
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__group--folded .sidebar-nav__body {
    grid-template-rows: 0fr;
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__group--folded .sidebar-nav__inner {
    opacity: 0;
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__link {
    justify-content: flex-start;
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__icon {
    margin-right: var(--sidebar-icon-gap);
  }

  html.sidebar-collapsed .sidebar .sidebar-nav__label {
    display: block;
  }

  .sidebar-nav__link {
    padding: 12px var(--sidebar-item-padding-x);
    font-size: 0.875rem;
  }

  .sidebar__brand {
    padding: 8px var(--sidebar-item-padding-x);
    margin-bottom: 12px;
  }
}
