/* ─────────────────────────────────────────────────────────────────────────
   Design tokens. Everything visual is expressed through these — restyling
   the app means editing this block, not the components below.
   ───────────────────────────────────────────────────────────────────────── */
:root {
  --bg:            #0f1115;
  --bg-elev:       #171a21;
  --surface:       #1d212a;
  --surface-2:     #252a35;
  --border:        #2c3240;
  --text:          #e8eaf0;
  --text-dim:      #98a0b3;
  --text-faint:    #6b7488;
  --accent:        #6d8cff;
  --accent-soft:   #6d8cff1f;
  --accent-fg:     #ffffff;
  --danger:        #ff6b6b;
  --ok:            #4ade80;
  --star:          #f5b82e;

  --radius:        14px;
  --radius-sm:     10px;
  --radius-lg:     20px;
  --gap:           12px;
  --pad:           16px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  --fs:            16px;
  --fs-sm:         13px;
  --fs-xs:         11.5px;

  --shadow:        0 8px 28px #0008;
  --dur:           .18s;

  --safe-top:      env(safe-area-inset-top, 0px);
  --safe-bottom:   env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f9; --bg-elev: #ffffff; --surface: #ffffff; --surface-2: #eef0f4;
    --border: #dfe3ea; --text: #16181d; --text-dim: #5c6474; --text-faint: #8a92a3;
    --accent: #3b62f6; --accent-soft: #3b62f614; --shadow: 0 8px 28px #0f172a14;
    --star: #d99a00;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* Все иконки — из одного спрайта и одного размера: только так кнопки
   выравниваются друг относительно друга. */
.ic { width: 20px; height: 20px; display: block; flex: none; pointer-events: none; }
.ic-sm { width: 16px; height: 16px; }

/* A class-level display rule outranks the hidden attribute's UA style. */
[hidden] { display: none !important; }

html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font: var(--fs)/1.5 var(--font);
  overscroll-behavior: none;
}

body { display: flex; flex-direction: column; }

.view { display: none; flex: 1; min-height: 0; flex-direction: column; }
.view.active { display: flex; }

.scroll { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* ── controls ───────────────────────────────────────────────────────────── */

.input {
  width: 100%; padding: 12px 14px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  font: inherit; outline: none;
  transition: border-color var(--dur);
}
.input:focus { border-color: var(--accent); }
.input::placeholder { color: var(--text-faint); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 18px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  font: 600 var(--fs)/1 var(--font); cursor: pointer;
  transition: background var(--dur), transform var(--dur);
  touch-action: manipulation;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn-block { width: 100%; }

.icon-btn {
  flex: none;
  width: 42px; height: 42px;
  display: grid; place-items: center;
  background: transparent; color: var(--text-dim);
  border: none; border-radius: var(--radius-sm);
  font-size: 20px; line-height: 1; cursor: pointer;
  touch-action: manipulation; position: relative;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn-pin.on { color: var(--star); --star-fill: currentColor; }

.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text-dim); font-size: var(--fs-sm); cursor: pointer;
  white-space: nowrap;
}
.chip-install { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

.badge {
  position: absolute; top: 3px; right: 3px;
  min-width: 16px; height: 16px; padding: 0 4px;
  border-radius: 999px; background: var(--accent); color: var(--accent-fg);
  font: 600 var(--fs-xs)/16px var(--font); text-align: center;
}

/* ── login ──────────────────────────────────────────────────────────────── */

#view-login { align-items: center; justify-content: center; padding: var(--pad); }
.login-card {
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: var(--gap);
  padding: 28px 24px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow);
}
.login-title { margin: 0; font-size: 26px; letter-spacing: -.02em; }
.login-sub { margin: -4px 0 4px; color: var(--text-dim); font-size: var(--fs-sm); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: var(--fs-sm); color: var(--text-dim); }
.login-err { margin: 0; color: var(--danger); font-size: var(--fs-sm); }

/* ── top bars ───────────────────────────────────────────────────────────── */

.topbar {
  padding: calc(var(--safe-top) + 12px) var(--pad) 12px;
  background: var(--bg-elev); border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 10px;
}
.topbar-main { display: flex; align-items: center; gap: var(--gap); }
.topbar-title { margin: 0; font-size: 20px; flex: 1; letter-spacing: -.01em; }
.input-search { padding: 9px 12px; font-size: var(--fs-sm); border-radius: var(--radius-sm); }

.topbar-chat { flex-direction: row; align-items: center; gap: 4px; padding-right: 8px; }
#btn-back { font-size: 30px; margin-left: -8px; }
.chat-title {
  flex: 1; min-width: 0;
  background: none; border: none; color: var(--text);
  font: 600 var(--fs)/1.3 var(--font); text-align: left; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.model-select {
  flex: none; max-width: 96px; height: 30px;
  padding: 0 6px; border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text-dim);
  border: 1px solid var(--border); font-size: var(--fs-xs); outline: none;
}

/* ── chat list ──────────────────────────────────────────────────────────── */

#chat-list { padding: 8px var(--pad) 96px; display: flex; flex-direction: column; gap: 8px; }

.chat-card {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px;
  padding: 14px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  cursor: pointer; transition: border-color var(--dur);
}
.chat-card:active { border-color: var(--accent); }
.chat-card-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-card-time { color: var(--text-faint); font-size: var(--fs-xs); white-space: nowrap; }
.chat-card-star { color: var(--star); margin-right: 6px; }
.chat-card-preview {
  grid-column: 1 / -1; color: var(--text-dim); font-size: var(--fs-sm);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.chat-card-meta { grid-column: 1 / -1; display: flex; gap: 8px; margin-top: 6px; }
.tag {
  padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-faint);
  font-size: var(--fs-xs);
}
.tag-files { background: var(--accent-soft); color: var(--accent); }

.empty { padding: 48px 24px; text-align: center; color: var(--text-faint); }

.fab-wrap {
  position: sticky; bottom: 0;
  padding: 12px var(--pad) calc(var(--safe-bottom) + 12px);
  background: linear-gradient(to top, var(--bg) 55%, transparent);
}
.btn-fab { width: 100%; box-shadow: var(--shadow); }
.build {
  margin-top: 8px; text-align: center;
  font-size: var(--fs-xs); color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

/* ── messages ───────────────────────────────────────────────────────────── */

.messages { padding: var(--pad) var(--pad) 8px; display: flex; flex-direction: column; gap: 14px; }

.msg { display: flex; flex-direction: column; gap: 8px; max-width: 100%; }
.msg-user { align-items: flex-end; }

.bubble {
  max-width: min(86%, 640px);
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--accent); color: var(--accent-fg);
  white-space: pre-wrap; word-wrap: break-word;
}
.msg-assistant .bubble {
  max-width: 100%; width: 100%;
  background: transparent; color: var(--text);
  padding: 0; white-space: normal;
}

/* markdown */
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 .7em; }
.bubble ul, .bubble ol { margin: 0 0 .7em; padding-left: 1.3em; }
.bubble li { margin: .2em 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 1em 0 .4em; line-height: 1.25; }
.bubble h1 { font-size: 1.35em; } .bubble h2 { font-size: 1.2em; } .bubble h3 { font-size: 1.05em; }
.bubble code {
  font: .88em/1.4 var(--mono);
  background: var(--surface-2); padding: .15em .4em; border-radius: 6px;
}
.bubble pre {
  margin: 0 0 .7em; padding: 12px 14px; overflow-x: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.bubble pre code { background: none; padding: 0; font-size: .85em; }
.bubble table { border-collapse: collapse; margin: 0 0 .7em; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.bubble blockquote {
  margin: 0 0 .7em; padding: .1em 0 .1em 14px;
  border-left: 3px solid var(--border); color: var(--text-dim);
}
.bubble a { color: var(--accent); }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }

.cursor::after {
  content: '▍'; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

/* thinking / tools */
.fold {
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-elev); overflow: hidden; width: 100%;
}
.fold > summary {
  padding: 9px 12px; cursor: pointer; list-style: none;
  color: var(--text-dim); font-size: var(--fs-sm);
  display: flex; align-items: center; gap: 8px;
}
.fold > summary::-webkit-details-marker { display: none; }
.fold-ic { display: grid; place-items: center; flex: none; color: var(--text-faint); }
.fold-name { font-weight: 600; color: var(--text); }
.fold-arg { color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fold-body {
  padding: 0 12px 12px; font: var(--fs-xs)/1.5 var(--mono);
  color: var(--text-dim); white-space: pre-wrap; word-break: break-word;
  max-height: 320px; overflow-y: auto;
}
.fold-err > summary,
.fold-err > summary .fold-name,
.fold-err > summary .fold-ic { color: var(--danger); }
.fold-sep { margin: 8px 0 4px; color: var(--text-faint); }

/* ход работы: одна строка-спойлер вместо стопки вызовов */
.steps { align-self: flex-start; max-width: 100%; }
.steps[open] { align-self: stretch; }
.steps > summary {
  display: inline-flex; align-items: center; gap: 7px; max-width: 100%;
  padding: 5px 10px 5px 9px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  color: var(--text-dim); font-size: var(--fs-xs); line-height: 1.35;
  cursor: pointer; list-style: none; user-select: none; -webkit-user-select: none;
}
.steps > summary::-webkit-details-marker { display: none; }
.steps-label { font-weight: 600; white-space: nowrap; }
.steps-err { color: var(--danger); white-space: nowrap; }
.steps-last {
  min-width: 0; color: var(--text-faint); font-family: var(--mono);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.steps-chev { transition: transform var(--dur); }
.steps[open] .steps-chev { transform: rotate(90deg); }
.steps.is-live > summary > .fold-ic:first-child { color: var(--accent); animation: pulse 1.1s ease-in-out infinite; }
.steps-body {
  margin-top: 6px; overflow: hidden;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--bg-elev);
}
.steps-body > .fold { border: 0; border-radius: 0; background: none; }
.steps-body > .fold + .fold { border-top: 1px solid var(--border); }

/* files */
.files { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.msg-user .files { justify-content: flex-end; }
.file-card {
  position: relative; overflow: hidden;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; min-width: 190px; max-width: 100%;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm); color: inherit; text-decoration: none;
  cursor: pointer;
}
.file-card:active { background: var(--surface-2); }
.file-card.is-done { border-left-color: var(--ok); }
.file-card.is-done .file-dl { color: var(--ok); }

/* полоса хода скачивания по нижней кромке карточки */
.file-bar {
  position: absolute; left: 0; right: 0; bottom: 0; height: 3px;
  background: transparent; pointer-events: none; overflow: hidden;
}
.file-bar > i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .12s linear; }
.file-card.is-done .file-bar > i { background: var(--ok); }

/* пока идёт скачивание, карточка заметно живёт: бегущий блик и пульс кромки */
.file-card.is-busy { border-left-color: var(--accent); }
.file-card.is-busy::after {
  content: ''; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(100deg, transparent 20%, var(--accent-soft) 50%, transparent 80%);
  background-size: 220% 100%;
  animation: sweep 1.1s linear infinite;
}
@keyframes sweep { from { background-position: 140% 0; } to { background-position: -60% 0; } }

.file-card.is-busy .file-dl { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.file-card.is-done { animation: saved .45s ease-out; }
@keyframes saved { 0% { transform: scale(1); } 40% { transform: scale(1.035); } 100% { transform: scale(1); } }
.file-icon { font-size: 20px; flex: none; }
.file-meta { min-width: 0; flex: 1; }
.file-name { font-size: var(--fs-sm); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-sub { font-size: var(--fs-xs); color: var(--text-faint); }
.file-dl { flex: none; color: var(--accent); font-size: 17px; }

.msg-note {
  align-self: center; padding: 4px 12px; border-radius: 999px;
  background: var(--surface); color: var(--text-faint); font-size: var(--fs-xs);
}
.msg-error { color: var(--danger); font-size: var(--fs-sm); }

/* ── composer ───────────────────────────────────────────────────────────── */

.composer {
  padding: 10px var(--pad) calc(var(--safe-bottom) + 10px);
  background: var(--bg-elev); border-top: 1px solid var(--border);
}
.composer-row { display: flex; align-items: flex-end; gap: 6px; }

.input-wrap { position: relative; flex: 1; min-width: 0; }
.composer-input {
  display: block; width: 100%;
  min-height: 42px; max-height: 40vh; resize: none;
  padding: 10px 40px 10px 12px; border-radius: var(--radius);
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  font: var(--fs)/1.4 var(--font); outline: none;
}
.composer-input:focus { border-color: var(--accent); }
/* полоса прокрутки в поле ввода только мешает: высота и так растёт по тексту */
.composer-input { scrollbar-width: none; }
.composer-input::-webkit-scrollbar { display: none; }

.mini-btn {
  position: absolute; right: 5px; top: 5px;
  display: grid; place-items: center;
  width: 30px; height: 30px;
  background: transparent; border: none; border-radius: 8px;
  color: var(--text-faint); cursor: pointer;
  transition: color var(--dur), background var(--dur), transform var(--dur);
}
.mini-btn:active { color: var(--text); background: var(--surface-2); transform: scale(.9); }

.composer-count {
  position: absolute; right: 12px; bottom: 8px;
  font-size: var(--fs-xs); color: var(--text-faint);
  pointer-events: none; font-variant-numeric: tabular-nums;
  display: none;
}
.icon-btn-send { background: var(--accent); color: var(--accent-fg); }
.icon-btn-send.stop { background: var(--danger); }
.icon-btn-send .ic-stop { display: none; }
.icon-btn-send.stop .ic-send { display: none; }
.icon-btn-send.stop .ic-stop { display: block; }
.icon-btn-attach { color: var(--text-dim); }

.attach-strip { display: flex; flex-wrap: wrap; gap: 6px; padding-bottom: 8px; }
.attach-strip:empty { display: none; }
.attach-pill {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px 6px 10px; border-radius: 999px;
  background: var(--surface-2); font-size: var(--fs-xs); max-width: 100%;
}
.attach-pill-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px; }
.attach-pill-x { cursor: pointer; color: var(--text-faint); font-size: 14px; }
.attach-pill-bar { width: 44px; height: 3px; border-radius: 2px; background: var(--border); overflow: hidden; }
.attach-pill-bar > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .15s; }

/* поле ввода во весь экран: чистая смена раскладки, элемент тот же */
#view-chat.expanded .messages { display: none; }
#view-chat.expanded .composer { flex: 1 1 auto; display: flex; flex-direction: column; padding-top: 12px; }
#view-chat.expanded .composer-row { flex: 1; align-items: stretch; }
#view-chat.expanded .icon-btn-attach,
#view-chat.expanded .icon-btn-send { align-self: flex-end; }
#view-chat.expanded .input-wrap { display: flex; }
#view-chat.expanded .composer-input {
  flex: 1; height: 100%; max-height: none;
  border-radius: var(--radius-lg); padding-bottom: 28px;
  line-height: 1.5;
}
#view-chat.expanded .composer-count { display: block; }

/* drag & drop */
.drop-hint {
  position: fixed; inset: 0; z-index: 40;
  display: none; place-items: center;
  background: #0f1115cc; color: var(--text);
  font-weight: 600; border: 2px dashed var(--accent);
}
body.dragging .drop-hint { display: grid; }

/* ── sheet ──────────────────────────────────────────────────────────────── */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: #0009; display: flex; align-items: flex-end;
}
.sheet {
  width: 100%; max-height: 76vh;
  display: flex; flex-direction: column;
  background: var(--bg-elev);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  animation: rise var(--dur) ease-out;
}
@keyframes rise { from { transform: translateY(16px); opacity: .6; } }
.sheet-head {
  display: flex; align-items: center; gap: var(--gap);
  padding: 12px var(--pad); border-bottom: 1px solid var(--border);
}
.sheet-title { margin: 0; font-size: var(--fs); flex: 1; }
.sheet-body { overflow-y: auto; padding: var(--pad); display: flex; flex-direction: column; gap: 8px; }
.sheet-group { color: var(--text-faint); font-size: var(--fs-xs); margin-top: 4px; }

/* ── toasts ─────────────────────────────────────────────────────────────── */

.toasts {
  position: fixed; z-index: 60; left: 0; right: 0;
  top: calc(var(--safe-top) + 10px);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  pointer-events: none;
}
.toast {
  max-width: 88vw; padding: 9px 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  box-shadow: var(--shadow); font-size: var(--fs-sm);
  animation: pop var(--dur) ease-out;
}
.toast-err { border-color: var(--danger); color: var(--danger); }
@keyframes pop { from { transform: translateY(-6px); opacity: 0; } }
