/* QubKey Fumadocs theme — loaded after /fumadocs-ui.css
   Align spacing with https://www.fumadocs.dev/docs inside landing Header/Footer shell */

:root {
  --qubkey-header-height: 4rem;
  --spacing: 0.25rem; /* Tailwind CSS v4 spacing unit */
}

@media (min-width: 1024px) {
  :root {
    --qubkey-header-height: 5rem;
  }
}

/* ── Landing shell: offset below fixed Header (SSR-safe via :has) ───── */
main:has(#nd-docs-layout) {
  padding-top: var(--qubkey-header-height);
}

main:has(#nd-docs-layout) + footer {
  display: none;
}

/* ── Fumadocs grid root — QubKey design tokens (globals.css) ───────── */
#nd-docs-layout.fumadocs-docs-root {
  --color-fd-primary: hsl(var(--primary));
  --color-fd-primary-foreground: hsl(var(--primary-foreground));
  --color-fd-accent: hsl(var(--accent));
  --color-fd-accent-foreground: hsl(var(--accent-foreground));
  --color-fd-ring: hsl(var(--ring));
  --color-fd-background: hsl(var(--background));
  --color-fd-foreground: hsl(var(--foreground));
  --color-fd-muted: hsl(var(--muted));
  --color-fd-muted-foreground: hsl(var(--muted-foreground));
  --color-fd-card: hsl(var(--card));
  --color-fd-card-foreground: hsl(var(--card-foreground));
  --color-fd-border: hsl(var(--border));
  --color-fd-secondary: hsl(var(--secondary));
  --color-fd-secondary-foreground: hsl(var(--secondary-foreground));
  --color-fd-popover: hsl(var(--popover));
  --color-fd-popover-foreground: hsl(var(--popover-foreground));
  --fd-banner-height: var(--qubkey-header-height);
  --fd-docs-height: calc(100dvh - var(--qubkey-header-height));
  min-height: var(--fd-docs-height);
  background-color: hsl(var(--background));
  border-top: 1px solid hsl(var(--border));
  isolation: isolate;
  color: hsl(var(--foreground));
}

/* Force Fumadocs utility classes to follow QubKey tokens */
#nd-docs-layout .text-fd-foreground {
  color: hsl(var(--foreground)) !important;
}

#nd-docs-layout .text-fd-muted-foreground {
  color: hsl(var(--muted-foreground)) !important;
}

#nd-docs-layout .text-fd-primary {
  color: hsl(var(--primary)) !important;
}

#nd-docs-layout .text-fd-card-foreground {
  color: hsl(var(--card-foreground)) !important;
}

#nd-docs-layout .text-fd-secondary-foreground {
  color: hsl(var(--secondary-foreground)) !important;
}

#nd-docs-layout .bg-fd-background {
  background-color: hsl(var(--background)) !important;
}

#nd-docs-layout .bg-fd-card {
  background-color: hsl(var(--card)) !important;
}

#nd-docs-layout .bg-fd-muted {
  background-color: hsl(var(--muted)) !important;
}

#nd-docs-layout .bg-fd-secondary {
  background-color: hsl(var(--secondary)) !important;
}

#nd-docs-layout .border-fd-border {
  border-color: hsl(var(--border)) !important;
}

/* Sidebar / TOC widths are driven by Fumadocs layout: variants in fumadocs-ui.css
   (md → sidebar 268px, xl → toc 268px). Do NOT override --fd-toc-width below xl. */

/* ── Tailwind v3 / v4 conflict fixes ───────────────────────────────────
   Landing globals.css emits unlayered `*, ::before, ::after { --tw-translate-x: 0 }`
   which overrides Fumadocs @layer utilities (translate-x, md:top, etc.).
   Use unlayered transform/top/z-index rules here (theme loads after fumadocs-ui.css). */

/* Sticky chrome must NOT use transition:all — Fumadocs `duration-250` alone
   defaults transition-property to `all`, which interpolates sticky/layout
   updates during scroll → left sidebar + TOC breadcrumb bar tremble.
   Do NOT weaken these rules when restyling docs. */

#nd-docs-layout #nd-sidebar {
  transition-property: width, inset-block, translate, background-color, border-color,
    border-radius, box-shadow !important;
  transition-duration: 0.25s;
  transition-timing-function: cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Sticky shells: never transition top/position/transform during scroll */
#nd-docs-layout [data-sidebar-placeholder],
#nd-docs-layout #nd-toc,
#nd-docs-layout #nd-subnav {
  top: var(--qubkey-header-height) !important;
  transition: none !important;
  will-change: auto;
}

/* Below landing header (+ mobile #nd-subnav when --fd-header-height > 0) */
#nd-docs-layout [data-toc-popover] {
  top: var(--fd-docs-row-2, var(--qubkey-header-height)) !important;
  transition: none !important;
  will-change: auto;
}

#nd-docs-layout [data-toc-popover] > header {
  transition-property: background-color, box-shadow, color !important;
}

/* TOC popover active-item crossfade uses translate — disable to avoid scroll jitter */
#nd-docs-layout [data-toc-popover-trigger] span.grid > span {
  transition-property: opacity, color !important;
  transform: none !important;
  translate: none !important;
}

/* Prefer CSS `translate` (Fumadocs v4) over `transform` so sticky compositing
   stays stable. Expanded sidebar must be identity — never translateX(0.5rem). */
@media (min-width: 768px) {
  #nd-docs-layout.fumadocs-docs-root {
    --fd-sidebar-width: 268px;
  }

  #nd-docs-layout #nd-sidebar[data-collapsed="false"] {
    transform: none !important;
    translate: none !important;
  }

  #nd-docs-layout #nd-sidebar[data-collapsed="true"]:not([data-hovered="true"]) {
    transform: none !important;
    translate: calc(-1 * var(--fd-sidebar-width, 268px)) 0 !important;
  }

  #nd-docs-layout #nd-sidebar[data-collapsed="true"][data-hovered="true"] {
    transform: none !important;
    translate: calc(var(--spacing, 0.25rem) * 2) 0 !important;
  }
}

/* Smooth scroll + sticky = subpixel jitter on trackpads; keep docs instant.
   Landing globals.css sets html { scroll-behavior: smooth } — override here. */
html:has(#nd-docs-layout),
html:has(#nd-docs-layout) body {
  scroll-behavior: auto !important;
}

/* ── Fumadocs #nd-subnav (secondary "Documentation" bar) ───────────────
   Landing Header already provides site chrome. Never show nav.title here.
   Desktop: hide entirely. Mobile: keep only SidebarTrigger (drawer), hide
   title + duplicate search (full search lives in the left sidebar). */
#nd-docs-layout #nd-subnav > a,
#nd-docs-layout #nd-subnav button[data-search],
#nd-docs-layout #nd-subnav [data-search],
/* Empty InlineNavTitle link when nav.title is unset */
#nd-docs-layout #nd-sidebar .flex.flex-col.gap-3.p-4 > .flex > a:empty {
  display: none !important;
}

@media (min-width: 768px) {
  #nd-docs-layout #nd-subnav {
    display: none !important;
    height: 0 !important;
    min-height: 0 !important;
    overflow: hidden !important;
    border: none !important;
    padding: 0 !important;
    pointer-events: none !important;
  }

  #nd-docs-layout.fumadocs-docs-root {
    --fd-header-height: 0px;
  }
}

/* Collapsed mini-panel (search + collapse) — force hidden while sidebar open.
   Fumadocs opacity-0 is @layer utilities and can lose to landing CSS. */
#nd-docs-layout[data-sidebar-collapsed="false"] [data-sidebar-panel] {
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

#nd-docs-layout [data-sidebar-panel] {
  transition-property: opacity !important;
}

@media (min-width: 1280px) {
  #nd-docs-layout.fumadocs-docs-root {
    --fd-toc-width: 268px;
  }
}

/* Search overlay above QubKey header (z-50) */
body:has(#nd-docs-layout) .fixed.inset-0.z-50.backdrop-blur-xs.bg-fd-overlay {
  z-index: 100 !important;
}

body:has(#nd-docs-layout) [role="dialog"].fixed.left-1\/2.z-50 {
  z-index: 101 !important;
  transform: translateX(-50%) !important;
  translate: none !important;
}

@media (min-width: 768px) {
  body:has(#nd-docs-layout) [role="dialog"].fixed.left-1\/2.z-50 {
    top: calc(50% - 250px) !important;
  }
}

/* ── Sidebar shell ───────────────────────────────────────────────────── */
#nd-docs-layout #nd-sidebar > div,
#nd-docs-layout #nd-sidebar aside {
  background-color: hsl(var(--card)) !important;
  color: hsl(var(--card-foreground));
  border-inline-end-color: hsl(var(--border)) !important;
}

/* Sidebar header row (collapse only — no "Documentation" title) */
#nd-docs-layout #nd-sidebar .flex.flex-col.gap-3.p-4 {
  padding: 1.25rem 1rem 0.75rem;
  gap: 0.75rem;
}

#nd-docs-layout #nd-sidebar .flex.flex-col.gap-3.p-4 > .flex {
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
}

/* Collapse / icon buttons in sidebar header */
#nd-docs-layout #nd-sidebar button[data-search],
#nd-docs-layout #nd-sidebar button:has(svg.lucide-sidebar) {
  color: hsl(var(--muted-foreground));
  border-radius: 0.5rem;
}

#nd-docs-layout #nd-sidebar button[data-search]:hover,
#nd-docs-layout #nd-sidebar button:has(svg.lucide-sidebar):hover {
  background: hsl(var(--primary) / 0.08);
  color: hsl(var(--primary));
}

/* Search bar — globals `* { padding: 0 }` overrides Fumadocs @layer utilities */
#nd-docs-layout button[data-search-full] {
  display: flex !important;
  align-items: center;
  width: 100%;
  gap: 0.5rem;
  padding: 0.375rem 0.5rem 0.375rem 0.625rem !important;
  border-radius: 0.5rem !important;
  border: 1px solid var(--color-fd-border) !important;
  background: var(--color-fd-secondary) !important;
  color: var(--color-fd-muted-foreground) !important;
  font-size: 0.875rem;
}

#nd-docs-layout button[data-search-full]:hover {
  background: hsl(var(--primary) / 0.08) !important;
  border-color: hsl(var(--primary) / 0.35) !important;
  color: hsl(var(--foreground)) !important;
}

#nd-docs-layout button[data-search-full] > div {
  display: inline-flex !important;
  align-items: center;
  gap: 0.25rem !important;
  margin-inline-start: auto;
}

#nd-docs-layout button[data-search-full] kbd {
  display: inline-flex !important;
  align-items: center;
  justify-content: center;
  min-width: 1.375rem;
  height: 1.375rem;
  padding: 0 0.375rem !important;
  border-radius: 0.375rem !important;
  border: 1px solid var(--color-fd-border) !important;
  background: var(--color-fd-background) !important;
  font-size: 0.6875rem;
}

/* Collapsed sidebar mini-panel — restore padding on icon buttons */
#nd-docs-layout [data-sidebar-panel] button {
  padding: 0.5rem !important;
}

/* ── Sidebar navigation links & folder buttons ─────────────────────── */
#nd-docs-layout #nd-sidebar a,
#nd-docs-layout #nd-sidebar button[aria-expanded] {
  color: hsl(var(--muted-foreground));
  border-radius: 0.5rem;
  transition: background-color 0.15s ease, color 0.15s ease;
}

#nd-docs-layout #nd-sidebar a:hover,
#nd-docs-layout #nd-sidebar button[aria-expanded]:hover {
  background: hsl(var(--primary) / 0.05) !important;
  color: hsl(var(--primary)) !important;
}

#nd-docs-layout #nd-sidebar a[data-active="true"] {
  background: hsl(var(--primary) / 0.1) !important;
  color: hsl(var(--primary)) !important;
  font-weight: 500;
}

#nd-docs-layout #nd-sidebar a[data-active="true"]::before {
  background: hsl(var(--primary)) !important;
}

/* Section labels (Démarrage, Fonctionnalités…) */
#nd-docs-layout #nd-sidebar [class*="SidebarSeparator"],
#nd-docs-layout #nd-sidebar button[aria-expanded] {
  color: hsl(var(--muted-foreground));
  font-weight: 500;
}

#nd-docs-layout #nd-sidebar button[aria-expanded][data-state="open"] {
  color: hsl(var(--foreground));
}

/* Hide empty theme switch footer row */
#nd-docs-layout #nd-sidebar .flex.text-fd-muted-foreground.items-center.border:empty {
  display: none;
}

/* ── Main article (#nd-page) — fill grid column from sidebar to TOC ─────
   Fumadocs default: max-w-[900px] mx-auto (centered narrow column).
   We override so substance uses the full [grid-area:main] slot. */
#nd-docs-layout #nd-page {
  box-sizing: border-box;
  max-width: none !important;
  width: 100% !important;
  margin-inline: 0 !important;
  margin-left: 0 !important;
  margin-right: 0 !important;
  padding-top: 1.5rem;
  padding-bottom: 3rem;
  gap: 0.375rem;
}

#nd-docs-layout #nd-page .prose {
  max-width: none !important;
  width: 100%;
}

@media (min-width: 768px) {
  #nd-docs-layout #nd-page {
    padding-top: 2rem;
    padding-inline: 1.5rem;
  }
}

@media (min-width: 1280px) {
  #nd-docs-layout #nd-page {
    padding-top: 3.5rem;
    padding-inline: 2rem;
  }
}

/* Page chrome only (outside MDX .prose body) */
#nd-docs-layout #nd-page > h1 {
  font-size: clamp(1.75rem, 2.5vw, 2.25rem);
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.25;
  margin-bottom: 0.25rem;
  color: hsl(var(--foreground));
}

#nd-docs-layout #nd-page > p.mb-8 {
  margin-bottom: 0 !important;
  font-size: 1.0625rem;
  line-height: 1.65;
  color: hsl(var(--muted-foreground));
}

/* Do NOT override #nd-page h2/h3/a/p — Fumadocs .prose + components handle MDX content */

/* Mobile page nav / breadcrumb dropdown */
#nd-docs-layout [data-breadcrumb],
#nd-docs-layout [class*="PageBreadcrumb"],
#nd-docs-layout [class*="pageTree"] button {
  color: hsl(var(--muted-foreground));
}

#nd-docs-layout [data-breadcrumb]:hover,
#nd-docs-layout [class*="PageBreadcrumb"]:hover,
#nd-docs-layout [class*="pageTree"] button:hover {
  color: hsl(var(--primary));
}

/* ── TOC (right column) ─────────────────────────────────────────────── */
#nd-docs-layout #nd-toc {
  padding-top: 3rem;
  padding-inline-end: 1.25rem;
}

#nd-docs-layout #nd-toc a {
  color: hsl(var(--muted-foreground));
}

#nd-docs-layout #nd-toc a:hover,
#nd-docs-layout #nd-toc a[data-active="true"] {
  color: hsl(var(--primary));
}

#nd-docs-layout #nd-toc h3,
#nd-docs-layout #nd-toc [class*="toc"] {
  color: hsl(var(--muted-foreground));
}

@media (max-width: 767px) {
  #nd-docs-layout.fumadocs-docs-root {
    --fd-header-height: 3.5rem;
  }

  #nd-docs-layout #nd-page {
    padding-top: 1.25rem;
    padding-inline: 1rem;
  }
}

/* ── Prose tokens — restore Fumadocs values (landing @tailwindcss/typography overrides) */
#nd-docs-layout .prose {
  --tw-prose-body: color-mix(in oklab, var(--color-fd-foreground) 90%, transparent);
  --tw-prose-headings: var(--color-fd-foreground);
  --tw-prose-lead: var(--color-fd-foreground);
  --tw-prose-links: var(--color-fd-foreground);
  --tw-prose-bold: var(--color-fd-foreground);
  --tw-prose-counters: var(--color-fd-muted-foreground);
  --tw-prose-bullets: var(--color-fd-muted-foreground);
  --tw-prose-hr: var(--color-fd-border);
  --tw-prose-quotes: var(--color-fd-foreground);
  --tw-prose-quote-borders: var(--color-fd-border);
  --tw-prose-captions: var(--color-fd-foreground);
  --tw-prose-code: var(--color-fd-foreground);
  --tw-prose-th-borders: var(--color-fd-border);
  --tw-prose-td-borders: var(--color-fd-border);
  --tw-prose-kbd: var(--color-fd-foreground);
  color: var(--tw-prose-body);
}

/* Vertical rhythm — landing `* { margin: 0 }` overrides layered .prose / space-y-* */
#nd-docs-layout .prose > * {
  margin-block: 0.625rem;
}

#nd-docs-layout .prose > :first-child {
  margin-block-start: 0;
}

#nd-docs-layout .prose > :last-child {
  margin-block-end: 0;
}

#nd-docs-layout .prose > :where(h2, h3, h4, h5, h6) {
  margin-block-start: 1.25rem;
  margin-block-end: 0.25rem;
}

#nd-docs-layout .prose > .not-prose {
  margin-block: 0.875rem;
}

#nd-docs-layout .space-y-2 > :not([hidden]) ~ :not([hidden]) {
  margin-top: 0.5rem !important;
}

#nd-docs-layout .space-y-3 > :not([hidden]) ~ :not([hidden]) {
  margin-top: 0.75rem !important;
}

#nd-docs-layout .space-y-4 > :not([hidden]) ~ :not([hidden]) {
  margin-top: 1rem !important;
}

/* Espacement serré uniquement après les titres (h1 page + h2–h6 contenu) */
#nd-docs-layout #nd-page > h1 + p {
  margin-block-start: 0.25rem;
}

#nd-docs-layout #nd-page > .prose {
  margin-block-start: 0;
}

#nd-docs-layout .prose > :where(h2, h3, h4, h5, h6) + * {
  margin-block-start: 0.25rem !important;
}

#nd-docs-layout .prose > :first-child:is(:where(h2, h3, h4, h5, h6)) {
  margin-block-start: 0;
}

/* Blank lines in MDX often become empty <p> with vertical margin */
#nd-docs-layout .prose p:empty {
  display: none;
  margin: 0;
}

/* ── Files — https://www.fumadocs.dev/docs/ui/components/files
   Inherits prose body color otherwise; set card foreground + icon colors explicitly. */
#nd-docs-layout .not-prose.rounded-md.border.bg-fd-card.p-2 {
  color: var(--color-fd-card-foreground);
}

#nd-docs-layout .not-prose.rounded-md.border.bg-fd-card.p-2 .ms-2.flex.flex-col.border-l {
  margin-inline-start: calc(var(--spacing) * 2);
  padding-inline-start: calc(var(--spacing) * 2);
  border-inline-start-width: 1px;
  border-inline-start-style: solid;
  border-color: var(--color-fd-border);
}

#nd-docs-layout .not-prose.rounded-md.border.bg-fd-card.p-2 svg {
  color: var(--color-fd-muted-foreground);
  flex-shrink: 0;
}

#nd-docs-layout .not-prose.rounded-md.border.bg-fd-card.p-2 [class*="hover:bg-fd-accent"]:hover {
  color: var(--color-fd-accent-foreground);
}

#nd-docs-layout .not-prose.rounded-md.border.bg-fd-card.p-2 [class*="hover:bg-fd-accent"]:hover svg {
  color: var(--color-fd-accent-foreground);
}

/* ── Steps — https://www.fumadocs.dev/docs/ui/components/steps
   Restore layout: landing globals.css `* { padding: 0 }` overrides @layer utilities
   in fumadocs-ui.css (same issue as Files block above). */
#nd-docs-layout .fd-steps {
  counter-reset: step;
  position: relative;
  margin-inline-start: calc(var(--spacing) * 2);
  border-inline-start-style: solid;
  border-inline-start-width: 1px;
  border-inline-start-color: var(--color-fd-border);
  padding-inline-start: calc(var(--spacing) * 6);
}

@media (width >= 40rem) {
  #nd-docs-layout .fd-steps {
    margin-inline-start: calc(var(--spacing) * 4);
    padding-inline-start: calc(var(--spacing) * 7);
  }
}

#nd-docs-layout .fd-step {
  position: relative;
}

#nd-docs-layout .fd-step > :first-child {
  margin-block-start: 0;
}

#nd-docs-layout .fd-step::before {
  background-color: var(--color-fd-secondary);
  color: var(--color-fd-secondary-foreground);
  content: counter(step);
  counter-increment: step;
  display: flex;
  position: absolute;
  top: 0.125rem;
  /* Center badge on border: -(steps padding + half badge width) */
  inset-inline-start: calc(var(--spacing) * -10);
  width: calc(var(--spacing) * 8);
  height: calc(var(--spacing) * 8);
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  line-height: 1.25rem;
  border-radius: 9999px;
  z-index: 1;
}

@media (width >= 40rem) {
  #nd-docs-layout .fd-step::before {
    inset-inline-start: calc(var(--spacing) * -11);
  }
}

#nd-docs-layout .fd-steps .fd-step + .fd-step {
  margin-top: 2rem;
}

#nd-docs-layout .fd-steps .fd-step > h3:first-child,
#nd-docs-layout .fd-steps .fd-step > h4:first-child {
  margin-top: 0;
  margin-bottom: 0.5rem;
  /* Text clearance only — badge stays on .fd-step wrapper (official Fumadocs pattern) */
  padding-inline-start: calc(var(--spacing) * 5);
}

#nd-docs-layout .fd-steps .fd-step > p {
  margin-top: 0;
  margin-bottom: 0;
  padding-inline-start: calc(var(--spacing) * 5);
}

#nd-docs-layout .fd-steps h3 a,
#nd-docs-layout .fd-steps h4 a {
  color: inherit;
  text-decoration: none;
  font-weight: inherit;
}

#nd-docs-layout .fd-steps h3 button,
#nd-docs-layout .fd-steps h4 button {
  display: none;
}
