/*
 * find.css: the in-document find bar (js/reader/find.js) and its highlights.
 *
 * Uses the theme tokens from tokens.css, always loaded with the shell (input, icon buttons and marks
 * match the shell's search box, .icon-btn and search highlights).
 * The bar is sticky at the top of the text pane and overlays the content (negative bottom margin),
 * so opening it never pushes the text down.
 *   --find-top   sticky offset (e.g. the header height when the document itself scrolls)
 */

.find-bar {
  --find-h: 48px;
  position: sticky;
  top: var(--find-top, 0px);
  z-index: 20;
  height: var(--find-h);
  margin-bottom: calc(-1 * var(--find-h));
  border-bottom: 1px solid var(--border);
  background: var(--bg-reading);
  color: var(--text);
  font: 400 var(--fs-ui-lg) / 1.4 var(--font-ui);
}

.find-bar[hidden] {
  display: none;
}

.find-bar--page {
  top: var(--find-top, var(--header-h));
}

.find-inner {
  display: flex;
  align-items: center;
  gap: 2px;
  max-width: calc(var(--measure) + 32px);
  height: 100%;
  margin: 0 auto;
  padding: 0 16px;
}

.find-field {
  position: relative;
  display: flex;
  flex: 1 1 auto;
  align-items: center;
  min-width: 0;
  margin-right: 8px;
}

.find-icon {
  position: absolute;
  left: 13px;
  width: 15px;
  height: 15px;
  color: var(--text-muted);
  pointer-events: none;
}

.find-input {
  width: 100%;
  min-width: 0;
  height: max(32px, var(--tap-min));
  padding: 0 12px 0 36px;
  border: 1px solid var(--control-border);
  border-radius: var(--radius-pill);
  background: var(--control-bg);
  color: var(--text);
  font: inherit;
  font-size: var(--fs-search);
  appearance: none;
  transition: border-color 0.12s ease;
}

.find-input:hover {
  border-color: var(--rule);
}

.find-input::placeholder {
  color: var(--text-muted);
  opacity: 1;
}

.find-input::-webkit-search-cancel-button,
.find-input::-webkit-search-decoration {
  appearance: none;
  display: none;
}

.find-input:focus-visible,
.find-btn:focus-visible,
.find-result:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

.find-count {
  flex: none;
  min-width: 5.5em;
  margin-right: 4px;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

.find-bar.is-empty .find-count {
  color: var(--accent);
}

.find-btn {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: max(32px, var(--tap-min));
  height: max(32px, var(--tap-min));
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-nav);
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.find-btn .icon {
  width: 16px;
  height: 16px;
}

@media (hover: hover) {
  .find-btn:hover {
    background: var(--hover-bg);
    color: var(--text-title);
  }
}

/* Focus given back on close to an element that had no visible focus before (see find.js). */
html.find-return-quiet :focus-visible {
  outline: none;
}

/* Server results: a quiet list under the bar. */
.find-results {
  position: absolute;
  top: calc(100% + 1px);
  left: 50%;
  width: min(calc(var(--measure) + 32px), 100%);
  max-height: min(50vh, 360px);
  margin: 0;
  padding: 4px 0;
  overflow: auto;
  overscroll-behavior: contain;
  transform: translateX(-50%);
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 8px 8px;
  background: var(--bg-elevated);
  list-style: none;
}

.find-results[hidden] {
  display: none;
}

.find-result {
  display: grid;
  grid-template-columns: 5.5em 1fr;
  gap: 12px;
  width: 100%;
  margin: 0;
  padding: 8px 16px;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: var(--fs-ui-lg);
  line-height: 1.45;
  text-align: left;
  cursor: pointer;
}

.find-result:hover {
  background: var(--hover-bg);
}

.find-result[aria-current="true"] {
  background: var(--current-bg);
}

.find-result-page {
  padding-top: 1px;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.find-result[aria-current="true"] .find-result-page {
  color: var(--accent);
}

.find-result-snippet {
  min-width: 0;
  overflow-wrap: anywhere;
}

/* Highlights in the text, with the same tokens as the search highlights in reader.css. The current
   hit is a clearly darker (light theme) or lighter (dark theme) amber, so it differs in lightness and
   not only in hue. */
mark.find-hit,
.find-result-snippet mark {
  border-radius: 2px;
  background: var(--mark-bg);
  color: var(--mark-fg);
  box-shadow: 0 0 0 1px var(--mark-bg);
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

mark.find-hit.is-current {
  background: var(--mark-current-bg);
  color: var(--mark-current-fg);
  box-shadow: 0 0 0 1px var(--mark-current-bg);
}

@media (forced-colors: active) {
  mark.find-hit {
    background: Mark;
    color: MarkText;
  }
  mark.find-hit.is-current {
    background: Highlight;
    color: HighlightText;
  }
}
