/* Дві теми: «Зошит» (типова, світла, папір у клітинку) і «Космос» (темна, data-theme="space").
   Тему ставить скрипт у <head> index.html ще до малювання сторінки; перемикач — у шапці. */
:root {
  --bg: #fbfaf3;
  --grid: #dbe5f1;
  --paper: linear-gradient(var(--grid) 1px, transparent 1px) 0 0 / 22px 22px,
           linear-gradient(90deg, var(--grid) 1px, transparent 1px) 0 0 / 22px 22px, var(--bg);
  --panel: #ffffff;
  --panel-2: #eef2f9;
  --line: #d3dbe8;
  --text: #1f2a44;
  --muted: #5b6884;
  --accent: #2d5bd1;
  --accent-bg: #dde7ff;
  --margin-line: #e8a0a0;
  --overlay: rgba(255, 255, 255, 0.9);
  --glass: rgba(45, 91, 209, 0.08);
  --glass-line: rgba(45, 91, 209, 0.3);
  --hook: linear-gradient(135deg, #e2ebff, #f1e6ff);
  --h3: #2d4f9e;
  --ok: #1d7f4c;
  --ok-bg: #c8efd7;
  --warn: #b25b00;
  --err: #c62828;
  --star: #f2a200;
  --star-off: #d3d9e4;
  --analogy-bg: #fff6df;
  --analogy-line: #ecd08a;
  --lab-glass: rgba(214, 232, 248, 0.35);
  --lab-glass-line: #7d8799;
  --radius: 10px;
  --header-h: 52px;
  /* Групи елементів — пастель з темним текстом */
  --c-alkali: #ffcfc8;
  --c-alkaline: #ffe1b3;
  --c-transition: #cfdfff;
  --c-post: #c9e9ec;
  --c-metalloid: #e0efc0;
  --c-nonmetal: #c5efdd;
  --c-halogen: #d3f4c2;
  --c-noble: #e2d4ff;
  --c-lanthanide: #ffd5eb;
  --c-actinide: #f3c6da;
  /* Стани */
  --s-gas: #cde2ff;
  --s-liquid: #c3eee3;
  --s-solid: #e1e4eb;
  --s-unknown: #f2f3f6;
  color-scheme: light;
}
:root[data-theme="space"] {
  --bg: #0b1020;
  --paper: var(--bg);
  --panel: #121a2e;
  --panel-2: #18223b;
  --line: #26314d;
  --text: #e8ecf6;
  --muted: #9aa6c0;
  --accent: #6cb4ff;
  --accent-bg: #1b3358;
  --margin-line: var(--line);
  --overlay: rgba(18, 26, 46, 0.88);
  --glass: rgba(255, 255, 255, 0.08);
  --glass-line: rgba(255, 255, 255, 0.25);
  --hook: linear-gradient(135deg, #1b3358, #2a1f4d);
  --h3: #cfe3ff;
  --ok: #6fe0a0;
  --ok-bg: #1f6b3a;
  --warn: #ffb05a;
  --err: #ff7a72;
  --star: #ffcf4a;
  --star-off: #3a4360;
  --analogy-bg: #1f1c12;
  --analogy-line: #5a4a1f;
  --lab-glass: rgba(140, 170, 210, 0.12);
  --lab-glass-line: #b8c3d8;
  /* Групи елементів */
  --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(--paper);
  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: 2px solid var(--margin-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; min-width: 0; scrollbar-width: none; }
.brand, .head-right { flex: none; }
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; }
.theme-btn { width: 36px; height: 34px; padding: 0; font-size: 18px; line-height: 1; flex: none; }

.uk-only { max-width: 900px; margin: 12px auto 0; padding: 10px 14px; border-radius: var(--radius); background: var(--analogy-bg); border: 1px solid var(--analogy-line); color: var(--text); font-size: 14px; }

/* bit.city account (bitcity.js) */
.bc-acct { display: flex; align-items: center; gap: 6px; min-width: 0; }
.head-right .bc-acct a { display: inline-flex; color: var(--text); text-decoration: none; }
.bc-login { align-items: center; min-height: 34px; padding: 0 14px; border-radius: 8px; background: var(--accent); color: #fff !important; font-weight: 700; font-size: 14px; }
.bc-me { align-items: center; gap: 6px; min-height: 34px; font-weight: 700; font-size: 14px; min-width: 0; }
.bc-av { width: 28px; height: 28px; flex: none; border-radius: 50%; background: #ff5b3a; color: #fff; display: grid; place-items: center; font-size: 13px; }
.bc-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 12ch; }
.bc-out { min-height: 34px; padding: 0 8px; font-size: 13px; }
@media (max-width: 700px) { .bc-name, .bc-out { display: none; } }

/* ---------- Таблиця ---------- */
#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; }
/* Запас по краях: елемент під курсором збільшується (scale 1.06), і прокрутка не має обрізати його рамку */
.pt-wrap { overflow-x: auto; padding: 5px 5px 8px; margin: -5px -5px 0; }
.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));
}
/* У «Зошиті» 3D-сцена прозора — під моделлю видно клітинку */
#stage { position: relative; overflow: hidden; min-height: 300px; touch-action: none; background: var(--paper); }
#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;
  transition: opacity 0.12s;
}
/* Атом закритий іншим атомом — його символ не просвічує крізь сусіда */
.atom-label.covered { opacity: 0; }
.species-label {
  font-size: 15px;
  color: var(--text);
  background: var(--overlay);
  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: var(--overlay);
  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; }
/* Кульки-позначки частинок атома — ті самі кольори, що й у 3D-моделі (app.js: protonMat, neutronMat, THEMES.electron) */
.ball {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: -1px;
  flex: none;
}
.ball.proton { background: radial-gradient(circle at 35% 30%, #ffb3ad, #ff5a4f 55%, #c2362c); }
.ball.neutron { background: radial-gradient(circle at 35% 30%, #e3e7ef, #9aa3b5 55%, #6b7385); }
.ball.electron { background: radial-gradient(circle at 35% 30%, #8cc8ff, #1e88e5 55%, #0d5aa8); }
:root[data-theme="space"] .ball.electron { background: radial-gradient(circle at 35% 30%, #d4f4ff, #5fd3ff 55%, #2a8fd0); }
.stage-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 4px; font-size: 14px; color: var(--text); }
.stage-legend span { display: inline-flex; align-items: center; }
.stage-legend .ball { width: 14px; height: 14px; }
.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: var(--overlay);
  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;
  width: 0;
  min-width: 0;
  text-overflow: ellipsis;
  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; }

/* ---------- Уроки ---------- */
#lessonView { padding: 16px; }
.lesson-wrap { max-width: 820px; margin: 0 auto; }
.lesson-wrap h1 { font-size: 26px; margin: 4px 0 12px; }
.lesson-wrap .card h2, .lesson-wrap > h2 { font-size: 19px; color: var(--text); margin: 0 0 10px; }
.lesson-wrap > h2 { margin-top: 24px; }
.crumbs { color: var(--muted); font-size: 14px; margin: 0; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  margin: 14px 0;
}
.hook {
  background: var(--hook);
  border-radius: 14px;
  padding: 20px;
  margin: 14px 0;
}
.hook-q { font-size: 18px; margin-bottom: 12px; }
.btn-link, .btn-ghost, .btn-primary {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  text-decoration: none;
  font-size: 15px;
  cursor: pointer;
}
.btn-link { background: var(--glass); border-color: var(--glass-line); }
.btn-primary { background: var(--accent-bg); border-color: var(--accent); font-weight: 600; }
.nuc { display: inline-flex; align-items: center; font-weight: 600; white-space: nowrap; }
.nuc-n { display: inline-flex; flex-direction: column; font-size: 0.62em; line-height: 1.05; text-align: right; margin-right: 1px; font-weight: 500; }
.iso-pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin: 12px 0; }
.iso-pair figure { margin: 0; background: var(--panel-2); border-radius: 12px; padding: 12px; text-align: center; }
.iso-pair figcaption { font-size: 14px; color: var(--muted); }
.iso-pair figcaption .nuc { font-size: 22px; color: var(--text); }
.nucleus { width: 100%; max-width: 150px; height: auto; }
.bag { display: grid; grid-template-columns: repeat(20, minmax(0, 1fr)); gap: 4px; margin: 12px 0; max-width: 520px; }
.bag i { aspect-ratio: 1; border-radius: 50%; background: #6fe0a0; transition: background 0.25s; }
.bag i.b { background: #1d7a4f; box-shadow: inset 0 0 0 2px #0f4a2f; }
.bag-legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 15px; }
.dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; margin-right: 6px; vertical-align: -1px; }
.dot.a { background: #6fe0a0; }
.dot.b { background: #1d7a4f; }
.slider-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 12px 0; }
.slider-row input[type=range] { flex: 1 1 180px; accent-color: #6fe0a0; padding: 0; }
.scale { margin: 8px 0; max-width: 520px; }
.scale-bar { position: relative; height: 10px; border-radius: 5px; background: linear-gradient(90deg, #6fe0a0, #1d7a4f); }
.scale-mark { position: absolute; top: -6px; width: 4px; height: 22px; margin-left: -2px; border-radius: 2px; background: #fff; transition: left 0.15s; }
.scale-ends { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-top: 6px; }
.result { font-size: 18px; margin: 8px 0; }
.tip { color: var(--ok); min-height: 1.5em; margin: 4px 0 10px; }
.analogy { border-color: var(--analogy-line); background: var(--analogy-bg); }
.formula { font-size: 20px; text-align: center; padding: 12px; background: var(--panel-2); border-radius: 10px; margin: 6px 0; overflow-x: auto; }
.steps li { margin-bottom: 6px; }
.steps .nuc { font-size: 1.05em; }
table.calc { width: 100%; border-collapse: collapse; font-size: 15px; margin-top: 8px; }
table.calc th, table.calc td { border-bottom: 1px solid var(--line); padding: 6px 4px; text-align: left; }
table.calc th { color: var(--muted); font-weight: 500; }
table.calc .nuc { font-size: 18px; }
.qz { border-top: 1px solid var(--line); padding: 14px 0; }
.qz-q { font-size: 16px; margin: 0 0 10px; }
.qz-q .nuc { font-size: 1.15em; }
.qz-n {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 50%; background: var(--accent-bg); margin-right: 8px; font-size: 14px; font-weight: 600;
}
.qz.done-ok .qz-n { background: var(--ok-bg); }
.tf-row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; }
.tf-row.wrong, .chip.wrong { box-shadow: inset 0 0 0 2px #e0564f; }
.tf-row .nuc { font-size: 1.1em; }
.tf-btns { display: flex; gap: 6px; flex: none; }
.chip { border-radius: 999px; padding: 5px 12px; font-size: 14px; }
.chip.on { background: var(--accent-bg); border-color: var(--accent); }
.chip.big { font-size: 20px; padding: 6px 16px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.num-row { display: flex; align-items: center; gap: 8px; }
.num-row input { width: 130px; font-size: 17px; }
.mcq { display: flex; flex-direction: column; gap: 6px; }
.mcq label { display: flex; gap: 8px; align-items: flex-start; padding: 6px 8px; border-radius: 8px; cursor: pointer; }
.mcq label:hover { background: var(--panel-2); }
.qz-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-top: 10px; }
.qz-msg { font-size: 14px; }
.qz-msg.ok { color: var(--ok); font-weight: 600; }
.qz-msg.bad { color: var(--warn); }
.qz-msg.err { color: var(--err); }
.qz-explain { margin-top: 8px; padding: 10px 12px; border-left: 3px solid var(--accent); background: var(--panel-2); font-size: 14px; }
.qz-explain .nuc { font-size: 1.1em; }
.qz-finish { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; border-top: 1px solid var(--line); padding-top: 14px; }
.summary { text-align: center; border-color: var(--accent); }
.summary .learned { text-align: left; }
.summary .export-row { justify-content: center; }
.badge-big { font-size: 52px; line-height: 1; }
.score { font-size: 20px; }
.stars { color: var(--star); letter-spacing: 2px; }
.stars .off { color: var(--star-off); }
.learned li { margin-bottom: 6px; }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin: 14px 0; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; }
.stat b { font-size: 20px; }
.stat span { color: var(--muted); font-size: 13px; }
.lesson-cards { display: grid; gap: 10px; }
.lesson-card {
  display: flex; flex-direction: column; gap: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px;
  color: var(--text); text-decoration: none;
}
.lesson-card:hover { border-color: var(--accent); }
.lc-title { font-size: 17px; font-weight: 600; }
.lc-text { color: var(--muted); font-size: 14px; }
.lc-status { font-size: 14px; color: var(--accent); }
.lesson-wrap h3 { font-size: 16px; margin: 18px 0 6px; color: var(--h3); }
.fact-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin: 12px 0; }
.fact { background: var(--panel-2); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 2px; }
.fact b { font-size: 16px; }
.fact span { color: var(--muted); font-size: 14px; }
.scheme { width: 100%; max-width: 420px; display: block; margin: 8px auto; }
.flow { counter-reset: none; padding-left: 22px; }
.flow li { margin-bottom: 8px; }
.term { display: inline-flex; flex-direction: column; align-items: center; vertical-align: top; }
.term small, .arrow small { font-size: 11px; color: var(--muted); font-weight: 400; }
.arrow { display: inline-flex; flex-direction: column; align-items: center; vertical-align: top; margin: 0 4px; }
.lab { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.lab-label { color: var(--muted); font-size: 13px; margin: 6px 0; }
.lab .chip-row { margin-bottom: 8px; }
.lab-stage { display: flex; gap: 12px; align-items: flex-start; background: var(--panel-2); border-radius: 12px; padding: 12px; }
.tube { width: 90px; flex: none; }
.tube .liquid { transition: fill 0.6s; }
.tube .char { animation: grow 1.6s ease-out; transform-origin: 60px 220px; }
@keyframes grow { from { transform: scaleY(0.1); } to { transform: scaleY(1); } }
.lab-result p { margin: 0 0 6px; font-size: 14px; }
.lab-verdict { font-size: 17px !important; font-weight: 700; }
.lab-verdict.yes { color: var(--ok); }
.lab-verdict.no { color: var(--warn); }
/* Схема сахарози в уроці: кольори залежать від теми */
.sch-glc { fill: var(--accent-bg); stroke: var(--accent); }
.sch-fru { fill: #f1e2ff; stroke: #9b4fd6; }
:root[data-theme="space"] .sch-fru { fill: #4a2a5c; stroke: #d58cff; }
.sch-text { fill: var(--text); }
.sch-muted { fill: var(--muted); }
.sch-bond { stroke: var(--muted); }
@media (max-width: 700px) {
  .lab { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- Моя лабораторія ---------- */
#labView { padding: 16px; }
.lab-page { max-width: 1280px; margin: 0 auto; }
/* Робоча зона (полиця — стіл — результат) і нижній ряд (завдання — журнал) — окремі сітки,
   щоб закріплені колонки не наповзали на журнал */
.lab-work, .lab-bottom { display: grid; gap: 0 14px; align-items: start; }
.lab-work { grid-template-columns: minmax(0, 290px) minmax(0, 1fr) minmax(0, 1fr); grid-template-areas: "shelf bench result"; }
.lab-bottom { grid-template-columns: minmax(0, 290px) minmax(0, 2fr); grid-template-areas: "quests journal"; }
.lab-head h1 { font-size: 26px; margin: 4px 0 4px; }
.lab-head .sub { margin: 0; }
.lab-stats { margin: 6px 0 0; font-size: 14px; color: var(--muted); }
.lab-stats b { color: var(--text); }
.lab-bench { grid-area: bench; }
.lab-shelf { grid-area: shelf; }
.lab-result { grid-area: result; }
.lab-quests { grid-area: quests; }
.lab-journal { grid-area: journal; }
.lab-page .card h2 { font-size: 18px; color: var(--text); margin: 0 0 10px; }
.lab-page h3 { font-size: 15px; margin: 14px 0 4px; color: var(--h3); }
.lab-label { color: var(--muted); font-size: 13px; margin: 10px 0 5px; }
.lab-shelf .chip-row { gap: 6px; }
.lab-chip { text-align: left; padding: 4px 10px; font-size: 13px; }
.lab-chip b { font-weight: 700; }
.lab-chip small { color: var(--muted); }
.chip.cat { border-style: dashed; }
.lab-items { display: flex; flex-wrap: wrap; gap: 6px; min-height: 32px; align-items: center; }
.lab-heat { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin: 10px 0; }
.lab-heat button { font-size: 13px; padding: 6px 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lab-stage { position: relative; display: flex; justify-content: center; align-items: flex-end; gap: 6px; border-radius: 12px; overflow: hidden; }
.lab-flask { width: min(100%, 260px); }
.lab-flask svg { display: block; width: 100%; height: auto; overflow: visible; }
.lab-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.lab-power { width: 100%; margin: 0 0 10px; font-size: 14px; text-align: left; }
.lab-power.on { background: #fff3c4; border-color: #e0a800; color: #5a4300; }
:root[data-theme="space"] .lab-power.on { background: #3d3410; border-color: #c9a227; color: #ffe9a6; }
.lab-meter { position: absolute; top: 8px; left: 8px; display: flex; flex-direction: column; gap: 6px; padding: 8px 10px; border-radius: 10px; background: var(--overlay); border: 1px solid var(--line); font-size: 13px; }
.lab-volt { display: flex; align-items: baseline; gap: 8px; }
.lab-volt span { color: var(--muted); }
.lab-volt b { font: 700 20px/1 ui-monospace, 'Cascadia Mono', Consolas, monospace; background: #1f2a1f; color: #8dff8d; padding: 4px 8px; border-radius: 6px; }
.lab-led { display: flex; align-items: center; gap: 6px; }
.lab-led i { width: 14px; height: 14px; border-radius: 50%; background: #6b2b2b; border: 1px solid #3d1717; }
.lab-led.dim i { background: #d0463c; box-shadow: 0 0 6px 2px rgba(255, 70, 50, 0.45); }
.lab-led.bright i { background: #ff3b2f; box-shadow: 0 0 14px 6px rgba(255, 60, 40, 0.75); }
.lab-lemons { display: flex; align-items: center; gap: 8px; }
.lab-lemons button { width: 30px; height: 30px; padding: 0; font-size: 18px; line-height: 1; }
.lab-sub { border-top: 1px solid var(--line); margin-top: 10px; padding-top: 6px; }
.lab-tag.t-lamp { background: #fff3c4; color: #5a4300; }
:root[data-theme="space"] .lab-tag.t-lamp { background: #3d3410; color: #ffe9a6; }
.lab-actions .btn-primary { flex: 1 1 auto; }
.lab-line { min-height: 1.5em; margin: 8px 0 0; font-weight: 600; }
.lab-line.pulse { animation: lab-pulse 0.6s; color: var(--warn); }
.lab-tests { margin-top: 6px; }
.lab-test-result { margin: 8px 0 0; padding: 8px 10px; border-radius: 8px; background: var(--panel-2); font-size: 14px; }
.lab-test-result.ok { color: var(--ok); font-weight: 600; }
.lab-new { color: var(--ok); font-weight: 700; margin: 0 0 4px; animation: lab-pulse 0.8s; }
.lab-eq { font-size: 18px; margin: 4px 0 8px; overflow-wrap: anywhere; }
.lab-eq small, .lab-eq-s small { font-size: 12px; color: var(--muted); }
.eq-arr { display: inline-flex; flex-direction: column; align-items: center; line-height: 1; vertical-align: -2px; margin: 0 2px; }
.eq-arr small { font-size: 10px; color: var(--muted); }
.lab-tag { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; margin: 0 4px 4px 0; background: var(--panel-2); }
.lab-tag.t-exo { background: #ffe0dc; color: #8a2116; }
.lab-tag.t-endo { background: #dcefff; color: #134d86; }
.lab-tag.t-cat { background: #ece2ff; color: #4b2e8a; }
:root[data-theme="space"] .lab-tag.t-exo { background: #4a1f1c; color: #ffc9c2; }
:root[data-theme="space"] .lab-tag.t-endo { background: #173554; color: #cfe6ff; }
:root[data-theme="space"] .lab-tag.t-cat { background: #33264f; color: #e2d4ff; }
.lab-note { font-size: 14px; color: var(--muted); margin: 8px 0 0; }
.lab-hint { padding: 8px 10px; border-radius: 8px; background: var(--analogy-bg); border: 1px solid var(--analogy-line); }
.lab-safety { margin: 12px 0 0; padding: 8px 10px; border-radius: 8px; font-size: 14px; background: #fff0e0; color: #7a3d00; border: 1px solid #f0c48a; }
.lab-safety.home { background: #e6f6ea; color: #1d5e33; border-color: #a9dcb7; }
:root[data-theme="space"] .lab-safety { background: #3a2610; color: #ffd9a8; border-color: #6b4a1e; }
:root[data-theme="space"] .lab-safety.home { background: #15331f; color: #bdf0cd; border-color: #2c6b41; }
.lab-quests ul { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; font-size: 14px; }
.lab-quests li.done { color: var(--ok); }
.lab-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.lab-card { display: flex; flex-direction: column; gap: 3px; text-align: left; padding: 8px 10px; border-radius: 10px; border: 1px dashed var(--line); background: var(--panel-2); font-size: 13px; color: var(--muted); }
.lab-card.found { border-style: solid; color: var(--text); background: var(--panel); cursor: pointer; }
.lab-card.found:hover { border-color: var(--accent); }
.lab-card small { color: var(--muted); font-size: 11px; }
.lab-eq-s { font-size: 12px; overflow-wrap: anywhere; }
.lab-eq-s .eq-arr small { font-size: 9px; }

/* Термометр */
.thermo { display: flex; flex-direction: column; align-items: center; width: 54px; flex: none; margin-bottom: 18px; }
.th-tube { width: 12px; height: 150px; border: 2px solid var(--lab-glass-line); border-bottom: none; border-radius: 8px 8px 0 0; position: relative; overflow: hidden; background: var(--panel); }
.th-fill { position: absolute; left: 0; right: 0; bottom: 0; height: 36%; background: #e2534a; }
.th-fill.cold { background: #3d8be0; }
.th-bulb { width: 22px; height: 22px; border-radius: 50%; margin-top: -3px; border: 2px solid var(--lab-glass-line); background: #e2534a; }
.th-fill.cold + .th-bulb { background: #3d8be0; }
.thermo b { font-size: 13px; margin-top: 4px; white-space: nowrap; }

/* Ефекти в колбі */
.lab-flash { position: absolute; inset: 0; pointer-events: none; opacity: 0; }
.lab-flash.white { background: #ffffff; animation: lab-flash 0.9s ease-out; }
.lab-flash.warm { background: radial-gradient(circle at 50% 35%, #fff3b0, rgba(255, 170, 60, 0.6) 40%, transparent 70%); animation: lab-flash 0.7s ease-out; }
.lab-stage.shake { animation: lab-shake 0.45s; }
.lab-bub { animation: lab-bub 2s ease-in infinite; }
.lab-fall { animation: lab-fall 2.4s ease-in 1 forwards; opacity: 0; }
.lab-fadein { animation: lab-fadein 1.5s ease-out both; }
.lab-fadeout { animation: lab-fadeout 0.6s ease-out both; }
.lab-puff { animation: lab-puff 2.6s ease-out infinite; opacity: 0; transform-box: fill-box; transform-origin: center; }
.lab-foam { animation: lab-foam 2.2s ease-out infinite; transform-box: fill-box; transform-origin: center; opacity: 0; }
.lab-fire { animation: lab-flicker 0.25s ease-in-out infinite alternate; transform-box: fill-box; transform-origin: 50% 100%; }
.lab-fire.big { animation-duration: 0.18s; }
.lab-glow { animation: lab-glow 1.2s ease-in-out infinite alternate; }
.lab-shrink, .lab-shrink-fast, .lab-shrink-slow { transform-box: fill-box; transform-origin: 50% 100%; animation: lab-shrink 9s ease-in forwards; }
.lab-shrink-fast { animation-duration: 5s; }
.lab-shrink-slow { animation-duration: 16s; }
.lab-float { animation: lab-float 1.6s ease-in-out infinite alternate; }
.lab-char { transform-box: fill-box; transform-origin: 50% 100%; animation: lab-grow 6s ease-out both; }
.lab-boom { transform-box: fill-box; transform-origin: center; animation: lab-boom 1.1s ease-out both; }
/* Інструменти перевірки газу над колбою */
.lab-flask { position: relative; }
.lab-tool { position: absolute; inset: 0; pointer-events: none; }
.lab-tool svg { display: block; width: 100%; height: 100%; overflow: visible; }
.lab-tool-in { animation: lab-tool-in 0.9s ease-out both; }
.lab-late { animation: lab-fadein 0.5s ease-out 1s both; }
.lab-out { animation: lab-fadeout 0.6s ease-in 1.1s both; }
.lab-flame-grow { transform-box: fill-box; transform-origin: 50% 100%; animation: lab-flame-grow 0.6s ease-out 1s both; }
@keyframes lab-tool-in { from { transform: translateY(-90px); opacity: 0; } 30% { opacity: 1; } to { transform: translateY(0); opacity: 1; } }
@keyframes lab-flame-grow { from { transform: scale(1); } to { transform: scale(1.9); } }
@keyframes lab-bub { from { transform: translateY(0); opacity: 0.95; } 85% { opacity: 0.95; } to { transform: translateY(var(--rise)); opacity: 0; } }
@keyframes lab-fall { from { transform: translateY(0); opacity: 0; } 15% { opacity: 1; } to { transform: translateY(var(--fall)); opacity: 1; } }
@keyframes lab-fadein { from { opacity: 0; } to { opacity: 1; } }
@keyframes lab-fadeout { to { opacity: 0; } }
@keyframes lab-puff { 0% { opacity: 0; transform: translateY(0) scale(0.6); } 20% { opacity: 0.85; } 100% { opacity: 0; transform: translateY(-46px) scale(1.7); } }
@keyframes lab-foam { 0% { opacity: 0; transform: translateY(0) scale(0.4); } 15% { opacity: 1; } 100% { opacity: 0.9; transform: translateY(var(--rise)) scale(1.1); } }
@keyframes lab-flicker { from { transform: scale(1, 0.9); } to { transform: scale(0.94, 1.12); } }
@keyframes lab-glow { from { opacity: 0.45; } to { opacity: 1; } }
/* --end — яка частка шматочка лишиться (за розрахунком кількостей); без розрахунку — майже зникає */
@keyframes lab-shrink { from { transform: scale(1); opacity: 1; } to { transform: scale(var(--end, 0.15)); opacity: max(0.3, var(--end, 0.3)); } }
@keyframes lab-float { from { transform: translateX(-34px); } to { transform: translateX(34px); } }
@keyframes lab-grow { from { transform: scaleY(0.04); } to { transform: scaleY(1); } }
@keyframes lab-boom { 0% { opacity: 0; transform: scale(0.2); } 15% { opacity: 1; transform: scale(1.1); } 70% { opacity: 1; } 100% { opacity: 0; transform: scale(1.3); } }
@keyframes lab-flash { 0% { opacity: 0; } 12% { opacity: 0.85; } 100% { opacity: 0; } }
@keyframes lab-shake { 0%, 100% { transform: translate(0); } 20% { transform: translate(-6px, 3px); } 40% { transform: translate(6px, -3px); } 60% { transform: translate(-4px, 2px); } 80% { transform: translate(3px, -1px); } }
@keyframes lab-pulse { 0% { transform: scale(1); } 40% { transform: scale(1.04); } 100% { transform: scale(1); } }
/* Без різких спалахів і трясіння, якщо людина просила менше руху в налаштуваннях системи */
@media (prefers-reduced-motion: reduce) {
  .lab-flash.white, .lab-flash.warm, .lab-stage.shake { animation: none; }
}
/* Режими, умови, полиця з пошуком і групами */
.lab-title-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 16px; }
.lab-mode { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.lab-mode button { border: none; border-radius: 0; padding: 7px 12px; font-size: 14px; background: var(--panel); }
.lab-mode button.on { background: var(--accent-bg); color: var(--text); font-weight: 600; }
.lab-extra { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; margin: -4px 0 10px; }
.lab-extra button { font-size: 13px; text-align: left; }
.lab-extra button.on { background: var(--accent-bg); border-color: var(--accent); }
.lab-search { width: 100%; margin: 0 0 6px; }
.lab-group { border-top: 1px solid var(--line); padding: 6px 0; }
.lab-group summary { cursor: pointer; font-size: 14px; font-weight: 600; color: var(--text); padding: 4px 0; list-style-position: inside; }
.lab-group summary small { color: var(--muted); font-weight: 400; margin-left: 4px; }
.lab-group .chip-row { margin-top: 6px; }
/* Колір позначки — за класом речовини */
.lab-chip { border-left-width: 4px; }
.g-metal { border-left-color: #8a96a8; }
.g-nonmetal { border-left-color: #2f9e6e; }
.g-halogen { border-left-color: #c9a227; }
.g-noble { border-left-color: #8b6cd6; }
.g-gas { border-left-color: #5aa8e6; }
.g-acid { border-left-color: #e0564f; }
.g-base { border-left-color: #3a5fcf; }
.g-salt { border-left-color: #3fae8f; }
.g-oxide { border-left-color: #a86a3a; }
.g-solid { border-left-color: #b0a890; }
.g-org { border-left-color: #e08a2a; }
.g-special { border-left-color: #6fb3df; }
.g-ind { border-left-color: #b04fb8; }
.g-cat { border-left-color: #e0a800; }
/* Таблиця Менделєєва на полиці */
.lab-pt { display: grid; grid-template-columns: repeat(18, minmax(0, 1fr)); grid-template-rows: repeat(7, auto) 6px auto auto; gap: 2px; margin: 6px 0 4px; }
.pt-cell { padding: 0; min-width: 0; aspect-ratio: 1; border-radius: 3px; font-size: 10px; font-weight: 700; line-height: 1; border: 1px solid transparent; color: var(--text); }
.pt-cell:hover { border-color: var(--text); }
.pt-cell.on { outline: 2px solid var(--accent); outline-offset: 1px; }
.pt-cell.off { opacity: 0.3; filter: grayscale(0.9); cursor: help; }
.pt-gap { grid-column: 1 / -1; }
.lab-ionic { font-size: 14px; margin: -2px 0 8px; overflow-wrap: anywhere; }
.lab-ionic span { color: var(--muted); }
.lab-ph { margin: 8px 0 0; padding: 6px 10px; border-radius: 8px; background: var(--panel-2); font-size: 14px; }
.lab-tag.t-gen { background: #e6f0ff; color: #24478f; }
:root[data-theme="space"] .lab-tag.t-gen { background: #1c2d4f; color: #cfe0ff; }
.lab-gen-note { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.lab-gen-title { grid-column: 1 / -1; margin: 10px 0 2px; font-size: 15px; }
/* Кількості */
.lab-amounts { margin: 6px 0 4px; }
.lab-amount { display: grid; grid-template-columns: minmax(54px, auto) 84px auto minmax(0, 1fr); align-items: center; gap: 6px; margin: 4px 0; font-size: 14px; }
.lab-amount input { width: 84px; padding: 4px 6px; }
.lab-amount small { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.lab-stoich { margin: 12px 0 0; border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; }
.lab-stoich summary { cursor: pointer; font-weight: 600; color: var(--h3); }
.lab-table-wrap { overflow-x: auto; margin: 8px 0; }
.lab-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.lab-table th, .lab-table td { border-bottom: 1px solid var(--line); padding: 4px 6px; text-align: left; vertical-align: top; }
.lab-table th { color: var(--muted); font-weight: 500; }
.lab-table small { color: var(--muted); }
.lab-lim, .lab-ex, .lab-idle { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 999px; margin-top: 2px; }
.lab-lim { background: #fde2cf; color: #7a3d00; }
.lab-ex { background: #dcefff; color: #134d86; }
.lab-idle { background: var(--panel-2); color: var(--muted); }
:root[data-theme="space"] .lab-lim { background: #4a2a10; color: #ffd2a8; }
:root[data-theme="space"] .lab-ex { background: #173554; color: #cfe6ff; }
.lab-sum { font-size: 14px; font-weight: 600; margin: 4px 0; }
@media (max-width: 520px) {
  .lab-table { font-size: 12px; }
  .lab-table th, .lab-table td { padding: 3px 3px; }
  .lab-stoich { padding: 6px 6px; }
  .lab-amount { grid-template-columns: minmax(48px, auto) 76px auto minmax(0, 1fr); }
  .lab-amount input { width: 76px; }
}
.lab-others { margin: 12px 0 0; padding: 8px 12px; border-radius: 10px; border: 1px dashed var(--line); background: var(--panel-2); }
.lab-others h3 { margin: 0 0 4px; }
.lab-others p { margin: 4px 0; font-size: 14px; }
.lab-ppt-fade { animation: lab-fadeout 1.2s ease-in 4s both; }
@media (max-width: 900px) {
  .lab-work, .lab-page.expert .lab-work { grid-template-columns: minmax(0, 1fr); grid-template-areas: "bench" "shelf" "result"; }
  .lab-bottom { grid-template-columns: minmax(0, 1fr); grid-template-areas: "quests" "journal"; }
}
/* На комп’ютері стіл, полиця й результат стоять на місці; кожна колонка гортається окремо */
@media (min-width: 901px) {
  .lab-shelf, .lab-bench, .lab-result {
    position: sticky;
    top: calc(var(--header-h) + 8px);
    max-height: calc(100vh - var(--header-h) - 16px);
    max-height: calc(100dvh - var(--header-h) - 16px);
    overflow-y: auto;
    overscroll-behavior: contain;
    margin-top: 8px;
  }
  .lab-page.expert .lab-work { grid-template-columns: minmax(0, 330px) minmax(0, 1fr) minmax(0, 1fr); }
}
@media (max-width: 520px) {
  #labView { padding: 12px 10px; }
  .lab-head h1 { font-size: 22px; }
  .lab-heat { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  /* minmax(0, …): колонка не розширюється під найдовший пункт меню вибору — інакше горизонтальна прокрутка */
  #viewer { grid-template-columns: minmax(0, 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 { gap: 2px; }
  nav.tabs a { padding: 6px 4px; font-size: 14px; }
  .theme-btn { width: 30px; height: 32px; font-size: 16px; }
  #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; }
  #lessonView { padding: 12px 10px; }
  .card { padding: 14px; }
  .lesson-wrap h1 { font-size: 22px; }
  .bag { grid-template-columns: repeat(10, minmax(0, 1fr)); }
  .chip.big { font-size: 18px; padding: 5px 12px; }
}
/* На телефонах шапка у два рядки: угорі логотип, тема й мова, знизу — п’ять вкладок на всю ширину */
@media (max-width: 520px) {
  :root { --header-h: 86px; }
  header { flex-wrap: wrap; align-content: center; row-gap: 4px; }
  header .brand { margin-right: auto; }
  nav.tabs { order: 3; flex: 1 1 100%; justify-content: space-between; }
  .head-right { margin-left: 0; }
}
@media (max-width: 390px) {
  nav.tabs a { font-size: 13px; }
}
