:root {
  --bg-color: #f8fafb;
  --panel-bg: #ffffff;
  --card-bg: #ffffff;
  --text-main: #2d3436;
  --text-dim: #636e72;
  --border-color: #eef2f5;
  
  /* Academic Category Palette */
  --clr-alkali: #ff7f50;
  --clr-alkaline: #f9c74f;
  --clr-transition: #4361ee;
  --clr-post-transition: #4d908e;
  --clr-metalloid: #90be6d;
  --clr-reactive: #20b2aa;
  --clr-noble: #f94144;
  --clr-lanthanide: #da70d6;
  --clr-actinide: #7b2a2a;
  
  --shadow-soft: 0 4px 20px rgba(0,0,0,0.05);
  --shadow-strong: 0 10px 30px rgba(0,0,0,0.12);
}

body {
  font-family: 'Outfit', sans-serif;
  background-color: var(--bg-color);
  margin: 0;
  padding: 0;
  color: var(--text-main);
  min-height: 100vh;
}

#table-container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 40px 20px;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

header {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo {
  font-weight: 800;
  letter-spacing: 4px;
  font-size: 1.8rem;
  color: var(--text-main);
}

nav a {
  text-decoration: none;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 0.9rem;
  margin-left: 20px;
  transition: color 0.2s;
}

nav a:hover {
  color: var(--text-main);
}

/* Controls Box */
.controls-wrapper {
  background: var(--panel-bg);
  padding: 25px;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  box-shadow: var(--shadow-soft);
  border: 1px solid var(--border-color);
}

.search-box {
  position: relative;
  max-width: 400px;
}

#element-search {
  width: 100%;
  background: #fdfdfd;
  border: 1px solid #ddd;
  padding: 12px 40px 12px 15px;
  border-radius: 10px;
  color: var(--text-main);
  font-family: inherit;
  font-size: 1rem;
}

.search-icon {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0.3;
}

.filter-controls {
  display: flex;
  align-items: center;
  gap: 25px;
  flex-wrap: wrap;
}

#category-filter {
  background: #fff;
  color: var(--text-main);
  border: 1px solid #ddd;
  padding: 10px 15px;
  border-radius: 10px;
  font-family: inherit;
  cursor: pointer;
  min-width: 210px;
}

/* Legend Pills */
.legend-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.legend-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 12px;
  transition: all 0.2s;
  background: #f1f3f5;
  border: 1px solid transparent;
}

.legend-pill:hover, .legend-pill.active {
  color: var(--text-main);
  background: #fff;
  border-color: #ddd;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.dot.alkali { background: var(--clr-alkali); }
.dot.alkaline { background: var(--clr-alkaline); }
.dot.transition { background: var(--clr-transition); }
.dot.post-transition { background: var(--clr-post-transition); }
.dot.metalloid { background: var(--clr-metalloid); }
.dot.reactive { background: var(--clr-reactive); }
.dot.noble { background: var(--clr-noble); }
.dot.lanthanide { background: var(--clr-lanthanide); }
.dot.actinide { background: var(--clr-actinide); }

/* The Grid */
#periodic-grid-container {
  overflow-x: auto;
  padding: 10px 2px 20px 2px;
  -webkit-overflow-scrolling: touch; /* Smooth iOS scroll */
}

@media (max-width: 600px) {
  #table-container {
    padding: 20px 10px;
    gap: 15px;
  }

  .header-top {
    flex-direction: column;
    gap: 10px;
    text-align: center;
  }

  nav {
    display: flex;
    justify-content: center;
    width: 100%;
  }

  nav a {
    margin: 0 10px;
  }

  .controls-wrapper {
    padding: 15px;
  }

  .search-box {
    max-width: 100%;
  }

  .filter-controls {
    gap: 15px;
  }

  #category-filter {
    width: 100%;
  }

  .legend-pills {
    justify-content: center;
  }

  .element-tile {
    min-height: 70px;
    padding: 4px;
  }

  .tile-symbol {
    font-size: 1.1rem;
  }

  /* Educational Insight on Mobile */
  #category-insight {
    bottom: 20px;
    right: 10px;
    left: 10px;
    width: auto;
    max-width: none;
  }

  /* Detail Card Mobile */
  .card {
    padding: 25px;
    border-radius: 20px;
  }

  .detail-header {
    flex-direction: column;
    gap: 15px;
    text-align: center;
    margin-bottom: 25px;
  }

  #detail-symbol {
    font-size: 4.5rem;
  }

  #detail-name {
    font-size: 1.8rem;
  }

  .detail-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}

#periodic-grid {
  display: grid;
  grid-template-columns: repeat(18, 1fr);
  grid-template-rows: repeat(10, 1fr);
  gap: 4px;
  min-width: 1300px;
}

.element-tile {
  background: #fff;
  border: 1px solid #e0e0e0;
  padding: 6px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 85px;
  position: relative;
  box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.element-tile::after {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 5px;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}

.element-tile.dimmed {
  opacity: 0.15;
  filter: grayscale(1);
}

.element-tile:hover {
  transform: translateY(-4px) scale(1.1);
  box-shadow: 0 12px 24px rgba(0,0,0,0.1);
  z-index: 100;
  border-color: #bbb;
}

.tile-number {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-dim);
}

.tile-symbol {
  font-size: 1.5rem;
  font-weight: 800;
  text-align: center;
  color: var(--text-main);
}

.tile-name {
  font-size: 10px;
  text-align: center;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-main);
}

.tile-mass {
  font-size: 8px;
  text-align: center;
  color: var(--text-dim);
}

/* Category Accent Borders */
.alkali-metal::after { background: var(--clr-alkali); }
.alkaline-earth-metal::after { background: var(--clr-alkaline); }
.transition-metal::after { background: var(--clr-transition); }
.post-transition-metal::after { background: var(--clr-post-transition); }
.metalloid::after { background: var(--clr-metalloid); }
.reactive-nonmetal::after { background: var(--clr-reactive); }
.noble-gas::after { background: var(--clr-noble); }
.lanthanide::after { background: var(--clr-lanthanide); }
.actinide::after { background: var(--clr-actinide); }

/* Lanthanide/Actinine separation spacing */
.element-tile[style*="grid-row: 9"] { margin-top: 30px; }

/* Detail Overlay */
#element-detail {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255,255,255,0.7);
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 1;
  transition: opacity 0.3s ease;
}

#element-detail.hidden {
  display: none;
  opacity: 0;
}

.card {
  background: var(--card-bg);
  width: 90%;
  max-width: 600px;
  padding: 40px;
  border-radius: 30px;
  position: relative;
  box-shadow: 0 30px 60px rgba(0,0,0,0.15);
  border: 1px solid var(--border-color);
}

#close-detail {
  position: absolute;
  top: 25px;
  right: 25px;
  background: #f1f3f5;
  color: var(--text-main);
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

#close-detail:hover {
  background: #e9ecef;
}

.detail-header {
  display: flex;
  align-items: center;
  gap: 30px;
  margin-bottom: 35px;
}

#detail-symbol {
  font-family: 'Playfair Display', serif;
  font-size: 6rem;
  font-weight: 800;
  line-height: 1;
  color: var(--text-main);
}

#detail-name {
  font-size: 2.5rem;
  margin: 0;
  font-weight: 800;
}

#detail-category {
  text-transform: uppercase;
  font-weight: 800;
  letter-spacing: 2px;
  font-size: 0.9rem;
  color: var(--text-dim);
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 35px;
}

.detail-item {
  background: #f8fafc;
  padding: 15px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid #edf2f7;
}

.detail-item strong {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-dim);
  letter-spacing: 1px;
}

.detail-item span {
  font-size: 1.2rem;
  font-weight: 600;
}

.detail-item.full {
  grid-column: span 2;
}

.summary {
  font-size: 1rem;
  line-height: 1.7;
  color: #4a4a4a;
  margin-bottom: 30px;
}

#detail-wiki {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #2d3436;
  color: #fff;
  text-decoration: none;
  font-weight: 700;
  font-size: 0.95rem;
  padding: 12px 25px;
  border-radius: 10px;
  transition: transform 0.2s, background 0.2s;
}

#detail-wiki:hover {
  transform: translateY(-2px);
  background: #000;
}

/* Category Insight Floating Panel */
#category-insight {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 350px;
  z-index: 500;
  transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#category-insight.hidden {
  transform: translateY(120%) scale(0.8);
  opacity: 0;
  pointer-events: none;
}

.insight-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  padding: 25px;
  border-radius: 20px;
  box-shadow: 0 15px 45px rgba(0,0,0,0.12);
  border: 1px solid #eee;
  position: relative;
}

#close-insight {
  position: absolute;
  top: 15px;
  right: 15px;
  background: #f1f3f5;
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: var(--text-dim);
}

.insight-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.insight-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

#insight-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--text-main);
  text-transform: uppercase;
  letter-spacing: 1px;
}

#insight-text {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.6;
  color: #4a4a4a;
}
