:root {
  --bg: #fff8ea;
  --panel: #fffdf7;
  --ink: #2f3047;
  --muted: #4f5d75;
  --accent: #ff7f50;
  --gravity: #2d6cdf;
  --joint: #ff5c39;
  --grf: #8b5e34;
  --external: #2cb67d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Trebuchet MS", "Avenir Next", "Segoe UI", sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at top, #ffe5b8 0%, var(--bg) 68%);
}

.back-link {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 10;
  padding: 10px 14px;
  border: 2px solid #2f304733;
  border-radius: 999px;
  background: #fffdf7;
  box-shadow: 0 6px 0 #2f30471a;
  color: var(--ink);
  font-weight: 700;
  text-decoration: none;
}

.back-link:hover {
  background: #fff2de;
}

.app {
  display: grid;
  grid-template-columns: minmax(310px, 380px) 1fr;
  gap: 16px;
  padding: 16px;
  min-height: 100vh;
}

.panel {
  background: var(--panel);
  border: 3px solid #2f304733;
  border-radius: 18px;
  box-shadow: 0 10px 0 #2f30471f;
}

.controls {
  padding: 14px;
  overflow-y: auto;
  max-height: calc(100vh - 32px);
}

h1 {
  margin: 0 0 8px 0;
  font-size: 2rem;
}

h2 {
  margin: 0;
  font-size: 1.2rem;
}

p {
  margin: 0 0 12px 0;
  color: var(--muted);
  line-height: 1.3;
}

.math {
  font-family: "IBM Plex Mono", "Menlo", monospace;
}

.group {
  margin-top: 12px;
  padding: 10px;
  border: 2px solid #2f304726;
  border-radius: 14px;
  background: #fff6e9;
}

label {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 1.05rem;
}

input[type="range"] {
  grid-column: 1 / 2;
  width: 100%;
  accent-color: var(--accent);
}

select {
  grid-column: 1 / -1;
  width: 100%;
  margin-top: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 2px solid #2f304733;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 700;
}

button {
  padding: 8px 10px;
  border-radius: 10px;
  border: 2px solid #2f304733;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

button:hover {
  background: #fff2de;
}

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

.playback-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

output {
  font-family: "Trebuchet MS", "Avenir Next", sans-serif;
  font-size: 1.2rem;
  font-weight: 700;
  min-width: 48px;
  text-align: right;
}

.toggles label {
  display: block;
  margin-top: 7px;
}

.viz {
  position: relative;
  padding: 10px;
}

canvas {
  width: 100%;
  height: min(calc(100vh - 270px), 560px);
  display: block;
  border-radius: 18px;
  border: 3px solid #2f30472e;
  background: linear-gradient(180deg, #fff6e4 0%, #e7f6ff 100%);
}

.legend {
  display: flex;
  gap: 14px;
  padding: 8px 2px 0 2px;
  color: #304452;
  font-size: 1.05rem;
  font-weight: 700;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.hover-card {
  margin-top: 12px;
  padding: 14px 14px 12px 14px;
  border-radius: 16px;
  border: 3px dashed #2f30473d;
  background: linear-gradient(180deg, #fff8df 0%, #edf8ff 100%);
  box-shadow: 0 7px 0 #2f30471f;
  position: relative;
}

.hover-title {
  margin-top: 2px;
  font-size: 1.15rem;
  font-weight: 800;
  color: #2f3047;
}

.hover-details {
  margin: 8px 0 0 0;
  padding: 10px 12px;
  border-radius: 12px;
  border: 2px solid #2f304720;
  background: #ffffffb8;
  font-family: "Trebuchet MS", "Avenir Next", sans-serif;
  font-size: 0.98rem;
  color: #26354b;
  white-space: pre-wrap;
  min-height: 120px;
}

.swatch {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  display: inline-block;
  border: 2px solid #2f304733;
}

.gravity {
  background: var(--gravity);
}

.joint {
  background: var(--joint);
}

.grf {
  background: var(--grf);
}

.external {
  background: var(--external);
}

@media (max-width: 980px) {
  .back-link {
    position: static;
    display: inline-block;
    margin: 16px 16px 0 16px;
  }

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

  .controls {
    max-height: none;
  }

  canvas {
    height: 54vh;
  }
}
