:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #0e1015);
  --bg-secondary: var(--tg-theme-secondary-bg-color, #171a21);
  --bg-elevated: #1e222b;
  --text: var(--tg-theme-text-color, #f2f3f5);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #6ab7ff);
  --button: var(--tg-theme-button-color, #3b82f6);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --accent: #f5b642;
  --accent-2: #ff9f5a;
  --danger: #ff5a5f;
  --success: #34d399;
  --card-radius: 16px;
  --radius-lg: 20px;
  --radius-sm: 10px;
  --shadow-card: 0 2px 6px rgba(0, 0, 0, 0.28), 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-pop: 0 10px 30px rgba(0, 0, 0, 0.45);
}

:root[data-theme="light"] {
  --bg-elevated: #ffffff;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  padding: 12px 12px calc(24px + env(safe-area-inset-bottom));
  gap: 12px;
  background:
    radial-gradient(600px 300px at 100% 0%, rgba(59, 130, 246, 0.10), transparent 60%),
    radial-gradient(500px 260px at 0% 0%, rgba(245, 182, 66, 0.08), transparent 55%);
}

#content {
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: viewIn 0.28s ease both;
}
@keyframes viewIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.center-msg {
  margin: auto;
  text-align: center;
  color: var(--hint);
  padding: 24px;
  line-height: 1.5;
}

.spinner {
  width: 30px;
  height: 30px;
  margin: 0 auto 12px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.12);
  border-top-color: var(--accent);
  animation: spin 0.75s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* --- шапка стану --- */
.turn-banner {
  position: sticky;
  top: 4px;
  z-index: 40;
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  gap: 12px;
  overflow: hidden;
  box-shadow: var(--shadow-card), 0 8px 16px rgba(0, 0, 0, 0.35);
}
.turn-banner::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.4), rgba(245, 182, 66, 0.35));
  transition: opacity 0.3s ease;
}
.turn-banner.your-turn::before {
  opacity: 1;
}
.turn-banner.your-turn {
  box-shadow: 0 0 0 1.5px rgba(245, 182, 66, 0.6) inset, var(--shadow-card);
  animation: bannerPulse 2.2s ease-in-out infinite;
}
@keyframes bannerPulse {
  0%, 100% { box-shadow: 0 0 0 1.5px rgba(245, 182, 66, 0.6) inset, var(--shadow-card); }
  50% { box-shadow: 0 0 0 1.5px rgba(245, 182, 66, 0.95) inset, 0 0 18px rgba(245, 182, 66, 0.25), var(--shadow-card); }
}
.turn-banner-icon {
  position: relative;
  font-size: 26px;
  line-height: 1;
  flex-shrink: 0;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
}
.turn-banner-text {
  position: relative;
  flex: 1;
  min-width: 0;
  font-weight: 800;
  font-size: 16px;
  letter-spacing: 0.01em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* --- секції --- */
.section-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--hint);
  margin: 6px 2px 2px;
}
.section-title::before {
  content: "";
  width: 3px;
  height: 12px;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  flex-shrink: 0;
}

.card-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(126px, 1fr));
  gap: 9px;
}

/* --- ігрова картка --- */
.card {
  position: relative;
  border-radius: var(--card-radius);
  padding: 11px;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 3px;
  color: #fff;
  overflow: hidden;
  background: var(--bg-secondary);
  box-shadow: var(--shadow-card);
  animation: cardIn 0.22s ease both;
}
@keyframes cardIn {
  from { opacity: 0; transform: scale(0.94); }
  to { opacity: 1; transform: scale(1); }
}
.card::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0) 45%);
  pointer-events: none;
}
.card .card-name {
  font-weight: 800;
  font-size: 16px;
  line-height: 1.25;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}
.card .card-meta {
  font-size: 13.5px;
  font-weight: 600;
  opacity: 0.92;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}
.card .card-meta.tappable {
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}
.card .card-meta.tappable:active {
  opacity: 0.65;
}
.card .card-price {
  align-self: flex-end;
  font-weight: 800;
  font-size: 13px;
  color: #1a1200;
  background: linear-gradient(135deg, #ffd27a, var(--accent));
  padding: 3px 9px;
  border-radius: 999px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  margin-top: 2px;
}
.card .chip {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px);
  padding: 2px 7px;
  border-radius: 999px;
  margin: 2px 4px 0 0;
}
.card.locked {
  filter: grayscale(0.6);
  opacity: 0.6;
}

/* --- гараж / список гравців --- */
.player-rows {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.player-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  font-size: 13px;
  box-shadow: var(--shadow-card);
  transition: box-shadow 0.2s ease;
}
.player-row.active {
  box-shadow: 0 0 0 1.5px var(--accent) inset, 0 0 14px rgba(245, 182, 66, 0.2);
}
.player-row .info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.player-row .name {
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.player-row .stats {
  color: var(--hint);
  font-size: 11.5px;
}
.player-row.tappable {
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.2s ease;
}
.player-row.tappable:active {
  transform: scale(0.98);
}
.player-row .chip {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  flex-shrink: 0;
}

.avatar {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
}

.finished-banner {
  border-radius: var(--radius-lg);
  padding: 22px 16px;
  text-align: center;
  background: linear-gradient(135deg, rgba(245, 182, 66, 0.4), rgba(52, 211, 153, 0.22));
  box-shadow: var(--shadow-card);
  font-weight: 700;
  font-size: 15px;
  line-height: 1.5;
}

.error-toast {
  position: fixed;
  left: 12px;
  right: 12px;
  /* вище за липку панель дії (action-bar) знизу екрана, щоб не перекривати
     кнопку "Закінчити хід"/"Кинути кубик" */
  bottom: calc(84px + env(safe-area-inset-bottom));
  background: var(--danger);
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-pop);
  z-index: 80;
  animation: toastIn 0.25s cubic-bezier(0.2, 0.9, 0.3, 1.2) both;
}
.error-toast.toast-ok {
  background: var(--success);
  color: #06231a;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

/* --- кнопки --- */
.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 13px 18px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.12s ease, filter 0.15s ease;
}
.btn:active {
  transform: scale(0.97);
  filter: brightness(0.94);
}
.btn-block {
  width: 100%;
}
.btn-primary {
  background: linear-gradient(135deg, var(--button), #2f6fe0);
  color: var(--button-text);
  box-shadow: 0 4px 14px rgba(59, 130, 246, 0.35);
}
.btn-secondary {
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08) inset;
}
.btn-danger {
  background: transparent;
  color: var(--danger);
  box-shadow: 0 0 0 1px var(--danger) inset;
}
.btn-small {
  padding: 7px 13px;
  font-size: 12px;
  border-radius: 999px;
}

/* --- рядок адмін-дій (напр. дострокове завершення гри) — маленька кнопка
   вгорі екрана, подалі від ігрових кнопок знизу, щоб не зачепити випадково --- */
.admin-row {
  display: flex;
  justify-content: flex-end;
}

/* --- панель головної дії (закінчити хід / кинути кубик) — приклеєна до низу
   екрана, щоб завжди була під рукою, не треба гортати сторінку --- */
.action-bar {
  position: sticky;
  bottom: calc(10px + env(safe-area-inset-bottom));
  z-index: 45;
  padding-top: 4px;
}
.action-bar .btn {
  box-shadow: var(--shadow-card), 0 10px 22px rgba(0, 0, 0, 0.45);
}

/* --- інтерактивні картки --- */
.card.tappable {
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.2s ease;
}
.card.tappable:active {
  transform: scale(0.95);
}
.card-row.dim {
  opacity: 0.45;
  filter: saturate(0.6);
  pointer-events: none;
}

.icon-btn {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 23px;
  height: 23px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  text-shadow: none;
  box-shadow: none;
}
.upg-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 2px;
}
.chip-removable {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.chip.tappable {
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
}
.chip.tappable:active {
  transform: scale(0.95);
  background: rgba(255, 255, 255, 0.12);
}
.icon-btn-tiny {
  position: static;
  width: 15px;
  height: 15px;
  font-size: 11px;
}

/* --- створення/приєднання до лобі --- */
.join-row {
  display: flex;
  gap: 8px;
}
.code-input {
  flex: 1;
  min-width: 0;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08) inset;
}
.code-input:focus {
  outline: none;
  box-shadow: 0 0 0 1.5px var(--accent) inset;
}

.lobby-code-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 8px 16px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.25), rgba(245, 182, 66, 0.2));
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08) inset;
}

/* --- оверлей-пікер (вибір цілі/покупця тощо) --- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-end;
  z-index: 60;
  animation: overlayIn 0.2s ease both;
}
@keyframes overlayIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
.overlay-panel {
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 16px 14px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.4);
  animation: sheetIn 0.28s cubic-bezier(0.2, 0.9, 0.3, 1.1) both;
}
@keyframes sheetIn {
  from { transform: translateY(24px); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}
.overlay-panel::before {
  content: "";
  align-self: center;
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  margin-bottom: 2px;
}

/* --- анімація кидка кубика й перевороту картки сюрпризу --- */
.dice-overlay {
  align-items: center;
  justify-content: center;
}

/* Справжній 3D-кубик: 6 граней куба, що обертається CSS-трансформацією і
   зупиняється на реальному числі, яке вже визначив сервер. */
.die-scene {
  width: 92px;
  height: 92px;
  perspective: 700px;
  filter: drop-shadow(0 10px 22px rgba(0, 0, 0, 0.55));
}
.die {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 1.5s cubic-bezier(0.15, 0.85, 0.25, 1);
}
.die-face {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 13px;
  border-radius: 14px;
  background: linear-gradient(150deg, #ffffff, #d7d9de);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.die-dot {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  align-self: center;
  justify-self: center;
  background: radial-gradient(circle at 35% 35%, #4a4a4a, #101010);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.15);
}
.die-face-1 { transform: translateZ(46px); }
.die-face-2 { transform: rotateY(90deg) translateZ(46px); }
.die-face-3 { transform: rotateX(-90deg) translateZ(46px); }
.die-face-4 { transform: rotateX(90deg) translateZ(46px); }
.die-face-5 { transform: rotateY(-90deg) translateZ(46px); }
.die-face-6 { transform: rotateY(180deg) translateZ(46px); }

.flip-card {
  width: min(88vw, 300px);
  height: min(60vh, 340px);
  perspective: 900px;
}
.flip-card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.6s cubic-bezier(0.4, 0.2, 0.2, 1);
  transform-style: preserve-3d;
}
.flip-card.flipped .flip-card-inner {
  transform: rotateY(180deg);
}
.flip-card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: var(--radius-lg);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  font-weight: 700;
  box-shadow: var(--shadow-pop);
  overflow-y: auto;
  word-break: break-word;
}
.flip-card-front {
  font-size: 76px;
  background: linear-gradient(135deg, #454b59, #1c1f26);
  color: #fff;
}
.flip-card-back {
  transform: rotateY(180deg);
  align-items: flex-start;
  text-align: left;
  font-size: 15px;
  line-height: 1.5;
  background: linear-gradient(160deg, var(--bg-elevated), var(--bg-secondary));
  color: var(--text);
}
