:root {
  --paper: #f3efe6;
  --card: #faf8f3;
  --ink: #2a2620;
  --muted: #6f675b;
  --rule: #d9d1c2;
  --accent: #8a4b2a;
  --shadow: rgba(60, 45, 25, 0.28);
  --dialog: #fbf9f4;
  --serif: "Spectral", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #1c1a17;
    --card: #24211d;
    --ink: #ece6da;
    --muted: #a39a8b;
    --rule: #3a352e;
    --accent: #e0a37a;
    --shadow: rgba(0, 0, 0, 0.6);
    --dialog: #22201c;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --paper: #1c1a17;
  --card: #24211d;
  --ink: #ece6da;
  --muted: #a39a8b;
  --rule: #3a352e;
  --accent: #e0a37a;
  --shadow: rgba(0, 0, 0, 0.6);
  --dialog: #22201c;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.45;
}

.masthead, main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.masthead { padding-top: 40px; }

.overline {
  margin: 0;
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

h1 {
  margin: 4px 0 2px;
  font-family: var(--serif);
  font-weight: 600;
  font-size: clamp(30px, 5vw, 44px);
  letter-spacing: -0.01em;
}

.count { margin: 0; color: var(--muted); font-size: 14px; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 20px;
  margin: 22px 0 8px;
  padding: 12px 0;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
}

#search {
  flex: 1 1 220px;
  max-width: 320px;
  padding: 8px 12px;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 6px;
}
#search:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.toggle { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }

.scalebar { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.scalebar .bar { display: inline-block; height: 4px; background: var(--ink); border-radius: 1px; }

.grid {
  list-style: none;
  margin: 24px 0 64px;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 20px;
}

.grid > li { display: flex; }

.card {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 0;
  text-align: left;
  font: inherit;
  color: inherit;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px -8px var(--shadow); }
.card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.specimen {
  aspect-ratio: 9 / 16;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
}
.specimen img {
  height: 100%;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  filter: drop-shadow(0 4px 5px var(--shadow));
}

/* Every label has the same height: names get exactly two lines each (clamped,
   blank lines reserved), the meta line one. */
.label {
  padding: 10px 12px 12px;
  border-top: 1px dashed var(--rule);
}
.label .sci, .label .common {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
}
.label .sci { margin: 0; font-family: var(--serif); font-style: italic; font-size: 17px; line-height: 1.25; height: 2.5em; }
.label .common { margin: 2px 0 6px; font-size: 13px; line-height: 1.35; height: 2.7em; color: var(--muted); }
.label .meta { margin: 0; font-size: 12px; line-height: 1.4; color: var(--muted); font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.empty { color: var(--muted); }

/* ---------- detail dialog ---------- */

dialog {
  width: min(1100px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--dialog);
  color: var(--ink);
  overflow: auto;
}
dialog::backdrop { background: rgba(20, 16, 10, 0.55); }

.detail-nav {
  position: absolute;
  top: 8px;
  right: 10px;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 2px;
}
.nav-pos { min-width: 4.5em; text-align: center; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.nav, .close {
  width: 36px;
  height: 36px;
  font-size: 26px;
  line-height: 1;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}
.nav { font-size: 30px; padding-bottom: 4px; }
.close { margin-left: 8px; }
.nav:hover, .close:hover { background: var(--rule); }
.nav:focus-visible, .close:focus-visible { outline: 2px solid var(--accent); }

.detail-body { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); }

model-viewer {
  width: 100%;
  height: min(70vh, 620px);
  background: radial-gradient(circle at 50% 40%, var(--card), var(--paper));
  --poster-color: transparent;
}

.info { padding: 60px 24px 24px; border-left: 1px solid var(--rule); }
.info h2 { margin: 0; font-family: var(--serif); font-style: italic; font-weight: 600; font-size: 26px; line-height: 1.2; }
.info .common { margin: 4px 0 16px; color: var(--muted); }
.info dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0 0 20px; font-size: 14px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; }
.info dd small { display: block; color: var(--muted); font-size: 12px; }
.info h3 { margin: 0 0 4px; font-size: 13px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.info .note { margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.info a { color: var(--accent); }

.refs { list-style: none; margin: 0 0 20px; padding: 0; display: grid; gap: 10px; }
.refs li { display: grid; grid-template-columns: 64px 1fr; gap: 10px; align-items: center; font-size: 12px; color: var(--muted); }
.refs img { width: 64px; height: 64px; object-fit: cover; border-radius: 3px; background: var(--rule); }
.refs button { padding: 0; border: 0; background: none; cursor: zoom-in; border-radius: 3px; }
.refs button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; }
.button {
  display: inline-block;
  padding: 8px 14px;
  font: inherit;
  font-size: 14px;
  color: var(--accent);
  text-decoration: none;
  background: transparent;
  border: 1px solid var(--accent);
  border-radius: 6px;
  cursor: pointer;
}
.button:hover, .button[aria-expanded="true"] { background: var(--accent); color: var(--dialog) !important; }

/* Enlarged reference photos, below the model and info, to compare against the model. */
.photos {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 360px), 1fr));
  gap: 16px;
  padding: 16px;
  border-top: 1px solid var(--rule);
}
/* With photos open, shrink the viewer so model and photos fit on one screen. */
.detail-body.with-photos model-viewer { height: min(50vh, 520px); }
.photos figure { margin: 0; }
/* Fixed-height photo boxes: the layout is known before any image loads and stays the
   same from species to species. */
.photos img {
  display: block;
  width: 100%;
  height: min(42vh, 460px);
  object-fit: contain;
  background: var(--card);
  border-radius: 4px;
}
.photos figcaption { margin-top: 6px; font-size: 12px; line-height: 1.4; min-height: 2.8em; color: var(--muted); }
.photos a { color: var(--accent); }

@media (max-width: 760px) {
  .detail-body { grid-template-columns: 1fr; }
  model-viewer { height: 52vh; }
  .info { border-left: 0; border-top: 1px solid var(--rule); padding-top: 24px; }
  .detail-nav { position: sticky; top: 0; justify-content: flex-end; padding: 6px 8px; background: var(--dialog); border-bottom: 1px solid var(--rule); right: auto; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
}

/* Species notes (rendered from notes.md). */
.notes {
  grid-column: 1 / -1;
  padding: 8px 24px 28px;
  border-top: 1px solid var(--rule);
  font-size: 15px;
  line-height: 1.55;
}
.notes-inner { max-width: 760px; }
.notes h1 { display: none; }   /* the dialog header already names the species */
.notes h2 { margin: 24px 0 8px; font-family: var(--serif); font-size: 20px; font-weight: 600; }
.notes p, .notes ul { margin: 0 0 12px; }
.notes ul { padding-left: 20px; }
.notes li { margin-bottom: 4px; }
.notes a { color: var(--accent); }
.notes code { font-size: 13px; padding: 1px 4px; background: var(--card); border: 1px solid var(--rule); border-radius: 3px; }
.notes blockquote {
  margin: 0 0 14px;
  padding: 8px 14px;
  font-family: var(--serif);
  font-size: 16px;
  color: var(--ink);
  background: var(--card);
  border-left: 3px solid var(--accent);
}
.notes blockquote p { margin: 0; }
.notes table { border-collapse: collapse; margin: 0 0 14px; font-size: 14px; }
.notes th, .notes td { padding: 4px 12px 4px 0; text-align: left; border-bottom: 1px solid var(--rule); vertical-align: top; }
.notes th { color: var(--muted); font-weight: 600; }
.notes .status { color: var(--muted); }
