/* Sperm · Embryo 3D Viewer — minimal shell.
   Visual tokens mirror the MERFISH atlas (matching look & feel). */
:root {
  --bg:        #fafbfc;
  --surface:   #ffffff;
  --ink:       #0b0d13;
  --ink-2:     #3c4453;
  --ink-soft:  #8a91a2;
  --line:      #e7e9ef;
  --line-2:    #eef0f4;
  --accent:    #3b82f6;
  --sperm:     #ff2d95;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto,
          Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  /* per-stage accents */
  --zygote: #6366f1;
  --e2c:    #0ea5e9;
  --l2c:    #14b8a6;
  --oocyte: #f59e0b;
}
* { box-sizing: border-box; }
/* Explicit display rules below (grid/flex) would otherwise override the
   [hidden] attribute's UA display:none — force hidden to win. */
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ============================ Top bar ============================ */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: stretch;
  gap: 14px;
  padding: 0 14px;
  height: 60px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}
.brand {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding-right: 14px;
  border-right: 1px solid var(--line);
}
.brand-dot {
  width: 11px; height: 11px; border-radius: 50%;
  background: var(--sperm);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--sperm) 18%, transparent);
}
.brand-name { font-weight: 700; font-size: 14px; letter-spacing: .2px; white-space: nowrap; }
.brand-sub  { font-family: var(--mono); font-size: 11px; color: var(--ink-soft); white-space: nowrap; }

/* Embryo tab strip — horizontally scrollable */
.tabs {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: thin;
  padding: 8px 2px;
  mask-image: linear-gradient(90deg, transparent 0, #000 12px,
              #000 calc(100% - 12px), transparent 100%);
}
.tabs::-webkit-scrollbar { height: 6px; }
.tabs::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

.stage-group {
  display: flex;
  align-items: stretch;
  gap: 6px;
  flex: 0 0 auto;
  padding-right: 8px;
  border-right: 1px dashed var(--line);
}
.stage-group:last-child { border-right: none; }
.stage-tag {
  display: flex; align-items: center;
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .6px;
  color: var(--stage, var(--ink-soft));
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  padding: 2px 0;
  font-weight: 700;
  opacity: .85;
}
.tab {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1px;
  min-width: 62px;
  padding: 5px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .1s;
  line-height: 1.15;
}
.tab:hover { border-color: color-mix(in srgb, var(--stage) 45%, var(--line)); transform: translateY(-1px); }
.tab-label { font-size: 12.5px; font-weight: 650; white-space: nowrap; }
.tab-date  { font-family: var(--mono); font-size: 9.5px; color: var(--ink-soft); white-space: nowrap; }
.tab.active {
  background: color-mix(in srgb, var(--stage) 10%, var(--surface));
  border-color: var(--stage);
  box-shadow: 0 1px 0 var(--stage) inset, 0 0 0 1px var(--stage);
}
.tab.active .tab-label { color: color-mix(in srgb, var(--stage) 75%, var(--ink)); }

/* ============================ Stage / viewer ============================ */
.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}
.plot-host { position: absolute; inset: 0; }
.plot-host .plotly-graph-div { width: 100%; height: 100%; }

/* Floating control panel (gene picker + toggles + PCA plot) */
.controls {
  position: absolute;
  top: 12px; left: 12px;
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 8px 12px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px) saturate(1.3);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 4px 18px rgba(16, 24, 40, .06);
}
.controls-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* Mini section titles (shared by PCA + violin cards) */
.mini-title {
  font-family: var(--mono); font-size: 10.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .5px; color: var(--ink-2);
  margin-bottom: 2px;
}
.mini-sub { color: var(--ink-soft); font-weight: 500; text-transform: none; letter-spacing: 0; }

/* PCA scree plot inside the control panel */
.pca-panel { border-top: 1px solid var(--line); padding-top: 8px; }
.pca-plot { width: 260px; height: 116px; }
.ctl { display: flex; align-items: center; gap: 8px; }
.ctl-label {
  font-family: var(--mono);
  font-size: 10px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--ink-soft);
}
#gene-select {
  font-family: var(--sans);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  padding: 6px 26px 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  min-width: 190px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a91a2' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
}
#gene-select:focus { outline: none; border-color: var(--accent); }
/* Analysis-vector toggle chips */
.vec-toggles {
  display: flex; align-items: center; gap: 6px;
  padding-left: 12px; border-left: 1px solid var(--line);
}
.vec-btn {
  --vc: #666;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px 5px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  font-family: var(--sans);
  font-size: 12px; font-weight: 600; color: var(--ink-2);
  transition: border-color .15s, background .15s, color .15s;
  white-space: nowrap;
}
.vec-btn:hover { border-color: color-mix(in srgb, var(--vc) 45%, var(--line)); }
.vec-arrow {
  width: 18px; height: 2px; border-radius: 2px;
  background: var(--vc); position: relative; flex: 0 0 auto;
  opacity: .5; transition: opacity .15s;
}
.vec-arrow::after {                       /* arrowhead */
  content: ""; position: absolute; right: -1px; top: 50%;
  transform: translateY(-50%);
  border-left: 6px solid var(--vc);
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
}
.vec-btn.on {
  color: color-mix(in srgb, var(--vc) 78%, var(--ink));
  border-color: var(--vc);
  background: color-mix(in srgb, var(--vc) 10%, var(--surface));
  box-shadow: 0 0 0 1px var(--vc);
}
.vec-btn.on .vec-arrow { opacity: 1; }

.readout {
  display: flex; align-items: center; gap: 14px;
  font-size: 12px; color: var(--ink-2);
  padding-left: 12px;
  border-left: 1px solid var(--line);
}
.readout b { font-variant-numeric: tabular-nums; color: var(--ink); }
.muted-note { color: var(--ink-soft); font-style: italic; }
.readout .sperm-chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-family: var(--mono); font-size: 11px;
}
.readout .sperm-chip::before {
  content: ""; width: 9px; height: 9px; border-radius: 2px;
  background: var(--sperm); transform: rotate(45deg);
  box-shadow: 0 0 0 1.5px #fff, 0 0 0 2.5px var(--sperm);
}

/* Placeholder + loading */
.placeholder {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--ink-soft);
  pointer-events: none;
}
.ph-inner { text-align: center; max-width: 380px; padding: 0 20px; }
.ph-title { font-size: 20px; font-weight: 700; color: var(--ink-2); }
.ph-sub { margin-top: 8px; font-size: 13.5px; line-height: 1.5; }
.loading {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%);
  z-index: 4;
  display: flex; align-items: center; gap: 9px;
  padding: 8px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 12.5px; color: var(--ink-2);
  box-shadow: 0 4px 18px rgba(16, 24, 40, .08);
}
.spinner {
  width: 15px; height: 15px; border-radius: 50%;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================ Bottom drawer ============================ */
.drawer {
  position: fixed; left: 0; right: 0; bottom: 0;
  z-index: 6;
  display: flex; flex-direction: column;
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  backdrop-filter: blur(10px) saturate(1.3);
  border-top: 1px solid var(--line);
  box-shadow: 0 -6px 24px rgba(16, 24, 40, .08);
  transform: translateY(calc(100% - 40px));   /* collapsed: only handle shows */
  transition: transform .28s var(--ease);
}
.drawer[data-open="true"] { transform: translateY(0); }
.drawer-handle {
  flex: 0 0 40px;
  display: flex; align-items: center; gap: 12px;
  height: 40px; padding: 0 16px;
  border: none; background: transparent; cursor: pointer;
  font-family: var(--sans); color: var(--ink);
  border-bottom: 1px solid transparent;
}
.drawer[data-open="true"] .drawer-handle { border-bottom-color: var(--line); }
.drawer-title { font-size: 13px; font-weight: 700; }
.drawer-gene { color: var(--zygote); font-family: var(--mono); font-weight: 600; }
.drawer-hint { font-size: 11px; color: var(--ink-soft); }
.drawer-chevron {
  margin-left: auto; width: 10px; height: 10px;
  border-right: 2px solid var(--ink-soft); border-bottom: 2px solid var(--ink-soft);
  transform: rotate(-135deg);            /* points up when collapsed */
  transition: transform .28s var(--ease);
}
.drawer[data-open="true"] .drawer-chevron { transform: rotate(45deg); } /* down */
.drawer-body {
  flex: 1 1 auto;
  height: var(--drawer-h, 46vh);
  overflow-y: auto;
  padding: 10px 16px 18px;
}
/* Top-edge grabber to resize the drawer's height */
.drawer-resize {
  position: absolute; top: -3px; left: 0; right: 0; height: 9px;
  z-index: 8; cursor: ns-resize; touch-action: none;
  display: flex; align-items: center; justify-content: center;
}
.drawer-resize::before {
  content: ""; width: 54px; height: 4px; border-radius: 3px;
  background: var(--line); transition: background .15s, width .15s;
}
.drawer-resize:hover::before, .drawer-resize.dragging::before {
  background: var(--accent); width: 84px;
}
.drawer[data-open="false"] .drawer-resize { display: none; }
/* Centered, width-resizable group holding the two stacked violins. Width is
   driven by an inline style set from the right-edge drag; margin:0 auto keeps
   it centered, so growing the width expands both sides symmetrically. */
.violin-wrap {
  position: relative;
  width: 680px;
  max-width: 100%;
  margin: 0 auto;
  display: flex; flex-direction: column; gap: 14px;
}
.violin-card {
  flex: 0 0 auto;
  border: 1px solid var(--line-2);
  border-radius: 10px;
  padding: 8px 10px 4px;
  background: var(--surface);
}
.violin-plot { width: 100%; height: 200px; }
.violin-empty { color: var(--ink-soft); font-size: 12.5px; padding: 24px 8px; text-align: center; }

/* Right-edge drag handle to resize the violin group width */
.resize-handle {
  position: absolute; top: 0; right: -12px;
  width: 18px; height: 100%;
  display: flex; align-items: center; justify-content: center;
  cursor: ew-resize; touch-action: none; z-index: 2;
}
.resize-handle::before {
  content: ""; width: 4px; height: 54px; border-radius: 3px;
  background: var(--line); transition: background .15s, height .15s;
}
.resize-handle:hover::before, .resize-handle.dragging::before {
  background: var(--accent); height: 76px;
}

/* ============================ Right drawer (gene ranking) ============================ */
.rdrawer {
  position: fixed; top: 60px; right: 0; bottom: 0;
  width: var(--rdrawer-w, 320px);
  z-index: 7;
  display: flex; flex-direction: row;
  transform: translateX(calc(100% - 34px));   /* collapsed: only handle shows */
  transition: transform .28s var(--ease);
}
.rdrawer[data-open="true"] { transform: translateX(0); }
/* Left-edge grabber to resize the panel's width */
.rdrawer-resize {
  position: absolute; top: 0; left: -3px; width: 9px; height: 100%;
  z-index: 8; cursor: ew-resize; touch-action: none;
  display: flex; align-items: center; justify-content: center;
}
.rdrawer-resize::before {
  content: ""; width: 4px; height: 54px; border-radius: 3px;
  background: var(--line); transition: background .15s, height .15s;
}
.rdrawer-resize:hover::before, .rdrawer-resize.dragging::before {
  background: var(--accent); height: 84px;
}
.rdrawer[data-open="false"] .rdrawer-resize { display: none; }
.rdrawer-handle {
  flex: 0 0 34px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 12px 0; height: 100%;
  border: none; border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(8px);
  cursor: pointer; color: var(--ink-2);
  box-shadow: -4px 0 16px rgba(16,24,40,.05);
}
.rdrawer-handle-label {
  writing-mode: vertical-rl; transform: rotate(180deg);
  font-size: 12px; font-weight: 700; letter-spacing: .3px;
}
.rdrawer-chevron {
  width: 9px; height: 9px;
  border-left: 2px solid var(--ink-soft); border-bottom: 2px solid var(--ink-soft);
  transform: rotate(45deg);                    /* points left when collapsed */
  transition: transform .28s var(--ease);
}
.rdrawer[data-open="true"] .rdrawer-chevron { transform: rotate(-135deg); }
.rdrawer-body {
  flex: 1 1 auto;
  display: flex; flex-direction: column;
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  backdrop-filter: blur(10px) saturate(1.3);
  border-left: 1px solid var(--line);
  box-shadow: -6px 0 24px rgba(16,24,40,.08);
  min-width: 0;
}
.rdrawer-head { padding: 12px 14px 8px; border-bottom: 1px solid var(--line-2); }
.rdrawer-title { font-size: 14px; font-weight: 700; }
.rdrawer-desc { font-size: 11px; color: var(--ink-soft); margin-top: 3px; line-height: 1.4; }
/* Predicted sperm-location panel */
.predict-panel {
  padding: 10px 14px;
  border-bottom: 1px solid var(--line-2);
  background: color-mix(in srgb, #f59e0b 5%, var(--surface));
}
.predict-enable {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 700; cursor: pointer;
}
.predict-enable input { accent-color: #f59e0b; width: 15px; height: 15px; }
.predict-config { margin-top: 9px; display: flex; flex-direction: column; gap: 8px; }
.predict-ctl { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.predict-label { font-size: 11.5px; color: var(--ink-2); }
#predict-topn {
  font-family: var(--sans); font-size: 12px; font-weight: 600; color: var(--ink);
  padding: 3px 20px 3px 8px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--surface); cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a91a2' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 6px center;
}
.predict-loo-row { font-size: 12px; color: var(--ink-2); cursor: pointer; }
.predict-loo-row input { accent-color: #f59e0b; }
.predict-readout {
  font-size: 12px; color: var(--ink-2); line-height: 1.5;
  padding: 7px 9px; border-radius: 8px;
  background: color-mix(in srgb, #f59e0b 12%, var(--surface));
  border: 1px solid color-mix(in srgb, #f59e0b 35%, var(--line));
}
.predict-readout b { color: var(--ink); font-variant-numeric: tabular-nums; }
.predict-readout:empty { display: none; }
.predict-warn { color: #b45309; }

.rank-filter { display: flex; align-items: center; gap: 8px; margin-top: 9px; }
.rank-filter-label {
  font-family: var(--mono); font-size: 10px; text-transform: uppercase;
  letter-spacing: .5px; color: var(--ink-soft);
}
#rank-filter {
  flex: 1 1 auto; min-width: 0;
  font-family: var(--sans); font-size: 12px; font-weight: 600; color: var(--ink);
  padding: 5px 24px 5px 9px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238a91a2' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center;
}
#rank-filter:focus { outline: none; border-color: var(--accent); }

.rtabs { display: flex; gap: 4px; padding: 8px 10px 0; }
.rtab {
  flex: 1 1 0; min-width: 0;
  padding: 7px 6px; border: 1px solid var(--line); border-bottom: none;
  border-radius: 8px 8px 0 0; background: transparent;
  font-family: var(--mono); font-size: 10px; font-weight: 600; color: var(--ink-soft);
  cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.rtab.active { background: var(--surface); color: var(--ink); border-color: var(--line); }
.rank-list {
  flex: 1 1 auto; overflow-y: auto;
  padding: 6px 8px 16px; border-top: 1px solid var(--line);
}
.rank-row {
  display: grid;
  grid-template-columns: 26px 1fr 46px;
  grid-template-areas: "num gene std" "num bar bar";
  align-items: center; column-gap: 8px; row-gap: 3px;
  padding: 7px 8px; border-radius: 8px; cursor: pointer;
  border: 1px solid transparent;
}
.rank-row:hover { background: var(--line-2); }
.rank-row.current { border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.rank-num { grid-area: num; font-family: var(--mono); font-size: 11px; color: var(--ink-soft);
  text-align: right; font-variant-numeric: tabular-nums; }
.rank-gene { grid-area: gene; font-size: 13px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-std { grid-area: std; font-family: var(--mono); font-size: 10.5px; color: var(--ink-2);
  text-align: right; font-variant-numeric: tabular-nums; }
.rank-bar { grid-area: bar; height: 5px; border-radius: 3px; background: var(--line-2);
  overflow: hidden; }
.rank-bar > span { display: block; height: 100%; border-radius: 3px; }
.rank-empty { color: var(--ink-soft); font-size: 12.5px; padding: 24px 10px; text-align: center; }

@media (max-width: 720px) {
  .brand-sub { display: none; }
  .controls-row { flex-wrap: wrap; }
  .drawer-hint { display: none; }
  .rdrawer { width: 86vw; }
}
