/* Помощник — тёплая терракотовая тема.
   Пишется под Safari/WebKit iOS 15: без :has(), dvh, вложенного CSS, container queries, gap. */
:root {
  --bg: #faf6f1;        /* тёплый кремовый фон */
  --side: #f2e9e0;      /* боковая панель */
  --card: #ffffff;
  --line: #e7dbcf;
  --text: #2d2521;
  --muted: #8f7d72;
  --accent: #c76a4b;    /* терракота */
  --accent-dark: #b05a3d;
  --accent-soft: #f6e4da;
  --accent-text: #fff;
  --danger: #a7382a;
  --shadow: 0 1px 2px rgba(80, 50, 30, .08), 0 4px 14px rgba(80, 50, 30, .06);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  font-size: 20px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
.hidden { display: none !important; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

#app { position: fixed; left: 0; right: 0; top: 0; height: 100%; }
.screen {
  position: absolute; left: 0; right: 0; top: 0; bottom: 0;
  display: flex; flex-direction: column; background: var(--bg);
}

/* ---- кнопки ---- */
.btn {
  font: inherit; border: 0; border-radius: 14px; padding: 10px 16px; cursor: pointer;
  -webkit-appearance: none; appearance: none; min-height: 48px; color: var(--text); background: transparent;
  white-space: nowrap;
}
.btn:active { opacity: .75; }
.btn-primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn-primary:disabled { background: #e2b9a8; }
.btn-ghost { color: var(--accent-dark); font-weight: 600; }
.btn-ghost .ico { font-size: 1.15em; margin-right: 6px; }
.btn-link { color: var(--muted); text-decoration: underline; font-size: .9em; }
.btn-big { width: 100%; font-size: 24px; min-height: 64px; margin-top: 16px; border-radius: 18px; }

/* ---- вход ---- */
#login { justify-content: center; padding: 24px; overflow-y: auto; }
.login-box { max-width: 400px; width: 100%; margin: 0 auto; text-align: center; }
.login-logo img { width: 96px; height: 96px; border-radius: 24px; box-shadow: var(--shadow); }
.login-box h1 { font-size: 32px; margin: 16px 0 4px; letter-spacing: -.01em; }
.hint { color: var(--muted); margin: 0 0 18px; }
#login-code {
  width: 100%; font-size: 32px; letter-spacing: 8px; text-align: center; padding: 14px;
  border: 2px solid var(--line); border-radius: 18px; background: var(--card); -webkit-appearance: none; color: var(--text);
}
#login-code:focus { outline: none; border-color: var(--accent); }
.error { color: var(--danger); min-height: 1.5em; margin-top: 16px; font-weight: 600; }

/* ---- шапка чата ---- */
.topbar {
  flex-shrink: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 6px 8px; padding-top: calc(6px + env(safe-area-inset-top));
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.topbar-title {
  display: none; flex: 1 1 auto; min-width: 0; text-align: center; font-weight: 600; font-size: .9em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 6px; color: var(--muted);
}

/* ---- сообщения ---- */
#messages { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 12px 20px; }
.empty { color: var(--muted); text-align: center; padding: 40px 16px; max-width: 520px; margin: 0 auto; }
.empty b { color: var(--text); font-size: 1.2em; }
.empty p { margin: 0 0 14px; }
.msg { margin: 0 0 12px; display: flex; }
.msg-user { justify-content: flex-end; }
.bubble { max-width: 86%; padding: 11px 16px; border-radius: 20px; word-wrap: break-word; overflow-wrap: break-word; }
.msg-user .bubble { background: var(--accent); color: var(--accent-text); border-bottom-right-radius: 6px; }
.msg-assistant .bubble { background: var(--card); color: var(--text); border-bottom-left-radius: 6px; box-shadow: var(--shadow); }
.msg-error .bubble { background: #fff1ee; color: var(--danger); }
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 26px; }
.bubble li { margin-bottom: 6px; }
.bubble a { color: inherit; text-decoration: underline; word-break: break-all; }
.bubble pre { white-space: pre-wrap; background: var(--accent-soft); padding: 8px 10px; border-radius: 10px; font-size: .85em; }
.bubble img { display: block; max-width: 100%; max-height: 60vh; border-radius: 14px; margin: 0 0 8px; background: var(--line); cursor: pointer; }
.doc-chip {
  display: inline-block; background: rgba(0,0,0,.14); border-radius: 10px; padding: 5px 10px; margin-bottom: 8px;
  font-size: .9em; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.status { color: var(--muted); font-style: italic; }
.dots::after { content: ''; -webkit-animation: dots 1.4s steps(4, end) infinite; animation: dots 1.4s steps(4, end) infinite; }
@-webkit-keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* ---- поле ввода ---- */
.attach-preview { flex-shrink: 0; background: var(--bg); border-top: 1px solid var(--line); padding: 8px 10px; }
.attach-preview .inner { display: flex; align-items: center; }
.attach-preview img { width: 64px; height: 64px; object-fit: cover; border-radius: 12px; margin-right: 12px; }
.attach-doc { flex: 1 1 auto; min-width: 0; margin-right: 12px; font-size: .9em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.composer { flex-shrink: 0; padding: 8px 10px; padding-bottom: calc(8px + env(safe-area-inset-bottom)); background: var(--bg); border-top: 1px solid var(--line); }
.composer .inner { display: flex; align-items: flex-end; }
.btn-attach {
  flex-shrink: 0; width: 52px; height: 52px; min-height: 0; padding: 0; margin-right: 8px; border-radius: 50%;
  background: var(--card); color: var(--accent-dark); border: 1.5px solid var(--line);
  display: flex; align-items: center; justify-content: center;
}
#input {
  flex: 1 1 auto; min-width: 0; font: inherit; padding: 12px 16px; border: 1.5px solid var(--line); border-radius: 26px;
  resize: none; max-height: 40vh; min-height: 52px; -webkit-appearance: none; background: var(--card); color: var(--text);
}
#input:focus { outline: none; border-color: var(--accent); }
.btn-send {
  flex-shrink: 0; width: 52px; height: 52px; min-height: 0; padding: 0; margin-left: 8px; border-radius: 50%;
  background: var(--accent); color: var(--accent-text); display: flex; align-items: center; justify-content: center;
}
.btn-send:disabled { background: #e2b9a8; }
.btn-send svg { margin-left: 2px; }
.composer-hint { display: none; color: var(--muted); font-size: 12px; text-align: center; padding: 6px 0 0; }

/* ---- разговоры (на телефоне — отдельный экран, на ПК — боковая панель) ---- */
#list { background: var(--side); }
.side-head {
  flex-shrink: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 8px 8px 4px 16px; padding-top: calc(8px + env(safe-area-inset-top));
}
.brand { font-weight: 700; font-size: 1.05em; display: flex; align-items: center; }
.brand img { width: 30px; height: 30px; border-radius: 8px; margin-right: 10px; }
.btn-new {
  margin: 8px 12px 10px; background: var(--accent); color: var(--accent-text); font-weight: 600; border-radius: 16px;
  display: flex; align-items: center; justify-content: center; min-height: 52px;
}
.btn-new .ico { font-size: 1.2em; margin-right: 8px; font-weight: 400; }
.chat-list { flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 12px 10px; }
.chat-item { display: flex; align-items: center; border-radius: 14px; margin-bottom: 4px; }
.chat-item.active { background: var(--card); box-shadow: var(--shadow); }
.chat-open {
  flex: 1 1 auto; min-width: 0; text-align: left; background: none; border: 0; font: inherit; padding: 12px 14px; color: var(--text);
  overflow: hidden; cursor: pointer;
}
.chat-open .t { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-open small { display: block; color: var(--muted); font-size: .78em; margin-top: 2px; }
.chat-del { background: none; border: 0; font-size: 1.1em; padding: 12px 12px; color: var(--muted); cursor: pointer; }
.list-footer { text-align: center; padding: 6px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }

/* ---- компьютер: панель слева, чат справа, всё компактнее ---- */
@media (min-width: 900px) {
  body { font-size: 16px; line-height: 1.5; }
  body.in #app { display: flex; }
  body.in #list, body.in #chat { display: flex !important; position: static; height: 100%; }
  body.in #list {
    order: 0; width: 280px; flex-shrink: 0; border-right: 1px solid var(--line);
    -webkit-transition: width .2s ease, margin-left .2s ease; transition: width .2s ease, margin-left .2s ease; overflow: hidden;
  }
  body.in.nosb #list { width: 0; margin-left: -1px; border-right: 0; }
  body.in #chat { order: 1; flex: 1 1 auto; min-width: 0; }
  #btn-back { display: none; }
  #btn-new { display: none; }                 /* одна кнопка — в панели */
  body.nosb #btn-new { display: inline-block; } /* панель скрыта — кнопка в шапке */
  .side-head { padding: 12px 10px 4px 18px; }
  .btn-new { margin: 6px 12px 10px; min-height: 42px; border-radius: 12px; }
  .btn { min-height: 38px; padding: 6px 12px; border-radius: 10px; }
  #btn-list .lbl { display: none; }
  #btn-list { font-size: 20px; padding: 4px 10px; }
  #btn-list .ico { margin: 0; }
  .topbar { padding: 8px 12px; min-height: 52px; }
  .topbar-title { display: block; text-align: left; font-size: 15px; }
  .chat-open { padding: 9px 12px; }
  .chat-del { padding: 8px 10px; }
  .chat-item { margin-bottom: 2px; }
  .chat-item:hover { background: rgba(255,255,255,.55); }
  .chat-item.active:hover { background: var(--card); }
  .btn-ghost:hover { background: var(--accent-soft); }
  #messages { padding: 24px 28px; }
  .composer, .attach-preview { padding-left: 28px; padding-right: 28px; }
  .inner, .msg, .empty { max-width: 760px; margin-left: auto; margin-right: auto; }
  .msg { margin-bottom: 12px; }
  .bubble { max-width: 78%; padding: 10px 14px; border-radius: 16px; }
  .msg-user .bubble { border-bottom-right-radius: 4px; }
  .msg-assistant .bubble { border-bottom-left-radius: 4px; }
  .btn-attach, .btn-send { width: 42px; height: 42px; border-radius: 50%; padding: 0; }
  .btn-attach svg, .btn-send svg { width: 20px; height: 20px; }
  #input { min-height: 42px; padding: 9px 14px; border-radius: 21px; }
  .btn-big { font-size: 20px; min-height: 52px; }
  .login-box h1 { font-size: 28px; }
  #login-code { font-size: 26px; }
  .composer-hint { display: block; }
  .btn-link { font-size: 14px; }
}

/* ---- просмотр картинки ---- */
.viewer {
  position: fixed; left: 0; top: 0; right: 0; bottom: 0; background: #1a1412; z-index: 10;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
}
.viewer img { max-width: 100%; max-height: 80%; -webkit-touch-callout: default; }
.viewer-hint { color: #cbb; text-align: center; font-size: .85em; padding: 0 16px; }
.viewer-close { position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 10px; font-size: 24px; color: #fff; }
