/* ==========================================================================
   Avatar One Mini App — Phase 1 (App Shell + Chat)
   Тёмная тема — канонический вид. Визуальный язык сверен с /v2/test.
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  /* --- палитра (dark canon) --- */
  --bg: #141414;
  --surface-2: #202020;
  --surface-3: #2a2a2a;
  --surface-4: #313131;

  --text: #ffffff;
  --text-dim: #f0f0f0;
  --text-2: #909090;

  --border: #313131;

  --out-bg: #f0f0f0;
  --out-text: #141414;
  --in-bg: #202020;
  --in-text: #f0f0f0;

  --radius-bubble: 16px;
  --radius-item: 8px;
  --radius-chip: 10px;
  --sidebar-w: 300px;

  /* --- safe-area: считается из реального окружения, без хардкода --- */

  /* только вырез устройства (статус-бар / Dynamic Island) */
  --safe-top-device: var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px));

  /* вырез устройства + полоса, зарезервированная под элементы управления Telegram */
  --safe-top: calc(
    var(--safe-top-device) +
    var(--tg-content-safe-area-inset-top, 0px)
  );
  --safe-bottom: calc(
    var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)) +
    var(--tg-content-safe-area-inset-bottom, 0px)
  );
  --safe-left: var(--tg-safe-area-inset-left, env(safe-area-inset-left, 0px));
  --safe-right: var(--tg-safe-area-inset-right, env(safe-area-inset-right, 0px));
}

html,
body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}

img {
  display: block;
  max-width: 100%;
}

svg {
  display: block;
}

/* --------------------------------------------------------------------------
   App shell
   -------------------------------------------------------------------------- */

#app {
  position: relative;
  height: 100vh;
  height: 100dvh;
  height: var(--tg-viewport-stable-height, 100dvh);
  overflow: hidden;
  background: var(--bg);
}

.view-root {
  height: 100%;
}

.chat-view,
.feed-view,
.avatars-view,
.avatar-profile-view {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}

/* --------------------------------------------------------------------------
   Avatar chip + badge (переиспользуемый элемент)
   -------------------------------------------------------------------------- */

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  flex: none;
  background: transparent;
}

.avatar--sm { width: 28px; height: 28px; }
.avatar--chip { width: 32px; height: 32px; }
.avatar--xs { width: 20px; height: 20px; }
.avatar--lg { width: 88px; height: 88px; }

.avatar-wrap {
  position: relative;
  display: inline-flex;
  flex: none;
}

.avatar-badge {
  position: absolute;
  left: 50%;
  bottom: -6px;
  transform: translateX(-50%);
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid transparent;
  color: var(--text-2);
  font-size: 9px;
  line-height: 1;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   Sidebar
   -------------------------------------------------------------------------- */

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
  z-index: 30;
}

.is-sidebar-open .sidebar-backdrop {
  opacity: 1;
  pointer-events: auto;
}

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-w);
  max-width: 86vw;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  z-index: 40;
  /* логотип живёт в одной полосе с элементами управления Telegram (они справа,
     логотип слева) — поэтому клирим только вырез устройства, не контент-инсет */
  padding-top: var(--safe-top-device);
  padding-bottom: var(--safe-bottom);
  padding-left: var(--safe-left);
}

.is-sidebar-open .sidebar {
  transform: translateX(0);
}

.sidebar-header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 8px 12px;
}

.sidebar-logo-btn {
  display: block;
  padding: 4px 0;
  border-radius: var(--radius-item);
}

.sidebar-logo {
  height: 20px;
  width: auto;
}

.sidebar-nav {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  height: 46px;
  padding: 0 12px;
  border-radius: var(--radius-item);
  color: var(--text-dim);
  font-size: 17px;
  text-align: left;
}

.nav-item.is-active {
  background: var(--surface-2);
}

/* nav-пункты будущих разделов — визуальные, технически инертны */
.nav-item[aria-disabled="true"] {
  cursor: default;
}

.nav-icon {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  color: var(--text-2);
  flex: none;
}

.nav-icon svg { width: 20px; height: 20px; }

.sidebar-section-label {
  flex: none;
  padding: 12px 20px 6px;
  font-size: 14px;
  color: var(--text-2);
}

.sidebar-chats {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  height: 58px;
  padding: 0 12px;
  border-radius: var(--radius-item);
  text-align: left;
  color: var(--text-dim);
  font-size: 17px;
}

.chat-item.is-active {
  background: var(--surface-2);
}

.chat-item-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-account {
  position: relative;
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: auto;
  padding: 14px 12px;
  color: var(--text-dim);
  font-size: 16px;
}

/* разделитель над блоком аккаунта — с отступами, не во всю ширину сайдбара */
.sidebar-account::before {
  content: "";
  position: absolute;
  top: 0;
  left: 12px;
  right: 12px;
  border-top: 1px solid var(--border);
}

/* --------------------------------------------------------------------------
   Chat header
   -------------------------------------------------------------------------- */

.chat-header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  padding-top: calc(var(--safe-top) + 10px);
  padding-left: calc(var(--safe-left) + 12px);
  padding-right: calc(var(--safe-right) + 16px);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-chip);
  color: var(--text);
  flex: none;
}

.icon-btn svg { width: 22px; height: 22px; }

.chat-header-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header-name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   Message list
   -------------------------------------------------------------------------- */

.message-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px;
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
  padding-bottom: 10px;
}

.msg-row {
  display: flex;
  gap: 8px;
  max-width: 100%;
}

.msg-row--in { justify-content: flex-start; }
.msg-row--out { justify-content: flex-end; }

.msg-avatar-slot {
  width: 28px;
  flex: none;
}

.msg-col {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-width: 78%;
  min-width: 0;
}

.msg-row--out .msg-col { align-items: flex-end; }

.msg-name {
  padding-left: 2px;
  margin-bottom: 2px;
  font-size: 13px;
  color: var(--text-2);
}

.msg-bubble {
  border-radius: var(--radius-bubble);
  padding: 10px 14px;
  font-size: 15px;
  line-height: 21px;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-row--in .msg-bubble {
  background: var(--in-bg);
  color: var(--in-text);
}

.msg-row--out .msg-bubble {
  background: var(--out-bg);
  color: var(--out-text);
}

.msg-time {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 0 4px;
  font-size: 12px;
  color: var(--text-2);
}

.msg-check svg {
  width: 14px;
  height: 11px;
}

/* --------------------------------------------------------------------------
   Composer
   -------------------------------------------------------------------------- */

.composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 12px 16px;
  padding-bottom: calc(var(--safe-bottom) + 12px);
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
}

.composer-field {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  min-height: 44px;
  padding: 5px 5px 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-bubble);
  background: var(--bg);
  transition: border-color 0.15s ease;
}

.composer-field.is-focused {
  border-color: var(--text-2);
}

.composer-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: var(--radius-item);
  color: var(--text-2);
}

.composer-btn svg { width: 20px; height: 20px; }

.composer-input {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  background: none;
  resize: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 20px;
  max-height: 96px;
  padding: 6px 2px;
}

.composer-input::placeholder {
  color: var(--text-2);
}

.send-btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  flex: none;
  border-radius: 50%;
  background: var(--surface-4);
  color: var(--text-2);
  transition: background 0.15s ease, color 0.15s ease;
}

.send-btn svg { width: 18px; height: 18px; }

.send-btn:not(:disabled) {
  background: var(--out-bg);
  color: var(--out-text);
}

.send-btn:disabled {
  cursor: default;
}

/* --------------------------------------------------------------------------
   Feed (домашний экран)
   -------------------------------------------------------------------------- */

.feed-header,
.profile-header {
  flex: none;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  padding-top: calc(var(--safe-top) + 10px);
  padding-left: calc(var(--safe-left) + 12px);
  padding-right: calc(var(--safe-right) + 16px);
}

/* Шапка Аватаров: бургер слева + лупа справа, симметрично; разделитель — не во всю
   ширину, поля как у сетки карточек (16px). */
.avatars-header {
  position: relative;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  padding-top: calc(var(--safe-top) + 10px);
  padding-left: calc(var(--safe-left) + 12px);
  padding-right: calc(var(--safe-right) + 12px);
}

.avatars-header::after {
  content: "";
  position: absolute;
  left: calc(var(--safe-left) + 16px);
  right: calc(var(--safe-right) + 16px);
  bottom: 0;
  border-bottom: 1px solid var(--border);
}

.feed-scroll,
.avatars-scroll,
.profile-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.feed-grid {
  columns: 3 auto;
  column-gap: 8px;
  padding: 16px;
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
  padding-bottom: calc(var(--safe-bottom) + 16px);
}

.feed-card {
  break-inside: avoid;
  margin: 0 0 14px;
}

.feed-card__media {
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface-2);
  line-height: 0;
}

.feed-card__media img {
  width: 100%;
  height: auto;
}

.feed-card__meta {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  margin-top: 6px;
}

.feed-card__handle {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: var(--text-2);
}

.feed-card__caption {
  margin-top: 4px;
  font-size: 12px;
  line-height: 16px;
  color: var(--text-dim);
}

/* --------------------------------------------------------------------------
   Avatars grid — равномерная сетка: все карточки одного размера, вертикальные,
   выровнены по верху; заполнение слева-направо, сверху-вниз (первый = левый верх).
   -------------------------------------------------------------------------- */

.avatars-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  align-content: start;
  padding: 16px;
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
  padding-bottom: calc(var(--safe-bottom) + 16px);
}

.avatar-card {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 3 / 4;
  border-radius: 12px;
  overflow: hidden;
  text-align: left;
}

.avatar-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* нижний градиент под именем — лёгкий overlay, не сплошная плашка */
.avatar-card::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 55%;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0));
  pointer-events: none;
}

.avatar-card__name {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 8px;
  z-index: 1;
  color: #ffffff;
  font-size: 14px;
  font-weight: 400;
  line-height: 1.2;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.45);
}

/* --------------------------------------------------------------------------
   Avatar profile
   -------------------------------------------------------------------------- */

.profile-back svg {
  width: 22px;
  height: 22px;
}

.profile-cover {
  flex: none;
}

.profile-cover img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 190px;
  object-fit: cover;
}

.profile-id {
  position: relative;
  margin-top: -32px;
  padding: 0 16px;
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
}

/* Тонкий cutout цвета фона страницы (#141414 / var(--bg)) вокруг круглого
   аватара и пилюли «АВАТАР» — отделяет их от обложки как одну композицию.
   box-shadow не меняет layout-box: геометрия, позиция и размеры сохраняются.
   Только на странице профиля (Sidebar/лента/чат не затрагиваются). */
.profile-id .avatar {
  box-shadow: 0 0 0 2px var(--bg);
}

.profile-id .avatar-badge {
  box-shadow: 0 0 0 2px var(--bg);
}

.profile-name {
  margin-top: 14px;
  font-size: 22px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text);
}

.profile-handle {
  display: block;
  margin-top: 2px;
  font-size: 14px;
  color: var(--text-2);
}

.profile-tabs {
  display: flex;
  gap: 6px;
  margin-top: 16px;
  padding: 0 16px;
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
  border-bottom: 1px solid var(--border);
}

.profile-tab {
  padding: 8px 4px 10px;
  font-size: 15px;
  color: var(--text-2);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}

.profile-tab.is-active {
  color: var(--text);
  border-bottom-color: var(--text);
}

.profile-panels {
  padding: 16px;
  padding-left: calc(var(--safe-left) + 16px);
  padding-right: calc(var(--safe-right) + 16px);
  padding-bottom: calc(var(--safe-bottom) + 16px);
}

.profile-panel {
  display: none;
}

.profile-panel.is-active {
  display: block;
}

.profile-about {
  font-size: 15px;
  line-height: 21px;
  color: var(--text-dim);
}

.profile-empty {
  padding: 28px 16px;
  text-align: center;
  font-size: 14px;
  color: var(--text-2);
}

.profile-feed-grid {
  columns: 3 auto;
  column-gap: 8px;
}

.profile-post {
  break-inside: avoid;
  margin: 0 0 10px;
}

.profile-lists {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-card {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
}

.list-card__title {
  font-size: 15px;
  color: var(--text);
}

.list-card__count {
  margin-top: 2px;
  font-size: 13px;
  color: var(--text-2);
}
