/* ===== Theme variables ===== */
:root {
  --bg: #f6f7fb;
  --bg-panel: #ffffff;
  --bg-tile: #ffffff;
  --fg: #1c2333;
  --fg-dim: #5a6478;
  --border: #d8dce6;
  --tile-fg: #10151f;
  --shadow: 0 1px 3px rgba(20, 25, 40, .12);
  --hover: rgba(0, 0, 0, .08);
  --accent: #3b6ef5;
}
[data-theme="dark"] {
  --bg: #12141c;
  --bg-panel: #1a1d29;
  --bg-tile: #20242f;
  --fg: #e8ebf2;
  --fg-dim: #98a1b5;
  --border: #2c3140;
  --tile-fg: #f2f4f8;
  --shadow: 0 1px 3px rgba(0, 0, 0, .5);
  --hover: rgba(255, 255, 255, .10);
  --accent: #6f96ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--fg);
  transition: background .25s, color .25s;
}

/* ===== Top bar ===== */
.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: .75rem;
  align-items: center;
  justify-content: space-between;
  padding: .9rem 1.25rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar h1 { font-size: 1.15rem; margin: 0; letter-spacing: .02em; display: inline-flex; align-items: center; gap: .5rem; }
.topbar .logo { width: 1.6em; height: 1.6em; flex-shrink: 0; }
.topbar .logo .logo-spin { transform-origin: 24px 24px; animation: logo-spin 28s linear infinite; }
@keyframes logo-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .topbar .logo .logo-spin { animation: none; }
}
.controls { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.controls input[type="search"], .controls select {
  font: inherit;
  padding: .45rem .7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
}
.controls input[type="search"] { min-width: 220px; }
.controls button {
  font-size: 1.1rem;
  padding: .4rem .65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  cursor: pointer;
}
.controls button:hover { border-color: var(--accent); }

/* ===== Legend ===== */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: .35rem .8rem;
  padding: .6rem 1.25rem;
  font-size: .78rem;
  color: var(--fg-dim);
}
.legend button {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  border: none;
  background: none;
  cursor: pointer;
  color: inherit;
  font: inherit;
  padding: .15rem 0;
}
.legend button .swatch {
  width: .85em; height: .85em;
  border-radius: 3px;
  display: inline-block;
}
.legend button[aria-pressed="false"] { opacity: .35; }

/* ===== Category colors (swatch + tile tint) ===== */
.cat-alkali        { --cat: #e5484d; }
.cat-alkaline      { --cat: #f76b15; }
.cat-transition    { --cat: #f5b50a; }
.cat-post-transition { --cat: #8bd450; }
.cat-metalloid     { --cat: #3fb950; }
.cat-nonmetal      { --cat: #2d9cdb; }
.cat-halogen       { --cat: #6e56cf; }
.cat-noble         { --cat: #d656c4; }
.cat-lanthanide    { --cat: #e0719c; }
.cat-actinide      { --cat: #b8744a; }

/* ===== Layout ===== */
main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 1rem;
  padding: 1rem 1.25rem;
  align-items: start;
}
.table-wrap { overflow-x: auto; padding-bottom: .5rem; }
.intro { padding: .55rem 1.25rem 0; max-width: 72rem; }
.intro h2 { font-size: .82rem; font-weight: 600; margin: 0 0 .25rem; }
.intro p { margin: 0; color: var(--fg-dim); font-size: .76rem; line-height: 1.5; }

/* ===== Grid ===== */
.pt-grid {
  display: grid;
  grid-template-columns: repeat(18, minmax(44px, 1fr));
  grid-auto-rows: minmax(52px, auto);
  gap: 4px;
  min-width: 820px;
}
.tile {
  position: relative;
  border: 1px solid color-mix(in srgb, var(--cat) 55%, transparent);
  border-radius: 6px;
  background: color-mix(in srgb, var(--cat) 16%, var(--bg-tile));
  padding: .3rem .35rem;
  cursor: pointer;
  text-align: left;
  color: var(--tile-fg);
  font: inherit;
  transition: transform .12s, filter .12s, opacity .2s;
}
.tile:hover { transform: translateY(-2px); filter: brightness(1.08); box-shadow: var(--shadow); }
.tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tile .num { font-size: .6rem; color: var(--fg-dim); display: block; line-height: 1; }
.tile .sym { font-weight: 700; font-size: .95rem; display: block; line-height: 1.25; }
.tile .nm { font-size: .58rem; color: var(--fg-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.tile.sel { outline: 2px solid var(--accent); outline-offset: 1px; background: color-mix(in srgb, var(--cat) 38%, var(--bg-tile)); }
.tile.dimmed { opacity: .18; pointer-events: none; }
.tile.f-marker {
  background: color-mix(in srgb, var(--cat) 7%, transparent);
  border: 1.5px dashed color-mix(in srgb, var(--cat) 50%, transparent);
  color: color-mix(in srgb, var(--cat) 80%, var(--fg-dim));
  font-size: 1.05rem;
  font-weight: 600;
  display: flex; align-items: center; justify-content: center;
  cursor: default;
}
.tile.f-marker:hover { transform: none; filter: none; box-shadow: none; }
.f-connector {
  width: 2px;
  height: 85%;
  align-self: center;
  justify-self: center;
  background: repeating-linear-gradient(to bottom, var(--border) 0 5px, transparent 5px 10px);
  border-radius: 2px;
}

/* ===== Detail panel ===== */
.detail {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem 1.1rem;
  position: sticky;
  top: 64px;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
}
.detail-empty { color: var(--fg-dim); font-size: .9rem; }
.detail h2 { margin: 0 0 .1rem; font-size: 1.5rem; display: flex; align-items: baseline; gap: .5rem; }
.detail h2 .z { font-size: .85rem; color: var(--fg-dim); font-weight: 400; }
.detail .sub { color: var(--fg-dim); font-size: .85rem; margin-bottom: .8rem; }
.detail .badge {
  display: inline-block;
  font-size: .72rem;
  padding: .15rem .55rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--cat) 22%, transparent);
  color: var(--fg);
  margin-bottom: .8rem;
}
.el-photo { margin: 0 0 .9rem; }
.el-photo img {
  display: block;
  width: 100%;
  max-height: 230px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.el-photo .el-ph {
  height: 130px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1.5px dashed color-mix(in srgb, var(--cat) 50%, var(--border));
  border-radius: 8px;
  background: color-mix(in srgb, var(--cat) 8%, transparent);
  font-size: 2.6rem;
  font-weight: 700;
  color: color-mix(in srgb, var(--cat) 75%, var(--fg-dim));
}
.el-photo figcaption {
  font-size: .68rem;
  color: var(--fg-dim);
  margin-top: .35rem;
  line-height: 1.4;
}
.el-photo figcaption a { color: var(--accent); }
.dl { display: grid; grid-template-columns: auto 1fr; gap: .3rem .9rem; font-size: .84rem; margin: .4rem 0 .9rem; }
.dl dt { color: var(--fg-dim); }
.dl dd { margin: 0; font-variant-numeric: tabular-nums; }
.detail .fact {
  font-size: .88rem;
  line-height: 1.45;
  border-left: 3px solid var(--cat, var(--border));
  padding-left: .7rem;
  margin: 0;
}
.shells-row { display: flex; gap: .3rem; flex-wrap: wrap; margin: .5rem 0 .8rem; }
.shells-row span {
  font-size: .72rem;
  padding: .1rem .45rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  color: var(--fg-dim);
}

/* ===== Footer ===== */
.footer { padding: 1rem 1.25rem 1.5rem; color: var(--fg-dim); font-size: .78rem; }

/* ===== Detail close button ===== */
.detail-x {
  position: absolute; top: .55rem; right: .7rem;
  width: 1.9rem; height: 1.9rem; padding: 0;
  font-size: 1.25rem; line-height: 1;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--fg);
  cursor: pointer;
}
.detail-x:hover { border-color: var(--accent); }
.detail h2 { padding-right: 2.4rem; }

/* ===== Responsive: tablet & below ===== */
@media (max-width: 900px) {
  main { grid-template-columns: 1fr; }
}

/* Bottom-sheet detail on phone/tablet/folded widths */
@media (max-width: 899px) {
  .detail {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    width: 100%;
    max-height: 78vh;
    max-height: 78dvh;
    transform: translateY(105%);
    transition: transform .25s ease;
    border-radius: 14px 14px 0 0;
    box-shadow: 0 -10px 30px rgba(0, 0, 0, .3);
    z-index: 50;
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  }
  .detail.open { transform: translateY(0); }
  body.sheet-open { overflow: hidden; }
}

/* Tablet portrait / large phone */
@media (max-width: 700px) {
  .pt-grid {
    grid-template-columns: repeat(18, minmax(30px, 1fr));
    grid-auto-rows: minmax(38px, auto);
    gap: 3px;
    min-width: 620px;
  }
  .tile { padding: .18rem .22rem; border-radius: 5px; }
  .tile .num { font-size: .48rem; }
  .tile .sym { font-size: .78rem; }
  .tile .nm { font-size: .45rem; }
  .tile.f-marker { font-size: .8rem; }
}

/* Small phone / foldable in half */
@media (max-width: 430px) {
  .pt-grid {
    grid-template-columns: repeat(18, minmax(26px, 1fr));
    grid-auto-rows: minmax(34px, auto);
    min-width: 540px;
  }
  .tile .nm { display: none; }
  .tile .sym { font-size: .72rem; }
  .legend { font-size: .7rem; gap: .3rem .6rem; padding: .5rem .8rem; }
}

/* Top bar on narrow screens */
@media (max-width: 640px) {
  .topbar { padding: .65rem .8rem; gap: .5rem; }
  .topbar h1 { width: 100%; font-size: 1rem; }
  .controls { width: 100%; }
  .controls input[type="search"] { min-width: 0; flex: 1; }
  .controls select { flex: 1; min-width: 0; }
  .intro { padding: .5rem .8rem 0; }
}

/* Touch devices */
@media (hover: none) {
  .tile { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
}
