:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --border: #334155;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --accent: #38bdf8;
  --accent-glow: rgba(56, 189, 248, 0.25);
  --tile-bg: #1e293b;
  --tile-border: #38bdf8;
  --tile-symbol: #f8fafc;
  --tile-number: #38bdf8;
  --tile-name: #94a3b8;
  --unmatched-bg: #334155;
  --unmatched-text: #64748b;
  --category-nonmetal: #22d3ee;
  --category-noble: #a78bfa;
  --category-alkali: #f87171;
  --category-alkaline: #fb923c;
  --category-metalloid: #34d399;
  --category-halogen: #facc15;
  --category-transition: #60a5fa;
  --category-post-transition: #4ade80;
  --category-lanthanide: #f472b6;
  --category-actinide: #e879f9;
  --category-custom: #f59e0b;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
    sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

header {
  text-align: center;
  padding: 3rem 1rem 1rem;
}

header h1 {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-muted);
  margin-top: 0.4rem;
  font-size: 1rem;
}

main {
  width: 100%;
  max-width: 720px;
  padding: 1rem 1.5rem 3rem;
}

.input-area {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 2rem;
  position: relative;
}

.input-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
}

#wordInput {
  width: 100%;
  padding: 1rem 2.5rem 1rem 1.25rem;
  font-size: 1.4rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition:
    border-color 0.2s,
    box-shadow 0.2s;
}

#wordInput::placeholder {
  color: var(--text-muted);
  font-weight: 400;
}

#wordInput {
  resize: vertical;
  font-family: inherit;
  line-height: 1.4;
}

#wordInput:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
}

#clearBtn {
  position: absolute;
  right: 0.75rem;
  top: 0.75rem;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.4rem;
  border-radius: 6px;
  line-height: 1;
  transition:
    color 0.15s,
    background 0.15s;
  display: none;
}

#clearBtn:hover {
  color: var(--text);
  background: var(--border);
}

.tiles {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: center;
  min-height: 120px;
}

.tile-word {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
}

.tile {
  width: 100px;
  height: 120px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--tile-border);
  background: var(--tile-bg);
  position: relative;
  transition:
    transform 0.2s,
    box-shadow 0.2s;
  animation: tileIn 0.3s ease-out both;
}

.tile:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

@keyframes tileIn {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.92);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.tile-number {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--tile-number);
  position: absolute;
  top: 6px;
  left: 8px;
}

.tile-symbol {
  font-size: 2.6rem;
  font-weight: 800;
  color: var(--tile-symbol);
  line-height: 1;
}

.tile-name {
  font-size: 0.65rem;
  color: var(--tile-name);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-top: 4px;
}

.tile-mass {
  font-size: 0.6rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.tile-unmatched {
  width: 100px;
  height: 120px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--unmatched-text);
  background: var(--unmatched-bg);
  animation: tileIn 0.3s ease-out both;
}

.tile-unmatched .tile-symbol {
  color: var(--unmatched-text);
  font-size: 2.6rem;
  font-weight: 800;
}

.tile-unmatched .tile-name {
  font-size: 0.65rem;
  color: var(--unmatched-text);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-top: 4px;
}

.no-match {
  text-align: center;
  padding: 1rem 0 0.5rem;
  color: var(--text-muted);
}

.no-match-icon {
  font-size: 2rem;
}

.no-match p {
  margin-top: 0.3rem;
}

.no-match-hint {
  font-size: 0.85rem;
  color: var(--unmatched-text);
}

/* Theme picker */
.theme-picker-wrap {
  position: relative;
}

.theme-popover {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.6rem;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.4rem;
  min-width: 240px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
  animation: modalIn 0.15s ease-out;
  z-index: 10;
}

.theme-popover[hidden] {
  display: none;
}

.theme-swatch {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem 0.4rem;
  border-radius: 8px;
  border: 2px solid transparent;
  background: none;
  cursor: pointer;
  transition:
    border-color 0.15s,
    background 0.15s;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 600;
}

.theme-swatch:hover {
  background: var(--border);
}

.theme-swatch.active {
  border-color: var(--accent);
  color: var(--text);
}

.theme-swatch-preview {
  width: 36px;
  height: 28px;
  border-radius: 5px;
  border: 1.5px solid rgba(255, 255, 255, 0.1);
}

/* Theme overrides on tiles */
.tiles[data-theme='pastel'] .tile {
  background: #f0f4ff;
  border-width: 2px;
}
.tiles[data-theme='pastel'] .tile .tile-symbol {
  color: #1e293b;
}
.tiles[data-theme='pastel'] .tile .tile-name {
  color: #64748b;
}
.tiles[data-theme='pastel'] .tile .tile-mass {
  color: #94a3b8;
}
.tiles[data-theme='pastel'] .tile-unmatched {
  background: #f1f5f9;
  border-color: #cbd5e1;
}
.tiles[data-theme='pastel'] .tile-unmatched .tile-symbol {
  color: #94a3b8;
}
.tiles[data-theme='pastel'] .tile-unmatched .tile-name {
  color: #cbd5e1;
}

.tiles[data-theme='neon'] .tile {
  background: #0a0a0a;
  box-shadow:
    0 0 10px var(--tile-border),
    inset 0 0 8px rgba(255, 255, 255, 0.03);
}
.tiles[data-theme='neon'] .tile .tile-symbol {
  text-shadow: 0 0 12px currentColor;
}

.tiles[data-theme='mono'] .tile {
  border-color: #e2e8f0;
  background: var(--tile-bg);
}
.tiles[data-theme='mono'] .tile .tile-number {
  color: #e2e8f0;
}
.tiles[data-theme='mono'] .tile .tile-symbol {
  color: #f8fafc;
}
.tiles[data-theme='mono'] .tile .tile-name {
  color: #94a3b8;
}

.tiles[data-theme='heisenberg'] .tile {
  background: #0c1a0c;
  border-color: #22c55e;
}
.tiles[data-theme='heisenberg'] .tile .tile-number {
  color: #22c55e;
}
.tiles[data-theme='heisenberg'] .tile .tile-symbol {
  color: #4ade80;
}
.tiles[data-theme='heisenberg'] .tile .tile-name {
  color: #166534;
}
.tiles[data-theme='heisenberg'] .tile .tile-mass {
  color: #14532d;
}
.tiles[data-theme='heisenberg'] .tile-unmatched {
  background: #0c1a0c;
  border-color: #166534;
}
.tiles[data-theme='heisenberg'] .tile-unmatched .tile-symbol {
  color: #166534;
}
.tiles[data-theme='heisenberg'] .tile-unmatched .tile-name {
  color: #14532d;
}

.tiles[data-theme='vintage'] .tile {
  background: #fef3c7;
  border-color: #92400e;
  border-width: 2px;
}
.tiles[data-theme='vintage'] .tile .tile-number {
  color: #92400e;
}
.tiles[data-theme='vintage'] .tile .tile-symbol {
  color: #451a03;
}
.tiles[data-theme='vintage'] .tile .tile-name {
  color: #78350f;
}
.tiles[data-theme='vintage'] .tile .tile-mass {
  color: #92400e;
}
.tiles[data-theme='vintage'] .tile-unmatched {
  background: #fef9ee;
  border-color: #d6d3d1;
}
.tiles[data-theme='vintage'] .tile-unmatched .tile-symbol {
  color: #a8a29e;
}
.tiles[data-theme='vintage'] .tile-unmatched .tile-name {
  color: #d6d3d1;
}

.export-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.export-option {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

.export-option input[type='checkbox'] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.export-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.5rem 1.2rem;
  border-radius: 8px;
  cursor: pointer;
  transition:
    color 0.15s,
    background 0.15s,
    border-color 0.15s;
}

.export-btn:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--border);
}

/* Custom element dialog */
.custom-el-dialog {
  max-width: 360px;
}

.custom-el-hint {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 1rem;
  line-height: 1.5;
}

.custom-el-hint span {
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
}

.custom-el-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-bottom: 0.8rem;
}

.custom-el-field label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.custom-el-field input {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}

.custom-el-field input:focus {
  border-color: var(--accent);
}

.custom-el-field input[readonly] {
  opacity: 0.6;
  cursor: not-allowed;
}

.custom-el-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}

.custom-el-save {
  background: var(--accent) !important;
  color: #0f172a !important;
  border-color: var(--accent) !important;
}

.custom-el-save:hover {
  opacity: 0.9;
}

/* Unmatched tile clickable */
.tile-unmatched.clickable {
  cursor: pointer;
  transition:
    border-color 0.15s,
    background 0.15s;
}

.tile-unmatched.clickable:hover {
  border-color: var(--accent);
  background: rgba(56, 189, 248, 0.08);
}

.tile-unmatched.clickable .tile-name {
  color: var(--accent);
}

/* Custom element list bar */
.custom-el-list {
  width: 100%;
  margin-top: 1rem;
}

.custom-el-list[hidden] {
  display: none;
}

.custom-el-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.5rem;
}

.custom-el-clear-all {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.75rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  transition:
    color 0.15s,
    background 0.15s;
}

.custom-el-clear-all:hover {
  color: #f87171;
  background: rgba(248, 113, 113, 0.1);
}

#customElItems {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.custom-el-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.3rem 0.5rem 0.3rem 0.7rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.8rem;
  color: var(--text);
}

.custom-el-chip-sym {
  font-weight: 700;
  color: var(--accent);
}

.custom-el-chip-name {
  color: var(--text-muted);
}

.custom-el-chip-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0 0.15rem;
  line-height: 1;
  border-radius: 3px;
  transition:
    color 0.15s,
    background 0.15s;
}

.custom-el-chip-remove:hover {
  color: #f87171;
  background: rgba(248, 113, 113, 0.15);
}

.how-it-works {
  margin-top: 2.5rem;
  padding: 1rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.6;
}

.how-it-works summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  user-select: none;
}

.how-it-works p {
  margin-top: 0.6rem;
}

/* Share toast */
.share-toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #0f172a;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.6rem 1.4rem;
  border-radius: 8px;
  z-index: 9999;
  animation: toastIn 0.25s ease-out;
  box-shadow: 0 4px 20px rgba(56, 189, 248, 0.35);
}

.share-toast-out {
  opacity: 0;
  transform: translateX(-50%) translateY(8px);
  transition:
    opacity 0.3s,
    transform 0.3s;
}

@keyframes toastIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* Category-colored left border on tiles */
.tile[data-category='nonmetal'] {
  border-color: var(--category-nonmetal);
}
.tile[data-category='noble gas'] {
  border-color: var(--category-noble);
}
.tile[data-category='alkali metal'] {
  border-color: var(--category-alkali);
}
.tile[data-category='alkaline earth metal'] {
  border-color: var(--category-alkaline);
}
.tile[data-category='metalloid'] {
  border-color: var(--category-metalloid);
}
.tile[data-category='halogen'] {
  border-color: var(--category-halogen);
}
.tile[data-category='transition metal'] {
  border-color: var(--category-transition);
}
.tile[data-category='post-transition metal'] {
  border-color: var(--category-post-transition);
}
.tile[data-category='lanthanide'] {
  border-color: var(--category-lanthanide);
}
.tile[data-category='actinide'] {
  border-color: var(--category-actinide);
}
.tile[data-category='custom'] {
  border-color: var(--category-custom);
}

.tile[data-category='nonmetal'] .tile-number {
  color: var(--category-nonmetal);
}
.tile[data-category='noble gas'] .tile-number {
  color: var(--category-noble);
}
.tile[data-category='alkali metal'] .tile-number {
  color: var(--category-alkali);
}
.tile[data-category='alkaline earth metal'] .tile-number {
  color: var(--category-alkaline);
}
.tile[data-category='metalloid'] .tile-number {
  color: var(--category-metalloid);
}
.tile[data-category='halogen'] .tile-number {
  color: var(--category-halogen);
}
.tile[data-category='transition metal'] .tile-number {
  color: var(--category-transition);
}
.tile[data-category='post-transition metal'] .tile-number {
  color: var(--category-post-transition);
}
.tile[data-category='lanthanide'] .tile-number {
  color: var(--category-lanthanide);
}
.tile[data-category='actinide'] .tile-number {
  color: var(--category-actinide);
}
.tile[data-category='custom'] .tile-number {
  color: var(--category-custom);
}

/* Table reference button */
#tableBtn {
  flex-shrink: 0;
  background: none;
  border: 2px solid var(--border);
  color: var(--accent);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0.75rem;
  border-radius: 12px;
  line-height: 1;
  transition:
    color 0.15s,
    background 0.15s,
    border-color 0.15s;
}

#tableBtn:hover {
  background: var(--border);
  color: var(--text);
}

/* Modal overlay */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  animation: fadeIn 0.2s ease-out;
}

.modal-overlay[hidden] {
  display: none;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal-content {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  max-width: 95vw;
  max-height: 92vh;
  overflow: auto;
  padding: 1.5rem;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5);
  animation: modalIn 0.25s ease-out;
}

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

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.modal-header h2 {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
}

.modal-close {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  padding: 0.3rem 0.6rem;
  border-radius: 8px;
  transition:
    color 0.15s,
    background 0.15s;
}

.modal-close:hover {
  color: var(--text);
  background: var(--border);
}

/* Legend */
.modal-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 1.2rem;
  margin-bottom: 1rem;
  font-size: 0.75rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  color: var(--text-muted);
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

/* Periodic table grid: 18 columns x 10 rows (7 main + gap + 2 for lanthanides/actinides) */
.periodic-grid {
  display: grid;
  grid-template-columns: repeat(18, 1fr);
  grid-template-rows: repeat(10, auto);
  gap: 2px;
  min-width: 750px;
}

.pgrid-cell {
  aspect-ratio: 1;
  min-width: 38px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: default;
  transition:
    transform 0.15s,
    box-shadow 0.15s;
  position: relative;
  padding: 2px;
}

.pgrid-cell:hover {
  transform: scale(1.25);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 2;
}

.pgrid-cell .pgrid-num {
  font-size: 0.5rem;
  font-weight: 600;
  opacity: 0.7;
  line-height: 1;
}

.pgrid-cell .pgrid-sym {
  font-size: 0.95rem;
  font-weight: 800;
  line-height: 1.15;
  color: #fff;
}

.pgrid-cell .pgrid-name {
  font-size: 0.38rem;
  opacity: 0.7;
  line-height: 1;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.pgrid-label {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.6rem;
  font-weight: 600;
  color: var(--text-muted);
  min-width: 38px;
}

@media (max-width: 480px) {
  header {
    padding-top: 4rem;
  }

  header h1 {
    font-size: 1.6rem;
  }

  #wordInput {
    font-size: 1.1rem;
    padding: 0.8rem 2.5rem 0.8rem 1rem;
  }

  .tile,
  .tile-unmatched {
    width: 76px;
    height: 96px;
  }

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

  .tile-unmatched .tile-symbol {
    font-size: 2rem;
  }

  .tiles {
    gap: 0.4rem;
  }

  .modal-content {
    padding: 1rem;
  }

  .modal-header h2 {
    font-size: 1rem;
  }

  .periodic-grid {
    min-width: 600px;
    gap: 1px;
  }

  .pgrid-cell {
    min-width: 30px;
  }

  .pgrid-cell .pgrid-sym {
    font-size: 0.75rem;
  }

  .pgrid-cell .pgrid-num,
  .pgrid-cell .pgrid-name {
    display: none;
  }
}
