* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, "Segoe UI", system-ui, "Helvetica Neue", sans-serif;
  font-size: 16px;
  background: #f6f6f6;
  color: #1a1a1a;
  -webkit-tap-highlight-color: transparent;
}
.screen { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
.screen[hidden] { display: none; }
#screen-chat { height: 100dvh; }
.muted { color: #888; font-size: 13px; }
.error { color: #c33; }
button {
  font: inherit; cursor: pointer; border: 0; padding: 10px 16px; border-radius: 6px;
  background: #2563eb; color: white; min-height: 44px; min-width: 44px;
}
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.ghost { background: transparent; color: inherit; padding: 8px; }
input[type="password"], input[type="text"], textarea {
  font: inherit; padding: 10px 12px; border: 1px solid #ccc; border-radius: 6px;
  width: 100%; background: white; color: inherit;
}

/* Login */
.login-box { margin: auto; padding: 24px; max-width: 420px; width: 92%; text-align: center; }
.login-box h1 { font-size: 22px; margin: 0 0 12px; }
.login-box input { margin: 12px 0; }
.login-box button { width: 100%; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  background: #1a1a1a; color: white; position: sticky; top: 0; z-index: 10;
}
.topbar .title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar button { background: transparent; color: white; padding: 6px 10px; min-height: 36px; min-width: 36px; }
.topbar .status { font-size: 12px; opacity: 0.75; }

/* Подпись машины (27.08.2026). Мостов два — ES и VPS, — и страницы у них до знака
   одинаковые: заголовок вкладки, иконка и вёрстка совпадают, отличается только адрес.
   Заливка, а не цветной текст: метка стоит и на тёмной шапке, и на светлом экране входа. */
.host-badge {
  display: inline-block; padding: 1px 7px; border-radius: 999px;
  font-size: 12px; font-weight: 700; letter-spacing: 0.04em;
  color: #fff; background: #555; vertical-align: middle;
}
.host-badge.host-es  { background: #0369a1; }
.host-badge.host-vps { background: #b45309; }

/* Search bar */
.search-bar { padding: 8px 8px 0; }
.search-bar input { width: 100%; }

/* Sessions list */
.sessions-list { padding: 8px; overflow-y: auto; flex: 1; }
.session-card {
  background: white; border-radius: 8px; padding: 12px 14px; margin-bottom: 8px;
  display: flex; gap: 10px; align-items: flex-start; cursor: pointer;
  border: 1px solid #e5e5e5;
}
@keyframes unread-pulse { 0%,100% { opacity: 1; } 50% { opacity: .4; } }
.session-card .dot {
  width: 10px; height: 10px; border-radius: 50%; background: #ccc; margin-top: 6px; flex-shrink: 0;
}
.session-card .meta { flex: 1; min-width: 0; }
.session-card .meta .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis;
                          display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.session-card .meta .sub { font-size: 12px; color: #666; margin-top: 2px; }

/* Chat */
.ctx-bar {
  position: relative; height: 18px; background: #2a2a2e; flex-shrink: 0;
  border-bottom: 1px solid #3a3a3e; overflow: hidden;
}
.ctx-fill {
  position: absolute; left: 0; top: 0; bottom: 0; background: #16a34a;
  width: 0%; transition: width 0.3s, background 0.3s;
}
.ctx-bar.warn .ctx-fill { background: #f59e0b; }
.ctx-bar.danger .ctx-fill { background: #dc2626; }
.ctx-label {
  position: absolute; left: 0; right: 0; top: 0; bottom: 0;
  text-align: center; font-size: 11px; line-height: 18px;
  color: #fff; text-shadow: 0 0 2px rgba(0,0,0,0.7); pointer-events: none;
}
.chat-log { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.chat-input {
  border-top: 1px solid #ddd; background: white; padding: 8px;
  display: flex; flex-direction: column; gap: 6px;
}
.chat-input textarea { resize: none; }
.chat-buttons { display: flex; gap: 8px; align-items: center; }
.chat-buttons button { flex: 1; }
.attach-btn {
  cursor: pointer; font-size: 22px; min-width: 44px; min-height: 44px;
  display: flex; align-items: center; justify-content: center; flex: 0 0 auto;
}
.image-preview {
  display: flex; gap: 6px; overflow-x: auto; padding: 4px 0;
}
.image-preview .thumb {
  position: relative; flex-shrink: 0;
}
.image-preview .thumb img {
  height: 60px; border-radius: 6px; border: 1px solid #444;
}
/* min-width/min-height обязательны: глобальное `button {min-width:44px}` (строка 17)
   иначе раздувает крестик до 44px — width/height его не перебивают. */
.image-preview .thumb .remove {
  position: absolute; top: -4px; right: -4px; background: #c33; color: white;
  border: none; border-radius: 50%; width: 18px; height: 18px; font-size: 11px;
  min-width: 18px; min-height: 18px;
  cursor: pointer; line-height: 18px; text-align: center; padding: 0;
}
/* Файловое вложение (PDF, текст): картинки нет — показываем чип с именем. */
.image-preview .chip {
  position: relative; flex-shrink: 0; display: flex; align-items: center; gap: 6px;
  height: 60px; max-width: 150px; padding: 0 14px 0 10px; box-sizing: border-box;
  border: 1px solid #444; border-radius: 6px; font-size: 12px; line-height: 1.25;
}
.image-preview .chip .icon { font-size: 20px; flex: 0 0 auto; }
.image-preview .chip .meta { overflow: hidden; }
.image-preview .chip .fname {
  display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.image-preview .chip .fsize { opacity: .65; }
.image-preview .chip.pending { opacity: .55; }
.image-preview .chip .remove {
  position: absolute; top: -4px; right: -4px; background: #c33; color: white;
  border: none; border-radius: 50%; width: 18px; height: 18px; font-size: 11px;
  min-width: 18px; min-height: 18px;
  cursor: pointer; line-height: 18px; text-align: center; padding: 0;
}
/* Вложения в пузыре отправленного сообщения. */
.msg-files { margin-top: 4px; font-size: 12px; opacity: .8; }

.options-row {
  display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px;
  padding-top: 8px; border-top: 1px dashed #ccc;
}
.option-btn {
  padding: 8px 14px; border-radius: 14px; background: #2563eb; color: white;
  border: 0; font-size: 14px; cursor: pointer; min-height: 36px;
  white-space: normal; text-align: center; line-height: 1.2;
}
.option-btn:hover:not(:disabled) { background: #1e40af; }
.option-btn:disabled { opacity: 0.5; cursor: not-allowed; }
@media (prefers-color-scheme: dark) {
  .options-row { border-top-color: #444; }
}

.bubble { padding: 10px 12px; border-radius: 12px; max-width: 90%; word-wrap: break-word;
          white-space: pre-wrap; }
.bubble.user { align-self: flex-end; background: #2563eb; color: white; border-bottom-right-radius: 4px; }
.bubble.assistant { align-self: flex-start; background: white; border: 1px solid #e5e5e5;
                    border-bottom-left-radius: 4px; }
.bubble.system { align-self: center; background: #fffbe6; border: 1px solid #f5d96d;
                 font-size: 12px; max-width: 95%; }

/* Отметка времени сообщения. Пузырь — flex-колонка, а метка с order:999 всегда
   визуально последняя: у ответа блоки размышлений и инструментов дописываются уже
   после её вставки, иначе время оказывалось бы в середине пузыря. */
.bubble { display: flex; flex-direction: column; }
.bubble .ts {
  order: 999; align-self: flex-end; margin-top: 4px;
  font-size: 11px; line-height: 1; opacity: .65; white-space: nowrap;
}
.bubble.user .ts { opacity: .8; }

.bubble details { margin: 6px 0 0; }
.bubble details summary { cursor: pointer; font-size: 13px; opacity: 0.8; user-select: none; }
.bubble details pre { white-space: pre-wrap; word-wrap: break-word; font-size: 12px;
                       background: #0f1011; color: #c5e9a4; padding: 8px; border-radius: 6px;
                       overflow-x: auto; max-height: 280px; overflow-y: auto; margin: 6px 0 0; }

.bubble .thinking-block summary::before { content: "💭 "; }
.bubble .tool-block summary::before { content: "🔧 "; }

.turn-result {
  font-size: 11px; color: #888; margin-top: 4px; padding: 2px 6px;
  background: #f0f0f0; border-radius: 4px; align-self: flex-start;
}

.toast {
  position: fixed; top: 56px; left: 50%; transform: translateX(-50%);
  background: #f59e0b; color: black; padding: 8px 14px; border-radius: 6px;
  z-index: 100; font-size: 13px; max-width: 92%; box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}
.toast.error { background: #ef4444; color: white; }

@media (prefers-color-scheme: dark) {
  body { background: #0f0f10; color: #e8e8e8; }
  .session-card { background: #1c1c1f; border-color: #2a2a2e; }
  .session-card .meta .sub { color: #999; }
  .bubble.assistant { background: #1c1c1f; border-color: #2a2a2e; color: #e8e8e8; }
  .bubble.system { background: #2a2410; border-color: #614b1d; color: #e8d68a; }
  .turn-result { background: #1c1c1f; color: #999; }
  input[type="password"], input[type="text"], textarea {
    background: #1c1c1f; color: #e8e8e8; border-color: #2a2a2e;
  }
  .chat-input { background: #1c1c1f; border-color: #2a2a2e; }
}

/* Переключатель модели/усилия в чате.
   flex-wrap добавлен вместе с чипом аккаунта: пятый элемент на 360 px иначе
   выдавливал содержимое за экран (панель была flex без переноса). */
.model-bar { display:flex; flex-wrap:wrap; gap:14px; align-items:center; padding:4px 10px;
  background:#181818; border-bottom:1px solid #2a2a2a; font-size:12px; color:#9a9a9a; }
.model-bar label { display:flex; align-items:center; gap:5px; }
.model-bar select { background:#222; color:#ddd; border:1px solid #3a3a3a;
  border-radius:4px; padding:2px 6px; font-size:12px; }

/* Экран «мост уже открыт в другой вкладке» */
#screen-taken .login-box button { margin-top: 10px; }
#screen-taken button.secondary { background: #2a2a2a; color: #ddd; }
#screen-taken #taken-reason { line-height: 1.5; }

/* Кнопка «загрузить всю историю» вверху лога чата */
.load-all-btn { align-self: center; margin: 8px auto; background: #2a2a2a; color: #ddd;
  border: 1px solid #3a3a3a; font-size: 13px; padding: 8px 16px; }

/* ── Три категории сессий (цвет виден ВСЕГДА) ─────────────────────────────
   working — работа идёт, ответ не готов        → синий, пульсирует
   unread  — ответ готов, не прочитан           → оранжевый, пульсирует
   read    — ответы готовы и прочитаны          → зелёный, спокойный
   Цвет несёт полоса слева + точка. Не зависит от того, жив ли процесс claude:
   раньше окраска держалась только на живом процессе (30 мин) и «прочитано»
   вообще не выделялось. */
.session-card { position: relative; overflow: hidden; padding-left: 18px; }
.session-card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 6px;
  background: var(--status-color, #9ca3af);
}
.session-card .dot { background: var(--status-color, #9ca3af); }

.session-card.status-working { --status-color: #2563eb; border-color: #2563eb; }
.session-card.status-unread  { --status-color: #f59e0b; border-color: #f59e0b; background: #fffbeb; }
.session-card.status-read    { --status-color: #16a34a; }

/* Пульсация — только там, где ждём внимания */
.session-card.status-working .dot,
.session-card.status-unread .dot { animation: unread-pulse 1.5s ease-in-out infinite; }

@media (prefers-color-scheme: dark) {
  .session-card.status-working { --status-color: #3b82f6; border-color: #1d4ed8; }
  .session-card.status-unread  { --status-color: #f59e0b; border-color: #d97706; background: #292211; }
  .session-card.status-read    { --status-color: #22c55e; }
}

/* Легенда трёх категорий — чтобы цвет читался без объяснений.
   wrap — страховка после появления чипа аккаунта: на узком экране он переносится
   на вторую строку, а не выпихивает легенду за край (на 360 px не хватало 5 px). */
.status-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; padding: 4px 12px 0;
  font-size: 12px; color: #777; align-items: center; }
.status-legend .lg { display: flex; align-items: center; gap: 5px; }
.status-legend .lg::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor;
}
.status-legend .lg-working { color: #1d4ed8; }
.status-legend .lg-unread  { color: #a15c07; }
.status-legend .lg-read    { color: #15803d; }
@media (prefers-color-scheme: dark) {
  .status-legend { color: #888; }
  .status-legend .lg-working { color: #3b82f6; }
  /* Тёмный янтарь светлой темы на тёмном фоне почти не читался (контраст ~2.4:1) —
     берём тот же оттенок, что и у карточек в тёмной теме. */
  .status-legend .lg-unread  { color: #f59e0b; }
  .status-legend .lg-read    { color: #22c55e; }
}

/* Плашка «страница из кэша, есть новая версия» — поверх всего, вверху экрана.
   [hidden] прописан явно: display:flex в классе сильнее встроенного правила
   браузера для [hidden], и без этой строки плашка висела бы всегда. */
.update-bar[hidden] { display: none; }
.update-bar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; font-size: 13px; line-height: 1.35;
  background: #1d4ed8; color: #fff;
  box-shadow: 0 1px 6px rgba(0,0,0,.35);
  padding-top: calc(8px + env(safe-area-inset-top));
}
.update-bar button {
  margin-left: auto; flex: none; padding: 6px 14px; font-size: 13px;
  background: #fff; color: #1d4ed8; border: none; border-radius: 5px; font-weight: 600;
}
.update-bar button[disabled] { opacity: .6; }
/* Пока плашка видна — экран уезжает вниз на её высоту, иначе она накрывает шапку
   с кнопками «обновить список» / «выйти» и строку поиска. */
body.update-open .screen { margin-top: var(--update-h, 44px);
  height: calc(100vh - var(--update-h, 44px)); }
body.update-open #screen-chat { height: calc(100dvh - var(--update-h, 44px)); }

/* ── Режим планирования ────────────────────────────────────────────────────
   Пока включён — панель настроек и полоса слева от ленты янтарные, чтобы режим
   был виден при любой прокрутке (а не только по галочке вверху). */
.model-bar .plan-toggle { gap: 5px; }
.model-bar .plan-toggle input { accent-color: #d08a2e; width: 15px; height: 15px; }
.plan-badge { margin-left: auto; color: #e0a44a; font-weight: 600; font-size: 12px; }
/* Значок мультиагентного режима. Своим цветом — чтобы не путался с «планирую»:
   режим дорогой, и с телефона должно быть видно сразу. Когда оба значка видны,
   margin-left:auto первого забирает свободное место, второй прижимается к нему. */
.ultra-badge { color: #7fd1ff; margin-left: auto; }
body.plan-on .model-bar { background: #241d10; border-bottom-color: #5a4416; color: #e0a44a; }
body.plan-on .chat-log { box-shadow: inset 3px 0 0 #b8791f; }
body.plan-on #chat-text { border-color: #5a4416; }
/* Блок плана в ленте — раскрыт и выделен, это главное содержимое хода */
.bubble .tool-block.tool-plan { border-left: 3px solid #e0a44a; padding-left: 8px; }
.bubble .tool-block.tool-plan > summary { color: #e0a44a; font-weight: 600; }

/* ── Аккаунт Claude ────────────────────────────────────────────────────────
   Чип живёт в двух местах: в строке легенды (экран сессий) и в панели настроек
   чата — аккаунт нужен именно там, где обнаруживается упор в лимит. */
.acct-btn {
  margin-left: auto; flex: none; max-width: 55%;
  background: transparent; color: inherit; border: 1px solid currentColor;
  border-radius: 999px; padding: 2px 10px; font-size: 12px; line-height: 1.5;
  min-height: 24px; min-width: 0; opacity: .85;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.acct-btn[hidden] { display: none; }
.status-legend .acct-btn { color: #555; }
.model-bar .acct-btn { color: #9a9a9a; }
@media (prefers-color-scheme: dark) { .status-legend .acct-btn { color: #999; } }

/* Шторка. [hidden] прописан явно — тот же капкан, что у .update-bar:
   display:flex в классе сильнее встроенного правила браузера для [hidden]. */
.acct-sheet[hidden] { display: none; }
.acct-sheet { position: fixed; inset: 0; z-index: 90; display: flex; align-items: flex-end; }
.acct-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.acct-panel {
  position: relative; width: 100%; max-height: 80dvh; overflow-y: auto;
  background: #fff; color: #1a1a1a; border-radius: 14px 14px 0 0;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  box-shadow: 0 -2px 16px rgba(0,0,0,.35);
}
.acct-head { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 8px; }
.acct-head #acct-title { flex: 1; }
.acct-head button { background: transparent; color: inherit; padding: 4px 8px;
  min-height: 36px; min-width: 36px; font-size: 16px; }
.acct-list { display: flex; flex-direction: column; gap: 8px; }

.acct-row {
  display: block; width: 100%; text-align: left; background: #f3f4f6; color: inherit;
  border: 1px solid #e5e5e5; border-radius: 10px; padding: 10px 12px; min-height: 44px;
}
.acct-row.is-active { border-color: #16a34a; background: #f0fdf4; }
.acct-row[disabled] { opacity: .55; }
.acct-row .r1 { display: flex; align-items: baseline; gap: 8px; font-weight: 600; }
.acct-row .r-alias { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct-row .r-badge { font-size: 11px; font-weight: 600; color: #15803d; flex: none; }
.acct-row .r-mail { font-size: 12px; color: #666; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.acct-row .r-usage { font-size: 12px; color: #444; margin-top: 3px; }
.acct-row .r-warn { font-size: 12px; color: #a15c07; margin-top: 2px; }
.acct-row .r-block { font-size: 12px; color: #c33; margin-top: 2px; }
/* Полоска расхода — та же палитра, что у ctx-bar, чтобы «много/мало» читалось одинаково */
.acct-meter { height: 5px; border-radius: 3px; background: #e5e7eb; margin-top: 6px; overflow: hidden; }
.acct-meter i { display: block; height: 100%; background: #16a34a; }
.acct-meter.warn i { background: #f59e0b; }
.acct-meter.danger i { background: #dc2626; }

.acct-confirm { display: flex; gap: 8px; margin-top: 8px; }
.acct-confirm button { flex: 1; font-size: 14px; padding: 8px 10px; min-height: 40px; }
.acct-confirm .no { background: #6b7280; }
.acct-note { font-size: 12px; margin: 8px 0 0; line-height: 1.4; }
.acct-note:empty { display: none; }
.acct-note.muted { color: #888; }

@media (prefers-color-scheme: dark) {
  .acct-panel { background: #1c1c1f; color: #e8e8e8; }
  .acct-row { background: #232327; border-color: #2a2a2e; }
  .acct-row.is-active { border-color: #22c55e; background: #16241a; }
  .acct-row .r-badge { color: #22c55e; }
  .acct-row .r-mail { color: #9a9a9a; }
  .acct-row .r-usage { color: #c8c8c8; }
  /* Тёмный янтарь светлой темы на тёмном фоне почти не читается — тот же приём,
     что у легенды статусов выше. */
  .acct-row .r-warn { color: #f59e0b; }
  .acct-row .r-block { color: #f87171; }
  .acct-meter { background: #3a3a3e; }
}

/* Автопереключение аккаунта — блок под списком в шторке */
.acct-auto { margin-top: 10px; padding-top: 10px; border-top: 1px solid #e5e5e5; }
.acct-auto-row { display: flex; align-items: center; gap: 8px; font-size: 13px; line-height: 1.4; }
.acct-auto-row input[type="checkbox"] { width: 18px; height: 18px; flex: none; accent-color: #16a34a; }
.acct-auto-row input[type="number"] { width: 4.2em; padding: 2px 4px; font-size: 13px;
  border: 1px solid #ccc; border-radius: 4px; background: #fff; color: inherit; }
.acct-auto-row input[type="number"]:disabled { opacity: .5; }
@media (prefers-color-scheme: dark) {
  .acct-auto { border-top-color: #2a2a2e; }
  .acct-auto-row input[type="number"] { background: #232327; border-color: #3a3a3e; color: #e8e8e8; }
}
