:root {
  --bg: #f6f1ea;
  --panel: #fffdf9;
  --text: #2b2724;
  --muted: #8a8079;
  --me: #e3ece4;
  --me-text: #22301f;
  --bot: #fffdf9;
  --border: #e6ddd2;
  --accent: #6f8f72;
  --accent-text: #ffffff;
  --error: #b5513f;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b1a;
    --panel: #252321;
    --text: #ebe6e0;
    --muted: #9b928a;
    --me: #2f3d31;
    --me-text: #e3eee2;
    --bot: #2a2826;
    --border: #3a3633;
    --accent: #8db190;
    --accent-text: #15200f;
    --error: #e08a78;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
#app { height: 100%; max-width: 760px; margin: 0 auto; }
#app.loading { visibility: hidden; }
button { font: inherit; cursor: pointer; }
.ghost { background: none; border: 0; color: var(--muted); padding: 6px 10px; border-radius: 8px; }
.ghost:hover { color: var(--text); }

/* вход */
#login { height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
#login[hidden] { display: none; }
.login-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 18px; padding: 28px 24px; box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 8px; font-size: 24px; font-weight: 600; }
.login-card p { margin: 0 0 18px; color: var(--muted); }
#login-form { display: flex; gap: 8px; }
#login-form input {
  flex: 1; min-width: 0; font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
#login-form button, #send {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 10px; padding: 10px 16px;
}
.error { color: var(--error); min-height: 1.5em; margin: 12px 0 0 !important; }

/* чат */
#chat { height: 100%; display: flex; flex-direction: column; }
#chat[hidden] { display: none; }
header {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border); background: var(--bg);
}
.title { font-weight: 600; }
.status { flex: 1; color: var(--muted); font-size: 13px; }
#log { flex: 1; overflow-y: auto; padding: 16px; overscroll-behavior: contain; }
.older { display: block; margin: 0 auto 12px; }
.older[hidden] { display: none; }

.day { text-align: center; color: var(--muted); font-size: 12px; margin: 16px 0 8px; }
.msg {
  max-width: 85%; margin: 6px 0; padding: 9px 13px; border-radius: 16px;
  white-space: pre-wrap; overflow-wrap: anywhere; box-shadow: var(--shadow);
}
.msg.me { margin-left: auto; background: var(--me); color: var(--me-text); border-bottom-right-radius: 6px; }
.msg.bot { margin-right: auto; background: var(--bot); border: 1px solid var(--border); border-bottom-left-radius: 6px; }
.msg.service { margin: 8px auto; background: none; border: 1px dashed var(--border); color: var(--muted); box-shadow: none; }
.meta { display: block; font-size: 11px; color: var(--muted); margin-top: 3px; text-align: right; }
.msg.pending { opacity: .6; }
.msg.failed { border: 1px solid var(--error); }

.typing { display: flex; gap: 4px; width: 58px; }
.typing[hidden] { display: none; }
.typing span { width: 7px; height: 7px; 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%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing span { animation: none; opacity: .6; } }

#composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--bg);
}
#input {
  flex: 1; min-width: 0; resize: none; font: inherit; line-height: 1.4; max-height: 40vh;
  padding: 10px 12px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text);
}
#input:focus, #login-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#send { display: flex; align-items: center; justify-content: center; width: 44px; height: 44px; padding: 0; border-radius: 50%; }
#send:disabled { opacity: .5; cursor: default; }
