/* ============================================================================
   V2PRO — app shell: sidebar, top bar, content, page scaffolding
   Depends on tokens.css + base.css.
   ========================================================================== */

/* ============================================================================
   Shell grid
   The sidebar is a grid column on desktop and an overlay drawer on mobile;
   only the column width changes, so nothing reflows when it collapses.
   ========================================================================== */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100dvh;
}

.app[data-rail='1'] {
  grid-template-columns: var(--sidebar-rail) 1fr;
}

/* ============================================================================
   Sidebar
   ========================================================================== */
.sidebar {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--sf-1), var(--bg-1) 60%);
  border-inline-start: 1px solid var(--line-1);
  box-shadow: 1px 0 0 rgba(0, 0, 0, .2);
  z-index: var(--z-sticky);
  transition: width var(--dur-3) var(--ease-out);
}

/* --- brand --- */
.sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  height: var(--topbar-h);
  padding-inline: var(--sp-4);
  flex: none;
  border-bottom: 1px solid var(--line-1);
  overflow: hidden;
}

.sidebar__mark {
  flex: none;
  width: 30px;
  height: 30px;
  display: grid;
  place-content: center;
  border-radius: 9px;
  background: linear-gradient(160deg, var(--brand-2), var(--brand-3));
  color: var(--brand-ink);
  font-weight: 800;
  font-size: var(--fs-14);
  line-height: 1;
  box-shadow: var(--rim-strong), 0 4px 12px -4px rgba(34, 211, 238, .5);
}

.sidebar__name {
  min-width: 0;
  line-height: 1.2;
}

.sidebar__name b {
  display: block;
  font-size: var(--fs-14);
  font-weight: 700;
  letter-spacing: .2px;
}

.sidebar__name span {
  display: block;
  font-size: var(--fs-11);
  color: var(--ink-3);
}

/* --- nav --- */
.sidebar__nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--sp-3) var(--sp-2) var(--sp-4);
}

.navgroup + .navgroup {
  margin-top: var(--sp-4);
}

.navgroup__title {
  padding: 0 var(--sp-3) var(--sp-2);
  font-size: var(--fs-11);
  font-weight: 600;
  color: var(--ink-3);
  letter-spacing: .4px;
  white-space: nowrap;
}

.navitem {
  position: relative;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  width: 100%;
  padding: 9px var(--sp-3);
  border-radius: var(--r-1);
  color: var(--ink-2);
  font-size: var(--fs-13);
  font-weight: 500;
  white-space: nowrap;
  transition:
    background-color var(--dur-2) var(--ease-out),
    color var(--dur-2) var(--ease-out),
    transform var(--dur-1) var(--ease-out);
}

.navitem + .navitem {
  margin-top: 2px;
}

.navitem svg {
  flex: none;
  width: 17px;
  height: 17px;
  stroke-width: 1.7;
  opacity: .85;
}

.navitem:hover {
  background: var(--sf-2);
  color: var(--ink-1);
}

.navitem:active {
  transform: translateY(1px);
}

.navitem[aria-current='page'] {
  background: linear-gradient(90deg, var(--brand-soft), transparent 85%);
  color: var(--ink-1);
  font-weight: 600;
  box-shadow: var(--rim);
}

.navitem[aria-current='page'] svg {
  color: var(--brand);
  opacity: 1;
}

/* The active marker sits on the sidebar's outer edge — inline-start is the
   right edge in RTL and flips automatically if the app is ever LTR. */
.navitem[aria-current='page']::before {
  content: '';
  position: absolute;
  inset-inline-start: -8px;
  top: 50%;
  translate: 0 -50%;
  width: 3px;
  height: 18px;
  border-radius: var(--r-pill);
  background: var(--brand);
  box-shadow: 0 0 10px var(--brand);
}

.navitem__label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.navitem__badge {
  flex: none;
  min-width: 19px;
  height: 19px;
  padding: 0 6px;
  border-radius: var(--r-pill);
  display: grid;
  place-content: center;
  background: var(--crit-soft);
  color: var(--crit);
  border: 1px solid color-mix(in srgb, var(--crit) 30%, transparent);
  font-size: var(--fs-11);
  font-weight: 700;
  line-height: 1;
}

.navitem__badge[data-tone='warn'] {
  background: var(--warn-soft);
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 30%, transparent);
}

/* --- footer --- */
.sidebar__foot {
  flex: none;
  padding: var(--sp-3);
  border-top: 1px solid var(--line-1);
}

.sidebar__collapse {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  width: 100%;
  height: 32px;
  border-radius: var(--r-1);
  color: var(--ink-3);
  font-size: var(--fs-12);
  transition: background-color var(--dur-2) var(--ease-out), color var(--dur-2) var(--ease-out);
}

.sidebar__collapse:hover {
  background: var(--sf-2);
  color: var(--ink-1);
}

.sidebar__collapse svg {
  width: 15px;
  height: 15px;
  transition: rotate var(--dur-3) var(--ease-out);
}

/* --- rail (collapsed) ---
   Labels are hidden visually but stay in the accessibility tree; a CSS-only
   tooltip restores the affordance on hover/focus. */
.app[data-rail='1'] .sidebar__name,
.app[data-rail='1'] .navgroup__title,
.app[data-rail='1'] .navitem__label,
.app[data-rail='1'] .sidebar__collapse span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.app[data-rail='1'] .sidebar__brand,
.app[data-rail='1'] .navitem {
  justify-content: center;
  padding-inline: 0;
}

.app[data-rail='1'] .sidebar__collapse svg {
  rotate: 180deg;
}

.app[data-rail='1'] .navitem__badge {
  position: absolute;
  top: 3px;
  inset-inline-end: 9px;
  min-width: 8px;
  height: 8px;
  padding: 0;
  font-size: 0;
  border-width: 0;
  background: var(--crit);
}

.app[data-rail='1'] .navitem::after {
  content: attr(data-label);
  position: absolute;
  inset-inline-start: calc(100% + var(--sp-2));
  top: 50%;
  translate: 0 -50%;
  padding: 5px 10px;
  border-radius: var(--r-1);
  background: var(--sf-3);
  border: 1px solid var(--line-2);
  box-shadow: var(--e3);
  color: var(--ink-1);
  font-size: var(--fs-12);
  font-weight: 500;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  z-index: var(--z-dropdown);
  transition: opacity var(--dur-2) var(--ease-out);
}

.app[data-rail='1'] .navitem:hover::after,
.app[data-rail='1'] .navitem:focus-visible::after {
  opacity: 1;
}

/* ============================================================================
   Main column
   ========================================================================== */
.main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* --- top bar ---
   e2: the one place glass is correct — it floats over scrolling content. */
.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  flex: none;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding-inline: var(--sp-5);
  background: var(--bg-0);
  border-bottom: 1px solid var(--line-1);
  box-shadow: var(--rim);
}

.topbar__title {
  min-width: 0;
  font-size: var(--fs-16);
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar__spacer {
  flex: 1;
}

.topbar__actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}

/* Hamburger — mobile only. */
.topbar__menu {
  display: none;
}

/* ⌘K trigger: a fake search field. Real input would steal focus management
   from the palette itself. */
.cmdk-trigger {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-width: 216px;
  height: 34px;
  padding: 0 var(--sp-3);
  border-radius: var(--r-1);
  background: var(--bg-1);
  border: 1px solid var(--line-2);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, .18);
  color: var(--ink-3);
  font-size: var(--fs-13);
  transition: border-color var(--dur-2) var(--ease-out), color var(--dur-2) var(--ease-out);
}

.cmdk-trigger:hover {
  border-color: var(--brand-line);
  color: var(--ink-2);
}

.cmdk-trigger svg {
  width: 15px;
  height: 15px;
}

.cmdk-trigger span {
  flex: 1;
  text-align: start;
}

/* --- content --- */
.content {
  flex: 1;
  min-width: 0;
  width: 100%;
  max-width: var(--content-max);
  margin-inline: auto;
  padding: var(--sp-5);
  /* Room past the last row so sticky footers and toasts never cover content. */
  padding-bottom: var(--sp-7);
}

/* ============================================================================
   Page scaffolding — every view opens with .page-head and stacks .section
   ========================================================================== */
.page-head {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: var(--sp-3);
  margin-bottom: var(--sp-5);
}

.page-head__text {
  min-width: 0;
  flex: 1;
}

.page-head h1 {
  font-size: var(--fs-20);
  font-weight: 700;
}

.page-head p {
  margin-top: 3px;
  font-size: var(--fs-13);
  color: var(--ink-2);
}

.page-head__actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
}

/* Vertical rhythm.
   This used to be `.section + .section { margin-top }`, which quietly failed
   the moment a block was wrapped in an async loading slot — the adjacency was
   broken by the wrapper div and rows ended up flush against each other. A flow
   gap on the container survives any amount of wrapping. */
.view {
  display: flex;
  flex-direction: column;
  gap: var(--sp-5);
  min-width: 0;
}

/* Same rhythm inside a slot whose contents arrive after a fetch. */
.stack-lg {
  display: flex;
  flex-direction: column;
  gap: var(--sp-5);
  min-width: 0;
}

.section + .section {
  margin-top: var(--sp-5);
}

/* The gap already separates these; the legacy margins would double it. */
.view > .page-head {
  margin-bottom: 0;
}

.view > .section + .section,
.stack-lg > .section + .section {
  margin-top: 0;
}

.section__head {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
}

.section__head h2 {
  font-size: var(--fs-16);
  font-weight: 600;
}

.section__head p {
  font-size: var(--fs-12);
  color: var(--ink-3);
}

/* ============================================================================
   Grids
   auto-fit + minmax so every grid is intrinsically responsive; no per-grid
   breakpoints to maintain.
   ========================================================================== */
.grid {
  display: grid;
  gap: var(--sp-4);
}

.grid--stats {
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
}

.grid--cards {
  grid-template-columns: repeat(auto-fit, minmax(290px, 1fr));
}

.grid--wide {
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
}

/* Hero split: the 3D network widget beside a stat rail. */
.grid--hero {
  grid-template-columns: minmax(0, 1.9fr) minmax(260px, 1fr);
  align-items: stretch;
}

@media (max-width: 1100px) {
  .grid--hero {
    grid-template-columns: 1fr;
  }
}

.row {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
}

.row--between {
  justify-content: space-between;
}

.row--end {
  justify-content: flex-end;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}

.spacer {
  flex: 1;
}

/* Span helpers for grid children. */
.col-2 { grid-column: span 2; }
.col-full { grid-column: 1 / -1; }

@media (max-width: 760px) {
  .col-2 { grid-column: span 1; }
}

/* ============================================================================
   Responsive
   ========================================================================== */

/* Below 1200px the sidebar auto-rails; main.js still honours an explicit
   user choice above this width. */
@media (max-width: 1200px) {
  .app {
    grid-template-columns: var(--sidebar-rail) 1fr;
  }

  .app .sidebar__name,
  .app .navgroup__title,
  .app .navitem__label,
  .app .sidebar__collapse span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .app .sidebar__brand,
  .app .navitem {
    justify-content: center;
    padding-inline: 0;
  }

  .app .navitem__badge {
    position: absolute;
    top: 3px;
    inset-inline-end: 9px;
    min-width: 8px;
    height: 8px;
    padding: 0;
    font-size: 0;
    border-width: 0;
    background: var(--crit);
  }

  .sidebar__collapse {
    display: none;
  }
}

/* Below 1024px the sidebar leaves the grid entirely and becomes an overlay
   drawer — no icon rail eating width on a tablet in portrait. */
@media (max-width: 1023px) {
  .app,
  .app[data-rail='1'] {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-end: 0;
    width: min(280px, 84vw);
    z-index: var(--z-drawer);
    border-inline-start: 1px solid var(--line-2);
    box-shadow: var(--e4);
    translate: 100% 0;
    visibility: hidden;
    transition: translate var(--dur-4) var(--ease-out), visibility var(--dur-4);
  }

  /* Overlay mode always shows full labels, regardless of the rail preference. */
  .app .sidebar__name,
  .app .navgroup__title,
  .app .navitem__label {
    position: static;
    width: auto;
    height: auto;
    clip-path: none;
  }

  .app .sidebar__brand,
  .app .navitem {
    justify-content: flex-start;
    padding-inline: var(--sp-3);
  }

  .app .navitem {
    /* 44px touch target. */
    min-height: 44px;
  }

  .app .navitem__badge {
    position: static;
    min-width: 19px;
    height: 19px;
    padding: 0 6px;
    font-size: var(--fs-11);
    border-width: 1px;
    background: var(--crit-soft);
  }

  .app[data-nav='open'] .sidebar {
    translate: 0 0;
    visibility: visible;
  }

  .nav-scrim {
    position: fixed;
    inset: 0;
    z-index: calc(var(--z-drawer) - 1);
    background: var(--scrim);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur-3) var(--ease-out), visibility var(--dur-3);
  }

  .app[data-nav='open'] .nav-scrim {
    opacity: 1;
    visibility: visible;
  }

  .topbar__menu {
    display: grid;
  }

  .cmdk-trigger {
    min-width: 0;
  }

  .cmdk-trigger span,
  .cmdk-trigger kbd {
    display: none;
  }

  .cmdk-trigger {
    width: 34px;
    justify-content: center;
    padding: 0;
  }
}

@media (min-width: 1024px) {
  .nav-scrim {
    display: none;
  }
}

@media (max-width: 767px) {
  .topbar {
    padding-inline: var(--sp-3);
    gap: var(--sp-2);
  }

  .content {
    padding: var(--sp-4) var(--sp-3) var(--sp-6);
  }

  .page-head {
    margin-bottom: var(--sp-4);
  }

  .page-head__actions {
    width: 100%;
  }

  .page-head__actions > * {
    flex: 1;
  }

  .grid {
    gap: var(--sp-3);
  }

  .grid--stats {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  }
}
