:root {
  --felt: #0f3d2e;
  --felt-dark: #0a2a20;
  --panel: rgba(255, 255, 255, 0.08);
  --panel-strong: rgba(255, 255, 255, 0.16);
  --text: #f4f1e8;
  --muted: rgba(244, 241, 232, 0.65);
  --accent: #f2c94c;
  --danger: #e5484d;
  --slap: #ff5a5f;
  --red: #c8102e;
  --black: #1b1b1b;
  --radius: 10px;
  --sheet-h: 0px;
  /* card sizes: opponents' cards are the smallest, and still 44px wide for the tap target */
  --opp-w: 44px; --opp-h: 48px;
  --own-w: 48px; --own-h: 66px;
  --pile-w: 44px; --pile-h: 60px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 100%);
  color: var(--text);
  overscroll-behavior: none;
  user-select: none;
}
#app { min-height: 100dvh; }
.screen { display: none; min-height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

h1 { margin: 0; font-size: 2.2rem; letter-spacing: 0.02em; }
h2 { margin: 0 0 8px; }
h3 { margin: 18px 0 6px; font-size: 1.05rem; }
.muted { color: var(--muted); margin: 4px 0; }
.small { font-size: 0.8rem; color: var(--muted); }
.error { color: #ffb4b6; min-height: 1.2em; margin: 6px 0 0; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.25em; }
.ghost-link { color: var(--muted); text-decoration: underline; padding: 10px; display: inline-block; }

.card-stack {
  margin: auto;
  width: min(92vw, 380px);
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.card-stack.wide { width: min(100vw - 32px, 560px); }
.rules-page { text-align: left; margin: 0 auto; }
.rules-page h1 { text-align: left; }

label { display: flex; flex-direction: column; gap: 6px; text-align: left; font-size: 0.9rem; color: var(--muted); }
input {
  font: inherit; font-size: 1.1rem;
  padding: 12px 14px; border-radius: var(--radius); border: 1px solid rgba(255,255,255,0.2);
  background: rgba(0,0,0,0.25); color: var(--text); width: 100%;
}
#code { text-transform: uppercase; letter-spacing: 0.2em; font-family: ui-monospace, Menlo, monospace; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; }

button {
  font: inherit; font-size: 1rem; font-weight: 600;
  padding: 10px 16px; border-radius: var(--radius); border: 1px solid rgba(255,255,255,0.25);
  background: var(--panel-strong); color: var(--text); cursor: pointer;
  min-height: 44px;
}
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--accent); color: #2a2200; border-color: transparent; }
button.ghost { background: transparent; }
button.danger { background: var(--danger); border-color: transparent; }
button.icon { width: 44px; height: 44px; padding: 0; border-radius: 50%; font-size: 1.2rem; }

.player-list { list-style: none; padding: 0; margin: 8px 0; display: flex; flex-direction: column; gap: 6px; }
.player-list li { background: var(--panel); border-radius: var(--radius); padding: 10px 14px; display: flex; justify-content: space-between; }
.badge { font-size: 0.7rem; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: #2a2200; font-weight: 700; }
.badge.out { background: #666; color: #fff; }

/* ---------- table ---------- */

#screen-table.active { display: block; }
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 5; height: calc(52px + env(safe-area-inset-top));
  display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: 8px;
  padding: 0 10px; padding-top: env(safe-area-inset-top);
  background: rgba(10, 42, 32, 0.94); backdrop-filter: blur(6px);
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
#topbar #timer { text-align: right; font-variant-numeric: tabular-nums; }
#turn-banner {
  justify-self: center; font-weight: 700; padding: 6px 14px; border-radius: 999px; background: var(--panel-strong);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
#turn-banner.mine { background: var(--accent); color: #2a2200; animation: pulse 1.4s ease-in-out infinite; }
#turn-banner.final { outline: 2px solid var(--danger); }
#turn-banner.out { background: #555; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.04); } }

/* the middle scrolls; the top bar and the bottom sheet do not */
#table-scroll {
  padding: calc(60px + env(safe-area-inset-top)) 8px calc(var(--sheet-h) + 12px);
  min-height: 100dvh;
}

#opponents { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.opp {
  background: var(--panel); border-radius: var(--radius); padding: 6px; border: 2px solid transparent;
  display: flex; flex-direction: column; gap: 4px; align-items: center; min-width: 0;
}
.opp.turn { border-color: var(--accent); }
.opp.offline { opacity: 0.55; }
.opp.out { opacity: 0.5; }
.opp.out .card.back { filter: grayscale(1); }
.opp-name { display: flex; align-items: center; gap: 4px; font-weight: 600; font-size: 0.8rem; width: 100%; min-width: 0; }
.opp-name .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opp-name .dot { width: 7px; height: 7px; border-radius: 50%; background: #4ade80; flex: none; }
.opp.offline .opp-name .dot { background: #999; }
.opp-name .count { margin-left: auto; color: var(--muted); font-weight: 400; font-size: 0.75rem; flex: none; }
.opp-cards { display: grid; grid-template-columns: repeat(2, var(--opp-w)); gap: 3px; }
.opp .card { --card-w: var(--opp-w); --card-h: var(--opp-h); font-size: 0.8rem; }
.opp .card .corner { display: none; }
.cambio-tag { background: var(--danger); color: #fff; font-size: 0.6rem; padding: 1px 5px; border-radius: 999px; font-weight: 700; flex: none; }
.out-tag { background: #555; color: #fff; font-size: 0.6rem; padding: 1px 5px; border-radius: 999px; font-weight: 700; flex: none; }

/* the bottom sheet: everything you can act on lives here, always visible */
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 4;
  background: rgba(10, 42, 32, 0.96); backdrop-filter: blur(6px);
  border-top: 1px solid rgba(255,255,255,0.12);
  padding: 5px 10px calc(6px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 4px;
}
#me { display: flex; justify-content: center; align-items: flex-start; gap: 18px; }
#hand { display: grid; grid-template-columns: repeat(2, var(--own-w)); gap: 6px; }
#hand .card { --card-w: var(--own-w); --card-h: var(--own-h); font-size: 1.15rem; }
#piles { display: flex; flex-direction: column; gap: 4px; }
#piles .card { --card-w: var(--pile-w); --card-h: var(--pile-h); font-size: 0.95rem; }
.pile { display: flex; flex-direction: column; align-items: center; gap: 1px; }
.pile-label { font-size: 0.65rem; color: var(--muted); }
#drawn-pile.hidden { visibility: hidden; }

#prompt {
  padding: 4px 8px; border-radius: var(--radius); background: var(--panel); text-align: center; font-size: 0.85rem;
  min-height: 30px; display: flex; align-items: center; justify-content: center; line-height: 1.2;
}
#prompt:empty { display: none; }
#prompt.mine { background: rgba(242,201,76,0.18); border: 1px solid rgba(242,201,76,0.5); }
#prompt.out { background: rgba(0,0,0,0.3); color: var(--muted); }
#actions { display: flex; flex-wrap: nowrap; gap: 6px; justify-content: center; }
#actions:empty { display: none; }
#actions button { flex: 1 1 0; min-width: 0; padding: 8px 6px; font-size: 0.9rem; white-space: nowrap; }

/* slap countdown */
#snapbar { display: flex; flex-direction: column; gap: 3px; }
#snapbar.hidden { display: none; }
#snapbar .bar { height: 8px; border-radius: 4px; background: rgba(255,255,255,0.15); overflow: hidden; }
#snapbar .fill { height: 100%; width: 100%; background: var(--slap); transition: width 0.1s linear; }
#snapbar .caption { font-size: 0.8rem; font-weight: 700; color: var(--slap); text-align: center; }
#snapbar.suppressed .fill { background: #777; }
#snapbar.suppressed .caption { color: var(--muted); font-weight: 500; }
#snapbar.pending .fill { background: var(--accent); }
#snapbar.pending .caption { color: var(--accent); }

/* cards */
.card {
  width: var(--card-w); height: var(--card-h); border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; position: relative;
  background: #fbf8f0; color: var(--black);
  box-shadow: 0 2px 5px rgba(0,0,0,0.35);
  border: 2px solid transparent;
}
.card.red { color: var(--red); }
.card.joker { font-size: 0.65em; color: #6b21a8; }
.card.back {
  background: repeating-linear-gradient(45deg, #2b4fa8 0 6px, #24428f 6px 12px);
  color: rgba(255,255,255,0.85); font-size: 0.8em;
}
.card.empty { background: rgba(255,255,255,0.05); box-shadow: none; border: 1.5px dashed rgba(255,255,255,0.3); }
.card.placeholder { background: transparent; box-shadow: none; border: 1px dashed rgba(255,255,255,0.15); }
.card.pickable { border-color: var(--accent); cursor: pointer; box-shadow: 0 0 0 3px rgba(242,201,76,0.4), 0 2px 5px rgba(0,0,0,0.35); }
.card.slappable { border-color: var(--slap); cursor: pointer; box-shadow: 0 0 0 3px rgba(255,90,95,0.4), 0 2px 5px rgba(0,0,0,0.35); animation: slapglow 0.7s ease-in-out infinite alternate; }
@keyframes slapglow { from { box-shadow: 0 0 0 2px rgba(255,90,95,0.3), 0 2px 5px rgba(0,0,0,0.35); } to { box-shadow: 0 0 0 4px rgba(255,90,95,0.7), 0 2px 5px rgba(0,0,0,0.35); } }
.card.picked { border-color: #4ade80; }
.card.newcard { animation: arrive 0.6s ease-out 3; }
@keyframes arrive { 0% { transform: scale(1.35) rotate(-6deg); box-shadow: 0 0 0 6px rgba(255,90,95,0.8); } 100% { transform: scale(1); } }
.card .corner { position: absolute; top: 3px; left: 5px; font-size: 0.6em; }
.card .idx { position: absolute; bottom: 2px; right: 4px; font-size: 0.55em; opacity: 0.5; }

#log { margin: 10px 0 0; background: rgba(0,0,0,0.25); border-radius: var(--radius); padding: 6px 10px; font-size: 0.85rem; }
#log summary { cursor: pointer; color: var(--muted); }
#log-list, #scored-log { list-style: none; padding: 0; margin: 6px 0 0; max-height: 200px; overflow: auto; display: flex; flex-direction: column; gap: 4px; }
#log-list li, #scored-log li { color: var(--muted); }
#log-list li:first-child { color: var(--text); }

/* ---------- scored / match over ---------- */
#scored-hands { display: flex; flex-direction: column; gap: 10px; text-align: left; }
.scored-player { background: var(--panel); border-radius: var(--radius); padding: 10px; }
.scored-player .row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; gap: 8px; }
.scored-player .cards { display: flex; flex-wrap: wrap; gap: 6px; }
.scored-player .cards .card { --card-w: 44px; --card-h: 62px; font-size: 0.9rem; }
.scored-player.winner { outline: 2px solid var(--accent); }
.scored-player.loser { outline: 2px solid var(--danger); }
#scored-table { width: 100%; border-collapse: collapse; margin-top: 8px; }
#scored-table th, #scored-table td { padding: 8px 6px; border-bottom: 1px solid rgba(255,255,255,0.12); text-align: left; }
#scored-table td:last-child, #scored-table th:last-child, #scored-table td:nth-child(2), #scored-table th:nth-child(2) { text-align: right; font-variant-numeric: tabular-nums; }
#scored-table tr.out td { color: var(--muted); text-decoration: line-through; }

/* ---------- rules ---------- */
#rules-dialog { border: none; border-radius: 14px; padding: 0; width: min(94vw, 560px); max-height: 86dvh; background: #12352a; color: var(--text); }
#rules-dialog::backdrop { background: rgba(0,0,0,0.6); }
.rules-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid rgba(255,255,255,0.12); position: sticky; top: 0; background: #12352a; }
.rules { padding: 4px 16px 20px; text-align: left; line-height: 1.45; font-size: 0.95rem; }
.rules ul { padding-left: 18px; }
.rules li { margin: 4px 0; }
.rules-table { border-collapse: collapse; width: 100%; margin: 6px 0; }
.rules-table td { padding: 5px 8px; border-bottom: 1px solid rgba(255,255,255,0.12); }
.rules-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--sheet-h) + 12px); transform: translateX(-50%);
  background: #1b1b1b; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 0.9rem;
  opacity: 0; transition: opacity 0.2s; pointer-events: none; z-index: 20; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; }
#toast.bad { background: var(--danger); }

/* ---------- larger screens ---------- */
@media (min-width: 700px) {
  :root { --opp-w: 52px; --opp-h: 70px; --own-w: 72px; --own-h: 100px; --pile-w: 60px; --pile-h: 84px; }
  #opponents { grid-template-columns: repeat(auto-fit, minmax(150px, 190px)); justify-content: center; gap: 10px; }
  #me { gap: 40px; }
  #sheet { padding-left: 24px; padding-right: 24px; }
  #actions button { flex: 0 1 auto; }
}
@media (max-height: 700px) {
  :root { --own-w: 44px; --own-h: 60px; --pile-w: 40px; --pile-h: 56px; }
}
