/* ═══════════════════════════════════════════════════════════════
   «Стол» — мини-апп Лавки Игр.
   Тактильный вид: сукно, дерево, тяжёлые предметы под палец.
   Работает поверх темы Telegram, но не растворяется в ней —
   стол должен выглядеть столом в любом клиенте.
   ═══════════════════════════════════════════════════════════════ */

:root {
  /* Сукно */
  --felt:        #1d4536;
  --felt-deep:   #143026;
  --felt-lit:    #26614a;
  /* Дерево борта */
  --wood:        #4a3320;
  --wood-lit:    #6b4a2e;
  /* Кость и бумага */
  --bone:        #f2ece0;
  --bone-shade:  #cfc5b2;
  --ink:         #23201b;
  /* Акцент Лавки — тёплая киноварь */
  --accent:      #c8552f;
  --accent-lit:  #e0693f;
  /* Метрики */
  --r:           14px;
  --drawer-h:    64px;
  --safe-b:      env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--bone);
  background: var(--felt-deep);
  user-select: none;
  -webkit-user-select: none;
}

/* ── Стол ──────────────────────────────────────────────────── */

#table {
  position: fixed;
  inset: 0 0 calc(var(--drawer-h) + var(--safe-b)) 0;
  padding: 18px 16px 8px;
  display: flex;
  overflow: hidden;

  /* Сукно: тканое переплетение + мягкая подсветка от центра */
  background-color: var(--felt);
  background-image:
    radial-gradient(ellipse 80% 55% at 50% 30%, rgba(255,255,255,.09), transparent 70%),
    repeating-linear-gradient(45deg,  rgba(0,0,0,.045) 0 1px, transparent 1px 3px),
    repeating-linear-gradient(-45deg, rgba(0,0,0,.045) 0 1px, transparent 1px 3px);
  /* Борт стола */
  box-shadow:
    inset 0 0 0 6px var(--wood),
    inset 0 0 0 7px rgba(0,0,0,.35),
    inset 0 14px 34px rgba(0,0,0,.42);
}

/* Инструмент занимает стол целиком */
.tool {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  min-height: 0;
}
.tool[hidden] { display: none; }

/* ── Переключатели (тип кости, шаг, пресеты) ───────────────── */

.picker {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  flex: 0 0 auto;
}
.picker button {
  min-width: 52px;
  padding: 9px 11px;
  font: 600 15px/1 inherit;
  color: var(--bone);
  background: linear-gradient(180deg, rgba(255,255,255,.13), rgba(0,0,0,.16));
  border: 1px solid rgba(0,0,0,.34);
  border-radius: 10px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.16), 0 2px 4px rgba(0,0,0,.28);
  cursor: pointer;
  transition: transform .09s, background .16s;
}
.picker.small button { min-width: 44px; padding: 7px 9px; font-size: 14px; }
.picker button:active { transform: translateY(1px); }
.picker button.on {
  background: linear-gradient(180deg, var(--accent-lit), var(--accent));
  border-color: rgba(0,0,0,.5);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.28), 0 2px 8px rgba(200,85,47,.42);
}

/* ── Главная кнопка действия ───────────────────────────────── */

.act {
  flex: 0 0 auto;
  min-width: 190px;
  padding: 15px 26px;
  font: 700 17px/1 inherit;
  letter-spacing: .2px;
  color: #fff;
  background: linear-gradient(180deg, var(--accent-lit), var(--accent));
  border: 1px solid rgba(0,0,0,.42);
  border-radius: var(--r);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.3), 0 4px 12px rgba(0,0,0,.4);
  cursor: pointer;
  transition: transform .09s, filter .16s;
}
.act:active { transform: translateY(2px); filter: brightness(.94); }
.act.ghost {
  min-width: 0;
  padding: 11px 18px;
  font-size: 15px;
  color: var(--bone);
  background: linear-gradient(180deg, rgba(255,255,255,.11), rgba(0,0,0,.16));
  box-shadow: inset 0 1px 0 rgba(255,255,255,.14), 0 2px 6px rgba(0,0,0,.26);
}
.row { display: flex; gap: 10px; align-items: center; flex: 0 0 auto; }

/* ── Кости: настоящий 3D-куб с точками ─────────────────────── */

.tray {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 18px;
  padding: 6px;
  perspective: 800px;
}

.die {
  --s: 80px;                 /* ребро куба; половина уходит в translateZ */
  width: var(--s); height: var(--s);
  position: relative;
  transform-style: preserve-3d;
}

.die__face {
  position: absolute;
  width: var(--s); height: var(--s);
  background: linear-gradient(155deg, #fff 0%, var(--bone) 46%, var(--bone-shade) 100%);
  border: 1px solid rgba(0,0,0,.18);
  border-radius: 12px;
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.95),
    inset 0 -3px 5px rgba(0,0,0,.16);
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  gap: 4px;
  padding: 10px;
  backface-visibility: hidden;   /* иначе сквозь куб видны точки дальних граней */
}

/* Тень на сукне под костью: отдельный слой, иначе в 3D она повисает
   на каждой грани и куб выглядит как ворох карточек. */
.die::after {
  content: '';
  position: absolute;
  left: 8%; right: 8%;
  bottom: -10px;
  height: 12px;
  border-radius: 50%;
  background: radial-gradient(ellipse, rgba(0,0,0,.5), transparent 72%);
  transform: translateZ(calc(var(--s) / -2 - 1px));
  animation: shade .9s cubic-bezier(.32,.72,.36,1) var(--d, 0ms) both;
}
@keyframes shade {
  0%   { opacity: .12; transform: translateZ(calc(var(--s) / -2 - 1px)) scale(.6); }
  70%  { opacity: .5;  transform: translateZ(calc(var(--s) / -2 - 1px)) scale(1); }
  100% { opacity: .5;  transform: translateZ(calc(var(--s) / -2 - 1px)) scale(1); }
}

.die__dot {
  width: 12px; height: 12px;
  background: var(--ink);
  border-radius: 50%;
  box-shadow: inset 0 1px 2px rgba(0,0,0,.4);
}

/* Расстановка точек — классика */
.die__face--1 .die__dot { grid-area: 2/2; }
.die__face--2 .die__dot:nth-child(1) { grid-area: 1/1; }
.die__face--2 .die__dot:nth-child(2) { grid-area: 3/3; }
.die__face--3 .die__dot:nth-child(1) { grid-area: 1/1; }
.die__face--3 .die__dot:nth-child(2) { grid-area: 2/2; }
.die__face--3 .die__dot:nth-child(3) { grid-area: 3/3; }
.die__face--4 .die__dot:nth-child(1) { grid-area: 1/1; }
.die__face--4 .die__dot:nth-child(2) { grid-area: 1/3; }
.die__face--4 .die__dot:nth-child(3) { grid-area: 3/1; }
.die__face--4 .die__dot:nth-child(4) { grid-area: 3/3; }
.die__face--5 .die__dot:nth-child(1) { grid-area: 1/1; }
.die__face--5 .die__dot:nth-child(2) { grid-area: 1/3; }
.die__face--5 .die__dot:nth-child(3) { grid-area: 2/2; }
.die__face--5 .die__dot:nth-child(4) { grid-area: 3/1; }
.die__face--5 .die__dot:nth-child(5) { grid-area: 3/3; }
.die__face--6 .die__dot:nth-child(1) { grid-area: 1/1; }
.die__face--6 .die__dot:nth-child(2) { grid-area: 1/3; }
.die__face--6 .die__dot:nth-child(3) { grid-area: 2/1; }
.die__face--6 .die__dot:nth-child(4) { grid-area: 2/3; }
.die__face--6 .die__dot:nth-child(5) { grid-area: 3/1; }
.die__face--6 .die__dot:nth-child(6) { grid-area: 3/3; }

/* Геометрия куба: грань 1 смотрит вперёд, 6 назад (сумма противоположных = 7) */
.die__face--1 { transform: translateZ(calc(var(--s) / 2)); }
.die__face--6 { transform: rotateY(180deg) translateZ(calc(var(--s) / 2)); }
.die__face--2 { transform: rotateY(90deg)  translateZ(calc(var(--s) / 2)); }
.die__face--5 { transform: rotateY(-90deg) translateZ(calc(var(--s) / 2)); }
.die__face--3 { transform: rotateX(90deg)  translateZ(calc(var(--s) / 2)); }
.die__face--4 { transform: rotateX(-90deg) translateZ(calc(var(--s) / 2)); }

/* Куда довернуть куб, чтобы нужная грань смотрела в камеру */
.die[data-face="1"] { --rx: 0deg;   --ry: 0deg;   }
.die[data-face="2"] { --rx: 0deg;   --ry: -90deg; }
.die[data-face="3"] { --rx: -90deg; --ry: 0deg;   }
.die[data-face="4"] { --rx: 90deg;  --ry: 0deg;   }
.die[data-face="5"] { --rx: 0deg;   --ry: 90deg;  }
.die[data-face="6"] { --rx: 0deg;   --ry: 180deg; }

/* Бросок: кость подлетает, кувыркается и садится на свою грань.
   Оборота считаем от целевого угла, чтобы посадка была точной. */
@keyframes roll {
  0% {
    transform: translateY(-46px) rotateX(calc(var(--rx) - 720deg))
                                 rotateY(calc(var(--ry) - 540deg));
  }
  70% {
    transform: translateY(0) rotateX(calc(var(--rx) + 14deg))
                             rotateY(calc(var(--ry) + 10deg));
  }
  85% {
    transform: translateY(-7px) rotateX(calc(var(--rx) - 5deg))
                                rotateY(calc(var(--ry) - 3deg));
  }
  100% {
    transform: translateY(0) rotateX(var(--rx)) rotateY(var(--ry));
  }
}

.die {
  animation: roll .9s cubic-bezier(.32,.72,.36,1) var(--d, 0ms) both;
}

.total {
  flex: 0 0 auto;
  margin: 0;
  min-height: 22px;
  font: 600 16px/1.3 inherit;
  color: rgba(242,236,224,.72);
}
.total b { color: var(--bone); font-size: 19px; }

/* ── Первый игрок: круги под пальцами ──────────────────────── */

.pad {
  position: relative;
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  border-radius: var(--r);
  border: 1px dashed rgba(242,236,224,.2);
  display: grid;
  place-items: center;
  touch-action: none;   /* иначе браузер съест мультитач жестами */
}
.hint {
  margin: 0;
  text-align: center;
  font: 600 17px/1.5 inherit;
  color: rgba(242,236,224,.6);
  pointer-events: none;
  transition: opacity .2s;
}
.hint span { font-weight: 400; font-size: 14px; color: rgba(242,236,224,.42); }
.pad.busy .hint { opacity: 0; }

.finger {
  position: absolute;
  width: 92px; height: 92px;
  margin: -46px 0 0 -46px;
  border-radius: 50%;
  pointer-events: none;
  border: 3px solid var(--c, var(--bone));
  background: color-mix(in srgb, var(--c, var(--bone)) 20%, transparent);
  box-shadow: 0 0 22px color-mix(in srgb, var(--c, var(--bone)) 55%, transparent);
  animation: breathe 1.5s ease-in-out infinite;
}
@keyframes breathe {
  0%, 100% { transform: scale(1);    opacity: .92; }
  50%      { transform: scale(1.09); opacity: 1; }
}
/* Победитель остаётся, остальные гаснут */
.finger.won {
  animation: chosen .6s cubic-bezier(.3,1.4,.5,1) both;
  border-width: 5px;
  z-index: 2;
}
@keyframes chosen {
  0%   { transform: scale(1); }
  60%  { transform: scale(1.5); }
  100% { transform: scale(1.34); }
}
.finger.lost { animation: none; opacity: 0; transition: opacity .4s; }

/* ── Таймер: песочные часы кольцом ─────────────────────────── */

.glass {
  position: relative;
  flex: 0 1 auto;
  width: min(62vw, 232px);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
}
.glass svg { position: absolute; inset: 0; transform: rotate(-90deg); }
.glass circle { fill: none; stroke-width: 13; }
.glass .track { stroke: rgba(0,0,0,.3); }
.glass .run {
  stroke: var(--accent);
  stroke-linecap: round;
  stroke-dasharray: 553;      /* 2πr, r=88 */
  stroke-dashoffset: 0;
  transition: stroke-dashoffset .25s linear, stroke .3s;
}
.glass.low .run { stroke: #e8b437; }
.glass.out .run { stroke: #d9453c; }

.clock {
  font: 700 46px/1 "Georgia", serif;
  font-variant-numeric: tabular-nums;
  color: var(--bone);
  text-shadow: 0 2px 6px rgba(0,0,0,.45);
}
.glass.out .clock { color: #ff8a82; animation: buzz .45s 3; }
@keyframes buzz {
  0%,100% { transform: translateX(0); }
  25%     { transform: translateX(-5px); }
  75%     { transform: translateX(5px); }
}

/* ── Счёт: лист бумаги на сукне ────────────────────────────── */

.sheet {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  margin: 0;
  padding: 10px;
  list-style: none;
  overflow-y: auto;
  border-radius: var(--r);
  background: linear-gradient(180deg, #fbf7ee, #efe8d8);
  box-shadow: inset 0 2px 8px rgba(0,0,0,.2), 0 3px 10px rgba(0,0,0,.32);
}
.sheet li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 6px;
  border-bottom: 1px solid rgba(35,32,27,.13);
}
.sheet li:last-child { border-bottom: 0; }

.sheet .who {
  flex: 1 1 auto;
  min-width: 0;
  padding: 7px 10px;
  font: 600 15px/1.3 inherit;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 7px;
}
.sheet .who:focus { outline: 2px solid var(--accent); background: #fff; }

.sheet .pts {
  min-width: 48px;
  text-align: center;
  font: 700 22px/1 "Georgia", serif;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.sheet .pm {
  width: 38px; height: 38px;
  flex: 0 0 auto;
  font: 700 20px/1 inherit;
  color: #fff;
  background: var(--ink);
  border: 0;
  border-radius: 9px;
  cursor: pointer;
  touch-action: manipulation;
}
.sheet .pm:active { transform: scale(0.95); }
.sheet .pm.minus { background: #8a8175; }
.sheet .del {
  width: 32px; height: 32px;
  flex: 0 0 auto;
  font-size: 16px;
  color: rgba(35,32,27,.4);
  background: none;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}

/* Адаптация для узких экранов */
@media (max-width: 340px) {
  .sheet li { gap: 6px; padding: 7px 4px; }
  .sheet .who { padding: 6px 8px; font-size: 14px; }
  .sheet .pts { min-width: 42px; font-size: 20px; }
  .sheet .pm { width: 36px; height: 36px; font-size: 18px; }
  .sheet .del { width: 28px; height: 28px; font-size: 15px; }
}

/* ── Ящик стола: нижняя навигация ──────────────────────────── */

#drawer {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--drawer-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: flex;
  background: linear-gradient(180deg, var(--wood-lit), var(--wood));
  box-shadow: 0 -3px 14px rgba(0,0,0,.5), inset 0 1px 0 rgba(255,255,255,.14);
}
#drawer button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: rgba(242,236,224,.56);
  background: none;
  border: 0;
  cursor: pointer;
  transition: color .18s, background .18s;
}
#drawer button i { font-size: 21px; font-style: normal; line-height: 1; }
#drawer button b { font: 600 11px/1 inherit; letter-spacing: .3px; }
#drawer button.on {
  color: #fff;
  background: linear-gradient(180deg, rgba(200,85,47,.34), transparent);
  box-shadow: inset 0 2px 0 var(--accent);
}

/* Тесный экран — не даём инструментам разъехаться */
@media (max-height: 620px) {
  .die   { --s: 58px; }
  .die__face { padding: 7px; gap: 3px; }
  .die__dot  { width: 9px; height: 9px; }
  .clock { font-size: 38px; }
  .glass { width: min(52vw, 190px); }
  #table { padding-top: 12px; }
}

/* Уважаем системную настройку «меньше движения»:
   кость сразу стоит нужной гранью, без полёта и кувырков. */
@media (prefers-reduced-motion: reduce) {
  .die {
    animation: none;
    transform: rotateX(var(--rx)) rotateY(var(--ry));
  }
  .die::after { animation: none; opacity: .5; }
  .finger, .finger.won, .glass.out .clock { animation: none; }
}
