/* =============================================================
   Chemulate VSEPR: the builder page (index.html).
   Standalone from styles.css (the polarity app). Tokens only: every colour
   comes from design-system.css. Rules: nothing scrolls, no overflow:auto|scroll,
   text >= 12px (subscripts >= 11px), popups are native dialogs.
   Sizes: 1280x720, 1366x768 and 1024x640 = stage left, tray right
          (360px; 300px at 1024 wide); 820x1180 = stage on top (55%), tray below in two columns.
   ============================================================= */

[hidden] { display: none !important; }

html, body { height: 100%; overflow: hidden; }
body {
  margin: 0;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
}

/* ---------- app bar ---------- */
.vs-bar {
  position: relative; z-index: 40; flex: none;
  display: flex; align-items: center; flex-wrap: wrap;
  gap: var(--sp-2) var(--sp-4);
  padding: var(--sp-2) var(--sp-4);
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.vs-bar__btn { min-height: var(--control-h); padding: 6px 14px; font: var(--t-small); font-weight: var(--w-semi); }
.vs-brand {
  display: inline-flex; align-items: center; gap: var(--sp-2);
  margin: 0; flex: none; line-height: 1;
}
.vs-brand img { height: 30px; width: auto; }
.vs-brand__title {
  font: var(--w-display) 1.0625rem/1.2 var(--font-display);
  letter-spacing: var(--tr-heading); color: var(--title); white-space: nowrap;
}
.vs-tools {
  margin-left: auto; display: flex; align-items: center; flex-wrap: wrap;
  gap: var(--sp-2); min-width: 0;
}
.vs-bar .theme-toggle { position: static; flex: none; width: var(--control-h); height: var(--control-h); font-size: 1.05rem; box-shadow: none; }

/* ---------- main: stage left, tray right ---------- */
.vs-main {
  flex: 1 1 0; min-height: 0; min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(300px, 28vw, 360px);
  grid-template-rows: minmax(0, 1fr);
  gap: var(--sp-4);
  padding: var(--sp-4);
}

/* ---------- stage ---------- */
.vs-stage-col {
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column; gap: var(--sp-2);
}
.vs-toolbar { flex: none; display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-2); min-width: 0; }
.vs-toggle { min-height: 36px; padding: 4px 14px; font: var(--t-small); font-weight: var(--w-semi); }
.vs-toggle[aria-pressed="true"] { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.vs-toggle[aria-pressed="true"]:hover { background: var(--primary-hover); }

/* the scene and the inset shape panel share the card: scene left, panel right */
.vs-sim {
  flex: 1 1 0; min-height: 0; min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(204px, 22%, 224px);
  grid-template-rows: minmax(0, 1fr);
  gap: var(--sp-2);
  padding: var(--sp-2);
}
#vs-stage {
  display: block; width: 100%; height: 100%; min-width: 0; min-height: 0;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
#vs-stage:focus { outline: none; }
#vs-stage:focus-visible { outline: 2px solid var(--primary); outline-offset: 4px; border-radius: var(--r-sm); }

.vs-shape-panel {
  margin: 0; min-width: 0; min-height: 0;
  display: flex; flex-direction: column; align-items: stretch; justify-content: center; gap: 4px;
  padding: 6px;
  background: var(--bg-tint); border: 1px solid var(--line); border-radius: var(--r-md);
}
/* the inset fills the panel width (its caption stays at 12px or more on screen: vsepr-shape.js floors it at 11) */
#vs-shape {
  display: block; width: 100%; height: auto; max-height: none; min-height: 0; flex: none;
  aspect-ratio: 1 / 1; margin: auto 0;
}
.vs-shape-cap { margin: 0; font: var(--w-semi) 13px/1.2 var(--font-body); color: var(--muted); text-align: center; }

.vs-pickbar { flex: none; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px var(--sp-3); min-width: 0; }
.vs-pick { margin: 0; max-width: none; font: var(--w-semi) 14px/1.3 var(--font-body); color: var(--muted); overflow-wrap: anywhere; }
.vs-pick--on { color: var(--ink-2); }
.vs-measure { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; min-width: 0; font: var(--w-normal) 13px/1.3 var(--font-body); color: var(--muted); }
.vs-m-select { flex: 0 1 150px; min-width: 0; width: 150px; min-height: 32px; padding: 4px 8px; font-size: 14px; }
.vs-caption { flex: none; margin: 0; max-width: none; font: var(--t-small); color: var(--muted); }

/* ---------- tray ---------- */
.vs-tray {
  min-width: 0; min-height: 0;
  display: flex; flex-direction: column; gap: var(--sp-3);
  padding: var(--sp-4);
  overflow: hidden;
}
.vs-field { display: flex; flex-direction: column; gap: var(--sp-1); min-width: 0; }
.vs-label {
  margin: 0; max-width: none;
  font: var(--w-semi) var(--fs-small)/1.3 var(--font-body); color: var(--ink-2);
}
.vs-select { width: 100%; min-height: var(--control-h); padding: 8px 12px; font: var(--t-body); font-size: 15px; }

/* mode switch: two equal segments */
.vs-modes { flex: none; display: grid; grid-template-columns: 1fr 1fr; gap: 0; padding: 3px; background: var(--bg-tint); border: 1px solid var(--line-strong); border-radius: var(--r-pill); }
.vs-mode {
  min-height: 38px; padding: 4px 10px; border: 0; border-radius: var(--r-pill);
  background: transparent; font: var(--t-small); font-weight: var(--w-semi); color: var(--ink-2);
}
.vs-mode:hover { background: var(--card); }
.vs-mode[aria-pressed="true"] { background: var(--primary); color: var(--primary-ink); box-shadow: var(--shadow-1); }
.vs-mode[aria-pressed="true"]:hover { background: var(--primary-hover); }

/* simulate: counts line, four controls, reset */
.vs-counts { margin: 0; max-width: none; font: var(--w-semi) 16px/1.3 var(--font-body); color: var(--ink-2); }
.vs-ctl-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.vs-ctl { min-height: 36px; padding: 4px 8px; font: var(--t-small); font-weight: var(--w-semi); }
.vs-ctl:disabled { opacity: .45; cursor: not-allowed; }
.vs-ctl--reset { width: 100%; margin-top: 2px; }

/* VSEPR class quick-jump: one row per domain count (2, 3, 4, 5, 6) */
.vs-classes { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.vs-chips { display: flex; flex-direction: column; gap: 4px; }
.vs-class-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; min-width: 0; }
.vs-row-n {
  flex: none; width: 16px; text-align: center;
  font: var(--w-semi) 12px/1 var(--font-body); color: var(--muted);
}
.vs-chip {
  flex: 0 0 auto; min-width: 58px; min-height: 30px; margin: 0;
  padding: 3px 8px;
  font: var(--w-semi) 14px/1.2 var(--font-body); color: var(--ink-2); white-space: nowrap;
  background: var(--card); border: 1px solid var(--line-strong); border-radius: var(--r-pill);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.vs-chip:hover { border-color: var(--primary); background: var(--bg-tint); }
.vs-chip[aria-pressed="true"] { background: var(--primary); border-color: transparent; color: var(--primary-ink); }
.vs-chip sub, .vs-chip-out sub, .vs-row-btn sub { font-size: 11px; line-height: 0; vertical-align: -0.28em; }

/* real molecules: the picker */
.vs-real-ctl { gap: var(--sp-1); }

/* readout card */
.vs-readout {
  margin-top: auto;
  display: flex; flex-direction: column; gap: 6px;
  padding: var(--sp-3);
  background: var(--bg-tint); border: 1px solid var(--line-strong); border-radius: var(--r-md);
  min-width: 0;
}
.vs-readout > .vs-label { margin: 0; }
.vs-readout__list { margin: 0; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 4px 12px; align-items: baseline; }
.vs-readout dt { margin: 0; font: var(--w-semi) 13px/1.3 var(--font-body); color: var(--muted); white-space: nowrap; }
.vs-readout dd { margin: 0; min-width: 0; font: var(--w-semi) 15px/1.35 var(--font-body); color: var(--ink-2); overflow-wrap: anywhere; }
.vs-chip-out {
  display: inline-flex; align-items: center;
  padding: 1px 9px; border-radius: var(--r-pill);
  font: var(--w-semi) 14px/1.4 var(--font-body);
  background: var(--primary-soft); color: var(--primary-soft-ink);
}
.vs-src { font: var(--w-normal) 13px/1.3 var(--font-body); color: var(--muted); }
.vs-angle-item { display: inline-block; white-space: nowrap; margin-right: 8px; }
.vs-verdict {
  display: inline-flex; align-items: center;
  padding: 3px 11px; border-radius: var(--r-pill);
  font: var(--w-semi) 14px/1.3 var(--font-body);
  background: var(--primary-soft); color: var(--primary-soft-ink);
}
.vs-verdict--apolar { background: var(--accent-soft); color: var(--accent-soft-ink); }
.vs-note { margin: 0; max-width: none; font: var(--w-normal) 13px/1.35 var(--font-body); color: var(--muted); }

/* the warning for an in-between count: obvious, not colour alone (the word and the border) */
.vs-warn {
  margin: 0; max-width: none;
  padding: 6px 10px;
  font: var(--w-semi) 14px/1.3 var(--font-body);
  color: var(--warn-ink);
  background: var(--warn-soft);
  border: 1px solid var(--warn);
  border-radius: var(--r-sm);
}
.vs-readout--invalid { border-color: var(--warn); }

/* ---------- bond orders: the popover (inside the stage card) and the Bond types dialog ---------- */
.vs-sim { position: relative; }
.vs-bondpop {
  position: absolute; z-index: 6;
  top: 8px; left: 8px; width: 220px; box-sizing: border-box;
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px 12px;
  background: var(--card); border: 1px solid var(--line-strong); border-radius: var(--r-md);
  box-shadow: var(--shadow-2);
}
.vs-bondpop__title { margin: 0; max-width: none; font: var(--w-semi) 14px/1.3 var(--font-body); color: var(--ink-2); white-space: nowrap; }
.vs-bondpop__row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.vs-bondpop__btn { min-height: 34px; padding: 4px 4px; font: var(--w-semi) 14px/1.2 var(--font-body); color: var(--ink-2); }
.vs-bondpop__btn[aria-pressed="true"] { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.vs-bondpop__btn[aria-pressed="true"]:hover { background: var(--primary-hover); }
.vs-bond-list { display: flex; flex-direction: column; gap: 8px; }
.vs-bond-row { display: grid; grid-template-columns: minmax(0, 1fr) 150px; align-items: center; gap: 12px; }
.vs-bond-row__label { font: var(--w-semi) 15px/1.3 var(--font-body); color: var(--ink-2); }
.vs-bond-row__sel { width: 150px; min-height: 36px; font-size: 15px; }

/* ---------- popups (native dialog) ---------- */
dialog.pop {
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--card);
  color: var(--ink);
  box-shadow: var(--shadow-3);
  width: min(92vw, 720px);
  max-width: none;
  max-height: 96vh; max-height: 96dvh;
  overflow: hidden;
}
dialog.pop[open] {
  display: flex; flex-direction: column;
  animation: vsRise var(--dur-slow) var(--ease) backwards;
}
dialog.pop::backdrop { background: var(--overlay); }
.pop-body {
  flex: 1 1 auto; min-height: 0;
  display: flex; flex-direction: column; gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-5) var(--sp-5);
}
.pop-head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.pop-title { margin: 0; max-width: none; font: var(--t-h2); letter-spacing: var(--tr-heading); color: var(--ink-2); }
.pop-close { width: var(--control-h); padding: 0; flex: none; }
.pop-content { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: var(--sp-3); overflow: hidden; }
.pop-content p, .pop-content li { font: var(--w-normal) 15px/1.5 var(--font-body); color: var(--ink); max-width: none; margin: 0; }
.pop-note { color: var(--muted) !important; font-size: 14px !important; }
.pop-credit { color: var(--muted) !important; font-size: 13px !important; }

/* Classes table: 15 rows, one per class, fits without scrolling */
.vs-table { width: 100%; border-collapse: collapse; }
.vs-table th {
  padding: 4px 8px; text-align: left; vertical-align: bottom; line-height: 1.25;
  font: var(--w-semi) 13px/1.25 var(--font-body); color: var(--muted);
  border-bottom: 1px solid var(--line-strong);
}
.vs-table td {
  padding: 3px 8px; text-align: left;
  font: var(--w-normal) 14px/1.3 var(--font-body); color: var(--ink);
  border-bottom: 1px solid var(--line);
}
.vs-table tbody tr { cursor: pointer; }
.vs-table tbody tr:hover td { background: var(--bg-tint); }
.vs-table tbody tr[aria-current="true"] td { background: var(--primary-soft); color: var(--primary-soft-ink); }
.vs-table td:nth-child(2), .vs-table td:nth-child(3),
.vs-table th:nth-child(2), .vs-table th:nth-child(3) { text-align: center; font-variant-numeric: tabular-nums; }
.vs-row-btn {
  padding: 2px 8px; margin: -2px -8px;
  font: var(--w-semi) 14px/1.3 var(--font-body); color: var(--link);
  background: none; border: 0; border-radius: var(--r-sm); cursor: pointer;
  white-space: nowrap;
}

/* About: five points */
.vs-about { margin: 0; padding-left: 1.25em; display: flex; flex-direction: column; gap: 8px; }
.vs-about strong { font-weight: var(--w-semi); color: var(--ink-2); }

@keyframes vsRise {
  0% { opacity: 0; transform: translateY(24px) scale(.98); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- portrait tablet (820x1180): stage on top (55%), tray below in two columns ---------- */
@media (orientation: portrait), (max-width: 899px) {
  .vs-main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 55fr) minmax(0, 45fr); }
  .vs-tray {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas: "modes modes" "sim readout";
    align-content: start;
    column-gap: var(--sp-4); row-gap: var(--sp-3);
    overflow: hidden;
  }
  .vs-modes { grid-area: modes; }
  .vs-sim-ctl, .vs-real-ctl { grid-area: sim; }
  .vs-readout { grid-area: readout; margin-top: 0; }
  .vs-classes { margin-top: var(--sp-2); }
}

/* ---------- short screens (1024x640, 1280x720, 1366x768): tighter tray ---------- */
@media (max-height: 800px) {
  .vs-main { padding: var(--sp-3); gap: var(--sp-3); }
  .vs-tray { padding: 10px; gap: 6px; }
  .vs-bar { padding: var(--sp-2) var(--sp-3); }
  .vs-field { gap: 2px; }
  .vs-readout { padding: 8px 10px; gap: 3px; }
  .vs-readout__list { gap: 2px 10px; }
  .vs-readout dd { font-size: 14px; }
  .vs-modes { padding: 2px; }
  .vs-mode { min-height: 32px; }
  .vs-counts { font-size: 15px; }
  .vs-ctl { min-height: 32px; }
  .vs-ctl-grid { gap: 4px; }
  .vs-ctl--reset { min-height: 30px; }
  .vs-chip { min-height: 26px; padding: 2px 6px; font-size: 13px; }
  .vs-class-row { gap: 3px; }
  .vs-chips { gap: 3px; }
  .vs-classes { gap: 3px; }
  .vs-select { min-height: 34px; }
  .vs-toggle { min-height: 34px; }
  .vs-caption { display: none; }
  .vs-pick { font-size: 13px; }
  .vs-m-select { min-height: 30px; padding: 2px 8px; width: 130px; }
  .vs-bondpop { width: 200px; padding: 8px 10px; gap: 6px; }
  .vs-bondpop__btn { min-height: 30px; }
  .vs-note { font-size: 12px; }
}

/* 1024x640 and other short screens: the quick-jump grid goes (the Classes popup still loads any class) */
@media (max-height: 680px) {
  .vs-classes { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  dialog.pop[open] { animation: none; }
  .vs-chip { transition: none; }
}
