/*
 * The search of a site: the button of the header of the home page and of
 * the sheets (TD, exams), and the box that the button, or the key "/",
 * opens over the page (theme/js/search.js fills it); the slides have the
 * key only. Colours, fonts and the tags Cours, TD, EXAM come from base.css
 * and the theme (graphite.css).
 */

/* ------------------------------------------------------------ the button */

/* In the header, right of "Sommaire", at the right edge of the page: no
   wider than its words, it shows the key that opens the search. */
.home-header,
.sheet-header {
  position: relative;
}

.search-open {
  position: absolute;
  top: 48px;                 /* centred on "Sommaire" */
  right: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  box-sizing: border-box;
  padding: 7px 9px 7px 11px;
  border: 1px solid var(--rule);
  border-radius: 10px;
  background: #ffffff;
  font: inherit;
  font-size: 14.5px;
  color: var(--muted);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.12s, color 0.12s;
}

/* A sheet: within the paper. */
.sheet-header .search-open {
  right: 16px;
}

.search-open:hover,
.search-open:focus-visible {
  border-color: var(--muted);
  color: var(--heading);
}

.search-open:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.search-open svg {
  flex: none;
}

.search-open kbd {
  margin-left: 4px;
}

/* A key: on the button, in the box. */
.search-open kbd,
.search kbd {
  flex: none;
  min-width: 20px;
  padding: 1px 5px;
  box-sizing: border-box;
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--bg);
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.5;
  text-align: center;
  color: var(--muted);
}

/* --------------------------------------------------------------- the box */

.search {
  width: min(720px, calc(100vw - 32px));
  max-width: none;
  max-height: none;
  margin: 11vh auto 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--rule);
  border-radius: 12px;
  background: #ffffff;
  box-shadow: 0 24px 64px -16px rgba(11, 13, 16, 0.45);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  color: var(--fg);
}

/* (Not on .search alone: a closed <dialog> must stay display: none.) */
.search[open] {
  display: flex;
  flex-direction: column;
  max-height: min(78vh, 680px);
}

.search::backdrop {
  background: rgba(11, 13, 16, 0.4);
}

/* The page behind does not scroll. */
html:has(.search[open]) {
  overflow: hidden;
}

.search-field {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px 14px 18px;
  color: var(--muted);
}

.search-field svg {
  flex: none;
}

.search-field input {
  flex: 1;
  min-width: 0;
  padding: 0;
  border: 0;
  outline: 0;
  background: none;
  font: inherit;
  font-size: 18px;
  color: var(--heading);
}

.search-field input::placeholder {
  color: var(--muted);
  opacity: 0.8;
}

/* ----------------------------------------------------------- the results */

.search-results {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px;
  border-top: 1px solid var(--rule);
}

/* Nothing typed yet: the field and the keys only. */
.search-results:empty {
  display: none;
}

.search-message {
  margin: 0;
  padding: 18px 10px;
  color: var(--muted);
}

.search-page + .search-page {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--rule);
}

/* A row: a page, one of its passages, or "n autres passages". */
.search-row {
  display: grid;
  align-items: baseline;
  column-gap: 12px;
  width: 100%;
  box-sizing: border-box;
  padding: 6px 10px;
  border: 0;
  border-radius: 7px;
  background: none;
  font: inherit;
  color: var(--fg);
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

/* The box looks the same on every page: the rules of a page for its links,
   its code and its paragraphs (sheet.css: .sheet a, .sheet a code, .sheet p)
   do not reach into it. */
.search a.search-row {
  color: var(--fg);
  text-decoration: none;
}

.search .search-snippet code {
  padding: 0;
  border-radius: 0;
  background: none;
  color: var(--inline-code-fg);
}

.search .search-foot,
.search .search-message {
  margin: 0;
}

.search-row[aria-selected="true"] {
  background: var(--accent-soft);
}

/* A page: its tag, in the column of the tags of the home page, its title,
   and its session on the right. */
.search-title {
  grid-template-columns: 54px minmax(0, 1fr) auto;
}

.search-title .home-label {
  grid-column: 1;
}

.search-page-title {
  grid-column: 2;
  overflow: hidden;
  font-weight: 600;
  color: var(--heading);
  white-space: nowrap;
  text-overflow: ellipsis;
}

.search-session {
  grid-column: 3;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

/* A passage: where the word was found (code or text), the words around it
   on one line, and the slide or the exercise it is in. */
/* What belongs to a page (its passages, "n autres passages") is set a little
   to the right of its title. */
.search-hit,
.search-more {
  width: calc(100% - 18px);
  margin-left: 18px;
}

.search-hit {
  grid-template-columns: 54px minmax(0, 1fr) auto;
  padding-top: 4px;
  padding-bottom: 4px;
  font-size: 14px;
}

.search-snippet {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.search-snippet-code,
.search-snippet code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  color: var(--inline-code-fg);
}

.search-snippet mark {
  padding: 0.05em 0;
  border-radius: 2px;
  background: var(--mark);
  color: var(--heading);
}

.search-where {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
}

/* Found in code: a dark tag, in the font of the code; in the text: the same
   tag, outlined in grey. (Also in the legend, under the results.) */
.search-kind {
  justify-self: start;
  min-width: 44px;
  padding: 1px 6px;
  box-sizing: border-box;
  border: 1px solid color-mix(in srgb, var(--muted) 55%, #ffffff);
  border-radius: 5px;
  background: #ffffff;
  font-size: 11.5px;
  font-weight: 500;
  line-height: 1.5;
  text-align: center;
  color: var(--muted);
}

.search-kind[data-kind="code"] {
  border-color: var(--accent);
  background: var(--accent);
  font-family: var(--font-mono);
  color: #ffffff;
}

.search-more {
  grid-template-columns: minmax(0, 1fr);
  padding: 4px 10px 4px 76px;       /* in line with the passages */
  font-size: 13px;
  color: var(--muted);
}

.search-more[aria-selected="true"] {
  color: var(--heading);
}

/* ------------------------------------------------------- keys and legend */

.search-foot {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 16px;
  margin: 0;
  padding: 9px 16px;
  border-top: 1px solid var(--rule);
  background: var(--bg);
  font-size: 12.5px;
  color: var(--muted);
}

.search-foot kbd {
  margin-right: 3px;
  background: #ffffff;
}

.search-keys kbd:nth-of-type(3) {
  margin-left: 10px;
}

.search-legend .search-kind {
  display: inline-block;
  min-width: 0;
  background: #ffffff;
}

.search-legend .search-kind[data-kind="code"] {
  background: var(--accent);
}

/* Printed: no button. */
@media print {
  .search-open {
    display: none;
  }
}

@media (max-width: 720px) {
  /* On a phone: a small button, without the key; the box takes the top of
     the screen, and the passages do not say where they are. */
  .search-open {
    top: 24px;
    padding: 6px 10px;
  }

  .search-open kbd,
  .search-field kbd,
  .search-keys,
  .search-where,
  .search-session {
    display: none;
  }

  .search {
    margin-top: 16px;
  }

  .search[open] {
    max-height: calc(100dvh - 32px);
  }

  .search-title,
  .search-hit {
    grid-template-columns: 54px minmax(0, 1fr);
  }
}
