  :root {
    --accent: #c0392b; --muted: #8a8f98; --line: #e7e7e7;
    /* ---- Палитра пейзажа по времени суток: по умолчанию — «день» ---- */
    --sky-top: #6ec6ff; --sky-bottom: #eaf7ff;
    --celestial: #fff6c0;
    --mountain-far: #93a8c4; --mountain-near: #5f7796;
    --forest: #2f6b45; --sea: #1f7fae; --reflection: rgba(255,255,255,.35);
  }
  :root[data-time="morning"] {
    --sky-top: #ffd9a0; --sky-bottom: #cfe9ff; --celestial: #ffe8a3;
    --mountain-far: #a897bd; --mountain-near: #7c6f9a;
    --forest: #2f5d43; --sea: #4fa9c9; --reflection: rgba(255,214,140,.45);
  }
  :root[data-time="sunset"] {
    --sky-top: #4a2f6b; --sky-bottom: #ffb26b; --celestial: #ff6a3d;
    --mountain-far: #5b3a63; --mountain-near: #3c2440;
    --forest: #1c2b1e; --sea: #b4552f; --reflection: rgba(255,170,110,.55);
  }
  :root[data-time="evening"] {
    --sky-top: #141b3d; --sky-bottom: #5b6ea3; --celestial: #f3f0e6;
    --mountain-far: #2a3350; --mountain-near: #1b2138;
    --forest: #10160f; --sea: #16233f; --reflection: rgba(220,220,255,.25);
  }
  :root[data-time="night"] {
    --sky-top: #05070f; --sky-bottom: #131b32; --celestial: #eef1f7;
    --mountain-far: #141a2c; --mountain-near: #0c0f1c;
    --forest: #05070a; --sea: #060a16; --reflection: rgba(200,210,255,.2);
  }
  * { box-sizing: border-box; }
  /* Фон-подложка (белый) — только на html, а не на body: если бы явный фон
     был ещё и на body, он рисовался бы ПОВЕРХ фиксированной SVG-иллюстрации
     ниже (у той z-index:-1), потому что для непозиционированного body это
     более поздний слой отрисовки, чем негативный z-index — иллюстрация была
     бы полностью не видна, хотя все её стили и цвета были бы верны. */
  html { background: #ffffff; }
  html, body {
    margin: 0; height: 100%;
    overflow: hidden;
    color: #111;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  }
  [hidden] { display: none !important; }

  /* ---- Фоновая пейзажная иллюстрация стартовых экранов ---- */
  #scenery { position: fixed; inset: 0; z-index: -1; overflow: hidden; background: var(--sky-bottom); }
  /* Логотип поверх пейзажа, в правом верхнем углу ЭКРАНА (не карточки) —
     fixed относительно viewport, поэтому при растягивании окна остаётся
     у правого края, а не у карточки по центру. */
  #global-logo {
    position: fixed; top: 18px; right: 22px; z-index: 3;
    font-size: 15px; font-weight: 700; color: #fff;
    text-shadow: 0 1px 3px rgba(0,0,0,.35), 0 1px 14px rgba(0,0,0,.18);
    pointer-events: none; user-select: none;
  }
  #scenery svg { width: 100%; height: 100%; display: block; }
  #scenery .sun, #scenery .moon, #scenery .star { transition: opacity .8s; }
  #scenery .sun { opacity: 1; }
  #scenery .moon, #scenery .star { opacity: 0; }
  :root[data-time="evening"] #scenery .sun,
  :root[data-time="night"] #scenery .sun { opacity: 0; }
  :root[data-time="evening"] #scenery .moon,
  :root[data-time="night"] #scenery .moon { opacity: 1; }
  :root[data-time="evening"] #scenery .star,
  :root[data-time="night"] #scenery .star { opacity: 1; }
  :root[data-time="sunset"] #scenery .star { opacity: .35; }

  /* ---- Общие мелкие элементы форм ---- */
  .screen {
    max-width: 440px; margin: 9vh auto 0; padding: 34px 26px 26px; text-align: center;
    max-height: 82vh; max-height: 82dvh; overflow-y: auto; box-sizing: border-box;
    background: rgba(255,255,255,.92); border-radius: 18px;
    box-shadow: 0 18px 50px rgba(0,0,0,.22);
    backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    position: relative; z-index: 1;
  }
  .screen h1 { font-size: 20px; margin-bottom: 4px; }
  .screen .hint { color: var(--muted); font-size: 13px; margin-bottom: 24px; }
  .invite-headline {
    font-size: 21px; font-weight: 700; color: #111;
    line-height: 1.35; margin: 8vh 0 26px;
  }
  .screen input[type=text], .screen input[type=password] {
    width: 100%; padding: 10px 12px; font-size: 14px;
    border: 1px solid var(--line); border-radius: 8px; margin-bottom: 10px;
    font-family: inherit; box-sizing: border-box;
  }
  .screen button.primary {
    width: 100%; padding: 11px; font-size: 14px; border: none; border-radius: 8px;
    background: var(--accent); color: #fff; cursor: pointer;
  }
  .screen button.primary:disabled { opacity: 0.5; cursor: default; }
  .invite-box {
    background: #faf6f5; border: 1px solid var(--line); border-radius: 8px;
    padding: 10px; font-size: 13px; word-break: break-all; margin-top: 16px;
  }
  .invite-box button { margin-top: 8px; font-size: 12px; padding: 4px 10px; cursor: pointer; }

  /* ---- Профиль: вход/регистрация/главная ---- */
  .screen textarea {
    width: 100%; padding: 10px 12px; font-size: 14px; font-family: inherit;
    border: 1px solid var(--line); border-radius: 8px; margin-bottom: 10px;
    resize: vertical; min-height: 54px;
  }
  .auth-switch { font-size: 12.5px; color: var(--muted); margin-top: 14px; }
  .auth-switch a { color: var(--accent); text-decoration: none; }
  .auth-switch a:hover { text-decoration: underline; }
  .form-error { color: var(--accent); font-size: 12.5px; margin: -2px 0 10px; text-align: left; }
  .secondary-btn {
    border: 1px solid var(--line); background: #fff; padding: 6px 14px;
    border-radius: 8px; font-size: 12.5px; cursor: pointer; color: #111;
  }
  .avatar-upload { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 16px; }
  .avatar-big {
    width: 84px; height: 84px; border-radius: 50%; object-fit: cover;
    background: #eee; display: block;
  }
  .avatar-placeholder {
    display: flex; align-items: center; justify-content: center;
    font-size: 30px; font-weight: 600; color: #fff; background: var(--accent);
  }
  .profile-card { display: flex; flex-direction: column; align-items: center; }
  .avatar-wrap { position: relative; display: inline-block; margin-bottom: 10px; }
  .avatar-wrap .avatar-big { margin-bottom: 0; display: block; }
  .own-online-dot {
    position: absolute; top: 1px; right: 1px;
    width: 14px; height: 14px; border-radius: 50%;
    background: #3cb371; border: 2px solid #fff;
  }
  .profile-name { font-size: 17px; font-weight: 700; }
  .profile-bio { color: var(--muted); font-size: 13px; margin-top: 4px; max-width: 100%; word-break: break-word; }

  /* ---- Шапка главного экрана профиля: не прокручивается вместе с остальным
     содержимым и визуально отделена от него — тонкой линией и своим фоном,
     который перекрывает то, что уезжает под неё при скролле.
     #screen-profile-home сам без отступов (padding: 0) — и шапка, и тело
     под ней задают отступы каждая себе сама. Раньше шапка «занимала» верхний
     отступ .screen отрицательными margin, но position:sticky и отрицательные
     margin вместе — плохо сочетающаяся комбинация: на широких экранах браузер
     ошибался с резервируемым местом под шапку, и следующий блок наезжал на
     неё. Явные, ничем не аннулируемые padding у обоих блоков это исключают. ---- */
  #screen-profile-home { padding: 0; }
  .profile-home-header {
    position: sticky; top: 0; z-index: 2;
    padding: 34px 26px 16px;
    background: rgba(255,255,255,.97);
    border-bottom: 1px solid var(--line);
  }
  .profile-home-header .invite-banner { margin-bottom: 14px; }
  .profile-home-header .auth-switch { margin-top: 10px; }
  .profile-home-body { padding: 20px 26px 26px; }

  /* ---- Кнопка «Зажечь чат»: анимированный огонёк внутри ---- */
  .flame-icon { display: inline-block; animation: flame-flicker 1.1s ease-in-out infinite; transform-origin: 50% 90%; }
  @keyframes flame-flicker {
    0%, 100% { transform: scale(1) rotate(-2deg); }
    25% { transform: scale(1.08, 0.94) rotate(2deg); }
    50% { transform: scale(0.94, 1.06) rotate(-3deg); }
    75% { transform: scale(1.05, 0.97) rotate(1deg); }
  }

  .chat-history-toggle {
    display: block; font-size: 12.5px; color: var(--muted); text-decoration: none;
    margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); cursor: pointer;
  }
  .chat-history-toggle:hover { color: #666; text-decoration: underline; }
  .chat-history { margin-top: 10px; text-align: left; }
  .chat-history ul { list-style: none; margin: 0; padding: 0; }
  .chat-history li {
    font-size: 12.5px; padding: 6px 0; border-bottom: 1px solid var(--line);
    word-break: break-all; color: #444;
  }
  .chat-history li:last-child { border-bottom: none; }
  .chat-history-who { font-size: 12.5px; font-weight: 600; color: #222; }
  .chat-history-meta { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

  /* ---- Собеседники с профилем на главном экране: аватарки, статус «в сети» ----
     Список со своей прокруткой (не тянет весь экран вниз), если контактов много. */
  .contacts-block { margin-top: 18px; margin-bottom: 18px; text-align: left; }
  .contacts-title { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; }
  .contacts-block ul {
    list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px;
    max-height: min(320px, 42vh); overflow-y: auto;
  }
  .contact-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; cursor: pointer; }
  .contact-row:hover { background: #faf6f5; }
  .contact-avatar {
    width: 32px; height: 32px; border-radius: 50%; object-fit: cover;
    background: #eee; flex-shrink: 0;
  }
  .contact-avatar.avatar-placeholder { font-size: 13px; }
  .contact-name { font-size: 13.5px; flex: 1; }
  .contact-dot { width: 9px; height: 9px; border-radius: 50%; background: #d5d5d5; flex-shrink: 0; }
  .contact-dot.online { background: #3cb371; box-shadow: 0 0 0 3px rgba(60,179,113,0.15); }
  /* Кнопка «Зажечь чат» появляется при наведении на строку собеседника —
     клик по ней (как и по любому месту строки) создаёт чат с этим человеком,
     в сети он сейчас или нет. */
  .contact-ignite-btn {
    display: none; align-items: center; gap: 4px; flex-shrink: 0;
    font-size: 11.5px; padding: 4px 10px; border-radius: 14px;
    border: none; background: #e3e3e3; color: #444; cursor: pointer; white-space: nowrap;
  }
  .contact-ignite-btn:hover { background: #d6d6d6; }
  .contact-row:hover .contact-ignite-btn { display: inline-flex; }
  .contact-row:hover .contact-dot { display: none; }

  /* ---- Приглашение в чат от собеседника, пока вы на главном экране ---- */
  .invite-banner {
    display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
    background: #eef8f0; border: 1px solid #bfe6c8; border-radius: 8px;
    padding: 10px 12px; margin-bottom: 16px; font-size: 12.5px; text-align: left;
  }
  .invite-banner button { font-size: 12px; padding: 4px 10px; cursor: pointer; }

  /* Круглая аватарка рядом с именем отправителя в переписке */
  .msg-avatar {
    width: 20px; height: 20px; border-radius: 50%; object-fit: cover;
    vertical-align: middle; margin-right: 5px;
  }

  /* ---- Экран чата ---- */
  /* height: 100dvh (динамическая высота видимой области) переопределяет 100vh там,
     где браузер это понимает — иначе на мобильных (особенно при выезжающей адресной
     строке) 100vh больше реально видимой области, и поле ввода уезжает за нижний край. */
  /* #screen-chat — на всю ширину и прозрачный, чтобы фоновая иллюстрация была видна
     по бокам; #chat-panel внутри — сама переписка, непрозрачная белая панель. */
  #screen-chat {
    display: flex; justify-content: center; height: 100vh; height: 100dvh;
    position: relative; z-index: 1;
  }
  #chat-panel {
    display: flex; flex-direction: column; width: 100%; height: 100%; background: #fff;
  }
  header {
    padding: 10px 16px; border-bottom: 1px solid var(--line);
    display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  }
  header .title { font-size: 14px; }
  header .title b { font-weight: 600; }
  header .title .topic { color: var(--muted); }
  header .right { display: flex; align-items: center; gap: 14px; font-size: 12px; color: var(--muted); }
  header label { display: flex; align-items: center; gap: 5px; cursor: pointer; }
  header button#end-btn {
    border: 1px solid var(--line); background: #fff; padding: 5px 10px;
    border-radius: 6px; cursor: pointer; font-size: 12px; color: var(--accent);
  }
  #link-bar {
    font-size: 12px; color: var(--muted); padding: 6px 16px; border-bottom: 1px solid var(--line);
    display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  }
  #link-bar button { font-size: 11px; padding: 2px 8px; cursor: pointer; }
  #link-bar input[type=text] {
    font-size: 11px; padding: 4px 7px; border: 1px solid var(--line); border-radius: 6px;
    flex: 1; min-width: 120px; font-family: inherit;
  }

  #chat {
    /* Один вертикальный поток сообщений с прокруткой (раньше был
       CSS multi-column — из-за него при длинной переписке сообщения
       раскладывались по нескольким колонкам вместо привычной ленты). */
    flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 16px;
  }
  .msg { margin-bottom: 6px; font-size: 13.5px; line-height: 1.35; }
  .msg .name { font-weight: 600; }
  .msg.mine .name { color: var(--accent); }
  .msg .time { color: var(--muted); font-size: 11px; margin-left: 4px; }
  .msg .text { white-space: pre-wrap; word-wrap: break-word; }
  .msg .text a { color: var(--accent); text-decoration: underline; }
  .msg .text.sticker { font-size: 42px; line-height: 1.1; margin: 2px 0; }
  .msg .text img.chat-image {
    display: block; max-width: min(280px, 100%); max-height: 280px;
    border-radius: 10px; margin-top: 3px; cursor: zoom-in; object-fit: contain;
  }
  /* Место фото на время его сжатия/отправки — крутящийся индикатор вместо пустоты */
  .image-placeholder {
    width: 140px; height: 140px; margin-top: 3px;
    display: flex; align-items: center; justify-content: center;
    background: #f4f4f4; border-radius: 10px;
  }
  .image-spinner {
    width: 30px; height: 30px; border-radius: 50%;
    border: 3px solid #e0e0e0; border-top-color: #aaa;
    animation: image-spin 0.8s linear infinite;
  }
  @keyframes image-spin { to { transform: rotate(360deg); } }
  .msg .reactions { margin-top: 1px; }
  .reaction-pill {
    display: inline-block; font-size: 11.5px; color: var(--muted);
    cursor: pointer; margin-right: 6px; user-select: none;
  }
  .reaction-picker { display: inline-flex; gap: 4px; opacity: 0; transition: opacity .12s; margin-left: 6px; vertical-align: middle; }
  .msg:hover .reaction-picker { opacity: 1; }
  .reaction-picker span {
    cursor: pointer; font-size: 20px; line-height: 1; display: inline-block;
    transition: transform .1s;
  }
  .reaction-picker span:hover { transform: scale(1.25); }
  .system, .ended-note {
    color: var(--muted); font-size: 12px; margin-bottom: 6px; font-style: italic;
  }
  .ended-note { color: var(--accent); font-style: normal; }
  .waiting-note { color: var(--muted); font-size: 12px; padding: 0 16px 8px; }

  #composer {
    display: flex; flex-direction: column; gap: 6px; padding: 10px 16px; border-top: 1px solid var(--line);
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    flex-shrink: 0; position: relative;
  }
  #composer-tools {
    display: flex; gap: 8px;
  }
  #composer-input-row {
    display: flex; gap: 8px; align-items: flex-end;
  }
  #composer-input-row textarea#text-input {
    flex: 1; padding: 9px 12px; border: 1px solid var(--line); border-radius: 18px; font-size: 14px;
    font-family: inherit; line-height: 1.35; resize: none; overflow-y: hidden;
    max-height: 160px; min-height: 36px;
  }
  #composer button {
    padding: 0 16px; border: none; background: var(--accent); color: #fff; border-radius: 18px; cursor: pointer;
    flex-shrink: 0;
  }
  #send-btn {
    padding: 9px 16px; align-self: flex-end;
  }
  #composer button:disabled, #composer textarea:disabled { opacity: 0.5; }
  #sticker-toggle-btn, #image-btn {
    flex: 0 0 auto; width: 40px; height: 40px; padding: 0 !important; font-size: 19px;
    background: #fff !important; border: 1px solid var(--line) !important; color: initial;
    border-radius: 50% !important;
  }
  #sticker-panel {
    /* Позиционируется от самого composer (а не от края экрана), поэтому
       остаётся на месте и на широких экранах, где чат сужен и центрирован. */
    position: absolute; left: 16px; bottom: calc(100% + 8px); z-index: 2;
    display: flex; flex-wrap: wrap; gap: 8px; max-width: calc(100vw - 32px);
    background: #fff; border: 1px solid var(--line);
    border-radius: 14px; padding: 10px 12px; box-shadow: 0 8px 26px rgba(0,0,0,.15);
  }
  #sticker-panel span {
    font-size: 26px; cursor: pointer; user-select: none; line-height: 1;
    transition: transform .1s;
  }
  #sticker-panel span:hover { transform: scale(1.2); }

  #image-lightbox {
    position: fixed; inset: 0; background: rgba(0,0,0,.88); z-index: 100;
    display: flex; align-items: center; justify-content: center; padding: 24px;
    cursor: zoom-out;
  }
  #image-lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

  /* ---- На широких экранах переписка не растягивается на всю ширину, а
     превращается в узкую панель по центру (до 700px) с отступами сверху/снизу —
     по бокам и сверху/снизу становится виден фоновый пейзаж, как на стартовых
     экранах. ---- */
  @media (min-width: 1200px) {
    #chat-panel {
      max-width: 700px; width: 100%;
      height: calc(100% - 6vh); margin: 3vh 0;
      border-radius: 18px; overflow: hidden;
      background: rgba(255,255,255,.96);
      box-shadow: 0 18px 50px rgba(0,0,0,.22);
      backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
    }
  }
