:root {
  --bg: #faf9f5; --side: #f3f1ea; --surface: #ffffff; --bubble: #ece9e0; --hover: #e8e5db;
  --fg: #1f1e1d; --muted: #6f6d66; --line: #dedbd1; --accent: #c96442; --accent-fg: #ffffff;
  --send: #c96442; --send-fg: #ffffff; --danger: #b3261e; --shadow: 0 1px 3px rgba(0,0,0,.06), 0 4px 18px rgba(0,0,0,.05);
  --serif: ui-serif, "Iowan Old Style", "Charter", "Noto Serif", Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 19px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624; --side: #1f1e1d; --surface: #30302e; --bubble: #141413; --hover: #3a3936;
    --fg: #f5f4ef; --muted: #a6a39b; --line: #3e3d39; --accent: #d97757; --accent-fg: #ffffff;
    --send: #f5f4ef; --send-fg: #1f1e1d; --danger: #ff8a80; --shadow: 0 1px 3px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--fg); font-family: var(--sans); line-height: 1.55;
       -webkit-text-size-adjust: 100%; overflow: hidden; }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
svg { width: 24px; height: 24px; flex: none; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.spark { color: var(--accent); }
.spark.big { width: 44px; height: 44px; }

/* ---------- кнопки ---------- */
.icon-btn { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center;
            border-radius: 12px; color: var(--fg); flex: none; }
.icon-btn:hover { background: var(--hover); }
.icon-btn.round { border-radius: 50%; }
.icon-btn.outline { border: 1px solid var(--line); cursor: pointer; }
.text-btn { color: var(--muted); padding: 0.4rem 0.6rem; border-radius: 10px; }
.text-btn:hover { background: var(--hover); color: var(--fg); }
.primary { background: var(--accent); color: var(--accent-fg); border-radius: 14px; padding: 0.8rem 1rem;
           font-weight: 600; min-height: 3.1rem; }
.primary:disabled { opacity: .5; }
.error { color: var(--danger); min-height: 1.4em; margin: 0; }

/* ---------- вход ---------- */
.login { height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 400px; display: flex; flex-direction: column; gap: 1rem; }
.login-card h1 { font-family: var(--serif); font-weight: 500; font-size: 1.8rem; margin: 0 0 0.4rem; }
.login-card label { display: flex; flex-direction: column; gap: 0.35rem; color: var(--muted); font-size: 0.9rem; }
.login-card input { padding: 0.8rem 0.9rem; border: 1px solid var(--line); border-radius: 14px;
                    background: var(--surface); color: var(--fg); font-size: 1.05rem; }
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* ---------- каркас ---------- */
.app { display: flex; height: 100%; height: 100dvh; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.drawer { width: 300px; background: var(--side); display: flex; flex-direction: column;
          padding: calc(env(safe-area-inset-top) + 10px) 12px calc(env(safe-area-inset-bottom) + 10px);
          border-right: 1px solid var(--line); }
.scrim { display: none; }
.only-mobile { display: none; }
@media (max-width: 899px) {
  .only-mobile { display: inline-flex; }
  .drawer { position: fixed; inset: 0 auto 0 0; width: min(86vw, 330px); z-index: 20;
            transform: translateX(-102%); transition: transform .22s ease; box-shadow: var(--shadow); border: 0; }
  .app.drawer-open .drawer { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 10;
           opacity: 0; pointer-events: none; transition: opacity .22s; }
  .app.drawer-open .scrim { opacity: 1; pointer-events: auto; }
}

/* ---------- боковое меню ---------- */
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 0 4px 10px; }
.brand { display: flex; align-items: center; gap: 8px; font-family: var(--serif); font-size: 1.3rem; }
.new-chat { display: flex; align-items: center; gap: 10px; padding: 0.7rem 0.8rem; border-radius: 14px;
            color: var(--accent); font-weight: 600; }
.new-chat svg { background: var(--accent); color: var(--accent-fg); border-radius: 50%; padding: 4px; width: 28px; height: 28px; }
.new-chat:hover { background: var(--hover); }
.drawer-label { color: var(--muted); font-size: 0.8rem; padding: 1rem 0.8rem 0.3rem; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-list button { width: 100%; text-align: left; padding: 0.6rem 0.8rem; border-radius: 12px;
                    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.chat-list button:hover { background: var(--hover); }
.chat-list button.active { background: var(--hover); font-weight: 600; }
.drawer-foot { display: flex; align-items: center; justify-content: space-between; border-top: 1px solid var(--line);
               padding: 10px 4px 0; }
.me { font-weight: 600; }

/* ---------- верхняя панель ---------- */
.topbar { display: flex; align-items: center; gap: 6px; padding: calc(env(safe-area-inset-top) + 6px) 10px 6px; }
.title { flex: 1; min-width: 0; margin: 0; font-size: 1rem; font-weight: 500; white-space: nowrap;
         overflow: hidden; text-overflow: ellipsis; padding-left: 6px; }
#new-chat-top { background: var(--surface); box-shadow: var(--shadow); }

/* ---------- лента ---------- */
.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 0 16px; }
.thread { max-width: 760px; margin: 0 auto; padding: 8px 0 24px; display: flex; flex-direction: column; gap: 22px; }
.empty { max-width: 760px; margin: 0 auto; min-height: 60%; display: flex; flex-direction: column; align-items: center;
         justify-content: center; text-align: center; gap: 0.4rem; padding-top: 10vh; }
.empty h2 { font-family: var(--serif); font-weight: 400; font-size: 1.9rem; margin: 0.3rem 0 0; line-height: 1.25; }
.empty p { color: var(--muted); margin: 0; max-width: 26em; }

.msg-user { align-self: flex-end; max-width: 85%; background: var(--bubble); border-radius: 22px 22px 6px 22px;
            padding: 0.7rem 1.05rem; white-space: pre-wrap; overflow-wrap: anywhere; }
@media (prefers-color-scheme: dark) { .msg-user { background: #3a3936; } }
.msg-user .thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 0.45rem; }
.msg-user .thumbs:last-child { margin-bottom: 0; }
.msg-user img { max-width: 220px; max-height: 220px; border-radius: 14px; display: block; }

.chip { display: inline-flex; align-items: center; gap: 6px; padding: 0.35rem 0.7rem; border-radius: 12px;
        background: var(--surface); border: 1px solid var(--line); font-size: 0.85rem; max-width: 100%; }
.chip svg { width: 20px; height: 20px; color: var(--accent); }
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.msg-ai { font-family: var(--serif); font-size: 1.08rem; line-height: 1.6; overflow-wrap: anywhere; }
.msg-ai p { margin: 0 0 0.75rem; }
.msg-ai ul, .msg-ai ol { margin: 0 0 0.75rem; padding-left: 1.4rem; }
.msg-ai li { margin: 0.2rem 0; }
.msg-ai strong { font-weight: 700; }
.msg-ai a { color: var(--accent); }
.msg-ai code { font-family: ui-monospace, monospace; font-size: 0.85em; background: var(--surface); padding: 0.1em 0.3em; border-radius: 6px; }
.msg-ai pre { font-family: ui-monospace, monospace; font-size: 0.85rem; background: var(--surface); padding: 0.8rem;
              border-radius: 12px; overflow-x: auto; white-space: pre-wrap; border: 1px solid var(--line); }
.msg-ai .body > :last-child { margin-bottom: 0; }
.msg-ai.error .body { color: var(--danger); font-family: var(--sans); }

.status { display: flex; align-items: center; gap: 10px; color: var(--muted); font-family: var(--sans); font-size: 0.95rem; }
.status svg { width: 22px; height: 22px; color: var(--accent); animation: spin 2.4s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.sources { margin-top: 0.9rem; display: flex; flex-wrap: wrap; gap: 8px; font-family: var(--sans); }
.sources a { display: inline-flex; align-items: center; gap: 6px; max-width: 100%; padding: 0.3rem 0.7rem;
             border: 1px solid var(--line); border-radius: 999px; font-size: 0.8rem; color: var(--fg); text-decoration: none; }
.sources a:hover { background: var(--hover); }
.sources a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sources a b { color: var(--accent); font-weight: 600; }

.actions { display: flex; gap: 4px; margin-top: 0.6rem; margin-left: -10px; font-family: var(--sans); }
.actions button { display: inline-flex; align-items: center; gap: 6px; padding: 0.45rem 0.7rem; border-radius: 12px;
                  color: var(--muted); font-size: 0.85rem; min-height: 44px; }
.actions button:hover { background: var(--hover); color: var(--fg); }
.actions svg { width: 22px; height: 22px; }

/* ---------- поле ввода ---------- */
.composer-wrap { padding: 6px 12px calc(env(safe-area-inset-bottom) + 6px); }
.composer { max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--line);
            border-radius: 26px; padding: 10px 10px 8px 14px; box-shadow: var(--shadow); }
.composer textarea { width: 100%; resize: none; border: 0; outline: 0; background: transparent; color: var(--fg);
                     padding: 6px 4px 8px; max-height: 40vh; line-height: 1.45; display: block; }
.composer textarea::placeholder { color: var(--muted); }
.composer-row { display: flex; align-items: center; gap: 8px; }
.spacer { flex: 1; }
.send { width: 46px; height: 46px; border-radius: 50%; background: var(--send); color: var(--send-fg);
        display: inline-flex; align-items: center; justify-content: center; transition: opacity .15s; }
.send:disabled { opacity: .3; cursor: default; }
.send.busy svg { animation: spin 1.2s linear infinite; }
.pending-files { display: flex; flex-wrap: wrap; gap: 10px; }
.pending-files:not(:empty) { padding: 4px 0 8px; }
.pending { position: relative; }
.pending img { width: 76px; height: 76px; object-fit: cover; border-radius: 14px; display: block; border: 1px solid var(--line); }
.pending.loading { opacity: .5; }
.pending .remove { position: absolute; top: -8px; right: -8px; width: 28px; height: 28px; border-radius: 50%;
                   background: var(--fg); color: var(--bg); display: flex; align-items: center; justify-content: center; }
.pending .remove svg { width: 16px; height: 16px; }
.disclaimer { text-align: center; color: var(--muted); font-size: 0.72rem; margin: 6px 0 0; }

.toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 110px); transform: translateX(-50%);
         background: var(--fg); color: var(--bg); padding: 0.6rem 1rem; border-radius: 14px; font-size: 0.9rem;
         z-index: 30; max-width: calc(100vw - 32px); box-shadow: var(--shadow); }

/* ---------- Markdown ---------- */
.msg-ai h2, .msg-ai h3, .msg-ai h4 { font-family: var(--serif); font-weight: 600; line-height: 1.3; margin: 1.1rem 0 0.5rem; }
.msg-ai h2 { font-size: 1.3rem; }
.msg-ai h3 { font-size: 1.15rem; }
.msg-ai h4 { font-size: 1.05rem; }
.msg-ai .body > :first-child { margin-top: 0; }
.msg-ai blockquote { margin: 0 0 0.75rem; padding: 0.1rem 0 0.1rem 1rem; border-left: 3px solid var(--line); color: var(--muted); }
.msg-ai hr { border: 0; border-top: 1px solid var(--line); margin: 1.2rem 0; }
.msg-ai li > ul, .msg-ai li > ol { margin: 0.2rem 0 0; }
.msg-ai pre code { background: none; padding: 0; font-size: inherit; }
.table-wrap { overflow-x: auto; margin: 0 0 0.9rem; border: 1px solid var(--line); border-radius: 12px; }
.msg-ai table { border-collapse: collapse; width: 100%; font-family: var(--sans); font-size: 0.88rem; line-height: 1.4; }
.msg-ai th, .msg-ai td { padding: 0.55rem 0.75rem; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
.msg-ai tr:last-child td { border-bottom: 0; }
.msg-ai th { font-weight: 600; background: var(--surface); white-space: nowrap; }
.msg-ai .a-right { text-align: right; }
.msg-ai .a-center { text-align: center; }

/* ---------- файлы от Claude ---------- */
.files:empty { display: none; }
.files { display: flex; flex-direction: column; gap: 10px; margin: 0.9rem 0 0.2rem; font-family: var(--sans); }
.file-card { display: flex; align-items: center; gap: 14px; padding: 12px 12px 12px 14px; border: 1px solid var(--line);
             border-radius: 16px; background: var(--surface); color: var(--fg); text-decoration: none; }
.file-card:hover { background: var(--hover); }
.file-icon { width: 46px; height: 54px; border-radius: 8px; border: 1px solid var(--line); display: flex;
             align-items: center; justify-content: center; color: var(--accent); background: var(--bg); flex: none; }
.file-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.file-name { font-weight: 600; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow-wrap: anywhere; line-height: 1.3; }
.msg-ai .file-card { color: var(--fg); }
.file-type { color: var(--muted); font-size: 0.8rem; }
.file-dl { display: inline-flex; align-items: center; gap: 6px; padding: 0.45rem 0.8rem; border-radius: 12px;
           background: var(--hover); font-size: 0.85rem; font-weight: 600; flex: none; }
.file-dl svg { width: 20px; height: 20px; }
@media (max-width: 420px) { .file-dl span { display: none; } .file-dl { padding: 0.5rem; } }

/* ---------- даты ---------- */
.day-sep { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 0.8rem; margin: 6px 0 -6px; }
.day-sep::before, .day-sep::after { content: ""; flex: 1; border-top: 1px solid var(--line); }
.msg-time { align-self: flex-end; color: var(--muted); font-size: 0.75rem; margin: -16px 6px 0 0; }
.chat-list button { display: flex; align-items: baseline; gap: 10px; }
.chat-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-date { color: var(--muted); font-size: 0.75rem; font-weight: 400; flex: none; }

/* ---------- память ---------- */
.drawer-item { display: flex; align-items: center; gap: 10px; padding: 0.6rem 0.8rem; border-radius: 12px; color: var(--fg); }
.drawer-item svg { width: 22px; height: 22px; color: var(--muted); margin: 0 3px; }
.drawer-item:hover { background: var(--hover); }
.mem-notes { display: flex; flex-direction: column; gap: 4px; margin-top: 0.7rem; font-family: var(--sans); }
.mem-note { display: inline-flex; align-items: flex-start; gap: 8px; color: var(--muted); font-size: 0.82rem;
            text-decoration: none; line-height: 1.4; }
.mem-note:hover { color: var(--fg); }
.mem-note svg { width: 18px; height: 18px; flex: none; margin-top: 1px; }
.memory-view { flex: 1; overflow-y: auto; padding: 8px 16px 24px; }
.memory-inner { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 0.9rem; }
.memory-inner h2 { font-family: var(--serif); font-weight: 500; font-size: 1.5rem; margin: 0.4rem 0 0; }
.memory-hint { color: var(--muted); margin: 0; font-size: 0.92rem; }
.memory-actions { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.memory-actions .primary { padding: 0.7rem 1.6rem; }
.text-btn.danger { color: var(--danger); }
.msg-ai .mem-note { color: var(--muted); }
.msg-ai .mem-note:hover { color: var(--fg); }

.memory-files { display: flex; flex-direction: column; gap: 10px; }
.mem-group { color: var(--muted); font-size: 0.8rem; margin: 0.8rem 0 -2px 4px; }
.mem-file { border: 1px solid var(--line); border-radius: 16px; background: var(--surface); }
.mem-file.done { opacity: 0.7; }
.mem-file summary { list-style: none; cursor: pointer; padding: 0.8rem 1rem; display: flex; align-items: center; gap: 10px; }
.mem-file summary::-webkit-details-marker { display: none; }
.mem-file summary::after { content: "›"; color: var(--muted); font-size: 1.4rem; line-height: 1; transition: transform .15s; }
.mem-file[open] summary::after { transform: rotate(90deg); }
.mem-head { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.mem-title { font-weight: 600; overflow-wrap: anywhere; }
.mem-meta { color: var(--muted); font-size: 0.8rem; }
.mem-body { padding: 0 0.8rem 0.9rem; display: flex; flex-direction: column; gap: 8px; }
.mem-lines { display: flex; flex-direction: column; gap: 6px; }
.mem-line { display: flex; align-items: flex-start; gap: 4px; }
.mem-line textarea { flex: 1; resize: none; overflow: hidden; border: 1px solid var(--line); border-radius: 12px;
                     background: var(--bg); color: var(--fg); padding: 0.55rem 0.7rem; line-height: 1.45; }
.mem-line textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.mem-line .icon-btn { color: var(--muted); }
.mem-body > .text-btn { align-self: flex-start; color: var(--accent); }
