/* Secret Santa Mini App
   Base: Telegram's own theme colours. Identity: kraft gift tags, ribbon red, pine, foil gold, handwritten names. */

@font-face {
  font-family: 'Caveat';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/caveat-cyrillic-wght-normal.woff2') format('woff2');
  unicode-range: U+0301, U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
}
@font-face {
  font-family: 'Caveat';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('/fonts/caveat-latin-wght-normal.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

:root {
  color-scheme: light dark;

  --ribbon: #c62e3e;
  --ribbon-deep: #a32230;
  --ribbon-text: #c62e3e; /* ribbon colour when used as text on the page background */
  --on-ribbon: #fff;
  --pine: #1e6b52;
  --pine-text: #1e6b52;
  --foil: #d9a441;
  --foil-ink: #3a2a08;
  --kraft: #d9bc90;
  --kraft-edge: #b08d58;
  --twine: #7b5b32;
  --ink: #2b1d10;
  --ink-soft: #5b4630;

  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f1f5);
  --text: var(--tg-theme-text-color, #17181b);
  --hint: var(--tg-theme-hint-color, #6b7075);
  --line: color-mix(in srgb, var(--text) 13%, transparent);

  --font-ui: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-hand: 'Caveat', 'Segoe Print', 'Bradley Hand', cursive;

  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
  --safe-top: max(env(safe-area-inset-top, 0px), var(--tg-content-safe-area-inset-top, 0px));
}

/* Outside Telegram (plain browser) there are no theme variables: follow the OS instead. */
@media (prefers-color-scheme: dark) {
  :root:not(.in-tg) {
    --bg: #17181c;
    --bg2: #22242a;
    --text: #f1f1f4;
    --hint: #9aa0a6;
  }
}
/* Telegram dark themes: keep ribbon/pine readable as text */
:root.dark {
  --ribbon-text: #ff7f8d;
  --pine-text: #5cc3a0;
}
@media (prefers-color-scheme: dark) {
  :root:not(.in-tg) { --ribbon-text: #ff7f8d; --pine-text: #5cc3a0; }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font-ui);
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3, p { margin: 0; }
ul { list-style: none; margin: 0; padding: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--ribbon-text); }

:focus-visible { outline: 3px solid var(--pine); outline-offset: 2px; border-radius: 6px; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* ---------- layout ---------- */
.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 12px) 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.screen.has-bar { padding-bottom: calc(96px + var(--safe-bottom)); }

.top { display: flex; align-items: flex-start; gap: 10px; }
.top h1 {
  flex: 1;
  min-width: 0;
  font: 700 38px/1.02 var(--font-hand);
  letter-spacing: 0.2px;
  overflow-wrap: anywhere;
  text-wrap: balance;
}
.top .iconbtn { margin-top: 2px; }
.sub { color: var(--hint); font-size: 14px; margin-top: 4px; text-wrap: pretty; }

.sec { display: flex; flex-direction: column; gap: 10px; }
.sec > h2 { font-size: 14px; font-weight: 600; color: var(--hint); display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.desc { color: var(--text); max-width: 60ch; text-wrap: pretty; overflow-wrap: anywhere; }

.bar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 5;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.bar > * { width: 100%; max-width: 528px; }
.bar .hint { text-align: center; margin-bottom: 8px; }
.hint { color: var(--hint); font-size: 14px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 50px;
  padding: 10px 20px;
  line-height: 1.2;
  text-align: center;
  border: 0;
  border-radius: 999px;
  background: var(--bg2);
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
  transition: transform 0.12s ease, filter 0.12s ease;
}
.btn:active { transform: scale(0.98); }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--ribbon); color: var(--on-ribbon); }
.btn.pine { background: var(--pine); color: #fff; }
.btn.foil { background: var(--foil); color: var(--foil-ink); }
.btn.ghost { background: transparent; color: var(--ribbon-text); box-shadow: inset 0 0 0 2px var(--line); }
.btn.danger { background: transparent; color: var(--ribbon-text); }
.btn.small { width: auto; min-height: 40px; padding: 0 16px; font-size: 15px; }
.btn svg { flex: none; }
.row-btns { display: flex; gap: 10px; }
.row-btns > .btn { flex: 1; }
.row-btns.stack { flex-direction: column; }

.iconbtn {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--bg2);
  color: var(--text);
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label, .field > .label { font-size: 14px; font-weight: 600; color: var(--hint); }
.input {
  width: 100%;
  min-height: 50px;
  padding: 12px 14px;
  background: var(--bg2);
  border: 2px solid transparent;
  border-radius: 14px;
  outline: none;
}
.input:focus { border-color: var(--pine); }
textarea.input { min-height: 96px; resize: vertical; }
.input::placeholder { color: var(--hint); }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 4px 0; }
.switch input { width: 52px; height: 30px; accent-color: var(--pine); flex: none; }

/* ---------- badges, avatars ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 26px;
  padding: 0 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  background: var(--bg2);
  color: var(--text);
  white-space: nowrap;
}
.badge.pine { background: color-mix(in srgb, var(--pine) 18%, transparent); color: var(--pine-text); }
.badge.foil { background: var(--foil); color: var(--foil-ink); }
.badge.ribbon { background: color-mix(in srgb, var(--ribbon) 16%, transparent); color: var(--ribbon-text); }

.avatar {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  font-size: 16px;
}

/* ---------- gift tags (game cards) ---------- */
.tags { display: flex; flex-direction: column; gap: 14px; }
.tag {
  --n: 18px;
  position: relative;
  display: block;
  width: 100%;
  padding: 16px 16px 14px 58px;
  border: 0;
  text-align: left;
  color: var(--ink);
  background:
    radial-gradient(circle at 29px 28px, var(--bg) 0 6px, var(--kraft-edge) 6.5px 8.5px, transparent 9px),
    var(--kraft);
  clip-path: polygon(var(--n) 0, 100% 0, 100% 100%, 0 100%, 0 var(--n));
  transition: transform 0.12s ease;
}
button.tag:active { transform: scale(0.99); }
button.tag:focus-visible { outline: none; box-shadow: inset 0 0 0 3px var(--pine); }
.tag::after { /* twine leaving the hole */
  content: '';
  position: absolute;
  left: 29px;
  top: 28px;
  width: 34px;
  height: 2px;
  background: var(--twine);
  transform-origin: 0 50%;
  transform: rotate(222deg);
  pointer-events: none;
}
.tag-title { display: block; font: 700 28px/1.08 var(--font-hand); overflow-wrap: anywhere; }
.tag-meta { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-size: 14px; color: var(--ink-soft); align-items: center; }
.tag .badge { background: rgb(43 29 16 / 0.12); color: var(--ink); }
.tag .badge.pine { background: var(--pine); color: #fff; }
.tag .badge.foil { background: var(--foil); color: var(--foil-ink); }

.empty { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 10px; padding: 24px 8px; }
.empty h2 { font: 700 32px/1.1 var(--font-hand); }
.empty p { color: var(--hint); max-width: 34ch; text-wrap: balance; }
.empty svg { width: 120px; height: 120px; }

/* ---------- rows ---------- */
.rows { display: flex; flex-direction: column; }
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  padding: 8px 0;
  background: none;
  border: 0;
  border-bottom: 1px solid var(--line);
  text-align: left;
}
.rows > .row:last-child, .rows > li:last-child > .row { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .name { font-weight: 600; overflow-wrap: anywhere; }
.row .meta { font-size: 13px; color: var(--hint); }
.row .chev { color: var(--hint); flex: none; }
button.row:active { background: var(--bg2); }

.kv { display: flex; flex-wrap: wrap; gap: 14px 28px; }
.kv > div { display: flex; flex-direction: column; min-width: 0; }
.kv .k { font-size: 13px; color: var(--hint); }
.kv .v { font-weight: 600; overflow-wrap: anywhere; }

.invite {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  border-radius: 18px;
  background: var(--bg2);
}
.invite .code {
  align-self: flex-start;
  font: 700 22px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 3px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px dashed var(--hint);
  user-select: all;
}

/* ---------- wishes ---------- */
.wish { display: flex; flex-direction: column; gap: 8px; padding: 12px; border-radius: 16px; background: var(--bg2); }
.wish .remove { align-self: flex-end; }
.wishes { display: flex; flex-direction: column; gap: 10px; }
.wish-list li { display: flex; gap: 8px; align-items: baseline; padding: 6px 0; border-bottom: 1px dashed rgb(43 29 16 / 0.25); overflow-wrap: anywhere; }
.wish-list li:last-child { border-bottom: 0; }
.wish-list a { color: var(--ink); text-underline-offset: 3px; }

/* ---------- reveal ---------- */
.reveal { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.gift-btn { background: none; border: 0; padding: 0; width: min(260px, 70%); border-radius: 24px; }
.gift-btn svg { width: 100%; height: auto; overflow: visible; display: block; }
.gift-btn .tap { display: block; text-align: center; font: 700 26px/1 var(--font-hand); color: var(--ribbon-text); margin-top: 4px; }
.gift .lid, .gift .bow { transform-box: fill-box; transform-origin: 50% 100%; }
.gift.open .lid { transform: translate(14px, -70px) rotate(14deg); opacity: 0; transition: transform 0.7s cubic-bezier(.2,.8,.2,1), opacity 0.6s ease 0.1s; }
.gift.open .bow { transform: translate(-6px, -80px) rotate(-18deg); opacity: 0; transition: transform 0.7s cubic-bezier(.2,.8,.2,1), opacity 0.5s ease; }
.gift.open .box { opacity: 0; transition: opacity 0.4s ease 0.45s; }
.gift:not(.open) { animation: wiggle 2.6s ease-in-out 1.2s infinite; }
@keyframes wiggle {
  0%, 86%, 100% { transform: rotate(0); }
  90% { transform: rotate(-3deg); }
  94% { transform: rotate(3deg); }
  98% { transform: rotate(-1.5deg); }
}

.tag.big { padding: 22px 20px 20px 64px; cursor: default; }
.tag.big .label { display: block; font-size: 14px; color: var(--ink-soft); }
.tag.big .to { display: block; font: 700 46px/1.02 var(--font-hand); overflow-wrap: anywhere; margin: 2px 0 2px; }
.tag.big h3 { font: 700 22px/1.1 var(--font-hand); margin: 14px 0 4px; }
.tag.big .note { margin-top: 10px; padding-top: 10px; border-top: 1px dashed rgb(43 29 16 / 0.25); color: var(--ink); overflow-wrap: anywhere; }
.tag.big .none { color: var(--ink-soft); }
.tag.big .username { color: var(--ink-soft); font-size: 15px; }
@media (prefers-reduced-motion: no-preference) {
  .tag.big.appear { animation: tag-in 0.5s cubic-bezier(.2,.9,.3,1.2) both; }
}
@keyframes tag-in { from { opacity: 0; transform: translateY(18px) rotate(-1.5deg) scale(0.96); } to { opacity: 1; transform: none; } }

/* ---------- chat ---------- */
.screen.chat { padding-bottom: calc(86px + var(--safe-bottom)); min-height: 100dvh; }
.msgs { display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 82%; padding: 10px 14px; border-radius: 18px; overflow-wrap: anywhere; white-space: pre-wrap; }
.msg.mine { align-self: flex-end; background: var(--ribbon); color: var(--on-ribbon); border-bottom-right-radius: 6px; }
.msg.them { align-self: flex-start; background: var(--bg2); border-bottom-left-radius: 6px; }
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer .input { min-height: 48px; max-height: 120px; border-radius: 24px; padding: 12px 16px; resize: none; }
.composer .btn { width: 48px; min-height: 48px; padding: 0; flex: none; }

/* ---------- plans (paywall) ---------- */
.plans { display: flex; flex-direction: column; gap: 12px; }
.plan { display: flex; flex-direction: column; gap: 10px; padding: 16px; border-radius: 18px; border: 2px solid var(--line); }
.plan.best { border-color: var(--foil); }
.plan .head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.plan .pname { font: 700 30px/1 var(--font-hand); }
.plan .price { font-weight: 700; }
.plan ul { display: flex; flex-direction: column; gap: 4px; font-size: 15px; }
.plan li { display: flex; gap: 8px; }
.plan li::before { content: '✓'; color: var(--pine-text); font-weight: 700; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  z-index: 100;
  top: calc(var(--safe-top) + 12px);
  left: 50%;
  translate: -50% 0;
  max-width: min(92vw, 520px);
  padding: 12px 18px;
  border-radius: 16px;
  background: var(--ink);
  color: #fff;
  font-weight: 600;
  text-align: center;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
  animation: toast-in 0.2s ease both;
}
.toast.err { background: var(--ribbon-deep); }
@keyframes toast-in { from { opacity: 0; translate: -50% -10px; } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

.spinner { display: grid; place-items: center; min-height: 40vh; }
.spinner i {
  width: 34px; height: 34px; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--ribbon);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { rotate: 360deg; } }
@media (prefers-reduced-motion: reduce) { .spinner i { animation-duration: 3s; } }

/* ---------- native <dialog>: confirmations and the bottom sheet ---------- */
dialog {
  color: var(--text);
  background: var(--bg);
  border: 0;
  padding: 0;
}
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop { overflow: hidden; overscroll-behavior: contain; }

dialog.sheet {
  width: min(560px, 100%);
  max-width: 100%;
  max-height: 90dvh;
  margin: auto auto 0;
  border-radius: 24px 24px 0 0;
  padding: 22px 16px calc(18px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}
dialog.confirm {
  width: min(380px, calc(100% - 32px));
  border-radius: 24px;
  padding: 22px 20px 18px;
  margin: auto;
}
dialog.confirm p { margin: 8px 0 18px; color: var(--hint); }
dialog.sheet h2 { font: 700 34px/1.05 var(--font-hand); }

/* entry/exit animation (opacity + transform only) */
dialog {
  opacity: 0;
  translate: 0 24px;
  transition: opacity 0.22s ease-out, translate 0.22s ease-out, display 0.22s allow-discrete, overlay 0.22s allow-discrete;
}
dialog[open]:not([data-closing]) {
  opacity: 1;
  translate: 0 0;
  @starting-style { opacity: 0; translate: 0 24px; }
}
dialog::backdrop {
  background: rgb(0 0 0 / 0);
  transition: background 0.22s ease-out, display 0.22s allow-discrete, overlay 0.22s allow-discrete;
}
dialog[open]:not([data-closing])::backdrop {
  background: rgb(0 0 0 / 0.5);
  @starting-style { background: rgb(0 0 0 / 0); }
}
@media (prefers-reduced-motion: reduce) {
  dialog { translate: 0 0; transition-duration: 0.08s; }
  dialog[open]:not([data-closing]) { @starting-style { translate: 0 0; } }
}

/* ---------- confetti canvas ---------- */
.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 90; }

.gate { min-height: 100dvh; display: grid; place-content: center; gap: 12px; text-align: center; padding: 24px; }
.gate h1 { font: 700 44px/1 var(--font-hand); }
.gate p { color: var(--hint); max-width: 30ch; margin-inline: auto; }
