:root {
  --felt: #1f5a3c;
  --felt-dark: #17452e;
  --ink: #f4f1e8;
  --muted: rgba(244, 241, 232, .65);
  --accent: #f2c94c;
  --sel: #4fc3f7;
  --red: #c51e2b;
  --green: #0b8a4a;
  --blue: #057bb8;
  --card-ratio: 0.65; /* Breite / Höhe einer Karte im Sprite (260 x 400) */
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--felt-dark);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

/* ---------- Tisch-Layout (Hochformat zuerst) ---------- */
.table {
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 64px 1fr 64px;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto auto;
  grid-template-areas:
    "bar   bar   bar"
    ".     top   ."
    "left  trick right"
    "sel   sel   sel"
    "hand  hand  hand"
    "act   act   act";
  background: radial-gradient(ellipse at 50% 40%, #2b7550 0%, var(--felt) 55%, var(--felt-dark) 100%);
  overflow: hidden;
}

.topbar {
  grid-area: bar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 0;
  font-size: 14px;
}
.score { display: flex; gap: 14px; min-width: 0; white-space: nowrap; }
.tools { display: flex; gap: 6px; }
.iconbtn.tichu { background: var(--red); border-color: transparent; font-weight: 700; }
[hidden] { display: none !important; }
.team b { font-variant-numeric: tabular-nums; }
.team.us b { color: var(--accent); }
.iconbtn {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: rgba(0, 0, 0, .25);
  border: 1px solid rgba(255, 255, 255, .2);
  border-radius: 999px;
  padding: 6px 12px;
  min-height: 32px;
}

/* ---------- Karten aus dem Sprite ---------- */
.card {
  --w: 60px;
  width: var(--w);
  height: calc(var(--w) / var(--card-ratio));
  border-radius: calc(var(--w) * .075);
  background-image: url("cards.png");
  background-size: 1500% 400%; /* 15 Spalten, 4 Zeilen */
  background-repeat: no-repeat;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .45);
  flex: none;
}

/* ---------- Mitspieler ---------- */
.seat {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  min-width: 0;
}
.seat > * { flex: none; }
.seat-top { grid-area: top; flex-direction: column; padding-top: 4px; }
.seat-left, .seat-right {
  flex-direction: column;
  justify-content: center;
  text-align: center;
}
.seat-left { grid-area: left; }
.seat-right { grid-area: right; }

.seat .name {
  max-width: 64px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}
.seat-top .name { max-width: none; }
.seat { position: relative; }
.seat .name { transition: color .3s; }
.seat.active .name { color: var(--accent); }
.seat.active .backs { animation: glow 1.4s ease-in-out infinite; }
@keyframes glow {
  0%, 100% { filter: drop-shadow(0 0 2px rgba(242, 201, 76, .4)); }
  50% { filter: drop-shadow(0 0 9px rgba(242, 201, 76, .95)); }
}
.bubble {
  position: absolute;
  left: 50%;
  top: 40%;
  z-index: 50;
  padding: 5px 10px;
  border-radius: 12px;
  background: #fff;
  color: #1b1b1b;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .35);
  transform: translateX(-50%);
  pointer-events: none;
}
.btn { transition: transform .1s, opacity .2s, background-color .2s; }

.badge {
  display: inline-block;
  min-width: 22px;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgba(0, 0, 0, .35);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.tag {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .04em;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(0, 0, 0, .35);
  white-space: nowrap;
}
.tag.red { background: var(--red); }
.tag.gold { background: var(--accent); color: #1b1b1b; }
.tag.ok { background: var(--green); }
.seat .info { display: flex; flex-wrap: wrap; justify-content: center; gap: 3px; max-width: 100%; }
.seat-me { grid-area: hand; flex-direction: column; padding: 6px 0 10px; }

/* Kartenrücken: gleicher Fächer für alle, links und rechts gedreht (Masse setzt app.js) */
.backs { position: relative; flex: none; }
.backs .fan { position: absolute; left: 0; top: 0; }
.backs .card { --w: 26px; position: absolute; }

/* ---------- Stich in der Mitte ---------- */
.trick {
  grid-area: trick;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 0;
}
.move {
  position: absolute;
  left: 50%;
  top: 58%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.move .cards { display: flex; }
.move .who { font-size: 12px; color: var(--muted); }
.move.old { opacity: .6; }
.move.old .who { display: none; }
.move.last .who { color: var(--ink); font-weight: 600; }
.trick .empty { color: var(--muted); font-size: 14px; }
.trick .collect {
  position: absolute;
  bottom: 4px;
  font-size: 12px;
  color: var(--accent);
}
.wishchip {
  grid-area: trick;
  align-self: start;
  justify-self: center;
  z-index: 20;
  margin-top: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--accent);
  color: #1b1b1b;
  font-size: 12px;
  font-weight: 700;
}

/* ---------- Auswahl-Leiste ---------- */
.selbar {
  grid-area: sel;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 12px;
  font-size: 14px;
}
.selinfo { color: var(--muted); }
.selinfo.valid { color: var(--ink); font-weight: 600; }
.selinfo.invalid { color: #ffb3b3; }
.selbar.slots { min-height: 64px; }
.slotrow { display: flex; gap: 8px; width: 100%; justify-content: center; }
.slot {
  font: inherit;
  font-size: 11px;
  color: var(--ink);
  flex: 1;
  max-width: 110px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 10px;
  border: 1px dashed rgba(255, 255, 255, .35);
  background: rgba(0, 0, 0, .2);
  min-width: 0;
}
.slot span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot.filled { border-style: solid; border-color: var(--sel); }
.slot .card { --w: 26px; }
.slotempty { width: 26px; height: 40px; flex: none; border-radius: 3px; border: 1px dashed rgba(255, 255, 255, .3); }
.clearbtn {
  font: inherit;
  color: var(--ink);
  background: rgba(0, 0, 0, .3);
  border: 0;
  border-radius: 50%;
  width: 32px;
  height: 32px;
}

/* ---------- Eigene Hand ---------- */
.hand {
  grid-area: hand;
  position: relative;
  touch-action: none; /* Wischen wählt Karten statt zu scrollen */
  margin: 0 auto;
  width: 100%;
}
.hand .card {
  position: absolute;
  /* leichtes Überschwingen beim Anheben; left/top gleiten beim Sortieren und Nachrücken */
  transition: transform .22s cubic-bezier(.34, 1.56, .64, 1), box-shadow .15s ease,
    left .35s cubic-bezier(.2, .8, .2, 1), top .35s cubic-bezier(.2, .8, .2, 1);
  cursor: pointer;
  transform: translateY(var(--dy, 0px)) rotate(var(--rot, 0rad));
}
.hand .card.sel {
  transform: translateY(calc(var(--dy, 0px) - var(--lift))) rotate(var(--rot, 0rad));
  box-shadow: 0 0 0 3px var(--sel), 0 6px 12px rgba(0, 0, 0, .45);
}
/* Untere Reihe hebt sich weniger, damit die obere Reihe antippbar bleibt */
.hand .card.sel[data-row="1"] {
  transform: translateY(calc(var(--dy, 0px) - var(--lift) * .4)) rotate(var(--rot, 0rad));
  box-shadow: 0 0 0 3px var(--sel), 0 6px 12px rgba(0, 0, 0, .45);
}

.hand .card[data-tag]::after {
  content: attr(data-tag);
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 22px;
  height: 22px;
  line-height: 22px;
  text-align: center;
  border-radius: 50%;
  background: var(--sel);
  color: #10324a;
  font-weight: 700;
  font-size: 14px;
}
.hand .card.fresh { box-shadow: 0 0 0 3px var(--accent), 0 0 14px rgba(242, 201, 76, .8); }

/* ---------- Aktionen ---------- */
.actions {
  grid-area: act;
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 10px;
  padding: 8px 12px calc(10px + var(--safe-b));
}
.btn {
  font: inherit;
  font-size: 17px;
  font-weight: 600;
  min-height: 52px;
  border: 0;
  border-radius: 14px;
  color: #fff;
}
.btn.pass { background: var(--blue); }
.btn.play { background: var(--green); }
.btn.red { background: var(--red); }
.btn.ghost { background: transparent; border: 1px solid rgba(255, 255, 255, .35); }
.btn.mini { font-size: 13px; min-height: 32px; padding: 0 10px; border-radius: 999px; background: rgba(0, 0, 0, .3); }
.btn.wide { grid-column: 1 / -1; }
.table.myturn .btn.play:not(:disabled) { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(242, 201, 76, 0); }
  50% { box-shadow: 0 0 0 4px rgba(242, 201, 76, .55); }
}
.btn:disabled { opacity: .4; }
.btn:active:not(:disabled) { transform: scale(.98); }

/* ---------- Start und Lobby ---------- */
.screen {
  min-height: 100dvh;
  max-width: 460px;
  margin: 0 auto;
  padding: 24px 16px calc(24px + var(--safe-b));
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: radial-gradient(ellipse at 50% 20%, #2b7550 0%, var(--felt) 60%, var(--felt-dark) 100%);
  -webkit-user-select: text;
  user-select: text;
}
.logo { margin: 8px 0 4px; text-align: center; font-size: 40px; letter-spacing: .08em; color: var(--accent); }
.screen h2 { margin: 0; font-size: 16px; }
.box { display: flex; flex-direction: column; gap: 10px; padding: 14px; border-radius: 16px; background: rgba(0, 0, 0, .22); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.field.inline { flex-direction: row; align-items: center; gap: 8px; }
.screen input, .screen select {
  font: inherit;
  font-size: 17px;
  color: #1b1b1b;
  background: #fbfaf6;
  border: 0;
  border-radius: 10px;
  padding: 12px;
}
.screen select { padding: 8px; }
.lobbyhead { display: flex; align-items: center; justify-content: space-between; font-size: 18px; }
.code { font-size: 26px; letter-spacing: .12em; color: var(--accent); font-family: ui-monospace, monospace; }
.hint { margin: 0; color: var(--muted); font-size: 14px; }
.lobbytable {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-areas: ". top ." "left mid right" ". me .";
  gap: 8px;
  padding: 8px;
  border-radius: 20px;
  background: rgba(0, 0, 0, .15);
}
.lobbytable::before { content: "Tisch"; grid-area: mid; align-self: center; text-align: center; color: var(--muted); font-size: 13px; }
.lseat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 4px;
  border-radius: 14px;
  background: rgba(0, 0, 0, .25);
  text-align: center;
  min-width: 0;
}
.lseat b { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lseat small { color: var(--muted); font-size: 11px; min-height: 13px; }
.lseat.free { background: transparent; border: 1px dashed rgba(255, 255, 255, .3); }
.lseat.free b { color: var(--muted); font-weight: 400; }
.lseat.you { box-shadow: 0 0 0 2px var(--accent); }
.l-top { grid-area: top; }
.l-left { grid-area: left; }
.l-right { grid-area: right; }
.l-me { grid-area: me; }

/* ---------- Dialoge, Hinweise ---------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(0, 0, 0, .45);
}
.sheetcard {
  width: 100%;
  max-width: 560px;
  padding: 18px 16px calc(16px + var(--safe-b));
  border-radius: 20px 20px 0 0;
  background: #20402f;
  box-shadow: 0 -6px 20px rgba(0, 0, 0, .4);
}
.sheetcard h2 { margin: 0 0 8px; font-size: 20px; }
.sheetbody { color: var(--muted); font-size: 15px; margin-bottom: 14px; }
.sheetbtns { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 10px; }
.sheetbtns.grid { grid-auto-flow: row; grid-template-columns: repeat(5, 1fr); }
.sheetbtns.grid .btn { min-height: 46px; }
.sum { width: 100%; border-collapse: collapse; color: var(--ink); font-variant-numeric: tabular-nums; }
.sum td, .sum th { padding: 6px 4px; text-align: right; }
.sum td:first-child { text-align: left; }
.sum th { color: var(--muted); font-weight: 400; font-size: 13px; }
.toasts {
  position: fixed;
  top: 52px;
  left: 50%;
  z-index: 200;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  width: max-content;
  max-width: 92vw;
}
.toast {
  padding: 8px 14px;
  border-radius: 12px;
  background: rgba(20, 20, 20, .88);
  font-size: 14px;
  text-align: center;
  animation: toastIn .2s ease-out;
  transition: opacity .3s, transform .3s;
}
.toast.err { background: #8b1d25; }
.toast.out { opacity: 0; transform: translateY(-6px); }
@keyframes toastIn { from { opacity: 0; transform: translateY(-8px); } }
.offline {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 300;
  padding: 10px calc(10px + var(--safe-b));
  background: #8b1d25;
  text-align: center;
  font-size: 14px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Querformat / Desktop: etwas mehr Luft für die Seiten */
@media (min-width: 600px) {
  .table { grid-template-columns: 90px 1fr 90px; }
}
