@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap');

:root {
  --bg-color: #0a0e1a;
  --text-color: #e8eaf6;
  --title-color: #8c9eff;
  --cell-border: rgba(255,255,255,0.1);
  --cell-hover-border: rgba(255,255,255,0.6);
  --cell-hover-shadow: rgba(255,255,255,0.2);
  --modal-overlay: rgba(0,0,0,0.85);
  --panel-bg: #0f1427;
  --panel-border: rgba(255,255,255,0.2);
  --scrollbar-thumb: #4f5b93;
  --scrollbar-thumb-hover: #8c9eff;
  --btn-bg: rgba(255,255,255,0.1);
  --btn-hover: rgba(255,255,255,0.25);
  --tab-inactive: rgba(255,255,255,0.55);
  --tab-active: #fff;
  --tab-border: #8c9eff;
  --info-text: rgba(255,255,255,0.85);
  --prop-bg: rgba(255,255,255,0.05);
  --header-border: rgba(255,255,255,0.1);
  --help-title: #a0b0ff;
  --footer-border: rgba(255,255,255,0.1);
  --footer-link: #a0b0ff;
  --footer-link-hover: #ffffff;
}

[data-theme="light"] {
  --bg-color: #f0f2f5;
  --text-color: #1a1a1a;
  --title-color: #1a237e;
  --cell-border: rgba(0,0,0,0.15);
  --cell-hover-border: rgba(0,0,0,0.5);
  --cell-hover-shadow: rgba(0,0,0,0.15);
  --modal-overlay: rgba(255,255,255,0.85);
  --panel-bg: #ffffff;
  --panel-border: rgba(0,0,0,0.2);
  --scrollbar-thumb: #9fa8da;
  --scrollbar-thumb-hover: #5c6bc0;
  --btn-bg: rgba(0,0,0,0.05);
  --btn-hover: rgba(0,0,0,0.15);
  --tab-inactive: rgba(0,0,0,0.6);
  --tab-active: #000;
  --tab-border: #1a237e;
  --info-text: #333333;
  --prop-bg: rgba(0,0,0,0.05);
  --header-border: rgba(0,0,0,0.1);
  --help-title: #283593;
  --footer-border: rgba(0,0,0,0.1);
  --footer-link: #283593;
  --footer-link-hover: #000000;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Inter', 'Arial', sans-serif;
  background: var(--bg-color);
  color: var(--text-color);
  min-height: 100vh;
  line-height: 1.4;
  transition: background 0.3s, color 0.3s;
}

#controls {
  position: fixed;
  top: 12px;
  right: 12px;
  display: flex;
  gap: 8px;
  z-index: 100;
}

#controls button {
  background: var(--btn-bg);
  border: 1px solid var(--panel-border);
  color: var(--text-color);
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-family: inherit;
  font-size: 13px;
  transition: background 0.2s;
  backdrop-filter: blur(8px);
}
#controls button:hover { background: var(--btn-hover); }

#pt-root {
  padding: 52px 10px 20px;
  max-width: 1400px;
  margin: 0 auto;
}

#pt-title {
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--title-color);
  margin-bottom: 4px;
  text-align: center;
}

#pt-subtitle {
  font-size: 12px;
  font-weight: 400;
  color: var(--tab-inactive);
  text-align: center;
  margin-bottom: 16px;
  letter-spacing: 0.02em;
}

/* ---- GRID ---- */
#pt-grid {
  display: grid;
  grid-template-columns: repeat(18, 1fr);
  gap: 3px;
  width: 100%;
  overflow-x: auto;
  padding-bottom: 6px;
}

#pt-grid::-webkit-scrollbar { height: 6px; }
#pt-grid::-webkit-scrollbar-track { background: transparent; }
#pt-grid::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }

.el-cell {
  aspect-ratio: 1 / 1;
  border-radius: 5px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--cell-border);
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
  position: relative;
  min-width: 0;
  padding: 2px 1px;
  font-weight: bold;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.el-cell:hover {
  transform: scale(1.18);
  z-index: 5;
  border-color: var(--cell-hover-border);
  box-shadow: 0 0 14px var(--cell-hover-shadow);
}

.el-num { font-size: 8px; font-weight: 400; opacity: 0.8; line-height: 1.1; }
.el-sym { font-size: 14px; font-weight: 700; line-height: 1.15; }
.el-name { font-size: 7px; font-weight: 400; opacity: 0.85; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; padding: 0 1px; }
.el-spacer { min-height: 0; }

/* Lanthanide/Actinide label cells */
.la-label {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  opacity: 0.6;
  letter-spacing: 0.05em;
  text-align: center;
  line-height: 1.1;
}

/* Category colors */
.cat-alkali     { background: #b71c1c; color: #ffcdd2; }
.cat-alkaline   { background: #e65100; color: #ffe0b2; }
.cat-transition { background: #1a237e; color: #c5cae9; }
.cat-post       { background: #1b5e20; color: #c8e6c9; }
.cat-metalloid  { background: #4a148c; color: #e1bee7; }
.cat-nonmetal   { background: #006064; color: #b2ebf2; }
.cat-halogen    { background: #01579b; color: #b3e5fc; }
.cat-noble      { background: #311b92; color: #d1c4e9; }
.cat-lanthanide { background: #33691e; color: #dcedc8; }
.cat-actinide   { background: #bf360c; color: #ffccbc; }

/* ---- MODAIS ---- */
#el-modal, #help-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  backdrop-filter: blur(5px);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
#el-modal.open, #help-modal.open { display: flex; }

#el-panel, .help-panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  width: min(620px, 96vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  position: relative;
  animation: panelIn 0.25s ease-out;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.4);
  overflow: hidden;
}

@keyframes panelIn {
  from { opacity: 0; transform: scale(0.95) translateY(20px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}

#el-header {
  padding: 20px 20px 14px;
  display: flex;
  align-items: center;
  gap: 20px;
  border-bottom: 1px solid var(--header-border);
  flex-wrap: wrap;
  flex-shrink: 0;
}

#el-symbol-big {
  width: 82px;
  height: 82px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: rgba(0,0,0,0.15);
  box-shadow: inset 0 0 10px rgba(0,0,0,0.1);
}
[data-theme="light"] #el-symbol-big {
  box-shadow: inset 0 0 10px rgba(255,255,255,0.3);
}

#el-symbol-big .big-num { font-size: 13px; opacity: 0.85; }
#el-symbol-big .big-sym { font-size: 36px; font-weight: 800; line-height: 1; }
#el-symbol-big .big-mass { font-size: 11px; font-family: 'Inter', monospace; opacity: 0.85; }

#el-meta { flex: 1; min-width: 160px; }
#el-name-big { font-size: 24px; font-weight: 800; margin-bottom: 6px; }

#el-cat-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  padding: 5px 12px;
  border-radius: 4px;
  margin-bottom: 12px;
  background: var(--prop-bg);
}

#el-props {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 8px;
}
.prop-item { background: var(--prop-bg); border-radius: 8px; padding: 6px 10px; }
.prop-label { font-size: 10px; text-transform: uppercase; opacity: 0.7; margin-bottom: 3px; }
.prop-val { font-size: 13px; font-weight: 700; }

#el-tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--header-border);
  padding: 0 16px;
  flex-wrap: wrap;
  justify-content: center;
  overflow-x: hidden;
  flex-shrink: 0;
}
/* No scrollbar needed for tabs since they wrap */

.tab-btn {
  background: none;
  border: none;
  color: var(--tab-inactive);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all 0.2s;
  white-space: nowrap;
}
.tab-btn:hover { color: var(--text-color); }
.tab-btn.active { color: var(--tab-active); border-bottom-color: var(--tab-border); }

#el-content {
  padding: 20px;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1;
}
#el-content::-webkit-scrollbar { width: 6px; }
#el-content::-webkit-scrollbar-track { background: transparent; }
#el-content::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }

.tab-panel { display: none; animation: fade 0.3s ease; }
.tab-panel.active { display: block; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.info-block { font-size: 14px; line-height: 1.6; color: var(--info-text); word-wrap: break-word; overflow-wrap: break-word; }
.info-block h4 { font-size: 13px; text-transform: uppercase; color: var(--help-title); margin: 14px 0 8px; }

.anim-wrapper { display: flex; justify-content: center; margin: 8px 0 16px; }
.atom-svg { width: 150px; height: 150px; }
.nucleus { animation: pulse 2.2s ease-in-out infinite; }
@keyframes pulse { 0%,100% { r: 14px; opacity: 1; } 50% { r: 16px; opacity: 0.8; } }
.orbit1 { animation: spin1 3s linear infinite; transform-origin: 80px 80px; }
.orbit2 { animation: spin2 4.8s linear infinite; transform-origin: 80px 80px; }
.orbit3 { animation: spin3 6.5s linear infinite; transform-origin: 80px 80px; }
@keyframes spin1 { to { transform: rotate(360deg); } }
@keyframes spin2 { to { transform: rotate(-360deg); } }
@keyframes spin3 { to { transform: rotate(360deg); } }

/* ---- LEGEND ---- */
#legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 20px;
  justify-content: center;
}
.leg-item { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.leg-dot { width: 12px; height: 12px; border-radius: 50%; }

/* ---- RISK ---- */
.risk-badge {
  display: inline-block;
  padding: 5px 14px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 700;
  margin: 5px 0 10px;
}
.risk-low    { background: #1b5e20; color: #c8e6c9; }
.risk-medium { background: #e65100; color: #ffe0b2; }
.risk-high   { background: #b71c1c; color: #ffcdd2; }

/* ---- CLOSE BUTTONS ---- */
#el-close, #help-close {
  position: absolute;
  top: 14px; right: 14px;
  background: var(--btn-bg);
  border: none;
  color: var(--text-color);
  width: 34px; height: 34px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
  z-index: 10;
}
#el-close:hover, #help-close:hover { background: var(--btn-hover); }

/* ---- HELP ---- */
.help-panel { padding: 28px; }
.help-panel h2 { color: var(--title-color); margin-bottom: 18px; border-bottom: 1px solid var(--header-border); padding-bottom: 10px; font-size: 20px; }
.help-content { overflow-y: auto; flex: 1; font-size: 14px; color: var(--info-text); line-height: 1.6; }
.help-content p { margin-bottom: 12px; }
.help-content ul { margin-left: 20px; margin-bottom: 14px; }
.help-content li { margin-bottom: 6px; }
.help-content::-webkit-scrollbar { width: 6px; }
.help-content::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }

/* ---- GROUP MODAL ---- */
#group-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--modal-overlay);
  backdrop-filter: blur(5px);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
#group-modal.open { display: flex; }

#group-panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  width: min(660px, 96vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  position: relative;
  animation: panelIn 0.25s ease-out;
  box-shadow: 0 25px 50px -12px rgba(0,0,0,0.4);
  overflow: hidden;
}

#group-header {
  padding: 20px 20px 16px;
  display: flex;
  align-items: center;
  gap: 18px;
  border-bottom: 1px solid var(--header-border);
  flex-shrink: 0;
}

#group-icon {
  width: 72px;
  height: 72px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  flex-shrink: 0;
}

#group-name {
  font-size: 22px;
  font-weight: 800;
  margin-bottom: 8px;
}

#group-range-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 12px;
  border-radius: 4px;
}

#group-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
#group-body::-webkit-scrollbar { width: 6px; }
#group-body::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }

#group-props-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

#group-el-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.group-el-chip {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid var(--cell-border);
  transition: transform 0.12s, border-color 0.12s;
  font-size: 13px;
  font-weight: 700;
}
.group-el-chip:hover {
  transform: scale(1.08);
  border-color: var(--cell-hover-border);
}
.group-el-chip .chip-num { font-size: 10px; font-weight: 400; opacity: 0.75; }
.group-el-chip .chip-sym { font-size: 15px; font-weight: 800; }
.group-el-chip .chip-name { font-size: 11px; opacity: 0.85; }

#group-close {
  position: absolute;
  top: 14px; right: 14px;
  background: var(--btn-bg);
  border: none;
  color: var(--text-color);
  width: 34px; height: 34px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
  z-index: 10;
}
#group-close:hover { background: var(--btn-hover); }

/* ---- FOOTER ---- */
.footer {
  margin-top: 40px;
  padding: 28px 16px 20px;
  border-top: 1px solid var(--footer-border);
  text-align: center;
  font-size: 13px;
  background: var(--bg-color);
  transition: background 0.3s;
}

.footer-links {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 24px;
  margin-bottom: 18px;
  padding: 0;
}

.footer-links li a {
  color: var(--footer-link);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s, opacity 0.2s;
}

.footer-links li a:hover {
  color: var(--footer-link-hover);
  text-decoration: underline;
}

.footer-copy {
  font-size: 12px;
  opacity: 0.7;
  margin: 0;
}

.footer-copy a {
  color: var(--footer-link);
  text-decoration: none;
  font-weight: 500;
}

.footer-copy a:hover {
  color: var(--footer-link-hover);
  text-decoration: underline;
}

/* ---- RESPONSIVE: Tablet ---- */
@media (max-width: 900px) {
  #pt-grid { grid-template-columns: repeat(18, minmax(32px, 1fr)); gap: 2px; }
  .el-num { font-size: 6px; }
  .el-sym { font-size: 11px; }
  .el-name { font-size: 5.5px; }
  .la-label { font-size: 7px; }
}

/* ---- RESPONSIVE: Modal on smaller tablets ---- */
@media (max-width: 780px) {
  #el-header { flex-direction: column; align-items: center; text-align: center; padding: 16px 16px 12px; }
  #el-meta { min-width: 100%; }
  #el-props { justify-content: center; }
  #el-name-big { font-size: 20px; }
  .tab-btn { font-size: 12px; padding: 8px 10px; }
  #el-content { padding: 16px; }
  .footer {
    margin-top: 24px;
    padding: 20px 12px 16px;
  }
  .footer-links { gap: 18px; }
}

/* ---- RESPONSIVE: Phone landscape / small tablet ---- */
@media (max-width: 600px) {
  #pt-root { padding: 48px 6px 14px; }
  #pt-grid { grid-template-columns: repeat(18, minmax(18px, 1fr)); gap: 1px; }
  .el-cell { border-radius: 3px; padding: 1px; }
  .el-cell:hover { transform: scale(1.35); }
  .el-num { font-size: 5px; }
  .el-sym { font-size: 9px; }
  .el-name { display: none; }
  .la-label { font-size: 5px; }
  #pt-title { font-size: 11px; margin-bottom: 2px; }
  #pt-subtitle { font-size: 9px; margin-bottom: 8px; }
  #controls button { padding: 5px 8px; font-size: 11px; }
  #el-panel, .help-panel { width: 98vw; max-height: 94vh; border-radius: 12px; }
  #el-symbol-big { width: 64px; height: 64px; }
  #el-symbol-big .big-sym { font-size: 28px; }
  #el-props { grid-template-columns: repeat(2, 1fr); }
  #group-panel { width: 98vw; max-height: 92vh; border-radius: 12px; }
  #group-icon { width: 56px; height: 56px; font-size: 24px; }
  #group-name { font-size: 18px; }
  #legend { gap: 8px; margin-top: 12px; }
  .leg-item { font-size: 10px; }
  .leg-dot { width: 10px; height: 10px; }
  .footer {
    margin-top: 20px;
    padding: 16px 12px 14px;
  }
  .footer-links { gap: 14px; margin-bottom: 12px; }
  .footer-links li a { font-size: 12px; }
  .footer-copy { font-size: 10px; }
}

/* ---- RESPONSIVE: Very small phones (iPhone SE, Galaxy S, etc.) ---- */
@media (max-width: 400px) {
  #pt-root { padding: 44px 3px 10px; }
  #pt-grid { grid-template-columns: repeat(18, minmax(15px, 1fr)); gap: 1px; }
  .el-cell { border-radius: 2px; }
  .el-num { font-size: 4px; }
  .el-sym { font-size: 7px; }
  .la-label { font-size: 4px; }
  #pt-title { font-size: 10px; letter-spacing: 0.06em; }
  #pt-subtitle { font-size: 8px; }
  #controls { top: 6px; right: 6px; gap: 4px; }
  #controls button { padding: 4px 6px; font-size: 10px; border-radius: 6px; }
  #el-panel, .help-panel { width: 100vw; max-height: 96vh; border-radius: 8px; }
  #el-header { padding: 12px 12px 10px; gap: 12px; }
  #el-symbol-big { width: 56px; height: 56px; border-radius: 8px; }
  #el-symbol-big .big-num { font-size: 10px; }
  #el-symbol-big .big-sym { font-size: 22px; }
  #el-symbol-big .big-mass { font-size: 9px; }
  #el-name-big { font-size: 18px; }
  #el-cat-badge { font-size: 11px; padding: 4px 10px; }
  #el-props { grid-template-columns: repeat(2, 1fr); gap: 5px; }
  .prop-item { padding: 5px 7px; }
  .prop-label { font-size: 9px; }
  .prop-val { font-size: 12px; }
  .tab-btn { font-size: 11px; padding: 7px 6px; }
  #el-content { padding: 12px; }
  .info-block { font-size: 13px; }
  #group-panel { width: 100vw; max-height: 96vh; border-radius: 8px; }
  #group-icon { width: 48px; height: 48px; font-size: 20px; }
  #group-name { font-size: 16px; }
  #group-range-badge { font-size: 11px; }
  #group-body { padding: 14px; }
  .group-el-chip { padding: 5px 8px; font-size: 12px; }
  #legend { gap: 6px; margin-top: 10px; }
  .leg-item { font-size: 9px; gap: 4px; }
  .leg-dot { width: 8px; height: 8px; }
  .footer {
    margin-top: 16px;
    padding: 14px 8px 12px;
  }
  .footer-links { gap: 12px; }
  .footer-links li a { font-size: 10px; }
  .footer-copy { font-size: 9px; }
}

/* ---- RESPONSIVE: Landscape orientation on phones ---- */
@media (max-height: 500px) and (orientation: landscape) {
  #el-panel, .help-panel, #group-panel { max-height: 96vh; }
  #el-header { padding: 10px 14px 8px; gap: 12px; flex-direction: row; }
  #el-symbol-big { width: 54px; height: 54px; }
  #el-name-big { font-size: 16px; }
  .tab-btn { padding: 6px 8px; font-size: 11px; }
  #el-content { padding: 10px; }
  .footer {
    margin-top: 16px;
    padding: 12px 8px;
  }
}