:root {
  --bg: #f4f5f7;
  --panel-bg: #ffffff;
  --border: #d5d8de;
  --text: #1f2328;
  --muted: #6b7280;
  --source: #2f6fdb;
  --target: #e07b24;
  --gap: 6px;
  --title-h: 28px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Four-panel layout ----------
   Left column 70% wide: scene 70% / controls 30% of the height.
   Right column 30% wide: source 50% / target 50% of the height. */
.app {
  display: grid;
  grid-template-columns: 7fr 3fr;
  gap: var(--gap);
  height: 100vh;
  height: 100dvh;
  padding: var(--gap);
}

.col {
  display: grid;
  gap: var(--gap);
  min-width: 0;
  min-height: 0;
}

.col-left {
  grid-template-rows: 7fr 3fr;
}

.col-right {
  grid-template-rows: 1fr 1fr;
}

/* ---------- Panels ---------- */
.panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

.panel-title {
  flex: 0 0 var(--title-h);
  display: flex;
  align-items: center;
  gap: 0.4em;
  padding: 0 10px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 13px;
}

.panel-source .panel-title {
  border-top: 3px solid var(--source);
}

.panel-target .panel-title {
  border-top: 3px solid var(--target);
}

.help-link {
  margin-left: auto;
  font-weight: 400;
  font-size: 12px;
  color: var(--source);
}

.axes-hint {
  font-weight: 400;
  color: var(--muted);
}

/* The body is the drawing surface later views size themselves to. */
.panel-body {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  color: var(--muted);
  font-style: italic;
  text-align: center;
  pointer-events: none;
}

/* ---------- 3D scene ---------- */
#scene-container canvas {
  display: block;
}

.label-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.label3d {
  padding: 0 3px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.75);
  color: var(--text);
  font: 12px system-ui, sans-serif;
  white-space: nowrap;
}

.label3d.axis {
  font-style: italic;
  font-weight: 600;
}

.label3d.frame {
  font-size: 11px;
  font-style: italic;
}

.label3d.source {
  color: var(--source);
  font-weight: 600;
}

.label3d.target {
  color: var(--target);
  font-weight: 600;
}

.label3d.projection {
  font-size: 11px;
}

.scene-buttons {
  position: absolute;
  top: 6px;
  right: 6px;
  display: flex;
  gap: 4px;
}

.scene-buttons button {
  padding: 2px 8px;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

/* ---------- 2D plane views ---------- */
.plane-svg {
  display: block;
  font: 11px system-ui, sans-serif;
}

.plane-svg .grid line {
  stroke: #eceef1;
  stroke-width: 1;
}

.plane-svg .axis-u,
.plane-svg .axis-v {
  stroke-width: 1.5;
}

.plane-svg .axis-u { stroke: #d93025; }
.plane-svg .axis-v { stroke: #188038; }
.plane-svg .axis-u-head,
.plane-svg .axis-u-text { fill: #d93025; }
.plane-svg .axis-v-head,
.plane-svg .axis-v-text { fill: #188038; }

.plane-svg .axis-name {
  font-size: 13px;
  font-style: italic;
  font-weight: 600;
}

.plane-svg .sub {
  font-size: 9px;
  font-style: normal;
}

.plane-svg .ticks text {
  fill: var(--muted);
  font-size: 10px;
}

.plane-svg .common-line {
  stroke: #00796b;
  stroke-width: 1.5;
  stroke-dasharray: 2 4;
  stroke-linecap: round;
}

.plane-svg .figure-shape {
  fill: rgba(123, 63, 191, 0.25);
  stroke: #7b3fbf;
  stroke-width: 2;
  stroke-linejoin: round;
}

.plane-svg .edge-arrow,
.plane-svg .vertex0 {
  fill: #7b3fbf;
}

.plane-svg .origin {
  fill: #fff;
  stroke: var(--text);
  stroke-width: 1.5;
}

.plane-svg .origin-label {
  fill: var(--text);
  font-size: 11px;
  font-style: italic;
}

.map-readout {
  margin-top: 4px;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}

.map-readout .readout-title {
  margin-bottom: 2px;
  color: var(--muted);
  font: 12px system-ui, sans-serif;
}

.map-readout .matrix {
  border-collapse: collapse;
}

.map-readout .matrix td {
  padding: 0 3px;
  text-align: right;
}

.map-readout .matrix .mlabel {
  padding-right: 4px;
}

.map-readout .matrix .bracket {
  padding: 0;
  color: var(--muted);
}

.map-readout .badge {
  margin-left: 4px;
  padding: 0 5px;
  border-radius: 8px;
  background: #fdecea;
  color: #b3261e;
  font: 600 11px system-ui, sans-serif;
}

/* ---------- Controls panel ---------- */
.controls-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  padding: 8px 12px;
  overflow: auto;
}

.controls-col {
  position: relative;
  min-width: 0;
}

.controls-col h2 {
  margin: 0 0 6px;
  font-size: 13px;
}

#controls-source h2 {
  color: var(--source);
}

#controls-target h2 {
  color: var(--target);
}

.controls-col .placeholder {
  position: static;
  display: block;
  text-align: left;
}

/* group label | axis label | slider | number box */
.slider-grid {
  display: grid;
  grid-template-columns: 4.6em 1.6em minmax(40px, 1fr) 4.6em;
  align-items: center;
  column-gap: 6px;
  row-gap: 2px;
}

.group-label {
  color: var(--muted);
  font-size: 12px;
}

.axis-label {
  font-style: italic;
  text-align: right;
}

.slider-grid input[type="range"] {
  width: 100%;
  margin: 0;
}

.num {
  width: 100%;
  padding: 1px 4px;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--border);
  border-radius: 3px;
}

.num.invalid {
  border-color: #d93025;
  background: #fdecea;
  outline-color: #d93025;
}

/* Normal y-sign switch: left = −1, right = +1. */
.ysign {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  user-select: none;
}

.switch-side {
  color: var(--muted);
}

.switch-side.active {
  color: var(--text);
  font-weight: 700;
}

.switch-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.switch-track {
  position: relative;
  width: 30px;
  height: 16px;
  border-radius: 8px;
  background: #9aa1ad;
  transition: background 0.15s;
}

.switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}

.switch-input:checked + .switch-track {
  background: #4b8a5c;
}

.switch-input:checked + .switch-track::after {
  transform: translateX(14px);
}

.switch-input:focus-visible + .switch-track {
  outline: 2px solid var(--source);
  outline-offset: 2px;
}

.fixed-value {
  padding-left: 2px;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
}

.hint {
  color: var(--muted);
  font-size: 11px;
}

.normal-readout {
  margin-top: 4px;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
}

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

/* Figure dropdown below the source plane's normal readout */
#controls-source .field {
  margin-top: 8px;
}

.field > label:first-child,
.field > span:first-child {
  min-width: 3.5em;
  color: var(--muted);
  font-size: 12px;
}

.check {
  white-space: nowrap;
}

button,
select {
  font: inherit;
  font-size: 13px;
}


/* ---------- Narrow screens: stack panels vertically ---------- */
@media (max-width: 800px) {
  .app {
    grid-template-columns: 1fr;
    height: auto;
  }

  .col-left {
    grid-template-rows: 60vh auto;
  }

  .col-right {
    grid-template-rows: 45vh 45vh;
  }

  .controls-grid {
    grid-template-columns: 1fr;
  }
}
