/* ── Topic card grid: title + icon only, centered ────────────────────────── */

/* Narrow, centered column of cards (2-up) instead of stretching full width.
   Shared by the topic grid and the Preface/General Index/References
   quicklink cards so both families render at identical height/width. */
.index-topic-grid,
.index-quicklinks {
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
}

/* The search line above them (index.md) spans the cards themselves, which
   sit 0.75rem inside the column for the grid's gutters. The space under it
   is the space over it: the article container's 1rem and its own 0.5rem,
   and from 1200px the article's 1.5rem (the theme's rule, below). */
.pf-inline-search.index-search {
  width: calc(100% - 1.5rem);
  max-width: calc(640px - 1.5rem);
  margin: 0.5rem auto 1.5rem;
}
@media (min-width: 1200px) {
  .pf-inline-search.index-search { margin-bottom: 3rem; }
}
/* A preview build's notice follows; its own top margin (the theme's
   1.5625em) would outweigh the line's and make the space under it 1px more. */
.pf-inline-search.index-search + .admonition { margin-top: 0; }

/* From 960px the article is padded 2rem on the left alone (the theme's rule,
   which custom.css starts there), so a grid centred in it sits 1rem right of
   the article's middle and the right column's tooltip (index-cards.js) runs
   out of room 2rem before the left one's (#2730). */
@media (min-width: 960px) {
  .bd-main .bd-content .bd-article-container .bd-article:has(.index-topic-grid:not(.index-list)) {
    padding-right: 2rem;
  }
}

.index-topic-grid .sd-card-text,
.index-quicklinks .sd-card-text {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.index-topic-grid .sd-card-body,
.index-quicklinks .sd-card-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.55rem 0.6rem;
  min-height: 2.5rem;
}

.index-topic-grid:not(.index-list) .sd-card-body {
  padding: 0.35rem 0.5rem;
  min-height: 2rem;
}

.index-topic-grid .sd-card-title,
.index-quicklinks .sd-card-title {
  text-align: center;
  margin: 0;
  font-size: 0.82rem;
}

/* ── 1. Column-major reading order (issue #130) ──────────────────────────────
   sphinx-design's .sd-row is a flex row that wraps row-major: with 2 columns,
   card 2 lands beside card 1, card 3 below card 1, etc, so reading down the
   first column jumps between unrelated subjects instead of continuing the
   list. CSS multi-column layout flows content down one column before
   starting the next -- same order as index.rst's grid-item-card list --
   and balances the two columns' heights automatically, with no hardcoded
   item/row count to keep in sync as subjects are added or removed.
   Only the topic grid needs this: the quicklink grids are 1-3 items, too
   short for column order to matter. Scoped to >= 576px (sphinx-design's own
   "sm" breakpoint, where this grid becomes 2 columns via "grid:: 1 2 2 2");
   below that it's already a single column, where source order already
   reads correctly top-to-bottom.

   :not(.index-list) -- the homepage's topic grid only (issue #137): depth-2
   category index pages (e.g. Representation Theory) carry .index-topic-grid
   too (see custom.css's .index-list rules), but they use "grid:: 1" and are
   meant to stay a single column of full-width rows regardless of viewport
   width; without this exclusion they were being swept into the homepage's
   2-column layout by class alone. */
@media (min-width: 576px) {
  .index-topic-grid:not(.index-list):not(.index-equal-rows) .sd-row {
    column-count: 2;
    column-gap: var(--sd-gutter-x, 0.5rem);
    display: block;
    margin-left: 0;
    margin-right: 0;
  }

  /* sphinx-design's base ".sd-row > *" rule puts the vertical gutter on
     margin-top, which browsers trim to 0 on whichever card a column break
     lands before -- including the second column's first card, which then
     sits flush against the row's top edge while the first column's own
     first card keeps its margin-top, misaligning the two columns' tops.
     Putting the gutter on margin-bottom instead (the original #130 fix)
     misaligned them the other way in Safari (issue #426): WebKit doesn't
     truncate the first column's last card's trailing margin at the column
     break, so it carried over to the top of the second column and pushed
     every right-hand card down by one gutter. Blink and Gecko do truncate
     it, which is why Chrome and Firefox looked right.
     So zero both margins and carry the gutter as padding-bottom: padding
     sits inside the card's break-inside: avoid box, so no margin adjoins
     the column break for any engine to keep or drop, and every card --
     first-in-column or not -- gets consistent spacing with both columns
     starting flush at the row's top edge. */
  .index-topic-grid:not(.index-list):not(.index-equal-rows) .sd-row > .sd-col {
    -webkit-column-break-inside: avoid;
    break-inside: avoid;
    margin-top: 0;
    margin-bottom: 0;
    padding-bottom: var(--sd-gutter-y, 0.5rem);
    padding-left: 0;
    padding-right: 0;
    width: 100%;
  }
}

/* The columns above, read down equal rows once index-cards.js has counted the
   cards: all one height, so a card that wraps is no taller than the rest
   (#3696). Not a list (#137); without the script the columns are unchanged. */
@media (min-width: 576px) {
  .index-topic-grid:not(.index-list).index-equal-rows .sd-row {
    display: grid;
    gap: var(--sd-gutter-y, 0.5rem) var(--sd-gutter-x, 0.5rem);
    grid-auto-flow: column;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(var(--mh-index-rows), 1fr);
    margin-left: 0;
    margin-right: 0;
    /* The columns above carry the gutter under each card; the last one's is
       kept here, so the space under the grid is as it was. */
    padding-bottom: var(--sd-gutter-y, 0.5rem);
  }

  .index-topic-grid.index-equal-rows .sd-row > .sd-col {
    margin: 0;
    padding: 0;
    width: 100%;
  }
}

/* ── 2. Card interaction (shared with the top quicklink cards) ──────────────
   Flat by design (issue #90): the hover feedback is a border-colour change
   handled globally for every .sd-card in custom.css, not a shadow/lift here. */

.index-topic-grid .sd-card,
.index-quicklinks .sd-card {
  cursor: pointer;
  overflow: hidden;
}

/* sphinx-design's card link is a zero-height anchor whose click area is an
   ::after, so its focus ring was clipped by the overflow above and link
   hinting (Vimium) found no box to hint (#2546). Give the anchor itself
   the card's box instead, with the ring drawn inside the clip. */
.index-topic-grid .sd-card .sd-stretched-link,
.index-quicklinks .sd-card .sd-stretched-link {
  border-radius: inherit;
  inset: 0;
  position: absolute;
  z-index: 1;
}

.index-topic-grid .sd-card .sd-stretched-link::after,
.index-quicklinks .sd-card .sd-stretched-link::after {
  content: none;
}

.index-topic-grid .sd-card .sd-stretched-link:focus-visible,
.index-quicklinks .sd-card .sd-stretched-link:focus-visible {
  outline: 3px solid var(--pst-color-accent);
  outline-offset: -3px;
}

/* Where a tooltip has no room beside its card, and on touch screens, which
   cannot hover, index-cards.js marks the grid to show each description in
   its card instead (#2719). */
.index-topic-grid.index-cards-described .sd-card .sd-card-text,
.index-quicklinks.index-cards-described .sd-card .sd-card-text {
  clip: auto;
  color: var(--pst-color-text-muted);
  font-size: 0.8rem;
  height: auto;
  margin: 0.2rem 0 0;
  overflow: visible;
  position: static;
  text-align: center;
  white-space: normal;
  width: auto;
}

/* custom.css lays the single-column list rows out as a row; stack the
   description under the title instead. */
.index-topic-grid.index-list.index-cards-described .sd-card-body {
  align-items: flex-start;
  flex-direction: column;
}

.index-topic-grid.index-list.index-cards-described .sd-card .sd-card-text {
  text-align: left;
}

/* The same three rules for touch screens without the class, so they show
   the descriptions before the script runs and without it. */
@media (hover: none) {
  .index-topic-grid .sd-card .sd-card-text,
  .index-quicklinks .sd-card .sd-card-text {
    clip: auto;
    color: var(--pst-color-text-muted);
    font-size: 0.8rem;
    height: auto;
    margin: 0.2rem 0 0;
    overflow: visible;
    position: static;
    text-align: center;
    white-space: normal;
    width: auto;
  }

  .index-topic-grid.index-list .sd-card-body {
    align-items: flex-start;
    flex-direction: column;
  }

  .index-topic-grid.index-list .sd-card .sd-card-text {
    text-align: left;
  }
}

/* index-cards.js adds this class before the cards are parsed and removes it
   once it has decided where each grid shows its descriptions, which changes
   the cards' heights. Hidden, not removed, so the page keeps its layout.
   Only what will change is hidden: a touch screen's descriptions are in the
   stylesheet, so only the topic grid, from 576px, where it becomes rows. */
@media not all and (hover: none) {
  html.index-cards-pending .index-topic-grid,
  html.index-cards-pending .index-quicklinks {
    visibility: hidden;
  }
}

@media (hover: none) and (min-width: 576px) {
  html.index-cards-pending .index-topic-grid:not(.index-list) {
    visibility: hidden;
  }
}

/* ── Hover tooltip (dialog field) ────────────────────────────────────────── */

#card-tooltip {
  position: fixed;
  z-index: 8000;
  display: none;
  background: var(--pst-color-background, #fff);
  border: 1px solid var(--pst-color-border, #dee2e6);
  border-radius: 0.5rem;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.15);
  padding: 0.75rem 1rem;
  max-width: 260px;
  font-size: 0.84rem;
  line-height: 1.55;
  color: var(--pst-color-text-base, #212529);
}

#card-tooltip.visible {
  display: block;
}

/* index-cards.js places it only beside the grid, where it covers no card, so
   the pointer may rest on it (WCAG 1.4.13). The arrow points sideways at
   the card. */
#card-tooltip::before,
#card-tooltip::after {
  border: 7px solid transparent;
  content: "";
  position: absolute;
  top: var(--arrow-top, 50%);
  transform: translateY(-50%);
}

#card-tooltip::after {
  border-width: 6px;
}

#card-tooltip.left::before {
  border-left-color: var(--pst-color-border, #dee2e6);
  border-right-width: 0;
  right: -7px;
}

#card-tooltip.left::after {
  border-left-color: var(--pst-color-background, #fff);
  border-right-width: 0;
  right: -6px;
}

#card-tooltip.right::before {
  border-left-width: 0;
  border-right-color: var(--pst-color-border, #dee2e6);
  left: -7px;
}

#card-tooltip.right::after {
  border-left-width: 0;
  border-right-color: var(--pst-color-background, #fff);
  left: -6px;
}

@media (max-width: 575.98px) {
  .index-quicklinks > .sd-row {
    grid-template-columns: 1fr !important;
  }

  .index-quicklinks > .sd-row > .sd-col {
    width: 100% !important;
    max-width: 100% !important;
    flex: 0 0 100% !important;
  }
}
