/* ── Handbook search: quick-search modal + full results page (issue #389) ── */

.pf-visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* Lock background scroll while the modal is open. */
html.pf-modal-open { overflow: hidden; }

/* Colours beyond web-base's --mg-* tokens, the same on every site: the
   handbook's accent (custom.css's --mg-ochre; web-base's is too light for
   text), the active option's tint and a key's shadow (#2659). */
#pf-overlay,
.pf-page {
  --pf-accent: #B63A1D;
  --pf-active-bg: rgba(230, 71, 37, 0.08);  /* 0.12 left the accent at 4.42:1 */
  --pf-kbd-shadow: rgba(0, 0, 0, 0.1);
}
html[data-theme="dark"] #pf-overlay,
html[data-theme="dark"] .pf-page {
  --pf-accent: #F06A4D;
  --pf-active-bg: rgba(230, 71, 37, 0.18);
  --pf-kbd-shadow: rgba(0, 0, 0, 0.2);
}

/* A host's styles (magma-maths.org's Pico) reach the modal's inputs,
   buttons, links and [role=group]s: the rules below set what those change
   to the handbook's values, `revert` where that is the browser's own. */

/* ── overlay + modal shell ─────────────────────────────────────────────── */

#pf-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: clamp(1rem, 8vh, 6rem) 1rem 1rem;
  background: rgba(0, 0, 0, 0.5);
}
#pf-overlay:not([hidden]) { display: flex; }

#pf-modal {
  display: flex;
  flex-direction: column;
  width: min(680px, 100%);
  max-height: min(80vh, 40rem);
  background: var(--mg-bg, #fff);
  border: 1px solid var(--mg-border, #dee2e6);
  border-radius: 0.75rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  font: 400 1rem/1.65 var(--mg-font-sans, sans-serif);
  letter-spacing: normal;
  text-align: start;
  text-transform: none;
  color: var(--mg-text, #212529);
}

/* ── search input + close ──────────────────────────────────────────────── */
/* The modal is an unfiltered type-ahead (issue #389 PR C) -- no tabs, no
   area scope; those live in the Type/Kind/Area rail on search.html, the
   "Advanced search" surface the footer link hands off to. */

#pf-search-wrap {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.7rem 0.9rem;
  border-bottom: 1px solid var(--mg-border, #dee2e6);
}

#pf-search-icon {
  flex-shrink: 0;
  color: var(--mg-text-muted, #6c757d);
}

#pf-close {
  flex-shrink: 0;
  display: flex;
  padding: 0.35rem;
  background: none;
  border: none;
  color: var(--mg-text-muted, #6c757d);
  cursor: pointer;
  border-radius: 0.3rem;
  margin: 0;
  box-shadow: none;
  transition: background 0.15s, color 0.15s;
}
#pf-close:hover {
  background: var(--mg-sandstone, #f0f0f0);
  color: var(--mg-text);
}
#pf-close:focus-visible { outline: 2px solid var(--pf-accent); }

#pf-input {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  background: transparent;
  font-family: var(--mg-font-sans, inherit);
  font-size: 1rem;
  line-height: inherit;
  color: var(--mg-text, #212529);
  height: revert;
  margin: 0;
  padding: revert;
  box-shadow: none;
  background-image: none;
  -webkit-appearance: textfield;
  appearance: textfield;
  cursor: revert;
  transition: revert;
}
#pf-input::placeholder { color: var(--mg-text-muted, #6c757d); opacity: 1; }
#pf-input::-webkit-search-cancel-button { cursor: pointer; }

/* ── site switch (#2659) ──────────────────────────────────────────────── */
/* All / Handbook / Website under the input, when the website is searched
   too: native radios, hidden, behind their labels drawn as one control. */

#pf-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem 1rem;
  padding: 0.45rem 0.9rem;
  font-size: 0.72rem;
  color: var(--mg-text, #212529);
  border-bottom: 1px solid var(--mg-border, #dee2e6);
}
#pf-site {
  display: flex;
  gap: 0.35rem;
}
#pf-site label {
  position: relative;
  display: block;
  margin: 0;
  cursor: pointer;
}
#pf-site input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
#pf-site .pf-site-choice {
  display: block;
  padding: 0.15rem 0.7rem;
  font-size: 0.78rem;
  font-weight: 600;
  line-height: 1.5;
  color: var(--mg-text, #212529);
  border: 1px solid var(--mg-border, #dee2e6);
  border-radius: 99px;
}
#pf-site input:checked + .pf-site-choice {
  color: var(--pf-accent);
  border-color: var(--pf-accent);
  background: var(--pf-active-bg);
}
/* :focus, as WebKit drops :focus-visible when an arrow key moves the choice. */
#pf-site input:focus + .pf-site-choice {
  outline: 2px solid var(--pf-accent);
  outline-offset: 1px;
}

/* The website's results are unavailable: said under the list. */
.pf-note {
  margin: 0;
  padding: 0.45rem 1rem;
  font-size: 0.78rem;
  color: var(--mg-text-muted, #6c757d);
  border-top: 1px solid var(--mg-border, #dee2e6);
}

/* ── results ──────────────────────────────────────────────────────────── */

#pf-results {
  flex: 1;
  overflow-y: auto;
  padding: 0.35rem 0 0.5rem;
}
/* Chromium and Firefox make the scrolling list a Tab stop. */
#pf-results:focus-visible { outline: 3px solid var(--pf-accent); outline-offset: -3px; }

/* Just-opened / cleared modal: #pf-results has nothing in it, so its
   padding plus #pf-search-wrap's border-bottom and #pf-footer's border-top
   stack up as two hairlines with a dead band between them. Collapse the
   band and drop the redundant second rule until there's actually a result
   list (or a "no results" message) to separate the footer from. */
#pf-results:empty {
  padding: 0;
}
#pf-results:empty ~ #pf-footer {
  border-top: 0;
}
/* "No results" sits outside the listbox, which may hold only options. */
#pf-results:empty ~ .pf-msg:not([hidden]) ~ #pf-footer {
  border-top: 1px solid var(--mg-border, #dee2e6);
}

/* Divider between groups in the compact modal only -- the results page
   spaces its groups out instead (#pf-page-results .pf-group margin). */
#pf-results .pf-group {
  display: revert;
  position: revert;
  width: revert;
  margin: revert;
  border-radius: revert;
  box-shadow: revert;
  vertical-align: revert;
  transition: revert;
}
#pf-results .pf-group > * { position: revert; }
#pf-results .pf-group + .pf-group { border-top: 1px solid var(--mg-border, #dee2e6); }

.pf-group-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 1rem 0.3rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--mg-text, #212529);
}
.pf-group-count {
  font-weight: 600;
  letter-spacing: 0;
}

.pf-result {
  display: block;
  padding: 0.5rem 1rem;
  color: inherit;
  border-left: 3px solid transparent;
  transition: revert;
}
/* The whole card is one link; only the title should read as linked text.
   `!important` overrides the theme's own `a` / `.bd-article a` underline
   rules, which otherwise paint an underline through the whole card. */
.pf-result,
.pf-result:hover,
.pf-result:focus,
.pf-result:visited { text-decoration: none !important; }
.pf-result.pf-active {
  background: var(--pf-active-bg);
  border-left-color: var(--pf-accent);
}

.pf-result-top {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
}
.pf-result-title {
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}
/* Name + argument tail on one line ("EllipticCurve([a, b]): [ RngElt ] ->
   CrvEll"): the name reads as the link, the tail as its signature. */
.pf-result-name {
  font-weight: 600;
  color: var(--pf-accent);
}
.pf-result:hover .pf-result-name,
.pf-result.pf-active .pf-result-name { text-decoration: underline; }
.pf-result-area {
  flex-shrink: 0;
  margin-left: auto;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--mg-text, #212529);
  white-space: nowrap;
}
.pf-result-sig {
  font-family: var(--mg-font-mono, monospace);
  font-size: 0.82em;
  font-weight: 400;
  /* Primary information, not a caption -- full body contrast (WCAG 1.4.3). */
  color: var(--mg-text, #212529);
}
.pf-result-excerpt {
  margin-top: 0.15rem;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--mg-text, #212529);
}
.pf-result-excerpt mark {
  background: transparent;
  color: var(--pf-accent);
  font-weight: 700;
  padding: 0;
}

.pf-more {
  display: block;
  width: 100%;
  padding: 0.3rem 1rem 0.45rem;
  background: none;
  border: none;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  text-align: left;
  color: var(--pf-accent);
  cursor: pointer;
}
.pf-more:hover,
.pf-more.pf-active { text-decoration: underline; }
.pf-more.pf-active {
  background: var(--pf-active-bg);
  box-shadow: inset 3px 0 0 var(--pf-accent);
}
.pf-more:focus-visible { outline: 2px solid var(--pf-accent); outline-offset: -2px; }

.pf-msg {
  margin: 0 0 1.15rem;
  padding: 2rem 1rem;
  text-align: center;
  font-size: 0.9rem;
  color: var(--mg-text, #212529);
}

/* ── type badges ──────────────────────────────────────────────────────── */

.pf-badge {
  flex-shrink: 0;
  display: inline-block;
  padding: 0.1rem 0.45rem;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  border-radius: 99px;
}
.pf-badge--function    { background: #fef3c7; color: #92400e; }
.pf-badge--procedure   { background: #dbeafe; color: #1e40af; }
.pf-badge--constructor { background: #fce7f3; color: #9d174d; }
.pf-badge--operation   { background: #ede9fe; color: #5b21b6; }
.pf-badge--literal     { background: #ffedd5; color: #9a3412; }
.pf-badge--attribute   { background: #e0e7ff; color: #3730a3; }
.pf-badge--indexed     { background: #cffafe; color: #155e75; }
.pf-badge--type        { background: #dcfce7; color: #166534; }
.pf-badge--example     { background: #ecfccb; color: #3f6212; }
.pf-badge--section     { background: #d1fae5; color: #065f46; }
.pf-badge--page        { background: #e2e8f0; color: #334155; }

html[data-theme="dark"] .pf-badge--function    { background: #4a370d; color: #fde68a; }
html[data-theme="dark"] .pf-badge--procedure   { background: #1e3a5f; color: #bfdbfe; }
html[data-theme="dark"] .pf-badge--constructor { background: #500724; color: #fbcfe8; }
html[data-theme="dark"] .pf-badge--operation   { background: #2e1065; color: #ddd6fe; }
html[data-theme="dark"] .pf-badge--literal     { background: #431407; color: #fed7aa; }
html[data-theme="dark"] .pf-badge--attribute   { background: #1e1b4b; color: #c7d2fe; }
html[data-theme="dark"] .pf-badge--indexed     { background: #083344; color: #a5f3fc; }
html[data-theme="dark"] .pf-badge--type        { background: #052e16; color: #bbf7d0; }
html[data-theme="dark"] .pf-badge--example     { background: #1a2e05; color: #d9f99d; }
html[data-theme="dark"] .pf-badge--section     { background: #052e1c; color: #a7f3d0; }
html[data-theme="dark"] .pf-badge--page        { background: #1e293b; color: #cbd5e1; }

/* ── footer ───────────────────────────────────────────────────────────── */

#pf-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.45rem 1rem;
  font-size: 0.72rem;
  color: var(--mg-text, #212529);
  border-top: 1px solid var(--mg-border, #dee2e6);
}
#pf-footer kbd,
#pf-tools kbd {
  display: inline-block;
  padding: 0.05rem 0.3rem;
  margin: 0 0.1rem;
  font-family: var(--mg-font-mono, monospace);
  font-size: 0.9em;
  line-height: 1.4;
  border: 1px solid var(--mg-border, #dee2e6);
  border-radius: 0.25rem;
  background: var(--mg-sandstone, #f8f9fa);
  box-shadow: 1px 1px 1px var(--pf-kbd-shadow);
  font-weight: 400;
  color: var(--mg-text-muted, #6c757d);
}
#pf-footer a {
  color: var(--pf-accent);
  font-weight: 600;
  text-decoration: underline;
  transition: revert;
}
#pf-footer a:focus-visible {
  outline: 3px solid var(--pf-accent);
  outline-offset: 1px;
  box-shadow: none;
}

/* With the hints beside the site switch, the footer holds only the link. */
#pf-tools ~ #pf-footer { justify-content: flex-end; }

@media (max-width: 480px) {
  #pf-footer span:first-child,
  #pf-tools .pf-hints { display: none; }
  #pf-footer { justify-content: flex-end; }
}

/* Focus parked on <body> after the modal closes (restoreFocus in
   magma-search.js) marks where Tab resumes, not a control. */
body.pf-focus-start:focus { outline: none; }

/* ── full results page (search.html) ──────────────────────────────────── */
/* The layout, card, filters and pagers are web-base's search component
   (mg-search-*, mg-pager*; issue #2650). What follows centres it at
   web-base's width and draws the handbook's own result rows: a plain list,
   not cards. Every rule here is scoped under .pf-page. */

/* The page has no handbook sidebar (left-sidebar-collapse.js), so it is
   centred at web-base's width, as web-static's search pages are, with the
   heading on the layout's left edge. */
.pf-page {
  --mg-search-layout-max: 1120px;
}

.pf-page > h1 {
  max-width: var(--mg-search-layout-max);
  margin-inline: auto;
}

.pf-page .mg-search-layout {
  --mg-search-layout-pad: 1.25rem 0 0;
  --mg-search-layout-pad-narrow: 1rem 0 0;
  --mg-search-list-gap: 1rem;
}

/* The "Exact match" checkbox sits with the filters but is not an
   mg-search-field, whose full-width, appearance-less input rule would
   hide the box. */
.pf-page .pf-page-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.pf-page .pf-page-check input {
  width: 1.1rem;
  height: 1.1rem;
  margin: 0;
  accent-color: var(--mg-ochre);
}
.pf-page .pf-page-check input:disabled + label { color: var(--mg-text-muted); }
.pf-page .pf-page-check label {
  margin: 0;
  font-size: 0.875rem;
  line-height: 1.4;
}

.pf-page .pf-page-group { margin-top: 1.75rem; }
.pf-page .pf-page-group:first-child { margin-top: 1rem; }
.pf-page .pf-page-group-head {
  margin: 0;
  padding-bottom: 0.4rem;
  font-family: var(--mg-font-sans);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.4;
  text-transform: uppercase;
  color: var(--mg-text);
}
.pf-page .pf-page-group-count { font-weight: 600; letter-spacing: 0; }

.pf-page .pf-result { padding: 0; border-left: 0; }
.pf-page .pf-object .pf-result + .pf-result { margin-top: 0.35rem; }
.pf-page .pf-result-title { font-size: 1rem; }
.pf-page .pf-result-excerpt { margin-top: 0.3rem; font-size: 0.9rem; }
.pf-page .pf-more { padding: 0.3rem 0 0; }

/* Paging from the bottom pager scrolls back to the results head; clear the
   sticky site header (--pst-header-height is only its inner row, see
   custom.css). */
.pf-page .mg-search-results-head {
  scroll-margin-top: calc(var(--pst-header-height, 60px) + 33px + 1rem);
}
/* custom.css's article paragraph margin would add to the head's height,
   which web-base reserves before the first search. */
.pf-page .mg-search-results-count { margin: 0; }

/* A typed query, Order(GrpPerm): the label on the signature it names
   exactly, outlined so that it does not read as one more kind badge. */
.pf-page .pf-exact-match {
  flex-shrink: 0;
  align-self: center;
  padding: 0 0.45rem;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.5;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--mg-success);
  border: 1px solid currentColor;
  border-radius: 99px;
}

/* The website's results are unavailable (#2659): under the status line. */
.pf-page .pf-page-note {
  margin: 0.25rem 0 0;
  font-size: 0.875rem;
  color: var(--mg-text-muted);
}

/* No results: the hint under the status line, on its left edge. */
.pf-page .mg-search-empty {
  padding: 0;
  text-align: left;
}
.pf-page .mg-search-empty a { color: var(--mg-ochre-text); font-weight: 600; }

/* No query: the lead and the search tips fill the results column. */
.pf-page .pf-page-intro { padding-top: 0.25rem; font-size: 0.95rem; line-height: 1.55; }
.pf-page .pf-page-lead { margin: 0 0 1.5rem; font-size: 1.05rem; }
.pf-page .pf-page-intro h2 {
  margin: 1.5rem 0 0.5rem;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.pf-page .pf-page-intro ul { margin: 0; padding: 0; list-style: none; }
.pf-page .pf-page-intro li { margin: 0.55rem 0; }
.pf-page .pf-page-intro a { color: var(--mg-ochre-text); font-weight: 600; }
/* The site's --pst-color-inline-code is the muted token, which fails AA
   for small inline <code>; give the tips' code snippets full contrast. */
.pf-page .pf-page-intro code {
  color: var(--mg-text);
  background: var(--mg-sandstone);
}

@media (prefers-reduced-motion: reduce) {
  #pf-close, .pf-result { transition: none; }
}

/* -- the home page's search line ---------------------------------------- */
/* Drawn as the modal's top line, so the modal opened over it (.pf-anchored)
   looks like the line opening out into the results; bordered as the site's
   search fields are (web-base's .mg-search-field) so it reads as one. */

.pf-inline-search {
  --pf-line-height: 3.5rem;
  --pf-line-radius: 8px;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  box-sizing: border-box;
  min-height: var(--pf-line-height);
  padding: 0 1rem;
  margin: 0;
  background: var(--mg-white);
  border: 1px solid var(--mg-input-border);
  border-radius: var(--pf-line-radius);
  cursor: text;
  transition: border-color 0.15s;
}
.pf-inline-search:hover { border-color: var(--mg-text-muted); }
/* web-base's field focus ring, which the modal over the line takes on. */
.pf-inline-search:focus-within {
  border-color: var(--mg-ochre);
  outline: 3px solid var(--mg-ochre-text);
  outline-offset: 3px;
}

.pf-inline-search-icon {
  flex-shrink: 0;
  color: var(--mg-text);
}

/* Stretched to the line's height, so a click above or below the text
   still puts the caret where it lands. */
.pf-inline-search-input {
  flex: 1;
  align-self: stretch;
  min-width: 0;
  border: none;
  outline: none;
  background: transparent;
  font-family: var(--pst-font-family-base, inherit);
  font-size: 1.0625rem;
  color: var(--mg-text);
}
.pf-inline-search-input::placeholder { color: var(--mg-text-muted); opacity: 1; }

/* Hidden where there is no keyboard to press it on or no room for it. */
.pf-inline-search-key {
  display: none;
  flex-shrink: 0;
  padding: 0.15rem 0.45rem;
  font-family: var(--pst-font-family-base, inherit);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--mg-text-muted);
  background: var(--mg-bg);
  border: 1px solid var(--mg-border);
  border-radius: 4px;
  box-shadow: none;
  white-space: nowrap;
  word-spacing: 0.2em;
}
@media (hover: hover) and (min-width: 576px) {
  .pf-inline-search.pf-key-shown > .pf-inline-search-key,
  #pf-overlay.pf-anchored.pf-key-shown .pf-modal-key { display: inline; }
}

/* The variables are the line's box, set by magma-search.js's placeModal. */
#pf-overlay.pf-anchored {
  padding: var(--pf-anchor-top) 0 1rem var(--pf-anchor-left);
  justify-content: flex-start;
}
#pf-overlay.pf-anchored #pf-modal {
  box-sizing: border-box;
  width: var(--pf-anchor-width);
  max-height: min(40rem, calc(100vh - var(--pf-anchor-top) - 1rem));
  border-color: var(--mg-ochre);
  border-radius: 8px;
}
/* Ring the modal while its field has focus, not its close button or link. */
#pf-overlay.pf-anchored #pf-modal:has(#pf-input:focus) {
  outline: 3px solid var(--mg-ochre-text);
  outline-offset: 3px;
}
#pf-overlay.pf-anchored #pf-search-wrap {
  box-sizing: border-box;
  min-height: calc(var(--pf-anchor-height) - 1px);
  padding: 0 1rem;
  gap: 0.75rem;
}
#pf-overlay.pf-anchored #pf-search-icon { color: var(--mg-text); }
#pf-overlay.pf-anchored #pf-input { align-self: stretch; font-size: 1.0625rem; }
