/* Цвета берутся из темы Telegram; вне Telegram — из системной темы. */
:root {
  --fb-bg: #0f0f10;
  --fb-bg2: #1c1c1e;
  --fb-text: #f2f2f5;
  --fb-hint: #8e8e93;
  --fb-link: #6ab3f3;
  --fb-accent: #3e88f7;
  --fb-accent-text: #ffffff;
  --fb-danger: #ff5c5c;
}
@media (prefers-color-scheme: light) {
  :root {
    --fb-bg: #ffffff;
    --fb-bg2: #f1f1f4;
    --fb-text: #111114;
    --fb-hint: #7c7c82;
    --fb-link: #2a7ae2;
    --fb-accent: #2a7ae2;
    --fb-danger: #e0342f;
  }
}
:root {
  --bg: var(--tg-theme-bg-color, var(--fb-bg));
  --bg2: var(--tg-theme-secondary-bg-color, var(--fb-bg2));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --accent: var(--tg-theme-button-color, var(--fb-accent));
  --accent-text: var(--tg-theme-button-text-color, var(--fb-accent-text));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));
  --line: color-mix(in srgb, var(--text) 10%, transparent);
  --radius: 14px;
  --radius-sm: 10px;
  --pad: 12px;
  --top: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px));
  --bottom: calc(var(--tg-safe-area-inset-bottom, 0px) + var(--tg-content-safe-area-inset-bottom, 0px));
  color-scheme: light dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
}
#app { position: fixed; inset: 0; }
button, input, textarea, select { font: inherit; color: inherit; }
img { display: block; }
.hidden, [hidden] { display: none !important; }

/* Экраны лежат стопкой; у каждого свой скролл, поэтому «назад» возвращает на то же место. */
.screen {
  position: absolute; inset: 0; overflow-y: auto; overflow-x: hidden;
  background: var(--bg); padding-bottom: calc(var(--bottom) + 24px);
  -webkit-overflow-scrolling: touch;
}
.screen.covered { display: none; }
.wrap { max-width: 760px; margin: 0 auto; }

/* Шапка */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  height: calc(52px + var(--top)); padding: var(--top) var(--pad) 0;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 {
  flex: 1; margin: 0; font-size: 19px; font-weight: 700; letter-spacing: .2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border: 0; border-radius: 12px;
  background: transparent; color: var(--text); cursor: pointer; flex: none;
}
.icon-btn:active { background: var(--bg2); }
.icon-btn svg { width: 22px; height: 22px; }

/* Панель каналов */
.chanbar {
  display: flex; gap: 6px; overflow-x: auto; padding: 4px var(--pad) 10px;
  scrollbar-width: none;
}
.chanbar::-webkit-scrollbar { display: none; }
.chan-chip {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  width: 76px; flex: none; padding: 6px 0; border: 0; border-radius: var(--radius-sm);
  background: transparent; cursor: pointer;
}
.chan-chip:active { background: var(--bg2); }
.chan-chip .avatar { width: 56px; height: 56px; }
.chan-chip.active .avatar { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.chan-chip span {
  width: 100%; font-size: 12px; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.avatar {
  flex: none; border-radius: 50%; object-fit: cover; background: var(--bg2);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--hint); overflow: hidden;
}

/* Виртуальный список: строки позиционируются внутри «распорки» и переиспользуются. */
.vlist { position: relative; }
.vrow { position: absolute; left: 0; right: 0; top: 0; will-change: transform; }
.vrow[hidden] { display: none !important; }

/* Карточка публикации — одна высота у всех карточек одного вида */
.card {
  display: flex; flex-direction: column; height: 100%; overflow: hidden;
  margin: 0 var(--pad); padding-bottom: 16px; cursor: pointer;
}
.card-media {
  position: relative; width: 100%; aspect-ratio: 16 / 9; flex: none;
  border-radius: var(--radius); overflow: hidden; background: var(--bg2);
}
.card-media img { width: 100%; height: 100%; object-fit: cover; }
.card-media .text-cover {
  position: absolute; inset: 0; padding: 16px; font-size: 16px; line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden;
  white-space: pre-line;
}
.badge {
  position: absolute; right: 8px; bottom: 8px; padding: 2px 6px; border-radius: 6px;
  background: rgba(0, 0, 0, .72); color: #fff; font-size: 12px; font-weight: 600;
}
.badge.left { right: auto; left: 8px; }
.card-info { display: flex; gap: 10px; padding-top: 10px; height: 76px; }
.card-info .avatar { width: 36px; height: 36px; font-size: 14px; }
.card-text { flex: 1; min-width: 0; }
.card-title {
  font-weight: 600; font-size: 15px; line-height: 20px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.meta {
  color: var(--hint); font-size: 13px; margin-top: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card-ad {
  margin: 0 var(--pad); height: calc(100% - 16px); border-radius: var(--radius);
  background: var(--bg2); padding: 10px 12px; display: flex; flex-direction: column; gap: 8px;
  justify-content: center;
}
.ad-label { font-size: 12px; color: var(--hint); }

.empty { padding: 48px 24px; text-align: center; color: var(--hint); }
.loading-line { padding: 16px; text-align: center; color: var(--hint); font-size: 13px; }

/* Страница канала */
.chan-cover { aspect-ratio: 3 / 1; background: var(--bg2); overflow: hidden; }
.chan-cover img { width: 100%; height: 100%; object-fit: cover; }
.chan-head { display: flex; gap: 14px; align-items: center; padding: 14px var(--pad) 6px; }
.chan-head .avatar { width: 72px; height: 72px; font-size: 26px; }
.chan-head h2 { margin: 0; font-size: 20px; }
.chan-desc { padding: 4px var(--pad) 10px; color: var(--hint); white-space: pre-line; }
.tabs {
  display: flex; gap: 6px; margin: 6px var(--pad) 14px; padding: 4px;
  background: var(--bg2); border-radius: 12px;
}
.tab {
  flex: 1; height: 36px; border: 0; border-radius: 9px; background: transparent;
  font-weight: 600; cursor: pointer; color: var(--hint);
}
.tab.active { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }

/* Плеер */
.player {
  position: relative; width: 100%; aspect-ratio: 16 / 9; background: #000; overflow: hidden;
}
.player video { width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.player-poster { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.player-play {
  position: absolute; inset: 0; margin: auto; width: 76px; height: 76px; border-radius: 50%;
  border: 0; background: rgba(0, 0, 0, .6); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.player-play svg { width: 34px; height: 34px; margin-left: 4px; }
.player-play[disabled] { opacity: .6; }
.player-full {
  position: absolute; top: 8px; right: 8px; width: 40px; height: 40px; border-radius: 12px;
  border: 0; background: rgba(0, 0, 0, .5); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center; z-index: 2;
}
.player-full svg { width: 22px; height: 22px; }
.player-msg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 24px; text-align: center; color: #fff; background: rgba(0, 0, 0, .7);
}
.player.theater {
  position: fixed; inset: 0; z-index: 50; aspect-ratio: auto;
  padding: var(--top) 0 var(--bottom);
}

.item-body { padding: 14px var(--pad); }
.item-body h2 { margin: 0 0 8px; font-size: 19px; line-height: 1.3; }
.chan-row {
  display: flex; align-items: center; gap: 10px; padding: 10px 0; cursor: pointer;
}
.chan-row .avatar { width: 40px; height: 40px; }
.chan-row b { display: block; }
.desc { white-space: pre-line; overflow-wrap: anywhere; line-height: 1.45; }
.desc.clamped { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.desc a, .post-text a { color: var(--link); }
.more { color: var(--link); background: none; border: 0; padding: 6px 0; cursor: pointer; }
.actions { display: flex; gap: 8px; margin: 12px 0; }
.section-title { margin: 18px var(--pad) 10px; font-size: 16px; font-weight: 700; }
.post-text { white-space: pre-line; overflow-wrap: anywhere; font-size: 16px; line-height: 1.5; }
.post-images { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.post-images img { width: 100%; border-radius: var(--radius-sm); background: var(--bg2); }

/* Кнопки и поля */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 42px; padding: 0 16px; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--bg2); color: var(--text); font-weight: 600; text-align: center;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.wide { flex: 1; }
.btn:disabled { opacity: .5; cursor: default; }
.btn svg { width: 18px; height: 18px; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.form { padding: 4px var(--pad) 12px; display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 13px; color: var(--hint); padding-left: 4px; }
.input, .select, .textarea {
  width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg2); outline: none;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent); }
.textarea { min-height: 120px; resize: vertical; line-height: 1.4; }
.select { appearance: none; -webkit-appearance: none; }
.field-status { font-size: 12px; color: var(--hint); min-height: 16px; padding-left: 4px; }
.field-status.error { color: var(--danger); }

.progress { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.box {
  padding: 12px; border-radius: var(--radius); background: var(--bg2);
  display: flex; flex-direction: column; gap: 10px;
}
.box .note { color: var(--hint); font-size: 13px; }
.box .note.error { color: var(--danger); }
.preview { width: 100%; aspect-ratio: 16 / 9; border-radius: var(--radius-sm); object-fit: cover; background: var(--bg); }
.preview.square { width: 96px; height: 96px; aspect-ratio: auto; border-radius: 50%; }
.preview.wide { aspect-ratio: 3 / 1; }

.thumbs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.thumb { position: relative; aspect-ratio: 1; border-radius: var(--radius-sm); overflow: hidden; background: var(--bg); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb-bar {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; justify-content: space-between;
  background: rgba(0, 0, 0, .55);
}
.thumb-bar button {
  flex: 1; height: 32px; border: 0; background: transparent; color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.thumb-bar button:disabled { opacity: .35; }
.thumb-bar svg { width: 18px; height: 18px; }

/* Списки админки */
.alist { display: flex; flex-direction: column; }
.arow {
  display: flex; align-items: center; gap: 10px; height: 72px; padding: 0 var(--pad);
  cursor: pointer; border-bottom: 1px solid var(--line);
}
.arow:active { background: var(--bg2); }
.arow .thumbnail {
  width: 96px; height: 54px; flex: none; border-radius: 8px; object-fit: cover; background: var(--bg2);
}
.arow .avatar { width: 48px; height: 48px; }
.arow-text { flex: 1; min-width: 0; }
.arow-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.arow-order { display: flex; flex-direction: column; }
.arow-order .icon-btn { width: 36px; height: 30px; }
.state-ok { color: #34c759; }
.state-bad { color: var(--danger); }

.segmented { margin: 4px var(--pad) 12px; }
.admin-bar { display: flex; gap: 8px; padding: 0 var(--pad) 12px; }
.admin-bar > * { flex: 1; }

.unit { padding: 12px; border-radius: var(--radius); background: var(--bg2); display: flex; flex-direction: column; gap: 10px; }
.unit .input, .unit .select { background: var(--bg); }
.unit-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.unit-stats div { background: var(--bg); border-radius: 10px; padding: 6px 2px; }
.unit-stats b { display: block; font-size: 15px; }
.unit-stats span { font-size: 11px; color: var(--hint); }
.switch { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.switch input { width: 46px; height: 28px; accent-color: var(--accent); }
.units { display: flex; flex-direction: column; gap: 12px; padding: 0 var(--pad); }
