:root {
  color-scheme: dark;
  --bg: #06101f;
  --panel: rgba(15, 23, 42, 0.88);
  --panel-2: rgba(30, 41, 59, 0.82);
  --line: rgba(148, 163, 184, 0.18);
  --text: #eaf2ff;
  --muted: #9fb2d1;
  --blue: #60a5fa;
  --green: #4ade80;
  --cyan: #22d3ee;
  --yellow: #facc15;
  --red: #fb7185;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 12% 0%, rgba(34, 211, 238, 0.15), transparent 30%),
    radial-gradient(circle at 82% 4%, rgba(96, 165, 250, 0.13), transparent 34%),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", "Microsoft YaHei", sans-serif;
}

h1, h2, h3, h4, p { margin-top: 0; }

button, input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;
  min-width: 0;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(240px, 1fr) auto auto;
  align-items: center;
  gap: 18px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(16px);
}

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
}

.back-link:hover {
  color: var(--cyan);
}

.eyebrow {
  margin-bottom: 2px;
  color: var(--cyan);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(22px, 2.7vw, 34px);
  line-height: 1.05;
}

h2 { margin-bottom: 10px; font-size: 16px; }
h3 { margin-bottom: 10px; font-size: 15px; }
h4 { margin-bottom: 6px; font-size: 13px; }

.top-metrics {
  display: grid;
  grid-template-columns: repeat(3, minmax(86px, 1fr));
  gap: 8px;
}

.metric {
  min-width: 86px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255,255,255,0.055);
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 11px;
}

.metric strong {
  display: block;
  margin-top: 3px;
  font-size: 15px;
}

.drawer-toggle {
  width: auto;
  white-space: nowrap;
  text-align: center;
}

.workbench {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
  min-height: 0;
  gap: 12px;
  padding: 12px;
}

.side-panel,
.inspector-drawer,
.canvas-panel,
.quick-docs {
  border: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(255,255,255,0.072), rgba(255,255,255,0.028));
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.26);
}

.side-panel,
.inspector-drawer {
  min-height: 0;
  overflow: auto;
  border-radius: 20px;
}

.side-panel {
  padding: 16px;
}

.side-panel section + section,
.inspector-drawer section + section {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.canvas-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  border-radius: 22px;
  padding: 14px;
}

.canvas-toolbar {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto;
  gap: 16px;
  align-items: start;
  margin-bottom: 10px;
}

.canvas-toolbar p {
  margin-bottom: 0;
  color: var(--muted);
  line-height: 1.55;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 9px;
  max-width: 520px;
  color: var(--muted);
  font-size: 12px;
}

.legend i {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 4px;
  border-radius: 50%;
}

.posterior { background: var(--green); }
.predict { background: var(--yellow); }
.measurement { background: #f8fafc; }
.update { background: var(--cyan); }
.uncertainty {
  background: transparent;
  border: 2px solid rgba(234, 242, 255, 0.7);
}

.canvas-wrap {
  min-width: 0;
  min-height: 420px;
  height: 100%;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 18px;
  overflow: hidden;
  background: rgba(2, 6, 23, 0.58);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: rgba(2, 6, 23, 0.55);
  cursor: crosshair;
}

.inspector-drawer {
  position: fixed;
  top: 78px;
  right: 12px;
  bottom: 58px;
  z-index: 10;
  width: min(430px, calc(100vw - 32px));
  padding: 16px;
  transform: translateX(calc(100% + 18px));
  transition: transform 180ms ease;
  backdrop-filter: blur(20px);
}

body.drawer-open .inspector-drawer {
  transform: translateX(0);
}

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

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

button {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  background: rgba(255,255,255,0.055);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: 150ms ease;
}

button:hover {
  border-color: rgba(96, 165, 250, 0.85);
  transform: translateY(-1px);
}

button.active {
  background: rgba(96, 165, 250, 0.23);
  border-color: var(--blue);
}

button.primary {
  margin-top: 10px;
  text-align: center;
  background: rgba(34, 211, 238, 0.14);
  border-color: rgba(34, 211, 238, 0.35);
}

.field span, .slider label {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 13px;
}

input[type="text"] {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 12px;
  background: rgba(2, 6, 23, 0.52);
  color: var(--text);
}

.hint {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.55;
}

code {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 7px;
  background: rgba(255,255,255,0.08);
  color: #d9e7ff;
}

.slider { margin-bottom: 13px; }
.slider strong { color: var(--text); }
input[type="range"] { width: 100%; accent-color: var(--cyan); }

.step-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.step-pill {
  padding: 10px 12px;
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: 12px;
  background: rgba(255,255,255,0.045);
  color: var(--muted);
  text-align: center;
  font-size: 13px;
}

.step-pill.active {
  color: var(--text);
  border-color: var(--cyan);
  background: rgba(34, 211, 238, 0.16);
}

.formula-panel {
  display: grid;
  gap: 10px;
}

.formula {
  padding: 12px;
  border-left: 3px solid rgba(96, 165, 250, 0.35);
  border-radius: 12px;
  background: rgba(2, 6, 23, 0.42);
  color: var(--muted);
  line-height: 1.65;
}

.formula.active {
  border-left-color: var(--cyan);
  color: var(--text);
}

pre {
  overflow: auto;
  margin: 0;
  padding: 14px;
  max-height: 34vh;
  border-radius: 14px;
  background: rgba(2, 6, 23, 0.55);
  color: #d8e7ff;
  font-size: 12px;
  line-height: 1.55;
}

.history-list {
  display: grid;
  gap: 8px;
  max-height: 160px;
  overflow: auto;
}

.history-item {
  padding: 9px 10px;
  border-radius: 10px;
  background: rgba(255,255,255,0.045);
  color: var(--muted);
  font-size: 12px;
}

.quick-docs {
  margin: 0 12px 12px;
  border-radius: 18px;
}

.quick-docs summary {
  padding: 12px 16px;
  color: var(--muted);
  cursor: pointer;
}

.docs-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  padding: 0 16px 16px;
}

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

.formula-grid code {
  display: block;
  margin-top: 6px;
  padding: 8px 10px;
}

table {
  width: 100%;
  border-collapse: collapse;
  color: var(--muted);
}

th, td {
  padding: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  text-align: left;
  vertical-align: top;
}

th { color: var(--text); }

@media (min-width: 1440px) {
  .workbench {
    grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  }

  .canvas-wrap {
    min-height: 600px;
  }
}

@media (max-width: 980px) {
  body {
    overflow: auto;
  }

  .app-shell {
    min-height: 100vh;
    height: auto;
  }

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

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

  .workbench,
  .docs-grid {
    grid-template-columns: 1fr;
  }

  .canvas-panel {
    min-height: 70vh;
  }

  .canvas-toolbar {
    grid-template-columns: 1fr;
  }

  .legend {
    justify-content: flex-start;
  }

  .canvas-wrap {
    min-height: 56vh;
  }

  .inspector-drawer {
    top: 12px;
    bottom: 12px;
  }
}

@media (max-width: 640px) {
  .topbar,
  .workbench {
    padding: 10px;
  }

  .button-grid.two,
  .step-row,
  .formula-grid {
    grid-template-columns: 1fr;
  }
}
