/* ==========================================================================
   Kosmo — переписка, поле ввода, меню чатов, редактирование профиля,
   панель «Designs», формы аватарок Material 3.
   ========================================================================== */

/* ---------- Аватары: фото и формы Material 3 ---------- */
.avatar img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
}
.avatar.has-photo > span { opacity: 0; }
html[data-theme="material"] .chat .avatar,
html[data-theme="material"] .avatar-head,
html[data-theme="material"] .post-head .avatar,
html[data-theme="material"] .avatar-empty { clip-path: var(--shape-chat); border-radius: 0; }
html[data-theme="material"] .avatar-hero,
html[data-theme="material"] .avatar-edit { clip-path: var(--shape-profile); border-radius: 0; }
html[data-theme="material"] .hero-orbit { display: none; }
html[data-theme="material"] .hero-avatar { filter: drop-shadow(0 10px 26px color-mix(in srgb, var(--md-sys-color-primary) 35%, transparent)); }

/* ---------- Профиль: кнопки «редактировать» ---------- */
.hero-avatar {
  border: 0;
  padding: 0;
  background: none;
  cursor: pointer;
  transition: scale 300ms var(--spring-bouncy);
}
.hero-avatar:active { scale: 0.96; }
.hero-edit {
  position: absolute;
  right: 8px; bottom: 8px;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: #fff;
  color: #000;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
}
.hero-edit svg { width: 18px; height: 18px; }
html[data-theme="material"] .hero-edit { background: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); border-radius: 12px; }
.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  margin-top: 12px;
  padding: 0 16px;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  font-size: 14px;
  font-weight: 600;
  transition: scale 300ms var(--spring-bouncy);
}
.pill-btn:active { scale: 0.94; }
.pill-btn svg { width: 16px; height: 16px; }
html[data-theme="material"] .pill-btn { background: var(--md-sys-color-secondary-container); color: var(--md-sys-color-on-secondary-container); font-weight: 500; }

/* ---------- Список чатов: архив, удержание ---------- */
.archive-avatar { background: rgba(255, 255, 255, 0.1); color: var(--c-text-2); }
.archive-avatar svg { width: 26px; height: 26px; }
html[data-theme="material"] .archive-avatar { background: var(--md-sys-color-surface-container-highest); }
.archive-list { list-style: none; animation: chat-in 380ms var(--spring) both; }
.archive-list .chat-list { padding: 0; }
.list-empty {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 80px 24px; color: var(--c-text-3); text-align: center; list-style: none;
}
.list-empty svg { width: 44px; height: 44px; stroke-width: 1.6; }

.chat.is-pressing { scale: 0.98; transition-delay: 120ms; }
.chat.is-held {
  z-index: 31;
  scale: 1.02;
  background: #1c1c1e !important;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
}
html[data-theme="material"] .chat.is-held { background: var(--md-sys-color-surface-container-high) !important; }
.ctx-scrim {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.45);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade-in 220ms ease both;
}
.ctx-scrim.is-closing { animation: fade-out 160ms ease both; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
.ctx-menu { position: fixed; z-index: 32; left: 16px; width: 230px; }
.ctx-confirm { margin: 6px 12px 8px; font-size: 14px; line-height: 1.4; color: var(--c-text-2); }
.menu-item.danger { color: var(--c-error); }

/* ---------- Сообщения ---------- */

.chat-wallpaper { position: absolute; inset: 0; z-index: 0; }
.bubble-row { display: flex; }
.bubble-row.mine { justify-content: flex-end; }
.bubble {
  position: relative;
  max-width: min(78%, 460px);
  padding: 8px 12px;
  border-radius: 20px;
  background: #26262a;
  text-align: left;
}
.theirs .bubble { border-bottom-left-radius: 6px; }
.mine .bubble { background: var(--c-accent); color: #fff; border-bottom-right-radius: 6px; }
html[data-theme="material"] .theirs .bubble { background: var(--md-sys-color-surface-container-high); }
html[data-theme="material"] .mine .bubble { background: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); }
.bubble-row.is-new .bubble { animation: bubble-in 520ms var(--spring-bouncy) both; }
.theirs.is-new .bubble { transform-origin: 0 100%; }
.mine.is-new .bubble { transform-origin: 100% 100%; }
@keyframes bubble-in { from { opacity: 0; transform: translateY(14px) scale(0.8); } }

.b-text { margin: 0; font-size: 16px; line-height: 1.38; overflow-wrap: anywhere; }
/* время «обтекается» текстом, как в мессенджерах */
.b-meta {
  float: right;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin: 6px -4px -4px 10px;
  font-size: 11px;
  line-height: 14px;
  opacity: 0.72;
  font-variant-numeric: tabular-nums;
}
.b-meta svg { width: 15px; height: 15px; stroke-width: 2.2; }

.kind-photo, .kind-video { padding: 4px; }
.b-photo-btn { display: block; padding: 0; border: 0; background: none; cursor: zoom-in; }
.b-photo {
  display: block;
  width: min(260px, 62vw);
  max-height: 360px;
  object-fit: cover;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.06);
}
.b-video { display: block; width: min(280px, 66vw); max-height: 360px; border-radius: 16px; background: #000; }
.kind-photo .b-meta, .kind-video .b-meta {
  position: absolute;
  right: 10px; bottom: 10px;
  float: none;
  margin: 0;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  opacity: 1;
}
.kind-video .b-meta { top: 10px; bottom: auto; }

.b-player { display: flex; align-items: center; gap: 10px; min-width: min(240px, 60vw); }
.b-play {
  flex: none;
  width: 42px; height: 42px;
  display: grid; place-items: center;
  border: 0;
  border-radius: 50%;
  transition: scale 300ms var(--spring-bouncy);
}
.b-play:active { scale: 0.9; }
.b-play svg { width: 20px; height: 20px; }
.mine .b-play { background: #fff; color: var(--c-accent); }
.theirs .b-play { background: var(--c-accent); color: #fff; }
html[data-theme="material"] .mine .b-play { background: var(--md-sys-color-on-primary); color: var(--md-sys-color-primary); }
html[data-theme="material"] .theirs .b-play { background: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); }
.b-track { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.b-wave-wrap { position: relative; height: 26px; }
.b-wave { position: absolute; inset: 0; display: flex; align-items: center; gap: 2px; opacity: 0.38; }
.b-wave span { flex: 1; min-height: 3px; border-radius: 2px; background: currentColor; }
/* прослушанная часть волны — та же волна, обрезанная по прогрессу */
.b-wave-fill { opacity: 1; clip-path: inset(0 calc((1 - var(--p, 0)) * 100%) 0 0); }
.b-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; font-weight: 600; }
.b-progress { height: 4px; border-radius: 2px; background: color-mix(in srgb, currentColor 25%, transparent); overflow: hidden; }
.b-progress span { display: block; height: 100%; width: calc(var(--p, 0) * 100%); background: currentColor; }
.b-dur { font-size: 12px; opacity: 0.75; font-variant-numeric: tabular-nums; }

.kind-code { padding: 6px; width: min(78%, 460px); }
.b-code { border-radius: 14px; background: rgba(0, 0, 0, 0.38); overflow: hidden; }
.b-code-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 4px 12px; font-size: 12px; font-weight: 600; opacity: 0.85; }
.b-code-head button { width: 32px; height: 32px; display: grid; place-items: center; border: 0; border-radius: 10px; background: none; }
.b-code-head svg { width: 16px; height: 16px; }
.b-code pre {
  margin: 0;
  padding: 4px 12px 12px;
  overflow-x: auto;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #e8e8ea;
  tab-size: 2;
}
.kind-code .b-meta { margin: 6px 4px 0 10px; }

/* Пустой чат: аватар на «орбите» */
.chat-empty {
  margin: auto;
  max-width: 330px;
  border-radius: 32px;
  background: color-mix(in srgb, var(--page-bg) 72%, transparent);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 40px 24px;
  text-align: center;
  animation: chat-in 640ms var(--spring) both;
}
.chat-empty h3 { margin: 18px 0 0; font-size: 21px; font-weight: 700; letter-spacing: -0.01em; }
.chat-empty p { margin: 0; font-size: 15px; line-height: 1.4; color: var(--c-text-2); }
.chat-empty .btn { height: 46px; margin-top: 16px; padding: 0 22px; font-size: 16px; }
.empty-orbit { position: relative; width: 156px; height: 156px; display: grid; place-items: center; }
.avatar-empty { width: 88px; height: 88px; font-size: 34px; }
.orbit-ring { position: absolute; inset: 0; border-radius: 50%; border: 1.5px dashed rgba(255, 255, 255, 0.24); animation: orbit 24s linear infinite; }
.orbit-ring.r2 { inset: 16px; border: 1px solid rgba(255, 255, 255, 0.12); animation-duration: 16s; animation-direction: reverse; }
.orbit-moon { position: absolute; inset: 0; animation: orbit 7s linear infinite; }
.orbit-moon::after {
  content: "";
  position: absolute;
  top: -6px; left: 50%;
  width: 12px; height: 12px;
  margin-left: -6px;
  border-radius: 50%;
  background: var(--c-accent);
  box-shadow: 0 0 16px var(--c-accent);
}
html[data-theme="material"] .orbit-ring { border-color: color-mix(in srgb, var(--md-sys-color-primary) 45%, transparent); }

/* ---------- Поле ввода: «+», текст, голосовое / отправить ---------- */
.composer {
  position: fixed;
  z-index: 3;
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  width: min(680px, calc(100% - 20px));
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  gap: 8px;
  transform: translateX(-50%);
}
.composer-field {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 48px;
  display: flex;
  align-items: center;
  padding: 0 18px;
  border-radius: 24px;
}
html[data-theme="glass"] .composer-field,
html[data-theme="glass"] .composer .circle-btn.glass { background: rgba(28, 28, 30, 0.66); }
html[data-theme="material"] .composer-field { background: var(--md-sys-color-surface-container-high); }
.composer-field textarea {
  flex: 1;
  min-width: 0;
  max-height: 140px;
  padding: 13px 0;
  border: 0;
  outline: 0;
  background: transparent;
  resize: none;
  font-size: 16px;
  line-height: 22px;
  caret-color: var(--c-accent);
}
.composer-field textarea::placeholder { color: var(--c-text-3); }
.attach-btn svg { transition: rotate 350ms var(--spring-bouncy); }
.attach-btn.is-open svg { rotate: 45deg; }
.send-btn { background: var(--c-accent) !important; color: #fff !important; }
html[data-theme="material"] .send-btn { background: var(--md-sys-color-primary) !important; color: var(--md-sys-color-on-primary) !important; }
.send-btn svg { position: absolute; transition: scale 380ms var(--spring-bouncy), opacity 160ms ease, rotate 380ms var(--spring-bouncy); }
.send-btn[data-mode="voice"] .ic-send { scale: 0.3; opacity: 0; rotate: -90deg; }
.send-btn[data-mode="send"] .ic-mic { scale: 0.3; opacity: 0; rotate: 90deg; }
.ic-send { stroke-width: 2.4; }

.rec-strip { position: absolute; inset: 0; display: none; align-items: center; gap: 10px; padding: 0 6px 0 18px; }
.composer.is-recording .rec-strip { display: flex; }
.composer.is-recording textarea { visibility: hidden; }
.composer.is-recording .send-btn { animation: rec-pulse 1.4s ease-in-out infinite; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: #ff453a; animation: rec-blink 1s steps(1) infinite; }
.rec-time { font-weight: 600; font-variant-numeric: tabular-nums; }
.rec-label { flex: 1; color: var(--c-text-2); font-size: 14px; }
.rec-cancel { border: 0; background: none; padding: 8px 10px; color: var(--c-accent); font-weight: 600; }
@keyframes rec-blink { 50% { opacity: 0.2; } }
@keyframes rec-pulse { 50% { box-shadow: 0 0 0 9px color-mix(in srgb, var(--c-accent) 30%, transparent); } }

.attach-menu {
  position: fixed;
  top: auto;
  bottom: calc(max(12px, env(safe-area-inset-bottom)) + 58px);
  left: max(10px, calc((100% - 680px) / 2));
  transform-origin: 24px 100%;
  z-index: 6;
}
.menu-ic { width: 32px; height: 32px; display: grid; place-items: center; flex: none; border-radius: 10px; color: #fff; }
.menu-ic svg { width: 18px; height: 18px; }
.ic-photo { background: #0a84ff; }
.ic-video { background: #ff375f; }
.ic-code { background: #5e5ce6; }
.ic-music { background: #ff9f0a; }
html[data-theme="material"] .menu-ic { background: var(--md-sys-color-primary-container); color: var(--md-sys-color-on-primary-container); border-radius: 50%; }

/* Просмотр фото */
.viewer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.92);
  cursor: zoom-out;
  animation: fade-in 220ms ease both;
}
.viewer img { max-width: 100%; max-height: 100%; border-radius: 12px; animation: pop-in 360ms var(--spring) both; }
.viewer.is-closing { animation: fade-out 200ms ease both; }

/* ---------- Редактирование профиля ---------- */
.edit { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.edit .group, .edit form { width: 100%; }
.edit-avatar { margin-top: 6px; animation: pop-in 520ms var(--spring-bouncy) both; }
.avatar-edit { width: 124px; height: 124px; font-size: 46px; }
.edit-avatar-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
label.chip-btn { cursor: pointer; }
.edit-form { margin-top: 0; }
.group-pad { display: flex; flex-direction: column; gap: 10px; padding: 16px; }
.group-pad .field { padding: 0 20px; }
.group-pad .field input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 18px;
  caret-color: var(--c-accent);
}
.group-pad .field input::placeholder { color: var(--c-text-3); }
.color-row { display: flex; flex-wrap: wrap; gap: 10px; }
.color-chip {
  position: relative;
  width: 40px; height: 40px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: linear-gradient(160deg, var(--a1), var(--a2));
  transition: scale 300ms var(--spring-bouncy);
}
.color-chip:active { scale: 0.9; }
.color-chip::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--c-text);
  opacity: 0;
  scale: 0.8;
  transition: opacity 200ms ease, scale 300ms var(--spring-bouncy);
}
.color-chip[aria-checked="true"]::after { opacity: 1; scale: 1; }
.color-auto { background: rgba(255, 255, 255, 0.1); color: var(--c-text-2); font-size: 11px; font-weight: 700; }

/* ---------- Панель «Designs» ---------- */
.design { display: flex; flex-direction: column; gap: 12px; }
.design-preview {
  position: relative;
  height: 380px;
  overflow: hidden;
  isolation: isolate;
  border-radius: 28px;
  background: var(--page-bg);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  pointer-events: none;
  transition: background 400ms ease;
}
.design-preview .wallpaper { position: absolute; inset: 0; z-index: -1; }
.pv-list { padding: 8px 6px 0 !important; }
.pv-list .chat { animation: none; padding: 7px 10px; }
.pv-chat { position: absolute; left: 0; right: 0; bottom: 0; display: flex; flex-direction: column; gap: 6px; padding: 14px; }
.pv-chat .bubble { max-width: 76%; }
.palette-design { display: flex !important; margin: 0; padding: 0; background: none !important; }
.wp-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.wp-option { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 0; border: 0; background: none; font-size: 12px; color: var(--c-text-2); }
.wp-thumb {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 14;
  overflow: hidden;
  border-radius: 16px;
  background: #000;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
  transition: box-shadow 250ms ease, scale 300ms var(--spring-bouncy);
}
.wp-option:active .wp-thumb { scale: 0.94; }
.wp-thumb .wallpaper { position: absolute; inset: 0; }
.wp-option[aria-checked="true"] .wp-thumb { box-shadow: 0 0 0 2px var(--page-bg), 0 0 0 4px var(--c-accent); }
.wp-option[aria-checked="true"] .wp-label { color: var(--c-text); font-weight: 600; }
.material-only { display: none !important; }
html[data-theme="material"] .material-only { display: flex !important; }
.shape-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; justify-items: center; }
.shape-option { padding: 8px; border: 0; border-radius: 20px; background: none; transition: background 250ms ease, scale 300ms var(--spring-bouncy); }
.shape-option:active { scale: 0.92; }
.shape-option[aria-checked="true"] { background: var(--md-sys-color-secondary-container); }
.shape-demo { width: 54px; height: 54px; font-size: 18px; border-radius: 0; }

/* ---------- Отправка кода ---------- */
.lang-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 10px; }
.lang-chip {
  height: 34px;
  padding: 0 12px;
  border: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 13px;
  font-weight: 600;
  transition: background 200ms ease, color 200ms ease;
}
.lang-chip[aria-checked="true"] { background: var(--c-accent); color: #fff; }
html[data-theme="material"] .lang-chip { background: var(--md-sys-color-surface-container-high); }
html[data-theme="material"] .lang-chip[aria-checked="true"] { background: var(--md-sys-color-secondary-container); color: var(--md-sys-color-on-secondary-container); }
.code-field {
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 18px;
  background: #0b0b0c;
  border: 1px solid var(--c-cell-border);
}
.code-field.is-error { border-color: var(--c-error); animation: shake 420ms cubic-bezier(0.36, 0.07, 0.19, 0.97); }
.code-field textarea {
  width: 100%;
  min-height: 170px;
  border: 0;
  outline: 0;
  background: none;
  resize: vertical;
  font: 14px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--c-text);
  tab-size: 2;
}
#sheet-code .btn { width: 100%; margin-bottom: 4px; }

/* ---------- Окно чата: слои без position: fixed ----------
   fixed внутри сдвигаемой (transform) панели на телефонах рисовался с задержкой
   в несколько секунд. Теперь панель не прокручивается сама: фон, лента и поле
   ввода — абсолютные слои, прокручивается только #chat-scroll. */
.panel-chat { overflow: hidden; padding-inline: 0; }
.chat-scroll {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  padding-inline: max(0px, calc((100% - 680px) / 2));
}
.chat-scroll::-webkit-scrollbar { display: none; }
.panel-chat .messages { padding-bottom: 76px; }
.composer, .composer-ro, .compose-bar, .attach-menu, .emoji-picker, .chat-join { position: absolute; }
.composer-ro, .composer, .chat-join { z-index: 4; }
.compose-bar { z-index: 4; }
.attach-menu, .emoji-picker { z-index: 6; }
.panel-chat:not([data-composer="compose"]) .composer,
.panel-chat:not([data-composer="ro"]) .composer-ro,
.panel-chat:not([data-composer="join"]) .chat-join { display: none; }
.panel-chat:not([data-composer="compose"]) .compose-bar { display: none; }
.chat-join {
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  width: min(440px, calc(100% - 28px));
  transform: translateX(-50%);
}
.chat-join .btn { width: 100%; height: 52px; border-radius: 999px; font-size: 16px; }
.panel-chat.no-media .attach-btn { display: none; }
