@font-face {
  font-family: "Keeper Serif";
  src: url("/font");
  font-display: swap;
}

:root {
  --bg: #0d0a14;
  --surface: #191321;
  --surface-2: #231b2e;
  --line: #3a2f47;
  --gold: #c9a463;
  --gold-soft: #e7cf9b;
  --gold-dim: #8a7148;
  --text: #ece3cf;
  --muted: #a39788;
  --bad: #d9826f;
  --serif: "Keeper Serif", Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --vh: var(--tg-viewport-stable-height, 100dvh);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --card-ratio: 1.7267;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.45 var(--sans); overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#app { position: relative; height: var(--vh); display: flex; flex-direction: column; overflow: hidden;
  background: radial-gradient(120% 70% at 50% 18%, #241a30 0%, #120d19 55%, var(--bg) 100%); }

/* ---------- сцена ---------- */
.stage { position: relative; flex: 0 0 46%; min-height: 230px; display: flex; justify-content: center; align-items: flex-end; }
#keeper { position: relative; width: min(100%, 440px); height: 100%; z-index: 1; }
#keeper svg { width: 100%; height: 100%; display: block; overflow: visible; }

#keeper .body { transform-origin: 200px 420px; animation: breathe 7s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.012, 1.018); } }
#keeper .halo { transform-origin: 200px 175px; animation: halo 9s ease-in-out infinite; }
@keyframes halo { 0%, 100% { opacity: .75; } 50% { opacity: 1; } }
#keeper .eyes { transform-origin: 200px 167px; animation: blink 6.5s infinite; }
@keyframes blink { 0%, 46%, 50%, 100% { transform: scaleY(1); } 48% { transform: scaleY(.1); } }
#keeper .flame { transform-origin: 10px 30px; animation: flicker 1.8s ease-in-out infinite alternate; }
#keeper .flame-core { animation-duration: 1.1s; }
#keeper .candle-glow { transform-origin: 10px 4px; animation: glowpulse 2.4s ease-in-out infinite alternate; }
@keyframes flicker { 0% { transform: scale(1, 1) skewX(0); } 30% { transform: scale(.94, 1.06) skewX(2deg); }
  60% { transform: scale(1.04, .96) skewX(-2deg); } 100% { transform: scale(.97, 1.04) skewX(1deg); } }
@keyframes glowpulse { from { opacity: .7; transform: scale(.95); } to { opacity: 1; transform: scale(1.06); } }
#keeper .hand { transition: transform .5s ease; }

#keeper[data-state="shuffle"] .hand-left { animation: shuffle-l .42s ease-in-out infinite alternate; }
#keeper[data-state="shuffle"] .hand-right { animation: shuffle-r .42s ease-in-out infinite alternate; }
@keyframes shuffle-l { to { transform: translate(26px, -4px); } }
@keyframes shuffle-r { to { transform: translate(-26px, -4px); } }
#keeper[data-state="draw"] .hand-right { transform: translate(-46px, -14px) rotate(-8deg); }
#keeper[data-state="speak"] .eyes, #keeper[data-state="draw"] .eyes { filter: url(#glow) brightness(1.4); }
#keeper[data-state="speak"] .halo { animation-duration: 3s; }

.motes { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.mote { position: absolute; bottom: -10px; width: 3px; height: 3px; border-radius: 50%; background: var(--gold-soft);
  opacity: 0; animation: rise linear infinite; }
@keyframes rise { 0% { transform: translateY(0); opacity: 0; } 15% { opacity: .55; } 100% { transform: translateY(-260px) translateX(20px); opacity: 0; } }

.speech { position: absolute; left: 16px; right: 16px; bottom: -6px; z-index: 3; max-width: 520px; margin: 0 auto;
  background: rgba(16, 12, 22, .9); border: 1px solid var(--gold-dim); border-radius: 16px; padding: 12px 16px;
  font: 17px/1.45 var(--serif); color: var(--text); white-space: pre-line;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45); animation: fadeup .45s ease both; max-height: 42%; overflow-y: auto; }
@keyframes fadeup { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- стол ---------- */
.table { position: relative; flex: 1; min-height: 0; z-index: 2;
  background: radial-gradient(130% 90% at 50% 0%, #2c1d2f 0%, #1a1120 50%, #0d0a14 100%); }
.table::before { content: ""; position: absolute; left: -10%; right: -10%; top: -26px; height: 52px; border-radius: 50%;
  background: #2c1d2f; border-top: 1.5px solid var(--gold-dim); box-shadow: 0 -6px 24px rgba(201, 164, 99, .12); }
.layout { position: absolute; inset: 22px 12px calc(84px + var(--safe-bottom)) 12px; }
.deck { position: absolute; left: 50%; top: 34%; width: 62px; height: calc(62px * var(--card-ratio));
  transform: translate(-50%, -50%); transition: opacity .4s; }
.deck-card { position: absolute; inset: 0; border-radius: 6px; background: url("/cards/back.jpg") center / cover;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .5); }
.deck-card:nth-child(1) { transform: translate(-2px, 2px) rotate(-2deg); }
.deck-card:nth-child(2) { transform: translate(1px, 1px) rotate(1.5deg); }
.deck.shuffling .deck-card:nth-child(1) { animation: riffle-l .5s ease-in-out infinite alternate; }
.deck.shuffling .deck-card:nth-child(3) { animation: riffle-r .5s ease-in-out infinite alternate; }
@keyframes riffle-l { to { transform: translate(-34px, -6px) rotate(-12deg); } }
@keyframes riffle-r { to { transform: translate(34px, -6px) rotate(12deg); } }
.deck.hidden-deck { opacity: 0; }

.actions { position: absolute; left: 0; right: 0; bottom: calc(14px + var(--safe-bottom)); display: flex;
  gap: 8px; justify-content: center; padding: 0 12px; z-index: 4; }
.actions .btn { flex: 1 1 0; min-width: 0; padding: 11px 6px; font-size: 14px; max-width: 160px; }
.btn { border: 1px solid var(--gold-dim); background: rgba(20, 15, 28, .88); color: var(--gold-soft);
  border-radius: 999px; padding: 11px 16px; font-size: 15px; white-space: nowrap; transition: transform .1s, background .2s; }
.btn:active { transform: scale(.96); }
.btn-primary { background: var(--gold); color: #1a1408; border-color: var(--gold); font-weight: 600; }
.btn-block { display: block; width: 100%; padding: 13px; font-size: 16px; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.hint { position: absolute; left: 0; right: 0; bottom: calc(22px + var(--safe-bottom)); text-align: center;
  color: var(--muted); font-size: 14px; z-index: 4; animation: fadeup .4s ease both; }
.hint .btn { margin-top: 8px; }

.corner-btn { position: absolute; top: 10px; right: 12px; z-index: 5; width: 38px; height: 38px; border-radius: 50%;
  border: 1px solid var(--line); background: rgba(16, 12, 22, .7); color: var(--muted); font-size: 18px; }

/* ---------- карты ---------- */
.card { position: absolute; width: var(--w); height: calc(var(--w) * var(--card-ratio)); perspective: 900px;
  transition: transform .7s cubic-bezier(.2, .8, .2, 1), opacity .3s; z-index: 2; }
.card.rotated { z-index: 3; }
.card-inner { position: absolute; inset: 0; transform-style: preserve-3d; transition: transform .8s cubic-bezier(.3, .7, .2, 1); }
.card.flipped .card-inner { transform: rotateY(180deg); }
.face { position: absolute; inset: 0; border-radius: 6px; overflow: hidden; backface-visibility: hidden;
  -webkit-backface-visibility: hidden; box-shadow: 0 6px 18px rgba(0, 0, 0, .55); }
.face img { width: 100%; height: 100%; display: block; object-fit: cover; }
.face.front { transform: rotateY(180deg); }
.card.reversed .face.front img { transform: rotate(180deg); }
.card .num { position: absolute; top: -8px; left: -8px; width: 20px; height: 20px; border-radius: 50%; z-index: 5;
  background: var(--bg); border: 1px solid var(--gold); color: var(--gold); font: 11px/18px var(--serif); text-align: center; }
.card.rotated .num { transform: rotate(-90deg); }
.card.pending { cursor: pointer; }
.card.pending .face.back { animation: beckon 2.4s ease-in-out infinite; }
@keyframes beckon { 0%, 100% { box-shadow: 0 6px 18px rgba(0, 0, 0, .55); } 50% { box-shadow: 0 0 18px rgba(231, 207, 155, .55); } }

/* ---------- крупная карта и толкование ---------- */
.reveal { position: fixed; inset: 0; z-index: 30; background: rgba(8, 6, 12, .9); backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px); overflow-y: auto; padding: 18px 16px calc(28px + var(--safe-bottom)); animation: fade .3s; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.reveal-card-slot { position: relative; width: min(56vw, 240px); height: calc(min(56vw, 240px) * var(--card-ratio));
  margin: 4px auto 18px; }
.reveal-card-slot .card { position: absolute; inset: 0; width: 100%; height: 100%; --w: 100%; }
.reveal-text { max-width: 560px; margin: 0 auto; opacity: 0; transform: translateY(10px); transition: opacity .5s, transform .5s; }
.reveal-text:not(.shown) { pointer-events: none; }
.reveal-text.shown { opacity: 1; transform: none; }
.reveal-text .pos { color: var(--muted); font-size: 13px; letter-spacing: .06em; text-transform: uppercase; text-align: center; }
.reveal-text h2 { font: 26px/1.2 var(--serif); color: var(--gold); text-align: center; margin: 4px 0 2px; font-weight: normal; }
.reveal-text .rev { text-align: center; color: var(--muted); font-size: 14px; margin-bottom: 10px; }
.prose { font: 18px/1.55 var(--serif); color: var(--text); }
.prose p { margin: 0 0 12px; }
.reflection { margin: 18px 0; padding: 14px 16px; border-left: 2px solid var(--gold); background: var(--surface);
  border-radius: 0 12px 12px 0; font: italic 18px/1.5 var(--serif); }
.reflection .label { display: block; font: 12px/1 var(--sans); color: var(--gold); letter-spacing: .08em; text-transform: uppercase;
  margin-bottom: 8px; font-style: normal; }
.reveal .buttons { display: grid; gap: 8px; margin-top: 18px; }

/* ---------- шторка ---------- */
.backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(5, 4, 9, .6); animation: fade .25s; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-height: 88%; overflow-y: auto;
  background: var(--surface); border-top: 1px solid var(--gold-dim); border-radius: 20px 20px 0 0;
  padding: 10px 16px calc(20px + var(--safe-bottom)); animation: slideup .32s cubic-bezier(.2, .8, .2, 1); }
@media (min-width: 640px) { .sheet { left: 50%; width: 600px; margin-left: -300px; } }
@keyframes slideup { from { transform: translateY(100%); } to { transform: none; } }
.sheet .grip { width: 38px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 12px; }
.sheet h1 { font: 24px/1.2 var(--serif); color: var(--gold); margin: 0 0 6px; font-weight: normal; }
.sheet .lead { color: var(--muted); margin: 0 0 14px; }
.sheet .stack { display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr); }

.spread-item { display: flex; gap: 12px; align-items: center; width: 100%; text-align: left; padding: 12px 14px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 14px; }
.spread-item:active { border-color: var(--gold-dim); }
.spread-item .glyph { flex: 0 0 56px; display: flex; gap: 2px; justify-content: center; }
.spread-item .glyph i { display: block; width: 9px; height: 15px; border-radius: 2px; border: 1px solid var(--gold-dim); background: #2a2036; }
.spread-item .info { flex: 1; min-width: 0; }
.spread-item .title { font: 18px/1.2 var(--serif); color: var(--text); }
.spread-item .desc { font-size: 13px; color: var(--muted); margin-top: 2px; }
.badge { flex: 0 0 auto; font-size: 12.5px; border-radius: 999px; padding: 3px 10px; border: 1px solid var(--gold-dim);
  color: var(--gold-soft); white-space: nowrap; }
.badge.free { border-color: #5d8a66; color: #9fd0a8; }
.badge.locked { border-color: var(--line); color: var(--muted); }

textarea, input[type="text"] { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; font: 17px/1.4 var(--serif); resize: none; }
textarea:focus, input:focus { outline: none; border-color: var(--gold-dim); }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { color: var(--muted); font-size: 13px; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice button { padding: 11px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.choice button.on { border-color: var(--gold); color: var(--gold-soft); background: #2a2036; }
.error { color: var(--bad); font-size: 14px; min-height: 18px; }
.counter { text-align: right; color: var(--muted); font-size: 12px; }
.small { font-size: 13px; color: var(--muted); }

.diary-item { display: block; width: 100%; min-width: 0; overflow: hidden; text-align: left; padding: 12px 14px; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 14px; }
.diary-item .row { display: flex; justify-content: space-between; gap: 10px; }
.diary-item .title { font: 17px/1.2 var(--serif); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.diary-item .date { color: var(--muted); font-size: 13px; white-space: nowrap; }
.diary-item .q { color: var(--muted); font-size: 14px; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumbs { display: flex; gap: 3px; margin-top: 8px; }
.thumbs img { width: 22px; height: 38px; border-radius: 2px; object-fit: cover; }

.story-card { display: grid; grid-template-columns: 76px 1fr; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.story-card img { width: 76px; border-radius: 5px; }
.story-card img.rev { transform: rotate(180deg); }
.story-card .pos { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.story-card .name { font: 18px/1.25 var(--serif); color: var(--gold); margin-bottom: 4px; }
.story-card .prose { font-size: 16px; }

.toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%); z-index: 50;
  background: #2a2036; border: 1px solid var(--gold-dim); color: var(--text); padding: 10px 16px; border-radius: 12px;
  max-width: 90%; text-align: center; animation: fadeup .3s; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
