/* ===== Papos — estilos ===== */
:root {
  --bg: #1a1a2e;
  --bg-tile: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.22);
  --border-active: rgba(255, 255, 255, 0.55);
  --text: #fafafa;
  --text-dim: rgba(250, 250, 250, 0.6);
  --certo: #3aa394;
  --lugar: #d3ad69;
  --errado: #312a45;
  --aviso: #c8616c; /* letra já sabida como errada / fora de posição */
  --key: #4c4470;
  --key-text: #fafafa;
  --modal-bg: #24213a;
  --accent: #3aa394;
  --header-h: 52px;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  user-select: none;
  touch-action: manipulation;
}

/* ===== Header ===== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
  padding: 0 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  flex-shrink: 0;
}

.title {
  font-size: 1.7rem;
  font-weight: 800;
  letter-spacing: 0.25em;
  margin: 0 0 0 0.25em;
  text-align: center;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
}
.icon-btn:hover { color: var(--text); background: rgba(255,255,255,0.08); }

.header-right { display: flex; gap: 4px; }

/* ===== Modos ===== */
.modes {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 0;
  flex-shrink: 0;
}

.mode-btn {
  background: none;
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: var(--text-dim);
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 5px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mode-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); }
.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* ===== Layout do jogo ===== */
.game {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 8px;
  max-width: 500px;
  width: 100%;
  margin: 0 auto;
  min-height: 0;
}

/* modos com vários tabuleiros aproveitam mais largura no desktop */
body[data-mode="dupla"] .game      { max-width: 600px; }
body[data-mode="roda"] .game       { max-width: 640px; }
body[data-mode="tertulia"] .game   { max-width: 900px; }
body[data-mode="assembleia"] .game { max-width: 1100px; }

/* ===== Tabuleiros ===== */
.boards {
  display: grid;
  grid-template-columns: repeat(var(--board-cols, 1), 1fr);
  gap: 14px;
  margin: auto 0;
  justify-items: center;
  align-items: start; /* sem stretch: a altura de cada tabuleiro vem do aspect-ratio */
  /* a largura é definida em JS conforme o modo (limite horizontal e vertical) */
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.board {
  display: grid;
  gap: 6px;
  width: 100%;
  min-width: 0; /* permite encolher abaixo do min-content das letras */
}

.row { min-width: 0; }

.board.done { opacity: 0.55; }
.board.done .tile { transition: opacity 0.4s; }

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

/* modos com vários tabuleiros: tudo mais compacto */
body[data-mode="dupla"] .board,
body[data-mode="dupla"] .row { gap: 4px; }
body[data-mode="dupla"] .boards { gap: 8px; }
body[data-mode="roda"] .board,
body[data-mode="roda"] .row,
body[data-mode="tertulia"] .board,
body[data-mode="tertulia"] .row,
body[data-mode="assembleia"] .board,
body[data-mode="assembleia"] .row { gap: 3px; }
body[data-mode="roda"] .boards { gap: 10px; }
body[data-mode="tertulia"] .boards,
body[data-mode="assembleia"] .boards { gap: 8px; }

.row.shake { animation: shake 0.5s; }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--tile-font, clamp(1.3rem, 6dvh, 2.2rem));
  font-weight: 800;
  text-transform: uppercase;
  border: 2px solid transparent;
  border-radius: 10%;
  background: var(--bg-tile);
  color: var(--text);
  aspect-ratio: 1;
  position: relative;
  transition: border-color 0.12s, transform 0.1s;
  min-width: 0;
  overflow: hidden;
}

/* linha atual: borda visível e clicável */
.row.current .tile {
  border-color: var(--border);
  cursor: pointer;
  border-bottom-width: 4px;
}
/* letra que já se sabe estar errada (não existe ou fora de posição): aviso vermelho claro */
.row.current .tile.warn {
  background: rgba(200, 97, 108, 0.32);
  border-color: rgba(200, 97, 108, 0.7);
}
.row.current .tile.selected {
  border-color: var(--border-active);
  background: rgba(255, 255, 255, 0.12);
}
.row.current .tile.warn.selected {
  background: rgba(200, 97, 108, 0.5);
  border-color: var(--aviso);
}
.row.current .tile.selected::after {
  content: "";
  position: absolute;
  bottom: 12%;
  left: 20%;
  right: 20%;
  height: 3px;
  border-radius: 2px;
  background: var(--text-dim);
  animation: blink 1.1s steps(1) infinite;
}
.row.current .tile.filled::after { display: none; }
@keyframes blink { 50% { opacity: 0; } }

.tile.pop { animation: pop 0.1s; }
@keyframes pop {
  50% { transform: scale(1.12); }
}

/* linhas futuras: mais apagadas */
.row.future .tile { opacity: 0.45; }

/* estados após avaliação */
.tile.certo  { background: var(--certo);  border-color: var(--certo); }
.tile.lugar  { background: var(--lugar);  border-color: var(--lugar); }
.tile.errado { background: var(--errado); border-color: var(--errado); color: var(--text-dim); }

.tile.flip { animation: flip 0.5s ease; }
@keyframes flip {
  0%   { transform: rotateX(0); }
  50%  { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}

.tile.win-bounce { animation: winBounce 0.6s; }
@keyframes winBounce {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-22%); }
  65% { transform: translateY(6%); }
}

/* ===== Teclado ===== */
.keyboard {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  flex-shrink: 0;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 5px;
}

.key {
  flex: 1;
  max-width: 44px;
  height: clamp(44px, 7.5dvh, 58px);
  border: none;
  border-radius: 6px;
  background: var(--key);
  color: var(--key-text);
  font-size: 1.05rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s;
}
.key:active { transform: scale(0.94); }
.key.wide { max-width: 68px; flex: 1.6; font-size: 0.8rem; }

.key.certo  { background: var(--certo); }
.key.lugar  { background: var(--lugar); }
.key.errado { background: var(--errado); color: var(--text-dim); }
.key.dim    { color: var(--text-dim); }

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  top: calc(var(--header-h) + 14px);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 100;
  pointer-events: none;
}

.toast {
  background: #fafafa;
  color: #1a1a2e;
  font-weight: 700;
  padding: 12px 18px;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  animation: toastIn 0.2s ease;
  white-space: nowrap;
}
.toast.fade-out { opacity: 0; transition: opacity 0.4s; }
@keyframes toastIn {
  from { opacity: 0; transform: translateY(-8px); }
}

/* ===== Modais ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--modal-bg);
  border-radius: 16px;
  padding: 24px;
  max-width: 420px;
  width: 100%;
  max-height: 85dvh;
  overflow-y: auto;
  position: relative;
  animation: modalIn 0.18s ease;
  user-select: text;
}
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95); }
}

.modal h2 { margin-top: 0; letter-spacing: 0.05em; }
.modal p { line-height: 1.5; color: var(--text-dim); }
.modal p strong { color: var(--text); }
.modal hr { border: none; border-top: 1px solid rgba(255,255,255,0.12); margin: 16px 0; }
.modal .credits { font-size: 0.78rem; }
.modal .credits a { color: var(--accent); }

.modal-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }

.example-row { display: flex; gap: 5px; margin: 14px 0 6px; }
.tile.small {
  width: 46px;
  height: 46px;
  aspect-ratio: auto;
  font-size: 1.4rem;
  border: 2px solid var(--border);
  border-radius: 8px;
}
/* nos exemplos, a casa avaliada fica com a célula TODA pintada e a letra bem visível */
.example-row .tile.certo  { background: var(--certo);  border-color: var(--certo);  color: #fff; }
.example-row .tile.lugar  { background: var(--lugar);  border-color: var(--lugar);  color: #fff; }
.example-row .tile.errado { background: var(--errado); border-color: var(--errado); color: #fff; }
.example-row .tile.warn   { background: var(--aviso);  border-color: var(--aviso);  color: #fff; }

/* célula colorida pequena, embutida no texto das explicações */
.tile.inline {
  display: inline-flex;
  width: 1.5em;
  height: 1.5em;
  aspect-ratio: auto;
  font-size: 0.95em;
  border: none;
  border-radius: 4px;
  vertical-align: middle;
  margin: 0 2px 0.18em;
  color: #fff;
}
.tile.inline.certo  { background: var(--certo); }
.tile.inline.lugar  { background: var(--lugar); }
.tile.inline.errado { background: var(--errado); }
.tile.inline.warn   { background: var(--aviso); }

/* definição: modo difícil */
.setting { display: flex; align-items: center; gap: 0.75rem; text-align: left; margin: 0.2rem 0; }
.setting-text { display: flex; flex-direction: column; line-height: 1.3; }
.setting-text span { color: var(--text-dim); font-size: 0.85rem; }
.switch { position: relative; display: inline-block; width: 46px; height: 26px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: var(--key); border-radius: 999px; cursor: pointer; transition: background 0.2s; }
.switch .slider::before { content: ""; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform 0.2s; }
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(20px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--border-active); outline-offset: 2px; }

/* autoria */
.modal .author { text-align: center; color: var(--text); font-size: 0.95rem; }
.modal .author strong { color: var(--accent); }
.modal .author .flag-pt { width: 1.25em; height: 1.25em; vertical-align: -0.35em; margin-left: 0.35em; }

/* lista dos modos de jogo */
.modes-list {
  list-style: none;
  margin: 8px 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.modes-list li {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 0.9rem;
  color: var(--text-dim);
}
.modes-list li strong { color: var(--text); }

/* ===== Estatísticas ===== */
.answer-reveal {
  background: rgba(58, 163, 148, 0.15);
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 16px;
  text-align: center;
}
.answer-reveal.hidden { display: none; }
.answer-reveal a {
  color: var(--accent);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  text-decoration: underline;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  margin-bottom: 8px;
}
.stat-num { font-size: 1.8rem; font-weight: 800; }
.stat-label { font-size: 0.72rem; color: var(--text-dim); }

.modal h3 { margin-bottom: 8px; }

.dist { display: flex; flex-direction: column; gap: 5px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; }
.dist-label { flex: none; width: 1.7em; text-align: center; font-variant-numeric: tabular-nums; }
.dist-bar {
  background: var(--key);
  color: var(--text);
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 4px;
  min-width: 24px;
  text-align: right;
  transition: width 0.4s;
}
.dist-bar.best { background: var(--certo); }
.dist-bar.skull { background: var(--aviso); }

.stats-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 20px;
  gap: 12px;
}
.countdown-label { font-size: 0.75rem; color: var(--text-dim); }
.countdown-time { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; }

.share-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 1.05rem;
  font-weight: 800;
  padding: 12px 22px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.share-btn:hover { filter: brightness(1.1); }
.share-btn:active { transform: scale(0.97); }

/* ===== Ecrãs pequenos ===== */
@media (max-width: 380px) {
  .title { font-size: 1.4rem; }
  .key { font-size: 0.95rem; }
  .key.wide { font-size: 0.7rem; }
}

/* ===== Ecrãs grandes ===== */
@media (min-width: 768px) {
  .keyboard { max-width: 484px; }
  .key { max-width: 48px; }
  .key.wide { max-width: 76px; }
}
