/* =====================================================================
   AENET Cloud Public — NAVIGATION LAYER SYSTEM (v1.2)
   =====================================================================
   Canonical navigation stacking + floating K# navigator.

   This stylesheet is the single home for:
     - navigation layer / z-index tokens (no more scattered 999/9999)
     - the GLOBAL TOP BAR contract (height, sticky, hierarchy)
     - the CONTEXT BAR (sticky under the global header)
     - DISCOVERY POPOVER surfaces (opaque, elevated, viewport-safe)
     - the OVERLAY ROOT + floating K# NAVIGATOR (launcher + panel)
     - anchor scroll offsets so headings never hide under sticky bars

   Load order (generated pages):
     search-display.css → v09.css → navigation-fabric.css
     → navigation-layers.css → mobile.css
   ===================================================================== */

:root {
  /* --------------------------------------------------------------
     LAYER / Z-INDEX TOKENS (canonical — every component uses these)
     -------------------------------------------------------------- */
  --z-base: 0;
  --z-content: 10;
  --z-sticky-local: 300;
  --z-context-bar: 500;
  --z-global-header: 700;
  --z-dropdown: 900;
  --z-k-launcher: 1000;
  --z-k-panel: 1100;
  --z-modal-backdrop: 1300;
  --z-modal: 1400;
  --z-toast: 1500;

  /* --------------------------------------------------------------
     NAVIGATION DIMENSIONS (single source of truth — components never
     guess the header height themselves)
     -------------------------------------------------------------- */
  --aenet-global-header-height: 64px;
  --aenet-context-bar-height: 52px;
  --aenet-navigation-stack-height: calc(
    var(--aenet-global-header-height) + var(--aenet-context-bar-height)
  );

  /* --------------------------------------------------------------
     NAV DESIGN TOKENS
     -------------------------------------------------------------- */
  --nav-header-h: var(--aenet-global-header-height);
  --nav-context-h: var(--aenet-context-bar-height);
  --nav-surface: #0e1830;            /* opaque raised surface for popovers */
  --nav-surface-solid: #0b1424;
  --nav-border: var(--line, #1e2c49);
  --nav-text: var(--text, #e8eefc);
  --nav-muted: var(--muted, #9fb0cc);
  --nav-active: var(--system, #35e0e0);
  --nav-focus: var(--nav, #4da3ff);
  --nav-shadow: 0 18px 44px rgba(0, 0, 0, 0.55), 0 2px 8px rgba(0, 0, 0, 0.45);
  --nav-code: var(--system, #35e0e0);

  /* --------------------------------------------------------------
     K# NAVIGATOR TOKENS
     -------------------------------------------------------------- */
  --k-nav-size: 52px;
  --k-nav-offset-x: 28px;
  --k-nav-offset-y: 28px;
  --k-nav-panel-width: 400px;
  --k-nav-panel-max-height: min(640px, calc(100vh - 140px));
  --k-nav-mobile-sheet-height: 84vh;
}

/* =====================================================================
   OVERLAY ROOT
   Floating layers (K# panel, future modals) mount here — outside any
   overflow / transform / filter stacking context trap.
   ===================================================================== */
#aenet-overlay-root {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal-backdrop);
  pointer-events: none;
  visibility: hidden;
}
#aenet-overlay-root[data-active="true"] {
  pointer-events: auto;
  visibility: visible;
}

/* =====================================================================
   GLOBAL TOP BAR (LAYER N1)
   sticky, in the document flow, with a canonical height token.
   ===================================================================== */
/* The base shell (search-display.css) pins the header at z-index:1 on
   xgrid/workstation pages — equal to content. That traps the header AND
   its dropdowns below the content wrap. The header must sit above content
   (--z-global-header) so discovery popovers always paint on top. */
header.site[data-global-header],
body[data-xgrid="system"] > header.site[data-global-header],
body[data-page] > header.site[data-global-header] {
  position: sticky;
  top: 0;
  z-index: var(--z-global-header);
  min-height: var(--aenet-global-header-height);
}
header.site[data-global-header] .topbar {
  min-height: var(--aenet-global-header-height);
}

/* Topbar visual hierarchy (§15): coordinate code inline with the label —
   "A01 · Home" — one pattern for every primary menu item. Scoped to the
   primary A01–A05 links only; submenu (mega) items keep their own D-codes. */
nav.topnav > .nav-item > a[data-route-link]::before {
  content: attr(data-kgrid-slot) " · ";
  display: inline;
  color: var(--nav-code);
  font: 10px/1 var(--mono, ui-monospace, monospace);
  letter-spacing: 0.06em;
  margin-right: 2px;
  vertical-align: 1px;
}
nav.topnav > .nav-item > a[data-route-link].active::before,
nav.topnav > .nav-item > a[data-route-link][aria-current="true"]::before {
  color: var(--nav-active);
}
/* Brand never shows a coordinate chip; submenu items keep their own code. */
.brand[data-kgrid-lane="A"]::before,
.topbar-system::before,
.topbar-grid::before,
.mega-item::before {
  content: none !important;
}

/* Active global item: clear selected state, one cyan accent only. */
nav.topnav a[data-route-link].active {
  color: var(--text);
  background: rgba(53, 224, 224, 0.1);
  border-color: rgba(53, 224, 224, 0.45);
}
nav.topnav a[data-route-link].active::after,
body[data-page="home"] nav.topnav a.active::after {
  opacity: 0.95;
  transform: scaleX(1);
}

/* =====================================================================
   DISCOVERY POPOVERS (LAYER N2 — dropdown role)
   Opaque raised surface, subtle elevation, real hierarchy above content.
   No transparency that lets page text bleed through; no neon glow.
   ===================================================================== */
.nav-item-menu > .mega-panel {
  z-index: var(--z-dropdown);
  background: var(--nav-surface);
  border: 1px solid var(--nav-border);
  box-shadow: var(--nav-shadow);
  border-radius: 12px;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}
.mega-panel .mega-group {
  background: rgba(13, 22, 40, 0.96);
}
.mega-panel .mega-group:nth-child(even) {
  background: rgba(16, 28, 50, 0.96);
}
.mega-panel .mega-item,
.mega-panel a.mega-item {
  background: transparent;
}
.mega-panel a.mega-item:hover {
  background: rgba(77, 163, 255, 0.14);
}

/* Viewport-safe position (JS sets fixed coordinates when open; this is
   the no-JS fallback that stays inside the sticky header's stacking
   context). */
.nav-item-menu > .mega-panel {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  max-height: min(560px, calc(100vh - var(--aenet-global-header-height) - 24px));
  overflow-y: auto;
  overscroll-behavior: contain;
}
.nav-item-menu > .mega-panel.is-anchored {
  position: fixed;
  top: auto;
  left: auto;
}

/* Closed dropdowns must never extend the document's scrollable area. The
   hidden panel is positioned absolutely inside the sticky header, so a
   closed menu would otherwise add horizontal overflow (and empty scroll
   width) on every page. Open state is purely class/focus-driven. */
body[data-template] .mega-panel:not(.is-open):not(:focus-within) { display: none; }
body[data-template] .nav-item-menu.is-open > .mega-panel,
body[data-template] .nav-item-menu:focus-within > .mega-panel { display: grid; }

/* Medium panels (Architecture / Research / Developers) — consistent width. */
.nav-item-menu > .mega-panel:not(.mega-panel-systems) {
  width: min(360px, calc(100vw - 32px));
}

/* Systems mega-menu — full discovery matrix. The JS anchor centers it in
   the viewport; fallback keeps it readable. */
.nav-item-menu > .mega-panel.mega-panel-systems {
  width: min(1080px, calc(100vw - 36px));
  max-height: min(600px, calc(100vh - var(--aenet-global-header-height) - 20px));
  overflow-y: auto;
}

/* =====================================================================
   CONTEXT BAR (LAYER N3 — page section navigation)
   Sticky under the global header; it is real page navigation, not a
   floating dropdown. Sits in the layout flow and never covers content.
   ===================================================================== */
.context-bar {
  position: sticky;
  top: var(--aenet-global-header-height);
  z-index: var(--z-context-bar);
  background: linear-gradient(
    180deg,
    rgba(13, 21, 38, 0.99),
    rgba(11, 18, 32, 0.99)
  );
  border-bottom: 1px solid var(--line);
  padding: 8px 0 10px;
  margin: 0 0 6px;
}
.context-bar .route-context {
  border-bottom: 0;
  padding: 2px 0 4px;
}
.context-bar .context-local-nav {
  margin: 0 0 2px;
}

/* =====================================================================
   ANCHOR SCROLL OFFSET (§14)
   Section headings never slide under the sticky header + context bar.
   The JS refreshes --aenet-navigation-stack-height from real heights.
   ===================================================================== */
main [id],
section[id],
article[id],
[id^="section-"] {
  scroll-margin-top: calc(var(--aenet-navigation-stack-height, 128px) + 16px);
}

/* =====================================================================
   K# NAVIGATOR — LAUNCHER (LAYER N4)
   Floating fixed button, bottom-right, safe-area aware, ≥44×44px target.
   ===================================================================== */
.knav-launcher {
  position: fixed;
  right: calc(var(--k-nav-offset-x) + env(safe-area-inset-right));
  bottom: calc(var(--k-nav-offset-y) + env(safe-area-inset-bottom));
  z-index: var(--z-k-launcher);
  width: var(--k-nav-size);
  height: var(--k-nav-size);
  min-width: 48px;
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  border: 1px solid rgba(53, 224, 224, 0.55);
  background: linear-gradient(180deg, #101d38, #0b1424);
  color: var(--nav-active);
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5),
    inset 0 0 0 1px rgba(53, 224, 224, 0.08);
  transition: transform 0.14s ease, box-shadow 0.14s ease,
    border-color 0.14s ease, background 0.14s ease;
  appearance: none;
}
.knav-launcher:hover {
  border-color: rgba(53, 224, 224, 0.9);
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.55),
    0 0 18px rgba(53, 224, 224, 0.18);
  transform: translateY(-1px);
}
.knav-launcher:focus-visible {
  outline: 2px solid var(--nav-focus);
  outline-offset: 3px;
}
.knav-launcher[aria-expanded="true"] {
  border-color: var(--nav-active);
  box-shadow: 0 0 0 3px rgba(53, 224, 224, 0.18),
    0 0 22px rgba(53, 224, 224, 0.22);
}
.knav-launcher-label {
  font-family: inherit;
}
/* Micro status dot (§54): green = public gateway normal, amber = index
   stale. Never a private health signal; text always present in the panel. */
.knav-status-dot {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--action, #2edc8a);
  box-shadow: 0 0 6px color-mix(in srgb, var(--action, #2edc8a) 60%, transparent);
}
.knav-status-dot[data-status="stale"] {
  background: var(--warning, #ffb020);
  box-shadow: 0 0 6px color-mix(in srgb, var(--warning, #ffb020) 60%, transparent);
}

/* Home workstation footer is fixed at the bottom — keep the launcher
   above it instead of colliding. */
body[data-page="home"] .knav-launcher {
  bottom: calc(var(--k-nav-offset-y) + 34px + env(safe-area-inset-bottom));
}

/* =====================================================================
   K# NAVIGATOR — PANEL
   Desktop: floating card above the launcher. Mounted in #aenet-overlay-root
   so it can never be clipped or trapped by a page stacking context.
   ===================================================================== */
.knav-panel {
  position: fixed;
  right: calc(var(--k-nav-offset-x) + env(safe-area-inset-right));
  bottom: calc(
    var(--k-nav-size) + var(--k-nav-offset-y) + 14px + env(safe-area-inset-bottom)
  );
  z-index: var(--z-k-panel);
  width: min(var(--k-nav-panel-width), calc(100vw - 32px));
  max-height: var(--k-nav-panel-max-height);
  display: flex;
  flex-direction: column;
  background: var(--nav-surface);
  border: 1px solid var(--nav-border);
  border-radius: 14px;
  box-shadow: var(--nav-shadow);
  opacity: 0;
  visibility: hidden;
  transform: translateY(8px);
  transition: opacity 0.16s ease, transform 0.16s ease, visibility 0.16s;
}
.knav-panel.is-open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.knav-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px 10px;
  border-bottom: 1px solid var(--line);
}
.knav-title {
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 12px;
  letter-spacing: 0.1em;
  color: var(--nav-active);
  text-transform: uppercase;
  white-space: nowrap;
}
.knav-hint {
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--nav-muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 6px;
  white-space: nowrap;
}

.knav-search-wrap {
  padding: 10px 14px 6px;
}
.knav-search {
  width: 100%;
  min-height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--nav-surface-solid);
  color: var(--nav-text);
  font-size: 14px;
}
.knav-search:focus-visible {
  outline: 2px solid var(--nav-focus);
  outline-offset: 1px;
  border-color: rgba(77, 163, 255, 0.6);
}
.knav-search::placeholder {
  color: var(--nav-muted);
}

.knav-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 14px 14px;
  flex: 1 1 auto;
}
.knav-section + .knav-section {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}
.knav-section-title {
  margin: 0 0 6px;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--nav-muted);
}
.knav-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}
.knav-link {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 6px;
  color: var(--nav-text);
  font-size: 14px;
  text-decoration: none;
}
.knav-link:hover {
  background: rgba(77, 163, 255, 0.12);
  text-decoration: none;
}
.knav-link:focus-visible {
  outline: 2px solid var(--nav-focus);
  outline-offset: 1px;
}
.knav-link[aria-disabled="true"],
.knav-link.is-planned {
  opacity: 0.55;
  cursor: default;
}
.knav-link-code {
  flex: 0 0 auto;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  letter-spacing: 0.06em;
  color: var(--nav-active);
}
.knav-link-domain {
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  letter-spacing: 0.05em;
  color: var(--nav-muted);
  text-transform: uppercase;
}

/* Current coordinate block (§25). */
.knav-coordinate {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid rgba(53, 224, 224, 0.35);
  border-radius: 8px;
  background: rgba(53, 224, 224, 0.06);
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 12px;
  line-height: 1.5;
  color: var(--nav-text);
  letter-spacing: 0.04em;
}
.knav-coordinate b {
  color: var(--nav-active);
  font-weight: 600;
}
.knav-coordinate-path {
  display: block;
  color: var(--nav-muted);
  font-size: 11px;
  margin-top: 2px;
}
/* KGRID template badge (§ — K# shows the active page template, from the
   same data-template attribute that drives layout). */
.knav-template {
  display: inline-block;
  margin-left: 8px;
  padding: 1px 6px;
  border: 1px solid rgba(77, 163, 255, 0.4);
  border-radius: 4px;
  background: rgba(77, 163, 255, 0.12);
  color: var(--nav-active, #4da3ff);
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  letter-spacing: 0.08em;
  vertical-align: 1px;
}

/* Search results (§27). */
.knav-results {
  border-top: 1px solid var(--line);
  max-height: 46vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px 8px 10px;
}
.knav-results[hidden] {
  display: none;
}
.knav-result {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 6px;
  color: var(--nav-text);
  font-size: 14px;
  text-decoration: none;
}
.knav-result:hover {
  background: rgba(77, 163, 255, 0.12);
  text-decoration: none;
}
.knav-result:focus-visible {
  outline: 2px solid var(--nav-focus);
  outline-offset: 1px;
}
.knav-result-code {
  flex: 0 0 auto;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  color: var(--nav-active);
}
.knav-result-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.knav-result-meta {
  margin-left: auto;
  flex: 0 0 auto;
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 10px;
  letter-spacing: 0.05em;
  color: var(--nav-muted);
  text-transform: uppercase;
}
.knav-empty {
  margin: 8px 10px;
  font-size: 13px;
  color: var(--nav-muted);
}

/* =====================================================================
   TABLET (641–1024px)
   ===================================================================== */
@media (max-width: 1024px) {
  :root {
    --k-nav-offset-x: 20px;
    --k-nav-offset-y: 20px;
    --aenet-global-header-height: 60px;
  }
  .knav-panel {
    right: calc(20px + env(safe-area-inset-right));
    bottom: calc(var(--k-nav-size) + 20px + 14px + env(safe-area-inset-bottom));
    width: min(400px, calc(100vw - 24px));
  }
}

/* =====================================================================
   MOBILE (≤900px)
   Launcher stays; the panel becomes a bottom sheet (§30). The context bar
   stops being sticky so it can never sit under the taller wrapped header.
   ===================================================================== */
@media (max-width: 900px) {
  :root {
    --aenet-global-header-height: 56px;
    --aenet-context-bar-height: 0px;
  }
  .context-bar {
    position: static;
    top: auto;
  }
  .knav-launcher {
    right: calc(16px + env(safe-area-inset-right));
    bottom: calc(16px + env(safe-area-inset-bottom));
    width: 52px;
    height: 52px;
  }
  body[data-page="home"] .knav-launcher {
    bottom: calc(16px + 34px + env(safe-area-inset-bottom));
  }
  .knav-panel {
    left: 10px;
    right: 10px;
    bottom: 0;
    top: auto;
    width: auto;
    max-width: none;
    max-height: var(--k-nav-mobile-sheet-height);
    border-radius: 16px 16px 0 0;
    border-bottom: 0;
    transform: translateY(16px);
  }
  .knav-panel.is-open {
    transform: translateY(0);
  }
}

/* =====================================================================
   REDUCED MOTION
   ===================================================================== */
@media (prefers-reduced-motion: reduce) {
  .knav-panel,
  .knav-launcher,
  .nav-item-menu > .mega-panel {
    transition: none;
  }
}
