/* sm-fix.css v2 — small-mammal-register patches, loaded AFTER css/style.css.
   Sections 1–4 adapted from the mangrove register's mangrove-fix.css (same
   author, battle-tested mobile/contrast fixes), extended for the ask panel.
   Sections 5+ implement the improvement backlog the mangrove map never got:
   print view, chapter nav, annotations, active-chapter marking, contrast
   preferences, hover popups, data-integrity banner. */

/* ---------- 1. Mobile control strip: layers + ask never overlap the rail ---------- */
@media (max-width: 768px) {
  /* One predictable strip directly under the map, above the story. */
  #legend-toggle {
    position: fixed;
    left: 12px;
    right: auto;
    top: calc(58svh - 46px) !important;
    top: calc(58dvh - 46px) !important;
    bottom: auto;
    z-index: 620 !important;
    min-height: 44px;
    padding: 10px 14px;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
  }
  #ask-toggle {
    position: fixed;
    right: 12px;
    left: auto;
    top: calc(58svh - 46px);
    top: calc(58dvh - 46px);
    bottom: auto;
    z-index: 620;
    min-height: 44px;
    padding: 10px 14px;
    font-size: 13px;
    display: inline-flex;
    align-items: center;
  }
  #legend-toggle:has(+ #legend:not([hidden])) {
    top: calc(100vh - max(12px, env(safe-area-inset-bottom)) - min(58vh, 500px) - 52px) !important;
    top: calc(100svh - max(12px, env(safe-area-inset-bottom)) - min(58svh, 500px) - 52px) !important;
    top: calc(100dvh - max(12px, env(safe-area-inset-bottom)) - min(58dvh, 500px) - 52px) !important;
    bottom: auto;
  }
  /* The chapter rail lives INSIDE the map and must never sit on the strip. */
  #rail {
    position: fixed;
    left: 8px;
    right: 8px;
    top: auto;
    bottom: calc(42svh + 56px);
    z-index: 480;
    max-width: calc(100vw - 16px);
    pointer-events: none;
  }
  #rail .rail-btn { pointer-events: auto; }
  /* Give the story room so the fixed strip never covers the first card. */
  .scrolly-steps { padding-top: 64px; }
  /* Layers sheet opens as a bottom drawer anchored to the screen edge. */
  #legend {
    position: fixed;
    top: auto;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: 12px;
    right: 12px;
    width: auto;
    max-height: min(58vh, 500px);
    max-height: min(58dvh, 500px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }
  /* Ask panel: bottom drawer too; the inline visualViewport script lifts it
     above the virtual keyboard. */
  #ask {
    position: fixed;
    top: auto;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: 12px;
    right: 12px;
    width: auto;
    max-height: min(58vh, 480px);
    max-height: min(58dvh, 480px);
  }
  /* One search entry point on mobile: the taxon selector. */
  #btn-search { display: none !important; }
  .map-search select {
    width: 100%;
    min-height: 46px;
    font-size: 15px; /* stops iOS zoom-on-focus */
  }
}

/* ---------- 2. Visible state while satellite tiles load ---------- */
#btn-basemap[data-loading="true"] { opacity: .85; cursor: progress; }
#basemap-status {
  position: fixed;
  left: 50%;
  top: max(12px, env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 950;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 999px;
  background: #06201aee;
  color: #eaf7f0;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(4, 24, 20, .35);
  pointer-events: none;
}
#basemap-status[hidden] { display: none; }
#basemap-status::before {
  content: "";
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid rgba(234, 247, 240, .35);
  border-top-color: #38d384;
  animation: bm-spin .8s linear infinite;
}
@keyframes bm-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  #basemap-status::before { animation: none; }
  .anno-pin { transition: none; }
}

/* ---------- 3. Selected place feedback ---------- */
.map-search select:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }

/* ---------- 4. Map tools above the story cards ---------- */
.map-tools {
  position: fixed;
  top: max(14px, calc(env(safe-area-inset-top) + 14px));
  z-index: 610;
}
@media (max-width: 900px) {
  .map-tools {
    left: max(10px, env(safe-area-inset-left));
    width: calc(100% - max(10px, env(safe-area-inset-left)) - max(120px, calc(env(safe-area-inset-right) + 120px)));
  }
  .map-tool-row { flex-wrap: wrap; }
}
@media (min-width: 769px) {
  .scrolly-map-wrap { z-index: auto; }
  #map { z-index: 0; }
  .map-tools {
    top: max(14px, calc(env(safe-area-inset-top) + 14px));
    left: max(14px, env(safe-area-inset-left));
  }
}

/* ---------- 5. Dark mode: ask input legibility (mangrove-fix §7 port) ---------- */
:root[data-theme="dark"] #ask-input,
:root:not([data-theme="light"]) #ask-input {
  background: #0f2a25;
  color: var(--ink);
  border-color: var(--line);
  caret-color: var(--ink);
  -webkit-text-fill-color: var(--ink);
}
:root[data-theme="dark"] #ask-input::placeholder,
:root:not([data-theme="light"]) #ask-input::placeholder {
  color: var(--muted);
}

/* ---------- 6. Dark theme: white text on bright teal fails WCAG AA
   (mangrove-fix §9 port — navy label measures 9.2:1 on #2dd4bf) ---------- */
:root[data-theme="dark"] .mpa-chip[aria-pressed="true"],
:root[data-theme="dark"] .ask-form button,
:root[data-theme="dark"] .suggested-q {
  color: var(--navy);
}
:root[data-theme="dark"] #taxon-chips .mpa-chip[aria-pressed="true"] { color: var(--navy); }

/* ---------- 7. Figures: white data cards so SVGs stay legible in both themes ---------- */
.step figure {
  margin: 18px 0;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
}
.step figure img { width: 100%; height: auto; display: block; }
.step figcaption {
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}

/* ---------- 8. Hedge / interpretation note ---------- */
.step .note {
  background: rgba(230, 85, 13, .08);
  border: 1px solid rgba(230, 85, 13, .35);
  border-left: 4px solid var(--hl);
  border-radius: 8px;
  padding: 12px 14px;
  font-size: 14px;
  line-height: 1.55;
  margin: 16px 0;
}

/* ---------- 9. Sample/census counter + data-integrity banner ---------- */
#shown { color: var(--ink); font-weight: 600; }
#data-error {
  position: fixed;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 960;
  max-width: min(560px, calc(100vw - 28px));
  padding: 12px 16px;
  border-radius: 10px;
  background: #7a1f00;
  color: #ffe9df;
  font-size: 13px;
  line-height: 1.5;
  box-shadow: 0 10px 30px rgba(4, 24, 20, .45);
}
#data-error[hidden] { display: none; }

/* ---------- 10. Taxon chips inside the legend drawer ---------- */
#taxon-chips { max-height: 160px; overflow-y: auto; }
#taxon-chips .mpa-chip { font-size: 12px; }
.mpa-chip:disabled, .leg-row:disabled, .leg-opacity input:disabled, .js-focus:disabled, .leg-reset:disabled {
  opacity: .5;
  cursor: progress;
}

/* ---------- 11. Anchor buttons in the invite row must look like buttons ---------- */
a.bulk-btn { text-decoration: none; display: inline-flex; align-items: center; }

/* ---------- 12. Chapter Prev/Next nav (backlog item 7) ---------- */
.ch-nav {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}
.ch-nav button {
  flex: 0 1 auto;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--paper);
  color: var(--teal);
  font: inherit;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  min-height: 40px;
}
.ch-nav button:hover { border-color: var(--teal); }
.ch-nav .ch-nav-next { margin-left: auto; }

/* ---------- 13. Active chapter marking: unmistakable, still readable ---------- */
.step { transition: border-color .3s ease, box-shadow .3s ease; }
.step.active {
  border-color: var(--hl);
  box-shadow: 0 10px 30px #0418142e, 0 0 0 1px var(--hl);
}

/* ---------- 14. Chapter annotation pin on the map ---------- */
.anno-pin {
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .45s ease, transform .45s ease;
  pointer-events: none;
  max-width: 240px;
}
.anno-pin.on { opacity: 1; transform: translateY(0); }
.anno-pin span {
  display: inline-block;
  padding: 6px 10px;
  border-radius: 999px;
  background: #06201ae6;
  color: #eaf7f0;
  font-size: 11.5px;
  font-weight: 700;
  line-height: 1.35;
  border: 1px solid rgba(234, 247, 240, .35);
  box-shadow: 0 6px 18px #04181447;
}

/* ---------- 15. Hover tooltip stays lightweight ---------- */
.hover-pop .maplibregl-popup-content {
  padding: 6px 9px;
  border-radius: 8px;
  font-size: 12px;
  box-shadow: 0 6px 18px #0418142e;
}
.pick-row {
  display: block;
  width: 100%;
  margin: 3px 0;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  text-align: left;
  cursor: pointer;
}
.pick-row:hover { border-color: var(--teal); }

/* ---------- 16. Legend "Only" buttons sit beside Zoom to layer ---------- */
.leg-links { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.leg-only { font-weight: 700; }

/* ---------- 17. High-contrast / forced-colors preferences ---------- */
@media (prefers-contrast: more) {
  .step { border-width: 2px; }
  .mpa-chip, .ch-nav button, .suggested-q { border-width: 2px; }
  #shown, .coords { font-weight: 700; }
}
@media (forced-colors: active) {
  .anno-pin span, #basemap-status, #toast { border: 1px solid CanvasText; }
  .step.active { outline: 2px solid Highlight; }
}

/* ---------- 18. Print view (backlog item 21): story + figures + sources,
   with a static-map note where the interactive map would be ---------- */
@media print {
  html, body { background: #fff !important; color: #000 !important; }
  .scrolly-map-wrap, #rail, .map-tools, #legend-toggle, #legend,
  #ask-toggle, #ask, .north-arrow, .kbd-hint, #chapter-progress,
  #return-story, #toast, #basemap-status, #data-error, .skip-link,
  .ch-nav, .invite-row, #download-sources, .dl-row, #copy-citation,
  .maplibregl-ctrl-attrib, .omnisearch-overlay { display: none !important; }
  .scrolly { display: block; }
  .scrolly-steps {
    margin-top: 0 !important;
    padding: 0 !important;
    pointer-events: auto;
  }
  .step, .step.sources-step {
    width: 100% !important;
    max-width: 100% !important;
    max-height: none !important;
    overflow: visible !important;
    opacity: 1 !important;        /* beats html.js .step:not(.active){opacity:0} */
    pointer-events: auto !important;
    transform: none !important;
    margin: 0 0 18px !important;
    border: 1px solid #999 !important;
    border-radius: 8px !important;
    box-shadow: none !important;
    background: #fff !important;
    color: #000 !important;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .step .inner { max-height: none !important; overflow: visible !important; }
  .step figure { border: 1px solid #ccc; break-inside: avoid; }
  details { display: block; }
  details > summary { display: none; }
  details > *:not(summary) { display: block; }
  details[open] > * { display: revert; }
  .citation-block { border: 1px solid #999; padding: 10px; }
  body::before {
    content: "Thirty Years in the Dark — East Africa's Small Mammal Record Register · Gerald Shikunyi Kombo (2026) · CC-BY 4.0";
    display: block;
    font-weight: 700;
    margin-bottom: 12px;
  }
  body::after {
    content: "Interactive map, downloads and the ask assistant: https://small-mammal-register.pages.dev/ — this printout shows the story text, figures and sources; the map itself is interactive-only.";
    display: block;
    margin-top: 16px;
    font-size: 11px;
    border-top: 1px solid #999;
    padding-top: 8px;
  }
  a { color: #000 !important; text-decoration: underline; }
  .src-ref sup { font-size: 9px; }
}

/* ---------- 19. The timeline instrument (the atlas's signature) ----------
   Docked bottom-centre between the scale bar (left) and the map controls
   (right). Bars are the locked decade census; the readout states exactly
   what is census and what is display sample. */
#time-deck {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(10px, env(safe-area-inset-bottom));
  z-index: 615;
  width: min(780px, calc(100vw - 330px));
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border: 1px solid rgba(234, 247, 240, .28);
  border-radius: 14px;
  background: #06201ad9;
  color: #eaf7f0;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  box-shadow: 0 10px 30px #04181447;
}
#time-deck[hidden] { display: none; }
#td-play, #td-all {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid rgba(234, 247, 240, .35);
  border-radius: 999px;
  background: #0f2a25;
  color: #eaf7f0;
  font: inherit;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}
#td-play[aria-pressed="true"] { background: #E69F00; color: #241300; border-color: #E69F00; }
#td-bars {
  flex: 1 1 auto;
  min-width: 110px;
  height: 46px;
  display: flex;
  align-items: flex-end;
  gap: 3px;
}
.td-bar {
  flex: 1 1 0;
  min-width: 5px;
  border: none;
  border-radius: 2px 2px 0 0;
  padding: 0;
  cursor: pointer;
  opacity: .78;
}
.td-bar:hover { opacity: 1; }
.td-bar.on { opacity: 1; outline: 2px solid #eaf7f0; outline-offset: 1px; }
.td-pre { background: #56B4E9; }
.td-mid { background: #E69F00; }
.td-post { background: #009E73; }
.td-read {
  flex: 0 1 210px;
  display: flex;
  flex-direction: column;
  gap: 1px;
  line-height: 1.3;
}
#td-label { font-size: 14px; font-weight: 800; letter-spacing: .02em; }
#td-count { font-size: 10.5px; color: #b9d4c9; }
#td-dark { font-size: 10.5px; color: #ffd9a0; font-weight: 700; }
#td-dark[hidden] { display: none; }
#td-slider {
  flex: 0 0 100px;
  accent-color: #E69F00;
  height: 30px;
}
@media (max-width: 900px) {
  #time-deck {
    left: 8px;
    right: 8px;
    width: auto;
    transform: none;
    bottom: 30px;
    flex-wrap: wrap;
  }
  #td-bars { order: 3; flex-basis: 100%; height: 38px; }
  .td-read { flex: 1 1 140px; }
  #td-slider { flex: 1 1 90px; }
  .maplibregl-ctrl-scale { display: none; }
}

/* ---------- 20. The blackout veil: night closes over the map during
   1970–1999 decades. Pure atmosphere — pointer-events none, and every
   number on the page still comes from the locked census. ---------- */
#blackout-veil {
  position: absolute;
  inset: 0;
  z-index: 40;
  pointer-events: none;
  opacity: 0;
  transition: opacity .9s ease;
  background: radial-gradient(ellipse at center, transparent 34%, rgba(2, 10, 8, .62) 100%);
}
#blackout-veil::after {
  content: "1970–1999 · THE BLACKOUT";
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 14px;
  border: 1px solid rgba(230, 159, 0, .55);
  border-radius: 999px;
  background: rgba(6, 32, 26, .8);
  color: #E69F00;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .14em;
  opacity: 0;
  transition: opacity .9s ease;
}
.scrolly-map-wrap.veil-on #blackout-veil { opacity: 1; }
.scrolly-map-wrap.veil-on #blackout-veil::after { opacity: 1; }
@media (prefers-reduced-motion: reduce) {
  #blackout-veil, #blackout-veil::after { transition: none; }
}

/* ---------- 21. Story caption moves to top-centre: the deck owns the
   bottom edge now ---------- */
#story-caption {
  top: max(12px, env(safe-area-inset-top));
  bottom: auto;
  left: 50%;
  transform: translateX(-50%);
}

/* ---------- 22. Print: the instrument is interactive-only ---------- */
@media print {
  #time-deck, #blackout-veil { display: none !important; }
  #story-caption { position: static; transform: none; }
}

/* ---------- 23. Night-canvas layout repairs ---------- */
/* The caption owns the top-centre now, so the taxon selector stays compact
   on desktop instead of stretching under it. */
@media (min-width: 769px) {
  .map-search { max-width: 360px; }
  #story-caption { max-width: min(560px, 46vw); }
}
/* Readouts sit on the map itself: give them night-legible contrast. */
:root[data-theme="dark"] #shown,
:root[data-theme="dark"] .coords {
  color: #eaf7f0;
  text-shadow: 0 1px 3px #041814;
}
/* Keyboard hints move out of the north arrow's corner. */
.kbd-hint {
  top: max(56px, calc(env(safe-area-inset-top) + 56px));
  right: 14px;
  bottom: auto;
  left: auto;
}

/* ---------- 24. Educational hovercard + data tables (v6) ---------- */
/* WCAG 1.4.13: hoverable (pointer can travel onto it), dismissible (Esc),
   persistent (no timer while hovered). Positioned by main.js; styles only. */
.taxon-card {
  position: fixed;
  z-index: 950;
  max-width: min(340px, calc(100vw - 24px));
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 8px 28px rgba(4, 24, 20, .28);
  font-size: 12.5px;
  line-height: 1.45;
  pointer-events: auto;
}
.taxon-card[hidden] { display: none; }
:root[data-theme="dark"] .taxon-card {
  background: #0b2b23f2;
  color: #eaf7f0;
  border-color: rgba(234, 247, 240, .28);
}
.hc-head { font-size: 13px; margin-bottom: 4px; }
.hc-head i { color: var(--muted); font-weight: 400; }
:root[data-theme="dark"] .hc-head i { color: #9fc4b6; }
.hc-census { margin-bottom: 6px; color: var(--teal); font-weight: 600; }
:root[data-theme="dark"] .hc-census { color: #56b4e9; }
.hc-note { margin: 4px 0; }
.hc-year {
  display: inline-block;
  min-width: 34px;
  padding: 0 4px;
  margin-right: 4px;
  border-radius: 4px;
  background: rgba(154, 106, 0, .14);
  color: var(--gold);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: center;
}
:root[data-theme="dark"] .hc-year { background: rgba(230, 159, 0, .18); color: #e69f00; }
.hc-dark { color: var(--hl); font-weight: 600; }
:root[data-theme="dark"] .hc-dark { color: #ff8a4c; }
.hc-src { margin-top: 6px; font-size: 11px; color: var(--muted); }
:root[data-theme="dark"] .hc-src { color: #9fc4b6; }

/* Figure & timeline data tables (Sources drawer) */
#table-host { overflow-x: auto; }
.records-table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0 18px;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
}
.records-table caption {
  caption-side: top;
  text-align: left;
  padding: 4px 0 8px;
  color: var(--muted);
  font-size: 12px;
}
.records-table th, .records-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
}
.records-table thead th {
  cursor: pointer;
  user-select: none;
  border-bottom: 2px solid var(--green);
  position: relative;
  padding-right: 20px;
}
.records-table thead th[aria-sort="ascending"]::after { content: " \2191"; position: absolute; right: 6px; }
.records-table thead th[aria-sort="descending"]::after { content: " \2193"; position: absolute; right: 6px; }
.records-table thead th[aria-sort="none"]::after { content: " \2195"; position: absolute; right: 6px; opacity: .45; }
.records-table tbody th { font-weight: 600; }
.records-table tfoot td { font-weight: 700; border-top: 2px solid var(--green); border-bottom: none; }
:root[data-theme="dark"] .records-table th,
:root[data-theme="dark"] .records-table td { border-color: rgba(234, 247, 240, .18); }
:root[data-theme="dark"] .records-table thead th { border-bottom-color: #56b4e9; }
:root[data-theme="dark"] .records-table tfoot td { border-top-color: #56b4e9; }

/* Field-notes drawer typography */
#field-notes-body ul { margin: 4px 0 12px; padding-left: 20px; }
#field-notes-body li { margin: 4px 0; }

/* Quick-jump landmark: no visual change, semantics only */
.taxon-search-landmark { display: contents; }

@media (prefers-reduced-motion: reduce) {
  .taxon-card { transition: none; }
}
@media (forced-colors: active) {
  .taxon-card { border: 1px solid CanvasText; background: Canvas; color: CanvasText; }
  .hc-year { background: Canvas; color: CanvasText; border: 1px solid CanvasText; }
}
@media print {
  .taxon-card { display: none !important; }
  .records-table th, .records-table td { white-space: normal; }
}
@media (max-width: 768px) {
  .taxon-card { max-width: calc(100vw - 16px); font-size: 13px; }
}
