/* ==========================================================================
   Mini App SaveAiBOT — оформление поверх Telegram WebApp themeParams.
   Tailwind (CDN) отвечает за разметку, здесь — переменные темы и компоненты.
   ========================================================================== */

:root {
  /* Значения по умолчанию (тёмная тема Telegram) — перезаписываются из JS */
  --tg-bg: #17212b;
  --tg-card: #1f2b38;
  --tg-secondary: #232e3c;
  --tg-text: #ffffff;
  --tg-hint: #7d8b99;
  --tg-link: #6ab2f2;
  --tg-button: #2ea6ff;
  --tg-button-text: #ffffff;
  --tg-line: rgba(255, 255, 255, 0.08);
  --tg-accent: #2ea6ff;
  --tg-accent-text: #ffffff;
  --tg-danger: #ff6b6b;
  --tg-success: #4ad991;
  --tg-warning: #ffb547;
  --tg-radius: 16px;
  --tg-topbar-h: 56px;
  --tg-tabbar-h: 60px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Светлая тема: мягкий фон и читаемые подписи */
html[data-theme='light'] {
  --tg-line: rgba(0, 0, 0, 0.08);
  --tg-danger: #e5484d;
  --tg-success: #17915b;
  --tg-warning: #b26a00;
}

* { -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--tg-bg);
  color: var(--tg-text);
  overscroll-behavior-y: none;
}

body { min-height: 100vh; }

/* --------------------------------------------------------------------------
   Каркас: шапка + прокручиваемый контент + таб-бар
   -------------------------------------------------------------------------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 30;
  height: calc(var(--tg-topbar-h) + var(--safe-top));
  padding: var(--safe-top) 10px 0;
  display: flex;
  align-items: center;
  gap: 8px;
  background: color-mix(in srgb, var(--tg-bg) 92%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--tg-line);
}

.content {
  padding: calc(var(--tg-topbar-h) + var(--safe-top) + 10px) 12px
           calc(var(--tg-tabbar-h) + var(--safe-bottom) + 18px);
  min-height: 100vh;
  animation: fade-in 0.18s ease-out;
}

.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 30;
  height: calc(var(--tg-tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--tg-bg) 94%, transparent);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--tg-line);
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 0;
  background: transparent;
  color: var(--tg-hint);
  font-size: 10px;
  font-weight: 500;
  transition: color 0.15s ease, transform 0.12s ease;
}
.tab:active { transform: scale(0.94); }
.tab-icon { font-size: 20px; line-height: 1; }
.tab.is-active { color: var(--tg-accent); }
.tab.is-active .tab-icon { transform: translateY(-1px); }

/* --------------------------------------------------------------------------
   Кнопки
   -------------------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 600;
  padding: 10px 14px;
  transition: transform 0.12s ease, opacity 0.15s ease, background 0.15s ease;
  background: var(--tg-secondary);
  color: var(--tg-text);
  text-decoration: none;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; }
.btn-primary { background: var(--tg-button); color: var(--tg-button-text); }
.btn-ghost { background: transparent; border: 1px solid var(--tg-line); }
.btn-danger { background: transparent; color: var(--tg-danger); border: 1px solid var(--tg-danger); }
.btn-block { width: 100%; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--tg-text);
  font-size: 18px;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { background: var(--tg-secondary); }

/* --------------------------------------------------------------------------
   Карточки, списки, строки
   -------------------------------------------------------------------------- */
.card {
  background: var(--tg-card);
  border: 1px solid var(--tg-line);
  border-radius: var(--tg-radius);
  padding: 14px;
}
.card + .card { margin-top: 10px; }
.card-title { font-size: 14px; font-weight: 600; }
.muted { color: var(--tg-hint); }

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 14px;
  background: var(--tg-card);
  border: 1px solid var(--tg-line);
  margin-bottom: 8px;
  text-align: left;
  width: 100%;
  color: inherit;
}
.row:active { background: var(--tg-secondary); }
.row-avatar {
  width: 44px; height: 44px;
  flex: 0 0 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 15px;
  background: linear-gradient(140deg, var(--tg-button), var(--tg-accent));
  color: #fff;
}
.row-main { min-width: 0; flex: 1; }
/* display:block — иначе Tailwind-класс truncate не обрежет текст в inline-элементе */
.row-title { display: block; font-size: 14px; font-weight: 600; }
.row-sub { display: block; font-size: 12px; color: var(--tg-hint); }
.row-meta { font-size: 11px; color: var(--tg-hint); white-space: nowrap; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  background: var(--tg-secondary);
  color: var(--tg-hint);
  border: 1px solid transparent;
}
.chip.is-active { color: var(--tg-accent); border-color: var(--tg-accent); }
.chip-del { color: var(--tg-danger); }
.chip-edit { color: var(--tg-warning); }
.chip-one { color: #ff8a3d; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--tg-secondary);
  color: var(--tg-hint);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.badge-deleted { color: var(--tg-danger); }
.badge-edited { color: var(--tg-warning); }
.badge-one-time { color: #ff8a3d; }
.badge-media { color: var(--tg-accent); }

/* --------------------------------------------------------------------------
   Сообщения архива
   -------------------------------------------------------------------------- */
.msg { padding: 12px 14px; }
.msg-head {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.msg-sender { font-size: 13px; font-weight: 600; }
.msg-time { font-size: 11px; color: var(--tg-hint); }
.msg-text {
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-out { color: var(--tg-link); }
.msg-locked {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  margin-top: 8px;
}
.msg-locked-blur { filter: blur(12px); opacity: 0.55; user-select: none; }
.msg-locked-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  padding: 12px;
  background: color-mix(in srgb, var(--tg-card) 65%, transparent);
  font-size: 12px;
}
.msg-locked-overlay .lock { font-size: 22px; }

/* Было / Стало + HTML-diff */
.diff {
  margin-top: 8px;
  border-radius: 12px;
  background: var(--tg-secondary);
  padding: 10px;
  font-size: 13px;
  line-height: 1.5;
}
.diff-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--tg-hint); margin-bottom: 4px; }
.diff s { color: var(--tg-danger); text-decoration: line-through; background: color-mix(in srgb, var(--tg-danger) 14%, transparent); border-radius: 4px; padding: 0 2px; }
.diff u { color: var(--tg-success); text-decoration: none; background: color-mix(in srgb, var(--tg-success) 16%, transparent); border-radius: 4px; padding: 0 2px; }
.diff-old { color: var(--tg-hint); }

mark {
  background: color-mix(in srgb, var(--tg-warning) 35%, transparent);
  color: inherit;
  border-radius: 4px;
  padding: 0 2px;
}

/* --------------------------------------------------------------------------
   Медиа
   -------------------------------------------------------------------------- */
.media-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.media-tile {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--tg-line);
  background: var(--tg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--tg-hint);
  font-size: 22px;
}
.media-tile img, .media-tile video {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}
.media-tile-kind {
  position: absolute;
  left: 5px; bottom: 5px;
  font-size: 14px;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));
}
.media-tile-lock {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, #000 45%, transparent);
  font-size: 20px;
}
.media-inline {
  margin-top: 8px;
  border-radius: 12px;
  overflow: hidden;
  background: var(--tg-secondary);
}
.media-inline img, .media-inline video { width: 100%; display: block; max-height: 380px; object-fit: contain; }
.media-inline audio { width: 100%; display: block; padding: 8px; }
.media-stub {
  padding: 14px;
  text-align: center;
  font-size: 12px;
  color: var(--tg-hint);
}

/* --------------------------------------------------------------------------
   Формы
   -------------------------------------------------------------------------- */
.input, .textarea, .select {
  width: 100%;
  background: var(--tg-secondary);
  border: 1px solid var(--tg-line);
  border-radius: 12px;
  color: var(--tg-text);
  font-size: 14px;
  padding: 11px 12px;
  outline: none;
}
.input:focus, .textarea:focus, .select:focus { border-color: var(--tg-accent); }
.textarea { min-height: 84px; resize: vertical; }
.field { margin-bottom: 10px; }
.field-label { display: block; font-size: 12px; color: var(--tg-hint); margin-bottom: 5px; }

.switch {
  position: relative;
  width: 46px; height: 27px;
  border-radius: 999px;
  background: var(--tg-secondary);
  border: 1px solid var(--tg-line);
  flex: 0 0 46px;
  transition: background 0.18s ease;
}
.switch::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 21px; height: 21px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.18s ease;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}
.switch.is-on { background: var(--tg-button); border-color: var(--tg-button); }
.switch.is-on::after { transform: translateX(19px); }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid var(--tg-line);
}
.toggle-row:last-child { border-bottom: 0; }
.toggle-text { flex: 1; min-width: 0; }

/* --------------------------------------------------------------------------
   Тарифы, рефералка, тикеты
   -------------------------------------------------------------------------- */
.plan {
  position: relative;
  border-radius: var(--tg-radius);
  border: 1px solid var(--tg-line);
  background: var(--tg-card);
  padding: 16px;
  margin-bottom: 12px;
}
.plan.is-highlighted { border-color: var(--tg-accent); box-shadow: 0 0 0 1px var(--tg-accent) inset; }
.plan.is-current { border-color: var(--tg-success); }
.plan-price { font-size: 26px; font-weight: 700; }
.plan-feature { display: flex; gap: 8px; font-size: 13px; padding: 3px 0; }
.plan-feature.is-off { color: var(--tg-hint); text-decoration: line-through; }
.plan-ribbon {
  position: absolute;
  top: -9px; right: 14px;
  font-size: 10px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--tg-accent);
  color: var(--tg-accent-text);
  text-transform: uppercase;
}
.method {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border-radius: 12px;
  border: 1px solid var(--tg-line);
  background: var(--tg-secondary);
  margin-bottom: 8px;
  width: 100%;
  text-align: left;
  color: inherit;
}
.method.is-active { border-color: var(--tg-accent); }
.method-icon { font-size: 20px; }

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stat {
  background: var(--tg-card);
  border: 1px solid var(--tg-line);
  border-radius: 14px;
  padding: 12px;
}
.stat-value { font-size: 20px; font-weight: 700; }
.stat-label { font-size: 11px; color: var(--tg-hint); margin-top: 2px; }

.ticket-msg { padding: 10px 12px; border-radius: 12px; margin-bottom: 8px; max-width: 88%; font-size: 13px; line-height: 1.45; }
.ticket-msg.is-support { background: var(--tg-secondary); border-bottom-left-radius: 4px; }
.ticket-msg.is-mine { background: var(--tg-button); color: var(--tg-button-text); margin-left: auto; border-bottom-right-radius: 4px; }

/* --------------------------------------------------------------------------
   Служебные состояния: скелетоны, пусто, тосты, шторка
   -------------------------------------------------------------------------- */
.skeleton {
  background: linear-gradient(90deg, var(--tg-secondary) 25%, var(--tg-card) 50%, var(--tg-secondary) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite linear;
  border-radius: 12px;
}
.sk-row { height: 66px; margin-bottom: 8px; }
.sk-line { height: 12px; margin-bottom: 8px; }

.empty {
  text-align: center;
  padding: 44px 18px;
  color: var(--tg-hint);
}
.empty-emoji { font-size: 46px; margin-bottom: 10px; }
.empty-title { font-size: 15px; font-weight: 600; color: var(--tg-text); margin-bottom: 5px; }
.empty-text { font-size: 13px; line-height: 1.5; max-width: 280px; margin: 0 auto; }

.spinner {
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 3px solid var(--tg-line);
  border-top-color: var(--tg-accent);
  animation: spin 0.8s linear infinite;
}
.load-more { text-align: center; padding: 14px 0; color: var(--tg-hint); font-size: 12px; }

.toasts {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--tg-tabbar-h) + var(--safe-bottom) + 12px);
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--tg-card);
  border: 1px solid var(--tg-line);
  border-left: 3px solid var(--tg-accent);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.35);
  animation: slide-up 0.2s ease-out;
}
.toast.is-error { border-left-color: var(--tg-danger); }
.toast.is-success { border-left-color: var(--tg-success); }

.sheet { position: fixed; inset: 0; z-index: 70; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.5); animation: fade-in 0.15s ease-out; }
.sheet-body {
  position: relative;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--tg-bg);
  border-radius: 20px 20px 0 0;
  border-top: 1px solid var(--tg-line);
  padding: 16px 14px calc(20px + var(--safe-bottom));
  animation: slide-up 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet-handle { width: 42px; height: 4px; border-radius: 999px; background: var(--tg-line); margin: 0 auto 12px; }

.viewer { position: fixed; inset: 0; z-index: 80; background: rgba(0, 0, 0, 0.94); display: flex; align-items: center; justify-content: center; }
.viewer-inner { width: 100%; padding: 16px; text-align: center; }
.viewer-inner img, .viewer-inner video { max-width: 100%; max-height: 82vh; border-radius: 10px; }

.dot {
  position: absolute;
  top: 4px; right: 4px;
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--tg-danger);
}

.hidden { display: none !important; }
.flex-col-center { display: flex; flex-direction: column; align-items: center; justify-content: center; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-up { from { transform: translateY(14px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.sheet .sheet-body { animation-name: sheet-up; }
@keyframes sheet-up { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* ---------------------------------------------------------------------------
   Подсказка про одноразовые медиа.
   Telegram не отдаёт боту содержимое самоуничтожающихся медиа — без объяснения
   пользователь просто потеряет данные и не поймёт, почему.
   --------------------------------------------------------------------------- */
.hint-banner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  margin: 0 0 10px;
  border-radius: 14px;
  background: linear-gradient(135deg,
    color-mix(in srgb, var(--tg-button-color, #2AABEE) 16%, transparent),
    color-mix(in srgb, var(--tg-button-color, #2AABEE) 6%, transparent));
  border: 1px solid color-mix(in srgb, var(--tg-button-color, #2AABEE) 35%, transparent);
  cursor: pointer;
}

.hint-icon { font-size: 20px; line-height: 1.1; }

.hint-text { display: flex; flex-direction: column; gap: 3px; flex: 1; font-size: 13px; }
.hint-sub { opacity: .78; line-height: 1.35; }

.hint-close {
  border: 0;
  background: transparent;
  color: inherit;
  opacity: .55;
  font-size: 14px;
  line-height: 1;
  padding: 2px 4px;
  cursor: pointer;
}
.hint-close:hover { opacity: 1; }

/* @username рядом с именем отправителя */
.msg-handle { color: var(--tg-link-color, #2AABEE); opacity: .9; font-weight: 500; }


/* ---------------------------------------------------------------------------
   Иконки- SVG вместо эмодзи.
   Эмодзи рисуются системным шрифтом: на части устройств вместо иконки
   видеокружка появлялся большой красный кружок, похожий на «битую картинку».
   SVG выглядит одинаково везде.
   --------------------------------------------------------------------------- */
.svg-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1em;
  height: 1em;
  flex: 0 0 auto;
  vertical-align: -0.14em;
}
.svg-icon > svg { width: 100%; height: 100%; display: block; }

.tab-icon.svg-icon { width: 22px; height: 22px; }
.icon-btn .svg-icon { width: 20px; height: 20px; }
.badge .svg-icon { width: 13px; height: 13px; }

/* ---------------------------------------------------------------------------
   Плитки медиатеки.
   --------------------------------------------------------------------------- */
.media-tile {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  aspect-ratio: 1 / 1;
  padding: 8px;
  border: 1px solid var(--tg-line);
  border-radius: 14px;
  background: var(--tg-card);
  color: var(--tg-text);
  overflow: hidden;
  cursor: pointer;
}

.media-tile > img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.media-tile-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--tg-accent, #5288c1) 18%, transparent);
  color: var(--tg-accent, #5288c1);
  font-size: 22px;
}

.media-tile-play {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -60%);
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: rgba(0, 0, 0, .45);
  color: #fff;
  font-size: 22px;
  pointer-events: none;
}

.media-tile-caption {
  font-size: 11px;
  opacity: .72;
  text-align: center;
  line-height: 1.2;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.media-tile > img + .media-tile-caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 6px 5px;
  background: linear-gradient(transparent, rgba(0, 0, 0, .65));
  color: #fff;
  opacity: 1;
}

.media-tile-stub { opacity: .75; cursor: default; }
.media-tile-lock {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .35);
  color: #fff;
  font-size: 22px;
}

/* Лимиты тарифа: подпись слева, значение справа — без наездов друг на друга */
.limit-list { display: flex; flex-direction: column; gap: 6px; font-size: 12px; }
.limit-list li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}
.limit-list li > span { opacity: .72; }
.limit-list li > b { font-weight: 600; white-space: nowrap; }
