:root {
  color-scheme: dark;
  --bg: #080b14;
  --panel: rgba(18, 23, 39, 0.88);
  --panel-solid: #121727;
  --panel-soft: #171d30;
  --border: rgba(255, 255, 255, 0.11);
  --text: #f6f7fb;
  --muted: #a9b1c6;
  --accent: #9b87f5;
  --accent-strong: #7c64e8;
  --pendulum-a: #70e1f5;
  --pendulum-b: #ff8ac6;
  --grid: rgba(255, 255, 255, 0.028);
  --grid-strong: rgba(255, 255, 255, 0.09);
  --good: #6ee7b7;
  --warn: #fcd34d;
  --bad: #fb7185;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    radial-gradient(circle at 15% 10%, rgba(112, 225, 245, 0.09), transparent 28rem),
    radial-gradient(circle at 85% 85%, rgba(255, 138, 198, 0.08), transparent 30rem),
    var(--bg);
  color: var(--text);
}

button,
input,
select {
  font: inherit;
}

button {
  border: 1px solid var(--border);
  border-radius: 0.8rem;
  background: var(--panel-soft);
  color: var(--text);
  padding: 0.68rem 0.9rem;
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}

button:hover {
  transform: translateY(-1px);
  border-color: rgba(255, 255, 255, 0.22);
}

button:focus-visible,
input:focus-visible,
summary:focus-visible {
  outline: 3px solid rgba(155, 135, 245, 0.42);
  outline-offset: 2px;
}

button.primary {
  border-color: transparent;
  background: var(--accent);
  color: #0d1020;
  font-weight: 700;
}

button.primary:hover {
  background: var(--accent-strong);
  color: white;
}

.app-shell {
  width: min(1500px, 100%);
  min-height: 100vh;
  margin: 0 auto;
  padding: 1.1rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.25rem, 2vw, 1.8rem);
  letter-spacing: -0.03em;
}

.brand p {
  margin: 0.3rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  gap: 1rem;
  min-height: calc(100vh - 6rem);
}

.stage {
  min-width: 0;
  display: grid;
  grid-template-rows: auto auto auto;
  border: 1px solid var(--border);
  border-radius: 1.25rem;
  overflow: hidden;
  background: rgba(11, 15, 27, 0.72);
  backdrop-filter: blur(18px);
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.65rem;
  padding: 0.8rem;
  border-bottom: 1px solid var(--border);
}

.metric {
  padding: 0.7rem 0.8rem;
  border-radius: 0.9rem;
  background: rgba(255, 255, 255, 0.035);
  min-width: 0;
}

.metric-label {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  margin-bottom: 0.22rem;
}

.metric-value {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.metric-value[data-level="good"] { color: var(--good); }
.metric-value[data-level="warn"] { color: var(--warn); }
.metric-value[data-level="bad"] { color: var(--bad); }

.canvas-wrap {
  position: relative;
  width: 100%;
  height: clamp(440px, 66vh, 760px);
  height: clamp(440px, 66svh, 760px);
  min-height: 0;
  overflow: hidden;
  contain: layout paint size;
}

#pendulumCanvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  touch-action: none;
  cursor: grab;
}

#pendulumCanvas.is-dragging {
  cursor: grabbing;
}

.canvas-hint {
  position: absolute;
  left: 1rem;
  bottom: 0.85rem;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.42rem 0.7rem;
  background: rgba(8, 11, 20, 0.72);
  color: var(--muted);
  font-size: 0.78rem;
  pointer-events: none;
}

.chart-panel {
  padding: 0.75rem 0.9rem 0.55rem;
  border-top: 1px solid var(--border);
}

.chart-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
  margin-bottom: 0.4rem;
}

.chart-header h2 {
  margin: 0;
  font-size: 0.86rem;
  font-weight: 650;
}

.chart-header span {
  color: var(--muted);
  font-size: 0.75rem;
}

#separationChart {
  display: block;
  width: 100%;
  height: 112px;
}

.sidebar {
  min-width: 0;
  max-height: calc(100vh - 6rem);
  overflow: auto;
  padding-right: 0.2rem;
  scrollbar-width: thin;
}

.panel {
  border: 1px solid var(--border);
  border-radius: 1.1rem;
  background: var(--panel);
  backdrop-filter: blur(16px);
  padding: 0.9rem;
  margin-bottom: 0.8rem;
}

.panel h2 {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
}

.button-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.55rem;
}

.button-grid .wide {
  grid-column: 1 / -1;
}

.preset-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.45rem;
  margin-top: 0.7rem;
}

.preset-row button {
  padding-inline: 0.5rem;
  font-size: 0.8rem;
}

.toggle-list {
  display: grid;
  gap: 0.55rem;
  margin-top: 0.75rem;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.toggle input {
  width: 1.05rem;
  height: 1.05rem;
  accent-color: var(--accent);
}

.control {
  margin-bottom: 0.85rem;
}

.control:last-child {
  margin-bottom: 0;
}

.control-heading {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: baseline;
  margin-bottom: 0.38rem;
}

.control-heading label {
  font-size: 0.84rem;
}

.control-heading output {
  color: var(--muted);
  font-size: 0.76rem;
  font-variant-numeric: tabular-nums;
}

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

details.panel {
  padding: 0;
}

details.panel summary {
  cursor: pointer;
  padding: 0.9rem;
  font-size: 0.95rem;
  font-weight: 650;
}

details.panel .details-content {
  padding: 0 0.9rem 0.9rem;
}

.helper {
  margin: 0.7rem 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

.status-line {
  margin: 0.75rem 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

.legend {
  display: flex;
  gap: 0.9rem;
  flex-wrap: wrap;
  margin-top: 0.7rem;
  color: var(--muted);
  font-size: 0.78rem;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.legend-dot {
  width: 0.68rem;
  height: 0.68rem;
  border-radius: 999px;
  background: var(--pendulum-a);
}

.legend-dot.b {
  background: var(--pendulum-b);
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
    min-height: 0;
  }

  .sidebar {
    max-height: none;
    overflow: visible;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.8rem;
    padding: 0;
  }

  .panel {
    margin: 0;
  }
}

@media (max-width: 650px) {
  .app-shell {
    padding: 0.65rem;
  }

  .topbar {
    align-items: flex-start;
  }

  .topbar > button {
    padding-inline: 0.65rem;
    font-size: 0.8rem;
  }

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

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

  .stage,
  .panel {
    border-radius: 0.9rem;
  }

  .canvas-wrap {
    height: clamp(340px, 54vh, 480px);
    height: clamp(340px, 54svh, 480px);
  }

  #pendulumCanvas {
    min-height: 0;
  }

}