/* ── Base ────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #15130f;
  --bg2:      #201d18;
  --bg3:      #2b2721;
  --panel:    #191713;
  --border:   rgba(255, 248, 236, 0.14);
  --text:     #f5efe3;
  --muted:    #b8ae9f;
  --accent:   #2f6f91;
  --cyan:     #4aa4b8;
  --green:    #2e9b70;
  --orange:   #c96a3a;
  --yellow:   #d4a72c;
  --red:      #d05a55;
  --purple:   #8f72dc;
  --mono:     ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background:
    linear-gradient(90deg, rgba(255, 248, 236, 0.025) 1px, transparent 1px),
    linear-gradient(rgba(255, 248, 236, 0.025) 1px, transparent 1px),
    radial-gradient(circle at 18% 0%, rgba(201, 106, 58, 0.11), transparent 28rem),
    radial-gradient(circle at 86% 4%, rgba(47, 111, 145, 0.12), transparent 30rem),
    var(--bg);
  background-size: 34px 34px, 34px 34px, auto, auto, auto;
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* ── Loading overlay ─────────────────────────────────────────────────────── */
#loading-overlay {
  position: fixed; inset: 0; background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999; flex-direction: column; gap: 16px;
}
.spinner {
  width: 40px; height: 40px; border: 3px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--muted); font-family: var(--mono); font-size: 13px; }

/* ── Header ──────────────────────────────────────────────────────────────── */
#header {
  background: color-mix(in srgb, var(--bg2) 94%, transparent);
  border-bottom: 1px solid var(--border);
  padding: 11px 18px;
  display: flex; align-items: center; gap: 16px; flex-shrink: 0;
  z-index: 10;
}

#header h1 {
  font-size: 19px; font-weight: 750;
  color: var(--text);
  white-space: nowrap;
}

#org-count {
  font-family: var(--mono); font-size: 11px; color: var(--muted);
  background: var(--bg3); padding: 4px 9px; border-radius: 999px;
  border: 1px solid var(--border);
}

.header-spacer { flex: 1; }

/* ── Search ──────────────────────────────────────────────────────────────── */
#search-wrap { position: relative; }

#search-input {
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--text); border-radius: 7px; padding: 7px 12px;
  font-size: 13px; width: 260px; outline: none;
  font-family: inherit;
}
#search-input:focus { border-color: var(--accent); }

#search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 8px; z-index: 200; max-height: 280px; overflow-y: auto;
}

.search-result {
  padding: 8px 12px; cursor: pointer; font-size: 12px; font-family: var(--mono);
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.search-result:last-child { border-bottom: none; }
.search-result:hover,
.search-result.active {
  background: var(--bg3);
}
.search-result.active {
  outline: 1px solid var(--accent);
  outline-offset: -1px;
}
.search-result small { color: var(--muted); font-style: italic; margin-left: 6px; }

/* ── Button styles ───────────────────────────────────────────────────────── */
.btn {
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--text); padding: 7px 12px; border-radius: 7px;
  font-size: 12px; cursor: pointer; transition: background 0.15s, border-color 0.15s, color 0.15s;
  font-family: var(--mono); white-space: nowrap;
}
.btn:hover { background: var(--bg3); border-color: var(--accent); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: color-mix(in srgb, var(--accent) 75%, var(--text)); color: #f5efe3; }
.btn.primary:hover { background: #3f83a7; }
.btn.danger { border-color: var(--red); color: var(--red); }
.btn.danger:hover { background: #450a0a; }

/* ── Main layout ─────────────────────────────────────────────────────────── */
#main {
  display: grid;
  grid-template-columns: 1fr 380px;
  grid-template-rows: 1fr;
  flex: 1; overflow: hidden;
  gap: 0;
}

/* ── Left pane (tree / heatmap) ──────────────────────────────────────────── */
#left-pane {
  display: flex; flex-direction: column; overflow: hidden;
  border-right: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
}

/* ── Tabs ────────────────────────────────────────────────────────────────── */
#tabs {
  display: flex; gap: 0; border-bottom: 1px solid var(--border); flex-shrink: 0;
  padding: 0 12px;
}
.tab-btn {
  background: none; border: none; color: var(--muted);
  padding: 11px 16px; cursor: pointer; font-size: 13px;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
  transition: all 0.15s; font-family: var(--mono);
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--text); border-bottom-color: var(--cyan); background: color-mix(in srgb, var(--bg2) 55%, transparent); }

.tab-panel { flex: 1; overflow: hidden; display: flex; flex-direction: column; }
.tab-panel.hidden { display: none; }

/* ── Tree toolbar ────────────────────────────────────────────────────────── */
#tree-toolbar {
  display: flex; gap: 8px; padding: 9px 14px; flex-shrink: 0;
  border-bottom: 1px solid var(--border); align-items: center;
}
#tree-toolbar .toolbar-hint {
  color: var(--muted); font-size: 12px; font-family: var(--mono); flex: 1;
}

/* ── Tree container ──────────────────────────────────────────────────────── */
#tree-container {
  flex: 1; overflow: hidden; position: relative;
  background: transparent;
}

/* ── Heatmap toolbar ─────────────────────────────────────────────────────── */
#heatmap-toolbar {
  display: flex; gap: 6px; padding: 8px 12px; flex-shrink: 0;
  border-bottom: 1px solid var(--border); flex-wrap: wrap; align-items: center;
}
#heatmap-toolbar .toolbar-label { font-size: 11px; color: var(--muted); font-family: var(--mono); }
.group-btn {
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--muted); padding: 5px 10px; border-radius: 999px;
  font-size: 11px; cursor: pointer; transition: all 0.15s; font-family: var(--mono);
}
.group-btn:hover { border-color: var(--accent); color: var(--text); }
.group-btn.active { background: color-mix(in srgb, var(--accent) 30%, var(--bg2)); border-color: var(--accent); color: var(--text); }

#heatmap-container {
  flex: 1; overflow: auto; position: relative;
}

/* ── Right pane ──────────────────────────────────────────────────────────── */
#right-pane {
  display: flex; flex-direction: column; overflow: hidden;
  background: color-mix(in srgb, var(--bg2) 80%, transparent);
}

/* ── Selection bar ───────────────────────────────────────────────────────── */
#selection-bar {
  background: color-mix(in srgb, var(--bg2) 94%, transparent); border-bottom: 1px solid var(--border);
  padding: 10px 14px; flex-shrink: 0;
}
.selection-title { font-size: 10px; color: var(--muted); text-transform: uppercase;
                    letter-spacing: 0.08em; margin-bottom: 8px; font-family: var(--mono); }
.selection-slots { display: flex; gap: 8px; align-items: center; }

.org-slot {
  flex: 1; background: var(--bg3); border: 1px solid var(--border);
  border-radius: 6px; padding: 6px 10px; min-height: 42px;
  display: flex; flex-direction: column; justify-content: center; gap: 1px;
}
.org-slot span { font-size: 13px; color: var(--text); }
.org-slot small { font-size: 10px; color: var(--muted); font-style: italic; font-family: var(--mono); }
.org-slot .placeholder { color: var(--muted); font-style: italic; font-size: 12px; }

.slot-controls { display: flex; gap: 6px; margin-top: 8px; }

/* ── Stats panel ─────────────────────────────────────────────────────────── */
#right-scroll {
  flex: 1; overflow-y: auto; padding: 14px;
  scrollbar-width: thin; scrollbar-color: var(--border) var(--bg);
}

.stat-placeholder {
  text-align: center; color: var(--muted); font-size: 12px;
  padding: 40px 20px; font-family: var(--mono); line-height: 1.6;
}

.stats-header {
  font-family: var(--mono); font-size: 12px; margin-bottom: 12px;
  color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap;
}
.stats-org { color: var(--text); font-weight: 600; }
.stats-vs  { color: var(--border); }
.stats-time{ font-size: 10px; color: var(--muted); }

.stats-grid { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }

.stat-card {
  background: color-mix(in srgb, var(--bg2) 88%, transparent); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px;
}
.stat-title { font-size: 10px; color: var(--muted); text-transform: uppercase;
               letter-spacing: 0.06em; margin-bottom: 4px; font-family: var(--mono); }
.stat-value { font-size: 22px; font-weight: 700; font-family: var(--mono); margin-bottom: 3px; }
.stat-sub   { font-size: 10px; color: var(--muted); font-family: var(--mono); line-height: 1.4; }
.stat-sub b { color: var(--text); }

/* ── Lineage ─────────────────────────────────────────────────────────────── */
.stats-lineage {
  background: color-mix(in srgb, var(--bg2) 88%, transparent); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px; margin-bottom: 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.lineage-row { display: flex; align-items: center; flex-wrap: wrap; gap: 3px; }
.lineage-label { font-size: 10px; color: var(--muted); font-family: var(--mono);
                  margin-right: 4px; white-space: nowrap; }
.lineage-badge {
  font-size: 9px; font-family: var(--mono); padding: 2px 6px; border-radius: 3px;
  border: 1px solid transparent;
}
.lineage-sep { color: var(--border); font-size: 9px; }

.rank-domain    { background: rgba(76, 89, 160, 0.26); border-color: #7c6df0; color: #c8c2ff; }
.rank-kingdom   { background: rgba(103, 72, 157, 0.26); border-color: #8f72dc; color: #d7c8ff; }
.rank-phylum,
.rank-division  { background: #500724; border-color: #ec4899; color: #f9a8d4; }
.rank-class,
.rank-clade     { background: rgba(148, 74, 39, 0.28); border-color: #c96a3a; color: #f0b18c; }
.rank-order,
.rank-infraorder{ background: #422006; border-color: #eab308; color: #fde047; }
.rank-family    { background: rgba(35, 105, 76, 0.28); border-color: #2e9b70; color: #9edfc2; }
.rank-species   { background: rgba(44, 111, 152, 0.28); border-color: #2f6f91; color: #a8d2e5; }

/* ── Comparison section ──────────────────────────────────────────────────── */
#comparison-section { }
#comparison-section.hidden { display: none; }

.comp-title {
  font-size: 10px; color: var(--muted); text-transform: uppercase;
  letter-spacing: 0.08em; margin-bottom: 8px; font-family: var(--mono);
}

#spans-panel {
  background: color-mix(in srgb, var(--bg2) 88%, transparent); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px; margin-bottom: 14px;
  overflow-x: auto; min-height: 140px;
}

/* ── Add organism dialog ─────────────────────────────────────────────────── */
#add-dialog {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center; z-index: 500;
}
#add-dialog.hidden { display: none; }
.dialog-box {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: 12px; padding: 24px; width: 480px; max-width: 95vw;
  display: flex; flex-direction: column; gap: 12px;
}
.dialog-box h3 { font-size: 16px; color: var(--text); }
.dialog-box p  { font-size: 12px; color: var(--muted); }
.form-row { display: flex; gap: 8px; }
.form-field { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.form-field label { font-size: 11px; color: var(--muted); font-family: var(--mono); }
.form-field input, .form-field textarea, .form-field select {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 6px 10px; font-size: 12px; outline: none;
  font-family: var(--mono);
}
.form-field input:focus, .form-field textarea:focus { border-color: var(--accent); }
.form-field textarea { resize: vertical; min-height: 80px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ── Toast ───────────────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--bg2); border: 1px solid var(--border);
  color: var(--text); padding: 8px 18px; border-radius: 20px;
  font-size: 13px; font-family: var(--mono); z-index: 1000;
  opacity: 0; transition: opacity 0.3s; pointer-events: none;
}

/* ── Scrollbars ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 900px) {
  #main { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #right-pane { max-height: 340px; border-top: 1px solid var(--border); }
  #header h1 { font-size: 14px; }
  #search-input { width: 160px; }
}
