/* Dialog family: species popup + species dialog, legend, chapters, help
 * (js/ui/speciesPopup.js, speciesModal.js, legend.js, chapters.js, help.js)
 *
 * One visual language: glass panels that float over the visualization, solid cards for the two
 * modal dialogs, display-serif titles, kicker section labels, --r-3 corners. Chrome colours come
 * from the tokens in styles.css; the only other colours are data colours that JS hands in as
 * custom properties (--species-color, --event-color) or as sampled planet colours.
 *
 * Contents
 *   1. Shared bits            close button, [hidden], modal dialog shell
 *   2. Corner panels          shell shared by legend and chapters
 *   3. Legend
 *   4. Chapters
 *   5. Species popup
 *   6. Species dialog
 *   7. Help
 *   8. Small screens & touch
 */

/* ========================================================================== */
/* 1. Shared bits                                                             */
/* ========================================================================== */

/* Several primitives set `display`, which would beat the hidden attribute. */
#legend-panel [hidden],
#chapters-panel [hidden],
.dlg [hidden] {
  display: none !important;
}

.dlg-close {
  flex: none;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-3);
  transition: background var(--dur-1) var(--ease), color var(--dur-1) var(--ease);
}

.dlg-close:hover {
  background: var(--bg-3);
  color: var(--text-1);
}

/* Running text never ends a paragraph on one stranded word; titles balance their lines. */
#species-popup-body,
.sp-fact-value,
#species-modal-body,
.sp-life-story,
.sp-caption,
.sp-clade-text,
.sp-kin,
.sp-rel-note,
.lg-intro,
.lg-note,
.lg-blurb,
.lg-row-detail,
.ch-blurb,
.hp-lead,
.hp-read-text p,
.hp-data-row dd,
.hp-key-row dd,
.hp-gesture dd,
.hp-tip {
  text-wrap: pretty;
}

#species-popup-title,
#species-modal-title,
.ch-title,
.hp-read-title {
  text-wrap: balance;
}

/* Tab panels take focus so the keyboard can scroll them; a quiet ring is enough. */
.lg-pane:focus-visible,
.hp-pane:focus-visible {
  outline: 1px solid var(--accent-line);
  outline-offset: 6px;
  border-radius: var(--r-2);
}

/* ---- Modal dialog shell (species dialog, help) ------------------------------ */

/* The <dialog> itself is an invisible box around the card, so a click that lands on it is a
   click on the backdrop. */
dialog.dlg {
  position: fixed;
  inset: 0;
  width: min(640px, calc(100vw - 2 * var(--s-4)));
  max-width: none;
  height: fit-content;
  max-height: none;
  margin: auto;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text-1);
  overflow: visible;
}

dialog.dlg::backdrop {
  background: rgba(3, 5, 10, 0.66);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

dialog.dlg[open] {
  animation: dlg-in var(--dur-2) var(--ease);
}

dialog.dlg[open]::backdrop {
  animation: dlg-fade var(--dur-2) ease-out;
}

@keyframes dlg-in {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.985);
  }
}

@keyframes dlg-fade {
  from {
    opacity: 0;
  }
}

.dlg-surface {
  display: flex;
  flex-direction: column;
  max-height: min(88vh, 880px);
  max-height: min(88dvh, 880px);
  background: var(--bg-1);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-3);
  box-shadow: var(--shadow-3);
  overflow: hidden;
}

/* Browsers without showModal(): no top layer, so draw the scrim from the card itself. */
dialog.dlg[open]:not(:modal) {
  z-index: var(--z-dialog);
}

dialog.dlg[open]:not(:modal) .dlg-surface {
  box-shadow: var(--shadow-3), 0 0 0 100vmax rgba(3, 5, 10, 0.66);
}

/* Top bar: Back (when the dialog has history) and Close. A rule appears under it once content
   has scrolled beneath. */
.dlg-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-2);
  min-height: 44px;
  padding: var(--s-1) var(--s-2) var(--s-1) var(--s-3);
  border-bottom: 1px solid transparent;
  transition: border-color var(--dur-1) var(--ease);
}

.dlg-bar .dlg-close {
  margin-left: auto;
}

.dlg-surface.is-scrolled .dlg-bar {
  border-bottom-color: var(--line);
}

.dlg-back {
  min-width: 0;
  min-height: 30px;
  padding: 0 var(--s-3) 0 var(--s-2);
  gap: var(--s-1);
  font-size: var(--fs-small);
}

.dlg-back-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.dlg-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 var(--s-5) var(--s-5);
}

.dlg-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s-2) var(--s-3);
  padding: var(--s-3) var(--s-5);
  border-top: 1px solid var(--line);
  background: var(--bg-2);
}

/* ========================================================================== */
/* 2. Corner panels (legend: top-left, chapters: top-right)                   */
/* ========================================================================== */

#legend-panel,
#chapters-panel {
  position: absolute;
  top: var(--s-4);
  display: flex;
  flex-direction: column;
  max-height: calc(100% - 2 * var(--s-4));
  background: var(--glass-strong);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  border: 1px solid var(--glass-line);
  border-radius: var(--r-3);
  box-shadow: var(--shadow-2);
  z-index: var(--z-panel);
  overflow: hidden;
  animation: panel-in var(--dur-2) var(--ease);
}

#legend-panel {
  left: var(--s-4);
  width: min(336px, calc(100% - 2 * var(--s-4)));
}

#chapters-panel {
  right: var(--s-4);
  width: min(376px, calc(100% - 2 * var(--s-4)));
}

@keyframes panel-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
}

.panel-head {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-3) var(--s-3) var(--s-2) var(--s-4);
}

.panel-title {
  flex: 1;
  font-size: var(--fs-h3);
}

.panel-meta {
  font-size: var(--fs-small);
  color: var(--text-3);
}

.panel-tabs {
  display: flex;
  margin: 0 var(--s-4) var(--s-3);
}

.panel-tabs .seg-btn {
  flex: 1 1 auto;
  padding: 0 var(--s-2);
}

.panel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--s-1) var(--s-4) var(--s-5);
  /* Content fades out at the bottom edge: a hint that the panel scrolls. */
  -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - var(--s-5)), transparent);
  mask-image: linear-gradient(to bottom, #000 calc(100% - var(--s-5)), transparent);
}

/* …and, once scrolled, at the top too, so a half-hidden entry dissolves instead of being cut. */
.panel-body.is-scrolled {
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 var(--s-5), #000 calc(100% - var(--s-5)), transparent);
  mask-image: linear-gradient(to bottom, transparent, #000 var(--s-5), #000 calc(100% - var(--s-5)), transparent);
}

.panel-foot {
  display: flex;
  align-items: center;
  gap: var(--s-1);
  padding: var(--s-2) var(--s-4);
  border-top: 1px solid var(--line-soft);
  font-size: var(--fs-small);
  color: var(--text-3);
}

.panel-foot span {
  margin-left: var(--s-2);
}

/* ========================================================================== */
/* 3. Legend                                                                  */
/* ========================================================================== */

.lg-intro,
.lg-note {
  font-size: var(--fs-small);
  line-height: 1.45;
  color: var(--text-3);
}

.lg-intro {
  margin-bottom: var(--s-4);
}

.lg-note {
  margin: calc(-1 * var(--s-1)) 0 var(--s-3);
}

.lg-groups,
.lg-rows,
.lg-cats {
  list-style: none;
}

.lg-groups {
  display: grid;
  gap: var(--s-3);
}

.lg-group {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  gap: var(--s-3);
  align-items: start;
}

.lg-glyph {
  display: grid;
  place-items: center;
  width: 24px;
  height: 22px;
}

.lg-name,
.lg-row-label,
.lg-swatch-title {
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--text-1);
}

.lg-blurb,
.lg-row-detail {
  font-size: var(--fs-small);
  line-height: 1.4;
  color: var(--text-2);
}

.lg-cats {
  display: flex;
  flex-wrap: wrap;
  gap: 2px var(--s-3);
  margin-top: var(--s-1);
}

.lg-cat {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: var(--fs-small);
  color: var(--text-3);
}

.lg-cat-glyph {
  display: inline-grid;
  place-items: center;
}

.lg-section + .lg-section {
  margin-top: var(--s-4);
  padding-top: var(--s-4);
  border-top: 1px solid var(--line-soft);
}

.lg-title {
  margin-bottom: var(--s-3);
}

.lg-rows {
  display: grid;
  gap: var(--s-3);
}

.lg-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: var(--s-3);
  align-items: center;
}

.lg-icon {
  display: grid;
  place-items: center;
  color: var(--text-2);
}

.lg-row.is-muted .lg-icon {
  color: var(--text-3);
}

.lg-row-label,
.lg-row-detail {
  display: block;
}

.lg-swatch + .lg-swatch {
  margin-top: var(--s-4);
}

.lg-swatch-bar {
  height: 12px;
  margin-top: var(--s-1);
  border-radius: var(--r-pill);
  box-shadow: inset 0 0 0 1px var(--line-soft);
}

.lg-swatch-labels {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--s-2);
  margin-top: 5px;
  font-size: var(--fs-small);
  line-height: 1.3;
  color: var(--text-3);
}

.lg-swatch-labels span:nth-child(2) {
  text-align: center;
}

.lg-swatch-labels span:nth-child(3) {
  text-align: right;
}

/* The timeline footer's temperature stripe, at the same thickness. */
.lg-stripe {
  display: block;
  width: 38px;
  height: 6px;
  border-radius: 3px;
}

/* ========================================================================== */
/* 4. Chapters                                                                */
/* ========================================================================== */

#chapters-panel .panel-body {
  padding: 0 var(--s-2) var(--s-5);
}

.ch-list {
  list-style: none;
  display: grid;
  gap: 2px;
}

.ch-item {
  position: relative;
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  gap: var(--s-2);
  width: 100%;
  padding: var(--s-2) var(--s-3) 10px var(--s-2);
  border: 1px solid transparent;
  border-radius: var(--r-2);
  background: transparent;
  color: inherit;
  text-align: left;
  transition: background var(--dur-1) var(--ease), border-color var(--dur-1) var(--ease);
}

.ch-item:hover {
  background: var(--bg-2);
}

.ch-item[aria-current] {
  background: var(--accent-soft);
  border-color: var(--accent-line);
}

.ch-num {
  padding-top: 3px;
  font-size: var(--fs-small);
  font-weight: 600;
  color: var(--text-3);
}

.ch-item[aria-current] .ch-num {
  color: var(--accent-strong);
}

.ch-diamond::before {
  content: '';
  display: block;
  width: 9px;
  height: 9px;
  margin: 3px 0 0 4px;
  border-radius: 1px;
  background: var(--event-color, var(--text-3));
  transform: rotate(45deg);
}

.ch-text,
.ch-range,
.ch-blurb {
  display: block;
}

.ch-top {
  display: flex;
  align-items: baseline;
  gap: var(--s-3);
}

.ch-title {
  flex: 1;
  font-size: var(--fs-lead);
  color: var(--text-1);
}

.ch-item.is-past .ch-title {
  color: var(--text-2);
}

.ch-length,
.ch-range {
  font-size: var(--fs-small);
  color: var(--text-3);
}

.ch-blurb {
  margin-top: 3px;
  font-size: var(--fs-small);
  line-height: 1.4;
  color: var(--text-2);
}

.ch-event .ch-title {
  font-size: var(--fs-body);
}

/* How far the story is through the current chapter. */
.ch-progress {
  position: absolute;
  left: var(--s-2);
  right: var(--s-2);
  bottom: 3px;
  height: 2px;
  border-radius: 1px;
}

.ch-item[aria-current] .ch-progress {
  background: linear-gradient(90deg, var(--accent) var(--progress, 0%), var(--accent-line) var(--progress, 0%));
}

/* ========================================================================== */
/* 5. Species popup (hover card)                                              */
/* ========================================================================== */

#species-popup {
  position: fixed;
  top: 0;
  left: 0;
  width: max-content;
  max-width: min(300px, calc(100vw - 2 * var(--s-2)));
  padding: var(--s-3) var(--s-4);
  background: var(--glass-strong);
  -webkit-backdrop-filter: var(--glass-blur);
  backdrop-filter: var(--glass-blur);
  border: 1px solid var(--glass-line);
  border-radius: var(--r-3);
  box-shadow: var(--shadow-2);
  pointer-events: none;
  z-index: var(--z-popup);
  animation: pop-in var(--dur-1) ease-out;
}

@keyframes pop-in {
  from {
    opacity: 0;
  }
}

/* Row-anchored cards point back at their row. */
#species-popup[data-arrow='right']::after {
  content: '';
  position: absolute;
  top: calc(var(--arrow-y, 24px) - 6px);
  right: -6px;
  width: 11px;
  height: 11px;
  background: var(--bg-1);
  border-top: 1px solid var(--glass-line);
  border-right: 1px solid var(--glass-line);
  transform: rotate(45deg);
}

.pop-head {
  display: flex;
  align-items: flex-start;
  gap: var(--s-2);
}

.pop-glyph {
  flex: none;
  display: grid;
  place-items: center;
  width: 20px;
  height: 21px;
}

#species-popup-title {
  font-size: var(--fs-lead);
  color: var(--text-1);
}

#species-popup-sci {
  font-size: var(--fs-small);
  color: var(--text-3);
}

#species-popup-meta {
  margin-top: var(--s-2);
  font-size: var(--fs-small);
  color: var(--text-2);
}

#species-popup-status {
  display: flex;
  align-items: baseline;
  gap: 7px;
  margin-top: 2px;
  font-size: var(--fs-small);
  color: var(--text-2);
}

.pop-status-dot {
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-4);
  transform: translateY(-1px);
}

#species-popup-status[data-state='alive'] {
  color: var(--text-1);
}

#species-popup-status[data-state='alive'] .pop-status-dot {
  background: var(--good);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent);
}

#species-popup-status[data-state='future'] .pop-status-dot {
  background: transparent;
  box-shadow: inset 0 0 0 1.5px var(--text-2);
}

#species-popup-body {
  margin-top: var(--s-2);
  font-size: var(--fs-body);
  line-height: 1.4;
  color: var(--text-2);
}

#species-popup-hint {
  margin-top: var(--s-2);
  font-size: var(--fs-small);
  color: var(--text-3);
}

/* ========================================================================== */
/* 6. Species dialog                                                          */
/* ========================================================================== */

/* A wash of the life group's colour behind the header, like a spotlight on a museum label. */
.sp-dialog .dlg-surface {
  background:
    radial-gradient(85% 240px at 0% 0%, color-mix(in srgb, var(--species-color, var(--accent)) 15%, transparent), transparent 72%),
    var(--bg-1);
}

.sp-head {
  display: grid;
  grid-template-columns: 60px minmax(0, 1fr);
  gap: var(--s-4);
  align-items: start;
  padding: var(--s-1) 0 var(--s-5);
}

.sp-medallion {
  display: grid;
  place-items: center;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--species-color, var(--accent)) 14%, var(--bg-2));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--species-color, var(--accent)) 55%, transparent);
}

#species-modal-title {
  font-size: var(--fs-h1);
  outline: none;
}

#species-modal-sci {
  margin-top: 2px;
  font-size: var(--fs-lead);
  color: var(--text-3);
}

.sp-tags {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
  margin-top: var(--s-3);
}

.sp-tags .tag {
  padding: 2px 10px;
  font-size: var(--fs-small);
  letter-spacing: 0.01em;
  background: var(--bg-2);
}

.sp-tag-glyph {
  display: inline-grid;
  place-items: center;
  margin-right: 2px;
}

.sp-tags .sp-tag-extant {
  color: var(--good);
  border-color: color-mix(in srgb, var(--good) 45%, transparent);
}

/* ---- Facts ------------------------------------------------------------------ */

.sp-facts {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--s-4) var(--s-5);
  padding: var(--s-4) 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.sp-fact dt {
  margin-bottom: 3px;
}

.sp-fact-value,
.sp-fact-note {
  display: block;
}

.sp-fact-value {
  font-size: var(--fs-lead);
  color: var(--text-1);
}

.sp-fact-note {
  font-size: var(--fs-small);
  color: var(--text-3);
}

#species-modal-body {
  padding: var(--s-4) 0 var(--s-5);
  font-size: var(--fs-lead);
  line-height: 1.6;
  color: var(--text-2);
}

/* ---- Sections --------------------------------------------------------------- */

.sp-section {
  padding: var(--s-4) 0 var(--s-5);
  border-top: 1px solid var(--line);
}

.sp-section:last-child {
  padding-bottom: 0;
}

.sp-section > .kicker,
.sp-section-head {
  margin-bottom: var(--s-3);
}

.sp-section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s-3);
}

.sp-now-key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-small);
  color: var(--text-3);
}

.sp-now-swatch {
  border: 4px solid transparent;
  border-top: 6px solid var(--text-1);
  border-bottom-width: 0;
}

/* ---- Lifespan on the whole-story timeline ----------------------------------- */

/* A thin rail of period blocks with the species' own span riding on it as a taller pill. */
.sp-life-track {
  position: relative;
  height: 22px;
  margin-top: var(--s-3);
}

.sp-life-periods {
  position: absolute;
  inset: 7px 0;
  border-radius: 2px;
  overflow: hidden;
  opacity: 0.36;
}

.sp-life-period {
  position: absolute;
  top: 0;
  bottom: 0;
  box-shadow: inset -1px 0 0 var(--bg-1);
}

/* Short-lived species keep a visible minimum width, centred on their true position; the ring
   separates the pill from the rail behind it. */
.sp-life-span {
  position: absolute;
  top: 4px;
  bottom: 4px;
  width: var(--span, 0%);
  min-width: 8px;
  margin-left: min(0px, calc((var(--span, 0%) - 8px) / 2));
  border-radius: 4px;
  box-shadow: 0 0 0 2px var(--bg-1), 0 0 16px color-mix(in srgb, var(--species-color, var(--accent)) 75%, transparent);
}

/* Mass extinctions: a hairline in the event's colour inside a wider, hoverable strip. */
.sp-life-event {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 14px;
  margin-left: -7px;
}

.sp-life-event::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 6px;
  width: 2px;
  border-radius: 1px;
  background: var(--event-color);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--bg-1) 70%, transparent);
}

/* "Now": a pointer above the rail and a hair through it, light enough not to hide a short span. */
.sp-life-now {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: color-mix(in srgb, var(--text-1) 55%, transparent);
  pointer-events: none;
}

.sp-life-now::before {
  content: '';
  position: absolute;
  top: -7px;
  left: -5px;
  border: 5px solid transparent;
  border-top: 7px solid var(--text-1);
  border-bottom-width: 0;
}

.sp-life-eras {
  display: flex;
  margin-top: 5px;
}

.sp-life-era {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-align: center;
  font-size: var(--fs-micro);
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-3);
}

.sp-life-era + .sp-life-era {
  box-shadow: inset 1px 0 0 var(--line-strong);
}

.sp-life-era span {
  display: inline-block;
}

.sp-life-story {
  margin-top: var(--s-3);
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--text-1);
}

/* ---- Prominence sparkline ---------------------------------------------------- */

.sp-spark {
  position: relative;
  height: 60px;
  cursor: crosshair;
  touch-action: pan-y;
}

.sp-spark-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.sp-spark-area {
  fill: var(--species-color, var(--accent));
  opacity: 0.13;
}

.sp-spark-line {
  fill: none;
  stroke: var(--species-color, var(--accent));
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

/* Baseline (y = 38 of 40 in the SVG's viewBox). */
.sp-spark::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 95%;
  height: 1px;
  background: var(--line-strong);
}

.sp-spark-event,
.sp-spark-now,
.sp-spark-cursor {
  position: absolute;
  top: 0;
  bottom: 5%;
  pointer-events: none;
}

.sp-spark-event {
  width: 2px;
  margin-left: -1px;
  border-radius: 1px;
  background: var(--event-color);
  opacity: 0.85;
}

/* Same "now" pointer as on the timeline bar above. */
.sp-spark-now {
  width: 1px;
  background: color-mix(in srgb, var(--text-1) 70%, transparent);
}

.sp-spark-now::before {
  content: '';
  position: absolute;
  top: -1px;
  left: -4px;
  border: 4.5px solid transparent;
  border-top: 6px solid var(--text-1);
  border-bottom-width: 0;
}

.sp-spark-cursor {
  width: 1px;
  background: var(--text-2);
  opacity: 0;
}

.sp-spark-peak {
  position: absolute;
  width: 8px;
  height: 8px;
  margin: -4px 0 0 -4px;
  border-radius: 50%;
  background: var(--species-color, var(--accent));
  box-shadow: 0 0 0 2px var(--bg-1);
  pointer-events: none;
}

.sp-spark-axis {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: var(--fs-small);
  color: var(--text-3);
}

.sp-spark-readout {
  font-size: var(--fs-small);
  color: var(--text-2);
}

.sp-caption {
  margin-top: var(--s-2);
  font-size: var(--fs-small);
  line-height: 1.45;
  color: var(--text-3);
}

/* ---- Lineage ----------------------------------------------------------------- */

.sp-crumbs {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 0;
  margin-left: -7px;
}

.sp-crumbs li {
  display: inline-flex;
  align-items: center;
}

/* The separator trails its crumb, so a wrapped line never starts with one. */
.sp-crumbs li:not(:last-child)::after {
  content: '›';
  color: var(--text-4);
}

.sp-crumb {
  padding: 3px 7px;
  border: 1px solid transparent;
  border-radius: var(--r-pill);
  background: transparent;
  color: var(--text-3);
  font-size: var(--fs-body);
  transition: background var(--dur-1) var(--ease), color var(--dur-1) var(--ease), border-color var(--dur-1) var(--ease);
}

.sp-crumb.is-near {
  color: var(--text-1);
  font-weight: 600;
}

.sp-crumb:hover {
  background: var(--bg-3);
  color: var(--text-1);
}

.sp-crumb[aria-pressed='true'] {
  border-color: var(--accent-line);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.sp-clade {
  margin-top: var(--s-3);
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--line);
  border-radius: var(--r-2);
  background: var(--bg-2);
}

.sp-clade-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px var(--s-3);
}

.sp-clade-name {
  font-size: var(--fs-lead);
}

.sp-clade-sci,
.sp-clade-age {
  font-size: var(--fs-small);
  color: var(--text-3);
}

.sp-clade-age {
  margin-left: auto;
}

.sp-clade-text {
  margin-top: var(--s-1);
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--text-2);
}

.sp-kin {
  margin-top: var(--s-3);
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--text-1);
}

/* ---- Relatives --------------------------------------------------------------- */

#species-modal-relatives {
  list-style: none;
  display: grid;
  gap: var(--s-1);
}

.sp-rel {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto 12px;
  align-items: center;
  gap: var(--s-3);
  width: 100%;
  padding: 6px var(--s-3);
  border: 1px solid transparent;
  border-radius: var(--r-2);
  background: var(--bg-2);
  color: inherit;
  text-align: left;
  transition: background var(--dur-1) var(--ease), border-color var(--dur-1) var(--ease);
}

.sp-rel:hover {
  background: var(--bg-3);
  border-color: var(--line-strong);
}

.sp-rel-glyph {
  display: grid;
  place-items: center;
}

.sp-rel-name,
.sp-rel-kin,
.sp-rel-range {
  display: block;
}

.sp-rel-name {
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--text-1);
}

.sp-rel-kin,
.sp-rel-range {
  font-size: var(--fs-small);
  color: var(--text-3);
}

.sp-rel-side {
  text-align: right;
  white-space: nowrap;
}

.sp-rel-range {
  color: var(--text-2);
}

.sp-rel-live {
  display: none;
  font-size: var(--fs-small);
  color: var(--good);
}

.sp-rel.is-alive .sp-rel-live {
  display: block;
}

.sp-rel-go {
  display: grid;
  place-items: center;
  color: var(--text-4);
}

.sp-rel-note {
  margin-top: var(--s-3);
  font-size: var(--fs-small);
  line-height: 1.45;
  color: var(--text-3);
}

.sp-rel:hover .sp-rel-go {
  color: var(--text-1);
}

/* ---- Actions ----------------------------------------------------------------- */

.sp-actions-group {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
}

.dlg-actions .btn {
  min-height: 32px;
  padding: 0 var(--s-3);
  font-size: var(--fs-small);
}

.sp-action-tree {
  margin-left: auto;
  color: var(--accent-strong);
}

.sp-action-tree:hover {
  color: var(--accent-strong);
}

/* ========================================================================== */
/* 7. Help                                                                    */
/* ========================================================================== */

dialog.hp-dialog {
  width: min(720px, calc(100vw - 2 * var(--s-4)));
}

/* A fixed height so the card does not jump as the tabs change. */
.hp-dialog .dlg-surface {
  height: min(740px, 90vh);
  height: min(740px, 90dvh);
  max-height: none;
}

.hp-head {
  padding: var(--s-3) var(--s-3) var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--line);
}

.hp-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
}

#help-title {
  font-size: var(--fs-h2);
  outline: none;
}

.hp-tabs {
  margin-top: var(--s-3);
}

.hp-tabs .seg-btn {
  min-height: 30px;
}

.hp-dialog .dlg-scroll {
  padding: var(--s-4) var(--s-5) var(--s-5);
}

/* ---- How to read this -------------------------------------------------------- */

.hp-read {
  list-style: none;
  display: grid;
  gap: var(--s-4);
}

.hp-read-item {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  gap: var(--s-4);
  align-items: start;
}

.hp-art svg {
  width: 86px;
  height: 57px;
}

.hp-art {
  display: grid;
  place-items: center;
  height: 60px;
  border: 1px solid var(--line);
  border-radius: var(--r-2);
  background: var(--bg-2);
  color: var(--text-2);
}

.hp-read-title,
.hp-data-title {
  font-family: var(--font-display);
  font-size: var(--fs-lead);
  font-weight: 600;
  color: var(--text-1);
}

.hp-read-text p {
  margin-top: 2px;
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--text-2);
}

/* ---- Keyboard & mouse -------------------------------------------------------- */

.hp-kicker {
  margin: var(--s-4) 0 var(--s-2);
}

.hp-kicker:first-child {
  margin-top: 0;
}

.hp-keys {
  columns: 2;
  column-gap: var(--s-6);
}

.hp-key-row {
  display: grid;
  grid-template-columns: 112px minmax(0, 1fr);
  gap: var(--s-3);
  align-items: baseline;
  padding: 5px 0;
  break-inside: avoid;
}

.hp-key-row dt {
  display: flex;
  align-items: center;
  gap: 4px;
}

.hp-key-join {
  font-size: var(--fs-small);
  color: var(--text-3);
}

.hp-key-row dd,
.hp-gesture dd {
  font-size: var(--fs-body);
  line-height: 1.4;
  color: var(--text-2);
}

.hp-gestures {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--s-3) var(--s-6);
}

.hp-gesture dt {
  font-size: var(--fs-body);
  font-weight: 600;
  color: var(--text-1);
}

.hp-tip {
  margin-top: var(--s-4);
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--line);
  border-radius: var(--r-2);
  background: var(--bg-2);
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--text-2);
}

/* ---- About the data ---------------------------------------------------------- */

.hp-lead {
  margin-bottom: var(--s-4);
  font-size: var(--fs-lead);
  line-height: 1.55;
  color: var(--text-1);
}

.hp-data-row {
  padding: var(--s-3) 0;
  border-top: 1px solid var(--line);
}

.hp-data-row dt {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s-1) var(--s-3);
  margin-bottom: var(--s-1);
}

.hp-kind {
  padding: 2px 10px;
  font-size: var(--fs-small);
  letter-spacing: 0.01em;
}

.hp-kind.is-editorial {
  color: var(--accent-strong);
  border-color: var(--accent-line);
  background: var(--accent-soft);
}

.hp-data-row dd {
  font-size: var(--fs-body);
  line-height: 1.55;
  color: var(--text-2);
}

.hp-links {
  padding-top: var(--s-3);
  border-top: 1px solid var(--line);
  font-size: var(--fs-body);
  color: var(--text-3);
}

/* ---- Footer: motion preference ------------------------------------------------ */

.hp-foot {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s-2) var(--s-3);
  padding: var(--s-3) var(--s-5);
  border-top: 1px solid var(--line);
  background: var(--bg-2);
}

.hp-motion {
  background: var(--bg-1);
}

.hp-motion-note {
  flex: 1 1 220px;
  font-size: var(--fs-small);
  line-height: 1.4;
  color: var(--text-3);
}

/* ========================================================================== */
/* 8. Small screens & touch                                                   */
/* ========================================================================== */

/* Touch-only devices have no [ ] keys to advertise (the timeline's skip buttons step chapters). */
@media (hover: none) and (pointer: coarse) {
  #chapters-panel .panel-foot {
    display: none;
  }
}

/* Fingers need bigger targets than pointers. */
@media (pointer: coarse) {
  .dlg-close {
    width: 44px;
    height: 44px;
  }

  .sp-crumb {
    min-height: 36px;
    padding: 0 10px;
  }

  .sp-rel {
    min-height: 52px;
  }

  .dlg-actions .btn,
  .dlg-back {
    min-height: 40px;
  }
}

/* Phones: the corner panels become full-width sheets under the toolbar, and the dialogs become
   bottom sheets. */
@media (max-width: 720px) {
  #legend-panel,
  #chapters-panel {
    top: 0;
    left: 0;
    right: 0;
    width: auto;
    max-height: calc(100% - var(--s-5));
    border-width: 0 0 1px;
    border-radius: 0 0 var(--r-3) var(--r-3);
  }

  .dlg-close {
    width: 44px;
    height: 44px;
  }

  dialog.dlg,
  dialog.hp-dialog {
    width: 100vw;
    margin: auto 0 0;
  }

  dialog.dlg[open] {
    animation-name: sheet-in;
  }

  .dlg-surface,
  .hp-dialog .dlg-surface {
    max-height: 92vh;
    max-height: 92dvh;
    border-width: 1px 0 0;
    border-radius: var(--r-3) var(--r-3) 0 0;
  }

  .hp-dialog .dlg-surface {
    height: 92vh;
    height: 92dvh;
  }

  .dlg-scroll {
    padding: 0 var(--s-4) var(--s-4);
  }

  .hp-dialog .dlg-scroll {
    padding: var(--s-4);
  }

  .dlg-actions,
  .hp-foot {
    padding: var(--s-3) var(--s-4) calc(var(--s-3) + env(safe-area-inset-bottom, 0px));
  }

  .sp-head {
    grid-template-columns: 48px minmax(0, 1fr);
    gap: var(--s-3);
  }

  .sp-medallion {
    width: 48px;
    height: 48px;
  }

  #species-modal-title {
    font-size: var(--fs-h2);
  }

  .sp-facts {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--s-3);
  }

  /* Actions: the label and the tree link share a line, the three jumps sit under them. */
  .dlg-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
  }

  .sp-actions-group {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .sp-actions-group .btn {
    flex: 1 1 auto;
  }

  .sp-action-tree {
    grid-column: 2;
    grid-row: 1;
    margin: -6px calc(-1 * var(--s-3)) -6px 0;
  }

  .sp-clade-age {
    flex-basis: 100%;
    margin-left: 0;
  }

  /* Relatives: the range moves under the name instead of beside it. */
  .sp-rel {
    grid-template-columns: 20px minmax(0, 1fr) 12px;
    row-gap: 0;
  }

  .sp-rel-side {
    grid-column: 2;
    grid-row: 2;
    display: flex;
    gap: var(--s-3);
    text-align: left;
  }

  .sp-rel-go {
    grid-column: 3;
    grid-row: 1 / span 2;
  }

  .hp-head {
    padding-left: var(--s-4);
  }

  .hp-tabs {
    display: flex;
  }

  .hp-tabs .seg-btn {
    flex: 1 1 auto;
    min-height: 36px;
    padding: 0 var(--s-1);
    white-space: nowrap;
  }

  .hp-read-item {
    grid-template-columns: 72px minmax(0, 1fr);
    gap: var(--s-3);
  }

  .hp-art svg {
    width: 68px;
    height: 45px;
  }

  .hp-keys {
    columns: 1;
  }

  .hp-gestures {
    grid-template-columns: minmax(0, 1fr);
  }
}

@keyframes sheet-in {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
}

/* Short windows (landscape phones): give the dialogs all the height there is. */
@media (max-height: 520px) {
  .dlg-surface,
  .hp-dialog .dlg-surface {
    max-height: 96vh;
    max-height: 96dvh;
  }
}
