:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --ink: #1c1c1a;
  --ink-2: #5a5955;
  --line: #dcdad4;
  --accent: #1f5fbf;
  --accent-ink: #ffffff;
  --warn: #b35c00;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--sans);
  font-size: 14px;
  color: var(--ink);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-2); margin: 0 0 8px; }
label { display: flex; flex-direction: column; gap: 3px; font-size: 12.5px; color: var(--ink-2); }
input[type=text], input[type=number], select {
  font: inherit; font-size: 13px; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px;
  background: #fff; color: var(--ink); min-width: 0; width: 100%;
}
input[type=range] { width: 100%; }
.btn {
  font: inherit; font-size: 13px; padding: 6px 12px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  text-decoration: none; display: inline-block;
}
.btn:hover { filter: brightness(1.08); }
.btn-quiet { background: transparent; color: var(--accent); }
.btn-small { padding: 4px 9px; font-size: 12px; }
.btn-primary { width: 100%; padding: 8px; font-weight: 600; }
.btn[disabled] { opacity: 0.5; cursor: default; }
.hint { font-size: 11.5px; color: var(--ink-2); margin: 4px 0 6px; }
.row { display: flex; gap: 8px; align-items: flex-end; margin: 6px 0; flex-wrap: wrap; }
.row > label { flex: 1 1 60px; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 18px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; flex-direction: column; line-height: 1.1; min-width: 150px; }
.brand-title { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--ink-2); }
.tabs { display: flex; flex-wrap: wrap; gap: 2px 10px; flex: 1; align-items: flex-end; }
.tab-group { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 2px; padding: 2px 6px 0 6px; border-radius: 7px 7px 0 0; max-width: 100%; min-width: 0; --tint: transparent; --tint-ink: var(--ink-2); background: var(--tint); }
/* one faint tint per topic so the groups read at a glance; the label takes a darker shade of the same hue */
.tab-group[data-group="Electrostatics"] { --tint: #eaf1fb; --tint-ink: #2f5fa8; }
.tab-group[data-group="Currents & circuits"] { --tint: #fdf3e2; --tint-ink: #9a6410; }
.tab-group[data-group="Magnetostatics"] { --tint: #e8f5ea; --tint-ink: #2d7a3a; }
.tab-group[data-group="Induction"] { --tint: #f0ecfa; --tint-ink: #6344b8; }
.tab-group[data-group="Waves"] { --tint: #e4f4f3; --tint-ink: #1f7a78; }
.tab-group[data-group="Tools"] { --tint: #f0efec; --tint-ink: #5a5955; }
.tab-group-label { font-size: 10px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--tint-ink); padding: 0 4px 8px 0; white-space: nowrap; }
.tab {
  font: inherit; font-size: 12.5px; padding: 5px 7px; border: 0; border-bottom: 2px solid transparent;
  background: transparent; color: var(--ink-2); cursor: pointer; white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected=true] { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tab .phase { font-size: 10px; color: var(--ink-2); margin-left: 4px; }
.topbar-actions { display: flex; gap: 8px; }
.assign-banner[hidden] { display: none; }
.assign-banner { display: flex; gap: 12px; align-items: center; padding: 6px 16px; background: var(--accent); color: #fff; font-size: 13px; }
.assign-banner .assign-tag { font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; font-size: 11px; }
.assign-banner a { color: #fff; margin-left: auto; opacity: 0.85; }
.assign-box { margin-top: 8px; font-size: 12.5px; }
.foot-version { margin-left: auto; color: var(--ink-2); font-family: ui-monospace, monospace; }
.assign-box summary { cursor: pointer; color: var(--ink-2); }
.assign-box input[type=text] { width: 100%; box-sizing: border-box; }
body.locked .topbar .tabs, body.locked #controls, body.locked #resize-left, body.locked #resize-right, body.locked #reset-btn { display: none; }
body.locked .workspace { grid-template-columns: 1fr 6px var(--right-w); }

/* workspace */
.workspace {
  --left-w: 270px; --right-w: 300px;
  flex: 1; display: grid; grid-template-columns: var(--left-w) 6px 1fr 6px var(--right-w); gap: 0; min-height: 0;
}
.resizer { cursor: col-resize; background: transparent; position: relative; }
.resizer::after { content: ''; position: absolute; top: 0; bottom: 0; left: 2px; width: 2px; background: var(--line); }
.resizer:hover::after, .resizer.active::after { background: var(--accent); }
.range-hint { font-size: 10.5px; color: var(--ink-2); font-weight: 400; margin-left: 4px; }
.panel { background: var(--panel); padding: 12px 14px; overflow-y: auto; }
.panel-left { border-right: 1px solid var(--line); }
.panel-right { border-left: 1px solid var(--line); }
.panel-section { padding-bottom: 12px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
.panel-section:last-child { border-bottom: 0; }
.panel h1 { font-size: 16px; margin: 0 0 4px; }
.panel .lead { font-size: 12.5px; color: var(--ink-2); margin: 0 0 10px; }

.stage { display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 14px; min-width: 0;
  /* --ar: aspect of the field canvas (set by the shell from the tab's extent); --par: aspect of the plot views, at least 3:2;
     --war: whichever is showing. The box is as tall as the window allows and as wide as its aspect wants, so wide extents and
     plots use the width of a wide screen instead of sitting in a square. */
  --ar: 1; --par: max(var(--ar), 1.5); --war: var(--ar); --stage-h: calc(100vh - 240px); }
.stage-tabs { display: flex; gap: 2px; width: min(100%, calc(var(--stage-h) * var(--war))); margin-bottom: 6px; }
.stage-tabs:empty { display: none; }
.stage-tabs .stab { font: inherit; font-size: 12.5px; padding: 5px 10px; border: 1px solid var(--line); border-bottom: 0; border-radius: 6px 6px 0 0; background: #f1f0ec; color: var(--ink-2); cursor: pointer; }
.stage-tabs .stab[aria-selected=true] { background: var(--panel); color: var(--accent); font-weight: 600; }
.stage-view { width: min(100%, calc(var(--stage-h) * var(--par))); aspect-ratio: var(--par); max-height: var(--stage-h); background: var(--panel); border-radius: 8px; box-shadow: 0 1px 4px rgba(0,0,0,0.15); overflow: hidden; display: flex; flex-direction: column; }
.stage-view[hidden] { display: none; }
.stage-view.page { aspect-ratio: auto; width: 100%; max-width: 980px; height: calc(100vh - 150px); max-height: none; }
.stage-view canvas { width: 100%; height: 100%; display: block; }
.stage-view .view-controls { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; padding: 6px 10px; border-bottom: 1px solid var(--line); font-size: 12px; color: var(--ink-2); }
.stage-view .view-controls > span, .stage-view .view-controls > label { white-space: nowrap; }
.stage-view .view-controls label { flex-direction: row; align-items: center; gap: 6px; }
.stage-view .view-controls input[type=range] { width: 120px; }
.stage-view .view-body { flex: 1; min-height: 0; position: relative; }
.canvas-stack {
  position: relative; width: min(100%, calc(var(--stage-h) * var(--ar))); aspect-ratio: var(--ar); max-height: var(--stage-h);
  background: #111; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 4px rgba(0,0,0,0.15);
}
.canvas-stack canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.canvas-stack canvas.hidden { display: none; }
.canvas-stack .overlay-note {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: #ccc; font-size: 14px; text-align: center; padding: 24px; pointer-events: none;
}
.stage-footer { display: flex; align-items: center; gap: 16px; width: min(100%, calc(var(--stage-h) * var(--war))); margin-top: 8px; }
.colorbar { display: flex; flex-direction: column; gap: 3px; width: 220px; }
.colorbar canvas { width: 220px; height: 12px; border-radius: 3px; border: 1px solid var(--line); }
.colorbar-labels { display: flex; flex-direction: row-reverse; justify-content: space-between; font-family: var(--mono); font-size: 11px; color: var(--ink-2); }
.colorbar-title { font-size: 11px; color: var(--ink-2); }
.stage-status { font-size: 12px; color: var(--ink-2); flex: 1; }

/* layers */
.layer-list { display: flex; flex-direction: column; gap: 4px; }
.layer { display: flex; flex-direction: row; align-items: center; gap: 8px; font-size: 13px; color: var(--ink); }
.layer input { margin: 0; }
.layer .sw { width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--line); }
.layer.na { color: #aaa; }
.layer .sub { font-size: 11px; color: var(--ink-2); margin-left: auto; }
.layer select { width: auto; font-size: 11.5px; padding: 2px 4px; margin-left: auto; }

.row-minmax label { flex: 1 1 40px; }
.row-probe label { flex: 1 1 70px; }

.probe-table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 12px; margin-top: 4px; }
.probe-table td { padding: 2px 4px; border-bottom: 1px solid var(--line); }
.probe-table td:first-child { color: var(--ink-2); font-family: var(--sans); }
.probe-table td:last-child { text-align: right; white-space: nowrap; }
.panel-left .probe-table td:last-child { white-space: normal; }
.panel-left .probe-table td:first-child { width: 55%; }

/* equations */
.equations { display: flex; flex-direction: column; gap: 10px; }
.eq { border-left: 3px solid var(--line); padding: 2px 0 2px 10px; }
.eq.eq-governing { border-left-color: var(--accent); }
.eq .eq-title { font-size: 11px; color: var(--ink-2); text-transform: uppercase; letter-spacing: 0.05em; }
.eq .eq-tex { font-size: 14px; overflow-x: auto; }
.eq .eq-tex .katex-display { margin: 4px 0; }
.eq .eq-note { font-size: 11.5px; color: var(--ink-2); margin-top: 2px; }
.eq.eq-zero .eq-tex { color: #999; }
.eq-fallback { font-family: var(--mono); font-size: 12px; white-space: pre-wrap; }

/* controls: charge/wire source rows */
.source-row { display: grid; grid-template-columns: 22px 1fr 1fr 1fr; gap: 6px; align-items: end; margin: 4px 0; }
.source-row .idx { font-weight: 600; color: var(--ink-2); padding-bottom: 6px; }
.source-row.hidden-values input { background: #f1f1f1; color: transparent; text-shadow: 0 0 6px rgba(0,0,0,0.4); }
.badge { display: inline-block; font-size: 11px; padding: 2px 7px; border-radius: 10px; background: #eee; color: var(--ink-2); }
.badge-warn { background: #fff1e0; color: var(--warn); }
.placeholder { color: var(--ink-2); font-size: 13px; line-height: 1.5; }
.placeholder code { font-family: var(--mono); font-size: 12px; }

.foot { display: flex; gap: 16px; padding: 6px 16px; font-size: 11.5px; color: var(--ink-2); border-top: 1px solid var(--line); background: var(--panel); }

@media (max-width: 1100px) {
  .workspace, body.locked .workspace { grid-template-columns: 1fr; }
  .resizer { display: none; }
  .panel-left, .panel-right { border: 0; border-bottom: 1px solid var(--line); }
  .canvas-stack, .stage-footer { width: 100%; }
}

/* About page */
.panel-right.panel-empty .panel-section { display: none; }
.about-page { overflow-y: auto; padding: 18px 28px 40px; font-size: 14px; line-height: 1.5; height: 100%; }
.about-page h1 { font-size: 22px; margin: 0 0 8px; }
.about-page h2 { font-size: 16px; text-transform: none; letter-spacing: 0; color: var(--ink); margin: 22px 0 6px; border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.about-page h3 { font-size: 15px; margin: 20px 0 4px; display: flex; align-items: center; }
.about-page p { margin: 6px 0; }
.about-page .about-sub { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-2); margin: 10px 0 2px; }
.about-page ul { margin: 2px 0 6px; padding-left: 20px; }
.about-page li { margin: 2px 0; }
.about-page dl { display: grid; grid-template-columns: 110px 1fr; gap: 4px 12px; margin: 6px 0; }
.about-page dt { font-weight: 600; color: var(--ink-2); }
.about-page dd { margin: 0; }
.about-eqs { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 8px 16px; margin: 4px 0 8px; }
.about-link { font-size: 12px; color: var(--accent); text-decoration: none; white-space: nowrap; }
.stage-view .about-page { background: var(--panel); }
