:root {
  --bg: #0b1020;
  --panel: #121a2e;
  --panel-2: #18223b;
  --line: #26314d;
  --text: #e8ecf6;
  --muted: #9aa6c0;
  --accent: #6cb4ff;
  --accent-bg: #1b3358;
  --radius: 10px;
  --header-h: 52px;
  /* Групи елементів */
  --c-alkali: #7a2e2e;
  --c-alkaline: #7a4b1e;
  --c-transition: #34497a;
  --c-post: #2f5a5f;
  --c-metalloid: #4f6b3a;
  --c-nonmetal: #1f6b5a;
  --c-halogen: #2c7a3c;
  --c-noble: #5a3d7a;
  --c-lanthanide: #7a3d63;
  --c-actinide: #5e2a48;
  /* Стани */
  --s-gas: #2f5f8a;
  --s-liquid: #2a7a6b;
  --s-solid: #575a66;
  --s-unknown: #2b2f3c;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  overflow-x: hidden;
}
a { color: var(--accent); }
button, select, input {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
}
button { cursor: pointer; }
button:hover { border-color: var(--muted); }
button.on { background: var(--accent-bg); border-color: var(--accent); }
sub { font-size: 0.72em; }

/* ---------- Шапка ---------- */
header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 600; font-size: 17px; margin-right: 12px; white-space: nowrap; color: var(--text); text-decoration: none; }
.brand span { color: var(--accent); }
nav.tabs { display: flex; gap: 4px; overflow-x: auto; }
nav.tabs a {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
}
nav.tabs a.on { background: var(--panel-2); color: var(--text); }
.head-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.head-right a { color: var(--muted); font-size: 14px; }

/* ---------- Таблиця ---------- */
#tableView { padding: 16px; max-width: 1280px; margin: 0 auto; }
.table-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.table-tools input { flex: 1 1 240px; max-width: 360px; }
.table-tools .hint { color: var(--muted); font-size: 14px; }
.pt-wrap { overflow-x: auto; padding-bottom: 6px; }
.pt {
  display: grid;
  grid-template-columns: repeat(18, minmax(46px, 1fr));
  gap: 3px;
  min-width: 860px;
}
.el {
  position: relative;
  aspect-ratio: 1 / 1.08;
  border-radius: 6px;
  padding: 3px 4px;
  cursor: pointer;
  border: 1px solid transparent;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  text-decoration: none;
  color: var(--text);
  transition: transform 0.08s, opacity 0.15s;
}
.el:hover, .el:focus-visible { border-color: var(--text); transform: scale(1.06); z-index: 2; outline: none; }
.el .z { font-size: 10px; opacity: 0.8; line-height: 1; }
.el .sym { font-size: 19px; font-weight: 600; text-align: center; line-height: 1; }
.el .nm { font-size: 9px; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; opacity: 0.9; line-height: 1.1; }
.el.dim { opacity: 0.18; }
.el.placeholder { cursor: default; font-size: 10px; align-items: center; justify-content: center; color: var(--muted); border: 1px dashed var(--line); background: none; }
.el.placeholder:hover { transform: none; border-color: var(--line); }
.pt-gap { grid-column: 1 / -1; height: 10px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.legend button { background: none; border: none; padding: 2px 0; color: var(--muted); font-size: 13px; display: inline-flex; align-items: center; gap: 6px; }
.legend button:hover, .legend button.on { color: var(--text); }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

/* ---------- Перегляд (3D + панель) ---------- */
#viewer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  height: calc(100vh - var(--header-h));
  height: calc(100dvh - var(--header-h));
}
#stage { position: relative; overflow: hidden; min-height: 300px; touch-action: none; }
#stage canvas { display: block; }
.labels { position: absolute; inset: 0; pointer-events: none; }
.atom-label {
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 0 3px #000, 0 0 2px #000;
  pointer-events: none;
}
.species-label {
  font-size: 15px;
  color: var(--text);
  background: rgba(11, 16, 32, 0.75);
  padding: 2px 8px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.2s;
}
.stage-tools {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  pointer-events: none;
}
.stage-tools > * { pointer-events: auto; }
.stage-tools .spacer { flex: 1; pointer-events: none; }
.stage-tools button, .stage-tools label {
  background: rgba(18, 26, 46, 0.88);
  font-size: 13px;
  padding: 5px 9px;
}
.stage-tools label { border: 1px solid var(--line); border-radius: 8px; display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }
.stage-note {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  font-size: 12px;
  color: var(--muted);
  pointer-events: none;
}
.reaction-bar {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  background: rgba(18, 26, 46, 0.9);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.reaction-bar input[type=range] { flex: 1 1 160px; padding: 0; accent-color: var(--accent); }
.reaction-bar .step { flex: 1 1 100%; font-size: 14px; min-height: 1.5em; }
.toast {
  position: absolute;
  top: 54px;
  right: 10px;
  background: var(--accent-bg);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; }

#side {
  border-left: 1px solid var(--line);
  background: var(--panel);
  overflow-y: auto;
  padding: 16px;
}
.card-head { display: flex; gap: 12px; align-items: center; margin-bottom: 10px; }
.tile {
  width: 64px;
  height: 64px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: none;
}
.tile b { font-size: 26px; line-height: 1; }
.tile small { font-size: 11px; opacity: 0.85; }
h1 { font-size: 22px; margin: 0; line-height: 1.2; }
h2 { font-size: 15px; margin: 18px 0 8px; color: var(--muted); font-weight: 600; }
.sub { color: var(--muted); font-size: 14px; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 14px; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips a {
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  text-decoration: none;
}
.chips a:hover { border-color: var(--accent); }
.note { font-size: 13px; color: var(--muted); }
.list { display: flex; flex-direction: column; gap: 2px; }
.list a {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 10px;
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
}
.list a:hover { background: var(--panel-2); }
.list a.on { background: var(--accent-bg); }
.list a span:last-child { color: var(--muted); white-space: nowrap; }
.equation { font-size: 18px; margin: 8px 0; }

/* Вибір молекули/реакції: ‹ [список] › — прикріплений угорі під час гортання опису */
.picker {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  gap: 6px;
  align-items: center;
  margin: -16px -16px 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.pk-select {
  flex: 1;
  min-width: 0;
  height: 42px;
  font-size: 16px;
  padding: 0 10px;
  background: var(--panel-2);
  cursor: pointer;
}
.pk-btn {
  flex: none;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  line-height: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  text-decoration: none;
}
.pk-btn:hover { border-color: var(--muted); }
.balance { font-size: 13px; color: var(--muted); }
.export-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }

@media (max-width: 900px) {
  #viewer { grid-template-columns: 1fr; height: auto; }
  #stage { height: 58vh; height: 58dvh; }
  #side { border-left: none; border-top: 1px solid var(--line); overflow: visible; }
  /* Сторінку гортає браузер, тому рядок вибору прилипає під шапкою; довгий список замінює випадаюче меню */
  .picker { top: var(--header-h); }
  .side-list { display: none; }
  .head-right a { display: none; }
  .brand { margin-right: 4px; }
}
@media (max-width: 520px) {
  header { padding: 0 8px; gap: 4px; }
  .brand { font-size: 14px; }
  nav.tabs a { padding: 6px 6px; font-size: 14px; }
  #lang { max-width: 78px; padding: 5px 4px; font-size: 13px; }
  .reaction-bar { left: 6px; right: 6px; bottom: 6px; padding: 6px 8px; gap: 6px; }
  .reaction-bar .step { font-size: 13px; min-height: 0; }
  .reaction-bar button, .reaction-bar select { padding: 3px 8px; font-size: 13px; }
  .stage-tools { top: 6px; left: 6px; right: 6px; gap: 4px; }
  .stage-tools button, .stage-tools label { padding: 4px 7px; font-size: 12px; }
  #tableView { padding: 12px 10px; }
}
@media (max-width: 380px) {
  header .brand { display: none; }
}
