:root {
  --serif: "Source Serif 4", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --bg: #f4f2ee;
  --panel: #fbfaf8;
  --border: #e0dcd4;
  --text: #2b2a27;
  --muted: #6b6760;
  --light: #9a958c;
  --accent: #8a4b2f;
  --ok: #2f7d4f;
  --bad: #b23a2e;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--sans);
  color: var(--text);
  background: var(--bg);
}

#overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}
#overlay h2 { font-family: var(--serif); font-weight: 400; font-size: 1.6rem; margin: 0; }
#overlay h2 span { color: var(--accent); font-weight: 700; }
.load-bar-bg { width: 280px; height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; }
.load-bar-fill { height: 100%; background: var(--accent); transition: width 0.3s; }

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.8rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.site-title { font-family: var(--serif); font-size: 1.1rem; }
.site-title span:first-child { color: var(--accent); font-weight: 700; }
.poc {
  font-family: var(--sans); font-size: 0.62rem; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--light); border: 1px solid var(--border);
  padding: 0.1rem 0.4rem; border-radius: 3px; margin-left: 0.4rem;
}
.back { color: var(--muted); text-decoration: none; font-size: 0.82rem; }
.back:hover { color: var(--accent); }

#layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: calc(100% - 53px);
}

#panel {
  overflow-y: auto;
  border-right: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.9rem;
}
.card h3 {
  font-family: var(--serif); font-weight: 600; font-size: 0.95rem;
  margin: 0 0 0.7rem;
}

.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.xsmall { font-size: 0.7rem; }

#drop {
  display: flex; align-items: center; justify-content: center;
  text-align: center;
  min-height: 64px; padding: 0.8rem;
  border: 1.5px dashed var(--border); border-radius: 6px;
  color: var(--muted); font-size: 0.82rem; cursor: pointer;
}
#drop.over { border-color: var(--accent); color: var(--accent); background: #fff; }
#meshInfo { margin-top: 0.6rem; }

.field { margin-bottom: 0.8rem; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 0.8rem; margin-bottom: 0.3rem; }
.field.row { display: flex; gap: 0.6rem; }
.field.row > div { flex: 1; }
input[type="number"], select {
  width: 100%; padding: 0.35rem 0.5rem;
  border: 1px solid var(--border); border-radius: 4px;
  font-family: var(--sans); font-size: 0.85rem; background: #fff;
}
input[type="range"] { width: 100%; accent-color: var(--accent); }

/* ── Two-step run control (1 · preview the mesh → 2 · solve) ── */
.run-steps { display: flex; flex-direction: column; }
.step {
  display: flex; align-items: center; gap: 0.7rem; width: 100%;
  padding: 0.6rem 0.75rem; text-align: left; cursor: pointer;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  font-family: var(--sans);
  transition: border-color 0.15s, transform 0.1s, box-shadow 0.15s, background 0.15s;
}
.step:hover:not(:disabled) {
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 3px 10px -6px rgba(138, 75, 47, 0.55);
}
.step:active:not(:disabled) { transform: translateY(0); }
.step .step-num {
  flex: none; width: 1.9rem; height: 1.9rem;
  display: flex; align-items: center; justify-content: center;
  font-family: var(--serif); font-style: italic; font-size: 1.15rem;
  color: var(--accent); background: var(--bg);
  border: 1px solid var(--border); border-radius: 50%;
  font-variant-numeric: lining-nums;
}
.step .step-text { display: flex; flex-direction: column; line-height: 1.25; }
.step .step-label { font-size: 0.9rem; font-weight: 600; }
.step .step-sub { font-size: 0.7rem; color: var(--muted); }

/* Step 2 is the primary action — solid accent. */
.step.step-primary {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.step.step-primary .step-num {
  color: #fff; background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.35);
}
.step.step-primary .step-sub { color: rgba(255, 255, 255, 0.8); }
.step.step-primary:hover:not(:disabled) {
  background: #7a4029; border-color: #7a4029;
  box-shadow: 0 4px 14px -6px rgba(138, 75, 47, 0.75);
}

.step:disabled { cursor: not-allowed; opacity: 0.5; }

/* Connector between the two steps: a short rule + downward chevron = "then". */
.step-link {
  align-self: center; position: relative;
  width: 1px; height: 0.85rem; margin: 0.15rem 0;
  background: var(--border);
}
.step-link::after {
  content: ""; position: absolute; left: 50%; bottom: -1px;
  width: 5px; height: 5px; border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  transform: translateX(-50%) rotate(45deg);
}

#runMsg { margin-top: 0.6rem; }

/* ── Run progress UI (replaces the button while a solve is running) ── */
.run-wrap { position: relative; }
.run-progress {
  display: flex; flex-direction: column; gap: 0.35rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border); border-radius: 5px;
  background: #fff;
}
.run-progress-row {
  display: flex; align-items: center; gap: 0.55rem;
}
.run-bar {
  flex: 1; height: 6px;
  background: var(--border); border-radius: 3px; overflow: hidden;
}
.run-bar-fill {
  height: 100%; width: 0%;
  background: var(--accent);
  transition: width 120ms linear;
}
.run-progress-text {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 0.75rem; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
button.run-cancel {
  background: transparent; border: none; cursor: pointer;
  font-size: 0.95rem; line-height: 1; padding: 2px 4px;
  color: var(--light); transition: color 0.15s;
}
button.run-cancel:hover { color: var(--bad); }

/* ── Toast ── */
#toastHost {
  position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 0.4rem;
  z-index: 1200; pointer-events: none;
}
.toast {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 5px;
  padding: 0.5rem 0.85rem; font-size: 0.82rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  opacity: 0; transform: translateY(6px);
  transition: opacity 0.18s, transform 0.18s;
  pointer-events: auto;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { border-color: var(--bad); color: var(--bad); }

/* ── Download buttons ── */
.download-row {
  display: flex; gap: 0.5rem; margin-top: 0.7rem;
}
button.download-btn {
  flex: 1; padding: 0.4rem 0.5rem;
  background: #fff; color: var(--text);
  border: 1px solid var(--border); border-radius: 4px;
  font-family: var(--sans); font-size: 0.78rem; cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
button.download-btn:hover { border-color: var(--accent); color: var(--accent); }
button.download-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.checkrow { display: flex; align-items: center; gap: 0.4rem; font-size: 0.8rem; margin-bottom: 0.7rem; }

.pf-row { font-size: 0.85rem; margin-bottom: 0.4rem; }
.badge {
  font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.05em;
  padding: 0.05rem 0.4rem; border-radius: 3px; color: #fff; margin-left: 0.3rem;
}
.badge.ok { background: var(--ok); }
.badge.bad { background: var(--bad); }

table { width: 100%; border-collapse: collapse; font-size: 0.82rem; }
table.facts { margin-top: 0.5rem; }
table td { padding: 0.25rem 0; border-top: 1px solid var(--border); }
table td:last-child { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
#metrics td:last-child { color: var(--text); font-weight: 500; }

/* ─── Info button (ⓘ) + popover modal ─── */
.info-btn {
  background: none; border: none; padding: 0 0 0 3px;
  font-size: 0.78rem; color: var(--light); cursor: pointer;
  line-height: 1; vertical-align: middle; transition: color 0.15s;
}
.info-btn:hover { color: var(--accent); }

#popover-backdrop {
  display: none; position: fixed; inset: 0; z-index: 1100;
  background: rgba(0, 0, 0, 0.35);
  align-items: center; justify-content: center; padding: 16px;
}
#popover-backdrop.on { display: flex; }
#popover-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 6px; padding: 18px 20px 16px;
  max-width: 340px; width: 100%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  position: relative; font-size: 0.82rem; line-height: 1.65; color: var(--text);
}
#popover-body b { color: var(--text); font-weight: 600; }
#popover-body code {
  font-family: ui-monospace, Menlo, monospace; font-size: 0.78rem;
  background: var(--bg); padding: 0 0.2em; border-radius: 3px;
}
#popover-close {
  position: absolute; top: 8px; right: 10px;
  background: none; border: none; font-size: 0.9rem;
  color: var(--light); cursor: pointer; padding: 2px 4px; transition: color 0.15s;
}
#popover-close:hover { color: var(--text); }

#viewerWrap { position: relative; }
#viewer { position: absolute; inset: 0; }

#viewToggle {
  position: absolute; top: 1rem; left: 1rem;
  display: inline-flex; gap: 0;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--border); border-radius: 5px;
  padding: 3px; overflow: hidden;
  font-size: 0.78rem; font-family: var(--sans);
}
#viewToggle button {
  background: transparent; color: var(--muted);
  border: 0; border-radius: 3px; cursor: pointer;
  padding: 0.3rem 0.7rem;
  transition: background 0.15s, color 0.15s;
}
#viewToggle button:hover:not(:disabled) { color: var(--text); }
#viewToggle button:disabled { opacity: 0.4; cursor: not-allowed; }
#viewToggle button.active {
  background: var(--accent); color: #fff;
}

#legend {
  position: absolute; bottom: 1rem; left: 1rem;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--border); border-radius: 5px;
  padding: 0.5rem 0.7rem; width: 200px;
}
#legendBar {
  height: 10px; border-radius: 3px;
  background: linear-gradient(90deg,
    rgb(0,0,140), rgb(0,0,255), rgb(0,255,255),
    rgb(255,255,0), rgb(255,0,0), rgb(128,0,0));
}
.legend-labels {
  display: flex; justify-content: space-between;
  font-size: 0.7rem; color: var(--muted); margin-top: 0.25rem;
  font-variant-numeric: tabular-nums;
}
