:root {
  --bg: #F9F3EF;
  --surface: #ffffff;
  --text: #1B3C53;
  --text-light: #456882;
  --text-muted: #7A96A8;
  --accent: #456882;
  --accent-hover: #1B3C53;
  --accent-light: #7A96A8;
  --border: #D2C1B6;
  --border-light: #E8DDD6;
  --exotic: #9B3A3A;
  --tag-bg: #EDE5DC;
  --shadow: rgba(27, 60, 83, 0.08);
  --font-serif: 'Noto Serif TC', 'Noto Serif', Georgia, serif;
  --font-sans: system-ui, -apple-system, sans-serif;
  --font-hei: 'Noto Sans TC', system-ui, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-serif);
  line-height: 1.75;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ── */
.site-header {
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 10px var(--shadow);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 2rem;
  border-bottom: 1px solid var(--border-light);
}

.site-logo {
  font-size: 1rem;
  font-weight: 700;
  color: var(--accent);
  text-decoration: none;
  letter-spacing: 0.02em;
  font-family: var(--font-sans);
}
.site-logo:hover { color: var(--accent-hover); }

nav a {
  color: var(--text-light);
  text-decoration: none;
  font-size: 0.875rem;
  margin-left: 1.5rem;
  font-family: var(--font-sans);
  transition: color 0.15s;
}
nav a:hover { color: var(--accent); }

.header-main {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.75rem 2rem 0.6rem;
  flex-wrap: wrap;
}

.header-title {
  flex-shrink: 0;
}

.header-title h1 {
  font-size: 1.5rem;
  color: var(--accent);
  font-weight: 700;
  letter-spacing: 0.01em;
}

.version {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: var(--font-sans);
  margin-top: 0.05rem;
}
.exotic-note { color: var(--exotic); }

/* ── Search Controls ── */
.search-controls {
  flex: 1;
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  min-width: 0;
}

.search-wrapper {
  position: relative;
  flex: 1;
  min-width: 220px;
  max-width: 480px;
}

.search-icon {
  position: absolute;
  left: 0.7rem;
  top: 50%;
  transform: translateY(-50%);
  width: 15px;
  height: 15px;
  stroke: var(--text-muted);
  pointer-events: none;
}

#search-input {
  width: 100%;
  padding: 0.45rem 2.2rem 0.45rem 2.1rem;
  border: 1.5px solid var(--border);
  border-radius: 24px;
  background: #F9F3EF;
  font-family: var(--font-serif);
  font-size: 0.9rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}
#search-input:focus { border-color: var(--accent); }
#search-input::placeholder { color: var(--text-muted); }

.clear-btn {
  position: absolute;
  right: 0.6rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem 0.3rem;
  font-size: 0.75rem;
  line-height: 1;
}
.clear-btn:hover { color: var(--text); }

.filter-group {
  display: flex;
  gap: 0.45rem;
}

.filter-chip {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.7rem;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.82rem;
  color: var(--text-muted);
  background: var(--bg);
  transition: all 0.15s;
  user-select: none;
}
.filter-chip input { display: none; }
.filter-chip.active {
  border-color: var(--accent);
  color: var(--accent);
  background: #fdf5ee;
}
.filter-chip:hover { border-color: var(--accent-light); }

.stats {
  padding: 0.15rem 2rem 0.6rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: var(--font-sans);
}

/* ── Layout ── */
.page-layout {
  padding-left: 220px;
}

/* ── Sidebar ── */
.family-sidebar {
  width: 220px;
  position: fixed;
  left: 0;
  top: var(--header-h, 110px);
  height: calc(100vh - var(--header-h, 110px));
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.2rem 0.5rem 1.2rem 1rem;
  border-right: 1px solid var(--border-light);
  background: var(--bg);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  z-index: 50;
}

.sidebar-group {
  margin-bottom: 1rem;
}

.sidebar-group-title {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.04em;
  padding: 0.25rem 0.4rem 0.2rem;
  margin-bottom: 0.1rem;
  font-family: var(--font-sans);
  border-bottom: 1px solid var(--border-light);
}

.sidebar-family {
  display: block;
  font-size: 0.74rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.1rem 0.4rem 0.1rem 0.7rem;
  border-radius: 3px;
  transition: all 0.12s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-family: var(--font-hei);
  line-height: 1.65;
}
.sidebar-family:hover {
  color: var(--accent);
  background: var(--tag-bg);
}

.sidebar-group-en {
  font-size: 0.7rem;
  font-weight: 400;
  color: var(--accent-light);
  font-family: var(--font-sans);
  margin-left: 0.4rem;
}

.sidebar-order-lat {
  font-size: 0.62rem;
  color: var(--text-muted);
  font-family: var(--font-sans);
  margin-left: 0.3rem;
}

.sidebar-family-lat {
  font-size: 0.65rem;
  color: var(--text-muted);
  font-family: var(--font-sans);
  margin-left: 0.3rem;
}

.sidebar-order {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-light);
  font-family: var(--font-sans);
  padding: 0.22rem 0.4rem 0.05rem 0.4rem;
  margin-top: 0.25rem;
}

/* ── Main ── */
main {
  max-width: 880px;
  margin: 0 auto;
  padding: 2rem 1.5rem 3rem;
}

.loading {
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 0.9rem;
  padding: 2rem 0;
}

/* ── Higher Group (裸子植物 / 被子植物) ── */
.higher-group {
  margin-bottom: 3rem;
}

.higher-group-title {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
  padding-bottom: 0.4rem;
  margin-bottom: 1.5rem;
  border-bottom: 2px solid var(--border);
}

/* ── Family ── */
.family {
  margin-bottom: 1.75rem;
}

.family-header {
  display: flex;
  align-items: baseline;
  gap: 0.4rem;
  cursor: pointer;
  padding: 0.3rem 0;
  margin-bottom: 0.65rem;
  border-bottom: 1px solid var(--border-light);
  user-select: none;
}
.family-header:hover .family-title { color: var(--accent-hover); }

.family-num {
  font-size: 0.95rem;
  color: var(--text-muted);
  font-family: var(--font-sans);
  min-width: 1.4rem;
}

.family-title {
  font-size: 1.1rem;
  font-weight: 700;
  font-family: var(--font-hei);
  color: var(--accent);
  transition: color 0.15s;
}

.family-toggle {
  margin-left: auto;
  color: var(--text-muted);
  font-size: 0.75rem;
  transition: transform 0.2s;
  flex-shrink: 0;
}

.family.collapsed .family-toggle { transform: rotate(-90deg); }
.family.collapsed .family-content { display: none; }

/* ── Genus ── */
.genus {
  margin-bottom: 0.85rem;
  padding-left: 0.75rem;
}

.genus-title {
  font-size: 1.0rem;
  font-family: var(--font-hei);
  color: var(--text-light);
  margin-bottom: 0.25rem;
  font-weight: 500;
}

.genus-num {
  color: var(--text-muted);
  font-size: 0.85rem;
  font-family: var(--font-sans);
  margin-right: 0.1rem;
}

/* ── Species ── */
.species-list {
  list-style: none;
  padding-left: 0.75rem;
  font-size: 0.9rem;
}

.species-item {
  padding: 0.1rem 0;
}

.species-line {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
  flex-wrap: wrap;
  line-height: 1.6;
}

.species-item.has-refs .species-line {
  cursor: pointer;
}
.species-item.has-refs .species-line:hover .species-cn {
  color: var(--accent);
}

.sp-num {
  color: var(--text-muted);
  font-size: 0.82rem;
  font-family: var(--font-sans);
  min-width: 1.4rem;
  flex-shrink: 0;
}

.species-cn {
  font-weight: 500;
  font-family: var(--font-hei);
  transition: color 0.15s;
}

.species-lat { font-style: italic; }
.lat-abbr { font-style: normal; }

.species-author {
  color: var(--text-light);
  font-style: normal;
  font-size: 0.9em;
}

.exotic-mark {
  color: var(--exotic);
  font-size: 0.7rem;
  font-weight: 700;
  font-style: normal;
  vertical-align: super;
}

.ref-toggle-icon {
  color: var(--text-muted);
  font-size: 0.65rem;
  margin-left: 0.15rem;
  transition: transform 0.2s;
  flex-shrink: 0;
}
.species-item.refs-open .ref-toggle-icon { transform: rotate(180deg); }

/* ── References ── */
.species-refs {
  display: none;
  margin: 0.3rem 0 0.4rem 1.8rem;
  padding: 0.45rem 0.7rem;
  border-left: 2px solid var(--border);
  background: var(--tag-bg);
  border-radius: 0 4px 4px 0;
}
.species-item.refs-open .species-refs { display: block; }

.ref-item {
  font-size: 0.8rem;
  color: var(--text-light);
  margin-bottom: 0.3rem;
  line-height: 1.55;
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.ref-item:last-child { margin-bottom: 0; }

.ref-citation { font-style: italic; flex: 1; min-width: 0; }

.ref-doi {
  display: inline-block;
  flex-shrink: 0;
  color: var(--accent);
  text-decoration: none;
  font-style: normal;
  font-size: 0.75rem;
  font-family: var(--font-sans);
  border: 1px solid var(--border);
  padding: 0.1rem 0.4rem;
  border-radius: 3px;
  background: white;
  transition: all 0.15s;
  white-space: nowrap;
}
.ref-doi:hover {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

/* ── Highlight ── */
mark {
  background: #456882;
  color: #ffffff;
  border-radius: 2px;
  padding: 0 2px;
  font-style: inherit;
}

/* ── Utilities ── */
.hidden { display: none !important; }

.no-results {
  text-align: center;
  color: var(--text-muted);
  padding: 3rem 1rem;
  font-family: var(--font-sans);
  font-size: 0.95rem;
}

/* ── Footer ── */
footer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 1rem 2rem;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: var(--font-sans);
}
footer a { color: var(--accent); text-decoration: none; }
footer a:hover { text-decoration: underline; }
footer p + p { margin-top: 0.2rem; }

/* ── Family scroll offset ── */
.family {
  scroll-margin-top: calc(var(--header-h, 110px) + 1.5rem);
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .family-sidebar { display: none; }
  .page-layout { padding-left: 0; }
}

@media (max-width: 680px) {
  /* Header：標題在上，搜尋在下 */
  .header-main {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    padding: 0.7rem 1rem 0.5rem;
  }
  .header-title h1 { font-size: 1.2rem; }
  .version { font-size: 0.74rem; }

  /* 搜尋列：搜尋欄佔滿，篩選器緊跟在右 */
  .search-controls { flex-wrap: nowrap; gap: 0.4rem; }
  .search-wrapper { flex: 1; min-width: 0; max-width: none; }
  .filter-chip { padding: 0.22rem 0.45rem; font-size: 0.74rem; white-space: nowrap; }

  .stats { padding: 0.1rem 1rem 0.35rem; font-size: 0.74rem; }
  main { padding: 1rem 0.85rem 2rem; }

  /* 種名排版：學名+命名者+符號整組換行，不拆散 */
  .species-line { flex-wrap: wrap; }
  .species-suffix {
    display: inline-flex;
    align-items: baseline;
    gap: 0.2rem;
    white-space: nowrap;
  }

  /* 縮排收窄 */
  .genus { padding-left: 0.4rem; }
  .species-list { padding-left: 0.4rem; font-size: 0.86rem; }
  .species-refs { margin-left: 1rem; }

  .family-title { font-size: 1rem; }
  .genus-title { font-size: 0.93rem; }
}
