:root {
  --ink: #11141b;
  --ink-2: #161a23;
  --panel: #1b2030;
  --panel-2: #20273a;
  --line: #2b3650;
  --grid: rgba(96, 160, 220, 0.07);
  --text: #e8edf7;
  --muted: #8f9bb4;
  --cyan: #4fd6e0;
  --cyan-soft: rgba(79, 214, 224, 0.18);
  --yellow: #f5c518;
  --yellow-deep: #e0a800;
  --red: #ec4b43;
  --red-deep: #c5342d;
  --pos: #ff8a5c;
  --neg: #6fc3ff;
  --good: #7fe3a3;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
}

html, body { height: auto; }

body {
  background:
    radial-gradient(1200px 700px at 78% -10%, rgba(79, 214, 224, 0.10), transparent 60%),
    radial-gradient(900px 600px at -5% 110%, rgba(236, 75, 67, 0.10), transparent 55%),
    linear-gradient(180deg, var(--ink), var(--ink-2));
  color: var(--text);
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* film grain overlay */
.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.045;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---------- TOP BAR ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 22px clamp(18px, 4vw, 48px);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 16px; }

.spark {
  width: 38px; height: 38px;
  flex: none;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--yellow), var(--red));
  position: relative;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.08), 0 8px 22px rgba(236,75,67,0.35);
}
.spark::after {
  content: "";
  position: absolute; inset: 0;
  background:
    linear-gradient(45deg, transparent 44%, #fff 45%, transparent 47%),
    linear-gradient(-45deg, transparent 47%, #fff 48%, transparent 50%);
  opacity: 0.85;
  clip-path: polygon(50% 6%, 58% 42%, 92% 44%, 62% 60%, 74% 94%, 50% 70%, 26% 94%, 38% 60%, 8% 44%, 42% 42%);
  background: #fff;
}

h1 {
  font-family: "Bricolage Grotesque", sans-serif;
  font-weight: 800;
  font-size: clamp(1.5rem, 3.4vw, 2.35rem);
  letter-spacing: -0.02em;
  margin: 0;
  line-height: 0.98;
  background: linear-gradient(92deg, #fff 20%, var(--cyan) 120%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tagline {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.01em;
}

.assistant-btn {
  text-decoration: none;
  color: var(--ink);
  font-weight: 600;
  font-size: 0.92rem;
  padding: 12px 20px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--yellow), var(--yellow-deep));
  display: inline-flex;
  align-items: center;
  gap: 9px;
  box-shadow: 0 10px 26px rgba(245, 197, 24, 0.28);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
  white-space: nowrap;
}
.assistant-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 16px 34px rgba(245, 197, 24, 0.42);
}
.assistant-btn .arrow-glyph { transition: transform 0.18s ease; }
.assistant-btn:hover .arrow-glyph { transform: translateX(4px); }

/* ---------- WORKBENCH (vertical, scrolling) ---------- */
.workbench {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: clamp(16px, 2.5vw, 28px);
  padding: clamp(16px, 3vw, 32px) clamp(18px, 4vw, 48px);
  min-height: 0;
}

/* ---------- DECK (balloon settings, bottom) ---------- */
.deck {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  align-items: stretch;
}

.config {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px 18px;
  box-shadow: var(--shadow);
}

/* control panel sits alongside the two balloon configs */
.control-panel { display: flex; flex-direction: column; gap: 14px; }

.config-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.config-head h2 {
  font-family: "Bricolage Grotesque", sans-serif;
  font-size: 1.02rem; margin: 0; font-weight: 700; letter-spacing: 0.01em;
}
.swatch { width: 16px; height: 16px; border-radius: 5px; flex: none; }
.swatch-a { background: linear-gradient(135deg, var(--yellow), var(--yellow-deep)); }
.swatch-b { background: linear-gradient(135deg, var(--red), var(--red-deep)); }

.field {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin: 9px 0;
}
.field label {
  font-size: 0.84rem; color: var(--muted);
  display: flex; align-items: center; gap: 7px;
}
.field .sym {
  font-size: 0.7rem; color: var(--cyan);
  border: 1px solid var(--cyan-soft); padding: 1px 6px; border-radius: 6px;
  background: var(--cyan-soft);
}
.field input {
  width: 78px;
  background: var(--ink);
  border: 1px solid var(--line);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  text-align: center;
  padding: 8px 6px;
  border-radius: 9px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus {
  outline: none;
  border-color: var(--cyan);
  box-shadow: 0 0 0 3px var(--cyan-soft);
}
.field input:disabled { opacity: 0.5; cursor: not-allowed; }

.derived {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
  margin-top: 14px; padding-top: 13px; border-top: 1px dashed var(--line);
}
.derived > div { display: flex; flex-direction: column; gap: 3px; }
.dlabel { font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
.dval { font-size: 1.25rem; font-weight: 600; font-family: "Bricolage Grotesque", sans-serif; }
.dval.charge.pos { color: var(--pos); }
.dval.charge.neg { color: var(--neg); }
.dval.charge.zero { color: var(--good); }

.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn {
  font-family: "Bricolage Grotesque", sans-serif;
  font-weight: 700;
  font-size: 1rem;
  padding: 14px 10px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  letter-spacing: 0.01em;
  transition: transform 0.15s, box-shadow 0.15s, opacity 0.15s, filter 0.15s;
}
.btn:active { transform: translateY(1px) scale(0.99); }
.btn-start {
  color: #07291b;
  background: linear-gradient(135deg, var(--good), #46c97f);
  box-shadow: 0 10px 24px rgba(127, 227, 163, 0.25);
}
.btn-start:hover { filter: brightness(1.06); transform: translateY(-2px); }
.btn-stop {
  color: #fff;
  background: linear-gradient(135deg, #36405c, #2a3450);
  border: 1px solid var(--line);
}
.btn-stop:hover { filter: brightness(1.12); transform: translateY(-2px); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; filter: none; box-shadow: none; }

.readout-status {
  font-size: 0.82rem; color: var(--muted); line-height: 1.5;
  background: var(--ink); border: 1px solid var(--line);
  border-left: 3px solid var(--cyan);
  padding: 11px 13px; border-radius: 10px; margin: 0;
  min-height: 2.4em;
}

.legend { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 9px; }
.legend li { font-size: 0.76rem; color: var(--muted); display: flex; align-items: center; gap: 10px; line-height: 1.35; }
.key { width: 14px; height: 14px; border-radius: 4px; flex: none; }
.key-attract { background: var(--pos); }
.key-repel { background: var(--neg); }
.key-neutral { background: var(--good); }

/* ---------- STAGE ---------- */
.stage-wrap { min-width: 0; display: flex; }
.stage {
  position: relative;
  flex: 1;
  width: 100%;
  height: clamp(380px, 56vh, 620px);
  border-radius: 20px;
  border: 1px solid var(--line);
  background-color: #0e1119;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px),
    radial-gradient(700px 400px at 50% 40%, rgba(79,214,224,0.06), transparent 70%);
  background-size: 32px 32px, 32px 32px, 100% 100%;
  overflow: hidden;
  box-shadow: var(--shadow), inset 0 0 80px rgba(0,0,0,0.45);
  touch-action: none;
}

.overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }
#distline {
  stroke: var(--cyan);
  stroke-width: 2;
  stroke-dasharray: 7 6;
  opacity: 0;
  filter: drop-shadow(0 0 4px rgba(79,214,224,0.5));
  transition: opacity 0.25s;
}
.stage.running #distline { opacity: 0.9; }

.dist-badge {
  position: absolute;
  z-index: 4;
  transform: translate(-50%, -50%);
  background: var(--ink);
  border: 1px solid var(--cyan);
  color: var(--cyan);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.25s;
  box-shadow: 0 0 16px rgba(79,214,224,0.25);
  pointer-events: none;
}
.stage.running .dist-badge { opacity: 1; }

/* ---------- BALLOONS ---------- */
.balloon {
  position: absolute;
  width: 132px;
  height: 152px;
  left: 0; top: 0;
  cursor: grab;
  z-index: 5;
  user-select: none;
  will-change: transform;
  touch-action: none;
}
.balloon.grabbing { cursor: grabbing; z-index: 8; }
.balloon:focus-visible { outline: none; }
.balloon:focus-visible .balloon-body { box-shadow: 0 0 0 3px var(--cyan), inset 0 -18px 30px rgba(0,0,0,0.28); }

.balloon-body {
  position: relative;
  width: 132px;
  height: 152px;
  border-radius: 49% 49% 47% 47% / 44% 44% 56% 56%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-shadow: inset 0 -20px 34px rgba(0,0,0,0.28), inset 14px 10px 26px rgba(255,255,255,0.25), 0 16px 30px rgba(0,0,0,0.4);
  transition: box-shadow 0.2s;
}
.balloon-a .balloon-body {
  background: radial-gradient(circle at 34% 28%, #fff6c7, var(--yellow) 46%, var(--yellow-deep) 100%);
}
.balloon-b .balloon-body {
  background: radial-gradient(circle at 34% 28%, #ffb3ac, var(--red) 48%, var(--red-deep) 100%);
}
/* highlight glint */
.balloon-body::before {
  content: "";
  position: absolute;
  top: 16px; left: 22px;
  width: 30px; height: 42px;
  border-radius: 50%;
  background: rgba(255,255,255,0.6);
  filter: blur(3px);
  transform: rotate(-18deg);
}

.knot {
  position: absolute;
  left: 50%; bottom: -6px;
  width: 14px; height: 12px;
  transform: translateX(-50%);
  border-radius: 0 0 50% 50%;
  z-index: -1;
}
.balloon-a .knot { background: var(--yellow-deep); }
.balloon-b .knot { background: var(--red-deep); }
.string {
  position: absolute;
  left: 50%; bottom: -56px;
  width: 2px; height: 52px;
  transform: translateX(-50%);
  background: linear-gradient(180deg, rgba(255,255,255,0.5), rgba(255,255,255,0.05));
  border-radius: 2px;
}

.balloon-stats {
  display: flex;
  gap: 7px;
  align-items: flex-end;
}
.stat {
  display: flex; flex-direction: column; align-items: center;
  background: rgba(17,20,27,0.78);
  border-radius: 8px;
  padding: 4px 6px;
  min-width: 28px;
  line-height: 1;
}
.stat b { font-size: 0.92rem; font-weight: 700; color: #fff; font-family: "Bricolage Grotesque", sans-serif; }
.stat i { font-style: normal; font-size: 0.56rem; margin-top: 2px; letter-spacing: 0.02em; }
.stat-p i { color: var(--pos); }
.stat-e i { color: var(--neg); }
.stat-n i { color: var(--muted); }

.balloon-charge {
  font-family: "Bricolage Grotesque", sans-serif;
  font-weight: 800;
  font-size: 1.15rem;
  color: var(--ink);
  background: rgba(255,255,255,0.78);
  border-radius: 999px;
  padding: 1px 11px;
  min-width: 34px;
  text-align: center;
}

/* floating charge particles */
.particle {
  position: absolute;
  z-index: 6;
  width: 16px; height: 16px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 0.6rem;
  font-weight: 700;
  pointer-events: none;
  will-change: transform, opacity;
}
.particle.e { background: var(--neg); color: #06223a; box-shadow: 0 0 12px rgba(111,195,255,0.8); }
.particle.p { background: var(--pos); color: #3a1405; box-shadow: 0 0 12px rgba(255,138,92,0.8); }

@keyframes floatAway {
  0%   { transform: translate(0,0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(0.4); opacity: 0; }
}
@keyframes transferHop {
  0%   { transform: translate(0,0) scale(0.7); opacity: 0; }
  15%  { opacity: 1; transform: scale(1.1); }
  100% { transform: translate(var(--dx), var(--dy)) scale(0.8); opacity: 0; }
}

/* ---------- FORMULA BAR (panel under the settings) ---------- */
.formula-bar {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--ink-2), #0c0f16);
  padding: 18px clamp(18px, 3vw, 28px);
  display: flex;
  align-items: center;
  gap: clamp(16px, 4vw, 48px);
  flex-wrap: wrap;
  box-shadow: var(--shadow);
}
.formula-title {
  font-family: "Bricolage Grotesque", sans-serif;
  font-weight: 700;
  color: var(--cyan);
  font-size: 0.92rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}
.formula {
  display: flex; align-items: center; gap: 12px;
  font-size: clamp(1rem, 2.2vw, 1.5rem);
  flex-wrap: wrap;
}
.f-symbol { font-style: italic; font-weight: 700; color: #fff; }
.f-eq { color: var(--muted); }
.f-frac { display: inline-flex; flex-direction: column; align-items: center; }
.f-num { padding: 0 8px 3px; }
.f-den { padding: 3px 8px 0; border-top: 2px solid var(--muted); }
.f-num i, .f-den i { font-style: italic; color: var(--cyan); }
.f-result {
  font-weight: 700; color: var(--good);
  background: rgba(127,227,163,0.1);
  border: 1px solid rgba(127,227,163,0.3);
  padding: 4px 12px; border-radius: 9px;
  font-size: 0.95em;
}
.formula-meta {
  margin-left: auto;
  display: flex; flex-direction: column; gap: 6px; align-items: flex-end;
  font-size: 0.78rem; color: var(--muted);
}
.f-mode {
  text-transform: uppercase; letter-spacing: 0.08em; font-size: 0.7rem;
  padding: 3px 10px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--muted);
}
.f-mode.attract { color: var(--pos); border-color: var(--pos); }
.f-mode.repel { color: var(--neg); border-color: var(--neg); }
.f-mode.neutral { color: var(--good); border-color: var(--good); }
.f-vars b { color: var(--text); }

/* ---------- RESPONSIVE ---------- */
@media (max-width: 880px) {
  .stage { height: clamp(340px, 52vh, 520px); }
  .formula-meta { margin-left: 0; align-items: flex-start; }
  .formula-bar { gap: 14px; }
}
