:root {
  --bg: #262624;
  --side: #1f1e1d;
  --panel: #30302e;
  --line: #3e3e3a;
  --text: #ece9e1;
  --muted: #a3a097;
  --accent: #d97757;
  --accent-hover: #c4633f;
  --user-bubble: #141413;
  --code-bg: #1a1a18;
  --serif: "Tiempos Text", Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #faf9f5;
    --side: #f0eee6;
    --panel: #ffffff;
    --line: #e2ded2;
    --text: #1f1e1d;
    --muted: #6f6c64;
    --user-bubble: #f0eee6;
    --code-bg: #f3f1ea;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--sans); -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-card h1 { font-family: var(--serif); font-weight: 400; margin: 0 0 8px; }
.login-card input { padding: 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); color: var(--text); font-size: 16px; }
.login-card button { padding: 14px; border-radius: 12px; border: 0; background: var(--accent); color: #fff; font-weight: 600; font-size: 16px; }
.err { color: #e5786d; min-height: 1.2em; margin: 0; font-size: 14px; }
.logo-mark { color: var(--accent); font-size: 36px; line-height: 1; }
.logo-mark.big { font-size: 44px; }

/* ---------- layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar { width: 280px; flex-shrink: 0; background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 12px; padding-top: max(12px, env(safe-area-inset-top)); }
.new-chat { width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: transparent; text-align: left; }
.new-chat:hover { background: var(--panel); }
.side-label { font-size: 12px; color: var(--muted); margin: 18px 8px 6px; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.chat-item { display: flex; align-items: center; border-radius: 8px; }
.chat-item a { flex: 1; min-width: 0; padding: 8px 10px; color: var(--text); text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.chat-item:hover, .chat-item.active { background: var(--panel); }
.chat-item .del { opacity: 0; border: 0; background: none; color: var(--muted); padding: 6px 8px; }
.chat-item:hover .del, .chat-item.active .del { opacity: 1; }
.side-bottom { display: flex; justify-content: space-between; align-items: center; padding: 10px 8px 0; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); padding-bottom: var(--safe-b); }
.link { border: 0; background: none; color: var(--muted); text-decoration: underline; }

.scrim { display: none; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); min-height: 52px; }
.chat-title { flex: 1; text-align: center; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { border: 0; background: none; font-size: 18px; width: 38px; height: 38px; border-radius: 10px; display: inline-grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--panel); color: var(--text); }
#menuBtn, #newChatTop { visibility: hidden; }

@media (max-width: 800px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .22s ease; width: 84vw; max-width: 300px; }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 10; }
  #menuBtn, #newChatTop { visibility: visible; }
}

/* ---------- messages ---------- */
.scroller { flex: 1; overflow-y: auto; padding: 8px 16px 24px; }
.empty { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; }
.empty h2 { font-family: var(--serif); font-weight: 400; font-size: 30px; margin: 0; text-align: center; }
.messages { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 22px; }
.msg.user { align-self: flex-end; max-width: 85%; background: var(--user-bubble); padding: 10px 16px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; line-height: 1.5; }
.msg.assistant { font-family: var(--serif); font-size: 17px; line-height: 1.65; word-wrap: break-word; }
.msg.assistant p { margin: 0 0 .8em; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { font-family: var(--sans); font-size: 1.05em; margin: 1.2em 0 .5em; }
.msg.assistant ul, .msg.assistant ol { padding-left: 1.4em; margin: 0 0 .8em; }
.msg.assistant li { margin: .25em 0; }
.msg.assistant a { color: var(--accent); }
.msg.assistant blockquote { border-left: 3px solid var(--line); margin: 0 0 .8em; padding-left: 12px; color: var(--muted); }
.msg.assistant table { border-collapse: collapse; margin: 0 0 .8em; font-family: var(--sans); font-size: 14px; display: block; overflow-x: auto; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 6px 10px; }
.msg.assistant :not(pre) > code { font-family: var(--mono); font-size: .85em; background: var(--code-bg); padding: 2px 5px; border-radius: 5px; }
.code-block { position: relative; margin: 0 0 .9em; border: 1px solid var(--line); border-radius: 10px; background: var(--code-bg); overflow: hidden; }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 12px; font-family: var(--sans); font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.code-head button { border: 0; background: none; color: var(--muted); font-size: 12px; }
.code-block pre { margin: 0; padding: 12px; overflow-x: auto; }
.code-block code { font-family: var(--mono); font-size: 13.5px; line-height: 1.5; }
.msg-actions { margin-top: 8px; display: flex; gap: 4px; }
.msg-actions button { border: 0; background: none; color: var(--muted); font-size: 13px; padding: 4px 8px; border-radius: 6px; font-family: var(--sans); }
.msg-actions button:hover { background: var(--panel); color: var(--text); }
.msg.error { color: #e5786d; font-size: 14px; }
.cursor::after { content: "●"; color: var(--accent); animation: pulse 1s infinite; margin-left: 2px; font-size: .7em; }
@keyframes pulse { 50% { opacity: .2; } }

/* ---------- composer ---------- */
.composer-wrap { padding: 0 12px calc(12px + var(--safe-b)); }
.composer { max-width: 760px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 8px 8px 8px 14px; box-shadow: 0 2px 12px rgba(0,0,0,.12); display: flex; align-items: flex-end; gap: 8px; }
.composer textarea { flex: 1; width: 100%; border: 0; outline: 0; resize: none; background: transparent; color: var(--text); font: 16px/1.5 var(--sans); max-height: 40dvh; padding: 4px 0; }
.send-btn { width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; font-size: 18px; font-weight: 700; display: grid; place-items: center; }
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: .4; cursor: default; }
.send-btn.stop { background: var(--text); color: var(--bg); font-size: 12px; }

/* индикатор ожидания ответа */
.typing { display: inline-flex; gap: 5px; padding: 6px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite both; }
.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; } }
