:root {
  --paper: #f6efe1;
  --paper-2: #efe5d0;
  --ink: #1d1a17;
  --ink-soft: #5b534a;
  --line: #2a2520;
  --tomato: #e8492f;
  --tomato-soft: #ffd9c9;
  --teal: #0f8f7e;
  --teal-soft: #c9efe6;
  --sun: #ffc93c;
  --sun-soft: #fff0bd;
  --block: #1d1a17;

  --font-display: 'Fraunces', Georgia, serif;
  --font-body: 'Epilogue', system-ui, sans-serif;

  --radius: 14px;
  --ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease: cubic-bezier(0.25, 0.46, 0.45, 0.94);

  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 10%, rgba(255, 201, 60, 0.22), transparent 40%),
    radial-gradient(circle at 90% 90%, rgba(15, 143, 126, 0.12), transparent 45%),
    repeating-linear-gradient(0deg, rgba(0,0,0,0.018) 0 1px, transparent 1px 4px),
    var(--paper);
  overscroll-behavior: none;
  background-attachment: fixed;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- Bienvenida ---------- */
.welcome {
  min-height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  padding: 1.25rem 1.25rem calc(2rem + var(--safe-b));
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
}
.brand { text-align: center; animation: rise 600ms var(--ease) both; }
.hero {
  display: block;
  width: auto;
  max-width: min(100%, 340px);
  height: auto;
  max-height: 36dvh;
  margin: -0.75rem auto 0.25rem;
  filter: drop-shadow(0 6px 10px rgba(60, 40, 20, 0.18));
}
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.tagline { margin: 0; color: var(--ink-soft); }

.card {
  background: #fffaf0;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 1.1rem;
  box-shadow: 4px 4px 0 var(--line);
  animation: rise 600ms var(--ease) both;
}
.card:nth-of-type(1) { animation-delay: 90ms; }
.card:nth-of-type(2) { animation-delay: 180ms; }
.start { display: flex; flex-direction: column; gap: 0.6rem; }
.start label { font-weight: 800; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.08em; }
.start input {
  font: inherit;
  font-size: 1.2rem;
  padding: 0.8rem 0.9rem;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  outline: none;
}
.start input:focus { border-color: var(--tomato); box-shadow: 0 0 0 3px var(--tomato-soft); }
.error { color: var(--tomato); margin: 0; min-height: 1.2em; font-size: 0.95rem; }
.rules h2 { font-family: var(--font-display); margin: 0 0 0.4rem; font-size: 1.3rem; }
.rules p { margin: 0.3rem 0; line-height: 1.45; }
.example { display: flex; align-items: center; gap: 0.3rem; flex-wrap: wrap; }
.tile {
  display: inline-grid; place-items: center;
  width: 2rem; height: 2rem;
  border: 2px solid var(--line);
  background: #fff;
  font-weight: 800;
}
.tile.v { background: var(--sun-soft); }

/* ---------- Botones ---------- */
.btn {
  display: block;
  width: 100%;
  padding: 0.85rem 1rem;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-weight: 800;
  font-size: 1.05rem;
  transition: transform 120ms var(--ease-bounce), box-shadow 120ms var(--ease);
  box-shadow: 3px 3px 0 var(--line);
}
.btn:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--line); }
.btn.primary { background: var(--tomato); color: #fff; }
.btn.danger { color: var(--tomato); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.link {
  background: none; border: none; box-shadow: none;
  text-decoration: underline; text-underline-offset: 3px;
  color: var(--ink-soft); font-weight: 600;
}

/* ---------- Juego ---------- */
.game {
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  padding-bottom: var(--safe-b);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 2px solid var(--line);
  background: var(--paper);
}
.icon-btn {
  min-width: 44px; height: 44px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-size: 1.4rem;
  line-height: 1;
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--paper-2); }
.who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.who span { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who small { color: var(--ink-soft); }
.score {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 1.6rem;
  min-width: 3.2rem;
  text-align: center;
  padding: 0.1rem 0.6rem;
  background: var(--sun);
  border: 2px solid var(--line);
  border-radius: 999px;
}
.score.bump { animation: bump 450ms var(--ease-bounce); }

.board-wrap {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0.6rem;
  min-height: 0;
}
.board {
  --n: 8;
  --size: min(96vw, calc(100dvh - 330px), 500px);
  width: var(--size);
  height: var(--size);
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  grid-template-rows: repeat(var(--n), 1fr);
  gap: 2px;
  padding: 2px;
  background: var(--line);
  border-radius: 6px;
  box-shadow: 5px 5px 0 var(--tomato);
  touch-action: manipulation;
  user-select: none;
}
.cell {
  position: relative;
  background: #fffdf7;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: calc(var(--size) / 8 * 0.52);
  text-transform: uppercase;
  border: 0;
  padding: 0;
  transition: background-color 160ms var(--ease);
}
.cell.block { background: var(--block); pointer-events: none; }
.cell .num {
  position: absolute;
  top: 1px; left: 3px;
  font-size: calc(var(--size) / 8 * 0.22);
  font-weight: 600;
  color: var(--ink-soft);
  line-height: 1;
}
.cell.in-word { background: var(--sun-soft); }
.cell.active { background: var(--sun); }
.cell.solved { background: var(--teal-soft); color: #08493f; }
.cell.solved.in-word { background: #b2e6da; }
.cell.solved.active { background: #8fd8c8; }
.cell.revealed { color: var(--tomato); background: #fff1ea; }
.cell.wrong { animation: shake 380ms; background: var(--tomato-soft); }
.cell.pop { animation: pop 500ms var(--ease-bounce) both; }

.floaters { position: absolute; inset: 0; pointer-events: none; }
.floater {
  position: absolute;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 2rem;
  color: var(--tomato);
  text-shadow: 2px 2px 0 #fff, -1px -1px 0 #fff;
  animation: float-up 1100ms var(--ease) forwards;
  transform: translate(-50%, -50%);
}

.cluebar {
  display: flex;
  align-items: stretch;
  gap: 0.4rem;
  padding: 0.45rem 0.6rem;
  background: var(--ink);
  color: var(--paper);
}
.cluebar .icon-btn { background: transparent; border-color: #4a433b; color: var(--paper); height: auto; min-height: 48px; }
.clue {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  text-align: left;
  background: transparent;
  border: 0;
  color: inherit;
  padding: 0.2rem 0;
}
.clue-num {
  flex: none;
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 1.05rem;
  background: var(--sun);
  color: var(--ink);
  border-radius: 6px;
  padding: 0.1rem 0.4rem;
}
.clue-txt { font-size: 1rem; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 4px 10px;
  background: var(--paper-2);
  border-top: 2px solid var(--line);
}
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1 1 0;
  max-width: 48px;
  height: 50px;
  border: 0;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 2px 0 #b9ab92;
  font-weight: 800;
  font-size: 1.15rem;
  padding: 0;
  touch-action: manipulation;
}
.key:active, .key.down { background: var(--sun); transform: translateY(2px); box-shadow: none; }
.key.wide { flex-grow: 1.6; max-width: 72px; font-size: 1rem; }

/* ---------- Diálogos ---------- */
dialog.sheet {
  width: min(94vw, 460px);
  max-height: 86dvh;
  border: 2px solid var(--line);
  border-radius: 18px;
  padding: 1.2rem;
  background: #fffaf0;
  color: var(--ink);
  box-shadow: 6px 6px 0 var(--line);
  display: none;
}
dialog.sheet[open] { display: flex; flex-direction: column; gap: 0.6rem; animation: rise 280ms var(--ease) both; }
dialog.sheet.tall { height: 86dvh; }
dialog::backdrop { background: rgba(29, 26, 23, 0.55); }
dialog h2 { font-family: var(--font-display); font-weight: 900; margin: 0 0 0.3rem; font-size: 1.6rem; }

.clue-cols { overflow-y: auto; flex: 1; }
.clue-cols h3 { margin: 0.6rem 0 0.3rem; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft); }
.clue-cols ol { list-style: none; margin: 0; padding: 0; }
.clue-cols li {
  display: flex; gap: 0.6rem;
  padding: 0.55rem 0.3rem;
  border-bottom: 1px dashed #d6c9ae;
  line-height: 1.3;
}
.clue-cols li b { min-width: 1.6rem; font-family: var(--font-display); }
.clue-cols li.solved { color: var(--teal); text-decoration: line-through; text-decoration-color: rgba(15, 143, 126, 0.5); }
.clue-cols li .len { margin-left: auto; color: var(--ink-soft); font-size: 0.85rem; white-space: nowrap; }

.tabs { display: flex; gap: 0.4rem; }
.tab {
  flex: 1; padding: 0.55rem; border: 2px solid var(--line); border-radius: 999px;
  background: #fff; font-weight: 800;
}
.tab.active { background: var(--ink); color: var(--paper); }
.rank { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; counter-reset: r; }
.rank li {
  counter-increment: r;
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.6rem 0.3rem;
  border-bottom: 1px dashed #d6c9ae;
}
.rank li::before {
  content: counter(r);
  width: 1.9rem; height: 1.9rem; flex: none;
  display: grid; place-items: center;
  border: 2px solid var(--line); border-radius: 50%;
  font-weight: 800; font-size: 0.9rem;
}
.rank li:nth-child(1)::before { background: var(--sun); }
.rank li:nth-child(2)::before { background: #e3e3e3; }
.rank li:nth-child(3)::before { background: #f0b78a; }
.rank li.me { background: var(--sun-soft); }
.rank .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank .pts { font-family: var(--font-display); font-weight: 900; font-size: 1.2rem; }
.rank .empty { justify-content: center; color: var(--ink-soft); }
.rank .empty::before { display: none; }

.big-score { text-align: center; margin: 0.2rem 0; }
.big-score span { font-family: var(--font-display); font-weight: 900; font-size: 4rem; color: var(--tomato); line-height: 1; display: block; }
.big-score small { text-transform: uppercase; letter-spacing: 0.1em; font-weight: 800; }
#doneMsg { text-align: center; margin: 0 0 0.4rem; color: var(--ink-soft); }

.toast {
  position: fixed;
  left: 50%; bottom: calc(240px + var(--safe-b));
  transform: translate(-50%, 20px);
  background: var(--ink); color: var(--paper);
  padding: 0.6rem 1rem; border-radius: 999px;
  font-weight: 600; font-size: 0.95rem;
  opacity: 0; pointer-events: none;
  transition: opacity 200ms, transform 200ms var(--ease-bounce);
  max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Animaciones ---------- */
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.3) rotate(-4deg); } 100% { transform: scale(1); } }
@keyframes pop { 0% { transform: scale(1); } 45% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); } 40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); } 80% { transform: translateX(3px); }
}
@keyframes float-up {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.6); }
  20% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -180%) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}

/* Pantallas bajas (teclado del sistema, móviles pequeños) */
@media (max-height: 620px) {
  .key { height: 42px; }
  .board { --size: min(96vw, calc(100dvh - 290px), 500px); }
}
.error:empty { display: none; }

/* ---------- Bienvenida: botón dúo ---------- */
.btn.duo-btn { background: var(--teal); color: #fff; }

/* ---------- Dos jugadores ---------- */
.lbl { font-weight: 800; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; }
.inp {
  font: inherit; font-size: 1.1rem; width: 100%;
  padding: 0.7rem 0.8rem; border: 2px solid var(--line); border-radius: 10px; background: var(--paper);
  outline: none;
}
.inp:focus { border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-soft); }
.hint { margin: 0; color: var(--ink-soft); line-height: 1.4; font-size: 0.95rem; }
.or { display: flex; align-items: center; gap: 0.6rem; color: var(--ink-soft); font-size: 0.85rem; margin: 0.2rem 0; }
.or::before, .or::after { content: ''; flex: 1; border-top: 1.5px dashed #cdbf9f; }
.join-row { display: flex; gap: 0.5rem; }
.join-row .btn { width: auto; flex: none; }
.code-inp { text-transform: uppercase; letter-spacing: 0.4em; font-weight: 800; text-align: center; font-size: 1.4rem; }

.wait { justify-content: center; text-align: center; }
.code-tiles { display: flex; justify-content: center; gap: 0.5rem; margin: 0.5rem 0 1rem; }
.code-tiles span {
  width: 3.6rem; height: 4.2rem; display: grid; place-items: center;
  font-family: var(--font-display); font-weight: 900; font-size: 2.6rem;
  background: #fff; border: 3px solid var(--line); border-radius: 10px;
  box-shadow: 4px 4px 0 var(--teal);
  animation: rise 500ms var(--ease-bounce) both;
}
.code-tiles span:nth-child(2) { animation-delay: 70ms; box-shadow: 4px 4px 0 var(--tomato); }
.code-tiles span:nth-child(3) { animation-delay: 140ms; box-shadow: 4px 4px 0 var(--sun); }
.code-tiles span:nth-child(4) { animation-delay: 210ms; }
.wait-link { word-break: break-all; color: var(--ink-soft); font-size: 0.9rem; margin: 0; }
.waiting { display: flex; align-items: center; justify-content: center; gap: 0.6rem; font-weight: 600; }
.dots { display: inline-flex; gap: 4px; }
.dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--teal); animation: blink 1.2s infinite both; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); } 40% { opacity: 1; transform: scale(1.1); } }

.duo { grid-template-rows: auto auto 1fr auto auto; }
.duo .board { --size: min(96vw, calc(100dvh - 380px), 500px); }
.players { flex: 1; display: flex; gap: 0.4rem; min-width: 0; }
.pchip {
  --pc: var(--tomato);
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 0.35rem;
  padding: 0.3rem 0.5rem;
  border: 2px solid var(--line); border-radius: 12px;
  background: #fff;
  opacity: 0.55;
  transition: opacity 200ms, box-shadow 200ms, transform 200ms var(--ease-bounce);
}
.pchip.p2 { --pc: var(--teal); }
.pchip.active { opacity: 1; box-shadow: 3px 3px 0 var(--pc); transform: translateY(-1px); }
.pchip .dot { width: 8px; height: 8px; flex: none; border-radius: 50%; background: #c9bfae; }
.pchip .dot.on { background: #27b36a; }
.pchip .pname { flex: 1; min-width: 0; font-weight: 800; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pchip .pscore {
  font-family: var(--font-display); font-weight: 900; font-size: 1.25rem;
  color: #fff; background: var(--pc); border-radius: 999px; padding: 0 0.5rem; min-width: 2.2rem; text-align: center;
}
.pchip .pscore.bump { animation: bump 450ms var(--ease-bounce); }

.turnbar {
  --pc: var(--tomato);
  position: relative;
  display: flex; align-items: center; gap: 0.6rem;
  min-height: 44px;
  padding: 0.3rem 0.75rem 0.4rem;
  background: var(--paper-2);
  border-bottom: 2px solid var(--line);
  font-weight: 800;
}
.turnbar.p2 { --pc: var(--teal); }
.turnbar.mine { background: var(--sun-soft); }
.turn-txt { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.turn-time { font-variant-numeric: tabular-nums; }
.turn-time.low { color: var(--tomato); animation: pop 1s infinite; }
.turnbar .pass { width: auto; padding: 0.3rem 0.8rem; font-size: 0.9rem; box-shadow: 2px 2px 0 var(--line); }
.turnbar .bar {
  position: absolute; left: 0; bottom: 0; height: 5px; width: 0;
  background: var(--pc);
  transition: width 250ms linear;
}

.cell.by1 { background: var(--tomato-soft); color: #7a1f10; }
.cell.by2 { background: var(--teal-soft); color: #08493f; }
.cell.by1.in-word { background: #ffc6b0; }
.cell.by2.in-word { background: #b2e6da; }
.cell.by1.active, .cell.by2.active { background: var(--sun); }
#duoBoard[data-opp="1"] .cell.opp { box-shadow: inset 0 0 0 3px var(--tomato); }
#duoBoard[data-opp="2"] .cell.opp { box-shadow: inset 0 0 0 3px var(--teal); }
.cell.ghost { color: var(--ink-soft); font-style: italic; }
.floater.p2 { color: var(--teal); }

.kb-area { position: relative; }
.kb-cover {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  padding: 1rem; text-align: center;
  background: rgba(239, 229, 208, 0.82);
  backdrop-filter: blur(2px);
  font-weight: 800; font-size: 1.05rem;
  border-top: 2px solid var(--line);
}

.clue-cols li.by1 { color: var(--tomato); text-decoration-color: rgba(232, 73, 47, 0.45); }
.clue-cols li.by2 { color: var(--teal); }

.final-scores { display: flex; gap: 0.6rem; }
.fs {
  flex: 1; text-align: center; padding: 0.7rem 0.4rem;
  border: 2px solid var(--line); border-radius: 12px; background: #fff;
}
.fs span { display: block; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fs b { font-family: var(--font-display); font-weight: 900; font-size: 2.6rem; line-height: 1.1; }
.fs.p1 b { color: var(--tomato); }
.fs.p2 b { color: var(--teal); }
.fs.win { box-shadow: 4px 4px 0 var(--sun); border-width: 3px; }
.done-msg { text-align: center; margin: 0.2rem 0 0.4rem; color: var(--ink-soft); }

@media (max-height: 620px) {
  .duo .board { --size: min(96vw, calc(100dvh - 330px), 500px); }
}
