/* Цвета берём из темы Telegram; при явном выборе светлой/тёмной — переопределяем */
:root {
  /* Палитра с логотипа: тёмно-синий фон, градиент фиолетовый -> голубой */
  --brand-1: #a855f7;
  --brand-2: #22d3ee;
  --brand-grad: linear-gradient(135deg, var(--brand-1), var(--brand-2));

  --bg: var(--tg-theme-bg-color, #0e0e10);
  --text: var(--tg-theme-text-color, #f2f2f2);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --button: var(--tg-theme-button-color, #4c8bf5);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --card: var(--tg-theme-secondary-bg-color, #1c1c1e);
  --danger: #e5484d;
  --ok: #30a46c;
  --tabbar-h: 62px;
}

html[data-theme="light"] {
  --bg: #ffffff;
  --text: #1a1a1c;
  --hint: #7a7a80;
  --button: #3b82f6;
  --button-text: #ffffff;
  --card: #f2f3f5;
}

html[data-theme="dark"] {
  --bg: #0d1020;
  --text: #f2f3f8;
  --hint: #8b8fa6;
  --button: #a855f7;
  --button-text: #ffffff;
  --card: #171b30;
}

html[data-theme="light"] {
  --button: #8b3ff0;
}

* { box-sizing: border-box; }

/* Атрибут hidden должен побеждать любые display из наших правил.
   Без !important селекторы по id (#image-row { display: flex }) оказываются
   сильнее браузерного [hidden] { display: none } — и скрытые блоки видны. */
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
}

/* Верхняя панель */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  background: var(--bg);
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}

.topbar-title {
  font-weight: 700;
  background: var(--brand-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.balance {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--card);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 14px;
  white-space: nowrap;
}

main { padding: 16px; }

/* Формы */
.field { display: flex; flex-direction: column; gap: 10px; }

label { font-size: 13px; color: var(--hint); }

input[type="url"], input[type="number"] {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  background: var(--bg);
  color: var(--text);
  font-size: 16px; /* меньше 16px — iOS зумит страницу при фокусе */
}

input:focus { outline: none; border-color: var(--button); }

button.primary {
  width: 100%;
  padding: 14px;
  border: 0;
  border-radius: 12px;
  background: var(--brand-grad);
  color: var(--button-text);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

button.primary:disabled { opacity: 0.55; cursor: default; }

button.ghost {
  width: 100%;
  padding: 11px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  border-radius: 12px;
  background: none;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.hint { margin: 0; font-size: 12px; color: var(--hint); }
.hint.error { color: var(--danger); }
.hint.ok { color: var(--ok); }

.section-title {
  margin: 28px 0 12px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--hint);
}

.card {
  background: var(--card);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Переключатели язык/тема */
.segmented {
  display: flex;
  gap: 6px;
  background: var(--card);
  border-radius: 12px;
  padding: 4px;
}

.segmented button {
  flex: 1;
  padding: 10px 6px;
  border: 0;
  border-radius: 9px;
  background: none;
  color: var(--hint);
  font-size: 14px;
  cursor: pointer;
}

.segmented button.is-active {
  background: var(--bg);
  color: var(--text);
  font-weight: 600;
}

/* Карточки моделей */
.models { display: flex; flex-direction: column; gap: 8px; }

.model-card {
  width: 100%;
  text-align: left;
  background: var(--card);
  border: 1.5px solid transparent;
  border-radius: 14px;
  padding: 13px 14px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.model-card.is-active { border-color: var(--brand-1); }

.model-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.badge {
  flex: 0 0 auto;
  font-size: 12px;
  color: var(--hint);
  white-space: nowrap;
}

.badge.free { color: var(--ok); }

.model-desc { margin: 5px 0 0; font-size: 13px; color: var(--hint); }

/* Выбор качества */
.qualities {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.quality-btn {
  flex: 1 1 auto;
  min-width: 90px;
  padding: 11px 12px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  border-radius: 11px;
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.quality-btn:active { border-color: var(--button); }
.quality-btn.is-big { opacity: 0.6; }

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.link-btn {
  border: 0;
  background: none;
  color: var(--button);
  font-size: 13px;
  padding: 0;
  cursor: pointer;
}

.del-btn {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 8px;
  background: color-mix(in srgb, var(--hint) 18%, transparent);
  color: var(--hint);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}

.del-btn:disabled { opacity: 0.4; }

.gallery-item { position: relative; }
.gallery-item .del-btn { position: absolute; top: 16px; right: 16px; }

/* Мои работы: папки */
.folder-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

.folder-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 20px 10px;
  border: 0;
  border-radius: 16px;
  background:
    radial-gradient(120% 100% at 0% 0%, color-mix(in srgb, var(--brand-1) 20%, transparent), transparent 60%),
    radial-gradient(120% 100% at 100% 100%, color-mix(in srgb, var(--brand-2) 18%, transparent), transparent 60%),
    var(--card);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.folder-card:active { transform: scale(0.97); }
.folder-icon { font-size: 30px; line-height: 1; }
.folder-card b { font-size: 13px; }
.folder-count { font-size: 12px; color: var(--hint); }

/* Экраны каталога моделей */
.cat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.cat-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 20px 16px;
  border: 0;
  border-radius: 18px;
  /* фирменное свечение из логотипа: фиолетовое слева сверху, голубое справа снизу */
  background:
    radial-gradient(120% 100% at 0% 0%, color-mix(in srgb, var(--brand-1) 22%, transparent), transparent 60%),
    radial-gradient(120% 100% at 100% 100%, color-mix(in srgb, var(--brand-2) 20%, transparent), transparent 60%),
    var(--card);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.cat-card b { font-size: 15px; }

.cat-card:active { transform: scale(0.98); }

.cat-icon {
  font-size: 28px;
  line-height: 1;
  filter: drop-shadow(0 0 10px color-mix(in srgb, var(--brand-1) 55%, transparent));
}

/* тонкая фирменная обводка на карточках категорий */
.cat-card::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 16px;
  padding: 1px;
  background: var(--brand-grad);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  opacity: 0.55;
  pointer-events: none;
}

.cat-count {
  position: absolute;
  top: 14px;
  right: 14px;
  font-size: 12px;
  color: var(--hint);
}

.back-btn {
  border: 0;
  background: none;
  color: var(--button);
  font-size: 15px;
  padding: 0 0 12px;
  cursor: pointer;
}

.screen-title { margin: 0 0 4px; font-size: 20px; }
.screen-sub { margin: 0 0 16px; font-size: 13px; color: var(--hint); }

/* Админ-статистика */
.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.stat-card {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--card);
}

.stat-card b { font-size: 19px; }
.stat-card span { font-size: 11px; color: var(--hint); }

.stat-title { grid-column: 1 / -1; margin: 8px 0 0; font-weight: 600; }
.stat-row { grid-column: 1 / -1; margin: 2px 0; font-size: 13px; }

#lookup-result:not(:empty) { margin-top: 4px; }

#lyrics-row { display: flex; flex-direction: column; gap: 8px; }

.presets-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 4px;
}

.preset-chip {
  padding: 6px 12px;
  border: 1px solid color-mix(in srgb, var(--brand-1) 40%, transparent);
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.preset-chip:active { background: color-mix(in srgb, var(--brand-1) 15%, transparent); }

.report-row {
  padding: 9px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 16%, transparent);
}

.report-row:last-child { border-bottom: 0; }

.report-text {
  margin: 0 0 3px;
  font-size: 14px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

#reports-list { max-height: 340px; overflow-y: auto; }
#lyrics-input { font-size: 15px; line-height: 1.45; }

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 16%, transparent);
}

.user-row:last-child { border-bottom: 0; }

.user-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  cursor: pointer;
}

.user-info b {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-info span { font-size: 12px; color: var(--hint); }

#users-list { max-height: 420px; overflow-y: auto; }

/* Чат с моделью */
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 180px;
  max-height: 52vh;
  overflow-y: auto;
  padding: 4px 0 12px;
}

.bubble {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: 15px;
  font-size: 15px;
  line-height: 1.4;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble-user {
  align-self: flex-end;
  background: var(--button);
  color: var(--button-text);
  border-bottom-right-radius: 5px;
}

.bubble-bot {
  align-self: flex-start;
  background: var(--card);
  color: var(--text);
  border-bottom-left-radius: 5px;
}

.bubble-sources {
  margin-top: 7px;
  font-size: 12px;
  color: var(--hint);
}

.bubble-sources a {
  color: var(--button);
  text-decoration: none;
  padding: 0 3px;
}

.chat-input {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Генерация: форма и галерея */
textarea {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 16px;
  resize: vertical;
}

textarea:focus { outline: none; border-color: var(--button); }

input[type="file"] {
  width: 100%;
  font-size: 13px;
  color: var(--hint);
}

#image-preview {
  width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: 12px;
  margin-top: 10px;
  background: var(--bg);
}

#image-row, #prompt-row { display: flex; flex-direction: column; gap: 8px; }

.gallery { display: flex; flex-direction: column; gap: 10px; }

.gallery-item {
  background: var(--card);
  border-radius: 14px;
  padding: 10px;
  overflow: hidden;
}

.gallery-item img, .gallery-item video {
  width: 100%;
  border-radius: 10px;
  display: block;
  background: var(--bg);
}

.gallery-meta {
  font-size: 12px;
  color: var(--hint);
  margin-top: 8px;
  overflow-wrap: anywhere;
}

.gallery-meta.error { color: var(--danger); }

/* Категории прокручиваются, если не влезают */
#category-group { overflow-x: auto; }
#category-group button { flex: 0 0 auto; white-space: nowrap; }

/* История */
.history { display: flex; flex-direction: column; gap: 8px; }

.history-item {
  background: var(--card);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.history-icon { flex: 0 0 auto; }
.history-body { min-width: 0; flex: 1; }

.history-title {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-meta { font-size: 12px; color: var(--hint); margin-top: 3px; }
.history-meta.error { color: var(--danger); white-space: normal; }

.muted { color: var(--hint); font-size: 13px; }

/* Заглушка вкладки */
.empty { text-align: center; padding: 48px 20px; }
.empty-icon { font-size: 34px; margin: 0 0 8px; }
.empty h2 { margin: 0 0 8px; font-size: 18px; }
.empty p { margin: 0; color: var(--hint); font-size: 14px; }

/* Нижний таб-бар */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--card);
  border-top: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.tabbar-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: var(--tabbar-h);
  border: 0;
  background: none;
  color: var(--hint);
  font-size: 11px;
  cursor: pointer;
}

.tabbar-btn span { font-size: 19px; line-height: 1; }
.tabbar-btn i { font-style: normal; }
.tabbar-btn.is-active { color: var(--brand-1); }

@media (prefers-reduced-motion: no-preference) {
  .tabbar-btn, button.primary, .model-card, .segmented button {
    transition: opacity 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  }
}


/* Заставка при запуске */
.splash {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: var(--bg);
  transition: opacity 0.4s ease;
}

.splash.is-hidden { opacity: 0; pointer-events: none; }

.splash-logo { width: 128px; height: 128px; }

.splash-logo .ring {
  transform-origin: center;
  animation: tz-spin 2.4s linear infinite;
  opacity: 0.65;
  stroke-dasharray: 90 40;
}

.splash-logo .hex {
  transform-origin: center;
  animation: tz-pulse 1.8s ease-in-out infinite;
}

.splash-logo .mark {
  fill: var(--text);
  font-size: 42px;
  font-weight: 700;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.splash-title {
  margin: 14px 0 0;
  font-size: 22px;
  font-weight: 700;
  background: var(--brand-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.splash-sub { margin: 2px 0 0; font-size: 12px; color: var(--hint); }

@keyframes tz-spin { to { transform: rotate(360deg); } }

@keyframes tz-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.75; transform: scale(0.94); }
}

@media (prefers-reduced-motion: reduce) {
  .splash-logo .ring, .splash-logo .hex { animation: none; }
}

/* Параметры генерации */
.param-block {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

#params-row {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#params-row:not(:empty) { margin-top: 2px; }

#audio-row { display: flex; flex-direction: column; gap: 8px; }

.gallery-item audio { width: 100%; margin-top: 4px; }

.gallery-actions { margin-top: 6px; }

.bonus-card { display: flex; flex-direction: column; gap: 8px; }


/* Лайк-сердечко на карточке модели */
.like-heart {
  flex: 0 0 auto;
  margin-left: 6px;
  font-size: 18px;
  line-height: 1;
  color: var(--hint);
  cursor: pointer;
}
.like-heart.is-liked { color: #ef4444; }

/* Поиск моделей */
#model-search {
  width: 100%;
  padding: 10px 14px;
  margin-bottom: 10px;
  border-radius: 12px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
}
#model-search:focus { outline: none; border-color: var(--brand-1); }

/* Вложение картинки в чат */
.chat-attach { display: flex; align-items: center; gap: 8px; }
.attach-btn {
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--hint) 12%, transparent);
}
.attach-name { font-size: 12px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Отчёт по деньгам */
.money-totals { margin: 6px 0; }
.money-total-line { font-size: 14px; font-weight: 600; margin: 0 0 8px; }
.money-row { font-size: 12px; color: var(--hint); margin: 3px 0; overflow-wrap: anywhere; }
.money-row.loss { color: var(--danger); }
#money-list { max-height: 300px; overflow-y: auto; }


/* Главный экран: три больших блока */
.home-blocks { display: flex; flex-direction: column; gap: 12px; }
.home-block {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  padding: 22px 18px;
  border: 0;
  border-radius: 18px;
  background:
    radial-gradient(130% 100% at 0% 0%, color-mix(in srgb, var(--brand-1) 24%, transparent), transparent 60%),
    radial-gradient(130% 100% at 100% 100%, color-mix(in srgb, var(--brand-2) 22%, transparent), transparent 60%),
    var(--card);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.home-block:active { transform: scale(0.98); }
.home-block b { font-size: 17px; }
.home-icon { font-size: 30px; line-height: 1; }
.home-sub { font-size: 13px; color: var(--hint); }

/* Поле чата: компактная строка ввода, чтобы не наезжала на таббар */
.chat-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}
.chat-input textarea {
  flex: 1 1 100%;
  min-height: 44px;
  resize: none;
}
.chat-input .primary { flex: 1 1 auto; width: auto; padding: 12px 18px; }
.chat-attach { flex: 0 0 auto; }
