:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #6b7280;
  --accent: #0e7490;
  --accent-dark: #155e75;
  --accent-soft: #e0f2f1;
  --chip: #e0f2fe;
  --chip-border: #7dd3fc;
  --elec: #b45309;
  --chem: #0e7490;
  --warn-bg: #fef3c7;
  --warn-ink: #92400e;
  --radius: 10px;
  font-size: 16px;
}

* { box-sizing: border-box; }

.hidden { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
}

header {
  background: linear-gradient(120deg, #164e63, #0e7490);
  color: #fff;
  padding: 1.6rem 1.5rem 1.3rem;
}
header h1 { margin: 0; font-size: 1.7rem; letter-spacing: 0.02em; }
.tagline { margin: 0.25rem 0 0; opacity: 0.9; font-size: 0.95rem; }

main { max-width: 1100px; margin: 0 auto; padding: 1.2rem 1rem 3rem; }

.panel {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
  padding: 1rem 1.2rem;
}

/* --- controls --- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 1.5rem;
  align-items: flex-end;
  margin-bottom: 1rem;
}
.control label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 0.25rem; }
.control select {
  padding: 0.45rem 0.6rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
  background: #fff;
  min-width: 200px;
}
.control.checkbox { padding-bottom: 0.55rem; }
.control.checkbox label { font-size: 0.95rem; color: var(--ink); }
.control.checkbox input { margin-right: 0.35rem; }

/* --- gene boxes --- */
.geneboxes {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.8rem;
  align-items: stretch;
  margin-bottom: 1rem;
}
.arrow { align-self: center; font-size: 1.6rem; color: var(--muted); }
.genebox h2 { margin: 0 0 0.6rem; font-size: 1.05rem; }
.dir-hint { font-weight: normal; font-size: 0.8rem; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-bottom: 0.5rem; }
.chips:empty { margin-bottom: 0; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--chip);
  border: 1px solid var(--chip-border);
  border-radius: 999px;
  padding: 0.15rem 0.4rem 0.15rem 0.65rem;
  font-size: 0.9rem;
  font-style: italic;
}
.chip button {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  color: var(--muted);
  padding: 0 0.15rem;
}
.chip button:hover { color: #b91c1c; }

.inputwrap { position: relative; }
.inputwrap input {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
}
.inputwrap input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.18);
  max-height: 260px;
  overflow-y: auto;
  z-index: 30;
}
.suggestions.hidden { display: none; }
.suggestion {
  padding: 0.45rem 0.7rem;
  cursor: pointer;
  font-style: italic;
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
}
.suggestion .nclass { font-style: normal; color: var(--muted); font-size: 0.8rem; }
.suggestion:hover, .suggestion.active { background: var(--accent-soft); }
.suggestion mark { background: none; color: var(--accent-dark); font-weight: 700; }
.no-match { padding: 0.45rem 0.7rem; color: var(--muted); font-size: 0.9rem; }

/* --- actions --- */
.actions { display: flex; gap: 0.7rem; align-items: center; margin-bottom: 1.2rem; flex-wrap: wrap; }
button.primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.4rem;
  font-size: 1rem;
  cursor: pointer;
}
button.primary:hover:not(:disabled) { background: var(--accent-dark); }
button.primary:disabled { opacity: 0.45; cursor: not-allowed; }
button.ghost {
  background: transparent;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
  color: var(--ink);
  cursor: pointer;
}
button.ghost:hover { border-color: var(--accent); color: var(--accent-dark); }
button.small { padding: 0.35rem 0.8rem; }

/* --- results --- */
.results-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 0.6rem;
}
#summary { font-size: 0.95rem; }
#summary b { font-size: 1.1rem; }
#summary .sub { color: var(--muted); font-size: 0.85rem; display: block; margin-top: 0.15rem; }

#warnings { margin-bottom: 0.6rem; }
.warn {
  background: var(--warn-bg);
  color: var(--warn-ink);
  border-radius: 6px;
  padding: 0.45rem 0.7rem;
  font-size: 0.85rem;
  margin-bottom: 0.35rem;
}

.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid #e5e7eb; vertical-align: top; }
th { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
th[data-sort] { cursor: pointer; user-select: none; }
th[data-sort]:hover { color: var(--accent-dark); }
td .cell-name { font-weight: 600; }
td .cell-class { display: block; color: var(--muted); font-size: 0.78rem; }
td .genes { font-style: italic; }
.conn-col, td.conn { white-space: nowrap; }
.badge {
  display: inline-block;
  border-radius: 6px;
  padding: 0.1rem 0.45rem;
  font-size: 0.82rem;
  margin: 0.1rem 0.15rem 0.1rem 0;
}
.badge.chem { background: #cffafe; color: var(--chem); }
.badge.elec { background: #fef3c7; color: var(--elec); }
.empty-result { color: var(--muted); padding: 1.2rem 0.4rem; }

/* --- neuron tooltip --- */
.cell-link {
  cursor: pointer;
  color: var(--accent-dark);
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.cell-link:hover { color: #0c4a5e; }

.tooltip {
  position: fixed;
  z-index: 100;
  max-width: 360px;
  background: #0f2530;
  color: #e5e7eb;
  border-radius: 10px;
  padding: 0.75rem 0.9rem;
  font-size: 0.82rem;
  line-height: 1.4;
  box-shadow: 0 12px 32px rgba(2, 6, 23, 0.45);
  pointer-events: none;
}
.tooltip h4 {
  margin: 0 0 0.3rem;
  font-size: 0.95rem;
  color: #7dd3fc;
}
.tooltip .tt-type { color: #fbbf24; font-weight: 600; }
.tooltip .tt-src { display: block; margin-top: 0.4rem; color: #94a3b8; font-size: 0.72rem; }

/* --- role badges --- */
.role {
  display: inline-block;
  border-radius: 5px;
  padding: 0.05rem 0.4rem;
  font-size: 0.7rem;
  font-weight: 600;
  margin-left: 0.35rem;
  vertical-align: middle;
  cursor: default;
}
.role.sensory { background: #dcfce7; color: #166534; }
.role.inter { background: #ede9fe; color: #5b21b6; }
.role.motor { background: #ffedd5; color: #9a3412; }
.role.other { background: #e2e8f0; color: #475569; }

/* --- wiring diagram --- */
.wiring { display: inline-block; vertical-align: middle; margin-right: 0.5rem; }
td.conn { min-width: 210px; }
.conn-flex { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }

/* --- notes & footer --- */
.notes { margin-top: 1.5rem; color: var(--muted); font-size: 0.85rem; }
.notes h3 { color: var(--ink); font-size: 0.95rem; margin-bottom: 0.4rem; }
.notes ul { margin: 0; padding-left: 1.2rem; }
.notes li { margin-bottom: 0.35rem; }
.notes a { color: var(--accent-dark); }

footer {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 1.2rem;
}

/* --- sortable headers --- */
.th-sort {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  color: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
}
.th-sort:hover { color: var(--accent-dark); }
.th-sort:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }
.sort-ind { font-size: 0.7rem; }

/* --- status / error / hint --- */
.status { font-size: 0.85rem; color: var(--muted); display: inline-flex; align-items: center; gap: 0.45rem; }
.status.busy::before {
  content: "";
  width: 0.9rem;
  height: 0.9rem;
  border-radius: 50%;
  border: 2px solid var(--chip-border);
  border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.error {
  background: #fee2e2;
  color: #991b1b;
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}
.hint { color: var(--muted); font-size: 0.8rem; margin: -0.5rem 0 1rem; }
.results-actions { display: flex; gap: 0.5rem; flex-shrink: 0; }
.license-note { margin-top: 0.8rem; font-size: 0.82rem; }
.license-note a { color: var(--accent-dark); }

/* --- gene column filter --- */
.genes-col .th-inner { display: inline-flex; align-items: center; gap: 0.3rem; }
.gf-btn {
  background: none;
  border: none;
  padding: 0.1rem;
  margin: 0;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  border-radius: 4px;
  line-height: 0;
}
.gf-btn:hover:not(:disabled) { color: var(--accent-dark); background: var(--accent-soft); }
.gf-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.gf-btn:disabled { opacity: 0.35; cursor: default; }
.gf-btn.on { color: var(--accent); }
.gene-filter {
  position: fixed;
  z-index: 90;
  min-width: 200px;
  max-width: 280px;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.18);
  padding: 0.6rem 0.7rem;
  font-size: 0.85rem;
}
.gene-filter .gf-head { font-weight: 600; color: var(--ink); margin-bottom: 0.4rem; }
.gene-filter .gf-actions { display: flex; gap: 0.6rem; margin-bottom: 0.4rem; }
.gene-filter .gf-actions button {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent-dark);
  cursor: pointer;
  font-size: 0.8rem;
  text-decoration: underline;
}
.gene-filter .gf-list { max-height: 240px; overflow-y: auto; display: flex; flex-direction: column; gap: 0.1rem; }
.gf-item { display: flex; align-items: center; gap: 0.45rem; cursor: pointer; padding: 0.15rem 0.2rem; border-radius: 4px; }
.gf-item:hover { background: var(--accent-soft); }
.gf-item input { margin: 0; }
.gf-item .gf-name { font-style: italic; flex: 1; }
.gf-item .gf-count { color: var(--muted); font-size: 0.75rem; }

@media (max-width: 800px) {
  .geneboxes { grid-template-columns: 1fr; }
  .arrow { transform: rotate(90deg); justify-self: center; }
  table { font-size: 0.85rem; }
  th, td { padding: 0.4rem 0.45rem; }
  td.conn { min-width: 150px; }
  .wiring { width: 96px; height: 42px; }
  .results-head { flex-direction: column; }
  .controls { gap: 0.7rem 1rem; }
  .control select { min-width: 160px; }
}
