:root {
  --bg: #080910;
  --panel: #10121b;
  --panel-2: #171a26;
  --border: #22262f;
  --border-soft: #1a1d26;
  --ink: #e6e9f0;
  --ink-dim: #9aa3b2;
  --ink-mute: #666b78;
  --accent: #a78bfa;
  --accent-2: #7c6ad8;
  --red: #f87171;
  --good: #4ade80;
  --warn: #fbbf24;
  --sans: "Inter", -apple-system, system-ui, sans-serif;
  --serif: "Source Serif 4", Georgia, serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
body {
  background:
    radial-gradient(70% 50% at 20% 0%, rgba(167,139,250,0.07) 0%, transparent 60%),
    radial-gradient(60% 40% at 90% 10%, rgba(99,102,241,0.05) 0%, transparent 60%),
    var(--bg);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---- Top bar ---- */
.top {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 32px;
  align-items: center;
  padding: 18px 32px;
  border-bottom: 1px solid var(--border-soft);
  position: sticky;
  top: 0;
  background: color-mix(in oklab, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  z-index: 10;
}
.brand {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 20px;
  letter-spacing: -0.015em;
}
.brand .logo {
  display: inline-block;
  color: var(--accent);
  margin-right: 6px;
  transform: translateY(1px);
}
.lesson-dots {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--border);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background .2s, transform .2s;
}
.dot:hover { background: var(--ink-mute); }
.dot.done { background: var(--accent-2); border-color: var(--accent-2); }
.dot.active {
  background: var(--accent);
  border-color: var(--accent);
  transform: scale(1.3);
}
.top-right { justify-self: end; }
.home-link {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-mute);
}
.home-link:hover { color: var(--ink); text-decoration: none; }

/* ---- Layout ---- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 460px;
  gap: 0;
  min-height: calc(100vh - 62px - 60px);
}
.stage {
  position: relative;
  border-right: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: hidden;
}
.sphere-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 40px;
  width: 100%;
  height: 100%;
  min-height: 480px;
}
.sphere {
  width: 100%;
  max-width: 540px;
  aspect-ratio: 1 / 1;
  display: block;
  touch-action: none;
}
.sphere[hidden] { display: none !important; }
.sphere.twin {
  max-width: 340px;
}
.stage-overlay {
  position: absolute;
  left: 20px;
  top: 20px;
  right: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  pointer-events: none;
}
.chip {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(23,26,38,0.75);
  border: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--ink-dim);
  backdrop-filter: blur(6px);
}
.chip.accent { color: var(--accent); border-color: rgba(167,139,250,0.3); }
.chip.red { color: var(--red); border-color: rgba(248,113,113,0.3); }

/* ---- Lesson panel ---- */
.lesson {
  padding: 32px 36px 48px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}
.lesson-head {
  margin-bottom: 20px;
}
.lesson-num {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.2em;
  color: var(--accent);
  text-transform: uppercase;
}
.lesson-title {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 34px;
  line-height: 1.1;
  letter-spacing: -0.02em;
  margin: 8px 0 8px;
}
.lesson-sub {
  font-size: 14px;
  color: var(--ink-dim);
  margin: 0;
}

.lesson-body {
  font-size: 15.5px;
  color: var(--ink);
  line-height: 1.65;
  margin-bottom: 18px;
}
.lesson-body p { margin: 0 0 12px; }
.lesson-body code {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 4px;
}
.lesson-body strong { color: var(--ink); }
.lesson-body em { color: var(--accent); font-style: normal; }

/* ---- Probability bars ---- */
.probs {
  margin: 10px 0 18px;
  padding: 14px 16px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.prob-row {
  display: grid;
  grid-template-columns: 48px 1fr 56px;
  gap: 12px;
  align-items: center;
  margin: 6px 0;
}
.prob-label {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--ink-dim);
  letter-spacing: 0.05em;
}
.prob-bar {
  height: 8px;
  background: rgba(167,139,250,0.08);
  border-radius: 4px;
  overflow: hidden;
}
.prob-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  width: 0%;
  transition: width .25s ease;
}
.prob-fill.red {
  background: linear-gradient(90deg, #b94848, var(--red));
}
.prob-val {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--ink);
  text-align: right;
}

/* ---- Controls ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
}
.gate-btn, .action-btn {
  font-family: var(--sans);
  font-weight: 500;
  font-size: 13px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--ink);
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.gate-btn:hover, .action-btn:hover {
  background: #1e2130;
  border-color: var(--accent-2);
}
.gate-btn:active, .action-btn:active {
  transform: translateY(1px);
}
.gate-btn.gate-h { color: var(--accent); }
.gate-btn.gate-x { color: var(--red); }
.gate-btn.gate-z { color: #60a5fa; }
.gate-btn.gate-y { color: var(--warn); }
.gate-btn.gate-cnot { color: var(--good); font-family: var(--mono); }

.action-btn.primary {
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #0a0b14;
  font-weight: 600;
  border-color: transparent;
}
.action-btn.primary:hover {
  filter: brightness(1.1);
}
.action-btn.danger {
  color: var(--red);
  border-color: rgba(248,113,113,0.3);
}

/* ---- Lesson nav ---- */
.lesson-nav {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin: 10px 0 24px;
  padding: 16px 0 0;
  border-top: 1px solid var(--border-soft);
}
.nav-btn {
  font-family: var(--sans);
  font-size: 13px;
  padding: 10px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-btn.ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--ink-dim);
}
.nav-btn.ghost:hover:not(:disabled) { background: var(--panel-2); color: var(--ink); }
.nav-btn.primary {
  background: var(--accent);
  color: #0a0b14;
  border: 1px solid var(--accent);
  font-weight: 600;
}
.nav-btn.primary:hover:not(:disabled) { background: var(--accent-2); }
.nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---- Counters (measurement histogram, etc.) ---- */
.histogram {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  height: 80px;
  padding: 10px 14px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 16px;
}
.hist-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-dim);
  height: 100%;
}
.hist-bar {
  width: 100%;
  background: linear-gradient(0deg, var(--accent-2), var(--accent));
  border-radius: 3px 3px 0 0;
  transition: height .25s ease;
  min-height: 2px;
}
.hist-bar.red {
  background: linear-gradient(0deg, #b94848, var(--red));
}
.hist-label {
  font-size: 11px;
  color: var(--ink-mute);
}

/* ---- CHSH display ---- */
.chsh-s {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 14px;
  padding: 16px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.chsh-s-val {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 40px;
  color: var(--accent);
  letter-spacing: -0.02em;
}
.chsh-s-sub {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-dim);
  letter-spacing: 0.05em;
  line-height: 1.5;
}

/* ---- Tutor ---- */
.tutor {
  margin-top: auto;
  padding: 16px 0 0;
  border-top: 1px solid var(--border-soft);
}
.tutor-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}
.tutor-label {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.15em;
  color: var(--ink-mute);
  text-transform: uppercase;
}
.tutor-meta {
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--ink-mute);
}
.tutor-log {
  max-height: 230px;
  overflow-y: auto;
  padding: 8px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 10px;
}
.tutor-msg {
  font-size: 13.5px;
  line-height: 1.55;
  padding: 8px 10px;
  margin: 6px 0;
  border-radius: 8px;
}
.tutor-msg.system {
  color: var(--ink-mute);
  font-style: italic;
  font-size: 12.5px;
}
.tutor-msg.user {
  background: rgba(167,139,250,0.14);
  color: var(--ink);
}
.tutor-msg.assistant {
  background: rgba(255,255,255,0.03);
  color: var(--ink);
}
.tutor-msg.error {
  color: var(--red);
  font-size: 12.5px;
}
.tutor-form {
  display: flex;
  gap: 8px;
}
.tutor-input {
  flex: 1;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--ink);
  border-radius: 8px;
  font-family: var(--sans);
  font-size: 13px;
  outline: none;
  transition: border-color .15s;
}
.tutor-input:focus { border-color: var(--accent); }
.tutor-send {
  padding: 10px 16px;
  background: var(--accent);
  color: #0a0b14;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s;
}
.tutor-send:hover { background: var(--accent-2); }
.tutor-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---- Footer ---- */
.foot {
  display: flex;
  justify-content: space-between;
  padding: 16px 32px;
  border-top: 1px solid var(--border-soft);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-mute);
  letter-spacing: 0.05em;
}
.foot a { color: var(--ink-dim); }
.foot a:hover { color: var(--accent); }

/* ---- Entanglement / two-sphere layout ---- */
.sphere-wrap.twin-mode {
  flex-direction: row;
}
.sphere-wrap.twin-mode .sphere.single { display: none; }
.sphere-wrap.twin-mode .sphere.twin { display: block; }

/* ---- Sliders (for Bell test angle picks) ---- */
.slider-row {
  display: grid;
  grid-template-columns: 70px 1fr 50px;
  gap: 10px;
  align-items: center;
  margin: 6px 0;
}
.slider-label {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-dim);
}
.slider {
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--panel);
}
.slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--panel);
}
.slider-val {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink);
  text-align: right;
}

/* ---- Responsive ---- */
@media (max-width: 1000px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .stage {
    border-right: none;
    border-bottom: 1px solid var(--border-soft);
  }
  .sphere-wrap { min-height: 360px; }
  .sphere { max-width: 380px; }
  .sphere.twin { max-width: 240px; }
  .lesson { padding: 24px 20px 32px; }
  .lesson-title { font-size: 28px; }
}
@media (max-width: 640px) {
  .top { padding: 14px 16px; gap: 16px; }
  .top-right { display: none; }
  .brand { font-size: 17px; }
  .sphere-wrap { gap: 16px; }
  .sphere.twin { max-width: 180px; }
}
