:root {
  --background: #f8f9ff;
  --surface: #f8f9ff;
  --surface-container-lowest: #ffffff;
  --surface-container: #e5eeff;
  --on-surface: #0b1c30;
  --primary: #4648d4;
  --primary-container: #6063ee;
  --on-primary: #ffffff;
  --primary-fixed-dim: #c0c1ff;
  --outline: #767586;
  --outline-variant: #c7c4d7;
}
@media (prefers-color-scheme: dark) {
  :root {
    --background: #0b1c30;
    --surface: #0b1c30;
    --surface-container-lowest: #14253b;
    --surface-container: #213145;
    --on-surface: #eaf1ff;
    --primary: #c0c1ff;
    --primary-container: #2f2ebe;
    --on-primary: #07006c;
    --outline: #908fa0;
    --outline-variant: #464554;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  height: 100%; overflow: hidden;
  background: var(--background);
  color: var(--on-surface);
  font-family: "Be Vietnam Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px; line-height: 22px;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}
body { display: flex; flex-direction: column; height: 100dvh; height: 100vh; position: relative; }

/* ─── Marble background ─── */
.bg { position: fixed; inset: 0; z-index: 0; pointer-events: none; }
.bg img { width: 100%; height: 100%; object-fit: cover; opacity: 0.6; }
.bg-tint {
  position: absolute; inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.80) 0%, rgba(248,249,255,0.60) 50%, rgba(211,228,254,0.40) 100%);
}
@media (prefers-color-scheme: dark) {
  .bg img { opacity: 0.18; }
  .bg-tint {
    background: linear-gradient(135deg, rgba(11,28,48,0.80) 0%, rgba(11,28,48,0.65) 50%, rgba(33,49,69,0.55) 100%);
  }
}

/* ─── Header ─── */
.hdr {
  position: relative; z-index: 10; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center;
  padding: 8px 16px;
  background: rgba(255,255,255,0.70);
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(255,255,255,0.30);
  box-shadow: 0 1px 6px rgba(11,28,48,0.04);
}
@media (prefers-color-scheme: dark) {
  .hdr { background: rgba(20,37,59,0.70); border-bottom-color: rgba(255,255,255,0.08); }
}
.hdr-title {
  display: flex; align-items: center; gap: 7px;
  font-family: "Manrope", sans-serif;
  font-weight: 700; font-size: 15px; letter-spacing: -0.01em;
  color: var(--primary); margin: 0; line-height: 20px;
}
.hdr-icon {
  display: flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: 999px;
  background: var(--primary-container); color: var(--on-primary);
  flex: 0 0 auto;
}
.hdr-counter {
  font-family: "Inter", sans-serif;
  font-size: 12px; font-weight: 600;
  color: var(--outline);
  margin-left: 2px;
}
.hdr-counter:empty { display: none; }
.hdr-counter::before { content: "· "; }

/* ─── Comments feed ─── */
.list {
  position: relative; z-index: 5; flex: 1 1 auto;
  overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 12px 12px 10px;
  display: flex; flex-direction: column;
  scrollbar-width: none;
}
/* Между сообщениями ОДНОГО автора подряд — плотно */
.row + .row.same { margin-top: 3px; }
/* Между сообщениями РАЗНЫХ авторов — заметный пробел */
.row + .row.new { margin-top: 14px; }
/* После date-divider отступ не нужен (divider уже создаёт пробел) */
.divider + .row { margin-top: 0; }
.list::-webkit-scrollbar { display: none; }
.empty { margin: auto; text-align: center; color: var(--outline); font-size: 14px; padding: 32px 16px; }

.divider {
  display: flex; align-items: center; justify-content: center;
  margin: 6px 0 10px;
}
.divider .pill {
  background: rgba(255,255,255,0.50);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,0.30);
  padding: 4px 12px; border-radius: 999px;
  font-family: "Inter", sans-serif;
  font-size: 11px; font-weight: 700; letter-spacing: 0.05em;
  color: var(--outline); text-transform: uppercase;
}

.row { display: flex; gap: 8px; align-items: flex-end; max-width: 85%; }
.row.in { align-self: flex-start; }
.row.out { align-self: flex-end; flex-direction: row-reverse; }

.avatar {
  width: 30px; height: 30px; border-radius: 999px;
  display: flex; align-items: center; justify-content: center;
  font-family: "Manrope", sans-serif; font-weight: 700; font-size: 12px;
  color: white; flex: 0 0 auto;
  border: 1px solid rgba(255,255,255,0.50);
  box-shadow: 0 1px 3px rgba(11,28,48,0.08);
  margin-bottom: 2px;
}
.avatar.placeholder { visibility: hidden; }

.bubble {
  position: relative;
  padding: 5px 10px 4px;
  border-radius: 16px;
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 1px 4px rgba(11,28,48,0.05);
  max-width: 100%; word-break: break-word;
}
.row.in .bubble {
  background: rgba(255,255,255,0.65);
  border: 1px solid rgba(255,255,255,0.55);
  color: var(--on-surface); border-bottom-left-radius: 6px;
}
.row.out .bubble {
  background: rgba(165, 216, 255, 0.78);   /* светло-голубой */
  border: 1px solid rgba(255, 255, 255, 0.55);
  color: var(--on-surface);                /* тёмный текст для читаемости */
  border-bottom-right-radius: 6px;
}
@media (prefers-color-scheme: dark) {
  .row.out .bubble {
    background: rgba(74, 144, 226, 0.45);
    border-color: rgba(255, 255, 255, 0.10);
    color: var(--on-surface);
  }
}
@media (prefers-color-scheme: dark) {
  .row.in .bubble { background: rgba(33,49,69,0.70); border-color: rgba(255,255,255,0.10); }
}
.bubble .who {
  font-family: "Manrope", sans-serif;
  font-weight: 700;
  font-size: 12.5px;
  margin: 0 0 2px;
  line-height: 15px;
  letter-spacing: -0.005em;
  /* color задаётся inline в JS — индивидуальный для каждого пользователя */
}
/* Текст + невидимый spacer на конце, чтобы инлайн-время поместилось в последнюю строку */
.bubble .txt {
  white-space: pre-wrap;
  font-size: 14.5px;
  line-height: 19px;
  margin: 0;
  display: inline;
}
.bubble .txt::after {
  content: "\00a0";
  display: inline-block;
  width: 42px;          /* зарезервировано под время "12:34" */
  height: 1px;
}
/* Время абсолютным позиционированием — всегда в правом нижнем углу */
.bubble .when {
  position: absolute;
  right: 8px;
  bottom: 3px;
  font-family: "Inter", sans-serif;
  font-size: 10px; font-weight: 500;
  line-height: 12px;
  color: var(--outline);
  opacity: 0.8;
  pointer-events: none;
}
.row.out .bubble .when { color: var(--outline); opacity: 0.75; }
/* На голубом фоне имя автора и так читается нативным цветом — фильтр не нужен */
/* Если есть только аттачи без текста — время не «слипнется» с краем картинки */
.bubble.only-att { padding-bottom: 18px; }
.bubble.only-att .when { bottom: 4px; }

/* Attachments grid в bubble */
.atts {
  display: grid; gap: 3px;
  margin-top: 4px;
  border-radius: 12px;
  overflow: hidden;
}
.atts.n1 { grid-template-columns: 1fr; }
.atts.n2 { grid-template-columns: 1fr 1fr; }
.atts.n3 { grid-template-columns: 1fr 1fr; }
.atts.n3 > :first-child { grid-column: 1 / 3; }
.atts.n4 { grid-template-columns: 1fr 1fr; }
.atts.many { grid-template-columns: 1fr 1fr 1fr; }
.atts > .att {
  position: relative;
  width: 100%; aspect-ratio: 1 / 1;
  background: rgba(0,0,0,0.04);
  cursor: zoom-in;
}
.atts.n1 > .att { aspect-ratio: 4 / 3; }
.atts > .att img, .atts > .att video {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.atts > .att .play {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}
.atts > .att .play span {
  width: 36px; height: 36px; border-radius: 999px;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
}

/* ─── Composer ─── */
.composer {
  position: relative; z-index: 10; flex: 0 0 auto;
  padding: 6px 14px; padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.previews {
  display: flex; gap: 8px; padding: 6px 4px;
  overflow-x: auto; scrollbar-width: none;
}
.previews:empty { display: none; }
.previews::-webkit-scrollbar { display: none; }
.preview {
  position: relative; width: 64px; height: 64px;
  border-radius: 12px; overflow: hidden;
  border: 1px solid rgba(255,255,255,0.50);
  background: rgba(255,255,255,0.40);
  flex: 0 0 auto;
}
.preview img, .preview video { width: 100%; height: 100%; object-fit: cover; }
.preview .rm {
  position: absolute; top: 2px; right: 2px;
  width: 20px; height: 20px; border-radius: 999px;
  background: rgba(0,0,0,0.65); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; line-height: 1; cursor: pointer; border: none;
}
.preview .progress {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 3px; background: rgba(0,0,0,0.15);
}
.preview .progress > i {
  display: block; height: 100%; background: var(--primary);
  width: 0%; transition: width 0.25s ease;
}

.composer-inner {
  display: flex; align-items: flex-end; gap: 8px;
  background: rgba(255,255,255,0.55);
  backdrop-filter: blur(28px); -webkit-backdrop-filter: blur(28px);
  border: 1px solid rgba(255,255,255,0.55);
  border-radius: 24px;
  padding: 6px 6px 6px 6px;
  box-shadow: 0 8px 30px rgba(11,28,48,0.08);
}
@media (prefers-color-scheme: dark) {
  .composer-inner { background: rgba(33,49,69,0.65); border-color: rgba(255,255,255,0.10); }
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 999px;
  background: transparent; border: none; cursor: pointer;
  color: var(--outline);
  display: flex; align-items: center; justify-content: center;
  flex: 0 0 auto;
  transition: color 0.15s ease, background 0.15s ease;
}
.icon-btn:hover, .icon-btn:focus-visible {
  color: var(--primary); background: rgba(70,72,212,0.08); outline: none;
}
.icon-btn:active { transform: scale(0.95); }

#input {
  flex: 1 1 0;
  min-width: 0;       /* критично для flex: позволяет textarea сжиматься */
  width: 100%;
  background: transparent; border: 0; outline: 0; resize: none;
  height: 36px; min-height: 36px; max-height: 120px;
  font: inherit; font-family: "Be Vietnam Pro", sans-serif;
  font-size: 15px; line-height: 20px;
  color: var(--on-surface);
  padding: 8px 6px;
  margin: 0;
}
#input::placeholder { color: var(--outline); font-weight: 400; }
#file-input { display: none !important; }

.send-btn {
  width: 40px; height: 40px; border-radius: 999px;
  border: none; background: var(--primary); color: var(--on-primary);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex: 0 0 auto;
  box-shadow: inset 0 2px 4px rgba(255,255,255,0.30), 0 4px 12px rgba(70,72,212,0.30);
  transition: transform 0.1s ease, opacity 0.15s ease;
}
.send-btn svg { transform: translate(1px, 0); }
.send-btn:active { transform: scale(0.95); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }

/* ─── Lightbox ─── */
.lightbox {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  animation: fadein 0.15s ease;
}
.lightbox[hidden] { display: none; }
.lightbox-inner { max-width: 100%; max-height: 100%; }
.lightbox-inner img, .lightbox-inner video {
  max-width: 100%; max-height: 90vh;
  object-fit: contain; border-radius: 12px;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
