:root {
  --bg: #121213;
  --panel: #1a1a1b;
  --border: #3a3a3c;
  --border-light: #565758;
  --text: #ffffff;
  --muted: #9aa0a6;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --accent: #6aaa64;
  --danger: #d9534f;
  --tile: min(58px, 16vw);
  --gap: 5px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  /* Viewport height: 100vh works everywhere; upgrade to 100dvh (which excludes
     the mobile address bar) where supported. The game pins to this height and the
     board scrolls internally, so the keyboard is always on-screen. */
  --vh: 100vh;
}
@supports (height: 100dvh) { :root { --vh: 100dvh; } }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: var(--vh);
  overflow: hidden;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  margin: 0;
  text-transform: uppercase;
}
.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.3rem;
  font-weight: 700;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:active { background: var(--panel); }
.topbar-right { display: flex; align-items: center; gap: 2px; }

/* ---------- Hard-mode toggle ---------- */
.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 18px auto 6px;
  cursor: pointer;
  text-align: left;
  max-width: 300px;
}
.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track {
  flex: 0 0 auto;
  width: 44px;
  height: 26px;
  border-radius: 13px;
  background: var(--border);
  position: relative;
  transition: background 0.15s ease;
}
.toggle-thumb {
  position: absolute;
  top: 3px; left: 3px;
  width: 20px; height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s ease;
}
.toggle-row input:checked + .toggle-track { background: var(--accent); }
.toggle-row input:checked + .toggle-track .toggle-thumb { transform: translateX(18px); }
.toggle-row input:focus-visible + .toggle-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.toggle-text { font-size: 0.95rem; font-weight: 600; }
.toggle-text small { font-weight: 400; }

#app {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px;
}
.screen {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
#game { overflow: hidden; }
.hidden { display: none !important; }

/* ---------- Lobby ---------- */
.lobby-card {
  margin: auto;
  text-align: center;
  padding: 24px;
  max-width: 360px;
}
.lobby-emoji { font-size: 2.4rem; letter-spacing: 6px; margin-bottom: 8px; }
.lobby-card h2 { margin: 8px 0; }
.muted { color: var(--muted); font-size: 0.95rem; line-height: 1.4; }
.fineprint { color: var(--muted); font-size: 0.8rem; margin-top: 14px; }

/* ---------- Status ---------- */
.status-bar {
  min-height: 24px;
  padding: 10px 8px;
  font-size: 0.95rem;
  font-weight: 600;
  text-align: center;
}
.status-bar .turn-you { color: var(--accent); }
.status-bar .turn-wait { color: var(--muted); }

/* ---------- Board ---------- */
.board {
  display: grid;
  grid-template-rows: repeat(8, var(--tile));
  gap: var(--gap);
  margin: 4px auto 10px;
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  align-content: safe center;   /* center when it fits, scroll from top when it doesn't */
  scrollbar-width: thin;
  padding: 2px 4px;             /* a little room so tiles/scrollbar don't clip */
}
.row {
  display: grid;
  grid-template-columns: repeat(5, var(--tile));
  gap: var(--gap);
}
.tile {
  width: var(--tile);
  height: var(--tile);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile) * 0.5);
  font-weight: 700;
  text-transform: uppercase;
  border: 2px solid var(--border);
  color: var(--text);
  user-select: none;
}
.tile.filled { border-color: var(--border-light); animation: pop 0.1s ease; }
.tile.correct { background: var(--correct); border-color: var(--correct); }
.tile.present { background: var(--present); border-color: var(--present); }
.tile.absent  { background: var(--absent);  border-color: var(--absent); }
.row.p1 .tile:not(.correct):not(.present):not(.absent) { border-left-width: 4px; border-left-color: #4a76c9; }
.row.p2 .tile:not(.correct):not(.present):not(.absent) { border-left-width: 4px; border-left-color: #c96a9a; }
.row.reveal .tile { animation: flip 0.5s ease forwards; }
@keyframes pop { from { transform: scale(0.9);} to { transform: scale(1);} }
@keyframes flip { 0%{transform:rotateX(0)} 50%{transform:rotateX(90deg)} 100%{transform:rotateX(0)} }
.row.shake { animation: shake 0.4s; }
@keyframes shake { 0%,100%{transform:translateX(0)} 20%,60%{transform:translateX(-6px)} 40%,80%{transform:translateX(6px)} }

/* ---------- Share ---------- */
.share-row { display: flex; gap: 8px; margin: 6px 0 10px; width: 100%; max-width: 360px; }
.share-row > button { flex: 1; }

/* ---------- Keyboard ---------- */
.keyboard {
  width: 100%;
  max-width: 500px;
  margin-top: auto;
  padding: 6px 4px calc(10px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.kb-row { display: flex; justify-content: center; gap: 5px; }
.key {
  flex: 1;
  min-width: 0;
  height: 52px;
  border-radius: 6px;
  border: none;
  background: var(--border-light);
  color: var(--text);
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  cursor: pointer;
}
.key.wide { flex: 1.6; font-size: 1.4rem; line-height: 1; }
.key.enter { font-size: 2rem; font-weight: 400; }
.key:active { opacity: 0.8; }
.key.correct { background: var(--correct); }
.key.present { background: var(--present); }
.key.absent  { background: var(--absent); }
.key:disabled { opacity: 0.5; }

/* ---------- Buttons ---------- */
button.primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 12px 18px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  flex: 1;
}
button.primary.big { width: 100%; padding: 16px; font-size: 1.1rem; margin-top: 10px; }
button.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 12px 14px;
  font-weight: 600;
  cursor: pointer;
}
button:active { transform: scale(0.98); }

/* ---------- Endgame ---------- */
.endgame {
  text-align: center;
  padding: 16px;
  width: 100%;
  max-width: 360px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#endgame-text { font-size: 1.2rem; font-weight: 700; }
#endgame-text .win { color: var(--accent); }
#endgame-text .lose { color: var(--danger); }
#endgame-text .word { letter-spacing: 3px; text-transform: uppercase; }
.result-grid {
  font-family: inherit;
  font-size: 1.4rem;
  line-height: 1.15;
  letter-spacing: 3px;
  margin: 0;
  text-align: center;
}

/* ---------- Streaks / stats ---------- */
.stats-line {
  align-self: center;
  margin: 6px auto 2px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.85rem;
  color: var(--muted);
  background: var(--panel);
  cursor: pointer;
}
.stats-line:active { background: var(--border); }

/* Stats modal */
.stats-card { max-width: 380px; text-align: left; }
.stats-card h3 { text-align: center; margin-bottom: 2px; }
.stats-sub { text-align: center; font-size: 0.8rem; margin: 0 0 8px; }
.stats-summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 8px 0 4px; }
.stats-summary > div {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 2px;
  text-align: center;
}
.stats-num { display: block; font-size: 1.25rem; font-weight: 800; }
.stats-lbl { display: block; font-size: 0.65rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.stats-h { margin: 14px 0 4px; font-size: 0.85rem; color: var(--muted); }
.stats-list { list-style: none; padding: 0; margin: 0; max-height: 40vh; overflow-y: auto; }
.stats-list li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding: 8px 2px;
  border-top: 1px solid var(--border);
}
.stats-name { font-weight: 700; }
.stats-meta { font-size: 0.78rem; color: var(--muted); text-align: right; white-space: nowrap; }
.endgame-stats {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.endgame-stats .stat-partner { font-size: 1rem; font-weight: 600; color: var(--text); }
.endgame-stats .stat-global { font-size: 0.85rem; color: var(--muted); }

/* ---------- Confetti ---------- */
.confetti-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 40;
}

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 22px;
  max-width: 340px;
  width: 100%;
  text-align: center;
}
.modal-card h3 { margin-top: 0; }
.modal-card input {
  width: 100%;
  padding: 12px;
  font-size: 1rem;
  border-radius: 8px;
  border: 1px solid var(--border-light);
  background: var(--bg);
  color: var(--text);
  margin: 10px 0;
  text-align: center;
}
.help-list { text-align: left; line-height: 1.7; padding-left: 18px; }
.chip {
  display: inline-flex;
  width: 22px; height: 22px;
  align-items: center; justify-content: center;
  border-radius: 4px; font-weight: 700; font-size: 0.75rem;
  vertical-align: middle; color:#fff;
}
.chip.correct { background: var(--correct); }
.chip.present { background: var(--present); }
.chip.absent  { background: var(--absent); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  color: #000;
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 600;
  z-index: 60;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
