:root {
  --bg: #f3f6fb;
  --panel: #ffffff;
  --panel-border: #d7dfeb;
  --text: #142033;
  --muted: #61758d;
  --accent: #315b9a;
  --accent-soft: #eaf1fb;
  --warning: #a14b16;
  --error-bg: #fff2f2;
  --error-border: #e8b4b4;
  --shadow: 0 10px 24px rgba(18, 32, 51, 0.08);
  --success-bg: #effaf4;
  --success-border: #b9e3c8;
  --trace-upstream-bg: #eef2ff;
  --trace-upstream-border: #7385d8;
  --trace-downstream-bg: #edf9f4;
  --trace-downstream-border: #2f8e66;
  --trace-overlap-bg: #f5efff;
  --trace-overlap-border: #8b63c9;
  --trace-origin-bg: #fff5e6;
  --trace-origin-border: #b87812;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: Inter, "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  min-height: 100vh;
}

button,
input,
select {
  font: inherit;
}

a {
  color: var(--accent);
}

img {
  max-width: 100%;
  display: block;
}

.app-shell {
  display: flex;
  gap: 0;
  height: 100vh;
  padding: 16px;
  --right-panel-width: 430px;
}

.left-panel,
.right-panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.left-panel {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.right-panel {
  width: var(--right-panel-width);
  min-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  overflow-y: auto;
}

.panel-header {
  padding: 18px 20px 0;
}

.panel-header h1 {
  margin: 0;
  font-size: 1.3rem;
}

.panel-header p {
  margin: 8px 0 0;
  color: var(--muted);
  line-height: 1.5;
}

.graph-canvas {
  flex: 1 1 auto;
  min-height: 0;
  margin: 12px;
  border: 1px solid #dbe4f0;
  border-radius: 12px;
  background: linear-gradient(to bottom, #fbfdff, #f7faff);
}

.controls-card,
.legend-card,
.stats-card,
.tree-card,
.details-card {
  border: 1px solid #dfe7f2;
  border-radius: 12px;
  background: #fbfdff;
  padding: 14px;
  display: grid;
  gap: 12px;
}

.control-label,
.control-subheading,
.section-hint,
.section-subhint {
  color: var(--muted);
}

.control-label {
  font-size: 0.88rem;
  font-weight: 600;
}

.control-subheading {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

#search-input,
.trace-select {
  width: 100%;
  border: 1px solid #cad7e8;
  border-radius: 10px;
  background: #fff;
  color: var(--text);
  padding: 10px 12px;
}

#search-input:focus,
.trace-select:focus {
  outline: none;
  border-color: #6f93cb;
  box-shadow: 0 0 0 3px rgba(49, 91, 154, 0.12);
}

.toggle-grid {
  display: grid;
  gap: 8px;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
}

.positions-panel,
.positions-actions {
  display: grid;
  gap: 10px;
}

.positions-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

button {
  border: 1px solid #c9d7ea;
  border-radius: 10px;
  background: #ffffff;
  color: var(--text);
  padding: 9px 12px;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}

button:hover:not(:disabled) {
  background: #f3f7fd;
  border-color: #9fb8dd;
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.primary-button {
  background: #e8f0fb;
  border-color: #9bb6df;
  color: #1d426f;
}

.secondary-button {
  background: #fff;
}

.danger-button {
  background: #fff5f5;
  border-color: #efc2c2;
  color: #8b2f2f;
}

.status-pill,
.positions-status {
  font-size: 0.9rem;
  line-height: 1.45;
}

.positions-status.is-unsaved {
  color: var(--warning);
}

.positions-status.is-saved {
  color: #245f41;
}

.section-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-heading-group {
  display: grid;
  gap: 3px;
}

.section-heading-row h2,
.details-section-heading {
  margin: 0;
  font-size: 1.02rem;
}

.section-hint,
.section-subhint {
  font-size: 0.82rem;
}

.legend-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.legend-row {
  display: grid;
  grid-template-columns: auto 28px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid #e8eef6;
}

.legend-row:first-child {
  border-top: none;
}

.legend-row.is-disabled {
  opacity: 0.55;
}

.legend-line {
  display: inline-block;
  width: 22px;
  height: 3px;
  border-radius: 999px;
}

.legend-label-text {
  overflow-wrap: anywhere;
}

.legend-count {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.stats-summary {
  font-size: 0.92rem;
  color: var(--muted);
  line-height: 1.45;
}

.stats-section {
  display: grid;
  gap: 8px;
}

.stats-subheading {
  font-size: 0.84rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.stats-row,
.orphan-item,
.cluster-item {
  border: 1px solid #e2eaf4;
  border-radius: 10px;
  background: #fff;
  padding: 10px 12px;
}

.stats-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.stats-row.is-hidden,
.orphan-item.is-hidden,
.cluster-item.is-hidden {
  opacity: 0.6;
}

.stats-row-main,
.stats-row-right,
.stats-card-header,
.stats-entry-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stats-row-main {
  min-width: 0;
}

.stats-row-right {
  margin-left: auto;
}

.stats-count {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.stats-name {
  overflow-wrap: anywhere;
}

.stats-list {
  display: grid;
  gap: 8px;
}

.stats-entry-badge,
.trace-node-kind,
.detail-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.stats-entry-badge {
  background: #eef2f8;
  color: var(--muted);
}

.stats-inline-button {
  padding: 6px 10px;
  font-size: 0.84rem;
}

.orphan-link,
.cluster-focus-button,
.trace-node-button,
.edge-jump-button {
  background: transparent;
  border: 1px solid #d7e1ef;
}

.cluster-meta,
.orphan-meta {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

.panel-divider {
  flex: 0 0 12px;
  cursor: col-resize;
  position: relative;
  touch-action: none;
}

.panel-divider::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  width: 4px;
  height: 100%;
  border-radius: 999px;
  background: #d7dfeb;
}

.app-shell.is-resizing {
  user-select: none;
  cursor: col-resize;
}

.app-shell.is-resizing .panel-divider::before,
.panel-divider:hover::before {
  background: #9fb8dd;
}

.app-shell.right-panel-narrow {
  --right-panel-width: 360px;
}

.app-shell.right-panel-default {
  --right-panel-width: 430px;
}

.app-shell.right-panel-wide {
  --right-panel-width: 560px;
}

.study-review-card {
  border: 1px solid #e2eaf4;
  border-radius: 12px;
  background: #ffffff;
  padding: 12px;
  display: grid;
  gap: 10px;
}

.study-review-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.study-review-title {
  font-size: 1rem;
  font-weight: 700;
  color: #1f334f;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.study-review-meta {
  margin-top: 3px;
  font-size: 0.85rem;
  color: var(--muted);
}

.study-subsection {
  border: 1px solid #e6edf7;
  border-radius: 10px;
  background: #fbfdff;
  padding: 10px;
  display: grid;
  gap: 8px;
}

.study-subsection-label {
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}

.study-text-block {
  white-space: pre-wrap;
  line-height: 1.55;
  color: #1d304a;
  overflow-wrap: anywhere;
}

.study-text-block.is-markdown {
  white-space: normal;
}

.study-text-block.is-markdown p {
  margin: 0 0 0.65em;
}

.study-text-block.is-markdown p:last-child {
  margin-bottom: 0;
}

.study-text-block.is-markdown code {
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.85em;
  background: #eef3fb;
  border-radius: 6px;
  padding: 1px 5px;
}

.study-text-block.is-markdown pre {
  margin: 0.5em 0;
  border: 1px solid #dbe7f5;
  border-radius: 8px;
  background: #ffffff;
  padding: 10px;
  overflow-x: auto;
}

.study-text-block.is-markdown ul,
.study-text-block.is-markdown ol {
  margin: 0.3em 0 0.65em 1.2em;
  padding: 0;
}

.study-text-block.is-markdown img {
  max-height: 280px;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  border: 1px solid #dbe7f5;
  border-radius: 8px;
  background: #fff;
  padding: 6px;
}

.study-latex-block {
  border: 1px solid #dbe7f5;
  background: #ffffff;
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
}

.study-equation-list {
  display: grid;
  gap: 8px;
}

.study-image-gallery {
  display: grid;
  gap: 8px;
}

.study-image-card {
  margin: 0;
  border: 1px solid #dbe7f5;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
}

.study-image-card img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 360px;
  object-fit: contain;
  background: #fff;
}

.study-image-fallback {
  padding: 10px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.study-review-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.hierarchy-tree {
  display: grid;
  gap: 10px;
}

.tree-group,
.tree-subgroup {
  display: grid;
  gap: 8px;
}

.tree-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.tree-toggle,
.tree-action {
  flex: 0 0 auto;
  min-width: 0;
}

.tree-toggle {
  width: 28px;
  padding-inline: 0;
}

.tree-label {
  flex: 1 1 auto;
  min-width: 0;
  text-align: left;
  padding: 8px 10px;
}

.tree-label:disabled {
  opacity: 1;
  cursor: default;
}

.tree-label.is-muted {
  opacity: 0.6;
}

.tree-label.is-selected {
  border-color: #88a8d8;
  background: #eef4fd;
}

.topic-inline,
.meta-inline {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.topic-swatch {
  width: 12px;
  height: 12px;
  flex: 0 0 auto;
  border-radius: 999px;
  border: 1px solid rgba(20, 32, 51, 0.18);
}

.concept-primary {
  font-size: 0.9rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.concept-secondary {
  color: var(--muted);
  font-size: 0.8rem;
  overflow-wrap: anywhere;
}

.tree-children {
  margin-left: 18px;
  padding-left: 12px;
  border-left: 1px solid #dce5f2;
  display: grid;
  gap: 8px;
}

.empty-tree,
.details-empty {
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.5;
  padding: 8px 0;
}

.details-panel {
  display: grid;
  gap: 14px;
}

.details-header {
  display: grid;
  gap: 8px;
}

.details-title {
  margin: 0;
  font-size: 1.15rem;
  overflow-wrap: anywhere;
}

.details-subtitle {
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.details-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.detail-chip {
  border: 1px solid #dbe5f2;
  background: #f6f9fd;
  color: #35577d;
}

.detail-chip.trace-origin {
  border-color: #e2bf7b;
  background: var(--trace-origin-bg);
  color: #8c5e0d;
}

.details-section {
  display: grid;
  gap: 8px;
}

.details-label {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.latex-block,
.code-fallback,
.note-block,
.trace-box {
  border: 1px solid #dee8f5;
  border-radius: 10px;
  background: #ffffff;
  padding: 12px;
}

.code-fallback {
  font-family: "Cascadia Code", Consolas, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note-block {
  line-height: 1.6;
}

.note-block p:first-child {
  margin-top: 0;
}

.note-block p:last-child {
  margin-bottom: 0;
}

.note-block code {
  background: #eef3fa;
  padding: 0.1em 0.35em;
  border-radius: 6px;
}

.note-block pre {
  background: #f6f9fd;
  border: 1px solid #e1eaf5;
  border-radius: 10px;
  padding: 10px 12px;
  overflow-x: auto;
}

.meta-grid {
  display: grid;
  gap: 8px;
}

.meta-item {
  border: 1px solid #e3eaf4;
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
}

.meta-item strong {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}

.diagram-gallery {
  display: grid;
  gap: 12px;
}

.diagram-card {
  border: 1px solid #dfe7f3;
  border-radius: 12px;
  overflow: hidden;
  background: #fff;
}

.diagram-card img {
  width: 100%;
  background: #f8fbff;
}

.diagram-caption {
  padding: 10px 12px;
  border-top: 1px solid #e5edf7;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.diagram-broken {
  border: 1px dashed #cdd8e8;
  border-radius: 10px;
  background: #f7faff;
  color: #51657d;
  padding: 16px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.edge-jump-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.trace-panel {
  display: grid;
  gap: 12px;
}

.trace-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.trace-header-text {
  display: grid;
  gap: 4px;
}

.trace-controls {
  display: grid;
  gap: 10px;
}

.trace-control-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.trace-control-field {
  display: grid;
  gap: 6px;
}

.trace-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.trace-summary-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.trace-summary-item {
  border: 1px solid #e3eaf4;
  border-radius: 10px;
  background: #fff;
  padding: 10px 12px;
}

.trace-summary-item strong {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}

.trace-note {
  border: 1px solid #e1e8f4;
  border-radius: 10px;
  background: #f8fbff;
  color: #415a78;
  padding: 10px 12px;
  line-height: 1.5;
  font-size: 0.92rem;
}

.trace-note.hidden-results {
  border-color: #d8d7f1;
  background: #f7f6ff;
}

.trace-focus-indicator {
  border: 1px solid #c9e0d1;
  background: var(--success-bg);
  color: #245f41;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  line-height: 1.45;
}

.trace-node-list {
  display: grid;
  gap: 8px;
}

.trace-node-row {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid #e3eaf4;
  border-radius: 10px;
  background: #fff;
  padding: 8px;
}

.trace-node-button {
  flex: 1 1 auto;
  min-width: 0;
  text-align: left;
  padding: 8px 10px;
}

.trace-node-title {
  font-weight: 600;
  overflow-wrap: anywhere;
}

.trace-node-subtitle {
  color: var(--muted);
  font-size: 0.82rem;
  margin-top: 3px;
  overflow-wrap: anywhere;
}

.trace-node-kind.upstream {
  border: 1px solid #cfd8fb;
  background: var(--trace-upstream-bg);
  color: #4d63b7;
}

.trace-node-kind.downstream {
  border: 1px solid #c5ead9;
  background: var(--trace-downstream-bg);
  color: #266d4e;
}

.trace-node-kind.overlap {
  border: 1px solid #ddcff7;
  background: var(--trace-overlap-bg);
  color: #6f47b0;
}

.trace-node-kind.hidden {
  border: 1px solid #d7dee9;
  background: #f3f6fb;
  color: var(--muted);
}

.app-message {
  border: 1px solid var(--error-border);
  background: var(--error-bg);
  color: #7a2121;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.92rem;
  line-height: 1.4;
}

.app-message.info {
  background: #eef5ff;
  border-color: #bfd4f5;
  color: #234b80;
}

.hidden {
  display: none !important;
}

@media (max-width: 1100px) {
  .app-shell {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
  }

  .panel-divider {
    display: none;
  }

  .left-panel {
    min-height: 58vh;
  }

  .right-panel {
    width: 100%;
    min-width: 0;
    padding: 12px;
  }

  .positions-actions,
  .trace-control-grid,
  .trace-summary-grid {
    grid-template-columns: 1fr;
  }

  .study-review-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .study-review-actions button {
    min-height: 44px;
    font-size: 1rem;
  }

  .study-review-card {
    padding: 10px;
  }

  .study-subsection {
    padding: 8px;
  }
}

@media (max-width: 700px) {
  .panel-header h1 {
    font-size: 1.12rem;
  }

  .panel-header p {
    font-size: 0.88rem;
  }

  .study-review-actions {
    grid-template-columns: 1fr;
  }
}

#panel-navigation {
  position: sticky;
  top: 0;
  z-index: 2;
}

.panel-menu-list {
  display: grid;
  gap: 8px;
}

.panel-menu-button {
  text-align: left;
  font-weight: 600;
}

.panel-view-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.panel-view-title {
  font-size: 0.96rem;
  font-weight: 700;
}
