:root {
  --bg: #140d2e;
  --gold: #e8c37a;
  --gold-soft: rgba(232, 195, 122, 0.35);
  --text: #f3ecff;
  --muted: #b3a6d4;
  --radius: 10px;
  --card-back:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cg fill='none' stroke='%23e8c37a' stroke-width='1.6'%3E%3Ccircle cx='50' cy='50' r='31' opacity='.75'/%3E%3Ccircle cx='50' cy='50' r='23' opacity='.4'/%3E%3C/g%3E%3Cpath d='M50 17 L55.5 44.5 L83 50 L55.5 55.5 L50 83 L44.5 55.5 L17 50 L44.5 44.5 Z' fill='%23e8c37a'/%3E%3Ccircle cx='50' cy='50' r='4' fill='%23140d2e'/%3E%3C/svg%3E") center / 58% no-repeat,
    radial-gradient(circle at 50% 50%, rgba(232, 195, 122, 0.22), transparent 62%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.035) 0 5px, transparent 5px 10px),
    linear-gradient(160deg, #2c1d63, #120b2b);
}

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

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(120% 80% at 50% 0%, #2b1b60 0%, var(--bg) 55%, #0a0718 100%) fixed;
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* Звёздное небо */
.sky {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(1.5px 1.5px at 12% 18%, #fff, transparent),
    radial-gradient(1px 1px at 28% 72%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 47% 33%, #fff, transparent),
    radial-gradient(1px 1px at 63% 12%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 78% 58%, #fff, transparent),
    radial-gradient(1px 1px at 88% 26%, #fff, transparent),
    radial-gradient(1px 1px at 8% 86%, #fff, transparent),
    radial-gradient(1.5px 1.5px at 92% 88%, #fff, transparent),
    radial-gradient(1px 1px at 38% 94%, #fff, transparent),
    radial-gradient(1px 1px at 56% 64%, #fff, transparent);
  opacity: 0.55;
  animation: twinkle 6s ease-in-out infinite alternate;
}

@keyframes twinkle { from { opacity: 0.35; } to { opacity: 0.7; } }

.app {
  position: relative;
  max-width: 520px;
  margin: 0 auto;
  padding: 18px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.header { text-align: center; }

.header h1 {
  margin: 0;
  font: 600 22px/1.2 "Palatino Linotype", Palatino, Georgia, serif;
  letter-spacing: 0.02em;
  color: var(--gold);
}

.subtitle { margin: 6px 0 0; min-height: 1.4em; color: var(--muted); font-size: 14px; }

/* Переключатель режимов */
.modes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(232, 195, 122, 0.18);
}

.mode {
  border: 0;
  border-radius: 999px;
  padding: 9px 8px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  transition: background 0.25s, color 0.25s;
}

.mode.active { background: rgba(232, 195, 122, 0.18); color: var(--text); }

/* Позиции расклада */
.slots { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }

/* При выборе своих карт главное — колода внизу, поэтому позиции расклада компактнее */
.app[data-mode="pick"] .slots { width: 78%; margin: 0 auto; }

/* Длинные названия переносятся на две строки — выравниваем кнопки под картами */
.app[data-mode="pick"] .slot-name { min-height: 3.9em; }

.slot { display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 0; }

.slot-position { font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }

.slot-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 7 / 12;
  border-radius: var(--radius);
  border: 1.5px dashed var(--gold-soft);
  background: rgba(255, 255, 255, 0.03);
}

.slot-frame.filled { border-color: transparent; background: none; }

.slot-name { min-height: 2.8em; font-size: 12.5px; line-height: 1.3; text-align: center; }

.slot-name small { display: block; color: var(--muted); font-size: 11px; }

.slot-controls { display: flex; gap: 8px; }

.slot-controls[hidden] { display: none; }

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid rgba(232, 195, 122, 0.4);
  background: rgba(255, 255, 255, 0.05);
  font-size: 15px;
  line-height: 1;
}

/* Карта: рубашка и лицо, переворот через 3D */
.card { position: absolute; inset: 0; perspective: 1000px; }

.card.flying { position: fixed; z-index: 50; }

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.75s cubic-bezier(0.2, 0.75, 0.25, 1);
}

.card.flipped .card-inner { transform: rotateY(180deg); }

.card-face {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  overflow: hidden;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45);
}

.card-back, .deck-card {
  background: var(--card-back);
  border: 1px solid rgba(232, 195, 122, 0.65);
  box-shadow: inset 0 0 0 4px #150d31, inset 0 0 0 5px rgba(232, 195, 122, 0.45), 0 8px 22px rgba(0, 0, 0, 0.45);
}

.card-front { transform: rotateY(180deg); background: #e9e1cf; }

.card-front img { display: block; width: 100%; height: 100%; object-fit: cover; transition: transform 0.5s ease; }

.card.reversed .card-front img { transform: rotate(180deg); }

.card.revealed .card-front { animation: glow 1.6s ease-out; }

.card.popping { animation: pop 0.35s ease-out; }

.card.leaving { transition: opacity 0.3s, transform 0.3s; opacity: 0; transform: translateY(14px) scale(0.96); }

@keyframes glow {
  0% { box-shadow: 0 0 0 0 rgba(232, 195, 122, 0), 0 8px 22px rgba(0, 0, 0, 0.45); }
  30% { box-shadow: 0 0 30px 6px rgba(232, 195, 122, 0.6), 0 8px 22px rgba(0, 0, 0, 0.45); }
  100% { box-shadow: 0 0 0 0 rgba(232, 195, 122, 0), 0 8px 22px rgba(0, 0, 0, 0.45); }
}

@keyframes pop { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.spark {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 3;
  width: 6px;
  height: 6px;
  margin: -3px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 8px 2px rgba(232, 195, 122, 0.8);
  pointer-events: none;
  animation: spark 0.85s ease-out forwards;
}

@keyframes spark {
  from { transform: translate(0, 0) scale(1); opacity: 1; }
  to { transform: translate(var(--dx), var(--dy)) scale(0.2); opacity: 0; }
}

/* Колода */
.panel { display: flex; flex-direction: column; align-items: center; gap: 12px; }

.panel[hidden] { display: none; }

.deck {
  position: relative;
  width: min(34vw, 140px);
  aspect-ratio: 7 / 12;
  margin: 18px auto 4px;
  outline: none;
  animation: float 4s ease-in-out infinite;
}

.deck::before {
  content: "";
  position: absolute;
  inset: -22%;
  z-index: -1;
  background: radial-gradient(closest-side, rgba(232, 195, 122, 0.28), transparent);
  filter: blur(6px);
}

.deck-card { position: absolute; inset: 0; border-radius: var(--radius); transform: var(--base); }

.deck.empty { opacity: 0.3; animation: none; pointer-events: none; }

.deck.shuffling { animation: none; }

.deck.shuffling .deck-card:nth-child(odd) { animation: riffle-left 0.52s ease-in-out 2; }

.deck.shuffling .deck-card:nth-child(even) { animation: riffle-right 0.52s ease-in-out 2; }

@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }

@keyframes riffle-left {
  0%, 100% { transform: var(--base); }
  45% { transform: translate(-62%, -5%) rotate(-14deg); }
}

@keyframes riffle-right {
  0%, 100% { transform: var(--base); }
  45% { transform: translate(62%, -5%) rotate(14deg); }
}

.hint { margin: 0; min-height: 1.4em; color: var(--muted); font-size: 13.5px; text-align: center; animation: breathe 2.4s ease-in-out infinite; }

@keyframes breathe { 0%, 100% { opacity: 0.6; } 50% { opacity: 1; } }

.actions { display: flex; gap: 10px; }

.btn {
  border: 1px solid rgba(232, 195, 122, 0.45);
  border-radius: 999px;
  padding: 10px 18px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 14px;
}

.btn[hidden] { display: none; }

.btn.primary {
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(180deg, #f1d08f, #c99d4f);
  color: #1b1236;
  font-weight: 600;
}

/* Выбор своих карт */
.suits { display: flex; gap: 8px; width: 100%; overflow-x: auto; padding: 2px 2px 8px; scrollbar-width: none; }

.suits::-webkit-scrollbar { display: none; }

.suit {
  flex: none;
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid rgba(232, 195, 122, 0.3);
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}

.suit.active { border-color: var(--gold); background: rgba(232, 195, 122, 0.16); color: var(--text); }

.grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px 10px; width: 100%; }

.grid-item {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 0;
  border: 0;
  background: none;
  text-align: center;
}

.grid-item img {
  width: 100%;
  aspect-ratio: 7 / 12;
  object-fit: cover;
  border-radius: 6px;
  background: #2a2150;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.35);
  transition: transform 0.2s;
}

.grid-item:active img { transform: scale(0.96); }

.grid-item span { color: var(--muted); font-size: 10.5px; line-height: 1.2; overflow-wrap: anywhere; }

.grid-item.selected img { outline: 2px solid var(--gold); outline-offset: 2px; }

.badge {
  position: absolute;
  top: 4px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--gold);
  color: #1b1236;
  font-size: 12px;
}

/* Просмотр карты крупно */
.preview {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  background: rgba(8, 5, 20, 0.88);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.preview[hidden] { display: none; }

.preview img { width: min(72vw, 320px); border-radius: 14px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6); }

.preview.reversed img { transform: rotate(180deg); }

.preview-name { margin-top: 6px; font: 600 20px/1.2 "Palatino Linotype", Palatino, Georgia, serif; color: var(--gold); }

.preview-orientation { color: var(--muted); font-size: 14px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 120;
  max-width: 90vw;
  padding: 10px 16px;
  border: 1px solid rgba(232, 195, 122, 0.4);
  border-radius: 12px;
  background: rgba(20, 14, 46, 0.96);
  font-size: 13px;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 20px);
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
