:root {
  color-scheme: dark;
  --soot: #07070b;
  --bench: rgba(14, 15, 21, .74);
  --panel: rgba(22, 24, 33, .72);
  --raise: #1a1c26;
  --raise-2: #252836;
  --line: rgba(120, 128, 160, .16);
  --line-2: rgba(150, 158, 190, .28);
  --flame: #6f8cff;
  --flame-wash: rgba(90, 120, 255, .10);
  --ink: #eceae4;
  --ink-2: #a4a8b2;
  --ink-3: #6f7481;
  --brass: #d4a55b;
  --brass-2: #f0c47a;
  --brass-ink: #1b1409;
  --good: #4bd08a;
  --bad: #ff6b5b;
  --r-sm: 8px;
  --r-md: 14px;
  --f-display: "Cormorant Garamond", "Cormorant", Georgia, "Times New Roman", serif;
  --f-body: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(ellipse 55% 75% at 22% 42%, var(--flame-wash), transparent 72%),
    radial-gradient(ellipse 80% 50% at 80% 0%, rgba(212, 165, 91, .05), transparent 70%),
    var(--soot);
  background-attachment: fixed;
  color: var(--ink);
  font: 400 15px/1.55 var(--f-body);
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--brass-2); outline-offset: 2px; }

/* ---------- App frame ---------- */
.app {
  display: grid;
  grid-template-columns: minmax(380px, 45fr) 55fr;
  height: 100%;
}
.bench {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  overflow-y: auto;
  background: var(--bench);
  border-right: 1px solid var(--line);
}
.picker {
  overflow-y: auto;
  padding: 22px 24px 32px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
}

/* ---------- Stage ---------- */
.stage {
  position: relative;
  flex: 0 0 auto;
  height: min(62vh, 620px);
  min-height: 380px;
  border-radius: var(--r-md);
  overflow: hidden;
  background:
    radial-gradient(ellipse 70% 60% at 50% 58%, var(--flame-wash), transparent 70%),
    radial-gradient(ellipse 120% 80% at 50% 110%, #0d0e14, #040406 70%);
  border: 1px solid var(--line-2);
  box-shadow:
    0 0 0 1px rgba(0,0,0,.5),
    0 30px 80px -30px color-mix(in srgb, var(--flame) 45%, transparent),
    inset 0 0 120px rgba(0,0,0,.7);
  isolation: isolate;
  transition: box-shadow .8s ease;
}
.stage canvas.layer { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.stage canvas.gl { mix-blend-mode: screen; }
.stage .vignette {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background: radial-gradient(ellipse 75% 70% at 50% 50%, transparent 55%, rgba(0,0,0,.55));
}

.hud {
  position: absolute;
  top: 14px;
  left: 16px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  pointer-events: none;
  z-index: 3;
}
.eyebrow, .hud-label {
  font: 500 10.5px/1.2 var(--f-mono);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.hud-color {
  font: italic 500 clamp(32px, 4.2vw, 50px)/1 var(--f-display);
  letter-spacing: -0.005em;
  color: var(--hud, var(--ink));
  text-shadow: 0 0 24px color-mix(in srgb, var(--hud, #fff) 55%, transparent), 0 0 2px rgba(0,0,0,.6);
  transition: color .4s ease, text-shadow .4s ease;
  text-wrap: balance;
}
.hud-sub { font-size: 13px; color: var(--ink-2); }
.hud-active {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 200px;
}
.hud-active li {
  display: grid;
  grid-template-columns: 42px 1fr;
  align-items: center;
  gap: 8px;
  font: 500 11px/1 var(--f-mono);
  color: var(--ink-2);
}
.hud-active .bar { height: 4px; border-radius: 2px; background: var(--raise-2); overflow: hidden; }
.hud-active .bar i { display: block; height: 100%; background: var(--c); border-radius: 2px; }

.cobalt-pane {
  position: absolute;
  z-index: 2;
  left: 50%;
  top: 8%;
  width: 46%;
  height: 58%;
  border-radius: 6px;
  background: linear-gradient(115deg, rgba(60, 90, 255, .30), rgba(20, 40, 190, .38) 55%, rgba(70, 110, 255, .28));
  border: 1px solid rgba(140, 165, 255, .45);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.06), 0 10px 40px rgba(0, 20, 120, .35);
  transform: translate(120%, 0) rotate(3deg);
  opacity: 0;
  transition: transform .5s cubic-bezier(.2,.8,.2,1), opacity .3s ease;
  pointer-events: none;
}
.cobalt-pane::after {
  content: "Co glass";
  position: absolute;
  right: 8px;
  bottom: 6px;
  font: 500 9.5px var(--f-mono);
  letter-spacing: .16em;
  text-transform: uppercase;
  color: rgba(200, 212, 255, .7);
}
.stage.cobalt .cobalt-pane { transform: translate(-50%, 0) rotate(0); opacity: 1; }

.scope {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  z-index: 3;
  border-radius: 10px;
  background: rgba(4, 5, 7, .82);
  border: 1px solid var(--line);
  padding: 6px 8px 6px;
}
.scope-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}
.scope-head span:last-child { font: 400 10px var(--f-mono); color: var(--ink-3); }
.scope canvas { display: block; width: 100%; height: 58px; }

/* ---------- Controls ---------- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.seg {
  display: inline-flex;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.seg button {
  border: 0;
  background: transparent;
  padding: 6px 12px;
  border-radius: 7px;
  font-weight: 500;
  font-size: 13px;
  color: var(--ink-2);
}
.seg button[aria-pressed="true"] { background: var(--raise-2); color: var(--ink); }
.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
}
.btn:hover { color: var(--ink); border-color: #3a404e; }
.btn.toggle::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  border: 1.5px solid var(--ink-3);
}
.btn.toggle[aria-pressed="true"] { color: var(--ink); }
.btn.toggle[aria-pressed="true"]::before { background: var(--brass); border-color: var(--brass); box-shadow: 0 0 8px var(--brass); }
.btn.primary {
  background: var(--brass);
  border-color: var(--brass);
  color: var(--brass-ink);
  font-weight: 600;
  margin-left: auto;
}
.btn.primary:hover { background: var(--brass-2); color: var(--brass-ink); }

/* ---------- Detail card ---------- */
.detail {
  background: var(--panel);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: 16px 18px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.detail-top { display: flex; gap: 14px; align-items: center; }
.swatch {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 60%, #fff 0 8%, var(--c) 40%, color-mix(in srgb, var(--c) 20%, transparent) 72%, transparent 73%);
  box-shadow: 0 0 22px color-mix(in srgb, var(--c) 55%, transparent);
}
.swatch.none { background: radial-gradient(circle, var(--raise-2) 0 60%, transparent 62%); box-shadow: none; border: 1px dashed var(--ink-3); }
.detail h2 {
  margin: 2px 0 0;
  font: 600 28px/1.05 var(--f-display);
  letter-spacing: 0;
  text-wrap: balance;
}
.formula { font: 500 13px var(--f-mono); color: var(--ink-2); }
.facts {
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px 16px;
}
.facts div { min-width: 0; }
.facts dt {
  font: 500 10px var(--f-mono);
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.facts dd { margin: 2px 0 0; font-size: 14px; }
.facts dd.mono { font: 400 12.5px/1.6 var(--f-mono); }
.line-chip { display: inline-flex; align-items: center; gap: 5px; margin-right: 10px; white-space: nowrap; }
.line-chip i { width: 7px; height: 7px; border-radius: 50%; background: var(--c); box-shadow: 0 0 6px var(--c); }
.note { margin: 0; color: var(--ink-2); font-size: 14px; max-width: 68ch; }
.warn {
  margin: 0;
  font-size: 12.5px;
  color: #f2b8a6;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  background: rgba(255, 107, 91, .08);
  border: 1px solid rgba(255, 107, 91, .22);
}
.quiz { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.quiz button {
  padding: 9px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--raise);
  text-align: left;
  font-size: 14px;
}
.quiz button:hover:not(:disabled) { border-color: var(--brass); }
.quiz button.right { border-color: var(--good); background: rgba(75, 208, 138, .12); }
.quiz button.wrong { border-color: var(--bad); background: rgba(255, 107, 91, .1); }
.quiz button:disabled { cursor: default; }
.score { font: 500 12px var(--f-mono); color: var(--ink-3); }
.result { margin: 0; font-weight: 600; font-size: 14px; }
.result.ok { color: var(--good); }
.result.no { color: var(--bad); }

/* ---------- Picker ---------- */
.picker-head h1 {
  margin: 4px 0 0;
  font: 500 clamp(36px, 4vw, 52px)/.95 var(--f-display);
  letter-spacing: -0.01em;
}
.picker-head h1 em {
  font-style: italic;
  color: var(--flame);
  text-shadow: 0 0 28px color-mix(in srgb, var(--flame) 60%, transparent);
  transition: color .6s ease, text-shadow .6s ease;
}
.picker-head p { margin: 6px 0 0; color: var(--ink-2); max-width: 60ch; }
.picker-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  align-items: center;
  position: sticky;
  top: -22px;
  z-index: 4;
  padding: 10px 0;
  background: linear-gradient(var(--soot) 75%, transparent);
}
.tabs { display: inline-flex; gap: 4px; }
.tabs button {
  border: 0;
  background: transparent;
  padding: 6px 2px;
  margin-right: 12px;
  font: 500 23px/1 var(--f-display);
  letter-spacing: 0;
  color: var(--ink-3);
  border-bottom: 2px solid transparent;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--brass); }
.search {
  flex: 1 1 200px;
  min-width: 0;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  font-size: 14px;
}
.search::placeholder { color: var(--ink-3); }
.only { display: inline-flex; gap: 7px; align-items: center; font-size: 13px; color: var(--ink-2); cursor: pointer; }
.only input { accent-color: var(--brass); }

/* Periodic table */
.ptable-wrap { overflow-x: auto; container-type: inline-size; padding-bottom: 4px; }
.ptable {
  display: grid;
  grid-template-columns: repeat(18, minmax(34px, 1fr));
  grid-template-rows: repeat(7, auto) 12px repeat(2, auto);
  gap: 3px;
  min-width: 640px;
}
.el {
  position: relative;
  aspect-ratio: 1 / 1.08;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--raise);
  padding: 3px 4px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  text-align: left;
  overflow: hidden;
  transition: transform .12s ease, border-color .12s ease;
}
.el .z { font: 400 9px/1 var(--f-mono); color: var(--ink-3); }
.el .sym { font: 600 clamp(13px, 1.45cqi, 19px)/1 var(--f-body); letter-spacing: 0; }
.el .nm { display: none; font-size: 8.5px; line-height: 1.1; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@container (min-width: 820px) { .el .nm { display: block; } .el { padding: 4px 5px; } }
.el[data-state="color"] {
  background: linear-gradient(to top, color-mix(in srgb, var(--fc) 30%, var(--raise)) 0, var(--raise) 70%);
  border-color: color-mix(in srgb, var(--fc) 40%, var(--line));
}
.el[data-state="color"]::after {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 3px;
  background: var(--fc);
  box-shadow: 0 0 10px var(--fc);
}
.el[data-state="gas"] { background: transparent; border-style: dashed; }
.el[data-state="gas"] .sym, .el[data-state="synthetic"] .sym { color: var(--ink-3); }
.el[data-state="synthetic"] {
  background: repeating-linear-gradient(135deg, var(--panel) 0 4px, transparent 4px 8px);
}
.el:hover { transform: translateY(-2px); border-color: var(--brass); }
.el.dim { opacity: .18; }
.el.flash { animation: flash .5s ease; }
@keyframes flash { 40% { transform: scale(1.12); border-color: var(--brass-2); } }
.fblock-label {
  grid-column: 3;
  font: 500 9px/1.2 var(--f-mono);
  color: var(--ink-3);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  border: 1px dotted var(--line);
  border-radius: 6px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 12px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.legend span { display: inline-flex; align-items: center; gap: 7px; }
.legend i { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--line); background: var(--raise); }
.legend i.c { border-bottom: 3px solid #35f26a; background: linear-gradient(to top, #1f3a2a, var(--raise)); }
.legend i.g { border-style: dashed; background: transparent; }
.legend i.s { background: repeating-linear-gradient(135deg, var(--panel) 0 3px, transparent 3px 6px); }

/* Compounds */
.cgroup + .cgroup { margin-top: 18px; }
.cgroup h3 {
  margin: 0 0 8px;
  font: 500 10.5px var(--f-mono);
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.clist { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 6px; }
.citem {
  display: grid;
  grid-template-columns: 12px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  text-align: left;
  min-width: 0;
}
.citem:hover { border-color: var(--brass); background: var(--raise); }
.citem .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--fc); box-shadow: 0 0 8px var(--fc); }
.citem .dot.none { background: transparent; border: 1px dashed var(--ink-3); box-shadow: none; }
.citem .cn { min-width: 0; display: flex; flex-direction: column; }
.citem .cn b { font-weight: 500; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.citem .cn small { font: 400 12px var(--f-mono); color: var(--ink-3); }
.citem .cc { font-size: 12px; color: var(--ink-2); text-align: right; }
.citem[hidden] { display: none; }
.empty { color: var(--ink-3); font-size: 14px; }

/* ---------- Narrow screens ---------- */
@media (max-width: 900px) {
  html, body { height: auto; }
  .app { display: flex; flex-direction: column; height: auto; }
  .bench { display: contents; }
  .stage {
    order: 1;
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 6;
    height: 46vh;
    min-height: 280px;
    margin: 12px 16px 0;
    box-shadow: 0 12px 24px var(--soot);
  }
  .controls { order: 2; padding: 12px 16px 0; }
  .btn.primary { margin-left: 0; }
  .detail { order: 3; margin: 12px 16px 0; }
  .picker { order: 4; overflow: visible; padding: 20px 16px 32px; }
  .picker-bar { position: static; }
  .hud-active { max-width: 150px; }
  .scope canvas { height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .el, .cobalt-pane, .hud-color { transition: none; }
  .el.flash { animation: none; }
}
