:root {
  --page: #FFFDF9;
  --surface: #FFFFFF;
  --board: #ECE6DC;
  --cell: #E0D9CD;
  --border: #E0E0E0;
  --text: #1A1A1A;
  --muted: #4A4A4A;
  --accent: #004A70;
  --veil: rgba(26, 26, 26, 0.38);
  --glow: rgba(255, 236, 170, 0.95);

  /* element families: tile ground / ink */
  --nonmetal-bg: #E2ECDD;   --nonmetal-ink: #2D4A2B;
  --noble-bg: #E7E1F2;      --noble-ink: #43376C;
  --alkaline-bg: #F3E5CF;   --alkaline-ink: #6A4A1F;
  --metalloid-bg: #D9EAE8;  --metalloid-ink: #22504E;
  --transition-bg: #DCE4F0; --transition-ink: #2A4467;
  --gold-bg: #F2E3B3;       --gold-ink: #6B5212;

  --gap: 8px;
  --radius: 10px;
  font-family: 'Proxima Nova', system-ui, 'Segoe UI', Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --page: #0C1417;
    --surface: #141D22;
    --board: #141D22;
    --cell: #1D282D;
    --border: #324148;
    --text: #EBEEEF;
    --muted: #9AA8AF;
    --accent: #2295D0;
    --veil: rgba(0, 0, 0, 0.55);
    --glow: rgba(255, 226, 140, 0.85);
    --nonmetal-bg: #22332A;   --nonmetal-ink: #CFE3C8;
    --noble-bg: #2B2741;      --noble-ink: #DDD4F6;
    --alkaline-bg: #3A2F21;   --alkaline-ink: #F0DCC0;
    --metalloid-bg: #1E3634;  --metalloid-ink: #C6E6E3;
    --transition-bg: #212F43; --transition-ink: #CFDCF0;
    --gold-bg: #3E3418;       --gold-ink: #F2DF9E;
  }
}
:root[data-theme="dark"] {
  --page: #0C1417; --surface: #141D22; --board: #141D22; --cell: #1D282D; --border: #324148;
  --text: #EBEEEF; --muted: #9AA8AF; --accent: #2295D0; --veil: rgba(0, 0, 0, 0.55); --glow: rgba(255, 226, 140, 0.85);
  --nonmetal-bg: #22332A; --nonmetal-ink: #CFE3C8; --noble-bg: #2B2741; --noble-ink: #DDD4F6;
  --alkaline-bg: #3A2F21; --alkaline-ink: #F0DCC0; --metalloid-bg: #1E3634; --metalloid-ink: #C6E6E3;
  --transition-bg: #212F43; --transition-ink: #CFDCF0; --gold-bg: #3E3418; --gold-ink: #F2DF9E;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--page);
  color: var(--text);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
button { font: inherit; color: inherit; }

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
  padding:
    calc(12px + env(safe-area-inset-top))
    calc(16px + env(safe-area-inset-right))
    calc(16px + env(safe-area-inset-bottom))
    calc(16px + env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { margin: 0; font-size: 2rem; font-weight: 600; letter-spacing: -0.02em; color: var(--accent); }
.stats { display: flex; gap: 8px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 6px 12px; min-width: 76px; text-align: center; display: flex; flex-direction: column;
}
.stat-label { font-size: 0.72rem; color: var(--muted); }
.stat-value { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.next-wrap { display: flex; align-items: center; gap: 8px; }
.next-label { font-size: 0.8rem; color: var(--muted); }
.next { width: 54px; height: 54px; position: relative; }
.next .tile { position: absolute; inset: 0; width: 100%; height: 100%; transform: none; }
.menu { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.menu button, .sheet .btn {
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: 8px 12px; font-size: 0.82rem; cursor: pointer; min-height: 36px;
}
.menu button:focus-visible, .sheet .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- board ---- */
.board-wrap { display: flex; justify-content: center; }
.board {
  --size: min(100%, 440px, calc(100svh - 280px - env(safe-area-inset-top) - env(safe-area-inset-bottom)));
  width: var(--size);
  aspect-ratio: 1;
  min-width: 260px;
  position: relative;
  background: var(--board);
  border-radius: 14px;
  touch-action: none;
}
.cells, .tiles { position: absolute; inset: 0; }
.cells {
  display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr);
  gap: var(--gap); padding: var(--gap);
}
.cells i { background: var(--cell); border-radius: var(--radius); }

.tile {
  --c: 0; --r: 0;
  position: absolute;
  left: var(--gap); top: var(--gap);
  width: calc((100% - 5 * var(--gap)) / 4);
  height: calc((100% - 5 * var(--gap)) / 4);
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
  border-radius: var(--radius);
  background: var(--fam-bg);
  color: var(--fam-ink);
  container-type: size;
  cursor: pointer;
}
.tile.f-nonmetal   { --fam-bg: var(--nonmetal-bg);   --fam-ink: var(--nonmetal-ink); }
.tile.f-noble      { --fam-bg: var(--noble-bg);      --fam-ink: var(--noble-ink); }
.tile.f-alkaline   { --fam-bg: var(--alkaline-bg);   --fam-ink: var(--alkaline-ink); }
.tile.f-metalloid  { --fam-bg: var(--metalloid-bg);  --fam-ink: var(--metalloid-ink); }
.tile.f-transition { --fam-bg: var(--transition-bg); --fam-ink: var(--transition-ink); }
.tile.f-gold       { --fam-bg: var(--gold-bg);       --fam-ink: var(--gold-ink); box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--gold-ink) 45%, transparent); }
.tile.dead { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--fam-ink) 30%, transparent); }

/* Standard nuclide notation (2026-10-07): mass number upper left, atomic number lower left of the
   symbol, both from the engine's table. The name sits below, smaller. */
.tile .nuc {
  position: absolute; left: 0; right: 0; top: 4cqh; bottom: 24cqh;
  display: flex; align-items: center; justify-content: center; gap: 1.5cqw;
}
.tile .az {
  display: flex; flex-direction: column; justify-content: space-between; align-items: flex-end;
  height: 38cqw; padding: 1cqw 0; font-size: 14.5cqw; line-height: 1; font-weight: 600;
  font-variant-numeric: tabular-nums; letter-spacing: -0.02em;
}
.tile .az .zn { opacity: 0.72; font-weight: 500; }
.tile .sym { font-size: 38cqw; line-height: 1; font-weight: 600; letter-spacing: -0.02em; }
.tile .name { position: absolute; bottom: 7cqh; left: 0; right: 0; text-align: center; font-size: 12cqw; opacity: 0.85; white-space: nowrap; }
.tile.long .name { font-size: 10.5cqw; letter-spacing: -0.01em; }
.tile .run { position: absolute; top: 3cqh; right: 7cqw; font-size: 15cqw; font-weight: 700; opacity: 0.7; letter-spacing: -0.04em; }
/* Decay clock: a ring that empties, with the swipes left in the middle. */
.tile .clock {
  position: absolute; top: 5cqh; right: 5cqw; width: 23cqw; height: 23cqw; border-radius: 50%;
  background: conic-gradient(var(--fam-ink) calc(var(--f) * 360deg), color-mix(in srgb, var(--fam-ink) 18%, transparent) 0);
  display: grid; place-items: center;
}
.tile .clock::before { content: ''; position: absolute; inset: 3.2cqw; border-radius: 50%; background: var(--fam-bg); }
.tile .clock b { position: relative; font-size: 12cqw; font-weight: 700; line-height: 1; }
/* Short-lived nuclei (13N, 15O): a dashed ring while they wait to decay. */
.tile.brief { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--fam-ink) 55%, transparent); outline: 2px dashed color-mix(in srgb, var(--fam-ink) 45%, transparent); outline-offset: -5px; }
.tile.decaying { animation: fuse 150ms ease-out both, jitter 300ms 60ms linear both; }
.tile.decayed { animation: decayed 380ms ease-out both; }
.tile.emitted { animation: fade-in 200ms ease-out both; }
@keyframes jitter {
  0%, 100% { translate: 0 0; } 20% { translate: -1.5px 0.5px; } 40% { translate: 1.5px -0.5px; }
  60% { translate: -1px -1px; } 80% { translate: 1px 1px; }
}
@keyframes decayed {
  0%   { filter: brightness(1.5) saturate(0.6); box-shadow: 0 0 0 0 var(--glow), inset 0 0 0 0 transparent; scale: 0.92; }
  40%  { box-shadow: 0 0 0 7px color-mix(in srgb, var(--glow) 55%, transparent); scale: 1.04; }
  100% { filter: none; box-shadow: 0 0 0 14px transparent; scale: 1; }
}

.tile.pop { animation: pop 160ms ease-out both; }
.tile.fused { animation: fuse 150ms ease-out both; }
.tile.nova-born { animation: nova 900ms ease-out both; }
.tile.leaving { transition: opacity 260ms ease-out; opacity: 0; }
@keyframes pop { from { scale: 0.2; opacity: 0; } to { scale: 1; opacity: 1; } }
@keyframes fuse {
  0%   { scale: 0.85; box-shadow: 0 0 0 0 var(--glow); filter: brightness(1.25); }
  35%  { scale: 1.08; box-shadow: 0 0 18px 6px var(--glow); }
  100% { scale: 1; box-shadow: 0 0 0 0 transparent; filter: none; }
}
@keyframes nova {
  0%   { scale: 0.3; opacity: 0; box-shadow: 0 0 0 0 var(--glow); }
  30%  { scale: 1.25; opacity: 1; box-shadow: 0 0 40px 18px var(--glow); }
  100% { scale: 1; box-shadow: 0 0 0 0 transparent; }
}
.flash { position: absolute; inset: 0; border-radius: 14px; background: var(--glow); pointer-events: none; animation: flash 700ms ease-out both; }
@keyframes flash { from { opacity: 0.85; } to { opacity: 0; } }
@keyframes fade-in { from { opacity: 0.25; } to { opacity: 1; } }
.ver { margin: auto 0 0; text-align: center; color: var(--muted); font-size: 0.7rem; opacity: 0.75; font-variant-numeric: tabular-nums; }
.sheet .ver-line { margin: 4px 0 0; color: var(--muted); font-size: 0.75rem; text-align: center; font-variant-numeric: tabular-nums; }

.points {
  position: absolute; pointer-events: none; font-weight: 700; font-size: 0.95rem; color: var(--accent);
  animation: rise 700ms ease-out both;
}
@keyframes rise { from { opacity: 0; translate: -50% 0; } 20% { opacity: 1; } to { opacity: 0; translate: -50% -34px; } }

.hint { margin: 0; text-align: center; color: var(--muted); font-size: 0.85rem; min-height: 1.2em; }

/* ---- sheet (cards, gallery, rules) ---- */
.veil {
  position: fixed; inset: 0; background: var(--veil); display: flex; align-items: flex-end; justify-content: center;
  padding: 16px calc(16px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  z-index: 10; animation: fade 180ms ease-out both;
}
.veil[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  background: var(--surface); color: var(--text); border-radius: 18px; width: 100%; max-width: 440px;
  max-height: calc(100svh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow-y: auto; padding: 22px 20px 18px; animation: rise-in 260ms cubic-bezier(.2,.8,.2,1) both;
  box-shadow: 0 12px 40px rgba(0,0,0,.18);
}
@keyframes rise-in { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet h2 { margin: 0 0 4px; font-size: 1.35rem; font-weight: 600; }
.sheet .sub { margin: 0 0 14px; color: var(--muted); font-size: 0.9rem; }
.sheet .big { width: 116px; height: 116px; position: relative; margin: 2px auto 14px; }
.sheet .big .tile { position: absolute; inset: 0; width: 100%; height: 100%; transform: none; cursor: default; }
.sheet .voice { font-size: 1.05rem; line-height: 1.45; margin: 0 0 14px; }
.sheet .label { font-size: 0.74rem; color: var(--muted); letter-spacing: 0.04em; margin: 0 0 3px; }
.sheet .science { margin: 0 0 12px; line-height: 1.45; font-size: 0.95rem; }
.sheet .note { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: var(--cell); font-size: 0.9rem; line-height: 1.4; }
.sheet .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; flex-wrap: wrap; }
.sheet .actions.left { justify-content: flex-start; }
.sheet .btn.primary { background: var(--accent); color: var(--page); border-color: var(--accent); }
.sheet .tap { text-align: center; color: var(--muted); font-size: 0.8rem; margin: 6px 0 0; }
.sheet ul { padding-left: 18px; margin: 0 0 12px; line-height: 1.45; }
.sheet li { margin-bottom: 6px; }
.sheet .center { text-align: center; }
.sheet .scoreline { font-size: 1.05rem; margin: 0 0 12px; text-align: center; }

/* gallery: a real periodic-table grid, 18 groups wide. Cells are at least 25px so the mass number
   stays readable; on a phone the table scrolls sideways inside its frame. */
.ptable-wrap { overflow-x: auto; margin: 8px 0 12px; padding-bottom: 4px; -webkit-overflow-scrolling: touch; }
.ptable { display: grid; grid-template-columns: repeat(18, minmax(25px, 1fr)); gap: 2px; min-width: 484px; }
.ptable .pc { aspect-ratio: 1; border-radius: 3px; background: var(--cell); opacity: 0.45; }
.ptable .pc.ours { opacity: 1; position: relative; container-type: size; display: block; font-weight: 600; border: 0; padding: 0; cursor: pointer; overflow: hidden; }
.ptable .pc.ours .pa { position: absolute; top: 6cqh; left: 8cqw; font-size: 30cqw; line-height: 1; font-weight: 600; opacity: 0.85; font-variant-numeric: tabular-nums; }
.ptable .pc.ours .ps { position: absolute; left: 0; right: 0; bottom: 8cqh; text-align: center; font-size: 46cqw; line-height: 1; }
.ptable .pc.ours.unmet { background: color-mix(in srgb, var(--muted) 38%, var(--cell)); color: transparent; cursor: default; }
.ptable .pc.met.f-nonmetal { background: var(--nonmetal-bg); color: var(--nonmetal-ink); }
.ptable .pc.met.f-noble { background: var(--noble-bg); color: var(--noble-ink); }
.ptable .pc.met.f-alkaline { background: var(--alkaline-bg); color: var(--alkaline-ink); }
.ptable .pc.met.f-metalloid { background: var(--metalloid-bg); color: var(--metalloid-ink); }
.ptable .pc.met.f-transition { background: var(--transition-bg); color: var(--transition-ink); }
.ptable .pc.met.f-gold { background: var(--gold-bg); color: var(--gold-ink); }
.isos { margin: 0 0 10px; }
.isos .iso-label { display: block; font-size: 0.78rem; color: var(--muted); margin: 0 0 4px; }
.isos .iso-tiles { display: flex; gap: 8px; flex-wrap: wrap; }
.isos .isocell { width: 58px; height: 58px; position: relative; border: 0; padding: 0; background: none; cursor: pointer; }
.isos .isocell .tile { position: absolute; inset: 0; width: 100%; height: 100%; transform: none; }
.isos .isocell.unmet .tile { background: color-mix(in srgb, var(--muted) 38%, var(--cell)); color: transparent; box-shadow: none; outline: none; }
.isos .isocell.unmet .clock { display: none; }
/* recipe row on a card: [C] + [He] -> [O] */
.recipe { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 6px; margin: 0 0 14px; }
.recipe .rc-tile { width: 46px; height: 46px; position: relative; flex: none; }
.recipe.cycle { gap: 4px; }
.recipe.cycle .rc-tile { width: 42px; height: 42px; }
.recipe .rc-tile .tile { position: absolute; inset: 0; width: 100%; height: 100%; transform: none; cursor: default; }
.recipe .rc-op { font-size: 1.1rem; color: var(--muted); }
.recipe .rc-word { font-size: 0.85rem; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  /* Tiles jump to their cells; new and fused tiles still fade gently. */
  .tile.pop, .tile.fused, .tile.nova-born, .tile.decayed, .tile.decaying, .tile.emitted { animation: fade-in 160ms ease-out both !important; }
  .points, .flash, .sheet, .veil { animation-duration: 1ms !important; }
}
@media (min-width: 700px) {
  .app { padding-top: calc(28px + env(safe-area-inset-top)); }
  .veil { align-items: center; }
}
