:root {
  --bg-primary: #17182b;
  --bg-secondary: #20223a;
  --bg-tertiary: #2a2c47;
  --border: #363858;
  --text-primary: #e2e4f0;
  --text-secondary: #9da0b8;
  --text-link: #7b8cff;
  --accent: #5b6ef5;
  --accent-hover: #7b8cff;
  --success: #2ea043;
  --error: #da3633;
  --warning: #d29922;
  --purple: #c9a0ff;
  --gradient-start: #1a1b2e;
  --gradient-mid: #2d1b69;
  --gradient-end: #1b3a5c;
  --card-bg: rgba(255,255,255,0.06);
  --sidebar-width: 220px;
}
[data-theme="light"] {
  --bg-primary: #f8f6ff;
  --bg-secondary: #eeedfb;
  --bg-tertiary: #e3e1f5;
  --border: #cdcae8;
  --text-primary: #1f1a3a;
  --text-secondary: #5e5a82;
  --text-link: #5b6ef5;
  --accent: #5b6ef5;
  --accent-hover: #4a5de0;
  --success: #1a7f37;
  --error: #cf222e;
  --warning: #9a6700;
  --purple: #7c3aed;
  --gradient-start: #f0eeff;
  --gradient-mid: #fce4ec;
  --gradient-end: #e0f0ff;
  --card-bg: rgba(255,255,255,0.7);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  display: flex;
  min-height: 100vh;
  transition: background 0.3s, color 0.3s;
}
/* ===== SIDEBAR ===== */
#sidebar {
  width: var(--sidebar-width); min-width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  padding: 1.5rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  transition: transform 0.3s, background 0.3s, border-color 0.3s;
  z-index: 100;
}
#sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
}
#sidebar h1 {
  font-size: 1.3rem;
  color: var(--text-link);
  letter-spacing: 2px;
}
#sidebar nav { display: flex; flex-direction: column; gap: 0.25rem; }
#sidebar nav a {
  color: var(--text-secondary);
  text-decoration: none;
  padding: 0.6rem 0.8rem;
  border-radius: 6px;
  font-size: 0.9rem;
  transition: all 0.2s;
  cursor: pointer;
}
#sidebar nav a:hover { color: var(--text-primary); background: var(--bg-tertiary); }
#sidebar nav a.active { color: #f0f6fc; background: var(--accent); }
#hamburger {
  display: none;
  background: none; border: none; color: var(--text-primary);
  font-size: 1.5rem; cursor: pointer; padding: 0.25rem;
}
#sidebar-overlay {
  display: none;
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  z-index: 99;
}
/* ===== CONTENT ===== */
#content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
main {
  flex: 1; padding: 2rem; max-width: 1000px; margin: 0 auto; width: 100%;
}
.tab-content { display: none; }
.tab-content.active { display: block; }
h2 {
  font-size: 1.4rem; margin-bottom: 0.25rem; color: var(--text-primary);
}
.desc { color: var(--text-secondary); margin-bottom: 1rem; font-size: 0.9rem; }
.desc code { background: var(--bg-tertiary); padding: 0.1rem 0.4rem; border-radius: 3px; font-size: 0.85rem; }
/* ===== THEME TOGGLE ===== */
#theme-toggle {
  display: flex; align-items: center; justify-content: center;
  padding: 0.4rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.2s;
}
#theme-toggle:hover { color: var(--text-primary); background: var(--border); }
/* ===== INPUTS ===== */
.wide-input {
  width: 100%; padding: 0.75rem 1rem; font-size: 1rem;
  background: var(--bg-primary); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text-primary); outline: none; transition: border 0.2s;
}
.wide-input:focus { border-color: var(--text-link); }
.wide-input::placeholder { color: var(--text-secondary); opacity: 0.6; }
textarea {
  width: 100%; padding: 0.75rem 1rem; font-size: 0.95rem;
  background: var(--bg-primary); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text-primary); outline: none; resize: vertical;
  font-family: 'Consolas', 'Courier New', monospace;
  transition: border 0.2s;
}
textarea:focus { border-color: var(--text-link); }
textarea::placeholder { color: var(--text-secondary); opacity: 0.6; }
.btn-row, .eq-var-row, .struct-row {
  display: flex; gap: 0.5rem; margin-top: 0.75rem; flex-wrap: wrap; align-items: center;
}
.eq-var-row label { font-size: 0.9rem; color: var(--text-secondary); }
.small-input {
  width: 120px; padding: 0.4rem 0.6rem; font-size: 0.9rem;
  background: var(--bg-primary); border: 1px solid var(--border); border-radius: 4px;
  color: var(--text-primary); outline: none;
}
.small-input:focus { border-color: var(--text-link); }
.btn {
  padding: 0.6rem 1.2rem; font-size: 0.9rem; font-weight: 500;
  border: none; border-radius: 6px; cursor: pointer; transition: all 0.2s;
  display: inline-flex; align-items: center; gap: 0.4rem;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.secondary { background: var(--bg-tertiary); color: var(--text-primary); border: 1px solid var(--border); }
.btn.secondary:hover:not(:disabled) { background: var(--border); }
.btn.icon { padding: 0.4rem 0.6rem; font-size: 0.85rem; }
/* ===== OUTPUT / RESULTS ===== */
.output, .steps {
  margin-top: 1rem; padding: 1rem; background: var(--bg-secondary);
  border: 1px solid var(--border); border-radius: 6px; display: none;
  white-space: pre-wrap; font-family: 'Consolas', 'Courier New', monospace;
  line-height: 1.6; transition: background 0.3s, border-color 0.3s;
}
.output.show, .steps.show { display: block; }
.output.error { border-color: var(--error); color: var(--error); }
.output.success { border-color: var(--success); }
.steps { border-color: var(--text-link); color: var(--text-secondary); font-size: 0.85rem; }
/* ===== RESULT CARD ===== */
.result-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  margin-top: 0.75rem;
  transition: background 0.3s, border-color 0.3s;
}
.result-card:first-of-type { margin-top: 0; }
.result-card .card-header {
  display: flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.result-card .type-badge {
  display: inline-block;
  background: var(--bg-tertiary);
  color: var(--purple);
  padding: 0.15rem 0.6rem;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.result-card .card-body { padding: 0.5rem 0; }
.result-card .card-actions { display: flex; gap: 0.4rem; }
.result-card .card-actions button {
  padding: 0.3rem 0.6rem;
  font-size: 0.75rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}
.result-card .card-actions button:hover {
  color: var(--text-primary);
  background: var(--border);
}
/* ===== RICH EQUATION RENDERING ===== */
.chem-eqn {
  font-size: 1.2rem; padding: 0.5rem 0;
  font-family: 'Consolas', 'Courier New', monospace;
  line-height: 1.8;
}
.chem-eqn sub { font-size: 0.75em; }
.chem-eqn .coeff { color: var(--text-link); font-weight: 600; }
.chem-eqn .arrow { color: var(--text-secondary); padding: 0 0.3rem; }
.chem-eqn .plus { color: var(--text-secondary); margin: 0 0.3rem; }
.chem-eqn .compound { color: var(--text-primary); }
.reaction-type { color: var(--purple); font-weight: 500; margin-bottom: 0.25rem; }
/* ===== LOADING SPINNER ===== */
.spinner {
  display: inline-block;
  width: 1rem; height: 1rem;
  border: 2px solid var(--border);
  border-top-color: var(--text-link);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-overlay {
  display: none; position: relative;
}
.loading-overlay.active { display: flex; align-items: center; gap: 0.5rem; padding: 1rem 0; }
/* ===== HISTORY PANEL ===== */
.history-panel {
  margin-top: 1rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}
.history-panel h4 {
  font-size: 0.8rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 0.5rem;
  cursor: pointer;
  user-select: none;
}
.history-panel h4:hover { color: var(--text-primary); }
.history-item {
  display: flex; align-items: center; justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}
.history-item:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.history-item .del-btn {
  background: none; border: none; color: var(--text-secondary);
  cursor: pointer; font-size: 0.8rem; padding: 0.1rem;
}
.history-item .del-btn:hover { color: var(--error); }
/* ===== TOOLTIP ===== */
.help-tip {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1rem; height: 1rem; border-radius: 50%;
  background: var(--bg-tertiary); border: 1px solid var(--border);
  color: var(--text-secondary); font-size: 0.65rem; cursor: help;
  position: relative; margin-left: 0.3rem;
}
.help-tip::after {
  content: attr(data-tip);
  position: absolute; bottom: 130%; left: 50%; transform: translateX(-50%);
  background: var(--bg-tertiary); color: var(--text-primary);
  padding: 0.4rem 0.7rem; border-radius: 4px;
  font-size: 0.75rem; white-space: nowrap;
  border: 1px solid var(--border);
  display: none; z-index: 50;
}
.help-tip:hover::after { display: block; }
/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  body { flex-direction: column; }
  #sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    z-index: 110;
  }
  #sidebar.open { transform: translateX(0); }
  #hamburger {
    display: inline-flex; align-items: center; justify-content: center;
    position: fixed; top: 0.5rem; left: 0.5rem; z-index: 105;
    background: var(--bg-secondary); border: 1px solid var(--border);
    border-radius: 6px; color: var(--text-primary);
    font-size: 1.4rem; cursor: pointer; padding: 0.35rem 0.5rem;
    line-height: 1;
  }
  #hamburger:hover { background: var(--bg-tertiary); }
  #sidebar-overlay.active { display: block; }
  main { padding: 1.5rem 0.75rem; padding-top: 3rem; }
  #content { padding-top: 0; }
  .eq-var-row { flex-direction: column; align-items: stretch; }
  .eq-var-row label { display: flex; flex-direction: column; gap: 0.25rem; }
  .small-input { width: 100%; }
  footer { padding: 0.75rem; }
  #sec-home {
    margin: -1.5rem -0.75rem;
    padding: 2rem 1rem;
    border-radius: 0;
    min-height: auto;
  }
  .home-hero h1 { font-size: 2rem; }
  .home-grid { grid-template-columns: repeat(2, 1fr); gap: 0.75rem; }
  .home-card { padding: 1rem; }
}
/* ===== SUBSCRIPT RENDERING ===== */
.chem-formula sub, .chem-eqn sub { display: inline; }
/* ===== ERROR WITH SUGGESTION ===== */
.error-suggestion {
  margin-top: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.85rem;
}
.error-suggestion .suggestion-label { color: var(--warning); font-weight: 500; }
/* ===== FOOTER ===== */
footer {
  text-align: center; padding: 1rem; color: var(--text-secondary);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
  transition: border-color 0.3s;
}
/* ===== HOMEPAGE ===== */
#sec-home.active {
  padding: 2rem 0;
  min-height: calc(100vh - 8rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--gradient-start), var(--gradient-mid), var(--gradient-end));
  border-radius: 16px;
  margin: -2rem;
  padding: 3rem 2rem;
  transition: background 0.5s;
}
.home-hero {
  text-align: center;
  margin-bottom: 2.5rem;
}
.home-hero h1 {
  font-size: 2.8rem;
  font-weight: 800;
  background: linear-gradient(135deg, #7b8cff, #c9a0ff, #f0a0d0);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 1px;
  margin-bottom: 0.5rem;
}
.home-hero p {
  font-size: 1.1rem;
  color: var(--text-secondary);
  max-width: 500px;
  margin: 0 auto;
  line-height: 1.6;
}
.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  width: 100%;
  max-width: 900px;
}
.home-card {
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 1.5rem 1.25rem;
  cursor: pointer;
  transition: all 0.25s ease;
  text-align: center;
}
.home-card:hover {
  transform: translateY(-4px);
  background: rgba(255,255,255,0.12);
  border-color: rgba(255,255,255,0.2);
  box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}
.home-card .home-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
  display: block;
}
.home-card h3 {
  font-size: 1rem;
  color: var(--text-primary);
  margin-bottom: 0.3rem;
}
.home-card p {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.4;
}

#structure-viewer { margin-top: 1rem; border-radius: 6px; overflow: hidden; }
#structure-viewer .mol-container { width: 100%; height: 100%; }
/* ===== PERIODIC TABLE ===== */
.periodic-grid {
  display: grid;
  gap: 1px;
  margin-top: 0.5rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.el-cell {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  border: 1px solid var(--border); border-radius: 2px;
  background: var(--bg-secondary); cursor: pointer;
  transition: all 0.15s; padding: 1px;
  border-left: 2px solid var(--cat-color, #888);
  min-height: 36px;
  min-width: 36px;
}
.el-cell:hover { background: var(--bg-tertiary); transform: scale(1.05); z-index: 2; }
.el-cell.active { background: var(--bg-tertiary); border-color: var(--text-link); box-shadow: 0 0 4px var(--accent); }
.el-cell .el-num { font-size: 0.4rem; color: var(--text-secondary); line-height: 1; }
.el-cell .el-sym { font-size: 0.75rem; font-weight: 600; color: var(--text-primary); line-height: 1.1; }
.el-cell .el-name { font-size: 0.35rem; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; max-width: 100%; white-space: nowrap; display: none; }
.el-cell .el-mass { display: none; }
.el-series-label {
  display: flex; align-items: center; justify-content: center;
  font-size: 0.55rem; color: var(--text-secondary);
  font-weight: 500; letter-spacing: 0.3px;
  text-transform: uppercase;
  border: 1px dashed var(--border); border-radius: 3px;
  background: var(--bg-tertiary);
  aspect-ratio: auto;
  min-height: 36px;
}
.el-series-row-label {
  margin-top: 0.4rem;
}
.periodic-filters { display: flex; gap: 0.3rem; flex-wrap: wrap; margin-bottom: 0.4rem; }
.periodic-filters .active { background: var(--accent); color: #fff; border-color: var(--accent); }
.el-detail { margin-top: 0.75rem; }
#el-back { cursor: pointer; }
@media (max-width: 768px) {
  .periodic-grid { gap: 1px; }
  .el-cell { min-height: 28px; min-width: 28px; padding: 0; }
  .el-cell .el-num { font-size: 0.3rem; }
  .el-cell .el-sym { font-size: 0.6rem; }
  .el-series-label { font-size: 0.4rem; min-height: 28px; }
  .el-series-row-label { margin-top: 0.3rem; }
  #periodic-info .result-table { font-size: 0.7rem; }
}
/* ===== RESULT TABLE ===== */
.result-table {
  border-collapse: collapse; margin-top: 0.5rem;
  font-size: 0.85rem; color: var(--text-primary);
}
.result-table th, .result-table td {
  padding: 0.4rem 0.8rem; border: 1px solid var(--border);
  text-align: left;
}
.result-table th { background: var(--bg-tertiary); font-weight: 600; color: var(--text-secondary); }
.result-table tr:nth-child(even) { background: var(--bg-tertiary); }
/* ===== PH GRID ===== */
#ph-output .result-table td:first-child { font-weight: 500; color: var(--text-secondary); }
