:root {
  --bg: #0e1210;
  --bg-elev: #161c18;
  --panel: rgba(18, 26, 22, 0.92);
  --panel-border: rgba(196, 168, 110, 0.28);
  --gold: #c4a86e;
  --gold-dim: #8a7548;
  --text: #e8e4d8;
  --muted: #9a9588;
  --accent: #6b9b7a;
  --danger: #d4786a;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --font: "Noto Sans TC", system-ui, sans-serif;
  --serif: "Cormorant Garamond", "Noto Serif TC", serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
button, input { font-family: inherit; }
#app { position: relative; height: 100%; overflow: hidden; }

.view { position: absolute; inset: 0; display: none; flex-direction: column; }
.view.active { display: flex; }

/* —— Top bar —— */
.topbar, .detail-topbar {
  position: relative; z-index: 800;
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: 0.75rem 1.1rem;
  background: linear-gradient(180deg, rgba(14,18,16,0.96), rgba(14,18,16,0.75));
  border-bottom: 1px solid var(--panel-border);
  backdrop-filter: blur(10px);
}
.brand h1 {
  margin: 0; font-family: var(--serif); font-weight: 600;
  font-size: clamp(1.25rem, 3vw, 1.7rem); letter-spacing: 0.04em; color: var(--gold);
}
.subtitle { margin: 0.15rem 0 0; font-size: 0.78rem; color: var(--muted); }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.btn {
  appearance: none; border: 1px solid var(--panel-border);
  background: var(--bg-elev); color: var(--text);
  padding: 0.45rem 0.85rem; border-radius: 999px;
  cursor: pointer; font-size: 0.85rem; transition: border-color .15s, background .15s;
}
.btn:hover { border-color: var(--gold); background: #1c241e; }
.btn.ghost { background: transparent; }
.btn.icon {
  width: 2rem; height: 2rem; padding: 0; border-radius: 50%;
  font-size: 1.25rem; line-height: 1;
}

.search-wrap { position: relative; min-width: min(280px, 70vw); }
#search-input {
  width: 100%; padding: 0.5rem 0.9rem;
  border-radius: 999px; border: 1px solid var(--panel-border);
  background: var(--bg-elev); color: var(--text); outline: none;
}
#search-input:focus { border-color: var(--gold); }
.search-results {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px);
  margin: 0; padding: 0.35rem; list-style: none;
  background: var(--panel); border: 1px solid var(--panel-border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 280px; overflow: auto; z-index: 900;
}
.search-results li {
  padding: 0.55rem 0.7rem; border-radius: 8px; cursor: pointer;
}
.search-results li:hover, .search-results li[aria-selected="true"] {
  background: rgba(196,168,110,0.15);
}
.search-results .sci { font-style: italic; color: var(--muted); font-size: 0.8rem; }
.search-results .tag {
  display: inline-block; margin-left: 0.4rem;
  font-size: 0.65rem; padding: 0.1rem 0.4rem;
  border-radius: 4px; background: rgba(107,155,122,0.25); color: var(--accent);
}

/* —— Map —— */
#map { flex: 1; min-height: 0; background: #0a0e0c; }
.leaflet-container { background: #0a0e0c; font: inherit; }
.leaflet-control-attribution {
  background: rgba(14,18,16,0.8) !important; color: var(--muted) !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--gold-dim) !important; }

.insect-marker-wrap { background: transparent !important; border: none !important; }
.insect-marker {
  width: 40px; height: 40px; border-radius: 50%;
  background: radial-gradient(circle at 35% 28%, #f0e0b0, var(--gold) 42%, #5a4a28 78%);
  border: 2.5px solid rgba(255,255,255,0.7);
  box-shadow:
    0 0 0 4px rgba(196,168,110,0.35),
    0 6px 16px rgba(0,0,0,0.55),
    0 0 18px rgba(196,168,110,0.35);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; line-height: 1; color: #1a1508; font-weight: 700;
  transform-origin: center center;
  transition: transform .15s ease;
}
.insect-marker:hover { transform: scale(1.12); }
.insect-marker-emoji {
  display: block; filter: drop-shadow(0 1px 1px rgba(0,0,0,0.35));
  font-size: 1.05em;
}
.insect-marker.gbif {
  background: radial-gradient(circle at 35% 30%, #a8d4b5, var(--accent) 50%, #2a4a35);
}
.map-offline #map,
#map.map-offline { /* class on map el */ }
#map.map-ready { background: #0a0e0c; }
.leaflet-tooltip.insect-tooltip {
  background: rgba(18,26,22,0.95); color: var(--text);
  border: 1px solid var(--panel-border); border-radius: 8px;
  box-shadow: var(--shadow);
}
.leaflet-tooltip.insect-tooltip::before { border-top-color: rgba(18,26,22,0.95); }

.map-footer {
  position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%);
  z-index: 700; pointer-events: none;
  font-size: 0.75rem; color: var(--muted);
  background: rgba(14,18,16,0.75); padding: 0.3rem 0.8rem;
  border-radius: 999px; border: 1px solid var(--panel-border);
  white-space: nowrap; max-width: 95vw; overflow: hidden; text-overflow: ellipsis;
}
.status-pill {
  position: absolute; top: 76px; left: 50%; transform: translateX(-50%);
  z-index: 700; font-size: 0.8rem;
  background: var(--panel); border: 1px solid var(--panel-border);
  padding: 0.4rem 0.9rem; border-radius: 999px;
  opacity: 0; transition: opacity .25s; pointer-events: none;
}
.status-pill.show { opacity: 1; }
.status-pill.error { border-color: var(--danger); color: #f0b0a8; }

/* —— Drawer —— */
.drawer {
  position: absolute; top: 70px; right: 12px; bottom: 48px;
  width: min(320px, 92vw); z-index: 850;
  background: var(--panel); border: 1px solid var(--panel-border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.drawer[hidden] { display: none !important; }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; border-bottom: 1px solid var(--panel-border);
}
.drawer-head h2 { margin: 0; font-size: 1rem; color: var(--gold); font-family: var(--serif); }
.species-list {
  list-style: none; margin: 0; padding: 0.4rem; overflow: auto; flex: 1;
}
.species-list li {
  display: flex; gap: 0.7rem; align-items: center;
  padding: 0.55rem; border-radius: 10px; cursor: pointer;
}
.species-list li:hover { background: rgba(196,168,110,0.12); }
.species-list img {
  width: 48px; height: 48px; object-fit: cover; border-radius: 8px;
  border: 1px solid var(--panel-border);
}
.species-list .name { font-weight: 600; font-size: 0.9rem; }
.species-list .sci { font-style: italic; color: var(--muted); font-size: 0.75rem; }
.drawer-note {
  margin: 0; padding: 0.6rem 1rem; font-size: 0.72rem; color: var(--muted);
  border-top: 1px solid var(--panel-border);
}

/* —— Detail —— */
.detail-topbar { flex-shrink: 0; }
.detail-titles { flex: 1; min-width: 0; }
.detail-titles h2 {
  margin: 0; font-family: var(--serif); font-size: clamp(1.1rem, 2.5vw, 1.5rem);
  color: var(--gold); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.detail-titles .sci { margin: 0.1rem 0 0; font-style: italic; color: var(--muted); font-size: 0.85rem; }

.detail-grid {
  flex: 1; min-height: 0;
  display: grid; grid-template-columns: 1.35fr 1fr; gap: 0;
}
.viewer-pane {
  position: relative; background:
    radial-gradient(ellipse at 50% 40%, #1a2420 0%, #0a0e0c 70%);
  min-height: 280px;
}
#viewer3d { position: absolute; inset: 0; }
.viewer-hint {
  position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
  margin: 0; font-size: 0.72rem; color: var(--muted);
  background: rgba(0,0,0,0.45); padding: 0.25rem 0.7rem; border-radius: 999px;
  pointer-events: none; white-space: nowrap;
}
.overlay-msg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(10,14,12,0.55); color: var(--text); font-size: 0.95rem;
  z-index: 5; pointer-events: none;
}
.overlay-msg.error { color: #f0b0a8; pointer-events: auto; text-align: center; padding: 1rem; }
.overlay-msg[hidden] { display: none !important; }

.info-pane {
  overflow: auto; padding: 1rem 1.15rem 1.5rem;
  background: var(--bg-elev); border-left: 1px solid var(--panel-border);
}
.meta-row {
  font-size: 0.8rem; color: var(--accent); margin-bottom: 0.85rem;
  letter-spacing: 0.02em;
}
.meta-row .sep { color: var(--muted); margin: 0 0.35rem; }
.lang-stack .blurb { margin-bottom: 1rem; }
.lang-stack .blurb h3 {
  margin: 0 0 0.35rem; font-size: 0.75rem; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--gold-dim); font-weight: 600;
}
.lang-stack .blurb p {
  margin: 0 0 0.85rem; line-height: 1.65; color: var(--text); font-size: 0.9rem;
}
body.lang-en .blurb[data-lang="zh"] { display: none; }
body:not(.lang-en) .blurb[data-lang="en"] { display: none; }

#mini-map {
  height: 180px; border-radius: 10px; overflow: hidden;
  border: 1px solid var(--panel-border); margin: 0.5rem 0 0.75rem;
  background: #0a0e0c;
}
.credit {
  font-size: 0.72rem; color: var(--muted); line-height: 1.45; margin: 0 0 0.5rem;
}
.credit a { color: var(--gold-dim); }
.honesty {
  font-size: 0.72rem; color: var(--gold-dim); margin: 0;
  padding-top: 0.5rem; border-top: 1px dashed var(--panel-border);
}

/* Mobile */
@media (max-width: 860px) {
  .detail-grid { grid-template-columns: 1fr; grid-template-rows: minmax(42vh, 1fr) auto; }
  .info-pane { border-left: none; border-top: 1px solid var(--panel-border); max-height: 48vh; }
  .topbar { padding: 0.6rem 0.75rem; }
  .map-footer { display: none; }
}
