:root {
  --accent: #6d5ae6;
  --accent-soft: #efecff;
  --bg: #ffffff;
  --fg: #1b1b22;
  --muted: #6b6b7b;
  --line: #e7e7ef;
  --bubble: #f4f4f8;
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --accent: #9b8bff;
    --accent-soft: #262040;
    --bg: #14141a;
    --fg: #f2f2f6;
    --muted: #9a9aac;
    --line: #2a2a35;
    --bubble: #1f1f28;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* Быстрые касания подряд Safari считает двойным нажатием и увеличивает страницу -
   на цифровой клавиатуре это ломает экран кода. */
button, .starter, .keys, .dots { touch-action: manipulation; }
button { user-select: none; -webkit-user-select: none; }
/* Свой display у слоёв перебивает встроенное правило для hidden - объявляем явно. */
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
/* Установленное приложение на айфоне меряет высоту по всему экрану вместе с полосой
   домой. Со старым «100%» приложение занимало экран без неё, а отступ безопасной зоны
   добавлялся сверху - и внизу оставалась двойная пустота. */
@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}
body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: grid;
  grid-template-rows: auto 1fr;
  grid-template-areas: "header" "main";
  overflow: hidden;
}

/* Нижний блок приклеен к нижнему краю окна, а не стоит последней строкой сетки:
   в установленном приложении высота окна и видимая область не совпадают, и последняя
   строка оказывалась заметно выше края. */
#bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--bg); border-top: 1px solid var(--line);
  /* Полосу «домой» под кнопками оставляем, а под неподвижной строкой - нет:
     иначе внизу висит пустой белый лоскут в треть кнопки высотой. */
  padding-bottom: max(calc(env(safe-area-inset-bottom, 0px) - 22px), 2px);
}
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

header {
  grid-area: header;
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-t) + 8px) 10px 8px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
header h1 {
  flex: 1; margin: 0; font-size: 17px; font-weight: 600; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
button.icon { background: none; border: 0; color: var(--fg); padding: 8px; border-radius: 12px; cursor: pointer; }
button.icon:active { background: var(--bubble); }

main {
  grid-area: main;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px 14px calc(var(--bottom-h, 96px) + 8px);
  display: flex; flex-direction: column; gap: 12px;
}
.msg { max-width: 86%; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; word-wrap: break-word; position: relative; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 6px; }
.msg.bot { align-self: flex-start; background: var(--bubble); border-bottom-left-radius: 6px; }
.msg.err { align-self: center; background: transparent; color: var(--muted); font-size: 14px; text-align: center; }
.msg code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; background: rgba(127,127,127,.18); padding: 1px 5px; border-radius: 6px; }
.msg pre { background: rgba(127,127,127,.14); padding: 10px 12px; border-radius: 12px; overflow-x: auto; margin: 8px 0; }
.msg pre code { background: none; padding: 0; }
.msg b { font-weight: 600; }
.msg a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.msg img.shot { display: block; max-width: 100%; border-radius: 12px; margin-top: 6px; }
.msg .file { display: inline-block; margin-top: 6px; font-size: 13px; opacity: .85; }
.msg .slot { display: block; }
.msg .tools { display: flex; gap: 14px; margin-top: 4px; }
.msg button.say {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 4px 0 0; margin-top: 4px; font: inherit; font-size: 13px;
  display: inline-flex; align-items: center; gap: 5px;
}
.msg button.say svg { width: 17px; height: 17px; }
.msg button.say.playing { color: var(--accent); }
.msg.user button.say { color: rgba(255, 255, 255, .9); }
.typing span { display: inline-block; width: 7px; height: 7px; margin-right: 4px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25 } 30% { opacity: 1 } }

/* --- пустой экран --- */
.empty { margin: auto; text-align: center; color: var(--muted); max-width: 520px; padding: 4px; }
.empty .hi { font-size: 22px; font-weight: 600; color: var(--fg); margin-bottom: 2px; }
/* Заголовок раздела принадлежит тому, что ПОД ним: сверху воздуха заметно больше. */
.starters-title { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); margin: 6px 0 2px; letter-spacing: .02em; }
.starters-title:first-child { margin-top: 4px; }
.starters { display: grid; grid-template-columns: repeat(auto-fit, minmax(132px, 1fr)); gap: 6px; margin-top: 0; }
.starter {
  background: var(--bubble); border: 1px solid var(--line); border-radius: 14px;
  padding: 9px 10px; text-align: left; color: var(--fg); font: inherit; cursor: pointer;
  display: flex; gap: 8px; align-items: center; line-height: 1.25; min-height: 48px;
}
.starter > span:last-child { display: flex; align-items: center; }
.starter:active { background: var(--accent-soft); border-color: var(--accent); }
.starter .emoji { font-size: 20px; line-height: 1; display: flex; align-items: center; flex: none; }
.starter b { display: block; font-weight: 600; font-size: 14px; }
.starter i { font-style: normal; color: var(--muted); font-size: 13px; }
/* Снимок из её альбома: кружок над приветствием, нажатие открывает целиком. */
.empty .photo { margin: 2px 0 8px; display: flex; justify-content: center; }
.empty .photo img { width: 96px; height: 96px; object-fit: cover; border-radius: 50%; cursor: pointer; box-shadow: 0 2px 10px rgba(0, 0, 0, .12); }
.empty .photo img:active { transform: scale(.96); }
.photo-view {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center;
  padding: 24px; background: rgba(0, 0, 0, .82); opacity: 0; transition: opacity .2s ease;
}
.photo-view.on { opacity: 1; }
.photo-view img {
  max-width: 100%; max-height: 100%; border-radius: 16px;
  transform: scale(.96); transition: transform .2s ease;
}
.photo-view.on img { transform: scale(1); }
.empty .private { font-size: 12px; margin-top: 8px; }

#chips { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 12px 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 60vw;
  background: var(--bubble); border-radius: 12px; padding: 6px 8px; font-size: 13px;
}
.chip img { width: 32px; height: 32px; object-fit: cover; border-radius: 8px; }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0 2px; font-size: 15px; }

#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 6px 10px 0;
  background: var(--bg);
}
#input {
  flex: 1; resize: none; max-height: 40vh;
  font: inherit; color: inherit;
  background: var(--bubble); border: 0; border-radius: 20px;
  padding: 10px 14px;
}
#input:focus { outline: 2px solid var(--accent); }
#send {
  border: 0; border-radius: 50%; width: 42px; height: 42px; flex: none;
  background: var(--accent); color: #fff; display: grid; place-items: center; cursor: pointer;
}
#send:disabled { opacity: .4; }
#stop {
  border: 0; border-radius: 50%; width: 42px; height: 42px; flex: none;
  background: #e5484d; color: #fff; display: grid; place-items: center; cursor: pointer;
}
@media (max-width: 420px) {
  #send, #stop, button.round { width: 38px; height: 38px; }
  #composer { gap: 6px; }
}
button.round {
  border: 0; border-radius: 50%; width: 42px; height: 42px; flex: none;
  background: var(--bubble); color: var(--fg); display: grid; place-items: center; cursor: pointer;
}
button.round:active { background: var(--accent-soft); }
button.round.recording { background: #e5484d; color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .55 } }

#disclosure {
  margin: 0;
  padding: 2px 14px 4px;
  text-align: center; color: var(--muted); font-size: 11px; line-height: 1.2;
  background: var(--bg);
}

/* --- список чатов --- */
#drawer {
  position: fixed; inset: 0 auto 0 0; width: min(78vw, 320px); z-index: 20;
  background: var(--bg); border-right: 1px solid var(--line);
  transform: translateX(-102%); transition: transform .22s ease;
  display: flex; flex-direction: column;
  padding-top: var(--safe-t);
}
#drawer.open { transform: none; }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 600; }
button.ghost { background: var(--accent-soft); color: var(--accent); border: 0; border-radius: 12px; padding: 7px 12px; font: inherit; font-size: 14px; cursor: pointer; }
.search-box { position: relative; margin: 10px 12px 4px; display: flex; }
#search {
  flex: 1; padding: 10px 34px 10px 12px; border: 0; border-radius: 12px;
  background: var(--bubble); color: var(--fg); font: inherit;
  -webkit-appearance: none; appearance: none;
}
#search:focus { outline: 2px solid var(--accent); }
#search-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 26px; height: 26px; border: 0; border-radius: 50%;
  background: var(--line); color: var(--fg); font-size: 17px; line-height: 1; cursor: pointer;
}
#chat-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
#chat-list li { display: flex; align-items: center; gap: 2px; border-radius: 12px; }
#chat-list li.active { background: var(--accent-soft); }
#chat-list button.pick { flex: 1; text-align: left; background: none; border: 0; color: inherit; font: inherit; padding: 11px 10px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chat-list button.del, #chat-list button.edit { background: none; border: 0; color: var(--muted); padding: 8px 4px; cursor: pointer; }
#chat-list li.result { display: block; padding: 8px 10px; }
#chat-list li.result button { width: 100%; text-align: left; background: none; border: 0; color: inherit; font: inherit; padding: 0; cursor: pointer; }
#chat-list li.result .snippet { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; white-space: normal; }
#chat-list .empty-note { color: var(--muted); font-size: 14px; padding: 12px 14px; }
/* Низ меню живёт по тому же правилу, что и низ экрана: под неподвижным текстом
   полосу «домой» не резервируем. */
.drawer-foot {
  border-top: 1px solid var(--line);
  padding: 12px 14px max(calc(var(--safe-b) - 22px), 6px);
}
.switch { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; margin-bottom: 18px; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }
.private { color: var(--muted); font-size: 12px; margin: 10px 0 0; line-height: 1.4; }
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 15; }

/* --- широкий экран: список чатов стоит слева и сам не прячется --- */
/* Низкое окно (Safari с панелью, маленький телефон): стартовый экран сжимается,
   чтобы уместиться целиком - листать за кнопками ей не приходится. */
@media (max-height: 820px) {
  .empty .photo img { width: 84px; height: 84px; }
  .empty .photo { margin: 0 0 6px; }
  .empty .hi { font-size: 20px; }
  .starters-title { margin: 5px 0 2px; }
  .starter { min-height: 44px; padding: 7px 9px; }
  .starters { gap: 5px; }
  .empty .private { margin-top: 6px; }
}
@media (max-height: 700px) {
  .empty .photo img { width: 64px; height: 64px; }
  .empty .hi { font-size: 18px; }
  .empty > p { font-size: 13px; }
  .starters { gap: 4px; }
  .starter { min-height: 38px; padding: 5px 8px; }
  .starter b { font-size: 13px; }
  .starter .emoji { font-size: 17px; }
  .empty .private { font-size: 11px; }
}

@media (min-width: 900px) {
  body {
    grid-template-columns: 300px 1fr;
    grid-template-areas: "side header" "side main";
  }
  body.side-off { grid-template-columns: 0 1fr; }
  #bottom { left: 300px; }
  body.side-off #bottom { left: 0; }
  #drawer {
    grid-area: side; position: static; width: auto; transform: none; z-index: auto;
    transition: none; padding-top: 0;
  }
  body.side-off #drawer { display: none; }
  #scrim { display: none !important; }
  header { padding-top: 12px; }
  header h1 { text-align: left; padding-left: 4px; }
  main { padding: 20px 24px calc(var(--bottom-h, 96px) + 8px); }
  .msg { max-width: 720px; }
  #composer { padding-left: 24px; padding-right: 24px; }
}

/* --- экран кода и шторка установки --- */
#lock {
  position: fixed; inset: 0; z-index: 40; background: var(--bg);
  display: grid; place-items: center;
  padding: calc(var(--safe-t) + 16px) 16px calc(var(--safe-b) + 16px);
  overflow-y: auto;
}
.lock-box { text-align: center; width: min(320px, 100%); margin: auto; }
.lock-box img { border-radius: 20px; }
.lock-app { font-size: 17px; font-weight: 600; margin: 12px 0 2px; }
.lock-title { font-size: 15px; font-weight: 500; color: var(--muted); margin: 0 0 16px; }
.dots { display: flex; gap: 14px; justify-content: center; height: 16px; }
.dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--accent); display: block; }
.dots i.on { background: var(--accent); }
.lock-note { color: var(--muted); font-size: 14px; min-height: 20px; margin: 14px 0 6px; }
.lock-private { color: var(--muted); font-size: 12px; margin-top: 18px; }
.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 10px; }
.keys button {
  height: 64px; border: 0; border-radius: 50%; background: var(--bubble); color: var(--fg);
  font: inherit; font-size: 24px; cursor: pointer; display: grid; place-items: center;
}
/* Невысокий экран телефона: клавиатура ужимается, чтобы влезала целиком */
@media (max-height: 740px) {
  .lock-box img { width: 48px; height: 48px; }
  .lock-title { margin: 10px 0 12px; font-size: 18px; }
  .lock-note { margin: 10px 0 4px; min-height: 18px; }
  .lock-private { margin-top: 12px; }
  .keys { gap: 9px; margin-top: 6px; }
  .keys button { height: 54px; font-size: 22px; }
}
@media (max-height: 620px) {
  .lock-box img { display: none; }
  .keys button { height: 46px; font-size: 20px; }
}
.keys button:active { background: var(--accent-soft); }
.keys button:disabled { opacity: .4; }

#install { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.35); display: grid; align-items: end; }
.sheet {
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 22px 20px calc(20px + var(--safe-b));
  text-align: center; animation: rise .22s ease;
}
@keyframes rise { from { transform: translateY(100%) } }
.sheet img { border-radius: 16px; }
.sheet-title { font-size: 18px; font-weight: 600; margin: 10px 0 6px; }
.sheet p { color: var(--muted); margin: 0 0 6px; }
.sheet .step { color: var(--fg); }
.sheet-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 16px; }
.sheet-buttons button { border: 0; border-radius: 14px; padding: 12px 20px; font: inherit; cursor: pointer; }
#install-go { background: var(--accent); color: #fff; }
.share-glyph { display: inline-block; vertical-align: -4px; width: 18px; height: 18px; }

/* Страницы входа и приглашения стоят по центру окна. Колонки сбрасываются
   вместе со строками: на широком экране body получает боковую колонку списка
   чатов, и без сброса карточка встаёт в неё - у левого края. */
body.centered {
  display: grid; place-items: center; padding: 24px;
  grid-template-rows: none; grid-template-columns: none; grid-template-areas: none;
}
.card { text-align: center; max-width: 320px; color: var(--muted); }
.card img { border-radius: 22px; }
.card h1 { color: var(--fg); font-size: 22px; margin: 12px 0 8px; }

#recbar {
  flex: 1; display: flex; align-items: center; gap: 10px;
  background: var(--bubble); border-radius: 20px; padding: 11px 16px;
  color: var(--fg); font-variant-numeric: tabular-nums;
}
#recbar .dot { width: 10px; height: 10px; border-radius: 50%; background: #e5484d; animation: pulse 1s infinite; flex: none; }
#recbar #rectime { margin-left: auto; color: var(--muted); }

.msg button.wish, .msg .wish.done {
  display: block; margin-top: 10px; padding: 9px 14px; border: 0; border-radius: 14px;
  background: var(--accent); color: #fff; font: inherit; font-size: 14px; cursor: pointer;
}
.msg .wish.done { background: var(--accent-soft); color: var(--accent); cursor: default; }
.msg button.wish:disabled { opacity: .6; }

button.ghost.wide { display: block; width: 100%; margin-bottom: 10px; padding: 10px 12px; }
#report-box { position: fixed; inset: 0; z-index: 35; background: rgba(0,0,0,.35); display: grid; align-items: end; }
#report-text {
  width: 100%; margin-top: 6px; padding: 12px 14px; border: 0; border-radius: 14px;
  background: var(--bubble); color: var(--fg); font: inherit; resize: none;
}
#report-text:focus { outline: 2px solid var(--accent); }
#report-send { background: var(--accent); color: #fff; }
#report-box .sheet-buttons { align-items: center; }

.made {
  color: var(--muted); font-size: 11px; line-height: 1.4; text-align: center;
  margin: 14px 0 0; opacity: .85;
}

.ask-dad { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; cursor: pointer; }

/* Круглая кнопка в шторке не должна получать отступы обычных кнопок - от них значок
   съезжает из центра. */
.sheet-buttons button.round {
  padding: 0; width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center;
}
.sheet-buttons button.round svg { display: block; }

.card .enter {
  margin-top: 16px; border: 0; border-radius: 16px; padding: 14px 28px;
  background: var(--accent); color: #fff; font: inherit; font-size: 16px; cursor: pointer;
}
