/* =============================================================================
   Interactive Periodic Table — styles
   Layout: CSS Grid, 18 columns. Theme: light/dark via prefers-color-scheme.
   Element tiles keep colored backgrounds with dark text in both themes so the
   color coding stays intact; only the page chrome flips light/dark.
   ============================================================================= */

:root {
  /* Page chrome (light theme defaults) */
  --bg: #f4f6fb;
  --surface: #ffffff;
  --text: #1c2833;
  --text-muted: #5b6b7b;
  --border: #d5dbe6;
  --accent: #2f6fed;
  --shadow: 0 6px 24px rgba(20, 30, 50, 0.14);

  /* Category colors — tuned to be distinguishable and readable with dark text */
  --cat-alkali-metal:          #ff8a80;
  --cat-alkaline-earth-metal:  #ffb74d;
  --cat-transition-metal:      #ffd54f;
  --cat-post-transition-metal: #90a4ae;
  --cat-metalloid:             #9ccc65;
  --cat-reactive-nonmetal:     #4db6ac;
  --cat-halogen:               #4fc3f7;
  --cat-noble-gas:             #b39ddb;
  --cat-lanthanide:            #f48fb1;
  --cat-actinide:              #ce93d8;

  --tile-text: #16202b; /* dark text used on every colored tile, both themes */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --surface: #1b2029;
    --text: #e8edf4;
    --text-muted: #9fb0c3;
    --border: #2c3542;
    --accent: #6ea8ff;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* ---- Header --------------------------------------------------------------- */
.site-header {
  text-align: center;
  padding: 1.4rem 1rem 0.4rem;
}
.site-header h1 {
  margin: 0;
  font-size: clamp(1.4rem, 3.5vw, 2.1rem);
  letter-spacing: -0.01em;
}
.tagline {
  margin: 0.3rem 0 0;
  color: var(--text-muted);
  font-size: clamp(0.85rem, 2vw, 1rem);
}

main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 0.5rem 0.75rem 1rem;
}

/* ---- Controls: search + filters ------------------------------------------ */
.controls { margin: 0.75rem auto 1rem; }

.search-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  max-width: 620px;
  margin: 0 auto 0.9rem;
}
.search-label { font-weight: 600; font-size: 0.9rem; color: var(--text-muted); }
.search-input {
  flex: 1;
  padding: 0.55rem 0.75rem;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.search-input:focus-visible,
.reset-btn:focus-visible,
.filter-btn:focus-visible,
.element:focus-visible,
.popup-close:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}
.reset-btn {
  padding: 0.55rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.reset-btn:hover { border-color: var(--accent); color: var(--accent); }

.filter-groups {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1rem 1.6rem;
}
.filter-group { text-align: center; }
.filter-group h2 {
  margin: 0 0 0.4rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}
.filter-group-btns {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.35rem;
  max-width: 640px;
}

.filter-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.32rem 0.6rem;
  font-size: 0.8rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.12s, box-shadow 0.12s, transform 0.05s;
}
.filter-btn:hover { border-color: var(--accent); }
.filter-btn:active { transform: translateY(1px); }
.filter-btn[aria-pressed="true"] {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  font-weight: 700;
}
.swatch {
  width: 0.85rem;
  height: 0.85rem;
  border-radius: 3px;
  flex: 0 0 auto;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

/* Download reference-summary row */
.download-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 0.75rem;
  margin-top: 1.1rem;
}
.download-btn {
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  font-weight: 700;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: filter 0.12s, transform 0.05s;
}
.download-btn:hover { filter: brightness(1.08); }
.download-btn:active { transform: translateY(1px); }
.download-hint { font-size: 0.78rem; color: var(--text-muted); }

/* ---- Periodic table grid -------------------------------------------------- */
.pt-scroll {
  overflow-x: auto;
  padding-bottom: 0.4rem;
  -webkit-overflow-scrolling: touch;
}
.periodic-table {
  display: grid;
  grid-template-columns: repeat(18, minmax(0, 1fr));
  grid-auto-rows: 1fr;
  gap: 3px;
  /* Keep tiles legible: on narrow screens the grid overflows and scrolls. */
  min-width: 760px;
  max-width: 1140px;
  margin: 0 auto;
}

.element {
  position: relative;
  aspect-ratio: 1 / 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  color: var(--tile-text);
  font-family: inherit;
  overflow: hidden;
  transition: transform 0.08s ease, box-shadow 0.08s ease, opacity 0.12s ease;
}
.element:hover {
  transform: scale(1.08);
  box-shadow: var(--shadow);
  z-index: 5;
}
.element .e-number {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: clamp(0.4rem, 0.85vw, 0.62rem);
  font-weight: 600;
  opacity: 0.85;
}
.element .e-symbol {
  font-size: clamp(0.7rem, 1.55vw, 1.15rem);
  font-weight: 800;
  line-height: 1;
}
.element .e-name {
  font-size: clamp(0.32rem, 0.72vw, 0.5rem);
  line-height: 1.05;
  text-align: center;
  margin-top: 1px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Category background colors */
.element.cat-alkali-metal          { background: var(--cat-alkali-metal); }
.element.cat-alkaline-earth-metal  { background: var(--cat-alkaline-earth-metal); }
.element.cat-transition-metal      { background: var(--cat-transition-metal); }
.element.cat-post-transition-metal { background: var(--cat-post-transition-metal); }
.element.cat-metalloid             { background: var(--cat-metalloid); }
.element.cat-reactive-nonmetal     { background: var(--cat-reactive-nonmetal); }
.element.cat-halogen               { background: var(--cat-halogen); }
.element.cat-noble-gas             { background: var(--cat-noble-gas); }
.element.cat-lanthanide            { background: var(--cat-lanthanide); }
.element.cat-actinide              { background: var(--cat-actinide); }

/* Highlight / dim states driven by filters + search */
.periodic-table.filtering .element:not(.matched) { opacity: 0.18; }
.periodic-table.filtering .element.matched {
  box-shadow: 0 0 0 2px var(--text), var(--shadow);
  z-index: 3;
}

/* f-block marker cells ("57–71" / "89–103") in the main table */
.fblock-marker {
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--border);
  border-radius: 5px;
  color: var(--text-muted);
  font-size: clamp(0.4rem, 0.85vw, 0.6rem);
  font-weight: 600;
  text-align: center;
}

/* ---- Element popup ("bubble") -------------------------------------------- */
.popup-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 15, 25, 0.35);
  z-index: 40;
}
.element-popup {
  position: fixed;
  z-index: 50;
  width: min(300px, calc(100vw - 24px));
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 0;
  overflow: hidden;
}
.popup-close {
  position: absolute;
  top: 6px;
  right: 8px;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
}
.popup-close:hover { background: rgba(127, 127, 127, 0.18); }

.popup-header {
  padding: 0.9rem 1rem 0.75rem;
  color: var(--tile-text);
}
.popup-header .ph-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 0.8rem;
  font-weight: 600;
  opacity: 0.9;
}
.popup-header .ph-symbol {
  font-size: 2.4rem;
  font-weight: 800;
  line-height: 1.05;
  margin: 0.1rem 0;
}
.popup-header .ph-name {
  font-size: 1.05rem;
  font-weight: 700;
  margin: 0;
}
.popup-details {
  padding: 0.75rem 1rem 1rem;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.4rem 0.9rem;
  font-size: 0.9rem;
}
.popup-details dt { color: var(--text-muted); font-weight: 600; }
.popup-details dd { margin: 0; font-variant-numeric: tabular-nums; }
.popup-details .config { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-size: 0.85rem; }

/* Full-details toggle + expandable section inside the popup */
.popup-toggle {
  display: block;
  width: calc(100% - 2rem);
  margin: 0 1rem;
  padding: 0.4rem 0.6rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.popup-toggle:hover { border-color: var(--accent); }
.popup-toggle:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.popup-full { padding-top: 0.25rem; border-top: 0; }
.popup-full[hidden] { display: none; }

/* On small screens the popup becomes a bottom sheet */
@media (max-width: 600px) {
  .element-popup {
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    top: auto !important;
    width: 100%;
    border-radius: 16px 16px 0 0;
  }
}

/* ---- Table section chrome ------------------------------------------------- */
.table-section { margin-top: 0.5rem; }
.scroll-hint {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
  margin: 0.4rem 0 0;
}
@media (min-width: 800px) { .scroll-hint { display: none; } }

/* ---- Footer + ads --------------------------------------------------------- */
.site-footer {
  margin-top: 1.5rem;
  padding: 1.25rem 1rem 2rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.ad-container {
  max-width: 970px;
  margin: 0 auto 1rem;
  min-height: 90px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
}
.ad-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}
.ad-container .adsbygoogle { width: 100%; }
.footer-note {
  max-width: 720px;
  margin: 0 auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.78rem;
}

/* ---- Supporting content (About / How-to / FAQ on the home page) ----------- */
.info {
  margin: 2rem auto 0;
  padding: 0 0.5rem;
}
.info-inner {
  max-width: 760px;
  margin: 0 auto;
}
.info h2 {
  font-size: clamp(1.15rem, 2.6vw, 1.5rem);
  margin: 1.6rem 0 0.5rem;
}
.info h3 {
  font-size: 1.02rem;
  margin: 1.1rem 0 0.3rem;
}
.info p, .info li { color: var(--text); font-size: 0.95rem; }
.info ul { padding-left: 1.2rem; }
.info .faq h3 { color: var(--text); }
.info .faq p { color: var(--text-muted); }

/* ---- Footer navigation ---------------------------------------------------- */
.footer-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem 1.25rem;
  margin: 0 auto 0.75rem;
}
.footer-nav a {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
}
.footer-nav a:hover { text-decoration: underline; }
.footer-copy {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.75rem;
  margin: 0.5rem 0 0;
}

/* ---- Standalone content pages (about, privacy, 404) ----------------------- */
body.content .site-header { padding: 1rem; text-align: left; max-width: 820px; margin: 0 auto; }
.brand { margin: 0; font-weight: 800; font-size: 1.05rem; }
.brand a { color: var(--text); text-decoration: none; }
.content-page {
  max-width: 760px;
  margin: 0 auto;
  padding: 0.5rem 1rem 2rem;
}
.prose h1 { font-size: clamp(1.5rem, 4vw, 2rem); margin: 0.5rem 0 0.75rem; }
.prose h2 { font-size: 1.2rem; margin: 1.5rem 0 0.4rem; }
.prose p, .prose li { color: var(--text); line-height: 1.65; }
.prose a { color: var(--accent); }
.prose ul { padding-left: 1.25rem; }
.prose .updated { color: var(--text-muted); font-size: 0.85rem; margin-top: 0; }
.error-page { text-align: center; padding-top: 2rem; }
.error-page .download-btn { display: inline-block; text-decoration: none; margin-top: 0.5rem; }

/* ---- Cookie / consent banner ---------------------------------------------- */
.consent-banner {
  position: fixed;
  left: 50%;
  bottom: 1rem;
  transform: translateX(-50%);
  z-index: 60;
  width: min(680px, calc(100vw - 1.5rem));
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.consent-banner[hidden] { display: none; }
.consent-text { margin: 0; font-size: 0.85rem; flex: 1 1 260px; }
.consent-text a { color: var(--accent); }
.consent-actions { display: flex; gap: 0.5rem; flex: 0 0 auto; }
.consent-btn {
  padding: 0.45rem 1rem;
  font-size: 0.85rem;
  font-weight: 700;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid var(--border);
}
.consent-reject { background: transparent; color: var(--text); }
.consent-reject:hover { border-color: var(--accent); }
.consent-accept { background: var(--accent); color: #fff; border-color: var(--accent); }
.consent-accept:hover { filter: brightness(1.08); }
.consent-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Respect reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  .element, .filter-btn { transition: none; }
  .element:hover { transform: none; }
}

/* =============================================================================
   Element reference pages + study guides (added v2)
   ============================================================================= */

/* Breadcrumb trail */
.breadcrumbs {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin: 0 0 1rem;
}
.breadcrumbs a { color: var(--accent); text-decoration: none; }
.breadcrumbs a:hover { text-decoration: underline; }
.breadcrumbs .sep { margin: 0 0.35rem; }

/* Element page hero: big colored symbol badge + name */
.element-hero {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 0.5rem 0 1rem;
}
.element-hero .hero-badge {
  flex: 0 0 auto;
  width: 92px;
  height: 92px;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #16202b;
  line-height: 1.1;
}
.element-hero .hero-badge .hb-number { font-size: 0.85rem; font-weight: 600; }
.element-hero .hero-badge .hb-symbol { font-size: 2.1rem; font-weight: 800; }
.element-hero h1 { margin: 0; }
.element-hero .hero-sub { margin: 0.25rem 0 0; color: var(--text-muted); font-size: 0.95rem; }

/* Properties table */
.props-table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.75rem 0 1.25rem;
  font-size: 0.95rem;
}
.props-table th,
.props-table td {
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.props-table th {
  width: 40%;
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}
.props-table td { color: var(--text); }
.props-table .config { font-family: ui-monospace, Consolas, monospace; font-size: 0.9rem; }

/* Prev / next element navigation */
.prevnext {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin: 1.5rem 0 0.5rem;
  font-size: 0.92rem;
}
.prevnext a {
  color: var(--accent);
  text-decoration: none;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.prevnext a:hover { border-color: var(--accent); }
.prevnext .pn-spacer { flex: 1; }

/* "Full page" link inside the popup bubble */
.popup-more {
  display: block;
  margin: 0.6rem 0.9rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
}
.popup-more:hover { text-decoration: underline; }

/* Element index: category-grouped link columns */
.element-index-group { margin: 1.25rem 0; }
.element-index-group h2 { margin-bottom: 0.5rem; }
.element-index-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 0.25rem 1rem;
}
.element-index-list a { color: var(--accent); text-decoration: none; }
.element-index-list a:hover { text-decoration: underline; }
.element-index-list .ei-num { color: var(--text-muted); font-size: 0.85rem; margin-right: 0.35rem; }

/* Guide cards on the guides index */
.guide-list { list-style: none; padding: 0; margin: 1rem 0; }
.guide-list li {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem 1rem;
  margin-bottom: 0.75rem;
}
.guide-list a { font-weight: 700; color: var(--accent); text-decoration: none; }
.guide-list a:hover { text-decoration: underline; }
.guide-list p { margin: 0.35rem 0 0; color: var(--text-muted); font-size: 0.92rem; }
