/* Buran — mini-app в духе Apple Liquid Glass.
   Принципы: стекло — только у плавающих элементов управления (контент под ним),
   прозрачность + размытие + насыщенность фона, зеркальный блик по кромке,
   капсулы и концентрические скругления, «желейный» отклик на нажатие. */

:root {
  /* Чёрный, белый, красный акцент */
  --bg: #000;
  --text: #fff;
  --text-2: rgba(255, 255, 255, .64);
  --text-3: rgba(255, 255, 255, .38);
  --accent: #ff2d3c;
  --accent-2: #b3001b;
  --danger: #ff453a;
  --live: #fff;

  --glass-fill: rgba(255, 255, 255, .08);
  --glass-fill-2: rgba(255, 255, 255, .02);
  --glass-rim: rgba(255, 255, 255, .6);
  --glass-rim-soft: rgba(255, 255, 255, .14);
  --glass-shadow: 0 12px 34px -10px rgba(0, 0, 0, .8), 0 2px 6px rgba(0, 0, 0, .3);
  --glass-blur: 18px;
  --inset-fill: rgba(255, 255, 255, .04);
  --scrim: rgba(0, 0, 0, .55);
  --card: #111;
  --blob-1: #7a0010;
  --blob-2: #ff2d3c;
  --blob-3: #3a3a3a;

  --spring: cubic-bezier(.34, 1.56, .64, 1);
  --smooth: cubic-bezier(.22, 1, .36, 1);

  --safe-top: calc(var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px)) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: max(var(--tg-safe-area-inset-bottom, 0px), env(safe-area-inset-bottom, 0px));
  --gutter: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Roboto", "Segoe UI", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  overscroll-behavior-y: none;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* ---------- Подложка ---------- */
.ambient { position: fixed; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; }
.ambient i {
  position: absolute; width: 70vmax; height: 70vmax; border-radius: 50%;
  filter: blur(90px); opacity: .32; mix-blend-mode: screen;
  animation: drift 28s ease-in-out infinite alternate;
}
.ambient i:nth-child(1) { background: var(--blob-1); left: -30vmax; top: -25vmax; }
.ambient i:nth-child(2) { opacity: .16; background: var(--blob-2); right: -35vmax; top: 20vh; animation-duration: 34s; animation-delay: -8s; }
.ambient i:nth-child(3) { background: var(--blob-3); left: -10vmax; bottom: -45vmax; animation-duration: 40s; animation-delay: -16s; }
@keyframes drift {
  0% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(8vmax, 6vmax) scale(1.12); }
  100% { transform: translate(-6vmax, 10vmax) scale(.92); }
}

/* ---------- Материал «жидкое стекло» ---------- */
.glass {
  position: relative;
  isolation: isolate;
  background:
    radial-gradient(120% 90% at var(--lx, 30%) var(--ly, 0%), rgba(255, 255, 255, .16), transparent 55%),
    linear-gradient(180deg, var(--glass-fill), var(--glass-fill-2));
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(190%) brightness(1.06);
  backdrop-filter: blur(var(--glass-blur)) saturate(190%) brightness(1.06);
  box-shadow:
    inset 0 1px .5px var(--glass-rim),
    inset 0 -.5px .5px var(--glass-rim-soft),
    inset 0 0 0 .5px var(--glass-rim-soft),
    inset 0 0 18px rgba(255, 255, 255, .05),
    var(--glass-shadow);
}
/* Зеркальная кромка: светлее сверху-слева и снизу-справа, как у толстого стекла */
.glass::before {
  content: ""; position: absolute; inset: 0; border-radius: inherit; padding: 1px; pointer-events: none; z-index: -1;
  background: linear-gradient(140deg, rgba(255, 255, 255, .75), rgba(255, 255, 255, .08) 32%, rgba(255, 255, 255, 0) 55%, rgba(255, 255, 255, .28) 90%);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor; mask-composite: exclude;
  opacity: .7;
}
/* Линза (Chromium): кромка преломляет то, что под ней — filter задаётся из JS */
.glass.lens { backdrop-filter: var(--lens) blur(var(--glass-blur)) saturate(190%) brightness(1.06); }

.glass.tinted {
  background:
    radial-gradient(120% 90% at var(--lx, 30%) var(--ly, 0%), rgba(255, 255, 255, .35), transparent 55%),
    linear-gradient(160deg, color-mix(in srgb, var(--accent) 92%, transparent), color-mix(in srgb, var(--accent-2) 88%, transparent));
  color: #fff;
}
.glass.inset {
  background: var(--inset-fill);
  -webkit-backdrop-filter: blur(10px) saturate(150%);
  backdrop-filter: blur(10px) saturate(150%);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, .28), inset 0 -.5px 0 var(--glass-rim-soft), inset 0 0 0 .5px var(--glass-rim-soft);
}
.glass.inset::before { opacity: .25; }

/* «Желейный» отклик: стекло чуть сжимается и вспыхивает */
.press { transition: transform .45s var(--spring), filter .3s; }
.press:active, .press.pressed { transform: scale(.94); filter: brightness(1.15); transition-duration: .12s; }

/* ---------- Шапка ---------- */
.topbar {
  position: relative; z-index: 2;
  display: flex; align-items: flex-end; justify-content: space-between; gap: 12px;
  padding: calc(var(--safe-top) + 18px) calc(var(--gutter) + 4px) 10px;
}
.title h1 { margin: 0; font-size: 32px; line-height: 1.05; font-weight: 750; letter-spacing: -.02em; }
.title p { margin: 4px 0 0; color: var(--text-2); font-size: 14px; }
.pill { display: inline-flex; align-items: center; gap: 7px; height: 34px; padding: 0 14px; border-radius: 999px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); }
.status.online .dot { background: var(--accent); box-shadow: 0 0 10px var(--accent); animation: pulse 2.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 2px var(--accent); opacity: .7; } }

/* ---------- Галерея ---------- */
.gallery {
  position: relative; z-index: 1;
  display: flex; gap: 8px; align-items: flex-start;
  padding: 8px var(--gutter) calc(var(--safe-bottom) + 120px);
}
.gallery .col { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }

.card {
  position: relative; width: 100%;
  border-radius: 22px; overflow: hidden; background: var(--card);
  transform: translateZ(0);
  animation: card-in .6s var(--smooth) both;
}
@keyframes card-in { from { opacity: 0; transform: translateY(14px) scale(.96); } }
.card .ratio { width: 100%; }
.card img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; opacity: 0; transition: opacity .5s, filter .9s var(--smooth), transform .9s var(--smooth); }
.card img.loaded { opacity: 1; }
.card.reveal img { filter: blur(24px) saturate(1.6); transform: scale(1.08); }
.card.reveal img.loaded { filter: none; transform: none; }
.card::after { content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; box-shadow: inset 0 0 0 .5px rgba(255, 255, 255, .12); }

/* Ожидающая карточка: «жидкая» переливающаяся заливка + стеклянный статус */
.card.pending { background: #0d0d0d; }
.card.pending .aurora { position: absolute; inset: -40%; filter: blur(28px); opacity: .95;
  background:
    radial-gradient(35% 35% at 30% 30%, #ff2d3c, transparent 70%),
    radial-gradient(30% 30% at 70% 40%, #6b0010, transparent 70%),
    radial-gradient(35% 35% at 45% 75%, rgba(255, 255, 255, .55), transparent 70%);
  animation: swirl 7s linear infinite;
}
.card.queued .aurora { animation-duration: 16s; opacity: .45; filter: blur(30px) saturate(.6); }
@keyframes swirl { to { transform: rotate(360deg); } }
.card .state {
  --glass-fill: rgba(255, 255, 255, .12); --glass-fill-2: rgba(255, 255, 255, .04); --glass-rim: rgba(255, 255, 255, .55); --glass-rim-soft: rgba(255, 255, 255, .14);
  position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 1;
  display: flex; align-items: center; gap: 10px; padding: 9px 11px 9px 9px; border-radius: 16px;
}
.ring { width: 34px; height: 34px; flex: none; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 3; }
.ring .track { stroke: rgba(255, 255, 255, .18); }
.ring .bar { stroke: #fff; stroke-linecap: round; transition: stroke-dashoffset .8s var(--smooth); }
.card.queued .ring .bar { stroke-dasharray: 20 200 !important; animation: spin 1.4s linear infinite; transform-origin: center; }
@keyframes spin { to { transform: rotate(360deg); } }
.state .txt { min-width: 0; color: #fff; }
.state b { display: block; font-size: 13px; font-weight: 650; }
.state small { display: block; font-size: 11.5px; opacity: .72; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .peek { position: absolute; top: 12px; left: 12px; right: 12px; z-index: 1; color: rgba(255, 255, 255, .92); font-size: 12.5px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; text-shadow: 0 1px 8px rgba(0, 0, 0, .4); }

.card.failed { background: color-mix(in srgb, var(--danger) 16%, var(--card)); }
.card.failed .fail { position: absolute; inset: 0; padding: 14px; display: flex; flex-direction: column; justify-content: space-between; }
.fail p { margin: 0; font-size: 12.5px; color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.fail .err { color: var(--danger); font-weight: 600; font-size: 13px; }
.fail .row { display: flex; gap: 6px; }
.fail .row button { flex: 1; height: 34px; border-radius: 12px; font-size: 13px; font-weight: 600; }

/* ---------- Пустое состояние ---------- */
.empty { position: fixed; inset: 0; z-index: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 0 40px 60px; pointer-events: none; }
.orb { width: 96px; height: 96px; border-radius: 50%; display: grid; place-items: center; margin-bottom: 22px; animation: float 5s ease-in-out infinite; }
.orb svg { width: 40px; height: 40px; stroke-width: 1.4; }
@keyframes float { 50% { transform: translateY(-8px); } }
.empty h2 { margin: 0 0 8px; font-size: 21px; font-weight: 700; letter-spacing: -.01em; }
.empty p { margin: 0; color: var(--text-2); max-width: 280px; }

/* ---------- Кнопка «+» ---------- */
.fab {
  position: fixed; z-index: 5; left: 50%; bottom: calc(var(--safe-bottom) + 22px);
  width: 66px; height: 66px; margin-left: -33px; border-radius: 50%;
  display: grid; place-items: center;
  transition: transform .5s var(--spring), opacity .3s;
}
.fab svg { width: 28px; height: 28px; stroke-width: 2.4; }
.fab:active { transform: scale(.9); }
body.composing .fab { transform: scale(.6) translateY(30px); opacity: 0; pointer-events: none; }

/* ---------- Лист создания ---------- */
.scrim { position: fixed; inset: 0; z-index: 9; background: var(--scrim); opacity: 0; pointer-events: none; transition: opacity .4s; -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
body.composing .scrim { opacity: 1; pointer-events: auto; }

.sheet {
  --glass-blur: 30px;
  position: fixed; z-index: 10; left: 8px; right: 8px; bottom: calc(var(--safe-bottom) + 8px);
  max-width: 560px; margin: 0 auto; max-height: calc(100vh - var(--safe-top) - 40px); overflow-y: auto;
  padding: 8px 16px 16px; border-radius: 34px;
  transform-origin: 50% 100%;
  visibility: hidden;
}
.sheet.open { visibility: visible; }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--text-3); margin: 0 auto 6px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin: 2px 0 12px; }
.sheet-head h3 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -.01em; padding-left: 4px; }
.icon-btn { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; }
.icon-btn svg { width: 18px; height: 18px; stroke-width: 2.2; }

.field { border-radius: 22px; padding: 12px 14px 8px; }
.field textarea {
  width: 100%; resize: none; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.4; min-height: 92px; user-select: text; -webkit-user-select: text;
}
.field textarea::placeholder { color: var(--text-3); }
.field-foot { display: flex; align-items: center; justify-content: space-between; color: var(--text-3); font-size: 12px; }
.chip-btn { display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px 0 8px; border-radius: 999px; background: rgba(255, 255, 255, .08); color: var(--text-2); font-size: 13px; font-weight: 600; }
.chip-btn svg { width: 16px; height: 16px; }

.label { display: block; margin: 16px 4px 8px; font-size: 13px; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: .04em; }

.models { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px; padding: 2px 16px 6px; scroll-snap-type: x mandatory; scrollbar-width: none; }
.models::-webkit-scrollbar { display: none; }
.model {
  flex: none; width: 168px; scroll-snap-align: start; text-align: left;
  padding: 12px 13px; border-radius: 20px;
  transition: transform .45s var(--spring), box-shadow .3s;
}
.model b { display: flex; align-items: center; gap: 6px; font-size: 15px; font-weight: 650; }
.model b .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.model.offline b .dot { background: var(--text-3); }
.model p { margin: 4px 0 8px; font-size: 12.5px; color: var(--text-2); line-height: 1.3; min-height: 32px; }
.model small { font-size: 12px; color: var(--text-3); font-weight: 600; }
.model.selected { box-shadow: inset 0 0 0 1.5px var(--accent), inset 0 1px .5px var(--glass-rim), 0 8px 26px -10px var(--accent); }
.model.selected small { color: var(--accent); }
.model.offline { opacity: .55; }

.segmented { position: relative; display: grid; grid-template-columns: repeat(3, 1fr); padding: 4px; border-radius: 999px; }
.segmented button { position: relative; z-index: 1; height: 38px; display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 14px; font-weight: 600; color: var(--text-2); transition: color .3s, opacity .3s; }
.segmented button.on { color: var(--text); }
.segmented button:disabled { opacity: .3; cursor: default; }
.segmented .thumb {
  position: absolute; top: 4px; bottom: 4px; left: 4px; width: calc((100% - 8px) / 3); border-radius: 999px;
  background: linear-gradient(180deg, rgba(255, 255, 255, .22), rgba(255, 255, 255, .08));
  box-shadow: inset 0 1px .5px rgba(255, 255, 255, .7), inset 0 0 0 .5px rgba(255, 255, 255, .25), 0 4px 14px rgba(0, 0, 0, .25);
  transition: transform .5s var(--spring);
}
.shape { display: inline-block; border: 1.6px solid currentColor; border-radius: 3px; }
.s11 { width: 13px; height: 13px; } .s34 { width: 11px; height: 14px; } .s43 { width: 15px; height: 11px; }

.hint { min-height: 18px; margin: 12px 4px 10px; font-size: 12.5px; color: var(--text-2); }
.primary { width: 100%; height: 56px; border-radius: 999px; display: flex; align-items: center; justify-content: center; gap: 10px; font-size: 17px; font-weight: 700; letter-spacing: -.01em; }
.primary:disabled { filter: grayscale(.8) brightness(.8); opacity: .6; }
.primary.busy svg { animation: spin 1s linear infinite; }

/* ---------- Просмотр ---------- */
.viewer { position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; }
.viewer-bg { position: absolute; inset: 0; background: rgba(0, 0, 0, .55); -webkit-backdrop-filter: blur(40px) saturate(160%); backdrop-filter: blur(40px) saturate(160%); opacity: 0; transition: opacity .35s; }
.viewer.open .viewer-bg { opacity: 1; }
.viewer-stage { position: relative; flex: 1; margin: 0; display: flex; align-items: center; justify-content: center; padding: calc(var(--safe-top) + 64px) 12px 12px; min-height: 0; }
.viewer-stage img { max-width: 100%; max-height: 100%; border-radius: 24px; box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .7); transform-origin: center; user-select: none; -webkit-user-drag: none; }
.viewer-info { position: relative; margin: 0 12px 10px; padding: 12px 14px; border-radius: 22px; color: #fff; opacity: 0; transform: translateY(10px); transition: opacity .35s .08s, transform .5s .08s var(--spring); }
.viewer-info p { margin: 0 0 4px; font-size: 14px; line-height: 1.35; max-height: 4.1em; overflow-y: auto; user-select: text; -webkit-user-select: text; }
.viewer-info span { font-size: 12px; color: rgba(255, 255, 255, .55); }
.toolbar { position: relative; margin: 0 12px calc(var(--safe-bottom) + 12px); padding: 6px; border-radius: 999px; display: grid; grid-template-columns: repeat(4, 1fr); color: #fff; opacity: 0; transform: translateY(16px); transition: opacity .35s .12s, transform .55s .12s var(--spring); }
.toolbar button { height: 52px; border-radius: 999px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 11px; font-weight: 600; transition: background .2s, transform .4s var(--spring); }
.toolbar button:active { background: rgba(255, 255, 255, .14); transform: scale(.92); }
.toolbar button.danger { color: var(--danger); }
.viewer.open .viewer-info, .viewer.open .toolbar { opacity: 1; transform: none; }
.viewer-close { position: absolute; top: calc(var(--safe-top) + 14px); right: 14px; color: #fff; opacity: 0; transition: opacity .3s .1s; }
.viewer.open .viewer-close { opacity: 1; }

/* ---------- Тост ---------- */
.toast {
  position: fixed; z-index: 40; left: 50%; top: calc(var(--safe-top) + 12px); max-width: calc(100% - 32px);
  padding: 11px 18px; border-radius: 999px; font-size: 14px; font-weight: 600; text-align: center;
  transform: translate(-50%, -30px) scale(.9); opacity: 0; pointer-events: none;
  transition: transform .5s var(--spring), opacity .25s;
}
.toast.show { transform: translate(-50%, 0) scale(1); opacity: 1; }

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