/* Чат PWA: лента, композер, список диалогов, Markdown, просмотр картинок. Токены — из app.css. */

.content:has(.chat-shell) { max-width: none; padding: 0; }
.chat-shell { --chat-top: calc(65px + env(safe-area-inset-top)); position: relative; display: grid; grid-template-columns: 1fr;
    height: calc(100dvh - var(--chat-top) - var(--ab-nav-h) - env(safe-area-inset-bottom)); min-height: 360px; background: var(--ab-bg); }
@media (min-width: 1024px) {
    .chat-shell { grid-template-columns: 300px minmax(0, 1fr); height: calc(100dvh - var(--chat-top)); }
}

/* ---------- список диалогов ---------- */
.chat-sidebar { display: none; flex-direction: column; min-height: 0; background: var(--ab-surface); border-right: 1px solid var(--ab-border); }
@media (min-width: 1024px) { .chat-sidebar { display: flex; } }
.chat-shell.show-list .chat-sidebar { display: flex; position: absolute; inset: 0; z-index: 30; }
.chat-sidebar-head { padding: 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--ab-border); }
.chat-sidebar-head .row { display: flex; gap: 8px; }
.chat-list { flex: 1; overflow-y: auto; padding: 6px; overscroll-behavior: contain; }
.chat-row { position: relative; display: flex; align-items: center; border-radius: 10px; }
.chat-row:hover { background: var(--ab-surface-2); }
.chat-row.active { background: var(--ab-primary-soft); }
.chat-item { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 8px 40px 8px 10px; border: 0; background: transparent;
    color: var(--ab-text); font: inherit; font-size: .93rem; text-align: left; cursor: pointer; border-radius: 10px; }
.chat-row.active .chat-item { font-weight: 600; }
.chat-item .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.chat-row .row-menu { position: absolute; right: 2px; top: 50%; transform: translateY(-50%); opacity: 0; transition: opacity .15s; }
.chat-row:hover .row-menu, .chat-row:focus-within .row-menu, .chat-row.active .row-menu { opacity: 1; }
@media (hover: none) { .chat-row .row-menu { opacity: 1; } }
.chat-folder-head .chat-item { color: var(--ab-text-2); font-weight: 600; }
.chat-folder-count { font-size: .75rem; color: var(--ab-text-2); margin-right: 4px; }
.chat-folder-body { margin-left: 14px; padding-left: 6px; border-left: 1px solid var(--ab-border); }
.chat-list .empty { padding: 20px 10px; font-size: .9rem; }

/* ---------- основная колонка ---------- */
.chat-main { position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-toolbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; min-height: 56px; border-bottom: 1px solid var(--ab-border); background: var(--ab-surface); flex-wrap: nowrap; }
.chat-toolbar .title { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-toolbar select { min-height: 40px; max-width: 46vw; border-radius: 10px; border: 1px solid var(--ab-border); background: var(--ab-surface-2); color: var(--ab-text);
    font: 500 .88rem var(--ab-font); padding: 0 28px 0 10px; }
@media (min-width: 1024px) { .chat-toolbar .list-toggle { display: none; } .chat-toolbar select { max-width: 240px; } }
.ctx-chip { display: inline-flex; flex-direction: column; gap: 3px; flex: none; min-width: 70px; color: var(--ab-text-2); font-size: .72rem; line-height: 1.1; }
.ctx-chip .ctx-text { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.ctx-bar { display: block; height: 3px; border-radius: 2px; background: var(--ab-border); overflow: hidden; }
.ctx-bar i { display: block; height: 100%; min-width: 2px; background: var(--ab-grad); transition: width .3s; }
.ctx-chip.warn { color: var(--ab-warning); } .ctx-chip.warn .ctx-bar i { background: var(--ab-warning); }
.ctx-chip.danger { color: var(--ab-error); } .ctx-chip.danger .ctx-bar i { background: var(--ab-error); }
@media (max-width: 600px) { .ctx-chip { display: none; } }

.chat-scroll { flex: 1; overflow-y: auto; padding: 20px 14px 8px; overscroll-behavior: contain; }
.chat-thread { max-width: 860px; margin: 0 auto; display: flex; flex-direction: column; gap: 20px; }
.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg.user { justify-content: flex-end; }
.msg-avatar { flex: 0 0 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--ab-surface-2); border: 1px solid var(--ab-border); }
.msg-avatar img { width: 22px; height: 22px; }
.msg-body { min-width: 0; max-width: 100%; flex: 1; }
.msg.user .msg-body { flex: 0 1 auto; max-width: min(85%, 680px); }
.bubble-user { background: var(--ab-primary-soft); border: 1px solid color-mix(in srgb, var(--ab-primary) 30%, transparent); border-radius: 16px 16px 4px 16px;
    padding: 10px 14px; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.55; }
.msg-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; margin-top: 6px; color: var(--ab-text-2); font-size: .78rem; }
.msg-meta button { border: 0; background: transparent; color: var(--ab-text-2); cursor: pointer; padding: 6px; border-radius: 8px; display: inline-flex; min-width: 32px; min-height: 32px; align-items: center; justify-content: center; }
.msg-meta button:hover { background: var(--ab-surface-2); color: var(--ab-text); }
.msg-error { background: rgba(248, 113, 113, .12); color: var(--ab-error); border: 1px solid rgba(248, 113, 113, .3); border-radius: 12px; padding: 10px 14px; }
.msg-attachments { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-bottom: 6px; }
.attach-thumb { width: 96px; height: 96px; border-radius: 10px; object-fit: cover; border: 1px solid var(--ab-border); background: var(--ab-surface-2); display: block; }
.attach-file { display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px; border: 1px solid var(--ab-border); border-radius: 10px; background: var(--ab-surface); font-size: .85rem; max-width: 240px; color: var(--ab-text); text-decoration: none; }
.attach-file span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.typing { display: inline-flex; gap: 4px; align-items: center; color: var(--ab-text-2); font-size: .9rem; margin-top: 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--ab-accent); animation: ab-blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes ab-blink { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

.chat-welcome { max-width: 640px; margin: 4vh auto 0; text-align: center; }
.chat-welcome .brain { max-width: 260px; }
.chat-welcome h2 { font-size: 1.5rem; margin: 6px 0; }
.chat-suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-top: 18px; text-align: left; }
.suggestion { border: 1px solid var(--ab-border); border-radius: 12px; padding: 12px 14px; background: var(--ab-surface); cursor: pointer; font: inherit; color: var(--ab-text); text-align: left; min-height: 56px; transition: border-color .15s; }
.suggestion:hover { border-color: var(--ab-primary); }

/* ---------- композер ---------- */
.composer-wrap { position: relative; padding: 8px 12px 10px; border-top: 1px solid var(--ab-border); background: var(--ab-surface); }
.composer { max-width: 860px; margin: 0 auto; border: 1px solid var(--ab-border); border-radius: 16px; background: var(--ab-surface-2); padding: 6px 6px 6px 12px;
    display: flex; flex-direction: column; gap: 4px; transition: border-color .15s, box-shadow .15s; }
.composer:focus-within { border-color: var(--ab-primary); box-shadow: 0 0 0 3px var(--ab-primary-soft); }
.composer textarea { width: 100%; border: 0; outline: none; resize: none; background: transparent; color: var(--ab-text); font: 400 1rem/1.5 var(--ab-font); max-height: 240px; min-height: 26px; padding: 6px 2px; }
.composer-row { display: flex; align-items: center; gap: 4px; }
.composer-row .grow { flex: 1; }
.composer-pending { display: flex; flex-wrap: wrap; gap: 6px; }
.pending-chip { display: inline-flex; align-items: center; gap: 6px; max-width: 220px; padding: 4px 4px 4px 10px; border-radius: 999px; background: var(--ab-surface); border: 1px solid var(--ab-border); font-size: .82rem; }
.pending-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pending-chip button { border: 0; background: transparent; color: var(--ab-text-2); cursor: pointer; border-radius: 50%; width: 28px; height: 28px; display: grid; place-items: center; }
.cbtn { width: 40px; height: 40px; border-radius: 12px; border: 0; background: transparent; color: var(--ab-text-2); display: grid; place-items: center; cursor: pointer; }
.cbtn:hover { background: var(--ab-surface); color: var(--ab-text); }
.cbtn.on { color: var(--ab-accent); }
.cbtn.send { background: var(--ab-grad); color: var(--ab-on-primary); }
.cbtn.send:disabled { opacity: .4; cursor: not-allowed; }
.cbtn.stop { background: var(--ab-error); color: #fff; }
.composer select { min-height: 36px; border-radius: 10px; border: 1px solid var(--ab-border); background: var(--ab-surface); color: var(--ab-text-2); font: 500 .8rem var(--ab-font); padding: 0 6px; max-width: 150px; }
.scroll-down { position: absolute; left: 50%; top: -48px; transform: translate(-50%, 6px); width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--ab-border);
    background: var(--ab-surface); color: var(--ab-text); display: grid; place-items: center; cursor: pointer; opacity: 0; pointer-events: none; transition: opacity .15s, transform .15s; box-shadow: var(--ab-shadow); }
.scroll-down.visible { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
.context-hint { max-width: 860px; margin: 0 auto 8px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 8px 12px; border-radius: 12px;
    background: rgba(245, 158, 11, .1); border: 1px solid rgba(245, 158, 11, .35); font-size: .88rem; }
.context-hint .text { flex: 1; min-width: 200px; }
.context-hint button { border: 0; background: transparent; color: var(--ab-accent); font: 600 .88rem var(--ab-font); cursor: pointer; padding: 6px 8px; border-radius: 8px; min-height: 36px; }
.drop-overlay { position: absolute; inset: 0; z-index: 10; display: none; align-items: center; justify-content: center; background: var(--ab-primary-soft);
    border: 2px dashed var(--ab-primary); font-weight: 600; color: var(--ab-text); pointer-events: none; }
.chat-main.dragging .drop-overlay { display: flex; }

/* ---------- Markdown ответа ---------- */
.markdown { line-height: 1.65; overflow-wrap: anywhere; }
.markdown > :first-child { margin-top: 0; }
.markdown > :last-child { margin-bottom: 0; }
.markdown p { margin: 0 0 12px; }
.markdown h1, .markdown h2, .markdown h3, .markdown h4 { margin: 18px 0 8px; line-height: 1.3; font-weight: 650; }
.markdown h1 { font-size: 1.35rem; } .markdown h2 { font-size: 1.2rem; } .markdown h3 { font-size: 1.07rem; }
.markdown ul, .markdown ol { padding-left: 1.4em; margin: 0 0 12px; }
.markdown li { margin: 4px 0; }
.markdown a { color: var(--ab-accent); }
.markdown blockquote { margin: 0 0 12px; padding: 4px 14px; border-left: 3px solid var(--ab-primary); color: var(--ab-text-2); }
.markdown code { font: .875em var(--ab-mono); background: var(--ab-surface-2); border: 1px solid var(--ab-border); border-radius: 6px; padding: 1px 6px; }
.markdown pre { position: relative; margin: 0 0 14px; background: #070B16; border: 1px solid var(--ab-border); border-radius: 12px; overflow: auto; }
:root[data-theme="light"] .markdown pre { background: #F3F5FA; }
.markdown pre code { display: block; padding: 14px 16px; background: transparent; border: 0; font-size: .84rem; line-height: 1.55; }
.markdown pre .code-copy { position: absolute; top: 6px; right: 6px; border: 1px solid var(--ab-border); background: var(--ab-surface); color: var(--ab-text-2); border-radius: 8px;
    font: .75rem var(--ab-font); padding: 4px 8px; cursor: pointer; opacity: 0; transition: opacity .15s; }
.markdown pre:hover .code-copy, .markdown pre .code-copy:focus-visible { opacity: 1; }
@media (hover: none) { .markdown pre .code-copy { opacity: 1; } }
.markdown table { border-collapse: collapse; margin: 0 0 14px; display: block; overflow-x: auto; max-width: 100%; }
.markdown th, .markdown td { border: 1px solid var(--ab-border); padding: 6px 10px; text-align: left; }
.markdown th { background: var(--ab-surface-2); font-weight: 600; }
.markdown hr { border: 0; border-top: 1px solid var(--ab-border); margin: 16px 0; }
.markdown a.zoom-link { display: inline-block; text-decoration: none; line-height: 0; }
.markdown img { max-width: min(100%, 520px); max-height: 460px; object-fit: contain; background: #fff; border: 1px solid var(--ab-border); border-radius: 10px; }
.markdown.streaming { white-space: pre-wrap; }

/* Подсветка кода (highlight.js) — тёмная тема. */
:root[data-theme="dark"] .hljs { color: #E6EDF3; background: transparent; }
:root[data-theme="dark"] .hljs-keyword, :root[data-theme="dark"] .hljs-type, :root[data-theme="dark"] .hljs-template-tag { color: #FF7B72; }
:root[data-theme="dark"] .hljs-title, :root[data-theme="dark"] .hljs-title.class_, :root[data-theme="dark"] .hljs-title.function_ { color: #D2A8FF; }
:root[data-theme="dark"] .hljs-attr, :root[data-theme="dark"] .hljs-literal, :root[data-theme="dark"] .hljs-number, :root[data-theme="dark"] .hljs-variable, :root[data-theme="dark"] .hljs-meta { color: #79C0FF; }
:root[data-theme="dark"] .hljs-string, :root[data-theme="dark"] .hljs-regexp { color: #A5D6FF; }
:root[data-theme="dark"] .hljs-built_in, :root[data-theme="dark"] .hljs-symbol { color: #FFA657; }
:root[data-theme="dark"] .hljs-comment { color: #8B949E; }

/* ---------- просмотр картинок ---------- */
body.lightbox-open { overflow: hidden; }
.lightbox { position: fixed; inset: 0; z-index: 3000; display: none; align-items: center; justify-content: center; padding: 56px 72px 48px; background: rgba(5, 8, 16, .9); }
.lightbox.open { display: flex; }
.lightbox figure { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 10px; max-width: 100%; max-height: 100%; }
.lightbox img { max-width: min(94vw, 1500px); max-height: calc(100dvh - 130px); object-fit: contain; background: #fff; border-radius: 8px; }
.lightbox figcaption { color: #e5e7eb; font-size: .85rem; text-align: center; }
.lb-btn { position: absolute; display: inline-flex; align-items: center; justify-content: center; border: 1px solid rgba(255, 255, 255, .28); border-radius: 999px;
    background: rgba(255, 255, 255, .12); color: #fff; cursor: pointer; font: inherit; text-decoration: none; }
.lb-close { top: calc(14px + env(safe-area-inset-top)); right: 16px; width: 44px; height: 44px; }
.lb-nav { top: 50%; width: 46px; height: 46px; margin-top: -23px; font-size: 1.8rem; }
.lb-prev { left: 14px; } .lb-next { right: 14px; }
.lb-download { top: calc(14px + env(safe-area-inset-top)); right: 70px; height: 44px; padding: 0 16px; font-size: .9rem; }
@media (max-width: 720px) { .lightbox { padding: 64px 10px 64px; } .lb-nav { top: auto; bottom: calc(10px + env(safe-area-inset-bottom)); margin: 0; } }
