:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #516173;
  --panel-height: min(760px, calc(100vh - 132px));
  --panel-height-mid: min(720px, calc(100vh - 120px));
  --lesion-plot-size: clamp(240px, 28vh, 318px);
  --matrix-plot-size: clamp(220px, 26vh, 292px);
  --map-plot-height: clamp(178px, 21.5vh, 242px);
  --model-plot-min-height: clamp(172px, 21vh, 232px);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #ffffff;
  color: var(--text);
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

html {
  height: auto;
  overflow: auto;
}

.page {
  max-width: 2050px;
  margin: 0 auto;
  padding: 10px 12px 6px;
  display: grid;
  grid-template-columns: minmax(336px, 0.78fr) minmax(220px, 0.54fr) minmax(430px, 1.08fr) minmax(430px, 1.08fr);
  grid-template-areas:
    "lesion matrix fc corr"
    "footer footer footer footer";
  gap: 10px;
  align-items: stretch;
}

.loading-indicator {
  position: fixed;
  top: 10px;
  right: 14px;
  z-index: 30;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #0f172a;
  font-size: 0.95rem;
  padding: 6px 10px;
  border: 1px solid #e7eef5;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(6px);
  box-shadow: 0 3px 14px rgba(15, 23, 42, 0.06);
}

.app-switch-link {
  position: fixed !important;
  top: 10px;
  left: max(12px, calc((100vw - 2050px) / 2 + 12px));
  z-index: 40;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 8px 16px;
  border: 1px solid #0a84ff;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.96);
  color: #0a84ff;
  font-size: 0.92rem;
  font-weight: 700;
  line-height: 1;
  text-decoration: none;
  backdrop-filter: blur(6px);
  box-shadow: 0 3px 14px rgba(15, 23, 42, 0.06);
}

.app-switch-link:hover {
  background: #0a84ff;
  color: #ffffff;
}

.loading-indicator.hidden {
  display: none;
}

.spinner {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #cbd5e1;
  border-top-color: #0071e3;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.panel {
  background: var(--panel);
  border-radius: 18px;
  padding: 10px;
  border: 1px solid #f1f5f9;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  height: var(--panel-height);
  min-height: 0;
}

.panel-lesion {
  grid-area: lesion;
}

.panel-matrix {
  grid-area: matrix;
  position: relative;
  z-index: 3;
}

.panel-fc {
  grid-area: fc;
  position: relative;
  z-index: 1;
}

.panel-correlation {
  grid-area: corr;
}

.footer {
  grid-area: footer;
  position: fixed;
  left: 12px;
  bottom: 8px;
  z-index: 5;
  max-width: 700px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 0;
  color: #516173;
  font-size: 0.9rem;
  text-align: left;
}

.paper-link,
.github-link {
  color: inherit;
  text-decoration: none;
}

.paper-link {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.paper-link:hover,
.github-link:hover {
  text-decoration: underline;
}

h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

h3 {
  margin: 0 0 4px;
  font-size: 0.92rem;
  font-weight: 600;
  color: #182b3e;
}

.plot {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
}

.small-plot {
  margin-top: 2px;
}

.status {
  margin-bottom: 4px;
  color: #223443;
  font-size: 0.87rem;
  min-height: 18px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.controls-vertical {
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.controls-vertical select {
  width: 100%;
  max-width: 100%;
}

.controls select,
.controls button {
  border: 1px solid #d3dde7;
  border-radius: 8px;
  padding: 6px 10px;
  background: #ffffff;
  color: var(--text);
}

.controls button {
  cursor: pointer;
  background: #f8fbfd;
}

.panel-lesion .controls {
  margin-bottom: 2px;
}

.panel-lesion .status {
  margin-bottom: 2px;
}

.panel-lesion #prevalence-brain {
  flex: 0 0 var(--lesion-plot-size);
  height: var(--lesion-plot-size);
  width: 100%;
  max-width: var(--lesion-plot-size);
  align-self: center;
  margin-top: 4px;
  margin-bottom: 0;
}

.panel-matrix .status {
  display: none;
}

.panel-matrix #matrix-plot {
  margin-top: 2px;
  flex: 0 0 var(--matrix-plot-size);
  height: var(--matrix-plot-size);
  width: 100%;
  max-width: var(--matrix-plot-size);
  align-self: center;
}

.matrix-combine {
  margin-top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 100%;
  position: relative;
}

.matrix-dots {
  font-size: 1rem;
  font-weight: 700;
  line-height: 0.72;
  color: #1f2f3f;
  opacity: 0.72;
}

.matrix-divider-row {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 300px;
}

.matrix-divider {
  width: 100%;
  height: 2px;
  background: #1f2f3f;
  opacity: 0.78;
}

.matrix-plus {
  font-size: 1.28rem;
  font-weight: 700;
  line-height: 1;
  color: #1f2f3f;
  margin-left: 6px;
  margin-right: -2px;
}

.matrix-sum-plot {
  flex: 0 0 10px;
  height: 10px;
  width: 100%;
  max-width: 300px;
}

.matrix-arrow-elbow {
  width: 226px;
  height: 320px;
  position: absolute;
  right: -216px;
  top: -256px;
  z-index: 10;
  pointer-events: none;
}

.matrix-arrow-elbow svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.matrix-arrow-elbow polyline {
  fill: none;
  stroke: #1f2f3f;
  stroke-width: 2.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.map-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1 1 auto;
  min-height: 0;
}

.map-block {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.panel-fc .map-block-spec {
  margin-top: 38px;
}

.map-plot {
  height: var(--map-plot-height);
  min-height: var(--map-plot-height);
  flex: 0 0 var(--map-plot-height);
}

.spec-controls {
  margin-top: 0;
  margin-bottom: 4px;
}

#spec-test-toggle {
  width: 100%;
}

#spec-test-toggle .model-btn {
  min-width: 140px;
  padding: 6px 10px;
}

.model-layout {
  display: block;
  flex: 1 1 auto;
  min-height: 0;
}

.model-stack {
  display: grid;
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  min-height: 0;
}

.model-block {
  display: grid;
  grid-template-columns: 108px minmax(0, 1fr);
  grid-template-areas:
    "title title"
    "controls status"
    "controls plot";
  column-gap: 10px;
  row-gap: 4px;
  align-items: start;
  min-height: 0;
}

.model-plot {
  min-height: 0;
  width: min(66%, 360px);
  aspect-ratio: 1 / 1;
  align-self: stretch;
  justify-self: start;
  grid-area: plot;
}

.model-toggle {
  gap: 8px;
  flex-wrap: wrap;
}

.model-block > h3 {
  grid-area: title;
  margin-bottom: 2px;
}

.model-block > .factor-toggle-grid {
  grid-area: controls;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 108px;
  margin: 0;
}

.model-block > .status {
  grid-area: status;
  margin-bottom: 0;
}

.controls .model-btn {
  min-width: 108px;
  border-radius: 999px;
  border: 1px solid #0a84ff;
  background: #ffffff;
  color: #0a84ff;
  font-weight: 600;
  font-size: 0.72rem;
  line-height: 1.05;
  padding: 5px 10px;
  white-space: normal;
  text-wrap: balance;
  text-align: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.18s ease;
}

.controls .model-btn.active {
  background: #0a84ff;
  color: #ffffff;
}

.controls .model-btn:disabled {
  border-color: #d3dde7;
  color: #97a7b7;
  background: #f8fafc;
  cursor: not-allowed;
}

.factor-toggle-grid {
  gap: 6px;
  width: 100%;
}

.factor-toggle-grid .factor-btn {
  min-width: 0;
  width: 100%;
  min-height: 28px;
  padding: 3px 5px;
  font-size: 0.68rem;
}

.model-view-side {
  flex-direction: row;
  justify-content: flex-start;
  align-self: flex-start;
  margin: 2px 0 2px;
}

.model-view-side .model-btn {
  min-width: 90px;
}

.panel-lesion .model-view-side {
  margin: 0 0 8px;
}

@media (max-width: 1500px) {
  .page {
    grid-template-columns: repeat(2, minmax(420px, 1fr));
    grid-template-areas:
      "title title"
      "subtitle subtitle"
      "loading loading"
      "lesion matrix"
      "fc corr";
  }

  .panel {
    height: min(680px, calc((100vh - 170px) / 2));
  }
}

@media (max-height: 920px) {
  :root {
    --panel-height: calc(100vh - 196px);
    --panel-height-mid: calc(100vh - 186px);
    --lesion-plot-size: clamp(220px, 26vh, 292px);
    --matrix-plot-size: clamp(205px, 24vh, 270px);
    --map-plot-height: clamp(166px, 20vh, 222px);
    --model-plot-min-height: clamp(148px, 18vh, 196px);
  }
}

@media (max-width: 1180px) {
  .page {
    grid-template-columns: 1fr;
    grid-template-areas:
      "loading"
      "lesion"
      "matrix"
      "fc"
      "corr"
      "footer";
  }

  .panel {
    height: auto;
    min-height: 540px;
  }

  .model-block {
    grid-template-columns: 1fr;
    grid-template-areas:
      "title"
      "controls"
      "status"
      "plot";
    row-gap: 6px;
  }

  .model-view-side {
    flex-direction: row;
    justify-content: flex-start;
    margin: 0;
  }

  .model-block > .factor-toggle-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .model-plot {
    width: min(100%, 520px);
    aspect-ratio: 1 / 1;
    justify-self: stretch;
  }
}
