/* ===================================================================
   Periodic Table Explorer — Tomato + cream design system.
   =================================================================== */

:root {
  --bg:           #faf6f1;
  --bg-2:         #f3eee5;
  --surface:      #ffffff;
  --surface-2:    #faf6ed;
  --surface-3:    #eee7d8;
  --border:       #e8dfcc;
  --border-soft:  #f0e9da;
  --text:         #1f1a14;
  --text-2:       #5c5547;
  --text-3:       #6e6557;
  --text-faint:   #b5ac9b;

  --accent:       #e85f4d;
  --accent-2:     #d44a39;
  --accent-3:     #b8392b;
  --accent-soft:  rgba(232, 95, 77, 0.10);
  --accent-glow:  rgba(232, 95, 77, 0.30);

  --tomato-from:  #ff6b4a;
  --tomato-to:    #e85f4d;
  --sage:         #5eb985;
  --sage-soft:    rgba(94, 185, 133, 0.14);

  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 20px;
  --r-pill: 999px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.06);
  --shadow-lg: 0 24px 48px rgba(0,0,0,0.12);

  --font-ui: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI",
             "PingFang TC", "PingFang SC", "Hiragino Sans", "Noto Sans CJK",
             sans-serif;
  --font-mono: "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --transition: 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
html, body {
  font-family: var(--font-ui);
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%, rgba(255, 235, 220, 0.55) 0%, transparent 70%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select, textarea { font-family: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; transition: color var(--transition); }
a:hover { color: var(--accent-2); }
ul, ol { list-style: none; }
[hidden] { display: none !important; }

.skip-link {
  position: absolute;
  left: -9999px; top: 0;
  background: var(--accent);
  color: #fff;
  padding: 10px 16px;
  font-weight: 600;
  border-radius: 0 0 var(--r-sm) 0;
  z-index: 1000;
}
.skip-link:focus { left: 0; }

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: clamp(12px, 3vw, 24px) clamp(12px, 3vw, 24px) 80px;
}

/* ============ Top bar ============ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0 28px;
}
.brand {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
  color: var(--text-2);
}
.brand:hover { color: var(--accent); }
.topbar-nav { display: flex; align-items: center; gap: 8px; }

.nav-link {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-3);
  padding: 6px 10px;
  border-radius: var(--r-sm);
  white-space: nowrap;
  transition: background var(--transition), color var(--transition);
}
.nav-link:hover { color: var(--accent); background: var(--surface-2); }

.lang-switcher {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 6px 28px 6px 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 12 12' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M3 5l3 3 3-3' stroke='%237a7973' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 12px;
}
.lang-switcher:hover { border-color: var(--text-3); }
.lang-switcher:focus { outline: 2px solid var(--accent); outline-offset: 2px; border-color: var(--accent); }

.page-title {
  font-size: clamp(24px, 4vw, 32px);
  font-weight: 700;
  letter-spacing: -0.02em;
  text-align: center;
  margin-bottom: 6px;
}
.page-sub {
  text-align: center;
  font-size: 14px;
  color: var(--text-3);
  margin-bottom: 20px;
}

/* ============ Search ============ */
.controls {
  display: flex;
  justify-content: center;
  margin-bottom: 24px;
}
.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 400px;
}
.search-icon {
  position: absolute;
  left: 12px;
  color: var(--text-3);
  pointer-events: none;
}
.search-input {
  width: 100%;
  padding: 10px 40px 10px 36px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  font-size: 14px;
  color: var(--text);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.search-input::placeholder { color: var(--text-faint); }
.search-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.search-clear {
  position: absolute;
  right: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  color: var(--text-3);
  transition: background var(--transition), color var(--transition);
}
.search-clear:hover { color: var(--accent); background: var(--accent-soft); }
.search-clear[hidden] { display: none; }

/* ============ Periodic table grid wrapper ============ */
.pt-grid-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
  margin-bottom: 28px;
  scrollbar-width: thin;
}
.pt-grid-wrap::-webkit-scrollbar { height: 6px; }
.pt-grid-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.pt-grid {
  display: grid;
  grid-template-columns: repeat(18, 1fr);
  grid-template-rows: repeat(7, 1fr);
  gap: 3px;
  min-width: 756px;
  max-width: 1100px;
  margin: 0 auto;
}

/* ============ Element cell ============ */
.el-cell {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 2px 3px;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow var(--transition), border-color var(--transition);
  min-width: 44px;
  min-height: 44px;
  user-select: none;
}
.el-cell:hover {
  transform: translateY(-2px) scale(1.04);
  z-index: 2;
  box-shadow: 0 6px 16px rgba(0,0,0,0.12);
  border-color: var(--accent);
}
.el-cell:active { transform: scale(0.96); }
.el-cell.is-matched {}

.el-cell .el-number {
  font-size: 9px;
  font-weight: 600;
  color: var(--text-3);
  line-height: 1;
  letter-spacing: 0.02em;
}
.el-cell .el-symbol {
  font-size: clamp(12px, 1.5vw, 18px);
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
  margin: 1px 0;
}
.el-cell .el-name {
  font-size: 8px;
  color: var(--text-3);
  line-height: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* Category color stripes */
.el-cell[data-category="alkali-metal"] { border-top: 3px solid #3b82f6; }
.el-cell[data-category="alkaline-earth-metal"] { border-top: 3px solid #f59e0b; }
.el-cell[data-category="transition-metal"] { border-top: 3px solid #ef4444; }
.el-cell[data-category="post-transition-metal"] { border-top: 3px solid #8b5cf6; }
.el-cell[data-category="metalloid"] { border-top: 3px solid #06b6d4; }
.el-cell[data-category="nonmetal"] { border-top: 3px solid #10b981; }
.el-cell[data-category="halogen"] { border-top: 3px solid #6366f1; }
.el-cell[data-category="noble-gas"] { border-top: 3px solid #ec4899; }
.el-cell[data-category="lanthanide"] { border-top: 3px solid #14b8a6; }
.el-cell[data-category="actinide"] { border-top: 3px solid #f97316; }

/* Empty placeholder cells */
.el-cell.is-empty {
  background: transparent;
  border: none;
  cursor: default;
  pointer-events: none;
  min-height: 52px;
}

/* Search highlight / dim */
.el-cell.is-dimmed {
  opacity: 0.25;
  pointer-events: none;
}
.el-cell.is-highlight {
  box-shadow: 0 0 0 3px var(--accent-glow), 0 4px 12px rgba(0,0,0,0.10);
  border-color: var(--accent);
  z-index: 3;
}

/* ============ F-block (lanthanides + actinides) ============ */
.f-block-rows {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-top: 8px;
}
.f-row {
  display: flex;
  align-items: center;
  gap: 4px;
}
.f-row-label {
  flex-shrink: 0;
  width: 48px;
  text-align: right;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-3);
  padding-right: 6px;
}
.f-row-grid {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  gap: 3px;
  flex: 1;
  max-width: 920px;
}

/* ============ Legend ============ */
.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 16px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  margin-bottom: 36px;
  max-width: 1100px;
  margin-left: auto;
  margin-right: auto;
}
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-2);
}
.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--cat-color);
  flex-shrink: 0;
}

/* ============ Overlay & Detail Panel ============ */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(31, 26, 20, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  padding: 20px;
  animation: fadeIn 180ms ease-out;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.overlay[hidden] { display: none; }

.detail-panel {
  position: relative;
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  max-width: 420px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: 32px 28px 28px;
  animation: slideUp 240ms ease-out;
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.detail-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  color: var(--text-3);
  transition: background var(--transition), color var(--transition);
}
.detail-close:hover { background: var(--accent-soft); color: var(--accent); }

.detail-header {
  text-align: center;
  margin-bottom: 20px;
}
.detail-header .big-symbol {
  display: block;
  font-size: 56px;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.03em;
  color: var(--text);
}
.detail-header .big-number {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-3);
  margin-bottom: 2px;
}
.detail-header .big-name {
  display: block;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-2);
  margin-top: 4px;
}

.detail-props {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 16px;
  margin-bottom: 16px;
}
.detail-props dt {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-3);
}
.detail-props dd {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}

.detail-desc {
  font-size: 14px;
  color: var(--text-2);
  line-height: 1.6;
  text-align: center;
}

/* ============ Info section ============ */
.info {
  max-width: 760px;
  margin: 0 auto;
}
.info h2 {
  font-size: clamp(18px, 3vw, 22px);
  font-weight: 600;
  letter-spacing: -0.015em;
  margin: 24px 0 8px;
  color: var(--text);
}
.info h2:first-child { margin-top: 0; }
.info p {
  font-size: 15px;
  color: var(--text-2);
  line-height: 1.65;
  margin-bottom: 10px;
}
.info ul.bullet {
  margin: 8px 0 12px 18px;
}
.info ul.bullet li {
  list-style: disc;
  padding-left: 4px;
  margin-bottom: 6px;
  font-size: 15px;
  color: var(--text-2);
  line-height: 1.65;
}
.info strong { color: var(--text); font-weight: 600; }

/* ============ Footer ============ */
.site-footer {
  margin-top: 56px;
  padding-top: 24px;
  border-top: 1px solid var(--border-soft);
  text-align: center;
}
.site-footer-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 16px;
  font-size: 13px;
  color: var(--text-3);
}
.site-footer a { color: var(--text-2); }
.site-footer a:hover { color: var(--accent); }
.site-footer .sub {
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-3);
}

/* ============ Responsive ============ */
@media (max-width: 820px) {
  .pt-grid {
    grid-template-columns: repeat(18, 1fr);
    min-width: 720px;
  }
  .el-cell { min-height: 46px; padding: 3px 1px 2px; }
  .el-cell .el-symbol { font-size: 13px; }
  .el-cell .el-number { font-size: 8px; }
  .el-cell .el-name { font-size: 7px; }
}
@media (max-width: 720px) {
  .controls { padding: 0 4px; }
  /* TODO: optional list-view fallback for the grid on very narrow phones */
}
@media (max-width: 480px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  .detail-panel { padding: 24px 20px 22px; }
  .detail-header .big-symbol { font-size: 42px; }
  .detail-props { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ============ Focus visible ============ */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:focus:not(:focus-visible),
select:focus:not(:focus-visible),
input:focus:not(:focus-visible),
a:focus:not(:focus-visible) { outline: none; }
