/* ═══════════════════════════════════════════════════════════
   Particle Swarm Optimization (Explorables)
   A dark, minimal scrollytelling learning page that embeds the
   Three.js PSO explorer as a playground.
   ═══════════════════════════════════════════════════════════ */

:root {
  --bg: #070a12;
  --bg-2: #0b0f1a;
  --surface: rgba(255, 255, 255, 0.035);
  --surface-2: rgba(255, 255, 255, 0.06);
  --border: rgba(120, 170, 255, 0.14);
  --border-2: rgba(120, 170, 255, 0.30);
  --text: #e7ecf5;
  --text-2: #a6b0c4;
  --text-3: #6d778c;
  --accent: #4da6ff;   /* UI / personal-best (c1) */
  --accent-2: #ffd24d; /* social / global-best (c2) */
  --gold: #ffd24d;
  --good: #38e0c8;     /* inertia (w) / convergence */
  --teal: #38e0c8;
  /* app overlay vars (kept for the embedded explorer's own CSS) */
  --panel-bg: rgba(16, 20, 32, 0.85);
  --panel-border: rgba(120, 170, 255, 0.18);
  --text-dim: #8b93a7;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  --mono: "SF Mono", "Cascadia Code", Consolas, Menlo, monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.9;
  -webkit-font-smoothing: antialiased;
}

::selection { background: rgba(77, 166, 255, 0.32); }

/* ── Top navigation ─────────────────────────────────────── */
.topnav {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.55rem 1.2rem;
  background: rgba(7, 10, 18, 0.82);
  backdrop-filter: blur(14px) saturate(1.4);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
}
.topnav-home {
  color: var(--text-2);
  text-decoration: none;
  letter-spacing: 0.04em;
  white-space: nowrap;
  transition: color 0.2s;
}
.topnav-home:hover { color: var(--text); }
.topnav-title {
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--text-3);
  text-transform: uppercase;
  font-size: 0.7rem;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topnav-links { display: flex; gap: 0.3rem; }
.topnav-links a {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.7rem; height: 1.7rem;
  border-radius: 50%;
  color: var(--text-3);
  text-decoration: none;
  font-size: 0.72rem;
  font-family: var(--mono);
  border: 1px solid transparent;
  transition: all 0.2s;
}
.topnav-links a:hover { color: var(--text); border-color: var(--border-2); }
.topnav-links a.active {
  color: #070a12;
  background: var(--accent);
  font-weight: 700;
}

/* ── Hero ───────────────────────────────────────────────── */
.hero {
  position: relative;
  min-height: 90vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 8%, rgba(77, 166, 255, 0.16), transparent 34%),
    radial-gradient(circle at 82% 88%, rgba(255, 210, 77, 0.08), transparent 30%),
    radial-gradient(130% 90% at 50% 0%, #0d1626 0%, var(--bg) 62%);
}
.hero-inner {
  position: relative;
  z-index: 1;
  max-width: 800px;
  padding: 6rem 1.5rem 4rem;
  text-align: center;
}
.hero-eyebrow {
  font-size: 0.72rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 1.4rem;
}
.hero h1 {
  font-size: clamp(1.7rem, 4.6vw, 2.9rem);
  font-weight: 700;
  letter-spacing: 0.03em;
  line-height: 1.45;
  margin-bottom: 1.6rem;
}
.hero-dash { margin: 0 0.35em; color: var(--text-3); font-weight: 300; }
.hero-lead {
  font-size: 0.95rem;
  color: var(--text-2);
  text-align: left;
  margin-bottom: 2.4rem;
}
.hero-lead strong { color: var(--gold); font-weight: 600; }
.hero-cta {
  display: inline-block;
  padding: 0.75rem 2.2rem;
  border: 1px solid var(--border-2);
  border-radius: 999px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.85rem;
  letter-spacing: 0.08em;
  background: var(--surface);
  transition: all 0.25s;
}
.hero-cta:hover { background: var(--surface-2); border-color: var(--accent); }

/* ── Chapters ───────────────────────────────────────────── */
.content { max-width: 880px; margin: 0 auto; padding: 0 1.4rem; }

.chapter {
  padding: 5.5rem 0 3rem;
  border-bottom: 1px solid var(--border);
}
.js .chapter {
  opacity: 0;
  transform: translateY(18px);
  transition: opacity 0.7s ease, transform 0.7s ease;
}
.js .chapter.visible { opacity: 1; transform: none; }
.chapter:last-of-type { border-bottom: none; }

.ch-head { margin-bottom: 2rem; }
.ch-index {
  font-family: var(--mono);
  font-size: 0.72rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
}
.ch-head h2 {
  font-size: clamp(1.35rem, 3vw, 1.85rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.5;
  margin: 0.5rem 0 0.4rem;
}
.ch-sub { font-size: 0.9rem; color: var(--text-3); }

.ch-prose p { margin-bottom: 1.3rem; font-size: 0.95rem; color: var(--text-2); }
.ch-prose strong { color: var(--text); font-weight: 600; }
.ch-prose .accent { color: var(--accent); font-weight: 600; }
.ch-prose .term-w  { color: var(--good); font-weight: 600; }
.ch-prose .term-c1 { color: var(--accent); font-weight: 600; }
.ch-prose .term-c2 { color: var(--accent-2); font-weight: 600; }

/* formulas / inline code */
.eq {
  font-family: var(--mono);
  font-size: 0.9rem;
  color: var(--gold);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem 1.2rem;
  margin: 0 0 1.3rem;
  text-align: center;
  overflow-x: auto;
  white-space: nowrap;
}
.eq .t-w  { color: var(--good); }
.eq .t-c1 { color: var(--accent); }
.eq .t-c2 { color: var(--accent-2); }
.eq-inline {
  font-family: var(--mono);
  font-size: 0.88em;
  color: var(--gold);
  white-space: nowrap;
}

/* step lists (numbered process) */
.steps {
  list-style: none;
  counter-reset: step;
  margin: 0.4rem 0 1.5rem;
  display: grid;
  gap: 0.7rem;
}
.steps li {
  position: relative;
  padding: 0.15rem 0 0 3rem;
  font-size: 0.92rem;
  color: var(--text-2);
  min-height: 2rem;
  counter-increment: step;
}
.steps li::before {
  content: counter(step);
  position: absolute;
  left: 0; top: 0;
  width: 2rem; height: 2rem;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 0.85rem;
  font-weight: 600;
}
.steps strong { color: var(--text); }

/* note (comparison / aside) */
.note {
  position: relative;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 1.1rem 1.3rem 1rem;
  margin: 1.6rem 0;
  border-left: 3px solid var(--accent);
}
.note p { margin: 0.35rem 0 0; font-size: 0.88rem; color: var(--text-2); }
.note p:first-child { margin-top: 0; }
.note-tag {
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 0.18rem 0.7rem;
  border-radius: 999px;
  background: rgba(77, 166, 255, 0.14);
  color: var(--accent);
}
.note.gold { border-left-color: var(--gold); }
.note.gold .note-tag { background: rgba(255, 210, 77, 0.15); color: var(--gold); }
.note strong { color: var(--text); }

/* visual legend for the playground */
.legend {
  list-style: none;
  display: grid;
  gap: 0.6rem;
  margin: 0.4rem 0 1.4rem;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 1.1rem 1.3rem;
}
.legend li {
  display: flex;
  align-items: baseline;
  gap: 0.7rem;
  font-size: 0.88rem;
  color: var(--text-2);
}
.legend .sw {
  flex-shrink: 0;
  width: 0.95rem; height: 0.95rem;
  border-radius: 50%;
  transform: translateY(0.12em);
}
.legend .sw.ring { background: transparent; border: 2px solid #ffffff; }
.legend strong { color: var(--text); font-weight: 600; }

/* comparison table */
.tool-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  margin: 0 0 1.5rem;
}
.tool-table th, .tool-table td {
  text-align: left;
  padding: 0.6rem 0.85rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-2);
  vertical-align: top;
}
.tool-table thead th {
  color: var(--text-3);
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 600;
}
.tool-table td:first-child, .tool-table tbody th:first-child {
  color: var(--text);
  white-space: nowrap;
  font-weight: 600;
}

/* generic lists */
.glist { margin: 0 0 1.3rem 1.2rem; }
.glist li { font-size: 0.92rem; color: var(--text-2); margin-bottom: 0.55rem; }
.glist strong { color: var(--text); }

/* ── Takeaway box ───────────────────────────────────────── */
.takeaway {
  margin-top: 2.4rem;
  border: 1px solid rgba(77, 166, 255, 0.25);
  background: rgba(77, 166, 255, 0.05);
  border-radius: 14px;
  padding: 1.3rem 1.6rem 1.1rem;
}
.takeaway h3 {
  font-size: 0.75rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.7rem;
}
.takeaway ul { margin-left: 1.2rem; }
.takeaway li { font-size: 0.88rem; color: var(--text-2); margin-bottom: 0.4rem; }
.takeaway strong { color: var(--text); }

/* ── Playground (the Three.js explorer) ─────────────────── */
.playground-window {
  position: relative;
  width: 100%;
  height: min(80vh, 720px);
  min-height: 500px;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}
.demo-lead {
  font-size: 0.95rem;
  color: var(--text-2);
  margin: 0.4rem 0 1.2rem;
}
.demo-lead strong { color: var(--text); font-weight: 600; }
.demo-hint {
  margin-top: 0.7rem;
  font-size: 0.78rem;
  color: var(--text-3);
  line-height: 1.7;
}
.demo-hint strong { color: var(--text-2); }

/* ── Q&A ────────────────────────────────────────────────── */
.qa {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  margin-bottom: 0.8rem;
  overflow: hidden;
}
.qa summary {
  cursor: pointer;
  padding: 1rem 1.3rem;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text);
  list-style: none;
  position: relative;
  padding-right: 3rem;
  transition: background 0.2s;
}
.qa summary::-webkit-details-marker { display: none; }
.qa summary::after {
  content: "+";
  position: absolute;
  right: 1.2rem; top: 50%;
  transform: translateY(-50%);
  color: var(--accent);
  font-size: 1.2rem;
  font-weight: 400;
  transition: transform 0.25s;
}
.qa[open] summary::after { transform: translateY(-50%) rotate(45deg); }
.qa summary:hover { background: var(--surface-2); }
.qa-body { padding: 0 1.3rem 1.1rem; }
.qa-body p { font-size: 0.88rem; color: var(--text-2); margin: 0 0 0.6rem; }
.qa-body strong { color: var(--text); }

/* ── Footer ─────────────────────────────────────────────── */
.site-footer {
  margin-top: 4rem;
  padding: 2.4rem 1.5rem 3rem;
  border-top: 1px solid var(--border);
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-3);
}
.site-footer nav { margin-top: 0.6rem; display: flex; gap: 1.4rem; justify-content: center; }
.site-footer a { color: var(--text-2); text-decoration: none; }
.site-footer a:hover { color: var(--text); }

/* ── Responsive (page) ──────────────────────────────────── */
@media (max-width: 640px) {
  body { font-size: 15px; }
  .topnav { padding: 0.5rem 0.8rem; gap: 0.6rem; }
  .topnav-title { display: none; }
  .chapter { padding-top: 4rem; }
  .playground-window { height: 84vh; min-height: 460px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .js .chapter { opacity: 1; transform: none; transition: none; }
}

/* ═══════════════════════════════════════════════════════════
   Embedded PSO explorer — overlay UI, scoped to #pso-app and
   positioned relative to the playground window (not the viewport).
   ═══════════════════════════════════════════════════════════ */
/* #pso-app IS the playground window (see .playground-window); keep it the
   positioned container so its absolute children resolve against it. */
#pso-app { position: relative; overflow: hidden; }

#scene-container { position: absolute; inset: 0; z-index: 0; }
#scene-container canvas { display: block; }

/* Title bar — only the dynamic educational label (page supplies the heading) */
#pso-app #title-bar {
  position: absolute;
  top: 0; left: 0; right: 0;
  z-index: 2;
  text-align: center;
  padding: 12px 14px 0 262px; /* reserve the left column for the control panel */
  pointer-events: none;
}
#pso-app .edu-label {
  margin: 0 auto;
  max-width: min(520px, 100%);
  font-size: 0.86rem;
  line-height: 1.5;
  color: var(--text);
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 7px 16px;
  display: inline-block;
  backdrop-filter: blur(5px);
  transition: opacity 0.4s ease;
}

/* Control panel (left) */
#pso-app #control-panel {
  position: absolute;
  top: 14px;
  left: 14px;
  z-index: 3;
  width: 232px;
  max-height: calc(100% - 28px);
  overflow-y: auto;
  padding: 16px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  backdrop-filter: blur(9px);
  box-shadow: 0 10px 34px rgba(0, 0, 0, 0.5);
}
#pso-app #control-panel h2 {
  margin: 0 0 14px;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 1px;
}
#pso-app .button-row { display: flex; gap: 10px; margin-bottom: 16px; }
#pso-app button {
  flex: 1;
  padding: 9px 0;
  font-size: 0.9rem;
  font-weight: 600;
  color: #06121f;
  background: linear-gradient(180deg, #7cc0ff, var(--accent));
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.05s ease;
}
#pso-app button:hover { filter: brightness(1.1); }
#pso-app button:active { transform: translateY(1px); }
#pso-app .select-label,
#pso-app .slider-label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 14px;
}
#pso-app .slider-label .value {
  float: right;
  color: #cfe0ff;
  font-variant-numeric: tabular-nums;
}
#pso-app select {
  width: 100%;
  margin-top: 6px;
  padding: 7px 8px;
  color-scheme: dark;
  background: #0e1322;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  cursor: pointer;
}
#pso-app select option { background: #0e1322; color: var(--text); }
#pso-app input[type="range"] {
  width: 100%;
  margin-top: 6px;
  accent-color: var(--accent);
  cursor: pointer;
}
#pso-app .toggles {
  margin-top: 4px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding-top: 12px;
}
#pso-app .toggles label {
  display: block;
  font-size: 0.82rem;
  color: var(--text);
  margin-bottom: 8px;
  cursor: pointer;
}
#pso-app .toggles input { margin-right: 8px; accent-color: var(--accent); }
#pso-app .hint {
  margin: 10px 0 0;
  font-size: 0.7rem;
  color: var(--text-dim);
  text-align: center;
}

/* Readout + convergence graph (bottom-right) */
#pso-app #readout {
  position: absolute;
  bottom: 14px;
  right: 14px;
  z-index: 3;
  width: min(290px, calc(100% - 28px));
  padding: 12px 14px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  backdrop-filter: blur(9px);
}
#pso-app .stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 8px;
}
#pso-app .stats b { color: var(--good); font-variant-numeric: tabular-nums; }
#pso-app #conv-graph {
  width: 100%;
  height: 120px;
  display: block;
  background: #0a0e18;
  border-radius: 8px;
}
#pso-app .graph-caption {
  margin: 6px 0 0;
  font-size: 0.68rem;
  color: var(--text-dim);
  text-align: center;
}

/* Responsive: declutter overlays on small playgrounds */
@media (max-width: 820px) {
  #pso-app #readout { display: none; }
}
@media (max-width: 560px) {
  #pso-app #control-panel { width: 200px; padding: 12px; max-height: calc(100% - 20px); top: 10px; left: 10px; }
  #pso-app .edu-label { display: none; }
}
