:root {
  --night: #1b1340;
  --night-2: #2b1a54;
  --night-3: #391f57;
  --paper: #fbf3e4;
  --ink: #e7e0ff;
  --muted: #9f94c9;
  --gold: #ffd76a;
  --font: ui-rounded, "Nunito", "Baloo 2", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

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

body {
  font-family: var(--font);
  color: var(--ink);
  background: radial-gradient(ellipse 600px 400px at 50% 0%, rgba(167, 139, 250, 0.18), transparent 60%),
    linear-gradient(170deg, var(--night), var(--night-2) 55%, var(--night-3));
  background-attachment: fixed;
  min-height: 100vh;
  min-height: 100dvh;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100vh;
  min-height: 100dvh;
}

/* La mesa de juego ocupa exactamente la pantalla visible (con la barra del
   navegador mostrada u oculta) y nunca hace scroll de página: el historial
   vive en su propia ventana. */
#app.game {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}
#app.game > * { flex-shrink: 0; }

h1, h2, h3, p { margin: 0; }
h1 { font-size: 30px; font-weight: 800; color: var(--gold); text-shadow: 0 0 24px rgba(255, 215, 106, 0.5); text-align: center; }
h2 { font-size: 22px; font-weight: 800; text-align: center; color: var(--paper); }
.sub { font-size: 14px; color: var(--muted); text-align: center; font-weight: 600; }

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; }
button:focus-visible, input:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.btn {
  border: 2px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  padding: 14px 18px;
  min-height: 48px;
  font-weight: 800;
  font-size: 16px;
  color: var(--paper);
}
.btn.primary { background: var(--gold); border-color: var(--gold); color: #4a3a00; box-shadow: 0 8px 20px rgba(255, 215, 106, 0.3); }
.btn.quiet { background: transparent; border-color: transparent; color: var(--muted); font-size: 14px; }
.btn:disabled { opacity: 0.45; box-shadow: none; }

.panel { background: rgba(255, 255, 255, 0.06); border-radius: 18px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.panel h3 { font-size: 13px; font-weight: 800; color: var(--gold); }
.spacer { flex: 1; }
.center { align-items: center; text-align: center; }

.topbar { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 6px; }
.pill { font-size: 12px; font-weight: 800; color: #c9bfea; background: rgba(255, 255, 255, 0.08); padding: 5px 12px; border-radius: 999px; }
.pill.warn { color: #4a1400; background: #ff8a5c; }
.pill-btn { border: 2px solid rgba(255, 255, 255, 0.22); min-height: 32px; color: var(--paper); }

.code-input {
  width: 100%;
  text-align: center;
  letter-spacing: 0.5em;
  text-transform: uppercase;
  font: 800 26px var(--font);
  color: var(--gold);
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-radius: 14px;
  padding: 12px 8px 12px 18px;
}

.alias-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.alias-btn {
  display: flex; align-items: center; gap: 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 2px solid rgba(255, 255, 255, 0.18);
  border-radius: 14px; padding: 10px 12px; min-height: 52px; text-align: left; font-weight: 800; font-size: 14px;
}
.alias-btn:disabled { opacity: 0.4; }
.dot { position: relative; width: 32px; height: 32px; border-radius: 50%; background: var(--c); flex-shrink: 0; display: grid; place-items: center; font-weight: 800; font-size: 13px; color: rgba(0, 0, 0, 0.6); }
.dot.away { outline: 2px dashed rgba(255, 255, 255, 0.6); outline-offset: 2px; }
.dot.away::before { content: ""; position: absolute; inset: 0; border-radius: 50%; background: rgba(27, 19, 64, 0.55); }
.badge-off { position: absolute; right: -6px; bottom: -6px; width: 18px; height: 18px; border-radius: 50%; background: #ff8a5c; border: 2px solid var(--night-2); display: grid; place-items: center; }
.badge-off svg { width: 11px; height: 11px; }

.roster { display: flex; flex-direction: column; gap: 8px; }
.roster-row { display: flex; align-items: center; gap: 10px; font-weight: 800; }
.roster-row .tag { font-size: 11px; color: var(--muted); font-weight: 700; margin-left: auto; }
.big-code { font-size: 34px; letter-spacing: 0.3em; font-weight: 800; color: var(--gold); padding-left: 0.3em; }

.tracker { display: flex; justify-content: space-between; }
.chip { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 38px; }
.rank { width: 24px; height: 24px; border-radius: 50%; background: var(--c); color: rgba(0, 0, 0, 0.65); font-weight: 800; font-size: 12px; display: grid; place-items: center; }
.rank.gone { background: rgba(255, 255, 255, 0.12); color: var(--muted); }
.pips { display: flex; gap: 2px; }
.pip { width: 5px; height: 5px; border-radius: 50%; background: var(--c); }
.pip.gone { background: rgba(255, 255, 255, 0.16); }
.legend { font-size: 10.5px; color: var(--muted); font-weight: 600; }

.strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); gap: 8px; }
.pchip { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px; border-radius: 14px; background: rgba(255, 255, 255, 0.05); font-size: 11px; font-weight: 700; text-align: center; }
.pchip.turn { background: rgba(255, 215, 106, 0.15); box-shadow: 0 0 0 2px var(--gold); }
.pchip.out { opacity: 0.45; }
.pchip.away { background: rgba(255, 138, 92, 0.1); }
.pchip.turn.away { box-shadow: 0 0 0 2px #ff8a5c; }
.pchip .state.off { color: #ffb58f; font-weight: 800; }
.pchip .name { line-height: 1.1; }
.pchip .state { font-size: 10.5px; color: var(--muted); }
.pchip .state.shield { color: #7be0a8; }

.status { text-align: center; font-weight: 800; font-size: 17px; color: var(--gold); min-height: 26px; }
.status.warn { color: #ffb58f; }
.status.urgent { color: #ff8a5c; }

.lastmove { display: flex; flex-direction: column; gap: 2px; text-align: left; background: rgba(255, 255, 255, 0.06); border: 0; border-radius: 14px; padding: 8px 12px; min-height: 44px; }
.lm-label { font-size: 10.5px; font-weight: 800; color: var(--gold); text-transform: uppercase; letter-spacing: 0.04em; }
.lm-text { font-size: 13px; font-weight: 600; color: var(--ink); line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.who { color: var(--c); font-weight: 800; }

.hand-zone { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }
.hand { display: flex; justify-content: center; gap: 12px; flex-wrap: wrap; }
.card {
  --c: #ffd76a;
  position: relative;
  width: clamp(104px, 38vw, 148px); min-height: clamp(150px, 26dvh, 206px);
  background: var(--paper);
  color: #2a2233;
  border: 5px solid var(--c);
  border-radius: 20px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.4);
  padding: 12px 8px 10px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  text-align: center;
}
.card-wrap { position: relative; }
.info-btn { position: absolute; right: -8px; bottom: -8px; width: 32px; height: 32px; border-radius: 50%; border: 2px solid var(--night-2); background: var(--gold); color: #4a3a00; font-weight: 800; font-size: 17px; line-height: 1; z-index: 1; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4); }
.info-btn::after { content: ""; position: absolute; inset: -7px; }

.card:disabled { filter: saturate(0.5) brightness(0.85); }
.card.locked { opacity: 0.4; }
.card.ready { transform: translateY(-4px); box-shadow: 0 0 22px var(--c), 0 10px 24px rgba(0, 0, 0, 0.4); }
.card .rank { position: absolute; top: 8px; left: 8px; width: 26px; height: 26px; font-size: 13px; }
.card .pips { position: absolute; top: 14px; right: 10px; gap: 3px; }
.card .pip { width: 6px; height: 6px; }
.card .cname { font-weight: 800; font-size: 16px; }
.card .chelp { font-size: 11.5px; line-height: 1.25; color: #4b4257; font-weight: 600; }
.card svg { width: clamp(44px, 9dvh, 66px); height: clamp(44px, 9dvh, 66px); filter: drop-shadow(0 0 8px var(--c)); }

.hint { font-size: 12px; color: var(--muted); text-align: center; font-weight: 700; }

.modal-backdrop { position: fixed; inset: 0; height: 100vh; height: 100dvh; background: rgba(10, 6, 30, 0.72); display: grid; place-items: center; padding: 16px; z-index: 5; }
.modal { width: min(440px, 100%); max-height: 92vh; max-height: 92dvh; overflow: auto; background: linear-gradient(170deg, var(--night-2), var(--night-3)); border: 2px solid rgba(255, 255, 255, 0.15); border-radius: 22px; padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.choices { display: flex; flex-direction: column; gap: 10px; }
.choice { display: flex; align-items: center; gap: 12px; text-align: left; background: rgba(255, 255, 255, 0.07); border: 2px solid var(--c, rgba(255, 255, 255, 0.2)); border-radius: 16px; padding: 12px 14px; min-height: 56px; font-weight: 800; }
.choice:disabled { opacity: 0.45; }
.choice small { display: block; font-weight: 600; color: var(--muted); font-size: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.guess { display: flex; flex-direction: column; align-items: center; gap: 2px; background: var(--paper); color: #2a2233; border: 4px solid var(--c); border-radius: 16px; padding: 8px 4px; font-weight: 800; }
.guess svg { width: 40px; height: 40px; }
.guess span { font-size: 11px; }
.reveal-card { width: 130px; margin: 0 auto; background: var(--paper); color: #2a2233; border: 5px solid var(--c); border-radius: 18px; padding: 12px; display: flex; flex-direction: column; align-items: center; gap: 6px; font-weight: 800; }
.reveal-card svg { width: 70px; height: 70px; }

.scorerow { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 13px; }
.scorerow .pips { gap: 4px; margin-left: auto; }
.scorerow.won { color: var(--gold); }
.win-tag { font-size: 11px; font-weight: 800; color: var(--gold); background: rgba(255, 215, 106, 0.16); padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.scorerow .pip { width: 12px; height: 12px; }

#toast { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #ff8a5c; color: #3a1400; font-weight: 800; padding: 12px 18px; border-radius: 14px; max-width: 90vw; z-index: 10; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); }
#toast[hidden] { display: none; }


.log-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; max-height: 60vh; max-height: 60dvh; overflow-y: auto; overscroll-behavior: contain; }
.log-item { display: flex; align-items: center; gap: 10px; background: rgba(255, 255, 255, 0.06); border-radius: 14px; padding: 8px 10px; }
.log-item.latest { box-shadow: 0 0 0 2px var(--gold); }
.log-item .dot { width: 28px; height: 28px; font-size: 12px; }
.log-text { flex: 1; font-size: 13px; font-weight: 600; line-height: 1.35; }
.log-icon { width: 30px; height: 30px; flex-shrink: 0; }
.log-icon svg { width: 100%; height: 100%; filter: drop-shadow(0 0 5px var(--c)); }

@media (prefers-reduced-motion: no-preference) {
  .pchip.turn.away { animation: waiting 1.6s ease-in-out infinite; }
}
@keyframes waiting { 50% { box-shadow: 0 0 0 4px rgba(255, 138, 92, 0.35); } }

/* Pantallas bajas (celulares chicos o con teclado/barras abiertas): se
   compacta lo secundario para que la mesa siga entrando sin scroll. */
@media (max-height: 720px) {
  #app { gap: 10px; }
  .legend, .card .chelp { display: none; }
  .card { min-height: clamp(130px, 24dvh, 180px); }
  .pchip { padding: 6px 4px; }
}
@media (max-height: 580px) {
  .panel h3 { display: none; }
  .hint, .lastmove .lm-label { display: none; }
  .card .cname { font-size: 14px; }
}

.info-list { display: flex; flex-direction: column; gap: 10px; max-height: 58vh; max-height: 58dvh; overflow-y: auto; overscroll-behavior: contain; }
.info-row { display: flex; gap: 12px; align-items: flex-start; background: rgba(255, 255, 255, 0.06); border-left: 5px solid var(--c); border-radius: 14px; padding: 10px 12px; }
.info-icon { width: 44px; height: 44px; flex-shrink: 0; }
.info-icon svg { width: 100%; height: 100%; filter: drop-shadow(0 0 6px var(--c)); }
.info-text p { margin: 2px 0 0; font-size: 13px; font-weight: 600; line-height: 1.35; }
.info-text small { color: var(--muted); font-weight: 700; }
.info-long { font-size: 15px; font-weight: 600; line-height: 1.4; text-align: center; }

.result { display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; border-radius: 16px; padding: 14px 12px; }
.result-title { font-size: 26px; font-weight: 800; line-height: 1.15; }
.result-sub { font-size: 14px; font-weight: 600; color: var(--ink); }
.result.win { background: rgba(255, 215, 106, 0.16); box-shadow: 0 0 0 2px var(--gold), 0 0 26px rgba(255, 215, 106, 0.35); }
.result.win .result-title { color: var(--gold); }
.result.lose { background: rgba(255, 255, 255, 0.06); box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.18); }
.result.lose .result-title { color: var(--paper); }

.gap-24 { height: 24px; }
.gap-40 { height: 40px; }
.link { align-self: center; color: var(--muted); font-size: 13px; font-weight: 700; text-decoration: underline; padding: 8px; }
.link:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.back { align-self: flex-start; }
.rules p { font-size: 14px; font-weight: 600; line-height: 1.45; }
.info-list.open { max-height: none; overflow: visible; }

.compare { display: flex; gap: 12px; justify-content: center; }
.compare .reveal-card { width: 122px; margin: 0; }
.reveal-card small { font-size: 11px; font-weight: 800; color: #5a4f6a; }

.recent { font-size: 13px; font-weight: 600; line-height: 1.4; color: var(--muted); }
.recent.latest { color: var(--ink); font-weight: 700; }
