:root {
  --bg: #12271a;
  --panel: #1b3526;
  --panel-2: #234431;
  --text: #f2efe6;
  --muted: #a8bfae;
  --border: #3c5c48;
  --correct: #4f9d5d;
  --present: #d4a72c;
  --absent: #33453a;
  --key: #46604f;
  --accent: #e8b23a;
  --tile: min(60px, calc((100vw - 40px) / var(--cols)));
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Helvetica Neue", Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

header {
  width: 100%;
  max-width: 520px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px 6px;
  border-bottom: 1px solid var(--border);
}
header h1 {
  font-size: clamp(20px, 6vw, 28px);
  letter-spacing: 2px;
  font-weight: 900;
}
header h1 span { color: var(--accent); }
.icon-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}
.icon-btn:active { background: var(--panel-2); }

.daybar {
  font-size: 13px;
  color: var(--muted);
  padding: 6px 0 0;
  letter-spacing: 1px;
}

main {
  flex: 1;
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px 6px;
}

/* ---------------- board ---------------- */
#board {
  display: grid;
  grid-template-rows: repeat(6, var(--tile));
  gap: 6px;
  margin: auto 0;
  padding: 8px 0;
}
.row {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--tile));
  gap: 6px;
}
.row.shake { animation: shake 0.45s; }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}
.tile {
  width: var(--tile); height: var(--tile);
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--tile) * 0.5);
  font-weight: 800;
  text-transform: uppercase;
  user-select: none;
  background: transparent;
  transition: transform 0.08s;
}
.tile.filled { border-color: var(--muted); animation: pop 0.1s; }
@keyframes pop { 50% { transform: scale(1.1); } }
.tile.flip { animation: flip 0.5s ease forwards; }
@keyframes flip {
  0% { transform: rotateX(0); }
  49% { transform: rotateX(90deg); }
  50% { transform: rotateX(90deg); background: var(--reveal); border-color: var(--reveal); }
  100% { transform: rotateX(0); background: var(--reveal); border-color: var(--reveal); }
}
.tile.correct, .tile.present, .tile.absent { border-color: transparent; color: #fff; }
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }
.tile.bounce { animation: bounce 0.6s; }
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-14px); }
  60% { transform: translateY(4px); }
}

/* ---------------- keyboard ---------------- */
#keyboard {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 8px;
}
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1;
  max-width: 44px;
  height: 54px;
  border: none;
  border-radius: 5px;
  background: var(--key);
  color: var(--text);
  font-size: 14px;
  font-weight: 800;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
}
.key.wide {
  max-width: 68px; flex: 1.6; font-size: 11px;
  background: var(--panel-2);
  border: 1px solid var(--accent);
  color: var(--accent);
}
.key:active { filter: brightness(1.3); }

/* first-time cue: tap the on-screen keys */
.kb-hint {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  text-align: center;
  padding: 2px 0 8px;
  letter-spacing: 0.3px;
  animation: hintPulse 1.6s ease-in-out infinite;
}
.kb-hint.hidden { display: none; }
@keyframes hintPulse { 0%, 100% { opacity: 0.5; } 50% { opacity: 1; } }
.key.correct { background: var(--correct); }
.key.present { background: var(--present); }
.key.absent { background: var(--absent); color: var(--muted); }

/* ---------------- toast ---------------- */
#toast {
  position: fixed;
  top: 12%;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-weight: 800;
  padding: 10px 16px;
  border-radius: 8px;
  z-index: 50;
  pointer-events: none;
  white-space: nowrap;
}
#toast.hidden { display: none; }

/* ---------------- modals ---------------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 14px;
}
.overlay.hidden { display: none; }
.modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px 20px 18px;
  width: min(440px, 100%);
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}
.modal h2 { font-size: 18px; letter-spacing: 1px; margin-bottom: 10px; }
.modal h3 { font-size: 13px; letter-spacing: 1px; text-transform: uppercase; color: var(--muted); margin: 16px 0 8px; }
.modal p { font-size: 14px; line-height: 1.5; margin-bottom: 10px; }
.modal .fine { font-size: 12px; color: var(--muted); }
.modal a { color: var(--accent); }
.close {
  position: absolute;
  top: 10px; right: 12px;
  background: none; border: none;
  color: var(--muted);
  font-size: 26px;
  cursor: pointer;
  line-height: 1;
}
.swatch {
  display: inline-block;
  width: 12px; height: 12px;
  border-radius: 3px;
  vertical-align: -1px;
}
.swatch.correct { background: var(--correct); }
.swatch.present { background: var(--present); }
.swatch.absent { background: var(--absent); }
.ex-row { display: flex; gap: 4px; margin-bottom: 8px; }
.ex-row .tile { --tile: 38px; font-size: 19px; }

/* why-local card */
.why-card {
  background: var(--panel-2);
  border-left: 4px solid var(--accent);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 16px;
}
.why-label { font-size: 10px; letter-spacing: 2px; color: var(--accent); font-weight: 800; }
.why-answer { font-size: 24px; font-weight: 900; letter-spacing: 3px; margin: 4px 0 6px; }
.why-card p { font-size: 14px; line-height: 1.5; color: var(--text); margin-bottom: 6px; }
.why-card a { font-size: 13px; font-weight: 700; }
.why-card a.hidden { display: none; }

/* stats */
.stat-row { display: flex; gap: 8px; text-align: center; }
.stat { flex: 1; background: var(--panel-2); border-radius: 8px; padding: 10px 4px; }
.stat b { display: block; font-size: 24px; }
.stat small { font-size: 10px; color: var(--muted); line-height: 1.2; display: block; margin-top: 2px; }
#dist { display: flex; flex-direction: column; gap: 4px; }
.dist-row { display: flex; align-items: center; gap: 6px; font-size: 12px; }
.dist-row .n { width: 12px; color: var(--muted); font-weight: 700; }
.dist-row .bar {
  background: var(--absent);
  color: #fff;
  font-weight: 800;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 3px;
  min-width: 20px;
  text-align: right;
}
.dist-row .bar.hl { background: var(--correct); }

/* finished row: countdown + share */
#finishedRow {
  display: flex;
  gap: 12px;
  align-items: stretch;
  margin-top: 16px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
#finishedRow.hidden { display: none; }
.next-box { flex: 1; text-align: center; }
.next-label { font-size: 10px; letter-spacing: 2px; color: var(--muted); font-weight: 800; }
.countdown { font-size: 26px; font-weight: 800; font-variant-numeric: tabular-nums; }
.share-btn {
  flex: 1;
  background: var(--correct);
  border: none;
  border-radius: 8px;
  color: #fff;
  font-size: 16px;
  font-weight: 900;
  letter-spacing: 1px;
  cursor: pointer;
}
.share-btn:active { filter: brightness(1.15); }

/* ---------------- leaderboard ---------------- */
.lb { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 4px; }
.lb.hidden { display: none; }
.lb-tabs { display: flex; gap: 6px; }
.tab {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.tab.sel { background: var(--accent); border-color: var(--accent); color: #1c1607; }
.lb-status { font-size: 12px; color: var(--muted); margin: 8px 0 2px; }
.lb ol { list-style: none; margin: 8px 0 4px; }
.lb li {
  display: flex; gap: 8px; align-items: baseline;
  font-size: 14px; font-weight: 600;
  padding: 3px 6px; border-radius: 6px;
}
.lb li .rank { width: 24px; text-align: right; opacity: 0.7; font-size: 12px; }
.lb li .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb li .sc { font-weight: 800; color: var(--accent); }
.lb li.me { background: rgba(232, 178, 58, 0.18); }
.lb-form { display: flex; gap: 8px; margin-top: 10px; }
.lb-form input {
  flex: 1;
  font: inherit; font-size: 14px; font-weight: 600;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.lb-form input:focus { border-color: var(--accent); }
.lb-form.hidden, .lb-rename.hidden { display: none; }
.lb-rename { margin-top: 8px; font-size: 11px; opacity: 0.85; }

footer {
  width: 100%;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  padding: 8px 10px 14px;
}
footer a { color: var(--accent); text-decoration: none; }
.hidden { display: none; }
