/* ============================================================================
   PERIODIC TABLE — interactive component for Earth Systems Science
   ----------------------------------------------------------------------------
   Mount point:  <div data-periodic-table></div>
   Renderer:     /assets/js/periodic-table.js
   Data:         /assets/data/periodic-table.json

   Colour rules:
     * Every colour is an existing studio token. No new literals.
     * Category hues reuse the per-course accent tokens (--c-alg1 … --c-ess),
       which studio-tokens.css already contrast-verifies in BOTH the dark
       theme and the .light-band daylight scope (lowest pair 4.59:1).
     * Category is never the only signal: the symbol always renders in --ink
       on a light wash, the category name is in the cell's accessible label,
       and the detail panel spells it out.
     * The eight crust-abundant elements are marked by SHAPE (heavy ink ring
       + a diamond glyph), not by hue, so the mark survives colour blindness
       and grayscale printing.
   ========================================================================== */

.pt {
  --pt-cell: clamp(44px, 4.6vw, 58px);   /* never below the 44px touch target */
  --pt-gap: 3px;
  --pt-radius: 7px;
  --pt-cat: var(--muted);   /* inherited default; every hue below overrides it */
  display: block;
  container-type: inline-size;
}

/* ---- category hues --------------------------------------------------------
   Scoped with a descendant selector on purpose. A bare .pt-cat--x would tie
   on specificity with the component defaults (.pt-cell, .pt-chip, …) and lose
   to whichever came later in the file, flattening the whole table to --muted. */
.pt .pt-cat--alkali-metal          { --pt-cat: var(--c-calc); }
.pt .pt-cat--alkaline-earth-metal  { --pt-cat: var(--c-sci); }
.pt .pt-cat--transition-metal      { --pt-cat: var(--c-alg1); }
.pt .pt-cat--post-transition-metal { --pt-cat: var(--c-apphys); }
.pt .pt-cat--metalloid             { --pt-cat: var(--c-precalc); }
.pt .pt-cat--reactive-nonmetal     { --pt-cat: var(--c-geo); }
.pt .pt-cat--noble-gas             { --pt-cat: var(--c-alg2); }
.pt .pt-cat--lanthanide            { --pt-cat: var(--c-ess); }
.pt .pt-cat--actinide              { --pt-cat: var(--c-phys); }
.pt .pt-cat--unknown               { --pt-cat: var(--muted); }
.pt .pt-detail--idle               { --pt-cat: var(--accent); }

/* ============================================================
   TOOLBAR — search, category filters, crust filter
   ============================================================ */
.pt-toolbar {
  display: grid;
  gap: 1rem;
  padding: 1rem 1.1rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--arch-shadow-sm);
}

.pt-searchrow {
  display: flex;
  flex-wrap: wrap;
  gap: .75rem;
  align-items: flex-end;
}

.pt-field { flex: 1 1 15rem; min-width: 0; }

.pt-label {
  display: block;
  margin-bottom: .3rem;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .64rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.pt-input {
  width: 100%;
  min-height: 44px;
  padding: .55rem .75rem;
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 1rem;   /* 16px stops iOS Safari zooming the page on focus */
}
.pt-input::placeholder { color: var(--muted); opacity: 1; }
.pt-input:focus-visible {
  outline: 3px solid var(--accent-bright);
  outline-offset: 1px;
  border-color: var(--accent);
}

.pt-chipset {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
}

.pt-chip {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  min-height: 44px;
  padding: .35rem .8rem;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink-2);
  font-family: var(--font-mono);
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  cursor: pointer;
}
.pt-chip::before {
  content: "";
  width: .62rem;
  height: .62rem;
  flex: 0 0 auto;
  border-radius: 3px;
  background: var(--pt-cat);
}
.pt-chip--plain::before { display: none; }
.pt-chip:hover { border-color: var(--accent); color: var(--ink); }
.pt-chip:focus-visible {
  outline: 3px solid var(--accent-bright);
  outline-offset: 2px;
}
.pt-chip[aria-pressed="true"] {
  border-color: var(--pt-cat);
  background: color-mix(in srgb, var(--pt-cat) 14%, var(--surface));
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--pt-cat);
}
.pt-chip--plain[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-tint);
  box-shadow: inset 0 0 0 1px var(--accent);
}
.pt-chip .pt-chip-count {
  color: var(--muted);
  font-weight: 600;
}
.pt-chip[aria-pressed="true"] .pt-chip-count { color: var(--ink-2); }

/* the crust chip carries the same diamond used on the grid cells */
.pt-chip--crust::before {
  content: "\25C6";
  width: auto;
  height: auto;
  background: none;
  color: var(--ink);
  font-size: .7rem;
}

.pt-status {
  margin: .85rem 0 .5rem;
  color: var(--ink-2);
  font-size: .9rem;
  line-height: 1.5;
}
.pt-status strong { color: var(--ink); }

/* ============================================================
   PREVIEW — updates on hover and on keyboard focus
   ============================================================ */
.pt-preview {
  display: flex;
  align-items: center;
  gap: .9rem;
  min-height: 4.6rem;
  margin: 0 0 .75rem;
  padding: .7rem .9rem;
  border: 1px dashed var(--line-strong);
  border-radius: 12px;
  background: var(--surface-2);
}
.pt-preview-tile {
  display: grid;
  place-content: center;
  width: 3.2rem;
  height: 3.2rem;
  flex: 0 0 auto;
  border: 1px solid var(--line-strong);
  border-top: 3px solid var(--pt-cat);
  border-radius: 8px;
  background: color-mix(in srgb, var(--pt-cat) 12%, var(--surface));
  color: var(--ink);
  font-family: var(--font-heading);
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1;
}
.pt-preview-body { min-width: 0; }
.pt-preview-name {
  display: block;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 1.2rem;
  line-height: 1.2;
}
.pt-preview-meta {
  display: block;
  margin-top: .2rem;
  color: var(--ink-2);
  font-size: .82rem;
  line-height: 1.45;
}
.pt-preview-hint {
  color: var(--muted);
  font-size: .85rem;
  line-height: 1.5;
}

/* ============================================================
   THE GRID — 18 columns x 7 periods, f-block below
   ============================================================ */
.pt-scroll {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;   /* keep the page from scrolling sideways */
  -webkit-overflow-scrolling: touch;
  padding-bottom: .5rem;
}

.pt-grid {
  display: grid;
  width: max-content;
  grid-template-columns: 1.6rem repeat(18, var(--pt-cell));
  /* row 1 = group numbers, rows 2-8 = periods 1-7,
     row 9 = the gap, rows 10-11 = lanthanides / actinides */
  grid-template-rows: 1.15rem repeat(7, var(--pt-cell)) .95rem repeat(2, var(--pt-cell));
  gap: var(--pt-gap);
}

.pt-axis {
  display: grid;
  place-content: center;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .58rem;
  font-weight: 700;
  letter-spacing: .04em;
}

/* the "57-71" / "89-103" placeholders sitting in group 3 */
.pt-marker {
  display: grid;
  place-content: center;
  border: 1px dashed var(--line-strong);
  border-radius: var(--pt-radius);
  background: var(--surface-2);
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .52rem;
  font-weight: 700;
  line-height: 1.2;
  text-align: center;
}

.pt-fseries {
  grid-column: 1 / 4;
  display: grid;
  place-content: center end;
  padding-right: .3rem;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .5rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
  text-align: right;
}

/* ---- the element cell ----------------------------------------------------- */
.pt-cell {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: 0;
  padding: 2px;
  border: 1px solid var(--line-strong);
  border-top: 3px solid var(--pt-cat);
  border-radius: var(--pt-radius);
  background: color-mix(in srgb, var(--pt-cat) 11%, var(--surface));
  color: var(--ink);
  font-family: var(--font-body);
  text-align: center;
  cursor: pointer;
  appearance: none;
}

.pt-cell-num {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .5rem;
  font-weight: 700;
  line-height: 1;
}
.pt-cell-sym {
  color: var(--ink);
  font-family: var(--font-heading);
  font-size: clamp(.85rem, 1.6vw, 1.05rem);
  font-weight: 700;
  line-height: 1.05;
}
.pt-cell-name {
  display: none;   /* revealed once the cells are wide enough to hold it */
  max-width: 100%;
  overflow: hidden;
  color: var(--ink-2);
  /* .52rem = 8.3px, the same floor the axis/marker labels already use. It was
     .42rem, which rendered at 6.7px: contrast-passing and still unreadable.
     Legibility wins over completeness here — the full name is always in the
     cell's accessible label, the hover/focus preview, and the detail card. */
  font-size: .52rem;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* 1060px of container is where the cells reach their 55-58px cap. Below that
   the names are truncated more than they are read, so they stay hidden. */
@container (min-width: 1060px) {
  .pt-cell-name { display: block; }
}

.pt-cell:hover {
  border-color: var(--pt-cat);
  background: color-mix(in srgb, var(--pt-cat) 22%, var(--surface));
  box-shadow: var(--arch-shadow-sm);
}
.pt-cell:focus-visible {
  outline: 3px solid var(--accent-bright);
  outline-offset: 2px;
  z-index: 2;
}
.pt-cell[aria-current="true"] {
  box-shadow: inset 0 0 0 2px var(--accent), var(--arch-shadow-sm);
  border-color: var(--accent);
}
.pt-cell:disabled {
  cursor: default;
  opacity: .3;
  filter: grayscale(1);
  box-shadow: none;
}

/* ---- the eight crust-abundant elements ------------------------------------
   Shape-based, not hue-based: heavy ink ring + diamond glyph. -------------- */
.pt-cell--crust {
  border: 2px solid var(--ink);
  border-top: 3px solid var(--pt-cat);
  box-shadow: inset 0 0 0 2px var(--surface);
}
.pt-cell--crust .pt-cell-sym { font-weight: 800; }
.pt-cell--crust::after {
  content: "\25C6";
  position: absolute;
  top: 1px;
  right: 3px;
  color: var(--ink);
  font-size: .46rem;
  line-height: 1;
  pointer-events: none;
}
.pt-cell--crust:hover { box-shadow: inset 0 0 0 2px var(--surface), var(--arch-shadow-sm); }

/* ============================================================
   LEGEND
   ============================================================ */
.pt-legend {
  display: flex;
  flex-wrap: wrap;
  gap: .45rem .9rem;
  margin: .9rem 0 0;
  padding: .85rem .95rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}
.pt-legend-item {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  color: var(--ink-2);
  font-size: .76rem;
  line-height: 1.35;
}
.pt-legend-swatch {
  width: .8rem;
  height: .8rem;
  flex: 0 0 auto;
  border: 1px solid var(--line-strong);
  border-top: 2px solid var(--pt-cat);
  border-radius: 3px;
  background: color-mix(in srgb, var(--pt-cat) 22%, var(--surface));
}
.pt-legend-item--crust {
  flex: 1 1 100%;
  padding-top: .55rem;
  border-top: 1px solid var(--line);
  color: var(--ink);
}
.pt-legend-item--crust .pt-legend-swatch {
  border: 2px solid var(--ink);
  border-top: 2px solid var(--ink);
  background: var(--surface);
}

/* ============================================================
   DETAIL PANEL
   ============================================================ */
.pt-detail {
  margin-top: 1rem;
  padding: 1.1rem 1.15rem;
  border: 1px solid var(--line);
  border-left: 4px solid var(--pt-cat);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--arch-shadow-sm);
}
.pt-detail-empty {
  margin: 0;
  color: var(--muted);
  font-size: .92rem;
  line-height: 1.6;
}
.pt-detail-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .9rem;
  margin-bottom: .9rem;
}
.pt-detail-tile {
  display: grid;
  place-content: center;
  width: 4.4rem;
  height: 4.4rem;
  flex: 0 0 auto;
  border: 1px solid var(--line-strong);
  border-top: 4px solid var(--pt-cat);
  border-radius: 10px;
  background: color-mix(in srgb, var(--pt-cat) 13%, var(--surface));
  color: var(--ink);
  font-family: var(--font-heading);
  font-size: 1.7rem;
  font-weight: 800;
  line-height: 1;
}
.pt-detail-titles { min-width: 0; }
.pt-detail-eyebrow {
  display: block;
  color: var(--pt-cat);
  font-family: var(--font-mono);
  font-size: .64rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
}
.pt-detail-name {
  margin: .2rem 0 0;
  color: var(--ink);
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 3.4vw, 1.9rem);
  line-height: 1.15;
}

.pt-facts {
  display: grid;
  gap: .55rem 1rem;
  margin: 0 0 .9rem;
  grid-template-columns: 1fr;
}
@media (min-width: 560px) { .pt-facts { grid-template-columns: 1fr 1fr; } }
@media (min-width: 900px) { .pt-facts { grid-template-columns: repeat(3, 1fr); } }
.pt-fact {
  min-width: 0;
  padding-top: .45rem;
  border-top: 1px solid var(--line);
}
.pt-fact dt {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .6rem;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
}
.pt-fact dd {
  margin: .2rem 0 0;
  color: var(--ink);
  font-size: .95rem;
  line-height: 1.45;
  overflow-wrap: break-word;
}
.pt-fact dd sup { font-size: .68em; }
.pt-fact-note {
  display: block;
  margin-top: .15rem;
  color: var(--muted);
  font-size: .74rem;
  line-height: 1.4;
}

.pt-callout {
  margin: 0 0 .75rem;
  padding: .8rem .9rem;
  border: 1px solid var(--line-strong);
  border-left: 4px solid var(--ink);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--ink);
  font-size: .92rem;
  line-height: 1.6;
}
.pt-callout strong { font-weight: 700; }
.pt-note {
  margin: 0 0 .6rem;
  color: var(--ink-2);
  font-size: .92rem;
  line-height: 1.65;
}
.pt-note:last-child { margin-bottom: 0; }
.pt-note--quiet { color: var(--muted); font-size: .82rem; }

.pt-sources {
  margin: 1rem 0 0;
  color: var(--muted);
  font-size: .74rem;
  line-height: 1.6;
}

/* ============================================================
   MOTION — opt in only when the visitor has not asked us to stop
   ============================================================ */
@media (prefers-reduced-motion: no-preference) {
  .pt-cell,
  .pt-chip,
  .pt-input { transition: background-color .16s ease, border-color .16s ease, box-shadow .16s ease, color .16s ease; }
  .pt-cell:hover { transform: translateY(-1px); }
}

/* ============================================================
   PRINT — flatten to ink-light, drop the controls
   ============================================================ */
@media print {
  .pt-toolbar,
  .pt-preview { display: none !important; }
  .pt-scroll { overflow: visible !important; }
  .pt-cell { box-shadow: none !important; }
  .pt-cell:disabled { opacity: 1 !important; filter: none !important; }
}

/* ---------------------------------------------------------------------------
   Contested values.

   Some numbers on this table are genuinely disputed between authorities —
   Technetium's bracketed mass is the live case: IUPAC/CIAAW publishes [98],
   many reference tables publish [97], and neither is wrong. Rather than pick a
   side silently, the cell is marked and the detail panel explains the split.

   The marker sits bottom-left so it cannot collide with the crust diamond,
   which occupies top-right.
   --------------------------------------------------------------------------- */
.pt-cell--contested::before {
  content: "?";
  position: absolute;
  bottom: 1px;
  left: 3px;
  font-family: var(--font-mono);
  font-size: .48rem;
  font-weight: 700;
  line-height: 1;
  color: var(--muted);
}
.pt-cell--contested:hover::before,
.pt-cell--contested:focus-visible::before { color: var(--ink); }

.pt-contested {
  margin-top: .85rem;
  padding: .75rem .9rem;
  border: 1px solid var(--line-strong);
  border-left: 3px solid var(--muted);
  border-radius: 10px;
  background: var(--recessed);
}
.pt-contested-head {
  margin: 0;
  font-weight: 700;
  color: var(--ink);
  font-size: .9rem;
}
.pt-contested-values {
  margin: .4rem 0 0;
  font-family: var(--font-mono);
  font-size: .82rem;
  color: var(--ink-2);
}
.pt-contested-value { color: var(--ink); font-weight: 700; }
.pt-contested-note,
.pt-contested-do {
  margin: .5rem 0 0;
  font-size: .84rem;
  line-height: 1.55;
  color: var(--ink-2);
}
.pt-contested-do { color: var(--ink); font-weight: 600; }
