:root {
  --felt: #1f5c3f;
  --felt-dark: #154330;
  --ink: #f3f1e8;
  --muted: #b9c9bf;
  --gold: #f2c14e;
  --red: #d8333a;
  --black: #1b1b1f;
  --panel: rgba(0, 0, 0, .28);
  --card-w: min(15vw, 64px);
  --card-h: calc(var(--card-w) * 1.42);
  --mini-w: min(8.5vw, 34px);
  --mini-h: calc(var(--mini-w) * 1.42);
  --safe-top: var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px));
  --safe-bottom: var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 40%, var(--felt) 0%, var(--felt-dark) 85%);
  color: var(--ink);
  font: 15px/1.35 -apple-system, "Segoe UI", Roboto, system-ui, sans-serif;
  overflow: hidden;
  user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
#app { display: flex; flex-direction: column; height: 100%; padding-top: var(--safe-top); }

#topbar { display: flex; align-items: center; justify-content: space-between; padding: 6px 12px; background: var(--panel); }
#title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 4px; }
.icon-btn { position: relative; background: none; border: 0; font-size: 20px; padding: 4px 8px; border-radius: 8px; }
.icon-btn:active { background: rgba(255,255,255,.1); }
#chat-badge { position: absolute; top: 0; right: 0; min-width: 16px; height: 16px; border-radius: 8px; background: var(--red); font-size: 11px; line-height: 16px; text-align: center; padding: 0 3px; }
#call-banner { background: #7a1d22; text-align: center; padding: 5px; font-size: 13px; }
#call-banner a { color: #fff; font-weight: 600; }

#screen { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; padding: 0 16px calc(8px + var(--safe-bottom)); }

/* --- лобби --- */
.lobby { margin: auto 0; display: flex; flex-direction: column; gap: 14px; align-items: center; text-align: center; }
.lobby h2 { margin: 0; }
.seats { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.seat-box { width: 100px; padding: 12px 6px; border-radius: 12px; background: var(--panel); display: flex; flex-direction: column; gap: 6px; align-items: center; }
.avatar { width: 44px; height: 44px; border-radius: 50%; background: #2f7a55; display: grid; place-items: center; font-weight: 700; overflow: hidden; flex: none; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.btn { background: var(--gold); color: #222; border: 0; border-radius: 10px; padding: 10px 18px; font-weight: 600; }
.btn.ghost { background: rgba(255,255,255,.12); color: var(--ink); }
.btn:disabled { opacity: .4; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.hint { color: var(--muted); font-size: 13px; max-width: 340px; }
.seg { display: inline-flex; background: var(--panel); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: none; padding: 6px 12px; border-radius: 8px; }
.seg button.on { background: var(--gold); color: #222; font-weight: 600; }

/* --- стол --- */
.table { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr auto auto; min-height: 0; gap: 6px; }
.opponents { display: flex; justify-content: space-between; gap: 8px; padding-top: 8px; }
.opp { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; position: relative; }
.opp.right { align-items: flex-end; text-align: right; }
.pl-head { display: flex; align-items: center; gap: 6px; max-width: 100%; }
.opp.right .pl-head { flex-direction: row-reverse; }
.pl-head .avatar { width: 34px; height: 34px; font-size: 13px; }
.pl-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl-sub { font-size: 12px; color: var(--muted); }
.turn .avatar { box-shadow: 0 0 0 3px var(--gold); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px rgba(242,193,78,.4); } }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: rgba(255,255,255,.14); margin: 0 2px; }
.tag.gold { background: var(--gold); color: #222; }
.backs { display: flex; }
.back { width: calc(var(--mini-w) * .7); height: calc(var(--mini-h) * .7); border-radius: 3px; background: repeating-linear-gradient(45deg, #8b1e2b 0 3px, #a8283a 3px 6px); border: 1px solid #fff5; margin-left: -12px; }
.back:first-child { margin-left: 0; }
.mini-hand { display: flex; flex-wrap: wrap; gap: 2px; max-width: 100%; }
.opp.right .mini-hand { justify-content: flex-end; }

.center { position: relative; display: grid; place-items: center; min-height: 170px; }
.contract-info { position: absolute; top: 0; left: 50%; transform: translateX(-50%); font-size: 13px; color: var(--muted); white-space: nowrap; }
.trick { position: relative; width: calc(var(--card-w) * 3.2); height: calc(var(--card-h) * 1.9); }
.trick .card { position: absolute; transition: transform .2s; }
.trick .pos-bottom { left: 50%; bottom: 0; transform: translateX(-50%); }
.trick .pos-left { left: 0; top: 10%; transform: rotate(-8deg); }
.trick .pos-right { right: 0; top: 10%; transform: rotate(8deg); }
.trick .pos-top { left: 50%; top: 0; transform: translateX(-50%) scale(.85); opacity: .85; }
.trick .winner { box-shadow: 0 0 0 3px var(--gold); }
.talon { display: flex; gap: 6px; }
.talon-label { font-size: 12px; color: var(--muted); text-align: center; margin-bottom: 4px; }

/* --- карты --- */
.card {
  width: var(--card-w); height: var(--card-h); border-radius: 7px; background: #fffdf7; color: var(--black);
  box-shadow: 0 1px 4px rgba(0,0,0,.45); position: relative; flex: none;
  display: flex; flex-direction: column; padding: 3px 4px; font-weight: 700;
}
.card.red { color: var(--red); }
.card .r { font-size: calc(var(--card-w) * .3); line-height: 1; }
.card .s { font-size: calc(var(--card-w) * .28); line-height: 1; }
.card .big { position: absolute; right: 4px; bottom: 2px; font-size: calc(var(--card-w) * .5); line-height: 1; }
.card.mini { width: var(--mini-w); height: var(--mini-h); border-radius: 4px; padding: 1px 2px; }
.card.mini .r { font-size: calc(var(--mini-w) * .36); }
.card.mini .s { font-size: calc(var(--mini-w) * .34); }
.card.mini .big { display: none; }
.card.legal { cursor: pointer; }
.card.dim { filter: brightness(.6); }
.card.sel { transform: translateY(-14px); box-shadow: 0 0 0 3px var(--gold), 0 4px 10px rgba(0,0,0,.5); }

.hand-wrap { padding: 16px 0 4px; }
.hand { display: flex; justify-content: center; min-width: 0; }
.hand .card { transition: transform .15s; }
.hand .card.legal:hover, .hand .card.legal:active { transform: translateY(-10px); }
.me-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; position: relative; }

/* --- панель действий --- */
.actions { background: var(--panel); border-radius: 12px; padding: 8px; display: flex; flex-direction: column; gap: 6px; align-items: center; }
.actions .msg { font-size: 14px; text-align: center; }
.bid-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; width: 100%; max-width: 380px; }
.bid-grid button { border: 0; border-radius: 7px; padding: 6px 0; background: #fffdf7; color: var(--black); font-weight: 700; font-size: 14px; }
.bid-grid button.red { color: var(--red); }
.bid-grid button:disabled { opacity: .25; }
.bid-grid button.on { outline: 3px solid var(--gold); }
.bid-grid .wide { grid-column: span 5; }
.bid-log { font-size: 12px; color: var(--muted); text-align: center; }

.result { text-align: center; }
.result h3 { margin: 0 0 4px; }
.result div { font-size: 13px; }

/* --- эмоции --- */
.emote-float { position: absolute; font-size: 34px; animation: floatUp 2.2s ease-out forwards; pointer-events: none; z-index: 5; }
@keyframes floatUp { from { opacity: 0; transform: translateY(10px) scale(.6); } 15% { opacity: 1; transform: scale(1.1); } to { opacity: 0; transform: translateY(-50px); } }

/* --- выезжающие панели --- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 78%; z-index: 20;
  background: #102e21; border-radius: 16px 16px 0 0; box-shadow: 0 -6px 30px rgba(0,0,0,.5);
  display: flex; flex-direction: column; padding: 0 16px calc(10px + var(--safe-bottom));
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; }
#chat-panel { height: 70%; }
#chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding-bottom: 6px; user-select: text; }
.cmsg { background: rgba(255,255,255,.08); border-radius: 10px; padding: 6px 10px; max-width: 85%; align-self: flex-start; white-space: pre-wrap; word-break: break-word; }
.cmsg.mine { align-self: flex-end; background: #2d6b4b; }
.cmsg.sys { align-self: center; background: none; color: var(--muted); font-size: 12px; text-align: center; max-width: 100%; }
.cmsg .who { font-size: 12px; color: var(--gold); font-weight: 600; }
#emote-bar { display: flex; gap: 4px; justify-content: space-between; padding: 6px 0; }
#emote-bar button { background: rgba(255,255,255,.08); border: 0; border-radius: 8px; font-size: 22px; flex: 1; padding: 4px 0; }
#chat-form { display: flex; gap: 6px; }
#chat-input { flex: 1; border-radius: 10px; border: 0; padding: 10px; font: inherit; background: #fff; color: #111; }
#chat-form button { border: 0; border-radius: 10px; background: var(--gold); color: #222; padding: 0 16px; font-size: 18px; }

#score-body { overflow-y: auto; }
.sheet-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.sheet-table th, .sheet-table td { padding: 6px 4px; border-bottom: 1px solid rgba(255,255,255,.1); text-align: center; }
.sheet-table th:first-child, .sheet-table td:first-child { text-align: left; }
.pos { color: #8fe3a8; } .neg { color: #ff9b9b; }
.history { font-size: 12px; color: var(--muted); margin-top: 10px; }
.history div { padding: 3px 0; border-bottom: 1px dashed rgba(255,255,255,.08); }

#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: rgba(0,0,0,.8); padding: 8px 14px; border-radius: 10px; z-index: 30; font-size: 14px; max-width: 90%; text-align: center; }
.tag .red, .bid-log .red, .msg .red, .contract-info .red { color: #ff7b7b; }
.tag.gold .red, .bid-grid .red { color: var(--red); }
.tag.off { background: #7a1d22; }
.lobby .seg button { padding: 6px 9px; font-size: 13px; }
.opp .btn.small { align-self: flex-start; }
.opp.right .btn.small { align-self: flex-end; }
