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

:root {
  --green:   #538d4e;
  --yellow:  #c9b458;
  --orange:  #e8a135;
  --grey:    #787c7e;
  --tile-bg: #d3d6da;
  --bg:      #f8f9fa;
  --surface: #ffffff;
  --text:    #1a1a1b;
  --border:  #d3d6da;
  --radius:  6px;
  /* Responsive cell width: scales with viewport (96% container width minus ~105px of padding/gaps)
     divided across 18 columns, capped at the original 2.8rem and never below 1.1rem */
  --cell-w: clamp(1.1rem, calc((96vw - 105px) / 18), 2.8rem);
  /* Font-size ratios relative to cell width (derived from original fixed sizes / 2.8rem) */
  --rect-num-ratio: 0.21;
  --rect-sym-ratio: 0.36;
  --rect-name-ratio: 0.135;
}

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

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

/* ── Header ── */
.game-header {
  width: 100%;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 24px;
  position: relative;
  margin-bottom: 20px;
}
.game-header h1 {
  font-size: 1.9rem;
  font-weight: 800;
  letter-spacing: 0.18rem;
  text-transform: uppercase;
}
.header-actions {
  position: absolute;
  right: 24px;
  display: flex;
  gap: 8px;
}
.icon-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 6px 10px;
  color: var(--text);
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--bg); }
.icon-btn:disabled { opacity: 0.4; cursor: default; background: none; }

/* ── Main container ── */
.container {
  background: var(--surface);
  padding: 20px 28px 32px;
  border-radius: 12px;
  box-shadow: 0 2px 14px rgba(0,0,0,0.07);
  text-align: center;
  max-width: 1040px;
  width: 96%;
  margin-bottom: 24px;
}

/* ── Rules ── */
.rules {
  text-align: left;
  font-size: 0.82rem;
  color: var(--grey);
  margin-bottom: 14px;
  padding: 10px 14px;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}
.rules p {
  margin-bottom: 6px;
  color: var(--text);
}
.rules ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.rules li {
  display: flex;
  align-items: center;
  gap: 6px;
}
.rule-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}
.green-swatch  { background: var(--green); }
.yellow-swatch { background: var(--yellow); }

/* ── Periodic table grid ── */
#elementGrid {
  display: grid;
  grid-template-columns: repeat(18, var(--cell-w));
  gap: 3px 2px;
  margin: 4px auto 18px;
  width: fit-content;
  overflow-x: auto;
}

.rectangle {
  width: var(--cell-w);
  height: calc(var(--cell-w) * 1.143);
  background: #eef0f3;
  border: 1px solid #cdd0d6;
  border-radius: 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2px 1px 2px;
  color: var(--text);
  transition: background 0.2s, color 0.2s;
  overflow: hidden;
}

.rect-num,
.rect-sym,
.rect-name {
  display: none;
}

.incorrectGuess .rect-num,
.correctGuess .rect-num {
  display: block;
  font-size: calc(var(--cell-w) * var(--rect-num-ratio));
  font-weight: 600;
  opacity: 0.8;
  line-height: 1;
  align-self: flex-start;
  padding-left: 2px;
}

.incorrectGuess .rect-sym,
.correctGuess .rect-sym {
  display: block;
  font-size: calc(var(--cell-w) * var(--rect-sym-ratio));
  font-weight: 800;
  line-height: 1.1;
}

.incorrectGuess .rect-name,
.correctGuess .rect-name {
  display: block;
  font-size: calc(var(--cell-w) * var(--rect-name-ratio));
  font-weight: 400;
  line-height: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
  text-align: center;
  opacity: 0.9;
}

/* visual gap before lanthanide / actinide rows */
.rectangle:nth-child(n+127):nth-child(-n+144) { margin-top: 6px; }

.incorrectGuess {
  background: var(--orange);
  border-color: #c8861a;
  color: #fff;
}
.correctGuess {
  background: var(--green);
  border-color: #3a6b37;
  color: #fff;
}

/* ── Input row ── */
.input-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin: 12px 0 6px;
}
#inputContainer { display: contents; }

/* ── Autocomplete ── */
.autocomplete-wrapper {
  position: relative;
  width: 230px;
}
.autocomplete-list {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 200px;
  overflow-y: auto;
  z-index: 50;
  list-style: none;
  padding: 0;
  margin: 0;
  box-shadow: 0 4px 12px rgba(0,0,0,0.10);
}
.autocomplete-list.open { display: block; }
.autocomplete-list li {
  padding: 9px 14px;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--text);
  border-bottom: 1px solid var(--border);
}
.autocomplete-list li:last-child { border-bottom: none; }
.autocomplete-list li:hover,
.autocomplete-list li.active { background: var(--bg); }

#guessInput {
  padding: 10px 14px;
  font-size: 1rem;
  font-family: inherit;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  outline: none;
  transition: border-color 0.2s;
  background: var(--surface);
  color: var(--text);
}
#guessInput:focus { border-color: var(--text); }

button {
  padding: 10px 20px;
  background: var(--text);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  transition: background 0.15s;
}
button:hover  { background: #3d3d3d; }
button:disabled { background: var(--grey); cursor: not-allowed; }

/* ── Status text ── */
#message {
  font-size: 1rem;
  font-weight: 600;
  min-height: 1.4em;
  margin: 6px 0 2px;
}
#attempts {
  font-size: 0.85rem;
  color: var(--grey);
  margin-bottom: 14px;
}

/* ── Share button (hidden until game ends) ── */
#shareBtn {
  display: none;
  margin: 18px auto 0;
  background: var(--green);
}
#shareBtn:hover { background: #406e3c; }
#shareToast {
  display: none;
  font-size: 0.8rem;
  color: var(--green);
  font-weight: 600;
  margin-top: 6px;
}

/* ── Bonus page link ── */
.bonus-link {
  display: inline-block;
  background: var(--green);
  color: #fff;
  padding: 10px 24px;
  border-radius: var(--radius);
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: background 0.15s;
}
.bonus-link:hover { background: #406e3c; }

/* ── Guess history ── */
#guessedWordsContainer {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.wordDiv {
  display: flex;
  align-items: center;
  gap: 2px;
}
.letterRectangle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--tile-bg);
  width: 26px;
  height: 26px;
  border-radius: 4px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text);
}
.transparentPlaceholder { opacity: 0; }
.green  { background: var(--green);  color: #fff; }
.yellow { background: var(--yellow); color: #fff; }

.sign {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  font-size: 0.82rem;
  margin-left: 4px;
}
.percentageBox {
  display: inline-block;
  width: 11px;
  height: 11px;
  margin-left: 2px;
  border-radius: 2px;
  background: var(--tile-bg);
  border: 1px solid #b9bcc2;
}
.greenBox  { background: var(--green);  border-color: #3a6b37; }
.yellowBox { background: var(--yellow); border-color: #8a7520; }

/* ── Stats modal ── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: var(--surface);
  border-radius: 12px;
  padding: 28px 32px 24px;
  width: min(480px, 94vw);
  position: relative;
  text-align: center;
}
.modal-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--grey);
  padding: 4px 6px;
}
.modal-close:hover { color: var(--text); background: none; }
.modal-box h2 {
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 20px;
}
.stats-summary {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-bottom: 24px;
}
.stat { display: flex; flex-direction: column; align-items: center; }
.stat span { font-size: 2rem; font-weight: 800; }
.stat label { font-size: 0.72rem; color: var(--grey); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 2px; }
.modal-box h3 {
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--grey);
  margin-bottom: 12px;
}
.dist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 0.85rem;
}
.dist-row .dist-num { width: 14px; font-weight: 700; text-align: right; }
.dist-bar-wrap { flex: 1; background: var(--tile-bg); border-radius: 3px; height: 22px; }
.dist-bar {
  height: 100%;
  background: var(--grey);
  border-radius: 3px;
  min-width: 22px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 6px;
  color: #fff;
  font-size: 0.78rem;
  font-weight: 700;
  transition: width 0.4s ease;
}
.dist-bar.highlight { background: var(--green); }

/* ── Responsive tweaks ── */
@media (max-width: 600px) {
  .game-header h1 { font-size: 1.4rem; }
  .container { padding: 14px 14px 24px; }
  .autocomplete-wrapper { width: 100%; }
  .input-row { flex-wrap: wrap; }
}

@media (max-width: 400px) {
  .game-header h1 { font-size: 1.1rem; }
}
