:root {
  --paper: #f8f7f2;
  --paper-dark: #efede5;
  --paper-light: #fffefa;
  --ink: #24231f;
  --muted: #68645b;
  --line: #cbc6b9;
  --red: #c40000;
  --red-dark: #9d0000;
  --ochre: #9b6d13;
  --on-ink: #fff;
  --text-soft: #46433d;
  --text-note: #4d4942;
  --edge-stroke: #4c4943;
  --tree-stroke: #555149;
  --disabled: #99958b;
  --scroll-thumb: #aaa59a;
  --subtle-line: #ded9ce;
  --tree-line: #e4e0d6;
  --node-bg: rgba(248, 247, 242, 0.97);
  --label-bg: rgba(255, 254, 250, 0.92);
  --stage-chip-bg: rgba(248, 247, 242, 0.9);
  --control-bg: rgba(255, 254, 250, 0.96);
  --label-border: rgba(203, 198, 185, 0.8);
  --serif: Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
  --paper: #171816;
  --paper-dark: #20211e;
  --paper-light: #1c1d1a;
  --ink: #ece8df;
  --muted: #aaa59a;
  --line: #454640;
  --red: #ff6f65;
  --red-dark: #ff928a;
  --ochre: #d6aa4c;
  --on-ink: #171816;
  --text-soft: #ccc6bc;
  --text-note: #c2bcb2;
  --edge-stroke: #b9b3a8;
  --tree-stroke: #c0baaf;
  --disabled: #74766f;
  --scroll-thumb: #65665f;
  --subtle-line: #3b3c37;
  --tree-line: #383934;
  --node-bg: rgba(23, 24, 22, 0.97);
  --label-bg: rgba(28, 29, 26, 0.94);
  --stage-chip-bg: rgba(23, 24, 22, 0.92);
  --control-bg: rgba(28, 29, 26, 0.97);
  --label-border: rgba(104, 105, 97, 0.9);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  min-height: 100%;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
}

body {
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
}

button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

a {
  color: var(--red-dark);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

a:hover { color: var(--red); }

button:focus-visible,
a:focus-visible,
canvas:focus-visible,
input:focus-visible,
.relation-stage:focus-visible {
  outline: 2px solid var(--red);
  outline-offset: 3px;
}

mjx-container { margin: 0 !important; }

.topbar {
  display: grid;
  grid-template-columns: minmax(360px, 1fr) auto;
  gap: 28px;
  align-items: end;
  padding: 20px 24px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--paper-light);
}

.back-link {
  display: inline-block;
  margin-bottom: 8px;
  font-size: 12px;
  color: var(--muted);
}

h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: clamp(27px, 3vw, 38px);
  font-weight: normal;
  line-height: 1;
}

.intro {
  max-width: 790px;
  margin: 9px 0 0;
  color: var(--text-soft);
}

.toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 590px;
}

.n-switch, .view-switch {
  display: inline-flex;
  border: 1px solid var(--line);
  background: var(--paper);
}

.n-switch button,
.view-switch button,
#reset-view,
#theme-toggle {
  min-height: 34px;
  border: 0;
  border-right: 1px solid var(--line);
  border-radius: 0;
  padding: 6px 11px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
}

.n-switch button:last-child,
.view-switch button:last-child { border-right: 0; }

.n-switch button:hover,
.view-switch button:hover,
#reset-view:hover,
#theme-toggle:hover { background: var(--paper-dark); }

.n-switch button[aria-pressed="true"],
.view-switch button[aria-pressed="true"] {
  background: var(--ink);
  color: var(--on-ink);
}

.view-switch button:disabled {
  color: var(--disabled);
  cursor: not-allowed;
  text-decoration: line-through;
}

#reset-view,
#theme-toggle {
  border: 1px solid var(--line);
  background: var(--paper-light);
}

#theme-toggle {
  min-width: 78px;
}

.label-toggle, .face-toggle {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  min-height: 34px;
  padding: 0 3px;
  color: var(--muted);
  white-space: nowrap;
}

.label-toggle input, .face-toggle input { accent-color: var(--red); }
.face-toggle.disabled { opacity: 0.48; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 380px;
  align-items: start;
  min-height: calc(100vh - 138px);
}

.figure-panel {
  display: grid;
  grid-template-rows: auto minmax(560px, 1fr);
  min-width: 0;
  height: max(650px, calc(100vh - 154px));
  position: sticky;
  top: 0;
  border-right: 1px solid var(--line);
}

.figure-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--paper-dark);
}

.figure-head h2 {
  margin: 0;
  font-family: var(--serif);
  font-size: 20px;
  font-weight: normal;
}

.counts {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.stage {
  position: relative;
  min-height: 560px;
  overflow: hidden;
  background: var(--paper);
}

.relation-stage {
  position: absolute;
  inset: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-color: var(--scroll-thumb) transparent;
}

.relation-surface {
  position: relative;
  min-width: 100%;
  min-height: 100%;
  margin: 0 auto;
}

#relation-svg,
.diagram-label-layer {
  position: absolute;
  inset: 0;
}

#relation-svg { display: block; }
.diagram-label-layer { pointer-events: none; }

.relation-edge {
  fill: none;
  stroke: var(--edge-stroke);
  stroke-width: 1.35;
  vector-effect: non-scaling-stroke;
}

.relation-edge.incident { stroke: var(--ochre); stroke-width: 1.8; }
.relation-edge.selected { stroke: var(--red); stroke-width: 2.3; }
.arrowhead { fill: var(--edge-stroke); }

.relation-edge-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 15;
  cursor: pointer;
  pointer-events: stroke;
}

.diagram-node,
.diagram-edge-label {
  position: absolute;
  z-index: 2;
  transform: translate(-50%, -50%);
  border: 0;
  border-radius: 0;
  padding: 5px 7px;
  background: var(--node-bg);
  color: var(--ink);
  cursor: pointer;
  pointer-events: auto;
  font-family: var(--serif);
  white-space: nowrap;
}

.diagram-node { min-height: 31px; font-size: 15px; }
.diagram-node.full.n-5 { width: 212px; font-size: 12.5px; }
.diagram-node.full.n-6 { width: 232px; font-size: 11.5px; }
.diagram-node.compact { width: 44px; padding: 4px; }

.diagram-node:hover,
.diagram-edge-label:hover { color: var(--red-dark); }

.diagram-node.neighbour { color: var(--ochre); }
.diagram-node.selected { color: var(--red); }
.diagram-node.selected::after {
  position: absolute;
  right: 8px;
  bottom: 2px;
  left: 8px;
  height: 1px;
  background: currentColor;
  content: "";
}

.diagram-edge-label {
  z-index: 3;
  max-width: 430px;
  padding: 3px 6px;
  color: var(--text-soft);
  font-size: 13.5px;
}

.diagram-edge-label.selected { color: var(--red); }
.selected-edge-label {
  border-bottom: 1px solid var(--red);
  font-size: 12.5px;
}

#polytope-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: default;
  touch-action: pan-y;
}

#polytope-canvas.rotatable { cursor: grab; touch-action: none; }
#polytope-canvas.rotatable:active { cursor: grabbing; }
#polytope-canvas.over-vertex { cursor: pointer; }

.geometry-label-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  overflow: hidden;
  pointer-events: none;
}

.geometry-node-label,
.geometry-edge-label {
  position: absolute;
  transform: translate(-50%, -50%);
  border: 1px solid var(--label-border);
  background: var(--label-bg);
  color: var(--ink);
  font-family: var(--serif);
  line-height: 1;
  white-space: nowrap;
}

.geometry-node-label {
  min-width: 22px;
  padding: 2px 4px;
  font-size: 10px;
  text-align: center;
}

.geometry-node-label.neighbour { color: var(--ochre); }
.geometry-node-label.selected { border-color: var(--red); color: var(--red); }

.geometry-edge-label {
  z-index: 2;
  max-width: 260px;
  overflow: hidden;
  padding: 3px 5px;
  color: var(--red-dark);
  font-size: 11px;
  text-overflow: ellipsis;
}

.stage-note, .stage-mode, .legend {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  color: var(--muted);
  font-size: 11px;
}

.stage-mode {
  top: 12px;
  left: 14px;
  padding: 3px 5px;
  border-bottom: 1px solid var(--line);
  background: var(--stage-chip-bg);
  color: var(--text-soft);
  font-family: var(--serif);
  font-size: 13px;
}

.stage-note {
  left: 14px;
  bottom: 11px;
  padding: 2px 4px;
  background: var(--stage-chip-bg);
  font-family: var(--mono);
}

.legend {
  display: flex;
  gap: 13px;
  top: 12px;
  right: 14px;
  padding: 2px 4px;
  background: var(--stage-chip-bg);
}

.projection-control {
  position: absolute;
  z-index: 7;
  right: 14px;
  bottom: 13px;
  display: grid;
  grid-template-columns: auto minmax(130px, 220px) 48px;
  gap: 9px;
  align-items: center;
  padding: 7px 9px;
  border: 1px solid var(--line);
  background: var(--control-bg);
  color: var(--text-soft);
  font-family: var(--serif);
  font-size: 12px;
}

.projection-control input {
  width: min(22vw, 220px);
  accent-color: var(--red);
}

.projection-control output {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 10px;
  text-align: right;
}

.legend span { display: inline-flex; align-items: center; gap: 5px; }
.line-key { width: 18px; height: 2px; background: var(--ink); }
.line-key.selected { background: var(--red); }
.line-key.incident { background: var(--ochre); }

.notes {
  min-width: 0;
  background: var(--paper-light);
}

.notes section {
  padding: 17px 20px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 9px;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.055em;
  text-transform: uppercase;
}

#bracketing {
  margin: 0;
  overflow-wrap: anywhere;
  font-family: var(--serif);
  font-size: 19px;
  font-weight: normal;
  line-height: 1.3;
}

.coordinate-line {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.tree-wrap {
  position: relative;
  height: 174px;
  margin: 11px 0 1px;
  border-top: 1px solid var(--tree-line);
  border-bottom: 1px solid var(--tree-line);
}

#tree-svg, .tree-label-layer {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

#tree-svg line { stroke: var(--tree-stroke); stroke-width: 1.35; }
#tree-svg circle { fill: var(--red); }

.tree-label-layer { pointer-events: none; }
.tree-leaf-label {
  position: absolute;
  transform: translate(-50%, -50%);
  font-family: var(--serif);
  font-size: 15px;
}

.section-title {
  margin: 0 0 9px;
  font-family: var(--serif);
  font-size: 16px;
  font-weight: normal;
}

.plain-note, .small-note {
  margin: 0;
  color: var(--text-note);
  font-size: 13px;
}

.small-note { margin-top: 8px; font-size: 11px; color: var(--muted); }

.arrow-list {
  display: grid;
  border-top: 1px solid var(--subtle-line);
}

.arrow-list button {
  display: grid;
  grid-template-columns: 31px minmax(0, 1fr);
  gap: 7px;
  align-items: center;
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--subtle-line);
  border-radius: 0;
  padding: 8px 2px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
}

.arrow-list button:hover { color: var(--red-dark); }
.arrow-list button.selected { color: var(--red); }
.arrow-direction {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 9px;
  text-transform: uppercase;
}

.arrow-formula { min-width: 0; overflow-x: auto; font-family: var(--serif); }
.empty-arrows { margin: 0; color: var(--muted); font-size: 12px; }

.selected-arrow {
  min-height: 39px;
  overflow-x: auto;
  padding: 8px 9px;
  border-left: 2px solid var(--red);
  background: var(--paper);
  font-family: var(--serif);
  font-size: 13px;
}

.formula {
  margin: 10px 0;
  overflow-x: auto;
  padding: 8px 9px;
  border-left: 2px solid var(--red);
  background: var(--paper);
  font-family: var(--serif);
  font-size: 13px;
}

.source {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 11px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

@media (max-width: 1040px) {
  .topbar { grid-template-columns: 1fr; gap: 15px; }
  .toolbar { justify-content: flex-start; max-width: none; }
  .workspace { grid-template-columns: 1fr; }
  .figure-panel {
    height: auto;
    position: static;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .stage { height: min(72vh, 690px); }
  .notes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .notes section { border-right: 1px solid var(--line); }
  .notes section:last-child { grid-column: 1 / -1; border-right: 0; }
}

@media (max-width: 650px) {
  .topbar { padding: 16px; }
  .toolbar { gap: 7px; }
  .view-switch { order: 2; }
  .label-toggle, .face-toggle, #reset-view, #theme-toggle { order: 3; }
  .workspace { min-height: auto; }
  .figure-panel { grid-template-rows: auto 500px; }
  .figure-head { align-items: flex-start; flex-direction: column; gap: 5px; }
  .counts { white-space: normal; }
  .stage { min-height: 500px; height: 500px; }
  .legend { left: 10px; right: auto; }
  .legend { top: 43px; }
  .stage-note { max-width: calc(100% - 24px); }
  .projection-control {
    right: 10px;
    bottom: 38px;
    left: 10px;
    grid-template-columns: auto minmax(80px, 1fr) 42px;
  }
  .projection-control input { width: 100%; }
  .diagram-node.full.n-4 { width: 170px; font-size: 11.5px; }
  .diagram-edge-label { max-width: 230px; font-size: 10.5px; }
  .notes { display: block; }
  .notes section { border-right: 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
