/**
 * Navigation shell — persistent sidebar (desktop), collapsible rail (tablet),
 * hamburger + bottom-nav (mobile).
 *
 * Layout:
 *   Desktop  ≥1024px  : body gets .has-sidebar; main shifts right by --sidebar-w.
 *   Tablet   768–1023 : icon-rail (collapsed sidebar, icons + tooltips).
 *   Mobile   <768px   : sidebar behind hamburger + top bar + bottom-nav.
 *
 * WCAG 2.1 AA: keyboard navigation, ARIA landmarks, focus trap on mobile drawer.
 */

/* ── Layout tokens ─────────────────────────────────────────────── */
:root {
  --sidebar-w:        240px;
  --sidebar-rail-w:    64px;
  --topbar-h:          56px;
  --nav-shell-z:      1000;
  --nav-shell-bg:     var(--color-nav-bg);   /* Stripe navy, both themes */
  --nav-shell-text:   #839bc8;
  --nav-shell-active: #f6f9fc;
  --nav-shell-hover:  rgba(246 249 252 / 0.08);
  --nav-shell-active-bg: rgba(99 91 255 / 0.22);
  --nav-shell-mark:   #7a73ff;
  --nav-shell-lock:   #50617a;
  --nav-transition:   200ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Body layout ───────────────────────────────────────────────── */

/* Desktop: content area pushed right */
@media (min-width: 1024px) {
  body.has-sidebar {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    grid-template-rows: auto;
    min-height: 100vh;
  }
  body.has-sidebar > .skip-link,
  body.has-sidebar > .nav-shell-topbar { display: none; }
  body.has-sidebar > .nav-shell        { grid-column: 1; grid-row: 1 / -1; }
  body.has-sidebar > main,
  body.has-sidebar > [id="main-content"] { grid-column: 2; }
}

/* Tablet: icon rail */
@media (min-width: 768px) and (max-width: 1023px) {
  body.has-sidebar {
    display: grid;
    grid-template-columns: var(--sidebar-rail-w) 1fr;
    min-height: 100vh;
  }
  body.has-sidebar > .nav-shell-topbar { display: none; }
  body.has-sidebar > .nav-shell        { grid-column: 1; grid-row: 1 / -1; }
  body.has-sidebar > main,
  body.has-sidebar > [id="main-content"] { grid-column: 2; }
}

/* Mobile: account for top bar */
@media (max-width: 767px) {
  body.has-sidebar {
    padding-top: var(--topbar-h);
  }
}

/* ── Sidebar panel ─────────────────────────────────────────────── */

.nav-shell {
  position: relative;
  z-index: var(--nav-shell-z);
}

.nav-shell__panel {
  background:    var(--nav-shell-bg);
  display:       flex;
  flex-direction: column;
  height:        100vh;
  overflow-y:    auto;
  overflow-x:    hidden;
  scrollbar-width: thin;
  scrollbar-color: rgba(255 255 255 / 0.1) transparent;
}

/* Desktop: sticky panel */
@media (min-width: 1024px) {
  .nav-shell__panel {
    position: sticky;
    top: 0;
    width: var(--sidebar-w);
  }
}

/* Tablet: fixed narrow rail */
@media (min-width: 768px) and (max-width: 1023px) {
  .nav-shell__panel {
    position: sticky;
    top: 0;
    width: var(--sidebar-rail-w);
    align-items: center;
  }
  .nav-shell__label,
  .nav-shell__brand-name,
  .nav-shell__lock-icon { display: none; }
  .nav-shell__brand-mark { display: flex !important; }
  .nav-shell__header { justify-content: center; padding: var(--space-4) 0; }
  .nav-shell__item { justify-content: center; padding: var(--space-3); }
  .nav-shell__icon { margin: 0; }
  /* Show label as tooltip on hover/focus */
  .nav-shell__item { position: relative; }
  .nav-shell__item:hover .nav-shell__label,
  .nav-shell__item:focus .nav-shell__label {
    display: block !important;
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--color-tooltip-bg);
    color: var(--color-tooltip-text);
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-md);
    white-space: nowrap;
    font-size: var(--font-size-sm);
    pointer-events: none;
    z-index: 10;
  }
}

/* Mobile: off-canvas drawer */
@media (max-width: 767px) {
  .nav-shell__panel {
    position: fixed;
    top: 0;
    left: 0;
    width: min(280px, 85vw);
    height: 100%;
    transform: translateX(-100%);
    transition: transform var(--nav-transition);
    z-index: var(--nav-shell-z);
    box-shadow: var(--shadow-xl);
  }
  .nav-shell.is-open .nav-shell__panel {
    transform: translateX(0);
  }
}

/* ── Header: brand ─────────────────────────────────────────────── */

.nav-shell__header {
  display: flex;
  align-items: center;
  padding: var(--space-6) var(--space-5);
  border-bottom: 1px solid rgba(255 255 255 / 0.06);
  flex-shrink: 0;
}

.nav-shell__brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  text-decoration: none;
  color: var(--nav-shell-active);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-lg);
  letter-spacing: var(--tracking-tight);
}

.nav-shell__brand:hover { color: #fff; }

.nav-shell__brand-mark {
  display: none; /* shown only on tablet/collapsed */
  width: 32px;
  height: 32px;
  border-radius: var(--radius-md);
  overflow: hidden;
  flex-shrink: 0;
}

.nav-shell__brand-mark img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* Horizontal logo — full sidebar */
.nav-shell__brand-logo {
  display: block;
  height: 28px;
  width: auto;
  object-fit: contain;
  flex-shrink: 0;
}

.nav-shell__brand-name mark {
  background: none;
  color: var(--nav-shell-mark);
}

/* Mobile topbar logo */
.nav-shell-topbar__brand-logo {
  height: 22px;
  width: auto;
  object-fit: contain;
  display: block;
}

/* ── Navigation list ───────────────────────────────────────────── */

.nav-shell__nav {
  flex: 1;
  padding: var(--space-4) var(--space-3);
  overflow-y: auto;
}

.nav-shell__section-label {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-widest);
  color: var(--nav-shell-lock);
  padding: var(--space-4) var(--space-2) var(--space-2);
}

.nav-shell__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-shell__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--nav-shell-text);
  text-decoration: none;
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: background var(--nav-transition), color var(--nav-transition);
  min-height: 44px; /* WCAG touch target */
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}

.nav-shell__item:hover {
  background: var(--nav-shell-hover);
  color: var(--nav-shell-active);
}

.nav-shell__item:focus-visible {
  outline: 2px solid var(--nav-shell-mark);
  outline-offset: 2px;
}

.nav-shell__item[aria-current="page"],
.nav-shell__item.is-active {
  background: var(--nav-shell-active-bg);
  color: var(--nav-shell-active);
  position: relative;
}

/* Left accent bar — animates in on page load via scaleY */
.nav-shell__item[aria-current="page"]::before,
.nav-shell__item.is-active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%) scaleY(0);
  width: 3px;
  height: 60%;
  background: var(--nav-shell-mark);
  border-radius: 0 3px 3px 0;
  animation: nav-accent-in 300ms cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes nav-accent-in {
  from { transform: translateY(-50%) scaleY(0); opacity: 0; }
  to   { transform: translateY(-50%) scaleY(1); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .nav-shell__item[aria-current="page"]::before,
  .nav-shell__item.is-active::before {
    animation: none;
    transform: translateY(-50%) scaleY(1);
  }
}

.nav-shell__item--locked {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.nav-shell__icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  opacity: 0.7;
}
.nav-shell__item:hover .nav-shell__icon,
.nav-shell__item.is-active .nav-shell__icon { opacity: 1; }

.nav-shell__label { flex: 1; }

.nav-shell__lock-icon {
  font-size: 11px;
  opacity: 0.5;
  flex-shrink: 0;
}

.nav-shell__section { margin-bottom: var(--space-4); }

/* ── Footer: account area ──────────────────────────────────────── */

.nav-shell__footer {
  padding: var(--space-4) var(--space-3);
  border-top: 1px solid rgba(255 255 255 / 0.06);
  flex-shrink: 0;
}

/* ── Overlay (mobile backdrop) ─────────────────────────────────── */

.nav-shell__overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0 0 0 / 0.5);
  z-index: calc(var(--nav-shell-z) - 1);
  backdrop-filter: blur(2px);
}

@media (max-width: 767px) {
  .nav-shell.is-open .nav-shell__overlay {
    display: block;
  }
}

/* ── Top bar (mobile only) ─────────────────────────────────────── */

.nav-shell-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  background: var(--nav-shell-bg);
  display: flex;
  align-items: center;
  padding: 0 var(--space-4);
  gap: var(--space-3);
  z-index: calc(var(--nav-shell-z) - 2);
  border-bottom: 1px solid rgba(255 255 255 / 0.06);
}

@media (min-width: 768px) {
  .nav-shell-topbar { display: none; }
}

.nav-shell-topbar__hamburger {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 44px;
  height: 44px;
  padding: var(--space-2);
  background: none;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  transition: background var(--nav-transition);
}

.nav-shell-topbar__hamburger:hover { background: var(--nav-shell-hover); }
.nav-shell-topbar__hamburger:focus-visible {
  outline: 2px solid var(--nav-shell-mark);
  outline-offset: 2px;
}

.nav-shell-topbar__bar {
  display: block;
  height: 2px;
  width: 20px;
  background: var(--nav-shell-active);
  border-radius: 2px;
  transition: transform var(--nav-transition), opacity var(--nav-transition);
}

.nav-shell-topbar__brand {
  flex: 1;
  color: var(--nav-shell-active);
  font-weight: var(--font-weight-bold);
  font-size: var(--font-size-body);
}

.nav-shell-topbar__brand mark {
  background: none;
  color: var(--nav-shell-mark);
}

.nav-shell-topbar__account { margin-left: auto; }

/* ── Hamburger → X animation when drawer is open ───────────────── */

@media (max-width: 767px) {
  body:has(.nav-shell.is-open) .nav-shell-topbar__bar:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
  }
  body:has(.nav-shell.is-open) .nav-shell-topbar__bar:nth-child(2) {
    opacity: 0;
    transform: scaleX(0);
  }
  body:has(.nav-shell.is-open) .nav-shell-topbar__bar:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
  }
}

/* ── Sidebar collapse button (desktop) ─────────────────────────── */

.nav-shell__collapse-btn {
  display: none; /* hidden on mobile / tablet */
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: rgba(246 249 252 / 0.06);
  border-radius: var(--radius-md);
  color: var(--nav-shell-text);
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--nav-transition), color var(--nav-transition),
              transform var(--nav-transition);
}

.nav-shell__collapse-btn:hover {
  background: var(--nav-shell-hover);
  color: var(--nav-shell-active);
}

.nav-shell__collapse-btn:focus-visible {
  outline: 2px solid var(--nav-shell-mark);
  outline-offset: 2px;
}

@media (min-width: 1024px) {
  .nav-shell__collapse-btn { display: flex; }
}

/* ── Collapsed state (desktop) ─────────────────────────────────── */

@media (min-width: 1024px) {
  body.has-sidebar.sidebar-collapsed {
    grid-template-columns: var(--sidebar-rail-w) 1fr;
  }

  body.sidebar-collapsed .nav-shell__panel {
    width: var(--sidebar-rail-w);
    align-items: center;
    overflow: visible;
  }

  body.sidebar-collapsed .nav-shell__brand-name,
  body.sidebar-collapsed .nav-shell__brand-logo,
  body.sidebar-collapsed .nav-shell__label,
  body.sidebar-collapsed .nav-shell__lock-icon,
  body.sidebar-collapsed .nav-shell__section-label,
  body.sidebar-collapsed .nav-shell__theme-label { display: none; }

  body.sidebar-collapsed .nav-shell__brand-mark { display: flex !important; }

  body.sidebar-collapsed .nav-shell__header {
    justify-content: center;
    padding: var(--space-4) 0;
    flex-direction: column;
    gap: var(--space-3);
  }

  body.sidebar-collapsed .nav-shell__collapse-btn {
    transform: rotate(180deg);
  }

  body.sidebar-collapsed .nav-shell__item {
    justify-content: center;
    padding: var(--space-3);
  }

  body.sidebar-collapsed .nav-shell__icon { margin: 0; }

  body.sidebar-collapsed .nav-shell__theme-row { justify-content: center; }

  /* Tooltip on hover when collapsed */
  body.sidebar-collapsed .nav-shell__item {
    position: relative;
  }

  body.sidebar-collapsed .nav-shell__item:hover .nav-shell__label,
  body.sidebar-collapsed .nav-shell__item:focus-visible .nav-shell__label {
    display: block !important;
    position: absolute;
    left: calc(100% + 8px);
    top: 50%;
    transform: translateY(-50%);
    background: var(--color-tooltip-bg);
    color: var(--color-tooltip-text);
    padding: var(--space-1) var(--space-3);
    border-radius: var(--radius-md);
    white-space: nowrap;
    font-size: var(--font-size-sm);
    pointer-events: none;
    z-index: 20;
    box-shadow: 0 2px 8px rgba(0,0,0,.25);
  }
}

/* ── In-app back button ─────────────────────────────────────────── */

.page-back-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-text-secondary);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  transition: background var(--duration-fast), color var(--duration-fast);
  min-height: 44px;
}

.page-back-btn:hover { background: var(--color-surface-subtle); color: var(--color-text-primary); }
.page-back-btn:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 2px; }

.page-back-btn__arrow { font-size: 1em; }

/* ── Theme toggle ──────────────────────────────────────────────────
   Shared switch styling; variants for the dark nav rail and mobile bar. */
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  color: var(--color-text-secondary);
  background: var(--color-surface-subtle);
  border: 1px solid var(--color-border);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out),
              color var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out);
}
.theme-toggle svg { width: 18px; height: 18px; }
.theme-toggle:hover {
  color: var(--color-primary);
  border-color: var(--color-primary-border);
  background: var(--color-primary-light);
}
.theme-toggle:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* On the dark nav rail */
.nav-shell__theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2) var(--space-3);
}
.nav-shell__theme-label {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  letter-spacing: var(--tracking-wider);
  text-transform: uppercase;
  color: var(--nav-shell-lock);
}
.theme-toggle--nav {
  background: rgba(246 249 252 / 0.06);
  border-color: rgba(246 249 252 / 0.12);
  color: var(--nav-shell-text);
}
.theme-toggle--nav:hover {
  background: var(--nav-shell-active-bg);
  border-color: var(--nav-shell-mark);
  color: var(--nav-shell-active);
}

/* Tablet rail: hide the "Appearance" label, keep the button centred */
@media (min-width: 768px) and (max-width: 1023px) {
  .nav-shell__theme-row { justify-content: center; }
  .nav-shell__theme-label { display: none; }
}

/* Mobile top bar toggle */
.nav-shell-topbar__theme { display: inline-flex; }
.theme-toggle--bar {
  background: rgba(246 249 252 / 0.06);
  border-color: rgba(246 249 252 / 0.12);
  color: var(--nav-shell-text);
}
.theme-toggle--bar:hover {
  background: var(--nav-shell-active-bg);
  border-color: var(--nav-shell-mark);
  color: var(--nav-shell-active);
}
