import { usePublishAeroView } from '../../contexts/aero-view-context.js'
import {
  useEffect,
  useId,
  useMemo,
  useRef,
  useState,
  type KeyboardEvent,
  type ReactNode,
} from 'react'
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
import {
  AlertTriangle,
  Check,
  ChevronDown,
  ChevronRight,
  ExternalLink,
  LoaderCircle,
  Play,
  Search,
  Settings2,
} from 'lucide-react'
import {
  formatPercent,
  RatioUnits,
  RunKinds,
  RunStatuses,
  SITE_AUDIT_DEFAULT_MAX_DEPTH,
  SITE_CRAWL_GRAPH_MAX_EDGES,
  SITE_CRAWL_GRAPH_MAX_NODES,
  SiteCrawlIndexabilityReasons,
  SiteHealthStates,
  TEMPLATE_LINK_MIN_FETCHED_PAGES,
  isTemplateDetectionApplied,
  type SiteAuditScoreDto,
  type SiteCrawlEdgeDto,
  type SiteCrawlGraphNodeDto,
  type SiteCrawlIndexabilityReason,
  type SiteCrawlPageAuditDto,
  type SiteCrawlPageDto,
  type SiteCrawlSiteDto,
  type SiteCrawlStructureChildDto,
  type SiteCrawlStructureResponseDto,
  type SiteCrawlTermination,
  type SiteHealthScanDto,
  type SiteHealthState,
  type SiteHealthTemplateDetection,
} from '@canonry/canonry-contracts'
import {
  getApiV1ProjectsByNameOptions,
  getApiV1ProjectsByNameAgentReadinessOptions,
  getApiV1ProjectsByNameAgentReadinessCrawlOptions,
  getApiV1ProjectsByNameAgentReadinessCrawlPagesAuditOptions,
  getApiV1ProjectsByNameAgentReadinessCrawlPagesOptions,
  getApiV1ProjectsByNameAgentReadinessCrawlPagesInfiniteOptions,
  getApiV1ProjectsByNameAgentReadinessDeadLinksOptions,
  getApiV1ProjectsByNameAgentReadinessGraphOptions,
  getApiV1ProjectsByNameAgentReadinessInternalLinksNeighborsOptions,
  getApiV1ProjectsByNameAgentReadinessRunsOptions,
  getApiV1ProjectsByNameAgentReadinessRunsByRunIdProgressOptions,
  getApiV1ProjectsByNameAgentReadinessStructureInfiniteOptions,
} from '@canonry/canonry-api-client/react-query'

import { heyClient, isDashboardManagedRunKind, isEmbed, isPublicDemo } from '../../api.js'
import { useAccount } from '../../contexts/account-context.js'
import { ManagedSweepStatus, MANAGED_SCANS_COPY } from './ManagedSweepStatus.js'
import { cn } from '../../lib/utils.js'
import { PAGE_BUDGET_HELP, PAGE_BUDGET_PRESETS, oneOffFullSiteChoice, projectDefaultPageBudgetLabel } from '../../lib/site-audit-page-budget.js'
import { useTriggerSiteAudit } from '../../queries/mutations.js'
import { STATIC_VISIBILITY_STALE_MS } from '../../queries/query-client.js'
import type { MetricTone } from '../../view-models.js'
import { SiteGraphSigma } from './SiteGraphSigma.js'
import {
  siteGraphStatusDescription,
  siteGraphStatusLabel,
  siteGraphVisualState,
  type SiteGraphVisualState,
} from './site-graph-sigma.js'
import { displayPagePath, otherSiteAddress, siteHostFromUrl, siteOfUrl } from './site-health-paths.js'
import { scanSiteChoices, toggleSite } from '../../lib/site-audit-sites.js'
import { PageAuditEvidence } from './PageAuditEvidence.js'
import { SITE_HEALTH_CHECK_HELP } from './site-health-check-help.js'
import { TechnicalAeoSection } from './TechnicalAeoSection.js'
import { WriteButton } from '../shared/AccessControls.js'
import { ToneBadge } from '../shared/ToneBadge.js'
import { InfoTooltip } from '../shared/InfoTooltip.js'
import { Button } from '../ui/button.js'

type SiteHealthView = 'map' | 'inventory' | 'technical'

// The `inventory` id is the stable wire/route token. Only the label reads
// "Pages"; nothing keyed off the id changes with it.
const SITE_HEALTH_VIEWS = [
  { id: 'map', label: 'Map' },
  { id: 'inventory', label: 'Pages' },
  { id: 'technical', label: 'Score' },
] as const satisfies ReadonlyArray<{ id: SiteHealthView; label: string }>

const SITE_HEALTH_VIEW_DESCRIPTIONS: Record<SiteHealthView, string> = {
  map: 'Explore how pages, site sections, and internal links fit together.',
  inventory: 'Review discovered pages and the links that shape their visibility.',
  technical: 'Prioritize findings and inspect the pages that need work.',
}

/**
 * Page-list filters. Each one maps to a server-side filter the crawl/pages
 * route already supports, so a chip never narrows only the loaded window.
 */
type InventoryFilterId = 'all' | 'hidden'

/** One site a scan covered: the main domain first, then the owned domains it included. */
type SiteScanSite = Pick<SiteCrawlSiteDto, 'host' | 'pages' | 'error'>

/** Whether the selected page's own row (which carries its reasons) is known. */
type PageReasonsState = 'idle' | 'loading' | 'ready' | 'error'

/**
 * `hidden` is the DERIVED health state, not `indexabilityState=noindex`: a
 * redirect, a robots block, a non-HTML response, and a canonical pointing
 * elsewhere all hide a page from answer engines without ever setting noindex.
 * The route derives this with the same contract function the map uses.
 */
const INVENTORY_FILTERS = [
  { id: 'all', label: 'All pages', healthState: undefined },
  { id: 'hidden', label: 'Hidden pages', healthState: SiteHealthStates.hidden },
] as const satisfies ReadonlyArray<{
  id: InventoryFilterId
  label: string
  healthState: SiteHealthState | undefined
}>

/**
 * The address as two lines: the path, then any parameters, which are what tell
 * two rows with the same path apart. Parameters are decoded for reading only;
 * the full URL stays in the row's title and in what it opens.
 */
function inventoryAddress(url: string, fallbackPath: string, rootHost: string | null): { path: string; query: string | null } {
  return splitAddress(displayPagePath(url, rootHost) || fallbackPath)
}

function splitAddress(display: string): { path: string; query: string | null } {
  const start = display.indexOf('?')
  if (start < 0) return { path: display, query: null }
  const query = display.slice(start)
  let readable = query
  try {
    readable = decodeURIComponent(query.replaceAll('+', ' '))
  } catch {
    readable = query
  }
  return { path: display.slice(0, start), query: readable }
}

/**
 * Every URL version a scan found: those fetched and set aside, plus those the
 * crawl never fetched once their parameter pattern proved to make copies.
 */
function urlVersionsFound(summary: { urlVersions: number; urlVersionsNotFetched?: number } | null | undefined): number | null {
  if (!summary) return null
  return summary.urlVersions + (summary.urlVersionsNotFetched ?? 0)
}

/** The caveat beside the page count; its sentence is the tooltip. */
function urlVersionsSkippedHelp(count: number, wholeScan = false): { label: string; text: string } {
  // A list narrowed to one site still counts the whole scan's URL versions.
  const label = `${metricValue(count)} URL version${count === 1 ? '' : 's'} skipped${wholeScan ? ' in this scan' : ''}`
  return {
    label,
    text: `${label}. These addresses add parameters, such as a filter, to a page and name that page as canonical, so they are copies of it. The scan does not score them or list them here.`,
  }
}

/**
 * Plain-word reading of the crawler's machine indexability reasons. The
 * `Record` over the closed union is what makes a newly added reason a compile
 * error here instead of raw machine text in the UI.
 */
const INDEXABILITY_REASON_COPY: Record<SiteCrawlIndexabilityReason, string> = {
  [SiteCrawlIndexabilityReasons.metaRobotsNoindex]: 'Hidden by meta robots tag',
  [SiteCrawlIndexabilityReasons.xRobotsNoindex]: 'Hidden by X-Robots-Tag header',
  [SiteCrawlIndexabilityReasons.robotsDisallow]: 'Blocked by robots.txt',
  [SiteCrawlIndexabilityReasons.redirectTerminal]: 'Redirects to another page',
  [SiteCrawlIndexabilityReasons.canonicalToOther]: 'Points to another page as canonical',
  [SiteCrawlIndexabilityReasons.notHtmlOrUnavailable]: 'Not an HTML page, so it was not scored',
}

/** Persisted rows stay string-backed, so the lookup must admit a miss. */
const INDEXABILITY_REASON_LABELS = new Map<string, string>(Object.entries(INDEXABILITY_REASON_COPY))

/**
 * An unrecognized reason is shown verbatim. Dropping it would hide the only
 * evidence the crawler gave for a page being out of the index.
 */
function indexabilityReasonLabel(reason: string): string {
  return INDEXABILITY_REASON_LABELS.get(reason) ?? reason
}

const SCAN_HISTORY_LIMIT = 20
const INVENTORY_LIMIT = 200
const STRUCTURE_LIMIT = 100
const NEIGHBOR_LIMIT = 100
const DEAD_LINK_LIMIT = 50
const GRAPH_NODE_LIMIT = SITE_CRAWL_GRAPH_MAX_NODES
const GRAPH_EDGE_LIMIT = SITE_CRAWL_GRAPH_MAX_EDGES

const numberFormatter = new Intl.NumberFormat()
const scanDateFormatter = new Intl.DateTimeFormat(undefined, {
  month: 'short',
  day: 'numeric',
  year: 'numeric',
})

const SITE_HEALTH_STATUS_TONES: Record<SiteGraphVisualState, MetricTone> = {
  eligible: 'positive',
  hidden: 'caution',
  // Neutral: a file that is not a page, and a page that moved, are facts about
  // what the crawler found, not problems to fix.
  resource: 'neutral',
  redirect: 'neutral',
  failed: 'negative',
  unchecked: 'neutral',
}

function formatScanDate(value: string | null | undefined): string {
  if (!value) return 'Date unavailable'
  const date = new Date(value)
  return Number.isNaN(date.getTime()) ? 'Date unavailable' : scanDateFormatter.format(date)
}

function titleCase(value: string): string {
  return value
    .replaceAll('_', ' ')
    .replaceAll('-', ' ')
    .replace(/\b\w/g, (letter) => letter.toUpperCase())
}

type InspectableCrawlPage = SiteCrawlPageDto | SiteCrawlGraphNodeDto

function crawlStatus(page: InspectableCrawlPage): { label: string; tone: MetricTone } {
  const state = siteGraphVisualState(page)
  return {
    label: siteGraphStatusLabel(state),
    tone: SITE_HEALTH_STATUS_TONES[state],
  }
}

/**
 * `linkScoreNormalized` is 0 to 100 against the crawl's top page, from a real
 * audit and the demo alike, so a page scoring 0.85 reads 0.9%.
 */
function formatImportance(value: number | null): string {
  if (value == null) return 'Not scored'
  return formatPercent(value, RatioUnits.percent)
}

function formatHealth(page: InspectableCrawlPage): string {
  return page.auditScore == null ? 'Not checked' : `${Math.round(page.auditScore)}/100`
}

function metricValue(value: number | null | undefined): string {
  return value == null ? 'Not available' : numberFormatter.format(value)
}

/**
 * What every Agent Readiness number means, in plain words.
 *
 * One source of truth, because the same four metrics appear on the page
 * inspector tiles AND as Pages table columns. Two copies would drift, and a
 * metric explained differently in two places is worse than one not explained
 * at all.
 */
type SiteHealthMetric =
  | 'clicksFromHome'
  | 'linksIn'
  | 'linksOut'
  | 'linkImportance'
  | 'technicalScore'
  | 'linkTimes'
  | 'pagesFound'
  | 'pagesChecked'
  | 'internalLinks'

const SITE_HEALTH_METRIC_HELP: Record<SiteHealthMetric, string> = {
  clicksFromHome: 'How many clicks it takes to reach this page from the home page, following links.',
  linksIn: 'How many other pages link to this page.',
  linksOut: 'How many other pages this page links to.',
  linkImportance: 'How much link value flows to this page, based on how many pages link to it and how important those pages are. Shown relative to the highest page on this site, which is 100%.',
  technicalScore: 'How well this page is set up for AI and search engines to read, from 0 to 100. Open a page to see what it is marked down for.',
  linkTimes: 'How many times this link appears on the page it comes from.',
  pagesFound: 'How many pages this scan discovered, whether or not it could load them.',
  pagesChecked: 'How many of the pages it found this scan actually loaded.',
  internalLinks: 'How many links between pages on this site the scan recorded.',
}

/**
 * Metrics the crawl engine computes over the FULL link graph, before nav and
 * footer links are told apart. The filter cannot change them, so they say so
 * out loud: without that, a reader seeing them beside two filtered counts
 * would reasonably assume all four were filtered.
 */
const FULL_GRAPH_METRICS: ReadonlySet<SiteHealthMetric> = new Set([
  'clicksFromHome',
  'linkImportance',
  'internalLinks',
  'pagesFound',
  'pagesChecked',
])

/**
 * Help text for one metric, told truthfully for the surface it sits on.
 * `filtered` means THIS surface is currently hiding nav and footer links.
 */
function siteHealthMetricHelp(metric: SiteHealthMetric, filtered: boolean): string {
  const base = SITE_HEALTH_METRIC_HELP[metric]
  if (FULL_GRAPH_METRICS.has(metric)) {
    // Page counts are not link counts: say what the menu/footer toggle does to them.
    return metric === 'pagesFound' || metric === 'pagesChecked'
      ? `${base} Hiding menu and footer links does not change it.`
      : `${base} This always counts every link, including menu and footer.`
  }
  if (metric === 'technicalScore' || metric === 'linkTimes') return base
  return filtered
    ? `${base} Right now this counts only links written in your page text. Menu and footer links are hidden.`
    : `${base} This counts every link, including menu and footer.`
}

/** Counted nouns in map copy. "1 content links" reads like a bug report. */
function countedLinks(count: number, noun: string): string {
  return `${numberFormatter.format(count)} ${noun}${count === 1 ? '' : 's'}`
}

function movedSiteHosts(requestedRootUrl: string | null, effectiveRootUrl: string | null): {
  requested: string
  effective: string
} | null {
  if (!requestedRootUrl || !effectiveRootUrl) return null
  try {
    const requestedUrl = new URL(requestedRootUrl)
    const effectiveUrl = new URL(effectiveRootUrl)
    const identity = (url: URL) => url.hostname.toLowerCase().replace(/\.$/, '').replace(/^www\./, '')
    return identity(requestedUrl) !== identity(effectiveUrl)
      ? { requested: requestedUrl.host, effective: effectiveUrl.host }
      : null
  } catch {
    return null
  }
}

function scanTone(status: string | null | undefined): MetricTone {
  if (status === 'completed') return 'positive'
  if (status === 'partial' || status === 'queued' || status === 'running') return 'caution'
  if (status === 'failed' || status === 'cancelled') return 'negative'
  return 'neutral'
}

/**
 * The shown scan's score, for the header beside its status: the API's
 * `aggregateScore` as sent, from the same read the Score view makes. A partial
 * scan scored only the pages it reached, so it reads "Score so far" there too.
 */
function headerScoreLabel(score: Pick<SiteAuditScoreDto, 'hasData' | 'runStatus' | 'aggregateScore'> | undefined): string | null {
  if (!score?.hasData) return null
  if (score.runStatus === RunStatuses.partial) return `Score so far ${score.aggregateScore}/100`
  if (score.runStatus === RunStatuses.completed) return `Score ${score.aggregateScore}/100`
  return null
}

const DEFAULT_SCAN_OPTION_LABEL = 'Latest scan'
const DEFAULT_SCAN_HELP = 'Shows the most complete scan from the latest day with results. Choose a dated entry to view another scan.'

/**
 * A scan that kept no crawl is still real, selectable history. Say what it
 * holds ("Score only") rather than hiding it or letting it look broken.
 */
function scanOptionLabel(scan: SiteHealthScanDto): string {
  const when = formatScanDate(scan.finishedAt ?? scan.startedAt ?? scan.createdAt)
  const suffix = scan.hasCrawlData ? '' : ' · Score only'
  return `${when} · ${titleCase(scan.status)}${suffix}`
}

/**
 * Why a scan stopped, in plain words. A closed Record over the crawler's own
 * vocabulary, so a new reason is a compile error rather than silently falling
 * through to generic copy.
 */
const TERMINATION_COPY: Record<SiteCrawlTermination, string> = {
  'complete': 'This scan finished on its own.',
  'unknown': 'This scan stopped before it checked every page it found.',
  'max-pages': 'This scan stopped at the page limit, so some pages were not checked.',
  'max-edges': 'This scan stopped at the link limit, so some links are missing.',
  'max-fetches': 'This scan stopped after checking as many pages as it could.',
  'max-duration': 'This scan ran out of time, so some pages were not checked.',
  'max-bytes': 'This scan stopped at the data limit, so some pages were not checked.',
  'max-page-bytes': 'This scan skipped a page that was too large to read.',
  'max-depth': 'This scan stopped at the depth limit, so deeper pages were not checked.',
  'max-links-per-page': 'This scan stopped reading links on pages that had too many.',
  'max-query-variants': 'This scan stopped at the limit for pages that differ only by a query string.',
  'max-sitemap-fanout': 'This scan stopped at the sitemap limit, so some sitemaps were not read.',
  'max-sitemap-urls': 'This scan stopped at the sitemap page limit, so some pages were not found.',
  'root-host-redirect': 'The site moved to another address during this scan.',
}

const TERMINATION_LABELS = new Map<string, string>(Object.entries(TERMINATION_COPY))

/**
 * Crawl budgets the operator can actually set for one scan, as a short list
 * rather than a free number. A scan that stops early is the single most common
 * complaint about a first result, and until now the only way to change either
 * budget was the CLI, which the dashboard never mentions.
 *
 * The picker's first option, ahead of these, sends no budget: the server then
 * applies the project's saved budget (Project Settings), else the full site.
 * It resolves that before computing request identity, so sending nothing and
 * sending the same number explicitly are the same request. The option's label
 * reads the saved budget from the project; the dashboard never fills one in.
 */
const PAGE_BUDGET_CHOICES = PAGE_BUDGET_PRESETS

// "No limit set" is not unlimited: the crawler stops at its own default. Name
// it, and offer deeper limits, or "Raise the crawl depth" has nowhere to go
// from a default scan.
const CRAWL_DEPTH_CHOICES: readonly { value: number | null; label: string }[] = [
  { value: null, label: `Default (${SITE_AUDIT_DEFAULT_MAX_DEPTH} clicks)` },
  { value: 1, label: '1 click from the home page' },
  { value: 2, label: '2 clicks' },
  { value: 3, label: '3 clicks' },
  { value: 5, label: '5 clicks' },
  { value: 20, label: '20 clicks' },
  { value: 50, label: '50 clicks' },
  { value: 100, label: '100 clicks (maximum)' },
]

/**
 * What to change so the next scan gets further. Every budget in this list is
 * editable in Scan settings; the reasons that are not about a budget get no
 * suggestion rather than a misleading one.
 */
const TERMINATION_REMEDY: Partial<Record<SiteCrawlTermination, string>> = {
  'max-pages': 'Raise the page budget in Scan settings.',
  'max-edges': 'Raise the page budget in Scan settings.',
  'max-fetches': 'Raise the page budget in Scan settings.',
  // A time limit is the one case where the fix is a SMALLER scan, which is
  // the direction nobody guesses.
  'max-duration': 'Try a smaller scan in Scan settings.',
  'max-depth': 'Raise the crawl depth in Scan settings.',
}

function terminationRemedy(termination: string | null): string | null {
  if (!termination) return null
  return TERMINATION_REMEDY[termination as SiteCrawlTermination] ?? null
}

function terminationCopy(termination: string | null): string {
  if (!termination) return 'This scan stopped before it checked every page it found.'
  // An unrecognized reason is shown as-is: it is the only thing the crawler
  // told us about why the scan stopped.
  return TERMINATION_LABELS.get(termination) ?? `This scan stopped early: ${termination}.`
}

/**
 * How this scan told nav and footer links apart, in plain words. A closed
 * Record over the contract's own vocabulary, so a new state is a compile error
 * rather than a silently missing explanation.
 *
 * Every state has copy, including the ones where the control works, because the
 * two rules do not measure the same thing: a reader comparing this month's
 * content-link count with last month's needs to know whether the number moved
 * because the site changed or because the scan could finally see where each
 * link sits.
 */
const TEMPLATE_DETECTION_COPY: Record<SiteHealthTemplateDetection, string> = {
  'applied': 'This scan told menu and footer links apart by how often the same link repeats across pages. It cannot spot a link written into the page text when its wording matches the menu. Run a new scan to read the page layout instead.',
  'applied-placement': 'This scan read where each link sits in the page, so links in the page text are separated from the menu, header, and footer even when they use the same wording.',
  'applied-placement-with-ubiquity': 'This scan read where each link sits in the page. Some pages mark out no menu or main area, so those links fall back to how often the link repeats across pages, which can miss a link written into the page text.',
  'applied-placement-partial': `This scan read where each link sits in the page. Some pages mark out no menu or main area, and this scan found fewer than ${TEMPLATE_LINK_MIN_FETCHED_PAGES} pages, so nothing could tell those links apart. They are counted as links in your page text, which is what a link no rule marked as menu, header, or footer means here.`,
  'unavailable-too-few-pages': `This scan found fewer than ${TEMPLATE_LINK_MIN_FETCHED_PAGES} pages and did not read where each link sits in the page. On a site that small every link is on most pages, so menu and footer links cannot be told apart from the rest.`,
  'unavailable-legacy-scan': 'This scan ran before menu and footer links were separated. Run a new scan to split them out.',
}

/** Persisted rows stay string-backed, so the lookup must admit a miss. */
const TEMPLATE_DETECTION_LABELS = new Map<string, string>(Object.entries(TEMPLATE_DETECTION_COPY))

function templateDetectionCopy(detection: SiteHealthTemplateDetection | null): string {
  if (detection === null) return ''
  // A value outside the union can still arrive on the wire. Fall back to the
  // "run a new scan" copy, which is true of any scan we cannot classify.
  return TEMPLATE_DETECTION_LABELS.get(detection) ?? TEMPLATE_DETECTION_COPY['unavailable-legacy-scan']
}

/**
 * Why a map's stored page positions predate the current link split. Kept beside
 * the detection copy because both answer the same question a reader has about
 * the same numbers, and both belong in the same tooltip rather than in a second
 * and third line of the header strip.
 */
/** Heading help for the map. Was a subtitle line under the heading. */
const SITE_MAP_HELP = 'Scroll to zoom. Click a page to inspect it.'

/** Heading help for the page inspector's link section. Was a subtitle line. */
export const PAGE_INTERNAL_LINKS_HELP = 'Observed links to and from this page in the selected scan.'

const SITE_MAP_STALE_LAYOUT_COPY =
  'Page positions on this map were set before menu and footer links were separated. Run a new scan to update them.'

/**
 * The map header's VISIBLE line: the numbers, and nothing else.
 *
 * It used to carry the counts plus two more lines of prose, which is more copy
 * than a header strip can hold. The numbers are what a reader scans for; the
 * explanation behind them is one hover or one tab away in `siteMapLinkRuleHelp`.
 *
 * "Content link" and "template link" are OUR words, not a reader's. The real
 * distinction is where the link was written: in the page's own text, or in the
 * furniture that repeats on every page. The copy says that; the wire format
 * (`linkKind=content|template|all`) is unchanged and stays our vocabulary.
 */
function siteMapLinkCountsLabel(counts: {
  filterUnavailable: boolean
  showTemplateLinks: boolean
  contentEdgeCount: number
  templateEdgeCount: number
  totalEdgeCount: number
}): string {
  if (counts.filterUnavailable) return `All ${countedLinks(counts.totalEdgeCount, 'link')} shown.`
  const inText = `${numberFormatter.format(counts.contentEdgeCount)} link${counts.contentEdgeCount === 1 ? '' : 's'} in your page text`
  const template = numberFormatter.format(counts.templateEdgeCount)
  return counts.showTemplateLinks
    ? `${inText}, ${template} menu and footer.`
    : `${inText}. ${template} menu and footer link${counts.templateEdgeCount === 1 ? '' : 's'} hidden.`
}

/**
 * Why the split is worth having, before how it was made.
 *
 * A reader does not arrive wanting to know which rule ran. They want to know
 * why the map hides most of their links, so the tooltip answers that first and
 * only then explains the rule behind the numbers.
 */
const SITE_MAP_LINK_SPLIT_COPY =
  'Menu, header, and footer links repeat on every page, so they say nothing about which pages relate to each other. Links written in your page text do.'

/**
 * Everything the header line no longer says out loud: what the split is for,
 * which rule produced it, and whether the positions predate it. Nothing was
 * dropped in the compression, it moved into the tooltip.
 */
function siteMapLinkRuleHelp(
  detection: SiteHealthTemplateDetection | null,
  options: { staleLayout: boolean },
): string {
  const parts = [SITE_MAP_LINK_SPLIT_COPY, templateDetectionCopy(detection)]
  if (options.staleLayout) parts.push(SITE_MAP_STALE_LAYOUT_COPY)
  return parts.filter(Boolean).join(' ')
}

/**
 * Tri-state, and it never shows a bare zero when the check did not run: "0"
 * would read as "we looked and found none".
 *
 * `unverified` links are ones the crawler never got a response for, which is a
 * fact about the scan and not about the link. They must never be added to
 * `found`, and "none found" must not be shown as a clean bill of health while
 * some links went unchecked — that is the same claim of proven absence, one
 * step further along.
 */
function deadLinkLabel(state: string, found?: number, unverified?: number): { label: string; tone: MetricTone } {
  if (state === 'disabled') return { label: 'Broken links: not checked', tone: 'neutral' }
  const unchecked = unverified ?? 0
  const uncheckedSuffix = unchecked > 0 ? `, ${numberFormatter.format(unchecked)} unchecked` : ''
  if (state === 'complete') {
    if (found) return { label: `Broken links: ${numberFormatter.format(found)} found${uncheckedSuffix}`, tone: 'negative' }
    return unchecked > 0
      ? { label: `Broken links: none found, ${numberFormatter.format(unchecked)} unchecked`, tone: 'caution' }
      : { label: 'Broken links: none found', tone: 'positive' }
  }
  if (state === 'partial') {
    return { label: `Broken links: ${numberFormatter.format(found ?? 0)} found so far${uncheckedSuffix}`, tone: 'caution' }
  }
  return { label: 'Broken links: not checked', tone: 'neutral' }
}

/**
 * What one direction's tile should say, given the active link filter.
 *
 * The tiles used to always show the crawl's full-graph totals while the tables
 * beneath them honoured the content-only filter, so one panel read "Links in
 * 48" directly above a table headed "Links in (1)". Both were right and the
 * pair was unreadable. The tile now counts exactly what the table lists.
 */
function linkTileCount(counts: {
  total: number
  visible: number
  hidden: number
  truncated: boolean
  showTemplateLinks: boolean
  known: boolean
}): { value: string; hiddenNote: string | null; filtered: boolean } {
  // `filtered` is decided HERE, by the same branch that decides the number, so
  // the tooltip can never describe a count this function did not produce.
  //
  // Filter off, or no per-kind answer to give: the crawl's own total, which is
  // the only number here that covers every link.
  if (counts.showTemplateLinks || !counts.known) {
    return { value: metricValue(counts.total), hiddenNote: null, filtered: false }
  }
  // The neighbour read is bounded, so a truncated list can only prove a lower
  // bound. Say so rather than presenting a partial count as the answer.
  return {
    filtered: true,
    value: counts.truncated ? `${metricValue(counts.visible)}+` : metricValue(counts.visible),
    hiddenNote: counts.hidden === 0
      ? null
      : counts.truncated
        ? `At least ${metricValue(counts.hidden)} menu and footer hidden`
        : `${metricValue(counts.hidden)} menu and footer hidden`,
  }
}

function LinkMetricTile({ label, value, hiddenNote, help }: {
  label: string
  value: string
  hiddenNote?: string | null
  help: string
}) {
  return (
    <div className="px-4 py-3">
      <dt className="flex items-center gap-1 text-label-12 text-muted">
        {label}
        <InfoTooltip text={help} />
      </dt>
      <dd className="mt-1 text-label-14-strong tabular-nums text-heading">{value}</dd>
      {hiddenNote && <dd className="mt-0.5 text-label-12 text-muted">{hiddenNote}</dd>}
    </div>
  )
}

function LinkMetrics({ page, inbound, outbound }: {
  page: InspectableCrawlPage
  inbound: ReturnType<typeof linkTileCount>
  outbound: ReturnType<typeof linkTileCount>
}) {
  // Depth and link importance are computed by the crawl engine over the FULL
  // link graph, before nav and footer links are classified, so the filter does
  // not change them. That used to be said twice: once in a footnote under this
  // grid, and once in the two affected tiles' own tooltips, which
  // `siteHealthMetricHelp` already appends it to for every FULL_GRAPH_METRIC.
  // The footnote was the duplicate, so it is gone and nothing was lost.
  return (
    <>
      <dl className="grid grid-cols-2 divide-x divide-y divide-default material-base sm:grid-cols-4 sm:divide-y-0">
        <LinkMetricTile
          label="Clicks from home"
          value={String(page.depth ?? 'Not reached')}
          help={siteHealthMetricHelp('clicksFromHome', false)}
        />
        <LinkMetricTile
          label="Links in"
          value={inbound.value}
          hiddenNote={inbound.hiddenNote}
          help={siteHealthMetricHelp('linksIn', inbound.filtered)}
        />
        <LinkMetricTile
          label="Links out"
          value={outbound.value}
          hiddenNote={outbound.hiddenNote}
          help={siteHealthMetricHelp('linksOut', outbound.filtered)}
        />
        <LinkMetricTile
          label="Link importance"
          value={formatImportance(page.linkScoreNormalized)}
          help={siteHealthMetricHelp('linkImportance', false)}
        />
      </dl>
    </>
  )
}

/**
 * What an empty link list MEANS, in plain words with the real numbers.
 *
 * A page whose only connections are nav and footer chrome is editorially
 * orphaned, which is the single most useful thing this map can tell someone.
 * Drawing nothing and saying nothing turns that finding into what looks like a
 * broken map, so the counts are always named. Both numbers come from the
 * neighbours read; nothing here is inferred.
 */
export function emptyLinkCopy(
  direction: 'inbound' | 'outbound',
  hiddenTemplateCount: number,
  truncated: boolean,
): string {
  if (hiddenTemplateCount === 0) {
    return direction === 'inbound'
      ? 'Nothing links to this page.'
      : 'This page links to nothing.'
  }
  const lead = direction === 'inbound'
    ? 'No links in the page text point here.'
    : 'No links in the page text lead away from here.'
  // A truncated list can only prove a lower bound, so it says so rather than
  // rounding a partial count into a fact.
  const hidden = truncated
    ? `At least ${metricValue(hiddenTemplateCount)} menu and footer links hidden.`
    : `${countedLinks(hiddenTemplateCount, 'menu and footer link')} hidden.`
  return `${lead} ${hidden}`
}

/**
 * A table column heading with its plain-words explanation attached.
 *
 * Table columns read the server's full-graph counts, so they are never the
 * filtered variant. The page inspector tiles are, which is why they call
 * `siteHealthMetricHelp` themselves with the live toggle state.
 */
function ColumnLabel({ label, metric }: { label: string; metric: SiteHealthMetric }) {
  return (
    <span className="inline-flex items-center gap-1">
      {label}
      <InfoTooltip text={siteHealthMetricHelp(metric, false)} />
    </span>
  )
}

/**
 * A linked page's address. In a scan of more than one site, a page on another
 * site than the open page's names that site under its path; a page on the open
 * page's own site needs no site name.
 */
function LinkedPageAddress({ url, rootHost, siteHosts, pageSite }: {
  url: string
  rootHost: string | null
  siteHosts: readonly string[]
  pageSite: string | null
}) {
  const linkSite = siteOfUrl(url)
  if (siteHosts.length < 2 || linkSite === null || !siteHosts.includes(linkSite)) {
    return <>{displayPagePath(url, rootHost)}</>
  }
  let path = url
  try {
    const parsed = new URL(url)
    path = `${parsed.pathname}${parsed.search}` || '/'
  } catch {
    // An address that does not parse is shown whole.
  }
  return (
    <>
      <span className="block truncate">{path}</span>
      {linkSite !== pageSite && <span className="block truncate text-label-12-mono text-secondary">{linkSite}</span>}
    </>
  )
}

function NeighborTable({
  direction,
  edges,
  truncated,
  rootHost,
  hiddenTemplateCount,
  siteHosts = [],
  pageUrl = null,
}: {
  direction: 'inbound' | 'outbound'
  edges: SiteCrawlEdgeDto[]
  truncated: boolean
  rootHost: string | null
  hiddenTemplateCount: number
  siteHosts?: readonly string[]
  /** The inspected page: a linked page on its own site needs no site name. */
  pageUrl?: string | null
}) {
  const pageSite = siteOfUrl(pageUrl)
  const heading = direction === 'inbound' ? 'Links in' : 'Links out'
  return (
    <section className="min-w-0" aria-labelledby={`site-health-${direction}-heading`}>
      <div className="mb-2 flex items-center justify-between gap-3">
        <h4 id={`site-health-${direction}-heading`} className="text-heading-14 text-heading">
          {heading} ({metricValue(edges.length)})
        </h4>
        {truncated && <span className="text-label-12 text-muted">First {NEIGHBOR_LIMIT}</span>}
      </div>
      {edges.length === 0 ? (
        <p className="material-base px-3 py-4 text-copy-14 text-secondary">
          {emptyLinkCopy(direction, hiddenTemplateCount, truncated)}
        </p>
      ) : (
        <div className="evidence-table-wrap max-h-64 overflow-auto">
          <table className="evidence-table site-health-table min-w-[420px]">
            <thead>
              <tr>
                <th scope="col">Page</th>
                <th scope="col">Anchor text</th>
                <th scope="col"><ColumnLabel label="Times" metric="linkTimes" /></th>
              </tr>
            </thead>
            <tbody>
              {edges.map((edge) => {
                const url = direction === 'inbound' ? edge.sourceUrl : edge.targetUrl
                return (
                <tr key={edge.edgeKey}>
                  {/* The path is the display; the full URL stays on hover. */}
                  <td className="max-w-64 truncate font-mono" title={url}>
                    <LinkedPageAddress url={url} rootHost={rootHost} siteHosts={siteHosts} pageSite={pageSite} />
                  </td>
                  <td className="max-w-52 truncate" title={edge.anchors.join(', ') || undefined}>
                    {edge.anchors.join(', ') || 'No anchor text'}
                  </td>
                  <td className="tabular-nums">{metricValue(edge.occurrences)}</td>
                </tr>
                )
              })}
            </tbody>
          </table>
        </div>
      )}
    </section>
  )
}

/**
 * Only the full page DTO carries the crawler's reasons; the compact graph node
 * does not. The inspector must therefore look the page up in the inventory
 * read rather than reuse whichever object it selected from, or a page picked
 * on the map silently shows no reason at all.
 */
function indexabilityReasons(page: InspectableCrawlPage | null): readonly string[] {
  return page && 'indexabilityReasons' in page ? page.indexabilityReasons : []
}

/** The open page's heading: its path, after its site's name when that is not the main domain. */
function PageHeadingAddress({ url, path, rootHost, siteHosts }: {
  url: string
  path: string
  rootHost: string | null
  siteHosts: readonly string[]
}) {
  const other = otherSiteAddress(url, rootHost, siteHosts)
  return other ? <><span className="text-secondary">{other.site}</span>{other.path}</> : <>{path}</>
}

function PageInspector({
  page,
  isLoading,
  error,
  inbound,
  outbound,
  inboundTruncated,
  outboundTruncated,
  audit,
  auditLoading,
  auditError,
  onRetryAudit,
  rootHost,
  reasonSource,
  reasonsState,
  onRetryReasons,
  showTemplateLinks,
  siteHosts = [],
}: {
  page: InspectableCrawlPage | null
  isLoading: boolean
  error: unknown
  inbound: SiteCrawlEdgeDto[]
  outbound: SiteCrawlEdgeDto[]
  inboundTruncated: boolean
  outboundTruncated: boolean
  audit: SiteCrawlPageAuditDto | undefined
  auditLoading: boolean
  auditError: unknown
  onRetryAudit: () => void
  rootHost: string | null
  /** The same page from the inventory read, which carries the crawler reasons. */
  reasonSource: SiteCrawlPageDto | null
  reasonsState: PageReasonsState
  onRetryReasons: () => void
  /** Mirrors the map, so the two never disagree about one page. */
  showTemplateLinks: boolean
  siteHosts?: readonly string[]
}) {
  if (!page) {
    return (
      <section className="border-t border-default pt-5" aria-label="Selected page details">
        <h3 className="text-heading-16 text-heading">Page links</h3>
        <p className="mt-1 text-copy-14 text-secondary">Select a page to inspect its internal links and crawl signals.</p>
      </section>
    )
  }

  const status = crawlStatus(page)
  const reasons = indexabilityReasons(reasonSource ?? page)
  // Only the full page DTO knows; a URL version is opened from the map, never the list.
  const versionOf = reasonSource?.urlVersionOf ?? null
  // The inspector shows what the MAP shows. Listing nav links the map is
  // hiding would make the two disagree about the same page.
  const visibleInbound = showTemplateLinks ? inbound : inbound.filter((edge) => !edge.isTemplate)
  const visibleOutbound = showTemplateLinks ? outbound : outbound.filter((edge) => !edge.isTemplate)
  // Until the neighbour read lands there is no per-kind answer, so the tiles
  // stay on the crawl's own totals rather than briefly showing a zero.
  const linkCountsKnown = !isLoading && !error
  const hiddenInboundCount = inbound.length - visibleInbound.length
  const hiddenOutboundCount = outbound.length - visibleOutbound.length
  // The tiles read from exactly the lists the tables render, so the panel
  // cannot show two different answers for the same page again.
  const inboundTile = linkTileCount({
    total: page.inboundUniqueEdges,
    visible: visibleInbound.length,
    hidden: hiddenInboundCount,
    truncated: inboundTruncated,
    showTemplateLinks,
    known: linkCountsKnown,
  })
  const outboundTile = linkTileCount({
    total: page.outboundUniqueEdges,
    visible: visibleOutbound.length,
    hidden: hiddenOutboundCount,
    truncated: outboundTruncated,
    showTemplateLinks,
    known: linkCountsKnown,
  })
  return (
    <section className="border-t border-default pt-5" aria-labelledby="site-health-page-inspector-title">
      <div className="flex flex-col justify-between gap-3 sm:flex-row sm:items-start">
        <div className="min-w-0">
          <div className="flex flex-wrap items-center gap-2">
            <h3 id="site-health-page-inspector-title" className="break-words font-mono text-heading-16 text-heading">
              <PageHeadingAddress url={page.url} path={page.path} rootHost={rootHost} siteHosts={siteHosts} />
            </h3>
            <ToneBadge tone={status.tone}>{status.label}</ToneBadge>
          </div>
          <p className="mt-1 break-all text-copy-14 text-secondary">{page.url}</p>
          {reasonsState === 'loading' ? (
            <p className="mt-2 text-copy-14 text-secondary" role="status">Loading page details…</p>
          ) : reasonsState === 'error' ? (
            <p className="mt-2 flex flex-wrap items-center gap-2 text-copy-14 text-negative" role="alert">
              Page details could not be loaded, so any reason this page is hidden is unknown.
              <Button type="button" variant="secondary" size="sm" onClick={onRetryReasons}>Try again</Button>
            </p>
          ) : reasons.length > 0 || versionOf ? (
            <ul className="mt-2 flex flex-wrap gap-x-4 gap-y-1 text-label-14 text-secondary" aria-label="Why this page is hidden">
              {versionOf && <li>{`URL version of ${displayPagePath(versionOf, rootHost)}, not scored`}</li>}
              {reasons.map((reason) => (
                <li key={reason}>{indexabilityReasonLabel(reason)}</li>
              ))}
            </ul>
          ) : null}
        </div>
        <Button asChild variant="secondary" size="sm">
          <a href={page.url} target="_blank" rel="noreferrer">
            Open page <ExternalLink className="size-3.5" aria-hidden="true" />
          </a>
        </Button>
      </div>

      <div className="mt-4">
        <PageAuditEvidence
          audit={audit}
          isLoading={auditLoading}
          error={auditError}
          onRetry={onRetryAudit}
        />
      </div>

      <section className="mt-5 border-t border-default pt-5" aria-labelledby="site-health-page-links-heading">
        <div className="flex items-center gap-1">
          <h3 id="site-health-page-links-heading" className="text-heading-16 text-heading">Internal links</h3>
          <InfoTooltip text={PAGE_INTERNAL_LINKS_HELP} />
        </div>
        <div className="mt-4">
          <LinkMetrics page={page} inbound={inboundTile} outbound={outboundTile} />
        </div>
        <div className="mt-5">
          {isLoading ? (
          <div className="flex items-center gap-2 py-6 text-label-14 text-secondary">
            <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" />
            Loading page links…
          </div>
        ) : error ? (
          <p className="rounded-md border border-negative bg-negative-soft px-4 py-3 text-copy-14 text-negative" role="alert">
            Page links could not be loaded.
          </p>
        ) : (
          <div className="grid gap-5 lg:grid-cols-2">
            <NeighborTable
              direction="inbound"
              edges={visibleInbound}
              truncated={inboundTruncated}
              rootHost={rootHost}
              hiddenTemplateCount={hiddenInboundCount}
              siteHosts={siteHosts}
              pageUrl={page.url}
            />
            <NeighborTable
              direction="outbound"
              edges={visibleOutbound}
              truncated={outboundTruncated}
              rootHost={rootHost}
              hiddenTemplateCount={hiddenOutboundCount}
              siteHosts={siteHosts}
              pageUrl={page.url}
            />
          </div>
          )}
        </div>
      </section>
    </section>
  )
}

function InventoryTable({
  pages,
  total,
  skippedUrlVersions,
  rootHost,
  selectedNodeKey,
  onSelect,
  hasNextPage,
  isFetchingNextPage,
  onLoadMore,
  filter,
  onFilterChange,
  filterUnavailable,
  sites,
  siteFilter,
  onSiteFilterChange,
}: {
  pages: SiteCrawlPageDto[]
  total: number
  /** URL versions the scan found (see `urlVersionsFound`); null or 0 shows nothing. */
  skippedUrlVersions: number | null
  rootHost: string | null
  selectedNodeKey: string | null
  onSelect: (nodeKey: string) => void
  hasNextPage: boolean
  isFetchingNextPage: boolean
  onLoadMore: () => void
  filter: InventoryFilterId
  onFilterChange: (filter: InventoryFilterId) => void
  /** This scan predates persisted health state, so it cannot be filtered. */
  filterUnavailable: boolean
  /** The sites this scan covered, main site first. One site shows no site filter or site names. */
  sites: readonly SiteScanSite[]
  siteFilter: string
  onSiteFilterChange: (site: string) => void
}) {
  const [search, setSearch] = useState('')
  const siteHosts = sites.map((site) => site.host)
  const normalizedSearch = search.trim().toLowerCase()
  const visiblePages = useMemo(
    () => normalizedSearch
      ? pages.filter((page) => `${page.path} ${page.url}`.toLowerCase().includes(normalizedSearch))
      : pages,
    [normalizedSearch, pages],
  )
  const searchCoversLoadedWindowOnly = normalizedSearch.length > 0 && (hasNextPage || pages.length < total)

  return (
    <section aria-labelledby="site-health-inventory-heading">
      <div className="mb-3 flex flex-col justify-between gap-3 sm:flex-row sm:items-end">
        <div>
          <h2 id="site-health-inventory-heading" className="text-heading-16 text-heading">Pages</h2>
          <div className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1">
            {/* Not "pages found": the scan counts strip says "Pages found" for
                every address the scan discovered, which is a bigger number
                than this list of pages (URL versions are left out). */}
            <p className="text-copy-14 text-secondary">
              {filter === 'all'
                ? `Showing ${metricValue(pages.length)} of ${metricValue(total)} pages.`
                : `Showing ${metricValue(pages.length)} of ${metricValue(total)} hidden pages.`}
            </p>
            {skippedUrlVersions != null && skippedUrlVersions > 0 && (
              <InfoTooltip {...urlVersionsSkippedHelp(skippedUrlVersions, siteFilter !== 'all')} />
            )}
          </div>
        </div>
        <div className="flex w-full flex-wrap items-center gap-2 sm:w-auto" role="group" aria-label="Page filters">
          {sites.length > 1 && (
            <select
              aria-label="Filter pages by site"
              value={siteFilter}
              onChange={(event) => onSiteFilterChange(event.target.value)}
              className="h-9 rounded-md border border-base bg-bg px-3 text-label-14 text-primary outline-none focus:border-strong focus:ring-2 focus:ring-mono-600"
            >
              <option value="all">All sites</option>
              {sites.map((site) => (
                <option key={site.host} value={site.host}>{site.host}</option>
              ))}
            </select>
          )}
          <select
            aria-label="Filter pages by status"
            value={filter}
            onChange={(event) => {
              const next = INVENTORY_FILTERS.find((option) => option.id === event.target.value)
              if (next) onFilterChange(next.id)
            }}
            className="h-9 rounded-md border border-base bg-bg px-3 text-label-14 text-primary outline-none focus:border-strong focus:ring-2 focus:ring-mono-600"
          >
            {INVENTORY_FILTERS.map((option) => (
              <option key={option.id} value={option.id}>{option.label}</option>
            ))}
          </select>
          <label className={cn('relative block min-w-0 flex-1 sm:w-72 sm:flex-none', sites.length > 1 && 'basis-full sm:basis-auto')}>
            <span className="sr-only">Search loaded pages</span>
            <Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted" aria-hidden="true" />
            <input
              type="search"
              value={search}
              onChange={(event) => setSearch(event.target.value)}
              placeholder="Search loaded pages"
              className="h-9 w-full rounded-md border border-base bg-bg pl-9 pr-3 text-label-14 text-primary outline-none placeholder-mono-400 focus:border-strong focus:ring-2 focus:ring-mono-600"
            />
          </label>
        </div>
      </div>

      <div className="evidence-table-wrap">
        <table className="evidence-table site-health-table min-w-[800px]">
          <thead>
            <tr>
              <th scope="col">Page</th>
              <th scope="col" title={siteGraphStatusDescription('eligible')}>Status</th>
              <th scope="col"><ColumnLabel label="Clicks from home" metric="clicksFromHome" /></th>
              <th scope="col"><ColumnLabel label="Links in" metric="linksIn" /></th>
              <th scope="col"><ColumnLabel label="Links out" metric="linksOut" /></th>
              <th scope="col"><ColumnLabel label="Link importance" metric="linkImportance" /></th>
              <th scope="col"><ColumnLabel label="Score" metric="technicalScore" /></th>
            </tr>
          </thead>
          <tbody>
            {visiblePages.map((page) => {
              const status = crawlStatus(page)
              const other = otherSiteAddress(page.url, rootHost, siteHosts)
              const address = other ? splitAddress(other.path) : inventoryAddress(page.url, page.path, rootHost)
              // A list of one site needs no site name; a mixed list names the other site under the path.
              const siteLine = other && siteFilter === 'all' ? other.site : null
              return (
                <tr key={page.nodeKey} className={selectedNodeKey === page.nodeKey ? 'bg-surface-active' : undefined}>
                  <td className="max-w-80">
                    <button
                      type="button"
                      onClick={() => onSelect(page.nodeKey)}
                      className="block max-w-full rounded-sm text-left outline-none focus-visible:ring-2 focus-visible:ring-mono-400"
                      title={page.url}
                    >
                      <span className="block truncate font-mono text-button-14 text-link">{address.path}</span>
                      {siteLine && <span className="block truncate text-label-12-mono text-secondary">{siteLine}</span>}
                      {address.query && (
                        <span className="line-clamp-2 [overflow-wrap:anywhere] text-label-12-mono text-secondary">{address.query}</span>
                      )}
                    </button>
                  </td>
                  <td title={siteGraphStatusDescription(siteGraphVisualState(page))}>
                    <ToneBadge tone={status.tone}>{status.label}</ToneBadge>
                  </td>
                  <td className="tabular-nums">{page.depth ?? 'Not reached'}</td>
                  <td className="tabular-nums">{metricValue(page.inboundUniqueEdges)}</td>
                  <td className="tabular-nums">{metricValue(page.outboundUniqueEdges)}</td>
                  <td className="tabular-nums">{formatImportance(page.linkScoreNormalized)}</td>
                  <td className="tabular-nums">{formatHealth(page)}</td>
                </tr>
              )
            })}
          </tbody>
        </table>
      </div>
      {visiblePages.length === 0 && (
        <p className="border-x border-b border-default px-4 py-8 text-center text-copy-14 text-secondary">
          {normalizedSearch.length === 0
            ? filterUnavailable
              ? 'This scan cannot be filtered. Run a new scan to filter its pages.'
              : filter === 'hidden'
                ? 'No hidden pages were found in this scan.'
                : 'No pages are available.'
            : searchCoversLoadedWindowOnly
              ? `No matches in the ${metricValue(pages.length)} loaded pages. Load more pages to continue searching.`
              : 'No pages match this search.'}
        </p>
      )}
      {hasNextPage && (
        <div className="flex justify-center border-x border-b border-default px-4 py-3">
          <Button variant="secondary" size="sm" disabled={isFetchingNextPage} onClick={onLoadMore}>
            {isFetchingNextPage && <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" />}
            {isFetchingNextPage ? 'Loading more pages' : 'Load more pages'}
          </Button>
        </div>
      )}
    </section>
  )
}

function SiteSectionRow({
  projectName,
  runId,
  site,
  section,
  onSelect,
}: {
  projectName: string
  runId: string
  /** The site these folders belong to, in a scan of more than one site. */
  site?: string
  section: SiteCrawlStructureChildDto
  onSelect: (path: string, site?: string) => void
}) {
  const [expanded, setExpanded] = useState(false)
  const descendantCount = section.pageCount - (section.hasPage ? 1 : 0)
  const expandable = descendantCount > 0

  return (
    <li>
      <div className="flex min-h-11 items-center gap-1 px-2 py-1">
        {expandable ? (
          <button
            type="button"
            aria-label={`${expanded ? 'Collapse' : 'Expand'} ${section.path}`}
            aria-expanded={expanded}
            onClick={() => setExpanded((current) => !current)}
            className="flex size-8 shrink-0 items-center justify-center rounded-sm text-muted outline-none hover:bg-surface-hover hover:text-heading focus-visible:ring-2 focus-visible:ring-mono-400"
          >
            {expanded
              ? <ChevronDown className="size-3.5" aria-hidden="true" />
              : <ChevronRight className="size-3.5" aria-hidden="true" />}
          </button>
        ) : (
          <span className="block size-8 shrink-0" aria-hidden="true" />
        )}
        <button
          type="button"
          onClick={() => onSelect(section.path, site)}
          className="min-w-0 flex-1 break-words rounded-sm py-2 text-left font-mono text-button-14 text-heading outline-none hover:text-link focus-visible:ring-2 focus-visible:ring-mono-400"
        >
          {section.path}
        </button>
        <span className="shrink-0 px-2 text-label-12-mono text-muted">{metricValue(section.pageCount)}</span>
      </div>
      {expanded && (
        <div className="ml-6 border-l border-default pl-1">
          <SiteSectionChildren
            projectName={projectName}
            runId={runId}
            site={site}
            parentPath={section.path}
            onSelect={onSelect}
          />
        </div>
      )}
    </li>
  )
}

/**
 * The crawl root sits in no folder, so a folders-only list left the home page
 * with nowhere to be clicked. It leads the top-level list as its own row.
 */
function RootPageRow({ page, onSelect }: {
  page: Pick<SiteCrawlGraphNodeDto, 'nodeKey' | 'path' | 'inventoryEligible'>
  onSelect: (nodeKey: string) => void
}) {
  return (
    <li className="bg-surface-subtle">
      <div className="flex min-h-11 items-center gap-1 px-2 py-1">
        <span className="block size-8 shrink-0" aria-hidden="true" />
        <button
          type="button"
          onClick={() => onSelect(page.nodeKey)}
          className="min-w-0 flex-1 break-words rounded-sm py-2 text-left font-mono text-button-14 text-heading outline-none hover:text-link focus-visible:ring-2 focus-visible:ring-mono-400"
        >
          {page.path || '/'}
        </button>
        <span className="shrink-0 px-2 text-label-12-mono text-muted">{metricValue(1)}</span>
      </div>
    </li>
  )
}

/**
 * One site's folders, under the site's name, when a scan covered more than one
 * site. Closed at first, so no site's folders bury the others. A site the scan
 * could not reach says so in place of a page count.
 */
function SiteBranch({ site, children }: { site: SiteScanSite; children: ReactNode }) {
  const [expanded, setExpanded] = useState(false)
  const reachable = site.error === null
  return (
    <li>
      <div className="flex min-h-11 items-center gap-1 px-2 py-1">
        {reachable ? (
          <button
            type="button"
            aria-label={`${expanded ? 'Collapse' : 'Expand'} ${site.host}`}
            aria-expanded={expanded}
            onClick={() => setExpanded((current) => !current)}
            className="flex size-8 shrink-0 items-center justify-center rounded-sm text-muted outline-none hover:bg-surface-hover hover:text-heading focus-visible:ring-2 focus-visible:ring-mono-400"
          >
            {expanded
              ? <ChevronDown className="size-3.5" aria-hidden="true" />
              : <ChevronRight className="size-3.5" aria-hidden="true" />}
          </button>
        ) : (
          <span className="block size-8 shrink-0" aria-hidden="true" />
        )}
        <span className="min-w-0 flex-1 break-words py-2 text-label-14-strong text-heading">{site.host}</span>
        {reachable
          ? <span className="shrink-0 px-2 text-label-12-mono text-muted">{metricValue(site.pages)}</span>
          : <span className="shrink-0 px-2 text-label-12 text-secondary" title={site.error ?? undefined}>Not reached</span>}
      </div>
      {expanded && <div className="ml-6 border-l border-default pl-1">{children}</div>}
    </li>
  )
}

function SiteSectionChildren({
  projectName,
  runId,
  site,
  parentPath,
  onSelect,
  rootPage,
  onSelectRootPage,
}: {
  projectName: string
  runId: string
  /** The site whose folders to list, in a scan of more than one site; the main domain when omitted. */
  site?: string
  parentPath: string
  onSelect: (path: string, site?: string) => void
  /** Only the top-level list carries it; nested folder lists pass null. */
  rootPage?: Pick<SiteCrawlGraphNodeDto, 'nodeKey' | 'path' | 'inventoryEligible'> | null
  onSelectRootPage?: (nodeKey: string) => void
}) {
  const structureInput = {
    client: heyClient,
    path: { name: projectName },
    query: { runId, parentPath, limit: STRUCTURE_LIMIT, ...(site ? { site } : {}) },
  } as const
  const structureQuery = useInfiniteQuery({
    ...getApiV1ProjectsByNameAgentReadinessStructureInfiniteOptions(structureInput),
    initialPageParam: structureInput,
    getNextPageParam: (lastPage: SiteCrawlStructureResponseDto) => lastPage.nextCursor
      ? {
          path: structureInput.path,
          query: { ...structureInput.query, cursor: lastPage.nextCursor },
        }
      : undefined,
  })
  const sections = structureQuery.data?.pages.flatMap((page) => page.children) ?? []

  if (structureQuery.isLoading) {
    return (
      <div className="flex items-center gap-2 px-3 py-4 text-label-14 text-secondary" role="status">
        <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" /> Loading sections…
      </div>
    )
  }
  if (structureQuery.error) {
    return <p className="px-3 py-4 text-copy-14 text-negative" role="alert">Site sections could not be loaded.</p>
  }
  if (sections.length === 0 && !rootPage) {
    return <p className="px-3 py-4 text-copy-14 text-secondary">No nested sections were found.</p>
  }

  return (
    <>
      <ul className="divide-y divide-default">
        {rootPage && onSelectRootPage && (
          <RootPageRow page={rootPage} onSelect={onSelectRootPage} />
        )}
        {sections.map((section) => (
          <SiteSectionRow
            key={section.path}
            projectName={projectName}
            runId={runId}
            site={site}
            section={section}
            onSelect={onSelect}
          />
        ))}
      </ul>
      {structureQuery.hasNextPage && (
        <div className="px-3 py-2">
          <Button
            variant="ghost"
            size="sm"
            disabled={structureQuery.isFetchingNextPage}
            onClick={() => void structureQuery.fetchNextPage()}
          >
            {structureQuery.isFetchingNextPage ? 'Loading more sections' : 'Load more sections'}
          </Button>
        </div>
      )}
    </>
  )
}

function GraphLoadingState() {
  return (
    <div
      role="status"
      className="flex min-h-[420px] items-center justify-center material-base px-6 text-center lg:min-h-[520px]"
    >
      <div className="max-w-sm">
        <LoaderCircle className="mx-auto size-5 motion-safe:animate-spin text-muted" aria-hidden="true" />
        <p className="mt-3 text-copy-14-strong text-heading">Preparing the interactive site map</p>
        <p className="mt-1 text-copy-14 text-secondary">Page and link data is ready for the graph renderer.</p>
      </div>
    </div>
  )
}

type SiteAuditProgressSnapshot = {
  phase: 'queued' | 'discovering' | 'checking' | 'arranging-map' | 'completed' | 'partial' | 'failed' | 'cancelled'
  attempt: {
    pagesDiscovered: number
    pagesFetched: number
    edgesDiscovered: number
    pagesErrored: number
    error: string | null
  } | null
  error: string | null
}

function scanPhaseCopy(phase: SiteAuditProgressSnapshot['phase'] | 'running' | 'queued' | null | undefined): string {
  if (phase === 'discovering') return 'Discovering pages'
  if (phase === 'checking') return 'Checking pages'
  if (phase === 'arranging-map') return 'Arranging map'
  if (phase === 'queued') return 'Waiting to start'
  return 'Preparing scan'
}

const SCAN_STEP_COUNT = 3

/** Which of the three scan steps a phase is in: find pages, check pages, build the result. */
function scanStepIndex(phase: SiteAuditProgressSnapshot['phase'] | 'running' | 'queued' | null | undefined): number {
  if (phase === 'checking') return 1
  if (phase === 'arranging-map') return 2
  return 0
}

/**
 * The scan as three steps. Finished steps are full and the current step
 * pulses: a scan's page budget is not known here, so the bar never shows a
 * guessed percentage.
 */
function ScanPhaseBar({ phase, pagesChecked }: {
  phase: SiteAuditProgressSnapshot['phase'] | 'running' | 'queued' | null | undefined
  pagesChecked: number | null
}) {
  const current = scanStepIndex(phase)
  const labels = [
    'Find pages',
    current === 1 && pagesChecked !== null ? `Check pages · ${metricValue(pagesChecked)}` : 'Check pages',
    'Build map',
  ]
  return (
    <div className="mt-4" role="progressbar" aria-label="Scan progress" aria-valuemin={0} aria-valuemax={SCAN_STEP_COUNT} aria-valuenow={current} aria-valuetext={labels[current]}>
      <div className="grid grid-cols-[1fr_2fr_1fr] gap-1.5" aria-hidden="true">
        {labels.map((label, index) => (
          <div key={label} className="h-2 overflow-hidden rounded-full bg-surface-inset">
            {index < current ? <div className="h-full w-full rounded-full bg-positive-400" />
              : index === current ? <div className="h-full w-full rounded-full bg-positive-400/40 motion-safe:animate-pulse" />
                : null}
          </div>
        ))}
      </div>
      <div className="mt-2 grid grid-cols-[1fr_2fr_1fr] gap-1.5 text-label-14" aria-hidden="true">
        {labels.map((label, index) => (
          <span key={label} className={`flex min-w-0 items-center gap-1 truncate ${index === current ? 'text-label-14-strong text-heading' : index < current ? 'text-secondary' : 'text-muted'}`}>
            {index < current ? <Check className="h-3.5 w-3.5 shrink-0" aria-hidden="true" /> : null}
            <span className="truncate">{label}</span>
          </span>
        ))}
      </div>
    </div>
  )
}

function shouldPollSiteAuditProgress(phase: SiteAuditProgressSnapshot['phase'] | undefined): boolean {
  return phase !== 'completed'
    && phase !== 'partial'
    && phase !== 'failed'
    && phase !== 'cancelled'
}

function isActiveSiteAuditPhase(phase: SiteAuditProgressSnapshot['phase'] | undefined): boolean {
  return phase === 'queued'
    || phase === 'discovering'
    || phase === 'checking'
    || phase === 'arranging-map'
}

function isTerminalSiteAuditPhase(phase: SiteAuditProgressSnapshot['phase'] | undefined): boolean {
  return phase === 'completed'
    || phase === 'partial'
    || phase === 'failed'
    || phase === 'cancelled'
}

function queryErrorCode(error: unknown): string | null {
  if (!error || typeof error !== 'object') return null
  const candidate = error as { code?: unknown; error?: unknown }
  if (typeof candidate.code === 'string') return candidate.code
  if (candidate.error && typeof candidate.error === 'object') {
    const nested = candidate.error as { code?: unknown }
    return typeof nested.code === 'string' ? nested.code : null
  }
  return null
}

function TransientSiteHealthPanel({
  view,
  children,
}: {
  view: Exclude<SiteHealthView, 'technical'>
  children: ReactNode
}) {
  return (
    <div
      id={`site-health-${view}-panel`}
      role="tabpanel"
      aria-labelledby={`site-health-${view}-tab`}
      className="min-w-0"
    >
      {children}
    </div>
  )
}

function ActiveScanState({
  status,
  progress,
  progressError,
  onRetryProgress,
}: {
  status: SiteAuditProgressSnapshot['phase'] | 'running'
  progress?: SiteAuditProgressSnapshot | null
  progressError?: boolean
  onRetryProgress?: () => void
}) {
  const attempt = progress?.attempt ?? null
  const phase = progress?.phase ?? status
  const arrangingMap = phase === 'arranging-map'

  return (
    <section
      aria-label="Current scan progress"
      className="material-base px-5 py-6"
    >
      <div className="flex min-h-6 items-center gap-2">
        <h2 className="text-heading-16 text-heading">
          {arrangingMap ? 'Preparing site map' : 'Scanning site'}
        </h2>
      </div>
      <p aria-atomic="true" aria-label="Current scan progress" aria-live="polite" role="status" className="sr-only">
        {arrangingMap
          ? 'Arranging map. The scan is complete and its map is being published.'
          : `${scanPhaseCopy(phase)}. The map appears after the scan finishes.`}
      </p>
      <ScanPhaseBar phase={phase} pagesChecked={attempt?.pagesFetched ?? null} />
      <dl aria-label="Live scan counters" className="mt-5 grid grid-cols-2 divide-x divide-y divide-default material-base sm:grid-cols-4 sm:divide-y-0">
          <div className="px-4 py-3">
            <dt className="text-label-14 text-secondary">Pages found</dt>
            <dd className="mt-1 font-mono text-label-18-strong tabular-nums text-heading">{attempt ? metricValue(attempt.pagesDiscovered) : '—'}</dd>
          </div>
          <div className="px-4 py-3">
            <dt className="text-label-14 text-secondary">Pages checked</dt>
            <dd className="mt-1 font-mono text-label-18-strong tabular-nums text-heading">{attempt ? metricValue(attempt.pagesFetched) : '—'}</dd>
          </div>
          <div className="px-4 py-3">
            <dt className="text-label-14 text-secondary">Links found</dt>
            <dd className="mt-1 font-mono text-label-18-strong tabular-nums text-heading">{attempt ? metricValue(attempt.edgesDiscovered) : '—'}</dd>
          </div>
          <div className="px-4 py-3">
            <dt className="text-label-14 text-secondary">Pages failed</dt>
            <dd className="mt-1 font-mono text-label-18-strong tabular-nums text-heading">{attempt ? metricValue(attempt.pagesErrored) : '—'}</dd>
          </div>
        </dl>
      {progressError && (
        <div className="mt-4 flex flex-wrap items-center gap-3 text-label-14 text-secondary">
          <span>{arrangingMap
            ? 'Map publishing status could not load.'
            : 'Live scan counts could not load. The scan is still running.'}</span>
          {onRetryProgress && <Button type="button" variant="secondary" size="sm" onClick={onRetryProgress}>Retry progress</Button>}
        </div>
      )}
    </section>
  )
}

function TerminalScanRecoveryState({
  phase,
  progress,
  onRunAgain,
}: {
  phase: 'failed' | 'cancelled'
  progress?: SiteAuditProgressSnapshot | null
  onRunAgain?: () => void
}) {
  const cancelled = phase === 'cancelled'
  const error = progress?.error ?? progress?.attempt?.error

  return (
    <section
      aria-label="Audit scan recovery"
      role="alert"
      className="rounded-md border border-negative bg-negative-soft px-5 py-6"
    >
      <h2 className="text-heading-16 text-negative">{cancelled ? 'Scan cancelled' : 'Scan failed'}</h2>
      <p className="mt-1 text-copy-14 text-secondary">
        {cancelled
          ? 'The scan was cancelled before Canonry could publish a site map.'
          : 'The scan did not complete, so there is no site map for this run.'}
      </p>
      {error && <p className="mt-3 text-copy-14 text-negative">{error}</p>}
      {onRunAgain && <Button type="button" className="mt-4" onClick={onRunAgain}>Run scan again</Button>}
    </section>
  )
}

export function SiteHealthSection({
  projectName,
  projectId,
  initialRunId,
  onReleaseInitialRun,
}: {
  projectName: string
  projectId: string
  /** The durable route handoff wins over a derived latest scan after reload. */
  initialRunId?: string
  /** Clears the durable route handoff before a replacement scan. */
  onReleaseInitialRun?: () => void
}) {
  const scanHistoryId = useId()
  const [view, setView] = useState<SiteHealthView>('map')
  const tabRefs = useRef<Array<HTMLButtonElement | null>>([])
  // Scan settings is a native <details> used as a popover; like the scope
  // picker, it closes on an outside pointerdown or Escape and when a scan starts.
  const scanSettings = useRef<HTMLDetailsElement>(null)
  useEffect(() => {
    const closeOutside = (event: PointerEvent) => {
      if (scanSettings.current?.open && event.target instanceof Node && !scanSettings.current.contains(event.target)) scanSettings.current.open = false
    }
    document.addEventListener('pointerdown', closeOutside)
    return () => document.removeEventListener('pointerdown', closeOutside)
  }, [])
  const [selectedRunId, setSelectedRunId] = useState<string | null>(() => initialRunId ?? null)
  // A scan this section started stops being followed once the operator picks a scan.
  const [releasedRescanRunId, setReleasedRescanRunId] = useState<string | null>(null)
  const previousInitialRunId = useRef(initialRunId)
  const [selectedNodeKey, setSelectedNodeKey] = useState<string | null>(null)
  const [checkDeadLinks, setCheckDeadLinks] = useState(false)
  const [includeGeo, setIncludeGeo] = useState(false)
  const [includeAgentSkills, setIncludeAgentSkills] = useState(true)
  // `null` on both = send nothing: the server applies the project's saved
  // page budget (else the full site) and the crawler's default depth.
  const [pageBudget, setPageBudget] = useState<number | null>(null)
  const [crawlDepth, setCrawlDepth] = useState<number | null>(null)
  const [inventoryFilter, setInventoryFilter] = useState<InventoryFilterId>('all')
  // 'all', or one site of the selected scan; a site that scan did not cover reads as 'all'.
  const [siteFilter, setSiteFilter] = useState('all')
  // The owned domains the next scan covers. Null until the operator ticks
  // one, which means the project's saved choice.
  const [nextScanSiteTicks, setNextScanSiteTicks] = useState<string[] | null>(null)
  /**
   * The map opens on content links only. Nav and footer links repeat on every
   * page, so drawing them buries the structure this view exists to show. The
   * server sends both kinds tagged, so this only changes what is DRAWN: node
   * positions were computed without template links and never move.
  */
  const [showTemplateLinks, setShowTemplateLinks] = useState(false)
  const embedded = isEmbed()
  const { isAdmin } = useAccount()
  const publicDemo = isPublicDemo()
  const managedScanForViewer = (publicDemo || isDashboardManagedRunKind(RunKinds['site-audit'])) && !isAdmin
  const runMutation = useTriggerSiteAudit()
  // Scan settings names the project's saved page budget. This is the project
  // page's own cached project read (same key and freshness), so it adds no
  // request there, and it stays off wherever Scan settings is not shown.
  const scanSettingsShown = !embedded && !managedScanForViewer
  const projectQuery = useQuery({
    ...getApiV1ProjectsByNameOptions({ client: heyClient, path: { name: projectName } }),
    enabled: scanSettingsShown,
    staleTime: STATIC_VISIBILITY_STALE_MS,
  })

  useEffect(() => {
    const previous = previousInitialRunId.current
    if (previous === initialRunId) return
    previousInitialRunId.current = initialRunId
    setSelectedRunId((current) => initialRunId ?? (current === previous ? null : current))
    setSelectedNodeKey(null)
  }, [initialRunId])

  // The Agent Readiness audit scan history, not the generic run list: it already excludes
  // probes and says which scans kept a crawl.
  const auditRunsQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessRunsOptions({
      client: heyClient,
      path: { name: projectName },
      query: { limit: SCAN_HISTORY_LIMIT },
    }),
    refetchOnWindowFocus: 'always',
    refetchInterval: (query) => query.state.data?.scans.some(
      (scan) => scan.status === 'queued' || scan.status === 'running',
    ) ? 3_000 : 15_000,
  })
  const auditScans = auditRunsQuery.data?.scans ?? []
  const activeAudit = auditScans.find((scan) => scan.status === 'queued' || scan.status === 'running')
  const latestTerminalAudit = auditScans
    .find((scan) => scan.status === 'completed' || scan.status === 'partial')
  // The default is the API's preferred scan, which can be an earlier, fuller
  // scan from the same day. A scan started here is the result the operator is
  // waiting for, so once it finishes it is shown until they pick another scan.
  const rescanRunId = runMutation.data?.runId ?? null
  const finishedRescanRunId = rescanRunId && rescanRunId !== releasedRescanRunId && auditScans.some(
    (scan) => scan.runId === rescanRunId && (scan.status === 'completed' || scan.status === 'partial'),
  ) ? rescanRunId : null
  const requestedRunId = selectedRunId
    ?? finishedRescanRunId
    ?? auditRunsQuery.data?.preferredRunId
    ?? latestTerminalAudit?.runId
    ?? activeAudit?.runId
    ?? null
  // Scan history is eventually consistent. Keep its active state only until
  // the exact progress read for this selected run can say otherwise.
  const activeRequestedRun = activeAudit?.runId === requestedRunId ? activeAudit : null
  const requestedAuditRun = requestedRunId
    ? auditScans.find((scan) => scan.runId === requestedRunId)
    : undefined
  const isInitialRunSelection = Boolean(initialRunId && requestedRunId === initialRunId)
  const exactProgressRunId = activeRequestedRun?.runId
    ?? (isInitialRunSelection ? requestedRunId : null)

  const activeProgressQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessRunsByRunIdProgressOptions({
      client: heyClient,
      path: { name: projectName, runId: exactProgressRunId ?? '' },
    }),
    enabled: Boolean(exactProgressRunId),
    refetchInterval: (query) => shouldPollSiteAuditProgress(query.state.data?.phase) ? 3_000 : false,
    // react-query suspends interval refetches while the window is unfocused or
    // occluded. A scan runs for minutes, so the operator often looks away;
    // without this the counters freeze on whatever they read at mount.
    refetchIntervalInBackground: true,
  })
  useEffect(() => {
    if (!isInitialRunSelection || !activeProgressQuery.isError) return
    if (queryErrorCode(activeProgressQuery.error) !== 'NOT_FOUND') return
    setSelectedRunId(null)
    setSelectedNodeKey(null)
    onReleaseInitialRun?.()
  }, [activeProgressQuery.error, activeProgressQuery.isError, isInitialRunSelection, onReleaseInitialRun])
  const exactProgressActive = isActiveSiteAuditPhase(activeProgressQuery.data?.phase)
  const exactProgressTerminal = isTerminalSiteAuditPhase(activeProgressQuery.data?.phase)
  // A terminal exact run is authoritative over stale `running` scan history.
  // Without this, the active-history guard can withhold a persisted crawl and
  // leave the view on provisional progress after the scan has finished.
  const activeRunWithoutPublishedMap = exactProgressTerminal ? null : activeRequestedRun
  const exactProgressPending = Boolean(exactProgressRunId && activeProgressQuery.isPending)
  const exactProgressNeedsAuthority = Boolean(isInitialRunSelection && !requestedAuditRun)
  const exactProgressUnavailable = Boolean(exactProgressNeedsAuthority && activeProgressQuery.isError)
  const deferTerminalEvidence = Boolean(
    activeRunWithoutPublishedMap
    || exactProgressActive
    || exactProgressPending
    || exactProgressUnavailable,
  )
  const recoveryPhase = activeProgressQuery.data?.phase === 'failed' || activeProgressQuery.data?.phase === 'cancelled'
    ? activeProgressQuery.data.phase
    : isInitialRunSelection
      && (requestedAuditRun?.status === 'failed' || requestedAuditRun?.status === 'cancelled')
      ? requestedAuditRun.status
      : null

  const crawlQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessCrawlOptions({
      client: heyClient,
      path: { name: projectName },
      ...(requestedRunId ? { query: { runId: requestedRunId } } : {}),
    }),
    // The crawl endpoint deliberately exposes terminal persisted crawls only.
    // Exact progress is the authoritative read while this run is still active.
    enabled: !deferTerminalEvidence,
  })
  const crawl = crawlQuery.data
  const resolvedRunId = requestedRunId ?? crawl?.runId ?? null
  const detailsEnabled = Boolean(resolvedRunId && crawl?.hasCrawlData && crawl.detailsAvailable)
  const scopedRunQuery = resolvedRunId ? { runId: resolvedRunId } : {}

  const graphQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessGraphOptions({
      client: heyClient,
      path: { name: projectName },
      query: { ...scopedRunQuery, maxNodes: GRAPH_NODE_LIMIT, maxEdges: GRAPH_EDGE_LIMIT },
    }),
    // Layout publication is terminal. Fetch this large payload once after the
    // exact progress route leaves `arranging-map`, never while it is pending.
    enabled: detailsEnabled && !deferTerminalEvidence,
  })
  // The chip narrows on the server, so `total` and the cursor stay truthful
  // instead of describing an unfiltered list.
  const inventoryHealthState = INVENTORY_FILTERS
    .find((option) => option.id === inventoryFilter)?.healthState
  // The sites the selected scan covered, main domain first. One site, or a
  // scan made before scans recorded their sites, shows no site controls.
  const crawlSites: SiteScanSite[] = crawl?.sites ?? []
  const crawlSiteHosts = crawlSites.map((site) => site.host)
  const pageSiteFilter = crawlSites.length > 1 && crawlSiteHosts.includes(siteFilter) ? siteFilter : 'all'
  const pagesInput = {
    client: heyClient,
    path: { name: projectName },
    query: {
      ...scopedRunQuery,
      ...(inventoryHealthState ? { healthState: inventoryHealthState } : {}),
      // Narrowed on the server too, so the total and the cursor describe one site.
      ...(pageSiteFilter !== 'all' ? { site: pageSiteFilter } : {}),
      // A URL version is a copy of a page the audit did not score; the list
      // holds pages and the skipped copies are one count beside it.
      urlVersions: 'exclude' as const,
      limit: INVENTORY_LIMIT,
      sort: 'path' as const,
    },
  }
  const pagesQuery = useInfiniteQuery({
    ...getApiV1ProjectsByNameAgentReadinessCrawlPagesInfiniteOptions(pagesInput),
    enabled: detailsEnabled,
    initialPageParam: pagesInput,
    getNextPageParam: (lastPage) => lastPage.nextCursor
      ? {
          path: pagesInput.path,
          query: { ...pagesInput.query, cursor: lastPage.nextCursor },
        }
      : undefined,
  })
  const deadLinkDetailsEnabled = detailsEnabled
    && (crawl?.deadLinks.state === 'complete' || crawl?.deadLinks.state === 'partial')
  const deadLinksQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessDeadLinksOptions({
      client: heyClient,
      path: { name: projectName },
      query: { ...scopedRunQuery, limit: DEAD_LINK_LIMIT },
    }),
    enabled: deadLinkDetailsEnabled,
  })

  const graphPages = graphQuery.data?.nodes ?? []
  const graphEdges = graphQuery.data?.edges ?? []
  // Counted from the edges actually drawn, not from a site-wide total, so the
  // legend describes THIS map rather than a number the reader cannot see.
  const templateEdgeCount = useMemo(
    () => graphEdges.filter((edge) => edge.isTemplate).length,
    [graphEdges],
  )
  const contentEdgeCount = graphEdges.length - templateEdgeCount
  const templateDetection = graphQuery.data?.templateDetection ?? null
  // Which rule ran is a separate question from whether one ran at all, and only
  // the second one decides the control. Asking the contract keeps a new
  // detection value a compile error instead of a silently disabled toggle.
  const templateFilterUnavailable = templateDetection === null || !isTemplateDetectionApplied(templateDetection)
  // When detection did not run, the per-link flag proves nothing, so nothing
  // is hidden. Hiding on an untrustworthy flag would quietly drop real links
  // from the map and the copy would be a lie.
  const visibleGraphEdges = useMemo(
    () => showTemplateLinks || templateFilterUnavailable
      ? graphEdges
      : graphEdges.filter((edge) => !edge.isTemplate),
    [graphEdges, showTemplateLinks, templateFilterUnavailable],
  )
  // A scan published before template detection still has classified links (the
  // migration backfills them) but its stored positions were computed with the
  // nav mesh included. Say so rather than implying the map already reflects
  // content structure.
  const staleTemplateLayout = graphQuery.data?.layout.state === 'ready'
    && !graphQuery.data.layout.templateLinksExcluded
  const inventoryPages = useMemo(
    () => pagesQuery.data?.pages.flatMap((page) => page.pages) ?? [],
    [pagesQuery.data],
  )
  const inventoryTotal = pagesQuery.data?.pages[0]?.total ?? inventoryPages.length
  // Scan settings offers each owned domain as a tick, the saved ones ticked.
  const nextScanSiteChoices = scanSiteChoices(projectQuery.data)
  // Filtered by this project's choices, so a tick never outlives its owned domain.
  const nextScanSites = (nextScanSiteTicks ?? projectQuery.data?.siteAuditDomains ?? [])
    .filter((site) => nextScanSiteChoices.owned.includes(site))
  const loadedInventoryPage = useMemo(
    () => inventoryPages.find((page) => page.nodeKey === selectedNodeKey) ?? null,
    [inventoryPages, selectedNodeKey],
  )
  // The map holds up to 20,000 nodes while the inventory loads 200 at a time,
  // so a page selected on the map usually is not in the loaded window. Read
  // that ONE row by key rather than degrading: paging to find it would be the
  // scan this surface is built to avoid, and giving up loses its reasons.
  const selectedPageNeedsRead = Boolean(detailsEnabled && selectedNodeKey && !loadedInventoryPage)
  const selectedPageQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessCrawlPagesOptions({
      client: heyClient,
      path: { name: projectName },
      query: {
        ...scopedRunQuery,
        ...(inventoryHealthState ? { healthState: inventoryHealthState } : {}),
        nodeKey: selectedNodeKey ?? undefined,
        limit: 1,
      },
    }),
    enabled: selectedPageNeedsRead,
  })
  const inventoryPage = loadedInventoryPage ?? selectedPageQuery.data?.pages[0] ?? null

  /**
   * Drop the selection only when the SERVER has confirmed this page is not in
   * the filtered set. Absence from the loaded window proves nothing, and a
   * scan that cannot be filtered at all must not deselect anything either.
   */
  const selectionFilteredOut = Boolean(
    selectedNodeKey
    && inventoryHealthState
    && selectedPageNeedsRead
    && selectedPageQuery.isSuccess
    && selectedPageQuery.data.healthStateFilter === 'applied'
    && selectedPageQuery.data.pages.length === 0,
  )
  useEffect(() => {
    if (selectionFilteredOut) setSelectedNodeKey(null)
  }, [selectionFilteredOut])
  const selectedPage = useMemo(
    () => selectionFilteredOut
      ? null
      : graphPages.find((page) => page.nodeKey === selectedNodeKey) ?? inventoryPage,
    [graphPages, inventoryPage, selectedNodeKey, selectionFilteredOut],
  )
  const effectiveSelectedNodeKey = selectedPage?.nodeKey ?? null
  usePublishAeroView(projectName, { view: 'site-health', page: { ...(resolvedRunId ? { runId: resolvedRunId } : {}), ...(effectiveSelectedNodeKey ? { nodeKey: effectiveSelectedNodeKey } : {}) } })
  /**
   * A page whose own row could not be read is NOT a page with nothing to say.
   * Without this the inspector renders a hidden page with no reasons and no
   * error, which is indistinguishable from a page that genuinely has none.
   */
  const reasonsState: PageReasonsState = !selectedPage
    ? 'idle'
    : inventoryPage
      ? 'ready'
      : selectedPageQuery.isError
        ? 'error'
        : selectedPageNeedsRead && selectedPageQuery.isLoading
          ? 'loading'
          : 'ready'

  const neighborsQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessInternalLinksNeighborsOptions({
      client: heyClient,
      path: { name: projectName },
      query: {
        ...scopedRunQuery,
        nodeKey: effectiveSelectedNodeKey ?? undefined,
        limit: NEIGHBOR_LIMIT,
      },
    }),
    enabled: detailsEnabled && Boolean(effectiveSelectedNodeKey),
  })
  const pageAuditQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessCrawlPagesAuditOptions({
      client: heyClient,
      path: { name: projectName },
      query: {
        ...scopedRunQuery,
        nodeKey: effectiveSelectedNodeKey ?? undefined,
      },
    }),
    enabled: detailsEnabled && Boolean(effectiveSelectedNodeKey),
  })

  // The server already orders scans newest first, so the dropdown does not
  // keep a second ordering of the same list.
  const selectableScans = useMemo(() => {
    const terminalScans = auditScans
      .filter((scan) => scan.status === 'completed' || scan.status === 'partial')
    const selectedScan = selectedRunId
      ? auditScans.find((scan) => scan.runId === selectedRunId)
      : undefined
    return selectedScan && !terminalScans.some((scan) => scan.runId === selectedScan.runId)
      ? [selectedScan, ...terminalScans]
      : terminalScans
  }, [auditScans, selectedRunId])
  // Scan history appears once there is a scan to choose: a finished scan, or
  // one this view was opened on. Before that it would offer "Latest scan" alone.
  const hasScanHistory = selectableScans.length > 0
  const newestRunStatus = auditScans[0]?.status ?? null
  const selectedRun = resolvedRunId ? requestedAuditRun : undefined
  const status = activeRunWithoutPublishedMap
    ? activeRunWithoutPublishedMap.status
    : exactProgressActive
      ? activeProgressQuery.data?.status ?? null
    : recoveryPhase ?? crawl?.runStatus ?? selectedRun?.status ?? null
  const statusLabel = status === 'running'
    ? 'Scan running'
    : status === 'queued'
      ? 'Scan queued'
      : status === 'partial'
        ? 'Partial scan'
        : status === 'completed'
          ? 'Complete'
          : status === 'failed'
            ? 'Scan failed'
            : status === 'cancelled'
              ? 'Scan cancelled'
            : 'No scan'
  // The header leads with the shown scan's score on every view. This is the
  // Score view's own read (same key, pinned to the same scan), so the two
  // share one cache entry and can never disagree. Only a finished scan has a
  // score to read.
  const headerScoreReadable = Boolean(resolvedRunId)
    && (status === RunStatuses.completed || status === RunStatuses.partial)
  const headerScoreQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessOptions({
      client: heyClient,
      path: { name: projectName },
      ...(resolvedRunId ? { query: { runId: resolvedRunId } } : {}),
    }),
    enabled: headerScoreReadable,
  })
  const headerScore = headerScoreReadable ? headerScoreLabel(headerScoreQuery.data) : null
  const deadLinks = deadLinksQuery.data
  const deadLinkCounts = deadLinks && 'found' in deadLinks
    ? deadLinks
    : crawl?.deadLinks && 'found' in crawl.deadLinks ? crawl.deadLinks : undefined
  const deadLinkStatus = deadLinkLabel(
    deadLinks?.state ?? crawl?.deadLinks?.state ?? 'unavailable',
    deadLinkCounts?.found,
    deadLinkCounts?.unverified,
  )
  const movedSite = movedSiteHosts(crawl?.requestedRootUrl ?? null, crawl?.rootUrl ?? null)
  const rootHost = siteHostFromUrl(crawl?.rootUrl ?? null)
  // The root is identified by the server. Sourcing it from the graph alone
  // made the row disappear on exactly the scans whose layout is missing, so
  // the inventory read (which every crawl has) is the fallback.
  const rootNodeKey = graphQuery.data?.rootNodeKey ?? null
  const rootPageQuery = useQuery({
    ...getApiV1ProjectsByNameAgentReadinessCrawlPagesOptions({
      client: heyClient,
      path: { name: projectName },
      query: { ...scopedRunQuery, nodeKey: rootNodeKey ?? undefined, limit: 1 },
    }),
    enabled: Boolean(detailsEnabled && rootNodeKey),
  })
  const rootGraphPage = useMemo(
    () => graphPages.find((page) => page.nodeKey === rootNodeKey)
      ?? rootPageQuery.data?.pages[0]
      ?? null,
    [graphPages, rootNodeKey, rootPageQuery.data],
  )
  // A crawl that is in flight for some OTHER run than the selected one still
  // blocks a new one. The route consolidates only onto an in-flight request
  // with identical options and rejects everything else with a conflict, and a
  // scan started elsewhere (the CLI, another tab) can carry different options.
  // Without this the button reads "Scan running" (it already keys that copy on
  // `activeAudit`) while staying enabled, and pressing it produces a bare error
  // toast.
  const conflictingActiveAudit = activeAudit && activeAudit.runId !== requestedRunId ? activeAudit : null
  const scanBusy = runMutation.isPending || Boolean(activeRunWithoutPublishedMap) || Boolean(conflictingActiveAudit) || exactProgressActive || exactProgressPending
  const transientView = view === 'technical' ? 'map' : view
  const selectRun = (runId: string) => {
    setSelectedRunId(runId || null)
    setReleasedRescanRunId(rescanRunId)
    setSelectedNodeKey(null)
  }
  const startScan = () => {
    // Gate the dispatcher too: recovery actions also use plain Buttons.
    // This is presentation only; the server independently refuses viewer writes.
    if ((publicDemo || isDashboardManagedRunKind(RunKinds['site-audit'])) && !isAdmin) return
    // The open settings panel would otherwise cover the scan's progress.
    if (scanSettings.current) scanSettings.current.open = false
    // Release any pinned scan before dispatching its replacement.
    // Otherwise the durable URL handoff keeps the old run selected while the
    // newly queued scan progresses invisibly in the background.
    setSelectedRunId(null)
    setSelectedNodeKey(null)
    if (initialRunId) onReleaseInitialRun?.()
    runMutation.mutate({
      projectName,
      projectId,
      projectLabel: projectQuery.data?.displayName || projectName,
      body: {
        checkDeadLinks,
        ...(includeGeo ? { includeGeo: true } : {}),
        ...(!includeAgentSkills ? { includeAgentSkills: false } : {}),
        // The budget is whatever Scan settings says. An unset budget is
        // omitted so the server applies the project's saved budget, else the
        // full site.
        ...(pageBudget === null ? {} : { maxPages: pageBudget }),
        ...(crawlDepth === null ? {} : { maxDepth: crawlDepth }),
        // Sent only when there are owned domains to choose from; otherwise
        // the server applies the project's saved choice, the main domain alone.
        ...(nextScanSiteChoices.owned.length > 0 ? { sites: nextScanSites } : {}),
      },
    })
    // Ticks apply to one scan; the next one starts from the saved choice again.
    setNextScanSiteTicks(null)
  }
  const selectSection = (path: string, site?: string) => {
    // Folder paths carry no host, so in a scan of more than one site the
    // match is also on the folder's site.
    const onSite = (url: string) => !site || siteOfUrl(url) === site
    const matchingPage = graphPages.find((page) => (page.path === path || page.path.startsWith(`${path}/`)) && onSite(page.url))
    if (matchingPage) setSelectedNodeKey(matchingPage.nodeKey)
  }

  const selectView = (nextView: SiteHealthView) => setView(nextView)
  const handleViewKeyDown = (event: KeyboardEvent<HTMLButtonElement>, currentIndex: number) => {
    let nextIndex: number | null = null
    if (event.key === 'ArrowRight' || event.key === 'ArrowDown') {
      nextIndex = (currentIndex + 1) % SITE_HEALTH_VIEWS.length
    } else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') {
      nextIndex = (currentIndex - 1 + SITE_HEALTH_VIEWS.length) % SITE_HEALTH_VIEWS.length
    } else if (event.key === 'Home') {
      nextIndex = 0
    } else if (event.key === 'End') {
      nextIndex = SITE_HEALTH_VIEWS.length - 1
    }
    if (nextIndex == null) return
    event.preventDefault()
    selectView(SITE_HEALTH_VIEWS[nextIndex]!.id)
    tabRefs.current[nextIndex]?.focus()
  }

  return (
    <div className="space-y-5">
      <header className="flex flex-col justify-between gap-4 lg:flex-row lg:items-start">
        <div>
          {/* The selected tab already says "Agent Readiness", so this row is
              named for what it reports: the shown scan's state, score and date. */}
          <div className="flex flex-wrap items-center gap-3">
            <h2 className="text-heading-20 text-heading">Audit scan</h2>
            <InfoTooltip text={SITE_HEALTH_VIEW_DESCRIPTIONS[view]} />
            <ToneBadge tone={scanTone(status)}>{statusLabel}</ToneBadge>
            {headerScore && (
              <span className="text-label-14-strong tabular-nums text-heading">{headerScore}</span>
            )}
            {selectedRun && (
              <span className="text-label-12 text-muted">{formatScanDate(selectedRun.finishedAt ?? selectedRun.startedAt)}</span>
            )}
          </div>
        </div>

        <div className="flex flex-wrap items-start gap-2">
          {/* A picker with nothing to pick until a scan has finished. */}
          {hasScanHistory && (
            <div className="grid gap-1 text-label-12 text-muted">
              <div className="flex items-center gap-1.5">
                <label htmlFor={scanHistoryId}>Scan history</label>
                <InfoTooltip text={DEFAULT_SCAN_HELP} placement="bottom" />
              </div>
              <select
                id={scanHistoryId}
                aria-label="View an Agent Readiness audit scan"
                value={selectedRunId ?? finishedRescanRunId ?? ''}
                onChange={(event) => selectRun(event.target.value)}
                className="h-9 min-w-48 rounded-md border border-base bg-bg px-3 text-label-14 text-primary outline-none focus:border-strong focus:ring-2 focus:ring-mono-600"
              >
                <option value="">{DEFAULT_SCAN_OPTION_LABEL}</option>
                {selectableScans.map((scan) => (
                  <option key={scan.runId} value={scan.runId}>{scanOptionLabel(scan)}</option>
                ))}
              </select>
            </div>
          )}

          {!embedded && !managedScanForViewer && (
            // Without the picker there is no label row above it to line up with.
            <div className={cn('flex items-end gap-2', hasScanHistory && 'pt-5')}>
              <details ref={scanSettings} className="group relative" onKeyDown={(event) => {
                if (event.key !== 'Escape' || !event.currentTarget.open) return
                event.preventDefault()
                event.currentTarget.open = false
                event.currentTarget.querySelector('summary')?.focus()
              }}>
                <summary className="flex h-9 cursor-pointer list-none items-center gap-2 rounded-md border border-base bg-bg px-3 text-label-14-strong text-heading outline-none hover:bg-bg-elevated focus-visible:ring-2 focus-visible:ring-mono-400 [&::-webkit-details-marker]:hidden">
                  <Settings2 className="size-4" aria-hidden="true" />
                  Scan settings
                  <ChevronDown className="size-3.5 transition-transform group-open:rotate-180" aria-hidden="true" />
                </summary>
                <div className="absolute left-0 z-20 mt-2 w-72 material-menu p-4 sm:left-auto sm:right-0">
                  {/* The sites this scan covers: the main domain always, each owned domain by tick. */}
                  {nextScanSiteChoices.main && nextScanSiteChoices.owned.length > 0 && (
                    <fieldset className="mb-4 grid gap-2 border-b border-default pb-4">
                      <legend className="mb-2 flex items-center gap-1.5 text-label-14-strong text-heading">
                        Sites
                        <InfoTooltip text="The main domain is always scanned. Ticks here apply to this scan; the saved choice is in Settings." placement="bottom" />
                      </legend>
                      <label className="flex items-center justify-between gap-3" title="The main domain is always scanned">
                        <span className="min-w-0 truncate text-label-14-mono text-primary">{nextScanSiteChoices.main}</span>
                        <input type="checkbox" checked disabled aria-label={`Scan ${nextScanSiteChoices.main}`} className="size-4 shrink-0 rounded border-base accent-mono-200" />
                      </label>
                      {nextScanSiteChoices.owned.map((site) => (
                        <label key={site} className="flex items-center justify-between gap-3">
                          <span className="min-w-0 truncate text-label-14-mono text-primary">{site}</span>
                          <input
                            type="checkbox"
                            aria-label={`Scan ${site}`}
                            checked={nextScanSites.includes(site)}
                            disabled={scanBusy}
                            onChange={(event) => setNextScanSiteTicks(toggleSite(nextScanSiteChoices.owned, nextScanSites, site, event.target.checked))}
                            className="size-4 shrink-0 rounded border-base accent-mono-200 focus-visible:ring-2 focus-visible:ring-mono-400"
                          />
                        </label>
                      ))}
                    </fieldset>
                  )}
                  <div className="flex items-center justify-between gap-3 text-label-14 text-heading">
                    <span className="flex items-center gap-1.5 text-label-14-strong">
                      Check dead links
                      <InfoTooltip text="Adds dead-link analysis to this scan. Slower on a large site." />
                    </span>
                    <input
                      type="checkbox"
                      aria-label="Check dead links"
                      checked={checkDeadLinks}
                      disabled={scanBusy}
                      onChange={(event) => setCheckDeadLinks(event.target.checked)}
                      className="size-4 rounded border-base accent-mono-200 focus:ring-2 focus:ring-mono-400"
                    />
                  </div>
                  <div className="mt-3 flex items-center justify-between gap-3 text-label-14 text-heading">
                    <span className="flex items-center gap-1.5 text-label-14-strong">
                      Geographic signals
                      <InfoTooltip text={SITE_HEALTH_CHECK_HELP['geographic-signals']!} placement="bottom" />
                    </span>
                    <input
                      type="checkbox"
                      aria-label="Geographic signals"
                      checked={includeGeo}
                      disabled={scanBusy}
                      onChange={(event) => setIncludeGeo(event.target.checked)}
                      className="size-4 rounded border-base accent-mono-200 focus:ring-2 focus:ring-mono-400"
                    />
                  </div>
                  <div className="mt-3 flex items-center justify-between gap-3 text-label-14 text-heading">
                    <span className="flex items-center gap-1.5 text-label-14-strong">
                      Agent integrations
                      <InfoTooltip text={SITE_HEALTH_CHECK_HELP['agent-skill-exposure']!} placement="bottom" />
                    </span>
                    <input
                      type="checkbox"
                      aria-label="Agent integrations"
                      checked={includeAgentSkills}
                      disabled={scanBusy}
                      onChange={(event) => setIncludeAgentSkills(event.target.checked)}
                      className="size-4 rounded border-base accent-mono-200 focus:ring-2 focus:ring-mono-400"
                    />
                  </div>
                  {/* The tooltip sits OUTSIDE the label: inside it, its own
                      accessible name folds into the control's, and both answer
                      to the same name. */}
                  <div className="mt-4">
                    <span className="flex items-center gap-1.5 text-label-14-strong text-heading">
                      Page budget
                      <InfoTooltip text={PAGE_BUDGET_HELP} placement="bottom" />
                    </span>
                    <select
                      aria-label="Page budget"
                      value={pageBudget === null ? '' : String(pageBudget)}
                      disabled={scanBusy}
                      onChange={(event) => setPageBudget(event.target.value === '' ? null : Number(event.target.value))}
                      className="mt-1 h-9 w-full rounded-md border border-base bg-bg px-2 text-label-14 text-primary outline-none focus:border-strong focus:ring-2 focus:ring-mono-600"
                    >
                      <option value="">{projectDefaultPageBudgetLabel(projectQuery.data)}</option>
                      {[...(oneOffFullSiteChoice(projectQuery.data) ? [oneOffFullSiteChoice(projectQuery.data)!] : []), ...PAGE_BUDGET_CHOICES].map((choice) => (
                        <option key={choice.value} value={String(choice.value)}>{choice.label}</option>
                      ))}
                    </select>
                  </div>
                  <div className="mt-3">
                    <span className="flex items-center gap-1.5 text-label-14-strong text-heading">
                      Crawl depth
                      <InfoTooltip text="How far from the home page to follow links. A smaller scan is what fixes a scan that ran out of time." />
                    </span>
                    <select
                      aria-label="Crawl depth"
                      value={crawlDepth === null ? '' : String(crawlDepth)}
                      disabled={scanBusy}
                      onChange={(event) => setCrawlDepth(event.target.value === '' ? null : Number(event.target.value))}
                      className="mt-1 h-9 w-full rounded-md border border-base bg-bg px-2 text-label-14 text-primary outline-none focus:border-strong focus:ring-2 focus:ring-mono-600"
                    >
                      {CRAWL_DEPTH_CHOICES.map((choice) => (
                        <option key={String(choice.value)} value={choice.value === null ? '' : String(choice.value)}>{choice.label}</option>
                      ))}
                    </select>
                  </div>
                </div>
              </details>
              <WriteButton onClick={startScan} disabled={scanBusy}>
                {scanBusy ? <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" /> : <Play className="size-4" aria-hidden="true" />}
                {activeAudit?.status === 'running' ? 'Scan running' : activeAudit?.status === 'queued' ? 'Scan queued' : 'Run scan'}
              </WriteButton>
            </div>
          )}
        </div>
      </header>
      {managedScanForViewer && <ManagedSweepStatus projectName={projectName} kind={RunKinds['site-audit']} loadSchedule={!publicDemo} />}

      {/* The scan's counts stay put across Map, Pages and Score, so each view's
          own counts ("72 pages", "70 pages scored") read against the same four
          numbers. */}
      {crawl?.hasCrawlData && (
        <div className="grid grid-cols-2 divide-x divide-y divide-default material-base sm:grid-cols-4 sm:divide-y-0">
          <div className="px-4 py-3">
            <div className="flex items-center gap-1 text-label-12 text-muted">
              Pages found
              <InfoTooltip text={siteHealthMetricHelp('pagesFound', false)} />
            </div>
            <div className="mt-1 text-label-20-strong tabular-nums text-heading">{metricValue(crawl.counts.pagesDiscovered)}</div>
          </div>
          <div className="px-4 py-3">
            <div className="flex items-center gap-1 text-label-12 text-muted">
              Pages checked
              <InfoTooltip text={siteHealthMetricHelp('pagesChecked', false)} />
            </div>
            <div className="mt-1 text-label-20-strong tabular-nums text-heading">{metricValue(crawl.counts.pagesFetched)}</div>
          </div>
          <div className="px-4 py-3">
            <div className="text-label-12 text-muted" title={siteGraphStatusDescription('eligible')}>Indexable</div>
            <div className="mt-1 text-label-20-strong tabular-nums text-heading">{metricValue(crawl.counts.pagesEligible)}</div>
          </div>
          <div className="px-4 py-3">
            <div className="flex items-center gap-1 text-label-12 text-muted">
              Internal links
              <InfoTooltip text={siteHealthMetricHelp('internalLinks', false)} />
            </div>
            <div className="mt-1 text-label-20-strong tabular-nums text-heading">{metricValue(crawl.counts.internalLinks ?? null)}</div>
          </div>
        </div>
      )}

      <div className="flex flex-wrap items-center justify-between gap-3 border-b border-default">
        <div role="tablist" aria-label="Agent Readiness views" aria-orientation="horizontal" className="flex min-w-0 gap-5">
          {SITE_HEALTH_VIEWS.map((item, index) => (
            <button
              key={item.id}
              type="button"
              role="tab"
              id={`site-health-${item.id}-tab`}
              aria-selected={view === item.id}
              aria-controls={`site-health-${item.id}-panel`}
              tabIndex={view === item.id ? 0 : -1}
              ref={(element) => { tabRefs.current[index] = element }}
              onClick={() => selectView(item.id)}
              onKeyDown={(event) => handleViewKeyDown(event, index)}
              className={`min-h-11 border-b-2 px-0.5 text-button-14 outline-none transition-colors focus-visible:ring-2 focus-visible:ring-mono-400 disabled:cursor-not-allowed disabled:opacity-50 ${
                view === item.id
                  ? 'border-strong text-heading'
                  : 'border-transparent text-secondary hover:border-base hover:text-primary'
              }`}
            >
              {item.label}
            </button>
          ))}
        </div>
        {crawl?.hasCrawlData && view !== 'technical' && (
          <div className="flex items-center gap-2 pb-2">
            <span className="text-label-12 text-muted">Dead-link check</span>
            <ToneBadge tone={deadLinkStatus.tone}>{deadLinkStatus.label}</ToneBadge>
          </div>
        )}
      </div>

      {!selectedRunId && activeAudit && !activeRunWithoutPublishedMap && (
        <div className="rounded-md border border-caution bg-caution-soft px-4 py-3 text-label-14 text-caution" role="status">
          A newer scan is running. The latest published result remains available until it finishes.
        </div>
      )}
      {movedSite && (
        <div className="material-base px-4 py-3 text-label-14 text-secondary" role="status">
          <span className="text-label-14-strong text-heading">Site address changed during this scan.</span>{' '}
          The scan started at <span className="font-mono text-primary">{movedSite.requested}</span> and continued at{' '}
          <span className="font-mono text-primary">{movedSite.effective}</span>.{' '}
          The map and inventory use the new address.
        </div>
      )}
      {crawl?.hasCrawlData && !crawl.complete && (
        <div className="rounded-md border border-caution bg-caution-soft px-4 py-3 text-label-14 text-caution" role="status">
          {terminationCopy(crawl.termination)}{' '}
          {/* Naming the limit without naming the remedy leaves the operator
              looking at a result they have been told is incomplete with
              nothing to do about it. The budget lives in Scan settings. */}
          {!embedded && !managedScanForViewer ? terminationRemedy(crawl.termination) : null}
        </div>
      )}
      {!activeAudit && newestRunStatus === 'failed' && !selectedRunId && (
        <div className="flex gap-3 rounded-md border border-negative bg-negative-soft px-4 py-3 text-label-14 text-negative" role="alert">
          <AlertTriangle className="mt-0.5 size-4 shrink-0" aria-hidden="true" />
          <span>{crawl?.hasCrawlData
            ? 'The latest scan failed. The previous completed results remain available.'
            : 'The latest scan failed, and there are no earlier results to show.'}</span>
        </div>
      )}

      {recoveryPhase ? (
        <TransientSiteHealthPanel view={transientView}>
          <TerminalScanRecoveryState
            phase={recoveryPhase}
            progress={activeProgressQuery.data}
            onRunAgain={managedScanForViewer ? undefined : startScan}
          />
        </TransientSiteHealthPanel>
      ) : deferTerminalEvidence ? (
        <TransientSiteHealthPanel view={transientView}>
          <ActiveScanState
            status={activeProgressQuery.data?.phase ?? (activeRunWithoutPublishedMap?.status === 'running' ? 'running' : 'queued')}
            progress={activeProgressQuery.data}
            progressError={activeProgressQuery.isError}
            onRetryProgress={() => { void activeProgressQuery.refetch() }}
          />
        </TransientSiteHealthPanel>
      ) : view === 'technical' ? (
        <div
          id="site-health-technical-panel"
          role="tabpanel"
          aria-labelledby="site-health-technical-tab"
          className="min-w-0 space-y-6"
        >
          <TechnicalAeoSection
            projectName={projectName}
            projectId={projectId}
            runId={resolvedRunId}
            integrated
          />
        </div>
      ) : crawlQuery.isLoading ? (
        <TransientSiteHealthPanel view={view}>
          <div className="flex min-h-72 items-center justify-center gap-2 text-label-14 text-secondary" role="status">
            <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" />
            Loading Agent Readiness…
          </div>
        </TransientSiteHealthPanel>
      ) : crawlQuery.error ? (
        <TransientSiteHealthPanel view={view}>
          <section className="rounded-md border border-negative bg-negative-soft px-5 py-6" role="alert">
            <h2 className="text-heading-14 text-negative">Agent Readiness could not load</h2>
            <p className="mt-1 text-copy-14 text-negative">Try loading the crawl again.</p>
            <Button variant="secondary" size="sm" className="mt-4" onClick={() => void crawlQuery.refetch()}>Try again</Button>
          </section>
        </TransientSiteHealthPanel>
      ) : !crawl?.hasCrawlData ? (
        <TransientSiteHealthPanel view={view}>
          <section className="material-base px-5 py-8 text-center">
            <h2 className="text-heading-16 text-heading">Full-site map not available</h2>
            <p className="mx-auto mt-2 max-w-xl text-copy-14 text-secondary">
              {managedScanForViewer
                ? MANAGED_SCANS_COPY
                : crawl?.legacyAuditAvailable
                ? 'The existing score is preserved. Run a new scan to build the page and internal-link map.'
                : embedded
                  ? 'An Agent Readiness audit scan has not been run for this project.'
                  : 'Run a scan to discover pages, site sections, and internal links.'}
            </p>
            {crawl?.legacyAuditAvailable && (
              <div className="mt-4 flex flex-wrap justify-center gap-2">
                <Button variant="secondary" size="sm" onClick={() => setView('technical')}>View score</Button>
              </div>
            )}
          </section>
        </TransientSiteHealthPanel>
      ) : !crawl.detailsAvailable ? (
        <TransientSiteHealthPanel view={view}>
          <section className="rounded-md border border-caution bg-caution-soft px-5 py-6">
            <h2 className="text-heading-14 text-caution">Page details unavailable</h2>
            <p className="mt-1 text-copy-14 text-caution">
              Summary metrics are preserved for this scan, but its page graph cannot be opened.
            </p>
          </section>
        </TransientSiteHealthPanel>
      ) : view === 'inventory' ? (
        <div id="site-health-inventory-panel" role="tabpanel" aria-labelledby="site-health-inventory-tab" className="space-y-5">
          {pagesQuery.isLoading ? (
            <div className="flex min-h-56 items-center justify-center gap-2 text-label-14 text-secondary" role="status">
              <LoaderCircle className="size-4 motion-safe:animate-spin" aria-hidden="true" />
              Loading page inventory…
            </div>
          ) : pagesQuery.error ? (
            <p className="rounded-md border border-negative bg-negative-soft px-4 py-3 text-copy-14 text-negative" role="alert">
              The page inventory could not be loaded.
            </p>
          ) : (
            <InventoryTable
              pages={inventoryPages}
              total={inventoryTotal}
              skippedUrlVersions={urlVersionsFound(pagesQuery.data?.pages[0]?.inventorySummary)}
              rootHost={rootHost}
              filterUnavailable={pagesQuery.data?.pages[0]?.healthStateFilter === 'unavailable-legacy-scan'}
              selectedNodeKey={effectiveSelectedNodeKey}
              onSelect={setSelectedNodeKey}
              hasNextPage={Boolean(pagesQuery.hasNextPage)}
              isFetchingNextPage={pagesQuery.isFetchingNextPage}
              onLoadMore={() => void pagesQuery.fetchNextPage()}
              filter={inventoryFilter}
              onFilterChange={setInventoryFilter}
              sites={crawlSites}
              siteFilter={pageSiteFilter}
              onSiteFilterChange={setSiteFilter}
            />
          )}
          <PageInspector
            page={selectedPage}
            isLoading={neighborsQuery.isLoading && Boolean(selectedPage)}
            error={neighborsQuery.error}
            inbound={neighborsQuery.data?.inbound ?? []}
            outbound={neighborsQuery.data?.outbound ?? []}
            inboundTruncated={neighborsQuery.data?.inboundTruncated ?? false}
            outboundTruncated={neighborsQuery.data?.outboundTruncated ?? false}
            audit={pageAuditQuery.data}
            auditLoading={pageAuditQuery.isLoading && Boolean(selectedPage)}
            auditError={pageAuditQuery.error}
            onRetryAudit={() => { void pageAuditQuery.refetch() }}
            rootHost={rootHost}
            reasonSource={inventoryPage}
            reasonsState={reasonsState}
            onRetryReasons={() => { void selectedPageQuery.refetch() }}
            showTemplateLinks={showTemplateLinks || templateFilterUnavailable}
            siteHosts={crawlSiteHosts}
          />
        </div>
      ) : (
        <div id="site-health-map-panel" role="tabpanel" aria-labelledby="site-health-map-tab" className="space-y-5">
          <section aria-labelledby="site-map-heading">
            <div className="mb-3 flex flex-col justify-between gap-2 sm:flex-row sm:items-end">
              <div>
                <div className="flex items-center gap-1">
                  <h2 id="site-map-heading" className="text-heading-16 text-heading">Site map</h2>
                  <InfoTooltip text={SITE_MAP_HELP} />
                </div>
              </div>
              {/* What the map was given, against the whole scan. "Shown" is
                  reserved for the map's own count of the links it draws. */}
              {graphQuery.data?.sampled && (
                <span className="text-label-12 text-muted">
                  Map limited to {metricValue(graphQuery.data.nodes.length)} of {metricValue(graphQuery.data.totalNodes)} pages
                  {' and '}
                  {metricValue(graphQuery.data.edges.length)} of {metricValue(graphQuery.data.totalEdges)} links
                </span>
              )}
            </div>

            {graphQuery.data?.layout.state === 'ready' && graphEdges.length > 0 && (
              <div className="mb-3 flex flex-col gap-1.5 material-base px-4 py-3 sm:flex-row sm:items-center sm:justify-between">
                {/*
                  One line, the numbers only. The rule that produced them and
                  any staleness warning are always available on the tooltip, in
                  every state, because these counts are the output of a rule and
                  the rule can change between scans: a reader who cannot see
                  which rule ran cannot tell a real change on the site from a
                  change in how it was measured.
                */}
                <p className="flex items-center gap-1 text-copy-14 text-secondary" data-testid="site-map-link-counts">
                  {siteMapLinkCountsLabel({
                    filterUnavailable: templateFilterUnavailable,
                    showTemplateLinks,
                    contentEdgeCount,
                    templateEdgeCount,
                    totalEdgeCount: graphEdges.length,
                  })}
                  <InfoTooltip text={siteMapLinkRuleHelp(templateDetection, {
                    staleLayout: staleTemplateLayout && !templateFilterUnavailable,
                  })} />
                </p>
                <label
                  className={cn(
                    'flex shrink-0 items-center gap-2 text-label-14',
                    templateFilterUnavailable ? 'cursor-not-allowed text-muted' : 'cursor-pointer text-heading',
                  )}
                  title={templateFilterUnavailable
                    ? templateDetectionCopy(templateDetection)
                    : 'Menu and footer links are drawn on top. Pages do not move, because the map was laid out from the links in your page text.'}
                >
                  <input
                    type="checkbox"
                    checked={showTemplateLinks && !templateFilterUnavailable}
                    disabled={templateFilterUnavailable}
                    onChange={(event) => setShowTemplateLinks(event.target.checked)}
                    className="size-4 rounded border-base accent-mono-200 focus:ring-2 focus:ring-mono-400"
                  />
                  Show menu and footer links
                </label>
              </div>
            )}

            <div id="site-health-map-explorer" className="grid gap-4 lg:grid-cols-[minmax(14rem,18rem)_minmax(0,1fr)]">
              <aside className="material-base" aria-labelledby="site-sections-heading">
                <div className="border-b border-default px-4 py-3">
                  <h3 id="site-sections-heading" className="text-heading-14 text-heading">Site sections</h3>
                  <p className="mt-1 text-copy-13 text-muted">{crawlSites.length > 1 ? 'Sites and folders in this scan' : 'Folders in this scan'}</p>
                </div>
                <div className="max-h-[468px] overflow-auto">
                  {resolvedRunId && crawlSites.length > 1 ? (
                    <ul className="divide-y divide-default">
                      {crawlSites.map((site, index) => (
                        <SiteBranch key={site.host} site={site}>
                          <SiteSectionChildren
                            projectName={projectName}
                            runId={resolvedRunId}
                            site={site.host}
                            parentPath="/"
                            onSelect={selectSection}
                            rootPage={index === 0 ? rootGraphPage : null}
                            onSelectRootPage={setSelectedNodeKey}
                          />
                        </SiteBranch>
                      ))}
                    </ul>
                  ) : resolvedRunId && (
                    <SiteSectionChildren
                      projectName={projectName}
                      runId={resolvedRunId}
                      parentPath="/"
                      onSelect={selectSection}
                      rootPage={rootGraphPage}
                      onSelectRootPage={setSelectedNodeKey}
                    />
                  )}
                </div>
              </aside>

              <div className="min-w-0">
                {graphQuery.isLoading ? (
                  <GraphLoadingState />
                ) : graphQuery.error ? (
                  <div className="flex min-h-[420px] items-center justify-center rounded-md border border-negative bg-negative-soft px-6 text-center lg:min-h-[520px]" role="alert">
                    <div>
                      <p className="text-copy-14-strong text-negative">The interactive map could not be loaded.</p>
                      <p className="mt-1 text-copy-14 text-secondary">The page inventory and findings remain available.</p>
                      <Button variant="secondary" size="sm" className="mt-4" onClick={() => setView('inventory')}>Open page inventory</Button>
                    </div>
                  </div>
                ) : (
                  <SiteGraphSigma
                    nodes={graphQuery.data?.nodes ?? []}
                    // Every edge, always. Hiding is done by the edge reducer,
                    // so toggling template links never rebuilds the renderer.
                    edges={graphEdges}
                    showTemplateLinks={showTemplateLinks || templateFilterUnavailable}
                    layoutState={graphQuery.data?.layout.state ?? 'unavailable'}
                    layoutUnavailableReason={graphQuery.data?.layout.state === 'unavailable' ? graphQuery.data.layout.reason : null}
                    rootNodeKey={graphQuery.data?.rootNodeKey ?? null}
                    selectedNodeKey={effectiveSelectedNodeKey}
                    onSelectNode={(node) => setSelectedNodeKey(node.nodeKey)}
                    onOpenInventory={() => setView('inventory')}
                    ariaLabel={`Interactive site map showing ${countedLinks(graphQuery.data?.nodes.length ?? 0, 'page')} and ${countedLinks(visibleGraphEdges.length, 'internal link')}`}
                  />
                )}
              </div>
            </div>

          </section>

          <PageInspector
            page={selectedPage}
            isLoading={neighborsQuery.isLoading && Boolean(selectedPage)}
            error={neighborsQuery.error}
            inbound={neighborsQuery.data?.inbound ?? []}
            outbound={neighborsQuery.data?.outbound ?? []}
            inboundTruncated={neighborsQuery.data?.inboundTruncated ?? false}
            outboundTruncated={neighborsQuery.data?.outboundTruncated ?? false}
            audit={pageAuditQuery.data}
            auditLoading={pageAuditQuery.isLoading && Boolean(selectedPage)}
            auditError={pageAuditQuery.error}
            onRetryAudit={() => { void pageAuditQuery.refetch() }}
            rootHost={rootHost}
            reasonSource={inventoryPage}
            reasonsState={reasonsState}
            onRetryReasons={() => { void selectedPageQuery.refetch() }}
            showTemplateLinks={showTemplateLinks || templateFilterUnavailable}
            siteHosts={crawlSiteHosts}
          />
        </div>
      )}
    </div>
  )
}
