:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --sunken: #efefec;
  --line: #e3e3df;
  --text: #1d1d1f;
  --muted: #626267; /* ≥4.5:1 и на --bg, и на --sunken (выбранная строка чата) */
  --accent: #2f6fe4;
  --accent-text: #ffffff;
  --danger: #d1342f;
  --user: #e9eefb;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151515;
    --surface: #1e1e1f;
    --sunken: #262628;
    --line: #333335;
    --text: #f2f2f2;
    --muted: #9a9aa0;
    --accent: #5b8cf5;
    --accent-text: #0d1424;
    --danger: #ff6b63;
    --user: #24304a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
::selection { background: color-mix(in srgb, var(--accent) 28%, transparent); }
input, textarea { caret-color: var(--accent); }
* { scrollbar-color: var(--line) transparent; }
p, li, .msg-text, .lead, .empty-chat { text-wrap: pretty; }
h1, h2, h3 { text-wrap: balance; }
code, pre { font-family: var(--mono); font-size: 14px; }
.muted { color: var(--muted); }
/* атрибуты корня спрайта не доходят до <use>, поэтому обводка задаётся здесь */
.i { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* --- кнопки --- */
.btn, .btn-text, .tab, .nav-item {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent;
  transition: transform 80ms ease, opacity 80ms ease, background 80ms ease;
  white-space: nowrap;
}
.btn:active, .btn-text:active, .tab:active, .nav-item:active, .session:active { transform: scale(0.97); opacity: 0.75; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn.wide { width: 100%; }
.btn-text { border-color: transparent; background: transparent; padding: 0 10px; }
.btn-text.danger { color: var(--danger); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* --- вход --- */
.login {
  max-width: 380px; margin: 0 auto; min-height: 100dvh;
  display: flex; flex-direction: column; justify-content: center; gap: 12px;
  padding: max(24px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}
.login h1 { margin: 0; font-size: 28px; }
.login p { margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 14px; color: var(--muted); }
.field input {
  min-height: 48px; padding: 0 14px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}
.error-text { color: var(--danger); }

/* --- раскладка: телефон --- */
.app { height: var(--app-h, 100dvh); display: flex; flex-direction: column; } /* --app-h ставит app.js по visualViewport */
.pane { flex: 1; min-height: 0; display: none; flex-direction: column; background: var(--bg); }
body[data-view="sessions"] .pane-sessions,
body[data-view="chat"] .pane-chat,
body[data-view="api"] .pane-api { display: flex; }

.bar {
  display: flex; align-items: center; gap: 8px; justify-content: space-between;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line); background: var(--bg); min-height: 56px;
}
.bar h1, .bar h2 { margin: 0; font-size: 17px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-title { min-width: 0; display: flex; flex-direction: column; }
.status { margin: 0; font-size: 13px; color: var(--accent); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status::before { content: ""; display: inline-block; width: 6px; height: 6px; margin-right: 6px; border-radius: 50%; background: currentColor; vertical-align: 2px; }

.tabbar {
  display: grid; grid-template-columns: repeat(3, 1fr);
  border-top: 1px solid var(--line); background: var(--surface);
  padding: 4px max(8px, env(safe-area-inset-right)) max(4px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}
.tab {
  flex-direction: column; gap: 2px; border: 0; background: transparent;
  font-size: 11px; color: var(--muted); min-height: 50px;
}
.tab .i { width: 22px; height: 22px; }
body[data-view="sessions"] .tab[data-go="sessions"],
body[data-view="chat"] .tab[data-go="chat"],
body[data-view="api"] .tab[data-go="api"] { color: var(--accent); }
/* клавиатура открыта: прячем таббар, чтобы осталось место под ленту */
@media (max-width: 899px) {
  body:has(#input:focus) .tabbar { display: none; }
  .side-nav { display: none; }
}

/* --- список чатов --- */
.sessions { list-style: none; margin: 0; padding: 6px 8px; overflow-y: auto; flex: 1; }
.session {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 10px 12px; border-radius: 10px; cursor: pointer; min-height: 56px;
  transition: transform 80ms ease, opacity 80ms ease;
}
.session[aria-current="true"] { background: var(--sunken); }
.session-row { display: flex; gap: 8px; align-items: baseline; }
.session-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.session .when { flex: none; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.session-preview { display: block; font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-preview .live { color: var(--accent); }
.session-group { padding: 16px 12px 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
.session-group:first-child { padding-top: 6px; }
.sessions .empty { padding: 24px 12px; color: var(--muted); }

/* --- чат --- */
.scroller { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.messages {
  max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px;
  padding: 16px max(16px, env(safe-area-inset-right)) 24px max(16px, env(safe-area-inset-left));
}
.empty-chat { margin: 12vh auto 0; text-align: center; color: var(--muted); max-width: 360px; }
.empty-chat strong { display: block; color: var(--text); font-size: 22px; margin-bottom: 6px; }
.empty-chat p { margin: 0 0 20px; }
.examples { display: grid; gap: 8px; }
.btn.example { justify-content: flex-start; text-align: left; white-space: normal; padding: 10px 14px; color: var(--text); }

.msg-user {
  align-self: flex-end; max-width: 85%; background: var(--user);
  padding: 10px 14px; border-radius: 18px 18px 6px 18px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg-text { overflow-wrap: anywhere; }
.msg-text > :first-child { margin-top: 0; }
.msg-text > :last-child { margin-bottom: 0; }
.msg-text pre { background: var(--sunken); border-radius: 10px; padding: 12px; overflow-x: auto; }
.msg-text pre code.hljs { background: transparent; padding: 0; }
/* цвета github-темы на --sunken не дотягивают до 4.5:1, затемняем их */
@media (prefers-color-scheme: light) {
  .hljs-doctag, .hljs-keyword, .hljs-template-tag, .hljs-template-variable, .hljs-type, .hljs-variable.language_ { color: #b3202f; }
  .hljs-built_in, .hljs-symbol { color: #9a4500; }
  .hljs-name, .hljs-quote, .hljs-selector-pseudo, .hljs-selector-tag { color: #17692e; }
  .hljs-comment, .hljs-code, .hljs-formula { color: #5a6169; }
}
.msg-text :not(pre) > code { background: var(--sunken); padding: 1px 5px; border-radius: 5px; }
.msg-text table { border-collapse: collapse; display: block; overflow-x: auto; }
.msg-text th, .msg-text td { border: 1px solid var(--line); padding: 6px 10px; }
.msg-text a { color: var(--accent); }
.msg-text.live::after { content: ""; display: inline-block; width: 8px; height: 1em; margin-left: 2px; vertical-align: -2px; background: var(--muted); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.tool { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.tool > summary {
  display: flex; align-items: center; gap: 8px; min-height: 44px; padding: 0 12px;
  cursor: pointer; list-style: none; font-size: 14px; -webkit-tap-highlight-color: transparent;
}
.tool > summary::-webkit-details-marker { display: none; }
.tool-name { font-weight: 600; flex: none; }
.tool-arg { color: var(--muted); font-family: var(--mono); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.tool .chev { transition: transform 120ms ease; color: var(--muted); }
.tool[open] .chev { transform: rotate(90deg); }
.tool.pending .tool-name::after { content: ""; display: inline-block; width: 10px; height: 10px; margin-left: 8px; border: 2px solid var(--muted); border-right-color: transparent; border-radius: 50%; animation: spin 700ms linear infinite; vertical-align: -1px; }
.tool.failed .tool-name { color: var(--danger); }
@keyframes spin { to { transform: rotate(360deg); } }
.tool-body { border-top: 1px solid var(--line); padding: 10px 12px; display: grid; gap: 8px; }
.tool-body pre { margin: 0; max-height: 360px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; background: var(--sunken); border-radius: 8px; padding: 10px; }

.msg-result { font-size: 13px; color: var(--muted); }
.msg-error { display: flex; gap: 8px; color: var(--danger); white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; }
.msg-note { font-size: 14px; color: var(--muted); }

.composer {
  display: flex; flex-direction: column; gap: 6px;
  padding: 10px max(12px, env(safe-area-inset-right)) 6px max(12px, env(safe-area-inset-left));
  border-top: 1px solid var(--line); background: var(--bg);
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; resize: none; min-height: 44px; max-height: 40dvh; font-size: 16px; line-height: 1.4;
  padding: 11px 14px; border: 1px solid var(--line); border-radius: 22px; background: var(--surface);
}
#send { border-radius: 22px; }
.composer-tools { display: flex; gap: 2px; align-items: center; margin-left: -6px; }
.tool-btn { min-height: 36px; padding: 0 8px; font-size: 14px; color: var(--muted); gap: 5px; }
.tool-btn .i { width: 16px; height: 16px; }
#model-label { max-width: 52vw; overflow: hidden; text-overflow: ellipsis; }
.edit-note { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 14px; color: var(--accent); }
.edit-note .btn-text { min-height: 32px; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; }
.file-chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%;
  padding: 4px 4px 4px 10px; border-radius: 999px; background: var(--sunken); font-size: 14px;
}
.file-chip > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chip .i { width: 15px; height: 15px; }
.file-chip.uploading { color: var(--muted); }
.file-chip.error { color: var(--danger); }
.chip-x { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border: 0; border-radius: 50%; background: transparent; cursor: pointer; color: var(--muted); }
.chip-x:active { opacity: 0.6; }
.msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.msg-user .file-chip { background: color-mix(in srgb, var(--surface) 60%, transparent); padding-right: 10px; }
@media (max-width: 899px) {
  body:has(#input:focus) .composer { padding-bottom: max(6px, env(safe-area-inset-bottom)); }
  #send span { display: none; } /* на телефоне кнопка круглая, подпись в aria-label */
  #send { width: 44px; padding: 0; }
}

/* действия у последнего ответа и сообщения */
.msg-actions { display: flex; gap: 4px; margin-top: -6px; margin-left: -10px; }
.msg-actions .btn-text, .edit-btn { min-height: 36px; font-size: 14px; color: var(--muted); }
.edit-btn { align-self: flex-end; margin-top: -10px; }
.msg-actions .i, .edit-btn .i { width: 16px; height: 16px; }
.msg-text pre { position: relative; }
.code-copy {
  position: absolute; top: 6px; right: 6px; display: inline-flex; align-items: center; gap: 4px;
  min-height: 32px; padding: 0 8px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--muted); font: 13px/1 -apple-system, system-ui, sans-serif; cursor: pointer;
}
.code-copy .i { width: 14px; height: 14px; }
.code-copy:active { opacity: 0.7; }
@media (hover: hover) {
  .code-copy { opacity: 0; transition: opacity 120ms ease; }
  .msg-text pre:hover .code-copy, .code-copy:focus-visible { opacity: 1; }
}

.thinking { border-left: 2px solid var(--line); padding-left: 12px; color: var(--muted); }
.thinking > summary { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; cursor: pointer; list-style: none; font-size: 14px; }
.thinking > summary::-webkit-details-marker { display: none; }
.thinking .i { width: 16px; height: 16px; }
.thinking[open] .chev { transform: rotate(90deg); }
.thinking-body { white-space: pre-wrap; font-size: 14px; padding-bottom: 6px; max-height: 360px; overflow: auto; }

.composer { position: relative; }
.to-bottom {
  position: absolute; right: 16px; top: -56px; z-index: 5;
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.12); cursor: pointer;
}
.to-bottom:active { transform: scale(0.95); }

.search { padding: 8px 8px 0; }
.search input {
  width: 100%; min-height: 40px; padding: 0 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}

/* листы выбора: снизу на телефоне, по центру на десктопе */
dialog.sheet {
  border: 0; padding: 16px 16px max(16px, env(safe-area-inset-bottom)); margin: auto auto 0; width: 100%; max-width: 520px;
  max-height: 85dvh; overflow-y: auto; border-radius: 16px 16px 0 0; background: var(--surface); color: var(--text);
}
dialog.sheet::backdrop { background: rgb(0 0 0 / 0.32); }
dialog.sheet h2 { margin: 0 0 8px; font-size: 17px; }
dialog.sheet fieldset { border: 0; margin: 0 0 12px; padding: 0; }
dialog.sheet legend { font-size: 13px; font-weight: 600; color: var(--muted); padding: 8px 0 6px; }
.choice { display: flex; gap: 12px; align-items: flex-start; padding: 10px 4px; border-radius: 10px; cursor: pointer; }
.choice:has(input:checked) { background: var(--sunken); }
.choice input { margin-top: 4px; accent-color: var(--accent); width: 18px; height: 18px; flex: none; }
.choice b { display: block; font-weight: 600; }
.choice small { display: block; font-size: 14px; color: var(--muted); }
.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; background: var(--sunken); padding: 4px; border-radius: 12px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; }
.segmented span { display: flex; align-items: center; justify-content: center; min-height: 40px; border-radius: 9px; font-size: 14px; cursor: pointer; }
.segmented input:checked + span { background: var(--surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }
.menu { display: grid; gap: 2px; }
.menu-item {
  display: flex; align-items: center; gap: 10px; min-height: 50px; padding: 0 12px; border: 0; border-radius: 10px;
  background: transparent; font-size: 17px; text-align: left; cursor: pointer;
}
.menu-item:active { background: var(--sunken); }
.menu-item.danger { color: var(--danger); }
.ins-field textarea {
  min-height: 120px; padding: 10px 14px; font-size: 16px; line-height: 1.45; resize: vertical;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}
.row { display: flex; gap: 8px; align-items: center; }
@media (min-width: 900px) {
  dialog.sheet { margin: auto; border-radius: 16px; }
  .menu-item:hover, .choice:hover { background: var(--sunken); }
}

/* --- подключение --- */
.api-body { overflow-y: auto; flex: 1; padding: 16px max(16px, env(safe-area-inset-right)) 32px max(16px, env(safe-area-inset-left)); max-width: 820px; width: 100%; margin: 0 auto; }
.api-body h3 { margin: 32px 0 8px; font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
.api-body p { margin: 0 0 10px; }
.api-body pre { background: var(--sunken); border-radius: 10px; padding: 12px; margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.code-block { display: grid; gap: 8px; justify-items: start; }
.lead { color: var(--muted); }
.key-status { display: flex; gap: 8px; align-items: center; color: var(--danger); }
.key-status.ok { color: var(--text); }
.key-status.ok .i { color: var(--accent); }
.key-form { display: grid; gap: 12px; justify-items: start; margin-top: 4px; }
.key-form .field { width: 100%; }
.key-form .field a { color: var(--accent); }
.key-form .hint, .api-body .hint { font-size: 14px; margin: 0 0 10px; }
.key-form .step { margin: 4px 0 0; }
.key-form .row { display: flex; gap: 8px; align-items: center; }
a.btn { color: inherit; text-decoration: none; }
.tok-form { display: flex; gap: 8px; }
.tok-form input { flex: 1; min-width: 0; min-height: 44px; padding: 0 12px; font-size: 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.tok-new { margin-top: 10px; padding: 12px 14px; border-radius: 12px; background: var(--user); }
.tok-new p { margin: 0 0 8px; font-size: 14px; }
.tok-new code { overflow-wrap: anywhere; white-space: normal; }
.tok-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 4px; }
.tok-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.tok-info { min-width: 0; display: grid; }
.tok-meta { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.segmented.tabs { grid-template-columns: repeat(5, 1fr); margin-bottom: 10px; }
.segmented.tabs span { font-size: 13px; padding: 0 4px; text-align: center; }
.api-body h3:first-child { margin-top: 4px; }
.copy-row { display: flex; gap: 8px; align-items: center; }
.copy-row code { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; background: var(--sunken); padding: 11px 12px; border-radius: 10px; }
.models { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.models li { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.models code { overflow: hidden; text-overflow: ellipsis; }
#logout { margin-top: 32px; }

/* тост внизу, над полем ввода и таббаром: под большим пальцем и не закрывает шапку */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 128px); z-index: 10;
  display: flex; align-items: center; gap: 12px;
  width: max-content; max-width: calc(100% - 32px); padding: 6px 6px 6px 16px; min-height: 48px; border-radius: 14px;
  background: var(--text); color: var(--bg); font-size: 15px;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.18);
}
.toast:not(:has(.toast-action)) { padding-right: 16px; }
.toast-action {
  min-height: 36px; padding: 0 12px; border: 0; border-radius: 10px; cursor: pointer;
  background: color-mix(in srgb, var(--bg) 16%, transparent); color: inherit; font-weight: 600;
}
.toast-action:active { opacity: 0.7; }

/* --- раскладка: десктоп --- */
@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: 300px 1fr; grid-template-rows: var(--app-h, 100dvh); }
  .toast { bottom: 96px; }
  .tabbar { display: none; }
  .pane-sessions { display: flex !important; border-right: 1px solid var(--line); background: var(--surface); }
  .pane-sessions .bar { background: var(--surface); }
  body[data-view="sessions"] .pane-chat { display: flex; }
  .pane-chat, .pane-api { grid-column: 2; grid-row: 1; }
  .side-nav { padding: 8px; border-top: 1px solid var(--line); }
  .nav-item { width: 100%; justify-content: flex-start; border: 0; background: transparent; }
  body[data-view="api"] .nav-item[data-go="api"] { background: var(--sunken); }
  .session:hover { background: var(--sunken); }
  .tool > summary:hover { background: var(--sunken); border-radius: var(--radius); }
  .composer { padding-inline: max(16px, calc((100% - 820px) / 2 + 16px)); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
